2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Main configuration file parser for Bacula User Agent
22 * some parts may be split into separate files such as
23 * the schedule configuration (sch_config.c).
25 * Note, the configuration file parser consists of three parts
27 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
29 * 2. The generic config scanner in lib/parse_config.c and
31 * These files contain the parser code, some utility
32 * routines, and the common store routines (name, int,
35 * 3. The daemon specific file, which contains the Resource
36 * definitions as well as any specific store routines
37 * for the resource records.
39 * Kern Sibbald, January MM, September MM
43 #include "console_conf.h"
45 /* Define the first and last resource ID record
46 * types. Note, these should be unique for each
47 * daemon though not a requirement.
49 int32_t r_first = R_FIRST;
50 int32_t r_last = R_LAST;
51 static RES *sres_head[R_LAST - R_FIRST + 1];
52 RES **res_head = sres_head;
54 /* Forward referenced subroutines */
57 /* We build the current resource here as we are
58 * scanning the resource configuration definition,
59 * then move it to allocated memory when the resource
63 extern "C" { // work around visual compiler mangling variables
69 int32_t res_all_size = sizeof(res_all);
71 /* Definition of records permitted within each
72 * resource with the routine to process the record
76 /* Console "globals" */
77 static RES_ITEM cons_items[] = {
78 {"Name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
79 {"Description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
80 {"RCFile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
81 {"HistoryFile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
82 {"Password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
83 {"TlsAuthenticate",store_bool, ITEM(res_cons.tls_authenticate), 0, 0, 0},
84 {"TlsEnable", store_bool, ITEM(res_cons.tls_enable), 0, 0, 0},
85 {"TlsRequire", store_bool, ITEM(res_cons.tls_require), 0, 0, 0},
86 {"TlsCaCertificateFile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
87 {"TlsCaCertificateDir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
88 {"TlsCertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
89 {"TlsKey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
90 {"Director", store_str, ITEM(res_cons.director), 0, 0, 0},
91 {"HeartbeatInterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
92 {NULL, NULL, {0}, 0, 0, 0}
96 /* Director's that we can contact */
97 static RES_ITEM dir_items[] = {
98 {"Name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
99 {"Description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
100 {"DirPort", store_pint32, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
101 {"Address", store_str, ITEM(res_dir.address), 0, 0, 0},
102 {"Password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
103 {"TlsAuthenticate",store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
104 {"TlsEnable", store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
105 {"TlsRequire", store_bool, ITEM(res_dir.tls_require), 0, 0, 0},
106 {"TlsCaCertificateFile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
107 {"TlsCaCertificateDir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
108 {"TlsCertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
109 {"TlsKey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
110 {"HeartbeatInterval", store_time, ITEM(res_dir.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
111 {NULL, NULL, {0}, 0, 0, 0}
115 * This is the master resource definition.
116 * It must have one item for each of the resources.
120 RES_TABLE resources[] = {
121 {"Console", cons_items, R_CONSOLE},
122 {"Director", dir_items, R_DIRECTOR},
126 /* Dump contents of resource */
127 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
129 URES *res = (URES *)reshdr;
133 printf(_("No record for %d %s\n"), type, res_to_str(type));
136 if (type < 0) { /* no recursion */
142 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
143 res->res_cons.rc_file, res->res_cons.hist_file);
146 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
147 res->res_dir.address, res->res_dir.DIRport);
150 printf(_("Unknown resource type %d\n"), type);
152 if (recurse && res->res_dir.hdr.next) {
153 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
158 * Free memory of resource.
159 * NB, we don't need to worry about freeing any references
160 * to other resources as they will be freed when that
161 * resource chain is traversed. Mainly we worry about freeing
162 * allocated strings (names).
164 void free_resource(RES *sres, int type)
167 URES *res = (URES *)sres;
173 /* common stuff -- free the resource name */
174 nres = (RES *)res->res_dir.hdr.next;
175 if (res->res_dir.hdr.name) {
176 free(res->res_dir.hdr.name);
178 if (res->res_dir.hdr.desc) {
179 free(res->res_dir.hdr.desc);
184 if (res->res_cons.rc_file) {
185 free(res->res_cons.rc_file);
187 if (res->res_cons.hist_file) {
188 free(res->res_cons.hist_file);
190 if (res->res_cons.tls_ctx) {
191 free_tls_context(res->res_cons.tls_ctx);
193 if (res->res_cons.tls_ca_certfile) {
194 free(res->res_cons.tls_ca_certfile);
196 if (res->res_cons.tls_ca_certdir) {
197 free(res->res_cons.tls_ca_certdir);
199 if (res->res_cons.tls_certfile) {
200 free(res->res_cons.tls_certfile);
202 if (res->res_cons.tls_keyfile) {
203 free(res->res_cons.tls_keyfile);
205 if (res->res_cons.director) {
206 free(res->res_cons.director);
210 if (res->res_dir.address) {
211 free(res->res_dir.address);
213 if (res->res_dir.tls_ctx) {
214 free_tls_context(res->res_dir.tls_ctx);
216 if (res->res_dir.tls_ca_certfile) {
217 free(res->res_dir.tls_ca_certfile);
219 if (res->res_dir.tls_ca_certdir) {
220 free(res->res_dir.tls_ca_certdir);
222 if (res->res_dir.tls_certfile) {
223 free(res->res_dir.tls_certfile);
225 if (res->res_dir.tls_keyfile) {
226 free(res->res_dir.tls_keyfile);
230 printf(_("Unknown resource type %d\n"), type);
232 /* Common stuff again -- free the resource, recurse to next one */
235 free_resource(nres, type);
239 /* Save the new resource by chaining it into the head list for
240 * the resource. If this is pass 2, we update any resource
241 * pointers (currently only in the Job resource).
243 void save_resource(int type, RES_ITEM *items, int pass)
246 int rindex = type - r_first;
251 * Ensure that all required items are present
253 for (i=0; items[i].name; i++) {
254 if (items[i].flags & ITEM_REQUIRED) {
255 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
256 Emsg2(M_ERROR_TERM, 0, _("%s item is required in %s resource, but not found.\n"),
257 items[i].name, resources[rindex]);
262 /* During pass 2, we looked up pointers to all the resources
263 * referrenced in the current resource, , now we
264 * must copy their address from the static record to the allocated
269 /* Resources not containing a resource */
275 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
279 /* Note, the resoure name was already saved during pass 1,
280 * so here, we can just release it.
282 if (res_all.res_dir.hdr.name) {
283 free(res_all.res_dir.hdr.name);
284 res_all.res_dir.hdr.name = NULL;
286 if (res_all.res_dir.hdr.desc) {
287 free(res_all.res_dir.hdr.desc);
288 res_all.res_dir.hdr.desc = NULL;
293 /* The following code is only executed during pass 1 */
296 size = sizeof(CONRES);
299 size = sizeof(DIRRES);
302 printf(_("Unknown resource type %d\n"), type);
309 res = (URES *)malloc(size);
310 memcpy(res, &res_all, size);
311 if (!res_head[rindex]) {
312 res_head[rindex] = (RES *)res; /* store first entry */
315 for (last=next=res_head[rindex]; next; next=next->next) {
317 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
318 Emsg2(M_ERROR_TERM, 0,
319 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
320 resources[rindex].name, res->res_dir.hdr.name);
323 last->next = (RES *)res;
324 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
325 res->res_dir.hdr.name);
330 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
332 config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
333 r_first, r_last, resources, res_head);
334 return config->parse_config();