]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/dird.c
Partial modification of copyrights as per agreement with FSFE
[bacula/bacula] / bacula / src / dird / dird.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *
21  *   Bacula Director daemon -- this is the main program
22  *
23  *     Kern Sibbald, March MM
24  *
25  */
26
27 #include "bacula.h"
28 #include "dird.h"
29 #ifndef HAVE_REGEX_H
30 #include "lib/bregex.h"
31 #else
32 #include <regex.h>
33 #endif
34 #ifdef HAVE_DIRENT_H
35 #include <dirent.h>
36 #define NAMELEN(dirent) (strlen((dirent)->d_name))
37 #endif
38 #ifndef HAVE_READDIR_R
39 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
40 #endif
41
42 /* Forward referenced subroutines */
43 void terminate_dird(int sig);
44 static bool check_resources();
45 static void cleanup_old_files();
46
47 /* Exported subroutines */
48 extern "C" void reload_config(int sig);
49 extern void invalidate_schedules();
50 extern bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code);
51
52 /* Imported subroutines */
53 JCR *wait_for_next_job(char *runjob);
54 void term_scheduler();
55 void term_ua_server();
56 void start_UA_server(dlist *addrs);
57 void init_job_server(int max_workers);
58 void term_job_server();
59 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
60 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
61 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
62 void store_migtype(LEX *lc, RES_ITEM *item, int index, int pass);
63 void init_device_resources();
64
65
66 static char *runjob = NULL;
67 static bool background = true;
68 static void init_reload(void);
69 static CONFIG *config;
70 static bool test_config = false;
71
72 /* Globals Exported */
73 DIRRES *director;                     /* Director resource */
74 int FDConnectTimeout;
75 int SDConnectTimeout;
76 char *configfile = NULL;
77 void *start_heap;
78 utime_t last_reload_time = 0;
79
80 /* Globals Imported */
81 extern RES_ITEM job_items[];
82 #if defined(_MSC_VER)
83 extern "C" { // work around visual compiler mangling variables
84    extern URES res_all;
85 }
86 #else
87 extern URES res_all;
88 #endif
89
90 typedef enum {
91    CHECK_CONNECTION,  /* Check catalog connection */
92    UPDATE_CATALOG,    /* Ensure that catalog is ok with conf */
93    UPDATE_AND_FIX     /* Ensure that catalog is ok, and fix old jobs */
94 } cat_op;
95 static bool check_catalog(cat_op mode);
96
97 #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */
98
99 static bool dir_sql_query(JCR *jcr, const char *cmd) 
100
101    if (jcr && jcr->db && jcr->db->is_connected()) {
102       return db_sql_query(jcr->db, cmd, NULL, NULL);
103    } 
104    return false; 
105
106
107 static bool dir_sql_escape(JCR *jcr, BDB *mdb, char *snew, char *sold, int len) 
108
109    if (jcr && jcr->db && jcr->db->is_connected()) { 
110       db_escape_string(jcr, mdb, snew, sold, len);
111       return true;
112    } 
113    return false; 
114
115
116 static void usage()
117 {
118    fprintf(stderr, _(
119       PROG_COPYRIGHT
120       "\nVersion: %s (%s)\n\n"
121       "Usage: bacula-dir [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
122       "     -c <file>        set configuration file to file\n"
123       "     -d <nn>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
124       "     -dt              print timestamp in debug output\n"
125       "     -T               set trace on\n"
126       "     -f               run in foreground (for debugging)\n"
127       "     -g               groupid\n"
128       "     -m               print kaboom output (for debugging)\n"
129       "     -r <job>         run <job> now\n"
130       "     -s               no signals\n"
131       "     -t               test - read configuration and exit\n"
132       "     -u               userid\n"
133       "     -v               verbose user messages\n"
134       "     -?               print this message.\n"
135       "\n"), 2000, VERSION, BDATE);
136
137    exit(1);
138 }
139
140 /*
141  * !!! WARNING !!! Use this function only when bacula is stopped.
142  * ie, after a fatal signal and before exiting the program
143  * Print information about a JCR
144  */
145 static void dir_debug_print(JCR *jcr, FILE *fp)
146 {
147    fprintf(fp, "\twstore=%p rstore=%p wjcr=%p client=%p reschedule_count=%d SD_msg_chan_started=%d\n",
148            jcr->wstore, jcr->rstore, jcr->wjcr, jcr->client, jcr->reschedule_count, (int)jcr->SD_msg_chan_started);
149 }
150
151 /*********************************************************************
152  *
153  *         Main Bacula Director Server program
154  *
155  */
156 #if defined(HAVE_WIN32)
157 /* For Win32 main() is in src/win32 code ... */
158 #define main BaculaMain
159 #endif
160
161 int main (int argc, char *argv[])
162 {
163    int ch;
164    JCR *jcr;
165    bool no_signals = false;
166    char *uid = NULL;
167    char *gid = NULL;
168
169    start_heap = sbrk(0);
170    setlocale(LC_ALL, "");
171    bindtextdomain("bacula", LOCALEDIR);
172    textdomain("bacula");
173
174    init_stack_dump();
175    my_name_is(argc, argv, "bacula-dir");
176    init_msg(NULL, NULL);              /* initialize message handler */
177    init_reload();
178    daemon_start_time = time(NULL);
179
180    console_command = run_console_command;
181
182    while ((ch = getopt(argc, argv, "c:d:fg:mr:stu:v?T")) != -1) {
183       switch (ch) {
184       case 'c':                    /* specify config file */
185          if (configfile != NULL) {
186             free(configfile);
187          }
188          configfile = bstrdup(optarg);
189          break;
190
191       case 'd':                    /* set debug level */
192          if (*optarg == 't') {
193             dbg_timestamp = true;
194          } else {
195             char *p;
196             /* We probably find a tag list -d 10,sql,bvfs */
197             if ((p = strchr(optarg, ',')) != NULL) {
198                *p = 0;
199             }
200             debug_level = atoi(optarg);
201             if (debug_level <= 0) {
202                debug_level = 1;
203             }
204             if (p) {
205                debug_parse_tags(p+1, &debug_level_tags);
206             }
207          }
208          Dmsg1(10, "Debug level = %lld\n", debug_level);
209          break;
210
211       case 'T':
212          set_trace(true);
213          break;
214
215       case 'f':                    /* run in foreground */
216          background = false;
217          break;
218
219       case 'g':                    /* set group id */
220          gid = optarg;
221          break;
222
223       case 'm':                    /* print kaboom output */
224          prt_kaboom = true;
225          break;
226
227       case 'r':                    /* run job */
228          if (runjob != NULL) {
229             free(runjob);
230          }
231          if (optarg) {
232             runjob = bstrdup(optarg);
233          }
234          break;
235
236       case 's':                    /* turn off signals */
237          no_signals = true;
238          break;
239
240       case 't':                    /* test config */
241          test_config = true;
242          break;
243
244       case 'u':                    /* set uid */
245          uid = optarg;
246          break;
247
248       case 'v':                    /* verbose */
249          verbose++;
250          break;
251
252       case '?':
253       default:
254          usage();
255
256       }
257    }
258    argc -= optind;
259    argv += optind;
260
261    if (!no_signals) {
262       init_signals(terminate_dird);
263    }
264
265    if (argc) {
266       if (configfile != NULL) {
267          free(configfile);
268       }
269       configfile = bstrdup(*argv);
270       argc--;
271       argv++;
272    }
273    if (argc) {
274       usage();
275    }
276
277    if (!test_config) {                /* we don't need to do this block in test mode */
278       if (background) {
279          daemon_start();
280          init_stack_dump();              /* grab new pid */
281       }
282    }
283
284    if (configfile == NULL) {
285       configfile = bstrdup(CONFIG_FILE);
286    }
287
288    config = new_config_parser();
289
290    parse_dir_config(config, configfile, M_ERROR_TERM);
291
292    if (init_crypto() != 0) {
293       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
294    }
295
296    if (!check_resources()) {
297       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
298    }
299
300    if (!test_config) {
301       /* Create pid must come after we are a daemon -- so we have our final pid */
302       create_pid_file(director->pid_directory, "bacula-dir",
303                       get_first_port_host_order(director->DIRaddrs));
304       read_state_file(director->working_directory, "bacula-dir",
305                       get_first_port_host_order(director->DIRaddrs));
306    }
307
308    set_jcr_in_tsd(INVALID_JCR);
309    set_thread_concurrency(director->MaxConcurrentJobs * 2 +
310                           4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */);
311    lmgr_init_thread(); /* initialize the lockmanager stack */
312
313    load_dir_plugins(director->plugin_directory);
314
315    drop(uid, gid, false);                    /* reduce privileges if requested */
316
317    /* If we are in testing mode, we don't try to fix the catalog */
318    cat_op mode=(test_config)?CHECK_CONNECTION:UPDATE_AND_FIX;
319
320    if (!check_catalog(mode)) {
321       Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
322    }
323
324    if (test_config) {
325       terminate_dird(0);
326    }
327
328    my_name_is(0, NULL, director->name());    /* set user defined name */
329
330    cleanup_old_files();
331
332    /* Plug database interface for library routines */
333    p_sql_query = (sql_query_call)dir_sql_query;
334    p_sql_escape = (sql_escape_call)dir_sql_escape;
335
336    FDConnectTimeout = (int)director->FDConnectTimeout;
337    SDConnectTimeout = (int)director->SDConnectTimeout;
338
339 #if !defined(HAVE_WIN32)
340    signal(SIGHUP, reload_config);
341 #endif
342
343    init_console_msg(working_directory);
344
345    Dmsg0(200, "Start UA server\n");
346    start_UA_server(director->DIRaddrs);
347
348    start_watchdog();                  /* start network watchdog thread */
349
350    init_jcr_subsystem();              /* start JCR watchdogs etc. */
351
352    init_job_server(director->MaxConcurrentJobs);
353
354    dbg_jcr_add_hook(dir_debug_print); /* used to director variables */
355    dbg_jcr_add_hook(bdb_debug_print);     /* used to debug B_DB connexion after fatal signal */
356
357 //   init_device_resources();
358
359    Dmsg0(200, "wait for next job\n");
360    /* Main loop -- call scheduler to get next job to run */
361    while ( (jcr = wait_for_next_job(runjob)) ) {
362       run_job(jcr);                   /* run job */
363       free_jcr(jcr);                  /* release jcr */
364       set_jcr_in_tsd(INVALID_JCR);
365       if (runjob) {                   /* command line, run a single job? */
366          break;                       /* yes, terminate */
367       }
368    }
369
370    terminate_dird(0);
371
372    return 0;
373 }
374
375 struct RELOAD_TABLE {
376    int job_count;
377    RES **res_table;
378 };
379
380 static const int max_reloads = 50;
381 static RELOAD_TABLE reload_table[max_reloads];
382
383 static void init_reload(void)
384 {
385    for (int i=0; i < max_reloads; i++) {
386       reload_table[i].job_count = 0;
387       reload_table[i].res_table = NULL;
388    }
389 }
390
391 /*
392  * This subroutine frees a saved resource table.
393  *  It was saved when a new table was created with "reload"
394  */
395 static void free_saved_resources(int table)
396 {
397    int num = r_last - r_first + 1;
398    RES **res_tab = reload_table[table].res_table;
399    if (!res_tab) {
400       Dmsg1(100, "res_tab for table %d already released.\n", table);
401       return;
402    }
403    Dmsg1(100, "Freeing resources for table %d\n", table);
404    for (int j=0; j<num; j++) {
405       free_resource(res_tab[j], r_first + j);
406    }
407    free(res_tab);
408    reload_table[table].job_count = 0;
409    reload_table[table].res_table = NULL;
410 }
411
412 /*
413  * Called here at the end of every job that was
414  * hooked decrementing the active job_count. When
415  * it goes to zero, no one is using the associated
416  * resource table, so free it.
417  */
418 static void reload_job_end_cb(JCR *jcr, void *ctx)
419 {
420    int reload_id = (int)((intptr_t)ctx);
421    Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
422       reload_id, reload_table[reload_id].job_count);
423    lock_jobs();
424    LockRes();
425    if (--reload_table[reload_id].job_count <= 0) {
426       free_saved_resources(reload_id);
427    }
428    UnlockRes();
429    unlock_jobs();
430 }
431
432 static int find_free_reload_table_entry()
433 {
434    int table = -1;
435    for (int i=0; i < max_reloads; i++) {
436       if (reload_table[i].res_table == NULL) {
437          table = i;
438          break;
439       }
440    }
441    return table;
442 }
443
444 /*
445  * If we get here, we have received a SIGHUP, which means to
446  *    reread our configuration file.
447  *
448  * The algorithm used is as follows: we count how many jobs are
449  *   running and mark the running jobs to make a callback on
450  *   exiting. The old config is saved with the reload table
451  *   id in a reload table. The new config file is read. Now, as
452  *   each job exits, it calls back to the reload_job_end_cb(), which
453  *   decrements the count of open jobs for the given reload table.
454  *   When the count goes to zero, we release those resources.
455  *   This allows us to have pointers into the resource table (from
456  *   jobs), and once they exit and all the pointers are released, we
457  *   release the old table. Note, if no new jobs are running since the
458  *   last reload, then the old resources will be immediately release.
459  *   A console is considered a job because it may have pointers to
460  *   resources, but a SYSTEM job is not since it *should* not have any
461  *   permanent pointers to jobs.
462  */
463 extern "C"
464 void reload_config(int sig)
465 {
466    static bool already_here = false;
467 #if !defined(HAVE_WIN32)
468    sigset_t set;
469 #endif
470    JCR *jcr;
471    int njobs = 0;                     /* number of running jobs */
472    int table, rtable;
473    bool ok;
474
475    if (already_here) {
476       abort();                        /* Oops, recursion -> die */
477    }
478    already_here = true;
479
480 #if !defined(HAVE_WIN32)
481    sigemptyset(&set);
482    sigaddset(&set, SIGHUP);
483    sigprocmask(SIG_BLOCK, &set, NULL);
484 #endif
485
486    lock_jobs();
487    LockRes();
488
489    table = find_free_reload_table_entry();
490    if (table < 0) {
491       Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. Request ignored.\n"));
492       goto bail_out;
493    }
494
495    Dmsg1(100, "Reload_config njobs=%d\n", njobs);
496    reload_table[table].res_table = config->save_resources();
497    Dmsg1(100, "Saved old config in table %d\n", table);
498
499    ok = parse_dir_config(config, configfile, M_ERROR);
500
501    Dmsg0(100, "Reloaded config file\n");
502    if (!ok || !check_resources() || !check_catalog(UPDATE_CATALOG)) {
503       rtable = find_free_reload_table_entry();    /* save new, bad table */
504       if (rtable < 0) {
505          Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
506          Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
507       } else {
508          Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
509          Jmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
510       }
511       reload_table[rtable].res_table = config->save_resources();
512       /* Now restore old resource values */
513       int num = r_last - r_first + 1;
514       RES **res_tab = reload_table[table].res_table;
515       for (int i=0; i<num; i++) {
516          res_head[i] = res_tab[i];
517       }
518       table = rtable;                 /* release new, bad, saved table below */
519    } else {
520       invalidate_schedules();
521       /*
522        * Hook all active jobs so that they release this table
523        */
524       foreach_jcr(jcr) {
525          if (jcr->getJobType() != JT_SYSTEM) {
526             reload_table[table].job_count++;
527             job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
528             njobs++;
529          }
530       }
531       endeach_jcr(jcr);
532    }
533
534    /* Reset globals */
535    set_working_directory(director->working_directory);
536    FDConnectTimeout = director->FDConnectTimeout;
537    SDConnectTimeout = director->SDConnectTimeout;
538    Dmsg0(10, "Director's configuration file reread.\n");
539
540    /* Now release saved resources, if no jobs using the resources */
541    if (njobs == 0) {
542       free_saved_resources(table);
543    }
544
545 bail_out:
546    UnlockRes();
547    unlock_jobs();
548 #if !defined(HAVE_WIN32)
549    sigprocmask(SIG_UNBLOCK, &set, NULL);
550    signal(SIGHUP, reload_config);
551 #endif
552    already_here = false;
553 }
554
555 /* Cleanup and then exit */
556 void terminate_dird(int sig)
557 {
558    static bool already_here = false;
559
560    if (already_here) {                /* avoid recursive temination problems */
561       bmicrosleep(2, 0);              /* yield */
562       exit(1);
563    }
564    already_here = true;
565    debug_level = 0;                   /* turn off debug */
566    stop_watchdog();
567    generate_daemon_event(NULL, "Exit");
568    unload_plugins();
569    if (!test_config) {
570       write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
571       delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
572    }
573    term_scheduler();
574    term_job_server();
575    if (runjob) {
576       free(runjob);
577    }
578    if (configfile != NULL) {
579       free(configfile);
580    }
581    if (chk_dbglvl(5)) {
582       print_memory_pool_stats();
583    }
584    if (config) {
585       config->free_resources();
586       free(config);
587       config = NULL;
588    }
589    term_ua_server();
590    term_msg();                        /* terminate message handler */
591    cleanup_crypto();
592    close_memory_pool();               /* release free memory in pool */
593    lmgr_cleanup_main();
594    sm_dump(false);
595    exit(sig);
596 }
597
598 /*
599  * Make a quick check to see that we have all the
600  * resources needed.
601  *
602  *  **** FIXME **** this routine could be a lot more
603  *   intelligent and comprehensive.
604  */
605 static bool check_resources()
606 {
607    bool OK = true;
608    JOB *job;
609    bool need_tls;
610
611    LockRes();
612
613    job = (JOB *)GetNextRes(R_JOB, NULL);
614    director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
615    if (!director) {
616       Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
617 "Without that I don't know who I am :-(\n"), configfile);
618       OK = false;
619    } else {
620       set_working_directory(director->working_directory);
621       if (!director->messages) {       /* If message resource not specified */
622          director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
623          if (!director->messages) {
624             Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
625             OK = false;
626          }
627       }
628       if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
629          Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
630             configfile);
631          OK = false;
632       }
633       /* tls_require implies tls_enable */
634       if (director->tls_require) {
635          if (have_tls) {
636             director->tls_enable = true;
637          } else {
638             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
639             OK = false;
640          }
641       }
642
643       need_tls = director->tls_enable || director->tls_authenticate;
644
645       if (!director->tls_certfile && need_tls) {
646          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
647             director->name(), configfile);
648          OK = false;
649       }
650
651       if (!director->tls_keyfile && need_tls) {
652          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
653             director->name(), configfile);
654          OK = false;
655       }
656
657       if ((!director->tls_ca_certfile && !director->tls_ca_certdir) &&
658            need_tls && director->tls_verify_peer) {
659          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
660               " Certificate Dir\" are defined for Director \"%s\" in %s."
661               " At least one CA certificate store is required"
662               " when using \"TLS Verify Peer\".\n"),
663               director->name(), configfile);
664          OK = false;
665       }
666
667       /* If everything is well, attempt to initialize our per-resource TLS context */
668       if (OK && (need_tls || director->tls_require)) {
669          /* Initialize TLS context:
670           * Args: CA certfile, CA certdir, Certfile, Keyfile,
671           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
672          director->tls_ctx = new_tls_context(director->tls_ca_certfile,
673             director->tls_ca_certdir, director->tls_certfile,
674             director->tls_keyfile, NULL, NULL, director->tls_dhfile,
675             director->tls_verify_peer);
676
677          if (!director->tls_ctx) {
678             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
679                  director->name(), configfile);
680             OK = false;
681          }
682       }
683    }
684
685    if (!job) {
686       Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
687       OK = false;
688    }
689    foreach_res(job, R_JOB) {
690       int i;
691
692       if (job->jobdefs) {
693          /* Handle Storage alists specifically */
694          JOB *jobdefs = job->jobdefs;
695          if (jobdefs->storage && !job->storage) {
696             STORE *st;
697             job->storage = New(alist(10, not_owned_by_alist));
698             foreach_alist(st, jobdefs->storage) {
699                job->storage->append(st);
700             }
701          }
702          /* Handle RunScripts alists specifically */
703          if (jobdefs->RunScripts) {
704             RUNSCRIPT *rs, *elt;
705
706             if (!job->RunScripts) {
707                job->RunScripts = New(alist(10, not_owned_by_alist));
708             }
709
710             foreach_alist(rs, jobdefs->RunScripts) {
711                elt = copy_runscript(rs);
712                job->RunScripts->append(elt); /* we have to free it */
713             }
714          }
715
716          /* Transfer default items from JobDefs Resource */
717          for (i=0; job_items[i].name; i++) {
718             char **def_svalue, **svalue;  /* string value */
719             uint32_t *def_ivalue, *ivalue;     /* integer value */
720             bool *def_bvalue, *bvalue;    /* bool value */
721             int64_t *def_lvalue, *lvalue; /* 64 bit values */
722             uint32_t offset;
723
724             Dmsg4(1400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
725                 job->name(), job_items[i].name,
726                 bit_is_set(i, job->hdr.item_present),
727                 bit_is_set(i, job->jobdefs->hdr.item_present));
728
729             if (!bit_is_set(i, job->hdr.item_present) &&
730                  bit_is_set(i, job->jobdefs->hdr.item_present)) {
731                Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
732                  job->name(), job_items[i].name);
733                offset = (char *)(job_items[i].value) - (char *)&res_all;
734                /*
735                 * Handle strings and directory strings
736                 */
737                if (job_items[i].handler == store_str ||
738                    job_items[i].handler == store_dir) {
739                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
740                   Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
741                        job->name(), job_items[i].name, *def_svalue, i, offset);
742                   svalue = (char **)((char *)job + offset);
743                   if (*svalue) {
744                      Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
745                   }
746                   *svalue = bstrdup(*def_svalue);
747                   set_bit(i, job->hdr.item_present);
748                /*
749                 * Handle resources
750                 */
751                } else if (job_items[i].handler == store_res) {
752                   def_svalue = (char **)((char *)(job->jobdefs) + offset);
753                   Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
754                        job->name(), job_items[i].name, i, offset);
755                   svalue = (char **)((char *)job + offset);
756                   if (*svalue) {
757                      Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
758                   }
759                   *svalue = *def_svalue;
760                   set_bit(i, job->hdr.item_present);
761                /*
762                 * Handle alist resources
763                 */
764                } else if (job_items[i].handler == store_alist_res) {
765                   if (bit_is_set(i, job->jobdefs->hdr.item_present)) {
766                      set_bit(i, job->hdr.item_present);
767                   }
768                /*
769                 * Handle integer fields
770                 *    Note, our store_bit does not handle bitmaped fields
771                 */
772                } else if (job_items[i].handler == store_bit     ||
773                           job_items[i].handler == store_pint32  ||
774                           job_items[i].handler == store_jobtype ||
775                           job_items[i].handler == store_level   ||
776                           job_items[i].handler == store_int32   ||
777                           job_items[i].handler == store_size32  ||
778                           job_items[i].handler == store_migtype ||
779                           job_items[i].handler == store_replace) {
780                   def_ivalue = (uint32_t *)((char *)(job->jobdefs) + offset);
781                   Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
782                        job->name(), job_items[i].name, *def_ivalue, i, offset);
783                   ivalue = (uint32_t *)((char *)job + offset);
784                   *ivalue = *def_ivalue;
785                   set_bit(i, job->hdr.item_present);
786                /*
787                 * Handle 64 bit integer fields
788                 */
789                } else if (job_items[i].handler == store_time   ||
790                           job_items[i].handler == store_size64 ||
791                           job_items[i].handler == store_speed  ||
792                           job_items[i].handler == store_int64) {
793                   def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
794                   Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
795                        job->name(), job_items[i].name, *def_lvalue, i, offset);
796                   lvalue = (int64_t *)((char *)job + offset);
797                   *lvalue = *def_lvalue;
798                   set_bit(i, job->hdr.item_present);
799                /*
800                 * Handle bool fields
801                 */
802                } else if (job_items[i].handler == store_bool) {
803                   def_bvalue = (bool *)((char *)(job->jobdefs) + offset);
804                   Dmsg5(400, "Job \"%s\", field \"%s\" def_bvalue=%d item %d offset=%u\n",
805                        job->name(), job_items[i].name, *def_bvalue, i, offset);
806                   bvalue = (bool *)((char *)job + offset);
807                   *bvalue = *def_bvalue;
808                   set_bit(i, job->hdr.item_present);
809                }
810             }
811          }
812       }
813       /*
814        * Ensure that all required items are present
815        */
816       for (i=0; job_items[i].name; i++) {
817          if (job_items[i].flags & ITEM_REQUIRED) {
818                if (!bit_is_set(i, job->hdr.item_present)) {
819                   Jmsg(NULL, M_ERROR_TERM, 0, _("\"%s\" directive in Job \"%s\" resource is required, but not found.\n"),
820                     job_items[i].name, job->name());
821                   OK = false;
822                 }
823          }
824          /* If this triggers, take a look at lib/parse_conf.h */
825          if (i >= MAX_RES_ITEMS) {
826             Emsg0(M_ERROR_TERM, 0, _("Too many items in Job resource\n"));
827          }
828       }
829       if (!job->storage && !job->pool->storage) {
830          Jmsg(NULL, M_FATAL, 0, _("No storage specified in Job \"%s\" nor in Pool.\n"),
831             job->name());
832          OK = false;
833       }
834    } /* End loop over Job res */
835
836
837    /* Loop over Consoles */
838    CONRES *cons;
839    foreach_res(cons, R_CONSOLE) {
840       /* tls_require implies tls_enable */
841       if (cons->tls_require) {
842          if (have_tls) {
843             cons->tls_enable = true;
844          } else {
845             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
846             OK = false;
847             continue;
848          }
849       }
850
851       need_tls = cons->tls_enable || cons->tls_authenticate;
852
853       if (!cons->tls_certfile && need_tls) {
854          Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Console \"%s\" in %s.\n"),
855             cons->name(), configfile);
856          OK = false;
857       }
858
859       if (!cons->tls_keyfile && need_tls) {
860          Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Console \"%s\" in %s.\n"),
861             cons->name(), configfile);
862          OK = false;
863       }
864
865       if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir)
866             && need_tls && cons->tls_verify_peer) {
867          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
868             " Certificate Dir\" are defined for Console \"%s\" in %s."
869             " At least one CA certificate store is required"
870             " when using \"TLS Verify Peer\".\n"),
871             cons->name(), configfile);
872          OK = false;
873       }
874       /* If everything is well, attempt to initialize our per-resource TLS context */
875       if (OK && (need_tls || cons->tls_require)) {
876          /* Initialize TLS context:
877           * Args: CA certfile, CA certdir, Certfile, Keyfile,
878           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
879          cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
880             cons->tls_ca_certdir, cons->tls_certfile,
881             cons->tls_keyfile, NULL, NULL, cons->tls_dhfile, cons->tls_verify_peer);
882
883          if (!cons->tls_ctx) {
884             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
885                cons->name(), configfile);
886             OK = false;
887          }
888       }
889
890    }
891
892    /* Loop over Clients */
893    CLIENT *client;
894    foreach_res(client, R_CLIENT) {
895       /* tls_require implies tls_enable */
896       if (client->tls_require) {
897          if (have_tls) {
898             client->tls_enable = true;
899          } else {
900             Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
901             OK = false;
902             continue;
903          }
904       }
905       need_tls = client->tls_enable || client->tls_authenticate;
906       if ((!client->tls_ca_certfile && !client->tls_ca_certdir) && need_tls) {
907          Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
908             " or \"TLS CA Certificate Dir\" are defined for File daemon \"%s\" in %s.\n"),
909             client->name(), configfile);
910          OK = false;
911       }
912
913       /* If everything is well, attempt to initialize our per-resource TLS context */
914       if (OK && (need_tls || client->tls_require)) {
915          /* Initialize TLS context:
916           * Args: CA certfile, CA certdir, Certfile, Keyfile,
917           * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
918          client->tls_ctx = new_tls_context(client->tls_ca_certfile,
919             client->tls_ca_certdir, client->tls_certfile,
920             client->tls_keyfile, NULL, NULL, NULL,
921             true);
922
923          if (!client->tls_ctx) {
924             Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
925                client->name(), configfile);
926             OK = false;
927          }
928       }
929    }
930
931    UnlockRes();
932    if (OK) {
933       close_msg(NULL);                /* close temp message handler */
934       init_msg(NULL, director->messages); /* open daemon message handler */
935       last_reload_time = time(NULL);
936    }
937    return OK;
938 }
939
940 /*
941  * In this routine,
942  *  - we can check the connection (mode=CHECK_CONNECTION)
943  *  - we can synchronize the catalog with the configuration (mode=UPDATE_CATALOG)
944  *  - we can synchronize, and fix old job records (mode=UPDATE_AND_FIX)
945  */
946 static bool check_catalog(cat_op mode)
947 {
948    bool OK = true;
949    bool need_tls;
950
951    /* Loop over databases */
952    CAT *catalog;
953    foreach_res(catalog, R_CATALOG) {
954       BDB *db;
955       /*
956        * Make sure we can open catalog, otherwise print a warning
957        * message because the server is probably not running.
958        */
959       db = db_init_database(NULL, catalog->db_driver, catalog->db_name, 
960               catalog->db_user,
961               catalog->db_password, catalog->db_address,
962               catalog->db_port, catalog->db_socket,
963               catalog->mult_db_connections,
964               catalog->disable_batch_insert);
965       if (!db || !db_open_database(NULL, db)) {
966          Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
967               catalog->name(), catalog->db_name);
968          Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
969               catalog->name(), catalog->db_name);
970          if (db) {
971             Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
972             Pmsg1(000, "%s", db_strerror(db));
973             db_close_database(NULL, db);
974          }
975          OK = false;
976          continue;
977       }
978
979       /* Display a message if the db max_connections is too low */
980       if (!db_check_max_connections(NULL, db, director->MaxConcurrentJobs)) {
981          Pmsg1(000, "Warning, settings problem for Catalog=%s\n", catalog->name());
982          Pmsg1(000, "%s", db_strerror(db));
983       }
984
985       /* we are in testing mode, so don't touch anything in the catalog */
986       if (mode == CHECK_CONNECTION) {
987          if (db) db_close_database(NULL, db);
988          continue;
989       }
990
991       /* Loop over all pools, defining/updating them in each database */
992       POOL *pool;
993       foreach_res(pool, R_POOL) {
994          /*
995           * If the Pool has a catalog resource create the pool only
996           *   in that catalog.
997           */
998          if (!pool->catalog || pool->catalog == catalog) {
999             create_pool(NULL, db, pool, POOL_OP_UPDATE);  /* update request */
1000          }
1001       }
1002
1003       /* Once they are created, we can loop over them again, updating
1004        * references (RecyclePool)
1005        */
1006       foreach_res(pool, R_POOL) {
1007          /*
1008           * If the Pool has a catalog resource update the pool only
1009           *   in that catalog.
1010           */
1011          if (!pool->catalog || pool->catalog == catalog) {
1012             update_pool_references(NULL, db, pool);
1013          }
1014       }
1015
1016       /* Ensure basic client record is in DB */
1017       CLIENT *client;
1018       foreach_res(client, R_CLIENT) {
1019          CLIENT_DBR cr;
1020          /* Create clients only if they use the current catalog */
1021          if (client->catalog != catalog) {
1022             Dmsg3(500, "Skip client=%s with cat=%s not catalog=%s\n",
1023                   client->name(), client->catalog->name(), catalog->name());
1024             continue;
1025          }
1026          Dmsg2(500, "create cat=%s for client=%s\n",
1027                client->catalog->name(), client->name());
1028          memset(&cr, 0, sizeof(cr));
1029          bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
1030
1031          db_create_client_record(NULL, db, &cr);
1032       }
1033
1034       /* Ensure basic storage record is in DB */
1035       STORE *store;
1036       foreach_res(store, R_STORAGE) {
1037          STORAGE_DBR sr;
1038          MEDIATYPE_DBR mtr;
1039          memset(&sr, 0, sizeof(sr));
1040          memset(&mtr, 0, sizeof(mtr));
1041          if (store->media_type) {
1042             bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType));
1043             mtr.ReadOnly = 0;
1044             db_create_mediatype_record(NULL, db, &mtr);
1045          } else {
1046             mtr.MediaTypeId = 0;
1047          }
1048          bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
1049          sr.AutoChanger = store->autochanger;
1050          if (!db_create_storage_record(NULL, db, &sr)) {
1051             Jmsg(NULL, M_FATAL, 0, _("Could not create storage record for %s\n"),
1052                  store->name());
1053             OK = false;
1054          }
1055          store->StorageId = sr.StorageId;   /* set storage Id */
1056          if (!sr.created) {                 /* if not created, update it */
1057             sr.AutoChanger = store->autochanger;
1058             if (!db_update_storage_record(NULL, db, &sr)) {
1059                Jmsg(NULL, M_FATAL, 0, _("Could not update storage record for %s\n"),
1060                     store->name());
1061                OK = false;
1062             }
1063          }
1064
1065          /* tls_require implies tls_enable */
1066          if (store->tls_require) {
1067             if (have_tls) {
1068                store->tls_enable = true;
1069             } else {
1070                Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1071                OK = false;
1072             }
1073          }
1074
1075          need_tls = store->tls_enable || store->tls_authenticate;
1076
1077          if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
1078             Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
1079                  " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
1080                  store->name(), configfile);
1081             OK = false;
1082          }
1083
1084          /* If everything is well, attempt to initialize our per-resource TLS context */
1085          if (OK && (need_tls || store->tls_require)) {
1086            /* Initialize TLS context:
1087             * Args: CA certfile, CA certdir, Certfile, Keyfile,
1088             * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1089             store->tls_ctx = new_tls_context(store->tls_ca_certfile,
1090                store->tls_ca_certdir, store->tls_certfile,
1091                store->tls_keyfile, NULL, NULL, NULL, true);
1092
1093             if (!store->tls_ctx) {
1094                Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
1095                     store->name(), configfile);
1096                OK = false;
1097             }
1098          }
1099       }
1100
1101       /* Loop over all counters, defining them in each database */
1102       /* Set default value in all counters */
1103       COUNTER *counter;
1104       foreach_res(counter, R_COUNTER) {
1105          /* Write to catalog? */
1106          if (!counter->created && counter->Catalog == catalog) {
1107             COUNTER_DBR cr;
1108             bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
1109             cr.MinValue = counter->MinValue;
1110             cr.MaxValue = counter->MaxValue;
1111             cr.CurrentValue = counter->MinValue;
1112             if (counter->WrapCounter) {
1113                bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
1114             } else {
1115                cr.WrapCounter[0] = 0;  /* empty string */
1116             }
1117             if (db_create_counter_record(NULL, db, &cr)) {
1118                counter->CurrentValue = cr.CurrentValue;
1119                counter->created = true;
1120                Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
1121             }
1122          }
1123          if (!counter->created) {
1124             counter->CurrentValue = counter->MinValue;  /* default value */
1125          }
1126       }
1127       /* cleanup old job records */
1128       if (mode == UPDATE_AND_FIX) {
1129          db_sql_query(db, cleanup_created_job, NULL, NULL);
1130          db_sql_query(db, cleanup_running_job, NULL, NULL);
1131       }
1132
1133       /* Set SQL engine name in global for debugging */
1134       set_db_engine_name(db_get_engine_name(db));
1135       if (db) db_close_database(NULL, db);
1136    }
1137    return OK;
1138 }
1139
1140 static void cleanup_old_files()
1141 {
1142    DIR* dp;
1143    struct dirent *entry, *result;
1144    int rc, name_max;
1145    int my_name_len = strlen(my_name);
1146    int len = strlen(director->working_directory);
1147    POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
1148    POOLMEM *basename = get_pool_memory(PM_MESSAGE);
1149    regex_t preg1;
1150    char prbuf[500];
1151    const int nmatch = 30;
1152    regmatch_t pmatch[nmatch];
1153    berrno be;
1154
1155    /* Exclude spaces and look for .mail, .tmp or .restore.xx.bsr files */
1156    const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail|tmp)$";
1157
1158    /* Setup working directory prefix */
1159    pm_strcpy(basename, director->working_directory);
1160    if (len > 0 && !IsPathSeparator(director->working_directory[len-1])) {
1161       pm_strcat(basename, "/");
1162    }
1163
1164    /* Compile regex expressions */
1165    rc = regcomp(&preg1, pat1, REG_EXTENDED);
1166    if (rc != 0) {
1167       regerror(rc, &preg1, prbuf, sizeof(prbuf));
1168       Pmsg2(000,  _("Could not compile regex pattern \"%s\" ERR=%s\n"),
1169            pat1, prbuf);
1170       goto get_out2;
1171    }
1172
1173    name_max = pathconf(".", _PC_NAME_MAX);
1174    if (name_max < 1024) {
1175       name_max = 1024;
1176    }
1177
1178    if (!(dp = opendir(director->working_directory))) {
1179       berrno be;
1180       Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
1181             director->working_directory, be.bstrerror());
1182       goto get_out1;
1183       return;
1184    }
1185
1186    entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
1187    while (1) {
1188       if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
1189          break;
1190       }
1191       /* Exclude any name with ., .., not my_name or containing a space */
1192       if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
1193           strncmp(result->d_name, my_name, my_name_len) != 0) {
1194          Dmsg1(500, "Skipped: %s\n", result->d_name);
1195          continue;
1196       }
1197
1198       /* Unlink files that match regexes */
1199       if (regexec(&preg1, result->d_name, nmatch, pmatch,  0) == 0) {
1200          pm_strcpy(cleanup, basename);
1201          pm_strcat(cleanup, result->d_name);
1202          Dmsg1(100, "Unlink: %s\n", cleanup);
1203          unlink(cleanup);
1204       }
1205    }
1206
1207    free(entry);
1208    closedir(dp);
1209 /* Be careful to free up the correct resources */
1210 get_out1:
1211    regfree(&preg1);
1212 get_out2:
1213    free_pool_memory(cleanup);
1214    free_pool_memory(basename);
1215 }