]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/stored.c
- Integrated TLS network encryption
[bacula/bacula] / bacula / src / stored / stored.c
1 /*
2  * Second generation Storage daemon.
3  *
4  * It accepts a number of simple commands from the File daemon
5  * and acts on them. When a request to append data is made,
6  * it opens a data channel and accepts data from the
7  * File daemon.
8  *
9  *   Version $Id$
10  *
11  */
12 /*
13    Copyright (C) 2000-2005 Kern Sibbald
14
15    This program is free software; you can redistribute it and/or
16    modify it under the terms of the GNU General Public License as
17    published by the Free Software Foundation; either version 2 of
18    the License, or (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23    General Public License for more details.
24
25    You should have received a copy of the GNU General Public
26    License along with this program; if not, write to the Free
27    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28    MA 02111-1307, USA.
29
30  */
31
32 #include "bacula.h"
33 #include "stored.h"
34
35 /* Imported functions */
36
37
38 /* Forward referenced functions */
39 void terminate_stored(int sig);
40 static int check_resources();
41
42 extern "C" void *device_initialization(void *arg);
43
44 #define CONFIG_FILE "bacula-sd.conf"  /* Default config file */
45
46 /* Global variables exported */
47 char OK_msg[]   = "3000 OK\n";
48 char TERM_msg[] = "3999 Terminate\n";
49 STORES *me = NULL;                    /* our Global resource */
50 bool forge_on = false;                /* proceed inspite of I/O errors */
51 pthread_mutex_t device_release_mutex = PTHREAD_MUTEX_INITIALIZER;
52 pthread_cond_t wait_device_release = PTHREAD_COND_INITIALIZER;
53
54
55 static uint32_t VolSessionId = 0;
56 uint32_t VolSessionTime;
57 char *configfile;
58
59 /* Global static variables */
60 static int foreground = 0;
61 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
62 static workq_t dird_workq;            /* queue for processing connections */
63
64
65 static void usage()
66 {
67    fprintf(stderr, _(
68 "Copyright (C) 2000-2005 Kern Sibbald.\n"
69 "\nVersion: " VERSION " (" BDATE ")\n\n"
70 "Usage: stored [options] [-c config_file] [config_file]\n"
71 "        -c <file>   use <file> as configuration file\n"
72 "        -dnn        set debug level to nn\n"
73 "        -f          run in foreground (for debugging)\n"
74 "        -g <group>  set groupid to group\n"
75 "        -p          proceed despite I/O errors\n"
76 "        -s          no signals (for debugging)\n"
77 "        -t          test - read config and exit\n"
78 "        -u <user>   userid to <user>\n"
79 "        -v          verbose user messages\n"
80 "        -?          print this message.\n"
81 "\n"));
82    exit(1);
83 }
84
85 /*********************************************************************
86  *
87  *  Main Bacula Unix Storage Daemon
88  *
89  */
90 int main (int argc, char *argv[])
91 {
92    int ch;
93    int no_signals = FALSE;
94    int test_config = FALSE;
95    pthread_t thid;
96    char *uid = NULL;
97    char *gid = NULL;
98
99    init_stack_dump();
100    my_name_is(argc, argv, "bacula-sd");
101    textdomain("bacula");
102    init_msg(NULL, NULL);
103    daemon_start_time = time(NULL);
104
105    /* Sanity checks */
106    if (TAPE_BSIZE % B_DEV_BSIZE != 0 || TAPE_BSIZE / B_DEV_BSIZE == 0) {
107       Emsg2(M_ABORT, 0, "Tape block size (%d) not multiple of system size (%d)\n",
108          TAPE_BSIZE, B_DEV_BSIZE);
109    }
110    if (TAPE_BSIZE != (1 << (ffs(TAPE_BSIZE)-1))) {
111       Emsg1(M_ABORT, 0, "Tape block size (%d) is not a power of 2\n", TAPE_BSIZE);
112    }
113
114    while ((ch = getopt(argc, argv, "c:d:fg:pstu:v?")) != -1) {
115       switch (ch) {
116       case 'c':                    /* configuration file */
117          if (configfile != NULL) {
118             free(configfile);
119          }
120          configfile = bstrdup(optarg);
121          break;
122
123       case 'd':                    /* debug level */
124          debug_level = atoi(optarg);
125          if (debug_level <= 0) {
126             debug_level = 1;
127          }
128          break;
129
130       case 'f':                    /* run in foreground */
131          foreground = TRUE;
132          break;
133
134       case 'g':                    /* set group id */
135          gid = optarg;
136          break;
137
138       case 'p':                    /* proceed in spite of I/O errors */
139          forge_on = true;
140          break;
141
142       case 's':                    /* no signals */
143          no_signals = TRUE;
144          break;
145
146       case 't':
147          test_config = TRUE;
148          break;
149
150       case 'u':                    /* set uid */
151          uid = optarg;
152          break;
153
154       case 'v':                    /* verbose */
155          verbose++;
156          break;
157
158       case '?':
159       default:
160          usage();
161          break;
162       }
163    }
164    argc -= optind;
165    argv += optind;
166
167    if (argc) {
168       if (configfile != NULL) {
169          free(configfile);
170       }
171       configfile = bstrdup(*argv);
172       argc--;
173       argv++;
174    }
175    if (argc)
176       usage();
177
178    if (!no_signals) {
179       init_signals(terminate_stored);
180    }
181
182    if (configfile == NULL) {
183       configfile = bstrdup(CONFIG_FILE);
184    }
185
186    parse_config(configfile);
187
188 #ifdef HAVE_TLS
189    if (init_tls() != 0) {
190       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("TLS library initialization failed.\n"));
191    }
192 #endif
193
194    if (!check_resources()) {
195       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
196    }
197
198    if (test_config) {
199       terminate_stored(0);
200    }
201
202    my_name_is(0, (char **)NULL, me->hdr.name);     /* Set our real name */
203
204    if (!foreground) {
205       daemon_start();                 /* become daemon */
206       init_stack_dump();              /* pick up new pid */
207    }
208
209    create_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
210    read_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
211
212    drop(uid, gid);
213
214    /* Ensure that Volume Session Time and Id are both
215     * set and are both non-zero.
216     */
217    VolSessionTime = (long)daemon_start_time;
218    if (VolSessionTime == 0) { /* paranoid */
219       Jmsg0(NULL, M_ABORT, 0, _("Volume Session Time is ZERO!\n"));
220    }
221
222    init_python_interpreter(me->hdr.name, me->scripts_directory, "SDStartUp");
223
224    /* Make sure on Solaris we can run concurrent, watch dog + servers + misc */
225    set_thread_concurrency(me->max_concurrent_jobs * 2 + 4);
226
227     /*
228      * Start the device allocation thread
229      */
230    if (pthread_create(&thid, NULL, device_initialization, NULL) != 0) {
231       Emsg1(M_ABORT, 0, _("Unable to create thread. ERR=%s\n"), strerror(errno));
232    }
233
234    start_watchdog();                  /* start watchdog thread */
235
236    init_jcr_subsystem();              /* start JCR watchdogs etc. */
237
238    /*
239     * Sleep a bit to give device thread a chance to lock the resource
240     * chain before we start the server.
241     */
242    bmicrosleep(1, 0);
243
244    /* Single server used for Director and File daemon */
245    bnet_thread_server(me->sdaddrs, me->max_concurrent_jobs * 2 + 1,
246                       &dird_workq, handle_connection_request);
247    exit(1);                           /* to keep compiler quiet */
248 }
249
250 /* Return a new Session Id */
251 uint32_t newVolSessionId()
252 {
253    uint32_t Id;
254
255    P(mutex);
256    VolSessionId++;
257    Id = VolSessionId;
258    V(mutex);
259    return Id;
260 }
261
262 /* Check Configuration file for necessary info */
263 static int check_resources()
264 {
265    bool OK = true;
266    AUTOCHANGER *changer;
267 #ifdef HAVE_TLS
268    DIRRES *director;
269    STORES *store;
270 #endif
271
272    LockRes();
273
274    me = (STORES *)GetNextRes(R_STORAGE, NULL);
275    if (!me) {
276       Jmsg1(NULL, M_ERROR, 0, _("No Storage resource defined in %s. Cannot continue.\n"),
277          configfile);
278       OK = false;
279    }
280
281    if (GetNextRes(R_STORAGE, (RES *)me) != NULL) {
282       Jmsg1(NULL, M_ERROR, 0, _("Only one Storage resource permitted in %s\n"),
283          configfile);
284       OK = false;
285    }
286    if (GetNextRes(R_DIRECTOR, NULL) == NULL) {
287       Jmsg1(NULL, M_ERROR, 0, _("No Director resource defined in %s. Cannot continue.\n"),
288          configfile);
289       OK = false;
290    }
291    if (GetNextRes(R_DEVICE, NULL) == NULL){
292       Jmsg1(NULL, M_ERROR, 0, _("No Device resource defined in %s. Cannot continue.\n"),
293            configfile);
294       OK = false;
295    }
296
297    if (!me->messages) {
298       me->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
299       if (!me->messages) {
300          Jmsg1(NULL, M_ERROR, 0, _("No Messages resource defined in %s. Cannot continue.\n"),
301             configfile);
302          OK = false;
303       }
304    }
305
306    if (!me->working_directory) {
307       Jmsg1(NULL, M_ERROR, 0, _("No Working Directory defined in %s. Cannot continue.\n"),
308          configfile);
309       OK = false;
310    }
311
312 #ifdef HAVE_TLS
313    foreach_res(store, R_STORAGE) { 
314       /* tls_require implies tls_enable */
315       if (store->tls_require) {
316          store->tls_enable = true;
317       }
318
319       if (!store->tls_certfile && store->tls_enable) {
320          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Storage \"%s\" in %s.\n"),
321               store->hdr.name, configfile);
322          OK = false;
323       }
324
325       if (!store->tls_keyfile && store->tls_enable) {
326          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Storage \"%s\" in %s.\n"),
327               store->hdr.name, configfile);
328          OK = false;
329       }
330
331       if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && store->tls_enable && store->tls_verify_peer) {
332          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
333               " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s."
334               " At least one CA certificate store is required"
335               " when using \"TLS Verify Peer\".\n"),
336               store->hdr.name, configfile);
337          OK = false;
338       }
339
340       /* If everything is well, attempt to initialize our per-resource TLS context */
341       if (OK && (store->tls_enable || store->tls_require)) {
342          /* Initialize TLS context:
343           * Args: CA certfile, CA certdir, Certfile, Keyfile,
344           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
345          store->tls_ctx = new_tls_context(store->tls_ca_certfile,
346             store->tls_ca_certdir, store->tls_certfile,
347             store->tls_keyfile, NULL, NULL, store->tls_dhfile,
348             store->tls_verify_peer);
349
350          if (!store->tls_ctx) { 
351             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
352                  store->hdr.name, configfile);
353             OK = false;
354          }
355       }
356    }
357 #endif /* HAVE_TLS */
358
359
360 #ifdef HAVE_TLS
361    foreach_res(director, R_DIRECTOR) { 
362       /* tls_require implies tls_enable */
363       if (director->tls_require) {
364          director->tls_enable = true;
365       }
366
367       if (!director->tls_certfile && director->tls_enable) {
368          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
369               director->hdr.name, configfile);
370          OK = false;
371       }
372
373       if (!director->tls_keyfile && director->tls_enable) {
374          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
375               director->hdr.name, configfile);
376          OK = false;
377       }
378
379       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) && director->tls_enable && director->tls_verify_peer) {
380          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
381               " or \"TLS CA Certificate Dir\" are defined for Director \"%s\" in %s."
382               " At least one CA certificate store is required"
383               " when using \"TLS Verify Peer\".\n"),
384               director->hdr.name, configfile);
385          OK = false;
386       }
387
388       /* If everything is well, attempt to initialize our per-resource TLS context */
389       if (OK && (director->tls_enable || director->tls_require)) {
390          /* Initialize TLS context:
391           * Args: CA certfile, CA certdir, Certfile, Keyfile,
392           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
393          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
394             director->tls_ca_certdir, director->tls_certfile,
395             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
396             director->tls_verify_peer);
397
398          if (!director->tls_ctx) { 
399             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
400                  director->hdr.name, configfile);
401             OK = false;
402          }
403       }
404    }
405 #endif /* HAVE_TLS */
406
407    /* Ensure that the media_type for each device is the same */
408    foreach_res(changer, R_AUTOCHANGER) {
409       DEVRES *device;
410       char *media_type = NULL;
411       foreach_alist(device, changer->device) {
412          if (media_type == NULL) {
413             media_type = device->media_type;
414             continue;
415          }     
416          if (strcmp(media_type, device->media_type) != 0) {
417             Jmsg(NULL, M_ERROR, 0, 
418                _("Media Type not the same for all devices in changer %s. Cannot continue.\n"),
419                changer->hdr.name);
420             OK = false;
421             continue;
422          }
423          /*
424           * If the device does not have a changer name or changer command
425           * defined, used the one from the Autochanger resource 
426           */
427          if (!device->changer_name) {
428             device->changer_name = bstrdup(changer->changer_name);
429          }
430          if (!device->changer_command) {
431             device->changer_command = bstrdup(changer->changer_command);
432          }
433       }
434    }
435    
436    UnlockRes();
437
438    if (OK) {
439       close_msg(NULL);                   /* close temp message handler */
440       init_msg(NULL, me->messages);      /* open daemon message handler */
441       set_working_directory(me->working_directory);
442    }
443
444    return OK;
445 }
446
447 /*
448  * Here we attempt to init and open each device. This is done
449  *  once at startup in a separate thread.
450  */
451 extern "C"
452 void *device_initialization(void *arg)
453 {
454    DEVRES *device;
455
456    LockRes();
457    pthread_detach(pthread_self());
458
459    foreach_res(device, R_DEVICE) {
460       Dmsg1(90, "calling init_dev %s\n", device->device_name);
461       device->dev = init_dev(NULL, NULL, device);
462       Dmsg1(10, "SD init done %s\n", device->device_name);
463       if (!device->dev) {
464          Jmsg1(NULL, M_ERROR, 0, _("Could not initialize %s\n"), device->device_name);
465          continue;
466       }
467
468       if (device->cap_bits & CAP_ALWAYSOPEN) {
469          Dmsg1(20, "calling first_open_device %s\n", device->device_name);
470          if (!first_open_device(device->dev)) {
471             Jmsg1(NULL, M_ERROR, 0, _("Could not open device %s\n"), device->device_name);
472          }
473       }
474       if (device->cap_bits & CAP_AUTOMOUNT && device->dev &&
475           device->dev->is_open()) {
476          JCR *jcr;
477          DCR *dcr;
478          jcr = new_jcr(sizeof(JCR), stored_free_jcr);
479          jcr->JobType = JT_SYSTEM;
480          /* Initialize FD start condition variable */
481          int errstat = pthread_cond_init(&jcr->job_start_wait, NULL);
482          if (errstat != 0) {
483             Jmsg1(jcr, M_ABORT, 0, _("Unable to init job cond variable: ERR=%s\n"), strerror(errstat));
484          }
485          dcr = new_dcr(jcr, device->dev);
486          switch (read_dev_volume_label(dcr)) {
487          case VOL_OK:
488             memcpy(&dcr->dev->VolCatInfo, &dcr->VolCatInfo, sizeof(dcr->dev->VolCatInfo));
489             break;
490          default:
491             Jmsg1(NULL, M_WARNING, 0, _("Could not mount device %s\n"), device->device_name);
492             break;
493          }
494          free_jcr(jcr);
495       }
496    }
497    UnlockRes();
498    return NULL;
499 }
500
501
502 /* Clean up and then exit */
503 void terminate_stored(int sig)
504 {
505    static bool in_here = false;
506    DEVRES *device;
507    JCR *jcr;
508
509    if (in_here) {                     /* prevent loops */
510       exit(1);
511    }
512    in_here = true;
513
514    if (sig == SIGTERM) {              /* normal shutdown request? */
515       /*
516        * This is a normal shutdown request. We wiffle through
517        *   all open jobs canceling them and trying to wake
518        *   them up so that they will report back the correct
519        *   volume status.
520        */
521       lock_jcr_chain();
522       foreach_jcr(jcr) {
523          BSOCK *fd;
524          free_locked_jcr(jcr);
525          if (jcr->JobId == 0) {
526             continue;                 /* ignore console */
527          }
528          set_jcr_job_status(jcr, JS_Canceled);
529          fd = jcr->file_bsock;
530          if (fd) {
531             fd->timed_out = true;
532             Dmsg1(100, "term_stored killing JobId=%d\n", jcr->JobId);
533             pthread_kill(jcr->my_thread_id, TIMEOUT_SIGNAL);
534             /* ***FIXME*** wiffle through all dcrs */
535             if (jcr->dcr && jcr->dcr->dev && jcr->dcr->dev->dev_blocked) {
536                pthread_cond_broadcast(&jcr->dcr->dev->wait_next_vol);
537                pthread_cond_broadcast(&wait_device_release);
538             }
539             bmicrosleep(0, 50000);
540           }
541       }
542       unlock_jcr_chain();
543       bmicrosleep(0, 500000);         /* give them 1/2 sec to clean up */
544    }
545
546    write_state_file(me->working_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
547    delete_pid_file(me->pid_directory, "bacula-sd", get_first_port_host_order(me->sdaddrs));
548
549    Dmsg1(200, "In terminate_stored() sig=%d\n", sig);
550
551    LockRes();
552    foreach_res(device, R_DEVICE) {
553       if (device->dev) {
554          term_dev(device->dev);
555       }
556    }
557    UnlockRes();
558
559    if (configfile)
560    free(configfile);
561    free_config_resources();
562
563    if (debug_level > 10) {
564       print_memory_pool_stats();
565    }
566    term_msg();
567    stop_watchdog();
568 #ifdef HAVE_TLS
569    cleanup_tls();
570 #endif
571    close_memory_pool();
572
573    sm_dump(false);                    /* dump orphaned buffers */
574    exit(sig);
575 }