]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/bat_conf.cpp
Big backport from Enterprise
[bacula/bacula] / bacula / src / qt-console / bat_conf.cpp
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  *   Main configuration file parser for Bacula User Agent
21  *    some parts may be split into separate files such as
22  *    the schedule configuration (sch_config.c).
23  *
24  *   Note, the configuration file parser consists of three parts
25  *
26  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
27  *
28  *   2. The generic config  scanner in lib/parse_config.c and
29  *      lib/parse_config.h.
30  *      These files contain the parser code, some utility
31  *      routines, and the common store routines (name, int,
32  *      string).
33  *
34  *   3. The daemon specific file, which contains the Resource
35  *      definitions as well as any specific store routines
36  *      for the resource records.
37  *
38  *     Kern Sibbald, January MM, September MM
39  *
40  */
41
42 #include "bacula.h"
43 #include "bat_conf.h"
44
45 /* Define the first and last resource ID record
46  * types. Note, these should be unique for each
47  * daemon though not a requirement.
48  */
49 int32_t r_first = R_FIRST;
50 int32_t r_last  = R_LAST;
51 RES_HEAD **res_head;
52
53 /* Forward referenced subroutines */
54
55
56 /* We build the current resource here as we are
57  * scanning the resource configuration definition,
58  * then move it to allocated memory when the resource
59  * scan is complete.
60  */
61 #if defined(_MSC_VER)
62 extern "C" URES res_all; /* declare as C to avoid name mangling by visual c */
63 #endif
64 URES res_all;
65 int32_t res_all_size = sizeof(res_all);
66
67 /* Definition of records permitted within each
68  * resource with the routine to process the record
69  * information.
70  */
71 static RES_ITEM dir_items[] = {
72    {"name",        store_name,     ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
73    {"description", store_str,      ITEM(dir_res.hdr.desc), 0, 0, 0},
74    {"dirport",     store_pint32,   ITEM(dir_res.DIRport),  0, ITEM_DEFAULT, 9101},
75    {"address",     store_str,      ITEM(dir_res.address),  0, ITEM_REQUIRED, 0},
76    {"password",    store_password, ITEM(dir_res.password), 0, 0, 0},
77    {"tlsauthenticate",store_bool,    ITEM(dir_res.tls_authenticate), 0, 0, 0},
78    {"tlsenable",      store_bool,    ITEM(dir_res.tls_enable), 0, 0, 0},
79    {"tlsrequire",     store_bool,    ITEM(dir_res.tls_require), 0, 0, 0},
80    {"tlscacertificatefile", store_dir, ITEM(dir_res.tls_ca_certfile), 0, 0, 0},
81    {"tlscacertificatedir", store_dir,  ITEM(dir_res.tls_ca_certdir), 0, 0, 0},
82    {"tlscertificate", store_dir,       ITEM(dir_res.tls_certfile), 0, 0, 0},
83    {"tlskey",         store_dir,       ITEM(dir_res.tls_keyfile), 0, 0, 0},
84    {"heartbeatinterval", store_time, ITEM(dir_res.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
85    {NULL, NULL, {0}, 0, 0, 0}
86 };
87
88 static RES_ITEM con_items[] = {
89    {"name",        store_name,     ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
90    {"description", store_str,      ITEM(con_res.hdr.desc), 0, 0, 0},
91    {"password",    store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
92    {"tlsauthenticate",store_bool,    ITEM(con_res.tls_authenticate), 0, 0, 0},
93    {"tlsenable",      store_bool,    ITEM(con_res.tls_enable), 0, 0, 0},
94    {"tlsrequire",     store_bool,    ITEM(con_res.tls_require), 0, 0, 0},
95    {"tlscacertificatefile", store_dir, ITEM(con_res.tls_ca_certfile), 0, 0, 0},
96    {"tlscacertificatedir", store_dir,  ITEM(con_res.tls_ca_certdir), 0, 0, 0},
97    {"tlscertificate", store_dir,       ITEM(con_res.tls_certfile), 0, 0, 0},
98    {"tlskey",         store_dir,       ITEM(con_res.tls_keyfile), 0, 0, 0},
99    {"heartbeatinterval", store_time, ITEM(con_res.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
100    {"director",       store_str,       ITEM(con_res.director), 0, 0, 0},
101    {"CommCompression",   store_bool, ITEM(con_res.comm_compression), 0, ITEM_DEFAULT, true},
102    {NULL, NULL, {0}, 0, 0, 0}
103 };
104
105 static RES_ITEM con_font_items[] = {
106    {"name",        store_name,     ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
107    {"description", store_str,      ITEM(con_font.hdr.desc), 0, 0, 0},
108    {"font",        store_str,      ITEM(con_font.fontface), 0, 0, 0},
109    {NULL, NULL, {0}, 0, 0, 0}
110 };
111
112
113 /*
114  * This is the master resource definition.
115  * It must have one item for each of the resources.
116  */
117 RES_TABLE resources[] = {
118    {"director",      dir_items,   R_DIRECTOR},
119    {"console",       con_items,   R_CONSOLE},
120    {"consolefont",   con_font_items, R_CONSOLE_FONT},
121    {NULL,            NULL,        0}
122 };
123
124
125 /* Dump contents of resource */
126 void dump_resource(int type, RES *ares, void sendit(void *sock, const char *fmt, ...), void *sock)
127 {
128    RES *next;
129    URES *res = (URES *)ares;
130    bool recurse = true;
131
132    if (res == NULL) {
133       printf(_("No record for %d %s\n"), type, res_to_str(type));
134       return;
135    }
136    if (type < 0) {                    /* no recursion */
137       type = - type;
138       recurse = false;
139    }
140    switch (type) {
141    case R_DIRECTOR:
142       printf(_("Director: name=%s address=%s DIRport=%d\n"), ares->name,
143               res->dir_res.address, res->dir_res.DIRport);
144       break;
145    case R_CONSOLE:
146       printf(_("Console: name=%s\n"), ares->name);
147       break;
148    case R_CONSOLE_FONT:
149       printf(_("ConsoleFont: name=%s font face=%s\n"),
150              ares->name, NPRT(res->con_font.fontface));
151       break;
152    default:
153       printf(_("Unknown resource type %d\n"), type);
154    }
155    if (recurse) {
156       next = GetNextRes(0, (RES *)res);
157       if (next) {
158          dump_resource(type, next, sendit, sock);
159       }
160    }
161 }
162
163 /*
164  * Free memory of resource.
165  * NB, we don't need to worry about freeing any references
166  * to other resources as they will be freed when that
167  * resource chain is traversed.  Mainly we worry about freeing
168  * allocated strings (names).
169  */
170 void free_resource(RES *sres, int type)
171 {
172    URES *res = (URES *)sres;
173
174    if (res == NULL)
175       return;
176
177    /* common stuff -- free the resource name */
178    if (res->dir_res.hdr.name) {
179       free(res->dir_res.hdr.name);
180    }
181    if (res->dir_res.hdr.desc) {
182       free(res->dir_res.hdr.desc);
183    }
184
185    switch (type) {
186    case R_DIRECTOR:
187       if (res->dir_res.address) {
188          free(res->dir_res.address);
189       }
190       if (res->dir_res.tls_ctx) { 
191          free_tls_context(res->dir_res.tls_ctx);
192       }
193       if (res->dir_res.tls_ca_certfile) {
194          free(res->dir_res.tls_ca_certfile);
195       }
196       if (res->dir_res.tls_ca_certdir) {
197          free(res->dir_res.tls_ca_certdir);
198       }
199       if (res->dir_res.tls_certfile) {
200          free(res->dir_res.tls_certfile);
201       }
202       if (res->dir_res.tls_keyfile) {
203          free(res->dir_res.tls_keyfile);
204       }
205       break;
206    case R_CONSOLE:
207       if (res->con_res.password) {
208          free(res->con_res.password);
209       }
210       if (res->con_res.tls_ctx) { 
211          free_tls_context(res->con_res.tls_ctx);
212       }
213       if (res->con_res.tls_ca_certfile) {
214          free(res->con_res.tls_ca_certfile);
215       }
216       if (res->con_res.tls_ca_certdir) {
217          free(res->con_res.tls_ca_certdir);
218       }
219       if (res->con_res.tls_certfile) {
220          free(res->con_res.tls_certfile);
221       }
222       if (res->con_res.tls_keyfile) {
223          free(res->con_res.tls_keyfile);
224       }
225       if (res->con_res.director) {
226          free(res->con_res.director);
227       }
228       break;
229    case R_CONSOLE_FONT:
230       if (res->con_font.fontface) {
231          free(res->con_font.fontface);
232       }
233       break;
234    default:
235       printf(_("Unknown resource type %d\n"), type);
236    }
237    /* Common stuff again -- free the resource, recurse to next one */
238    if (res) {
239       free(res);
240    }
241 }
242
243 /* Save the new resource by chaining it into the head list for
244  * the resource. If this is pass 2, we update any resource
245  * pointers (currently only in the Job resource).
246  */
247 bool save_resource(CONFIG *config, int type, RES_ITEM *items, int pass)
248 {
249    int rindex = type - r_first;
250    int i, size = 0;
251    int error = 0;
252
253    /*
254     * Ensure that all required items are present
255     */
256    for (i=0; items[i].name; i++) {
257       if (items[i].flags & ITEM_REQUIRED) {
258          if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {
259             Mmsg(config->m_errmsg, _("\"%s\" directive is required in \"%s\" resource, but not found.\n"),
260                  items[i].name, resources[rindex].name);
261             return false;
262          }
263       }
264    }
265
266    /* During pass 2, we looked up pointers to all the resources
267     * referrenced in the current resource, , now we
268     * must copy their address from the static record to the allocated
269     * record.
270     */
271    if (pass == 2) {
272       switch (type) {
273       /* Resources not containing a resource */
274       case R_DIRECTOR:
275          break;
276
277       case R_CONSOLE:
278       case R_CONSOLE_FONT:
279          break;
280
281       default:
282          Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
283          error = 1;
284          break;
285       }
286       /* Note, the resoure name was already saved during pass 1,
287        * so here, we can just release it.
288        */
289       if (res_all.dir_res.hdr.name) {
290          free(res_all.dir_res.hdr.name);
291          res_all.dir_res.hdr.name = NULL;
292       }
293       if (res_all.dir_res.hdr.desc) {
294          free(res_all.dir_res.hdr.desc);
295          res_all.dir_res.hdr.desc = NULL;
296       }
297       return true;
298    }
299
300    /* The following code is only executed during pass 1 */
301    switch (type) {
302    case R_DIRECTOR:
303       size = sizeof(DIRRES);
304       break;
305    case R_CONSOLE_FONT:
306       size = sizeof(CONFONTRES);
307       break;
308    case R_CONSOLE:
309       size = sizeof(CONRES);
310       break;
311    default:
312       printf(_("Unknown resource type %d\n"), type);
313       error = 1;
314       break;
315    }
316    /* Common */
317    if (!error) {
318       if (!config->insert_res(rindex, size)) {
319          return false;
320       }
321    }
322    return true;
323 }
324
325 bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code)
326 {
327    config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
328       r_first, r_last, resources, &res_head);
329    return config->parse_config();
330 }