2 * Main configuration file parser for Bacula Tray Monitor.
4 * Adapted from dird_conf.c
6 * Note, the configuration file parser consists of three parts
8 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
10 * 2. The generic config scanner in lib/parse_config.c and
12 * These files contain the parser code, some utility
13 * routines, and the common store routines (name, int,
16 * 3. The daemon specific file, which contains the Resource
17 * definitions as well as any specific store routines
18 * for the resource records.
20 * Nicolas Boichat, August MMIV
25 Copyright (C) 2004 Kern Sibbald and John Walker
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.
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.
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,
45 #include "tray_conf.h"
47 /* Define the first and last resource ID record
48 * types. Note, these should be unique for each
49 * daemon though not a requirement.
51 int r_first = R_FIRST;
53 static RES *sres_head[R_LAST - R_FIRST + 1];
54 RES **res_head = sres_head;
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 int res_all_size = sizeof(res_all);
65 /* Definition of records permitted within each
66 * resource with the routine to process the record
67 * information. NOTE! quoted names must be in lower case.
72 * name handler value code flags default_value
74 static RES_ITEM mon_items[] = {
75 {"name", store_name, ITEM(res_monitor.hdr.name), 0, ITEM_REQUIRED, 0},
76 {"description", store_str, ITEM(res_monitor.hdr.desc), 0, 0, 0},
77 {"requiressl", store_bit, ITEM(res_monitor.require_ssl), 1, ITEM_DEFAULT, 0},
78 {"password", store_password, ITEM(res_monitor.password), 0, ITEM_REQUIRED, 0},
79 {"refreshinterval", store_time,ITEM(res_monitor.RefreshInterval), 0, ITEM_DEFAULT, 5},
80 {"fdconnecttimeout", store_time,ITEM(res_monitor.FDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
81 {"sdconnecttimeout", store_time,ITEM(res_monitor.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
82 {NULL, NULL, NULL, 0, 0, 0}
85 /* Director's that we can contact */
86 static RES_ITEM dir_items[] = {
87 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
88 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
89 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
90 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
91 {"enablessl", store_bit, ITEM(res_dir.enable_ssl), 1, ITEM_DEFAULT, 0},
92 {NULL, NULL, NULL, 0, 0, 0}
96 * Client or File daemon resource
98 * name handler value code flags default_value
101 static RES_ITEM cli_items[] = {
102 {"name", store_name, ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
103 {"description", store_str, ITEM(res_client.hdr.desc), 0, 0, 0},
104 {"address", store_str, ITEM(res_client.address), 0, ITEM_REQUIRED, 0},
105 {"fdport", store_pint, ITEM(res_client.FDport), 0, ITEM_DEFAULT, 9102},
106 {"password", store_password, ITEM(res_client.password), 0, ITEM_REQUIRED, 0},
107 {"enablessl", store_bit, ITEM(res_client.enable_ssl), 1, ITEM_DEFAULT, 0},
108 {NULL, NULL, NULL, 0, 0, 0}
111 /* Storage daemon resource
113 * name handler value code flags default_value
115 static RES_ITEM store_items[] = {
116 {"name", store_name, ITEM(res_store.hdr.name), 0, ITEM_REQUIRED, 0},
117 {"description", store_str, ITEM(res_store.hdr.desc), 0, 0, 0},
118 {"sdport", store_pint, ITEM(res_store.SDport), 0, ITEM_DEFAULT, 9103},
119 {"address", store_str, ITEM(res_store.address), 0, ITEM_REQUIRED, 0},
120 {"sdaddress", store_str, ITEM(res_store.address), 0, 0, 0},
121 {"password", store_password, ITEM(res_store.password), 0, ITEM_REQUIRED, 0},
122 {"sdpassword", store_password, ITEM(res_store.password), 0, 0, 0},
123 {"enablessl", store_bit, ITEM(res_store.enable_ssl), 1, ITEM_DEFAULT, 0},
124 {NULL, NULL, NULL, 0, 0, 0}
128 * This is the master resource definition.
129 * It must have one item for each of the resources.
131 * NOTE!!! keep it in the same order as the R_codes
132 * or eliminate all resources[rindex].name
134 * name items rcode res_head
136 RES_TABLE resources[] = {
137 {"monitor", mon_items, R_MONITOR},
138 {"director", dir_items, R_DIRECTOR},
139 {"client", cli_items, R_CLIENT},
140 {"storage", store_items, R_STORAGE},
144 /* Dump contents of resource */
145 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
147 URES *res = (URES *)reshdr;
149 char ed1[100], ed2[100];
152 sendit(sock, _("No %s resource defined\n"), res_to_str(type));
155 if (type < 0) { /* no recursion */
161 sendit(sock, _("Monitor: name=%s FDtimeout=%s SDtimeout=%s\n"),
163 edit_uint64(res->res_monitor.FDConnectTimeout, ed1),
164 edit_uint64(res->res_monitor.SDConnectTimeout, ed2));
167 sendit(sock, _("Director: name=%s address=%s FDport=%d\n"),
168 res->res_dir.hdr.name, res->res_dir.address, res->res_dir.DIRport);
171 sendit(sock, _("Client: name=%s address=%s FDport=%d\n"),
172 res->res_client.hdr.name, res->res_client.address, res->res_client.FDport);
175 sendit(sock, _("Storage: name=%s address=%s SDport=%d\n"),
176 res->res_store.hdr.name, res->res_store.address, res->res_store.SDport);
179 sendit(sock, _("Unknown resource type %d in dump_resource.\n"), type);
182 if (recurse && res->res_monitor.hdr.next) {
183 dump_resource(type, res->res_monitor.hdr.next, sendit, sock);
189 * Free memory of resource -- called when daemon terminates.
190 * NB, we don't need to worry about freeing any references
191 * to other resources as they will be freed when that
192 * resource chain is traversed. Mainly we worry about freeing
193 * allocated strings (names).
195 void free_resource(RES *sres, int type)
197 RES *nres; /* next resource if linked */
198 URES *res = (URES *)sres;
203 /* common stuff -- free the resource name and description */
204 nres = (RES *)res->res_monitor.hdr.next;
205 if (res->res_monitor.hdr.name) {
206 free(res->res_monitor.hdr.name);
208 if (res->res_monitor.hdr.desc) {
209 free(res->res_monitor.hdr.desc);
216 if (res->res_client.address) {
217 free(res->res_client.address);
219 if (res->res_client.password) {
220 free(res->res_client.password);
224 if (res->res_store.address) {
225 free(res->res_store.address);
227 if (res->res_store.password) {
228 free(res->res_store.password);
232 printf(_("Unknown resource type %d in free_resource.\n"), type);
234 /* Common stuff again -- free the resource, recurse to next one */
239 free_resource(nres, type);
244 * Save the new resource by chaining it into the head list for
245 * the resource. If this is pass 2, we update any resource
246 * pointers because they may not have been defined until
249 void save_resource(int type, RES_ITEM *items, int pass)
252 int rindex = type - r_first;
257 * Ensure that all required items are present
259 for (i=0; items[i].name; i++) {
260 if (items[i].flags & ITEM_REQUIRED) {
261 if (!bit_is_set(i, res_all.res_monitor.hdr.item_present)) {
262 Emsg2(M_ERROR_TERM, 0, _("%s item is required in %s resource, but not found.\n"),
263 items[i].name, resources[rindex]);
266 /* If this triggers, take a look at lib/parse_conf.h */
267 if (i >= MAX_RES_ITEMS) {
268 Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
273 * During pass 2 in each "store" routine, we looked up pointers
274 * to all the resources referrenced in the current resource, now we
275 * must copy their addresses from the static record to the allocated
280 /* Resources not containing a resource */
287 Emsg1(M_ERROR, 0, _("Unknown resource type %d in save_resource.\n"), type);
291 /* Note, the resource name was already saved during pass 1,
292 * so here, we can just release it.
294 if (res_all.res_monitor.hdr.name) {
295 free(res_all.res_monitor.hdr.name);
296 res_all.res_monitor.hdr.name = NULL;
298 if (res_all.res_monitor.hdr.desc) {
299 free(res_all.res_monitor.hdr.desc);
300 res_all.res_monitor.hdr.desc = NULL;
306 * The following code is only executed during pass 1
310 size = sizeof(MONITOR);
313 size = sizeof(DIRRES);
316 size = sizeof(CLIENT);
319 size = sizeof(STORE);
322 printf(_("Unknown resource type %d in save_resrouce.\n"), type);
329 res = (URES *)malloc(size);
330 memcpy(res, &res_all, size);
331 if (!res_head[rindex]) {
332 res_head[rindex] = (RES *)res; /* store first entry */
333 Dmsg3(900, "Inserting first %s res: %s index=%d\n", res_to_str(type),
334 res->res_monitor.hdr.name, rindex);
337 /* Add new res to end of chain */
338 for (next=res_head[rindex]; next->next; next=next->next) {
339 if (strcmp(next->name, res->res_monitor.hdr.name) == 0) {
340 Emsg2(M_ERROR_TERM, 0,
341 _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
342 resources[rindex].name, res->res_monitor.hdr.name);
345 next->next = (RES *)res;
346 Dmsg4(900, "Inserting %s res: %s index=%d pass=%d\n", res_to_str(type),
347 res->res_monitor.hdr.name, rindex, pass);