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