]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/inc_conf.c
Print warning when drive is unmounted + misc cleanups
[bacula/bacula] / bacula / src / dird / inc_conf.c
1 /*
2  *   Configuration file parser for Include, Exclude, Finclude
3  *    and FExclude records.
4  *
5  *     Kern Sibbald, March MMIII
6  *
7  *     Version $Id$
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "dird.h"
31
32 /* Forward referenced subroutines */
33
34 void store_inc(LEX *lc, struct res_items *item, int index, int pass);
35 void store_finc(LEX *lc, struct res_items *item, int index, int pass);
36
37 static void store_match(LEX *lc, struct res_items *item, int index, int pass);
38 static void store_opts(LEX *lc, struct res_items *item, int index, int pass);
39 static void store_fname(LEX *lc, struct res_items *item, int index, int pass);
40 static void store_base(LEX *lc, struct res_items *item, int index, int pass);
41 static void setup_current_opts(void);
42
43
44 /* We build the current resource here as we are
45  * scanning the resource configuration definition,
46  * then move it to allocated memory when the resource
47  * scan is complete.
48  */
49 extern URES res_all;
50 extern int  res_all_size;
51
52 /* We build the current Finclude Fexclude item here */
53 static INCEXE res_incexe;
54
55 /* 
56  * FInclude/FExclude items
57  *   name             handler     value                        code flags default_value
58  */
59 static struct res_items finc_items[] = {
60    {"compression",     store_opts,    NULL,     0, 0, 0},
61    {"signature",       store_opts,    NULL,     0, 0, 0},
62    {"verify",          store_opts,    NULL,     0, 0, 0},
63    {"onefs",           store_opts,    NULL,     0, 0, 0},
64    {"recurse",         store_opts,    NULL,     0, 0, 0},
65    {"sparse",          store_opts,    NULL,     0, 0, 0},
66    {"readfifo",        store_opts,    NULL,     0, 0, 0},
67    {"replace",         store_opts,    NULL,     0, 0, 0},
68    {"portable",        store_opts,    NULL,     0, 0, 0},
69    {"match",           store_match,   NULL,     0, 0, 0},
70    {"file",            store_fname,   NULL,     0, 0, 0},
71    {"base",            store_base,    NULL,     0, 0, 0},
72    {NULL, NULL, NULL, 0, 0, 0} 
73 };
74
75 /* Define FileSet KeyWord values */
76
77 #define INC_KW_NONE         0
78 #define INC_KW_COMPRESSION  1
79 #define INC_KW_SIGNATURE    2
80 #define INC_KW_ENCRYPTION   3
81 #define INC_KW_VERIFY       4
82 #define INC_KW_ONEFS        5
83 #define INC_KW_RECURSE      6
84 #define INC_KW_SPARSE       7
85 #define INC_KW_REPLACE      8         /* restore options */
86 #define INC_KW_READFIFO     9         /* Causes fifo data to be read */
87 #define INC_KW_PORTABLE    10
88
89 /* Include keywords -- these are keywords that can appear
90  *    in the options lists of an include ( Include = compression= ...)
91  */
92 static struct s_kw FS_option_kw[] = {
93    {"compression", INC_KW_COMPRESSION},
94    {"signature",   INC_KW_SIGNATURE},
95    {"encryption",  INC_KW_ENCRYPTION},
96    {"verify",      INC_KW_VERIFY},
97    {"onefs",       INC_KW_ONEFS},
98    {"recurse",     INC_KW_RECURSE},
99    {"sparse",      INC_KW_SPARSE},
100    {"replace",     INC_KW_REPLACE},
101    {"readfifo",    INC_KW_READFIFO},
102    {"portable",    INC_KW_PORTABLE},
103    {NULL,          0}
104 };
105
106 /* Options for FileSet keywords */
107
108 struct s_fs_opt {
109    char *name;
110    int keyword;
111    char *option;
112 };
113
114 /*
115  * Options permitted for each keyword and resulting value.     
116  * The output goes into opts, which are then transmitted to
117  * the FD for application as options to the following list of
118  * included files.
119  */
120 static struct s_fs_opt FS_options[] = {
121    {"md5",      INC_KW_SIGNATURE,    "M"},
122    {"sha1",     INC_KW_SIGNATURE,    "S"},
123    {"gzip",     INC_KW_COMPRESSION,  "Z6"},
124    {"gzip1",    INC_KW_COMPRESSION,  "Z1"},
125    {"gzip2",    INC_KW_COMPRESSION,  "Z2"},
126    {"gzip3",    INC_KW_COMPRESSION,  "Z3"},
127    {"gzip4",    INC_KW_COMPRESSION,  "Z4"},
128    {"gzip5",    INC_KW_COMPRESSION,  "Z5"},
129    {"gzip6",    INC_KW_COMPRESSION,  "Z6"},
130    {"gzip7",    INC_KW_COMPRESSION,  "Z7"},
131    {"gzip8",    INC_KW_COMPRESSION,  "Z8"},
132    {"gzip9",    INC_KW_COMPRESSION,  "Z9"},
133    {"blowfish", INC_KW_ENCRYPTION,    "B"},   /* ***FIXME*** not implemented */
134    {"3des",     INC_KW_ENCRYPTION,    "3"},   /* ***FIXME*** not implemented */
135    {"yes",      INC_KW_ONEFS,         "0"},
136    {"no",       INC_KW_ONEFS,         "f"},
137    {"yes",      INC_KW_RECURSE,       "0"},
138    {"no",       INC_KW_RECURSE,       "h"},
139    {"yes",      INC_KW_SPARSE,        "s"},
140    {"no",       INC_KW_SPARSE,        "0"},
141    {"always",   INC_KW_REPLACE,       "a"},
142    {"ifnewer",  INC_KW_REPLACE,       "w"},
143    {"never",    INC_KW_REPLACE,       "n"},
144    {"yes",      INC_KW_READFIFO,      "r"},
145    {"no",       INC_KW_READFIFO,      "0"},
146    {"yes",      INC_KW_PORTABLE,      "p"},
147    {"no",       INC_KW_PORTABLE,      "0"},
148    {NULL,       0,                   0}
149 };
150
151
152
153 /* 
154  * Scan for Include options (keyword=option) is converted into one or
155  *  two characters. Verifyopts=xxxx is Vxxxx:
156  */
157 static void scan_include_options(LEX *lc, int keyword, char *opts, int optlen)
158 {
159    int token, i;
160    char option[3];
161
162    option[0] = 0;                     /* default option = none */
163    option[2] = 0;                     /* terminate options */
164    token = lex_get_token(lc, T_NAME);             /* expect at least one option */       
165    if (keyword == INC_KW_VERIFY) { /* special case */
166       /* ***FIXME**** ensure these are in permitted set */
167       bstrncat(opts, "V", optlen);         /* indicate Verify */
168       bstrncat(opts, lc->str, optlen);
169       bstrncat(opts, ":", optlen);         /* terminate it */
170
171    /*
172     * Standard keyword options for Include/Exclude 
173     */
174    } else {
175       for (i=0; FS_options[i].name; i++) {
176          if (strcasecmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
177             /* NOTE! maximum 2 letters here or increase option[3] */
178             option[0] = FS_options[i].option[0];
179             option[1] = FS_options[i].option[1];
180             i = 0;
181             break;
182          }
183       }
184       if (i != 0) {
185          scan_err1(lc, "Expected a FileSet option keyword, got:%s:", lc->str);
186       } else { /* add option */
187          bstrncat(opts, option, optlen);
188          Dmsg3(200, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
189       }
190    }
191
192    /* If option terminated by comma, eat it */
193    if (lc->ch == ',') {
194       token = lex_get_token(lc, T_ALL);      /* yes, eat comma */
195    }
196 }
197
198 /* Store FileSet Include/Exclude info */
199 void store_inc(LEX *lc, struct res_items *item, int index, int pass)
200 {
201    int token, i;
202    int options = lc->options;
203    int keyword;
204    char inc_opts[100];
205    int inc_opts_len;
206
207    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
208    memset(&res_incexe, 0, sizeof(INCEXE));
209
210    /* Get include options */
211    inc_opts[0] = 0;
212    while ((token=lex_get_token(lc, T_ALL)) != T_BOB) {
213       keyword = INC_KW_NONE;
214       for (i=0; FS_option_kw[i].name; i++) {
215          if (strcasecmp(lc->str, FS_option_kw[i].name) == 0) {
216             keyword = FS_option_kw[i].token;
217             break;
218          }
219       }
220       if (keyword == INC_KW_NONE) {
221          scan_err1(lc, _("Expected a FileSet keyword, got: %s"), lc->str);
222       }
223       /* Option keyword should be following by = <option> */
224       if ((token=lex_get_token(lc, T_ALL)) != T_EQUALS) {
225          scan_err1(lc, _("expected an = following keyword, got: %s"), lc->str);
226       } else {
227          scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
228       }
229       if (token == T_BOB) {
230          break;
231       }
232    }
233    if (!inc_opts[0]) {
234       strcat(inc_opts, "0");          /* set no options */
235    }
236    inc_opts_len = strlen(inc_opts);
237
238    if (pass == 1) {
239       INCEXE *incexe;
240       if (!res_all.res_fs.have_MD5) {
241          MD5Init(&res_all.res_fs.md5c);
242          res_all.res_fs.have_MD5 = TRUE;
243       }
244       setup_current_opts();
245       bstrncpy(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
246       Dmsg1(200, "incexe opts=%s\n", res_incexe.current_opts->opts);
247
248       /* Create incexe structure */
249       Dmsg0(200, "Create INCEXE structure\n");
250       incexe = (INCEXE *)malloc(sizeof(INCEXE));
251       memcpy(incexe, &res_incexe, sizeof(INCEXE));
252       memset(&res_incexe, 0, sizeof(INCEXE));
253       if (item->code == 0) { /* include */
254          if (res_all.res_fs.num_includes == 0) {
255             res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
256           } else {
257             res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
258                            sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
259           }
260           res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
261           Dmsg1(200, "num_includes=%d\n", res_all.res_fs.num_includes);
262       } else {    /* exclude */
263          if (res_all.res_fs.num_excludes == 0) {
264             res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
265           } else {
266             res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
267                            sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
268           }
269           res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
270           Dmsg1(200, "num_excludes=%d\n", res_all.res_fs.num_excludes);
271       }
272
273       /* Pickup include/exclude names.  They are stored in INCEXE
274        *  structures which contains the options and the name.
275        */
276       while ((token = lex_get_token(lc, T_ALL)) != T_EOB) {
277          switch (token) {
278             case T_COMMA:
279             case T_EOL:
280                continue;
281
282             case T_IDENTIFIER:
283             case T_UNQUOTED_STRING:
284             case T_QUOTED_STRING:
285                if (res_all.res_fs.have_MD5) {
286                   MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
287                }
288                if (incexe->name_list.size() == 0) {
289                   incexe->name_list.init(10, true);
290                }
291                incexe->name_list.append(bstrdup(lc->str));
292                Dmsg1(200, "Add to name_list %s\n", lc->str);
293                break;
294             default:
295                scan_err1(lc, "Expected a filename, got: %s", lc->str);
296          }                                 
297       }
298       /* Note, MD5Final is done in backup.c */
299    } else { /* pass 2 */
300       while (lex_get_token(lc, T_ALL) != T_EOB) 
301          {}
302    }
303    scan_to_eol(lc);
304    lc->options = options;
305    set_bit(index, res_all.hdr.item_present);
306 }
307
308
309 /*
310  * Store FileSet FInclude/FExclude info   
311  *  Note, when this routine is called, we are inside a FileSet
312  *  resource.  We treat the Finclude/Fexeclude like a sort of
313  *  mini-resource within the FileSet resource.
314  */
315 void store_finc(LEX *lc, struct res_items *item, int index, int pass)
316 {
317    int token, i;
318    INCEXE *incexe;
319
320    if (!res_all.res_fs.have_MD5) {
321       MD5Init(&res_all.res_fs.md5c);
322       res_all.res_fs.have_MD5 = TRUE;
323    }
324    res_all.res_fs.finclude = TRUE;
325    token = lex_get_token(lc, T_ALL);            
326    if (token != T_BOB) {
327       scan_err1(lc, _("Expecting a beginning brace, got: %s\n"), lc->str);
328    }
329    memset(&res_incexe, 0, sizeof(INCEXE));
330    while ((token = lex_get_token(lc, T_ALL)) != T_EOF) {
331       if (token == T_EOL) {
332          continue;
333       }
334       if (token == T_EOB) {
335          break;
336       }
337       if (token != T_IDENTIFIER) {
338          scan_err1(lc, _("Expecting keyword, got: %s\n"), lc->str);
339       }
340       for (i=0; finc_items[i].name; i++) {
341          if (strcasecmp(finc_items[i].name, lc->str) == 0) {
342             token = lex_get_token(lc, T_ALL);
343             if (token != T_EQUALS) {
344                scan_err1(lc, "expected an equals, got: %s", lc->str);
345             }
346             /* Call item handler */
347             finc_items[i].handler(lc, &finc_items[i], i, pass);
348             i = -1;
349             break;
350          }
351       }
352       if (i >=0) {
353          scan_err1(lc, "Keyword %s not permitted in this resource", lc->str);
354       }
355    }
356    if (pass == 1) {
357       incexe = (INCEXE *)malloc(sizeof(INCEXE));
358       memcpy(incexe, &res_incexe, sizeof(INCEXE));
359       memset(&res_incexe, 0, sizeof(INCEXE));
360       if (item->code == 0) { /* include */
361          if (res_all.res_fs.num_includes == 0) {
362             res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
363          } else {
364             res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
365                            sizeof(INCEXE *) * (res_all.res_fs.num_includes + 1));
366          }
367          res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
368          Dmsg1(200, "num_includes=%d\n", res_all.res_fs.num_includes);
369       } else {    /* exclude */
370          if (res_all.res_fs.num_excludes == 0) {
371             res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
372          } else {
373             res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
374                            sizeof(INCEXE *) * (res_all.res_fs.num_excludes + 1));
375          }
376          res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
377          Dmsg1(200, "num_excludes=%d\n", res_all.res_fs.num_excludes);
378       }
379    }
380    scan_to_eol(lc);
381    set_bit(index, res_all.hdr.item_present);
382 }
383
384
385 /* Store Match info */
386 static void store_match(LEX *lc, struct res_items *item, int index, int pass)
387 {
388    int token;
389
390    if (pass == 1) {
391       /* Pickup Match string
392        */
393       token = lex_get_token(lc, T_ALL);            
394       switch (token) {
395       case T_IDENTIFIER:
396       case T_UNQUOTED_STRING:
397       case T_QUOTED_STRING:
398          setup_current_opts();
399          res_incexe.current_opts->match.append(bstrdup(lc->str));
400          break;
401       default:
402          scan_err1(lc, _("Expected a filename, got: %s\n"), lc->str);
403       }                                 
404    } else { /* pass 2 */
405       lex_get_token(lc, T_ALL);          
406    }
407    scan_to_eol(lc);
408 }
409
410 /* Store Base info */
411 static void store_base(LEX *lc, struct res_items *item, int index, int pass)
412 {
413    int token;
414
415    if (pass == 1) {
416       setup_current_opts();
417       /*
418        * Pickup Base Job Name
419        */
420       token = lex_get_token(lc, T_NAME);           
421       res_incexe.current_opts->base_list.append(bstrdup(lc->str));
422    } else { /* pass 2 */
423       lex_get_token(lc, T_ALL);          
424    }
425    scan_to_eol(lc);
426 }
427 /*
428  * Store Filename info. Note, for minor efficiency reasons, we
429  * always increase the name buffer by 10 items because we expect
430  * to add more entries.
431  */
432 static void store_fname(LEX *lc, struct res_items *item, int index, int pass)
433 {
434    int token;
435    INCEXE *incexe;
436
437    if (pass == 1) {
438       /* Pickup Filename string
439        */
440       token = lex_get_token(lc, T_ALL);            
441       switch (token) {
442          case T_IDENTIFIER:
443          case T_UNQUOTED_STRING:
444          case T_QUOTED_STRING:
445             if (res_all.res_fs.have_MD5) {
446                MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
447             }
448             incexe = &res_incexe;
449             if (incexe->name_list.size() == 0) {
450                incexe->name_list.init(10, true);
451             }
452             incexe->name_list.append(bstrdup(lc->str));
453             Dmsg1(200, "Add to name_list %s\n", lc->str);
454             break;
455          default:
456             scan_err1(lc, _("Expected a filename, got: %s"), lc->str);
457       }                                 
458    } else { /* pass 2 */
459       lex_get_token(lc, T_ALL);          
460    }
461    scan_to_eol(lc);
462 }
463
464
465 static void store_opts(LEX *lc, struct res_items *item, int index, int pass)
466 {
467    int i;
468    int keyword;
469    char inc_opts[100];
470
471    inc_opts[0] = 0;
472    keyword = INC_KW_NONE;
473    for (i=0; FS_option_kw[i].name; i++) {
474       if (strcasecmp(item->name, FS_option_kw[i].name) == 0) {
475          keyword = FS_option_kw[i].token;
476          break;
477       }
478    }
479    if (keyword == INC_KW_NONE) {
480       scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
481    }
482    Dmsg2(200, "keyword=%d %s\n", keyword, FS_option_kw[keyword].name);
483    scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
484
485    if (pass == 1) {
486       setup_current_opts();
487       bstrncat(res_incexe.current_opts->opts, inc_opts, MAX_FOPTS);
488    }
489
490    scan_to_eol(lc);
491 }
492
493
494
495 /* If current_opts not defined, create first entry */
496 static void setup_current_opts(void)
497 {
498    if (res_incexe.current_opts == NULL) {
499       res_incexe.current_opts = (FOPTS *)malloc(sizeof(FOPTS));
500       memset(res_incexe.current_opts, 0, sizeof(FOPTS));
501       res_incexe.current_opts->match.init(1, true);
502       res_incexe.current_opts->base_list.init(1, true);
503       res_incexe.num_opts = 1;
504       res_incexe.opts_list = (FOPTS **)malloc(sizeof(FOPTS *));
505       res_incexe.opts_list[0] = res_incexe.current_opts;
506    }
507 }