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