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
25 Bacula® - The Network Backup Solution
27 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
29 The main author of Bacula is Kern Sibbald, with contributions from
30 many others, a complete list can be found in the file AUTHORS.
31 This program is Free Software; you can redistribute it and/or
32 modify it under the terms of version two of the GNU General Public
33 License as published by the Free Software Foundation plus additions
34 that are listed in the file LICENSE.
36 This program is distributed in the hope that it will be useful, but
37 WITHOUT ANY WARRANTY; without even the implied warranty of
38 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 General Public License for more details.
41 You should have received a copy of the GNU General Public License
42 along with this program; if not, write to the Free Software
43 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
46 Bacula® is a registered trademark of John Walker.
47 The licensor of Bacula is the Free Software Foundation Europe
48 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
49 Switzerland, email:ftf@fsfeurope.org.
55 /* Define the first and last resource ID record
56 * types. Note, these should be unique for each
57 * daemon though not a requirement.
59 int r_first = R_FIRST;
61 static RES *sres_head[R_LAST - R_FIRST + 1];
62 RES **res_head = sres_head;
64 /* Forward referenced subroutines */
67 /* We build the current resource here as we are
68 * scanning the resource configuration definition,
69 * then move it to allocated memory when the resource
73 int res_all_size = sizeof(res_all);
75 /* Definition of records permitted within each
76 * resource with the routine to process the record
79 static RES_ITEM dir_items[] = {
80 {"name", store_name, ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
81 {"description", store_str, ITEM(dir_res.hdr.desc), 0, 0, 0},
82 {"dirport", store_int, ITEM(dir_res.DIRport), 0, ITEM_DEFAULT, 9101},
83 {"address", store_str, ITEM(dir_res.address), 0, ITEM_REQUIRED, 0},
84 {"password", store_password, ITEM(dir_res.password), 0, 0, 0},
85 {"tlsenable", store_bit, ITEM(dir_res.tls_enable), 1, 0, 0},
86 {"tlsrequire", store_bit, ITEM(dir_res.tls_require), 1, 0, 0},
87 {"tlscacertificatefile", store_dir, ITEM(dir_res.tls_ca_certfile), 0, 0, 0},
88 {"tlscacertificatedir", store_dir, ITEM(dir_res.tls_ca_certdir), 0, 0, 0},
89 {"tlscertificate", store_dir, ITEM(dir_res.tls_certfile), 0, 0, 0},
90 {"tlskey", store_dir, ITEM(dir_res.tls_keyfile), 0, 0, 0},
91 {NULL, NULL, {0}, 0, 0, 0}
94 static RES_ITEM con_items[] = {
95 {"name", store_name, ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
96 {"description", store_str, ITEM(con_res.hdr.desc), 0, 0, 0},
97 {"password", store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
98 {"tlsenable", store_bit, ITEM(con_res.tls_enable), 1, 0, 0},
99 {"tlsrequire", store_bit, ITEM(con_res.tls_require), 1, 0, 0},
100 {"tlscacertificatefile", store_dir, ITEM(con_res.tls_ca_certfile), 0, 0, 0},
101 {"tlscacertificatedir", store_dir, ITEM(con_res.tls_ca_certdir), 0, 0, 0},
102 {"tlscertificate", store_dir, ITEM(con_res.tls_certfile), 0, 0, 0},
103 {"tlskey", store_dir, ITEM(con_res.tls_keyfile), 0, 0, 0},
104 {NULL, NULL, {0}, 0, 0, 0}
107 static RES_ITEM con_font_items[] = {
108 {"name", store_name, ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
109 {"description", store_str, ITEM(con_font.hdr.desc), 0, 0, 0},
110 {"font", store_str, ITEM(con_font.fontface), 0, 0, 0},
111 {"requiressl", store_bit, ITEM(con_font.require_ssl), 1, ITEM_DEFAULT, 0},
112 {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 {"director", dir_items, R_DIRECTOR},
122 {"console", con_items, R_CONSOLE},
123 {"consolefont", con_font_items, R_CONSOLE_FONT},
128 /* Dump contents of resource */
129 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
131 URES *res = (URES *)reshdr;
135 printf(_("No record for %d %s\n"), type, res_to_str(type));
138 if (type < 0) { /* no recursion */
144 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
145 res->dir_res.address, res->dir_res.DIRport);
148 printf(_("Console: name=%s\n"), reshdr->name);
151 printf(_("ConsoleFont: name=%s font face=%s\n"),
152 reshdr->name, NPRT(res->con_font.fontface));
155 printf(_("Unknown resource type %d\n"), type);
157 if (recurse && res->dir_res.hdr.next) {
158 dump_resource(type, res->dir_res.hdr.next, sendit, sock);
163 * Free memory of resource.
164 * NB, we don't need to worry about freeing any references
165 * to other resources as they will be freed when that
166 * resource chain is traversed. Mainly we worry about freeing
167 * allocated strings (names).
169 void free_resource(RES *sres, int type)
172 URES *res = (URES *)sres;
177 /* common stuff -- free the resource name */
178 nres = (RES *)res->dir_res.hdr.next;
179 if (res->dir_res.hdr.name) {
180 free(res->dir_res.hdr.name);
182 if (res->dir_res.hdr.desc) {
183 free(res->dir_res.hdr.desc);
188 if (res->dir_res.address) {
189 free(res->dir_res.address);
191 if (res->dir_res.tls_ctx) {
192 free_tls_context(res->dir_res.tls_ctx);
194 if (res->dir_res.tls_ca_certfile) {
195 free(res->dir_res.tls_ca_certfile);
197 if (res->dir_res.tls_ca_certdir) {
198 free(res->dir_res.tls_ca_certdir);
200 if (res->dir_res.tls_certfile) {
201 free(res->dir_res.tls_certfile);
203 if (res->dir_res.tls_keyfile) {
204 free(res->dir_res.tls_keyfile);
208 if (res->con_res.password) {
209 free(res->con_res.password);
211 if (res->con_res.tls_ctx) {
212 free_tls_context(res->con_res.tls_ctx);
214 if (res->con_res.tls_ca_certfile) {
215 free(res->con_res.tls_ca_certfile);
217 if (res->con_res.tls_ca_certdir) {
218 free(res->con_res.tls_ca_certdir);
220 if (res->con_res.tls_certfile) {
221 free(res->con_res.tls_certfile);
223 if (res->con_res.tls_keyfile) {
224 free(res->con_res.tls_keyfile);
228 if (res->con_font.fontface) {
229 free(res->con_font.fontface);
233 printf(_("Unknown resource type %d\n"), type);
235 /* Common stuff again -- free the resource, recurse to next one */
238 free_resource(nres, type);
242 /* Save the new resource by chaining it into the head list for
243 * the resource. If this is pass 2, we update any resource
244 * pointers (currently only in the Job resource).
246 void save_resource(int type, RES_ITEM *items, int pass)
249 int rindex = type - r_first;
254 * Ensure that all required items are present
256 for (i=0; items[i].name; i++) {
257 if (items[i].flags & ITEM_REQUIRED) {
258 if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {
259 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
260 items[i].name, resources[rindex]);
265 /* During pass 2, we looked up pointers to all the resources
266 * referrenced in the current resource, , now we
267 * must copy their address from the static record to the allocated
272 /* Resources not containing a resource */
281 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
285 /* Note, the resoure name was already saved during pass 1,
286 * so here, we can just release it.
288 if (res_all.dir_res.hdr.name) {
289 free(res_all.dir_res.hdr.name);
290 res_all.dir_res.hdr.name = NULL;
292 if (res_all.dir_res.hdr.desc) {
293 free(res_all.dir_res.hdr.desc);
294 res_all.dir_res.hdr.desc = NULL;
299 /* The following code is only executed during pass 1 */
302 size = sizeof(DIRRES);
305 size = sizeof(CONFONTRES);
308 size = sizeof(CONRES);
311 printf(_("Unknown resource type %d\n"), type);
317 res = (URES *)malloc(size);
318 memcpy(res, &res_all, size);
319 if (!res_head[rindex]) {
320 res_head[rindex] = (RES *)res; /* store first entry */
323 /* Add new res to end of chain */
324 for (last=next=res_head[rindex]; next; next=next->next) {
326 if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
327 Emsg2(M_ERROR_TERM, 0,
328 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
329 resources[rindex].name, res->dir_res.hdr.name);
332 last->next = (RES *)res;
333 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
334 res->dir_res.hdr.name);