]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored_conf.c
8eed0fa90709b023b99d87db6bbeb8bb316822e8
[bacula/bacula] / bacula / src / stored / stored_conf.c
1 /*
2  * Configuration file parser for Bacula Storage daemon
3  *
4  *     Kern Sibbald, March MM
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "stored.h"
31
32 extern int debug_level;
33
34
35 /* First and last resource ids */
36 int r_first = R_FIRST;
37 int r_last  = R_LAST;
38 pthread_mutex_t res_mutex =  PTHREAD_MUTEX_INITIALIZER;
39
40
41 /* Forward referenced subroutines */
42
43 /* We build the current resource here statically,
44  * then move it to dynamic memory */
45 URES res_all;
46 int res_all_size = sizeof(res_all);
47
48 /* Definition of records permitted within each
49  * resource with the routine to process the record 
50  * information.
51  */ 
52
53 /* Globals for the Storage daemon. */
54 static struct res_items store_items[] = {
55    {"name",                  store_name, ITEM(res_store.hdr.name),   0, ITEM_REQUIRED, 0},
56    {"description",           store_str,  ITEM(res_dir.hdr.desc),     0, 0, 0},
57    {"address",               store_str,  ITEM(res_store.address),    0, 0, 0},
58    {"messages",              store_res,  ITEM(res_store.messages),   0, R_MSGS, 0},
59    {"sdport",                store_int,  ITEM(res_store.SDport),     0, ITEM_DEFAULT, 9103},
60    {"sddport",               store_int,  ITEM(res_store.SDDport),    0, 0, 0}, /* depricated */
61    {"workingdirectory",      store_dir,  ITEM(res_store.working_directory), 0, ITEM_REQUIRED, 0},
62    {"piddirectory",          store_dir,  ITEM(res_store.pid_directory), 0, ITEM_REQUIRED, 0},
63    {"subsysdirectory",       store_dir,  ITEM(res_store.subsys_directory), 0, ITEM_REQUIRED, 0},
64    {"maximumconcurrentjobs", store_pint, ITEM(res_store.max_concurrent_jobs), 0, ITEM_DEFAULT, 2},
65    {NULL, NULL, 0, 0, 0, 0} 
66 };
67
68
69 /* Directors that can speak to the Storage daemon */
70 static struct res_items dir_items[] = {
71    {"name",        store_name,     ITEM(res_dir.hdr.name),   0, ITEM_REQUIRED, 0},
72    {"description", store_str,      ITEM(res_dir.hdr.desc),   0, 0, 0},
73    {"password",    store_password, ITEM(res_dir.password),   0, ITEM_REQUIRED, 0},
74    {"address",     store_str,      ITEM(res_dir.address),    0, 0, 0},
75    {NULL, NULL, 0, 0, 0, 0} 
76 };
77
78 /* Device definition */
79 static struct res_items dev_items[] = {
80    {"name",                  store_name,   ITEM(res_dev.hdr.name),        0, ITEM_REQUIRED, 0},
81    {"description",           store_str,    ITEM(res_dir.hdr.desc),        0, 0, 0},
82    {"mediatype",             store_strname,ITEM(res_dev.media_type),      0, ITEM_REQUIRED, 0},
83    {"archivedevice",         store_strname,ITEM(res_dev.device_name),     0, ITEM_REQUIRED, 0},
84    {"hardwareendoffile",     store_yesno,  ITEM(res_dev.cap_bits), CAP_EOF,  ITEM_DEFAULT, 1},
85    {"hardwareendofmedium",   store_yesno,  ITEM(res_dev.cap_bits), CAP_EOM,  ITEM_DEFAULT, 1},
86    {"backwardspacerecord",   store_yesno,  ITEM(res_dev.cap_bits), CAP_BSR,  ITEM_DEFAULT, 1},
87    {"backwardspacefile",     store_yesno,  ITEM(res_dev.cap_bits), CAP_BSF,  ITEM_DEFAULT, 1},
88    {"forwardspacerecord",    store_yesno,  ITEM(res_dev.cap_bits), CAP_FSR,  ITEM_DEFAULT, 1},
89    {"forwardspacefile",      store_yesno,  ITEM(res_dev.cap_bits), CAP_FSF,  ITEM_DEFAULT, 1},
90    {"removablemedia",        store_yesno,  ITEM(res_dev.cap_bits), CAP_REM,  ITEM_DEFAULT, 1},
91    {"randomaccess",          store_yesno,  ITEM(res_dev.cap_bits), CAP_RACCESS, 0, 0},
92    {"automaticmount",        store_yesno,  ITEM(res_dev.cap_bits), CAP_AUTOMOUNT,  ITEM_DEFAULT, 0},
93    {"labelmedia",            store_yesno,  ITEM(res_dev.cap_bits), CAP_LABEL,      ITEM_DEFAULT, 0},
94    {"mountanonymousvolumes", store_yesno,  ITEM(res_dev.cap_bits), CAP_ANONVOLS,   ITEM_DEFAULT, 0},
95    {"alwaysopen",            store_yesno,  ITEM(res_dev.cap_bits), CAP_ALWAYSOPEN, ITEM_DEFAULT, 1},
96    {"autochanger",           store_yesno,  ITEM(res_dev.cap_bits), CAP_AUTOCHANGER, ITEM_DEFAULT, 0},
97    {"changerdevice",         store_strname,ITEM(res_dev.changer_name), 0, 0, 0},
98    {"changercommand",        store_strname,ITEM(res_dev.changer_command), 0, 0, 0},
99    {"maximumchangerwait",    store_pint,   ITEM(res_dev.max_changer_wait), 0, ITEM_DEFAULT, 2 * 60},
100    {"maximumopenwait",       store_pint,   ITEM(res_dev.max_open_wait), 0, ITEM_DEFAULT, 5 * 60},
101    {"offlineonunmount",      store_yesno,  ITEM(res_dev.cap_bits), CAP_OFFLINEUNMOUNT, ITEM_DEFAULT, 0},
102    {"maximumrewindwait",     store_pint,   ITEM(res_dev.max_rewind_wait), 0, ITEM_DEFAULT, 5 * 60},
103    {"minimumblocksize",      store_pint,   ITEM(res_dev.min_block_size), 0, 0, 0},
104    {"maximumblocksize",      store_pint,   ITEM(res_dev.max_block_size), 0, 0, 0},
105    {"maximumvolumejobs",     store_pint,   ITEM(res_dev.max_volume_jobs), 0, 0, 0},
106    {"maximumvolumefiles",    store_int64,  ITEM(res_dev.max_volume_files), 0, 0, 0},
107    {"maximumvolumesize",     store_size,   ITEM(res_dev.max_volume_size), 0, 0, 0},
108    {"maximumfilesize",       store_size,   ITEM(res_dev.max_file_size), 0, 0, 0},
109    {"volumecapacity",        store_size,   ITEM(res_dev.volume_capacity), 0, 0, 0},
110    {NULL, NULL, 0, 0, 0, 0} 
111 };
112
113 /* Message resource */
114 extern struct res_items msgs_items[];
115
116
117 /* This is the master resource definition */
118 struct s_res resources[] = {
119    {"director",      dir_items,   R_DIRECTOR,  NULL},
120    {"storage",       store_items, R_STORAGE,   NULL},
121    {"device",        dev_items,   R_DEVICE,    NULL},
122    {"messages",      msgs_items,  R_MSGS,      NULL},
123    {NULL,            NULL,        0,           NULL}
124 };
125
126
127
128 /* Dump contents of resource */
129 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
130 {
131    URES *res = (URES *)reshdr;
132    char buf[MAXSTRING];
133    int recurse = 1;
134    if (res == NULL) {
135       sendit(sock, _("Warning: no %s resource (%d) defined.\n"), res_to_str(type), type);
136       return;
137    }
138    sendit(sock, "dump_resource type=%d\n", type);
139    if (type < 0) {                    /* no recursion */
140       type = - type;
141       recurse = 0;
142    }
143    switch (type) {
144       case R_DIRECTOR:
145          sendit(sock, "Director: name=%s\n", res->res_dir.hdr.name);
146          break;
147       case R_STORAGE:
148          sendit(sock, "Storage: name=%s address=%s SDport=%d SDDport=%d\n",
149             res->res_store.hdr.name, res->res_store.address, 
150             res->res_store.SDport, res->res_store.SDDport);
151          break;
152       case R_DEVICE:
153          sendit(sock, "Device: name=%s MediaType=%s Device=%s\n",
154             res->res_dev.hdr.name,
155             res->res_dev.media_type, res->res_dev.device_name);
156          sendit(sock, "        rew_wait=%d min_bs=%d max_bs=%d\n",
157             res->res_dev.max_rewind_wait, res->res_dev.min_block_size, 
158             res->res_dev.max_block_size);
159          sendit(sock, "        max_jobs=%d max_files=%" lld " max_size=%" lld "\n",
160             res->res_dev.max_volume_jobs, res->res_dev.max_volume_files,
161             res->res_dev.max_volume_size);
162          sendit(sock, "        max_file_size=%" lld " capacity=%" lld "\n",
163             res->res_dev.max_file_size, res->res_dev.volume_capacity);
164          strcpy(buf, "        ");
165          if (res->res_dev.cap_bits & CAP_EOF) {
166             strcat(buf, "CAP_EOF ");
167          }
168          if (res->res_dev.cap_bits & CAP_BSR) {
169             strcat(buf, "CAP_BSR ");
170          }
171          if (res->res_dev.cap_bits & CAP_BSF) {
172             strcat(buf, "CAP_BSF ");
173          }
174          if (res->res_dev.cap_bits & CAP_FSR) {
175             strcat(buf, "CAP_FSR ");
176          }
177          if (res->res_dev.cap_bits & CAP_FSF) {
178             strcat(buf, "CAP_FSF ");
179          }
180          if (res->res_dev.cap_bits & CAP_EOM) {
181             strcat(buf, "CAP_EOM ");
182          }
183          if (res->res_dev.cap_bits & CAP_REM) {
184             strcat(buf, "CAP_REM ");
185          }
186          if (res->res_dev.cap_bits & CAP_RACCESS) {
187             strcat(buf, "CAP_RACCESS ");
188          }
189          if (res->res_dev.cap_bits & CAP_AUTOMOUNT) {
190             strcat(buf, "CAP_AUTOMOUNT ");
191          }
192          if (res->res_dev.cap_bits & CAP_LABEL) {
193             strcat(buf, "CAP_LABEL ");
194          }
195          if (res->res_dev.cap_bits & CAP_ANONVOLS) {
196             strcat(buf, "CAP_ANONVOLS ");
197          }
198          if (res->res_dev.cap_bits & CAP_ALWAYSOPEN) {
199             strcat(buf, "CAP_ALWAYSOPEN ");
200          }
201          strcat(buf, "\n");
202          sendit(sock, buf);
203          break;
204       case R_MSGS:
205          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
206          if (res->res_msgs.mail_cmd) 
207             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
208          if (res->res_msgs.operator_cmd) 
209             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
210          break;
211       default:
212          sendit(sock, _("Warning: unknown resource type %d\n"), type);
213          break;
214    }
215    if (recurse && res->res_dir.hdr.next)
216       dump_resource(type, (RES *)res->res_dir.hdr.next, sendit, sock);
217 }
218
219 /* 
220  * Free memory of resource.  
221  * NB, we don't need to worry about freeing any references
222  * to other resources as they will be freed when that 
223  * resource chain is traversed.  Mainly we worry about freeing
224  * allocated strings (names).
225  */
226 void free_resource(int type)
227 {
228    URES *nres;
229    URES *res;
230    int rindex = type - r_first;
231    res = (URES *)resources[rindex].res_head;
232
233    if (res == NULL)
234       return;
235
236    /* common stuff -- free the resource name */
237    nres = (URES *)res->res_dir.hdr.next;
238    if (res->res_dir.hdr.name) {
239       free(res->res_dir.hdr.name);
240    }
241    if (res->res_dir.hdr.desc) {
242       free(res->res_dir.hdr.desc);
243    }
244
245
246    switch (type) {
247       case R_DIRECTOR:
248          if (res->res_dir.password)
249             free(res->res_dir.password);
250          if (res->res_dir.address)
251             free(res->res_dir.address);
252          break;
253       case R_STORAGE:
254          if (res->res_store.address)
255             free(res->res_store.address);
256          if (res->res_store.working_directory)
257             free(res->res_store.working_directory);
258          if (res->res_store.pid_directory)
259             free(res->res_store.pid_directory);
260          if (res->res_store.subsys_directory)
261             free(res->res_store.subsys_directory);
262          break;
263       case R_DEVICE:
264          if (res->res_dev.media_type)
265             free(res->res_dev.media_type);
266          if (res->res_dev.device_name)
267             free(res->res_dev.device_name);
268          if (res->res_dev.changer_name)
269             free(res->res_dev.changer_name);
270          if (res->res_dev.changer_command)
271             free(res->res_dev.changer_command);
272          break;
273       case R_MSGS:
274          if (res->res_msgs.mail_cmd)
275             free(res->res_msgs.mail_cmd);
276          if (res->res_msgs.operator_cmd)
277             free(res->res_msgs.operator_cmd);
278          free_msgs_res((MSGS *)res);  /* free message resource */
279          res = NULL;
280          break;
281       default:
282          Dmsg1(0, "Unknown resource type %d\n", type);
283          break;
284    }
285    /* Common stuff again -- free the resource, recurse to next one */
286    if (res) {
287       free(res);
288    }
289    resources[rindex].res_head = (RES *)nres;
290    if (nres) {
291       free_resource(type);
292    }
293 }
294
295 /* Save the new resource by chaining it into the head list for
296  * the resource. If this is pass 2, we update any resource
297  * pointers (currently only in the Job resource).
298  */
299 void save_resource(int type, struct res_items *items, int pass)
300 {
301    URES *res;
302    int rindex = type - r_first;
303    int i, size;
304    int error = 0;
305
306    /* 
307     * Ensure that all required items are present
308     */
309    for (i=0; items[i].name; i++) {
310       if (items[i].flags & ITEM_REQUIRED) {
311          if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
312             Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
313               items[i].name, resources[rindex]);
314           }
315       }
316       /* If this triggers, take a look at lib/parse_conf.h */
317       if (i >= MAX_RES_ITEMS) {
318          Emsg1(M_ABORT, 0, _("Too many items in %s resource\n"), resources[rindex]);
319       }
320    }
321
322    /* During pass 2, we looked up pointers to all the resources
323     * referrenced in the current resource, , now we
324     * must copy their address from the static record to the allocated
325     * record.
326     */
327    if (pass == 2) {
328       switch (type) {
329          /* Resources not containing a resource */
330          case R_DIRECTOR:
331          case R_DEVICE:
332          case R_MSGS:
333             break;
334
335          /* Resources containing a resource */
336          case R_STORAGE:
337             if ((res = (URES *)GetResWithName(R_STORAGE, res_all.res_dir.hdr.name)) == NULL) {
338                Emsg1(M_ABORT, 0, "Cannot find Storage resource %s\n", res_all.res_dir.hdr.name);
339             }
340             res->res_store.messages = res_all.res_store.messages;
341             break;
342          default:
343             printf("Unknown resource type %d\n", type);
344             error = 1;
345             break;
346       }
347
348
349       if (res_all.res_dir.hdr.name) {
350          free(res_all.res_dir.hdr.name);
351          res_all.res_dir.hdr.name = NULL;
352       }
353       if (res_all.res_dir.hdr.desc) {
354          free(res_all.res_dir.hdr.desc);
355          res_all.res_dir.hdr.desc = NULL;
356       }
357       return;
358    }
359
360    /* The following code is only executed on pass 1 */
361    switch (type) {
362       case R_DIRECTOR:
363          size = sizeof(DIRRES);
364          break;
365       case R_STORAGE:
366          size = sizeof(STORES);
367          break;
368       case R_DEVICE:
369          size = sizeof(DEVRES);
370          break;
371       case R_MSGS:
372          size = sizeof(MSGS);   
373          break;
374       default:
375          printf("Unknown resource type %d\n", type);
376          error = 1;
377          size = 1;
378          break;
379    }
380    /* Common */
381    if (!error) {
382       res = (URES *)malloc(size);
383       memcpy(res, &res_all, size);
384       if (!resources[rindex].res_head) {
385          resources[rindex].res_head = (RES *)res; /* store first entry */
386       } else {
387          RES *next;
388          /* Add new res to end of chain */
389          for (next=resources[rindex].res_head; next->next; next=next->next)
390             { }
391          next->next = (RES *)res;
392          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
393                res->res_dir.hdr.name);
394       }
395    }
396 }