]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console_conf.c
Add Peter Eriksson's const code + turn bsscanf code
[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
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 URES res_all;
62 int  res_all_size = sizeof(res_all);
63
64 /* Definition of records permitted within each
65  * resource with the routine to process the record 
66  * information.
67  */ 
68 static RES_ITEM dir_items[] = {
69    {"name",        store_name,     ITEM(dir_res.hdr.name), 0, ITEM_REQUIRED, 0},
70    {"description", store_str,      ITEM(dir_res.hdr.desc), 0, 0, 0},
71    {"dirport",     store_int,      ITEM(dir_res.DIRport),  0, ITEM_DEFAULT, 9101},
72    {"address",     store_str,      ITEM(dir_res.address),  0, ITEM_REQUIRED, 0},
73    {"password",    store_password, ITEM(dir_res.password), 0, 0, 0},
74    {"enablessl", store_yesno,      ITEM(dir_res.enable_ssl), 1, ITEM_DEFAULT, 0},
75    {NULL, NULL, NULL, 0, 0, 0} 
76 };
77
78 static RES_ITEM con_items[] = {
79    {"name",        store_name,     ITEM(con_res.hdr.name), 0, ITEM_REQUIRED, 0},
80    {"description", store_str,      ITEM(con_res.hdr.desc), 0, 0, 0},
81    {"password",    store_password, ITEM(con_res.password), 0, ITEM_REQUIRED, 0},
82    {"requiressl",  store_yesno,    ITEM(con_res.require_ssl), 1, ITEM_DEFAULT, 0},
83    {NULL, NULL, NULL, 0, 0, 0} 
84 };
85
86 static RES_ITEM con_font_items[] = {
87    {"name",        store_name,     ITEM(con_font.hdr.name), 0, ITEM_REQUIRED, 0},
88    {"description", store_str,      ITEM(con_font.hdr.desc), 0, 0, 0},
89    {"font",        store_str,      ITEM(con_font.fontface), 0, 0, 0},
90    {"requiressl",  store_yesno,    ITEM(con_font.require_ssl), 1, ITEM_DEFAULT, 0},
91    {NULL, NULL, NULL, 0, 0, 0} 
92 };
93
94
95 /* 
96  * This is the master resource definition.  
97  * It must have one item for each of the resources.
98  */
99 RES_TABLE resources[] = {
100    {"director",      dir_items,   R_DIRECTOR,  NULL},
101    {"console",       con_items,   R_CONSOLE,   NULL},
102    {"consolefont",   con_font_items, R_CONSOLE_FONT,   NULL},
103    {NULL,            NULL,        0,           NULL}
104 };
105
106
107 /* Dump contents of resource */
108 void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fmt, ...), void *sock)
109 {
110    URES *res = (URES *)reshdr;
111    int recurse = 1;
112
113    if (res == NULL) {
114       printf("No record for %d %s\n", type, res_to_str(type));
115       return;
116    }
117    if (type < 0) {                    /* no recursion */
118       type = - type;
119       recurse = 0;
120    }
121    switch (type) {
122    case R_DIRECTOR:
123       printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name, 
124               res->dir_res.address, res->dir_res.DIRport);
125       break;
126    case R_CONSOLE:
127       printf("Console: name=%s\n", reshdr->name);
128       break;
129    case R_CONSOLE_FONT:
130       printf("ConsoleFont: name=%s font face=%s\n", 
131              reshdr->name, NPRT(res->con_font.fontface));
132       break;
133    default:
134       printf("Unknown resource type %d\n", type);
135    }
136    if (recurse && res->dir_res.hdr.next) {
137       dump_resource(type, res->dir_res.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->dir_res.hdr.next;
158    if (res->dir_res.hdr.name) {
159       free(res->dir_res.hdr.name);
160    }
161    if (res->dir_res.hdr.desc) {
162       free(res->dir_res.hdr.desc);
163    }
164
165    switch (type) {
166    case R_DIRECTOR:
167       if (res->dir_res.address) {
168          free(res->dir_res.address);
169       }
170       break;
171    case R_CONSOLE:
172       if (res->con_res.password) {
173          free(res->con_res.password);
174       }
175       break;
176    case R_CONSOLE_FONT:
177       if (res->con_font.fontface) {
178          free(res->con_font.fontface);
179       }
180       break;
181    default:
182       printf("Unknown resource type %d\n", type);
183    }
184    /* Common stuff again -- free the resource, recurse to next one */
185    free(res);
186    if (nres) {
187       free_resource(nres, type);
188    }
189 }
190
191 /* Save the new resource by chaining it into the head list for
192  * the resource. If this is pass 2, we update any resource
193  * pointers (currently only in the Job resource).
194  */
195 void save_resource(int type, RES_ITEM *items, int pass)
196 {
197    URES *res;
198    int rindex = type - r_first;
199    int i, size = 0;
200    int error = 0;
201
202    /* 
203     * Ensure that all required items are present
204     */
205    for (i=0; items[i].name; i++) {
206       if (items[i].flags & ITEM_REQUIRED) {
207             if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {  
208                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
209                  items[i].name, resources[rindex]);
210              }
211       }
212    }
213
214    /* During pass 2, we looked up pointers to all the resources
215     * referrenced in the current resource, , now we
216     * must copy their address from the static record to the allocated
217     * record.
218     */
219    if (pass == 2) {
220       switch (type) {
221       /* Resources not containing a resource */
222       case R_DIRECTOR:
223          break;
224
225       case R_CONSOLE:
226       case R_CONSOLE_FONT:
227          break;
228
229       default:
230          Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
231          error = 1;
232          break;
233       }
234       /* Note, the resoure name was already saved during pass 1,
235        * so here, we can just release it.
236        */
237       if (res_all.dir_res.hdr.name) {
238          free(res_all.dir_res.hdr.name);
239          res_all.dir_res.hdr.name = NULL;
240       }
241       if (res_all.dir_res.hdr.desc) {
242          free(res_all.dir_res.hdr.desc);
243          res_all.dir_res.hdr.desc = NULL;
244       }
245       return;
246    }
247
248    /* The following code is only executed during pass 1 */
249    switch (type) {
250    case R_DIRECTOR:
251       size = sizeof(DIRRES);
252       break;
253    case R_CONSOLE_FONT:
254       size = sizeof(CONFONTRES);
255       break;
256    case R_CONSOLE:
257       size = sizeof(CONRES);
258       break;
259    default:
260       printf("Unknown resource type %d\n", type);
261       error = 1;
262       break;
263    }
264    /* Common */
265    if (!error) {
266       res = (URES *)malloc(size);
267       memcpy(res, &res_all, size);
268       if (!resources[rindex].res_head) {
269          resources[rindex].res_head = (RES *)res; /* store first entry */
270       } else {
271          RES *next;
272          /* Add new res to end of chain */
273          for (next=resources[rindex].res_head; next->next; next=next->next) {
274             if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
275                Emsg2(M_ERROR_TERM, 0,
276                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
277                   resources[rindex].name, res->dir_res.hdr.name);
278             }
279          }
280          next->next = (RES *)res;
281          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
282                res->dir_res.hdr.name);
283       }
284    }
285 }