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