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