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