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 Bacula® - The Network Backup Solution
25 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
27 The main author of Bacula is Kern Sibbald, with contributions from
28 many others, a complete list can be found in the file AUTHORS.
29 This program is Free Software; you can redistribute it and/or
30 modify it under the terms of version two of the GNU General Public
31 License as published by the Free Software Foundation and included
34 This program is distributed in the hope that it will be useful, but
35 WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 General Public License for more details.
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
44 Bacula® is a registered trademark of John Walker.
45 The licensor of Bacula is the Free Software Foundation Europe
46 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
47 Switzerland, email:ftf@fsfeurope.org.
50 /* _("...") macro returns a wxChar*, so if we need a char*, we need to convert it with:
51 * wxString(_("...")).mb_str(*wxConvCurrent) */
53 /* Windows debug builds set _DEBUG which is used by wxWidgets to select their
54 * debug memory allocator. Unfortunately it conflicts with Bacula's SmartAlloc.
55 * So we turn _DEBUG off since we aren't interested in things it enables.
61 #include "console_conf.h"
65 /* Define the first and last resource ID record
66 * types. Note, these should be unique for each
67 * daemon though not a requirement.
69 int r_first = R_FIRST;
71 static RES *sres_head[R_LAST - R_FIRST + 1];
72 RES **res_head = sres_head;
74 /* Forward referenced subroutines */
77 /* We build the current resource here as we are
78 * scanning the resource configuration definition,
79 * then move it to allocated memory when the resource
83 extern "C" { // work around visual compiler mangling variables
89 int res_all_size = sizeof(res_all);
91 /* Definition of records permitted within each
92 * resource with the routine to process the record
96 /* Console "globals" */
97 static RES_ITEM cons_items[] = {
98 {"name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
99 {"description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
100 {"rcfile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
101 {"historyfile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
102 {"password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
103 {"tlsenable", store_bit, ITEM(res_cons.tls_enable), 1, 0, 0},
104 {"tlsrequire", store_bit, ITEM(res_cons.tls_require), 1, 0, 0},
105 {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
106 {"tlscacertificatedir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
107 {"tlscertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
108 {"tlskey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
109 {NULL, NULL, {0}, 0, 0, 0}
113 /* Director's that we can contact */
114 static RES_ITEM dir_items[] = {
115 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
116 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
117 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
118 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
119 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
120 {"tlsenable", store_bit, ITEM(res_dir.tls_enable), 1, 0, 0},
121 {"tlsrequire", store_bit, ITEM(res_dir.tls_require), 1, 0, 0},
122 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
123 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
124 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
125 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
126 {NULL, NULL, {0}, 0, 0, 0}
130 * This is the master resource definition.
131 * It must have one item for each of the resources.
133 RES_TABLE resources[] = {
134 {"console", cons_items, R_CONSOLE},
135 {"director", dir_items, R_DIRECTOR},
140 /* Dump contents of resource */
141 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
143 URES *res = (URES *)reshdr;
147 printf(wxString(_("No record for %d %s\n")).mb_str(*wxConvCurrent), type, res_to_str(type));
150 if (type < 0) { /* no recursion */
156 printf(wxString(_("Console: name=%s rcfile=%s histfile=%s\n")).mb_str(*wxConvCurrent), reshdr->name,
157 res->res_cons.rc_file, res->res_cons.hist_file);
160 printf(wxString(_("Director: name=%s address=%s DIRport=%d\n")).mb_str(*wxConvCurrent), reshdr->name,
161 res->res_dir.address, res->res_dir.DIRport);
164 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
166 if (recurse && res->res_dir.hdr.next) {
167 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
172 * Free memory of resource.
173 * NB, we don't need to worry about freeing any references
174 * to other resources as they will be freed when that
175 * resource chain is traversed. Mainly we worry about freeing
176 * allocated strings (names).
178 void free_resource(RES *sres, int type)
181 URES *res = (URES *)sres;
186 /* common stuff -- free the resource name */
187 nres = (RES *)res->res_dir.hdr.next;
188 if (res->res_dir.hdr.name) {
189 bfree(res->res_dir.hdr.name);
191 if (res->res_dir.hdr.desc) {
192 bfree(res->res_dir.hdr.desc);
197 if (res->res_cons.rc_file) {
198 bfree(res->res_cons.rc_file);
200 if (res->res_cons.hist_file) {
201 bfree(res->res_cons.hist_file);
203 if (res->res_cons.tls_ctx) {
204 free_tls_context(res->res_cons.tls_ctx);
206 if (res->res_cons.tls_ca_certfile) {
207 bfree(res->res_cons.tls_ca_certfile);
209 if (res->res_cons.tls_ca_certdir) {
210 bfree(res->res_cons.tls_ca_certdir);
212 if (res->res_cons.tls_certfile) {
213 bfree(res->res_cons.tls_certfile);
215 if (res->res_cons.tls_keyfile) {
216 bfree(res->res_cons.tls_keyfile);
219 if (res->res_dir.address) {
220 bfree(res->res_dir.address);
222 if (res->res_dir.tls_ctx) {
223 free_tls_context(res->res_dir.tls_ctx);
225 if (res->res_dir.tls_ca_certfile) {
226 bfree(res->res_dir.tls_ca_certfile);
228 if (res->res_dir.tls_ca_certdir) {
229 bfree(res->res_dir.tls_ca_certdir);
231 if (res->res_dir.tls_certfile) {
232 bfree(res->res_dir.tls_certfile);
234 if (res->res_dir.tls_keyfile) {
235 bfree(res->res_dir.tls_keyfile);
239 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
241 /* Common stuff again -- free the resource, recurse to next one */
244 free_resource(nres, type);
248 /* Save the new resource by chaining it into the head list for
249 * the resource. If this is pass 2, we update any resource
250 * pointers (currently only in the Job resource).
252 void save_resource(int type, RES_ITEM *items, int pass)
255 int rindex = type - r_first;
260 * Ensure that all required items are present
262 for (i=0; items[i].name; i++) {
263 if (items[i].flags & ITEM_REQUIRED) {
264 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
265 Emsg2(M_ABORT, 0, wxString(_("%s item is required in %s resource, but not found.\n")).mb_str(*wxConvCurrent),
266 items[i].name, resources[rindex]);
271 /* During pass 2, we looked up pointers to all the resources
272 * referrenced in the current resource, , now we
273 * must copy their address from the static record to the allocated
278 /* Resources not containing a resource */
284 Emsg1(M_ERROR, 0, wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
288 /* Note, the resoure name was already saved during pass 1,
289 * so here, we can just release it.
291 if (res_all.res_dir.hdr.name) {
292 bfree(res_all.res_dir.hdr.name);
293 res_all.res_dir.hdr.name = NULL;
295 if (res_all.res_dir.hdr.desc) {
296 bfree(res_all.res_dir.hdr.desc);
297 res_all.res_dir.hdr.desc = NULL;
302 /* The following code is only executed during pass 1 */
305 size = sizeof(CONRES);
308 size = sizeof(DIRRES);
311 printf(wxString(_("Unknown resource type %d\n")).mb_str(*wxConvCurrent), type);
318 res = (URES *)bmalloc(size);
319 memcpy(res, &res_all, size);
320 if (!res_head[rindex]) {
321 res_head[rindex] = (RES *)res; /* store first entry */
324 for (last=next=res_head[rindex]; next; next=next->next) {
326 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
327 Emsg2(M_ERROR_TERM, 0,
328 wxString(_("Attempt to define second %s resource named \"%s\" is not permitted.\n")).mb_str(*wxConvCurrent),
329 resources[rindex].name, res->res_dir.hdr.name);
332 last->next = (RES *)res;
333 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
334 res->res_dir.hdr.name);