]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored_conf.c
Mult Vols + fix restore hard links + file permissions
[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}, /* deprecated */
58    {"sdaddress",             store_str,  ITEM(res_store.SDaddr),     0, 0, 0},
59    {"messages",              store_res,  ITEM(res_store.messages),   0, R_MSGS, 0},
60    {"sdport",                store_int,  ITEM(res_store.SDport),     0, ITEM_DEFAULT, 9103},
61    {"sddport",               store_int,  ITEM(res_store.SDDport),    0, 0, 0}, /* deprecated */
62    {"workingdirectory",      store_dir,  ITEM(res_store.working_directory), 0, ITEM_REQUIRED, 0},
63    {"piddirectory",          store_dir,  ITEM(res_store.pid_directory), 0, ITEM_REQUIRED, 0},
64    {"subsysdirectory",       store_dir,  ITEM(res_store.subsys_directory), 0, ITEM_REQUIRED, 0},
65    {"maximumconcurrentjobs", store_pint, ITEM(res_store.max_concurrent_jobs), 0, ITEM_DEFAULT, 3},
66    {NULL, NULL, 0, 0, 0, 0} 
67 };
68
69
70 /* Directors that can speak to the Storage daemon */
71 static struct res_items dir_items[] = {
72    {"name",        store_name,     ITEM(res_dir.hdr.name),   0, ITEM_REQUIRED, 0},
73    {"description", store_str,      ITEM(res_dir.hdr.desc),   0, 0, 0},
74    {"password",    store_password, ITEM(res_dir.password),   0, ITEM_REQUIRED, 0},
75    {"address",     store_str,      ITEM(res_dir.address),    0, 0, 0},
76    {NULL, NULL, 0, 0, 0, 0} 
77 };
78
79 /* Device definition */
80 static struct res_items dev_items[] = {
81    {"name",                  store_name,   ITEM(res_dev.hdr.name),        0, ITEM_REQUIRED, 0},
82    {"description",           store_str,    ITEM(res_dir.hdr.desc),        0, 0, 0},
83    {"mediatype",             store_strname,ITEM(res_dev.media_type),      0, ITEM_REQUIRED, 0},
84    {"archivedevice",         store_strname,ITEM(res_dev.device_name),     0, ITEM_REQUIRED, 0},
85    {"hardwareendoffile",     store_yesno,  ITEM(res_dev.cap_bits), CAP_EOF,  ITEM_DEFAULT, 1},
86    {"hardwareendofmedium",   store_yesno,  ITEM(res_dev.cap_bits), CAP_EOM,  ITEM_DEFAULT, 1},
87    {"backwardspacerecord",   store_yesno,  ITEM(res_dev.cap_bits), CAP_BSR,  ITEM_DEFAULT, 1},
88    {"backwardspacefile",     store_yesno,  ITEM(res_dev.cap_bits), CAP_BSF,  ITEM_DEFAULT, 1},
89    {"forwardspacerecord",    store_yesno,  ITEM(res_dev.cap_bits), CAP_FSR,  ITEM_DEFAULT, 1},
90    {"forwardspacefile",      store_yesno,  ITEM(res_dev.cap_bits), CAP_FSF,  ITEM_DEFAULT, 1},
91    {"removablemedia",        store_yesno,  ITEM(res_dev.cap_bits), CAP_REM,  ITEM_DEFAULT, 1},
92    {"randomaccess",          store_yesno,  ITEM(res_dev.cap_bits), CAP_RACCESS, 0, 0},
93    {"automaticmount",        store_yesno,  ITEM(res_dev.cap_bits), CAP_AUTOMOUNT,  ITEM_DEFAULT, 0},
94    {"labelmedia",            store_yesno,  ITEM(res_dev.cap_bits), CAP_LABEL,      ITEM_DEFAULT, 0},
95    {"mountanonymousvolumes", store_yesno,  ITEM(res_dev.cap_bits), CAP_ANONVOLS,   ITEM_DEFAULT, 0},
96    {"alwaysopen",            store_yesno,  ITEM(res_dev.cap_bits), CAP_ALWAYSOPEN, ITEM_DEFAULT, 1},
97    {"autochanger",           store_yesno,  ITEM(res_dev.cap_bits), CAP_AUTOCHANGER, ITEM_DEFAULT, 0},
98    {"changerdevice",         store_strname,ITEM(res_dev.changer_name), 0, 0, 0},
99    {"changercommand",        store_strname,ITEM(res_dev.changer_command), 0, 0, 0},
100    {"maximumchangerwait",    store_pint,   ITEM(res_dev.max_changer_wait), 0, ITEM_DEFAULT, 2 * 60},
101    {"maximumopenwait",       store_pint,   ITEM(res_dev.max_open_wait), 0, ITEM_DEFAULT, 5 * 60},
102    {"maximumopenvolumes",    store_pint,   ITEM(res_dev.max_open_vols), 0, ITEM_DEFAULT, 1},
103    {"offlineonunmount",      store_yesno,  ITEM(res_dev.cap_bits), CAP_OFFLINEUNMOUNT, ITEM_DEFAULT, 0},
104    {"maximumrewindwait",     store_pint,   ITEM(res_dev.max_rewind_wait), 0, ITEM_DEFAULT, 5 * 60},
105    {"minimumblocksize",      store_pint,   ITEM(res_dev.min_block_size), 0, 0, 0},
106    {"maximumblocksize",      store_pint,   ITEM(res_dev.max_block_size), 0, 0, 0},
107    {"maximumvolumejobs",     store_pint,   ITEM(res_dev.max_volume_jobs), 0, 0, 0},
108    {"maximumvolumefiles",    store_int64,  ITEM(res_dev.max_volume_files), 0, 0, 0},
109    {"maximumvolumesize",     store_size,   ITEM(res_dev.max_volume_size), 0, 0, 0},
110    {"maximumfilesize",       store_size,   ITEM(res_dev.max_file_size), 0, 0, 0},
111    {"volumecapacity",        store_size,   ITEM(res_dev.volume_capacity), 0, 0, 0},
112    {NULL, NULL, 0, 0, 0, 0} 
113 };
114
115 /* Message resource */
116 extern struct res_items msgs_items[];
117
118
119 /* This is the master resource definition */
120 struct s_res resources[] = {
121    {"director",      dir_items,   R_DIRECTOR,  NULL},
122    {"storage",       store_items, R_STORAGE,   NULL},
123    {"device",        dev_items,   R_DEVICE,    NULL},
124    {"messages",      msgs_items,  R_MSGS,      NULL},
125    {NULL,            NULL,        0,           NULL}
126 };
127
128
129
130 /* Dump contents of resource */
131 void dump_resource(int type, RES *reshdr, void sendit(void *sock, char *fmt, ...), void *sock)
132 {
133    URES *res = (URES *)reshdr;
134    char buf[MAXSTRING];
135    int recurse = 1;
136    if (res == NULL) {
137       sendit(sock, _("Warning: no %s resource (%d) defined.\n"), res_to_str(type), type);
138       return;
139    }
140    sendit(sock, "dump_resource type=%d\n", type);
141    if (type < 0) {                    /* no recursion */
142       type = - type;
143       recurse = 0;
144    }
145    switch (type) {
146       case R_DIRECTOR:
147          sendit(sock, "Director: name=%s\n", res->res_dir.hdr.name);
148          break;
149       case R_STORAGE:
150          sendit(sock, "Storage: name=%s SDaddr=%s SDport=%d SDDport=%d\n",
151             res->res_store.hdr.name, res->res_store.SDaddr, 
152             res->res_store.SDport, res->res_store.SDDport);
153          break;
154       case R_DEVICE:
155          sendit(sock, "Device: name=%s MediaType=%s Device=%s\n",
156             res->res_dev.hdr.name,
157             res->res_dev.media_type, res->res_dev.device_name);
158          sendit(sock, "        rew_wait=%d min_bs=%d max_bs=%d\n",
159             res->res_dev.max_rewind_wait, res->res_dev.min_block_size, 
160             res->res_dev.max_block_size);
161          sendit(sock, "        max_jobs=%d max_files=%" lld " max_size=%" lld "\n",
162             res->res_dev.max_volume_jobs, res->res_dev.max_volume_files,
163             res->res_dev.max_volume_size);
164          sendit(sock, "        max_file_size=%" lld " capacity=%" lld "\n",
165             res->res_dev.max_file_size, res->res_dev.volume_capacity);
166          strcpy(buf, "        ");
167          if (res->res_dev.cap_bits & CAP_EOF) {
168             strcat(buf, "CAP_EOF ");
169          }
170          if (res->res_dev.cap_bits & CAP_BSR) {
171             strcat(buf, "CAP_BSR ");
172          }
173          if (res->res_dev.cap_bits & CAP_BSF) {
174             strcat(buf, "CAP_BSF ");
175          }
176          if (res->res_dev.cap_bits & CAP_FSR) {
177             strcat(buf, "CAP_FSR ");
178          }
179          if (res->res_dev.cap_bits & CAP_FSF) {
180             strcat(buf, "CAP_FSF ");
181          }
182          if (res->res_dev.cap_bits & CAP_EOM) {
183             strcat(buf, "CAP_EOM ");
184          }
185          if (res->res_dev.cap_bits & CAP_REM) {
186             strcat(buf, "CAP_REM ");
187          }
188          if (res->res_dev.cap_bits & CAP_RACCESS) {
189             strcat(buf, "CAP_RACCESS ");
190          }
191          if (res->res_dev.cap_bits & CAP_AUTOMOUNT) {
192             strcat(buf, "CAP_AUTOMOUNT ");
193          }
194          if (res->res_dev.cap_bits & CAP_LABEL) {
195             strcat(buf, "CAP_LABEL ");
196          }
197          if (res->res_dev.cap_bits & CAP_ANONVOLS) {
198             strcat(buf, "CAP_ANONVOLS ");
199          }
200          if (res->res_dev.cap_bits & CAP_ALWAYSOPEN) {
201             strcat(buf, "CAP_ALWAYSOPEN ");
202          }
203          strcat(buf, "\n");
204          sendit(sock, buf);
205          break;
206       case R_MSGS:
207          sendit(sock, "Messages: name=%s\n", res->res_msgs.hdr.name);
208          if (res->res_msgs.mail_cmd) 
209             sendit(sock, "      mailcmd=%s\n", res->res_msgs.mail_cmd);
210          if (res->res_msgs.operator_cmd) 
211             sendit(sock, "      opcmd=%s\n", res->res_msgs.operator_cmd);
212          break;
213       default:
214          sendit(sock, _("Warning: unknown resource type %d\n"), type);
215          break;
216    }
217    if (recurse && res->res_dir.hdr.next)
218       dump_resource(type, (RES *)res->res_dir.hdr.next, sendit, sock);
219 }
220
221 /* 
222  * Free memory of resource.  
223  * NB, we don't need to worry about freeing any references
224  * to other resources as they will be freed when that 
225  * resource chain is traversed.  Mainly we worry about freeing
226  * allocated strings (names).
227  */
228 void free_resource(int type)
229 {
230    URES *nres;
231    URES *res;
232    int rindex = type - r_first;
233    res = (URES *)resources[rindex].res_head;
234
235    if (res == NULL)
236       return;
237
238    /* common stuff -- free the resource name */
239    nres = (URES *)res->res_dir.hdr.next;
240    if (res->res_dir.hdr.name) {
241       free(res->res_dir.hdr.name);
242    }
243    if (res->res_dir.hdr.desc) {
244       free(res->res_dir.hdr.desc);
245    }
246
247
248    switch (type) {
249       case R_DIRECTOR:
250          if (res->res_dir.password) {
251             free(res->res_dir.password);
252          }
253          if (res->res_dir.address) {
254             free(res->res_dir.address);
255          }
256          break;
257       case R_STORAGE:
258          if (res->res_store.address) {  /* ***FIXME*** deprecated */
259             free(res->res_store.address);
260          }
261          if (res->res_store.SDaddr) {
262             free(res->res_store.SDaddr);
263          }
264          if (res->res_store.working_directory) {
265             free(res->res_store.working_directory);
266          }
267          if (res->res_store.pid_directory) {
268             free(res->res_store.pid_directory);
269          }
270          if (res->res_store.subsys_directory) {
271             free(res->res_store.subsys_directory);
272          }
273          break;
274       case R_DEVICE:
275          if (res->res_dev.media_type) {
276             free(res->res_dev.media_type);
277          }
278          if (res->res_dev.device_name) {
279             free(res->res_dev.device_name);
280          }
281          if (res->res_dev.changer_name) {
282             free(res->res_dev.changer_name);
283          }
284          if (res->res_dev.changer_command) {
285             free(res->res_dev.changer_command);
286          }
287          break;
288       case R_MSGS:
289          if (res->res_msgs.mail_cmd) {
290             free(res->res_msgs.mail_cmd);
291          }
292          if (res->res_msgs.operator_cmd) {
293             free(res->res_msgs.operator_cmd);
294          }
295          free_msgs_res((MSGS *)res);  /* free message resource */
296          res = NULL;
297          break;
298       default:
299          Dmsg1(0, "Unknown resource type %d\n", type);
300          break;
301    }
302    /* Common stuff again -- free the resource, recurse to next one */
303    if (res) {
304       free(res);
305    }
306    resources[rindex].res_head = (RES *)nres;
307    if (nres) {
308       free_resource(type);
309    }
310 }
311
312 /* Save the new resource by chaining it into the head list for
313  * the resource. If this is pass 2, we update any resource
314  * pointers (currently only in the Job resource).
315  */
316 void save_resource(int type, struct res_items *items, int pass)
317 {
318    URES *res;
319    int rindex = type - r_first;
320    int i, size;
321    int error = 0;
322
323    /* 
324     * Ensure that all required items are present
325     */
326    for (i=0; items[i].name; i++) {
327       if (items[i].flags & ITEM_REQUIRED) {
328          if (!bit_is_set(i, res_all.res_dir.hdr.item_present)) {  
329             Emsg2(M_ABORT, 0, _("%s item is required in %s resource, but not found.\n"),
330               items[i].name, resources[rindex]);
331           }
332       }
333       /* If this triggers, take a look at lib/parse_conf.h */
334       if (i >= MAX_RES_ITEMS) {
335          Emsg1(M_ABORT, 0, _("Too many items in %s resource\n"), resources[rindex]);
336       }
337    }
338
339    /* During pass 2, we looked up pointers to all the resources
340     * referrenced in the current resource, , now we
341     * must copy their address from the static record to the allocated
342     * record.
343     */
344    if (pass == 2) {
345       switch (type) {
346          /* Resources not containing a resource */
347          case R_DIRECTOR:
348          case R_DEVICE:
349          case R_MSGS:
350             break;
351
352          /* Resources containing a resource */
353          case R_STORAGE:
354             if ((res = (URES *)GetResWithName(R_STORAGE, res_all.res_dir.hdr.name)) == NULL) {
355                Emsg1(M_ABORT, 0, "Cannot find Storage resource %s\n", res_all.res_dir.hdr.name);
356             }
357             res->res_store.messages = res_all.res_store.messages;
358             break;
359          default:
360             printf("Unknown resource type %d\n", type);
361             error = 1;
362             break;
363       }
364
365
366       if (res_all.res_dir.hdr.name) {
367          free(res_all.res_dir.hdr.name);
368          res_all.res_dir.hdr.name = NULL;
369       }
370       if (res_all.res_dir.hdr.desc) {
371          free(res_all.res_dir.hdr.desc);
372          res_all.res_dir.hdr.desc = NULL;
373       }
374       return;
375    }
376
377    /* The following code is only executed on pass 1 */
378    switch (type) {
379       case R_DIRECTOR:
380          size = sizeof(DIRRES);
381          break;
382       case R_STORAGE:
383          size = sizeof(STORES);
384          break;
385       case R_DEVICE:
386          size = sizeof(DEVRES);
387          break;
388       case R_MSGS:
389          size = sizeof(MSGS);   
390          break;
391       default:
392          printf("Unknown resource type %d\n", type);
393          error = 1;
394          size = 1;
395          break;
396    }
397    /* Common */
398    if (!error) {
399       res = (URES *)malloc(size);
400       memcpy(res, &res_all, size);
401       if (!resources[rindex].res_head) {
402          resources[rindex].res_head = (RES *)res; /* store first entry */
403       } else {
404          RES *next;
405          /* Add new res to end of chain */
406          for (next=resources[rindex].res_head; next->next; next=next->next)
407             { }
408          next->next = (RES *)res;
409          Dmsg2(90, "Inserting %s res: %s\n", res_to_str(type),
410                res->res_dir.hdr.name);
411       }
412    }
413 }