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