]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed_conf.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / filed / filed_conf.c
1 /*
2  *   Main configuration file parser for Bacula File Daemon (Client)
3  *    some parts may be split into separate files such as
4  *    the schedule configuration (sch_config.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  *     Kern Sibbald, September MM
21  *
22  *   Version $Id$
23  */
24 /*
25    Copyright (C) 2000-2005 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 "filed.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
52 /* Forward referenced subroutines */
53
54
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
58  * scan is complete.
59  */
60 #if defined(HAVE_WIN32) && !defined(HAVE_CYGWIN)
61 extern "C" { // work around visual compiler mangling variables
62     URES res_all;
63     int  res_all_size = sizeof(res_all);
64 }
65 #else
66 URES res_all;
67 int  res_all_size = sizeof(res_all);
68 #endif
69
70 /* Definition of records permitted within each
71  * resource with the routine to process the record
72  * information.
73  */
74
75 /* Client or File daemon "Global" resources */
76 static RES_ITEM cli_items[] = {
77    {"name",        store_name,  ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
78    {"description", store_str,   ITEM(res_client.hdr.desc), 0, 0, 0},
79    {"fdport",      store_addresses_port,    ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
80    {"fdaddress",   store_addresses_address, ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
81    {"fdaddresses", store_addresses,         ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
82
83    {"workingdirectory",  store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0},
84    {"piddirectory",  store_dir,     ITEM(res_client.pid_directory),     0, ITEM_REQUIRED, 0},
85    {"subsysdirectory",  store_dir,  ITEM(res_client.subsys_directory),  0, 0, 0},
86    {"scriptsdirectory",  store_dir,  ITEM(res_client.scripts_directory),  0, 0, 0},
87    {"maximumconcurrentjobs", store_pint,  ITEM(res_client.MaxConcurrentJobs), 0, ITEM_DEFAULT, 10},
88    {"messages",      store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
89    {"heartbeatinterval", store_time, ITEM(res_client.heartbeat_interval), 0, ITEM_DEFAULT, 0},
90    {"sdconnecttimeout", store_time,ITEM(res_client.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
91    {"maximumnetworkbuffersize", store_pint, ITEM(res_client.max_network_buffer_size), 0, 0, 0},
92    {"tlsenable",            store_yesno,     ITEM(res_client.tls_enable), 1, 0, 0},
93    {"tlsrequire",           store_yesno,     ITEM(res_client.tls_require), 1, 0, 0},
94    {"tlscacertificatefile", store_dir,       ITEM(res_client.tls_ca_certfile), 0, 0, 0},
95    {"tlscacertificatedir",  store_dir,       ITEM(res_client.tls_ca_certdir), 0, 0, 0},
96    {"tlscertificate",       store_dir,       ITEM(res_client.tls_certfile), 0, 0, 0},
97    {"tlskey",               store_dir,       ITEM(res_client.tls_keyfile), 0, 0, 0},
98    {NULL, NULL, NULL, 0, 0, 0}
99 };
100
101 /* Directors that can use our services */
102 static RES_ITEM dir_items[] = {
103    {"name",        store_name,     ITEM(res_dir.hdr.name),  0, ITEM_REQUIRED, 0},
104    {"description", store_str,      ITEM(res_dir.hdr.desc),  0, 0, 0},
105    {"password",    store_password, ITEM(res_dir.password),  0, ITEM_REQUIRED, 0},
106    {"address",     store_str,      ITEM(res_dir.address),   0, 0, 0},
107    {"monitor",     store_yesno,    ITEM(res_dir.monitor),   1, ITEM_DEFAULT, 0},
108    {"tlsenable",            store_yesno,     ITEM(res_dir.tls_enable), 1, 0, 0},
109    {"tlsrequire",           store_yesno,     ITEM(res_dir.tls_require), 1, 0, 0},
110    {"tlsverifypeer",        store_yesno,     ITEM(res_dir.tls_verify_peer), 1, ITEM_DEFAULT, 1},
111    {"tlscacertificatefile", store_dir,       ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
112    {"tlscacertificatedir",  store_dir,       ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
113    {"tlscertificate",       store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
114    {"tlskey",               store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
115    {"tlsdhfile",            store_dir,       ITEM(res_dir.tls_dhfile), 0, 0, 0},
116    {"tlsallowedcn",         store_alist_str, ITEM(res_dir.tls_allowed_cns), 0, 0, 0},
117    {NULL, NULL, NULL, 0, 0, 0}
118 };
119
120 /* Message resource */
121 extern RES_ITEM msgs_items[];
122
123 /*
124  * This is the master resource definition.
125  * It must have one item for each of the resources.
126  */
127 RES_TABLE resources[] = {
128    {"director",      dir_items,   R_DIRECTOR},
129    {"filedaemon",    cli_items,   R_CLIENT},
130    {"client",        cli_items,   R_CLIENT},     /* alias for filedaemon */
131    {"messages",      msgs_items,  R_MSGS},
132    {NULL,            NULL,        0}
133 };
134
135
136 /* Dump contents of resource */
137 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
138 {
139    URES *res = (URES *)reshdr;
140    int recurse = 1;
141
142    if (res == NULL) {
143       sendit(sock, "No record for %d %s\n", type, res_to_str(type));
144       return;
145    }
146    if (type < 0) {                    /* no recursion */
147       type = - type;
148       recurse = 0;
149    }
150    switch (type) {
151       case R_DIRECTOR:
152          sendit(sock, "Director: name=%s password=%s\n", reshdr->name,
153                  res->res_dir.password);
154          break;
155       case R_CLIENT:
156          sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
157                  get_first_port_host_order(res->res_client.FDaddrs));
158          break;
159       case R_MSGS:
160          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
161          if (res->res_msgs.mail_cmd)
162             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
163          if (res->res_msgs.operator_cmd)
164             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
165          break;
166       default:
167          sendit(sock, "Unknown resource type %d\n", type);
168    }
169    if (recurse && res->res_dir.hdr.next)
170       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
171 }
172
173 /*
174  * Free memory of resource.
175  * NB, we don't need to worry about freeing any references
176  * to other resources as they will be freed when that
177  * resource chain is traversed.  Mainly we worry about freeing
178  * allocated strings (names).
179  */
180 void free_resource(RES *sres, int type)
181 {
182    RES *nres;
183    URES *res = (URES *)sres;
184
185    if (res == NULL) {
186       return;
187    }
188
189    /* common stuff -- free the resource name */
190    nres = (RES *)res->res_dir.hdr.next;
191    if (res->res_dir.hdr.name) {
192       free(res->res_dir.hdr.name);
193    }
194    if (res->res_dir.hdr.desc) {
195       free(res->res_dir.hdr.desc);
196    }
197    switch (type) {
198    case R_DIRECTOR:
199       if (res->res_dir.password) {
200          free(res->res_dir.password);
201       }
202       if (res->res_dir.address) {
203          free(res->res_dir.address);
204       }
205       if (res->res_dir.tls_ctx) { 
206          free_tls_context(res->res_dir.tls_ctx);
207       }
208       if (res->res_dir.tls_ca_certfile) {
209          free(res->res_dir.tls_ca_certfile);
210       }
211       if (res->res_dir.tls_ca_certdir) {
212          free(res->res_dir.tls_ca_certdir);
213       }
214       if (res->res_dir.tls_certfile) {
215          free(res->res_dir.tls_certfile);
216       }
217       if (res->res_dir.tls_keyfile) {
218          free(res->res_dir.tls_keyfile);
219       }
220       if (res->res_dir.tls_dhfile) {
221          free(res->res_dir.tls_dhfile);
222       }
223       if (res->res_dir.tls_allowed_cns) {
224          delete res->res_dir.tls_allowed_cns;
225       }
226       break;
227    case R_CLIENT:
228       if (res->res_client.working_directory) {
229          free(res->res_client.working_directory);
230       }
231       if (res->res_client.pid_directory) {
232          free(res->res_client.pid_directory);
233       }
234       if (res->res_client.subsys_directory) {
235          free(res->res_client.subsys_directory);
236       }
237       if (res->res_client.scripts_directory) {
238          free(res->res_client.scripts_directory);
239       }
240       if (res->res_client.FDaddrs) {
241          free_addresses(res->res_client.FDaddrs);
242       }
243       if (res->res_client.tls_ctx) { 
244          free_tls_context(res->res_client.tls_ctx);
245       }
246       if (res->res_client.tls_ca_certfile) {
247          free(res->res_client.tls_ca_certfile);
248       }
249       if (res->res_client.tls_ca_certdir) {
250          free(res->res_client.tls_ca_certdir);
251       }
252       if (res->res_client.tls_certfile) {
253          free(res->res_client.tls_certfile);
254       }
255       if (res->res_client.tls_keyfile) {
256          free(res->res_client.tls_keyfile);
257       }
258       break;
259    case R_MSGS:
260       if (res->res_msgs.mail_cmd)
261          free(res->res_msgs.mail_cmd);
262       if (res->res_msgs.operator_cmd)
263          free(res->res_msgs.operator_cmd);
264       free_msgs_res((MSGS *)res);  /* free message resource */
265       res = NULL;
266       break;
267    default:
268       printf(_("Unknown resource type %d\n"), type);
269    }
270    /* Common stuff again -- free the resource, recurse to next one */
271    if (res) {
272       free(res);
273    }
274    if (nres) {
275       free_resource(nres, type);
276    }
277 }
278
279 /* Save the new resource by chaining it into the head list for
280  * the resource. If this is pass 2, we update any resource
281  * pointers (currently only in the Job resource).
282  */
283 void save_resource(int type, RES_ITEM *items, int pass)
284 {
285    URES *res;
286    int rindex = type - r_first;
287    int i, size;
288    int error = 0;
289
290    /*
291     * Ensure that all required items are present
292     */
293    for (i=0; items[i].name; i++) {
294       if (items[i].flags & ITEM_REQUIRED) {
295             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
296                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
297                  items[i].name, resources[rindex]);
298              }
299       }
300    }
301
302    /* During pass 2, we looked up pointers to all the resources
303     * referrenced in the current resource, , now we
304     * must copy their address from the static record to the allocated
305     * record.
306     */
307    if (pass == 2) {
308       switch (type) {
309          /* Resources not containing a resource */
310          case R_MSGS:
311             break;
312
313          /* Resources containing another resource */
314          case R_DIRECTOR:
315             if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
316                Emsg1(M_ABORT, 0, _("Cannot find Director resource %s\n"), res_all.res_dir.hdr.name);
317             }
318             res->res_dir.tls_allowed_cns = res_all.res_dir.tls_allowed_cns;
319             break;
320          case R_CLIENT:
321             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
322                Emsg1(M_ABORT, 0, _("Cannot find Client resource %s\n"), res_all.res_dir.hdr.name);
323             }
324             res->res_client.messages = res_all.res_client.messages;
325             break;
326          default:
327             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
328             error = 1;
329             break;
330       }
331       /* Note, the resoure name was already saved during pass 1,
332        * so here, we can just release it.
333        */
334       if (res_all.res_dir.hdr.name) {
335          free(res_all.res_dir.hdr.name);
336          res_all.res_dir.hdr.name = NULL;
337       }
338       if (res_all.res_dir.hdr.desc) {
339          free(res_all.res_dir.hdr.desc);
340          res_all.res_dir.hdr.desc = NULL;
341       }
342       return;
343    }
344
345    /* The following code is only executed on pass 1 */
346    switch (type) {
347       case R_DIRECTOR:
348          size = sizeof(DIRRES);
349          break;
350       case R_CLIENT:
351          size = sizeof(CLIENT);
352          break;
353       case R_MSGS:
354          size = sizeof(MSGS);
355          break;
356       default:
357          printf(_("Unknown resource type %d\n"), type);
358          error = 1;
359          size = 1;
360          break;
361    }
362    /* Common */
363    if (!error) {
364       res = (URES *)malloc(size);
365       memcpy(res, &res_all, size);
366       if (!res_head[rindex]) {
367          res_head[rindex] = (RES *)res; /* store first entry */
368       } else {
369          RES *next;
370          /* Add new res to end of chain */
371          for (next=res_head[rindex]; next->next; next=next->next) {
372             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
373                Emsg2(M_ERROR_TERM, 0,
374                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
375                   resources[rindex].name, res->res_dir.hdr.name);
376             }
377          }
378          next->next = (RES *)res;
379          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
380                res->res_dir.hdr.name);
381       }
382    }
383 }