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