]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/gnome2-console/console_conf.c
Start SIGHUP coding + misc
[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 struct res_items 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 struct res_items 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 struct res_items 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 struct s_res 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, 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(int type)
149 {
150    URES *res;
151    RES *nres;
152    int rindex = type - r_first;
153
154    res = (URES *)resources[rindex].res_head;
155
156    if (res == NULL)
157       return;
158
159    /* common stuff -- free the resource name */
160    nres = (RES *)res->dir_res.hdr.next;
161    if (res->dir_res.hdr.name) {
162       free(res->dir_res.hdr.name);
163    }
164    if (res->dir_res.hdr.desc) {
165       free(res->dir_res.hdr.desc);
166    }
167
168    switch (type) {
169    case R_DIRECTOR:
170       if (res->dir_res.address) {
171          free(res->dir_res.address);
172       }
173       break;
174    case R_CONSOLE:
175       if (res->con_res.password) {
176          free(res->con_res.password);
177       }
178       break;
179    case R_CONSOLE_FONT:
180       if (res->con_font.fontface) {
181          free(res->con_font.fontface);
182       }
183       break;
184    default:
185       printf("Unknown resource type %d\n", type);
186    }
187    /* Common stuff again -- free the resource, recurse to next one */
188    free(res);
189    resources[rindex].res_head = nres;
190    if (nres) {
191       free_resource(type);
192    }
193 }
194
195 /* Save the new resource by chaining it into the head list for
196  * the resource. If this is pass 2, we update any resource
197  * pointers (currently only in the Job resource).
198  */
199 void save_resource(int type, struct res_items *items, int pass)
200 {
201    URES *res;
202    int rindex = type - r_first;
203    int i, size = 0;
204    int error = 0;
205
206    /* 
207     * Ensure that all required items are present
208     */
209    for (i=0; items[i].name; i++) {
210       if (items[i].flags & ITEM_REQUIRED) {
211             if (!bit_is_set(i, res_all.dir_res.hdr.item_present)) {  
212                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
213                  items[i].name, resources[rindex]);
214              }
215       }
216    }
217
218    /* During pass 2, we looked up pointers to all the resources
219     * referrenced in the current resource, , now we
220     * must copy their address from the static record to the allocated
221     * record.
222     */
223    if (pass == 2) {
224       switch (type) {
225       /* Resources not containing a resource */
226       case R_DIRECTOR:
227          break;
228
229       case R_CONSOLE:
230       case R_CONSOLE_FONT:
231          break;
232
233       default:
234          Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
235          error = 1;
236          break;
237       }
238       /* Note, the resoure name was already saved during pass 1,
239        * so here, we can just release it.
240        */
241       if (res_all.dir_res.hdr.name) {
242          free(res_all.dir_res.hdr.name);
243          res_all.dir_res.hdr.name = NULL;
244       }
245       if (res_all.dir_res.hdr.desc) {
246          free(res_all.dir_res.hdr.desc);
247          res_all.dir_res.hdr.desc = NULL;
248       }
249       return;
250    }
251
252    /* The following code is only executed during pass 1 */
253    switch (type) {
254    case R_DIRECTOR:
255       size = sizeof(DIRRES);
256       break;
257    case R_CONSOLE_FONT:
258       size = sizeof(CONFONTRES);
259       break;
260    case R_CONSOLE:
261       size = sizeof(CONRES);
262       break;
263    default:
264       printf("Unknown resource type %d\n", type);
265       error = 1;
266       break;
267    }
268    /* Common */
269    if (!error) {
270       res = (URES *)malloc(size);
271       memcpy(res, &res_all, size);
272       if (!resources[rindex].res_head) {
273          resources[rindex].res_head = (RES *)res; /* store first entry */
274       } else {
275          RES *next;
276          /* Add new res to end of chain */
277          for (next=resources[rindex].res_head; next->next; next=next->next) {
278             if (strcmp(next->name, res->dir_res.hdr.name) == 0) {
279                Emsg2(M_ERROR_TERM, 0,
280                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
281                   resources[rindex].name, res->dir_res.hdr.name);
282             }
283          }
284          next->next = (RES *)res;
285          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
286                res->dir_res.hdr.name);
287       }
288    }
289 }