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