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