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