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