]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.h
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / parse_conf.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *     Kern Sibbald, January MM
22  *
23  */
24
25 /* Used for certain keyword tables */
26 struct s_kw {
27    const char *name;
28    int token;
29 };
30
31 struct RES_ITEM;                   /* Declare forward referenced structure */
32 struct RES_ITEM1;
33 struct RES_ITEM2;                  /* Declare forward referenced structure */
34 class RES;                         /* Declare forward referenced structure */
35 struct HPKT;                       /* Declare forward referenced structure */
36 typedef void (RES_HANDLER)(HPKT &hpkt);
37 typedef void (MSG_RES_HANDLER)(LEX *lc, RES_ITEM *item, int index, int pass);
38 /* The INC_RES handler has an extra argument */
39 typedef void (INC_RES_HANDLER)(LEX *lc, RES_ITEM2 *item, int index, int pass, bool exclude);
40
41 /* This is the structure that defines
42  * the record types (items) permitted within each
43  * resource. It is used to define the configuration
44  * tables.
45  */
46 struct RES_ITEM {
47    const char *name;                  /* Resource name i.e. Director, ... */
48    MSG_RES_HANDLER *handler;          /* Routine storing the resource item */
49    union {
50       char **value;                   /* Where to store the item */
51       char **charvalue;
52       uint32_t ui32value;
53       int32_t i32value;
54       uint64_t ui64value;
55       int64_t i64value;
56       bool boolvalue;
57       utime_t utimevalue;
58       RES *resvalue;
59       RES **presvalue;
60    };
61    int32_t  code;                     /* item code/additional info */
62    uint32_t  flags;                   /* flags: default, required, ... */
63    int32_t  default_value;            /* default value */
64 };
65
66 /*
67  * This handler takes only the RPKT as an argument
68  */
69 struct RES_ITEM1 {
70    const char *name;                  /* Resource name i.e. Director, ... */
71    RES_HANDLER *handler;              /* Routine storing/displaying the resource */
72    union {
73       char **value;                   /* Where to store the item */
74       char **charvalue;
75       uint32_t ui32value;
76       int32_t i32value;
77       uint64_t ui64value;
78       int64_t i64value;
79       bool boolvalue;
80       utime_t utimevalue;
81       RES *resvalue;
82       RES **presvalue;
83    };
84    int32_t  code;                     /* item code/additional info */
85    uint32_t  flags;                   /* flags: default, required, ... */
86    int32_t  default_value;            /* default value */
87 };
88
89 /* INC_RES_HANDLER has exclude argument */
90 struct RES_ITEM2 {
91    const char *name;                  /* Resource name i.e. Director, ... */
92    INC_RES_HANDLER *handler;          /* Routine storing the resource item */
93    union {
94       char **value;                   /* Where to store the item */
95       char **charvalue;
96       uint32_t ui32value;
97       int32_t i32value;
98       uint64_t ui64value;
99       int64_t i64value;
100       bool boolvalue;
101       utime_t utimevalue;
102       RES *resvalue;
103       RES **presvalue;
104    };
105    int32_t  code;                     /* item code/additional info */
106    uint32_t  flags;                   /* flags: default, required, ... */
107    int32_t  default_value;            /* default value */
108 };
109
110
111 /* For storing name_addr items in res_items table */
112 #define ITEM(x) {(char **)&res_all.x}
113
114 #define MAX_RES_ITEMS 100             /* maximum resource items per RES */
115
116 class RES_HEAD {
117 public:
118    rblist *res_list;                  /* Resource list */
119    RES *first;                        /* First RES item in list */
120    RES *last;                         /* Last RES item inserted */
121 };
122
123 /*
124  * This is the universal header that is
125  * at the beginning of every resource
126  * record.
127  */
128 class RES {
129 public:
130    rblink link;                       /* red-black link */
131    RES *res_next;                     /* pointer to next resource of this type */
132    char *name;                        /* resource name */
133    char *desc;                        /* resource description */
134    uint32_t rcode;                    /* resource id or type */
135    int32_t  refcnt;                   /* reference count for releasing */
136    char  item_present[MAX_RES_ITEMS]; /* set if item is present in conf file */
137 };
138
139
140 /*
141  * Master Resource configuration structure definition
142  * This is the structure that defines the
143  * resources that are available to this daemon.
144  */
145 struct RES_TABLE {
146    const char *name;                  /* resource name */
147    RES_ITEM *items;                   /* list of resource keywords */
148    uint32_t rcode;                    /* code if needed */
149 };
150
151 /* Common Resource definitions */
152
153 #define MAX_RES_NAME_LENGTH MAX_NAME_LENGTH-1       /* maximum resource name length */
154
155 /* Permitted bits in Flags field */
156 #define ITEM_REQUIRED    (1<<0)       /* item required */
157 #define ITEM_DEFAULT     (1<<1)       /* default supplied */
158 #define ITEM_NO_EQUALS   (1<<2)       /* Don't scan = after name */
159 #define ITEM_LAST        (1<<3)       /* Last item in list */
160 #define ITEM_ALLOW_DUPS  (1<<4)       /* Allow duplicate directives */
161
162 /* Message Resource */
163 class MSGS {
164 public:
165    RES   hdr;
166    char *mail_cmd;                    /* mail command */
167    char *operator_cmd;                /* Operator command */
168    DEST *dest_chain;                  /* chain of destinations */
169    char send_msg[nbytes_for_bits(M_MAX+1)];  /* bit array of types */
170
171 private:
172    bool m_in_use;                     /* set when using to send a message */
173    bool m_closing;                    /* set when closing message resource */
174
175 public:
176    /* Methods */
177    char *name() const;
178    void clear_in_use() { lock(); m_in_use=false; unlock(); }
179    void set_in_use() { wait_not_in_use(); m_in_use=true; unlock(); }
180    void set_closing() { m_closing=true; }
181    bool get_closing() { return m_closing; }
182    void clear_closing() { lock(); m_closing=false; unlock(); }
183    bool is_closing() { lock(); bool rtn=m_closing; unlock(); return rtn; }
184
185    void wait_not_in_use();            /* in message.c */
186    void lock();                       /* in message.c */
187    void unlock();                     /* in message.c */
188 };
189
190 inline char *MSGS::name() const { return hdr.name; }
191
192 /*
193  * New C++ configuration routines
194  */
195
196 class CONFIG: public SMARTALLOC {
197 public:
198    const char *m_cf;                   /* config file */
199    LEX_ERROR_HANDLER *m_scan_error;    /* error handler if non-null */
200    int32_t m_err_type;                 /* the way to terminate on failure */
201    void *m_res_all;                    /* pointer to res_all buffer */
202    int32_t m_res_all_size;             /* length of buffer */
203    bool  m_encode_pass;                /* Encode passwords with MD5 or not */
204
205    /* The below are not yet implemented */
206    int32_t m_r_first;                  /* first daemon resource type */
207    int32_t m_r_last;                   /* last daemon resource type */
208    RES_TABLE *m_resources;             /* pointer to table of permitted resources */
209    RES_HEAD **m_res_head;              /* pointer to list of resources this type */
210    brwlock_t m_res_lock;               /* resource lock */
211    POOLMEM *m_errmsg;
212
213    /* functions */
214    void init(
215       const char *cf,
216       LEX_ERROR_HANDLER *scan_error,
217       int32_t err_type,
218       void *vres_all,
219       int32_t res_all_size,
220       int32_t r_first,
221       int32_t r_last,
222       RES_TABLE *resources,
223       RES_HEAD ***res_head);
224
225    CONFIG();
226    ~CONFIG();
227    void encode_password(bool encode);
228    bool parse_config();
229    void free_all_resources();
230    bool insert_res(int rindex, int size);
231    RES_HEAD **save_resources();
232    RES_HEAD **new_res_head();
233    void init_res_head(RES_HEAD ***rhead, int32_t first, int32_t last);
234 };
235
236 /* Resource routines */
237 int res_compare(void *item1, void *item2);
238 RES *GetResWithName(int rcode, const char *name);
239 RES *GetNextRes(int rcode, RES *res);
240 RES *GetNextRes(RES_HEAD **rhead, int rcode, RES *res);
241 void b_LockRes(const char *file, int line);
242 void b_UnlockRes(const char *file, int line);
243 void dump_resource(int type, RES *res, void sendmsg(void *sock, const char *fmt, ...), void *sock);
244 void dump_each_resource(int type, void sendmsg(void *sock, const char *fmt, ...), void *sock);
245 void free_resource(RES *res, int type);
246 bool init_resource(CONFIG *config, uint32_t type, void *res);
247 bool save_resource(CONFIG *config, int type, RES_ITEM *item, int pass);
248 void unstrip_password(RES_TABLE *resources); /* Used for json stuff */
249 void strip_password(RES_TABLE *resources);   /* Used for tray monitor */
250 const char *res_to_str(int rcode);
251 bool find_config_file(const char *config_file, char *full_path, int max_path);
252
253 /* Loop through each resource of type, returning in var */
254 #ifdef HAVE_TYPEOF
255 #define foreach_res(var, type) \
256         for((var)=NULL; ((var)=(typeof(var))GetNextRes((type), (RES *)var));)
257 #else
258 #define foreach_res(var, type) \
259     for(var=NULL; (*((void **)&(var))=(void *)GetNextRes((type), (RES *)var));)
260 #endif
261
262
263 /*
264  * Standard global parsers defined in parse_config.c
265  */
266 void store_str(LEX *lc, RES_ITEM *item, int index, int pass);
267 void store_dir(LEX *lc, RES_ITEM *item, int index, int pass);
268 void store_clear_password(LEX *lc, RES_ITEM *item, int index, int pass);
269 void store_password(LEX *lc, RES_ITEM *item, int index, int pass);
270 void store_name(LEX *lc, RES_ITEM *item, int index, int pass);
271 void store_strname(LEX *lc, RES_ITEM *item, int index, int pass);
272 void store_res(LEX *lc, RES_ITEM *item, int index, int pass);
273 void store_alist_res(LEX *lc, RES_ITEM *item, int index, int pass);
274 void store_alist_str(LEX *lc, RES_ITEM *item, int index, int pass);
275 void store_int32(LEX *lc, RES_ITEM *item, int index, int pass);
276 void store_pint32(LEX *lc, RES_ITEM *item, int index, int pass);
277 void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass);
278 void store_int64(LEX *lc, RES_ITEM *item, int index, int pass);
279 void store_bit(LEX *lc, RES_ITEM *item, int index, int pass);
280 void store_bool(LEX *lc, RES_ITEM *item, int index, int pass);
281 void store_time(LEX *lc, RES_ITEM *item, int index, int pass);
282 void store_size64(LEX *lc, RES_ITEM *item, int index, int pass);
283 void store_size32(LEX *lc, RES_ITEM *item, int index, int pass);
284 void store_speed(LEX *lc, RES_ITEM *item, int index, int pass);
285 void store_defs(LEX *lc, RES_ITEM *item, int index, int pass);
286 void store_label(LEX *lc, RES_ITEM *item, int index, int pass);
287
288 /* ***FIXME*** eliminate these globals */
289 extern int32_t r_first;
290 extern int32_t r_last;
291 extern RES_TABLE resources[];
292 extern RES_HEAD **res_head;
293 extern int32_t res_all_size;