2 * Master Configuration routines.
4 * This file contains the common parts of the Bacula
5 * configuration routines.
7 * Note, the configuration file parser consists of three parts
9 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
11 * 2. The generic config scanner in lib/parse_conf.c and
13 * These files contain the parser code, some utility
14 * routines, and the common store routines (name, int,
17 * 3. The daemon specific file, which contains the Resource
18 * definitions as well as any specific store routines
19 * for the resource records.
21 * N.B. This is a two pass parser, so if you malloc() a string
22 * in a "store" routine, you must ensure to do it during
23 * only one of the two passes, or to free it between.
24 * Also, note that the resource record is malloced and
25 * saved in save_resource() during pass 1. Anything that
26 * you want saved after pass two (e.g. resource pointers)
27 * must explicitly be done in save_resource. Take a look
28 * at the Job resource in src/dird/dird_conf.c to see how
31 * Kern Sibbald, January MM
37 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
39 This program is free software; you can redistribute it and/or
40 modify it under the terms of the GNU General Public License as
41 published by the Free Software Foundation; either version 2 of
42 the License, or (at your option) any later version.
44 This program is distributed in the hope that it will be useful,
45 but WITHOUT ANY WARRANTY; without even the implied warranty of
46 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
47 General Public License for more details.
49 You should have received a copy of the GNU General Public
50 License along with this program; if not, write to the Free
51 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
59 extern int debug_level;
61 /* Each daemon has a slightly different set of
62 * resources, so it will define the following
67 extern pthread_mutex_t res_mutex;
68 extern struct s_res resources[];
70 extern int res_all_size;
72 static int res_locked = 0; /* set when resource chains locked */
74 /* Forward referenced subroutines */
75 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
78 /* Common Resource definitions */
80 /* Message resource directives
81 * name handler value code flags default_value
83 struct res_items msgs_items[] = {
84 {"name", store_name, ITEM(res_msgs.hdr.name), 0, 0, 0},
85 {"description", store_str, ITEM(res_msgs.hdr.desc), 0, 0, 0},
86 {"mailcommand", store_str, ITEM(res_msgs.mail_cmd), 0, 0, 0},
87 {"operatorcommand", store_str, ITEM(res_msgs.operator_cmd), 0, 0, 0},
88 {"syslog", store_msgs, ITEM(res_msgs), MD_SYSLOG, 0, 0},
89 {"mail", store_msgs, ITEM(res_msgs), MD_MAIL, 0, 0},
90 {"mailonerror", store_msgs, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, 0},
91 {"file", store_msgs, ITEM(res_msgs), MD_FILE, 0, 0},
92 {"append", store_msgs, ITEM(res_msgs), MD_APPEND, 0, 0},
93 {"stdout", store_msgs, ITEM(res_msgs), MD_STDOUT, 0, 0},
94 {"stderr", store_msgs, ITEM(res_msgs), MD_STDERR, 0, 0},
95 {"director", store_msgs, ITEM(res_msgs), MD_DIRECTOR, 0, 0},
96 {"console", store_msgs, ITEM(res_msgs), MD_CONSOLE, 0, 0},
97 {"operator", store_msgs, ITEM(res_msgs), MD_OPERATOR, 0, 0},
105 /* Various message types */
106 static struct s_mtypes msg_types[] = {
111 {"warning", M_WARNING},
114 {"notsaved", M_NOTSAVED},
115 {"skipped", M_SKIPPED},
117 {"terminate", M_TERM},
123 /* Simply print a message */
124 static void prtmsg(void *sock, char *fmt, ...)
128 va_start(arg_ptr, fmt);
129 vfprintf(stdout, fmt, arg_ptr);
134 char *res_to_str(int rcode)
136 if (rcode < r_first || rcode > r_last) {
137 return "***UNKNOWN***";
139 return resources[rcode-r_first].name;
146 * Initialize the static structure to zeros, then
147 * apply all the default values.
149 void init_resource(int type, struct res_items *items)
152 int rindex = type - r_first;
154 memset(&res_all, 0, res_all_size);
155 res_all.hdr.rcode = type;
156 res_all.hdr.refcnt = 1;
158 for (i=0; items[i].name; i++) {
159 Dmsg3(300, "Item=%s def=%s defval=%d\n", items[i].name,
160 (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
161 items[i].default_value);
162 if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
163 if (items[i].handler == store_yesno) {
164 *(int *)(items[i].value) |= items[i].code;
165 } else if (items[i].handler == store_pint ||
166 items[i].handler == store_int) {
167 *(int *)(items[i].value) = items[i].default_value;
168 } else if (items[i].handler == store_int64) {
169 *(int64_t *)(items[i].value) = items[i].default_value;
170 } else if (items[i].handler == store_size ||
171 items[i].handler == store_time) {
172 *(uint64_t *)(items[i].value) = items[i].default_value;
175 /* If this triggers, take a look at lib/parse_conf.h */
176 if (i >= MAX_RES_ITEMS) {
177 Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
183 /* Store Messages Destination information */
184 void store_msgs(LEX *lc, struct res_items *item, int index, int pass)
191 Dmsg2(200, "store_msgs pass=%d code=%d\n", pass, item->code);
193 switch (item->code) {
196 case MD_SYSLOG: /* syslog */
198 scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
200 case MD_OPERATOR: /* send to operator */
201 case MD_DIRECTOR: /* send to Director */
202 case MD_MAIL: /* mail */
203 case MD_MAIL_ON_ERROR: /* mail if Job errors */
204 if (item->code == MD_OPERATOR) {
205 cmd = res_all.res_msgs.operator_cmd;
207 cmd = res_all.res_msgs.mail_cmd;
209 dest = get_pool_memory(PM_MESSAGE);
212 /* Pick up comma separated list of destinations */
214 token = lex_get_token(lc); /* scan destination */
215 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
216 scan_err1(lc, "expected a message destination, got: %s", lc->str);
218 dest = (char *) check_pool_memory_size(dest, dest_len + lc->str_len + 2);
220 strcat(dest, " "); /* separate multiple destinations with space */
223 strcat(dest, lc->str);
224 dest_len += lc->str_len;
225 Dmsg2(100, "store_msgs newdest=%s: dest=%s:\n", lc->str, dest);
226 token = lex_get_token(lc);
227 if (token == T_COMMA) {
228 continue; /* get another destination */
230 if (token != T_EQUALS) {
231 scan_err1(lc, "expected an =, got: %s", lc->str);
235 Dmsg1(200, "mail_cmd=%s\n", cmd);
236 scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
237 free_pool_memory(dest);
238 Dmsg0(200, "done with dest codes\n");
240 case MD_FILE: /* file */
241 case MD_APPEND: /* append */
242 dest = get_pool_memory(PM_MESSAGE);
243 /* Pick up a single destination */
244 token = lex_get_token(lc); /* scan destination */
245 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
246 scan_err1(lc, "expected a message destination, got: %s", lc->str);
248 dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
249 strcpy(dest, lc->str);
250 dest_len = lc->str_len;
251 token = lex_get_token(lc);
252 Dmsg1(200, "store_msgs dest=%s:\n", dest);
253 if (token != T_EQUALS) {
254 scan_err1(lc, "expected an =, got: %s", lc->str);
256 scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
257 free_pool_memory(dest);
258 Dmsg0(200, "done with dest codes\n");
262 scan_err1(lc, "Unknown item code: %d\n", item->code);
267 set_bit(index, res_all.hdr.item_present);
268 Dmsg0(200, "Done store_msgs\n");
272 * Scan for message types and add them to the message
273 * destination. The basic job here is to connect message types
274 * (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
275 * destination (MAIL, FILE, OPERATOR, ...)
277 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
279 int token, i, found, quit, is_not;
283 for (quit=0; !quit;) {
284 token = lex_get_token(lc); /* expect at least one type */
285 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
286 scan_err1(lc, "expected a message type, got: %s", lc->str);
289 if (lc->str[0] == '!') {
296 for (i=0; msg_types[i].name; i++) {
297 if (strcmp(str, msg_types[i].name) == 0) {
298 msg_type = msg_types[i].token;
304 scan_err1(lc, "message type: %s not found", str);
307 if (msg_type == M_MAX+1) { /* all? */
308 for (i=1; i<=M_MAX; i++) { /* yes set all types */
309 add_msg_dest(msg, dest_code, i, where, cmd);
313 rem_msg_dest(msg, dest_code, msg_type, where);
315 add_msg_dest(msg, dest_code, msg_type, where, cmd);
321 Dmsg0(200, "call lex_get_token() to eat comma\n");
322 token = lex_get_token(lc); /* eat comma */
324 Dmsg0(200, "Done scan_types()\n");
329 * This routine is ONLY for resource names
330 * Store a name at specified address.
332 void store_name(LEX *lc, struct res_items *item, int index, int pass)
337 token = lex_get_token(lc);
338 /* Store the name both pass 1 and pass 2 */
339 *(item->value) = bstrdup(lc->str);
341 set_bit(index, res_all.hdr.item_present);
346 * Store a name string at specified address
347 * A name string is limited to MAX_RES_NAME_LENGTH
349 void store_strname(LEX *lc, struct res_items *item, int index, int pass)
354 token = lex_get_token(lc);
357 *(item->value) = bstrdup(lc->str);
360 set_bit(index, res_all.hdr.item_present);
365 /* Store a string at specified address */
366 void store_str(LEX *lc, struct res_items *item, int index, int pass)
370 token = lex_get_token(lc);
371 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
372 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
375 *(item->value) = bstrdup(lc->str);
379 set_bit(index, res_all.hdr.item_present);
382 /* Store a directory name at specified address */
383 void store_dir(LEX *lc, struct res_items *item, int index, int pass)
387 token = lex_get_token(lc);
388 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
389 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
392 do_shell_expansion(lc->str);
393 *(item->value) = bstrdup(lc->str);
397 set_bit(index, res_all.hdr.item_present);
401 /* Store a password specified address in MD5 coding */
402 void store_password(LEX *lc, struct res_items *item, int index, int pass)
404 unsigned int token, i, j;
405 struct MD5Context md5c;
406 unsigned char signature[16];
410 token = lex_get_token(lc);
411 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
412 scan_err1(lc, "expected an identifier or string, got: %s\n", lc->str);
416 MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
417 MD5Final(signature, &md5c);
418 for (i = j = 0; i < sizeof(signature); i++) {
419 sprintf(&sig[j], "%02x", signature[i]);
422 *(item->value) = bstrdup(sig);
426 set_bit(index, res_all.hdr.item_present);
430 /* Store a resource at specified address.
431 * If we are in pass 2, do a lookup of the
434 void store_res(LEX *lc, struct res_items *item, int index, int pass)
439 token = lex_get_token(lc);
440 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
441 scan_err1(lc, "expected a Resource name, got: %s", lc->str);
444 res = GetResWithName(item->code, lc->str);
446 scan_err3(lc, "Could not find Resource %s referenced on line %d : %s\n",
447 lc->str, lc->line_no, lc->line);
449 *(item->value) = (char *)res;
452 set_bit(index, res_all.hdr.item_present);
456 /* Store an integer at specified address */
457 void store_int(LEX *lc, struct res_items *item, int index, int pass)
461 token = lex_get_token(lc);
462 if (token != T_NUMBER || !is_a_number(lc->str)) {
463 scan_err1(lc, "expected an integer number, got: %s", lc->str);
466 *(int *)(item->value) = (int)strtod(lc->str, NULL);
468 scan_err1(lc, "expected an integer number, got: %s", lc->str);
472 set_bit(index, res_all.hdr.item_present);
475 /* Store a positive integer at specified address */
476 void store_pint(LEX *lc, struct res_items *item, int index, int pass)
480 lc->expect = T_PINT32;
481 token = lex_get_token(lc);
482 *(int *)(item->value) = lc->pint32_val;
484 set_bit(index, res_all.hdr.item_present);
488 /* Store an 64 bit integer at specified address */
489 void store_int64(LEX *lc, struct res_items *item, int index, int pass)
493 lc->expect = T_INT64;
494 token = lex_get_token(lc);
495 *(int64_t *)(item->value) = lc->int64_val;
497 set_bit(index, res_all.hdr.item_present);
500 /* Store a size in bytes */
501 void store_size(LEX *lc, struct res_items *item, int index, int pass)
505 int mod[] = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
506 uint64_t mult[] = {1, /* byte */
508 1048576, /* megabyte */
509 1073741824}; /* gigabyte */
511 #ifdef we_have_a_compiler_that_works
512 int mod[] = {'*', 'k', 'm', 'g', 't', 0};
513 uint64_t mult[] = {1, /* byte */
515 1048576, /* megabyte */
516 1073741824, /* gigabyte */
517 1099511627776};/* terabyte */
520 Dmsg0(400, "Enter store_size\n");
521 token = lex_get_token(lc);
525 Dmsg2(400, "size num=:%s: %f\n", lc->str, strtod(lc->str, NULL));
526 value = (uint64_t)strtod(lc->str, NULL);
527 if (errno != 0 || token < 0) {
528 scan_err1(lc, "expected a size number, got: %s", lc->str);
530 *(uint64_t *)(item->value) = value;
534 /* Look for modifier */
535 ch = lc->str[lc->str_len - 1];
541 while (mod[++i] != 0) {
544 lc->str[lc->str_len] = 0; /* strip modifier */
549 if (mod[i] == 0 || !is_a_number(lc->str)) {
550 scan_err1(lc, "expected a size number, got: %s", lc->str);
552 Dmsg3(400, "size str=:%s: %f i=%d\n", lc->str, strtod(lc->str, NULL), i);
554 value = (uint64_t)strtod(lc->str, NULL);
555 Dmsg1(400, "Int value = %d\n", (int)value);
556 if (errno != 0 || value < 0) {
557 scan_err1(lc, "expected a size number, got: %s", lc->str);
559 *(uint64_t *)(item->value) = value * mult[i];
560 Dmsg2(400, "Full value = %f %" lld "\n", strtod(lc->str, NULL) * mult[i],
564 scan_err1(lc, "expected a size, got: %s", lc->str);
568 set_bit(index, res_all.hdr.item_present);
569 Dmsg0(400, "Leave store_size\n");
573 /* Store a time period in seconds */
574 void store_time(LEX *lc, struct res_items *item, int index, int pass)
579 token = lex_get_token(lc);
583 value = (btime_t)strtod(lc->str, NULL);
584 if (errno != 0 || value < 0) {
585 scan_err1(lc, "expected a time period, got: %s", lc->str);
587 *(btime_t *)(item->value) = value;
591 if (!string_to_btime(lc->str, &value)) {
592 scan_err1(lc, "expected a time period, got: %s", lc->str);
594 *(btime_t *)(item->value) = value;
597 scan_err1(lc, "expected a time period, got: %s", lc->str);
601 set_bit(index, res_all.hdr.item_present);
605 /* Store a yes/no in a bit field */
606 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
610 token = lex_get_token(lc);
611 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
612 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
613 } else if (strcasecmp(lc->str, "yes") == 0) {
614 *(int *)(item->value) |= item->code;
615 } else if (strcasecmp(lc->str, "no") == 0) {
616 *(int *)(item->value) &= ~(item->code);
618 scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
621 set_bit(index, res_all.hdr.item_present);
639 * Return resource of type rcode that matches name
642 GetResWithName(int rcode, char *name)
645 int rindex = rcode - r_first;
648 res = resources[rindex].res_head;
650 if (strcmp(res->name, name) == 0) {
661 * Return next resource of type rcode. On first
662 * call second arg (res) is NULL, on subsequent
663 * calls, it is called with previous value.
666 GetNextRes(int rcode, RES *res)
669 int rindex = rcode - r_first;
673 Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
676 nres = resources[rindex].res_head;
690 /*********************************************************************
692 * Parse configuration file
696 parse_config(char *cf)
699 int token, i, res_type, pass;
700 enum parse_state state = p_none;
701 struct res_items *items;
704 /* Make two passes. The first builds the name symbol table,
705 * and the second picks up the items.
707 Dmsg0(200, "Enter parse_config()\n");
708 for (pass=1; pass <= 2; pass++) {
709 Dmsg1(200, "parse_config pass %d\n", pass);
710 lc = lex_open_file(lc, cf);
711 while ((token=lex_get_token(lc)) != T_EOF) {
712 Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
715 if (token == T_EOL) {
718 if (token != T_IDENTIFIER) {
719 scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
721 for (i=0; resources[i].name; i++)
722 if (strcasecmp(resources[i].name, lc->str) == 0) {
724 items = resources[i].items;
725 res_type = resources[i].rcode;
726 init_resource(res_type, items);
729 if (state == p_none) {
730 scan_err1(lc, "expected resource name, got: %s", lc->str);
740 scan_err1(lc, "not in resource definition: %s", lc->str);
742 for (i=0; items[i].name; i++) {
743 if (strcasecmp(items[i].name, lc->str) == 0) {
744 token = lex_get_token(lc);
745 Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
746 if (token != T_EQUALS) {
747 scan_err1(lc, "expected an equals, got: %s", lc->str);
749 Dmsg1(150, "calling handler for %s\n", items[i].name);
750 /* Call item handler */
751 items[i].handler(lc, &items[i], i, pass);
757 Dmsg2(150, "level=%d id=%s\n", level, lc->str);
758 Dmsg1(150, "Keyword = %s\n", lc->str);
759 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
766 Dmsg0(150, "T_EOB => define new resource\n");
767 save_resource(res_type, items, pass); /* save resource */
774 scan_err2(lc, "unexpected token %d %s in resource definition",
775 token, lex_tok_to_str(token));
779 scan_err1(lc, "Unknown parser state %d\n", state);
782 if (debug_level > 50 && pass == 2) {
784 for (i=r_first; i<=r_last; i++) {
785 dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
788 lc = lex_close_file(lc);
790 Dmsg0(200, "Leave parse_config()\n");
793 /*********************************************************************
795 * Free configuration resources
799 free_config_resources()
802 for (i=r_first; i<=r_last; i++) {