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).
6 * Note, the configuration file parser consists of three parts
8 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
10 * 2. The generic config scanner in lib/parse_config.c and
12 * These files contain the parser code, some utility
13 * routines, and the common store routines (name, int,
16 * 3. The daemon specific file, which contains the Resource
17 * definitions as well as any specific store routines
18 * for the resource records.
20 * Kern Sibbald, January MM, September MM
23 Copyright (C) 2000-2006 Kern Sibbald
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.
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.
37 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
38 * wxString(_("...")).mb_str(*wxConvCurrent) */
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.
48 #include "console_conf.h"
52 /* Define the first and last resource ID record
53 * types. Note, these should be unique for each
54 * daemon though not a requirement.
56 int r_first = R_FIRST;
58 static RES *sres_head[R_LAST - R_FIRST + 1];
59 RES **res_head = sres_head;
61 /* Forward referenced subroutines */
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
70 extern "C" { // work around visual compiler mangling variables
76 int res_all_size = sizeof(res_all);
78 /* Definition of records permitted within each
79 * resource with the routine to process the record
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}
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}
117 * This is the master resource definition.
118 * It must have one item for each of the resources.
120 RES_TABLE resources[] = {
121 {"console", cons_items, R_CONSOLE},
122 {"director", dir_items, R_DIRECTOR},
127 /* Dump contents of resource */
128 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
130 URES *res = (URES *)reshdr;
134 printf(wxString(_("No record for %d %s\n")).mb_str(*wxConvCurrent), type, res_to_str(type));
137 if (type < 0) { /* no recursion */
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);
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);
151 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
153 if (recurse && res->res_dir.hdr.next) {
154 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
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).
165 void free_resource(RES *sres, int type)
168 URES *res = (URES *)sres;
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);
178 if (res->res_dir.hdr.desc) {
179 bfree(res->res_dir.hdr.desc);
184 if (res->res_cons.rc_file) {
185 bfree(res->res_cons.rc_file);
187 if (res->res_cons.hist_file) {
188 bfree(res->res_cons.hist_file);
190 if (res->res_cons.tls_ctx) {
191 free_tls_context(res->res_cons.tls_ctx);
193 if (res->res_cons.tls_ca_certfile) {
194 bfree(res->res_cons.tls_ca_certfile);
196 if (res->res_cons.tls_ca_certdir) {
197 bfree(res->res_cons.tls_ca_certdir);
199 if (res->res_cons.tls_certfile) {
200 bfree(res->res_cons.tls_certfile);
202 if (res->res_cons.tls_keyfile) {
203 bfree(res->res_cons.tls_keyfile);
206 if (res->res_dir.address) {
207 bfree(res->res_dir.address);
209 if (res->res_dir.tls_ctx) {
210 free_tls_context(res->res_dir.tls_ctx);
212 if (res->res_dir.tls_ca_certfile) {
213 bfree(res->res_dir.tls_ca_certfile);
215 if (res->res_dir.tls_ca_certdir) {
216 bfree(res->res_dir.tls_ca_certdir);
218 if (res->res_dir.tls_certfile) {
219 bfree(res->res_dir.tls_certfile);
221 if (res->res_dir.tls_keyfile) {
222 bfree(res->res_dir.tls_keyfile);
226 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
228 /* Common stuff again -- free the resource, recurse to next one */
231 free_resource(nres, type);
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).
239 void save_resource(int type, RES_ITEM *items, int pass)
242 int rindex = type - r_first;
247 * Ensure that all required items are present
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]);
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
265 /* Resources not containing a resource */
271 Emsg1(M_ERROR, 0, wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
275 /* Note, the resoure name was already saved during pass 1,
276 * so here, we can just release it.
278 if (res_all.res_dir.hdr.name) {
279 bfree(res_all.res_dir.hdr.name);
280 res_all.res_dir.hdr.name = NULL;
282 if (res_all.res_dir.hdr.desc) {
283 bfree(res_all.res_dir.hdr.desc);
284 res_all.res_dir.hdr.desc = NULL;
289 /* The following code is only executed during pass 1 */
292 size = sizeof(CONRES);
295 size = sizeof(DIRRES);
298 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
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 */
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);
318 next->next = (RES *)res;
319 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
320 res->res_dir.hdr.name);