]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed_conf.c
Tweaks
[bacula/bacula] / bacula / src / filed / filed_conf.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *   Main configuration file parser for Bacula File Daemon (Client)
30  *    some parts may be split into separate files such as
31  *    the schedule configuration (sch_config.c).
32  *
33  *   Note, the configuration file parser consists of three parts
34  *
35  *   1. The generic lexical scanner in lib/lex.c and lib/lex.h
36  *
37  *   2. The generic config  scanner in lib/parse_config.c and
38  *      lib/parse_config.h.
39  *      These files contain the parser code, some utility
40  *      routines, and the common store routines (name, int,
41  *      string).
42  *
43  *   3. The daemon specific file, which contains the Resource
44  *      definitions as well as any specific store routines
45  *      for the resource records.
46  *
47  *     Kern Sibbald, September MM
48  *
49  *   Version $Id$
50  */
51
52 #include "bacula.h"
53 #include "filed.h"
54
55 /* Define the first and last resource ID record
56  * types. Note, these should be unique for each
57  * daemon though not a requirement.
58  */
59 int r_first = R_FIRST;
60 int r_last  = R_LAST;
61 static RES *sres_head[R_LAST - R_FIRST + 1];
62 RES **res_head = sres_head;
63
64
65 /* Forward referenced subroutines */
66
67
68 /* We build the current resource here as we are
69  * scanning the resource configuration definition,
70  * then move it to allocated memory when the resource
71  * scan is complete.
72  */
73 #if defined(_MSC_VER)
74 extern "C" { // work around visual compiler mangling variables
75     URES res_all;
76 }
77 #else
78 URES res_all;
79 #endif
80 int  res_all_size = sizeof(res_all);
81
82 /* Definition of records permitted within each
83  * resource with the routine to process the record
84  * information.
85  */
86
87 /* Client or File daemon "Global" resources */
88 static RES_ITEM cli_items[] = {
89    {"name",        store_name,  ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
90    {"description", store_str,   ITEM(res_client.hdr.desc), 0, 0, 0},
91    {"fdport",      store_addresses_port,    ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
92    {"fdaddress",   store_addresses_address, ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
93    {"fdaddresses", store_addresses,         ITEM(res_client.FDaddrs),  0, ITEM_DEFAULT, 9102},
94
95    {"workingdirectory",  store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0},
96    {"piddirectory",  store_dir,     ITEM(res_client.pid_directory),     0, ITEM_REQUIRED, 0},
97    {"subsysdirectory",  store_dir,  ITEM(res_client.subsys_directory),  0, 0, 0},
98    {"plugindirectory",  store_dir,  ITEM(res_client.plugin_directory),  0, 0, 0},
99    {"scriptsdirectory", store_dir,  ITEM(res_client.scripts_directory),  0, 0, 0},
100    {"maximumconcurrentjobs", store_pint,  ITEM(res_client.MaxConcurrentJobs), 0, ITEM_DEFAULT, 20},
101    {"messages",      store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
102    {"sdconnecttimeout", store_time,ITEM(res_client.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
103    {"heartbeatinterval", store_time, ITEM(res_client.heartbeat_interval), 0, ITEM_DEFAULT, 0},
104    {"maximumnetworkbuffersize", store_pint, ITEM(res_client.max_network_buffer_size), 0, 0, 0},
105 #ifdef DATA_ENCRYPTION
106    {"pkisignatures",         store_bool,    ITEM(res_client.pki_sign), 0, ITEM_DEFAULT, 0},
107    {"pkiencryption",         store_bool,    ITEM(res_client.pki_encrypt), 0, ITEM_DEFAULT, 0},
108    {"pkikeypair",            store_dir,       ITEM(res_client.pki_keypair_file), 0, 0, 0},
109    {"pkisigner",             store_alist_str, ITEM(res_client.pki_signing_key_files), 0, 0, 0},
110    {"pkimasterkey",          store_alist_str, ITEM(res_client.pki_master_key_files), 0, 0, 0},
111 #endif
112    {"tlsauthenticate",       store_bool,    ITEM(res_client.tls_authenticate),  0, 0, 0},
113    {"tlsenable",             store_bool,    ITEM(res_client.tls_enable),  0, 0, 0},
114    {"tlsrequire",            store_bool,    ITEM(res_client.tls_require), 0, 0, 0},
115    {"tlscacertificatefile",  store_dir,       ITEM(res_client.tls_ca_certfile), 0, 0, 0},
116    {"tlscacertificatedir",   store_dir,       ITEM(res_client.tls_ca_certdir), 0, 0, 0},
117    {"tlscertificate",        store_dir,       ITEM(res_client.tls_certfile), 0, 0, 0},
118    {"tlskey",                store_dir,       ITEM(res_client.tls_keyfile), 0, 0, 0},
119    {NULL, NULL, {0}, 0, 0, 0}
120 };
121
122 /* Directors that can use our services */
123 static RES_ITEM dir_items[] = {
124    {"name",        store_name,     ITEM(res_dir.hdr.name),  0, ITEM_REQUIRED, 0},
125    {"description", store_str,      ITEM(res_dir.hdr.desc),  0, 0, 0},
126    {"password",    store_password, ITEM(res_dir.password),  0, ITEM_REQUIRED, 0},
127    {"address",     store_str,      ITEM(res_dir.address),   0, 0, 0},
128    {"monitor",     store_bool,   ITEM(res_dir.monitor),   0, ITEM_DEFAULT, 0},
129    {"tlsauthenticate",      store_bool,    ITEM(res_dir.tls_authenticate), 0, 0, 0},
130    {"tlsenable",            store_bool,    ITEM(res_dir.tls_enable), 0, 0, 0},
131    {"tlsrequire",           store_bool,    ITEM(res_dir.tls_require), 0, 0, 0},
132    {"tlsverifypeer",        store_bool,    ITEM(res_dir.tls_verify_peer), 0, ITEM_DEFAULT, 1},
133    {"tlscacertificatefile", store_dir,       ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
134    {"tlscacertificatedir",  store_dir,       ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
135    {"tlscertificate",       store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
136    {"tlskey",               store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
137    {"tlsdhfile",            store_dir,       ITEM(res_dir.tls_dhfile), 0, 0, 0},
138    {"tlsallowedcn",         store_alist_str, ITEM(res_dir.tls_allowed_cns), 0, 0, 0},
139    {NULL, NULL, {0}, 0, 0, 0}
140 };
141
142 /* Message resource */
143 extern RES_ITEM msgs_items[];
144
145 /*
146  * This is the master resource definition.
147  * It must have one item for each of the resources.
148  */
149 RES_TABLE resources[] = {
150    {"director",      dir_items,   R_DIRECTOR},
151    {"filedaemon",    cli_items,   R_CLIENT},
152    {"client",        cli_items,   R_CLIENT},     /* alias for filedaemon */
153    {"messages",      msgs_items,  R_MSGS},
154    {NULL,            NULL,        0}
155 };
156
157
158 /* Dump contents of resource */
159 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
160 {
161    URES *res = (URES *)reshdr;
162    int recurse = 1;
163
164    if (res == NULL) {
165       sendit(sock, "No record for %d %s\n", type, res_to_str(type));
166       return;
167    }
168    if (type < 0) {                    /* no recursion */
169       type = - type;
170       recurse = 0;
171    }
172    switch (type) {
173    case R_DIRECTOR:
174       sendit(sock, "Director: name=%s password=%s\n", reshdr->name,
175               res->res_dir.password);
176       break;
177    case R_CLIENT:
178       sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
179               get_first_port_host_order(res->res_client.FDaddrs));
180       break;
181    case R_MSGS:
182       sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
183       if (res->res_msgs.mail_cmd)
184          sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
185       if (res->res_msgs.operator_cmd)
186          sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
187       break;
188    default:
189       sendit(sock, "Unknown resource type %d\n", type);
190    }
191    if (recurse && res->res_dir.hdr.next) {
192       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
193    }
194 }
195
196 /*
197  * Free memory of resource.
198  * NB, we don't need to worry about freeing any references
199  * to other resources as they will be freed when that
200  * resource chain is traversed.  Mainly we worry about freeing
201  * allocated strings (names).
202  */
203 void free_resource(RES *sres, int type)
204 {
205    RES *nres;
206    URES *res = (URES *)sres;
207
208    if (res == NULL) {
209       return;
210    }
211
212    /* common stuff -- free the resource name */
213    nres = (RES *)res->res_dir.hdr.next;
214    if (res->res_dir.hdr.name) {
215       free(res->res_dir.hdr.name);
216    }
217    if (res->res_dir.hdr.desc) {
218       free(res->res_dir.hdr.desc);
219    }
220    switch (type) {
221    case R_DIRECTOR:
222       if (res->res_dir.password) {
223          free(res->res_dir.password);
224       }
225       if (res->res_dir.address) {
226          free(res->res_dir.address);
227       }
228       if (res->res_dir.tls_ctx) { 
229          free_tls_context(res->res_dir.tls_ctx);
230       }
231       if (res->res_dir.tls_ca_certfile) {
232          free(res->res_dir.tls_ca_certfile);
233       }
234       if (res->res_dir.tls_ca_certdir) {
235          free(res->res_dir.tls_ca_certdir);
236       }
237       if (res->res_dir.tls_certfile) {
238          free(res->res_dir.tls_certfile);
239       }
240       if (res->res_dir.tls_keyfile) {
241          free(res->res_dir.tls_keyfile);
242       }
243       if (res->res_dir.tls_dhfile) {
244          free(res->res_dir.tls_dhfile);
245       }
246       if (res->res_dir.tls_allowed_cns) {
247          delete res->res_dir.tls_allowed_cns;
248       }
249       break;
250    case R_CLIENT:
251       if (res->res_client.working_directory) {
252          free(res->res_client.working_directory);
253       }
254       if (res->res_client.pid_directory) {
255          free(res->res_client.pid_directory);
256       }
257       if (res->res_client.subsys_directory) {
258          free(res->res_client.subsys_directory);
259       }
260       if (res->res_client.scripts_directory) {
261          free(res->res_client.scripts_directory);
262       }
263       if (res->res_client.FDaddrs) {
264          free_addresses(res->res_client.FDaddrs);
265       }
266
267       if (res->res_client.pki_keypair_file) { 
268          free(res->res_client.pki_keypair_file);
269       }
270       if (res->res_client.pki_keypair) {
271          crypto_keypair_free(res->res_client.pki_keypair);
272       }
273
274       if (res->res_client.pki_signing_key_files) {
275          delete res->res_client.pki_signing_key_files;
276       }
277       if (res->res_client.pki_signers) {
278          X509_KEYPAIR *keypair;
279          foreach_alist(keypair, res->res_client.pki_signers) {
280             crypto_keypair_free(keypair);
281          }
282          delete res->res_client.pki_signers;
283       }
284
285       if (res->res_client.pki_master_key_files) {
286          delete res->res_client.pki_master_key_files;
287       }
288
289       if (res->res_client.pki_recipients) {
290          X509_KEYPAIR *keypair;
291          foreach_alist(keypair, res->res_client.pki_recipients) {
292             crypto_keypair_free(keypair);
293          }
294          delete res->res_client.pki_recipients;
295       }
296
297       if (res->res_client.tls_ctx) { 
298          free_tls_context(res->res_client.tls_ctx);
299       }
300       if (res->res_client.tls_ca_certfile) {
301          free(res->res_client.tls_ca_certfile);
302       }
303       if (res->res_client.tls_ca_certdir) {
304          free(res->res_client.tls_ca_certdir);
305       }
306       if (res->res_client.tls_certfile) {
307          free(res->res_client.tls_certfile);
308       }
309       if (res->res_client.tls_keyfile) {
310          free(res->res_client.tls_keyfile);
311       }
312       break;
313    case R_MSGS:
314       if (res->res_msgs.mail_cmd)
315          free(res->res_msgs.mail_cmd);
316       if (res->res_msgs.operator_cmd)
317          free(res->res_msgs.operator_cmd);
318       free_msgs_res((MSGS *)res);  /* free message resource */
319       res = NULL;
320       break;
321    default:
322       printf(_("Unknown resource type %d\n"), type);
323    }
324    /* Common stuff again -- free the resource, recurse to next one */
325    if (res) {
326       free(res);
327    }
328    if (nres) {
329       free_resource(nres, type);
330    }
331 }
332
333 /* Save the new resource by chaining it into the head list for
334  * the resource. If this is pass 2, we update any resource
335  * pointers (currently only in the Job resource).
336  */
337 void save_resource(int type, RES_ITEM *items, int pass)
338 {
339    URES *res;
340    int rindex = type - r_first;
341    int i, size;
342    int error = 0;
343
344    /*
345     * Ensure that all required items are present
346     */
347    for (i=0; items[i].name; i++) {
348       if (items[i].flags & ITEM_REQUIRED) {
349             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
350                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
351                  items[i].name, resources[rindex]);
352              }
353       }
354    }
355
356    /* During pass 2, we looked up pointers to all the resources
357     * referrenced in the current resource, , now we
358     * must copy their address from the static record to the allocated
359     * record.
360     */
361    if (pass == 2) {
362       switch (type) {
363          /* Resources not containing a resource */
364          case R_MSGS:
365             break;
366
367          /* Resources containing another resource */
368          case R_DIRECTOR:
369             if ((res = (URES *)GetResWithName(R_DIRECTOR, res_all.res_dir.hdr.name)) == NULL) {
370                Emsg1(M_ABORT, 0, _("Cannot find Director resource %s\n"), res_all.res_dir.hdr.name);
371             }
372             res->res_dir.tls_allowed_cns = res_all.res_dir.tls_allowed_cns;
373             break;
374          case R_CLIENT:
375             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
376                Emsg1(M_ABORT, 0, _("Cannot find Client resource %s\n"), res_all.res_dir.hdr.name);
377             }
378             res->res_client.pki_signing_key_files = res_all.res_client.pki_signing_key_files;
379             res->res_client.pki_master_key_files = res_all.res_client.pki_master_key_files;
380
381             res->res_client.pki_signers = res_all.res_client.pki_signers;
382             res->res_client.pki_recipients = res_all.res_client.pki_recipients;
383
384             res->res_client.messages = res_all.res_client.messages;
385             break;
386          default:
387             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
388             error = 1;
389             break;
390       }
391       /* Note, the resoure name was already saved during pass 1,
392        * so here, we can just release it.
393        */
394       if (res_all.res_dir.hdr.name) {
395          free(res_all.res_dir.hdr.name);
396          res_all.res_dir.hdr.name = NULL;
397       }
398       if (res_all.res_dir.hdr.desc) {
399          free(res_all.res_dir.hdr.desc);
400          res_all.res_dir.hdr.desc = NULL;
401       }
402       return;
403    }
404
405    /* The following code is only executed on pass 1 */
406    switch (type) {
407       case R_DIRECTOR:
408          size = sizeof(DIRRES);
409          break;
410       case R_CLIENT:
411          size = sizeof(CLIENT);
412          break;
413       case R_MSGS:
414          size = sizeof(MSGS);
415          break;
416       default:
417          printf(_("Unknown resource type %d\n"), type);
418          error = 1;
419          size = 1;
420          break;
421    }
422    /* Common */
423    if (!error) {
424       res = (URES *)malloc(size);
425       memcpy(res, &res_all, size);
426       if (!res_head[rindex]) {
427          res_head[rindex] = (RES *)res; /* store first entry */
428       } else {
429          RES *next, *last;
430          /* Add new res to end of chain */
431          for (last=next=res_head[rindex]; next; next=next->next) {
432             last = next;
433             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
434                Emsg2(M_ERROR_TERM, 0,
435                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
436                   resources[rindex].name, res->res_dir.hdr.name);
437             }
438          }
439          last->next = (RES *)res;
440          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
441                res->res_dir.hdr.name);
442       }
443    }
444 }