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,
15 * string, time, int64, size, ...).
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-2004 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 RES_TABLE resources[];
68 extern RES **res_head;
71 // work around visual studio name manling preventing external linkage since res_all
72 // is declared as a different type when instantiated.
73 extern "C" CURES res_all;
74 extern "C" int res_all_size;
77 extern int res_all_size;
81 static brwlock_t res_lock; /* resource lock */
82 static int res_locked = 0; /* set when resource chains locked -- for debug */
84 /* Forward referenced subroutines */
85 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
88 /* Common Resource definitions */
90 /* Message resource directives
91 * name handler value code flags default_value
93 RES_ITEM msgs_items[] = {
94 {"name", store_name, ITEM(res_msgs.hdr.name), 0, 0, 0},
95 {"description", store_str, ITEM(res_msgs.hdr.desc), 0, 0, 0},
96 {"mailcommand", store_str, ITEM(res_msgs.mail_cmd), 0, 0, 0},
97 {"operatorcommand", store_str, ITEM(res_msgs.operator_cmd), 0, 0, 0},
98 {"syslog", store_msgs, ITEM(res_msgs), MD_SYSLOG, 0, 0},
99 {"mail", store_msgs, ITEM(res_msgs), MD_MAIL, 0, 0},
100 {"mailonerror", store_msgs, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, 0},
101 {"file", store_msgs, ITEM(res_msgs), MD_FILE, 0, 0},
102 {"append", store_msgs, ITEM(res_msgs), MD_APPEND, 0, 0},
103 {"stdout", store_msgs, ITEM(res_msgs), MD_STDOUT, 0, 0},
104 {"stderr", store_msgs, ITEM(res_msgs), MD_STDERR, 0, 0},
105 {"director", store_msgs, ITEM(res_msgs), MD_DIRECTOR, 0, 0},
106 {"console", store_msgs, ITEM(res_msgs), MD_CONSOLE, 0, 0},
107 {"operator", store_msgs, ITEM(res_msgs), MD_OPERATOR, 0, 0},
108 {NULL, NULL, NULL, 0, 0}
115 /* Various message types */
116 static struct s_mtypes msg_types[] = {
121 {"warning", M_WARNING},
124 {"notsaved", M_NOTSAVED},
125 {"skipped", M_SKIPPED},
127 {"terminate", M_TERM},
128 {"restored", M_RESTORED},
134 /* Simply print a message */
135 static void prtmsg(void *sock, const char *fmt, ...)
139 va_start(arg_ptr, fmt);
140 vfprintf(stdout, fmt, arg_ptr);
144 const char *res_to_str(int rcode)
146 if (rcode < r_first || rcode > r_last) {
147 return _("***UNKNOWN***");
149 return resources[rcode-r_first].name;
155 * Initialize the static structure to zeros, then
156 * apply all the default values.
158 void init_resource(int type, RES_ITEM *items, int pass)
161 int rindex = type - r_first;
162 static bool first = true;
165 if (first && (errstat=rwl_init(&res_lock)) != 0) {
166 Emsg1(M_ABORT, 0, _("Unable to initialize resource lock. ERR=%s\n"),
171 memset(&res_all, 0, res_all_size);
172 res_all.hdr.rcode = type;
173 res_all.hdr.refcnt = 1;
175 for (i=0; items[i].name; i++) {
176 Dmsg3(900, "Item=%s def=%s defval=%d\n", items[i].name,
177 (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
178 items[i].default_value);
179 if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
180 if (items[i].handler == store_yesno) {
181 *(int *)(items[i].value) |= items[i].code;
182 } else if (items[i].handler == store_pint ||
183 items[i].handler == store_int) {
184 *(int *)(items[i].value) = items[i].default_value;
185 } else if (items[i].handler == store_int64) {
186 *(int64_t *)(items[i].value) = items[i].default_value;
187 } else if (items[i].handler == store_size) {
188 *(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value;
189 } else if (items[i].handler == store_time) {
190 *(utime_t *)(items[i].value) = (utime_t)items[i].default_value;
191 } else if (pass == 1 && items[i].handler == store_addresses) {
192 init_default_addresses((dlist**)items[i].value, items[i].default_value);
195 /* If this triggers, take a look at lib/parse_conf.h */
196 if (i >= MAX_RES_ITEMS) {
197 Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
203 /* Store Messages Destination information */
204 void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass)
211 Dmsg2(900, "store_msgs pass=%d code=%d\n", pass, item->code);
213 switch (item->code) {
216 case MD_SYSLOG: /* syslog */
218 scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
220 case MD_OPERATOR: /* send to operator */
221 case MD_DIRECTOR: /* send to Director */
222 case MD_MAIL: /* mail */
223 case MD_MAIL_ON_ERROR: /* mail if Job errors */
224 if (item->code == MD_OPERATOR) {
225 cmd = res_all.res_msgs.operator_cmd;
227 cmd = res_all.res_msgs.mail_cmd;
229 dest = get_pool_memory(PM_MESSAGE);
232 /* Pick up comma separated list of destinations */
234 token = lex_get_token(lc, T_NAME); /* scan destination */
235 dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
237 pm_strcat(dest, " "); /* separate multiple destinations with space */
240 pm_strcat(dest, lc->str);
241 dest_len += lc->str_len;
242 Dmsg2(900, "store_msgs newdest=%s: dest=%s:\n", lc->str, NPRT(dest));
243 token = lex_get_token(lc, T_SKIP_EOL);
244 if (token == T_COMMA) {
245 continue; /* get another destination */
247 if (token != T_EQUALS) {
248 scan_err1(lc, _("expected an =, got: %s"), lc->str);
252 Dmsg1(900, "mail_cmd=%s\n", NPRT(cmd));
253 scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
254 free_pool_memory(dest);
255 Dmsg0(900, "done with dest codes\n");
257 case MD_FILE: /* file */
258 case MD_APPEND: /* append */
259 dest = get_pool_memory(PM_MESSAGE);
260 /* Pick up a single destination */
261 token = lex_get_token(lc, T_NAME); /* scan destination */
262 pm_strcpy(dest, lc->str);
263 dest_len = lc->str_len;
264 token = lex_get_token(lc, T_SKIP_EOL);
265 Dmsg1(900, "store_msgs dest=%s:\n", NPRT(dest));
266 if (token != T_EQUALS) {
267 scan_err1(lc, _("expected an =, got: %s"), lc->str);
269 scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
270 free_pool_memory(dest);
271 Dmsg0(900, "done with dest codes\n");
275 scan_err1(lc, _("Unknown item code: %d\n"), item->code);
280 set_bit(index, res_all.hdr.item_present);
281 Dmsg0(900, "Done store_msgs\n");
285 * Scan for message types and add them to the message
286 * destination. The basic job here is to connect message types
287 * (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
288 * destination (MAIL, FILE, OPERATOR, ...)
290 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
292 int i, found, quit, is_not;
296 for (quit=0; !quit;) {
297 lex_get_token(lc, T_NAME); /* expect at least one type */
299 if (lc->str[0] == '!') {
306 for (i=0; msg_types[i].name; i++) {
307 if (strcasecmp(str, msg_types[i].name) == 0) {
308 msg_type = msg_types[i].token;
314 scan_err1(lc, _("message type: %s not found"), str);
318 if (msg_type == M_MAX+1) { /* all? */
319 for (i=1; i<=M_MAX; i++) { /* yes set all types */
320 add_msg_dest(msg, dest_code, i, where, cmd);
324 rem_msg_dest(msg, dest_code, msg_type, where);
326 add_msg_dest(msg, dest_code, msg_type, where, cmd);
332 Dmsg0(900, "call lex_get_token() to eat comma\n");
333 lex_get_token(lc, T_ALL); /* eat comma */
335 Dmsg0(900, "Done scan_types()\n");
340 * This routine is ONLY for resource names
341 * Store a name at specified address.
343 void store_name(LEX *lc, RES_ITEM *item, int index, int pass)
345 POOLMEM *msg = get_pool_memory(PM_EMSG);
346 lex_get_token(lc, T_NAME);
347 if (!is_name_valid(lc->str, &msg)) {
348 scan_err1(lc, "%s\n", msg);
350 free_pool_memory(msg);
351 /* Store the name both pass 1 and pass 2 */
352 if (*(item->value)) {
353 scan_err2(lc, _("Attempt to redefine name \"%s\" to \"%s\"."),
354 *(item->value), lc->str);
356 *(item->value) = bstrdup(lc->str);
358 set_bit(index, res_all.hdr.item_present);
363 * Store a name string at specified address
364 * A name string is limited to MAX_RES_NAME_LENGTH
366 void store_strname(LEX *lc, RES_ITEM *item, int index, int pass)
368 lex_get_token(lc, T_NAME);
371 *(item->value) = bstrdup(lc->str);
374 set_bit(index, res_all.hdr.item_present);
377 /* Store a string at specified address */
378 void store_str(LEX *lc, RES_ITEM *item, int index, int pass)
380 lex_get_token(lc, T_STRING);
382 *(item->value) = bstrdup(lc->str);
385 set_bit(index, res_all.hdr.item_present);
389 * Store a directory name at specified address. Note, we do
390 * shell expansion except if the string begins with a vertical
391 * bar (i.e. it will likely be passed to the shell later).
393 void store_dir(LEX *lc, RES_ITEM *item, int index, int pass)
395 lex_get_token(lc, T_STRING);
397 if (lc->str[0] != '|') {
398 do_shell_expansion(lc->str, sizeof(lc->str));
400 *(item->value) = bstrdup(lc->str);
403 set_bit(index, res_all.hdr.item_present);
407 /* Store a password specified address in MD5 coding */
408 void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
411 struct MD5Context md5c;
412 unsigned char signature[16];
416 lex_get_token(lc, T_STRING);
419 MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
420 MD5Final(signature, &md5c);
421 for (i = j = 0; i < sizeof(signature); i++) {
422 sprintf(&sig[j], "%02x", signature[i]);
425 *(item->value) = bstrdup(sig);
428 set_bit(index, res_all.hdr.item_present);
432 /* Store a resource at specified address.
433 * If we are in pass 2, do a lookup of the
436 void store_res(LEX *lc, RES_ITEM *item, int index, int pass)
440 lex_get_token(lc, T_NAME);
442 res = GetResWithName(item->code, lc->str);
444 scan_err3(lc, _("Could not find config Resource %s referenced on line %d : %s\n"),
445 lc->str, lc->line_no, lc->line);
447 if (*(item->value)) {
448 scan_err3(lc, _("Attempt to redefine resource \"%s\" referenced on line %d : %s\n"),
449 item->name, lc->line_no, lc->line);
451 *(item->value) = (char *)res;
454 set_bit(index, res_all.hdr.item_present);
458 * Store a resource in an alist. default_value indicates how many
459 * times this routine can be called -- i.e. how many alists
461 * If we are in pass 2, do a lookup of the
464 void store_alist_res(LEX *lc, RES_ITEM *item, int index, int pass)
467 int count = item->default_value;
472 /* Find empty place to store this directive */
473 while ((item->value)[i] != NULL && i++ < count) { }
475 scan_err3(lc, _("Too many Storage directives. Max. is %d. line %d: %s\n"),
476 count, lc->line_no, lc->line);
478 list = New(alist(10, not_owned_by_alist));
481 lex_get_token(lc, T_NAME); /* scan next item */
482 res = GetResWithName(item->code, lc->str);
484 scan_err3(lc, _("Could not find config Resource \"%s\" referenced on line %d : %s\n"),
485 lc->str, lc->line_no, lc->line);
488 (item->value)[i] = (char *)list;
489 if (lc->ch != ',') { /* if no other item follows */
492 lex_get_token(lc, T_ALL); /* eat comma */
496 set_bit(index, res_all.hdr.item_present);
501 * Store default values for Resource from xxxDefs
502 * If we are in pass 2, do a lookup of the
503 * resource and store everything not explicitly set
506 * Note, here item points to the main resource (e.g. Job, not
507 * the jobdefs, which we look up).
509 void store_defs(LEX *lc, RES_ITEM *item, int index, int pass)
513 lex_get_token(lc, T_NAME);
515 Dmsg2(900, "Code=%d name=%s\n", item->code, lc->str);
516 res = GetResWithName(item->code, lc->str);
518 scan_err3(lc, _("Missing config Resource \"%s\" referenced on line %d : %s\n"),
519 lc->str, lc->line_no, lc->line);
521 /* for each item not set, we copy the field from res */
523 for (int i=0; item->name;; i++, item++) {
524 if (bit_is_set(i, res->item_present)) {
525 Dmsg2(900, "Item %d is present in %s\n", i, res->name);
527 Dmsg2(900, "Item %d is not present in %s\n", i, res->name);
530 /* ***FIXME **** add code */
538 /* Store an integer at specified address */
539 void store_int(LEX *lc, RES_ITEM *item, int index, int pass)
541 lex_get_token(lc, T_INT32);
542 *(int *)(item->value) = lc->int32_val;
544 set_bit(index, res_all.hdr.item_present);
547 /* Store a positive integer at specified address */
548 void store_pint(LEX *lc, RES_ITEM *item, int index, int pass)
550 lex_get_token(lc, T_PINT32);
551 *(int *)(item->value) = lc->pint32_val;
553 set_bit(index, res_all.hdr.item_present);
557 /* Store an 64 bit integer at specified address */
558 void store_int64(LEX *lc, RES_ITEM *item, int index, int pass)
560 lex_get_token(lc, T_INT64);
561 *(int64_t *)(item->value) = lc->int64_val;
563 set_bit(index, res_all.hdr.item_present);
566 /* Store a size in bytes */
567 void store_size(LEX *lc, RES_ITEM *item, int index, int pass)
572 Dmsg0(900, "Enter store_size\n");
573 token = lex_get_token(lc, T_SKIP_EOL);
578 case T_UNQUOTED_STRING:
579 if (!size_to_uint64(lc->str, lc->str_len, &uvalue)) {
580 scan_err1(lc, _("expected a size number, got: %s"), lc->str);
582 *(uint64_t *)(item->value) = uvalue;
585 scan_err1(lc, _("expected a size, got: %s"), lc->str);
589 set_bit(index, res_all.hdr.item_present);
590 Dmsg0(900, "Leave store_size\n");
594 /* Store a time period in seconds */
595 void store_time(LEX *lc, RES_ITEM *item, int index, int pass)
601 token = lex_get_token(lc, T_SKIP_EOL);
606 case T_UNQUOTED_STRING:
607 bstrncpy(period, lc->str, sizeof(period));
609 token = lex_get_token(lc, T_ALL);
612 case T_UNQUOTED_STRING:
613 bstrncat(period, lc->str, sizeof(period));
617 if (!duration_to_utime(period, &utime)) {
618 scan_err1(lc, _("expected a time period, got: %s"), period);
620 *(utime_t *)(item->value) = utime;
623 scan_err1(lc, _("expected a time period, got: %s"), lc->str);
626 if (token != T_EOL) {
629 set_bit(index, res_all.hdr.item_present);
633 /* Store a yes/no in a bit field */
634 void store_yesno(LEX *lc, RES_ITEM *item, int index, int pass)
636 lex_get_token(lc, T_NAME);
637 if (strcasecmp(lc->str, "yes") == 0) {
638 *(int *)(item->value) |= item->code;
639 } else if (strcasecmp(lc->str, "no") == 0) {
640 *(int *)(item->value) &= ~(item->code);
642 scan_err1(lc, _("Expect a YES or NO, got: %s"), lc->str);
645 set_bit(index, res_all.hdr.item_present);
649 /* #define TRACE_RES */
651 void b_LockRes(const char *file, int line)
655 Dmsg4(000, "LockRes %d,%d at %s:%d\n", res_locked, res_lock.w_active,
658 if ((errstat=rwl_writelock(&res_lock)) != 0) {
659 Emsg3(M_ABORT, 0, "rwl_writelock failure at %s:%d: ERR=%s\n",
660 file, line, strerror(errstat));
665 void b_UnlockRes(const char *file, int line)
670 Dmsg4(000, "UnLockRes %d,%d at %s:%d\n", res_locked, res_lock.w_active,
673 if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
674 Emsg3(M_ABORT, 0, "rwl_writeunlock failure at %s:%d:. ERR=%s\n",
675 file, line, strerror(errstat));
680 * Return resource of type rcode that matches name
683 GetResWithName(int rcode, char *name)
686 int rindex = rcode - r_first;
689 res = res_head[rindex];
691 if (strcmp(res->name, name) == 0) {
702 * Return next resource of type rcode. On first
703 * call second arg (res) is NULL, on subsequent
704 * calls, it is called with previous value.
707 GetNextRes(int rcode, RES *res)
710 int rindex = rcode - r_first;
714 Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
717 nres = res_head[rindex];
731 /*********************************************************************
733 * Parse configuration file
735 * Return 0 if reading failed, 1 otherwise
738 parse_config(const char *cf, int exit_on_error)
740 set_exit_on_error(exit_on_error);
744 enum parse_state state = p_none;
745 RES_ITEM *items = NULL;
748 /* Make two passes. The first builds the name symbol table,
749 * and the second picks up the items.
751 Dmsg0(900, "Enter parse_config()\n");
752 for (pass=1; pass <= 2; pass++) {
753 Dmsg1(900, "parse_config pass %d\n", pass);
754 if ((lc = lex_open_file(lc, cf, NULL)) == NULL) {
755 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
758 while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
759 Dmsg1(900, "parse got token=%s\n", lex_tok_to_str(token));
762 if (token == T_EOL) {
765 if (token != T_IDENTIFIER) {
766 scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str);
767 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
770 for (i=0; resources[i].name; i++)
771 if (strcasecmp(resources[i].name, lc->str) == 0) {
773 items = resources[i].items;
774 res_type = resources[i].rcode;
775 init_resource(res_type, items, pass);
778 if (state == p_none) {
779 scan_err1(lc, _("expected resource name, got: %s"), lc->str);
780 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
791 scan_err1(lc, _("not in resource definition: %s"), lc->str);
792 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
795 for (i=0; items[i].name; i++) {
796 if (strcasecmp(items[i].name, lc->str) == 0) {
797 /* If the ITEM_NO_EQUALS flag is set we do NOT
798 * scan for = after the keyword */
799 if (!(items[i].flags & ITEM_NO_EQUALS)) {
800 token = lex_get_token(lc, T_SKIP_EOL);
801 Dmsg1 (900, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
802 if (token != T_EQUALS) {
803 scan_err1(lc, _("expected an equals, got: %s"), lc->str);
804 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
808 Dmsg1(900, "calling handler for %s\n", items[i].name);
809 /* Call item handler */
810 items[i].handler(lc, &items[i], i, pass);
816 Dmsg2(900, "level=%d id=%s\n", level, lc->str);
817 Dmsg1(900, "Keyword = %s\n", lc->str);
818 scan_err1(lc, _("Keyword \"%s\" not permitted in this resource.\n"
819 "Perhaps you left the trailing brace off of the previous resource."), lc->str);
820 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
828 Dmsg0(900, "T_EOB => define new resource\n");
829 save_resource(res_type, items, pass); /* save resource */
836 scan_err2(lc, _("unexpected token %d %s in resource definition"),
837 token, lex_tok_to_str(token));
838 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
843 scan_err1(lc, _("Unknown parser state %d\n"), state);
844 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
848 if (state != p_none) {
849 scan_err0(lc, _("End of conf file reached with unclosed resource."));
850 set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
853 if (debug_level >= 900 && pass == 2) {
855 for (i=r_first; i<=r_last; i++) {
856 dump_resource(i, res_head[i-r_first], prtmsg, NULL);
859 lc = lex_close_file(lc);
861 Dmsg0(900, "Leave parse_config()\n");
862 set_exit_on_error(1);
866 /*********************************************************************
868 * Free configuration resources
871 void free_config_resources()
873 for (int i=r_first; i<=r_last; i++) {
874 free_resource(res_head[i-r_first], i);
875 res_head[i-r_first] = NULL;
879 RES **save_config_resources()
881 int num = r_last - r_first + 1;
882 RES **res = (RES **)malloc(num*sizeof(RES *));
883 for (int i=0; i<num; i++) {
884 res[i] = res_head[i];
892 int size = (r_last - r_first + 1) * sizeof(RES *);
893 RES **res = (RES **)malloc(size);
894 memset(res, 0, size);