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 setup_current_opts(void);
49 /* We build the current resource here as we are
50 * scanning the resource configuration definition,
51 * then move it to allocated memory when the resource
55 extern int res_all_size;
57 /* We build the current new Include and Exclude items here */
58 static INCEXE res_incexe;
61 * new Include/Exclude items
62 * name handler value code flags default_value
64 static RES_ITEM newinc_items[] = {
65 {"file", store_fname, NULL, 0, 0, 0},
66 {"options", options_res, NULL, 0, 0, 0},
67 {NULL, NULL, NULL, 0, 0, 0}
71 * Items that are valid in an Options resource
73 static RES_ITEM options_items[] = {
74 {"compression", store_opts, NULL, 0, 0, 0},
75 {"signature", store_opts, NULL, 0, 0, 0},
76 {"verify", store_opts, NULL, 0, 0, 0},
77 {"onefs", store_opts, NULL, 0, 0, 0},
78 {"recurse", store_opts, NULL, 0, 0, 0},
79 {"sparse", store_opts, NULL, 0, 0, 0},
80 {"readfifo", store_opts, NULL, 0, 0, 0},
81 {"replace", store_opts, NULL, 0, 0, 0},
82 {"portable", store_opts, NULL, 0, 0, 0},
83 {"mtimeonly", store_opts, NULL, 0, 0, 0},
84 {"keepatime", store_opts, NULL, 0, 0, 0},
85 {"regex", store_regex, NULL, 0, 0, 0},
86 {"base", store_base, NULL, 0, 0, 0},
87 {"wild", store_wild, NULL, 0, 0, 0},
88 {"exclude", store_opts, NULL, 0, 0, 0},
89 {"aclsupport", store_opts, NULL, 0, 0, 0},
90 {NULL, NULL, NULL, 0, 0, 0}
94 /* Define FileSet KeyWord values */
105 INC_KW_REPLACE, /* restore options */
106 INC_KW_READFIFO, /* Causes fifo data to be read */
115 * Include keywords -- these are keywords that can appear
116 * in the options lists of an old include ( Include = compression= ...)
118 static struct s_kw FS_option_kw[] = {
119 {"compression", INC_KW_COMPRESSION},
120 {"signature", INC_KW_SIGNATURE},
121 {"encryption", INC_KW_ENCRYPTION},
122 {"verify", INC_KW_VERIFY},
123 {"onefs", INC_KW_ONEFS},
124 {"recurse", INC_KW_RECURSE},
125 {"sparse", INC_KW_SPARSE},
126 {"replace", INC_KW_REPLACE},
127 {"readfifo", INC_KW_READFIFO},
128 {"portable", INC_KW_PORTABLE},
129 {"mtimeonly", INC_KW_MTIMEONLY},
130 {"keepatime", INC_KW_KEEPATIME},
131 {"exclude", INC_KW_EXCLUDE},
132 {"aclsupport", INC_KW_ACL},
136 /* Options for FileSet keywords */
145 * Options permitted for each keyword and resulting value.
146 * The output goes into opts, which are then transmitted to
147 * the FD for application as options to the following list of
150 static struct s_fs_opt FS_options[] = {
151 {"md5", INC_KW_SIGNATURE, "M"},
152 {"sha1", INC_KW_SIGNATURE, "S"},
153 {"gzip", INC_KW_COMPRESSION, "Z6"},
154 {"gzip1", INC_KW_COMPRESSION, "Z1"},
155 {"gzip2", INC_KW_COMPRESSION, "Z2"},
156 {"gzip3", INC_KW_COMPRESSION, "Z3"},
157 {"gzip4", INC_KW_COMPRESSION, "Z4"},
158 {"gzip5", INC_KW_COMPRESSION, "Z5"},
159 {"gzip6", INC_KW_COMPRESSION, "Z6"},
160 {"gzip7", INC_KW_COMPRESSION, "Z7"},
161 {"gzip8", INC_KW_COMPRESSION, "Z8"},
162 {"gzip9", INC_KW_COMPRESSION, "Z9"},
163 {"blowfish", INC_KW_ENCRYPTION, "B"}, /* ***FIXME*** not implemented */
164 {"3des", INC_KW_ENCRYPTION, "3"}, /* ***FIXME*** not implemented */
165 {"yes", INC_KW_ONEFS, "0"},
166 {"no", INC_KW_ONEFS, "f"},
167 {"yes", INC_KW_RECURSE, "0"},
168 {"no", INC_KW_RECURSE, "h"},
169 {"yes", INC_KW_SPARSE, "s"},
170 {"no", INC_KW_SPARSE, "0"},
171 {"always", INC_KW_REPLACE, "a"},
172 {"ifnewer", INC_KW_REPLACE, "w"},
173 {"never", INC_KW_REPLACE, "n"},
174 {"yes", INC_KW_READFIFO, "r"},
175 {"no", INC_KW_READFIFO, "0"},
176 {"yes", INC_KW_PORTABLE, "p"},
177 {"no", INC_KW_PORTABLE, "0"},
178 {"yes", INC_KW_MTIMEONLY, "m"},
179 {"no", INC_KW_MTIMEONLY, "0"},
180 {"yes", INC_KW_KEEPATIME, "k"},
181 {"no", INC_KW_KEEPATIME, "0"},
182 {"yes", INC_KW_EXCLUDE, "e"},
183 {"no", INC_KW_EXCLUDE, "0"},
184 {"yes", INC_KW_ACL, "A"},
185 {"no", INC_KW_ACL, "0"},
192 * Scan for right hand side of Include options (keyword=option) is
193 * converted into one or two characters. Verifyopts=xxxx is Vxxxx:
194 * Whatever is found is concatenated to the opts string.
195 * This code is also used inside an Options resource.
197 static void scan_include_options(LEX *lc, int keyword, char *opts, int optlen)
202 option[0] = 0; /* default option = none */
203 option[2] = 0; /* terminate options */
204 token = lex_get_token(lc, T_NAME); /* expect at least one option */
205 if (keyword == INC_KW_VERIFY) { /* special case */
206 /* ***FIXME**** ensure these are in permitted set */
207 bstrncat(opts, "V", optlen); /* indicate Verify */
208 bstrncat(opts, lc->str, optlen);
209 bstrncat(opts, ":", optlen); /* terminate it */
210 Dmsg3(900, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
213 * Standard keyword options for Include/Exclude
216 for (i=0; FS_options[i].name; i++) {
217 if (strcasecmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
218 /* NOTE! maximum 2 letters here or increase option[3] */
219 option[0] = FS_options[i].option[0];
220 option[1] = FS_options[i].option[1];
226 scan_err1(lc, "Expected a FileSet option keyword, got:%s:", lc->str);
227 } else { /* add option */
228 bstrncat(opts, option, optlen);
229 Dmsg3(900, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
233 /* If option terminated by comma, eat it */
235 token = lex_get_token(lc, T_ALL); /* yes, eat comma */
241 * Store FileSet Include/Exclude info
243 void store_inc(LEX *lc, RES_ITEM *item, int index, int pass)
246 int options = lc->options;
252 * Decide if we are doing a new Include or an old include. The
253 * new Include is followed immediately by open brace, whereas the
254 * old include has options following the Include.
256 token = lex_get_token(lc, T_ALL);
257 if (token == T_BOB) {
258 store_newinc(lc, item, index, pass);
261 if (token != T_EQUALS) {
262 scan_err1(lc, _("Expecting an equals sign, got: %s\n"), lc->str);
264 lc->options |= LOPT_NO_IDENT; /* make spaces significant */
265 memset(&res_incexe, 0, sizeof(INCEXE));
267 /* Get include options */
269 while ((token=lex_get_token(lc, T_ALL)) != T_BOB) {
271 keyword = INC_KW_NONE;
272 for (i=0; FS_option_kw[i].name; i++) {
273 if (strcasecmp(lc->str, FS_option_kw[i].name) == 0) {
274 keyword = FS_option_kw[i].token;
278 if (keyword == INC_KW_NONE) {
279 scan_err1(lc, _("Expected a FileSet keyword, got: %s"), lc->str);
281 /* Option keyword should be following by = <option> */
282 if ((token=lex_get_token(lc, T_ALL)) != T_EQUALS) {
283 scan_err1(lc, _("expected an = following keyword, got: %s"), lc->str);
285 /* Scan right hand side of option */
286 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
288 if (token == T_BOB) {
294 strcat(inc_opts, "0"); /* set no options */
296 inc_opts_len = strlen(inc_opts);
300 if (!res_all.res_fs.have_MD5) {
301 MD5Init(&res_all.res_fs.md5c);
302 res_all.res_fs.have_MD5 = TRUE;
304 setup_current_opts();
305 bstrncpy(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
306 Dmsg2(900, "old pass=%d incexe opts=%s\n", pass, res_incexe.current_opts->opts);
308 /* Create incexe structure */
309 Dmsg0(900, "Create INCEXE structure\n");
310 incexe = (INCEXE *)malloc(sizeof(INCEXE));
311 memcpy(incexe, &res_incexe, sizeof(INCEXE));
312 memset(&res_incexe, 0, sizeof(INCEXE));
313 if (item->code == 0) { /* include */
314 if (res_all.res_fs.num_includes == 0) {
315 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
317 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
318 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
320 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
321 Dmsg1(900, "num_includes=%d\n", res_all.res_fs.num_includes);
322 } else { /* exclude */
323 if (res_all.res_fs.num_excludes == 0) {
324 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
326 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
327 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
329 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
330 Dmsg1(900, "num_excludes=%d\n", res_all.res_fs.num_excludes);
333 /* Pickup include/exclude names. They are stored in INCEXE
334 * structures which contains the options and the name.
336 while ((token = lex_get_token(lc, T_ALL)) != T_EOB) {
343 case T_UNQUOTED_STRING:
344 case T_QUOTED_STRING:
345 if (res_all.res_fs.have_MD5) {
346 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
348 if (incexe->name_list.size() == 0) {
349 incexe->name_list.init(10, true);
351 incexe->name_list.append(bstrdup(lc->str));
352 Dmsg1(900, "Add to name_list %s\n", lc->str);
355 scan_err1(lc, "Expected a filename, got: %s", lc->str);
358 /* Note, MD5Final is done in backup.c */
359 } else { /* pass 2 */
360 while (lex_get_token(lc, T_ALL) != T_EOB)
364 lc->options = options;
365 set_bit(index, res_all.hdr.item_present);
370 * Store FileSet FInclude/FExclude info
371 * Note, when this routine is called, we are inside a FileSet
372 * resource. We treat the Include/Execlude like a sort of
373 * mini-resource within the FileSet resource.
375 static void store_newinc(LEX *lc, RES_ITEM *item, int index, int pass)
381 if (!res_all.res_fs.have_MD5) {
382 MD5Init(&res_all.res_fs.md5c);
383 res_all.res_fs.have_MD5 = TRUE;
385 memset(&res_incexe, 0, sizeof(INCEXE));
386 res_all.res_fs.new_include = true;
387 while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
388 if (token == T_EOL) {
391 if (token == T_EOB) {
394 if (token != T_IDENTIFIER) {
395 scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
397 for (i=0; newinc_items[i].name; i++) {
398 options = strcasecmp(lc->str, "options") == 0;
399 if (strcasecmp(newinc_items[i].name, lc->str) == 0) {
401 token = lex_get_token(lc, T_ALL);
402 if (token != T_EQUALS) {
403 scan_err1(lc, "expected an equals, got: %s", lc->str);
406 /* Call item handler */
407 newinc_items[i].handler(lc, &newinc_items[i], i, pass);
413 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
417 incexe = (INCEXE *)malloc(sizeof(INCEXE));
418 memcpy(incexe, &res_incexe, sizeof(INCEXE));
419 memset(&res_incexe, 0, sizeof(INCEXE));
420 if (item->code == 0) { /* include */
421 if (res_all.res_fs.num_includes == 0) {
422 res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
424 res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
425 sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
427 res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
428 Dmsg1(900, "num_includes=%d\n", res_all.res_fs.num_includes);
429 } else { /* exclude */
430 if (res_all.res_fs.num_excludes == 0) {
431 res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
433 res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
434 sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
436 res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
437 Dmsg1(900, "num_excludes=%d\n", res_all.res_fs.num_excludes);
441 set_bit(index, res_all.hdr.item_present);
445 /* Store regex info */
446 static void store_regex(LEX *lc, RES_ITEM *item, int index, int pass)
453 /* Pickup regex string
455 token = lex_get_token(lc, T_ALL);
458 case T_UNQUOTED_STRING:
459 case T_QUOTED_STRING:
460 rc = regcomp(&preg, lc->str, REG_EXTENDED);
462 regerror(rc, &preg, prbuf, sizeof(prbuf));
464 scan_err1(lc, _("Regex compile error. ERR=%s\n"), prbuf);
468 res_incexe.current_opts->regex.append(bstrdup(lc->str));
469 Dmsg3(900, "set regex %p size=%d %s\n",
470 res_incexe.current_opts, res_incexe.current_opts->regex.size(),lc->str);
473 scan_err1(lc, _("Expected a regex string, got: %s\n"), lc->str);
475 } else { /* pass 2 */
476 lex_get_token(lc, T_ALL);
481 /* Store Base info */
482 static void store_base(LEX *lc, RES_ITEM *item, int index, int pass)
488 * Pickup Base Job Name
490 token = lex_get_token(lc, T_NAME);
491 res_incexe.current_opts->base.append(bstrdup(lc->str));
492 } else { /* pass 2 */
493 lex_get_token(lc, T_ALL);
499 /* Store Wild-card info */
500 static void store_wild(LEX *lc, RES_ITEM *item, int index, int pass)
506 * Pickup Wild-card string
508 token = lex_get_token(lc, T_ALL);
511 case T_UNQUOTED_STRING:
512 case T_QUOTED_STRING:
513 res_incexe.current_opts->wild.append(bstrdup(lc->str));
514 Dmsg3(900, "set wild %p size=%d %s\n",
515 res_incexe.current_opts, res_incexe.current_opts->wild.size(),lc->str);
518 scan_err1(lc, _("Expected a wild-card string, got: %s\n"), lc->str);
520 } else { /* pass 2 */
521 lex_get_token(lc, T_ALL);
528 * Store Filename info. Note, for minor efficiency reasons, we
529 * always increase the name buffer by 10 items because we expect
530 * to add more entries.
532 static void store_fname(LEX *lc, RES_ITEM *item, int index, int pass)
538 /* Pickup Filename string
540 token = lex_get_token(lc, T_ALL);
543 case T_UNQUOTED_STRING:
544 case T_QUOTED_STRING:
545 if (res_all.res_fs.have_MD5) {
546 MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
548 incexe = &res_incexe;
549 if (incexe->name_list.size() == 0) {
550 incexe->name_list.init(10, true);
552 incexe->name_list.append(bstrdup(lc->str));
553 Dmsg1(900, "Add to name_list %s\n", lc->str);
556 scan_err1(lc, _("Expected a filename, got: %s"), lc->str);
558 } else { /* pass 2 */
559 lex_get_token(lc, T_ALL);
565 * Come here when Options seen in Include/Exclude
567 static void options_res(LEX *lc, RES_ITEM *item, int index, int pass)
571 token = lex_get_token(lc, T_ALL);
572 if (token != T_BOB) {
573 scan_err1(lc, "Expecting open brace. Got %s", lc->str);
577 setup_current_opts();
580 while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
581 if (token == T_EOL) {
584 if (token == T_EOB) {
587 if (token != T_IDENTIFIER) {
588 scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
590 for (i=0; options_items[i].name; i++) {
591 if (strcasecmp(options_items[i].name, lc->str) == 0) {
592 token = lex_get_token(lc, T_ALL);
593 if (token != T_EQUALS) {
594 scan_err1(lc, "expected an equals, got: %s", lc->str);
596 /* Call item handler */
597 options_items[i].handler(lc, &options_items[i], i, pass);
603 scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
611 * New style options come here
613 static void store_opts(LEX *lc, RES_ITEM *item, int index, int pass)
620 keyword = INC_KW_NONE;
621 /* Look up the keyword */
622 for (i=0; FS_option_kw[i].name; i++) {
623 if (strcasecmp(item->name, FS_option_kw[i].name) == 0) {
624 keyword = FS_option_kw[i].token;
628 if (keyword == INC_KW_NONE) {
629 scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
631 /* Now scan for the value */
632 scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
634 bstrncat(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
635 Dmsg2(900, "new pass=%d incexe opts=%s\n", pass, res_incexe.current_opts->opts);
642 /* If current_opts not defined, create first entry */
643 static void setup_current_opts(void)
645 FOPTS *fo = (FOPTS *)malloc(sizeof(FOPTS));
646 memset(fo, 0, sizeof(FOPTS));
647 fo->regex.init(1, true);
648 fo->wild.init(1, true);
649 fo->base.init(1, true);
650 res_incexe.current_opts = fo;
651 if (res_incexe.num_opts == 0) {
652 res_incexe.opts_list = (FOPTS **)malloc(sizeof(FOPTS *));
654 res_incexe.opts_list = (FOPTS **)realloc(res_incexe.opts_list,
655 sizeof(FOPTS *) * (res_incexe.num_opts + 1));
657 res_incexe.opts_list[res_incexe.num_opts++] = fo;