]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird_conf.c
Merge Meno's IPv6-1 code
[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), 
475          edit_utime(res->res_client.FileRetention, 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             sendit(sock, "      N\n");
585          }
586          for (j=0; j<incexe->name_list.size(); j++) {
587             sendit(sock, "      I %s\n", incexe->name_list.get(j));
588          }
589          if (incexe->name_list.size()) {
590             sendit(sock, "      N\n");
591          }
592       }
593          
594       for (i=0; i<res->res_fs.num_excludes; i++) {
595          INCEXE *incexe = res->res_fs.exclude_items[i];
596          for (j=0; j<incexe->name_list.size(); j++) {
597             sendit(sock, "      E %s\n", incexe->name_list.get(j));
598          }
599          if (incexe->name_list.size()) {
600             sendit(sock, "      N\n");
601          }
602       }
603       break;
604    }
605    case R_SCHEDULE:
606       if (res->res_sch.run) {
607          int i;
608          RUN *run = res->res_sch.run;
609          char buf[1000], num[30];
610          sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
611          if (!run) {
612             break;
613          }
614 next_run:
615          sendit(sock, "  --> Run Level=%s\n", level_to_str(run->level));
616          bstrncpy(buf, "      hour=", sizeof(buf));
617          for (i=0; i<24; i++) {
618             if (bit_is_set(i, run->hour)) {
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, "      mday=", sizeof(buf));
626          for (i=0; i<31; i++) {
627             if (bit_is_set(i, run->mday)) {
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, "      month=", sizeof(buf));
635          for (i=0; i<12; i++) {
636             if (bit_is_set(i, run->month)) {
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, "      wday=", sizeof(buf));
644          for (i=0; i<7; i++) {
645             if (bit_is_set(i, run->wday)) {
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, "      wom=", sizeof(buf));
653          for (i=0; i<5; i++) {
654             if (bit_is_set(i, run->wom)) {
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          bstrncpy(buf, "      woy=", sizeof(buf));
662          for (i=0; i<54; i++) {
663             if (bit_is_set(i, run->woy)) {
664                bsnprintf(num, sizeof(num), "%d ", i);
665                bstrncat(buf, num, sizeof(buf));
666             }
667          }
668          bstrncat(buf, "\n", sizeof(buf));
669          sendit(sock, buf);
670          sendit(sock, "      mins=%d\n", run->minute);
671          if (run->pool) {
672             sendit(sock, "     --> ");
673             dump_resource(-R_POOL, (RES *)run->pool, sendit, sock);
674          }
675          if (run->storage) {
676             sendit(sock, "     --> ");
677             dump_resource(-R_STORAGE, (RES *)run->storage, sendit, sock);
678          }
679          if (run->msgs) {
680             sendit(sock, "     --> ");
681             dump_resource(-R_MSGS, (RES *)run->msgs, sendit, sock);
682          }
683          /* If another Run record is chained in, go print it */
684          if (run->next) {
685             run = run->next;
686             goto next_run;
687          }
688       } else {
689          sendit(sock, "Schedule: name=%s\n", res->res_sch.hdr.name);
690       }
691       break;
692    case R_POOL:
693       sendit(sock, "Pool: name=%s PoolType=%s\n", res->res_pool.hdr.name,
694               res->res_pool.pool_type);
695       sendit(sock, "      use_cat=%d use_once=%d acpt_any=%d cat_files=%d\n",
696               res->res_pool.use_catalog, res->res_pool.use_volume_once,
697               res->res_pool.accept_any_volume, res->res_pool.catalog_files);
698       sendit(sock, "      max_vols=%d auto_prune=%d VolRetention=%s\n",
699               res->res_pool.max_volumes, res->res_pool.AutoPrune,
700               edit_utime(res->res_pool.VolRetention, ed1));
701       sendit(sock, "      VolUse=%s recycle=%d LabelFormat=%s\n", 
702               edit_utime(res->res_pool.VolUseDuration, ed1),
703               res->res_pool.Recycle,
704               NPRT(res->res_pool.label_format));
705       sendit(sock, "      CleaningPrefix=%s\n",
706               NPRT(res->res_pool.cleaning_prefix));
707       sendit(sock, "      recyleOldest=%d MaxVolJobs=%d MaxVolFiles=%d\n",
708               res->res_pool.purge_oldest_volume, 
709               res->res_pool.MaxVolJobs, res->res_pool.MaxVolFiles);
710       break;
711    case R_MSGS:
712       sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
713       if (res->res_msgs.mail_cmd) 
714          sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
715       if (res->res_msgs.operator_cmd) 
716          sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
717       break;
718    default:
719       sendit(sock, "Unknown resource type %d in dump_resource.\n", type);
720       break;
721    }
722    if (recurse && res->res_dir.hdr.next) {
723       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
724    }
725 }
726
727 /*
728  * Free all the members of an INCEXE structure
729  */
730 static void free_incexe(INCEXE *incexe)
731 {
732    incexe->name_list.destroy();
733    for (int i=0; i<incexe->num_opts; i++) {
734       FOPTS *fopt = incexe->opts_list[i];
735       fopt->regex.destroy();
736       fopt->wild.destroy();
737       fopt->base.destroy();
738       free(fopt);
739    }
740    if (incexe->opts_list) {
741       free(incexe->opts_list);
742    }
743    free(incexe);
744 }
745
746 /* 
747  * Free memory of resource -- called when daemon terminates.
748  * NB, we don't need to worry about freeing any references
749  * to other resources as they will be freed when that 
750  * resource chain is traversed.  Mainly we worry about freeing
751  * allocated strings (names).
752  */
753 void free_resource(RES *sres, int type)
754 {
755    int num;
756    RES *nres;                         /* next resource if linked */
757    URES *res = (URES *)sres;
758
759    if (res == NULL)
760       return;
761
762    /* common stuff -- free the resource name and description */
763    nres = (RES *)res->res_dir.hdr.next;
764    if (res->res_dir.hdr.name) {
765       free(res->res_dir.hdr.name);
766    }
767    if (res->res_dir.hdr.desc) {
768       free(res->res_dir.hdr.desc);
769    }
770
771    switch (type) {
772    case R_DIRECTOR:
773       if (res->res_dir.working_directory) {
774          free(res->res_dir.working_directory);
775       }
776       if (res->res_dir.pid_directory) {
777          free(res->res_dir.pid_directory);
778       }
779       if (res->res_dir.subsys_directory) {
780          free(res->res_dir.subsys_directory);
781       }
782       if (res->res_dir.password) {
783          free(res->res_dir.password);
784       }
785       if (res->res_dir.query_file) {
786          free(res->res_dir.query_file);
787       }
788       if (res->res_dir.DIRaddrs) {
789          free_addresses(res->res_dir.DIRaddrs);
790       }
791       break;
792    case R_COUNTER:
793        break;
794    case R_CONSOLE:
795       if (res->res_con.password) {
796          free(res->res_con.password);
797       }
798       for (int i=0; i<Num_ACL; i++) {
799          if (res->res_con.ACL_lists[i]) {
800             delete res->res_con.ACL_lists[i];
801             res->res_con.ACL_lists[i] = NULL;
802          }
803       }
804       break;
805    case R_CLIENT:
806       if (res->res_client.address) {
807          free(res->res_client.address);
808       }
809       if (res->res_client.password) {
810          free(res->res_client.password);
811       }
812       break;
813    case R_STORAGE:
814       if (res->res_store.address) {
815          free(res->res_store.address);
816       }
817       if (res->res_store.password) {
818          free(res->res_store.password);
819       }
820       if (res->res_store.media_type) {
821          free(res->res_store.media_type);
822       }
823       if (res->res_store.dev_name) {
824          free(res->res_store.dev_name);
825       }
826       break;
827    case R_CATALOG:
828       if (res->res_cat.db_address) {
829          free(res->res_cat.db_address);
830       }
831       if (res->res_cat.db_socket) {
832          free(res->res_cat.db_socket);
833       }
834       if (res->res_cat.db_user) {
835          free(res->res_cat.db_user);
836       }
837       if (res->res_cat.db_name) {
838          free(res->res_cat.db_name);
839       }
840       if (res->res_cat.db_password) {
841          free(res->res_cat.db_password);
842       }
843       break;
844    case R_FILESET:
845       if ((num=res->res_fs.num_includes)) {
846          while (--num >= 0) {   
847             free_incexe(res->res_fs.include_items[num]);
848          }
849          free(res->res_fs.include_items);
850       }
851       res->res_fs.num_includes = 0;
852       if ((num=res->res_fs.num_excludes)) {
853          while (--num >= 0) {   
854             free_incexe(res->res_fs.exclude_items[num]);
855          }
856          free(res->res_fs.exclude_items);
857       }
858       res->res_fs.num_excludes = 0;
859       break;
860    case R_POOL:
861       if (res->res_pool.pool_type) {
862          free(res->res_pool.pool_type);
863       }
864       if (res->res_pool.label_format) {
865          free(res->res_pool.label_format);
866       }
867       if (res->res_pool.cleaning_prefix) {
868          free(res->res_pool.cleaning_prefix);
869       }
870       break;
871    case R_SCHEDULE:
872       if (res->res_sch.run) {
873          RUN *nrun, *next;
874          nrun = res->res_sch.run;
875          while (nrun) {
876             next = nrun->next;
877             free(nrun);
878             nrun = next;
879          }
880       }
881       break;
882    case R_JOB:
883    case R_JOBDEFS:
884       if (res->res_job.RestoreWhere) {
885          free(res->res_job.RestoreWhere);
886       }
887       if (res->res_job.RestoreBootstrap) {
888          free(res->res_job.RestoreBootstrap);
889       }
890       if (res->res_job.WriteBootstrap) {
891          free(res->res_job.WriteBootstrap);
892       }
893       if (res->res_job.RunBeforeJob) {
894          free(res->res_job.RunBeforeJob);
895       }
896       if (res->res_job.RunAfterJob) {
897          free(res->res_job.RunAfterJob);
898       }
899       if (res->res_job.RunAfterFailedJob) {
900          free(res->res_job.RunAfterFailedJob);
901       }
902       if (res->res_job.ClientRunBeforeJob) {
903          free(res->res_job.ClientRunBeforeJob);
904       }
905       if (res->res_job.ClientRunAfterJob) {
906          free(res->res_job.ClientRunAfterJob);
907       }
908       break;
909    case R_MSGS:
910       if (res->res_msgs.mail_cmd) {
911          free(res->res_msgs.mail_cmd);
912       }
913       if (res->res_msgs.operator_cmd) {
914          free(res->res_msgs.operator_cmd);
915       }
916       free_msgs_res((MSGS *)res);  /* free message resource */
917       res = NULL;
918       break;
919    default:
920       printf("Unknown resource type %d in free_resource.\n", type);
921    }
922    /* Common stuff again -- free the resource, recurse to next one */
923    if (res) {
924       free(res);
925    }
926    if (nres) {
927       free_resource(nres, type);
928    }
929 }
930
931 /*
932  * Save the new resource by chaining it into the head list for
933  * the resource. If this is pass 2, we update any resource
934  * pointers because they may not have been defined until 
935  * later in pass 1.
936  */
937 void save_resource(int type, RES_ITEM *items, int pass)
938 {
939    URES *res;
940    int rindex = type - r_first;
941    int i, size;
942    int error = 0;
943    
944    /* Check Job requirements after applying JobDefs */
945    if (type != R_JOB && type != R_JOBDEFS) {
946       /* 
947        * Ensure that all required items are present
948        */
949       for (i=0; items[i].name; i++) {
950          if (items[i].flags & ITEM_REQUIRED) {
951                if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
952                   Emsg2(M_ERROR_TERM, 0, "%s item is required in %s resource, but not found.\n",
953                     items[i].name, resources[rindex]);
954                 }
955          }
956          /* If this triggers, take a look at lib/parse_conf.h */
957          if (i >= MAX_RES_ITEMS) {
958             Emsg1(M_ERROR_TERM, 0, "Too many items in %s resource\n", resources[rindex]);
959          }
960       }
961    }
962
963    /*
964     * During pass 2 in each "store" routine, we looked up pointers 
965     * to all the resources referrenced in the current resource, now we
966     * must copy their addresses from the static record to the allocated
967     * record.
968     */
969    if (pass == 2) {
970       switch (type) {
971       /* Resources not containing a resource */
972       case R_CONSOLE:
973       case R_CATALOG:
974       case R_STORAGE:
975       case R_POOL:
976       case R_MSGS:
977       case R_FILESET:
978          break;
979
980       /* Resources containing another resource */
981       case R_DIRECTOR:
982          if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
983             Emsg1(M_ERROR_TERM, 0, "Cannot find Director resource %s\n", res_all.res_dir.hdr.name);
984          }
985          res->res_dir.messages = res_all.res_dir.messages;
986          break;
987       case R_JOB:
988       case R_JOBDEFS:
989          if ((res = (URES *)GetResWithName(type, res_all.res_dir.hdr.name)) == NULL) {
990             Emsg1(M_ERROR_TERM, 0, "Cannot find Job resource %s\n", 
991                   res_all.res_dir.hdr.name);
992          }
993          res->res_job.messages   = res_all.res_job.messages;
994          res->res_job.schedule   = res_all.res_job.schedule;
995          res->res_job.client     = res_all.res_job.client;
996          res->res_job.fileset    = res_all.res_job.fileset;
997          res->res_job.storage    = res_all.res_job.storage;
998          res->res_job.pool       = res_all.res_job.pool;
999          res->res_job.full_pool  = res_all.res_job.full_pool;
1000          res->res_job.inc_pool   = res_all.res_job.inc_pool;
1001          res->res_job.dif_pool   = res_all.res_job.dif_pool;
1002          res->res_job.verify_job = res_all.res_job.verify_job;
1003          res->res_job.jobdefs    = res_all.res_job.jobdefs;
1004          break;
1005       case R_COUNTER:
1006          if ((res = (URES *)GetResWithName(R_COUNTER, res_all.res_counter.hdr.name)) == NULL) {
1007             Emsg1(M_ERROR_TERM, 0, "Cannot find Counter resource %s\n", res_all.res_counter.hdr.name);
1008          }
1009          res->res_counter.Catalog = res_all.res_counter.Catalog;
1010          res->res_counter.WrapCounter = res_all.res_counter.WrapCounter;
1011          break;
1012
1013       case R_CLIENT:
1014          if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_client.hdr.name)) == NULL) {
1015             Emsg1(M_ERROR_TERM, 0, "Cannot find Client resource %s\n", res_all.res_client.hdr.name);
1016          }
1017          res->res_client.catalog = res_all.res_client.catalog;
1018          break;
1019       case R_SCHEDULE:
1020          /*
1021           * Schedule is a bit different in that it contains a RUN record
1022           * chain which isn't a "named" resource. This chain was linked
1023           * in by run_conf.c during pass 2, so here we jam the pointer 
1024           * into the Schedule resource.                         
1025           */
1026          if ((res = (URES *)GetResWithName(R_SCHEDULE, res_all.res_client.hdr.name)) == NULL) {
1027             Emsg1(M_ERROR_TERM, 0, "Cannot find Schedule resource %s\n", res_all.res_client.hdr.name);
1028          }
1029          res->res_sch.run = res_all.res_sch.run;
1030          break;
1031       default:
1032          Emsg1(M_ERROR, 0, "Unknown resource type %d in save_resource.\n", type);
1033          error = 1;
1034          break;
1035       }
1036       /* Note, the resource name was already saved during pass 1,
1037        * so here, we can just release it.
1038        */
1039       if (res_all.res_dir.hdr.name) {
1040          free(res_all.res_dir.hdr.name);
1041          res_all.res_dir.hdr.name = NULL;
1042       }
1043       if (res_all.res_dir.hdr.desc) {
1044          free(res_all.res_dir.hdr.desc);
1045          res_all.res_dir.hdr.desc = NULL;
1046       }
1047       return;
1048    }
1049
1050    /*
1051     * The following code is only executed during pass 1   
1052     */
1053    switch (type) {
1054    case R_DIRECTOR:
1055       size = sizeof(DIRRES);
1056       break;
1057    case R_CONSOLE:
1058       size = sizeof(CONRES);
1059       break;
1060    case R_CLIENT:
1061       size =sizeof(CLIENT);
1062       break;
1063    case R_STORAGE:
1064       size = sizeof(STORE); 
1065       break;
1066    case R_CATALOG:
1067       size = sizeof(CAT);
1068       break;
1069    case R_JOB:
1070    case R_JOBDEFS:
1071       size = sizeof(JOB);
1072       break;
1073    case R_FILESET:
1074       size = sizeof(FILESET);
1075       break;
1076    case R_SCHEDULE:
1077       size = sizeof(SCHED);
1078       break;
1079    case R_POOL:
1080       size = sizeof(POOL);
1081       break;
1082    case R_MSGS:
1083       size = sizeof(MSGS);
1084       break;
1085    case R_COUNTER:
1086       size = sizeof(COUNTER);
1087       break;
1088    default:
1089       printf("Unknown resource type %d in save_resrouce.\n", type);
1090       error = 1;
1091       size = 1;
1092       break;
1093    }
1094    /* Common */
1095    if (!error) {
1096       res = (URES *)malloc(size);
1097       memcpy(res, &res_all, size);
1098       if (!res_head[rindex]) {
1099          res_head[rindex] = (RES *)res; /* store first entry */
1100          Dmsg3(900, "Inserting first %s res: %s index=%d\n", res_to_str(type),
1101                res->res_dir.hdr.name, rindex);
1102       } else {
1103          RES *next;
1104          /* Add new res to end of chain */
1105          for (next=res_head[rindex]; next->next; next=next->next) {
1106             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
1107                Emsg2(M_ERROR_TERM, 0,
1108                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
1109                   resources[rindex].name, res->res_dir.hdr.name);
1110             }
1111          }
1112          next->next = (RES *)res;
1113          Dmsg4(900, "Inserting %s res: %s index=%d pass=%d\n", res_to_str(type),
1114                res->res_dir.hdr.name, rindex, pass);
1115       }
1116    }
1117 }
1118
1119 /* 
1120  * Store JobType (backup, verify, restore)
1121  *
1122  */
1123 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass)
1124 {
1125    int token, i;   
1126
1127    token = lex_get_token(lc, T_NAME);
1128    /* Store the type both pass 1 and pass 2 */
1129    for (i=0; jobtypes[i].type_name; i++) {
1130       if (strcasecmp(lc->str, jobtypes[i].type_name) == 0) {
1131          *(int *)(item->value) = jobtypes[i].job_type;
1132          i = 0;
1133          break;
1134       }
1135    }
1136    if (i != 0) {
1137       scan_err1(lc, "Expected a Job Type keyword, got: %s", lc->str);
1138    }
1139    scan_to_eol(lc);
1140    set_bit(index, res_all.hdr.item_present);
1141 }
1142
1143 /* 
1144  * Store Job Level (Full, Incremental, ...)
1145  *
1146  */
1147 void store_level(LEX *lc, RES_ITEM *item, int index, int pass)
1148 {
1149    int token, i;
1150
1151    token = lex_get_token(lc, T_NAME);
1152    /* Store the level pass 2 so that type is defined */
1153    for (i=0; joblevels[i].level_name; i++) {
1154       if (strcasecmp(lc->str, joblevels[i].level_name) == 0) {
1155          *(int *)(item->value) = joblevels[i].level;
1156          i = 0;
1157          break;
1158       }
1159    }
1160    if (i != 0) {
1161       scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
1162    }
1163    scan_to_eol(lc);
1164    set_bit(index, res_all.hdr.item_present);
1165 }
1166
1167 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass)
1168 {
1169    int token, i;
1170    token = lex_get_token(lc, T_NAME);
1171    /* Scan Replacement options */
1172    for (i=0; ReplaceOptions[i].name; i++) {
1173       if (strcasecmp(lc->str, ReplaceOptions[i].name) == 0) {
1174          *(int *)(item->value) = ReplaceOptions[i].token;
1175          i = 0;
1176          break;
1177       }
1178    }
1179    if (i != 0) {
1180       scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
1181    }
1182    scan_to_eol(lc);
1183    set_bit(index, res_all.hdr.item_present);
1184 }
1185
1186 /* 
1187  * Store ACL (access control list)
1188  *
1189  */
1190 void store_acl(LEX *lc, RES_ITEM *item, int index, int pass)
1191 {
1192    int token;
1193
1194    for (;;) {
1195       token = lex_get_token(lc, T_NAME);
1196       if (pass == 1) {
1197          if (((alist **)item->value)[item->code] == NULL) {   
1198             ((alist **)item->value)[item->code] = new alist(10, owned_by_alist);
1199 //          Dmsg1(900, "Defined new ACL alist at %d\n", item->code);
1200          }
1201          ((alist **)item->value)[item->code]->append(bstrdup(lc->str));
1202 //       Dmsg2(900, "Appended to %d %s\n", item->code, lc->str);
1203       }
1204       token = lex_get_token(lc, T_ALL);
1205       if (token == T_COMMA) {
1206          continue;                    /* get another ACL */
1207       }
1208       break;
1209    }
1210    set_bit(index, res_all.hdr.item_present);
1211 }
1212
1213
1214 #ifdef old_deprecated_code
1215 /* 
1216  * Store backup/verify info for Job record 
1217  *
1218  * Note, this code is used for both BACKUP and VERIFY jobs
1219  *
1220  *    Backup = Client=<client-name> FileSet=<FileSet-name> Level=<level>
1221  */
1222 static void store_backup(LEX *lc, RES_ITEM *item, int index, int pass)
1223 {
1224    int token, i;
1225    RES *res;
1226    int options = lc->options;
1227
1228    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
1229
1230    
1231    ((JOB *)(item->value))->JobType = item->code;
1232    while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
1233       bool found = false;
1234
1235       Dmsg1(900, "store_backup got token=%s\n", lex_tok_to_str(token));
1236       
1237       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1238          scan_err1(lc, "Expected a backup/verify keyword, got: %s", lc->str);
1239       }
1240       Dmsg1(900, "Got keyword: %s\n", lc->str);
1241       for (i=0; BakVerFields[i].name; i++) {
1242          if (strcasecmp(lc->str, BakVerFields[i].name) == 0) {
1243             found = true;
1244             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
1245                scan_err1(lc, "Expected an equals, got: %s", lc->str);
1246             }
1247             token = lex_get_token(lc, T_NAME);
1248             Dmsg1(900, "Got value: %s\n", lc->str);
1249             switch (BakVerFields[i].token) {
1250             case 'C':
1251                /* Find Client Resource */
1252                if (pass == 2) {
1253                   res = GetResWithName(R_CLIENT, lc->str);
1254                   if (res == NULL) {
1255                      scan_err1(lc, "Could not find specified Client Resource: %s",
1256                                 lc->str);
1257                   }
1258                   res_all.res_job.client = (CLIENT *)res;
1259                }
1260                break;
1261             case 'F':
1262                /* Find FileSet Resource */
1263                if (pass == 2) {
1264                   res = GetResWithName(R_FILESET, lc->str);
1265                   if (res == NULL) {
1266                      scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
1267                                  lc->str);
1268                   }
1269                   res_all.res_job.fileset = (FILESET *)res;
1270                }
1271                break;
1272             case 'L':
1273                /* Get level */
1274                for (i=0; joblevels[i].level_name; i++) {
1275                   if (joblevels[i].job_type == item->code && 
1276                        strcasecmp(lc->str, joblevels[i].level_name) == 0) {
1277                      ((JOB *)(item->value))->level = joblevels[i].level;
1278                      i = 0;
1279                      break;
1280                   }
1281                }
1282                if (i != 0) {
1283                   scan_err1(lc, "Expected a Job Level keyword, got: %s", lc->str);
1284                }
1285                break;
1286             } /* end switch */
1287             break;
1288          } /* end if strcmp() */
1289       } /* end for */
1290       if (!found) {
1291          scan_err1(lc, "%s not a valid Backup/verify keyword", lc->str);
1292       }
1293    } /* end while */
1294    lc->options = options;             /* reset original options */
1295    set_bit(index, res_all.hdr.item_present);
1296 }
1297
1298 /* 
1299  * Store restore info for Job record 
1300  *
1301  *    Restore = JobId=<job-id> Where=<root-directory> Replace=<options> Bootstrap=<file>
1302  *
1303  */
1304 static void store_restore(LEX *lc, RES_ITEM *item, int index, int pass)
1305 {
1306    int token, i;
1307    RES *res;
1308    int options = lc->options;
1309
1310    lc->options |= LOPT_NO_IDENT;      /* make spaces significant */
1311
1312    Dmsg0(900, "Enter store_restore()\n");
1313    
1314    ((JOB *)(item->value))->JobType = item->code;
1315    while ((token = lex_get_token(lc, T_ALL)) != T_EOL) {
1316       bool found = false;
1317
1318       if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1319          scan_err1(lc, "expected a name, got: %s", lc->str);
1320       }
1321       for (i=0; RestoreFields[i].name; i++) {
1322          Dmsg1(900, "Restore kw=%s\n", lc->str);
1323          if (strcasecmp(lc->str, RestoreFields[i].name) == 0) {
1324             found = true;
1325             if (lex_get_token(lc, T_ALL) != T_EQUALS) {
1326                scan_err1(lc, "Expected an equals, got: %s", lc->str);
1327             }
1328             token = lex_get_token(lc, T_ALL);
1329             Dmsg1(900, "Restore value=%s\n", lc->str);
1330             switch (RestoreFields[i].token) {
1331             case 'B':
1332                /* Bootstrap */
1333                if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1334                   scan_err1(lc, "Expected a Restore bootstrap file, got: %s", lc->str);
1335                }
1336                if (pass == 1) {
1337                   res_all.res_job.RestoreBootstrap = bstrdup(lc->str);
1338                }
1339                break;
1340             case 'C':
1341                /* Find Client Resource */
1342                if (pass == 2) {
1343                   res = GetResWithName(R_CLIENT, lc->str);
1344                   if (res == NULL) {
1345                      scan_err1(lc, "Could not find specified Client Resource: %s",
1346                                 lc->str);
1347                   }
1348                   res_all.res_job.client = (CLIENT *)res;
1349                }
1350                break;
1351             case 'F':
1352                /* Find FileSet Resource */
1353                if (pass == 2) {
1354                   res = GetResWithName(R_FILESET, lc->str);
1355                   if (res == NULL) {
1356                      scan_err1(lc, "Could not find specified FileSet Resource: %s\n",
1357                                  lc->str);
1358                   }
1359                   res_all.res_job.fileset = (FILESET *)res;
1360                }
1361                break;
1362             case 'J':
1363                /* JobId */
1364                if (token != T_NUMBER) {
1365                   scan_err1(lc, "expected an integer number, got: %s", lc->str);
1366                }
1367                errno = 0;
1368                res_all.res_job.RestoreJobId = strtol(lc->str, NULL, 0);
1369                Dmsg1(900, "RestorJobId=%d\n", res_all.res_job.RestoreJobId);
1370                if (errno != 0) {
1371                   scan_err1(lc, "expected an integer number, got: %s", lc->str);
1372                }
1373                break;
1374             case 'W':
1375                /* Where */
1376                if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1377                   scan_err1(lc, "Expected a Restore root directory, got: %s", lc->str);
1378                }
1379                if (pass == 1) {
1380                   res_all.res_job.RestoreWhere = bstrdup(lc->str);
1381                }
1382                break;
1383             case 'R':
1384                /* Replacement options */
1385                if (token != T_IDENTIFIER && token != T_UNQUOTED_STRING && token != T_QUOTED_STRING) {
1386                   scan_err1(lc, "Expected a keyword name, got: %s", lc->str);
1387                }
1388                /* Fix to scan Replacement options */
1389                for (i=0; ReplaceOptions[i].name; i++) {
1390                   if (strcasecmp(lc->str, ReplaceOptions[i].name) == 0) {
1391                       ((JOB *)(item->value))->replace = ReplaceOptions[i].token;
1392                      i = 0;
1393                      break;
1394                   }
1395                }
1396                if (i != 0) {
1397                   scan_err1(lc, "Expected a Restore replacement option, got: %s", lc->str);
1398                }
1399                break;
1400             } /* end switch */
1401             break;
1402          } /* end if strcmp() */
1403       } /* end for */
1404       if (!found) {
1405          scan_err1(lc, "%s not a valid Restore keyword", lc->str);
1406       }
1407    } /* end while */
1408    lc->options = options;             /* reset original options */
1409    set_bit(index, res_all.hdr.item_present);
1410 }
1411 #endif