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-2005 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 {NULL, NULL, {0}, 0, 0, 0}
88 /* Director's that we can contact */
89 static RES_ITEM dir_items[] = {
90 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
91 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
92 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
93 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
94 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
95 {"tlsenable", store_bit, ITEM(res_dir.tls_enable), 1, 0, 0},
96 {"tlsrequire", store_bit, ITEM(res_dir.tls_require), 1, 0, 0},
97 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
98 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
99 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
100 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
101 {NULL, NULL, {0}, 0, 0, 0}
105 * This is the master resource definition.
106 * It must have one item for each of the resources.
108 RES_TABLE resources[] = {
109 {"console", cons_items, R_CONSOLE},
110 {"director", dir_items, R_DIRECTOR},
115 /* Dump contents of resource */
116 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
118 URES *res = (URES *)reshdr;
122 printf(_("No record for %d %s\n"), type, res_to_str(type));
125 if (type < 0) { /* no recursion */
131 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
132 res->res_cons.rc_file, res->res_cons.hist_file);
135 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
136 res->res_dir.address, res->res_dir.DIRport);
139 printf(_("Unknown resource type %d\n"), type);
141 if (recurse && res->res_dir.hdr.next) {
142 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
147 * Free memory of resource.
148 * NB, we don't need to worry about freeing any references
149 * to other resources as they will be freed when that
150 * resource chain is traversed. Mainly we worry about freeing
151 * allocated strings (names).
153 void free_resource(RES *sres, int type)
156 URES *res = (URES *)sres;
161 /* common stuff -- free the resource name */
162 nres = (RES *)res->res_dir.hdr.next;
163 if (res->res_dir.hdr.name) {
164 free(res->res_dir.hdr.name);
166 if (res->res_dir.hdr.desc) {
167 free(res->res_dir.hdr.desc);
172 if (res->res_cons.rc_file) {
173 free(res->res_cons.rc_file);
175 if (res->res_cons.hist_file) {
176 free(res->res_cons.hist_file);
178 if (res->res_cons.tls_ctx) {
179 free_tls_context(res->res_cons.tls_ctx);
181 if (res->res_cons.tls_ca_certfile) {
182 free(res->res_cons.tls_ca_certfile);
184 if (res->res_cons.tls_ca_certdir) {
185 free(res->res_cons.tls_ca_certdir);
187 if (res->res_cons.tls_certfile) {
188 free(res->res_cons.tls_certfile);
190 if (res->res_cons.tls_keyfile) {
191 free(res->res_cons.tls_keyfile);
195 if (res->res_dir.address) {
196 free(res->res_dir.address);
198 if (res->res_dir.tls_ctx) {
199 free_tls_context(res->res_dir.tls_ctx);
201 if (res->res_dir.tls_ca_certfile) {
202 free(res->res_dir.tls_ca_certfile);
204 if (res->res_dir.tls_ca_certdir) {
205 free(res->res_dir.tls_ca_certdir);
207 if (res->res_dir.tls_certfile) {
208 free(res->res_dir.tls_certfile);
210 if (res->res_dir.tls_keyfile) {
211 free(res->res_dir.tls_keyfile);
215 printf(_("Unknown resource type %d\n"), type);
217 /* Common stuff again -- free the resource, recurse to next one */
220 free_resource(nres, type);
224 /* Save the new resource by chaining it into the head list for
225 * the resource. If this is pass 2, we update any resource
226 * pointers (currently only in the Job resource).
228 void save_resource(int type, RES_ITEM *items, int pass)
231 int rindex = type - r_first;
236 * Ensure that all required items are present
238 for (i=0; items[i].name; i++) {
239 if (items[i].flags & ITEM_REQUIRED) {
240 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
241 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
242 items[i].name, resources[rindex]);
247 /* During pass 2, we looked up pointers to all the resources
248 * referrenced in the current resource, , now we
249 * must copy their address from the static record to the allocated
254 /* Resources not containing a resource */
260 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
264 /* Note, the resoure name was already saved during pass 1,
265 * so here, we can just release it.
267 if (res_all.res_dir.hdr.name) {
268 free(res_all.res_dir.hdr.name);
269 res_all.res_dir.hdr.name = NULL;
271 if (res_all.res_dir.hdr.desc) {
272 free(res_all.res_dir.hdr.desc);
273 res_all.res_dir.hdr.desc = NULL;
278 /* The following code is only executed during pass 1 */
281 size = sizeof(CONRES);
284 size = sizeof(DIRRES);
287 printf(_("Unknown resource type %d\n"), type);
294 res = (URES *)malloc(size);
295 memcpy(res, &res_all, size);
296 if (!res_head[rindex]) {
297 res_head[rindex] = (RES *)res; /* store first entry */
300 for (next=res_head[rindex]; next->next; next=next->next) {
301 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
302 Emsg2(M_ERROR_TERM, 0,
303 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
304 resources[rindex].name, res->res_dir.hdr.name);
307 next->next = (RES *)res;
308 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
309 res->res_dir.hdr.name);