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-2003 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);
133 char *res_to_str(int rcode)
135 if (rcode < r_first || rcode > r_last) {
136 return "***UNKNOWN***";
138 return resources[rcode-r_first].name;
144 * Initialize the static structure to zeros, then
145 * apply all the default values.
147 void init_resource(int type, struct res_items *items)
150 int rindex = type - r_first;
152 memset(&res_all, 0, res_all_size);
153 res_all.hdr.rcode = type;
154 res_all.hdr.refcnt = 1;
156 for (i=0; items[i].name; i++) {
157 Dmsg3(300, "Item=%s def=%s defval=%d\n", items[i].name,
158 (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
159 items[i].default_value);
160 if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
161 if (items[i].handler == store_yesno) {
162 *(int *)(items[i].value) |= items[i].code;
163 } else if (items[i].handler == store_pint ||
164 items[i].handler == store_int) {
165 *(int *)(items[i].value) = items[i].default_value;
166 } else if (items[i].handler == store_int64) {
167 *(int64_t *)(items[i].value) = items[i].default_value;
168 } else if (items[i].handler == store_size ||
169 items[i].handler == store_time) {
170 *(uint64_t *)(items[i].value) = items[i].default_value;
173 /* If this triggers, take a look at lib/parse_conf.h */
174 if (i >= MAX_RES_ITEMS) {
175 Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
181 /* Store Messages Destination information */
182 void store_msgs(LEX *lc, struct res_items *item, int index, int pass)
189 Dmsg2(200, "store_msgs pass=%d code=%d\n", pass, item->code);
191 switch (item->code) {
194 case MD_SYSLOG: /* syslog */
196 scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
198 case MD_OPERATOR: /* send to operator */
199 case MD_DIRECTOR: /* send to Director */
200 case MD_MAIL: /* mail */
201 case MD_MAIL_ON_ERROR: /* mail if Job errors */
202 if (item->code == MD_OPERATOR) {
203 cmd = res_all.res_msgs.operator_cmd;
205 cmd = res_all.res_msgs.mail_cmd;
207 dest = get_pool_memory(PM_MESSAGE);
210 /* Pick up comma separated list of destinations */
212 token = lex_get_token(lc, T_NAME); /* scan destination */
213 dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
215 strcat(dest, " "); /* separate multiple destinations with space */
218 strcat(dest, lc->str);
219 dest_len += lc->str_len;
220 Dmsg2(100, "store_msgs newdest=%s: dest=%s:\n", lc->str, NPRT(dest));
221 token = lex_get_token(lc, T_ALL);
222 if (token == T_COMMA) {
223 continue; /* get another destination */
225 if (token != T_EQUALS) {
226 scan_err1(lc, "expected an =, got: %s", lc->str);
230 Dmsg1(200, "mail_cmd=%s\n", NPRT(cmd));
231 scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
232 free_pool_memory(dest);
233 Dmsg0(200, "done with dest codes\n");
235 case MD_FILE: /* file */
236 case MD_APPEND: /* append */
237 dest = get_pool_memory(PM_MESSAGE);
238 /* Pick up a single destination */
239 token = lex_get_token(lc, T_NAME); /* scan destination */
240 pm_strcpy(&dest, lc->str);
241 dest_len = lc->str_len;
242 token = lex_get_token(lc, T_ALL);
243 Dmsg1(200, "store_msgs dest=%s:\n", NPRT(dest));
244 if (token != T_EQUALS) {
245 scan_err1(lc, "expected an =, got: %s", lc->str);
247 scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
248 free_pool_memory(dest);
249 Dmsg0(200, "done with dest codes\n");
253 scan_err1(lc, "Unknown item code: %d\n", item->code);
258 set_bit(index, res_all.hdr.item_present);
259 Dmsg0(200, "Done store_msgs\n");
263 * Scan for message types and add them to the message
264 * destination. The basic job here is to connect message types
265 * (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
266 * destination (MAIL, FILE, OPERATOR, ...)
268 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
270 int i, found, quit, is_not;
274 for (quit=0; !quit;) {
275 lex_get_token(lc, T_NAME); /* expect at least one type */
277 if (lc->str[0] == '!') {
284 for (i=0; msg_types[i].name; i++) {
285 if (strcmp(str, msg_types[i].name) == 0) {
286 msg_type = msg_types[i].token;
292 scan_err1(lc, "message type: %s not found", str);
296 if (msg_type == M_MAX+1) { /* all? */
297 for (i=1; i<=M_MAX; i++) { /* yes set all types */
298 add_msg_dest(msg, dest_code, i, where, cmd);
302 rem_msg_dest(msg, dest_code, msg_type, where);
304 add_msg_dest(msg, dest_code, msg_type, where, cmd);
310 Dmsg0(200, "call lex_get_token() to eat comma\n");
311 lex_get_token(lc, T_ALL); /* eat comma */
313 Dmsg0(200, "Done scan_types()\n");
318 * This routine is ONLY for resource names
319 * Store a name at specified address.
321 void store_name(LEX *lc, struct res_items *item, int index, int pass)
323 lex_get_token(lc, T_NAME);
324 /* Store the name both pass 1 and pass 2 */
325 *(item->value) = bstrdup(lc->str);
327 set_bit(index, res_all.hdr.item_present);
332 * Store a name string at specified address
333 * A name string is limited to MAX_RES_NAME_LENGTH
335 void store_strname(LEX *lc, struct res_items *item, int index, int pass)
337 lex_get_token(lc, T_NAME);
340 *(item->value) = bstrdup(lc->str);
343 set_bit(index, res_all.hdr.item_present);
348 /* Store a string at specified address */
349 void store_str(LEX *lc, struct res_items *item, int index, int pass)
351 lex_get_token(lc, T_STRING);
353 *(item->value) = bstrdup(lc->str);
356 set_bit(index, res_all.hdr.item_present);
360 * Store a directory name at specified address. Note, we do
361 * shell expansion except if the string begins with a vertical
362 * bar (i.e. it will likely be passed to the shell later).
364 void store_dir(LEX *lc, struct res_items *item, int index, int pass)
366 lex_get_token(lc, T_STRING);
368 if (lc->str[0] != '|') {
369 do_shell_expansion(lc->str);
371 *(item->value) = bstrdup(lc->str);
374 set_bit(index, res_all.hdr.item_present);
378 /* Store a password specified address in MD5 coding */
379 void store_password(LEX *lc, struct res_items *item, int index, int pass)
382 struct MD5Context md5c;
383 unsigned char signature[16];
387 lex_get_token(lc, T_STRING);
390 MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
391 MD5Final(signature, &md5c);
392 for (i = j = 0; i < sizeof(signature); i++) {
393 sprintf(&sig[j], "%02x", signature[i]);
396 *(item->value) = bstrdup(sig);
399 set_bit(index, res_all.hdr.item_present);
403 /* Store a resource at specified address.
404 * If we are in pass 2, do a lookup of the
407 void store_res(LEX *lc, struct res_items *item, int index, int pass)
411 lex_get_token(lc, T_NAME);
413 res = GetResWithName(item->code, lc->str);
415 scan_err3(lc, "Could not find Resource %s referenced on line %d : %s\n",
416 lc->str, lc->line_no, lc->line);
418 *(item->value) = (char *)res;
421 set_bit(index, res_all.hdr.item_present);
425 /* Store an integer at specified address */
426 void store_int(LEX *lc, struct res_items *item, int index, int pass)
428 lex_get_token(lc, T_INT32);
429 *(int *)(item->value) = lc->int32_val;
431 set_bit(index, res_all.hdr.item_present);
434 /* Store a positive integer at specified address */
435 void store_pint(LEX *lc, struct res_items *item, int index, int pass)
437 lex_get_token(lc, T_PINT32);
438 *(int *)(item->value) = lc->pint32_val;
440 set_bit(index, res_all.hdr.item_present);
444 /* Store an 64 bit integer at specified address */
445 void store_int64(LEX *lc, struct res_items *item, int index, int pass)
447 lex_get_token(lc, T_INT64);
448 *(int64_t *)(item->value) = lc->int64_val;
450 set_bit(index, res_all.hdr.item_present);
453 /* Store a size in bytes */
454 void store_size(LEX *lc, struct res_items *item, int index, int pass)
460 Dmsg0(400, "Enter store_size\n");
461 token = lex_get_token(lc, T_ALL);
465 Dmsg2(400, "size num=:%s: %f\n", lc->str, strtod(lc->str, NULL));
466 dvalue = strtod(lc->str, NULL);
467 if (errno != 0 || token < 0) {
468 scan_err1(lc, "expected a size number, got: %s", lc->str);
470 *(uint64_t *)(item->value) = (uint64_t)dvalue;
473 case T_UNQUOTED_STRING:
474 if (!size_to_uint64(lc->str, lc->str_len, &uvalue)) {
475 scan_err1(lc, "expected a size number, got: %s", lc->str);
477 *(uint64_t *)(item->value) = uvalue;
480 scan_err1(lc, "expected a size, got: %s", lc->str);
484 set_bit(index, res_all.hdr.item_present);
485 Dmsg0(400, "Leave store_size\n");
489 /* Store a time period in seconds */
490 void store_time(LEX *lc, struct res_items *item, int index, int pass)
496 token = lex_get_token(lc, T_ALL);
500 value = strtod(lc->str, NULL);
501 if (errno != 0 || value < 0) {
502 scan_err1(lc, "expected a time period, got: %s", lc->str);
504 *(utime_t *)(item->value) = (utime_t)value;
507 case T_UNQUOTED_STRING:
508 if (!duration_to_utime(lc->str, &utime)) {
509 scan_err1(lc, "expected a time period, got: %s", lc->str);
511 *(utime_t *)(item->value) = utime;
514 scan_err1(lc, "expected a time period, got: %s", lc->str);
518 set_bit(index, res_all.hdr.item_present);
522 /* Store a yes/no in a bit field */
523 void store_yesno(LEX *lc, struct res_items *item, int index, int pass)
525 lex_get_token(lc, T_NAME);
526 if (strcasecmp(lc->str, "yes") == 0) {
527 *(int *)(item->value) |= item->code;
528 } else if (strcasecmp(lc->str, "no") == 0) {
529 *(int *)(item->value) &= ~(item->code);
531 scan_err1(lc, "Expect a YES or NO, got: %s", lc->str);
534 set_bit(index, res_all.hdr.item_present);
552 * Return resource of type rcode that matches name
555 GetResWithName(int rcode, char *name)
558 int rindex = rcode - r_first;
561 res = resources[rindex].res_head;
563 if (strcmp(res->name, name) == 0) {
574 * Return next resource of type rcode. On first
575 * call second arg (res) is NULL, on subsequent
576 * calls, it is called with previous value.
579 GetNextRes(int rcode, RES *res)
582 int rindex = rcode - r_first;
586 Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
589 nres = resources[rindex].res_head;
603 /*********************************************************************
605 * Parse configuration file
609 parse_config(char *cf)
614 enum parse_state state = p_none;
615 struct res_items *items = NULL;
618 /* Make two passes. The first builds the name symbol table,
619 * and the second picks up the items.
621 Dmsg0(200, "Enter parse_config()\n");
622 for (pass=1; pass <= 2; pass++) {
623 Dmsg1(200, "parse_config pass %d\n", pass);
624 lc = lex_open_file(lc, cf, NULL);
625 while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
626 Dmsg1(150, "parse got token=%s\n", lex_tok_to_str(token));
629 if (token == T_EOL) {
632 if (token != T_IDENTIFIER) {
633 scan_err1(lc, "Expected a Resource name identifier, got: %s", lc->str);
636 for (i=0; resources[i].name; i++)
637 if (strcasecmp(resources[i].name, lc->str) == 0) {
639 items = resources[i].items;
640 res_type = resources[i].rcode;
641 init_resource(res_type, items);
644 if (state == p_none) {
645 scan_err1(lc, "expected resource name, got: %s", lc->str);
656 scan_err1(lc, "not in resource definition: %s", lc->str);
659 for (i=0; items[i].name; i++) {
660 if (strcasecmp(items[i].name, lc->str) == 0) {
661 /* If the ITEM_NO_EQUALS flag is set we do NOT
662 * scan for = after the keyword */
663 if (!(items[i].flags & ITEM_NO_EQUALS)) {
664 token = lex_get_token(lc, T_ALL);
665 Dmsg1 (150, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
666 if (token != T_EQUALS) {
667 scan_err1(lc, "expected an equals, got: %s", lc->str);
671 Dmsg1(150, "calling handler for %s\n", items[i].name);
672 /* Call item handler */
673 items[i].handler(lc, &items[i], i, pass);
679 Dmsg2(150, "level=%d id=%s\n", level, lc->str);
680 Dmsg1(150, "Keyword = %s\n", lc->str);
681 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
689 Dmsg0(150, "T_EOB => define new resource\n");
690 save_resource(res_type, items, pass); /* save resource */
697 scan_err2(lc, "unexpected token %d %s in resource definition",
698 token, lex_tok_to_str(token));
703 scan_err1(lc, "Unknown parser state %d\n", state);
707 if (debug_level > 50 && pass == 2) {
709 for (i=r_first; i<=r_last; i++) {
710 dump_resource(i, resources[i-r_first].res_head, prtmsg, NULL);
713 lc = lex_close_file(lc);
715 Dmsg0(200, "Leave parse_config()\n");
718 /*********************************************************************
720 * Free configuration resources
724 free_config_resources()
727 for (i=r_first; i<=r_last; i++) {