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 plus additions
32 that are listed in the file LICENSE.
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.
51 #include "console_conf.h"
53 /* Define the first and last resource ID record
54 * types. Note, these should be unique for each
55 * daemon though not a requirement.
57 int r_first = R_FIRST;
59 static RES *sres_head[R_LAST - R_FIRST + 1];
60 RES **res_head = sres_head;
62 /* Forward referenced subroutines */
65 /* We build the current resource here as we are
66 * scanning the resource configuration definition,
67 * then move it to allocated memory when the resource
71 extern "C" { // work around visual compiler mangling variables
77 int res_all_size = sizeof(res_all);
79 /* Definition of records permitted within each
80 * resource with the routine to process the record
84 /* Console "globals" */
85 static RES_ITEM cons_items[] = {
86 {"name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
87 {"description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
88 {"rcfile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
89 {"historyfile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
90 {"password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
91 {"tlsenable", store_bit, ITEM(res_cons.tls_enable), 1, 0, 0},
92 {"tlsrequire", store_bit, ITEM(res_cons.tls_require), 1, 0, 0},
93 {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
94 {"tlscacertificatedir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
95 {"tlscertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
96 {"tlskey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
97 {"director", store_str, ITEM(res_cons.director), 0, 0, 0},
98 {NULL, NULL, {0}, 0, 0, 0}
102 /* Director's that we can contact */
103 static RES_ITEM dir_items[] = {
104 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
105 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
106 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
107 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
108 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
109 {"tlsenable", store_bit, ITEM(res_dir.tls_enable), 1, 0, 0},
110 {"tlsrequire", store_bit, ITEM(res_dir.tls_require), 1, 0, 0},
111 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
112 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
113 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
114 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
115 {NULL, NULL, {0}, 0, 0, 0}
119 * This is the master resource definition.
120 * It must have one item for each of the resources.
122 RES_TABLE resources[] = {
123 {"console", cons_items, R_CONSOLE},
124 {"director", dir_items, R_DIRECTOR},
129 /* Dump contents of resource */
130 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
132 URES *res = (URES *)reshdr;
136 printf(_("No record for %d %s\n"), type, res_to_str(type));
139 if (type < 0) { /* no recursion */
145 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
146 res->res_cons.rc_file, res->res_cons.hist_file);
149 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
150 res->res_dir.address, res->res_dir.DIRport);
153 printf(_("Unknown resource type %d\n"), type);
155 if (recurse && res->res_dir.hdr.next) {
156 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
161 * Free memory of resource.
162 * NB, we don't need to worry about freeing any references
163 * to other resources as they will be freed when that
164 * resource chain is traversed. Mainly we worry about freeing
165 * allocated strings (names).
167 void free_resource(RES *sres, int type)
170 URES *res = (URES *)sres;
175 /* common stuff -- free the resource name */
176 nres = (RES *)res->res_dir.hdr.next;
177 if (res->res_dir.hdr.name) {
178 free(res->res_dir.hdr.name);
180 if (res->res_dir.hdr.desc) {
181 free(res->res_dir.hdr.desc);
186 if (res->res_cons.rc_file) {
187 free(res->res_cons.rc_file);
189 if (res->res_cons.hist_file) {
190 free(res->res_cons.hist_file);
192 if (res->res_cons.tls_ctx) {
193 free_tls_context(res->res_cons.tls_ctx);
195 if (res->res_cons.tls_ca_certfile) {
196 free(res->res_cons.tls_ca_certfile);
198 if (res->res_cons.tls_ca_certdir) {
199 free(res->res_cons.tls_ca_certdir);
201 if (res->res_cons.tls_certfile) {
202 free(res->res_cons.tls_certfile);
204 if (res->res_cons.tls_keyfile) {
205 free(res->res_cons.tls_keyfile);
209 if (res->res_dir.address) {
210 free(res->res_dir.address);
212 if (res->res_dir.tls_ctx) {
213 free_tls_context(res->res_dir.tls_ctx);
215 if (res->res_dir.tls_ca_certfile) {
216 free(res->res_dir.tls_ca_certfile);
218 if (res->res_dir.tls_ca_certdir) {
219 free(res->res_dir.tls_ca_certdir);
221 if (res->res_dir.tls_certfile) {
222 free(res->res_dir.tls_certfile);
224 if (res->res_dir.tls_keyfile) {
225 free(res->res_dir.tls_keyfile);
229 printf(_("Unknown resource type %d\n"), type);
231 /* Common stuff again -- free the resource, recurse to next one */
234 free_resource(nres, type);
238 /* Save the new resource by chaining it into the head list for
239 * the resource. If this is pass 2, we update any resource
240 * pointers (currently only in the Job resource).
242 void save_resource(int type, RES_ITEM *items, int pass)
245 int rindex = type - r_first;
250 * Ensure that all required items are present
252 for (i=0; items[i].name; i++) {
253 if (items[i].flags & ITEM_REQUIRED) {
254 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
255 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
256 items[i].name, resources[rindex]);
261 /* During pass 2, we looked up pointers to all the resources
262 * referrenced in the current resource, , now we
263 * must copy their address from the static record to the allocated
268 /* Resources not containing a resource */
274 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
278 /* Note, the resoure name was already saved during pass 1,
279 * so here, we can just release it.
281 if (res_all.res_dir.hdr.name) {
282 free(res_all.res_dir.hdr.name);
283 res_all.res_dir.hdr.name = NULL;
285 if (res_all.res_dir.hdr.desc) {
286 free(res_all.res_dir.hdr.desc);
287 res_all.res_dir.hdr.desc = NULL;
292 /* The following code is only executed during pass 1 */
295 size = sizeof(CONRES);
298 size = sizeof(DIRRES);
301 printf(_("Unknown resource type %d\n"), type);
308 res = (URES *)malloc(size);
309 memcpy(res, &res_all, size);
310 if (!res_head[rindex]) {
311 res_head[rindex] = (RES *)res; /* store first entry */
314 for (next=res_head[rindex]; next->next; next=next->next) {
315 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
316 Emsg2(M_ERROR_TERM, 0,
317 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
318 resources[rindex].name, res->res_dir.hdr.name);
321 next->next = (RES *)res;
322 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
323 res->res_dir.hdr.name);