]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird_conf.c
Initial revision
[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
96    {NULL, NULL, NULL, 0, 0, 0}
97 };
98
99 /* 
100  *    Client or File daemon resource
101  *
102  *   name          handler     value                 code flags    default_value
103  */
104
105 static struct res_items cli_items[] = {
106    {"name",     store_name,       ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
107    {"description", store_str,     ITEM(res_client.hdr.desc), 0, 0, 0},
108    {"address",  store_str,        ITEM(res_client.address),  0, ITEM_REQUIRED, 0},
109    {"fdport",   store_pint,       ITEM(res_client.FDport),   0, ITEM_REQUIRED, 0},
110    {"password", store_password,   ITEM(res_client.password), 0, ITEM_REQUIRED, 0},
111    {"catalog",  store_res,        ITEM(res_client.catalog),  R_CATALOG, 0, 0},
112    {"catalogretentionperiod", store_time,      
113                          ITEM(res_client.cat_ret_period), 0, ITEM_DEFAULT, 60},
114    {"mediaretentionperiod", store_time,      
115                          ITEM(res_client.media_ret_period), 0, ITEM_DEFAULT, 60},
116    {NULL, NULL, NULL, 0, 0, 0} 
117 };
118
119 /* Storage daemon resource
120  *
121  *   name          handler     value                 code flags    default_value
122  */
123 static struct res_items store_items[] = {
124    {"name",      store_name,     ITEM(res_store.hdr.name),   0, ITEM_REQUIRED, 0},
125    {"description", store_str,    ITEM(res_store.hdr.desc),   0, 0, 0},
126    {"sdport",    store_pint,     ITEM(res_store.SDport),     0, ITEM_REQUIRED, 0},
127    {"sddport",   store_pint,     ITEM(res_store.SDDport),    0, 0, 0}, /* deprecated */
128    {"address",   store_str,      ITEM(res_store.address),    0, ITEM_REQUIRED, 0},
129    {"password",  store_password, ITEM(res_store.password),   0, ITEM_REQUIRED, 0},
130    {"device",    store_strname,  ITEM(res_store.dev_name),   0, ITEM_REQUIRED, 0},
131    {"mediatype", store_strname,  ITEM(res_store.media_type), 0, ITEM_REQUIRED, 0},
132    {NULL, NULL, NULL, 0, 0, 0} 
133 };
134
135 /* 
136  *    Catalog Resource Directives
137  *
138  *   name          handler     value                 code flags    default_value
139  */
140 static struct res_items cat_items[] = {
141    {"name",     store_name,     ITEM(res_cat.hdr.name),    0, ITEM_REQUIRED, 0},
142    {"description", store_str,   ITEM(res_cat.hdr.desc),    0, 0, 0},
143    {"address",  store_str,      ITEM(res_cat.address),     0, 0, 0},
144    {"dbport",   store_pint,     ITEM(res_cat.DBport),      0, 0, 0},
145    /* keep this password as store_str for the moment */
146    {"password", store_str,      ITEM(res_cat.db_password), 0, 0, 0},
147    {"user",     store_str,      ITEM(res_cat.db_user),     0, 0, 0},
148    {"dbname",   store_str,      ITEM(res_cat.db_name),     0, ITEM_REQUIRED, 0},
149    {NULL, NULL, NULL, 0, 0, 0} 
150 };
151
152 /* 
153  *    Job Resource Directives
154  *
155  *   name          handler     value                 code flags    default_value
156  */
157 static struct res_items job_items[] = {
158    {"name",     store_name,   ITEM(res_job.hdr.name), 0, ITEM_REQUIRED, 0},
159    {"description", store_str, ITEM(res_job.hdr.desc), 0, 0, 0},
160    {"backup",   store_backup, ITEM(res_job),          JT_BACKUP, 0, 0},
161    {"verify",   store_backup, ITEM(res_job),          JT_VERIFY, 0, 0},
162    {"restore",  store_restore, ITEM(res_job),         JT_RESTORE, 0, 0},
163    {"schedule", store_res,    ITEM(res_job.schedule), R_SCHEDULE, 0, 0},
164    {"messages", store_res,    ITEM(res_job.messages), R_MSGS, 0, 0},
165    {"storage",  store_res,    ITEM(res_job.storage),  R_STORAGE, 0, 0},
166    {"pool",     store_res,    ITEM(res_job.pool),     R_POOL, 0, 0},
167    {"maxruntime", store_time, ITEM(res_job.MaxRunTime), 0, 0, 0},
168    {"maxstartdelay", store_time, ITEM(res_job.MaxStartDelay), 0, 0, 0},
169    {NULL, NULL, NULL, 0, 0, 0} 
170 };
171
172 /* FileSet resource
173  *
174  *   name          handler     value                 code flags    default_value
175  */
176 static struct res_items fs_items[] = {
177    {"name",        store_name, ITEM(res_fs.hdr.name), 0, ITEM_REQUIRED, 0},
178    {"description", store_str,  ITEM(res_fs.hdr.desc), 0, 0, 0},
179    {"include",     store_inc,  NULL,                  0, 0, 0},
180    {"exclude",     store_inc,  NULL,                  1, 0, 0},
181    {NULL,          NULL,       NULL,                  0, 0, 0} 
182 };
183
184 /* Schedule -- see run_conf.c */
185 /* Schedule
186  *
187  *   name          handler     value                 code flags    default_value
188  */
189 static struct res_items sch_items[] = {
190    {"name",     store_name,  ITEM(res_sch.hdr.name), 0, ITEM_REQUIRED, 0},
191    {"description", store_str, ITEM(res_sch.hdr.desc), 0, 0, 0},
192    {"run",      store_run,   ITEM(res_sch.run),      0, 0, 0},
193    {NULL, NULL, NULL, 0, 0, 0} 
194 };
195
196 /* Group resource -- not implemented
197  *
198  *   name          handler     value                 code flags    default_value
199  */
200 static struct res_items group_items[] = {
201    {"name",        store_name, ITEM(res_group.hdr.name), 0, ITEM_REQUIRED, 0},
202    {"description", store_str,  ITEM(res_group.hdr.desc), 0, 0, 0},
203    {NULL, NULL, NULL, 0, 0, 0} 
204 };
205
206 /* Pool resource
207  *
208  *   name             handler     value                        code flags default_value
209  */
210 static struct res_items pool_items[] = {
211    {"name",            store_name,    ITEM(res_pool.hdr.name),        0, ITEM_REQUIRED, 0},
212    {"description",     store_str,     ITEM(res_pool.hdr.desc),        0, 0,             0},
213    {"pooltype",        store_strname, ITEM(res_pool.pool_type),       0, ITEM_REQUIRED, 0},
214    {"labelformat",     store_strname, ITEM(res_pool.label_format),    0, 0,             0},
215    {"usecatalog",      store_yesno, ITEM(res_pool.use_catalog),     1, ITEM_DEFAULT,  1},
216    {"usevolumeonce",   store_yesno, ITEM(res_pool.use_volume_once), 1, 0,             0},
217    {"maximumvolumes",  store_pint,  ITEM(res_pool.max_volumes),     0, 0,             0},
218    {"acceptanyvolume", store_yesno, ITEM(res_pool.accept_any_volume), 1, 0,           0},
219    {"catalogfiles",    store_yesno, ITEM(res_pool.catalog_files),   1, 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          sendit(sock, "Director: name=%s maxjobs=%d FDtimeout=%d SDtimeout=%d\n", 
362             reshdr->name, res->res_dir.MaxConcurrentJobs, 
363             res->res_dir.FDConnectTimeout,
364             res->res_dir.SDConnectTimeout);
365          if (res->res_dir.query_file) {
366             sendit(sock, "   query_file=%s\n", res->res_dir.query_file);
367          }
368          if (res->res_dir.messages) {
369             sendit(sock, "  --> ");
370             dump_resource(-R_MSGS, (RES *)res->res_dir.messages, sendit, sock);
371          }
372          break;
373       case R_CLIENT:
374          sendit(sock, "Client: name=%s address=%s FDport=%d\n",
375             res->res_client.hdr.name, res->res_client.address, res->res_client.FDport);
376          sendit(sock, "CatRetPeriod=%d MediaRetPeriod=%d\n",
377             res->res_client.cat_ret_period, res->res_client.media_ret_period);
378          if (res->res_client.catalog) {
379             sendit(sock, "  --> ");
380             dump_resource(-R_CATALOG, (RES *)res->res_client.catalog, sendit, sock);
381          }
382          break;
383       case R_STORAGE:
384          sendit(sock, "Storage: name=%s address=%s SDport=%d\n\
385          DeviceName=%s MediaType=%s\n",
386             res->res_store.hdr.name, res->res_store.address, res->res_store.SDport,
387             res->res_store.dev_name, res->res_store.media_type);
388          break;
389       case R_CATALOG:
390          sendit(sock, "Catalog: name=%s address=%s DBport=%d db_name=%s\n\
391          db_user=%s\n",
392             res->res_cat.hdr.name, res->res_cat.address, res->res_cat.DBport,
393             res->res_cat.db_name, res->res_cat.db_user);
394          break;
395       case R_JOB:
396          sendit(sock, "Job: name=%s JobType=%d level=%s\n", res->res_job.hdr.name, 
397             res->res_job.JobType, level_to_str(res->res_job.level));
398          if (res->res_job.client) {
399             sendit(sock, "  --> ");
400             dump_resource(-R_CLIENT, (RES *)res->res_job.client, sendit, sock);
401          }
402          if (res->res_job.fs) {
403             sendit(sock, "  --> ");
404             dump_resource(-R_FILESET, (RES *)res->res_job.fs, sendit, sock);
405          }
406          if (res->res_job.schedule) {
407             sendit(sock, "  --> ");
408             dump_resource(-R_SCHEDULE, (RES *)res->res_job.schedule, sendit, sock);
409          }
410          if (res->res_job.RestoreWhere) {
411             sendit(sock, "  --> Where=%s\n", res->res_job.RestoreWhere);
412          }
413          if (res->res_job.storage) {
414             sendit(sock, "  --> ");
415             dump_resource(-R_STORAGE, (RES *)res->res_job.storage, sendit, sock);
416          }
417          if (res->res_job.pool) {
418             sendit(sock, "  --> ");
419             dump_resource(-R_POOL, (RES *)res->res_job.pool, sendit, sock);
420          } else {
421             sendit(sock, "!!! No Pool resource\n");
422          }
423          if (res->res_job.messages) {
424             sendit(sock, "  --> ");
425             dump_resource(-R_MSGS, (RES *)res->res_job.messages, sendit, sock);
426          }
427          break;
428       case R_FILESET:
429          sendit(sock, "FileSet: name=%s\n", res->res_fs.hdr.name);
430          for (i=0; i<res->res_fs.num_includes; i++)
431             sendit(sock, "      Inc: %s\n", res->res_fs.include_array[i]);
432          for (i=0; i<res->res_fs.num_excludes; i++)
433             sendit(sock, "      Exc: %s\n", res->res_fs.exclude_array[i]);
434          break;
435       case R_SCHEDULE:
436          if (res->res_sch.run)
437             sendit(sock, "Schedule: name=%s Level=%s\n", res->res_sch.hdr.name,
438                level_to_str(res->res_sch.run->level));
439          else
440             sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
441          break;
442       case R_GROUP:
443          sendit(sock, "Group: name=%s\n", res->res_group.hdr.name);
444          break;
445       case R_POOL:
446          sendit(sock, "Pool: name=%s PoolType=%s\n", res->res_pool.hdr.name,
447                  res->res_pool.pool_type);
448          sendit(sock, "      use_cat=%d use_once=%d acpt_any=%d\n",
449                  res->res_pool.use_catalog, res->res_pool.use_volume_once,
450                  res->res_pool.accept_any_volume);
451          sendit(sock, "      cat_files=%d max_vols=%d\n",
452                  res->res_pool.catalog_files, res->res_pool.max_volumes);
453          sendit(sock, "      LabelFormat=%s\n", res->res_pool.label_format?
454                  res->res_pool.label_format:"NONE");
455          break;
456       case R_MSGS:
457          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
458          if (res->res_msgs.mail_cmd) 
459             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
460          if (res->res_msgs.operator_cmd) 
461             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
462          break;
463       default:
464          sendit(sock, "Unknown resource type %d\n", type);
465          break;
466    }
467    if (recurse && res->res_dir.hdr.next) {
468       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
469    }
470 }
471
472 /* 
473  * Free memory of resource.  
474  * NB, we don't need to worry about freeing any references
475  * to other resources as they will be freed when that 
476  * resource chain is traversed.  Mainly we worry about freeing
477  * allocated strings (names).
478  */
479 void free_resource(int type)
480 {
481    int num;
482    URES *res;
483    RES *nres;
484    int rindex = type - r_first;
485
486    res = (URES *)resources[rindex].res_head;
487
488    if (res == NULL)
489       return;
490
491    /* common stuff -- free the resource name and description */
492    nres = (RES *)res->res_dir.hdr.next;
493    if (res->res_dir.hdr.name) {
494       free(res->res_dir.hdr.name);
495    }
496    if (res->res_dir.hdr.desc) {
497       free(res->res_dir.hdr.desc);
498    }
499
500    switch (type) {
501       case R_DIRECTOR:
502          if (res->res_dir.working_directory)
503             free(res->res_dir.working_directory);
504          if (res->res_dir.pid_directory)
505             free(res->res_dir.pid_directory);
506          if (res->res_dir.subsys_directory)
507             free(res->res_dir.subsys_directory);
508          if (res->res_dir.password)
509             free(res->res_dir.password);
510          if (res->res_dir.query_file)
511             free(res->res_dir.query_file);
512          break;
513       case R_CLIENT:
514          if (res->res_client.address)
515             free(res->res_client.address);
516          if (res->res_client.password)
517             free(res->res_client.password);
518          break;
519       case R_STORAGE:
520          if (res->res_store.address)
521             free(res->res_store.address);
522          if (res->res_store.password)
523             free(res->res_store.password);
524          if (res->res_store.media_type)
525             free(res->res_store.media_type);
526          if (res->res_store.dev_name)
527             free(res->res_store.dev_name);
528          break;
529       case R_CATALOG:
530          if (res->res_cat.address)
531             free(res->res_cat.address);
532          if (res->res_cat.db_user)
533             free(res->res_cat.db_user);
534          if (res->res_cat.db_name)
535             free(res->res_cat.db_name);
536          if (res->res_cat.db_password)
537             free(res->res_cat.db_password);
538          break;
539       case R_FILESET:
540          if ((num=res->res_fs.num_includes)) {
541             while (--num >= 0)    
542                free(res->res_fs.include_array[num]);
543             free(res->res_fs.include_array);
544          }
545          if ((num=res->res_fs.num_excludes)) {
546             while (--num >= 0)    
547                free(res->res_fs.exclude_array[num]);
548             free(res->res_fs.exclude_array);
549          }
550          break;
551       case R_POOL:
552          if (res->res_pool.pool_type) {
553             free(res->res_pool.pool_type);
554          }
555          if (res->res_pool.label_format) {
556             free(res->res_pool.label_format);
557          }
558          break;
559       case R_SCHEDULE:
560          if (res->res_sch.run) {
561             RUN *nrun, *next;
562             nrun = res->res_sch.run;
563             while (nrun) {
564                next = nrun->next;
565                free(nrun);
566                nrun = next;
567             }
568          }
569          break;
570       case R_JOB:
571          if (res->res_job.RestoreWhere) {
572             free(res->res_job.RestoreWhere);
573          }
574          break;
575       case R_MSGS:
576          if (res->res_msgs.mail_cmd)
577             free(res->res_msgs.mail_cmd);
578          if (res->res_msgs.operator_cmd)
579             free(res->res_msgs.operator_cmd);
580
581          break;
582       case R_GROUP:
583          break;
584       default:
585          printf("Unknown resource type %d\n", type);
586    }
587    /* Common stuff again -- free the resource, recurse to next one */
588    free(res);
589    resources[rindex].res_head = nres;
590    if (nres)
591       free_resource(type);
592 }
593
594 /* Save the new resource by chaining it into the head list for
595  * the resource. If this is pass 2, we update any resource
596  * pointers (currently only in the Job resource).
597  */
598 void save_resource(int type, struct res_items *items, int pass)
599 {
600    URES *res;
601    int rindex = type - r_first;
602    int i, size;
603    int error = 0;
604    
605    /* 
606     * Ensure that all required items are present
607     */
608    for (i=0; items[i].name; i++) {
609       if (items[i].flags & ITEM_REQUIRED) {
610             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
611                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
612                  items[i].name, resources[rindex]);
613              }
614       }
615       /* If this triggers, take a look at lib/parse_conf.h */
616       if (i >= MAX_RES_ITEMS) {
617          Emsg1(M_ABORT, 0, "Too many items in %s resource\n", resources[rindex]);
618       }
619    }
620
621    /* During pass 2, we looked up pointers to all the resources
622     * referrenced in the current resource, , now we
623     * must copy their address from the static record to the allocated
624     * record.
625     */
626    if (pass == 2) {
627       switch (type) {
628          /* Resources not containing a resource */
629          case R_CATALOG:
630          case R_STORAGE:
631          case R_FILESET:
632          case R_SCHEDULE:
633          case R_GROUP:
634          case R_POOL:
635          case R_MSGS:
636             break;
637
638          /* Resources containing another resource */
639          case R_DIRECTOR:
640             if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
641                Emsg1(M_ABORT, 0, "Cannot find Director resource %s\n", res_all.res_dir.hdr.name);
642             }
643             res->res_dir.messages = res_all.res_dir.messages;
644             break;
645          case R_JOB:
646             if ((res = (URES *)GetResWithName(R_JOB, res_all.res_dir.hdr.name)) == NULL) {
647                Emsg1(M_ABORT, 0, "Cannot find Job resource %s\n", res_all.res_dir.hdr.name);
648             }
649             res->res_job.messages = res_all.res_job.messages;
650             res->res_job.schedule = res_all.res_job.schedule;
651             res->res_job.client   = res_all.res_job.client;
652             res->res_job.fs       = res_all.res_job.fs;
653             res->res_job.storage  = res_all.res_job.storage;
654             res->res_job.pool     = res_all.res_job.pool;
655             break;
656          case R_CLIENT:
657             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_client.hdr.name)) == NULL) {
658                Emsg1(M_ABORT, 0, "Cannot find Client resource %s\n", res_all.res_client.hdr.name);
659             }
660             res->res_client.catalog = res_all.res_client.catalog;
661             break;
662          default:
663             Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
664             error = 1;
665             break;
666       }
667       /* Note, the resource name was already saved during pass 1,
668        * so here, we can just release it.
669        */
670       if (res_all.res_dir.hdr.name) {
671          free(res_all.res_dir.hdr.name);
672          res_all.res_dir.hdr.name = NULL;
673       }
674       return;
675    }
676
677    switch (type) {
678       case R_DIRECTOR:
679          size = sizeof(DIRRES);
680          break;
681       case R_CLIENT:
682          size =sizeof(CLIENT);
683          break;
684       case R_STORAGE:
685          size = sizeof(STORE); 
686          break;
687       case R_CATALOG:
688          size = sizeof(CAT);
689          break;
690       case R_JOB:
691          size = sizeof(JOB);
692          break;
693       case R_FILESET:
694          size = sizeof(FILESET);
695          break;
696       case R_SCHEDULE:
697          size = sizeof(SCHED);
698          break;
699       case R_GROUP:
700          size = sizeof(GROUP);
701          break;
702       case R_POOL:
703          size = sizeof(POOL);
704          break;
705       case R_MSGS:
706          size = sizeof(MSGS);
707          break;
708       default:
709          printf("Unknown resource type %d\n", type);
710          error = 1;
711          break;
712    }
713    /* Common */
714    if (!error) {
715       res = (URES *) malloc(size);
716       memcpy(res, &res_all, size);
717       res->res_dir.hdr.next = resources[rindex].res_head;
718       resources[rindex].res_head = (RES *)res;
719       Dmsg2(90, "dir_conf: inserting %s res: %s\n", res_to_str(type),
720          res->res_dir.hdr.name);
721    }
722
723 }
724
725 /* 
726  * Store backup/verify info for Job record 
727  *
728  * Note, this code is used for both BACKUP and VERIFY jobs
729  *
730  *    Backup = Client=<client-name> FileSet=<FileSet-name> Level=<level>
731  */
732 static void store_backup(LEX *lc, struct res_items *item, int index, int pass)
733 {
734    int token, i;
735    RES *res;
736    int options = lc->options;
737
738    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
739
740    
741    ((JOB *)(item->value))->JobType = item->code;
742    while ((token = lex_get_token(lc)) != T_EOL) {
743       int found;
744
745       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
746          scan_err1(lc, "Expected a backup/verify keyword, got: %s", lc->str);
747       } else {
748          lcase(lc->str);
749          Dmsg1(190, "Got keyword: %s\n", lc->str);
750          found = FALSE;
751          for (i=0; BakVerFields[i].name; i++) {
752             if (strcmp(lc->str, BakVerFields[i].name) == 0) {
753                found = TRUE;
754                if (lex_get_token(lc) != T_EQUALS) {
755                   scan_err1(lc, "Expected an equals, got: %s", lc->str);
756                }
757                token = lex_get_token(lc);
758                if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
759                   scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
760                }
761                Dmsg1(190, "Got value: %s\n", lc->str);
762                switch (BakVerFields[i].token) {
763                   case 'C':
764                      /* Find Client Resource */
765                      if (pass == 2) {
766                         res = GetResWithName(R_CLIENT, lc->str);
767                         if (res == NULL) {
768                            scan_err1(lc, "Could not find specified Client Resource: %s",
769                                       lc->str);
770                         }
771                         res_all.res_job.client = (CLIENT *)res;
772                      }
773                      break;
774                   case 'F':
775                      /* Find FileSet Resource */
776                      if (pass == 2) {
777                         res = GetResWithName(R_FILESET, lc->str);
778                         if (res == NULL) {
779                            scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
780                                        lc->str);
781                         }
782                         res_all.res_job.fs = (FILESET *)res;
783                      }
784                      break;
785                   case 'L':
786                      /* Get level */
787                      lcase(lc->str);
788                      for (i=0; joblevels[i].level_name; i++) {
789                         if (joblevels[i].job_class == item->code && 
790                              strcmp(lc->str, joblevels[i].level_name) == 0) {
791                            ((JOB *)(item->value))->level = joblevels[i].level;
792                            i = 0;
793                            break;
794                         }
795                      }
796                      if (i != 0) {
797                         scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
798                      }
799                      break;
800                } /* end switch */
801                break;
802             } /* end if strcmp() */
803          } /* end for */
804          if (!found) {
805             scan_err1(lc, "%s not a valid Backup/verify keyword", lc->str);
806          }
807       }
808    } /* end while */
809    lc->options = options;             /* reset original options */
810    set_bit(index, res_all.hdr.item_present);
811 }
812
813 /* 
814  * Store restore info for Job record 
815  *
816  *    Restore = JobId=<job-id> Where=<root-directory> Replace=<options>
817  *
818  */
819 static void store_restore(LEX *lc, struct res_items *item, int index, int pass)
820 {
821    int token, i;
822    RES *res;
823    int options = lc->options;
824
825    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
826
827    Dmsg0(190, "Enter store_restore()\n");
828    
829    ((JOB *)(item->value))->JobType = item->code;
830    while ((token = lex_get_token(lc)) != T_EOL) {
831       int found; 
832
833       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
834          scan_err1(lc, "Expected a Restore keyword, got: %s", lc->str);
835       } else {
836          lcase(lc->str);
837          found = FALSE;
838          for (i=0; RestoreFields[i].name; i++) {
839             Dmsg1(190, "Restore kw=%s\n", lc->str);
840             if (strcmp(lc->str, RestoreFields[i].name) == 0) {
841                found = TRUE;
842                if (lex_get_token(lc) != T_EQUALS) {
843                   scan_err1(lc, "Expected an equals, got: %s", lc->str);
844                }
845                token = lex_get_token(lc);
846                Dmsg1(190, "Restore value=%s\n", lc->str);
847                switch (RestoreFields[i].token) {
848                   case 'C':
849                      /* Find Client Resource */
850                      if (pass == 2) {
851                         res = GetResWithName(R_CLIENT, lc->str);
852                         if (res == NULL) {
853                            scan_err1(lc, "Could not find specified Client Resource: %s",
854                                       lc->str);
855                         }
856                         res_all.res_job.client = (CLIENT *)res;
857                      }
858                      break;
859                   case 'F':
860                      /* Find FileSet Resource */
861                      if (pass == 2) {
862                         res = GetResWithName(R_FILESET, lc->str);
863                         if (res == NULL) {
864                            scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
865                                        lc->str);
866                         }
867                         res_all.res_job.fs = (FILESET *)res;
868                      }
869                      break;
870                   case 'J':
871                      /* JobId */
872                      if (token != T_NUMBER) {
873                         scan_err1(lc, "expected an integer number, got: %s", lc->str);
874                      }
875                      errno = 0;
876                      res_all.res_job.RestoreJobId = strtol(lc->str, NULL, 0);
877                      Dmsg1(190, "RestorJobId=%d\n", res_all.res_job.RestoreJobId);
878                      if (errno != 0) {
879                         scan_err1(lc, "expected an integer number, got: %s", lc->str);
880                      }
881                      break;
882                   case 'W':
883                      /* Where */
884                      if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
885                         scan_err1(lc, "Expected a Restore root directory, got: %s", lc->str);
886                      }
887                      if (pass == 1) {
888                         res_all.res_job.RestoreWhere = bstrdup(lc->str);
889                      }
890                      break;
891                   case 'R':
892                      /* Replacement options */
893                      if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
894                         scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
895                      }
896                      lcase(lc->str);
897                      /* Fix to scan Replacement options */
898                      for (i=0; ReplaceOptions[i].name; i++) {
899                         if (strcmp(lc->str, ReplaceOptions[i].name) == 0) {
900                             ((JOB *)(item->value))->RestoreOptions = ReplaceOptions[i].token;
901                            i = 0;
902                            break;
903                         }
904                      }
905                      if (i != 0) {
906                         scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
907                      }
908                      break;
909                } /* end switch */
910                break;
911             } /* end if strcmp() */
912          } /* end for */
913          if (!found) {
914             scan_err1(lc, "%s not a valid Restore keyword", lc->str);
915          }
916       }
917    } /* end while */
918    lc->options = options;             /* reset original options */
919    set_bit(index, res_all.hdr.item_present);
920 }
921
922
923
924 /* 
925  * Scan for FileSet options
926  */
927 static char *scan_fs_options(LEX *lc, int keyword)
928 {
929    int token, i;
930    static char opts[100];
931    char option[2];
932
933    option[0] = 0;                     /* default option = none */
934    opts[0] = option[1] = 0;           /* terminate options */
935    for (;;) {
936       token = lex_get_token(lc);             /* expect at least one option */       
937       if (token != T_IDENTIFIER && token != T_STRING && token != T_QUOTED_STRING) {
938          scan_err1(lc, "expected a FileSet option, got: %s", lc->str);
939       }
940       lcase(lc->str);
941       if (keyword == FS_KW_VERIFY) { /* special case */
942          /* ***FIXME**** ensure these are in permitted set */
943          strcpy(option, "V");         /* indicate Verify */
944          strcat(option, lc->str);
945          strcat(option, ":");         /* terminate it */
946       } else {
947          for (i=0; FS_options[i].name; i++) {
948             if (strcmp(lc->str, FS_options[i].name) == 0 && FS_options[i].keyword == keyword) {
949                option[0] = FS_options[i].option;
950                i = 0;
951                break;
952             }
953          }
954          if (i != 0) {
955             scan_err1(lc, "Expected a FileSet option keyword, got: %s", lc->str);
956          }
957       }
958       strcat(opts, option);
959
960       /* check if more options are specified */
961       if (lc->ch != ',') {
962          break;                       /* no, get out */
963       }
964       token = lex_get_token(lc);      /* yes, eat comma */
965    }
966
967    return opts;
968 }
969
970
971 /* Store FileSet Include/Exclude info */
972 static void store_inc(LEX *lc, struct res_items *item, int index, int pass)
973 {
974    int token, i;
975    int options = lc->options;
976    int keyword;
977    char *fname;
978    char inc_opts[100];
979    int inc_opts_len;
980
981    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
982
983    /* Get include options */
984    strcpy(inc_opts, "0");             /* set no options */
985    while ((token=lex_get_token(lc)) != T_BOB) {
986       if (token != T_STRING) {
987          scan_err1(lc, "expected a FileSet option keyword, got: %s", lc->str);
988       } else {
989          keyword = FS_KW_NONE;
990          lcase(lc->str);
991          for (i=0; FS_option_kw[i].name; i++) {
992             if (strcmp(lc->str, FS_option_kw[i].name) == 0) {
993                keyword = FS_option_kw[i].token;
994                break;
995             }
996          }
997          if (keyword == FS_KW_NONE) {
998             scan_err1(lc, "Expected a FileSet keyword, got: %s", lc->str);
999          }
1000       }
1001       /* Option keyword should be following by = <option> */
1002       if ((token=lex_get_token(lc)) != T_EQUALS) {
1003          scan_err1(lc, "expected an = following keyword, got: %s", lc->str);
1004       }
1005       strcat(inc_opts, scan_fs_options(lc, keyword));
1006       if (token == T_BOB) {
1007          break;
1008       }
1009    }
1010    strcat(inc_opts, " ");             /* add field separator */
1011    inc_opts_len = strlen(inc_opts);
1012
1013
1014    if (pass == 1) {
1015       if (!res_all.res_fs.have_MD5) {
1016          MD5Init(&res_all.res_fs.md5c);
1017          res_all.res_fs.have_MD5 = TRUE;
1018       }
1019       /* Pickup include/exclude names. Note, they are stored as
1020        * XYZ fname
1021        * where XYZ are the include/exclude options for the FileSet
1022        *     a "0 " (zero) indicates no options,
1023        * and fname is the file/directory name given
1024        */
1025       while ((token = lex_get_token(lc)) != T_EOB) {
1026          switch (token) {
1027             case T_COMMA:
1028             case T_EOL:
1029                continue;
1030
1031             case T_IDENTIFIER:
1032             case T_STRING:
1033             case T_QUOTED_STRING:
1034                fname = (char *) malloc(lc->str_len + inc_opts_len + 1);
1035                strcpy(fname, inc_opts);
1036                strcat(fname, lc->str);
1037                if (res_all.res_fs.have_MD5) {
1038                   MD5Update(&res_all.res_fs.md5c, (unsigned char *) fname, inc_opts_len + lc->str_len);
1039                }
1040                if (item->code == 0) { /* include */
1041                   if (res_all.res_fs.num_includes == res_all.res_fs.include_size) {
1042                      res_all.res_fs.include_size += 10;
1043                      if (res_all.res_fs.include_array == NULL) {
1044                         res_all.res_fs.include_array = (char **) malloc(sizeof(char *) * res_all.res_fs.include_size);
1045                      } else {
1046                         res_all.res_fs.include_array = (char **) realloc(res_all.res_fs.include_array,
1047                            sizeof(char *) * res_all.res_fs.include_size);
1048                      }
1049                   }
1050                   res_all.res_fs.include_array[res_all.res_fs.num_includes++] =    
1051                      fname;
1052                } else {                /* exclude */
1053                   if (res_all.res_fs.num_excludes == res_all.res_fs.exclude_size) {
1054                      res_all.res_fs.exclude_size += 10;
1055                      if (res_all.res_fs.exclude_array == NULL) {
1056                         res_all.res_fs.exclude_array = (char **) malloc(sizeof(char *) * res_all.res_fs.exclude_size);
1057                      } else {
1058                         res_all.res_fs.exclude_array = (char **) realloc(res_all.res_fs.exclude_array,
1059                            sizeof(char *) * res_all.res_fs.exclude_size);
1060                      }
1061                   }
1062                   res_all.res_fs.exclude_array[res_all.res_fs.num_excludes++] =    
1063                      fname;
1064                }
1065                break;
1066             default:
1067                scan_err1(lc, "Expected a filename, got: %s", lc->str);
1068          }                                 
1069       }
1070    } else { /* pass 2 */
1071       while (lex_get_token(lc) != T_EOB) 
1072          {}
1073    }
1074    scan_to_eol(lc);
1075    lc->options = options;
1076    set_bit(index, res_all.hdr.item_present);
1077 }