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