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