2 * Main configuration file parser for Bacula User Agent
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, January MM, September MM
24 Copyright (C) 2000, 2001 Kern Sibbald and John Walker
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License
28 as published by the Free Software Foundation; either version 2
29 of the License, or (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 GNU General Public License for more details.
36 You should have received a copy of the GNU General Public License
37 along with this program; if not, write to the Free Software
38 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #include "console_conf.h"
44 /* Define the first and last resource ID record
45 * types. Note, these should be unique for each
46 * daemon though not a requirement.
48 int r_first = R_FIRST;
50 pthread_mutex_t res_mutex = PTHREAD_MUTEX_INITIALIZER;
52 /* Forward referenced subroutines */
55 /* We build the current resource here as we are
56 * scanning the resource configuration definition,
57 * then move it to allocated memory when the resource
61 int res_all_size = sizeof(res_all);
63 /* Definition of records permitted within each
64 * resource with the routine to process the record
68 /* Console "globals" */
69 static struct res_items cons_items[] = {
70 {"name", store_name, ITEM(res_cons.hdr.name), 0, ITEM_REQUIRED, 0},
71 {"description", store_str, ITEM(res_cons.hdr.desc), 0, 0, 0},
72 {"rcfile", store_dir, ITEM(res_cons.rc_file), 0, 0, 0},
73 {"historyfile", store_dir, ITEM(res_cons.hist_file), 0, 0, 0},
74 {NULL, NULL, NULL, 0, 0, 0}
78 /* Director's that we can contact */
79 static struct res_items dir_items[] = {
80 {"name", store_name, ITEM(res_dir.hdr.name), 0, ITEM_REQUIRED, 0},
81 {"description", store_str, ITEM(res_dir.hdr.desc), 0, 0, 0},
82 {"dirport", store_int, ITEM(res_dir.DIRport), 0, ITEM_DEFAULT, 9101},
83 {"address", store_str, ITEM(res_dir.address), 0, ITEM_REQUIRED, 0},
84 {"password", store_password, ITEM(res_dir.password), 0, ITEM_REQUIRED, 0},
85 {NULL, NULL, NULL, 0, 0, 0}
89 * This is the master resource definition.
90 * It must have one item for each of the resources.
92 struct s_res resources[] = {
93 {"console", cons_items, R_CONSOLE, NULL},
94 {"director", dir_items, R_DIRECTOR, NULL},
99 /* Dump contents of resource */
100 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
102 URES *res = (URES *)reshdr;
106 printf("No record for %d %s\n", type, res_to_str(type));
109 if (type < 0) { /* no recursion */
115 printf("Console: name=%s rcfile=%s histfile=%s\n", reshdr->name,
116 res->res_cons.rc_file, res->res_cons.hist_file);
119 printf("Director: name=%s address=%s DIRport=%d\n", reshdr->name,
120 res->res_dir.address, res->res_dir.DIRport);
123 printf("Unknown resource type %d\n", type);
125 if (recurse && res->res_dir.hdr.next) {
126 dump_resource(type, res->res_dir.hdr.next, sendit, sock);
131 * Free memory of resource.
132 * NB, we don't need to worry about freeing any references
133 * to other resources as they will be freed when that
134 * resource chain is traversed. Mainly we worry about freeing
135 * allocated strings (names).
137 void free_resource(int type)
141 int rindex = type - r_first;
143 res = (URES *)resources[rindex].res_head;
148 /* common stuff -- free the resource name */
149 nres = (RES *)res->res_dir.hdr.next;
150 if (res->res_dir.hdr.name)
151 free(res->res_dir.hdr.name);
152 if (res->res_dir.hdr.desc)
153 free(res->res_dir.hdr.desc);
157 if (res->res_cons.rc_file) {
158 free(res->res_cons.rc_file);
160 if (res->res_cons.hist_file) {
161 free(res->res_cons.hist_file);
164 if (res->res_dir.address)
165 free(res->res_dir.address);
168 printf("Unknown resource type %d\n", type);
170 /* Common stuff again -- free the resource, recurse to next one */
172 resources[rindex].res_head = nres;
177 /* Save the new resource by chaining it into the head list for
178 * the resource. If this is pass 2, we update any resource
179 * pointers (currently only in the Job resource).
181 void save_resource(int type, struct res_items *items, int pass)
184 int rindex = type - r_first;
189 * Ensure that all required items are present
191 for (i=0; items[i].name; i++) {
192 if (items[i].flags & ITEM_REQUIRED) {
193 if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {
194 Emsg2(M_ABORT, 0, "%s item is required in %s resource, but not found.\n",
195 items[i].name, resources[rindex]);
200 /* During pass 2, we looked up pointers to all the resources
201 * referrenced in the current resource, , now we
202 * must copy their address from the static record to the allocated
207 /* Resources not containing a resource */
213 Emsg1(M_ERROR, 0, "Unknown resource type %d\n", type);
217 /* Note, the resoure name was already saved during pass 1,
218 * so here, we can just release it.
220 if (res_all.res_dir.hdr.name) {
221 free(res_all.res_dir.hdr.name);
222 res_all.res_dir.hdr.name = NULL;
224 if (res_all.res_dir.hdr.desc) {
225 free(res_all.res_dir.hdr.desc);
226 res_all.res_dir.hdr.desc = NULL;
231 /* The following code is only executed during pass 1 */
234 size = sizeof(CONSRES);
237 size = sizeof(DIRRES);
240 printf("Unknown resource type %d\n", type);
247 res = (URES *)malloc(size);
248 memcpy(res, &res_all, size);
249 if (!resources[rindex].res_head) {
250 resources[rindex].res_head = (RES *)res; /* store first entry */
253 /* Add new res to end of chain */
254 for (next=resources[rindex].res_head; next->next; next=next->next)
256 next->next = (RES *)res;
257 Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
258 res->res_dir.hdr.name);