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