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