]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/console_conf.c
Initial revision
[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 static struct res_items dir_items[] = {
68    {"name",        store_name,     ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
69    {"description", store_str,      ITEM(res_dir.hdr.desc), 0, 0, 0},
70    {"dirport",     store_int,      ITEM(res_dir.DIRport),  0, ITEM_REQUIRED, 0},
71    {"address",     store_str,      ITEM(res_dir.address),  0, ITEM_REQUIRED, 0},
72    {"password",    store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
73    {NULL, NULL, NULL, 0, 0, 0} 
74 };
75
76 /* 
77  * This is the master resource definition.  
78  * It must have one item for each of the resources.
79  */
80 struct s_res resources[] = {
81    {"director",      dir_items,   R_DIRECTOR,  NULL},
82    {NULL,            NULL,        0,           NULL}
83 };
84
85
86 /* Dump contents of resource */
87 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
88 {
89    URES *res = (URES *)reshdr;
90    int recurse = 1;
91
92    if (res == NULL) {
93       printf("No record for %d %s\n", type, res_to_str(type));
94       return;
95    }
96    if (type < 0) {                    /* no recursion */
97       type = - type;
98       recurse = 0;
99    }
100    switch (type) {
101       case R_DIRECTOR:
102          printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name, 
103                  res->res_dir.address, res->res_dir.DIRport);
104          break;
105       default:
106          printf("Unknown resource type %d\n", type);
107    }
108    if (recurse && res->res_dir.hdr.next) {
109       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
110    }
111 }
112
113 /* 
114  * Free memory of resource.  
115  * NB, we don't need to worry about freeing any references
116  * to other resources as they will be freed when that 
117  * resource chain is traversed.  Mainly we worry about freeing
118  * allocated strings (names).
119  */
120 void free_resource(int type)
121 {
122    URES *res;
123    RES *nres;
124    int rindex = type - r_first;
125
126    res = (URES *)resources[rindex].res_head;
127
128    if (res == NULL)
129       return;
130
131    /* common stuff -- free the resource name */
132    nres = (RES *)res->res_dir.hdr.next;
133    if (res->res_dir.hdr.name)
134       free(res->res_dir.hdr.name);
135    if (res->res_dir.hdr.desc)
136       free(res->res_dir.hdr.desc);
137
138    switch (type) {
139       case R_DIRECTOR:
140          if (res->res_dir.address)
141             free(res->res_dir.address);
142          break;
143       default:
144          printf("Unknown resource type %d\n", type);
145    }
146    /* Common stuff again -- free the resource, recurse to next one */
147    free(res);
148    resources[rindex].res_head = nres;
149    if (nres)
150       free_resource(type);
151 }
152
153 /* Save the new resource by chaining it into the head list for
154  * the resource. If this is pass 2, we update any resource
155  * pointers (currently only in the Job resource).
156  */
157 void save_resource(int type, struct res_items *items, int pass)
158 {
159    URES *res;
160    int rindex = type - r_first;
161    int i, size;
162    int error = 0;
163
164    /* 
165     * Ensure that all required items are present
166     */
167    for (i=0; items[i].name; i++) {
168       if (items[i].flags & ITEM_REQUIRED) {
169             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
170                Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
171                  items[i].name, resources[rindex]);
172              }
173       }
174    }
175
176    /* During pass 2, we looked up pointers to all the resources
177     * referrenced in the current resource, , now we
178     * must copy their address from the static record to the allocated
179     * record.
180     */
181    if (pass == 2) {
182       switch (type) {
183          /* Resources not containing a resource */
184          case R_DIRECTOR:
185             break;
186
187          default:
188             Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
189             error = 1;
190             break;
191       }
192       /* Note, the resoure name was already saved during pass 1,
193        * so here, we can just release it.
194        */
195       if (res_all.res_dir.hdr.name) {
196          free(res_all.res_dir.hdr.name);
197          res_all.res_dir.hdr.name = NULL;
198       }
199       if (res_all.res_dir.hdr.desc) {
200          free(res_all.res_dir.hdr.desc);
201          res_all.res_dir.hdr.desc = NULL;
202       }
203       return;
204    }
205
206    switch (type) {
207       case R_DIRECTOR:
208          size = sizeof(DIRRES);
209          break;
210       default:
211          printf("Unknown resource type %d\n", type);
212          error = 1;
213          break;
214    }
215    /* Common */
216    if (!error) {
217       res = (URES *) malloc(size);
218       memcpy(res, &res_all, size);
219       res->res_dir.hdr.next = resources[rindex].res_head;
220       resources[rindex].res_head = (RES *)res;
221       Dmsg1(90, "dir_conf: inserting res: %s\n", res->res_dir.hdr.name);
222    }
223
224 }