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