]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/console_conf.c
This commit was manufactured by cvs2svn to create tag
[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 /*
24    Copyright (C) 2000, 2001 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    {"requiressl",  store_yesno,    ITEM(res_cons.require_ssl), 1, ITEM_DEFAULT, 0},
83    {"password",    store_password, ITEM(res_cons.password), 0, ITEM_REQUIRED, 0},
84    {NULL, NULL, NULL, 0, 0, 0}
85 };
86
87
88 /*  Director's that we can contact */
89 static RES_ITEM dir_items[] = {
90    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
91    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
92    {"dirport",     store_int,      ITEM(res_dir.DIRport),  0, ITEM_DEFAULT, 9101},
93    {"address",     store_str,      ITEM(res_dir.address),  0, 0, 0},
94    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
95    {"enablessl",   store_yesno,    ITEM(res_dir.enable_ssl), 1, ITEM_DEFAULT, 0},
96    {NULL, NULL, NULL, 0, 0, 0}
97 };
98
99 /*
100  * This is the master resource definition.
101  * It must have one item for each of the resources.
102  */
103 RES_TABLE resources[] = {
104    {"console",       cons_items,  R_CONSOLE},
105    {"director",      dir_items,   R_DIRECTOR},
106    {NULL,        NULL,    0}
107 };
108
109
110 /* Dump contents of resource */
111 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
112 {
113    URES *res = (URES *)reshdr;
114    int recurse = 1;
115
116    if (res == NULL) {
117       printf("No record for %d %s\n", type, res_to_str(type));
118       return;
119    }
120    if (type < 0) {            /* no recursion */
121       type = - type;
122       recurse = 0;
123    }
124    switch (type) {
125       case R_CONSOLE:
126          printf("Console: name=%s rcfile=%s histfile=%s\n", reshdr->name,
127       res->res_cons.rc_file, res->res_cons.hist_file);
128     break;
129       case R_DIRECTOR:
130          printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name,
131        res->res_dir.address, res->res_dir.DIRport);
132     break;
133       default:
134          printf("Unknown resource type %d\n", type);
135    }
136    if (recurse && res->res_dir.hdr.next) {
137       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
138    }
139 }
140
141 /*
142  * Free memory of resource.
143  * NB, we don't need to worry about freeing any references
144  * to other resources as they will be freed when that
145  * resource chain is traversed.  Mainly we worry about freeing
146  * allocated strings (names).
147  */
148 void free_resource(RES *sres, int type)
149 {
150    RES *nres;
151    URES *res = (URES *)sres;
152
153    if (res == NULL)
154       return;
155
156    /* common stuff -- free the resource name */
157    nres = (RES *)res->res_dir.hdr.next;
158    if (res->res_dir.hdr.name) {
159       free(res->res_dir.hdr.name);
160    }
161    if (res->res_dir.hdr.desc) {
162       free(res->res_dir.hdr.desc);
163    }
164
165    switch (type) {
166       case R_CONSOLE:
167     if (res->res_cons.rc_file) {
168        free(res->res_cons.rc_file);
169     }
170     if (res->res_cons.hist_file) {
171        free(res->res_cons.hist_file);
172     }
173       case R_DIRECTOR:
174     if (res->res_dir.address)
175        free(res->res_dir.address);
176     break;
177       default:
178          printf("Unknown resource type %d\n", type);
179    }
180    /* Common stuff again -- free the resource, recurse to next one */
181    free(res);
182    if (nres) {
183       free_resource(nres, type);
184    }
185 }
186
187 /* Save the new resource by chaining it into the head list for
188  * the resource. If this is pass 2, we update any resource
189  * pointers (currently only in the Job resource).
190  */
191 void save_resource(int type, RES_ITEM *items, int pass)
192 {
193    URES *res;
194    int rindex = type - r_first;
195    int i, size;
196    int error = 0;
197
198    /*
199     * Ensure that all required items are present
200     */
201    for (i=0; items[i].name; i++) {
202       if (items[i].flags & ITEM_REQUIRED) {
203        if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
204                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
205        items[i].name, resources[rindex]);
206         }
207       }
208    }
209
210    /* During pass 2, we looked up pointers to all the resources
211     * referrenced in the current resource, , now we
212     * must copy their address from the static record to the allocated
213     * record.
214     */
215    if (pass == 2) {
216       switch (type) {
217     /* Resources not containing a resource */
218     case R_CONSOLE:
219     case R_DIRECTOR:
220        break;
221
222     default:
223             Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
224        error = 1;
225        break;
226       }
227       /* Note, the resoure name was already saved during pass 1,
228        * so here, we can just release it.
229        */
230       if (res_all.res_dir.hdr.name) {
231     free(res_all.res_dir.hdr.name);
232     res_all.res_dir.hdr.name = NULL;
233       }
234       if (res_all.res_dir.hdr.desc) {
235     free(res_all.res_dir.hdr.desc);
236     res_all.res_dir.hdr.desc = NULL;
237       }
238       return;
239    }
240
241    /* The following code is only executed during pass 1 */
242    switch (type) {
243       case R_CONSOLE:
244     size = sizeof(CONRES);
245     break;
246       case R_DIRECTOR:
247     size = sizeof(DIRRES);
248     break;
249       default:
250          printf("Unknown resource type %d\n", type);
251     error = 1;
252     size = 1;
253     break;
254    }
255    /* Common */
256    if (!error) {
257       res = (URES *)malloc(size);
258       memcpy(res, &res_all, size);
259       if (!res_head[rindex]) {
260     res_head[rindex] = (RES *)res; /* store first entry */
261       } else {
262     RES *next;
263     for (next=res_head[rindex]; next->next; next=next->next) {
264        if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
265           Emsg2(M_ERROR_TERM, 0,
266                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
267         resources[rindex].name, res->res_dir.hdr.name);
268        }
269     }
270     next->next = (RES *)res;
271          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
272           res->res_dir.hdr.name);
273       }
274    }
275 }