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