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