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