]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
- Integrated TLS network encryption
[bacula/bacula] / bacula / src / console / console_conf.c
1 /*
2  *   Main configuration file parser for Bacula User Agent
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, January MM, September MM
21  */
22
23 /*
24    Copyright (C) 2000-2004 Kern Sibbald and John Walker
25
26    This program is free software; you can redistribute it and/or
27    modify it under the terms of the GNU General Public License
28    as published by the Free Software Foundation; either version 2
29    of the License, or (at your option) any later version.
30
31    This program is distributed in the hope that it will be useful,
32    but WITHOUT ANY WARRANTY; without even the implied warranty of
33    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34    GNU General Public License for more details.
35
36    You should have received a copy of the GNU General Public License
37    along with this program; if not, write to the Free Software
38    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
39  */
40
41 #include "bacula.h"
42 #include "console_conf.h"
43
44 /* Define the first and last resource ID record
45  * types. Note, these should be unique for each
46  * daemon though not a requirement.
47  */
48 int r_first = R_FIRST;
49 int r_last  = R_LAST;
50 static RES *sres_head[R_LAST - R_FIRST + 1];
51 RES **res_head = sres_head;
52
53 /* Forward referenced subroutines */
54
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 #if defined(HAVE_WIN32) && !defined(HAVE_CYGWIN)  && !defined(HAVE_MINGW)
62 extern "C" { // work around visual compiler mangling variables
63     URES res_all;
64     int  res_all_size = sizeof(res_all);
65 }
66 #else
67 URES res_all;
68 int  res_all_size = sizeof(res_all);
69 #endif
70
71 /* Definition of records permitted within each
72  * resource with the routine to process the record
73  * information.
74  */
75
76 /*  Console "globals" */
77 static RES_ITEM cons_items[] = {
78    {"name",           store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
79    {"description",    store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
80    {"rcfile",         store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
81    {"historyfile",    store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
82    {"password",       store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
83 #ifdef HAVE_TLS
84    {"tlsenable",      store_yesno,     ITEM(res_cons.tls_enable), 1, ITEM_DEFAULT, 0},
85    {"tlsrequire",     store_yesno,     ITEM(res_cons.tls_require), 1, ITEM_DEFAULT, 0},
86    {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
87    {"tlscacertificatedir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
88    {"tlscertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
89    {"tlskey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
90 #endif
91    {NULL, NULL, NULL, 0, 0, 0}
92 };
93
94
95 /*  Director's that we can contact */
96 static RES_ITEM dir_items[] = {
97    {"name",           store_name,      ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
98    {"description",    store_str,       ITEM(res_dir.hdr.desc), 0, 0, 0},
99    {"dirport",        store_int,       ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
100    {"address",        store_str,       ITEM(res_dir.address),  0, 0, 0},
101    {"password",       store_password,  ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
102 #ifdef HAVE_TLS
103    {"tlsenable",      store_yesno,     ITEM(res_dir.tls_enable), 1, ITEM_DEFAULT, 0},
104    {"tlsrequire",     store_yesno,     ITEM(res_dir.tls_require), 1, ITEM_DEFAULT, 0},
105    {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
106    {"tlscacertificatedir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
107    {"tlscertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
108    {"tlskey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
109 #endif
110    {NULL, NULL, NULL, 0, 0, 0}
111 };
112
113 /*
114  * This is the master resource definition.
115  * It must have one item for each of the resources.
116  */
117 RES_TABLE resources[] = {
118    {"console",       cons_items,  R_CONSOLE},
119    {"director",      dir_items,   R_DIRECTOR},
120    {NULL,            NULL,        0}
121 };
122
123
124 /* Dump contents of resource */
125 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
126 {
127    URES *res = (URES *)reshdr;
128    bool recurse = true;
129
130    if (res == NULL) {
131       printf("No record for %d %s\n", type, res_to_str(type));
132       return;
133    }
134    if (type < 0) {                    /* no recursion */
135       type = - type;
136       recurse = false;
137    }
138    switch (type) {
139    case R_CONSOLE:
140       printf("Console: name=%s rcfile=%s histfile=%s\n", reshdr->name,
141              res->res_cons.rc_file, res->res_cons.hist_file);
142       break;
143    case R_DIRECTOR:
144       printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name,
145               res->res_dir.address, res->res_dir.DIRport);
146       break;
147    default:
148       printf("Unknown resource type %d\n", type);
149    }
150    if (recurse && res->res_dir.hdr.next) {
151       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
152    }
153 }
154
155 /*
156  * Free memory of resource.
157  * NB, we don't need to worry about freeing any references
158  * to other resources as they will be freed when that
159  * resource chain is traversed.  Mainly we worry about freeing
160  * allocated strings (names).
161  */
162 void free_resource(RES *sres, int type)
163 {
164    RES *nres;
165    URES *res = (URES *)sres;
166
167    if (res == NULL)
168       return;
169
170    /* common stuff -- free the resource name */
171    nres = (RES *)res->res_dir.hdr.next;
172    if (res->res_dir.hdr.name) {
173       free(res->res_dir.hdr.name);
174    }
175    if (res->res_dir.hdr.desc) {
176       free(res->res_dir.hdr.desc);
177    }
178
179    switch (type) {
180    case R_CONSOLE:
181       if (res->res_cons.rc_file) {
182          free(res->res_cons.rc_file);
183       }
184       if (res->res_cons.hist_file) {
185          free(res->res_cons.hist_file);
186       }
187 #ifdef HAVE_TLS
188       if (res->res_cons.tls_ctx) { 
189          free_tls_context(res->res_cons.tls_ctx);
190       }
191       if (res->res_cons.tls_ca_certfile) {
192          free(res->res_cons.tls_ca_certfile);
193       }
194       if (res->res_cons.tls_ca_certdir) {
195          free(res->res_cons.tls_ca_certdir);
196       }
197       if (res->res_cons.tls_certfile) {
198          free(res->res_cons.tls_certfile);
199       }
200       if (res->res_cons.tls_keyfile) {
201          free(res->res_cons.tls_keyfile);
202       }
203 #endif /* HAVE_TLS */
204       break;
205    case R_DIRECTOR:
206       if (res->res_dir.address)
207          free(res->res_dir.address);
208 #ifdef HAVE_TLS
209       if (res->res_dir.tls_ctx) { 
210          free_tls_context(res->res_dir.tls_ctx);
211       }
212       if (res->res_dir.tls_ca_certfile) {
213          free(res->res_dir.tls_ca_certfile);
214       }
215       if (res->res_dir.tls_ca_certdir) {
216          free(res->res_dir.tls_ca_certdir);
217       }
218       if (res->res_dir.tls_certfile) {
219          free(res->res_dir.tls_certfile);
220       }
221       if (res->res_dir.tls_keyfile) {
222          free(res->res_dir.tls_keyfile);
223       }
224 #endif /* HAVE_TLS */
225       break;
226    default:
227       printf("Unknown resource type %d\n", type);
228    }
229    /* Common stuff again -- free the resource, recurse to next one */
230    free(res);
231    if (nres) {
232       free_resource(nres, type);
233    }
234 }
235
236 /* Save the new resource by chaining it into the head list for
237  * the resource. If this is pass 2, we update any resource
238  * pointers (currently only in the Job resource).
239  */
240 void save_resource(int type, RES_ITEM *items, int pass)
241 {
242    URES *res;
243    int rindex = type - r_first;
244    int i, size;
245    int error = 0;
246
247    /*
248     * Ensure that all required items are present
249     */
250    for (i=0; items[i].name; i++) {
251       if (items[i].flags & ITEM_REQUIRED) {
252             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
253                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
254                  items[i].name, resources[rindex]);
255              }
256       }
257    }
258
259    /* During pass 2, we looked up pointers to all the resources
260     * referrenced in the current resource, , now we
261     * must copy their address from the static record to the allocated
262     * record.
263     */
264    if (pass == 2) {
265       switch (type) {
266          /* Resources not containing a resource */
267          case R_CONSOLE:
268          case R_DIRECTOR:
269             break;
270
271          default:
272             Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
273             error = 1;
274             break;
275       }
276       /* Note, the resoure name was already saved during pass 1,
277        * so here, we can just release it.
278        */
279       if (res_all.res_dir.hdr.name) {
280          free(res_all.res_dir.hdr.name);
281          res_all.res_dir.hdr.name = NULL;
282       }
283       if (res_all.res_dir.hdr.desc) {
284          free(res_all.res_dir.hdr.desc);
285          res_all.res_dir.hdr.desc = NULL;
286       }
287       return;
288    }
289
290    /* The following code is only executed during pass 1 */
291    switch (type) {
292    case R_CONSOLE:
293       size = sizeof(CONRES);
294       break;
295    case R_DIRECTOR:
296       size = sizeof(DIRRES);
297       break;
298    default:
299       printf("Unknown resource type %d\n", type);
300       error = 1;
301       size = 1;
302       break;
303    }
304    /* Common */
305    if (!error) {
306       res = (URES *)malloc(size);
307       memcpy(res, &res_all, size);
308       if (!res_head[rindex]) {
309          res_head[rindex] = (RES *)res; /* store first entry */
310       } else {
311          RES *next;
312          for (next=res_head[rindex]; next->next; next=next->next) {
313             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
314                Emsg2(M_ERROR_TERM, 0,
315                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
316                   resources[rindex].name, res->res_dir.hdr.name);
317             }
318          }
319          next->next = (RES *)res;
320          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
321                res->res_dir.hdr.name);
322       }
323    }
324 }