]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed_conf.c
022d7ba6cb685234687fc205fca237b8d162d5c6
[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  *   Version $Id$
23  */
24 /*
25    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
26
27    This program is free software; you can redistribute it and/or
28    modify it under the terms of the GNU General Public License as
29    published by the Free Software Foundation; either version 2 of
30    the License, or (at your option) any later version.
31
32    This program is distributed in the hope that it will be useful,
33    but WITHOUT ANY WARRANTY; without even the implied warranty of
34    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35    General Public License for more details.
36
37    You should have received a copy of the GNU General Public
38    License along with this program; if not, write to the Free
39    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40    MA 02111-1307, USA.
41
42  */
43
44
45 #include "bacula.h"
46 #include "filed.h"
47
48 /* Define the first and last resource ID record
49  * types. Note, these should be unique for each
50  * daemon though not a requirement.
51  */
52 int r_first = R_FIRST;
53 int r_last  = R_LAST;
54 pthread_mutex_t res_mutex =  PTHREAD_MUTEX_INITIALIZER;
55
56 /* Forward referenced subroutines */
57
58
59 /* We build the current resource here as we are
60  * scanning the resource configuration definition,
61  * then move it to allocated memory when the resource
62  * scan is complete.
63  */
64 URES res_all;
65 int  res_all_size = sizeof(res_all);
66
67 /* Definition of records permitted within each
68  * resource with the routine to process the record 
69  * information.
70  */ 
71
72 /* Client or File daemon "Global" resources */
73 static struct res_items cli_items[] = {
74    {"name",        store_name,  ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
75    {"description", store_str,   ITEM(res_client.hdr.desc), 0, 0, 0},
76    {"fdport",      store_pint,  ITEM(res_client.FDport),  0, ITEM_DEFAULT, 9102},
77    {"fdaddress",   store_str,   ITEM(res_client.FDaddr),  0, 0, 0},
78    {"workingdirectory",  store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0}, 
79    {"piddirectory",  store_dir,     ITEM(res_client.pid_directory),     0, ITEM_REQUIRED, 0}, 
80    {"subsysdirectory",  store_dir,  ITEM(res_client.subsys_directory),  0, 0, 0}, 
81    {"requiressl",  store_yesno,     ITEM(res_client.require_ssl),       1, ITEM_DEFAULT, 0},
82    {"maximumconcurrentjobs", store_pint,  ITEM(res_client.MaxConcurrentJobs), 0, ITEM_DEFAULT, 10},
83    {"messages",      store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
84    {"heartbeatinterval", store_time, ITEM(res_client.heartbeat_interval), 0, ITEM_DEFAULT, 0},
85    {"sdconnecttimeout", store_time,ITEM(res_client.SDConnectTimeout), 0, ITEM_DEFAULT, 60 * 30},
86    {"maximumnetworkbuffersize", store_pint, ITEM(res_client.max_network_buffer_size), 0, 0, 0},
87    {NULL, NULL, NULL, 0, 0, 0} 
88 };
89
90 /* Directors that can use our services */
91 static struct res_items dir_items[] = {
92    {"name",        store_name,     ITEM(res_dir.hdr.name),  0, ITEM_REQUIRED, 0},
93    {"description", store_str,      ITEM(res_dir.hdr.desc),  0, 0, 0},
94    {"password",    store_password, ITEM(res_dir.password),  0, ITEM_REQUIRED, 0},
95    {"address",     store_str,      ITEM(res_dir.address),   0, 0, 0},
96    {"enablessl",   store_yesno,    ITEM(res_dir.enable_ssl),1, ITEM_DEFAULT, 0},
97    {NULL, NULL, NULL, 0, 0, 0} 
98 };
99
100 /* Message resource */
101 extern struct res_items msgs_items[];
102
103 /* 
104  * This is the master resource definition.  
105  * It must have one item for each of the resources.
106  */
107 struct s_res resources[] = {
108    {"director",      dir_items,   R_DIRECTOR,  NULL},
109    {"filedaemon",    cli_items,   R_CLIENT,    NULL},
110    {"client",        cli_items,   R_CLIENT,    NULL}, /* alias for filedaemon */
111    {"messages",      msgs_items,  R_MSGS,      NULL},
112    {NULL,            NULL,        0,           NULL}
113 };
114
115
116 /* Dump contents of resource */
117 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
118 {
119    URES *res = (URES *)reshdr;
120    int recurse = 1;
121
122    if (res == NULL) {
123       sendit(sock, "No record for %d %s\n", type, res_to_str(type));
124       return;
125    }
126    if (type < 0) {                    /* no recursion */
127       type = - type;
128       recurse = 0;
129    }
130    switch (type) {
131       case R_DIRECTOR:
132          sendit(sock, "Director: name=%s password=%s\n", reshdr->name, 
133                  res->res_dir.password);
134          break;
135       case R_CLIENT:
136          sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
137                  res->res_client.FDport);
138          break;
139       case R_MSGS:
140          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
141          if (res->res_msgs.mail_cmd) 
142             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
143          if (res->res_msgs.operator_cmd) 
144             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
145          break;
146       default:
147          sendit(sock, "Unknown resource type %d\n", type);
148    }
149    if (recurse && res->res_dir.hdr.next)
150       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
151 }
152
153 /* 
154  * Free memory of resource.  
155  * NB, we don't need to worry about freeing any references
156  * to other resources as they will be freed when that 
157  * resource chain is traversed.  Mainly we worry about freeing
158  * allocated strings (names).
159  */
160 void free_resource(int type)
161 {
162    URES *res;
163    RES *nres;
164    int rindex = type - r_first;
165
166    res = (URES *)resources[rindex].res_head;
167
168    if (res == NULL) {
169       return;
170    }
171
172    /* common stuff -- free the resource name */
173    nres = (RES *)res->res_dir.hdr.next;
174    if (res->res_dir.hdr.name) {
175       free(res->res_dir.hdr.name);
176    }
177    if (res->res_dir.hdr.desc) {
178       free(res->res_dir.hdr.desc);
179    }
180
181    switch (type) {
182       case R_DIRECTOR:
183          if (res->res_dir.password) {
184             free(res->res_dir.password);
185          }
186          if (res->res_dir.address) {
187             free(res->res_dir.address);
188          }
189          break;
190       case R_CLIENT:
191          if (res->res_client.working_directory) {
192             free(res->res_client.working_directory);
193          }
194          if (res->res_client.pid_directory) {
195             free(res->res_client.pid_directory);
196          }
197          if (res->res_client.subsys_directory) {
198             free(res->res_client.subsys_directory);
199          }
200          if (res->res_client.FDaddr) {
201             free(res->res_client.FDaddr);
202          }
203          break;
204       case R_MSGS:
205          if (res->res_msgs.mail_cmd)
206             free(res->res_msgs.mail_cmd);
207          if (res->res_msgs.operator_cmd)
208             free(res->res_msgs.operator_cmd);
209          free_msgs_res((MSGS *)res);  /* free message resource */
210          res = NULL;
211          break;
212       default:
213          printf("Unknown resource type %d\n", type);
214    }
215    /* Common stuff again -- free the resource, recurse to next one */
216    if (res) {
217       free(res);
218    }
219    resources[rindex].res_head = nres;
220    if (nres) {
221       free_resource(type);
222    }
223 }
224
225 /* Save the new resource by chaining it into the head list for
226  * the resource. If this is pass 2, we update any resource
227  * pointers (currently only in the Job resource).
228  */
229 void save_resource(int type, struct res_items *items, int pass)
230 {
231    URES *res;
232    int rindex = type - r_first;
233    int i, size;
234    int error = 0;
235
236    /* 
237     * Ensure that all required items are present
238     */
239    for (i=0; items[i].name; i++) {
240       if (items[i].flags & ITEM_REQUIRED) {
241             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
242                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
243                  items[i].name, resources[rindex]);
244              }
245       }
246    }
247
248    /* During pass 2, we looked up pointers to all the resources
249     * referrenced in the current resource, , now we
250     * must copy their address from the static record to the allocated
251     * record.
252     */
253    if (pass == 2) {
254       switch (type) {
255          /* Resources not containing a resource */
256          case R_MSGS:
257          case R_DIRECTOR:
258             break;
259
260          /* Resources containing another resource */
261          case R_CLIENT:
262             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
263                Emsg1(M_ABORT, 0, "Cannot find Client resource %s\n", res_all.res_dir.hdr.name);
264             }
265             res->res_client.messages = res_all.res_client.messages;
266             break;
267          default:
268             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
269             error = 1;
270             break;
271       }
272       /* Note, the resoure name was already saved during pass 1,
273        * so here, we can just release it.
274        */
275       if (res_all.res_dir.hdr.name) {
276          free(res_all.res_dir.hdr.name);
277          res_all.res_dir.hdr.name = NULL;
278       }
279       if (res_all.res_dir.hdr.desc) {
280          free(res_all.res_dir.hdr.desc);
281          res_all.res_dir.hdr.desc = NULL;
282       }
283       return;
284    }
285
286    /* The following code is only executed on pass 1 */
287    switch (type) {
288       case R_DIRECTOR:
289          size = sizeof(DIRRES);
290          break;
291       case R_CLIENT:
292          size = sizeof(CLIENT);
293          break;
294       case R_MSGS:
295          size = sizeof(MSGS);
296          break;
297       default:
298          printf(_("Unknown resource type %d\n"), type);
299          error = 1;
300          size = 1;
301          break;
302    }
303    /* Common */
304    if (!error) {
305       res = (URES *)malloc(size);
306       memcpy(res, &res_all, size);
307       if (!resources[rindex].res_head) {
308          resources[rindex].res_head = (RES *)res; /* store first entry */
309       } else {
310          RES *next;
311          /* Add new res to end of chain */
312          for (next=resources[rindex].res_head; next->next; next=next->next) {
313             if (strcmp(next->name, res->res_dir.hdr.name) == 0) {
314                Emsg2(M_ERROR_TERM, 0,
315                   _("Attempt to define second %s resource named \"%s\" is not permitted.\n"),
316                   resources[rindex].name, res->res_dir.hdr.name);
317             }
318          }
319          next->next = (RES *)res;
320          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
321                res->res_dir.hdr.name);
322       }
323    }
324 }