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