2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 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;
52 /* Forward referenced subroutines */
55 /* We build the current resource here as we are
56 * scanning the resource configuration definition,
57 * then move it to allocated memory when the resource
61 extern "C" { // work around visual compiler mangling variables
67 int32_t res_all_size = sizeof(res_all);
69 /* Definition of records permitted within each
70 * resource with the routine to process the record
74 /* Console "globals" */
75 static RES_ITEM cons_items[] = {
76 {"Name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
77 {"Description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
78 {"RCFile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
79 {"HistoryFile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
80 {"Password", store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
81 {"TlsAuthenticate",store_bool, ITEM(res_cons.tls_authenticate), 0, 0, 0},
82 {"TlsEnable", store_bool, ITEM(res_cons.tls_enable), 0, 0, 0},
83 {"TlsRequire", store_bool, ITEM(res_cons.tls_require), 0, 0, 0},
84 {"TlsCaCertificateFile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
85 {"TlsCaCertificateDir", store_dir, ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
86 {"TlsCertificate", store_dir, ITEM(res_cons.tls_certfile), 0, 0, 0},
87 {"TlsKey", store_dir, ITEM(res_cons.tls_keyfile), 0, 0, 0},
88 {"Director", store_str, ITEM(res_cons.director), 0, 0, 0},
89 {"HeartbeatInterval", store_time, ITEM(res_cons.heartbeat_interval), 0, ITEM_DEFAULT, 5 * 60},
90 {"CommCompression", store_bool, ITEM(res_cons.comm_compression), 0, ITEM_DEFAULT, true},
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 *rres, void sendit(void *sock, const char *fmt, ...), void *sock)
128 URES *res = (URES *)rres;
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"), rres->name,
142 res->res_cons.rc_file, res->res_cons.hist_file);
145 printf(_("Director: name=%s address=%s DIRport=%d\n"), rres->name,
146 res->res_dir.address, res->res_dir.DIRport);
149 printf(_("Unknown resource type %d\n"), type);
152 rres = GetNextRes(type, rres);
153 if (recurse && rres) {
154 dump_resource(type, rres, sendit, sock);
159 * Free memory of resource.
160 * NB, we don't need to worry about freeing any references
161 * to other resources as they will be freed when that
162 * resource chain is traversed. Mainly we worry about freeing
163 * allocated strings (names).
165 void free_resource(RES *rres, int type)
167 URES *res = (URES *)rres;
173 /* common stuff -- free the resource name */
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);
207 if (res->res_cons.password) {
208 free(res->res_cons.password);
212 if (res->res_dir.address) {
213 free(res->res_dir.address);
215 if (res->res_dir.tls_ctx) {
216 free_tls_context(res->res_dir.tls_ctx);
218 if (res->res_dir.tls_ca_certfile) {
219 free(res->res_dir.tls_ca_certfile);
221 if (res->res_dir.tls_ca_certdir) {
222 free(res->res_dir.tls_ca_certdir);
224 if (res->res_dir.tls_certfile) {
225 free(res->res_dir.tls_certfile);
227 if (res->res_dir.tls_keyfile) {
228 free(res->res_dir.tls_keyfile);
230 if (res->res_dir.password) {
231 free(res->res_dir.password);
235 printf(_("Unknown resource type %d\n"), type);
237 /* Common stuff again -- free the resource, recurse to next one */
241 /* Save the new resource by chaining it into the head list for
242 * the resource. If this is pass 2, we update any resource
243 * pointers (currently only in the Job resource).
245 bool save_resource(CONFIG *config, int type, RES_ITEM *items, int pass)
247 int rindex = type - r_first;
252 * Ensure that all required items are present
254 for (i=0; items[i].name; i++) {
255 if (items[i].flags & ITEM_REQUIRED) {
256 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
257 Mmsg(config->m_errmsg, _("\"%s\" directive is required in \"%s\" resource, but not found.\n"),
258 items[i].name, resources[rindex].name);
264 /* During pass 2, we looked up pointers to all the resources
265 * referrenced in the current resource, , now we
266 * must copy their address from the static record to the allocated
271 /* Resources not containing a resource */
277 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
281 /* Note, the resoure name was already saved during pass 1,
282 * so here, we can just release it.
284 if (res_all.res_dir.hdr.name) {
285 free(res_all.res_dir.hdr.name);
286 res_all.res_dir.hdr.name = NULL;
288 if (res_all.res_dir.hdr.desc) {
289 free(res_all.res_dir.hdr.desc);
290 res_all.res_dir.hdr.desc = NULL;
295 /* The following code is only executed during pass 1 */
298 size = sizeof(CONRES);
301 size = sizeof(DIRRES);
304 printf(_("Unknown resource type %d\n"), type);
311 if (!config->insert_res(rindex, size)) {
318 bool parse_cons_config(CONFIG *config, const char *configfile, int exit_code)
320 config->init(configfile, NULL, exit_code, (void *)&res_all, res_all_size,
321 r_first, r_last, resources, &res_head);
322 return config->parse_config();