]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console_conf.c
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[bacula/bacula] / bacula / src / gnome2-console / console_conf.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Main configuration file parser for Bacula User Agent
30  *    some parts may be split into separate files such as
31  *    the schedule configuration (sch_config.c).
32  *
33  *   Note, the configuration file parser consists of three parts
34  *
35  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
36  *
37  *   2. The generic config  scanner in lib/parse_config.c and
38  *      lib/parse_config.h.
39  *      These files contain the parser code, some utility
40  *      routines, and the common store routines (name, int,
41  *      string).
42  *
43  *   3. The daemon specific file, which contains the Resource
44  *      definitions as well as any specific store routines
45  *      for the resource records.
46  *
47  *     Kern Sibbald, January MM, September MM
48  *
49  *     Version $Id$
50  */
51
52 #include "bacula.h"
53 #include "console_conf.h"
54
55 /* Define the first and last resource ID record
56  * types. Note, these should be unique for each
57  * daemon though not a requirement.
58  */
59 int32_t r_first = R_FIRST;
60 int32_t r_last  = R_LAST;
61 static RES *sres_head[R_LAST - R_FIRST + 1];
62 RES **res_head = sres_head;
63
64 /* Forward referenced subroutines */
65
66
67 /* We build the current resource here as we are
68  * scanning the resource configuration definition,
69  * then move it to allocated memory when the resource
70  * scan is complete.
71  */
72 URES res_all;
73 int32_t res_all_size = sizeof(res_all);
74
75 /* Definition of records permitted within each
76  * resource with the routine to process the record
77  * information.
78  */
79 static RES_ITEM dir_items[] = {
80    {"name",        store_name,     ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
81    {"description", store_str,      ITEM(dir_res.hdr.desc), 0, 0, 0},
82    {"dirport",     store_pint32,   ITEM(dir_res.DIRport),  0, ITEM_DEFAULT, 9101},
83    {"address",     store_str,      ITEM(dir_res.address),  0, ITEM_REQUIRED, 0},
84    {"password",    store_password, ITEM(dir_res.password), 0, 0, 0},
85    {"tlsenable",   store_bool,     ITEM(dir_res.tls_enable), 1, 0, 0},
86    {"tlsrequire",  store_bool,     ITEM(dir_res.tls_require), 1, 0, 0},
87    {"tlscacertificatefile", store_dir, ITEM(dir_res.tls_ca_certfile), 0, 0, 0},
88    {"tlscacertificatedir", store_dir,  ITEM(dir_res.tls_ca_certdir), 0, 0, 0},
89    {"tlscertificate", store_dir,       ITEM(dir_res.tls_certfile), 0, 0, 0},
90    {"tlskey",         store_dir,       ITEM(dir_res.tls_keyfile), 0, 0, 0},
91    {NULL, NULL, {0}, 0, 0, 0}
92 };
93
94 static RES_ITEM con_items[] = {
95    {"name",        store_name,     ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
96    {"description", store_str,      ITEM(con_res.hdr.desc), 0, 0, 0},
97    {"password",    store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
98    {"tlsenable",      store_bool,    ITEM(con_res.tls_enable), 1, 0, 0},
99    {"tlsrequire",     store_bool,    ITEM(con_res.tls_require), 1, 0, 0},
100    {"tlscacertificatefile", store_dir, ITEM(con_res.tls_ca_certfile), 0, 0, 0},
101    {"tlscacertificatedir", store_dir,  ITEM(con_res.tls_ca_certdir), 0, 0, 0},
102    {"tlscertificate", store_dir,       ITEM(con_res.tls_certfile), 0, 0, 0},
103    {"tlskey",         store_dir,       ITEM(con_res.tls_keyfile), 0, 0, 0},
104    {NULL, NULL, {0}, 0, 0, 0}
105 };
106
107 static RES_ITEM con_font_items[] = {
108    {"name",        store_name,     ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
109    {"description", store_str,      ITEM(con_font.hdr.desc), 0, 0, 0},
110    {"font",        store_str,      ITEM(con_font.fontface), 0, 0, 0},
111    {NULL, NULL, {0}, 0, 0, 0}
112 };
113
114
115 /*
116  * This is the master resource definition.
117  * It must have one item for each of the resources.
118  */
119 RES_TABLE resources[] = {
120    {"director",      dir_items,   R_DIRECTOR},
121    {"console",       con_items,   R_CONSOLE},
122    {"consolefont",   con_font_items, R_CONSOLE_FONT},
123    {NULL,            NULL,        0}
124 };
125
126
127 /* Dump contents of resource */
128 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
129 {
130    URES *res = (URES *)reshdr;
131    bool recurse = true;
132
133    if (res == NULL) {
134       printf(_("No record for %d %s\n"), type, res_to_str(type));
135       return;
136    }
137    if (type < 0) {                    /* no recursion */
138       type = - type;
139       recurse = false;
140    }
141    switch (type) {
142    case R_DIRECTOR:
143       printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
144               res->dir_res.address, res->dir_res.DIRport);
145       break;
146    case R_CONSOLE:
147       printf(_("Console: name=%s\n"), reshdr->name);
148       break;
149    case R_CONSOLE_FONT:
150       printf(_("ConsoleFont: name=%s font face=%s\n"),
151              reshdr->name, NPRT(res->con_font.fontface));
152       break;
153    default:
154       printf(_("Unknown resource type %d\n"), type);
155    }
156    if (recurse && res->dir_res.hdr.next) {
157       dump_resource(type, res->dir_res.hdr.next, sendit, sock);
158    }
159 }
160
161 /*
162  * Free memory of resource.
163  * NB, we don't need to worry about freeing any references
164  * to other resources as they will be freed when that
165  * resource chain is traversed.  Mainly we worry about freeing
166  * allocated strings (names).
167  */
168 void free_resource(RES *sres, int type)
169 {
170    RES *nres;
171    URES *res = (URES *)sres;
172
173    if (res == NULL)
174       return;
175
176    /* common stuff -- free the resource name */
177    nres = (RES *)res->dir_res.hdr.next;
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       break;
226    case R_CONSOLE_FONT:
227       if (res->con_font.fontface) {
228          free(res->con_font.fontface);
229       }
230       break;
231    default:
232       printf(_("Unknown resource type %d\n"), type);
233    }
234    /* Common stuff again -- free the resource, recurse to next one */
235    free(res);
236    if (nres) {
237       free_resource(nres, type);
238    }
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 void save_resource(int type, RES_ITEM *items, int pass)
246 {
247    URES *res;
248    int rindex = type - r_first;
249    int i, size = 0;
250    int error = 0;
251
252    /*
253     * Ensure that all required items are present
254     */
255    for (i=0; items[i].name; i++) {
256       if (items[i].flags & ITEM_REQUIRED) {
257             if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {
258                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
259                  items[i].name, resources[rindex]);
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_DIRECTOR:
273          break;
274
275       case R_CONSOLE:
276       case R_CONSOLE_FONT:
277          break;
278
279       default:
280          Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
281          error = 1;
282          break;
283       }
284       /* Note, the resoure name was already saved during pass 1,
285        * so here, we can just release it.
286        */
287       if (res_all.dir_res.hdr.name) {
288          free(res_all.dir_res.hdr.name);
289          res_all.dir_res.hdr.name = NULL;
290       }
291       if (res_all.dir_res.hdr.desc) {
292          free(res_all.dir_res.hdr.desc);
293          res_all.dir_res.hdr.desc = NULL;
294       }
295       return;
296    }
297
298    /* The following code is only executed during pass 1 */
299    switch (type) {
300    case R_DIRECTOR:
301       size = sizeof(DIRRES);
302       break;
303    case R_CONSOLE_FONT:
304       size = sizeof(CONFONTRES);
305       break;
306    case R_CONSOLE:
307       size = sizeof(CONRES);
308       break;
309    default:
310       printf(_("Unknown resource type %d\n"), type);
311       error = 1;
312       break;
313    }
314    /* Common */
315    if (!error) {
316       res = (URES *)malloc(size);
317       memcpy(res, &res_all, size);
318       if (!res_head[rindex]) {
319          res_head[rindex] = (RES *)res; /* store first entry */
320       } else {
321          RES *next, *last;
322          /* Add new res to end of chain */
323          for (last=next=res_head[rindex]; next; next=next->next) {
324             last = next;
325             if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
326                Emsg2(M_ERROR_TERM, 0,
327                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
328                   resources[rindex].name, res->dir_res.hdr.name);
329             }
330          }
331          last->next = (RES *)res;
332          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
333                res->dir_res.hdr.name);
334       }
335    }
336 }
337
338 bool parse_gcons_config(CONFIG *config, const char *configfile, int exit_code)
339 {
340    config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
341       r_first, r_last, resources, res_head);
342    return config->parse_config();
343 }