2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2016 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Main configuration file parser for Bacula User Agent
21 * some parts may be split into separate files such as
22 * the schedule configuration (sch_config.c).
24 * Note, the configuration file parser consists of three parts
26 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
28 * 2. The generic config scanner in lib/parse_config.c and
30 * These files contain the parser code, some utility
31 * routines, and the common store routines (name, int,
34 * 3. The daemon specific file, which contains the Resource
35 * definitions as well as any specific store routines
36 * for the resource records.
38 * Kern Sibbald, January MM, September MM
42 #include "console_conf.h"
44 /* Define the first and last resource ID record
45 * types. Note, these should be unique for each
46 * daemon though not a requirement.
48 int32_t r_first = R_FIRST;
49 int32_t r_last = R_LAST;
50 static RES *sres_head[R_LAST - R_FIRST + 1];
51 RES **res_head = sres_head;
53 /* Forward referenced subroutines */
56 /* We build the current resource here as we are
57 * scanning the resource configuration definition,
58 * then move it to allocated memory when the resource
62 extern "C" { // work around visual compiler mangling variables
68 int32_t res_all_size = sizeof(res_all);
70 /* Definition of records permitted within each
71 * resource with the routine to process the record
75 /* Console "globals" */
76 static RES_ITEM cons_items[] = {
77 {"Name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
78 {"Description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
79 {"RCFile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
80 {"HistoryFile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
81 {"Password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
82 {"TlsAuthenticate",store_bool, ITEM(res_cons.tls_authenticate), 0, 0, 0},
83 {"TlsEnable", store_bool, ITEM(res_cons.tls_enable), 0, 0, 0},
84 {"TlsRequire", store_bool, ITEM(res_cons.tls_require), 0, 0, 0},
85 {"TlsCaCertificateFile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
86 {"TlsCaCertificateDir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
87 {"TlsCertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
88 {"TlsKey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
89 {"Director", store_str, ITEM(res_cons.director), 0, 0, 0},
90 {"HeartbeatInterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
91 {NULL, NULL, {0}, 0, 0, 0}
95 /* Director's that we can contact */
96 static RES_ITEM dir_items[] = {
97 {"Name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
98 {"Description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
99 {"DirPort", store_pint32, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
100 {"Address", store_str, ITEM(res_dir.address), 0, 0, 0},
101 {"Password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
102 {"TlsAuthenticate",store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
103 {"TlsEnable", store_bool, ITEM(res_dir.tls_enable), 0, 0, 0},
104 {"TlsRequire", store_bool, ITEM(res_dir.tls_require), 0, 0, 0},
105 {"TlsCaCertificateFile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
106 {"TlsCaCertificateDir", store_dir, ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
107 {"TlsCertificate", store_dir, ITEM(res_dir.tls_certfile), 0, 0, 0},
108 {"TlsKey", store_dir, ITEM(res_dir.tls_keyfile), 0, 0, 0},
109 {"HeartbeatInterval", store_time, ITEM(res_dir.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
110 {NULL, NULL, {0}, 0, 0, 0}
114 * This is the master resource definition.
115 * It must have one item for each of the resources.
119 RES_TABLE resources[] = {
120 {"Console", cons_items, R_CONSOLE},
121 {"Director", dir_items, R_DIRECTOR},
125 /* Dump contents of resource */
126 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
128 URES *res = (URES *)reshdr;
132 printf(_("No record for %d %s\n"), type, res_to_str(type));
135 if (type < 0) { /* no recursion */
141 printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
142 res->res_cons.rc_file, res->res_cons.hist_file);
145 printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
146 res->res_dir.address, res->res_dir.DIRport);
149 printf(_("Unknown resource type %d\n"), type);
151 if (recurse && res->res_dir.hdr.next) {
152 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
157 * Free memory of resource.
158 * NB, we don't need to worry about freeing any references
159 * to other resources as they will be freed when that
160 * resource chain is traversed. Mainly we worry about freeing
161 * allocated strings (names).
163 void free_resource(RES *sres, int type)
166 URES *res = (URES *)sres;
172 /* common stuff -- free the resource name */
173 nres = (RES *)res->res_dir.hdr.next;
174 if (res->res_dir.hdr.name) {
175 free(res->res_dir.hdr.name);
177 if (res->res_dir.hdr.desc) {
178 free(res->res_dir.hdr.desc);
183 if (res->res_cons.rc_file) {
184 free(res->res_cons.rc_file);
186 if (res->res_cons.hist_file) {
187 free(res->res_cons.hist_file);
189 if (res->res_cons.tls_ctx) {
190 free_tls_context(res->res_cons.tls_ctx);
192 if (res->res_cons.tls_ca_certfile) {
193 free(res->res_cons.tls_ca_certfile);
195 if (res->res_cons.tls_ca_certdir) {
196 free(res->res_cons.tls_ca_certdir);
198 if (res->res_cons.tls_certfile) {
199 free(res->res_cons.tls_certfile);
201 if (res->res_cons.tls_keyfile) {
202 free(res->res_cons.tls_keyfile);
204 if (res->res_cons.director) {
205 free(res->res_cons.director);
209 if (res->res_dir.address) {
210 free(res->res_dir.address);
212 if (res->res_dir.tls_ctx) {
213 free_tls_context(res->res_dir.tls_ctx);
215 if (res->res_dir.tls_ca_certfile) {
216 free(res->res_dir.tls_ca_certfile);
218 if (res->res_dir.tls_ca_certdir) {
219 free(res->res_dir.tls_ca_certdir);
221 if (res->res_dir.tls_certfile) {
222 free(res->res_dir.tls_certfile);
224 if (res->res_dir.tls_keyfile) {
225 free(res->res_dir.tls_keyfile);
229 printf(_("Unknown resource type %d\n"), type);
231 /* Common stuff again -- free the resource, recurse to next one */
234 free_resource(nres, type);
238 /* Save the new resource by chaining it into the head list for
239 * the resource. If this is pass 2, we update any resource
240 * pointers (currently only in the Job resource).
242 void save_resource(int type, RES_ITEM *items, int pass)
245 int rindex = type - r_first;
250 * Ensure that all required items are present
252 for (i=0; items[i].name; i++) {
253 if (items[i].flags & ITEM_REQUIRED) {
254 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
255 Emsg2(M_ERROR_TERM, 0, _("%s item is required in %s resource, but not found.\n"),
256 items[i].name, resources[rindex]);
261 /* During pass 2, we looked up pointers to all the resources
262 * referrenced in the current resource, , now we
263 * must copy their address from the static record to the allocated
268 /* Resources not containing a resource */
274 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
278 /* Note, the resoure name was already saved during pass 1,
279 * so here, we can just release it.
281 if (res_all.res_dir.hdr.name) {
282 free(res_all.res_dir.hdr.name);
283 res_all.res_dir.hdr.name = NULL;
285 if (res_all.res_dir.hdr.desc) {
286 free(res_all.res_dir.hdr.desc);
287 res_all.res_dir.hdr.desc = NULL;
292 /* The following code is only executed during pass 1 */
295 size = sizeof(CONRES);
298 size = sizeof(DIRRES);
301 printf(_("Unknown resource type %d\n"), type);
308 res = (URES *)malloc(size);
309 memcpy(res, &res_all, size);
310 if (!res_head[rindex]) {
311 res_head[rindex] = (RES *)res; /* store first entry */
314 for (last=next=res_head[rindex]; next; next=next->next) {
316 if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
317 Emsg2(M_ERROR_TERM, 0,
318 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
319 resources[rindex].name, res->res_dir.hdr.name);
322 last->next = (RES *)res;
323 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
324 res->res_dir.hdr.name);
329 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
331 config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
332 r_first, r_last, resources, res_head);
333 return config->parse_config();