]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console_conf.c
- Modify the depend section of each Makefile.in to reference
[bacula/bacula] / bacula / src / gnome2-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  *     Version $Id$
23  */
24
25 /*
26    Copyright (C) 2000, 2001 Kern Sibbald and John Walker
27
28    This program is free software; you can redistribute it and/or
29    modify it under the terms of the GNU General Public License
30    as published by the Free Software Foundation; either version 2
31    of the License, or (at your option) any later version.
32
33    This program is distributed in the hope that it will be useful,
34    but WITHOUT ANY WARRANTY; without even the implied warranty of
35    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36    GNU General Public License for more details.
37
38    You should have received a copy of the GNU General Public License
39    along with this program; if not, write to the Free Software
40    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
41  */
42
43 #include "bacula.h"
44 #include "console_conf.h"
45
46 /* Define the first and last resource ID record
47  * types. Note, these should be unique for each
48  * daemon though not a requirement.
49  */
50 int r_first = R_FIRST;
51 int r_last  = R_LAST;
52 static RES *sres_head[R_LAST - R_FIRST + 1];
53 RES **res_head = sres_head;
54
55 /* Forward referenced subroutines */
56
57
58 /* We build the current resource here as we are
59  * scanning the resource configuration definition,
60  * then move it to allocated memory when the resource
61  * scan is complete.
62  */
63 URES res_all;
64 int  res_all_size = sizeof(res_all);
65
66 /* Definition of records permitted within each
67  * resource with the routine to process the record 
68  * information.
69  */ 
70 static RES_ITEM dir_items[] = {
71    {"name",        store_name,     ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
72    {"description", store_str,      ITEM(dir_res.hdr.desc), 0, 0, 0},
73    {"dirport",     store_int,      ITEM(dir_res.DIRport),  0, ITEM_DEFAULT, 9101},
74    {"address",     store_str,      ITEM(dir_res.address),  0, ITEM_REQUIRED, 0},
75    {"password",    store_password, ITEM(dir_res.password), 0, 0, 0},
76    {"enablessl", store_yesno,      ITEM(dir_res.enable_ssl), 1, ITEM_DEFAULT, 0},
77    {NULL, NULL, NULL, 0, 0, 0} 
78 };
79
80 static RES_ITEM con_items[] = {
81    {"name",        store_name,     ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
82    {"description", store_str,      ITEM(con_res.hdr.desc), 0, 0, 0},
83    {"password",    store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
84    {"requiressl",  store_yesno,    ITEM(con_res.require_ssl), 1, ITEM_DEFAULT, 0},
85    {NULL, NULL, NULL, 0, 0, 0} 
86 };
87
88 static RES_ITEM con_font_items[] = {
89    {"name",        store_name,     ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
90    {"description", store_str,      ITEM(con_font.hdr.desc), 0, 0, 0},
91    {"font",        store_str,      ITEM(con_font.fontface), 0, 0, 0},
92    {"requiressl",  store_yesno,    ITEM(con_font.require_ssl), 1, ITEM_DEFAULT, 0},
93    {NULL, NULL, NULL, 0, 0, 0} 
94 };
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    {"director",      dir_items,   R_DIRECTOR},
103    {"console",       con_items,   R_CONSOLE},
104    {"consolefont",   con_font_items, R_CONSOLE_FONT},
105    {NULL,            NULL,        0}
106 };
107
108
109 /* Dump contents of resource */
110 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
111 {
112    URES *res = (URES *)reshdr;
113    bool recurse = true;
114
115    if (res == NULL) {
116       printf("No record for %d %s\n", type, res_to_str(type));
117       return;
118    }
119    if (type < 0) {                    /* no recursion */
120       type = - type;
121       recurse = false;
122    }
123    switch (type) {
124    case R_DIRECTOR:
125       printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name, 
126               res->dir_res.address, res->dir_res.DIRport);
127       break;
128    case R_CONSOLE:
129       printf("Console: name=%s\n", reshdr->name);
130       break;
131    case R_CONSOLE_FONT:
132       printf("ConsoleFont: name=%s font face=%s\n", 
133              reshdr->name, NPRT(res->con_font.fontface));
134       break;
135    default:
136       printf("Unknown resource type %d\n", type);
137    }
138    if (recurse && res->dir_res.hdr.next) {
139       dump_resource(type, res->dir_res.hdr.next, sendit, sock);
140    }
141 }
142
143 /* 
144  * Free memory of resource.  
145  * NB, we don't need to worry about freeing any references
146  * to other resources as they will be freed when that 
147  * resource chain is traversed.  Mainly we worry about freeing
148  * allocated strings (names).
149  */
150 void free_resource(RES *sres, int type)
151 {
152    RES *nres;
153    URES *res = (URES *)sres;
154
155    if (res == NULL)
156       return;
157
158    /* common stuff -- free the resource name */
159    nres = (RES *)res->dir_res.hdr.next;
160    if (res->dir_res.hdr.name) {
161       free(res->dir_res.hdr.name);
162    }
163    if (res->dir_res.hdr.desc) {
164       free(res->dir_res.hdr.desc);
165    }
166
167    switch (type) {
168    case R_DIRECTOR:
169       if (res->dir_res.address) {
170          free(res->dir_res.address);
171       }
172       break;
173    case R_CONSOLE:
174       if (res->con_res.password) {
175          free(res->con_res.password);
176       }
177       break;
178    case R_CONSOLE_FONT:
179       if (res->con_font.fontface) {
180          free(res->con_font.fontface);
181       }
182       break;
183    default:
184       printf("Unknown resource type %d\n", type);
185    }
186    /* Common stuff again -- free the resource, recurse to next one */
187    free(res);
188    if (nres) {
189       free_resource(nres, type);
190    }
191 }
192
193 /* Save the new resource by chaining it into the head list for
194  * the resource. If this is pass 2, we update any resource
195  * pointers (currently only in the Job resource).
196  */
197 void save_resource(int type, RES_ITEM *items, int pass)
198 {
199    URES *res;
200    int rindex = type - r_first;
201    int i, size = 0;
202    int error = 0;
203
204    /* 
205     * Ensure that all required items are present
206     */
207    for (i=0; items[i].name; i++) {
208       if (items[i].flags & ITEM_REQUIRED) {
209             if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {  
210                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
211                  items[i].name, resources[rindex]);
212              }
213       }
214    }
215
216    /* During pass 2, we looked up pointers to all the resources
217     * referrenced in the current resource, , now we
218     * must copy their address from the static record to the allocated
219     * record.
220     */
221    if (pass == 2) {
222       switch (type) {
223       /* Resources not containing a resource */
224       case R_DIRECTOR:
225          break;
226
227       case R_CONSOLE:
228       case R_CONSOLE_FONT:
229          break;
230
231       default:
232          Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
233          error = 1;
234          break;
235       }
236       /* Note, the resoure name was already saved during pass 1,
237        * so here, we can just release it.
238        */
239       if (res_all.dir_res.hdr.name) {
240          free(res_all.dir_res.hdr.name);
241          res_all.dir_res.hdr.name = NULL;
242       }
243       if (res_all.dir_res.hdr.desc) {
244          free(res_all.dir_res.hdr.desc);
245          res_all.dir_res.hdr.desc = NULL;
246       }
247       return;
248    }
249
250    /* The following code is only executed during pass 1 */
251    switch (type) {
252    case R_DIRECTOR:
253       size = sizeof(DIRRES);
254       break;
255    case R_CONSOLE_FONT:
256       size = sizeof(CONFONTRES);
257       break;
258    case R_CONSOLE:
259       size = sizeof(CONRES);
260       break;
261    default:
262       printf("Unknown resource type %d\n", type);
263       error = 1;
264       break;
265    }
266    /* Common */
267    if (!error) {
268       res = (URES *)malloc(size);
269       memcpy(res, &res_all, size);
270       if (!res_head[rindex]) {
271          res_head[rindex] = (RES *)res; /* store first entry */
272       } else {
273          RES *next;
274          /* Add new res to end of chain */
275          for (next=res_head[rindex]; next->next; next=next->next) {
276             if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
277                Emsg2(M_ERROR_TERM, 0,
278                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
279                   resources[rindex].name, res->dir_res.hdr.name);
280             }
281          }
282          next->next = (RES *)res;
283          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
284                res->dir_res.hdr.name);
285       }
286    }
287 }