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