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