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