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