2 * Configuration file parser for new and old Include and
5 * Kern Sibbald, March MMIII
10 Copyright (C) 2003-2004 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 /* Forward referenced subroutines */
37 void store_inc(LEX *lc, RES_ITEM *item, int index, int pass);
39 static void store_newinc(LEX *lc, RES_ITEM *item, int index, int pass);
40 static void store_regex(LEX *lc, RES_ITEM *item, int index, int pass);
41 static void store_wild(LEX *lc, RES_ITEM *item, int index, int pass);
42 static void store_opts(LEX *lc, RES_ITEM *item, int index, int pass);
43 static void store_fname(LEX *lc, RES_ITEM *item, int index, int pass);
44 static void options_res(LEX *lc, RES_ITEM *item, int index, int pass);
45 static void store_base(LEX *lc, RES_ITEM *item, int index, int pass);
46 static void store_reader(LEX *lc, RES_ITEM *item, int index, int pass);
47 static void store_writer(LEX *lc, RES_ITEM *item, int index, int pass);
48 static void setup_current_opts(void);
51 /* We build the current resource here as we are
52 * scanning the resource configuration definition,
53 * then move it to allocated memory when the resource
57 extern int res_all_size;
59 /* We build the current new Include and Exclude items here */
60 static INCEXE res_incexe;
63 * new Include/Exclude items
64 * name handler value code flags default_value
66 static RES_ITEM newinc_items[] = {
67 {"file", store_fname, NULL, 0, 0, 0},
68 {"options", options_res, NULL, 0, 0, 0},
69 {NULL, NULL, NULL, 0, 0, 0}
73 * Items that are valid in an Options resource
75 static RES_ITEM options_items[] = {
76 {"compression", store_opts, NULL, 0, 0, 0},
77 {"signature", store_opts, NULL, 0, 0, 0},
78 {"verify", store_opts, NULL, 0, 0, 0},
79 {"onefs", store_opts, NULL, 0, 0, 0},
80 {"recurse", store_opts, NULL, 0, 0, 0},
81 {"sparse", store_opts, NULL, 0, 0, 0},
82 {"readfifo", store_opts, NULL, 0, 0, 0},
83 {"replace", store_opts, NULL, 0, 0, 0},
84 {"portable", store_opts, NULL, 0, 0, 0},
85 {"mtimeonly", store_opts, NULL, 0, 0, 0},
86 {"keepatime", store_opts, NULL, 0, 0, 0},
87 {"regex", store_regex, NULL, 0, 0, 0},
88 {"base", store_base, NULL, 0, 0, 0},
89 {"wild", store_wild, NULL, 0, 0, 0},
90 {"exclude", store_opts, NULL, 0, 0, 0},
91 {"aclsupport", store_opts, NULL, 0, 0, 0},
92 {"reader", store_reader, NULL, 0, 0, 0},
93 {"writer", store_writer, NULL, 0, 0, 0},
94 {NULL, NULL, NULL, 0, 0, 0}
98 /* Define FileSet KeyWord values */
109 INC_KW_REPLACE, /* restore options */
110 INC_KW_READFIFO, /* Causes fifo data to be read */
119 * Include keywords -- these are keywords that can appear
120 * in the options lists of an old include ( Include = compression= ...)
122 static struct s_kw FS_option_kw[] = {
123 {"compression", INC_KW_COMPRESSION},
124 {"signature", INC_KW_SIGNATURE},
125 {"encryption", INC_KW_ENCRYPTION},
126 {"verify", INC_KW_VERIFY},
127 {"onefs", INC_KW_ONEFS},
128 {"recurse", INC_KW_RECURSE},
129 {"sparse", INC_KW_SPARSE},
130 {"replace", INC_KW_REPLACE},
131 {"readfifo", INC_KW_READFIFO},
132 {"portable", INC_KW_PORTABLE},
133 {"mtimeonly", INC_KW_MTIMEONLY},
134 {"keepatime", INC_KW_KEEPATIME},
135 {"exclude", INC_KW_EXCLUDE},
136 {"aclsupport", INC_KW_ACL},
140 /* Options for FileSet keywords */
149 * Options permitted for each keyword and resulting value.
150 * The output goes into opts, which are then transmitted to
151 * the FD for application as options to the following list of
154 static struct s_fs_opt FS_options[] = {
155 {"md5", INC_KW_SIGNATURE, "M"},
156 {"sha1", INC_KW_SIGNATURE, "S"},
157 {"gzip", INC_KW_COMPRESSION, "Z6"},
158 {"gzip1", INC_KW_COMPRESSION, "Z1"},
159 {"gzip2", INC_KW_COMPRESSION, "Z2"},
160 {"gzip3", INC_KW_COMPRESSION, "Z3"},
161 {"gzip4", INC_KW_COMPRESSION, "Z4"},
162 {"gzip5", INC_KW_COMPRESSION, "Z5"},
163 {"gzip6", INC_KW_COMPRESSION, "Z6"},
164 {"gzip7", INC_KW_COMPRESSION, "Z7"},
165 {"gzip8", INC_KW_COMPRESSION, "Z8"},
166 {"gzip9", INC_KW_COMPRESSION, "Z9"},
167 {"blowfish", INC_KW_ENCRYPTION, "B"}, /* ***FIXME*** not implemented */
168 {"3des", INC_KW_ENCRYPTION, "3"}, /* ***FIXME*** not implemented */
169 {"yes", INC_KW_ONEFS, "0"},
170 {"no", INC_KW_ONEFS, "f"},
171 {"yes", INC_KW_RECURSE, "0"},
172 {"no", INC_KW_RECURSE, "h"},
173 {"yes", INC_KW_SPARSE, "s"},
174 {"no", INC_KW_SPARSE, "0"},
175 {"always", INC_KW_REPLACE, "a"},
176 {"ifnewer", INC_KW_REPLACE, "w"},
177 {"never", INC_KW_REPLACE, "n"},
178 {"yes", INC_KW_READFIFO, "r"},
179 {"no", INC_KW_READFIFO, "0"},
180 {"yes", INC_KW_PORTABLE, "p"},
181 {"no", INC_KW_PORTABLE, "0"},
182 {"yes", INC_KW_MTIMEONLY, "m"},
183 {"no", INC_KW_MTIMEONLY, "0"},
184 {"yes", INC_KW_KEEPATIME, "k"},
185 {"no", INC_KW_KEEPATIME, "0"},
186 {"yes", INC_KW_EXCLUDE, "e"},
187 {"no", INC_KW_EXCLUDE, "0"},
188 {"yes", INC_KW_ACL, "A"},
189 {"no", INC_KW_ACL, "0"},
196 * Scan for right hand side of Include options (keyword=option) is
197 * converted into one or two characters. Verifyopts=xxxx is Vxxxx:
198 * Whatever is found is concatenated to the opts string.
199 * This code is also used inside an Options resource.
201 static void scan_include_options(LEX *lc, int keyword, char *opts, int optlen)
206 option[0] = 0; /* default option = none */
207 option[2] = 0; /* terminate options */
208 token = lex_get_token(lc, T_NAME); /* expect at least one option */
209 if (keyword == INC_KW_VERIFY) { /* special case */
210 /* ***FIXME**** ensure these are in permitted set */
211 bstrncat(opts, "V", optlen); /* indicate Verify */
212 bstrncat(opts, lc->str, optlen);
213 bstrncat(opts, ":", optlen); /* terminate it */
214 Dmsg3(900, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
217 * Standard keyword options for Include/Exclude
220 for (i=0; FS_options[i].name; i++) {
221 if (strcasecmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
222 /* NOTE! maximum 2 letters here or increase option[3] */
223 option[0] = FS_options[i].option[0];
224 option[1] = FS_options[i].option[1];
230 scan_err1(lc, "Expected a FileSet option keyword, got:%s:", lc->str);
231 } else { /* add option */
232 bstrncat(opts, option, optlen);
233 Dmsg3(900, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
237 /* If option terminated by comma, eat it */
239 token = lex_get_token(lc, T_ALL); /* yes, eat comma */
245 * Store FileSet Include/Exclude info
247 void store_inc(LEX *lc, RES_ITEM *item, int index, int pass)
250 int options = lc->options;
256 * Decide if we are doing a new Include or an old include. The
257 * new Include is followed immediately by open brace, whereas the
258 * old include has options following the Include.
260 token = lex_get_token(lc, T_ALL);
261 if (token == T_BOB) {
262 store_newinc(lc, item, index, pass);
265 if (token != T_EQUALS) {
266 scan_err1(lc, _("Expecting an equals sign, got: %s\n"), lc->str);
268 lc->options |= LOPT_NO_IDENT; /* make spaces significant */
269 memset(&res_incexe, 0, sizeof(INCEXE));
271 /* Get include options */
273 while ((token=lex_get_token(lc, T_ALL)) != T_BOB) {
275 keyword = INC_KW_NONE;
276 for (i=0; FS_option_kw[i].name; i++) {
277 if (strcasecmp(lc->str, FS_option_kw[i].name) == 0) {
278 keyword = FS_option_kw[i].token;
282 if (keyword == INC_KW_NONE) {
283 scan_err1(lc, _("Expected a FileSet keyword, got: %s"), lc->str);
285 /* Option keyword should be following by = <option> */
286 if ((token=lex_get_token(lc, T_ALL)) != T_EQUALS) {
287 scan_err1(lc, _("expected an = following keyword, got: %s"), lc->str);
289 /* Scan right hand side of option */
290 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
292 if (token == T_BOB) {
298 strcat(inc_opts, "0"); /* set no options */
300 inc_opts_len = strlen(inc_opts);
304 if (!res_all.res_fs.have_MD5) {
305 MD5Init(&res_all.res_fs.md5c);
306 res_all.res_fs.have_MD5 = TRUE;
308 setup_current_opts();
309 bstrncpy(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
310 Dmsg2(900, "old pass=%d incexe opts=%s\n", pass, res_incexe.current_opts->opts);
312 /* Create incexe structure */
313 Dmsg0(900, "Create INCEXE structure\n");
314 incexe = (INCEXE *)malloc(sizeof(INCEXE));
315 memcpy(incexe, &res_incexe, sizeof(INCEXE));
316 memset(&res_incexe, 0, sizeof(INCEXE));
317 if (item->code == 0) { /* include */
318 if (res_all.res_fs.num_includes == 0) {
319 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
321 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
322 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
324 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
325 Dmsg1(900, "num_includes=%d\n", res_all.res_fs.num_includes);
326 } else { /* exclude */
327 if (res_all.res_fs.num_excludes == 0) {
328 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
330 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
331 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
333 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
334 Dmsg1(900, "num_excludes=%d\n", res_all.res_fs.num_excludes);
337 /* Pickup include/exclude names. They are stored in INCEXE
338 * structures which contains the options and the name.
340 while ((token = lex_get_token(lc, T_ALL)) != T_EOB) {
347 case T_UNQUOTED_STRING:
348 case T_QUOTED_STRING:
349 if (res_all.res_fs.have_MD5) {
350 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
352 if (incexe->name_list.size() == 0) {
353 incexe->name_list.init(10, true);
355 incexe->name_list.append(bstrdup(lc->str));
356 Dmsg1(900, "Add to name_list %s\n", lc->str);
359 scan_err1(lc, "Expected a filename, got: %s", lc->str);
362 /* Note, MD5Final is done in backup.c */
363 } else { /* pass 2 */
364 while (lex_get_token(lc, T_ALL) != T_EOB)
368 lc->options = options;
369 set_bit(index, res_all.hdr.item_present);
374 * Store FileSet FInclude/FExclude info
375 * Note, when this routine is called, we are inside a FileSet
376 * resource. We treat the Include/Execlude like a sort of
377 * mini-resource within the FileSet resource.
379 static void store_newinc(LEX *lc, RES_ITEM *item, int index, int pass)
385 if (!res_all.res_fs.have_MD5) {
386 MD5Init(&res_all.res_fs.md5c);
387 res_all.res_fs.have_MD5 = TRUE;
389 memset(&res_incexe, 0, sizeof(INCEXE));
390 res_all.res_fs.new_include = true;
391 while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
392 if (token == T_EOL) {
395 if (token == T_EOB) {
398 if (token != T_IDENTIFIER) {
399 scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
401 for (i=0; newinc_items[i].name; i++) {
402 options = strcasecmp(lc->str, "options") == 0;
403 if (strcasecmp(newinc_items[i].name, lc->str) == 0) {
405 token = lex_get_token(lc, T_ALL);
406 if (token != T_EQUALS) {
407 scan_err1(lc, "expected an equals, got: %s", lc->str);
410 /* Call item handler */
411 newinc_items[i].handler(lc, &newinc_items[i], i, pass);
417 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
421 incexe = (INCEXE *)malloc(sizeof(INCEXE));
422 memcpy(incexe, &res_incexe, sizeof(INCEXE));
423 memset(&res_incexe, 0, sizeof(INCEXE));
424 if (item->code == 0) { /* include */
425 if (res_all.res_fs.num_includes == 0) {
426 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
428 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
429 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
431 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
432 Dmsg1(900, "num_includes=%d\n", res_all.res_fs.num_includes);
433 } else { /* exclude */
434 if (res_all.res_fs.num_excludes == 0) {
435 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
437 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
438 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
440 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
441 Dmsg1(900, "num_excludes=%d\n", res_all.res_fs.num_excludes);
445 set_bit(index, res_all.hdr.item_present);
449 /* Store regex info */
450 static void store_regex(LEX *lc, RES_ITEM *item, int index, int pass)
457 /* Pickup regex string
459 token = lex_get_token(lc, T_ALL);
462 case T_UNQUOTED_STRING:
463 case T_QUOTED_STRING:
464 rc = regcomp(&preg, lc->str, REG_EXTENDED);
466 regerror(rc, &preg, prbuf, sizeof(prbuf));
468 scan_err1(lc, _("Regex compile error. ERR=%s\n"), prbuf);
472 res_incexe.current_opts->regex.append(bstrdup(lc->str));
473 Dmsg3(900, "set regex %p size=%d %s\n",
474 res_incexe.current_opts, res_incexe.current_opts->regex.size(),lc->str);
477 scan_err1(lc, _("Expected a regex string, got: %s\n"), lc->str);
479 } else { /* pass 2 */
480 lex_get_token(lc, T_ALL);
485 /* Store Base info */
486 static void store_base(LEX *lc, RES_ITEM *item, int index, int pass)
492 * Pickup Base Job Name
494 token = lex_get_token(lc, T_NAME);
495 res_incexe.current_opts->base.append(bstrdup(lc->str));
496 } else { /* pass 2 */
497 lex_get_token(lc, T_ALL);
502 /* Store reader info */
503 static void store_reader(LEX *lc, RES_ITEM *item, int index, int pass)
509 * Pickup reader command
511 token = lex_get_token(lc, T_NAME);
512 res_incexe.current_opts->reader = bstrdup(lc->str);
513 } else { /* pass 2 */
514 lex_get_token(lc, T_ALL);
519 /* Store writer innfo */
520 static void store_writer(LEX *lc, RES_ITEM *item, int index, int pass)
526 * Pickup writer command
528 token = lex_get_token(lc, T_NAME);
529 res_incexe.current_opts->writer = bstrdup(lc->str);
530 } else { /* pass 2 */
531 lex_get_token(lc, T_ALL);
538 /* Store Wild-card info */
539 static void store_wild(LEX *lc, RES_ITEM *item, int index, int pass)
545 * Pickup Wild-card string
547 token = lex_get_token(lc, T_ALL);
550 case T_UNQUOTED_STRING:
551 case T_QUOTED_STRING:
552 res_incexe.current_opts->wild.append(bstrdup(lc->str));
553 Dmsg3(900, "set wild %p size=%d %s\n",
554 res_incexe.current_opts, res_incexe.current_opts->wild.size(),lc->str);
557 scan_err1(lc, _("Expected a wild-card string, got: %s\n"), lc->str);
559 } else { /* pass 2 */
560 lex_get_token(lc, T_ALL);
567 * Store Filename info. Note, for minor efficiency reasons, we
568 * always increase the name buffer by 10 items because we expect
569 * to add more entries.
571 static void store_fname(LEX *lc, RES_ITEM *item, int index, int pass)
577 /* Pickup Filename string
579 token = lex_get_token(lc, T_ALL);
582 case T_UNQUOTED_STRING:
583 case T_QUOTED_STRING:
584 if (res_all.res_fs.have_MD5) {
585 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
587 incexe = &res_incexe;
588 if (incexe->name_list.size() == 0) {
589 incexe->name_list.init(10, true);
591 incexe->name_list.append(bstrdup(lc->str));
592 Dmsg1(900, "Add to name_list %s\n", lc->str);
595 scan_err1(lc, _("Expected a filename, got: %s"), lc->str);
597 } else { /* pass 2 */
598 lex_get_token(lc, T_ALL);
604 * Come here when Options seen in Include/Exclude
606 static void options_res(LEX *lc, RES_ITEM *item, int index, int pass)
610 token = lex_get_token(lc, T_ALL);
611 if (token != T_BOB) {
612 scan_err1(lc, "Expecting open brace. Got %s", lc->str);
616 setup_current_opts();
619 while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
620 if (token == T_EOL) {
623 if (token == T_EOB) {
626 if (token != T_IDENTIFIER) {
627 scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
629 for (i=0; options_items[i].name; i++) {
630 if (strcasecmp(options_items[i].name, lc->str) == 0) {
631 token = lex_get_token(lc, T_ALL);
632 if (token != T_EQUALS) {
633 scan_err1(lc, "expected an equals, got: %s", lc->str);
635 /* Call item handler */
636 options_items[i].handler(lc, &options_items[i], i, pass);
642 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
650 * New style options come here
652 static void store_opts(LEX *lc, RES_ITEM *item, int index, int pass)
659 keyword = INC_KW_NONE;
660 /* Look up the keyword */
661 for (i=0; FS_option_kw[i].name; i++) {
662 if (strcasecmp(item->name, FS_option_kw[i].name) == 0) {
663 keyword = FS_option_kw[i].token;
667 if (keyword == INC_KW_NONE) {
668 scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
670 /* Now scan for the value */
671 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
673 bstrncat(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
674 Dmsg2(900, "new pass=%d incexe opts=%s\n", pass, res_incexe.current_opts->opts);
681 /* If current_opts not defined, create first entry */
682 static void setup_current_opts(void)
684 FOPTS *fo = (FOPTS *)malloc(sizeof(FOPTS));
685 memset(fo, 0, sizeof(FOPTS));
686 fo->regex.init(1, true);
687 fo->wild.init(1, true);
688 fo->base.init(1, true);
689 res_incexe.current_opts = fo;
690 if (res_incexe.num_opts == 0) {
691 res_incexe.opts_list = (FOPTS **)malloc(sizeof(FOPTS *));
693 res_incexe.opts_list = (FOPTS **)realloc(res_incexe.opts_list,
694 sizeof(FOPTS *) * (res_incexe.num_opts + 1));
696 res_incexe.opts_list[res_incexe.num_opts++] = fo;