2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2009 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 three of the GNU Affero 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 Affero 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
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 int32_t r_first = R_FIRST;
58 int32_t r_last = R_LAST;
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 int32_t 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 {"tlsauthenticate",store_bool, ITEM(res_cons.tls_authenticate), 0, 0, 0},
92 {"tlsenable", store_bool, ITEM(res_cons.tls_enable), 0, 0, 0},
93 {"tlsrequire", store_bool, ITEM(res_cons.tls_require), 0, 0, 0},
94 {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
95 {"tlscacertificatedir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
96 {"tlscertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
97 {"tlskey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
98 {"director", store_str, ITEM(res_cons.director), 0, 0, 0},
99 {"heartbeatinterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 0},
100 {NULL, NULL, {0}, 0, 0, 0}
104 /* Director's that we can contact */
105 static RES_ITEM dir_items[] = {
106 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
107 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
108 {"dirport", store_pint32, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
109 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
110 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
111 {"tlsauthenticate",store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
112 {"tlsenable", store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
113 {"tlsrequire", store_bool, ITEM(res_dir.tls_require), 0, 0, 0},
114 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
115 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
116 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
117 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
118 {"heartbeatinterval", store_time, ITEM(res_dir.heartbeat_interval), 0, ITEM_DEFAULT, 0},
119 {NULL, NULL, {0}, 0, 0, 0}
123 * This is the master resource definition.
124 * It must have one item for each of the resources.
126 RES_TABLE resources[] = {
127 {"console", cons_items, R_CONSOLE},
128 {"director", dir_items, R_DIRECTOR},
133 /* Dump contents of resource */
134 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
136 URES *res = (URES *)reshdr;
140 printf(_("No record for %d %s\n"), type, res_to_str(type));
143 if (type < 0) { /* no recursion */
149 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
150 res->res_cons.rc_file, res->res_cons.hist_file);
153 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
154 res->res_dir.address, res->res_dir.DIRport);
157 printf(_("Unknown resource type %d\n"), type);
159 if (recurse && res->res_dir.hdr.next) {
160 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
165 * Free memory of resource.
166 * NB, we don't need to worry about freeing any references
167 * to other resources as they will be freed when that
168 * resource chain is traversed. Mainly we worry about freeing
169 * allocated strings (names).
171 void free_resource(RES *sres, int type)
174 URES *res = (URES *)sres;
179 /* common stuff -- free the resource name */
180 nres = (RES *)res->res_dir.hdr.next;
181 if (res->res_dir.hdr.name) {
182 free(res->res_dir.hdr.name);
184 if (res->res_dir.hdr.desc) {
185 free(res->res_dir.hdr.desc);
190 if (res->res_cons.rc_file) {
191 free(res->res_cons.rc_file);
193 if (res->res_cons.hist_file) {
194 free(res->res_cons.hist_file);
196 if (res->res_cons.tls_ctx) {
197 free_tls_context(res->res_cons.tls_ctx);
199 if (res->res_cons.tls_ca_certfile) {
200 free(res->res_cons.tls_ca_certfile);
202 if (res->res_cons.tls_ca_certdir) {
203 free(res->res_cons.tls_ca_certdir);
205 if (res->res_cons.tls_certfile) {
206 free(res->res_cons.tls_certfile);
208 if (res->res_cons.tls_keyfile) {
209 free(res->res_cons.tls_keyfile);
213 if (res->res_dir.address) {
214 free(res->res_dir.address);
216 if (res->res_dir.tls_ctx) {
217 free_tls_context(res->res_dir.tls_ctx);
219 if (res->res_dir.tls_ca_certfile) {
220 free(res->res_dir.tls_ca_certfile);
222 if (res->res_dir.tls_ca_certdir) {
223 free(res->res_dir.tls_ca_certdir);
225 if (res->res_dir.tls_certfile) {
226 free(res->res_dir.tls_certfile);
228 if (res->res_dir.tls_keyfile) {
229 free(res->res_dir.tls_keyfile);
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.res_dir.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 */
278 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
282 /* Note, the resoure name was already saved during pass 1,
283 * so here, we can just release it.
285 if (res_all.res_dir.hdr.name) {
286 free(res_all.res_dir.hdr.name);
287 res_all.res_dir.hdr.name = NULL;
289 if (res_all.res_dir.hdr.desc) {
290 free(res_all.res_dir.hdr.desc);
291 res_all.res_dir.hdr.desc = NULL;
296 /* The following code is only executed during pass 1 */
299 size = sizeof(CONRES);
302 size = sizeof(DIRRES);
305 printf(_("Unknown resource type %d\n"), type);
312 res = (URES *)malloc(size);
313 memcpy(res, &res_all, size);
314 if (!res_head[rindex]) {
315 res_head[rindex] = (RES *)res; /* store first entry */
318 for (last=next=res_head[rindex]; next; next=next->next) {
320 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
321 Emsg2(M_ERROR_TERM, 0,
322 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
323 resources[rindex].name, res->res_dir.hdr.name);
326 last->next = (RES *)res;
327 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
328 res->res_dir.hdr.name);
333 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
335 config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
336 r_first, r_last, resources, res_head);
337 return config->parse_config();