]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
0336a957cbcee88210594fbb74969bffbcc365b2
[bacula/bacula] / bacula / src / console / console_conf.c
1 /*
2  *   Main configuration file parser for Bacula User Agent
3  *    some parts may be split into separate files such as
4  *    the schedule configuration (sch_config.c).
5  *
6  *   Note, the configuration file parser consists of three parts
7  *
8  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
9  *
10  *   2. The generic config  scanner in lib/parse_config.c and
11  *      lib/parse_config.h.
12  *      These files contain the parser code, some utility
13  *      routines, and the common store routines (name, int,
14  *      string).
15  *
16  *   3. The daemon specific file, which contains the Resource
17  *      definitions as well as any specific store routines
18  *      for the resource records.
19  *
20  *     Kern Sibbald, January MM, September MM
21  */
22 /*
23    Copyright (C) 2000-2005 Kern Sibbald
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License
27    version 2 as amended with additional clauses defined in the
28    file LICENSE in the main source directory.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
33    the file LICENSE for additional details.
34
35  */
36
37 #include "bacula.h"
38 #include "console_conf.h"
39
40 /* Define the first and last resource ID record
41  * types. Note, these should be unique for each
42  * daemon though not a requirement.
43  */
44 int r_first = R_FIRST;
45 int r_last  = R_LAST;
46 static RES *sres_head[R_LAST - R_FIRST + 1];
47 RES **res_head = sres_head;
48
49 /* Forward referenced subroutines */
50
51
52 /* We build the current resource here as we are
53  * scanning the resource configuration definition,
54  * then move it to allocated memory when the resource
55  * scan is complete.
56  */
57 #if defined(_MSC_VER)
58 extern "C" { // work around visual compiler mangling variables
59     URES res_all;
60 }
61 #else
62 URES res_all;
63 #endif
64 int  res_all_size = sizeof(res_all);
65
66 /* Definition of records permitted within each
67  * resource with the routine to process the record
68  * information.
69  */
70
71 /*  Console "globals" */
72 static RES_ITEM cons_items[] = {
73    {"name",           store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
74    {"description",    store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
75    {"rcfile",         store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
76    {"historyfile",    store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
77    {"password",       store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
78    {"tlsenable",      store_bit,     ITEM(res_cons.tls_enable), 1, 0, 0},
79    {"tlsrequire",     store_bit,     ITEM(res_cons.tls_require), 1, 0, 0},
80    {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
81    {"tlscacertificatedir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
82    {"tlscertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
83    {"tlskey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
84    {NULL, NULL, {0}, 0, 0, 0}
85 };
86
87
88 /*  Director's that we can contact */
89 static RES_ITEM dir_items[] = {
90    {"name",           store_name,      ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
91    {"description",    store_str,       ITEM(res_dir.hdr.desc), 0, 0, 0},
92    {"dirport",        store_int,       ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
93    {"address",        store_str,       ITEM(res_dir.address),  0, 0, 0},
94    {"password",       store_password,  ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
95    {"tlsenable",      store_bit,     ITEM(res_dir.tls_enable), 1, 0, 0},
96    {"tlsrequire",     store_bit,     ITEM(res_dir.tls_require), 1, 0, 0},
97    {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
98    {"tlscacertificatedir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
99    {"tlscertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
100    {"tlskey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
101    {NULL, NULL, {0}, 0, 0, 0}
102 };
103
104 /*
105  * This is the master resource definition.
106  * It must have one item for each of the resources.
107  */
108 RES_TABLE resources[] = {
109    {"console",       cons_items,  R_CONSOLE},
110    {"director",      dir_items,   R_DIRECTOR},
111    {NULL,            NULL,        0}
112 };
113
114
115 /* Dump contents of resource */
116 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
117 {
118    URES *res = (URES *)reshdr;
119    bool recurse = true;
120
121    if (res == NULL) {
122       printf(_("No record for %d %s\n"), type, res_to_str(type));
123       return;
124    }
125    if (type < 0) {                    /* no recursion */
126       type = - type;
127       recurse = false;
128    }
129    switch (type) {
130    case R_CONSOLE:
131       printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
132              res->res_cons.rc_file, res->res_cons.hist_file);
133       break;
134    case R_DIRECTOR:
135       printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
136               res->res_dir.address, res->res_dir.DIRport);
137       break;
138    default:
139       printf(_("Unknown resource type %d\n"), type);
140    }
141    if (recurse && res->res_dir.hdr.next) {
142       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
143    }
144 }
145
146 /*
147  * Free memory of resource.
148  * NB, we don't need to worry about freeing any references
149  * to other resources as they will be freed when that
150  * resource chain is traversed.  Mainly we worry about freeing
151  * allocated strings (names).
152  */
153 void free_resource(RES *sres, int type)
154 {
155    RES *nres;
156    URES *res = (URES *)sres;
157
158    if (res == NULL)
159       return;
160
161    /* common stuff -- free the resource name */
162    nres = (RES *)res->res_dir.hdr.next;
163    if (res->res_dir.hdr.name) {
164       free(res->res_dir.hdr.name);
165    }
166    if (res->res_dir.hdr.desc) {
167       free(res->res_dir.hdr.desc);
168    }
169
170    switch (type) {
171    case R_CONSOLE:
172       if (res->res_cons.rc_file) {
173          free(res->res_cons.rc_file);
174       }
175       if (res->res_cons.hist_file) {
176          free(res->res_cons.hist_file);
177       }
178       if (res->res_cons.tls_ctx) { 
179          free_tls_context(res->res_cons.tls_ctx);
180       }
181       if (res->res_cons.tls_ca_certfile) {
182          free(res->res_cons.tls_ca_certfile);
183       }
184       if (res->res_cons.tls_ca_certdir) {
185          free(res->res_cons.tls_ca_certdir);
186       }
187       if (res->res_cons.tls_certfile) {
188          free(res->res_cons.tls_certfile);
189       }
190       if (res->res_cons.tls_keyfile) {
191          free(res->res_cons.tls_keyfile);
192       }
193       break;
194    case R_DIRECTOR:
195       if (res->res_dir.address) {
196          free(res->res_dir.address);
197       }
198       if (res->res_dir.tls_ctx) { 
199          free_tls_context(res->res_dir.tls_ctx);
200       }
201       if (res->res_dir.tls_ca_certfile) {
202          free(res->res_dir.tls_ca_certfile);
203       }
204       if (res->res_dir.tls_ca_certdir) {
205          free(res->res_dir.tls_ca_certdir);
206       }
207       if (res->res_dir.tls_certfile) {
208          free(res->res_dir.tls_certfile);
209       }
210       if (res->res_dir.tls_keyfile) {
211          free(res->res_dir.tls_keyfile);
212       }
213       break;
214    default:
215       printf(_("Unknown resource type %d\n"), type);
216    }
217    /* Common stuff again -- free the resource, recurse to next one */
218    free(res);
219    if (nres) {
220       free_resource(nres, type);
221    }
222 }
223
224 /* Save the new resource by chaining it into the head list for
225  * the resource. If this is pass 2, we update any resource
226  * pointers (currently only in the Job resource).
227  */
228 void save_resource(int type, RES_ITEM *items, int pass)
229 {
230    URES *res;
231    int rindex = type - r_first;
232    int i, size;
233    int error = 0;
234
235    /*
236     * Ensure that all required items are present
237     */
238    for (i=0; items[i].name; i++) {
239       if (items[i].flags & ITEM_REQUIRED) {
240             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
241                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
242                  items[i].name, resources[rindex]);
243              }
244       }
245    }
246
247    /* During pass 2, we looked up pointers to all the resources
248     * referrenced in the current resource, , now we
249     * must copy their address from the static record to the allocated
250     * record.
251     */
252    if (pass == 2) {
253       switch (type) {
254          /* Resources not containing a resource */
255          case R_CONSOLE:
256          case R_DIRECTOR:
257             break;
258
259          default:
260             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
261             error = 1;
262             break;
263       }
264       /* Note, the resoure name was already saved during pass 1,
265        * so here, we can just release it.
266        */
267       if (res_all.res_dir.hdr.name) {
268          free(res_all.res_dir.hdr.name);
269          res_all.res_dir.hdr.name = NULL;
270       }
271       if (res_all.res_dir.hdr.desc) {
272          free(res_all.res_dir.hdr.desc);
273          res_all.res_dir.hdr.desc = NULL;
274       }
275       return;
276    }
277
278    /* The following code is only executed during pass 1 */
279    switch (type) {
280    case R_CONSOLE:
281       size = sizeof(CONRES);
282       break;
283    case R_DIRECTOR:
284       size = sizeof(DIRRES);
285       break;
286    default:
287       printf(_("Unknown resource type %d\n"), type);
288       error = 1;
289       size = 1;
290       break;
291    }
292    /* Common */
293    if (!error) {
294       res = (URES *)malloc(size);
295       memcpy(res, &res_all, size);
296       if (!res_head[rindex]) {
297          res_head[rindex] = (RES *)res; /* store first entry */
298       } else {
299          RES *next;
300          for (next=res_head[rindex]; next->next; next=next->next) {
301             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
302                Emsg2(M_ERROR_TERM, 0,
303                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
304                   resources[rindex].name, res->res_dir.hdr.name);
305             }
306          }
307          next->next = (RES *)res;
308          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
309                res->res_dir.hdr.name);
310       }
311    }
312 }