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