]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/qt-console/bat_conf.cpp
Change to Copyright (C) 2007-2009
[bacula/bacula] / bacula / src / qt-console / bat_conf.cpp
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2009 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 "bat_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 #if defined(_MSC_VER)
73 extern "C" URES res_all; /* declare as C to avoid name mangling by visual c */
74 #endif
75 URES res_all;
76 int32_t res_all_size = sizeof(res_all);
77
78 /* Definition of records permitted within each
79  * resource with the routine to process the record
80  * information.
81  */
82 static RES_ITEM dir_items[] = {
83    {"name",        store_name,     ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
84    {"description", store_str,      ITEM(dir_res.hdr.desc), 0, 0, 0},
85    {"dirport",     store_pint32,   ITEM(dir_res.DIRport),  0, ITEM_DEFAULT, 9101},
86    {"address",     store_str,      ITEM(dir_res.address),  0, ITEM_REQUIRED, 0},
87    {"password",    store_password, ITEM(dir_res.password), 0, 0, 0},
88    {"tlsauthenticate",store_bool,    ITEM(dir_res.tls_authenticate), 0, 0, 0},
89    {"tlsenable",      store_bool,    ITEM(dir_res.tls_enable), 0, 0, 0},
90    {"tlsrequire",     store_bool,    ITEM(dir_res.tls_require), 0, 0, 0},
91    {"tlscacertificatefile", store_dir, ITEM(dir_res.tls_ca_certfile), 0, 0, 0},
92    {"tlscacertificatedir", store_dir,  ITEM(dir_res.tls_ca_certdir), 0, 0, 0},
93    {"tlscertificate", store_dir,       ITEM(dir_res.tls_certfile), 0, 0, 0},
94    {"tlskey",         store_dir,       ITEM(dir_res.tls_keyfile), 0, 0, 0},
95    {"heartbeatinterval", store_time, ITEM(dir_res.heartbeat_interval), 0, ITEM_DEFAULT, 0},
96    {NULL, NULL, {0}, 0, 0, 0}
97 };
98
99 static RES_ITEM con_items[] = {
100    {"name",        store_name,     ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
101    {"description", store_str,      ITEM(con_res.hdr.desc), 0, 0, 0},
102    {"password",    store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
103    {"tlsauthenticate",store_bool,    ITEM(con_res.tls_authenticate), 0, 0, 0},
104    {"tlsenable",      store_bool,    ITEM(con_res.tls_enable), 0, 0, 0},
105    {"tlsrequire",     store_bool,    ITEM(con_res.tls_require), 0, 0, 0},
106    {"tlscacertificatefile", store_dir, ITEM(con_res.tls_ca_certfile), 0, 0, 0},
107    {"tlscacertificatedir", store_dir,  ITEM(con_res.tls_ca_certdir), 0, 0, 0},
108    {"tlscertificate", store_dir,       ITEM(con_res.tls_certfile), 0, 0, 0},
109    {"tlskey",         store_dir,       ITEM(con_res.tls_keyfile), 0, 0, 0},
110    {"heartbeatinterval", store_time, ITEM(con_res.heartbeat_interval), 0, ITEM_DEFAULT, 0},
111    {NULL, NULL, {0}, 0, 0, 0}
112 };
113
114 static RES_ITEM con_font_items[] = {
115    {"name",        store_name,     ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
116    {"description", store_str,      ITEM(con_font.hdr.desc), 0, 0, 0},
117    {"font",        store_str,      ITEM(con_font.fontface), 0, 0, 0},
118    {NULL, NULL, {0}, 0, 0, 0}
119 };
120
121
122 /*
123  * This is the master resource definition.
124  * It must have one item for each of the resources.
125  */
126 RES_TABLE resources[] = {
127    {"director",      dir_items,   R_DIRECTOR},
128    {"console",       con_items,   R_CONSOLE},
129    {"consolefont",   con_font_items, R_CONSOLE_FONT},
130    {NULL,            NULL,        0}
131 };
132
133
134 /* Dump contents of resource */
135 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
136 {
137    URES *res = (URES *)reshdr;
138    bool recurse = true;
139
140    if (res == NULL) {
141       printf(_("No record for %d %s\n"), type, res_to_str(type));
142       return;
143    }
144    if (type < 0) {                    /* no recursion */
145       type = - type;
146       recurse = false;
147    }
148    switch (type) {
149    case R_DIRECTOR:
150       printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
151               res->dir_res.address, res->dir_res.DIRport);
152       break;
153    case R_CONSOLE:
154       printf(_("Console: name=%s\n"), reshdr->name);
155       break;
156    case R_CONSOLE_FONT:
157       printf(_("ConsoleFont: name=%s font face=%s\n"),
158              reshdr->name, NPRT(res->con_font.fontface));
159       break;
160    default:
161       printf(_("Unknown resource type %d\n"), type);
162    }
163    if (recurse && res->dir_res.hdr.next) {
164       dump_resource(type, res->dir_res.hdr.next, sendit, sock);
165    }
166 }
167
168 /*
169  * Free memory of resource.
170  * NB, we don't need to worry about freeing any references
171  * to other resources as they will be freed when that
172  * resource chain is traversed.  Mainly we worry about freeing
173  * allocated strings (names).
174  */
175 void free_resource(RES *sres, int type)
176 {
177    RES *nres;
178    URES *res = (URES *)sres;
179
180    if (res == NULL)
181       return;
182
183    /* common stuff -- free the resource name */
184    nres = (RES *)res->dir_res.hdr.next;
185    if (res->dir_res.hdr.name) {
186       free(res->dir_res.hdr.name);
187    }
188    if (res->dir_res.hdr.desc) {
189       free(res->dir_res.hdr.desc);
190    }
191
192    switch (type) {
193    case R_DIRECTOR:
194       if (res->dir_res.address) {
195          free(res->dir_res.address);
196       }
197       if (res->dir_res.tls_ctx) { 
198          free_tls_context(res->dir_res.tls_ctx);
199       }
200       if (res->dir_res.tls_ca_certfile) {
201          free(res->dir_res.tls_ca_certfile);
202       }
203       if (res->dir_res.tls_ca_certdir) {
204          free(res->dir_res.tls_ca_certdir);
205       }
206       if (res->dir_res.tls_certfile) {
207          free(res->dir_res.tls_certfile);
208       }
209       if (res->dir_res.tls_keyfile) {
210          free(res->dir_res.tls_keyfile);
211       }
212       break;
213    case R_CONSOLE:
214       if (res->con_res.password) {
215          free(res->con_res.password);
216       }
217       if (res->con_res.tls_ctx) { 
218          free_tls_context(res->con_res.tls_ctx);
219       }
220       if (res->con_res.tls_ca_certfile) {
221          free(res->con_res.tls_ca_certfile);
222       }
223       if (res->con_res.tls_ca_certdir) {
224          free(res->con_res.tls_ca_certdir);
225       }
226       if (res->con_res.tls_certfile) {
227          free(res->con_res.tls_certfile);
228       }
229       if (res->con_res.tls_keyfile) {
230          free(res->con_res.tls_keyfile);
231       }
232       break;
233    case R_CONSOLE_FONT:
234       if (res->con_font.fontface) {
235          free(res->con_font.fontface);
236       }
237       break;
238    default:
239       printf(_("Unknown resource type %d\n"), type);
240    }
241    /* Common stuff again -- free the resource, recurse to next one */
242    free(res);
243    if (nres) {
244       free_resource(nres, type);
245    }
246 }
247
248 /* Save the new resource by chaining it into the head list for
249  * the resource. If this is pass 2, we update any resource
250  * pointers (currently only in the Job resource).
251  */
252 void save_resource(int type, RES_ITEM *items, int pass)
253 {
254    URES *res;
255    int rindex = type - r_first;
256    int i, size = 0;
257    int error = 0;
258
259    /*
260     * Ensure that all required items are present
261     */
262    for (i=0; items[i].name; i++) {
263       if (items[i].flags & ITEM_REQUIRED) {
264             if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {
265                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
266                  items[i].name, resources[rindex]);
267              }
268       }
269    }
270
271    /* During pass 2, we looked up pointers to all the resources
272     * referrenced in the current resource, , now we
273     * must copy their address from the static record to the allocated
274     * record.
275     */
276    if (pass == 2) {
277       switch (type) {
278       /* Resources not containing a resource */
279       case R_DIRECTOR:
280          break;
281
282       case R_CONSOLE:
283       case R_CONSOLE_FONT:
284          break;
285
286       default:
287          Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
288          error = 1;
289          break;
290       }
291       /* Note, the resoure name was already saved during pass 1,
292        * so here, we can just release it.
293        */
294       if (res_all.dir_res.hdr.name) {
295          free(res_all.dir_res.hdr.name);
296          res_all.dir_res.hdr.name = NULL;
297       }
298       if (res_all.dir_res.hdr.desc) {
299          free(res_all.dir_res.hdr.desc);
300          res_all.dir_res.hdr.desc = NULL;
301       }
302       return;
303    }
304
305    /* The following code is only executed during pass 1 */
306    switch (type) {
307    case R_DIRECTOR:
308       size = sizeof(DIRRES);
309       break;
310    case R_CONSOLE_FONT:
311       size = sizeof(CONFONTRES);
312       break;
313    case R_CONSOLE:
314       size = sizeof(CONRES);
315       break;
316    default:
317       printf(_("Unknown resource type %d\n"), type);
318       error = 1;
319       break;
320    }
321    /* Common */
322    if (!error) {
323       res = (URES *)malloc(size);
324       memcpy(res, &res_all, size);
325       if (!res_head[rindex]) {
326          res_head[rindex] = (RES *)res; /* store first entry */
327       } else {
328          RES *next, *last;
329          /* Add new res to end of chain */
330          for (last=next=res_head[rindex]; next; next=next->next) {
331             last = next;
332             if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
333                Emsg2(M_ERROR_TERM, 0,
334                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
335                   resources[rindex].name, res->dir_res.hdr.name);
336             }
337          }
338          last->next = (RES *)res;
339          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
340                res->dir_res.hdr.name);
341       }
342    }
343 }
344
345 bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code)
346 {
347    config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
348       r_first, r_last, resources, res_head);
349    return config->parse_config();
350 }