]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_conf.c
kes Simplify locking in the reservations system.
[bacula/bacula] / bacula / src / wx-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    Bacula® - The Network Backup Solution
24
25    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
26
27    The main author of Bacula is Kern Sibbald, with contributions from
28    many others, a complete list can be found in the file AUTHORS.
29    This program is Free Software; you can redistribute it and/or
30    modify it under the terms of version two of the GNU General Public
31    License as published by the Free Software Foundation plus additions
32    that are listed in the file LICENSE.
33
34    This program is distributed in the hope that it will be useful, but
35    WITHOUT ANY WARRANTY; without even the implied warranty of
36    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37    General Public License for more details.
38
39    You should have received a copy of the GNU General Public License
40    along with this program; if not, write to the Free Software
41    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
42    02110-1301, USA.
43
44    Bacula® is a registered trademark of John Walker.
45    The licensor of Bacula is the Free Software Foundation Europe
46    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
47    Switzerland, email:ftf@fsfeurope.org.
48 */
49
50 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
51  * wxString(_("...")).mb_str(*wxConvCurrent) */
52
53 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
54  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
55  * So we turn _DEBUG off since we aren't interested in things it enables.
56  */
57
58 #undef _DEBUG
59
60 #include "bacula.h"
61 #include "console_conf.h"
62
63 #include <wx/intl.h>
64
65 /* Define the first and last resource ID record
66  * types. Note, these should be unique for each
67  * daemon though not a requirement.
68  */
69 int r_first = R_FIRST;
70 int r_last  = R_LAST;
71 static RES *sres_head[R_LAST - R_FIRST + 1];
72 RES **res_head = sres_head;
73
74 /* Forward referenced subroutines */
75
76
77 /* We build the current resource here as we are
78  * scanning the resource configuration definition,
79  * then move it to allocated memory when the resource
80  * scan is complete.
81  */
82 #if defined(_MSC_VER)
83 extern "C" { // work around visual compiler mangling variables
84     URES res_all;
85 }
86 #else
87 URES res_all;
88 #endif
89 int  res_all_size = sizeof(res_all);
90
91 /* Definition of records permitted within each
92  * resource with the routine to process the record
93  * information.
94  */
95
96 /*  Console "globals" */
97 static RES_ITEM cons_items[] = {
98    {"name",        store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
99    {"description", store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
100    {"rcfile",      store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
101    {"historyfile", store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
102    {"password",    store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
103    {"tlsenable",      store_bit,     ITEM(res_cons.tls_enable), 1, 0, 0},
104    {"tlsrequire",     store_bit,     ITEM(res_cons.tls_require), 1, 0, 0},
105    {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
106    {"tlscacertificatedir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
107    {"tlscertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
108    {"tlskey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
109    {NULL, NULL, {0}, 0, 0, 0}
110 };
111
112
113 /*  Director's that we can contact */
114 static RES_ITEM dir_items[] = {
115    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
116    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
117    {"dirport",     store_int,      ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
118    {"address",     store_str,      ITEM(res_dir.address),  0, 0, 0},
119    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
120    {"tlsenable",      store_bit,     ITEM(res_dir.tls_enable), 1, 0, 0},
121    {"tlsrequire",     store_bit,     ITEM(res_dir.tls_require), 1, 0, 0},
122    {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
123    {"tlscacertificatedir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
124    {"tlscertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
125    {"tlskey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
126    {NULL, NULL, {0}, 0, 0, 0}
127 };
128
129 /*
130  * This is the master resource definition.
131  * It must have one item for each of the resources.
132  */
133 RES_TABLE resources[] = {
134    {"console",       cons_items,  R_CONSOLE},
135    {"director",      dir_items,   R_DIRECTOR},
136    {NULL,        NULL,    0}
137 };
138
139
140 /* Dump contents of resource */
141 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
142 {
143    URES *res = (URES *)reshdr;
144    int recurse = 1;
145
146    if (res == NULL) {
147       printf(wxString(_("No record for %d %s\n")).mb_str(*wxConvCurrent), type, res_to_str(type));
148       return;
149    }
150    if (type < 0) {            /* no recursion */
151       type = - type;
152       recurse = 0;
153    }
154    switch (type) {
155    case R_CONSOLE:
156       printf(wxString(_("Console: name=%s rcfile=%s histfile=%s\n")).mb_str(*wxConvCurrent), reshdr->name,
157              res->res_cons.rc_file, res->res_cons.hist_file);
158       break;
159    case R_DIRECTOR:
160       printf(wxString(_("Director: name=%s address=%s DIRport=%d\n")).mb_str(*wxConvCurrent), reshdr->name,
161              res->res_dir.address, res->res_dir.DIRport);
162       break;
163    default:
164       printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
165    }
166    if (recurse && res->res_dir.hdr.next) {
167       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
168    }
169 }
170
171 /*
172  * Free memory of resource.
173  * NB, we don't need to worry about freeing any references
174  * to other resources as they will be freed when that
175  * resource chain is traversed.  Mainly we worry about freeing
176  * allocated strings (names).
177  */
178 void free_resource(RES *sres, int type)
179 {
180    RES *nres;
181    URES *res = (URES *)sres;
182
183    if (res == NULL)
184       return;
185
186    /* common stuff -- free the resource name */
187    nres = (RES *)res->res_dir.hdr.next;
188    if (res->res_dir.hdr.name) {
189       bfree(res->res_dir.hdr.name);
190    }
191    if (res->res_dir.hdr.desc) {
192       bfree(res->res_dir.hdr.desc);
193    }
194
195    switch (type) {
196    case R_CONSOLE:
197       if (res->res_cons.rc_file) {
198          bfree(res->res_cons.rc_file);
199       }
200       if (res->res_cons.hist_file) {
201          bfree(res->res_cons.hist_file);
202       }
203       if (res->res_cons.tls_ctx) { 
204          free_tls_context(res->res_cons.tls_ctx);
205       }
206       if (res->res_cons.tls_ca_certfile) {
207          bfree(res->res_cons.tls_ca_certfile);
208       }
209       if (res->res_cons.tls_ca_certdir) {
210          bfree(res->res_cons.tls_ca_certdir);
211       }
212       if (res->res_cons.tls_certfile) {
213          bfree(res->res_cons.tls_certfile);
214       }
215       if (res->res_cons.tls_keyfile) {
216          bfree(res->res_cons.tls_keyfile);
217       }
218    case R_DIRECTOR:
219       if (res->res_dir.address) {
220          bfree(res->res_dir.address);
221       }
222       if (res->res_dir.tls_ctx) { 
223          free_tls_context(res->res_dir.tls_ctx);
224       }
225       if (res->res_dir.tls_ca_certfile) {
226          bfree(res->res_dir.tls_ca_certfile);
227       }
228       if (res->res_dir.tls_ca_certdir) {
229          bfree(res->res_dir.tls_ca_certdir);
230       }
231       if (res->res_dir.tls_certfile) {
232          bfree(res->res_dir.tls_certfile);
233       }
234       if (res->res_dir.tls_keyfile) {
235          bfree(res->res_dir.tls_keyfile);
236       }
237       break;
238    default:
239          printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
240       }
241    /* Common stuff again -- free the resource, recurse to next one */
242    bfree(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;
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.res_dir.hdr.item_present)) {
265                Emsg2(M_ABORT, 0, wxString(_("%s item is required in %s resource, but not found.\n")).mb_str(*wxConvCurrent),
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_CONSOLE:
280       case R_DIRECTOR:
281          break;
282
283       default:
284          Emsg1(M_ERROR, 0, wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), 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.res_dir.hdr.name) {
292          bfree(res_all.res_dir.hdr.name);
293          res_all.res_dir.hdr.name = NULL;
294       }
295       if (res_all.res_dir.hdr.desc) {
296          bfree(res_all.res_dir.hdr.desc);
297          res_all.res_dir.hdr.desc = NULL;
298       }
299       return;
300    }
301
302    /* The following code is only executed during pass 1 */
303    switch (type) {
304    case R_CONSOLE:
305       size = sizeof(CONRES);
306       break;
307    case R_DIRECTOR:
308       size = sizeof(DIRRES);
309       break;
310    default:
311       printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
312       error = 1;
313       size = 1;
314       break;
315    }
316    /* Common */
317    if (!error) {
318       res = (URES *)bmalloc(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          for (last=next=res_head[rindex]; next; next=next->next) {
325             last = next;
326             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
327                Emsg2(M_ERROR_TERM, 0,
328                    wxString(_("Attempt to define second %s resource named \"%s\" is not permitted.\n")).mb_str(*wxConvCurrent),
329                    resources[rindex].name, res->res_dir.hdr.name);
330             }
331          }
332          last->next = (RES *)res;
333             Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
334                   res->res_dir.hdr.name);
335       }
336    }
337 }