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