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