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 * Kern Sibbald, January MM
25 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
27 This program is free software; you can redistribute it and/or
28 modify it under the terms of the GNU General Public License as
29 published by the Free Software Foundation; either version 2 of
30 the License, or (at your option) any later version.
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 GNU
35 General Public License for more details.
37 You should have received a copy of the GNU General Public
38 License along with this program; if not, write to the Free
39 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
47 extern int debug_level;
49 /* Each daemon has a slightly different set of
50 * resources, so it will define the following
55 extern pthread_mutex_t res_mutex;
56 extern struct s_res resources[];
58 extern int res_all_size;
60 static int res_locked = 0; /* set when resource chains locked */
62 /* Forward referenced subroutines */
63 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
66 /* Common Resource definitions */
68 /* Message resource directives
69 * name handler value code flags default_value
71 struct res_items msgs_items[] = {
72 {"name", store_name, ITEM(res_msgs.hdr.name), 0, 0, 0},
73 {"description", store_str, ITEM(res_msgs.hdr.desc), 0, 0, 0},
74 {"mailcommand", store_str, ITEM(res_msgs.mail_cmd), 0, 0, 0},
75 {"operatorcommand", store_str, ITEM(res_msgs.operator_cmd), 0, 0, 0},
76 {"syslog", store_msgs, ITEM(res_msgs), MD_SYSLOG, 0, 0},
77 {"mail", store_msgs, ITEM(res_msgs), MD_MAIL, 0, 0},
78 {"mailonerror", store_msgs, ITEM(res_msgs), MD_MAIL_ON_ERROR, 0, 0},
79 {"file", store_msgs, ITEM(res_msgs), MD_FILE, 0, 0},
80 {"append", store_msgs, ITEM(res_msgs), MD_APPEND, 0, 0},
81 {"stdout", store_msgs, ITEM(res_msgs), MD_STDOUT, 0, 0},
82 {"stderr", store_msgs, ITEM(res_msgs), MD_STDERR, 0, 0},
83 {"director", store_msgs, ITEM(res_msgs), MD_DIRECTOR, 0, 0},
84 {"console", store_msgs, ITEM(res_msgs), MD_CONSOLE, 0, 0},
85 {"operator", store_msgs, ITEM(res_msgs), MD_OPERATOR, 0, 0},
93 /* Various message types */
94 static struct s_mtypes msg_types[] = {
99 {"warning", M_WARNING},
102 {"notsaved", M_NOTSAVED},
103 {"skipped", M_SKIPPED},
105 {"terminate", M_TERM},
111 /* Simply print a message */
112 static void prtmsg(void *sock, char *fmt, ...)
116 va_start(arg_ptr, fmt);
117 vfprintf(stdout, fmt, arg_ptr);
122 char *res_to_str(int rcode)
124 if (rcode < r_first || rcode > r_last) {
125 return "***UNKNOWN***";
127 return resources[rcode-r_first].name;
134 * Initialize the static structure to zeros, then
135 * apply all the default values.
137 void init_resource(int type, struct res_items *items)
140 int rindex = type - r_first;
142 memset(&res_all, 0, res_all_size);
143 res_all.hdr.rcode = type;
144 res_all.hdr.refcnt = 1;
146 for (i=0; items[i].name; i++) {
147 Dmsg3(300, "Item=%s def=%s defval=%d\n", items[i].name,
148 (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
149 items[i].default_value);
150 if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
151 if (items[i].handler == store_yesno) {
152 *(int *)(items[i].value) |= items[i].code;
153 } else if (items[i].handler == store_pint ||
154 items[i].handler == store_int) {
155 *(int *)(items[i].value) = items[i].default_value;
156 } else if (items[i].handler == store_int64) {
157 *(int64_t *)(items[i].value) = items[i].default_value;
158 } else if (items[i].handler == store_size ||
159 items[i].handler == store_time) {
160 *(uint64_t *)(items[i].value) = items[i].default_value;
163 /* If this triggers, take a look at lib/parse_conf.h */
164 if (i >= MAX_RES_ITEMS) {
165 Emsg1(M_ABORT, 0, _("Too many items in %s resource\n"), resources[rindex]);
171 /* Store Messages Destination information */
172 void store_msgs(LEX *lc, struct res_items *item, int index, int pass)
178 Dmsg2(200, "store_msgs pass=%d code=%d\n", pass, item->code);
180 switch (item->code) {
183 case MD_SYSLOG: /* syslog */
185 scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
187 case MD_OPERATOR: /* send to operator */
188 case MD_DIRECTOR: /* send to Director */
189 case MD_MAIL: /* mail */
190 case MD_MAIL_ON_ERROR: /* mail if Job errors */
191 if (item->code == MD_OPERATOR) {
192 cmd = res_all.res_msgs.operator_cmd;
194 cmd = res_all.res_msgs.mail_cmd;
196 dest = (char *) get_pool_memory(PM_MESSAGE);
199 /* Pick up comma separated list of destinations */
201 token = lex_get_token(lc); /* scan destination */
202 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
203 scan_err1(lc, "expected a message destination, got: %s", lc->str);
205 dest = (char *) check_pool_memory_size(dest, dest_len + lc->str_len + 2);
207 strcat(dest, " "); /* separate multiple destinations with space */
210 strcat(dest, lc->str);
211 dest_len += lc->str_len;
212 Dmsg2(100, "store_msgs newdest=%s: dest=%s:\n", lc->str, dest);
213 token = lex_get_token(lc);
214 if (token == T_COMMA) {
215 continue; /* get another destination */
217 if (token != T_EQUALS) {
218 scan_err1(lc, "expected an =, got: %s", lc->str);
222 Dmsg1(200, "mail_cmd=%s\n", cmd);
223 scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
224 free_pool_memory(dest);
225 Dmsg0(200, "done with dest codes\n");
227 case MD_FILE: /* file */
228 case MD_APPEND: /* append */
229 dest = (char *) get_pool_memory(PM_MESSAGE);
230 /* Pick up a single destination */
231 token = lex_get_token(lc); /* scan destination */
232 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
233 scan_err1(lc, "expected a message destination, got: %s", lc->str);
235 dest = (char *) check_pool_memory_size(dest, dest_len + lc->str_len + 2);
236 strcpy(dest, lc->str);
237 dest_len = lc->str_len;
238 token = lex_get_token(lc);
239 Dmsg1(200, "store_msgs dest=%s:\n", dest);
240 if (token != T_EQUALS) {
241 scan_err1(lc, "expected an =, got: %s", lc->str);
243 scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
244 free_pool_memory(dest);
245 Dmsg0(200, "done with dest codes\n");
249 scan_err1(lc, "Unknown item code: %d\n", item->code);
254 set_bit(index, res_all.hdr.item_present);
255 Dmsg0(200, "Done store_msgs\n");
259 * Scan for message types and add them to the message
260 * destination. The basic job here is to connect message types
261 * (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
262 * destination (MAIL, FILE, OPERATOR, ...)
264 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
266 int token, i, found, quit, is_not;
270 for (quit=0; !quit;) {
271 token = lex_get_token(lc); /* expect at least one type */
272 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
273 scan_err1(lc, "expected a message type, got: %s", lc->str);
276 if (lc->str[0] == '!') {
283 for (i=0; msg_types[i].name; i++) {
284 if (strcmp(str, msg_types[i].name) == 0) {
285 msg_type = msg_types[i].token;
291 scan_err1(lc, "message type: %s not found", str);
294 if (msg_type == M_MAX+1) { /* all? */
295 for (i=1; i<=M_MAX; i++) { /* yes set all types */
296 add_msg_dest(msg, dest_code, i, where, cmd);
300 rem_msg_dest(msg, dest_code, msg_type, where);
302 add_msg_dest(msg, dest_code, msg_type, where, cmd);
308 Dmsg0(200, "call lex_get_token() to eat comma\n");
309 token = lex_get_token(lc); /* eat comma */
311 Dmsg0(200, "Done scan_types()\n");
316 * This routine is ONLY for resource names
317 * Store a name at specified address.
319 void store_name(LEX *lc, struct res_items *item, int index, int pass)
323 token = lex_get_token(lc);
324 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
325 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
326 } else if (lc->str_len > MAX_RES_NAME_LENGTH) {
327 scan_err3(lc, "name %s length %d too long, max is %d\n", lc->str,
328 lc->str_len, MAX_RES_NAME_LENGTH);
330 /* Store the name both pass 1 and pass 2 */
331 *(item->value) = bstrdup(lc->str);
334 set_bit(index, res_all.hdr.item_present);
339 * Store a name string at specified address
340 * A name string is limited to MAX_RES_NAME_LENGTH
342 void store_strname(LEX *lc, struct res_items *item, int index, int pass)
346 token = lex_get_token(lc);
347 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
348 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
349 } else if (lc->str_len > MAX_RES_NAME_LENGTH) {
350 scan_err3(lc, "name %s length %d too long, max is %d\n", lc->str,
351 lc->str_len, MAX_RES_NAME_LENGTH);
355 *(item->value) = bstrdup(lc->str);
358 set_bit(index, res_all.hdr.item_present);
363 /* Store a string at specified address */
364 void store_str(LEX *lc, struct res_items *item, int index, int pass)
368 token = lex_get_token(lc);
369 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
370 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
373 *(item->value) = bstrdup(lc->str);
377 set_bit(index, res_all.hdr.item_present);
380 /* Store a directory name at specified address */
381 void store_dir(LEX *lc, struct res_items *item, int index, int pass)
385 token = lex_get_token(lc);
386 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
387 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
390 do_shell_expansion(lc->str);
391 *(item->value) = bstrdup(lc->str);
395 set_bit(index, res_all.hdr.item_present);
399 /* Store a password specified address in MD5 coding */
400 void store_password(LEX *lc, struct res_items *item, int index, int pass)
402 unsigned int token, i, j;
403 struct MD5Context md5c;
404 unsigned char signature[16];
408 token = lex_get_token(lc);
409 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
410 scan_err1(lc, "expected an identifier or string, got: %s\n", lc->str);
414 MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
415 MD5Final(signature, &md5c);
416 for (i = j = 0; i < sizeof(signature); i++) {
417 sprintf(&sig[j], "%02x", signature[i]);
420 *(item->value) = bstrdup(sig);
424 set_bit(index, res_all.hdr.item_present);
428 /* Store a resource at specified address.
429 * If we are in pass 2, do a lookup of the
432 void store_res(LEX *lc, struct res_items *item, int index, int pass)
437 token = lex_get_token(lc);
438 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
439 scan_err1(lc, "expected a Resource name, got: %s", lc->str);
442 res = GetResWithName(item->code, lc->str);
444 scan_err3(lc, "Could not find Resource %s referenced on line %d : %s\n",
445 lc->str, lc->line_no, lc->line);
447 *(item->value) = (char *)res;
450 set_bit(index, res_all.hdr.item_present);
454 /* Store an integer at specified address */
455 void store_int(LEX *lc, struct res_items *item, int index, int pass)
459 token = lex_get_token(lc);
460 if (token != T_NUMBER || !is_a_number(lc->str)) {
461 scan_err1(lc, "expected an integer number, got: %s", lc->str);
464 *(int *)(item->value) = (int)strtod(lc->str, NULL);
466 scan_err1(lc, "expected an integer number, got: %s", lc->str);
470 set_bit(index, res_all.hdr.item_present);
473 /* Store a positive integer at specified address */
474 void store_pint(LEX *lc, struct res_items *item, int index, int pass)
478 token = lex_get_token(lc);
479 if (token != T_NUMBER || !is_a_number(lc->str)) {
480 scan_err1(lc, "expected a positive integer number, got: %s", lc->str);
483 token = (int)strtod(lc->str, NULL);
484 if (errno != 0 || token < 0) {
485 scan_err1(lc, "expected a postive integer number, got: %s", lc->str);
487 *(int *)(item->value) = token;
490 set_bit(index, res_all.hdr.item_present);
494 /* Store an 64 bit integer at specified address */
495 void store_int64(LEX *lc, struct res_items *item, int index, int pass)
499 token = lex_get_token(lc);
500 Dmsg2(400, "int64=:%s: %f\n", lc->str, strtod(lc->str, NULL));
501 if (token != T_NUMBER || !is_a_number(lc->str)) {
502 scan_err1(lc, "expected an integer number, got: %s", lc->str);
505 *(int64_t *)(item->value) = (int64_t)strtod(lc->str, NULL);
507 scan_err1(lc, "expected an integer number, got: %s", lc->str);
510 set_bit(index, res_all.hdr.item_present);
513 /* Store a size in bytes */
514 void store_size(LEX *lc, struct res_items *item, int index, int pass)
518 int mod[] = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
519 uint64_t mult[] = {1, /* byte */
521 1048576, /* megabyte */
522 1073741824}; /* gigabyte */
524 #ifdef we_have_a_compiler_that_works
525 int mod[] = {'*', 'k', 'm', 'g', 't', 0};
526 uint64_t mult[] = {1, /* byte */
528 1048576, /* megabyte */
529 1073741824, /* gigabyte */
530 1099511627776};/* terabyte */
533 Dmsg0(400, "Enter store_size\n");
534 token = lex_get_token(lc);
538 Dmsg2(400, "size num=:%s: %f\n", lc->str, strtod(lc->str, NULL));
539 value = (uint64_t)strtod(lc->str, NULL);
540 if (errno != 0 || token < 0) {
541 scan_err1(lc, "expected a size number, got: %s", lc->str);
543 *(uint64_t *)(item->value) = value;
547 /* Look for modifier */
548 ch = lc->str[lc->str_len - 1];
554 while (mod[++i] != 0) {
557 lc->str[lc->str_len] = 0; /* strip modifier */
562 if (mod[i] == 0 || !is_a_number(lc->str)) {
563 scan_err1(lc, "expected a size number, got: %s", lc->str);
565 Dmsg3(400, "size str=:%s: %f i=%d\n", lc->str, strtod(lc->str, NULL), i);
567 value = (uint64_t)strtod(lc->str, NULL);
568 Dmsg1(400, "Int value = %d\n", (int)value);
569 if (errno != 0 || value < 0) {
570 scan_err1(lc, "expected a size number, got: %s", lc->str);
572 *(uint64_t *)(item->value) = value * mult[i];
573 Dmsg2(400, "Full value = %f %" lld "\n", strtod(lc->str, NULL) * mult[i],
577 scan_err1(lc, "expected a size, got: %s", lc->str);
581 set_bit(index, res_all.hdr.item_present);
582 Dmsg0(400, "Leave store_size\n");
586 /* Store a time period in seconds */
587 void store_time(LEX *lc, struct res_items *item, int index, int pass)
592 token = lex_get_token(lc);
596 value = (btime_t)strtod(lc->str, NULL);
597 if (errno != 0 || value < 0) {
598 scan_err1(lc, "expected a time period, got: %s", lc->str);
600 *(btime_t *)(item->value) = value;
604 if (!string_to_btime(lc->str, &value)) {
605 scan_err1(lc, "expected a time period, got: %s", lc->str);
607 *(btime_t *)(item->value) = value;
610 scan_err1(lc, "expected a time period, got: %s", lc->str);
614 set_bit(index, res_all.hdr.item_present);
618 /* Store a yes/no in a bit field */
619 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
623 token = lex_get_token(lc);
625 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
626 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
627 } else if (strcmp(lc->str, "yes") == 0) {
628 *(int *)(item->value) |= item->code;
629 } else if (strcmp(lc->str, "no") == 0) {
630 *(int *)(item->value) &= ~(item->code);
632 scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
635 set_bit(index, res_all.hdr.item_present);
640 * Scan to "logical" end of line. I.e. end of line,
643 void scan_to_eol(LEX *lc)
646 Dmsg0(150, "start scan to eof\n");
647 while ((token = lex_get_token(lc)) != T_EOL) {
649 Dmsg0(150, "done scan to eof\n");
654 * Format a scanner error message
656 void s_err(char *file, int line, LEX *lc, char *msg, ...)
661 va_start(arg_ptr, msg);
662 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
665 e_msg(file, line, M_ABORT, 0, "Config error: %s,\n\
666 : Line %d, col %d of file %s\n%s\n",
667 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
683 * Return resource of type rcode that matches name
686 GetResWithName(int rcode, char *name)
689 int rindex = rcode - r_first;
692 res = resources[rindex].res_head;
694 if (strcmp(res->name, name) == 0) {
705 * Return next resource of type rcode. On first
706 * call second arg (res) is NULL, on subsequent
707 * calls, it is called with previous value.
710 GetNextRes(int rcode, RES *res)
713 int rindex = rcode - r_first;
717 Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
720 nres = resources[rindex].res_head;
734 /*********************************************************************
736 * Parse configuration file
740 parse_config(char *cf)
743 int token, i, res_type, pass;
744 enum parse_state state = p_none;
745 struct res_items *items;
748 /* Make two passes. The first builds the name symbol table,
749 * and the second picks up the items.
751 Dmsg0(200, "Enter parse_config()\n");
752 for (pass=1; pass<= 2; pass++) {
753 Dmsg1(200, "parse_config pass %d\n", pass);
754 lc = lex_open_file(lc, cf);
755 while ((token=lex_get_token(lc)) != T_EOF) {
756 Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
759 if (token == T_EOL) {
762 if (token != T_IDENTIFIER) {
763 scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
766 for (i=0; resources[i].name; i++)
767 if (strcmp(resources[i].name, lc->str) == 0) {
769 items = resources[i].items;
770 res_type = resources[i].rcode;
771 init_resource(res_type, items);
774 if (state == p_none) {
775 scan_err1(lc, "expected resource name, got: %s", lc->str);
785 scan_err1(lc, "not in resource definition: %s", lc->str);
788 for (i=0; items[i].name; i++) {
789 if (strcmp(items[i].name, lc->str) == 0) {
790 token = lex_get_token(lc);
791 Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
792 if (token != T_EQUALS) {
793 scan_err1(lc, "expected an equals, got: %s", lc->str);
795 Dmsg1(150, "calling handler for %s\n", items[i].name);
796 /* Call item handler */
797 items[i].handler(lc, &items[i], i, pass);
803 Dmsg2(150, "level=%d id=%s\n", level, lc->str);
804 Dmsg1(150, "Keyword = %s\n", lc->str);
805 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
812 Dmsg0(150, "T_EOB => define new resource\n");
813 save_resource(res_type, items, pass); /* save resource */
820 scan_err2(lc, "unexpected token %d %s in resource definition",
821 token, lex_tok_to_str(token));
825 scan_err1(lc, "Unknown parser state %d\n", state);
828 if (debug_level > 50 && pass == 2) {
830 for (i=r_first; i<=r_last; i++) {
831 dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
834 lc = lex_close_file(lc);
836 Dmsg0(200, "Leave parse_config()\n");
839 /*********************************************************************
841 * Free configuration resources
845 free_config_resources()
848 for (i=r_first; i<=r_last; i++) {