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