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