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