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