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, int dest, char *where, char *cmd);
66 /* Common Resource definitions */
68 /* Message resource directives
69 * name handler store_addr 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, NULL, MD_SYSLOG, 0, 0},
77 {"mail", store_msgs, NULL, MD_MAIL, 0, 0},
78 {"mailonerror", store_msgs, NULL, MD_MAIL_ON_ERROR, 0, 0},
79 {"file", store_msgs, NULL, MD_FILE, 0, 0},
80 {"append", store_msgs, NULL, MD_APPEND, 0, 0},
81 {"stdout", store_msgs, NULL, MD_STDOUT, 0, 0},
82 {"stderr", store_msgs, NULL, MD_STDERR, 0, 0},
83 {"director", store_msgs, NULL, MD_DIRECTOR, 0, 0},
84 {"console", store_msgs, NULL, MD_CONSOLE, 0, 0},
85 {"operator", store_msgs, NULL, 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 items[i].handler == store_time) {
156 *(int *)(items[i].value) = items[i].default_value;
157 } else if (items[i].handler == store_int64) {
158 *(int64_t *)(items[i].value) = items[i].default_value;
159 } else if (items[i].handler == store_size) {
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, 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, 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, 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, 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(dest_code, i, where, cmd);
300 rem_msg_dest(dest_code, msg_type, where);
302 add_msg_dest(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) {
461 scan_err1(lc, "expected an integer number, got: %s", lc->str);
464 *(int *)(item->value) = strtol(lc->str, NULL, 0);
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) {
480 scan_err1(lc, "expected an integer number, got: %s", lc->str);
483 token = strtol(lc->str, NULL, 0);
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 if (token != T_NUMBER) {
501 scan_err1(lc, "expected an integer number, got: %s", lc->str);
504 *(int64_t *)(item->value) = (int64_t)strtod(lc->str, NULL);
506 scan_err1(lc, "expected an integer number, got: %s", lc->str);
509 set_bit(index, res_all.hdr.item_present);
512 /* Store a size in bytes */
513 void store_size(LEX *lc, struct res_items *item, int index, int pass)
517 int mod[] = {'k', 'm', 'g'};
518 uint64_t mult[] = {1024, /* kilobyte */
519 1048576, /* megabyte */
520 1073741824}; /* gigabyte */
522 #ifdef we_have_a_compiler_that_works
523 int mod[] = {'k', 'm', 'g', 't'};
524 uint64_t mult[] = {1024, /* kilobyte */
525 1048576, /* megabyte */
526 1073741824, /* gigabyte */
527 1099511627776};/* terabyte */
530 Dmsg0(400, "Enter store_size\n");
531 token = lex_get_token(lc);
535 value = (uint64_t)strtod(lc->str, NULL);
536 if (errno != 0 || token < 0) {
537 scan_err1(lc, "expected a size, got: %s", lc->str);
539 *(uint64_t *)(item->value) = value;
543 /* Look for modifier */
544 ch = lc->str[lc->str_len - 1];
550 while (i < (int)sizeof(mod)) {
553 lc->str[lc->str_len] = 0; /* strip modifier */
559 if (i >= (int)sizeof(mod)) {
560 scan_err1(lc, "expected a size, got: %s", lc->str);
562 value = (uint64_t)strtod(lc->str, NULL);
563 Dmsg1(400, "Int value = %d\n", (int)value);
564 if (errno != 0 || value < 0) {
565 scan_err1(lc, "expected a size, got: %s", lc->str);
567 *(uint64_t *)(item->value) = (uint64_t)(strtod(lc->str, NULL) * mult[i]);
568 Dmsg1(400, "Full value = %f\n", strtod(lc->str, NULL) * mult[i]);
571 scan_err1(lc, "expected a size, got: %s", lc->str);
575 set_bit(index, res_all.hdr.item_present);
576 Dmsg0(400, "Leave store_size\n");
580 /* Store a time period in seconds */
581 void store_time(LEX *lc, struct res_items *item, int index, int pass)
583 int token, i, ch, value;
584 int mod[] = {'s', 'm', 'h', 'd', 'w', 'o', 'q', 'y'};
585 int mult[] = {1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30,
586 60*60*24*91, 60*60*24*365};
588 token = lex_get_token(lc);
592 token = strtol(lc->str, NULL, 0);
593 if (errno != 0 || token < 0) {
594 scan_err1(lc, "expected a time period, got: %s", lc->str);
596 *(int *)(item->value) = token;
600 /* Look for modifier */
601 ch = lc->str[lc->str_len - 1];
607 while (i < (int)sizeof(mod)) {
614 if (i >= (int)sizeof(mod)) {
615 scan_err1(lc, "expected a time period, got: %s", lc->str);
617 value = strtol(lc->str, NULL, 0);
618 if (errno != 0 || value < 0) {
619 scan_err1(lc, "expected a time period, got: %s", lc->str);
621 *(int *)(item->value) = value * mult[i];
624 scan_err1(lc, "expected a time period, got: %s", lc->str);
628 set_bit(index, res_all.hdr.item_present);
632 /* Store a yes/no in a bit field */
633 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
637 token = lex_get_token(lc);
639 if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
640 scan_err1(lc, "expected an identifier or string, got: %s", lc->str);
641 } else if (strcmp(lc->str, "yes") == 0) {
642 *(int *)(item->value) |= item->code;
643 } else if (strcmp(lc->str, "no") == 0) {
644 *(int *)(item->value) &= ~(item->code);
646 scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
649 set_bit(index, res_all.hdr.item_present);
654 * Scan to "logical" end of line. I.e. end of line,
657 void scan_to_eol(LEX *lc)
660 Dmsg0(150, "start scan to eof\n");
661 while ((token = lex_get_token(lc)) != T_EOL) {
663 Dmsg0(150, "done scan to eof\n");
668 * Format a scanner error message
670 void s_err(char *file, int line, LEX *lc, char *msg, ...)
675 va_start(arg_ptr, msg);
676 bvsnprintf(buf, sizeof(buf), msg, arg_ptr);
679 e_msg(file, line, M_ABORT, 0, "Config error: %s,\n\
680 : Line %d, col %d of file %s\n%s\n",
681 buf, lc->line_no, lc->col_no, lc->fname, lc->line);
697 * Return resource of type rcode that matches name
700 GetResWithName(int rcode, char *name)
703 int rindex = rcode - r_first;
706 res = resources[rindex].res_head;
708 if (strcmp(res->name, name) == 0) {
719 * Return next resource of type rcode. On first
720 * call second arg (res) is NULL, on subsequent
721 * calls, it is called with previous value.
724 GetNextRes(int rcode, RES *res)
727 int rindex = rcode - r_first;
731 Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
734 nres = resources[rindex].res_head;
748 /*********************************************************************
750 * Parse configuration file
754 parse_config(char *cf)
757 int token, i, res_type, pass;
758 enum parse_state state = p_none;
759 struct res_items *items;
762 /* Make two passes. The first builds the name symbol table,
763 * and the second picks up the items.
765 Dmsg0(200, "Enter parse_config()\n");
766 for (pass=1; pass<= 2; pass++) {
767 Dmsg1(200, "parse_config pass %d\n", pass);
768 lc = lex_open_file(lc, cf);
769 while ((token=lex_get_token(lc)) != T_EOF) {
770 Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
773 if (token == T_EOL) {
776 if (token != T_IDENTIFIER) {
777 scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
780 for (i=0; resources[i].name; i++)
781 if (strcmp(resources[i].name, lc->str) == 0) {
783 items = resources[i].items;
784 res_type = resources[i].rcode;
785 init_resource(res_type, items);
788 if (state == p_none) {
789 scan_err1(lc, "expected resource name, got: %s", lc->str);
799 scan_err1(lc, "not in resource definition: %s", lc->str);
802 for (i=0; items[i].name; i++) {
803 if (strcmp(items[i].name, lc->str) == 0) {
804 token = lex_get_token(lc);
805 Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
806 if (token != T_EQUALS) {
807 scan_err1(lc, "expected an equals, got: %s", lc->str);
809 Dmsg1(150, "calling handler for %s\n", items[i].name);
810 /* Call item handler */
811 items[i].handler(lc, &items[i], i, pass);
817 Dmsg2(150, "level=%d id=%s\n", level, lc->str);
818 Dmsg1(150, "Keyword = %s\n", lc->str);
819 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
826 Dmsg0(150, "T_EOB => define new resource\n");
827 save_resource(res_type, items, pass); /* save resource */
834 scan_err2(lc, "unexpected token %d %s in resource definition",
835 token, lex_tok_to_str(token));
839 scan_err1(lc, "Unknown parser state %d\n", state);
842 if (debug_level > 50 && pass == 2) {
844 for (i=r_first; i<=r_last; i++) {
845 dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
848 lc = lex_close_file(lc);
850 Dmsg0(200, "Leave parse_config()\n");
853 /*********************************************************************
855 * Free configuration resources
859 free_config_resources()
862 for (i=r_first; i<=r_last; i++) {