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