2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2008 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
29 * Main configuration file parser for Bacula User Agent
30 * some parts may be split into separate files such as
31 * the schedule configuration (sch_config.c).
33 * Note, the configuration file parser consists of three parts
35 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
37 * 2. The generic config scanner in lib/parse_config.c and
39 * These files contain the parser code, some utility
40 * routines, and the common store routines (name, int,
43 * 3. The daemon specific file, which contains the Resource
44 * definitions as well as any specific store routines
45 * for the resource records.
47 * Kern Sibbald, January MM, September MM
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 int32_t r_first = R_FIRST;
60 int32_t r_last = R_LAST;
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 extern "C" URES res_all; /* visual c mangles variable names */
77 int32_t res_all_size = sizeof(res_all);
79 /* Definition of records permitted within each
80 * resource with the routine to process the record
83 static RES_ITEM dir_items[] = {
84 {"name", store_name, ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
85 {"description", store_str, ITEM(dir_res.hdr.desc), 0, 0, 0},
86 {"dirport", store_pint32, ITEM(dir_res.DIRport), 0, ITEM_DEFAULT, 9101},
87 {"address", store_str, ITEM(dir_res.address), 0, ITEM_REQUIRED, 0},
88 {"password", store_password, ITEM(dir_res.password), 0, 0, 0},
89 {"tlsauthenticate",store_bool, ITEM(dir_res.tls_authenticate), 0, 0, 0},
90 {"tlsenable", store_bool, ITEM(dir_res.tls_enable), 0, 0, 0},
91 {"tlsrequire", store_bool, ITEM(dir_res.tls_require), 0, 0, 0},
92 {"tlscacertificatefile", store_dir, ITEM(dir_res.tls_ca_certfile), 0, 0, 0},
93 {"tlscacertificatedir", store_dir, ITEM(dir_res.tls_ca_certdir), 0, 0, 0},
94 {"tlscertificate", store_dir, ITEM(dir_res.tls_certfile), 0, 0, 0},
95 {"tlskey", store_dir, ITEM(dir_res.tls_keyfile), 0, 0, 0},
96 {"heartbeatinterval", store_time, ITEM(dir_res.heartbeat_interval), 0, ITEM_DEFAULT, 0},
97 {NULL, NULL, {0}, 0, 0, 0}
100 static RES_ITEM con_items[] = {
101 {"name", store_name, ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
102 {"description", store_str, ITEM(con_res.hdr.desc), 0, 0, 0},
103 {"password", store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
104 {"tlsauthenticate",store_bool, ITEM(con_res.tls_authenticate), 0, 0, 0},
105 {"tlsenable", store_bool, ITEM(con_res.tls_enable), 0, 0, 0},
106 {"tlsrequire", store_bool, ITEM(con_res.tls_require), 0, 0, 0},
107 {"tlscacertificatefile", store_dir, ITEM(con_res.tls_ca_certfile), 0, 0, 0},
108 {"tlscacertificatedir", store_dir, ITEM(con_res.tls_ca_certdir), 0, 0, 0},
109 {"tlscertificate", store_dir, ITEM(con_res.tls_certfile), 0, 0, 0},
110 {"tlskey", store_dir, ITEM(con_res.tls_keyfile), 0, 0, 0},
111 {"heartbeatinterval", store_time, ITEM(con_res.heartbeat_interval), 0, ITEM_DEFAULT, 0},
112 {NULL, NULL, {0}, 0, 0, 0}
115 static RES_ITEM con_font_items[] = {
116 {"name", store_name, ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
117 {"description", store_str, ITEM(con_font.hdr.desc), 0, 0, 0},
118 {"font", store_str, ITEM(con_font.fontface), 0, 0, 0},
119 {NULL, NULL, {0}, 0, 0, 0}
124 * This is the master resource definition.
125 * It must have one item for each of the resources.
127 RES_TABLE resources[] = {
128 {"director", dir_items, R_DIRECTOR},
129 {"console", con_items, R_CONSOLE},
130 {"consolefont", con_font_items, R_CONSOLE_FONT},
135 /* Dump contents of resource */
136 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
138 URES *res = (URES *)reshdr;
142 printf(_("No record for %d %s\n"), type, res_to_str(type));
145 if (type < 0) { /* no recursion */
151 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
152 res->dir_res.address, res->dir_res.DIRport);
155 printf(_("Console: name=%s\n"), reshdr->name);
158 printf(_("ConsoleFont: name=%s font face=%s\n"),
159 reshdr->name, NPRT(res->con_font.fontface));
162 printf(_("Unknown resource type %d\n"), type);
164 if (recurse && res->dir_res.hdr.next) {
165 dump_resource(type, res->dir_res.hdr.next, sendit, sock);
170 * Free memory of resource.
171 * NB, we don't need to worry about freeing any references
172 * to other resources as they will be freed when that
173 * resource chain is traversed. Mainly we worry about freeing
174 * allocated strings (names).
176 void free_resource(RES *sres, int type)
179 URES *res = (URES *)sres;
184 /* common stuff -- free the resource name */
185 nres = (RES *)res->dir_res.hdr.next;
186 if (res->dir_res.hdr.name) {
187 free(res->dir_res.hdr.name);
189 if (res->dir_res.hdr.desc) {
190 free(res->dir_res.hdr.desc);
195 if (res->dir_res.address) {
196 free(res->dir_res.address);
198 if (res->dir_res.tls_ctx) {
199 free_tls_context(res->dir_res.tls_ctx);
201 if (res->dir_res.tls_ca_certfile) {
202 free(res->dir_res.tls_ca_certfile);
204 if (res->dir_res.tls_ca_certdir) {
205 free(res->dir_res.tls_ca_certdir);
207 if (res->dir_res.tls_certfile) {
208 free(res->dir_res.tls_certfile);
210 if (res->dir_res.tls_keyfile) {
211 free(res->dir_res.tls_keyfile);
215 if (res->con_res.password) {
216 free(res->con_res.password);
218 if (res->con_res.tls_ctx) {
219 free_tls_context(res->con_res.tls_ctx);
221 if (res->con_res.tls_ca_certfile) {
222 free(res->con_res.tls_ca_certfile);
224 if (res->con_res.tls_ca_certdir) {
225 free(res->con_res.tls_ca_certdir);
227 if (res->con_res.tls_certfile) {
228 free(res->con_res.tls_certfile);
230 if (res->con_res.tls_keyfile) {
231 free(res->con_res.tls_keyfile);
235 if (res->con_font.fontface) {
236 free(res->con_font.fontface);
240 printf(_("Unknown resource type %d\n"), type);
242 /* Common stuff again -- free the resource, recurse to next one */
245 free_resource(nres, type);
249 /* Save the new resource by chaining it into the head list for
250 * the resource. If this is pass 2, we update any resource
251 * pointers (currently only in the Job resource).
253 void save_resource(int type, RES_ITEM *items, int pass)
256 int rindex = type - r_first;
261 * Ensure that all required items are present
263 for (i=0; items[i].name; i++) {
264 if (items[i].flags & ITEM_REQUIRED) {
265 if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {
266 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
267 items[i].name, resources[rindex]);
272 /* During pass 2, we looked up pointers to all the resources
273 * referrenced in the current resource, , now we
274 * must copy their address from the static record to the allocated
279 /* Resources not containing a resource */
288 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
292 /* Note, the resoure name was already saved during pass 1,
293 * so here, we can just release it.
295 if (res_all.dir_res.hdr.name) {
296 free(res_all.dir_res.hdr.name);
297 res_all.dir_res.hdr.name = NULL;
299 if (res_all.dir_res.hdr.desc) {
300 free(res_all.dir_res.hdr.desc);
301 res_all.dir_res.hdr.desc = NULL;
306 /* The following code is only executed during pass 1 */
309 size = sizeof(DIRRES);
312 size = sizeof(CONFONTRES);
315 size = sizeof(CONRES);
318 printf(_("Unknown resource type %d\n"), type);
324 res = (URES *)malloc(size);
325 memcpy(res, &res_all, size);
326 if (!res_head[rindex]) {
327 res_head[rindex] = (RES *)res; /* store first entry */
330 /* Add new res to end of chain */
331 for (last=next=res_head[rindex]; next; next=next->next) {
333 if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
334 Emsg2(M_ERROR_TERM, 0,
335 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
336 resources[rindex].name, res->dir_res.hdr.name);
339 last->next = (RES *)res;
340 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
341 res->dir_res.hdr.name);
346 bool parse_bat_config(CONFIG *config, const char *configfile, int exit_code)
348 config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
349 r_first, r_last, resources, res_head);
350 return config->parse_config();