]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tray-monitor/tray_conf.c
Replace explicit checks for "/" with calls to IsPathSeparator, strchr with first_path...
[bacula/bacula] / bacula / src / tray-monitor / tray_conf.c
1 /*
2 *   Main configuration file parser for Bacula Tray Monitor.
3 *
4 *   Adapted from dird_conf.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 *     Nicolas Boichat, August MMIV
21 *
22 *     Version $Id$
23 */
24 /*
25    Copyright (C) 2004-2006 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
29    version 2 as amended with additional clauses defined in the
30    file LICENSE in the main source directory.
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 
35    the file LICENSE for additional details.
36
37  */
38
39 #include "bacula.h"
40 #include "tray_conf.h"
41
42 /* Define the first and last resource ID record
43 * types. Note, these should be unique for each
44 * daemon though not a requirement.
45 */
46 int r_first = R_FIRST;
47 int r_last  = R_LAST;
48 static RES *sres_head[R_LAST - R_FIRST + 1];
49 RES **res_head = sres_head;
50
51 /* We build the current resource here as we are
52 * scanning the resource configuration definition,
53 * then move it to allocated memory when the resource
54 * scan is complete.
55 */
56 URES res_all;
57 int  res_all_size = sizeof(res_all);
58
59
60 /* Definition of records permitted within each
61 * resource with the routine to process the record
62 * information.  NOTE! quoted names must be in lower case.
63 */
64 /*
65 *    Monitor Resource
66 *
67 *   name           handler     value                 code flags    default_value
68 */
69 static RES_ITEM mon_items[] = {
70    {"name",        store_name,     ITEM(res_monitor.hdr.name), 0, ITEM_REQUIRED, 0},
71    {"description", store_str,      ITEM(res_monitor.hdr.desc), 0, 0, 0},
72    {"requiressl",  store_bit,    ITEM(res_monitor.require_ssl), 1, ITEM_DEFAULT, 0},
73    {"password",    store_password, ITEM(res_monitor.password), 0, ITEM_REQUIRED, 0},
74    {"refreshinterval",  store_time,ITEM(res_monitor.RefreshInterval),  0, ITEM_DEFAULT, 5},
75    {"fdconnecttimeout", store_time,ITEM(res_monitor.FDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
76    {"sdconnecttimeout", store_time,ITEM(res_monitor.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
77    {NULL, NULL, {0}, 0, 0, 0}
78 };
79
80 /*  Director's that we can contact */
81 static RES_ITEM dir_items[] = {
82    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
83    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
84    {"dirport",     store_int,      ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
85    {"address",     store_str,      ITEM(res_dir.address),  0, 0, 0},
86    {"enablessl",   store_bit,    ITEM(res_dir.enable_ssl), 1, ITEM_DEFAULT, 0},
87    {NULL, NULL, {0}, 0, 0, 0}
88 };
89
90 /*
91 *    Client or File daemon resource
92 *
93 *   name           handler     value                 code flags    default_value
94 */
95
96 static RES_ITEM cli_items[] = {
97    {"name",     store_name,       ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
98    {"description", store_str,     ITEM(res_client.hdr.desc), 0, 0, 0},
99    {"address",  store_str,        ITEM(res_client.address),  0, ITEM_REQUIRED, 0},
100    {"fdport",   store_pint,       ITEM(res_client.FDport),   0, ITEM_DEFAULT, 9102},
101    {"password", store_password,   ITEM(res_client.password), 0, ITEM_REQUIRED, 0},
102    {"enablessl", store_bit,     ITEM(res_client.enable_ssl), 1, ITEM_DEFAULT, 0},
103    {NULL, NULL, {0}, 0, 0, 0}
104 };
105
106 /* Storage daemon resource
107 *
108 *   name           handler     value                 code flags    default_value
109 */
110 static RES_ITEM store_items[] = {
111    {"name",        store_name,     ITEM(res_store.hdr.name),   0, ITEM_REQUIRED, 0},
112    {"description", store_str,      ITEM(res_store.hdr.desc),   0, 0, 0},
113    {"sdport",      store_pint,     ITEM(res_store.SDport),     0, ITEM_DEFAULT, 9103},
114    {"address",     store_str,      ITEM(res_store.address),    0, ITEM_REQUIRED, 0},
115    {"sdaddress",   store_str,      ITEM(res_store.address),    0, 0, 0},
116    {"password",    store_password, ITEM(res_store.password),   0, ITEM_REQUIRED, 0},
117    {"sdpassword",  store_password, ITEM(res_store.password),   0, 0, 0},
118    {"enablessl",   store_bit,    ITEM(res_store.enable_ssl),  1, ITEM_DEFAULT, 0},
119    {NULL, NULL, {0}, 0, 0, 0}
120 };
121
122 /*
123 * This is the master resource definition.
124 * It must have one item for each of the resources.
125 *
126 *  NOTE!!! keep it in the same order as the R_codes
127 *    or eliminate all resources[rindex].name
128 *
129 *  name      items        rcode        res_head
130 */
131 RES_TABLE resources[] = {
132    {"monitor",      mon_items,    R_MONITOR},
133    {"director",     dir_items,    R_DIRECTOR},
134    {"client",       cli_items,    R_CLIENT},
135    {"storage",      store_items,  R_STORAGE},
136    {NULL,           NULL,         0}
137 };
138
139 /* Dump contents of resource */
140 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
141 {
142    URES *res = (URES *)reshdr;
143    bool recurse = true;
144    char ed1[100], ed2[100];
145
146    if (res == NULL) {
147       sendit(sock, _("No %s resource defined\n"), res_to_str(type));
148       return;
149    }
150    if (type < 0) {                    /* no recursion */
151       type = - type;
152       recurse = false;
153    }
154    switch (type) {
155    case R_MONITOR:
156       sendit(sock, _("Monitor: name=%s FDtimeout=%s SDtimeout=%s\n"),
157    reshdr->name,
158    edit_uint64(res->res_monitor.FDConnectTimeout, ed1),
159    edit_uint64(res->res_monitor.SDConnectTimeout, ed2));
160       break;
161    case R_DIRECTOR:
162       sendit(sock, _("Director: name=%s address=%s FDport=%d\n"),
163    res->res_dir.hdr.name, res->res_dir.address, res->res_dir.DIRport);
164       break;
165    case R_CLIENT:
166       sendit(sock, _("Client: name=%s address=%s FDport=%d\n"),
167    res->res_client.hdr.name, res->res_client.address, res->res_client.FDport);
168       break;
169    case R_STORAGE:
170       sendit(sock, _("Storage: name=%s address=%s SDport=%d\n"),
171    res->res_store.hdr.name, res->res_store.address, res->res_store.SDport);
172       break;
173    default:
174       sendit(sock, _("Unknown resource type %d in dump_resource.\n"), type);
175       break;
176    }
177    if (recurse && res->res_monitor.hdr.next) {
178       dump_resource(type, res->res_monitor.hdr.next, sendit, sock);
179    }
180 }
181
182
183 /*
184 * Free memory of resource -- called when daemon terminates.
185 * NB, we don't need to worry about freeing any references
186 * to other resources as they will be freed when that
187 * resource chain is traversed.  Mainly we worry about freeing
188 * allocated strings (names).
189 */
190 void free_resource(RES *sres, int type)
191 {
192    RES *nres;                         /* next resource if linked */
193    URES *res = (URES *)sres;
194
195    if (res == NULL)
196       return;
197
198    /* common stuff -- free the resource name and description */
199    nres = (RES *)res->res_monitor.hdr.next;
200    if (res->res_monitor.hdr.name) {
201       free(res->res_monitor.hdr.name);
202    }
203    if (res->res_monitor.hdr.desc) {
204       free(res->res_monitor.hdr.desc);
205    }
206
207    switch (type) {
208    case R_MONITOR:
209       break;
210    case R_CLIENT:
211       if (res->res_client.address) {
212    free(res->res_client.address);
213       }
214       if (res->res_client.password) {
215    free(res->res_client.password);
216       }
217       break;
218    case R_STORAGE:
219       if (res->res_store.address) {
220    free(res->res_store.address);
221       }
222       if (res->res_store.password) {
223    free(res->res_store.password);
224       }
225       break;
226    default:
227       printf(_("Unknown resource type %d in free_resource.\n"), type);
228    }
229    /* Common stuff again -- free the resource, recurse to next one */
230    if (res) {
231       free(res);
232    }
233    if (nres) {
234       free_resource(nres, type);
235    }
236 }
237
238 /*
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 because they may not have been defined until
242 * later in pass 1.
243 */
244 void save_resource(int type, RES_ITEM *items, int pass)
245 {
246    URES *res;
247    int rindex = type - r_first;
248    int i, size;
249    int error = 0;
250
251    /*
252    * Ensure that all required items are present
253    */
254    for (i=0; items[i].name; i++) {
255       if (items[i].flags & ITEM_REQUIRED) {
256          if (!bit_is_set(i, res_all.res_monitor.hdr.item_present)) {
257                Emsg2(M_ERROR_TERM, 0, _("%s item is required in %s resource, but not found.\n"),
258                   items[i].name, resources[rindex]);
259          }
260       }
261       /* If this triggers, take a look at lib/parse_conf.h */
262       if (i >= MAX_RES_ITEMS) {
263          Emsg1(M_ERROR_TERM, 0, _("Too many items in %s resource\n"), resources[rindex]);
264       }
265    }
266
267    /*
268    * During pass 2 in each "store" routine, we looked up pointers
269    * to all the resources referrenced in the current resource, now we
270    * must copy their addresses from the static record to the allocated
271    * record.
272    */
273    if (pass == 2) {
274       switch (type) {
275       /* Resources not containing a resource */
276       case R_MONITOR:
277       case R_CLIENT:
278       case R_STORAGE:
279       case R_DIRECTOR:
280          break;
281       default:
282          Emsg1(M_ERROR, 0, _("Unknown resource type %d in save_resource.\n"), type);
283          error = 1;
284          break;
285       }
286       /* Note, the resource name was already saved during pass 1,
287       * so here, we can just release it.
288       */
289       if (res_all.res_monitor.hdr.name) {
290          free(res_all.res_monitor.hdr.name);
291          res_all.res_monitor.hdr.name = NULL;
292       }
293       if (res_all.res_monitor.hdr.desc) {
294          free(res_all.res_monitor.hdr.desc);
295          res_all.res_monitor.hdr.desc = NULL;
296       }
297       return;
298    }
299
300    /*
301    * The following code is only executed during pass 1
302    */
303    switch (type) {
304    case R_MONITOR:
305       size = sizeof(MONITOR);
306       break;
307    case R_DIRECTOR:
308       size = sizeof(DIRRES);
309       break;
310    case R_CLIENT:
311       size = sizeof(CLIENT);
312       break;
313    case R_STORAGE:
314       size = sizeof(STORE);
315       break;
316    default:
317       printf(_("Unknown resource type %d in save_resrouce.\n"), type);
318       error = 1;
319       size = 1;
320       break;
321    }
322    /* Common */
323    if (!error) {
324       res = (URES *)malloc(size);
325       memcpy(res, &res_all, size);
326       if (!res_head[rindex]) {
327    res_head[rindex] = (RES *)res; /* store first entry */
328          Dmsg3(900, "Inserting first %s res: %s index=%d\n", res_to_str(type),
329          res->res_monitor.hdr.name, rindex);
330       } else {
331    RES *next;
332    /* Add new res to end of chain */
333    for (next=res_head[rindex]; next->next; next=next->next) {
334       if (strcmp(next->name, res->res_monitor.hdr.name) == 0) {
335          Emsg2(M_ERROR_TERM, 0,
336                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
337          resources[rindex].name, res->res_monitor.hdr.name);
338       }
339    }
340    next->next = (RES *)res;
341          Dmsg4(900, "Inserting %s res: %s index=%d pass=%d\n", res_to_str(type),
342          res->res_monitor.hdr.name, rindex, pass);
343       }
344    }
345 }