]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.c
06Mar05
[bacula/bacula] / bacula / src / lib / parse_conf.c
1 /*
2  *   Master Configuration routines.
3  *
4  *   This file contains the common parts of the Bacula
5  *   configuration routines.
6  *
7  *   Note, the configuration file parser consists of three parts
8  *
9  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
10  *
11  *   2. The generic config  scanner in lib/parse_conf.c and
12  *      lib/parse_conf.h.
13  *      These files contain the parser code, some utility
14  *      routines, and the common store routines (name, int,
15  *      string, time, int64, size, ...).
16  *
17  *   3. The daemon specific file, which contains the Resource
18  *      definitions as well as any specific store routines
19  *      for the resource records.
20  *
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
29  *         it is done.
30  *
31  *     Kern Sibbald, January MM
32  *
33  *   Version $Id$
34  */
35
36 /*
37    Copyright (C) 2000-2005 Kern Sibbald
38
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.
43
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.
48
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,
52    MA 02111-1307, USA.
53
54  */
55
56
57 #include "bacula.h"
58
59 extern int debug_level;
60
61 /* Each daemon has a slightly different set of
62  * resources, so it will define the following
63  * global values.
64  */
65 extern int r_first;
66 extern int r_last;
67 extern RES_TABLE resources[];
68 extern RES **res_head;
69
70 #ifdef HAVE_WIN32
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;
75 #else
76 extern  CURES res_all;
77 extern int res_all_size;
78 #endif
79
80
81 static brwlock_t res_lock;            /* resource lock */
82 static int res_locked = 0;            /* set when resource chains locked -- for debug */
83
84 /* Forward referenced subroutines */
85 static void scan_types(LEX *lc, MSGS *msg, int dest, char *where, char *cmd);
86
87
88 /* Common Resource definitions */
89
90 /* Message resource directives
91  *  name         handler      value       code   flags  default_value
92  */
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}
109 };
110
111 struct s_mtypes {
112    const char *name;
113    int token;
114 };
115 /* Various message types */
116 static struct s_mtypes msg_types[] = {
117    {"debug",         M_DEBUG},
118    {"abort",         M_ABORT},
119    {"fatal",         M_FATAL},
120    {"error",         M_ERROR},
121    {"warning",       M_WARNING},
122    {"info",          M_INFO},
123    {"saved",         M_SAVED},
124    {"notsaved",      M_NOTSAVED},
125    {"skipped",       M_SKIPPED},
126    {"mount",         M_MOUNT},
127    {"terminate",     M_TERM},
128    {"restored",      M_RESTORED},
129    {"security",      M_SECURITY},
130    {"alert",         M_ALERT},
131    {"all",           M_MAX+1},
132    {NULL,            0}
133 };
134
135 /* Used for certain KeyWord tables */
136 struct s_kw {
137    const char *name;
138    int token;
139 };
140
141 /*
142  * Tape Label types permitted in Pool records 
143  *
144  *   tape label      label code = token
145  */
146 struct s_kw tapelabels[] = {
147    {"bacula",        B_BACULA_LABEL},
148    {"ansi",          B_ANSI_LABEL},
149    {"ibm",           B_IBM_LABEL},
150    {NULL,            0}
151 };
152
153
154 /* Simply print a message */
155 static void prtmsg(void *sock, const char *fmt, ...)
156 {
157    va_list arg_ptr;
158
159    va_start(arg_ptr, fmt);
160    vfprintf(stdout, fmt, arg_ptr);
161    va_end(arg_ptr);
162 }
163
164 const char *res_to_str(int rcode)
165 {
166    if (rcode < r_first || rcode > r_last) {
167       return _("***UNKNOWN***");
168    } else {
169       return resources[rcode-r_first].name;
170    }
171 }
172
173
174 /*
175  * Initialize the static structure to zeros, then
176  *  apply all the default values.
177  */
178 void init_resource(int type, RES_ITEM *items, int pass)
179 {
180    int i;
181    int rindex = type - r_first;
182    static bool first = true;
183    int errstat;
184
185    if (first && (errstat=rwl_init(&res_lock)) != 0) {
186       Emsg1(M_ABORT, 0, _("Unable to initialize resource lock. ERR=%s\n"),
187             strerror(errstat));
188    }
189    first = false;
190
191    memset(&res_all, 0, res_all_size);
192    res_all.hdr.rcode = type;
193    res_all.hdr.refcnt = 1;
194
195    for (i=0; items[i].name; i++) {
196       Dmsg3(900, "Item=%s def=%s defval=%d\n", items[i].name,
197             (items[i].flags & ITEM_DEFAULT) ? "yes" : "no",
198             items[i].default_value);
199       if (items[i].flags & ITEM_DEFAULT && items[i].default_value != 0) {
200          if (items[i].handler == store_yesno) {
201             *(int *)(items[i].value) |= items[i].code;
202          } else if (items[i].handler == store_pint ||
203                     items[i].handler == store_int) {
204             *(int *)(items[i].value) = items[i].default_value;
205          } else if (items[i].handler == store_int64) {
206             *(int64_t *)(items[i].value) = items[i].default_value;
207          } else if (items[i].handler == store_size) {
208             *(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value;
209          } else if (items[i].handler == store_time) {
210             *(utime_t *)(items[i].value) = (utime_t)items[i].default_value;
211          } else if (pass == 1 && items[i].handler == store_addresses) {
212             init_default_addresses((dlist**)items[i].value, items[i].default_value);
213          }
214       }
215       /* If this triggers, take a look at lib/parse_conf.h */
216       if (i >= MAX_RES_ITEMS) {
217          Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
218       }
219    }
220 }
221
222
223 /* Store Messages Destination information */
224 void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass)
225 {
226    int token;
227    char *cmd;
228    POOLMEM *dest;
229    int dest_len;
230
231    Dmsg2(900, "store_msgs pass=%d code=%d\n", pass, item->code);
232    if (pass == 1) {
233       switch (item->code) {
234       case MD_STDOUT:
235       case MD_STDERR:
236       case MD_SYSLOG:              /* syslog */
237       case MD_CONSOLE:
238          scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
239          break;
240       case MD_OPERATOR:            /* send to operator */
241       case MD_DIRECTOR:            /* send to Director */
242       case MD_MAIL:                /* mail */
243       case MD_MAIL_ON_ERROR:       /* mail if Job errors */
244          if (item->code == MD_OPERATOR) {
245             cmd = res_all.res_msgs.operator_cmd;
246          } else {
247             cmd = res_all.res_msgs.mail_cmd;
248          }
249          dest = get_pool_memory(PM_MESSAGE);
250          dest[0] = 0;
251          dest_len = 0;
252          /* Pick up comma separated list of destinations */
253          for ( ;; ) {
254             token = lex_get_token(lc, T_NAME);   /* scan destination */
255             dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
256             if (dest[0] != 0) {
257                pm_strcat(dest, " ");  /* separate multiple destinations with space */
258                dest_len++;
259             }
260             pm_strcat(dest, lc->str);
261             dest_len += lc->str_len;
262             Dmsg2(900, "store_msgs newdest=%s: dest=%s:\n", lc->str, NPRT(dest));
263             token = lex_get_token(lc, T_SKIP_EOL);
264             if (token == T_COMMA) {
265                continue;           /* get another destination */
266             }
267             if (token != T_EQUALS) {
268                scan_err1(lc, _("expected an =, got: %s"), lc->str);
269             }
270             break;
271          }
272          Dmsg1(900, "mail_cmd=%s\n", NPRT(cmd));
273          scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
274          free_pool_memory(dest);
275          Dmsg0(900, "done with dest codes\n");
276          break;
277       case MD_FILE:                /* file */
278       case MD_APPEND:              /* append */
279          dest = get_pool_memory(PM_MESSAGE);
280          /* Pick up a single destination */
281          token = lex_get_token(lc, T_NAME);   /* scan destination */
282          pm_strcpy(dest, lc->str);
283          dest_len = lc->str_len;
284          token = lex_get_token(lc, T_SKIP_EOL);
285          Dmsg1(900, "store_msgs dest=%s:\n", NPRT(dest));
286          if (token != T_EQUALS) {
287             scan_err1(lc, _("expected an =, got: %s"), lc->str);
288          }
289          scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
290          free_pool_memory(dest);
291          Dmsg0(900, "done with dest codes\n");
292          break;
293
294       default:
295          scan_err1(lc, _("Unknown item code: %d\n"), item->code);
296          break;
297       }
298    }
299    scan_to_eol(lc);
300    set_bit(index, res_all.hdr.item_present);
301    Dmsg0(900, "Done store_msgs\n");
302 }
303
304 /*
305  * Scan for message types and add them to the message
306  * destination. The basic job here is to connect message types
307  *  (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
308  *  destination (MAIL, FILE, OPERATOR, ...)
309  */
310 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
311 {
312    int i, found, quit, is_not;
313    int msg_type = 0;
314    char *str;
315
316    for (quit=0; !quit;) {
317       lex_get_token(lc, T_NAME);            /* expect at least one type */
318       found = FALSE;
319       if (lc->str[0] == '!') {
320          is_not = TRUE;
321          str = &lc->str[1];
322       } else {
323          is_not = FALSE;
324          str = &lc->str[0];
325       }
326       for (i=0; msg_types[i].name; i++) {
327          if (strcasecmp(str, msg_types[i].name) == 0) {
328             msg_type = msg_types[i].token;
329             found = TRUE;
330             break;
331          }
332       }
333       if (!found) {
334          scan_err1(lc, _("message type: %s not found"), str);
335          /* NOT REACHED */
336       }
337
338       if (msg_type == M_MAX+1) {         /* all? */
339          for (i=1; i<=M_MAX; i++) {      /* yes set all types */
340             add_msg_dest(msg, dest_code, i, where, cmd);
341          }
342       } else {
343          if (is_not) {
344             rem_msg_dest(msg, dest_code, msg_type, where);
345          } else {
346             add_msg_dest(msg, dest_code, msg_type, where, cmd);
347          }
348       }
349       if (lc->ch != ',') {
350          break;
351       }
352       Dmsg0(900, "call lex_get_token() to eat comma\n");
353       lex_get_token(lc, T_ALL);          /* eat comma */
354    }
355    Dmsg0(900, "Done scan_types()\n");
356 }
357
358
359 /*
360  * This routine is ONLY for resource names
361  *  Store a name at specified address.
362  */
363 void store_name(LEX *lc, RES_ITEM *item, int index, int pass)
364 {
365    POOLMEM *msg = get_pool_memory(PM_EMSG);
366    lex_get_token(lc, T_NAME);
367    if (!is_name_valid(lc->str, &msg)) {
368       scan_err1(lc, "%s\n", msg);
369    }
370    free_pool_memory(msg);
371    /* Store the name both pass 1 and pass 2 */
372    if (*(item->value)) {
373       scan_err2(lc, _("Attempt to redefine name \"%s\" to \"%s\"."),
374          *(item->value), lc->str);
375    }
376    *(item->value) = bstrdup(lc->str);
377    scan_to_eol(lc);
378    set_bit(index, res_all.hdr.item_present);
379 }
380
381
382 /*
383  * Store a name string at specified address
384  * A name string is limited to MAX_RES_NAME_LENGTH
385  */
386 void store_strname(LEX *lc, RES_ITEM *item, int index, int pass)
387 {
388    lex_get_token(lc, T_NAME);
389    /* Store the name */
390    if (pass == 1) {
391       *(item->value) = bstrdup(lc->str);
392    }
393    scan_to_eol(lc);
394    set_bit(index, res_all.hdr.item_present);
395 }
396
397 /* Store a string at specified address */
398 void store_str(LEX *lc, RES_ITEM *item, int index, int pass)
399 {
400    lex_get_token(lc, T_STRING);
401    if (pass == 1) {
402       *(item->value) = bstrdup(lc->str);
403    }
404    scan_to_eol(lc);
405    set_bit(index, res_all.hdr.item_present);
406 }
407
408 /*
409  * Store a directory name at specified address. Note, we do
410  *   shell expansion except if the string begins with a vertical
411  *   bar (i.e. it will likely be passed to the shell later).
412  */
413 void store_dir(LEX *lc, RES_ITEM *item, int index, int pass)
414 {
415    lex_get_token(lc, T_STRING);
416    if (pass == 1) {
417       if (lc->str[0] != '|') {
418          do_shell_expansion(lc->str, sizeof(lc->str));
419       }
420       *(item->value) = bstrdup(lc->str);
421    }
422    scan_to_eol(lc);
423    set_bit(index, res_all.hdr.item_present);
424 }
425
426
427 /* Store a password specified address in MD5 coding */
428 void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
429 {
430    unsigned int i, j;
431    struct MD5Context md5c;
432    unsigned char signature[16];
433    char sig[100];
434
435
436    lex_get_token(lc, T_STRING);
437    if (pass == 1) {
438       MD5Init(&md5c);
439       MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
440       MD5Final(signature, &md5c);
441       for (i = j = 0; i < sizeof(signature); i++) {
442          sprintf(&sig[j], "%02x", signature[i]);
443          j += 2;
444       }
445       *(item->value) = bstrdup(sig);
446    }
447    scan_to_eol(lc);
448    set_bit(index, res_all.hdr.item_present);
449 }
450
451
452 /* Store a resource at specified address.
453  * If we are in pass 2, do a lookup of the
454  * resource.
455  */
456 void store_res(LEX *lc, RES_ITEM *item, int index, int pass)
457 {
458    RES *res;
459
460    lex_get_token(lc, T_NAME);
461    if (pass == 2) {
462       res = GetResWithName(item->code, lc->str);
463       if (res == NULL) {
464          scan_err3(lc, _("Could not find config Resource %s referenced on line %d : %s\n"),
465             lc->str, lc->line_no, lc->line);
466       }
467       if (*(item->value)) {
468          scan_err3(lc, _("Attempt to redefine resource \"%s\" referenced on line %d : %s\n"),
469             item->name, lc->line_no, lc->line);
470       }
471       *(item->value) = (char *)res;
472    }
473    scan_to_eol(lc);
474    set_bit(index, res_all.hdr.item_present);
475 }
476
477 /*
478  * Store a resource pointer in an alist. default_value indicates how many
479  *   times this routine can be called -- i.e. how many alists
480  *   there are.
481  * If we are in pass 2, do a lookup of the
482  *   resource.
483  */
484 void store_alist_res(LEX *lc, RES_ITEM *item, int index, int pass)
485 {
486    RES *res;
487    int count = item->default_value;
488    int i = 0;
489    alist *list;
490
491    if (pass == 2) {
492       if (count == 0) {               /* always store in item->value */
493          i = 0;
494          if ((item->value)[i] == NULL) {
495             list = New(alist(10, not_owned_by_alist));
496          } else {
497             list = (alist *)(item->value)[i];
498          }
499       } else {
500          /* Find empty place to store this directive */
501          while ((item->value)[i] != NULL && i++ < count) { }
502          if (i >= count) {
503             scan_err4(lc, _("Too many %s directives. Max. is %d. line %d: %s\n"),
504                lc->str, count, lc->line_no, lc->line);
505          }
506          list = New(alist(10, not_owned_by_alist));
507       }
508
509       for (;;) {
510          lex_get_token(lc, T_NAME);   /* scan next item */
511          res = GetResWithName(item->code, lc->str);
512          if (res == NULL) {
513             scan_err3(lc, _("Could not find config Resource \"%s\" referenced on line %d : %s\n"),
514                item->name, lc->line_no, lc->line);
515          }
516          Dmsg5(900, "Append %p to alist %p size=%d i=%d %s\n", 
517                res, list, list->size(), i, item->name);
518          list->append(res);
519          (item->value)[i] = (char *)list;
520          if (lc->ch != ',') {         /* if no other item follows */
521             break;                    /* get out */
522          }
523          lex_get_token(lc, T_ALL);    /* eat comma */
524       }
525    }
526    scan_to_eol(lc);
527    set_bit(index, res_all.hdr.item_present);
528 }
529
530
531 /*
532  * Store a string in an alist.
533  */
534 void store_alist_str(LEX *lc, RES_ITEM *item, int index, int pass)
535 {
536    alist *list;
537
538    if (pass == 2) {
539       if (*(item->value) == NULL) {
540          list = New(alist(10, owned_by_alist));
541       } else {
542          list = (alist *)(*(item->value));    
543       }
544
545       lex_get_token(lc, T_STRING);   /* scan next item */
546       Dmsg4(900, "Append %s to alist %p size=%d %s\n", 
547          lc->str, list, list->size(), item->name);
548       list->append(bstrdup(lc->str));
549       *(item->value) = (char *)list;
550    }
551    scan_to_eol(lc);
552    set_bit(index, res_all.hdr.item_present);
553 }
554
555
556
557 /*
558  * Store default values for Resource from xxxDefs
559  * If we are in pass 2, do a lookup of the
560  * resource and store everything not explicitly set
561  * in main resource.
562  *
563  * Note, here item points to the main resource (e.g. Job, not
564  *  the jobdefs, which we look up).
565  */
566 void store_defs(LEX *lc, RES_ITEM *item, int index, int pass)
567 {
568    RES *res;
569
570    lex_get_token(lc, T_NAME);
571    if (pass == 2) {
572      Dmsg2(900, "Code=%d name=%s\n", item->code, lc->str);
573      res = GetResWithName(item->code, lc->str);
574      if (res == NULL) {
575         scan_err3(lc, _("Missing config Resource \"%s\" referenced on line %d : %s\n"),
576            lc->str, lc->line_no, lc->line);
577      }
578      /* for each item not set, we copy the field from res */
579 #ifdef xxx
580      for (int i=0; item->name;; i++, item++) {
581         if (bit_is_set(i, res->item_present)) {
582            Dmsg2(900, "Item %d is present in %s\n", i, res->name);
583         } else {
584            Dmsg2(900, "Item %d is not present in %s\n", i, res->name);
585         }
586      }
587      /* ***FIXME **** add code */
588 #endif
589    }
590    scan_to_eol(lc);
591 }
592
593
594
595 /* Store an integer at specified address */
596 void store_int(LEX *lc, RES_ITEM *item, int index, int pass)
597 {
598    lex_get_token(lc, T_INT32);
599    *(int *)(item->value) = lc->int32_val;
600    scan_to_eol(lc);
601    set_bit(index, res_all.hdr.item_present);
602 }
603
604 /* Store a positive integer at specified address */
605 void store_pint(LEX *lc, RES_ITEM *item, int index, int pass)
606 {
607    lex_get_token(lc, T_PINT32);
608    *(int *)(item->value) = lc->pint32_val;
609    scan_to_eol(lc);
610    set_bit(index, res_all.hdr.item_present);
611 }
612
613
614 /* Store an 64 bit integer at specified address */
615 void store_int64(LEX *lc, RES_ITEM *item, int index, int pass)
616 {
617    lex_get_token(lc, T_INT64);
618    *(int64_t *)(item->value) = lc->int64_val;
619    scan_to_eol(lc);
620    set_bit(index, res_all.hdr.item_present);
621 }
622
623 /* Store a size in bytes */
624 void store_size(LEX *lc, RES_ITEM *item, int index, int pass)
625 {
626    int token;
627    uint64_t uvalue;
628
629    Dmsg0(900, "Enter store_size\n");
630    token = lex_get_token(lc, T_SKIP_EOL);
631    errno = 0;
632    switch (token) {
633    case T_NUMBER:
634    case T_IDENTIFIER:
635    case T_UNQUOTED_STRING:
636       if (!size_to_uint64(lc->str, lc->str_len, &uvalue)) {
637          scan_err1(lc, _("expected a size number, got: %s"), lc->str);
638       }
639       *(uint64_t *)(item->value) = uvalue;
640       break;
641    default:
642       scan_err1(lc, _("expected a size, got: %s"), lc->str);
643       break;
644    }
645    scan_to_eol(lc);
646    set_bit(index, res_all.hdr.item_present);
647    Dmsg0(900, "Leave store_size\n");
648 }
649
650
651 /* Store a time period in seconds */
652 void store_time(LEX *lc, RES_ITEM *item, int index, int pass)
653 {
654    int token;
655    utime_t utime;
656    char period[500];
657
658    token = lex_get_token(lc, T_SKIP_EOL);
659    errno = 0;
660    switch (token) {
661    case T_NUMBER:
662    case T_IDENTIFIER:
663    case T_UNQUOTED_STRING:
664       bstrncpy(period, lc->str, sizeof(period));
665       if (lc->ch == ' ') {
666          token = lex_get_token(lc, T_ALL);
667          switch (token) {
668          case T_IDENTIFIER:
669          case T_UNQUOTED_STRING:
670             bstrncat(period, lc->str, sizeof(period));
671             break;
672          }
673       }
674       if (!duration_to_utime(period, &utime)) {
675          scan_err1(lc, _("expected a time period, got: %s"), period);
676       }
677       *(utime_t *)(item->value) = utime;
678       break;
679    default:
680       scan_err1(lc, _("expected a time period, got: %s"), lc->str);
681       break;
682    }
683    if (token != T_EOL) {
684       scan_to_eol(lc);
685    }
686    set_bit(index, res_all.hdr.item_present);
687 }
688
689
690 /* Store a yes/no in a bit field */
691 void store_yesno(LEX *lc, RES_ITEM *item, int index, int pass)
692 {
693    lex_get_token(lc, T_NAME);
694    if (strcasecmp(lc->str, "yes") == 0) {
695       *(int *)(item->value) |= item->code;
696    } else if (strcasecmp(lc->str, "no") == 0) {
697       *(int *)(item->value) &= ~(item->code);
698    } else {
699       scan_err1(lc, _("Expect a YES or NO, got: %s"), lc->str);
700    }
701    scan_to_eol(lc);
702    set_bit(index, res_all.hdr.item_present);
703 }
704
705 /*
706  * Store Tape Label Type (Bacula, ANSI, IBM)
707  *
708  */
709 void store_label(LEX *lc, RES_ITEM *item, int index, int pass)
710 {
711    int token, i;
712
713    token = lex_get_token(lc, T_NAME);
714    /* Store the label pass 2 so that type is defined */
715    for (i=0; tapelabels[i].name; i++) {
716       if (strcasecmp(lc->str, tapelabels[i].name) == 0) {
717          *(int *)(item->value) = tapelabels[i].token;
718          i = 0;
719          break;
720       }
721    }
722    if (i != 0) {
723       scan_err1(lc, "Expected a Tape Label keyword, got: %s", lc->str);
724    }
725    scan_to_eol(lc);
726    set_bit(index, res_all.hdr.item_present);
727 }
728
729
730 /* #define TRACE_RES */
731
732 void b_LockRes(const char *file, int line)
733 {
734    int errstat;
735 #ifdef TRACE_RES
736    Pmsg4(000, "LockRes   %d,%d at %s:%d\n", res_locked, res_lock.w_active,
737          file, line);
738 #endif
739    if ((errstat=rwl_writelock(&res_lock)) != 0) {
740       Emsg3(M_ABORT, 0, "rwl_writelock failure at %s:%d:  ERR=%s\n",
741            file, line, strerror(errstat));
742    }
743    res_locked++;
744 }
745
746 void b_UnlockRes(const char *file, int line)
747 {
748    int errstat;
749    res_locked--;
750 #ifdef TRACE_RES
751    Pmsg4(000, "UnLockRes %d,%d at %s:%d\n", res_locked, res_lock.w_active,
752          file, line);
753 #endif
754    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
755       Emsg3(M_ABORT, 0, "rwl_writeunlock failure at %s:%d:. ERR=%s\n",
756            file, line, strerror(errstat));
757    }
758 }
759
760 /*
761  * Return resource of type rcode that matches name
762  */
763 RES *
764 GetResWithName(int rcode, char *name)
765 {
766    RES *res;
767    int rindex = rcode - r_first;
768
769    LockRes();
770    res = res_head[rindex];
771    while (res) {
772       if (strcmp(res->name, name) == 0) {
773          break;
774       }
775       res = res->next;
776    }
777    UnlockRes();
778    return res;
779
780 }
781
782 /*
783  * Return next resource of type rcode. On first
784  * call second arg (res) is NULL, on subsequent
785  * calls, it is called with previous value.
786  */
787 RES *
788 GetNextRes(int rcode, RES *res)
789 {
790    RES *nres;
791    int rindex = rcode - r_first;
792
793
794    if (!res_locked) {
795       Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
796    }
797    if (res == NULL) {
798       nres = res_head[rindex];
799    } else {
800       nres = res->next;
801    }
802    return nres;
803 }
804
805
806 /* Parser state */
807 enum parse_state {
808    p_none,
809    p_resource
810 };
811
812 /*********************************************************************
813  *
814  *      Parse configuration file
815  *
816  * Return 0 if reading failed, 1 otherwise
817  */
818 int
819 parse_config(const char *cf, int exit_on_error)
820 {
821    set_exit_on_error(exit_on_error);
822    LEX *lc = NULL;
823    int token, i, pass;
824    int res_type = 0;
825    enum parse_state state = p_none;
826    RES_ITEM *items = NULL;
827    int level = 0;
828
829    /* Make two passes. The first builds the name symbol table,
830     * and the second picks up the items.
831     */
832    Dmsg0(900, "Enter parse_config()\n");
833    for (pass=1; pass <= 2; pass++) {
834       Dmsg1(900, "parse_config pass %d\n", pass);
835       if ((lc = lex_open_file(lc, cf, NULL)) == NULL) {
836          set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
837          return 0;
838       }
839       while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
840          Dmsg1(900, "parse got token=%s\n", lex_tok_to_str(token));
841          switch (state) {
842          case p_none:
843             if (token == T_EOL) {
844                break;
845             }
846             if (token != T_IDENTIFIER) {
847                scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str);
848                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
849                return 0;
850             }
851             for (i=0; resources[i].name; i++)
852                if (strcasecmp(resources[i].name, lc->str) == 0) {
853                   state = p_resource;
854                   items = resources[i].items;
855                   res_type = resources[i].rcode;
856                   init_resource(res_type, items, pass);
857                   break;
858                }
859             if (state == p_none) {
860                scan_err1(lc, _("expected resource name, got: %s"), lc->str);
861                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
862                return 0;
863             }
864             break;
865          case p_resource:
866             switch (token) {
867             case T_BOB:
868                level++;
869                break;
870             case T_IDENTIFIER:
871                if (level != 1) {
872                   scan_err1(lc, _("not in resource definition: %s"), lc->str);
873                   set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
874                   return 0;
875                }
876                for (i=0; items[i].name; i++) {
877                   if (strcasecmp(items[i].name, lc->str) == 0) {
878                      /* If the ITEM_NO_EQUALS flag is set we do NOT
879                       *   scan for = after the keyword  */
880                      if (!(items[i].flags & ITEM_NO_EQUALS)) {
881                         token = lex_get_token(lc, T_SKIP_EOL);
882                         Dmsg1 (900, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
883                         if (token != T_EQUALS) {
884                            scan_err1(lc, _("expected an equals, got: %s"), lc->str);
885                            set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
886                            return 0;
887                         }
888                      }
889                      Dmsg1(900, "calling handler for %s\n", items[i].name);
890                      /* Call item handler */
891                      items[i].handler(lc, &items[i], i, pass);
892                      i = -1;
893                      break;
894                   }
895                }
896                if (i >= 0) {
897                   Dmsg2(900, "level=%d id=%s\n", level, lc->str);
898                   Dmsg1(900, "Keyword = %s\n", lc->str);
899                   scan_err1(lc, _("Keyword \"%s\" not permitted in this resource.\n"
900                      "Perhaps you left the trailing brace off of the previous resource."), lc->str);
901                   set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
902                   return 0;
903                }
904                break;
905
906             case T_EOB:
907                level--;
908                state = p_none;
909                Dmsg0(900, "T_EOB => define new resource\n");
910                save_resource(res_type, items, pass);  /* save resource */
911                break;
912
913             case T_EOL:
914                break;
915
916             default:
917                scan_err2(lc, _("unexpected token %d %s in resource definition"),
918                   token, lex_tok_to_str(token));
919                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
920                return 0;
921             }
922             break;
923          default:
924             scan_err1(lc, _("Unknown parser state %d\n"), state);
925             set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
926             return 0;
927          }
928       }
929       if (state != p_none) {
930          scan_err0(lc, _("End of conf file reached with unclosed resource."));
931          set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
932          return 0;
933       }
934       if (debug_level >= 900 && pass == 2) {
935          int i;
936          for (i=r_first; i<=r_last; i++) {
937             dump_resource(i, res_head[i-r_first], prtmsg, NULL);
938          }
939       }
940       lc = lex_close_file(lc);
941    }
942    Dmsg0(900, "Leave parse_config()\n");
943    set_exit_on_error(1);
944    return 1;
945 }
946
947 /*********************************************************************
948  *
949  *      Free configuration resources
950  *
951  */
952 void free_config_resources()
953 {
954    for (int i=r_first; i<=r_last; i++) {
955       free_resource(res_head[i-r_first], i);
956       res_head[i-r_first] = NULL;
957    }
958 }
959
960 RES **save_config_resources()
961 {
962    int num = r_last - r_first + 1;
963    RES **res = (RES **)malloc(num*sizeof(RES *));
964    for (int i=0; i<num; i++) {
965       res[i] = res_head[i];
966       res_head[i] = NULL;
967    }
968    return res;
969 }
970
971 RES **new_res_head()
972 {
973    int size = (r_last - r_first + 1) * sizeof(RES *);
974    RES **res = (RES **)malloc(size);
975    memset(res, 0, size);
976    return res;
977 }