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