]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/filed_conf.c
21a756765e1f90aad350e0d3ed19f614fd5c1632
[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 /*
23    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
24
25    This program is free software; you can redistribute it and/or
26    modify it under the terms of the GNU General Public License as
27    published by the Free Software Foundation; either version 2 of
28    the License, or (at your option) any later version.
29
30    This program is distributed in the hope that it will be useful,
31    but WITHOUT ANY WARRANTY; without even the implied warranty of
32    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33    General Public License for more details.
34
35    You should have received a copy of the GNU General Public
36    License along with this program; if not, write to the Free
37    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
38    MA 02111-1307, USA.
39
40  */
41
42
43 #include "bacula.h"
44 #include "filed.h"
45
46 /* Define the first and last resource ID record
47  * types. Note, these should be unique for each
48  * daemon though not a requirement.
49  */
50 int r_first = R_FIRST;
51 int r_last  = R_LAST;
52 pthread_mutex_t res_mutex =  PTHREAD_MUTEX_INITIALIZER;
53
54 /* Forward referenced subroutines */
55
56
57 /* We build the current resource here as we are
58  * scanning the resource configuration definition,
59  * then move it to allocated memory when the resource
60  * scan is complete.
61  */
62 URES res_all;
63 int  res_all_size = sizeof(res_all);
64
65 /* Definition of records permitted within each
66  * resource with the routine to process the record 
67  * information.
68  */ 
69
70 /* Client or File daemon "Global" resources */
71 static struct res_items cli_items[] = {
72    {"name",     store_name,     ITEM(res_client.hdr.name), 0, ITEM_REQUIRED, 0},
73    {"description", store_str,   ITEM(res_client.hdr.desc), 0, 0, 0},
74    {"fdport",   store_pint,     ITEM(res_client.FDport),  0, ITEM_REQUIRED, 0},
75    {"workingdirectory",  store_dir, ITEM(res_client.working_directory), 0, ITEM_REQUIRED, 0}, 
76    {"piddirectory",  store_dir, ITEM(res_client.pid_directory), 0, ITEM_REQUIRED, 0}, 
77    {"subsysdirectory",  store_dir,  ITEM(res_client.subsys_directory), 0, ITEM_REQUIRED, 0}, 
78    {"messages",      store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
79    {NULL, NULL, NULL, 0, 0, 0} 
80 };
81
82 /* Directors that can use our services */
83 static struct res_items dir_items[] = {
84    {"name",        store_name,     ITEM(res_dir.hdr.name),  0, ITEM_REQUIRED, 0},
85    {"description", store_str,      ITEM(res_dir.hdr.desc),  0, 0, 0},
86    {"password",    store_password, ITEM(res_dir.password),  0, ITEM_REQUIRED, 0},
87    {"address",     store_str,      ITEM(res_dir.address),   0, 0, 0},
88    {NULL, NULL, NULL, 0, 0, 0} 
89 };
90
91 /* Message resource */
92 extern struct res_items msgs_items[];
93
94 /* 
95  * This is the master resource definition.  
96  * It must have one item for each of the resources.
97  */
98 struct s_res resources[] = {
99    {"director",      dir_items,   R_DIRECTOR,  NULL},
100    {"filedaemon",    cli_items,   R_CLIENT,    NULL},
101    {"client",        cli_items,   R_CLIENT,    NULL}, /* alias for filedaemon */
102    {"messages",      msgs_items,  R_MSGS,      NULL},
103    {NULL,            NULL,        0,           NULL}
104 };
105
106
107 /* Dump contents of resource */
108 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
109 {
110    URES *res = (URES *)reshdr;
111    int recurse = 1;
112
113    if (res == NULL) {
114       sendit(sock, "No record for %d %s\n", type, res_to_str(type));
115       return;
116    }
117    if (type < 0) {                    /* no recursion */
118       type = - type;
119       recurse = 0;
120    }
121    switch (type) {
122       case R_DIRECTOR:
123          sendit(sock, "Director: name=%s password=%s\n", reshdr->name, 
124                  res->res_dir.password);
125          break;
126       case R_CLIENT:
127          sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
128                  res->res_client.FDport);
129          break;
130       case R_MSGS:
131          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
132          if (res->res_msgs.mail_cmd) 
133             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
134          if (res->res_msgs.operator_cmd) 
135             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
136          break;
137       default:
138          sendit(sock, "Unknown resource type %d\n", type);
139    }
140    if (recurse && res->res_dir.hdr.next)
141       dump_resource(type, res->res_dir.hdr.next, sendit, sock);
142 }
143
144 /* 
145  * Free memory of resource.  
146  * NB, we don't need to worry about freeing any references
147  * to other resources as they will be freed when that 
148  * resource chain is traversed.  Mainly we worry about freeing
149  * allocated strings (names).
150  */
151 void free_resource(int type)
152 {
153    URES *res;
154    RES *nres;
155    int rindex = type - r_first;
156
157    res = (URES *)resources[rindex].res_head;
158
159    if (res == NULL) {
160       return;
161    }
162
163    /* common stuff -- free the resource name */
164    nres = (RES *)res->res_dir.hdr.next;
165    if (res->res_dir.hdr.name)
166       free(res->res_dir.hdr.name);
167    if (res->res_dir.hdr.desc)
168       free(res->res_dir.hdr.desc);
169
170    switch (type) {
171       case R_DIRECTOR:
172          if (res->res_dir.password)
173             free(res->res_dir.password);
174          if (res->res_dir.address)
175             free(res->res_dir.address);
176          break;
177       case R_CLIENT:
178          if (res->res_client.working_directory)
179             free(res->res_client.working_directory);
180          if (res->res_client.pid_directory)
181             free(res->res_client.pid_directory);
182          if (res->res_client.subsys_directory)
183             free(res->res_client.subsys_directory);
184          break;
185       case R_MSGS:
186          if (res->res_msgs.mail_cmd)
187             free(res->res_msgs.mail_cmd);
188          if (res->res_msgs.operator_cmd)
189             free(res->res_msgs.operator_cmd);
190          free_msgs_res((MSGS *)res);  /* free message resource */
191          res = NULL;
192          break;
193       default:
194          printf("Unknown resource type %d\n", type);
195    }
196    /* Common stuff again -- free the resource, recurse to next one */
197    if (res) {
198       free(res);
199    }
200    resources[rindex].res_head = nres;
201    if (nres) {
202       free_resource(type);
203    }
204 }
205
206 /* Save the new resource by chaining it into the head list for
207  * the resource. If this is pass 2, we update any resource
208  * pointers (currently only in the Job resource).
209  */
210 void save_resource(int type, struct res_items *items, int pass)
211 {
212    URES *res;
213    int rindex = type - r_first;
214    int i, size;
215    int error = 0;
216
217    /* 
218     * Ensure that all required items are present
219     */
220    for (i=0; items[i].name; i++) {
221       if (items[i].flags & ITEM_REQUIRED) {
222             if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
223                Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
224                  items[i].name, resources[rindex]);
225              }
226       }
227    }
228
229    /* During pass 2, we looked up pointers to all the resources
230     * referrenced in the current resource, , now we
231     * must copy their address from the static record to the allocated
232     * record.
233     */
234    if (pass == 2) {
235       switch (type) {
236          /* Resources not containing a resource */
237          case R_MSGS:
238          case R_DIRECTOR:
239             break;
240
241          /* Resources containing another resource */
242          case R_CLIENT:
243             if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
244                Emsg1(M_ABORT, 0, "Cannot find Client resource %s\n", res_all.res_dir.hdr.name);
245             }
246             res->res_client.messages = res_all.res_client.messages;
247             break;
248          default:
249             Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
250             error = 1;
251             break;
252       }
253       /* Note, the resoure name was already saved during pass 1,
254        * so here, we can just release it.
255        */
256       if (res_all.res_dir.hdr.name) {
257          free(res_all.res_dir.hdr.name);
258          res_all.res_dir.hdr.name = NULL;
259       }
260       if (res_all.res_dir.hdr.desc) {
261          free(res_all.res_dir.hdr.desc);
262          res_all.res_dir.hdr.desc = NULL;
263       }
264       return;
265    }
266
267    switch (type) {
268       case R_DIRECTOR:
269          size = sizeof(DIRRES);
270          break;
271       case R_CLIENT:
272          size = sizeof(CLIENT);
273          break;
274       case R_MSGS:
275          size = sizeof(MSGS);
276          break;
277       default:
278          printf(_("Unknown resource type %d\n"), type);
279          error = 1;
280          break;
281    }
282    /* Common */
283    if (!error) {
284       res = (URES *)malloc(size);
285       memcpy(res, &res_all, size);
286       res->res_dir.hdr.next = resources[rindex].res_head;
287       resources[rindex].res_head = (RES *)res;
288       Dmsg1(90, "dir_conf: inserting res: %s\n", res->res_dir.hdr.name);
289    }
290
291 }