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).
6 * Note, the configuration file parser consists of three parts
8 * 1. The generic lexical scanner in lib/lex.c and lib/lex.h
10 * 2. The generic config scanner in lib/parse_config.c and
12 * These files contain the parser code, some utility
13 * routines, and the common store routines (name, int,
16 * 3. The daemon specific file, which contains the Resource
17 * definitions as well as any specific store routines
18 * for the resource records.
20 * Kern Sibbald, September MM
25 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
48 /* Define the first and last resource ID record
49 * types. Note, these should be unique for each
50 * daemon though not a requirement.
52 int r_first = R_FIRST;
54 pthread_mutex_t res_mutex = PTHREAD_MUTEX_INITIALIZER;
56 /* Forward referenced subroutines */
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
65 int res_all_size = sizeof(res_all);
67 /* Definition of records permitted within each
68 * resource with the routine to process the record
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_REQUIRED, 0},
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 {"messages", store_res, ITEM(res_client.messages), R_MSGS, 0, 0},
81 {NULL, NULL, NULL, 0, 0, 0}
84 /* Directors that can use our services */
85 static struct res_items dir_items[] = {
86 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
87 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
88 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
89 {"address", store_str, ITEM(res_dir.address), 0, 0, 0},
90 {NULL, NULL, NULL, 0, 0, 0}
93 /* Message resource */
94 extern struct res_items msgs_items[];
97 * This is the master resource definition.
98 * It must have one item for each of the resources.
100 struct s_res resources[] = {
101 {"director", dir_items, R_DIRECTOR, NULL},
102 {"filedaemon", cli_items, R_CLIENT, NULL},
103 {"client", cli_items, R_CLIENT, NULL}, /* alias for filedaemon */
104 {"messages", msgs_items, R_MSGS, NULL},
105 {NULL, NULL, 0, NULL}
109 /* Dump contents of resource */
110 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
112 URES *res = (URES *)reshdr;
116 sendit(sock, "No record for %d %s\n", type, res_to_str(type));
119 if (type < 0) { /* no recursion */
125 sendit(sock, "Director: name=%s password=%s\n", reshdr->name,
126 res->res_dir.password);
129 sendit(sock, "Client: name=%s FDport=%d\n", reshdr->name,
130 res->res_client.FDport);
133 sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
134 if (res->res_msgs.mail_cmd)
135 sendit(sock, " mailcmd=%s\n", res->res_msgs.mail_cmd);
136 if (res->res_msgs.operator_cmd)
137 sendit(sock, " opcmd=%s\n", res->res_msgs.operator_cmd);
140 sendit(sock, "Unknown resource type %d\n", type);
142 if (recurse && res->res_dir.hdr.next)
143 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
147 * Free memory of resource.
148 * NB, we don't need to worry about freeing any references
149 * to other resources as they will be freed when that
150 * resource chain is traversed. Mainly we worry about freeing
151 * allocated strings (names).
153 void free_resource(int type)
157 int rindex = type - r_first;
159 res = (URES *)resources[rindex].res_head;
165 /* common stuff -- free the resource name */
166 nres = (RES *)res->res_dir.hdr.next;
167 if (res->res_dir.hdr.name)
168 free(res->res_dir.hdr.name);
169 if (res->res_dir.hdr.desc)
170 free(res->res_dir.hdr.desc);
174 if (res->res_dir.password)
175 free(res->res_dir.password);
176 if (res->res_dir.address)
177 free(res->res_dir.address);
180 if (res->res_client.working_directory)
181 free(res->res_client.working_directory);
182 if (res->res_client.pid_directory)
183 free(res->res_client.pid_directory);
184 if (res->res_client.subsys_directory)
185 free(res->res_client.subsys_directory);
188 if (res->res_msgs.mail_cmd)
189 free(res->res_msgs.mail_cmd);
190 if (res->res_msgs.operator_cmd)
191 free(res->res_msgs.operator_cmd);
192 free_msgs_res((MSGS *)res); /* free message resource */
196 printf("Unknown resource type %d\n", type);
198 /* Common stuff again -- free the resource, recurse to next one */
202 resources[rindex].res_head = nres;
208 /* Save the new resource by chaining it into the head list for
209 * the resource. If this is pass 2, we update any resource
210 * pointers (currently only in the Job resource).
212 void save_resource(int type, struct res_items *items, int pass)
215 int rindex = type - r_first;
220 * Ensure that all required items are present
222 for (i=0; items[i].name; i++) {
223 if (items[i].flags & ITEM_REQUIRED) {
224 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
225 Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
226 items[i].name, resources[rindex]);
231 /* During pass 2, we looked up pointers to all the resources
232 * referrenced in the current resource, , now we
233 * must copy their address from the static record to the allocated
238 /* Resources not containing a resource */
243 /* Resources containing another resource */
245 if ((res = (URES *)GetResWithName(R_CLIENT, res_all.res_dir.hdr.name)) == NULL) {
246 Emsg1(M_ABORT, 0, "Cannot find Client resource %s\n", res_all.res_dir.hdr.name);
248 res->res_client.messages = res_all.res_client.messages;
251 Emsg1(M_ERROR, 0, _("Unknown resource type %d\n"), type);
255 /* Note, the resoure name was already saved during pass 1,
256 * so here, we can just release it.
258 if (res_all.res_dir.hdr.name) {
259 free(res_all.res_dir.hdr.name);
260 res_all.res_dir.hdr.name = NULL;
262 if (res_all.res_dir.hdr.desc) {
263 free(res_all.res_dir.hdr.desc);
264 res_all.res_dir.hdr.desc = NULL;
269 /* The following code is only executed on pass 1 */
272 size = sizeof(DIRRES);
275 size = sizeof(CLIENT);
281 printf(_("Unknown resource type %d\n"), type);
287 res = (URES *)malloc(size);
288 memcpy(res, &res_all, size);
289 res->res_dir.hdr.next = resources[rindex].res_head;
290 resources[rindex].res_head = (RES *)res;
291 Dmsg1(90, "dir_conf: inserting res: %s\n", res->res_dir.hdr.name);