]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_conf.c
Change Groupbox name to Remote Director in Configuration page if Director is not...
[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    Copyright (C) 2000-2006 Kern Sibbald
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License
27    version 2 as amended with additional clauses defined in the
28    file LICENSE in the main source directory.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
33    the file LICENSE for additional details.
34
35  */
36
37 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
38  * wxString(_("...")).mb_str(*wxConvCurrent) */
39
40 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
41  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
42  * So we turn _DEBUG off since we aren't interested in things it enables.
43  */
44
45 #undef _DEBUG
46
47 #include "bacula.h"
48 #include "console_conf.h"
49
50 #include <wx/intl.h>
51
52 /* Define the first and last resource ID record
53  * types. Note, these should be unique for each
54  * daemon though not a requirement.
55  */
56 int r_first = R_FIRST;
57 int r_last  = R_LAST;
58 static RES *sres_head[R_LAST - R_FIRST + 1];
59 RES **res_head = sres_head;
60
61 /* Forward referenced subroutines */
62
63
64 /* We build the current resource here as we are
65  * scanning the resource configuration definition,
66  * then move it to allocated memory when the resource
67  * scan is complete.
68  */
69 #if defined(_MSC_VER)
70 extern "C" { // work around visual compiler mangling variables
71     URES res_all;
72 }
73 #else
74 URES res_all;
75 #endif
76 int  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
83 /*  Console "globals" */
84 static RES_ITEM cons_items[] = {
85    {"name",        store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
86    {"description", store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
87    {"rcfile",      store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
88    {"historyfile", store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
89    {"password",    store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
90    {"tlsenable",      store_bit,     ITEM(res_cons.tls_enable), 1, 0, 0},
91    {"tlsrequire",     store_bit,     ITEM(res_cons.tls_require), 1, 0, 0},
92    {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
93    {"tlscacertificatedir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
94    {"tlscertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
95    {"tlskey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
96    {NULL, NULL, {0}, 0, 0, 0}
97 };
98
99
100 /*  Director's that we can contact */
101 static RES_ITEM dir_items[] = {
102    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
103    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
104    {"dirport",     store_int,      ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
105    {"address",     store_str,      ITEM(res_dir.address),  0, 0, 0},
106    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
107    {"tlsenable",      store_bit,     ITEM(res_dir.tls_enable), 1, 0, 0},
108    {"tlsrequire",     store_bit,     ITEM(res_dir.tls_require), 1, 0, 0},
109    {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
110    {"tlscacertificatedir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
111    {"tlscertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
112    {"tlskey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
113    {NULL, NULL, {0}, 0, 0, 0}
114 };
115
116 /*
117  * This is the master resource definition.
118  * It must have one item for each of the resources.
119  */
120 RES_TABLE resources[] = {
121    {"console",       cons_items,  R_CONSOLE},
122    {"director",      dir_items,   R_DIRECTOR},
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    int recurse = 1;
132
133    if (res == NULL) {
134       printf(wxString(_("No record for %d %s\n")).mb_str(*wxConvCurrent), type, res_to_str(type));
135       return;
136    }
137    if (type < 0) {            /* no recursion */
138       type = - type;
139       recurse = 0;
140    }
141    switch (type) {
142    case R_CONSOLE:
143       printf(wxString(_("Console: name=%s rcfile=%s histfile=%s\n")).mb_str(*wxConvCurrent), reshdr->name,
144              res->res_cons.rc_file, res->res_cons.hist_file);
145       break;
146    case R_DIRECTOR:
147       printf(wxString(_("Director: name=%s address=%s DIRport=%d\n")).mb_str(*wxConvCurrent), reshdr->name,
148              res->res_dir.address, res->res_dir.DIRport);
149       break;
150    default:
151       printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
152    }
153    if (recurse && res->res_dir.hdr.next) {
154       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
155    }
156 }
157
158 /*
159  * Free memory of resource.
160  * NB, we don't need to worry about freeing any references
161  * to other resources as they will be freed when that
162  * resource chain is traversed.  Mainly we worry about freeing
163  * allocated strings (names).
164  */
165 void free_resource(RES *sres, int type)
166 {
167    RES *nres;
168    URES *res = (URES *)sres;
169
170    if (res == NULL)
171       return;
172
173    /* common stuff -- free the resource name */
174    nres = (RES *)res->res_dir.hdr.next;
175    if (res->res_dir.hdr.name) {
176       bfree(res->res_dir.hdr.name);
177    }
178    if (res->res_dir.hdr.desc) {
179       bfree(res->res_dir.hdr.desc);
180    }
181
182    switch (type) {
183    case R_CONSOLE:
184       if (res->res_cons.rc_file) {
185          bfree(res->res_cons.rc_file);
186       }
187       if (res->res_cons.hist_file) {
188          bfree(res->res_cons.hist_file);
189       }
190       if (res->res_cons.tls_ctx) { 
191          free_tls_context(res->res_cons.tls_ctx);
192       }
193       if (res->res_cons.tls_ca_certfile) {
194          bfree(res->res_cons.tls_ca_certfile);
195       }
196       if (res->res_cons.tls_ca_certdir) {
197          bfree(res->res_cons.tls_ca_certdir);
198       }
199       if (res->res_cons.tls_certfile) {
200          bfree(res->res_cons.tls_certfile);
201       }
202       if (res->res_cons.tls_keyfile) {
203          bfree(res->res_cons.tls_keyfile);
204       }
205    case R_DIRECTOR:
206       if (res->res_dir.address) {
207          bfree(res->res_dir.address);
208       }
209       if (res->res_dir.tls_ctx) { 
210          free_tls_context(res->res_dir.tls_ctx);
211       }
212       if (res->res_dir.tls_ca_certfile) {
213          bfree(res->res_dir.tls_ca_certfile);
214       }
215       if (res->res_dir.tls_ca_certdir) {
216          bfree(res->res_dir.tls_ca_certdir);
217       }
218       if (res->res_dir.tls_certfile) {
219          bfree(res->res_dir.tls_certfile);
220       }
221       if (res->res_dir.tls_keyfile) {
222          bfree(res->res_dir.tls_keyfile);
223       }
224       break;
225    default:
226          printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
227       }
228    /* Common stuff again -- free the resource, recurse to next one */
229    bfree(res);
230    if (nres) {
231       free_resource(nres, type);
232    }
233 }
234
235 /* Save the new resource by chaining it into the head list for
236  * the resource. If this is pass 2, we update any resource
237  * pointers (currently only in the Job resource).
238  */
239 void save_resource(int type, RES_ITEM *items, int pass)
240 {
241    URES *res;
242    int rindex = type - r_first;
243    int i, size;
244    int error = 0;
245
246    /*
247     * Ensure that all required items are present
248     */
249    for (i=0; items[i].name; i++) {
250       if (items[i].flags & ITEM_REQUIRED) {
251        if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
252                Emsg2(M_ABORT, 0, wxString(_("%s item is required in %s resource, but not found.\n")).mb_str(*wxConvCurrent),
253        items[i].name, resources[rindex]);
254         }
255       }
256    }
257
258    /* During pass 2, we looked up pointers to all the resources
259     * referrenced in the current resource, , now we
260     * must copy their address from the static record to the allocated
261     * record.
262     */
263    if (pass == 2) {
264       switch (type) {
265       /* Resources not containing a resource */
266       case R_CONSOLE:
267       case R_DIRECTOR:
268          break;
269
270       default:
271          Emsg1(M_ERROR, 0, wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
272          error = 1;
273          break;
274       }
275       /* Note, the resoure name was already saved during pass 1,
276        * so here, we can just release it.
277        */
278       if (res_all.res_dir.hdr.name) {
279          bfree(res_all.res_dir.hdr.name);
280          res_all.res_dir.hdr.name = NULL;
281       }
282       if (res_all.res_dir.hdr.desc) {
283          bfree(res_all.res_dir.hdr.desc);
284          res_all.res_dir.hdr.desc = NULL;
285       }
286       return;
287    }
288
289    /* The following code is only executed during pass 1 */
290    switch (type) {
291    case R_CONSOLE:
292       size = sizeof(CONRES);
293       break;
294    case R_DIRECTOR:
295       size = sizeof(DIRRES);
296       break;
297    default:
298       printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
299       error = 1;
300       size = 1;
301       break;
302    }
303    /* Common */
304    if (!error) {
305       res = (URES *)bmalloc(size);
306       memcpy(res, &res_all, size);
307       if (!res_head[rindex]) {
308          res_head[rindex] = (RES *)res; /* store first entry */
309       } else {
310          RES *next;
311          for (next=res_head[rindex]; next->next; next=next->next) {
312             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
313                Emsg2(M_ERROR_TERM, 0,
314                    wxString(_("Attempt to define second %s resource named \"%s\" is not permitted.\n")).mb_str(*wxConvCurrent),
315                    resources[rindex].name, res->res_dir.hdr.name);
316             }
317          }
318          next->next = (RES *)res;
319             Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
320                   res->res_dir.hdr.name);
321       }
322    }
323 }