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