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