Branch data Line data Source code
1 : : // $Id: File.h 6888 2009-08-20 18:23:11Z vern $
2 : : //
3 : : // See the file "COPYING" in the main distribution directory for copyright.
4 : :
5 : : #ifndef file_h
6 : : #define file_h
7 : :
8 : : #include <fcntl.h>
9 : : #include "util.h"
10 : : #include "Obj.h"
11 : : #include "Attr.h"
12 : :
13 : : # ifdef NEED_KRB5_H
14 : : # include <krb5.h>
15 : : # endif // NEED_KRB5_H
16 : : extern "C" {
17 : : # include "openssl/evp.h"
18 : : # include "openssl/pem.h"
19 : : # include "openssl/err.h"
20 : : }
21 : :
22 : : class BroType;
23 : : class RotateTimer;
24 : :
25 : : class BroFile : public BroObj {
26 : : public:
27 : : BroFile(FILE* arg_f);
28 : : BroFile(FILE* arg_f, const char* filename, const char* access);
29 : : BroFile(const char* filename, const char* access, BroType* arg_t = 0);
30 : : virtual ~BroFile();
31 : :
32 : : const char* Name() const;
33 : :
34 : : // Returns false if an error occured.
35 : : int Write(const char* data, int len = 0);
36 : :
37 : 1379 : void Flush() { fflush(f); }
38 : :
39 : : FILE* Seek(long position); // seek to absolute position
40 : :
41 : : void SetBuf(bool buffered); // false=line buffered, true=fully buffered
42 : :
43 : 54 : BroType* FType() const { return t; }
44 : :
45 : : // Whether the file is open in a general sense; it might
46 : : // not be open as a Unix file due to our management of
47 : : // a finite number of FDs.
48 : 4965 : int IsOpen() const { return is_open; }
49 : :
50 : : // Returns true if the close made sense, false if it was already
51 : : // closed, not active, or whatever.
52 : : int Close();
53 : :
54 : : void Describe(ODesc* d) const;
55 : :
56 : : void SetRotateInterval(double secs);
57 : :
58 : : // Rotates the logfile. Returns rotate_info.
59 : : RecordVal* Rotate();
60 : :
61 : : // Set &rotate_interval, &rotate_size, &postprocessor,
62 : : // &disable_print_hook, and &raw_output attributes.
63 : : void SetAttrs(Attributes* attrs);
64 : :
65 : : // Returns the current size of the file, after fresh stat'ing.
66 : 0 : double Size() { fflush(f); UpdateFileSize(); return current_size; }
67 : :
68 : : // Set rotate/postprocessor for all files that don't define them
69 : : // by their own. (interval/max_size=0 for no rotation; size in bytes).
70 : : static void SetDefaultRotation(double interval, double max_size);
71 : :
72 : : // Close all files which are managed by us.
73 : : static void CloseCachedFiles();
74 : :
75 : : // Get the file with the given name, opening it if it doesn't yet exist.
76 : : static BroFile* GetFile(const char* name);
77 : :
78 : 0 : void DisablePrintHook() { print_hook = false; }
79 : 0 : bool IsPrintHookEnabled() const { return print_hook; }
80 : :
81 : 0 : void EnableRawOutput() { raw_output = true; }
82 : 4965 : bool IsRawOutput() const { return raw_output; }
83 : :
84 : : bool Serialize(SerialInfo* info) const;
85 : : static BroFile* Unserialize(UnserialInfo* info);
86 : :
87 : : protected:
88 : : friend class RotateTimer;
89 : :
90 : 0 : BroFile() { Init(); }
91 : : void Init();
92 : : bool Open(FILE* f = 0); // if file is given, it's an open file to use
93 : :
94 : 2 : BroFile* Prev() { return prev; }
95 : 14 : BroFile* Next() { return next; }
96 : 101 : void SetPrev(BroFile* f) { prev = f; }
97 : 48 : void SetNext(BroFile* f) { next = f; }
98 : :
99 : : void Suspend();
100 : : void PurgeCache();
101 : : void Unlink();
102 : : void InsertAtBeginning();
103 : : void MoveToBeginning();
104 : : void InstallRotateTimer();
105 : :
106 : : // Returns nil if the file is not active, was in error, etc.
107 : : // (Protected because we do not want anyone to write directly
108 : : // to the file.)
109 : : FILE* File();
110 : : FILE* BringIntoCache();
111 : :
112 : : // Stats the file to get its current size.
113 : : void UpdateFileSize();
114 : :
115 : : // Raises a file_opened event.
116 : : void RaiseOpenEvent();
117 : :
118 : : // Initialize encryption with the given public key.
119 : : void InitEncrypt(const char* keyfile);
120 : : // Finalize encryption.
121 : : void FinishEncrypt();
122 : :
123 : 0 : DECLARE_SERIAL(BroFile);
124 : :
125 : : FILE* f;
126 : : BroType* t;
127 : : char* name;
128 : : char* access;
129 : : int is_in_cache; // whether it's currently in the open-file cache
130 : : int is_open; // whether the file is open in a general sense
131 : : int okay_to_manage; // we're allowed to cache/uncache
132 : : long position; // only valid if ! is_in_cache
133 : : BroFile* next; // doubly-linked list of cached files
134 : : BroFile* prev;
135 : : Attributes* attrs;
136 : : double rotate_interval;
137 : : bool buffered;
138 : :
139 : : // Sizes are double's so that it's easy to specify large
140 : : // ones with scientific notation, and so they can exceed 4GB.
141 : : double rotate_size;
142 : : double current_size;
143 : :
144 : : Timer* rotate_timer;
145 : : double open_time;
146 : : bool dont_rotate; // See InstallRotateTimer()
147 : : bool print_hook;
148 : : bool raw_output;
149 : :
150 : : static double default_rotation_interval;
151 : : static double default_rotation_size;
152 : :
153 : : EVP_PKEY* pub_key;
154 : : EVP_CIPHER_CTX* cipher_ctx;
155 : :
156 : : static const int MIN_BUFFER_SIZE = 1024;
157 : : unsigned char* cipher_buffer;
158 : :
159 : : };
160 : :
161 : : #endif
|