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