]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
8eee61926c29ccdec02ae95111ffa73c9bf96eb0
[bacula/bacula] / bacula / src / console / console_conf.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 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 static RES *sres_head[R_LAST - R_FIRST + 1];
51 RES **res_head = sres_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" { // work around visual compiler mangling variables
63     URES res_all;
64 }
65 #else
66 URES res_all;
67 #endif
68 int32_t res_all_size = sizeof(res_all);
69
70 /* Definition of records permitted within each
71  * resource with the routine to process the record
72  * information.
73  */
74
75 /*  Console "globals" */
76 static RES_ITEM cons_items[] = {
77    {"Name",           store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
78    {"Description",    store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
79    {"RCFile",         store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
80    {"HistoryFile",    store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
81    {"Password",       store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
82    {"TlsAuthenticate",store_bool,    ITEM(res_cons.tls_authenticate), 0, 0, 0},
83    {"TlsEnable",      store_bool,    ITEM(res_cons.tls_enable), 0, 0, 0},
84    {"TlsRequire",     store_bool,    ITEM(res_cons.tls_require), 0, 0, 0},
85    {"TlsCaCertificateFile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
86    {"TlsCaCertificateDir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
87    {"TlsCertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
88    {"TlsKey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
89    {"Director",       store_str,       ITEM(res_cons.director), 0, 0, 0},
90    {"HeartbeatInterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
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 *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
127 {
128    URES *res = (URES *)reshdr;
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"), reshdr->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"), reshdr->name,
146               res->res_dir.address, res->res_dir.DIRport);
147       break;
148    default:
149       printf(_("Unknown resource type %d\n"), type);
150    }
151    if (recurse && res->res_dir.hdr.next) {
152       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
153    }
154 }
155
156 /*
157  * Free memory of resource.
158  * NB, we don't need to worry about freeing any references
159  * to other resources as they will be freed when that
160  * resource chain is traversed.  Mainly we worry about freeing
161  * allocated strings (names).
162  */
163 void free_resource(RES *sres, int type)
164 {
165    RES *nres;
166    URES *res = (URES *)sres;
167
168    if (res == NULL) {
169       return;
170    }
171
172    /* common stuff -- free the resource name */
173    nres = (RES *)res->res_dir.hdr.next;
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       break;
208    case R_DIRECTOR:
209       if (res->res_dir.address) {
210          free(res->res_dir.address);
211       }
212       if (res->res_dir.tls_ctx) {
213          free_tls_context(res->res_dir.tls_ctx);
214       }
215       if (res->res_dir.tls_ca_certfile) {
216          free(res->res_dir.tls_ca_certfile);
217       }
218       if (res->res_dir.tls_ca_certdir) {
219          free(res->res_dir.tls_ca_certdir);
220       }
221       if (res->res_dir.tls_certfile) {
222          free(res->res_dir.tls_certfile);
223       }
224       if (res->res_dir.tls_keyfile) {
225          free(res->res_dir.tls_keyfile);
226       }
227       break;
228    default:
229       printf(_("Unknown resource type %d\n"), type);
230    }
231    /* Common stuff again -- free the resource, recurse to next one */
232    free(res);
233    if (nres) {
234       free_resource(nres, type);
235    }
236 }
237
238 /* Save the new resource by chaining it into the head list for
239  * the resource. If this is pass 2, we update any resource
240  * pointers (currently only in the Job resource).
241  */
242 void save_resource(int type, RES_ITEM *items, int pass)
243 {
244    URES *res;
245    int rindex = type - r_first;
246    int i, size;
247    int error = 0;
248
249    /*
250     * Ensure that all required items are present
251     */
252    for (i=0; items[i].name; i++) {
253       if (items[i].flags & ITEM_REQUIRED) {
254             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
255                Emsg2(M_ERROR_TERM, 0, _("\"%s\" directive is required in \"%s\" resource, but not found.\n"),
256                  items[i].name, resources[rindex].name);
257              }
258       }
259    }
260
261    /* During pass 2, we looked up pointers to all the resources
262     * referrenced in the current resource, , now we
263     * must copy their address from the static record to the allocated
264     * record.
265     */
266    if (pass == 2) {
267       switch (type) {
268          /* Resources not containing a resource */
269          case R_CONSOLE:
270          case R_DIRECTOR:
271             break;
272
273          default:
274             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
275             error = 1;
276             break;
277       }
278       /* Note, the resoure name was already saved during pass 1,
279        * so here, we can just release it.
280        */
281       if (res_all.res_dir.hdr.name) {
282          free(res_all.res_dir.hdr.name);
283          res_all.res_dir.hdr.name = NULL;
284       }
285       if (res_all.res_dir.hdr.desc) {
286          free(res_all.res_dir.hdr.desc);
287          res_all.res_dir.hdr.desc = NULL;
288       }
289       return;
290    }
291
292    /* The following code is only executed during pass 1 */
293    switch (type) {
294    case R_CONSOLE:
295       size = sizeof(CONRES);
296       break;
297    case R_DIRECTOR:
298       size = sizeof(DIRRES);
299       break;
300    default:
301       printf(_("Unknown resource type %d\n"), type);
302       error = 1;
303       size = 1;
304       break;
305    }
306    /* Common */
307    if (!error) {
308       res = (URES *)malloc(size);
309       memcpy(res, &res_all, size);
310       if (!res_head[rindex]) {
311          res_head[rindex] = (RES *)res; /* store first entry */
312       } else {
313          RES *next, *last;
314          for (last=next=res_head[rindex]; next; next=next->next) {
315             last = next;
316             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
317                Emsg2(M_ERROR_TERM, 0,
318                   _("Attempt to define second \"\"%s\" resource named \"%s\" is not permitted.\n"),
319                   resources[rindex].name, res->res_dir.hdr.name);
320             }
321          }
322          last->next = (RES *)res;
323          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
324                res->res_dir.hdr.name);
325       }
326    }
327 }
328
329 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
330 {
331    config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
332       r_first, r_last, resources, res_head);
333    return config->parse_config();
334 }