]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/parse_conf.c
Pool + label cleanups from bug reports
[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).
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-2004 Kern Sibbald and John Walker
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    {"all",           M_MAX+1},
130    {NULL,            0}
131 };
132
133
134 /* Simply print a message */
135 static void prtmsg(void *sock, const char *fmt, ...)
136 {
137    va_list arg_ptr;
138
139    va_start(arg_ptr, fmt);
140    vfprintf(stdout, fmt, arg_ptr);
141    va_end(arg_ptr);
142 }
143
144 const char *res_to_str(int rcode)
145 {
146    if (rcode < r_first || rcode > r_last) {
147       return _("***UNKNOWN***");
148    } else {
149       return resources[rcode-r_first].name;
150    }
151 }
152
153
154 /* 
155  * Initialize the static structure to zeros, then
156  *  apply all the default values.
157  */
158 void init_resource(int type, RES_ITEM *items)
159 {
160    int i;
161    int rindex = type - r_first;
162    static bool first = true;
163    int errstat;
164
165    if (first && (errstat=rwl_init(&res_lock)) != 0) {
166       Emsg1(M_ABORT, 0, _("Unable to initialize resource lock. ERR=%s\n"), 
167             strerror(errstat));
168    }
169    first = false;
170
171    memset(&res_all, 0, res_all_size);
172    res_all.hdr.rcode = type;
173    res_all.hdr.refcnt = 1;
174
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          }
192       }
193       /* If this triggers, take a look at lib/parse_conf.h */
194       if (i >= MAX_RES_ITEMS) {
195          Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
196       }
197    }
198 }
199
200
201 /* Store Messages Destination information */
202 void store_msgs(LEX *lc, RES_ITEM *item, int index, int pass)
203 {
204    int token;
205    char *cmd;
206    POOLMEM *dest;
207    int dest_len;    
208
209    Dmsg2(900, "store_msgs pass=%d code=%d\n", pass, item->code);
210    if (pass == 1) {
211       switch (item->code) {
212       case MD_STDOUT:
213       case MD_STDERR:
214       case MD_SYSLOG:              /* syslog */
215       case MD_CONSOLE:
216          scan_types(lc, (MSGS *)(item->value), item->code, NULL, NULL);
217          break;
218       case MD_OPERATOR:            /* send to operator */
219       case MD_DIRECTOR:            /* send to Director */
220       case MD_MAIL:                /* mail */
221       case MD_MAIL_ON_ERROR:       /* mail if Job errors */
222          if (item->code == MD_OPERATOR) {
223             cmd = res_all.res_msgs.operator_cmd;
224          } else {
225             cmd = res_all.res_msgs.mail_cmd;
226          }
227          dest = get_pool_memory(PM_MESSAGE);
228          dest[0] = 0;
229          dest_len = 0;
230          /* Pick up comma separated list of destinations */
231          for ( ;; ) {
232             token = lex_get_token(lc, T_NAME);   /* scan destination */
233             dest = check_pool_memory_size(dest, dest_len + lc->str_len + 2);
234             if (dest[0] != 0) {
235                pm_strcat(&dest, " ");  /* separate multiple destinations with space */
236                dest_len++;
237             }
238             pm_strcat(&dest, lc->str);
239             dest_len += lc->str_len;
240             Dmsg2(900, "store_msgs newdest=%s: dest=%s:\n", lc->str, NPRT(dest));
241             token = lex_get_token(lc, T_ALL);
242             if (token == T_COMMA) { 
243                continue;           /* get another destination */
244             }
245             if (token != T_EQUALS) {
246                scan_err1(lc, _("expected an =, got: %s"), lc->str); 
247             }
248             break;
249          }
250          Dmsg1(900, "mail_cmd=%s\n", NPRT(cmd));
251          scan_types(lc, (MSGS *)(item->value), item->code, dest, cmd);
252          free_pool_memory(dest);
253          Dmsg0(900, "done with dest codes\n");
254          break;
255       case MD_FILE:                /* file */
256       case MD_APPEND:              /* append */
257          dest = get_pool_memory(PM_MESSAGE);
258          /* Pick up a single destination */
259          token = lex_get_token(lc, T_NAME);   /* scan destination */
260          pm_strcpy(&dest, lc->str);
261          dest_len = lc->str_len;
262          token = lex_get_token(lc, T_ALL);
263          Dmsg1(900, "store_msgs dest=%s:\n", NPRT(dest));
264          if (token != T_EQUALS) {
265             scan_err1(lc, _("expected an =, got: %s"), lc->str); 
266          }
267          scan_types(lc, (MSGS *)(item->value), item->code, dest, NULL);
268          free_pool_memory(dest);
269          Dmsg0(900, "done with dest codes\n");
270          break;
271
272       default:
273          scan_err1(lc, _("Unknown item code: %d\n"), item->code);
274          break;
275       }
276    }
277    scan_to_eol(lc);
278    set_bit(index, res_all.hdr.item_present);
279    Dmsg0(900, "Done store_msgs\n");
280 }
281
282 /* 
283  * Scan for message types and add them to the message
284  * destination. The basic job here is to connect message types
285  *  (WARNING, ERROR, FATAL, INFO, ...) with an appropriate
286  *  destination (MAIL, FILE, OPERATOR, ...)
287  */
288 static void scan_types(LEX *lc, MSGS *msg, int dest_code, char *where, char *cmd)
289 {
290    int i, found, quit, is_not;
291    int msg_type = 0;
292    char *str;
293
294    for (quit=0; !quit;) {
295       lex_get_token(lc, T_NAME);            /* expect at least one type */       
296       found = FALSE;
297       if (lc->str[0] == '!') {
298          is_not = TRUE;
299          str = &lc->str[1];
300       } else {
301          is_not = FALSE;
302          str = &lc->str[0];
303       }
304       for (i=0; msg_types[i].name; i++) {
305          if (strcasecmp(str, msg_types[i].name) == 0) {
306             msg_type = msg_types[i].token;
307             found = TRUE;
308             break;
309          }
310       }
311       if (!found) {
312          scan_err1(lc, _("message type: %s not found"), str);
313          /* NOT REACHED */
314       }
315
316       if (msg_type == M_MAX+1) {         /* all? */
317          for (i=1; i<=M_MAX; i++) {      /* yes set all types */
318             add_msg_dest(msg, dest_code, i, where, cmd);
319          }
320       } else {
321          if (is_not) {
322             rem_msg_dest(msg, dest_code, msg_type, where);
323          } else {
324             add_msg_dest(msg, dest_code, msg_type, where, cmd);
325          }
326       }
327       if (lc->ch != ',') {
328          break;
329       }
330       Dmsg0(900, "call lex_get_token() to eat comma\n");
331       lex_get_token(lc, T_ALL);          /* eat comma */
332    }
333    Dmsg0(900, "Done scan_types()\n");
334 }
335
336
337 /* 
338  * This routine is ONLY for resource names
339  *  Store a name at specified address.
340  */
341 void store_name(LEX *lc, RES_ITEM *item, int index, int pass)
342 {
343    POOLMEM *msg = get_pool_memory(PM_EMSG);
344    lex_get_token(lc, T_NAME);
345    if (!is_name_valid(lc->str, &msg)) {
346       scan_err1(lc, "%s\n", msg);
347    }
348    free_pool_memory(msg);
349    /* Store the name both pass 1 and pass 2 */
350    if (*(item->value)) {
351       scan_err2(lc, _("Attempt to redefine name \"%s\" to \"%s\"."), 
352          *(item->value), lc->str);
353    }
354    *(item->value) = bstrdup(lc->str);
355    scan_to_eol(lc);
356    set_bit(index, res_all.hdr.item_present);
357 }
358
359
360 /*
361  * Store a name string at specified address
362  * A name string is limited to MAX_RES_NAME_LENGTH
363  */
364 void store_strname(LEX *lc, RES_ITEM *item, int index, int pass)
365 {
366    lex_get_token(lc, T_NAME);
367    /* Store the name */
368    if (pass == 1) {
369       *(item->value) = bstrdup(lc->str);
370    }
371    scan_to_eol(lc);
372    set_bit(index, res_all.hdr.item_present);
373 }
374
375 /* Store a string at specified address */
376 void store_str(LEX *lc, RES_ITEM *item, int index, int pass)
377 {
378    lex_get_token(lc, T_STRING);
379    if (pass == 1) {
380       *(item->value) = bstrdup(lc->str);
381    }
382    scan_to_eol(lc);
383    set_bit(index, res_all.hdr.item_present);
384 }
385
386 /*
387  * Store a directory name at specified address. Note, we do
388  *   shell expansion except if the string begins with a vertical
389  *   bar (i.e. it will likely be passed to the shell later).
390  */
391 void store_dir(LEX *lc, RES_ITEM *item, int index, int pass)
392 {
393    lex_get_token(lc, T_STRING);
394    if (pass == 1) {
395       if (lc->str[0] != '|') {
396          do_shell_expansion(lc->str, sizeof(lc->str));
397       }
398       *(item->value) = bstrdup(lc->str);
399    }
400    scan_to_eol(lc);
401    set_bit(index, res_all.hdr.item_present);
402 }
403
404
405 /* Store a password specified address in MD5 coding */
406 void store_password(LEX *lc, RES_ITEM *item, int index, int pass)
407 {
408    unsigned int i, j;
409    struct MD5Context md5c;
410    unsigned char signature[16];
411    char sig[100];
412
413
414    lex_get_token(lc, T_STRING);
415    if (pass == 1) {
416       MD5Init(&md5c);
417       MD5Update(&md5c, (unsigned char *) (lc->str), lc->str_len);
418       MD5Final(signature, &md5c);
419       for (i = j = 0; i < sizeof(signature); i++) {
420          sprintf(&sig[j], "%02x", signature[i]); 
421          j += 2;
422       }
423       *(item->value) = bstrdup(sig);
424    }
425    scan_to_eol(lc);
426    set_bit(index, res_all.hdr.item_present);
427 }
428
429
430 /* Store a resource at specified address.
431  * If we are in pass 2, do a lookup of the 
432  * resource.
433  */
434 void store_res(LEX *lc, RES_ITEM *item, int index, int pass)
435 {
436    RES *res;
437
438    lex_get_token(lc, T_NAME);
439    if (pass == 2) {
440      res = GetResWithName(item->code, lc->str);
441      if (res == NULL) {
442         scan_err3(lc, _("Could not find config Resource %s referenced on line %d : %s\n"), 
443            lc->str, lc->line_no, lc->line);
444      }
445      if (*(item->value)) {
446         scan_err3(lc, _("Attempt to redefine resource \"%s\" referenced on line %d : %s\n"), 
447            lc->str, lc->line_no, lc->line);
448      }
449      *(item->value) = (char *)res;
450    }
451    scan_to_eol(lc);
452    set_bit(index, res_all.hdr.item_present);
453 }
454
455 /*
456  * Store default values for Resource from xxxDefs
457  * If we are in pass 2, do a lookup of the 
458  * resource and store everything not explicitly set
459  * in main resource.
460  *
461  * Note, here item points to the main resource (e.g. Job, not
462  *  the jobdefs, which we look up).
463  */
464 void store_defs(LEX *lc, RES_ITEM *item, int index, int pass)
465 {
466    RES *res;
467
468    lex_get_token(lc, T_NAME);
469    if (pass == 2) {
470      Dmsg2(900, "Code=%d name=%s\n", item->code, lc->str);
471      res = GetResWithName(item->code, lc->str);
472      if (res == NULL) {
473         scan_err3(lc, _("Missing config Resource \"%s\" referenced on line %d : %s\n"), 
474            lc->str, lc->line_no, lc->line);
475      }
476      /* for each item not set, we copy the field from res */
477 #ifdef xxx
478      for (int i=0; item->name;; i++, item++) {
479         if (bit_is_set(i, res->item_present)) {
480            Dmsg2(900, "Item %d is present in %s\n", i, res->name);
481         } else {
482            Dmsg2(900, "Item %d is not present in %s\n", i, res->name);
483         }
484      }
485      /* ***FIXME **** add code */
486 #endif
487    }
488    scan_to_eol(lc);
489 }
490
491
492
493 /* Store an integer at specified address */
494 void store_int(LEX *lc, RES_ITEM *item, int index, int pass)
495 {
496    lex_get_token(lc, T_INT32);
497    *(int *)(item->value) = lc->int32_val;
498    scan_to_eol(lc);
499    set_bit(index, res_all.hdr.item_present);
500 }
501
502 /* Store a positive integer at specified address */
503 void store_pint(LEX *lc, RES_ITEM *item, int index, int pass)
504 {
505    lex_get_token(lc, T_PINT32);
506    *(int *)(item->value) = lc->pint32_val;
507    scan_to_eol(lc);
508    set_bit(index, res_all.hdr.item_present);
509 }
510
511
512 /* Store an 64 bit integer at specified address */
513 void store_int64(LEX *lc, RES_ITEM *item, int index, int pass)
514 {
515    lex_get_token(lc, T_INT64);
516    *(int64_t *)(item->value) = lc->int64_val;
517    scan_to_eol(lc);
518    set_bit(index, res_all.hdr.item_present);
519 }
520
521 /* Store a size in bytes */
522 void store_size(LEX *lc, RES_ITEM *item, int index, int pass)
523 {
524    int token;
525    uint64_t uvalue;
526
527    Dmsg0(900, "Enter store_size\n");
528    token = lex_get_token(lc, T_ALL);
529    errno = 0;
530    switch (token) {
531    case T_NUMBER:
532    case T_IDENTIFIER:
533    case T_UNQUOTED_STRING:
534       if (!size_to_uint64(lc->str, lc->str_len, &uvalue)) {
535          scan_err1(lc, _("expected a size number, got: %s"), lc->str);
536       }
537       *(uint64_t *)(item->value) = uvalue;
538       break;
539    default:
540       scan_err1(lc, _("expected a size, got: %s"), lc->str);
541       break;
542    }
543    scan_to_eol(lc);
544    set_bit(index, res_all.hdr.item_present);
545    Dmsg0(900, "Leave store_size\n");
546 }
547
548
549 /* Store a time period in seconds */
550 void store_time(LEX *lc, RES_ITEM *item, int index, int pass)
551 {
552    int token; 
553    utime_t utime;
554    char period[500];
555
556    token = lex_get_token(lc, T_ALL);
557    errno = 0;
558    switch (token) {
559    case T_NUMBER:
560    case T_IDENTIFIER:
561    case T_UNQUOTED_STRING:
562       bstrncpy(period, lc->str, sizeof(period));
563       if (lc->ch == ' ') {
564          token = lex_get_token(lc, T_ALL);
565          switch (token) {
566          case T_IDENTIFIER:
567          case T_UNQUOTED_STRING:
568             bstrncat(period, lc->str, sizeof(period));
569             break;
570          }
571       }
572       if (!duration_to_utime(period, &utime)) {
573          scan_err1(lc, _("expected a time period, got: %s"), period);
574       }
575       *(utime_t *)(item->value) = utime;
576       break;
577    default:
578       scan_err1(lc, _("expected a time period, got: %s"), lc->str);
579       break;
580    }
581    if (token != T_EOL) {
582       scan_to_eol(lc);
583    }
584    set_bit(index, res_all.hdr.item_present);
585 }
586
587
588 /* Store a yes/no in a bit field */
589 void store_yesno(LEX *lc, RES_ITEM *item, int index, int pass)
590 {
591    lex_get_token(lc, T_NAME);
592    if (strcasecmp(lc->str, "yes") == 0) {
593       *(int *)(item->value) |= item->code;
594    } else if (strcasecmp(lc->str, "no") == 0) {
595       *(int *)(item->value) &= ~(item->code);
596    } else {
597       scan_err1(lc, _("Expect a YES or NO, got: %s"), lc->str);
598    }
599    scan_to_eol(lc);
600    set_bit(index, res_all.hdr.item_present);
601 }
602
603
604 /* #define TRACE_RES */
605
606 void b_LockRes(const char *file, int line)
607 {
608    int errstat;
609 #ifdef TRACE_RES
610    Dmsg4(000, "LockRes   %d,%d at %s:%d\n", res_locked, res_lock.w_active,
611          file, line);
612 #endif
613    if ((errstat=rwl_writelock(&res_lock)) != 0) {
614       Emsg3(M_ABORT, 0, "rwl_writelock failure at %s:%d:  ERR=%s\n",
615            file, line, strerror(errstat));
616    }
617    res_locked++;
618 }
619
620 void b_UnlockRes(const char *file, int line)
621 {
622    int errstat;
623    res_locked--;
624 #ifdef TRACE_RES
625    Dmsg4(000, "UnLockRes %d,%d at %s:%d\n", res_locked, res_lock.w_active,
626          file, line);
627 #endif
628    if ((errstat=rwl_writeunlock(&res_lock)) != 0) {
629       Emsg3(M_ABORT, 0, "rwl_writeunlock failure at %s:%d:. ERR=%s\n",
630            file, line, strerror(errstat));
631    }
632 }
633
634 /*
635  * Return resource of type rcode that matches name
636  */
637 RES *
638 GetResWithName(int rcode, char *name)
639 {
640    RES *res;
641    int rindex = rcode - r_first;
642
643    LockRes();
644    res = res_head[rindex];
645    while (res) {
646       if (strcmp(res->name, name) == 0) {
647          break;
648       }
649       res = res->next;
650    }
651    UnlockRes();
652    return res;
653    
654 }
655
656 /*
657  * Return next resource of type rcode. On first
658  * call second arg (res) is NULL, on subsequent
659  * calls, it is called with previous value.
660  */
661 RES *
662 GetNextRes(int rcode, RES *res)
663 {
664    RES *nres;
665    int rindex = rcode - r_first;
666        
667
668    if (!res_locked) {
669       Emsg0(M_ABORT, 0, "Resource chain not locked.\n");
670    }
671    if (res == NULL) {
672       nres = res_head[rindex];
673    } else {
674       nres = res->next;
675    }
676    return nres;
677 }
678
679
680 /* Parser state */
681 enum parse_state {
682    p_none,
683    p_resource
684 };
685
686 /*********************************************************************
687  *
688  *      Parse configuration file
689  * 
690  * Return 0 if reading failed, 1 otherwise
691  */
692 int 
693 parse_config(const char *cf, int exit_on_error)
694 {
695    set_exit_on_error(exit_on_error);
696    LEX *lc = NULL;
697    int token, i, pass;
698    int res_type = 0;
699    enum parse_state state = p_none;
700    RES_ITEM *items = NULL;
701    int level = 0;
702
703    /* Make two passes. The first builds the name symbol table,
704     * and the second picks up the items. 
705     */
706    Dmsg0(900, "Enter parse_config()\n");
707    for (pass=1; pass <= 2; pass++) {
708       Dmsg1(900, "parse_config pass %d\n", pass);
709       if ((lc = lex_open_file(lc, cf, NULL)) == NULL) {
710          set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
711          return 0;
712       }
713       while ((token=lex_get_token(lc, T_ALL)) != T_EOF) {
714          Dmsg1(900, "parse got token=%s\n", lex_tok_to_str(token));
715          switch (state) {
716          case p_none:
717             if (token == T_EOL) {
718                break;
719             }
720             if (token != T_IDENTIFIER) {
721                scan_err1(lc, _("Expected a Resource name identifier, got: %s"), lc->str);
722                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
723                return 0;
724             }
725             for (i=0; resources[i].name; i++)
726                if (strcasecmp(resources[i].name, lc->str) == 0) {
727                   state = p_resource;
728                   items = resources[i].items;
729                   res_type = resources[i].rcode;
730                   init_resource(res_type, items);
731                   break;
732                }
733             if (state == p_none) {
734                scan_err1(lc, _("expected resource name, got: %s"), lc->str);
735                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
736                return 0;
737             }
738             break;
739          case p_resource:
740             switch (token) {
741             case T_BOB:
742                level++;
743                break;
744             case T_IDENTIFIER:
745                if (level != 1) {
746                   scan_err1(lc, _("not in resource definition: %s"), lc->str);
747                   set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
748                   return 0;
749                }
750                for (i=0; items[i].name; i++) {
751                   if (strcasecmp(items[i].name, lc->str) == 0) {
752                      /* If the ITEM_NO_EQUALS flag is set we do NOT              
753                       *   scan for = after the keyword  */
754                      if (!(items[i].flags & ITEM_NO_EQUALS)) {
755                         token = lex_get_token(lc, T_ALL);
756                         Dmsg1 (900, "in T_IDENT got token=%s\n", lex_tok_to_str(token));
757                         if (token != T_EQUALS) {
758                            scan_err1(lc, _("expected an equals, got: %s"), lc->str);
759                            set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
760                            return 0;
761                         }
762                      }
763                      Dmsg1(900, "calling handler for %s\n", items[i].name);
764                      /* Call item handler */
765                      items[i].handler(lc, &items[i], i, pass);
766                      i = -1;
767                      break;
768                   }
769                }
770                if (i >= 0) {
771                   Dmsg2(900, "level=%d id=%s\n", level, lc->str);
772                   Dmsg1(900, "Keyword = %s\n", lc->str);
773                   scan_err1(lc, _("Keyword \"%s\" not permitted in this resource.\n"
774                      "Perhaps you left the trailing brace off of the previous resource."), lc->str);
775                   set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
776                   return 0;
777                }
778                break;
779
780             case T_EOB:
781                level--;
782                state = p_none;
783                Dmsg0(900, "T_EOB => define new resource\n");
784                save_resource(res_type, items, pass);  /* save resource */
785                break;
786
787             case T_EOL:
788                break;
789
790             default:
791                scan_err2(lc, _("unexpected token %d %s in resource definition"),    
792                   token, lex_tok_to_str(token));
793                set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
794                return 0;
795             }
796             break;
797          default:
798             scan_err1(lc, _("Unknown parser state %d\n"), state);
799             set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
800             return 0;
801          }
802       }
803       if (state != p_none) {
804          scan_err0(lc, _("End of conf file reached with unclosed resource."));
805          set_exit_on_error(1); /* Never reached if exit_on_error == 1 */
806          return 0;
807       }
808       if (debug_level >= 900 && pass == 2) {
809          int i;
810          for (i=r_first; i<=r_last; i++) {
811             dump_resource(i, res_head[i-r_first], prtmsg, NULL);
812          }
813       }
814       lc = lex_close_file(lc);
815    }
816    Dmsg0(900, "Leave parse_config()\n");
817    set_exit_on_error(1);
818    return 1;
819 }
820
821 /*********************************************************************
822  *
823  *      Free configuration resources
824  *
825  */
826 void free_config_resources()
827 {
828    for (int i=r_first; i<=r_last; i++) {
829       free_resource(res_head[i-r_first], i);
830       res_head[i-r_first] = NULL;
831    }
832 }
833
834 RES **save_config_resources() 
835 {
836    int num = r_last - r_first + 1;
837    RES **res = (RES **)malloc(num*sizeof(RES *));
838    for (int i=0; i<num; i++) {
839       res[i] = res_head[i];
840       res_head[i] = NULL;
841    }
842    return res;
843 }
844
845 RES **new_res_head()
846 {
847    int size = (r_last - r_first + 1) * sizeof(RES *);
848    RES **res = (RES **)malloc(size);
849    memset(res, 0, size);
850    return res;
851 }