]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird_conf.c
Work on message resources
[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 /*
23    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License as
27    published by the Free Software Foundation; either version 2 of
28    the License, or (at your option) any later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33    General Public License for more details.
34
35    You should have received a copy of the GNU General Public
36    License along with this program; if not, write to the Free
37    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38    MA 02111-1307, USA.
39
40  */
41
42 #include "bacula.h"
43 #include "dird.h"
44
45 /* Define the first and last resource ID record
46  * types. Note, these should be unique for each
47  * daemon though not a requirement.
48  */
49 int r_first = R_FIRST;
50 int r_last  = R_LAST;
51 pthread_mutex_t res_mutex =  PTHREAD_MUTEX_INITIALIZER;
52
53 /* Imported subroutines */
54 extern void store_run(LEX *lc, struct res_items *item, int index, int pass);
55
56
57 /* Forward referenced subroutines */
58
59 static void store_inc(LEX *lc, struct res_items *item, int index, int pass);
60 static void store_backup(LEX *lc, struct res_items *item, int index, int pass);
61 static void store_restore(LEX *lc, struct res_items *item, int index, int pass);
62
63
64 /* We build the current resource here as we are
65  * scanning the resource configuration definition,
66  * then move it to allocated memory when the resource
67  * scan is complete.
68  */
69 URES res_all;
70 int  res_all_size = sizeof(res_all);
71
72
73 /* Definition of records permitted within each
74  * resource with the routine to process the record 
75  * information.  NOTE! quoted names must be in lower case.
76  */ 
77 /* 
78  *    Director Resource
79  *
80  *   name          handler     value                 code flags    default_value
81  */
82 static struct res_items dir_items[] = {
83    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
84    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
85    {"messages",    store_res,      ITEM(res_dir.messages), R_MSGS, 0, 0},
86    {"dirport",     store_pint,     ITEM(res_dir.DIRport),  0, ITEM_REQUIRED, 0},
87    {"queryfile",   store_dir,      ITEM(res_dir.query_file), 0, 0, 0},
88    {"workingdirectory", store_dir, ITEM(res_dir.working_directory), 0, ITEM_REQUIRED, 0},
89    {"piddirectory", store_dir,     ITEM(res_dir.pid_directory), 0, ITEM_REQUIRED, 0},
90    {"subsysdirectory", store_dir,  ITEM(res_dir.subsys_directory), 0, ITEM_REQUIRED, 0},
91    {"maximumconcurrentjobs", store_pint, ITEM(res_dir.MaxConcurrentJobs), 0, ITEM_DEFAULT, 1},
92    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
93    {"fdconnecttimeout", store_time,ITEM(res_dir.FDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
94    {"sdconnecttimeout", store_time,ITEM(res_dir.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
95    {NULL, NULL, NULL, 0, 0, 0}
96 };
97
98 /* 
99  *    Client or File daemon resource
100  *
101  *   name          handler     value                 code flags    default_value
102  */
103
104 static struct res_items cli_items[] = {
105    {"name",     store_name,       ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
106    {"description", store_str,     ITEM(res_client.hdr.desc), 0, 0, 0},
107    {"address",  store_str,        ITEM(res_client.address),  0, ITEM_REQUIRED, 0},
108    {"fdport",   store_pint,       ITEM(res_client.FDport),   0, ITEM_REQUIRED, 0},
109    {"password", store_password,   ITEM(res_client.password), 0, ITEM_REQUIRED, 0},
110    {"catalog",  store_res,        ITEM(res_client.catalog),  R_CATALOG, 0, 0},
111    {"fileretention", store_time,  ITEM(res_client.FileRetention), 0, ITEM_DEFAULT, 60*60*24*30},
112    {"jobretention",  store_time,  ITEM(res_client.JobRetention),  0, ITEM_DEFAULT, 60*60*24*365},
113    {"autoprune", store_yesno,     ITEM(res_client.AutoPrune), 1, ITEM_DEFAULT, 0},
114    {NULL, NULL, NULL, 0, 0, 0} 
115 };
116
117 /* Storage daemon resource
118  *
119  *   name          handler     value                 code flags    default_value
120  */
121 static struct res_items store_items[] = {
122    {"name",      store_name,     ITEM(res_store.hdr.name),   0, ITEM_REQUIRED, 0},
123    {"description", store_str,    ITEM(res_store.hdr.desc),   0, 0, 0},
124    {"sdport",    store_pint,     ITEM(res_store.SDport),     0, ITEM_REQUIRED, 0},
125    {"sddport",   store_pint,     ITEM(res_store.SDDport),    0, 0, 0}, /* deprecated */
126    {"address",   store_str,      ITEM(res_store.address),    0, ITEM_REQUIRED, 0},
127    {"password",  store_password, ITEM(res_store.password),   0, ITEM_REQUIRED, 0},
128    {"device",    store_strname,  ITEM(res_store.dev_name),   0, ITEM_REQUIRED, 0},
129    {"mediatype", store_strname,  ITEM(res_store.media_type), 0, ITEM_REQUIRED, 0},
130    {NULL, NULL, NULL, 0, 0, 0} 
131 };
132
133 /* 
134  *    Catalog Resource Directives
135  *
136  *   name          handler     value                 code flags    default_value
137  */
138 static struct res_items cat_items[] = {
139    {"name",     store_name,     ITEM(res_cat.hdr.name),    0, ITEM_REQUIRED, 0},
140    {"description", store_str,   ITEM(res_cat.hdr.desc),    0, 0, 0},
141    {"address",  store_str,      ITEM(res_cat.address),     0, 0, 0},
142    {"dbport",   store_pint,     ITEM(res_cat.DBport),      0, 0, 0},
143    /* keep this password as store_str for the moment */
144    {"password", store_str,      ITEM(res_cat.db_password), 0, 0, 0},
145    {"user",     store_str,      ITEM(res_cat.db_user),     0, 0, 0},
146    {"dbname",   store_str,      ITEM(res_cat.db_name),     0, ITEM_REQUIRED, 0},
147    {NULL, NULL, NULL, 0, 0, 0} 
148 };
149
150 /* 
151  *    Job Resource Directives
152  *
153  *   name          handler     value                 code flags    default_value
154  */
155 static struct res_items job_items[] = {
156    {"name",     store_name,   ITEM(res_job.hdr.name), 0, ITEM_REQUIRED, 0},
157    {"description", store_str, ITEM(res_job.hdr.desc), 0, 0, 0},
158    {"backup",   store_backup, ITEM(res_job),          JT_BACKUP, 0, 0},
159    {"verify",   store_backup, ITEM(res_job),          JT_VERIFY, 0, 0},
160    {"restore",  store_restore, ITEM(res_job),         JT_RESTORE, 0, 0},
161    {"schedule", store_res,    ITEM(res_job.schedule), R_SCHEDULE, 0, 0},
162    {"messages", store_res,    ITEM(res_job.messages), R_MSGS, 0, 0},
163    {"storage",  store_res,    ITEM(res_job.storage),  R_STORAGE, 0, 0},
164    {"pool",     store_res,    ITEM(res_job.pool),     R_POOL, 0, 0},
165    {"maxruntime", store_time, ITEM(res_job.MaxRunTime), 0, 0, 0},
166    {"maxstartdelay", store_time, ITEM(res_job.MaxStartDelay), 0, 0, 0},
167    {NULL, NULL, NULL, 0, 0, 0} 
168 };
169
170 /* FileSet resource
171  *
172  *   name          handler     value                 code flags    default_value
173  */
174 static struct res_items fs_items[] = {
175    {"name",        store_name, ITEM(res_fs.hdr.name), 0, ITEM_REQUIRED, 0},
176    {"description", store_str,  ITEM(res_fs.hdr.desc), 0, 0, 0},
177    {"include",     store_inc,  NULL,                  0, 0, 0},
178    {"exclude",     store_inc,  NULL,                  1, 0, 0},
179    {NULL,          NULL,       NULL,                  0, 0, 0} 
180 };
181
182 /* Schedule -- see run_conf.c */
183 /* Schedule
184  *
185  *   name          handler     value                 code flags    default_value
186  */
187 static struct res_items sch_items[] = {
188    {"name",     store_name,  ITEM(res_sch.hdr.name), 0, ITEM_REQUIRED, 0},
189    {"description", store_str, ITEM(res_sch.hdr.desc), 0, 0, 0},
190    {"run",      store_run,   ITEM(res_sch.run),      0, 0, 0},
191    {NULL, NULL, NULL, 0, 0, 0} 
192 };
193
194 /* Group resource -- not implemented
195  *
196  *   name          handler     value                 code flags    default_value
197  */
198 static struct res_items group_items[] = {
199    {"name",        store_name, ITEM(res_group.hdr.name), 0, ITEM_REQUIRED, 0},
200    {"description", store_str,  ITEM(res_group.hdr.desc), 0, 0, 0},
201    {NULL, NULL, NULL, 0, 0, 0} 
202 };
203
204 /* Pool resource
205  *
206  *   name             handler     value                        code flags default_value
207  */
208 static struct res_items pool_items[] = {
209    {"name",            store_name,    ITEM(res_pool.hdr.name),        0, ITEM_REQUIRED, 0},
210    {"description",     store_str,     ITEM(res_pool.hdr.desc),        0, 0,     0},
211    {"pooltype",        store_strname, ITEM(res_pool.pool_type),       0, ITEM_REQUIRED, 0},
212    {"labelformat",     store_strname, ITEM(res_pool.label_format),    0, 0,     0},
213    {"usecatalog",      store_yesno, ITEM(res_pool.use_catalog),     1, ITEM_DEFAULT,  1},
214    {"usevolumeonce",   store_yesno, ITEM(res_pool.use_volume_once), 1, 0,       0},
215    {"maximumvolumes",  store_pint,  ITEM(res_pool.max_volumes),     0, 0,             0},
216    {"acceptanyvolume", store_yesno, ITEM(res_pool.accept_any_volume), 1, 0,     0},
217    {"catalogfiles",    store_yesno, ITEM(res_pool.catalog_files),   1, ITEM_DEFAULT,  1},
218    {"volumeretention", store_time,  ITEM(res_pool.VolRetention), 0, ITEM_DEFAULT, 60*60*24*365},
219    {"autoprune",       store_yesno, ITEM(res_pool.AutoPrune), 1, ITEM_DEFAULT, 0},
220    {"recycle",         store_yesno, ITEM(res_pool.Recycle),     1, ITEM_DEFAULT, 0},
221    {NULL, NULL, NULL, 0, 0, 0} 
222 };
223
224 /* Message resource */
225 extern struct res_items msgs_items[];
226
227 /* 
228  * This is the master resource definition.  
229  * It must have one item for each of the resources.
230  *
231  *  name             items        rcode        res_head
232  */
233 struct s_res resources[] = {
234    {"director",      dir_items,   R_DIRECTOR,  NULL},
235    {"client",        cli_items,   R_CLIENT,    NULL},
236    {"job",           job_items,   R_JOB,       NULL},
237    {"storage",       store_items, R_STORAGE,   NULL},
238    {"catalog",       cat_items,   R_CATALOG,   NULL},
239    {"schedule",      sch_items,   R_SCHEDULE,  NULL},
240    {"fileset",       fs_items,    R_FILESET,   NULL},
241    {"group",         group_items, R_GROUP,     NULL},
242    {"pool",          pool_items,  R_POOL,      NULL},
243    {"messages",      msgs_items,  R_MSGS,      NULL},
244    {NULL,            NULL,        0,           NULL}
245 };
246
247
248 /* Keywords (RHS) permitted in Job Level records   
249  *
250  *   level_name      level              level_class
251  */
252 struct s_jl joblevels[] = {
253    {"Full",          L_FULL,            JT_BACKUP},
254    {"Incremental",   L_INCREMENTAL,     JT_BACKUP},
255    {"Differential",  L_DIFFERENTIAL,    JT_BACKUP},
256    {"Level",         L_LEVEL,           JT_BACKUP},
257    {"Since",         L_SINCE,           JT_BACKUP},
258    {"Catalog",       L_VERIFY_CATALOG,  JT_VERIFY},
259    {"Initcatalog",   L_VERIFY_INIT,     JT_VERIFY},
260    {"Volume",        L_VERIFY_VOLUME,   JT_VERIFY},
261    {"Data",          L_VERIFY_DATA,     JT_VERIFY},
262    {NULL,            0}
263 };
264
265 /* Keywords (RHS) permitted in Backup and Verify records */
266 static struct s_kw BakVerFields[] = {
267    {"client",        'C'},
268    {"fileset",       'F'},
269    {"level",         'L'}, 
270    {NULL,            0}
271 };
272
273 /* Keywords (RHS) permitted in Restore records */
274 static struct s_kw RestoreFields[] = {
275    {"client",        'C'},
276    {"fileset",       'F'},
277    {"jobid",         'J'},            /* JobId to restore */
278    {"where",         'W'},            /* root of restore */
279    {"replace",       'R'},            /* replacement options */
280    {NULL,              0}
281 };
282
283 /* Options permitted in Restore replace= */
284 static struct s_kw ReplaceOptions[] = {
285    {"always",         'A'},           /* always */
286    {"ifnewer",        'W'},
287    {"never",          'N'},
288    {NULL,               0}
289 };
290
291
292
293 /* Define FileSet KeyWord values */
294
295 #define FS_KW_NONE         0
296 #define FS_KW_COMPRESSION  1
297 #define FS_KW_SIGNATURE    2
298 #define FS_KW_ENCRYPTION   3
299 #define FS_KW_VERIFY       4
300
301 /* FileSet keywords */
302 static struct s_kw FS_option_kw[] = {
303    {"compression", FS_KW_COMPRESSION},
304    {"signature",   FS_KW_SIGNATURE},
305    {"encryption",  FS_KW_ENCRYPTION},
306    {"verify",      FS_KW_VERIFY},
307    {NULL,          0}
308 };
309
310 /* Options for FileSet keywords */
311
312 struct s_fs_opt {
313    char *name;
314    int keyword;
315    char option;
316 };
317
318 /* Options permitted for each keyword and resulting value */
319 static struct s_fs_opt FS_options[] = {
320    {"md5",      FS_KW_SIGNATURE,    'M'},
321    {"gzip",     FS_KW_COMPRESSION,  'Z'},
322    {"blowfish", FS_KW_ENCRYPTION,   'B'},   /* ***FIXME*** not implemented */
323    {"3des",     FS_KW_ENCRYPTION,   '3'},   /* ***FIXME*** not implemented */
324    {NULL,       0,                   0}
325 };
326
327 char *level_to_str(int level)
328 {
329    int i;
330    static char level_no[30];
331    char *str = level_no;
332
333    sprintf(level_no, "%d", level);    /* default if not found */
334    for (i=0; joblevels[i].level_name; i++) {
335       if (level == joblevels[i].level) {
336          str = joblevels[i].level_name;
337          break;
338       }
339    }
340    return str;
341 }
342
343
344
345 /* Dump contents of resource */
346 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
347 {
348    int i;
349    URES *res = (URES *)reshdr;
350    int recurse = 1;
351
352    if (res == NULL) {
353       sendit(sock, "No %s resource defined\n", res_to_str(type));
354       return;
355    }
356    if (type < 0) {                    /* no recursion */
357       type = - type;
358       recurse = 0;
359    }
360    switch (type) {
361       case R_DIRECTOR:
362          char ed1[30], ed2[30];
363          sendit(sock, "Director: name=%s maxjobs=%d FDtimeout=%s SDtimeout=%s\n", 
364             reshdr->name, res->res_dir.MaxConcurrentJobs, 
365             edit_uint64(res->res_dir.FDConnectTimeout, ed1),
366             edit_uint64(res->res_dir.SDConnectTimeout, ed2));
367          if (res->res_dir.query_file) {
368             sendit(sock, "   query_file=%s\n", res->res_dir.query_file);
369          }
370          if (res->res_dir.messages) {
371             sendit(sock, "  --> ");
372             dump_resource(-R_MSGS, (RES *)res->res_dir.messages, sendit, sock);
373          }
374          break;
375       case R_CLIENT:
376          sendit(sock, "Client: name=%s address=%s FDport=%d\n",
377             res->res_client.hdr.name, res->res_client.address, res->res_client.FDport);
378          sendit(sock, "JobRetention=%" lld " FileRetention=%" lld " AutoPrune=%d\n",
379             res->res_client.JobRetention, res->res_client.FileRetention,
380             res->res_client.AutoPrune);
381          if (res->res_client.catalog) {
382             sendit(sock, "  --> ");
383             dump_resource(-R_CATALOG, (RES *)res->res_client.catalog, sendit, sock);
384          }
385          break;
386       case R_STORAGE:
387          sendit(sock, "Storage: name=%s address=%s SDport=%d\n\
388          DeviceName=%s MediaType=%s\n",
389             res->res_store.hdr.name, res->res_store.address, res->res_store.SDport,
390             res->res_store.dev_name, res->res_store.media_type);
391          break;
392       case R_CATALOG:
393          sendit(sock, "Catalog: name=%s address=%s DBport=%d db_name=%s\n\
394          db_user=%s\n",
395             res->res_cat.hdr.name, res->res_cat.address, res->res_cat.DBport,
396             res->res_cat.db_name, res->res_cat.db_user);
397          break;
398       case R_JOB:
399          sendit(sock, "Job: name=%s JobType=%d level=%s\n", res->res_job.hdr.name, 
400             res->res_job.JobType, level_to_str(res->res_job.level));
401          if (res->res_job.client) {
402             sendit(sock, "  --> ");
403             dump_resource(-R_CLIENT, (RES *)res->res_job.client, sendit, sock);
404          }
405          if (res->res_job.fs) {
406             sendit(sock, "  --> ");
407             dump_resource(-R_FILESET, (RES *)res->res_job.fs, sendit, sock);
408          }
409          if (res->res_job.schedule) {
410             sendit(sock, "  --> ");
411             dump_resource(-R_SCHEDULE, (RES *)res->res_job.schedule, sendit, sock);
412          }
413          if (res->res_job.RestoreWhere) {
414             sendit(sock, "  --> Where=%s\n", res->res_job.RestoreWhere);
415          }
416          if (res->res_job.storage) {
417             sendit(sock, "  --> ");
418             dump_resource(-R_STORAGE, (RES *)res->res_job.storage, sendit, sock);
419          }
420          if (res->res_job.pool) {
421             sendit(sock, "  --> ");
422             dump_resource(-R_POOL, (RES *)res->res_job.pool, sendit, sock);
423          } else {
424             sendit(sock, "!!! No Pool resource\n");
425          }
426          if (res->res_job.messages) {
427             sendit(sock, "  --> ");
428             dump_resource(-R_MSGS, (RES *)res->res_job.messages, sendit, sock);
429          }
430          break;
431       case R_FILESET:
432          sendit(sock, "FileSet: name=%s\n", res->res_fs.hdr.name);
433          for (i=0; i<res->res_fs.num_includes; i++)
434             sendit(sock, "      Inc: %s\n", res->res_fs.include_array[i]);
435          for (i=0; i<res->res_fs.num_excludes; i++)
436             sendit(sock, "      Exc: %s\n", res->res_fs.exclude_array[i]);
437          break;
438       case R_SCHEDULE:
439          if (res->res_sch.run)
440             sendit(sock, "Schedule: name=%s Level=%s\n", res->res_sch.hdr.name,
441                level_to_str(res->res_sch.run->level));
442          else
443             sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
444          break;
445       case R_GROUP:
446          sendit(sock, "Group: name=%s\n", res->res_group.hdr.name);
447          break;
448       case R_POOL:
449          sendit(sock, "Pool: name=%s PoolType=%s\n", res->res_pool.hdr.name,
450                  res->res_pool.pool_type);
451          sendit(sock, "      use_cat=%d use_once=%d acpt_any=%d cat_files=%d\n",
452                  res->res_pool.use_catalog, res->res_pool.use_volume_once,
453                  res->res_pool.accept_any_volume, res->res_pool.catalog_files);
454          sendit(sock, "      max_vols=%d auto_prune=%d VolRetention=%" lld "\n",
455                  res->res_pool.max_volumes, res->res_pool.AutoPrune,
456                  res->res_pool.VolRetention);
457          sendit(sock, "      recycle=%d\n",  res->res_pool.Recycle);
458          
459
460          sendit(sock, "      LabelFormat=%s\n", res->res_pool.label_format?
461                  res->res_pool.label_format:"NONE");
462          break;
463       case R_MSGS:
464          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
465          if (res->res_msgs.mail_cmd) 
466             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
467          if (res->res_msgs.operator_cmd) 
468             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
469          break;
470       default:
471          sendit(sock, "Unknown resource type %d\n", type);
472          break;
473    }
474    if (recurse && res->res_dir.hdr.next) {
475       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
476    }
477 }
478
479 /* 
480  * Free memory of resource.  
481  * NB, we don't need to worry about freeing any references
482  * to other resources as they will be freed when that 
483  * resource chain is traversed.  Mainly we worry about freeing
484  * allocated strings (names).
485  */
486 void free_resource(int type)
487 {
488    int num;
489    URES *res;
490    RES *nres;
491    int rindex = type - r_first;
492
493    res = (URES *)resources[rindex].res_head;
494
495    if (res == NULL)
496       return;
497
498    /* common stuff -- free the resource name and description */
499    nres = (RES *)res->res_dir.hdr.next;
500    if (res->res_dir.hdr.name) {
501       free(res->res_dir.hdr.name);
502    }
503    if (res->res_dir.hdr.desc) {
504       free(res->res_dir.hdr.desc);
505    }
506
507    switch (type) {
508       case R_DIRECTOR:
509          if (res->res_dir.working_directory)
510             free(res->res_dir.working_directory);
511          if (res->res_dir.pid_directory)
512             free(res->res_dir.pid_directory);
513          if (res->res_dir.subsys_directory)
514             free(res->res_dir.subsys_directory);
515          if (res->res_dir.password)
516             free(res->res_dir.password);
517          if (res->res_dir.query_file)
518             free(res->res_dir.query_file);
519          break;
520       case R_CLIENT:
521          if (res->res_client.address)
522             free(res->res_client.address);
523          if (res->res_client.password)
524             free(res->res_client.password);
525          break;
526       case R_STORAGE:
527          if (res->res_store.address)
528             free(res->res_store.address);
529          if (res->res_store.password)
530             free(res->res_store.password);
531          if (res->res_store.media_type)
532             free(res->res_store.media_type);
533          if (res->res_store.dev_name)
534             free(res->res_store.dev_name);
535          break;
536       case R_CATALOG:
537          if (res->res_cat.address)
538             free(res->res_cat.address);
539          if (res->res_cat.db_user)
540             free(res->res_cat.db_user);
541          if (res->res_cat.db_name)
542             free(res->res_cat.db_name);
543          if (res->res_cat.db_password)
544             free(res->res_cat.db_password);
545          break;
546       case R_FILESET:
547          if ((num=res->res_fs.num_includes)) {
548             while (--num >= 0)    
549                free(res->res_fs.include_array[num]);
550             free(res->res_fs.include_array);
551          }
552          if ((num=res->res_fs.num_excludes)) {
553             while (--num >= 0)    
554                free(res->res_fs.exclude_array[num]);
555             free(res->res_fs.exclude_array);
556          }
557          break;
558       case R_POOL:
559          if (res->res_pool.pool_type) {
560             free(res->res_pool.pool_type);
561          }
562          if (res->res_pool.label_format) {
563             free(res->res_pool.label_format);
564          }
565          break;
566       case R_SCHEDULE:
567          if (res->res_sch.run) {
568             RUN *nrun, *next;
569             nrun = res->res_sch.run;
570             while (nrun) {
571                next = nrun->next;
572                free(nrun);
573                nrun = next;
574             }
575          }
576          break;
577       case R_JOB:
578          if (res->res_job.RestoreWhere) {
579             free(res->res_job.RestoreWhere);
580          }
581          break;
582       case R_MSGS:
583          if (res->res_msgs.mail_cmd)
584             free(res->res_msgs.mail_cmd);
585          if (res->res_msgs.operator_cmd)
586             free(res->res_msgs.operator_cmd);
587          break;
588       case R_GROUP:
589          break;
590       default:
591          printf("Unknown resource type %d\n", type);
592    }
593    /* Common stuff again -- free the resource, recurse to next one */
594    free(res);
595    resources[rindex].res_head = nres;
596    if (nres)
597       free_resource(type);
598 }
599
600 /* Save the new resource by chaining it into the head list for
601  * the resource. If this is pass 2, we update any resource
602  * pointers (currently only in the Job resource).
603  */
604 void save_resource(int type, struct res_items *items, int pass)
605 {
606    URES *res;
607    int rindex = type - r_first;
608    int i, size;
609    int error = 0;
610    
611    /* 
612     * Ensure that all required items are present
613     */
614    for (i=0; items[i].name; i++) {
615       if (items[i].flags & ITEM_REQUIRED) {
616             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
617                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
618                  items[i].name, resources[rindex]);
619              }
620       }
621       /* If this triggers, take a look at lib/parse_conf.h */
622       if (i >= MAX_RES_ITEMS) {
623          Emsg1(M_ABORT, 0, "Too many items in %s resource\n", resources[rindex]);
624       }
625    }
626
627    /* During pass 2, we looked up pointers to all the resources
628     * referrenced in the current resource, , now we
629     * must copy their address from the static record to the allocated
630     * record.
631     */
632    if (pass == 2) {
633       switch (type) {
634          /* Resources not containing a resource */
635          case R_CATALOG:
636          case R_STORAGE:
637          case R_FILESET:
638          case R_SCHEDULE:
639          case R_GROUP:
640          case R_POOL:
641          case R_MSGS:
642             break;
643
644          /* Resources containing another resource */
645          case R_DIRECTOR:
646             if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
647                Emsg1(M_ABORT, 0, "Cannot find Director resource %s\n", res_all.res_dir.hdr.name);
648             }
649             res->res_dir.messages = res_all.res_dir.messages;
650             break;
651          case R_JOB:
652             if ((res = (URES *)GetResWithName(R_JOB, res_all.res_dir.hdr.name)) == NULL) {
653                Emsg1(M_ABORT, 0, "Cannot find Job resource %s\n", res_all.res_dir.hdr.name);
654             }
655             res->res_job.messages = res_all.res_job.messages;
656             res->res_job.schedule = res_all.res_job.schedule;
657             res->res_job.client   = res_all.res_job.client;
658             res->res_job.fs       = res_all.res_job.fs;
659             res->res_job.storage  = res_all.res_job.storage;
660             res->res_job.pool     = res_all.res_job.pool;
661             break;
662          case R_CLIENT:
663             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_client.hdr.name)) == NULL) {
664                Emsg1(M_ABORT, 0, "Cannot find Client resource %s\n", res_all.res_client.hdr.name);
665             }
666             res->res_client.catalog = res_all.res_client.catalog;
667             break;
668          default:
669             Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
670             error = 1;
671             break;
672       }
673       /* Note, the resource name was already saved during pass 1,
674        * so here, we can just release it.
675        */
676       if (res_all.res_dir.hdr.name) {
677          free(res_all.res_dir.hdr.name);
678          res_all.res_dir.hdr.name = NULL;
679       }
680       return;
681    }
682
683    switch (type) {
684       case R_DIRECTOR:
685          size = sizeof(DIRRES);
686          break;
687       case R_CLIENT:
688          size =sizeof(CLIENT);
689          break;
690       case R_STORAGE:
691          size = sizeof(STORE); 
692          break;
693       case R_CATALOG:
694          size = sizeof(CAT);
695          break;
696       case R_JOB:
697          size = sizeof(JOB);
698          break;
699       case R_FILESET:
700          size = sizeof(FILESET);
701          break;
702       case R_SCHEDULE:
703          size = sizeof(SCHED);
704          break;
705       case R_GROUP:
706          size = sizeof(GROUP);
707          break;
708       case R_POOL:
709          size = sizeof(POOL);
710          break;
711       case R_MSGS:
712          size = sizeof(MSGS);
713          break;
714       default:
715          printf("Unknown resource type %d\n", type);
716          error = 1;
717          break;
718    }
719    /* Common */
720    if (!error) {
721       res = (URES *) malloc(size);
722       memcpy(res, &res_all, size);
723       res->res_dir.hdr.next = resources[rindex].res_head;
724       resources[rindex].res_head = (RES *)res;
725       Dmsg2(90, "dir_conf: inserting %s res: %s\n", res_to_str(type),
726          res->res_dir.hdr.name);
727    }
728
729 }
730
731 /* 
732  * Store backup/verify info for Job record 
733  *
734  * Note, this code is used for both BACKUP and VERIFY jobs
735  *
736  *    Backup = Client=<client-name> FileSet=<FileSet-name> Level=<level>
737  */
738 static void store_backup(LEX *lc, struct res_items *item, int index, int pass)
739 {
740    int token, i;
741    RES *res;
742    int options = lc->options;
743
744    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
745
746    
747    ((JOB *)(item->value))->JobType = item->code;
748    while ((token = lex_get_token(lc)) != T_EOL) {
749       int found;
750
751       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
752          scan_err1(lc, "Expected a backup/verify keyword, got: %s", lc->str);
753       } else {
754          lcase(lc->str);
755          Dmsg1(190, "Got keyword: %s\n", lc->str);
756          found = FALSE;
757          for (i=0; BakVerFields[i].name; i++) {
758             if (strcasecmp(lc->str, BakVerFields[i].name) == 0) {
759                found = TRUE;
760                if (lex_get_token(lc) != T_EQUALS) {
761                   scan_err1(lc, "Expected an equals, got: %s", lc->str);
762                }
763                token = lex_get_token(lc);
764                if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
765                   scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
766                }
767                Dmsg1(190, "Got value: %s\n", lc->str);
768                switch (BakVerFields[i].token) {
769                   case 'C':
770                      /* Find Client Resource */
771                      if (pass == 2) {
772                         res = GetResWithName(R_CLIENT, lc->str);
773                         if (res == NULL) {
774                            scan_err1(lc, "Could not find specified Client Resource: %s",
775                                       lc->str);
776                         }
777                         res_all.res_job.client = (CLIENT *)res;
778                      }
779                      break;
780                   case 'F':
781                      /* Find FileSet Resource */
782                      if (pass == 2) {
783                         res = GetResWithName(R_FILESET, lc->str);
784                         if (res == NULL) {
785                            scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
786                                        lc->str);
787                         }
788                         res_all.res_job.fs = (FILESET *)res;
789                      }
790                      break;
791                   case 'L':
792                      /* Get level */
793                      lcase(lc->str);
794                      for (i=0; joblevels[i].level_name; i++) {
795                         if (joblevels[i].job_class == item->code && 
796                              strcasecmp(lc->str, joblevels[i].level_name) == 0) {
797                            ((JOB *)(item->value))->level = joblevels[i].level;
798                            i = 0;
799                            break;
800                         }
801                      }
802                      if (i != 0) {
803                         scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
804                      }
805                      break;
806                } /* end switch */
807                break;
808             } /* end if strcmp() */
809          } /* end for */
810          if (!found) {
811             scan_err1(lc, "%s not a valid Backup/verify keyword", lc->str);
812          }
813       }
814    } /* end while */
815    lc->options = options;             /* reset original options */
816    set_bit(index, res_all.hdr.item_present);
817 }
818
819 /* 
820  * Store restore info for Job record 
821  *
822  *    Restore = JobId=<job-id> Where=<root-directory> Replace=<options>
823  *
824  */
825 static void store_restore(LEX *lc, struct res_items *item, int index, int pass)
826 {
827    int token, i;
828    RES *res;
829    int options = lc->options;
830
831    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
832
833    Dmsg0(190, "Enter store_restore()\n");
834    
835    ((JOB *)(item->value))->JobType = item->code;
836    while ((token = lex_get_token(lc)) != T_EOL) {
837       int found; 
838
839       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
840          scan_err1(lc, "Expected a Restore keyword, got: %s", lc->str);
841       } else {
842          lcase(lc->str);
843          found = FALSE;
844          for (i=0; RestoreFields[i].name; i++) {
845             Dmsg1(190, "Restore kw=%s\n", lc->str);
846             if (strcmp(lc->str, RestoreFields[i].name) == 0) {
847                found = TRUE;
848                if (lex_get_token(lc) != T_EQUALS) {
849                   scan_err1(lc, "Expected an equals, got: %s", lc->str);
850                }
851                token = lex_get_token(lc);
852                Dmsg1(190, "Restore value=%s\n", lc->str);
853                switch (RestoreFields[i].token) {
854                   case 'C':
855                      /* Find Client Resource */
856                      if (pass == 2) {
857                         res = GetResWithName(R_CLIENT, lc->str);
858                         if (res == NULL) {
859                            scan_err1(lc, "Could not find specified Client Resource: %s",
860                                       lc->str);
861                         }
862                         res_all.res_job.client = (CLIENT *)res;
863                      }
864                      break;
865                   case 'F':
866                      /* Find FileSet Resource */
867                      if (pass == 2) {
868                         res = GetResWithName(R_FILESET, lc->str);
869                         if (res == NULL) {
870                            scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
871                                        lc->str);
872                         }
873                         res_all.res_job.fs = (FILESET *)res;
874                      }
875                      break;
876                   case 'J':
877                      /* JobId */
878                      if (token != T_NUMBER) {
879                         scan_err1(lc, "expected an integer number, got: %s", lc->str);
880                      }
881                      errno = 0;
882                      res_all.res_job.RestoreJobId = strtol(lc->str, NULL, 0);
883                      Dmsg1(190, "RestorJobId=%d\n", res_all.res_job.RestoreJobId);
884                      if (errno != 0) {
885                         scan_err1(lc, "expected an integer number, got: %s", lc->str);
886                      }
887                      break;
888                   case 'W':
889                      /* Where */
890                      if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
891                         scan_err1(lc, "Expected a Restore root directory, got: %s", lc->str);
892                      }
893                      if (pass == 1) {
894                         res_all.res_job.RestoreWhere = bstrdup(lc->str);
895                      }
896                      break;
897                   case 'R':
898                      /* Replacement options */
899                      if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
900                         scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
901                      }
902                      lcase(lc->str);
903                      /* Fix to scan Replacement options */
904                      for (i=0; ReplaceOptions[i].name; i++) {
905                         if (strcmp(lc->str, ReplaceOptions[i].name) == 0) {
906                             ((JOB *)(item->value))->RestoreOptions = ReplaceOptions[i].token;
907                            i = 0;
908                            break;
909                         }
910                      }
911                      if (i != 0) {
912                         scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
913                      }
914                      break;
915                } /* end switch */
916                break;
917             } /* end if strcmp() */
918          } /* end for */
919          if (!found) {
920             scan_err1(lc, "%s not a valid Restore keyword", lc->str);
921          }
922       }
923    } /* end while */
924    lc->options = options;             /* reset original options */
925    set_bit(index, res_all.hdr.item_present);
926 }
927
928
929
930 /* 
931  * Scan for FileSet options
932  */
933 static char *scan_fs_options(LEX *lc, int keyword)
934 {
935    int token, i;
936    static char opts[100];
937    char option[2];
938
939    option[0] = 0;                     /* default option = none */
940    opts[0] = option[1] = 0;           /* terminate options */
941    for (;;) {
942       token = lex_get_token(lc);             /* expect at least one option */       
943       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
944          scan_err1(lc, "expected a FileSet option, got: %s", lc->str);
945       }
946       lcase(lc->str);
947       if (keyword == FS_KW_VERIFY) { /* special case */
948          /* ***FIXME**** ensure these are in permitted set */
949          strcpy(option, "V");         /* indicate Verify */
950          strcat(option, lc->str);
951          strcat(option, ":");         /* terminate it */
952       } else {
953          for (i=0; FS_options[i].name; i++) {
954             if (strcmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
955                option[0] = FS_options[i].option;
956                i = 0;
957                break;
958             }
959          }
960          if (i != 0) {
961             scan_err1(lc, "Expected a FileSet option keyword, got: %s", lc->str);
962          }
963       }
964       strcat(opts, option);
965
966       /* check if more options are specified */
967       if (lc->ch != ',') {
968          break;                       /* no, get out */
969       }
970       token = lex_get_token(lc);      /* yes, eat comma */
971    }
972
973    return opts;
974 }
975
976
977 /* Store FileSet Include/Exclude info */
978 static void store_inc(LEX *lc, struct res_items *item, int index, int pass)
979 {
980    int token, i;
981    int options = lc->options;
982    int keyword;
983    char *fname;
984    char inc_opts[100];
985    int inc_opts_len;
986
987    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
988
989    /* Get include options */
990    strcpy(inc_opts, "0");             /* set no options */
991    while ((token=lex_get_token(lc)) != T_BOB) {
992       if (token != T_STRING) {
993          scan_err1(lc, "expected a FileSet option keyword, got: %s", lc->str);
994       } else {
995          keyword = FS_KW_NONE;
996          lcase(lc->str);
997          for (i=0; FS_option_kw[i].name; i++) {
998             if (strcmp(lc->str, FS_option_kw[i].name) == 0) {
999                keyword = FS_option_kw[i].token;
1000                break;
1001             }
1002          }
1003          if (keyword == FS_KW_NONE) {
1004             scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
1005          }
1006       }
1007       /* Option keyword should be following by = <option> */
1008       if ((token=lex_get_token(lc)) != T_EQUALS) {
1009          scan_err1(lc, "expected an = following keyword, got: %s", lc->str);
1010       }
1011       strcat(inc_opts, scan_fs_options(lc, keyword));
1012       if (token == T_BOB) {
1013          break;
1014       }
1015    }
1016    strcat(inc_opts, " ");             /* add field separator */
1017    inc_opts_len = strlen(inc_opts);
1018
1019
1020    if (pass == 1) {
1021       if (!res_all.res_fs.have_MD5) {
1022          MD5Init(&res_all.res_fs.md5c);
1023          res_all.res_fs.have_MD5 = TRUE;
1024       }
1025       /* Pickup include/exclude names. Note, they are stored as
1026        * XYZ fname
1027        * where XYZ are the include/exclude options for the FileSet
1028        *     a "0 " (zero) indicates no options,
1029        * and fname is the file/directory name given
1030        */
1031       while ((token = lex_get_token(lc)) != T_EOB) {
1032          switch (token) {
1033             case T_COMMA:
1034             case T_EOL:
1035                continue;
1036
1037             case T_IDENTIFIER:
1038             case T_STRING:
1039             case T_QUOTED_STRING:
1040                fname = (char *) malloc(lc->str_len + inc_opts_len + 1);
1041                strcpy(fname, inc_opts);
1042                strcat(fname, lc->str);
1043                if (res_all.res_fs.have_MD5) {
1044                   MD5Update(&res_all.res_fs.md5c, (unsigned char *) fname, inc_opts_len + lc->str_len);
1045                }
1046                if (item->code == 0) { /* include */
1047                   if (res_all.res_fs.num_includes == res_all.res_fs.include_size) {
1048                      res_all.res_fs.include_size += 10;
1049                      if (res_all.res_fs.include_array == NULL) {
1050                         res_all.res_fs.include_array = (char **) malloc(sizeof(char *) * res_all.res_fs.include_size);
1051                      } else {
1052                         res_all.res_fs.include_array = (char **) realloc(res_all.res_fs.include_array,
1053                            sizeof(char *) * res_all.res_fs.include_size);
1054                      }
1055                   }
1056                   res_all.res_fs.include_array[res_all.res_fs.num_includes++] =    
1057                      fname;
1058                } else {                /* exclude */
1059                   if (res_all.res_fs.num_excludes == res_all.res_fs.exclude_size) {
1060                      res_all.res_fs.exclude_size += 10;
1061                      if (res_all.res_fs.exclude_array == NULL) {
1062                         res_all.res_fs.exclude_array = (char **) malloc(sizeof(char *) * res_all.res_fs.exclude_size);
1063                      } else {
1064                         res_all.res_fs.exclude_array = (char **) realloc(res_all.res_fs.exclude_array,
1065                            sizeof(char *) * res_all.res_fs.exclude_size);
1066                      }
1067                   }
1068                   res_all.res_fs.exclude_array[res_all.res_fs.num_excludes++] =    
1069                      fname;
1070                }
1071                break;
1072             default:
1073                scan_err1(lc, "Expected a filename, got: %s", lc->str);
1074          }                                 
1075       }
1076    } else { /* pass 2 */
1077       while (lex_get_token(lc) != T_EOB) 
1078          {}
1079    }
1080    scan_to_eol(lc);
1081    lc->options = options;
1082    set_bit(index, res_all.hdr.item_present);
1083 }