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.
38 #include "console_conf.h"
40 /* Define the first and last resource ID record
41 * types. Note, these should be unique for each
42 * daemon though not a requirement.
44 int r_first = R_FIRST;
46 static RES *sres_head[R_LAST - R_FIRST + 1];
47 RES **res_head = sres_head;
49 /* Forward referenced subroutines */
52 /* We build the current resource here as we are
53 * scanning the resource configuration definition,
54 * then move it to allocated memory when the resource
58 extern "C" { // work around visual compiler mangling variables
64 int res_all_size = sizeof(res_all);
66 /* Definition of records permitted within each
67 * resource with the routine to process the record
71 /* Console "globals" */
72 static RES_ITEM cons_items[] = {
73 {"name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
74 {"description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
75 {"rcfile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
76 {"historyfile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
77 {"password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
78 {"tlsenable", store_bit, ITEM(res_cons.tls_enable), 1, 0, 0},
79 {"tlsrequire", store_bit, ITEM(res_cons.tls_require), 1, 0, 0},
80 {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
81 {"tlscacertificatedir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
82 {"tlscertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
83 {"tlskey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
84 {"director", store_str, ITEM(res_cons.director), 0, 0, 0},
85 {NULL, NULL, {0}, 0, 0, 0}
89 /* Director's that we can contact */
90 static RES_ITEM dir_items[] = {
91 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
92 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
93 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
94 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
95 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
96 {"tlsenable", store_bit, ITEM(res_dir.tls_enable), 1, 0, 0},
97 {"tlsrequire", store_bit, ITEM(res_dir.tls_require), 1, 0, 0},
98 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
99 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
100 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
101 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
102 {NULL, NULL, {0}, 0, 0, 0}
106 * This is the master resource definition.
107 * It must have one item for each of the resources.
109 RES_TABLE resources[] = {
110 {"console", cons_items, R_CONSOLE},
111 {"director", dir_items, R_DIRECTOR},
116 /* Dump contents of resource */
117 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
119 URES *res = (URES *)reshdr;
123 printf(_("No record for %d %s\n"), type, res_to_str(type));
126 if (type < 0) { /* no recursion */
132 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
133 res->res_cons.rc_file, res->res_cons.hist_file);
136 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
137 res->res_dir.address, res->res_dir.DIRport);
140 printf(_("Unknown resource type %d\n"), type);
142 if (recurse && res->res_dir.hdr.next) {
143 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
148 * Free memory of resource.
149 * NB, we don't need to worry about freeing any references
150 * to other resources as they will be freed when that
151 * resource chain is traversed. Mainly we worry about freeing
152 * allocated strings (names).
154 void free_resource(RES *sres, int type)
157 URES *res = (URES *)sres;
162 /* common stuff -- free the resource name */
163 nres = (RES *)res->res_dir.hdr.next;
164 if (res->res_dir.hdr.name) {
165 free(res->res_dir.hdr.name);
167 if (res->res_dir.hdr.desc) {
168 free(res->res_dir.hdr.desc);
173 if (res->res_cons.rc_file) {
174 free(res->res_cons.rc_file);
176 if (res->res_cons.hist_file) {
177 free(res->res_cons.hist_file);
179 if (res->res_cons.tls_ctx) {
180 free_tls_context(res->res_cons.tls_ctx);
182 if (res->res_cons.tls_ca_certfile) {
183 free(res->res_cons.tls_ca_certfile);
185 if (res->res_cons.tls_ca_certdir) {
186 free(res->res_cons.tls_ca_certdir);
188 if (res->res_cons.tls_certfile) {
189 free(res->res_cons.tls_certfile);
191 if (res->res_cons.tls_keyfile) {
192 free(res->res_cons.tls_keyfile);
196 if (res->res_dir.address) {
197 free(res->res_dir.address);
199 if (res->res_dir.tls_ctx) {
200 free_tls_context(res->res_dir.tls_ctx);
202 if (res->res_dir.tls_ca_certfile) {
203 free(res->res_dir.tls_ca_certfile);
205 if (res->res_dir.tls_ca_certdir) {
206 free(res->res_dir.tls_ca_certdir);
208 if (res->res_dir.tls_certfile) {
209 free(res->res_dir.tls_certfile);
211 if (res->res_dir.tls_keyfile) {
212 free(res->res_dir.tls_keyfile);
216 printf(_("Unknown resource type %d\n"), type);
218 /* Common stuff again -- free the resource, recurse to next one */
221 free_resource(nres, type);
225 /* Save the new resource by chaining it into the head list for
226 * the resource. If this is pass 2, we update any resource
227 * pointers (currently only in the Job resource).
229 void save_resource(int type, RES_ITEM *items, int pass)
232 int rindex = type - r_first;
237 * Ensure that all required items are present
239 for (i=0; items[i].name; i++) {
240 if (items[i].flags & ITEM_REQUIRED) {
241 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
242 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
243 items[i].name, resources[rindex]);
248 /* During pass 2, we looked up pointers to all the resources
249 * referrenced in the current resource, , now we
250 * must copy their address from the static record to the allocated
255 /* Resources not containing a resource */
261 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
265 /* Note, the resoure name was already saved during pass 1,
266 * so here, we can just release it.
268 if (res_all.res_dir.hdr.name) {
269 free(res_all.res_dir.hdr.name);
270 res_all.res_dir.hdr.name = NULL;
272 if (res_all.res_dir.hdr.desc) {
273 free(res_all.res_dir.hdr.desc);
274 res_all.res_dir.hdr.desc = NULL;
279 /* The following code is only executed during pass 1 */
282 size = sizeof(CONRES);
285 size = sizeof(DIRRES);
288 printf(_("Unknown resource type %d\n"), type);
295 res = (URES *)malloc(size);
296 memcpy(res, &res_all, size);
297 if (!res_head[rindex]) {
298 res_head[rindex] = (RES *)res; /* store first entry */
301 for (next=res_head[rindex]; next->next; next=next->next) {
302 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
303 Emsg2(M_ERROR_TERM, 0,
304 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
305 resources[rindex].name, res->res_dir.hdr.name);
308 next->next = (RES *)res;
309 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
310 res->res_dir.hdr.name);