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