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