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