]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
This commit was manufactured by cvs2svn to create tag
[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    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 #include "bacula.h"
38 #include "console_conf.h"
39
40 /* Define the first and last resource ID record
41  * types. Note, these should be unique for each
42  * daemon though not a requirement.
43  */
44 int r_first = R_FIRST;
45 int r_last  = R_LAST;
46 static RES *sres_head[R_LAST - R_FIRST + 1];
47 RES **res_head = sres_head;
48
49 /* Forward referenced subroutines */
50
51
52 /* We build the current resource here as we are
53  * scanning the resource configuration definition,
54  * then move it to allocated memory when the resource
55  * scan is complete.
56  */
57 #if defined(HAVE_WIN32) && !defined(HAVE_CYGWIN)  && !defined(HAVE_MINGW)
58 extern "C" { // work around visual compiler mangling variables
59     URES res_all;
60     int  res_all_size = sizeof(res_all);
61 }
62 #else
63 URES res_all;
64 int  res_all_size = sizeof(res_all);
65 #endif
66
67 /* Definition of records permitted within each
68  * resource with the routine to process the record
69  * information.
70  */
71
72 /*  Console "globals" */
73 static RES_ITEM cons_items[] = {
74    {"name",           store_name,     ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
75    {"description",    store_str,      ITEM(res_cons.hdr.desc), 0, 0, 0},
76    {"rcfile",         store_dir,      ITEM(res_cons.rc_file), 0, 0, 0},
77    {"historyfile",    store_dir,      ITEM(res_cons.hist_file), 0, 0, 0},
78    {"password",       store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
79    {"tlsenable",      store_yesno,     ITEM(res_cons.tls_enable), 1, 0, 0},
80    {"tlsrequire",     store_yesno,     ITEM(res_cons.tls_require), 1, 0, 0},
81    {"tlscacertificatefile", store_dir, ITEM(res_cons.tls_ca_certfile), 0, 0, 0},
82    {"tlscacertificatedir", store_dir,  ITEM(res_cons.tls_ca_certdir), 0, 0, 0},
83    {"tlscertificate", store_dir,       ITEM(res_cons.tls_certfile), 0, 0, 0},
84    {"tlskey",         store_dir,       ITEM(res_cons.tls_keyfile), 0, 0, 0},
85    {NULL, NULL, NULL, 0, 0, 0}
86 };
87
88
89 /*  Director's that we can contact */
90 static RES_ITEM dir_items[] = {
91    {"name",           store_name,      ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
92    {"description",    store_str,       ITEM(res_dir.hdr.desc), 0, 0, 0},
93    {"dirport",        store_int,       ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
94    {"address",        store_str,       ITEM(res_dir.address),  0, 0, 0},
95    {"password",       store_password,  ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
96    {"tlsenable",      store_yesno,     ITEM(res_dir.tls_enable), 1, 0, 0},
97    {"tlsrequire",     store_yesno,     ITEM(res_dir.tls_require), 1, 0, 0},
98    {"tlscacertificatefile", store_dir, ITEM(res_dir.tls_ca_certfile), 0, 0, 0},
99    {"tlscacertificatedir", store_dir,  ITEM(res_dir.tls_ca_certdir), 0, 0, 0},
100    {"tlscertificate", store_dir,       ITEM(res_dir.tls_certfile), 0, 0, 0},
101    {"tlskey",         store_dir,       ITEM(res_dir.tls_keyfile), 0, 0, 0},
102    {NULL, NULL, NULL, 0, 0, 0}
103 };
104
105 /*
106  * This is the master resource definition.
107  * It must have one item for each of the resources.
108  */
109 RES_TABLE resources[] = {
110    {"console",       cons_items,  R_CONSOLE},
111    {"director",      dir_items,   R_DIRECTOR},
112    {NULL,            NULL,        0}
113 };
114
115
116 /* Dump contents of resource */
117 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
118 {
119    URES *res = (URES *)reshdr;
120    bool recurse = true;
121
122    if (res == NULL) {
123       printf(_("No record for %d %s\n"), type, res_to_str(type));
124       return;
125    }
126    if (type < 0) {                    /* no recursion */
127       type = - type;
128       recurse = false;
129    }
130    switch (type) {
131    case R_CONSOLE:
132       printf(_("Console: name=%s rcfile=%s histfile=%s\n"), reshdr->name,
133              res->res_cons.rc_file, res->res_cons.hist_file);
134       break;
135    case R_DIRECTOR:
136       printf(_("Director: name=%s address=%s DIRport=%d\n"), reshdr->name,
137               res->res_dir.address, res->res_dir.DIRport);
138       break;
139    default:
140       printf(_("Unknown resource type %d\n"), type);
141    }
142    if (recurse && res->res_dir.hdr.next) {
143       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
144    }
145 }
146
147 /*
148  * Free memory of resource.
149  * NB, we don't need to worry about freeing any references
150  * to other resources as they will be freed when that
151  * resource chain is traversed.  Mainly we worry about freeing
152  * allocated strings (names).
153  */
154 void free_resource(RES *sres, int type)
155 {
156    RES *nres;
157    URES *res = (URES *)sres;
158
159    if (res == NULL)
160       return;
161
162    /* common stuff -- free the resource name */
163    nres = (RES *)res->res_dir.hdr.next;
164    if (res->res_dir.hdr.name) {
165       free(res->res_dir.hdr.name);
166    }
167    if (res->res_dir.hdr.desc) {
168       free(res->res_dir.hdr.desc);
169    }
170
171    switch (type) {
172    case R_CONSOLE:
173       if (res->res_cons.rc_file) {
174          free(res->res_cons.rc_file);
175       }
176       if (res->res_cons.hist_file) {
177          free(res->res_cons.hist_file);
178       }
179       if (res->res_cons.tls_ctx) { 
180          free_tls_context(res->res_cons.tls_ctx);
181       }
182       if (res->res_cons.tls_ca_certfile) {
183          free(res->res_cons.tls_ca_certfile);
184       }
185       if (res->res_cons.tls_ca_certdir) {
186          free(res->res_cons.tls_ca_certdir);
187       }
188       if (res->res_cons.tls_certfile) {
189          free(res->res_cons.tls_certfile);
190       }
191       if (res->res_cons.tls_keyfile) {
192          free(res->res_cons.tls_keyfile);
193       }
194       break;
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 }