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