2 * Main configuration file parser for Bacula File Daemon (Client)
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, September MM
25 Copyright (C) 2000-2005 Kern Sibbald
27 This program is free software; you can redistribute it and/or
28 modify it under the terms of the GNU General Public License
29 version 2 as amended with additional clauses defined in the
30 file LICENSE in the main source directory.
32 This program is distributed in the hope that it will be useful,
33 but WITHOUT ANY WARRANTY; without even the implied warranty of
34 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 the file LICENSE for additional details.
42 /* Define the first and last resource ID record
43 * types. Note, these should be unique for each
44 * daemon though not a requirement.
46 int r_first = R_FIRST;
48 static RES *sres_head[R_LAST - R_FIRST + 1];
49 RES **res_head = sres_head;
52 /* Forward referenced subroutines */
55 /* We build the current resource here as we are
56 * scanning the resource configuration definition,
57 * then move it to allocated memory when the resource
61 extern "C" { // work around visual compiler mangling variables
67 int res_all_size = sizeof(res_all);
69 /* Definition of records permitted within each
70 * resource with the routine to process the record
74 /* Client or File daemon "Global" resources */
75 static RES_ITEM cli_items[] = {
76 {"name", store_name, ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
77 {"description", store_str, ITEM(res_client.hdr.desc), 0, 0, 0},
78 {"fdport", store_addresses_port, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
79 {"fdaddress", store_addresses_address, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
80 {"fdaddresses", store_addresses, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
82 {"workingdirectory", store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0},
83 {"piddirectory", store_dir, ITEM(res_client.pid_directory), 0, ITEM_REQUIRED, 0},
84 {"subsysdirectory", store_dir, ITEM(res_client.subsys_directory), 0, 0, 0},
85 {"scriptsdirectory", store_dir, ITEM(res_client.scripts_directory), 0, 0, 0},
86 {"maximumconcurrentjobs", store_pint, ITEM(res_client.MaxConcurrentJobs), 0, ITEM_DEFAULT, 10},
87 {"messages", store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
88 {"heartbeatinterval", store_time, ITEM(res_client.heartbeat_interval), 0, ITEM_DEFAULT, 0},
89 {"sdconnecttimeout", store_time,ITEM(res_client.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
90 {"maximumnetworkbuffersize", store_pint, ITEM(res_client.max_network_buffer_size), 0, 0, 0},
91 {"pkisignatures", store_bool, ITEM(res_client.pki_sign), 0, ITEM_DEFAULT, 0},
92 {"pkiencryption", store_bool, ITEM(res_client.pki_encrypt), 0, ITEM_DEFAULT, 0},
93 {"pkikeypair", store_dir, ITEM(res_client.pki_keypair_file), 0, 0, 0},
94 {"pkisigner", store_alist_str, ITEM(res_client.pki_signing_key_files), 0, 0, 0},
95 {"pkimasterkey", store_alist_str, ITEM(res_client.pki_master_key_files), 0, 0, 0},
96 {"tlsenable", store_bool, ITEM(res_client.tls_enable), 0, 0, 0},
97 {"tlsrequire", store_bool, ITEM(res_client.tls_require), 0, 0, 0},
98 {"tlscacertificatefile", store_dir, ITEM(res_client.tls_ca_certfile), 0, 0, 0},
99 {"tlscacertificatedir", store_dir, ITEM(res_client.tls_ca_certdir), 0, 0, 0},
100 {"tlscertificate", store_dir, ITEM(res_client.tls_certfile), 0, 0, 0},
101 {"tlskey", store_dir, ITEM(res_client.tls_keyfile), 0, 0, 0},
102 {NULL, NULL, {0}, 0, 0, 0}
105 /* Directors that can use our services */
106 static RES_ITEM dir_items[] = {
107 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
108 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
109 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
110 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
111 {"monitor", store_bool, ITEM(res_dir.monitor), 0, ITEM_DEFAULT, 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 {"tlsverifypeer", store_bool, ITEM(res_dir.tls_verify_peer), 0, ITEM_DEFAULT, 1},
115 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
116 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
117 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
118 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
119 {"tlsdhfile", store_dir, ITEM(res_dir.tls_dhfile), 0, 0, 0},
120 {"tlsallowedcn", store_alist_str, ITEM(res_dir.tls_allowed_cns), 0, 0, 0},
121 {NULL, NULL, {0}, 0, 0, 0}
124 /* Message resource */
125 extern RES_ITEM msgs_items[];
128 * This is the master resource definition.
129 * It must have one item for each of the resources.
131 RES_TABLE resources[] = {
132 {"director", dir_items, R_DIRECTOR},
133 {"filedaemon", cli_items, R_CLIENT},
134 {"client", cli_items, R_CLIENT}, /* alias for filedaemon */
135 {"messages", msgs_items, R_MSGS},
140 /* Dump contents of resource */
141 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
143 URES *res = (URES *)reshdr;
147 sendit(sock, "No record for %d %s\n", type, res_to_str(type));
150 if (type < 0) { /* no recursion */
156 sendit(sock, "Director: name=%s password=%s\n", reshdr->name,
157 res->res_dir.password);
160 sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
161 get_first_port_host_order(res->res_client.FDaddrs));
164 sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
165 if (res->res_msgs.mail_cmd)
166 sendit(sock, " mailcmd=%s\n", res->res_msgs.mail_cmd);
167 if (res->res_msgs.operator_cmd)
168 sendit(sock, " opcmd=%s\n", res->res_msgs.operator_cmd);
171 sendit(sock, "Unknown resource type %d\n", type);
173 if (recurse && res->res_dir.hdr.next)
174 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
178 * Free memory of resource.
179 * NB, we don't need to worry about freeing any references
180 * to other resources as they will be freed when that
181 * resource chain is traversed. Mainly we worry about freeing
182 * allocated strings (names).
184 void free_resource(RES *sres, int type)
187 URES *res = (URES *)sres;
193 /* common stuff -- free the resource name */
194 nres = (RES *)res->res_dir.hdr.next;
195 if (res->res_dir.hdr.name) {
196 free(res->res_dir.hdr.name);
198 if (res->res_dir.hdr.desc) {
199 free(res->res_dir.hdr.desc);
203 if (res->res_dir.password) {
204 free(res->res_dir.password);
206 if (res->res_dir.address) {
207 free(res->res_dir.address);
209 if (res->res_dir.tls_ctx) {
210 free_tls_context(res->res_dir.tls_ctx);
212 if (res->res_dir.tls_ca_certfile) {
213 free(res->res_dir.tls_ca_certfile);
215 if (res->res_dir.tls_ca_certdir) {
216 free(res->res_dir.tls_ca_certdir);
218 if (res->res_dir.tls_certfile) {
219 free(res->res_dir.tls_certfile);
221 if (res->res_dir.tls_keyfile) {
222 free(res->res_dir.tls_keyfile);
224 if (res->res_dir.tls_dhfile) {
225 free(res->res_dir.tls_dhfile);
227 if (res->res_dir.tls_allowed_cns) {
228 delete res->res_dir.tls_allowed_cns;
232 if (res->res_client.working_directory) {
233 free(res->res_client.working_directory);
235 if (res->res_client.pid_directory) {
236 free(res->res_client.pid_directory);
238 if (res->res_client.subsys_directory) {
239 free(res->res_client.subsys_directory);
241 if (res->res_client.scripts_directory) {
242 free(res->res_client.scripts_directory);
244 if (res->res_client.FDaddrs) {
245 free_addresses(res->res_client.FDaddrs);
248 if (res->res_client.pki_keypair_file) {
249 free(res->res_client.pki_keypair_file);
251 if (res->res_client.pki_keypair) {
252 crypto_keypair_free(res->res_client.pki_keypair);
255 if (res->res_client.pki_signing_key_files) {
256 delete res->res_client.pki_signing_key_files;
258 if (res->res_client.pki_signers) {
259 X509_KEYPAIR *keypair;
260 foreach_alist(keypair, res->res_client.pki_signers) {
261 crypto_keypair_free(keypair);
263 delete res->res_client.pki_signers;
266 if (res->res_client.pki_master_key_files) {
267 delete res->res_client.pki_master_key_files;
270 if (res->res_client.pki_recipients) {
271 X509_KEYPAIR *keypair;
272 foreach_alist(keypair, res->res_client.pki_recipients) {
273 crypto_keypair_free(keypair);
275 delete res->res_client.pki_recipients;
278 if (res->res_client.tls_ctx) {
279 free_tls_context(res->res_client.tls_ctx);
281 if (res->res_client.tls_ca_certfile) {
282 free(res->res_client.tls_ca_certfile);
284 if (res->res_client.tls_ca_certdir) {
285 free(res->res_client.tls_ca_certdir);
287 if (res->res_client.tls_certfile) {
288 free(res->res_client.tls_certfile);
290 if (res->res_client.tls_keyfile) {
291 free(res->res_client.tls_keyfile);
295 if (res->res_msgs.mail_cmd)
296 free(res->res_msgs.mail_cmd);
297 if (res->res_msgs.operator_cmd)
298 free(res->res_msgs.operator_cmd);
299 free_msgs_res((MSGS *)res); /* free message resource */
303 printf(_("Unknown resource type %d\n"), type);
305 /* Common stuff again -- free the resource, recurse to next one */
310 free_resource(nres, type);
314 /* Save the new resource by chaining it into the head list for
315 * the resource. If this is pass 2, we update any resource
316 * pointers (currently only in the Job resource).
318 void save_resource(int type, RES_ITEM *items, int pass)
321 int rindex = type - r_first;
326 * Ensure that all required items are present
328 for (i=0; items[i].name; i++) {
329 if (items[i].flags & ITEM_REQUIRED) {
330 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
331 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
332 items[i].name, resources[rindex]);
337 /* During pass 2, we looked up pointers to all the resources
338 * referrenced in the current resource, , now we
339 * must copy their address from the static record to the allocated
344 /* Resources not containing a resource */
348 /* Resources containing another resource */
350 if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
351 Emsg1(M_ABORT, 0, _("Cannot find Director resource %s\n"), res_all.res_dir.hdr.name);
353 res->res_dir.tls_allowed_cns = res_all.res_dir.tls_allowed_cns;
356 if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
357 Emsg1(M_ABORT, 0, _("Cannot find Client resource %s\n"), res_all.res_dir.hdr.name);
359 res->res_client.pki_signing_key_files = res_all.res_client.pki_signing_key_files;
360 res->res_client.pki_master_key_files = res_all.res_client.pki_master_key_files;
362 res->res_client.pki_signers = res_all.res_client.pki_signers;
363 res->res_client.pki_recipients = res_all.res_client.pki_recipients;
365 res->res_client.messages = res_all.res_client.messages;
368 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
372 /* Note, the resoure name was already saved during pass 1,
373 * so here, we can just release it.
375 if (res_all.res_dir.hdr.name) {
376 free(res_all.res_dir.hdr.name);
377 res_all.res_dir.hdr.name = NULL;
379 if (res_all.res_dir.hdr.desc) {
380 free(res_all.res_dir.hdr.desc);
381 res_all.res_dir.hdr.desc = NULL;
386 /* The following code is only executed on pass 1 */
389 size = sizeof(DIRRES);
392 size = sizeof(CLIENT);
398 printf(_("Unknown resource type %d\n"), type);
405 res = (URES *)malloc(size);
406 memcpy(res, &res_all, size);
407 if (!res_head[rindex]) {
408 res_head[rindex] = (RES *)res; /* store first entry */
411 /* Add new res to end of chain */
412 for (next=res_head[rindex]; next->next; next=next->next) {
413 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
414 Emsg2(M_ERROR_TERM, 0,
415 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
416 resources[rindex].name, res->res_dir.hdr.name);
419 next->next = (RES *)res;
420 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
421 res->res_dir.hdr.name);