]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird_conf.c
088b9e23073ecbf75a78b317012d7bbe402c842f
[bacula/bacula] / bacula / src / dird / dird_conf.c
1 /*
2  *   Main configuration file parser for Bacula Directors,
3  *    some parts may be split into separate files such as
4  *    the schedule configuration (sch_config.c).
5  *
6  *   Note, the configuration file parser consists of three parts
7  *
8  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
9  *
10  *   2. The generic config  scanner in lib/parse_config.c and 
11  *      lib/parse_config.h.
12  *      These files contain the parser code, some utility
13  *      routines, and the common store routines (name, int,
14  *      string).
15  *
16  *   3. The daemon specific file, which contains the Resource
17  *      definitions as well as any specific store routines
18  *      for the resource records.
19  *
20  *     Kern Sibbald, January MM
21  *
22  *     Version $Id$
23  */
24 /*
25    Copyright (C) 2000-2003 Kern Sibbald and John Walker
26
27    This program is free software; you can redistribute it and/or
28    modify it under the terms of the GNU General Public License as
29    published by the Free Software Foundation; either version 2 of
30    the License, or (at your option) any later version.
31
32    This program is distributed in the hope that it will be useful,
33    but WITHOUT ANY WARRANTY; without even the implied warranty of
34    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35    General Public License for more details.
36
37    You should have received a copy of the GNU General Public
38    License along with this program; if not, write to the Free
39    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40    MA 02111-1307, USA.
41
42  */
43
44 #include "bacula.h"
45 #include "dird.h"
46
47 /* Define the first and last resource ID record
48  * types. Note, these should be unique for each
49  * daemon though not a requirement.
50  */
51 int r_first = R_FIRST;
52 int r_last  = R_LAST;
53 pthread_mutex_t res_mutex =  PTHREAD_MUTEX_INITIALIZER;
54
55 /* Imported subroutines */
56 extern void store_run(LEX *lc, struct res_items *item, int index, int pass);
57
58
59 /* Forward referenced subroutines */
60
61 static void store_inc(LEX *lc, struct res_items *item, int index, int pass);
62 static void store_match(LEX *lc, struct res_items *item, int index, int pass);
63 static void store_backup(LEX *lc, struct res_items *item, int index, int pass);
64 static void store_restore(LEX *lc, struct res_items *item, int index, int pass);
65 static void store_jobtype(LEX *lc, struct res_items *item, int index, int pass);
66 static void store_level(LEX *lc, struct res_items *item, int index, int pass);
67 static void store_replace(LEX *lc, struct res_items *item, int index, int pass);
68 static void store_opts(LEX *lc, struct res_items *item, int index, int pass);
69
70
71 /* We build the current resource here as we are
72  * scanning the resource configuration definition,
73  * then move it to allocated memory when the resource
74  * scan is complete.
75  */
76 URES res_all;
77 int  res_all_size = sizeof(res_all);
78
79
80 /* Definition of records permitted within each
81  * resource with the routine to process the record 
82  * information.  NOTE! quoted names must be in lower case.
83  */ 
84 /* 
85  *    Director Resource
86  *
87  *   name          handler     value                 code flags    default_value
88  */
89 static struct res_items dir_items[] = {
90    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
91    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
92    {"messages",    store_res,      ITEM(res_dir.messages), R_MSGS, 0, 0},
93    {"dirport",     store_pint,     ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
94    {"diraddress",  store_str,      ITEM(res_dir.DIRaddr),  0, 0, 0},
95    {"queryfile",   store_dir,      ITEM(res_dir.query_file), 0, ITEM_REQUIRED, 0},
96    {"workingdirectory", store_dir, ITEM(res_dir.working_directory), 0, ITEM_REQUIRED, 0},
97    {"piddirectory", store_dir,     ITEM(res_dir.pid_directory), 0, ITEM_REQUIRED, 0},
98    {"subsysdirectory", store_dir,  ITEM(res_dir.subsys_directory), 0, ITEM_REQUIRED, 0},
99    {"maximumconcurrentjobs", store_pint, ITEM(res_dir.MaxConcurrentJobs), 0, ITEM_DEFAULT, 1},
100    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
101    {"fdconnecttimeout", store_time,ITEM(res_dir.FDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
102    {"sdconnecttimeout", store_time,ITEM(res_dir.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
103    {NULL, NULL, NULL, 0, 0, 0}
104 };
105
106 /* 
107  *    Client or File daemon resource
108  *
109  *   name          handler     value                 code flags    default_value
110  */
111
112 static struct res_items cli_items[] = {
113    {"name",     store_name,       ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
114    {"description", store_str,     ITEM(res_client.hdr.desc), 0, 0, 0},
115    {"address",  store_str,        ITEM(res_client.address),  0, ITEM_REQUIRED, 0},
116    {"fdport",   store_pint,       ITEM(res_client.FDport),   0, ITEM_DEFAULT, 9102},
117    {"password", store_password,   ITEM(res_client.password), 0, ITEM_REQUIRED, 0},
118    {"catalog",  store_res,        ITEM(res_client.catalog),  R_CATALOG, 0, 0},
119    {"fileretention", store_time,  ITEM(res_client.FileRetention), 0, ITEM_DEFAULT, 60*60*24*60},
120    {"jobretention",  store_time,  ITEM(res_client.JobRetention),  0, ITEM_DEFAULT, 60*60*24*180},
121    {"autoprune", store_yesno,     ITEM(res_client.AutoPrune), 1, ITEM_DEFAULT, 1},
122    {NULL, NULL, NULL, 0, 0, 0} 
123 };
124
125 /* Storage daemon resource
126  *
127  *   name          handler     value                 code flags    default_value
128  */
129 static struct res_items store_items[] = {
130    {"name",      store_name,     ITEM(res_store.hdr.name),   0, ITEM_REQUIRED, 0},
131    {"description", store_str,    ITEM(res_store.hdr.desc),   0, 0, 0},
132    {"sdport",    store_pint,     ITEM(res_store.SDport),     0, ITEM_DEFAULT, 9103},
133    {"sddport",   store_pint,     ITEM(res_store.SDDport),    0, 0, 0}, /* deprecated */
134    {"address",   store_str,      ITEM(res_store.address),    0, ITEM_REQUIRED, 0},
135    {"password",  store_password, ITEM(res_store.password),   0, ITEM_REQUIRED, 0},
136    {"device",    store_strname,  ITEM(res_store.dev_name),   0, ITEM_REQUIRED, 0},
137    {"mediatype", store_strname,  ITEM(res_store.media_type), 0, ITEM_REQUIRED, 0},
138    {"autochanger", store_yesno,  ITEM(res_store.autochanger), 1, ITEM_DEFAULT, 0},
139    {NULL, NULL, NULL, 0, 0, 0} 
140 };
141
142 /* 
143  *    Catalog Resource Directives
144  *
145  *   name          handler     value                 code flags    default_value
146  */
147 static struct res_items cat_items[] = {
148    {"name",     store_name,     ITEM(res_cat.hdr.name),    0, ITEM_REQUIRED, 0},
149    {"description", store_str,   ITEM(res_cat.hdr.desc),    0, 0, 0},
150    {"address",  store_str,      ITEM(res_cat.db_address),  0, 0, 0},
151    {"dbaddress", store_str,     ITEM(res_cat.db_address),  0, 0, 0},
152    {"dbport",   store_pint,     ITEM(res_cat.db_port),      0, 0, 0},
153    /* keep this password as store_str for the moment */
154    {"password", store_str,      ITEM(res_cat.db_password), 0, 0, 0},
155    {"user",     store_str,      ITEM(res_cat.db_user),     0, 0, 0},
156    {"dbname",   store_str,      ITEM(res_cat.db_name),     0, ITEM_REQUIRED, 0},
157    {"dbsocket", store_str,      ITEM(res_cat.db_socket),   0, 0, 0}, 
158    {NULL, NULL, NULL, 0, 0, 0} 
159 };
160
161 /* 
162  *    Job Resource Directives
163  *
164  *   name          handler     value                 code flags    default_value
165  */
166 static struct res_items job_items[] = {
167    {"name",     store_name,    ITEM(res_job.hdr.name), 0, ITEM_REQUIRED, 0},
168    {"description", store_str,  ITEM(res_job.hdr.desc), 0, 0, 0},
169    {"backup",   store_backup,  ITEM(res_job),          JT_BACKUP, 0, 0},
170    {"verify",   store_backup,  ITEM(res_job),          JT_VERIFY, 0, 0},
171    {"restore",  store_restore, ITEM(res_job),          JT_RESTORE, 0, 0},
172    {"schedule", store_res,     ITEM(res_job.schedule), R_SCHEDULE, 0, 0},
173    {"type",     store_jobtype, ITEM(res_job),          0, 0, 0},
174    {"level",    store_level,   ITEM(res_job),          0, 0, 0},
175    {"messages", store_res,     ITEM(res_job.messages), R_MSGS, 0, 0},
176    {"storage",  store_res,     ITEM(res_job.storage),  R_STORAGE, 0, 0},
177    {"pool",     store_res,     ITEM(res_job.pool),     R_POOL, 0, 0},
178    {"client",   store_res,     ITEM(res_job.client),   R_CLIENT, 0, 0},
179    {"fileset",  store_res,     ITEM(res_job.fileset),  R_FILESET, 0, 0},
180    {"where",    store_dir,     ITEM(res_job.RestoreWhere), 0, 0, 0},
181    {"replace",  store_replace, ITEM(res_job.replace), REPLACE_ALWAYS, ITEM_DEFAULT, 0},
182    {"bootstrap",store_dir,     ITEM(res_job.RestoreBootstrap), 0, 0, 0},
183    {"maxruntime", store_time,  ITEM(res_job.MaxRunTime), 0, 0, 0},
184    {"maxstartdelay", store_time,ITEM(res_job.MaxStartDelay), 0, 0, 0},
185    {"prefixlinks", store_yesno, ITEM(res_job.PrefixLinks), 1, ITEM_DEFAULT, 0},
186    {"prunejobs",   store_yesno, ITEM(res_job.PruneJobs), 1, ITEM_DEFAULT, 0},
187    {"prunefiles",  store_yesno, ITEM(res_job.PruneFiles), 1, ITEM_DEFAULT, 0},
188    {"prunevolumes", store_yesno, ITEM(res_job.PruneVolumes), 1, ITEM_DEFAULT, 0},
189    {"runbeforejob", store_str,  ITEM(res_job.RunBeforeJob), 0, 0, 0},
190    {"runafterjob",  store_str,  ITEM(res_job.RunAfterJob),  0, 0, 0},
191    {"spoolattributes", store_yesno, ITEM(res_job.SpoolAttributes), 1, ITEM_DEFAULT, 0},
192    {"writebootstrap", store_dir, ITEM(res_job.WriteBootstrap), 0, 0, 0},
193    {NULL, NULL, NULL, 0, 0, 0} 
194 };
195
196 /* FileSet resource
197  *
198  *   name          handler     value                 code flags    default_value
199  */
200 static struct res_items fs_items[] = {
201    {"name",        store_name, ITEM(res_fs.hdr.name), 0, ITEM_REQUIRED, 0},
202    {"description", store_str,  ITEM(res_fs.hdr.desc), 0, 0, 0},
203    {"include",     store_inc,  NULL,                  0, 0, 0},
204    {"exclude",     store_inc,  NULL,                  1, 0, 0},
205    {NULL,          NULL,       NULL,                  0, 0, 0} 
206 };
207
208 /* Schedule -- see run_conf.c */
209 /* Schedule
210  *
211  *   name          handler     value                 code flags    default_value
212  */
213 static struct res_items sch_items[] = {
214    {"name",     store_name,  ITEM(res_sch.hdr.name), 0, ITEM_REQUIRED, 0},
215    {"description", store_str, ITEM(res_sch.hdr.desc), 0, 0, 0},
216    {"run",      store_run,   ITEM(res_sch.run),      0, 0, 0},
217    {NULL, NULL, NULL, 0, 0, 0} 
218 };
219
220 /* Group resource -- not implemented
221  *
222  *   name          handler     value                 code flags    default_value
223  */
224 static struct res_items group_items[] = {
225    {"name",        store_name, ITEM(res_group.hdr.name), 0, ITEM_REQUIRED, 0},
226    {"description", store_str,  ITEM(res_group.hdr.desc), 0, 0, 0},
227    {NULL, NULL, NULL, 0, 0, 0} 
228 };
229
230 /* Pool resource
231  *
232  *   name             handler     value                        code flags default_value
233  */
234 static struct res_items pool_items[] = {
235    {"name",            store_name,    ITEM(res_pool.hdr.name),      0, ITEM_REQUIRED, 0},
236    {"description",     store_str,     ITEM(res_pool.hdr.desc),      0, 0,     0},
237    {"pooltype",        store_strname, ITEM(res_pool.pool_type),     0, ITEM_REQUIRED, 0},
238    {"labelformat",     store_strname, ITEM(res_pool.label_format),  0, 0,     0},
239    {"usecatalog",      store_yesno, ITEM(res_pool.use_catalog),     1, ITEM_DEFAULT,  1},
240    {"usevolumeonce",   store_yesno, ITEM(res_pool.use_volume_once), 1, 0,        0},
241    {"maximumvolumes",  store_pint,  ITEM(res_pool.max_volumes),     0, 0,        0},
242    {"maximumvolumejobs", store_pint,  ITEM(res_pool.MaxVolJobs),    0, 0,       0},
243    {"maximumvolumefiles", store_pint, ITEM(res_pool.MaxVolFiles),   0, 0,       0},
244    {"maximumvolumebytes", store_size, ITEM(res_pool.MaxVolBytes),   0, 0,       0},
245    {"acceptanyvolume", store_yesno, ITEM(res_pool.accept_any_volume), 1, ITEM_DEFAULT,     1},
246    {"catalogfiles",    store_yesno, ITEM(res_pool.catalog_files),   1, ITEM_DEFAULT,  1},
247    {"volumeretention", store_time,  ITEM(res_pool.VolRetention),    0, ITEM_DEFAULT, 60*60*24*365},
248    {"volumeuseduration", store_time,  ITEM(res_pool.VolUseDuration),0, 0, 0},
249    {"autoprune",       store_yesno, ITEM(res_pool.AutoPrune), 1, ITEM_DEFAULT, 1},
250    {"recycle",         store_yesno, ITEM(res_pool.Recycle),     1, ITEM_DEFAULT, 1},
251    {NULL, NULL, NULL, 0, 0, 0} 
252 };
253
254 /* 
255  * Counter Resource
256  *   name             handler     value                        code flags default_value
257  */
258 static struct res_items counter_items[] = {
259    {"name",            store_name,    ITEM(res_counter.hdr.name),        0, ITEM_REQUIRED, 0},
260    {"description",     store_str,     ITEM(res_counter.hdr.desc),        0, 0,     0},
261    {"minimum",         store_int,     ITEM(res_counter.MinValue),        0, ITEM_DEFAULT, 0},
262    {"maximum",         store_pint,    ITEM(res_counter.MaxValue),        0, ITEM_DEFAULT, INT32_MAX},
263    {"global",          store_yesno,   ITEM(res_counter.Global),          0, ITEM_DEFAULT, 0},
264    {"wrapcounter",     store_strname, ITEM(res_counter.WrapCounter),     0, 0, 0},
265    {NULL, NULL, NULL, 0, 0, 0} 
266 };
267
268
269 /* 
270  * FileOptions Resource
271  *   name             handler     value                        code flags default_value
272  */
273 static struct res_items fo_items[] = {
274    {"name",            store_name,    ITEM(res_fopts.hdr.name),    0, ITEM_REQUIRED, 0},
275    {"description",     store_str,     ITEM(res_fopts.hdr.desc),    0, 0, 0},
276    {"compression",     store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
277    {"signature",       store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
278    {"verify",          store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
279    {"onefs",           store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
280    {"recurse",         store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
281    {"sparse",          store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
282    {"readfifo",        store_opts,    ITEM(res_fopts.opts),        0, 0, 0},
283    {"replace",         store_replace, ITEM(res_fopts.replace),     0, 0, 0},
284    {"match",           store_match,   ITEM(res_fopts.match),       0, 0, 0},
285    {NULL, NULL, NULL, 0, 0, 0} 
286 };
287
288
289
290 /* Message resource */
291 extern struct res_items msgs_items[];
292
293 /* 
294  * This is the master resource definition.  
295  * It must have one item for each of the resources.
296  *
297  *  name             items        rcode        res_head
298  */
299 struct s_res resources[] = {
300    {"director",      dir_items,   R_DIRECTOR,  NULL},
301    {"client",        cli_items,   R_CLIENT,    NULL},
302    {"job",           job_items,   R_JOB,       NULL},
303    {"storage",       store_items, R_STORAGE,   NULL},
304    {"catalog",       cat_items,   R_CATALOG,   NULL},
305    {"schedule",      sch_items,   R_SCHEDULE,  NULL},
306    {"fileset",       fs_items,    R_FILESET,   NULL},
307    {"group",         group_items, R_GROUP,     NULL},
308    {"pool",          pool_items,  R_POOL,      NULL},
309    {"messages",      msgs_items,  R_MSGS,      NULL},
310    {"counter",       counter_items, R_COUNTER, NULL},
311    {"fileoptions",   fo_items,    R_FILEOPTIONS, NULL},
312    {NULL,            NULL,        0,           NULL}
313 };
314
315
316 /* Keywords (RHS) permitted in Job Level records   
317  *
318  *   level_name      level              job_type
319  */
320 struct s_jl joblevels[] = {
321    {"Full",          L_FULL,            JT_BACKUP},
322    {"Incremental",   L_INCREMENTAL,     JT_BACKUP},
323    {"Differential",  L_DIFFERENTIAL,    JT_BACKUP},
324    {"Since",         L_SINCE,           JT_BACKUP},
325    {"Catalog",       L_VERIFY_CATALOG,  JT_VERIFY},
326    {"Initcatalog",   L_VERIFY_INIT,     JT_VERIFY},
327    {"VolumeToCatalog", L_VERIFY_VOLUME_TO_CATALOG,   JT_VERIFY},
328    {"Data",          L_VERIFY_DATA,     JT_VERIFY},
329    {NULL,            0}
330 };
331
332 /* Keywords (RHS) permitted in Job type records   
333  *
334  *   type_name       job_type
335  */
336 struct s_jt jobtypes[] = {
337    {"backup",        JT_BACKUP},
338    {"admin",         JT_ADMIN},
339    {"verify",        JT_VERIFY},
340    {"restore",       JT_RESTORE},
341    {NULL,            0}
342 };
343
344
345 /* Keywords (RHS) permitted in Backup and Verify records */
346 static struct s_kw BakVerFields[] = {
347    {"client",        'C'},
348    {"fileset",       'F'},
349    {"level",         'L'}, 
350    {NULL,            0}
351 };
352
353 /* Keywords (RHS) permitted in Restore records */
354 static struct s_kw RestoreFields[] = {
355    {"client",        'C'},
356    {"fileset",       'F'},
357    {"jobid",         'J'},            /* JobId to restore */
358    {"where",         'W'},            /* root of restore */
359    {"replace",       'R'},            /* replacement options */
360    {"bootstrap",     'B'},            /* bootstrap file */
361    {NULL,              0}
362 };
363
364 /* Options permitted in Restore replace= */
365 struct s_kw ReplaceOptions[] = {
366    {"always",         REPLACE_ALWAYS},
367    {"ifnewer",        REPLACE_IFNEWER},
368    {"ifolder",        REPLACE_IFOLDER},
369    {"never",          REPLACE_NEVER},
370    {NULL,               0}
371 };
372
373
374
375 /* Define FileSet KeyWord values */
376
377 #define INC_KW_NONE         0
378 #define INC_KW_COMPRESSION  1
379 #define INC_KW_SIGNATURE    2
380 #define INC_KW_ENCRYPTION   3
381 #define INC_KW_VERIFY       4
382 #define INC_KW_ONEFS        5
383 #define INC_KW_RECURSE      6
384 #define INC_KW_SPARSE       7
385 #define INC_KW_REPLACE      8         /* restore options */
386 #define INC_KW_READFIFO     9         /* Causes fifo data to be read */
387 #define INC_KW_FILEOPTIONS 10         /* file options */
388
389 /* Include keywords -- these are keywords that can appear
390  *    in the options lists of an include ( Include = compression= ...)
391  */
392 static struct s_kw FS_option_kw[] = {
393    {"compression", INC_KW_COMPRESSION},
394    {"signature",   INC_KW_SIGNATURE},
395    {"encryption",  INC_KW_ENCRYPTION},
396    {"verify",      INC_KW_VERIFY},
397    {"onefs",       INC_KW_ONEFS},
398    {"recurse",     INC_KW_RECURSE},
399    {"sparse",      INC_KW_SPARSE},
400    {"replace",     INC_KW_REPLACE},
401    {"readfifo",    INC_KW_READFIFO},
402    {"fileoptions", INC_KW_FILEOPTIONS},
403    {NULL,          0}
404 };
405
406 /* Options for FileSet keywords */
407
408 struct s_fs_opt {
409    char *name;
410    int keyword;
411    char *option;
412 };
413
414 /* Options permitted for each keyword and resulting value */
415 static struct s_fs_opt FS_options[] = {
416    {"md5",      INC_KW_SIGNATURE,    "M"},
417    {"sha1",     INC_KW_SIGNATURE,    "S"},
418    {"gzip",     INC_KW_COMPRESSION,  "Z6"},
419    {"gzip1",    INC_KW_COMPRESSION,  "Z1"},
420    {"gzip2",    INC_KW_COMPRESSION,  "Z2"},
421    {"gzip3",    INC_KW_COMPRESSION,  "Z3"},
422    {"gzip4",    INC_KW_COMPRESSION,  "Z4"},
423    {"gzip5",    INC_KW_COMPRESSION,  "Z5"},
424    {"gzip6",    INC_KW_COMPRESSION,  "Z6"},
425    {"gzip7",    INC_KW_COMPRESSION,  "Z7"},
426    {"gzip8",    INC_KW_COMPRESSION,  "Z8"},
427    {"gzip9",    INC_KW_COMPRESSION,  "Z9"},
428    {"blowfish", INC_KW_ENCRYPTION,    "B"},   /* ***FIXME*** not implemented */
429    {"3des",     INC_KW_ENCRYPTION,    "3"},   /* ***FIXME*** not implemented */
430    {"yes",      INC_KW_ONEFS,         "0"},
431    {"no",       INC_KW_ONEFS,         "f"},
432    {"yes",      INC_KW_RECURSE,       "0"},
433    {"no",       INC_KW_RECURSE,       "h"},
434    {"yes",      INC_KW_SPARSE,        "s"},
435    {"no",       INC_KW_SPARSE,        "0"},
436    {"always",   INC_KW_REPLACE,       "a"},
437    {"ifnewer",  INC_KW_REPLACE,       "w"},
438    {"never",    INC_KW_REPLACE,       "n"},
439    {"yes",      INC_KW_READFIFO,      "r"},
440    {"no",       INC_KW_READFIFO,      "0"},
441    {NULL,       0,                   0}
442 };
443
444 char *level_to_str(int level)
445 {
446    int i;
447    static char level_no[30];
448    char *str = level_no;
449
450    sprintf(level_no, "%d", level);    /* default if not found */
451    for (i=0; joblevels[i].level_name; i++) {
452       if (level == joblevels[i].level) {
453          str = joblevels[i].level_name;
454          break;
455       }
456    }
457    return str;
458 }
459
460
461
462 /* Dump contents of resource */
463 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
464 {
465    URES *res = (URES *)reshdr;
466    int recurse = 1;
467
468    if (res == NULL) {
469       sendit(sock, "No %s resource defined\n", res_to_str(type));
470       return;
471    }
472    if (type < 0) {                    /* no recursion */
473       type = - type;
474       recurse = 0;
475    }
476    switch (type) {
477       case R_DIRECTOR:
478          char ed1[30], ed2[30];
479          sendit(sock, "Director: name=%s maxjobs=%d FDtimeout=%s SDtimeout=%s\n", 
480             reshdr->name, res->res_dir.MaxConcurrentJobs, 
481             edit_uint64(res->res_dir.FDConnectTimeout, ed1),
482             edit_uint64(res->res_dir.SDConnectTimeout, ed2));
483          if (res->res_dir.query_file) {
484             sendit(sock, "   query_file=%s\n", res->res_dir.query_file);
485          }
486          if (res->res_dir.messages) {
487             sendit(sock, "  --> ");
488             dump_resource(-R_MSGS, (RES *)res->res_dir.messages, sendit, sock);
489          }
490          break;
491       case R_CLIENT:
492          sendit(sock, "Client: name=%s address=%s FDport=%d\n",
493             res->res_client.hdr.name, res->res_client.address, res->res_client.FDport);
494          sendit(sock, "      JobRetention=%" lld " FileRetention=%" lld " AutoPrune=%d\n",
495             res->res_client.JobRetention, res->res_client.FileRetention,
496             res->res_client.AutoPrune);
497          if (res->res_client.catalog) {
498             sendit(sock, "  --> ");
499             dump_resource(-R_CATALOG, (RES *)res->res_client.catalog, sendit, sock);
500          }
501          break;
502       case R_STORAGE:
503          sendit(sock, "Storage: name=%s address=%s SDport=%d\n\
504          DeviceName=%s MediaType=%s\n",
505             res->res_store.hdr.name, res->res_store.address, res->res_store.SDport,
506             res->res_store.dev_name, res->res_store.media_type);
507          break;
508       case R_CATALOG:
509          sendit(sock, "Catalog: name=%s address=%s DBport=%d db_name=%s\n\
510          db_user=%s\n",
511             res->res_cat.hdr.name, NPRT(res->res_cat.db_address),
512             res->res_cat.db_port, res->res_cat.db_name, NPRT(res->res_cat.db_user));
513          break;
514       case R_JOB:
515          sendit(sock, "Job: name=%s JobType=%d level=%s\n", res->res_job.hdr.name, 
516             res->res_job.JobType, level_to_str(res->res_job.level));
517          if (res->res_job.client) {
518             sendit(sock, "  --> ");
519             dump_resource(-R_CLIENT, (RES *)res->res_job.client, sendit, sock);
520          }
521          if (res->res_job.fileset) {
522             sendit(sock, "  --> ");
523             dump_resource(-R_FILESET, (RES *)res->res_job.fileset, sendit, sock);
524          }
525          if (res->res_job.schedule) {
526             sendit(sock, "  --> ");
527             dump_resource(-R_SCHEDULE, (RES *)res->res_job.schedule, sendit, sock);
528          }
529          if (res->res_job.RestoreWhere) {
530             sendit(sock, "  --> Where=%s\n", NPRT(res->res_job.RestoreWhere));
531          }
532          if (res->res_job.RestoreBootstrap) {
533             sendit(sock, "  --> Bootstrap=%s\n", NPRT(res->res_job.RestoreBootstrap));
534          }
535          if (res->res_job.RunBeforeJob) {
536             sendit(sock, "  --> RunBefore=%s\n", NPRT(res->res_job.RunBeforeJob));
537          }
538          if (res->res_job.RunAfterJob) {
539             sendit(sock, "  --> RunAfter=%s\n", NPRT(res->res_job.RunAfterJob));
540          }
541          if (res->res_job.WriteBootstrap) {
542             sendit(sock, "  --> WriteBootstrap=%s\n", NPRT(res->res_job.WriteBootstrap));
543          }
544          if (res->res_job.storage) {
545             sendit(sock, "  --> ");
546             dump_resource(-R_STORAGE, (RES *)res->res_job.storage, sendit, sock);
547          }
548          if (res->res_job.pool) {
549             sendit(sock, "  --> ");
550             dump_resource(-R_POOL, (RES *)res->res_job.pool, sendit, sock);
551          } else {
552             sendit(sock, "!!! No Pool resource\n");
553          }
554          if (res->res_job.messages) {
555             sendit(sock, "  --> ");
556             dump_resource(-R_MSGS, (RES *)res->res_job.messages, sendit, sock);
557          }
558          break;
559       case R_FILESET:
560          sendit(sock, "FileSet: name=%s\n", res->res_fs.hdr.name);
561          for (int i=0; i<res->res_fs.num_includes; i++) {
562             INCEXE *incexe = res->res_fs.include_items[i];
563             for (int j=0; j<incexe->num_names; j++) {
564                sendit(sock, "      Inc: %s\n", incexe->name_list[j]);
565             }
566          }
567          for (int i=0; i<res->res_fs.num_excludes; i++) {
568             INCEXE *incexe = res->res_fs.exclude_items[i];
569             for (int j=0; j<incexe->num_names; j++) {
570                sendit(sock, "      Exc: %s\n", incexe->name_list[j]);
571             }
572          }
573          break;
574       case R_SCHEDULE:
575          if (res->res_sch.run) {
576             int i;
577             RUN *run = res->res_sch.run;
578             char buf[1000], num[10];
579             sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
580             if (!run) {
581                break;
582             }
583 next_run:
584             sendit(sock, "  --> Run Level=%s\n", level_to_str(run->level));
585             strcpy(buf, "      hour=");
586             for (i=0; i<24; i++) {
587                if (bit_is_set(i, run->hour)) {
588                   sprintf(num, "%d ", i);
589                   strcat(buf, num);
590                }
591             }
592             strcat(buf, "\n");
593             sendit(sock, buf);
594             strcpy(buf, "      mday=");
595             for (i=0; i<31; i++) {
596                if (bit_is_set(i, run->mday)) {
597                   sprintf(num, "%d ", i+1);
598                   strcat(buf, num);
599                }
600             }
601             strcat(buf, "\n");
602             sendit(sock, buf);
603             strcpy(buf, "      month=");
604             for (i=0; i<12; i++) {
605                if (bit_is_set(i, run->month)) {
606                   sprintf(num, "%d ", i+1);
607                   strcat(buf, num);
608                }
609             }
610             strcat(buf, "\n");
611             sendit(sock, buf);
612             strcpy(buf, "      wday=");
613             for (i=0; i<7; i++) {
614                if (bit_is_set(i, run->wday)) {
615                   sprintf(num, "%d ", i+1);
616                   strcat(buf, num);
617                }
618             }
619             strcat(buf, "\n");
620             sendit(sock, buf);
621             strcpy(buf, "      wpos=");
622             for (i=0; i<5; i++) {
623                if (bit_is_set(i, run->wpos)) {
624                   sprintf(num, "%d ", i+1);
625                   strcat(buf, num);
626                }
627             }
628             strcat(buf, "\n");
629             sendit(sock, buf);
630             sendit(sock, "      mins=%d\n", run->minute);
631             if (run->pool) {
632                sendit(sock, "     --> ");
633                dump_resource(-R_POOL, (RES *)run->pool, sendit, sock);
634             }
635             if (run->storage) {
636                sendit(sock, "     --> ");
637                dump_resource(-R_STORAGE, (RES *)run->storage, sendit, sock);
638             }
639             if (run->msgs) {
640                sendit(sock, "     --> ");
641                dump_resource(-R_MSGS, (RES *)run->msgs, sendit, sock);
642             }
643             /* If another Run record is chained in, go print it */
644             if (run->next) {
645                run = run->next;
646                goto next_run;
647             }
648          } else {
649             sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
650          }
651          break;
652       case R_GROUP:
653          sendit(sock, "Group: name=%s\n", res->res_group.hdr.name);
654          break;
655       case R_POOL:
656          sendit(sock, "Pool: name=%s PoolType=%s\n", res->res_pool.hdr.name,
657                  res->res_pool.pool_type);
658          sendit(sock, "      use_cat=%d use_once=%d acpt_any=%d cat_files=%d\n",
659                  res->res_pool.use_catalog, res->res_pool.use_volume_once,
660                  res->res_pool.accept_any_volume, res->res_pool.catalog_files);
661          sendit(sock, "      max_vols=%d auto_prune=%d VolRetention=%" lld "\n",
662                  res->res_pool.max_volumes, res->res_pool.AutoPrune,
663                  res->res_pool.VolRetention);
664          sendit(sock, "      recycle=%d LabelFormat=%s\n", res->res_pool.Recycle,
665                  NPRT(res->res_pool.label_format));
666          break;
667       case R_MSGS:
668          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
669          if (res->res_msgs.mail_cmd) 
670             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
671          if (res->res_msgs.operator_cmd) 
672             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
673          break;
674       case R_FILEOPTIONS:
675          sendit(sock, "FileOptions: name=%s\n", res->res_msgs.hdr.name);
676          sendit(sock, "      opts=%s replace=%c\n", res->res_fopts.opts,
677                 res->res_fopts.replace);
678          for (int i=0; i < res->res_fopts.num_match; i++) {
679             sendit(sock, "      match=%s\n", res->res_fopts.match[i]);
680          }
681          break;
682       default:
683          sendit(sock, "Unknown resource type %d in dump_resource.\n", type);
684          break;
685    }
686    if (recurse && res->res_dir.hdr.next) {
687       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
688    }
689 }
690
691 /* 
692  * Free memory of resource.  
693  * NB, we don't need to worry about freeing any references
694  * to other resources as they will be freed when that 
695  * resource chain is traversed.  Mainly we worry about freeing
696  * allocated strings (names).
697  */
698 void free_resource(int type)
699 {
700    int num;
701    URES *res;
702    RES *nres;
703    int rindex = type - r_first;
704
705    res = (URES *)resources[rindex].res_head;
706
707    if (res == NULL)
708       return;
709
710    /* common stuff -- free the resource name and description */
711    nres = (RES *)res->res_dir.hdr.next;
712    if (res->res_dir.hdr.name) {
713       free(res->res_dir.hdr.name);
714    }
715    if (res->res_dir.hdr.desc) {
716       free(res->res_dir.hdr.desc);
717    }
718
719    switch (type) {
720       case R_DIRECTOR:
721          if (res->res_dir.working_directory) {
722             free(res->res_dir.working_directory);
723          }
724          if (res->res_dir.pid_directory) {
725             free(res->res_dir.pid_directory);
726          }
727          if (res->res_dir.subsys_directory) {
728             free(res->res_dir.subsys_directory);
729          }
730          if (res->res_dir.password) {
731             free(res->res_dir.password);
732          }
733          if (res->res_dir.query_file) {
734             free(res->res_dir.query_file);
735          }
736          if (res->res_dir.DIRaddr) {
737             free(res->res_dir.DIRaddr);
738          }
739          break;
740       case R_CLIENT:
741          if (res->res_client.address) {
742             free(res->res_client.address);
743          }
744          if (res->res_client.password) {
745             free(res->res_client.password);
746          }
747          break;
748       case R_STORAGE:
749          if (res->res_store.address) {
750             free(res->res_store.address);
751          }
752          if (res->res_store.password) {
753             free(res->res_store.password);
754          }
755          if (res->res_store.media_type) {
756             free(res->res_store.media_type);
757          }
758          if (res->res_store.dev_name) {
759             free(res->res_store.dev_name);
760          }
761          break;
762       case R_CATALOG:
763          if (res->res_cat.db_address) {
764             free(res->res_cat.db_address);
765          }
766          if (res->res_cat.db_socket) {
767             free(res->res_cat.db_socket);
768          }
769          if (res->res_cat.db_user) {
770             free(res->res_cat.db_user);
771          }
772          if (res->res_cat.db_name) {
773             free(res->res_cat.db_name);
774          }
775          if (res->res_cat.db_password) {
776             free(res->res_cat.db_password);
777          }
778          break;
779       case R_FILESET:
780          if ((num=res->res_fs.num_includes)) {
781             while (--num >= 0) {   
782                INCEXE *incexe = res->res_fs.include_items[num];
783                for (int i=0; i<incexe->num_names; i++) {
784                   free(incexe->name_list[i]);
785                }
786                if (incexe->name_list) {
787                   free(incexe->name_list);
788                }
789                free(incexe);
790             }
791             free(res->res_fs.include_items);
792          }
793          res->res_fs.num_includes = 0;
794          if ((num=res->res_fs.num_excludes)) {
795             while (--num >= 0) {   
796                INCEXE *incexe = res->res_fs.exclude_items[num];
797                for (int i=0; i<incexe->num_names; i++) {
798                   free(incexe->name_list[i]);
799                }
800                if (incexe->name_list) {
801                   free(incexe->name_list);
802                }
803                free(incexe);
804             }
805             free(res->res_fs.exclude_items);
806          }
807          res->res_fs.num_excludes = 0;
808          break;
809       case R_POOL:
810          if (res->res_pool.pool_type) {
811             free(res->res_pool.pool_type);
812          }
813          if (res->res_pool.label_format) {
814             free(res->res_pool.label_format);
815          }
816          break;
817       case R_SCHEDULE:
818          if (res->res_sch.run) {
819             RUN *nrun, *next;
820             nrun = res->res_sch.run;
821             while (nrun) {
822                next = nrun->next;
823                free(nrun);
824                nrun = next;
825             }
826          }
827          break;
828       case R_JOB:
829          if (res->res_job.RestoreWhere) {
830             free(res->res_job.RestoreWhere);
831          }
832          if (res->res_job.RestoreBootstrap) {
833             free(res->res_job.RestoreBootstrap);
834          }
835          if (res->res_job.WriteBootstrap) {
836             free(res->res_job.WriteBootstrap);
837          }
838          if (res->res_job.RunBeforeJob) {
839             free(res->res_job.RunBeforeJob);
840          }
841          if (res->res_job.RunAfterJob) {
842             free(res->res_job.RunAfterJob);
843          }
844          break;
845       case R_MSGS:
846          if (res->res_msgs.mail_cmd)
847             free(res->res_msgs.mail_cmd);
848          if (res->res_msgs.operator_cmd)
849             free(res->res_msgs.operator_cmd);
850          free_msgs_res((MSGS *)res);  /* free message resource */
851          res = NULL;
852          break;
853       case R_FILEOPTIONS:
854          if (res->res_fopts.num_match) {
855             for (int i=0; i < res->res_fopts.num_match; i++) {
856                free(res->res_fopts.match[i]);
857             }
858             free((char *)res->res_fopts.match);   
859          }
860          break;
861       case R_GROUP:
862          break;
863       default:
864          printf("Unknown resource type %d in free_resource.\n", type);
865    }
866    /* Common stuff again -- free the resource, recurse to next one */
867    if (res) {
868       free(res);
869    }
870    resources[rindex].res_head = nres;
871    if (nres) {
872       free_resource(type);
873    }
874 }
875
876 /*
877  * Save the new resource by chaining it into the head list for
878  * the resource. If this is pass 2, we update any resource
879  * pointers because they may not have been defined until 
880  * later in pass 1.
881  */
882 void save_resource(int type, struct res_items *items, int pass)
883 {
884    URES *res;
885    int rindex = type - r_first;
886    int i, size;
887    int error = 0;
888    
889    /* 
890     * Ensure that all required items are present
891     */
892    for (i=0; items[i].name; i++) {
893       if (items[i].flags & ITEM_REQUIRED) {
894             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
895                Emsg2(M_ERROR_TERM, 0, "%s item is required in %s resource, but not found.\n",
896                  items[i].name, resources[rindex]);
897              }
898       }
899       /* If this triggers, take a look at lib/parse_conf.h */
900       if (i >= MAX_RES_ITEMS) {
901          Emsg1(M_ERROR_TERM, 0, "Too many items in %s resource\n", resources[rindex]);
902       }
903    }
904
905    /* During pass 2 in each "store" routine, we looked up pointers 
906     * to all the resources referrenced in the current resource, now we
907     * must copy their addresses from the static record to the allocated
908     * record.
909     */
910    if (pass == 2) {
911       switch (type) {
912          /* Resources not containing a resource */
913          case R_CATALOG:
914          case R_STORAGE:
915          case R_FILEOPTIONS:
916          case R_GROUP:
917          case R_POOL:
918          case R_MSGS:
919             break;
920
921          /* Special case for FileSet, all allocations are done
922           * only in pass 2, so here we must copy the whole structure.
923           * This is done so that we can have forward references to
924           *   FileOptions resources within the fileset
925           */
926          case R_FILESET:
927             RES res_save;
928             if ((res = (URES *)GetResWithName(R_FILESET, res_all.res_dir.hdr.name)) == NULL) {
929                Emsg1(M_ERROR_TERM, 0, "Cannot find FileSet resource %s\n", res_all.res_dir.hdr.name);
930             }
931             memcpy(&res_save, &res_all, sizeof(RES));
932             memcpy(&res_all, res, sizeof(RES));
933             memcpy(res, &res_all, sizeof(FILESET));
934             memcpy(&res_all, &res_save, sizeof(RES));
935             break;
936
937          /* Resources containing another resource */
938          case R_DIRECTOR:
939             if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
940                Emsg1(M_ERROR_TERM, 0, "Cannot find Director resource %s\n", res_all.res_dir.hdr.name);
941             }
942             res->res_dir.messages = res_all.res_dir.messages;
943             break;
944          case R_JOB:
945             if ((res = (URES *)GetResWithName(R_JOB, res_all.res_dir.hdr.name)) == NULL) {
946                Emsg1(M_ERROR_TERM, 0, "Cannot find Job resource %s\n", res_all.res_dir.hdr.name);
947             }
948             res->res_job.messages = res_all.res_job.messages;
949             res->res_job.schedule = res_all.res_job.schedule;
950             res->res_job.client   = res_all.res_job.client;
951             res->res_job.fileset  = res_all.res_job.fileset;
952             res->res_job.storage  = res_all.res_job.storage;
953             res->res_job.pool     = res_all.res_job.pool;
954             if (res->res_job.JobType == 0) {
955                Emsg1(M_ERROR_TERM, 0, "Job Type not defined for Job resource %s\n", res_all.res_dir.hdr.name);
956             }
957             if (res->res_job.level != 0) {
958                int i;
959                for (i=0; joblevels[i].level_name; i++) {
960                   if (joblevels[i].level == res->res_job.level &&
961                       joblevels[i].job_type == res->res_job.JobType) {
962                      i = 0;
963                      break;
964                   }
965                }
966                if (i != 0) {
967                   Emsg1(M_ERROR_TERM, 0, "Inappropriate level specified in Job resource %s\n", 
968                      res_all.res_dir.hdr.name);
969                }
970             }
971             break;
972          case R_CLIENT:
973             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_client.hdr.name)) == NULL) {
974                Emsg1(M_ERROR_TERM, 0, "Cannot find Client resource %s\n", res_all.res_client.hdr.name);
975             }
976             res->res_client.catalog = res_all.res_client.catalog;
977             break;
978          case R_SCHEDULE:
979             /* Schedule is a bit different in that it contains a RUN record
980              * chain which isn't a "named" resource. This chain was linked
981              * in by run_conf.c during pass 2, so here we jam the pointer 
982              * into the Schedule resource.                         
983              */
984             if ((res = (URES *)GetResWithName(R_SCHEDULE, res_all.res_client.hdr.name)) == NULL) {
985                Emsg1(M_ERROR_TERM, 0, "Cannot find Schedule resource %s\n", res_all.res_client.hdr.name);
986             }
987             res->res_sch.run = res_all.res_sch.run;
988             break;
989          default:
990             Emsg1(M_ERROR, 0, "Unknown resource type %d in save_resource.\n", type);
991             error = 1;
992             break;
993       }
994       /* Note, the resource name was already saved during pass 1,
995        * so here, we can just release it.
996        */
997       if (res_all.res_dir.hdr.name) {
998          free(res_all.res_dir.hdr.name);
999          res_all.res_dir.hdr.name = NULL;
1000       }
1001       if (res_all.res_dir.hdr.desc) {
1002          free(res_all.res_dir.hdr.desc);
1003          res_all.res_dir.hdr.desc = NULL;
1004       }
1005       return;
1006    }
1007
1008    /* The following code is only executed for pass 1 */
1009    switch (type) {
1010       case R_DIRECTOR:
1011          size = sizeof(DIRRES);
1012          break;
1013       case R_CLIENT:
1014          size =sizeof(CLIENT);
1015          break;
1016       case R_STORAGE:
1017          size = sizeof(STORE); 
1018          break;
1019       case R_CATALOG:
1020          size = sizeof(CAT);
1021          break;
1022       case R_JOB:
1023          size = sizeof(JOB);
1024          break;
1025       case R_FILESET:
1026          size = sizeof(FILESET);
1027          break;
1028       case R_SCHEDULE:
1029          size = sizeof(SCHED);
1030          break;
1031       case R_GROUP:
1032          size = sizeof(GROUP);
1033          break;
1034       case R_POOL:
1035          size = sizeof(POOL);
1036          break;
1037       case R_MSGS:
1038          size = sizeof(MSGS);
1039          break;
1040       case R_COUNTER:
1041          size = sizeof(COUNTER);
1042          break;
1043       case R_FILEOPTIONS:
1044          size = sizeof(FILEOPTIONS);
1045          break;
1046       default:
1047          printf("Unknown resource type %d in save_resrouce.\n", type);
1048          error = 1;
1049          size = 1;
1050          break;
1051    }
1052    /* Common */
1053    if (!error) {
1054       res = (URES *)malloc(size);
1055       memcpy(res, &res_all, size);
1056       if (!resources[rindex].res_head) {
1057          resources[rindex].res_head = (RES *)res; /* store first entry */
1058          Dmsg3(200, "Inserting first %s res: %s index=%d\n", res_to_str(type),
1059                res->res_dir.hdr.name, rindex);
1060       } else {
1061          RES *next;
1062          /* Add new res to end of chain */
1063          for (next=resources[rindex].res_head; next->next; next=next->next)
1064             { }
1065          next->next = (RES *)res;
1066          Dmsg3(200, "Inserting %s res: %s index=%d\n", res_to_str(type),
1067                res->res_dir.hdr.name, rindex);
1068       }
1069    }
1070 }
1071
1072 /* 
1073  * Store JobType (backup, verify, restore)
1074  *
1075  */
1076 static void store_jobtype(LEX *lc, struct res_items *item, int index, int pass)
1077 {
1078    int token, i;   
1079
1080    token = lex_get_token(lc, T_NAME);
1081    /* Store the type both pass 1 and pass 2 */
1082    for (i=0; jobtypes[i].type_name; i++) {
1083       if (strcasecmp(lc->str, jobtypes[i].type_name) == 0) {
1084          ((JOB *)(item->value))->JobType = jobtypes[i].job_type;
1085          i = 0;
1086          break;
1087       }
1088    }
1089    if (i != 0) {
1090       scan_err1(lc, "Expected a Job Type keyword, got: %s", lc->str);
1091    }
1092    scan_to_eol(lc);
1093    set_bit(index, res_all.hdr.item_present);
1094 }
1095
1096 /* 
1097  * Store Job Level (Full, Incremental, ...)
1098  *
1099  */
1100 static void store_level(LEX *lc, struct res_items *item, int index, int pass)
1101 {
1102    int token, i;
1103
1104    token = lex_get_token(lc, T_NAME);
1105    /* Store the level pass 2 so that type is defined */
1106    for (i=0; joblevels[i].level_name; i++) {
1107       if (strcasecmp(lc->str, joblevels[i].level_name) == 0) {
1108          ((JOB *)(item->value))->level = joblevels[i].level;
1109          i = 0;
1110          break;
1111       }
1112    }
1113    if (i != 0) {
1114       scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
1115    }
1116    scan_to_eol(lc);
1117    set_bit(index, res_all.hdr.item_present);
1118 }
1119
1120 static void store_replace(LEX *lc, struct res_items *item, int index, int pass)
1121 {
1122    int token, i;
1123    token = lex_get_token(lc, T_NAME);
1124    /* Scan Replacement options */
1125    for (i=0; ReplaceOptions[i].name; i++) {
1126       if (strcasecmp(lc->str, ReplaceOptions[i].name) == 0) {
1127          *(int *)(item->value) = ReplaceOptions[i].token;
1128          i = 0;
1129          break;
1130       }
1131    }
1132    if (i != 0) {
1133       scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
1134    }
1135    scan_to_eol(lc);
1136    set_bit(index, res_all.hdr.item_present);
1137 }
1138
1139 /* 
1140  * Store backup/verify info for Job record 
1141  *
1142  * Note, this code is used for both BACKUP and VERIFY jobs
1143  *
1144  *    Backup = Client=<client-name> FileSet=<FileSet-name> Level=<level>
1145  */
1146 static void store_backup(LEX *lc, struct res_items *item, int index, int pass)
1147 {
1148    int token, i;
1149    RES *res;
1150    int options = lc->options;
1151
1152    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
1153
1154    
1155    ((JOB *)(item->value))->JobType = item->code;
1156    while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
1157       int found;
1158
1159       Dmsg1(150, "store_backup got token=%s\n", lex_tok_to_str(token));
1160       
1161       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1162          scan_err1(lc, "Expected a backup/verify keyword, got: %s", lc->str);
1163       }
1164       Dmsg1(190, "Got keyword: %s\n", lc->str);
1165       found = FALSE;
1166       for (i=0; BakVerFields[i].name; i++) {
1167          if (strcasecmp(lc->str, BakVerFields[i].name) == 0) {
1168             found = TRUE;
1169             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
1170                scan_err1(lc, "Expected an equals, got: %s", lc->str);
1171             }
1172             token = lex_get_token(lc, T_NAME);
1173             Dmsg1(190, "Got value: %s\n", lc->str);
1174             switch (BakVerFields[i].token) {
1175                case 'C':
1176                   /* Find Client Resource */
1177                   if (pass == 2) {
1178                      res = GetResWithName(R_CLIENT, lc->str);
1179                      if (res == NULL) {
1180                         scan_err1(lc, "Could not find specified Client Resource: %s",
1181                                    lc->str);
1182                      }
1183                      res_all.res_job.client = (CLIENT *)res;
1184                   }
1185                   break;
1186                case 'F':
1187                   /* Find FileSet Resource */
1188                   if (pass == 2) {
1189                      res = GetResWithName(R_FILESET, lc->str);
1190                      if (res == NULL) {
1191                         scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
1192                                     lc->str);
1193                      }
1194                      res_all.res_job.fileset = (FILESET *)res;
1195                   }
1196                   break;
1197                case 'L':
1198                   /* Get level */
1199                   for (i=0; joblevels[i].level_name; i++) {
1200                      if (joblevels[i].job_type == item->code && 
1201                           strcasecmp(lc->str, joblevels[i].level_name) == 0) {
1202                         ((JOB *)(item->value))->level = joblevels[i].level;
1203                         i = 0;
1204                         break;
1205                      }
1206                   }
1207                   if (i != 0) {
1208                      scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
1209                   }
1210                   break;
1211             } /* end switch */
1212             break;
1213          } /* end if strcmp() */
1214       } /* end for */
1215       if (!found) {
1216          scan_err1(lc, "%s not a valid Backup/verify keyword", lc->str);
1217       }
1218    } /* end while */
1219    lc->options = options;             /* reset original options */
1220    set_bit(index, res_all.hdr.item_present);
1221 }
1222
1223 /* 
1224  * Store restore info for Job record 
1225  *
1226  *    Restore = JobId=<job-id> Where=<root-directory> Replace=<options> Bootstrap=<file>
1227  *
1228  */
1229 static void store_restore(LEX *lc, struct res_items *item, int index, int pass)
1230 {
1231    int token, i;
1232    RES *res;
1233    int options = lc->options;
1234
1235    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
1236
1237    Dmsg0(190, "Enter store_restore()\n");
1238    
1239    ((JOB *)(item->value))->JobType = item->code;
1240    while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
1241       int found; 
1242
1243       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1244          scan_err1(lc, "expected a name, got: %s", lc->str);
1245       }
1246       found = FALSE;
1247       for (i=0; RestoreFields[i].name; i++) {
1248          Dmsg1(190, "Restore kw=%s\n", lc->str);
1249          if (strcasecmp(lc->str, RestoreFields[i].name) == 0) {
1250             found = TRUE;
1251             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
1252                scan_err1(lc, "Expected an equals, got: %s", lc->str);
1253             }
1254             token = lex_get_token(lc, T_ALL);
1255             Dmsg1(190, "Restore value=%s\n", lc->str);
1256             switch (RestoreFields[i].token) {
1257                case 'B':
1258                   /* Bootstrap */
1259                   if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1260                      scan_err1(lc, "Expected a Restore bootstrap file, got: %s", lc->str);
1261                   }
1262                   if (pass == 1) {
1263                      res_all.res_job.RestoreBootstrap = bstrdup(lc->str);
1264                   }
1265                   break;
1266                case 'C':
1267                   /* Find Client Resource */
1268                   if (pass == 2) {
1269                      res = GetResWithName(R_CLIENT, lc->str);
1270                      if (res == NULL) {
1271                         scan_err1(lc, "Could not find specified Client Resource: %s",
1272                                    lc->str);
1273                      }
1274                      res_all.res_job.client = (CLIENT *)res;
1275                   }
1276                   break;
1277                case 'F':
1278                   /* Find FileSet Resource */
1279                   if (pass == 2) {
1280                      res = GetResWithName(R_FILESET, lc->str);
1281                      if (res == NULL) {
1282                         scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
1283                                     lc->str);
1284                      }
1285                      res_all.res_job.fileset = (FILESET *)res;
1286                   }
1287                   break;
1288                case 'J':
1289                   /* JobId */
1290                   if (token != T_NUMBER) {
1291                      scan_err1(lc, "expected an integer number, got: %s", lc->str);
1292                   }
1293                   errno = 0;
1294                   res_all.res_job.RestoreJobId = strtol(lc->str, NULL, 0);
1295                   Dmsg1(190, "RestorJobId=%d\n", res_all.res_job.RestoreJobId);
1296                   if (errno != 0) {
1297                      scan_err1(lc, "expected an integer number, got: %s", lc->str);
1298                   }
1299                   break;
1300                case 'W':
1301                   /* Where */
1302                   if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1303                      scan_err1(lc, "Expected a Restore root directory, got: %s", lc->str);
1304                   }
1305                   if (pass == 1) {
1306                      res_all.res_job.RestoreWhere = bstrdup(lc->str);
1307                   }
1308                   break;
1309                case 'R':
1310                   /* Replacement options */
1311                   if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1312                      scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
1313                   }
1314                   /* Fix to scan Replacement options */
1315                   for (i=0; ReplaceOptions[i].name; i++) {
1316                      if (strcasecmp(lc->str, ReplaceOptions[i].name) == 0) {
1317                          ((JOB *)(item->value))->replace = ReplaceOptions[i].token;
1318                         i = 0;
1319                         break;
1320                      }
1321                   }
1322                   if (i != 0) {
1323                      scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
1324                   }
1325                   break;
1326             } /* end switch */
1327             break;
1328          } /* end if strcmp() */
1329       } /* end for */
1330       if (!found) {
1331          scan_err1(lc, "%s not a valid Restore keyword", lc->str);
1332       }
1333    } /* end while */
1334    lc->options = options;             /* reset original options */
1335    set_bit(index, res_all.hdr.item_present);
1336 }
1337
1338
1339
1340 /* 
1341  * Scan for Include options (keyword=option) is converted into one or
1342  *  two characters. Verifyopts=xxxx is Vxxxx:
1343  */
1344 static void scan_include_options(LEX *lc, int keyword, char *opts, int optlen)
1345 {
1346    int token, i;
1347    char option[3];
1348
1349    option[0] = 0;                     /* default option = none */
1350    option[2] = 0;                     /* terminate options */
1351    token = lex_get_token(lc, T_NAME);             /* expect at least one option */       
1352    if (keyword == INC_KW_VERIFY) { /* special case */
1353       /* ***FIXME**** ensure these are in permitted set */
1354       bstrncat(opts, "V", optlen);         /* indicate Verify */
1355       bstrncat(opts, lc->str, optlen);
1356       bstrncat(opts, ":", optlen);         /* terminate it */
1357
1358    /* Standard keyword -- these are now deprecated and should be specified
1359     *  in the FileOptions resource
1360     */
1361    } else {
1362       for (i=0; FS_options[i].name; i++) {
1363          if (strcasecmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
1364             /* NOTE! maximum 2 letters here or increase option[3] */
1365             option[0] = FS_options[i].option[0];
1366             option[1] = FS_options[i].option[1];
1367             i = 0;
1368             break;
1369          }
1370       }
1371       if (i != 0) {
1372          scan_err1(lc, "Expected a FileSet option keyword, got:%s:", lc->str);
1373       } else { /* add option */
1374          bstrncat(opts, option, optlen);
1375          Dmsg3(200, "Catopts=%s option=%s optlen=%d\n", opts, option,optlen);
1376       }
1377    }
1378
1379    /* If option terminated by comma, eat it */
1380    if (lc->ch == ',') {
1381       token = lex_get_token(lc, T_ALL);      /* yes, eat comma */
1382    }
1383 }
1384
1385 /* Store FileSet Include/Exclude info */
1386 static void store_inc(LEX *lc, struct res_items *item, int index, int pass)
1387 {
1388    int token, i;
1389    int options = lc->options;
1390    int keyword;
1391    char inc_opts[100];
1392    int inc_opts_len;
1393    RES *res = NULL;
1394
1395    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
1396
1397    /* Get include options */
1398    inc_opts[0] = 0;
1399    while ((token=lex_get_token(lc, T_ALL)) != T_BOB) {
1400       keyword = INC_KW_NONE;
1401       for (i=0; FS_option_kw[i].name; i++) {
1402          if (strcasecmp(lc->str, FS_option_kw[i].name) == 0) {
1403             keyword = FS_option_kw[i].token;
1404             break;
1405          }
1406       }
1407       if (keyword == INC_KW_NONE) {
1408          scan_err1(lc, _("Expected a FileSet keyword, got: %s"), lc->str);
1409       }
1410       /* Option keyword should be following by = <option> */
1411       if ((token=lex_get_token(lc, T_ALL)) != T_EQUALS) {
1412          scan_err1(lc, _("expected an = following keyword, got: %s"), lc->str);
1413       }
1414       if (keyword == INC_KW_FILEOPTIONS) {
1415          token = lex_get_token(lc, T_NAME);
1416          if (pass == 2) {
1417             res = GetResWithName(R_FILEOPTIONS, lc->str);
1418             if (res == NULL) {
1419                scan_err1(lc, _("Could not find specified FileOptions Resource: %s"), lc->str);
1420             }
1421          }
1422       } else {
1423          scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
1424       }
1425       if (token == T_BOB) {
1426          break;
1427       }
1428    }
1429    if (!inc_opts[0]) {
1430       strcat(inc_opts, "0");          /* set no options */
1431    }
1432    inc_opts_len = strlen(inc_opts);
1433
1434    if (pass == 2) {
1435       INCEXE *incexe;
1436       if (!res_all.res_fs.have_MD5) {
1437          MD5Init(&res_all.res_fs.md5c);
1438          res_all.res_fs.have_MD5 = TRUE;
1439       }
1440       /* Create incexe structure */
1441       Dmsg0(200, "Create INCEXE structure\n");
1442       incexe = (INCEXE *)malloc(sizeof(INCEXE));
1443       memset(incexe, 0, sizeof(INCEXE));
1444       bstrncpy(incexe->opts, inc_opts, sizeof(incexe->opts));
1445       incexe->fileopts = (struct s_res_fopts *)res;
1446       Dmsg1(200, "incexe opts=%s\n", incexe->opts);
1447       if (item->code == 0) { /* include */
1448          if (res_all.res_fs.num_includes == 0) {
1449             res_all.res_fs.include_items = (INCEXE **)malloc(sizeof(INCEXE *));
1450           } else {
1451             res_all.res_fs.include_items = (INCEXE **)realloc(res_all.res_fs.include_items,
1452                            sizeof(INCEXE *) * res_all.res_fs.num_includes + 1);
1453           }
1454           res_all.res_fs.include_items[res_all.res_fs.num_includes++] = incexe;
1455           Dmsg1(200, "num_includes=%d\n", res_all.res_fs.num_includes);
1456       } else {    /* exclude */
1457          if (res_all.res_fs.num_excludes == 0) {
1458             res_all.res_fs.exclude_items = (INCEXE **)malloc(sizeof(INCEXE *));
1459           } else {
1460             res_all.res_fs.exclude_items = (INCEXE **)realloc(res_all.res_fs.exclude_items,
1461                            sizeof(INCEXE *) * res_all.res_fs.num_excludes + 1);
1462           }
1463           res_all.res_fs.exclude_items[res_all.res_fs.num_excludes++] = incexe;
1464           Dmsg1(200, "num_excludes=%d\n", res_all.res_fs.num_excludes);
1465      }
1466
1467       /* Pickup include/exclude names.  They are stored in INCEXE
1468        *  structures which contains the options and the name.
1469        */
1470       while ((token = lex_get_token(lc, T_ALL)) != T_EOB) {
1471          switch (token) {
1472             case T_COMMA:
1473             case T_EOL:
1474                continue;
1475
1476             case T_IDENTIFIER:
1477             case T_UNQUOTED_STRING:
1478             case T_QUOTED_STRING:
1479                if (res_all.res_fs.have_MD5) {
1480                   MD5Update(&res_all.res_fs.md5c, (unsigned char *)lc->str, lc->str_len);
1481                }
1482                if (incexe->num_names == incexe->max_names) {
1483                   incexe->max_names += 10;
1484                   if (incexe->name_list == NULL) {
1485                      incexe->name_list = (char **)malloc(sizeof(char *) * incexe->max_names);
1486                   } else {
1487                      incexe->name_list = (char **)realloc(incexe->name_list,
1488                            sizeof(char *) * incexe->max_names);
1489                   }
1490                }
1491                incexe->name_list[incexe->num_names++] = bstrdup(lc->str);
1492                Dmsg1(200, "Add to name_list %s\n", incexe->name_list[incexe->num_names -1]);
1493                break;
1494             default:
1495                scan_err1(lc, "Expected a filename, got: %s", lc->str);
1496          }                                 
1497       }
1498       /* Note, MD5Final is done in backup.c */
1499    } else { /* pass 1 */
1500       while (lex_get_token(lc, T_ALL) != T_EOB) 
1501          {}
1502    }
1503    scan_to_eol(lc);
1504    lc->options = options;
1505    set_bit(index, res_all.hdr.item_present);
1506 }
1507
1508 /* Store FileOptions Match info */
1509 static void store_match(LEX *lc, struct res_items *item, int index, int pass)
1510 {
1511    int token;
1512    char *match;
1513
1514    if (pass == 1) {
1515       /* Pickup Match string
1516        */
1517       token = lex_get_token(lc, T_ALL);            
1518       switch (token) {
1519          case T_IDENTIFIER:
1520          case T_UNQUOTED_STRING:
1521          case T_QUOTED_STRING:
1522             match = (char *)malloc(lc->str_len + 1);
1523             strcpy(match, lc->str);
1524             res_all.res_fopts.num_match++;
1525             if (res_all.res_fopts.match == NULL) {
1526                res_all.res_fopts.match = (char **)malloc(sizeof(char *) * res_all.res_fopts.num_match);
1527             } else {
1528                res_all.res_fopts.match = (char **)realloc(res_all.res_fopts.match,
1529                   sizeof(char *) * res_all.res_fopts.num_match);
1530             }
1531             res_all.res_fopts.match[res_all.res_fopts.num_match-1] = match;
1532             break;
1533          default:
1534             scan_err1(lc, "Expected a filename, got: %s", lc->str);
1535       }                                 
1536    } else { /* pass 2 */
1537       lex_get_token(lc, T_ALL);          
1538    }
1539    scan_to_eol(lc);
1540    set_bit(index, res_all.hdr.item_present);
1541 }
1542
1543 static void store_opts(LEX *lc, struct res_items *item, int index, int pass)
1544 {
1545    int i;
1546    int keyword;
1547    char inc_opts[100];
1548
1549    inc_opts[0] = 0;
1550    keyword = INC_KW_NONE;
1551    for (i=0; FS_option_kw[i].name; i++) {
1552       if (strcasecmp(item->name, FS_option_kw[i].name) == 0) {
1553          keyword = FS_option_kw[i].token;
1554          break;
1555       }
1556    }
1557    if (keyword == INC_KW_NONE) {
1558       scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
1559    }
1560    Dmsg2(200, "keyword=%d %s\n", keyword, FS_option_kw[keyword].name);
1561    scan_include_options(lc, keyword, inc_opts, sizeof(inc_opts));
1562
1563    bstrncat((char *)item->value, inc_opts, MAX_FO_OPTS);
1564
1565    scan_to_eol(lc);
1566    set_bit(index, res_all.hdr.item_present);
1567 }