]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / console / console_conf.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  *   Main configuration file parser for Bacula User Agent
22  *    some parts may be split into separate files such as
23  *    the schedule configuration (sch_config.c).
24  *
25  *   Note, the configuration file parser consists of three parts
26  *
27  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
28  *
29  *   2. The generic config  scanner in lib/parse_config.c and
30  *      lib/parse_config.h.
31  *      These files contain the parser code, some utility
32  *      routines, and the common store routines (name, int,
33  *      string).
34  *
35  *   3. The daemon specific file, which contains the Resource
36  *      definitions as well as any specific store routines
37  *      for the resource records.
38  *
39  *     Kern Sibbald, January MM, September MM
40  */
41
42 #include "bacula.h"
43 #include "console_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 static RES *sres_head[R_LAST - R_FIRST + 1];
52 RES **res_head = sres_head;
53
54 /* Forward referenced subroutines */
55
56
57 /* We build the current resource here as we are
58  * scanning the resource configuration definition,
59  * then move it to allocated memory when the resource
60  * scan is complete.
61  */
62 #if defined(_MSC_VER)
63 extern "C" { // work around visual compiler mangling variables
64     URES res_all;
65 }
66 #else
67 URES res_all;
68 #endif
69 int32_t res_all_size = sizeof(res_all);
70
71 /* Definition of records permitted within each
72  * resource with the routine to process the record
73  * information.
74  */
75
76 /*  Console "globals" */
77 static RES_ITEM cons_items[] = {
78    {"Name",           store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
79    {"Description",    store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
80    {"RCFile",         store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
81    {"HistoryFile",    store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
82    {"Password",       store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
83    {"TlsAuthenticate",store_bool,    ITEM(res_cons.tls_authenticate), 0, 0, 0},
84    {"TlsEnable",      store_bool,    ITEM(res_cons.tls_enable), 0, 0, 0},
85    {"TlsRequire",     store_bool,    ITEM(res_cons.tls_require), 0, 0, 0},
86    {"TlsCaCertificateFile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
87    {"TlsCaCertificateDir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
88    {"TlsCertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
89    {"TlsKey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
90    {"Director",       store_str,       ITEM(res_cons.director), 0, 0, 0},
91    {"HeartbeatInterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
92    {NULL, NULL, {0}, 0, 0, 0}
93 };
94
95
96 /*  Director's that we can contact */
97 static RES_ITEM dir_items[] = {
98    {"Name",           store_name,      ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
99    {"Description",    store_str,       ITEM(res_dir.hdr.desc), 0, 0, 0},
100    {"DirPort",        store_pint32,    ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
101    {"Address",        store_str,       ITEM(res_dir.address),  0, 0, 0},
102    {"Password",       store_password,  ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
103    {"TlsAuthenticate",store_bool,    ITEM(res_dir.tls_enable), 0, 0, 0},
104    {"TlsEnable",      store_bool,    ITEM(res_dir.tls_enable), 0, 0, 0},
105    {"TlsRequire",     store_bool,    ITEM(res_dir.tls_require), 0, 0, 0},
106    {"TlsCaCertificateFile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
107    {"TlsCaCertificateDir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
108    {"TlsCertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
109    {"TlsKey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
110    {"HeartbeatInterval", store_time, ITEM(res_dir.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
111    {NULL, NULL, {0}, 0, 0, 0}
112 };
113
114 /*
115  * This is the master resource definition.
116  * It must have one item for each of the resources.
117  *
118  *   name            item         rcode
119  */
120 RES_TABLE resources[] = {
121    {"Console",       cons_items,  R_CONSOLE},
122    {"Director",      dir_items,   R_DIRECTOR},
123    {NULL,            NULL,        0}
124 };
125
126 /* Dump contents of resource */
127 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
128 {
129    URES *res = (URES *)reshdr;
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_CONSOLE:
142       printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
143              res->res_cons.rc_file, res->res_cons.hist_file);
144       break;
145    case R_DIRECTOR:
146       printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
147               res->res_dir.address, res->res_dir.DIRport);
148       break;
149    default:
150       printf(_("Unknown resource type %d\n"), type);
151    }
152    if (recurse && res->res_dir.hdr.next) {
153       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
154    }
155 }
156
157 /*
158  * Free memory of resource.
159  * NB, we don't need to worry about freeing any references
160  * to other resources as they will be freed when that
161  * resource chain is traversed.  Mainly we worry about freeing
162  * allocated strings (names).
163  */
164 void free_resource(RES *sres, int type)
165 {
166    RES *nres;
167    URES *res = (URES *)sres;
168
169    if (res == NULL) {
170       return;
171    }
172
173    /* common stuff -- free the resource name */
174    nres = (RES *)res->res_dir.hdr.next;
175    if (res->res_dir.hdr.name) {
176       free(res->res_dir.hdr.name);
177    }
178    if (res->res_dir.hdr.desc) {
179       free(res->res_dir.hdr.desc);
180    }
181
182    switch (type) {
183    case R_CONSOLE:
184       if (res->res_cons.rc_file) {
185          free(res->res_cons.rc_file);
186       }
187       if (res->res_cons.hist_file) {
188          free(res->res_cons.hist_file);
189       }
190       if (res->res_cons.tls_ctx) {
191          free_tls_context(res->res_cons.tls_ctx);
192       }
193       if (res->res_cons.tls_ca_certfile) {
194          free(res->res_cons.tls_ca_certfile);
195       }
196       if (res->res_cons.tls_ca_certdir) {
197          free(res->res_cons.tls_ca_certdir);
198       }
199       if (res->res_cons.tls_certfile) {
200          free(res->res_cons.tls_certfile);
201       }
202       if (res->res_cons.tls_keyfile) {
203          free(res->res_cons.tls_keyfile);
204       }
205       if (res->res_cons.director) {
206          free(res->res_cons.director);
207       }
208       break;
209    case R_DIRECTOR:
210       if (res->res_dir.address) {
211          free(res->res_dir.address);
212       }
213       if (res->res_dir.tls_ctx) {
214          free_tls_context(res->res_dir.tls_ctx);
215       }
216       if (res->res_dir.tls_ca_certfile) {
217          free(res->res_dir.tls_ca_certfile);
218       }
219       if (res->res_dir.tls_ca_certdir) {
220          free(res->res_dir.tls_ca_certdir);
221       }
222       if (res->res_dir.tls_certfile) {
223          free(res->res_dir.tls_certfile);
224       }
225       if (res->res_dir.tls_keyfile) {
226          free(res->res_dir.tls_keyfile);
227       }
228       break;
229    default:
230       printf(_("Unknown resource type %d\n"), type);
231    }
232    /* Common stuff again -- free the resource, recurse to next one */
233    free(res);
234    if (nres) {
235       free_resource(nres, type);
236    }
237 }
238
239 /* Save the new resource by chaining it into the head list for
240  * the resource. If this is pass 2, we update any resource
241  * pointers (currently only in the Job resource).
242  */
243 void save_resource(int type, RES_ITEM *items, int pass)
244 {
245    URES *res;
246    int rindex = type - r_first;
247    int i, size;
248    int error = 0;
249
250    /*
251     * Ensure that all required items are present
252     */
253    for (i=0; items[i].name; i++) {
254       if (items[i].flags & ITEM_REQUIRED) {
255             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
256                Emsg2(M_ERROR_TERM, 0, _("%s item is required in %s resource, but not found.\n"),
257                  items[i].name, resources[rindex]);
258              }
259       }
260    }
261
262    /* During pass 2, we looked up pointers to all the resources
263     * referrenced in the current resource, , now we
264     * must copy their address from the static record to the allocated
265     * record.
266     */
267    if (pass == 2) {
268       switch (type) {
269          /* Resources not containing a resource */
270          case R_CONSOLE:
271          case R_DIRECTOR:
272             break;
273
274          default:
275             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
276             error = 1;
277             break;
278       }
279       /* Note, the resoure name was already saved during pass 1,
280        * so here, we can just release it.
281        */
282       if (res_all.res_dir.hdr.name) {
283          free(res_all.res_dir.hdr.name);
284          res_all.res_dir.hdr.name = NULL;
285       }
286       if (res_all.res_dir.hdr.desc) {
287          free(res_all.res_dir.hdr.desc);
288          res_all.res_dir.hdr.desc = NULL;
289       }
290       return;
291    }
292
293    /* The following code is only executed during pass 1 */
294    switch (type) {
295    case R_CONSOLE:
296       size = sizeof(CONRES);
297       break;
298    case R_DIRECTOR:
299       size = sizeof(DIRRES);
300       break;
301    default:
302       printf(_("Unknown resource type %d\n"), type);
303       error = 1;
304       size = 1;
305       break;
306    }
307    /* Common */
308    if (!error) {
309       res = (URES *)malloc(size);
310       memcpy(res, &res_all, size);
311       if (!res_head[rindex]) {
312          res_head[rindex] = (RES *)res; /* store first entry */
313       } else {
314          RES *next, *last;
315          for (last=next=res_head[rindex]; next; next=next->next) {
316             last = next;
317             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
318                Emsg2(M_ERROR_TERM, 0,
319                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
320                   resources[rindex].name, res->res_dir.hdr.name);
321             }
322          }
323          last->next = (RES *)res;
324          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
325                res->res_dir.hdr.name);
326       }
327    }
328 }
329
330 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
331 {
332    config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
333       r_first, r_last, resources, res_head);
334    return config->parse_config();
335 }