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
24 Copyright (C) 2000-2004 Kern Sibbald and John Walker
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License
28 as published by the Free Software Foundation; either version 2
29 of the License, or (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include "console_conf.h"
44 /* Define the first and last resource ID record
45 * types. Note, these should be unique for each
46 * daemon though not a requirement.
48 int r_first = R_FIRST;
50 static RES *sres_head[R_LAST - R_FIRST + 1];
51 RES **res_head = sres_head;
53 /* Forward referenced subroutines */
56 /* We build the current resource here as we are
57 * scanning the resource configuration definition,
58 * then move it to allocated memory when the resource
61 #if defined(HAVE_WIN32) && !defined(HAVE_CYGWIN) && !defined(HAVE_MINGW)
62 extern "C" { // work around visual compiler mangling variables
64 int res_all_size = sizeof(res_all);
68 int res_all_size = sizeof(res_all);
71 /* Definition of records permitted within each
72 * resource with the routine to process the record
76 /* Console "globals" */
77 static RES_ITEM cons_items[] = {
78 {"name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
79 {"description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
80 {"rcfile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
81 {"historyfile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
82 {"password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
84 {"tlsenable", store_yesno, ITEM(res_cons.tls_enable), 1, ITEM_DEFAULT, 0},
85 {"tlsrequire", store_yesno, ITEM(res_cons.tls_require), 1, ITEM_DEFAULT, 0},
86 {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
87 {"tlscacertificatedir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
88 {"tlscertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
89 {"tlskey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
91 {NULL, NULL, NULL, 0, 0, 0}
95 /* Director's that we can contact */
96 static RES_ITEM dir_items[] = {
97 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
98 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
99 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
100 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
101 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
103 {"tlsenable", store_yesno, ITEM(res_dir.tls_enable), 1, ITEM_DEFAULT, 0},
104 {"tlsrequire", store_yesno, ITEM(res_dir.tls_require), 1, ITEM_DEFAULT, 0},
105 {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
106 {"tlscacertificatedir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
107 {"tlscertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
108 {"tlskey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
110 {NULL, NULL, NULL, 0, 0, 0}
114 * This is the master resource definition.
115 * It must have one item for each of the resources.
117 RES_TABLE resources[] = {
118 {"console", cons_items, R_CONSOLE},
119 {"director", dir_items, R_DIRECTOR},
124 /* Dump contents of resource */
125 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
127 URES *res = (URES *)reshdr;
131 printf("No record for %d %s\n", type, res_to_str(type));
134 if (type < 0) { /* no recursion */
140 printf("Console: name=%s rcfile=%s histfile=%s\n", reshdr->name,
141 res->res_cons.rc_file, res->res_cons.hist_file);
144 printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name,
145 res->res_dir.address, res->res_dir.DIRport);
148 printf("Unknown resource type %d\n", type);
150 if (recurse && res->res_dir.hdr.next) {
151 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
156 * Free memory of resource.
157 * NB, we don't need to worry about freeing any references
158 * to other resources as they will be freed when that
159 * resource chain is traversed. Mainly we worry about freeing
160 * allocated strings (names).
162 void free_resource(RES *sres, int type)
165 URES *res = (URES *)sres;
170 /* common stuff -- free the resource name */
171 nres = (RES *)res->res_dir.hdr.next;
172 if (res->res_dir.hdr.name) {
173 free(res->res_dir.hdr.name);
175 if (res->res_dir.hdr.desc) {
176 free(res->res_dir.hdr.desc);
181 if (res->res_cons.rc_file) {
182 free(res->res_cons.rc_file);
184 if (res->res_cons.hist_file) {
185 free(res->res_cons.hist_file);
188 if (res->res_cons.tls_ctx) {
189 free_tls_context(res->res_cons.tls_ctx);
191 if (res->res_cons.tls_ca_certfile) {
192 free(res->res_cons.tls_ca_certfile);
194 if (res->res_cons.tls_ca_certdir) {
195 free(res->res_cons.tls_ca_certdir);
197 if (res->res_cons.tls_certfile) {
198 free(res->res_cons.tls_certfile);
200 if (res->res_cons.tls_keyfile) {
201 free(res->res_cons.tls_keyfile);
203 #endif /* HAVE_TLS */
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 #endif /* HAVE_TLS */
227 printf("Unknown resource type %d\n", type);
229 /* Common stuff again -- free the resource, recurse to next one */
232 free_resource(nres, type);
236 /* Save the new resource by chaining it into the head list for
237 * the resource. If this is pass 2, we update any resource
238 * pointers (currently only in the Job resource).
240 void save_resource(int type, RES_ITEM *items, int pass)
243 int rindex = type - r_first;
248 * Ensure that all required items are present
250 for (i=0; items[i].name; i++) {
251 if (items[i].flags & ITEM_REQUIRED) {
252 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
253 Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
254 items[i].name, resources[rindex]);
259 /* During pass 2, we looked up pointers to all the resources
260 * referrenced in the current resource, , now we
261 * must copy their address from the static record to the allocated
266 /* Resources not containing a resource */
272 Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
276 /* Note, the resoure name was already saved during pass 1,
277 * so here, we can just release it.
279 if (res_all.res_dir.hdr.name) {
280 free(res_all.res_dir.hdr.name);
281 res_all.res_dir.hdr.name = NULL;
283 if (res_all.res_dir.hdr.desc) {
284 free(res_all.res_dir.hdr.desc);
285 res_all.res_dir.hdr.desc = NULL;
290 /* The following code is only executed during pass 1 */
293 size = sizeof(CONRES);
296 size = sizeof(DIRRES);
299 printf("Unknown resource type %d\n", type);
306 res = (URES *)malloc(size);
307 memcpy(res, &res_all, size);
308 if (!res_head[rindex]) {
309 res_head[rindex] = (RES *)res; /* store first entry */
312 for (next=res_head[rindex]; next->next; next=next->next) {
313 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
314 Emsg2(M_ERROR_TERM, 0,
315 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
316 resources[rindex].name, res->res_dir.hdr.name);
319 next->next = (RES *)res;
320 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
321 res->res_dir.hdr.name);