]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed_conf.c
708fafbd2b28202c791949d54614f4c7eecbb91f
[bacula/bacula] / bacula / src / filed / filed_conf.c
1 /*
2  *   Main configuration file parser for Bacula File Daemon (Client)
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, September MM
21  */
22 /*
23    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License as
27    published by the Free Software Foundation; either version 2 of
28    the License, or (at your option) any later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33    General Public License for more details.
34
35    You should have received a copy of the GNU General Public
36    License along with this program; if not, write to the Free
37    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38    MA 02111-1307, USA.
39
40  */
41
42
43 #include "bacula.h"
44 #include "filed.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
70 /* Client or File daemon "Global" resources */
71 static struct res_items cli_items[] = {
72    {"name",     store_name,     ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
73    {"description", store_str,   ITEM(res_client.hdr.desc), 0, 0, 0},
74    {"fdport",   store_pint,     ITEM(res_client.FDport),  0, ITEM_REQUIRED, 0},
75    {"workingdirectory",  store_dir,  ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0}, 
76    {"piddirectory",  store_dir,  ITEM(res_client.pid_directory), 0, ITEM_REQUIRED, 0}, 
77    {"subsysdirectory",  store_dir,  ITEM(res_client.subsys_directory), 0, ITEM_REQUIRED, 0}, 
78    {NULL, NULL, NULL, 0, 0, 0} 
79 };
80
81 /* Directors that can use our services */
82 static struct res_items dir_items[] = {
83    {"name",        store_name,     ITEM(res_dir.hdr.name),  0, ITEM_REQUIRED, 0},
84    {"description", store_str,      ITEM(res_dir.hdr.desc),  0, 0, 0},
85    {"password",    store_password, ITEM(res_dir.password),  0, ITEM_REQUIRED, 0},
86    {"address",     store_str,      ITEM(res_dir.address),   0, 0, 0},
87    {NULL, NULL, NULL, 0, 0, 0} 
88 };
89
90 /* Message resource */
91 extern struct res_items msgs_items[];
92
93 /* 
94  * This is the master resource definition.  
95  * It must have one item for each of the resources.
96  */
97 struct s_res resources[] = {
98    {"director",      dir_items,   R_DIRECTOR,  NULL},
99    {"filedaemon",    cli_items,   R_CLIENT,    NULL},
100    {"client",        cli_items,   R_CLIENT,    NULL}, /* alias for filedaemon */
101    {"messages",      msgs_items,  R_MSGS,      NULL},
102    {NULL,            NULL,        0,           NULL}
103 };
104
105
106 /* Dump contents of resource */
107 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
108 {
109    URES *res = (URES *)reshdr;
110    int recurse = 1;
111
112    if (res == NULL) {
113       sendit(sock, "No record for %d %s\n", type, res_to_str(type));
114       return;
115    }
116    if (type < 0) {                    /* no recursion */
117       type = - type;
118       recurse = 0;
119    }
120    switch (type) {
121       case R_DIRECTOR:
122          sendit(sock, "Director: name=%s password=%s\n", reshdr->name, 
123                  res->res_dir.password);
124          break;
125       case R_CLIENT:
126          sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
127                  res->res_client.FDport);
128          break;
129       case R_MSGS:
130          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
131          break;
132       default:
133          sendit(sock, "Unknown resource type %d\n", type);
134    }
135    if (recurse && res->res_dir.hdr.next)
136       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
137 }
138
139 /* 
140  * Free memory of resource.  
141  * NB, we don't need to worry about freeing any references
142  * to other resources as they will be freed when that 
143  * resource chain is traversed.  Mainly we worry about freeing
144  * allocated strings (names).
145  */
146 void free_resource(int type)
147 {
148    URES *res;
149    RES *nres;
150    int rindex = type - r_first;
151
152    res = (URES *)resources[rindex].res_head;
153
154    if (res == NULL) {
155       return;
156    }
157
158    /* common stuff -- free the resource name */
159    nres = (RES *)res->res_dir.hdr.next;
160    if (res->res_dir.hdr.name)
161       free(res->res_dir.hdr.name);
162    if (res->res_dir.hdr.desc)
163       free(res->res_dir.hdr.desc);
164
165    switch (type) {
166       case R_DIRECTOR:
167          if (res->res_dir.password)
168             free(res->res_dir.password);
169          if (res->res_dir.address)
170             free(res->res_dir.address);
171          break;
172       case R_CLIENT:
173          if (res->res_client.working_directory)
174             free(res->res_client.working_directory);
175          if (res->res_client.pid_directory)
176             free(res->res_client.pid_directory);
177          if (res->res_client.subsys_directory)
178             free(res->res_client.subsys_directory);
179          break;
180       case R_MSGS:
181          if (res->res_msgs.mail_cmd)
182             free(res->res_msgs.mail_cmd);
183          if (res->res_msgs.operator_cmd)
184             free(res->res_msgs.operator_cmd);
185          break;
186       default:
187          printf("Unknown resource type %d\n", type);
188    }
189    /* Common stuff again -- free the resource, recurse to next one */
190    free(res);
191    resources[rindex].res_head = nres;
192    if (nres) {
193       free_resource(type);
194    }
195 }
196
197 /* Save the new resource by chaining it into the head list for
198  * the resource. If this is pass 2, we update any resource
199  * pointers (currently only in the Job resource).
200  */
201 void save_resource(int type, struct res_items *items, int pass)
202 {
203    URES *res;
204    int rindex = type - r_first;
205    int i, size;
206    int error = 0;
207
208    /* 
209     * Ensure that all required items are present
210     */
211    for (i=0; items[i].name; i++) {
212       if (items[i].flags & ITEM_REQUIRED) {
213             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
214                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
215                  items[i].name, resources[rindex]);
216              }
217       }
218    }
219
220    /* During pass 2, we looked up pointers to all the resources
221     * referrenced in the current resource, , now we
222     * must copy their address from the static record to the allocated
223     * record.
224     */
225    if (pass == 2) {
226       switch (type) {
227          /* Resources not containing a resource */
228          case R_MSGS:
229          case R_DIRECTOR:
230          case R_CLIENT:
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.res_dir.hdr.name) {
242          free(res_all.res_dir.hdr.name);
243          res_all.res_dir.hdr.name = NULL;
244       }
245       if (res_all.res_dir.hdr.desc) {
246          free(res_all.res_dir.hdr.desc);
247          res_all.res_dir.hdr.desc = NULL;
248       }
249       return;
250    }
251
252    switch (type) {
253       case R_DIRECTOR:
254          size = sizeof(DIRRES);
255          break;
256       case R_CLIENT:
257          size = sizeof(CLIENT);
258          break;
259       case R_MSGS:
260          size = sizeof(MSGS);
261          break;
262       default:
263          printf(_("Unknown resource type %d\n"), type);
264          error = 1;
265          break;
266    }
267    /* Common */
268    if (!error) {
269       res = (URES *) malloc(size);
270       memcpy(res, &res_all, size);
271       res->res_dir.hdr.next = resources[rindex].res_head;
272       resources[rindex].res_head = (RES *)res;
273       Dmsg1(90, "dir_conf: inserting res: %s\n", res->res_dir.hdr.name);
274    }
275
276 }