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
60 #if defined(HAVE_WIN32) && !defined(HAVE_CYGWIN)
61 extern "C" { // work around visual compiler mangling variables
63 int res_all_size = sizeof(res_all);
67 int res_all_size = sizeof(res_all);
70 /* Definition of records permitted within each
71 * resource with the routine to process the record
75 /* Client or File daemon "Global" resources */
76 static RES_ITEM cli_items[] = {
77 {"name", store_name, ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
78 {"description", store_str, ITEM(res_client.hdr.desc), 0, 0, 0},
79 {"fdport", store_addresses_port, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
80 {"fdaddress", store_addresses_address, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
81 {"fdaddresses", store_addresses, ITEM(res_client.FDaddrs), 0, ITEM_DEFAULT, 9102},
83 {"workingdirectory", store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0},
84 {"piddirectory", store_dir, ITEM(res_client.pid_directory), 0, ITEM_REQUIRED, 0},
85 {"subsysdirectory", store_dir, ITEM(res_client.subsys_directory), 0, 0, 0},
86 {"scriptsdirectory", store_dir, ITEM(res_client.scripts_directory), 0, 0, 0},
87 {"maximumconcurrentjobs", store_pint, ITEM(res_client.MaxConcurrentJobs), 0, ITEM_DEFAULT, 10},
88 {"messages", store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
89 {"heartbeatinterval", store_time, ITEM(res_client.heartbeat_interval), 0, ITEM_DEFAULT, 0},
90 {"sdconnecttimeout", store_time,ITEM(res_client.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
91 {"maximumnetworkbuffersize", store_pint, ITEM(res_client.max_network_buffer_size), 0, 0, 0},
92 {"tlsenable", store_yesno, ITEM(res_client.tls_enable), 0, 0, 0},
93 {"tlsrequire", store_yesno, ITEM(res_client.tls_require), 0, 0, 0},
94 {"tlscacertificatefile", store_dir, ITEM(res_client.tls_ca_certfile), 0, 0, 0},
95 {"tlscacertificatedir", store_dir, ITEM(res_client.tls_ca_certdir), 0, 0, 0},
96 {"tlscertificate", store_dir, ITEM(res_client.tls_certfile), 0, 0, 0},
97 {"tlskey", store_dir, ITEM(res_client.tls_keyfile), 0, 0, 0},
98 {NULL, NULL, NULL, 0, 0, 0}
101 /* Directors that can use our services */
102 static RES_ITEM dir_items[] = {
103 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
104 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
105 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
106 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
107 {"monitor", store_yesno, ITEM(res_dir.monitor), 1, ITEM_DEFAULT, 0},
108 {"tlsenable", store_yesno, ITEM(res_dir.tls_enable), 0, 0, 0},
109 {"tlsrequire", store_yesno, ITEM(res_dir.tls_require), 0, 0, 0},
110 {"tlsverifypeer", store_yesno, ITEM(res_dir.tls_verify_peer), 1, ITEM_DEFAULT, 1},
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 {"tlsdhfile", store_dir, ITEM(res_dir.tls_dhfile), 0, 0, 0},
116 {"tlsallowedcn", store_alist_str, ITEM(res_dir.tls_allowed_cns), 0, 0, 0},
117 {NULL, NULL, NULL, 0, 0, 0}
120 /* Message resource */
121 extern RES_ITEM msgs_items[];
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 {"filedaemon", cli_items, R_CLIENT},
130 {"client", cli_items, R_CLIENT}, /* alias for filedaemon */
131 {"messages", msgs_items, R_MSGS},
136 /* Dump contents of resource */
137 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
139 URES *res = (URES *)reshdr;
143 sendit(sock, "No record for %d %s\n", type, res_to_str(type));
146 if (type < 0) { /* no recursion */
152 sendit(sock, "Director: name=%s password=%s\n", reshdr->name,
153 res->res_dir.password);
156 sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
157 get_first_port_host_order(res->res_client.FDaddrs));
160 sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
161 if (res->res_msgs.mail_cmd)
162 sendit(sock, " mailcmd=%s\n", res->res_msgs.mail_cmd);
163 if (res->res_msgs.operator_cmd)
164 sendit(sock, " opcmd=%s\n", res->res_msgs.operator_cmd);
167 sendit(sock, "Unknown resource type %d\n", type);
169 if (recurse && res->res_dir.hdr.next)
170 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
174 * Free memory of resource.
175 * NB, we don't need to worry about freeing any references
176 * to other resources as they will be freed when that
177 * resource chain is traversed. Mainly we worry about freeing
178 * allocated strings (names).
180 void free_resource(RES *sres, int type)
183 URES *res = (URES *)sres;
189 /* common stuff -- free the resource name */
190 nres = (RES *)res->res_dir.hdr.next;
191 if (res->res_dir.hdr.name) {
192 free(res->res_dir.hdr.name);
194 if (res->res_dir.hdr.desc) {
195 free(res->res_dir.hdr.desc);
199 if (res->res_dir.password) {
200 free(res->res_dir.password);
202 if (res->res_dir.address) {
203 free(res->res_dir.address);
205 if (res->res_dir.tls_ctx) {
206 free_tls_context(res->res_dir.tls_ctx);
208 if (res->res_dir.tls_ca_certfile) {
209 free(res->res_dir.tls_ca_certfile);
211 if (res->res_dir.tls_ca_certdir) {
212 free(res->res_dir.tls_ca_certdir);
214 if (res->res_dir.tls_certfile) {
215 free(res->res_dir.tls_certfile);
217 if (res->res_dir.tls_keyfile) {
218 free(res->res_dir.tls_keyfile);
220 if (res->res_dir.tls_dhfile) {
221 free(res->res_dir.tls_dhfile);
223 if (res->res_dir.tls_allowed_cns) {
224 delete res->res_dir.tls_allowed_cns;
228 if (res->res_client.working_directory) {
229 free(res->res_client.working_directory);
231 if (res->res_client.pid_directory) {
232 free(res->res_client.pid_directory);
234 if (res->res_client.subsys_directory) {
235 free(res->res_client.subsys_directory);
237 if (res->res_client.scripts_directory) {
238 free(res->res_client.scripts_directory);
240 if (res->res_client.FDaddrs) {
241 free_addresses(res->res_client.FDaddrs);
243 if (res->res_client.tls_ctx) {
244 free_tls_context(res->res_client.tls_ctx);
246 if (res->res_client.tls_ca_certfile) {
247 free(res->res_client.tls_ca_certfile);
249 if (res->res_client.tls_ca_certdir) {
250 free(res->res_client.tls_ca_certdir);
252 if (res->res_client.tls_certfile) {
253 free(res->res_client.tls_certfile);
255 if (res->res_client.tls_keyfile) {
256 free(res->res_client.tls_keyfile);
260 if (res->res_msgs.mail_cmd)
261 free(res->res_msgs.mail_cmd);
262 if (res->res_msgs.operator_cmd)
263 free(res->res_msgs.operator_cmd);
264 free_msgs_res((MSGS *)res); /* free message resource */
268 printf(_("Unknown resource type %d\n"), type);
270 /* Common stuff again -- free the resource, recurse to next one */
275 free_resource(nres, type);
279 /* Save the new resource by chaining it into the head list for
280 * the resource. If this is pass 2, we update any resource
281 * pointers (currently only in the Job resource).
283 void save_resource(int type, RES_ITEM *items, int pass)
286 int rindex = type - r_first;
291 * Ensure that all required items are present
293 for (i=0; items[i].name; i++) {
294 if (items[i].flags & ITEM_REQUIRED) {
295 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
296 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
297 items[i].name, resources[rindex]);
302 /* During pass 2, we looked up pointers to all the resources
303 * referrenced in the current resource, , now we
304 * must copy their address from the static record to the allocated
309 /* Resources not containing a resource */
313 /* Resources containing another resource */
315 if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
316 Emsg1(M_ABORT, 0, _("Cannot find Director resource %s\n"), res_all.res_dir.hdr.name);
318 res->res_dir.tls_allowed_cns = res_all.res_dir.tls_allowed_cns;
321 if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
322 Emsg1(M_ABORT, 0, _("Cannot find Client resource %s\n"), res_all.res_dir.hdr.name);
324 res->res_client.messages = res_all.res_client.messages;
327 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
331 /* Note, the resoure name was already saved during pass 1,
332 * so here, we can just release it.
334 if (res_all.res_dir.hdr.name) {
335 free(res_all.res_dir.hdr.name);
336 res_all.res_dir.hdr.name = NULL;
338 if (res_all.res_dir.hdr.desc) {
339 free(res_all.res_dir.hdr.desc);
340 res_all.res_dir.hdr.desc = NULL;
345 /* The following code is only executed on pass 1 */
348 size = sizeof(DIRRES);
351 size = sizeof(CLIENT);
357 printf(_("Unknown resource type %d\n"), type);
364 res = (URES *)malloc(size);
365 memcpy(res, &res_all, size);
366 if (!res_head[rindex]) {
367 res_head[rindex] = (RES *)res; /* store first entry */
370 /* Add new res to end of chain */
371 for (next=res_head[rindex]; next->next; next=next->next) {
372 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
373 Emsg2(M_ERROR_TERM, 0,
374 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
375 resources[rindex].name, res->res_dir.hdr.name);
378 next->next = (RES *)res;
379 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
380 res->res_dir.hdr.name);