2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director daemon -- this is the main program
32 * Kern Sibbald, March MM
39 #include "lib/bregex.h"
45 #define NAMELEN(dirent) (strlen((dirent)->d_name))
47 #ifndef HAVE_READDIR_R
48 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
54 #undef _POSIX_C_SOURCE
57 #include "lib/pythonlib.h"
59 /* Imported Functions */
60 extern PyObject *job_getattr(PyObject *self, char *attrname);
61 extern int job_setattr(PyObject *self, char *attrname, PyObject *value);
63 #endif /* HAVE_PYTHON */
65 /* Forward referenced subroutines */
66 void terminate_dird(int sig);
67 static bool check_resources();
68 static void dir_sql_query(JCR *jcr, const char *cmd);
69 static void cleanup_old_files();
71 /* Exported subroutines */
72 extern "C" void reload_config(int sig);
73 extern void invalidate_schedules();
74 extern bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code);
76 /* Imported subroutines */
77 JCR *wait_for_next_job(char *runjob);
78 void term_scheduler();
79 void term_ua_server();
80 void start_UA_server(dlist *addrs);
81 void init_job_server(int max_workers);
82 void term_job_server();
83 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
84 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
85 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
86 void store_migtype(LEX *lc, RES_ITEM *item, int index, int pass);
87 void init_device_resources();
89 static char *runjob = NULL;
90 static bool background = true;
91 static void init_reload(void);
92 static CONFIG *config;
94 /* Globals Exported */
95 DIRRES *director; /* Director resource */
98 char *configfile = NULL;
101 /* Globals Imported */
102 extern RES_ITEM job_items[];
103 #if defined(_MSC_VER)
104 extern "C" { // work around visual compiler mangling variables
112 CHECK_CONNECTION, /* Check catalog connection */
113 UPDATE_CATALOG, /* Ensure that catalog is ok with conf */
114 UPDATE_AND_FIX /* Ensure that catalog is ok, and fix old jobs */
116 static bool check_catalog(cat_op mode);
118 #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */
125 "\nVersion: %s (%s)\n\n"
126 "Usage: bacula-dir [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
127 " -c <file> set configuration file to file\n"
128 " -d <nn> set debug level to <nn>\n"
129 " -dt print timestamp in debug output\n"
130 " -f run in foreground (for debugging)\n"
132 " -m print kaboom output (for debugging)\n"
133 " -r <job> run <job> now\n"
135 " -t test - read configuration and exit\n"
137 " -v verbose user messages\n"
138 " -? print this message.\n"
139 "\n"), 2000, VERSION, BDATE);
145 /*********************************************************************
147 * Main Bacula Director Server program
150 #if defined(HAVE_WIN32)
151 /* For Win32 main() is in src/win32 code ... */
152 #define main BaculaMain
155 int main (int argc, char *argv[])
159 bool no_signals = false;
160 bool test_config = false;
164 init_python_interpreter_args python_args;
165 #endif /* HAVE_PYTHON */
167 start_heap = sbrk(0);
168 setlocale(LC_ALL, "");
169 bindtextdomain("bacula", LOCALEDIR);
170 textdomain("bacula");
173 my_name_is(argc, argv, "bacula-dir");
174 init_msg(NULL, NULL); /* initialize message handler */
176 daemon_start_time = time(NULL);
178 console_command = run_console_command;
180 while ((ch = getopt(argc, argv, "c:d:fg:mr:stu:v?")) != -1) {
182 case 'c': /* specify config file */
183 if (configfile != NULL) {
186 configfile = bstrdup(optarg);
189 case 'd': /* set debug level */
190 if (*optarg == 't') {
191 dbg_timestamp = true;
193 debug_level = atoi(optarg);
194 if (debug_level <= 0) {
198 Dmsg1(10, "Debug level = %d\n", debug_level);
201 case 'f': /* run in foreground */
205 case 'g': /* set group id */
209 case 'm': /* print kaboom output */
213 case 'r': /* run job */
214 if (runjob != NULL) {
218 runjob = bstrdup(optarg);
222 case 's': /* turn off signals */
226 case 't': /* test config */
230 case 'u': /* set uid */
234 case 'v': /* verbose */
248 init_signals(terminate_dird);
252 if (configfile != NULL) {
255 configfile = bstrdup(*argv);
263 if (configfile == NULL) {
264 configfile = bstrdup(CONFIG_FILE);
267 config = new_config_parser();
268 parse_dir_config(config, configfile, M_ERROR_TERM);
270 if (init_crypto() != 0) {
271 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
274 if (!check_resources()) {
275 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
278 if (!test_config) { /* we don't need to do this block in test mode */
281 init_stack_dump(); /* grab new pid */
283 /* Create pid must come after we are a daemon -- so we have our final pid */
284 create_pid_file(director->pid_directory, "bacula-dir",
285 get_first_port_host_order(director->DIRaddrs));
286 read_state_file(director->working_directory, "bacula-dir",
287 get_first_port_host_order(director->DIRaddrs));
290 set_jcr_in_tsd(INVALID_JCR);
291 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
292 4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */);
293 lmgr_init_thread(); /* initialize the lockmanager stack */
295 load_dir_plugins(director->plugin_directory);
297 drop(uid, gid, false); /* reduce privileges if requested */
299 /* If we are in testing mode, we don't try to fix the catalog */
300 cat_op mode=(test_config)?CHECK_CONNECTION:UPDATE_AND_FIX;
302 if (!check_catalog(mode)) {
303 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
310 my_name_is(0, NULL, director->name()); /* set user defined name */
314 /* Plug database interface for library routines */
315 p_sql_query = (sql_query_func)dir_sql_query;
316 p_sql_escape = (sql_escape_func)db_escape_string;
318 FDConnectTimeout = (int)director->FDConnectTimeout;
319 SDConnectTimeout = (int)director->SDConnectTimeout;
321 #if !defined(HAVE_WIN32)
322 signal(SIGHUP, reload_config);
325 init_console_msg(working_directory);
328 python_args.progname = director->name();
329 python_args.scriptdir = director->scripts_directory;
330 python_args.modulename = "DirStartUp";
331 python_args.configfile = configfile;
332 python_args.workingdir = director->working_directory;
333 python_args.job_getattr = job_getattr;
334 python_args.job_setattr = job_setattr;
336 init_python_interpreter(&python_args);
337 #endif /* HAVE_PYTHON */
339 Dmsg0(200, "Start UA server\n");
340 start_UA_server(director->DIRaddrs);
342 start_watchdog(); /* start network watchdog thread */
344 init_jcr_subsystem(); /* start JCR watchdogs etc. */
346 init_job_server(director->MaxConcurrentJobs);
348 dbg_jcr_add_hook(db_debug_print); /* used to debug B_DB connexion after fatal signal */
350 // init_device_resources();
352 Dmsg0(200, "wait for next job\n");
353 /* Main loop -- call scheduler to get next job to run */
354 while ( (jcr = wait_for_next_job(runjob)) ) {
355 run_job(jcr); /* run job */
356 free_jcr(jcr); /* release jcr */
357 set_jcr_in_tsd(INVALID_JCR);
358 if (runjob) { /* command line, run a single job? */
359 break; /* yes, terminate */
369 * This allows the message handler to operate on the database
370 * by using a pointer to this function. The pointer is
371 * needed because the other daemons do not have access
372 * to the database. If the pointer is
373 * not defined (other daemons), then writing the database
376 static void dir_sql_query(JCR *jcr, const char *cmd)
378 if (!jcr || !jcr->db) {
381 db_sql_query(jcr->db, cmd, NULL, NULL);
384 /* Cleanup and then exit */
385 void terminate_dird(int sig)
387 static bool already_here = false;
389 if (already_here) { /* avoid recursive temination problems */
390 bmicrosleep(2, 0); /* yield */
394 debug_level = 0; /* turn off debug */
396 generate_daemon_event(NULL, "Exit");
398 write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
399 delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
405 if (configfile != NULL) {
408 if (debug_level > 5) {
409 print_memory_pool_stats();
412 config->free_resources();
417 term_msg(); /* terminate message handler */
419 close_memory_pool(); /* release free memory in pool */
425 struct RELOAD_TABLE {
430 static const int max_reloads = 32;
431 static RELOAD_TABLE reload_table[max_reloads];
433 static void init_reload(void)
435 for (int i=0; i < max_reloads; i++) {
436 reload_table[i].job_count = 0;
437 reload_table[i].res_table = NULL;
441 static void free_saved_resources(int table)
443 int num = r_last - r_first + 1;
444 RES **res_tab = reload_table[table].res_table;
446 Dmsg1(100, "res_tab for table %d already released.\n", table);
449 Dmsg1(100, "Freeing resources for table %d\n", table);
450 for (int j=0; j<num; j++) {
451 free_resource(res_tab[j], r_first + j);
454 reload_table[table].job_count = 0;
455 reload_table[table].res_table = NULL;
459 * Called here at the end of every job that was
460 * hooked decrementing the active job_count. When
461 * it goes to zero, no one is using the associated
462 * resource table, so free it.
464 static void reload_job_end_cb(JCR *jcr, void *ctx)
466 int reload_id = (int)((intptr_t)ctx);
467 Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
468 reload_id, reload_table[reload_id].job_count);
471 if (--reload_table[reload_id].job_count <= 0) {
472 free_saved_resources(reload_id);
478 static int find_free_reload_table_entry()
481 for (int i=0; i < max_reloads; i++) {
482 if (reload_table[i].res_table == NULL) {
491 * If we get here, we have received a SIGHUP, which means to
492 * reread our configuration file.
494 * The algorithm used is as follows: we count how many jobs are
495 * running and mark the running jobs to make a callback on
496 * exiting. The old config is saved with the reload table
497 * id in a reload table. The new config file is read. Now, as
498 * each job exits, it calls back to the reload_job_end_cb(), which
499 * decrements the count of open jobs for the given reload table.
500 * When the count goes to zero, we release those resources.
501 * This allows us to have pointers into the resource table (from
502 * jobs), and once they exit and all the pointers are released, we
503 * release the old table. Note, if no new jobs are running since the
504 * last reload, then the old resources will be immediately release.
505 * A console is considered a job because it may have pointers to
506 * resources, but a SYSTEM job is not since it *should* not have any
507 * permanent pointers to jobs.
510 void reload_config(int sig)
512 static bool already_here = false;
513 #if !defined(HAVE_WIN32)
517 int njobs = 0; /* number of running jobs */
522 abort(); /* Oops, recursion -> die */
526 #if !defined(HAVE_WIN32)
528 sigaddset(&set, SIGHUP);
529 sigprocmask(SIG_BLOCK, &set, NULL);
535 table = find_free_reload_table_entry();
537 Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. Request ignored.\n"));
541 Dmsg1(100, "Reload_config njobs=%d\n", njobs);
542 reload_table[table].res_table = config->save_resources();
543 Dmsg1(100, "Saved old config in table %d\n", table);
545 ok = parse_dir_config(config, configfile, M_ERROR);
547 Dmsg0(100, "Reloaded config file\n");
548 if (!ok || !check_resources() || !check_catalog(UPDATE_CATALOG)) {
549 rtable = find_free_reload_table_entry(); /* save new, bad table */
551 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
552 Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
554 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
555 Jmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
557 reload_table[rtable].res_table = config->save_resources();
558 /* Now restore old resoure values */
559 int num = r_last - r_first + 1;
560 RES **res_tab = reload_table[table].res_table;
561 for (int i=0; i<num; i++) {
562 res_head[i] = res_tab[i];
564 table = rtable; /* release new, bad, saved table below */
566 invalidate_schedules();
568 * Hook all active jobs so that they release this table
571 if (jcr->getJobType() != JT_SYSTEM) {
572 reload_table[table].job_count++;
573 job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
581 set_working_directory(director->working_directory);
582 FDConnectTimeout = director->FDConnectTimeout;
583 SDConnectTimeout = director->SDConnectTimeout;
584 Dmsg0(10, "Director's configuration file reread.\n");
586 /* Now release saved resources, if no jobs using the resources */
588 free_saved_resources(table);
594 #if !defined(HAVE_WIN32)
595 sigprocmask(SIG_UNBLOCK, &set, NULL);
596 signal(SIGHUP, reload_config);
598 already_here = false;
602 * Make a quick check to see that we have all the
605 * **** FIXME **** this routine could be a lot more
606 * intelligent and comprehensive.
608 static bool check_resources()
616 job = (JOB *)GetNextRes(R_JOB, NULL);
617 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
619 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
620 "Without that I don't know who I am :-(\n"), configfile);
623 set_working_directory(director->working_directory);
624 if (!director->messages) { /* If message resource not specified */
625 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
626 if (!director->messages) {
627 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
631 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
632 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
636 /* tls_require implies tls_enable */
637 if (director->tls_require) {
639 director->tls_enable = true;
641 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
646 need_tls = director->tls_enable || director->tls_authenticate;
648 if (!director->tls_certfile && need_tls) {
649 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
650 director->name(), configfile);
654 if (!director->tls_keyfile && need_tls) {
655 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
656 director->name(), configfile);
660 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) &&
661 need_tls && director->tls_verify_peer) {
662 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
663 " Certificate Dir\" are defined for Director \"%s\" in %s."
664 " At least one CA certificate store is required"
665 " when using \"TLS Verify Peer\".\n"),
666 director->name(), configfile);
670 /* If everything is well, attempt to initialize our per-resource TLS context */
671 if (OK && (need_tls || director->tls_require)) {
672 /* Initialize TLS context:
673 * Args: CA certfile, CA certdir, Certfile, Keyfile,
674 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
675 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
676 director->tls_ca_certdir, director->tls_certfile,
677 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
678 director->tls_verify_peer);
680 if (!director->tls_ctx) {
681 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
682 director->name(), configfile);
689 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
692 foreach_res(job, R_JOB) {
696 /* Handle Storage alists specifically */
697 JOB *jobdefs = job->jobdefs;
698 if (jobdefs->storage && !job->storage) {
700 job->storage = New(alist(10, not_owned_by_alist));
701 foreach_alist(st, jobdefs->storage) {
702 job->storage->append(st);
705 /* Handle RunScripts alists specifically */
706 if (jobdefs->RunScripts) {
709 if (!job->RunScripts) {
710 job->RunScripts = New(alist(10, not_owned_by_alist));
713 foreach_alist(rs, jobdefs->RunScripts) {
714 elt = copy_runscript(rs);
715 job->RunScripts->append(elt); /* we have to free it */
719 /* Transfer default items from JobDefs Resource */
720 for (i=0; job_items[i].name; i++) {
721 char **def_svalue, **svalue; /* string value */
722 uint32_t *def_ivalue, *ivalue; /* integer value */
723 bool *def_bvalue, *bvalue; /* bool value */
724 int64_t *def_lvalue, *lvalue; /* 64 bit values */
727 Dmsg4(1400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
728 job->name(), job_items[i].name,
729 bit_is_set(i, job->hdr.item_present),
730 bit_is_set(i, job->jobdefs->hdr.item_present));
732 if (!bit_is_set(i, job->hdr.item_present) &&
733 bit_is_set(i, job->jobdefs->hdr.item_present)) {
734 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
735 job->name(), job_items[i].name);
736 offset = (char *)(job_items[i].value) - (char *)&res_all;
738 * Handle strings and directory strings
740 if (job_items[i].handler == store_str ||
741 job_items[i].handler == store_dir) {
742 def_svalue = (char **)((char *)(job->jobdefs) + offset);
743 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
744 job->name(), job_items[i].name, *def_svalue, i, offset);
745 svalue = (char **)((char *)job + offset);
747 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
749 *svalue = bstrdup(*def_svalue);
750 set_bit(i, job->hdr.item_present);
754 } else if (job_items[i].handler == store_res) {
755 def_svalue = (char **)((char *)(job->jobdefs) + offset);
756 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
757 job->name(), job_items[i].name, i, offset);
758 svalue = (char **)((char *)job + offset);
760 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
762 *svalue = *def_svalue;
763 set_bit(i, job->hdr.item_present);
765 * Handle alist resources
767 } else if (job_items[i].handler == store_alist_res) {
768 if (bit_is_set(i, job->jobdefs->hdr.item_present)) {
769 set_bit(i, job->hdr.item_present);
772 * Handle integer fields
773 * Note, our store_bit does not handle bitmaped fields
775 } else if (job_items[i].handler == store_bit ||
776 job_items[i].handler == store_pint32 ||
777 job_items[i].handler == store_jobtype ||
778 job_items[i].handler == store_level ||
779 job_items[i].handler == store_int32 ||
780 job_items[i].handler == store_size32 ||
781 job_items[i].handler == store_migtype ||
782 job_items[i].handler == store_replace) {
783 def_ivalue = (uint32_t *)((char *)(job->jobdefs) + offset);
784 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
785 job->name(), job_items[i].name, *def_ivalue, i, offset);
786 ivalue = (uint32_t *)((char *)job + offset);
787 *ivalue = *def_ivalue;
788 set_bit(i, job->hdr.item_present);
790 * Handle 64 bit integer fields
792 } else if (job_items[i].handler == store_time ||
793 job_items[i].handler == store_size64 ||
794 job_items[i].handler == store_int64) {
795 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
796 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
797 job->name(), job_items[i].name, *def_lvalue, i, offset);
798 lvalue = (int64_t *)((char *)job + offset);
799 *lvalue = *def_lvalue;
800 set_bit(i, job->hdr.item_present);
804 } else if (job_items[i].handler == store_bool) {
805 def_bvalue = (bool *)((char *)(job->jobdefs) + offset);
806 Dmsg5(400, "Job \"%s\", field \"%s\" def_bvalue=%d item %d offset=%u\n",
807 job->name(), job_items[i].name, *def_bvalue, i, offset);
808 bvalue = (bool *)((char *)job + offset);
809 *bvalue = *def_bvalue;
810 set_bit(i, job->hdr.item_present);
816 * Ensure that all required items are present
818 for (i=0; job_items[i].name; i++) {
819 if (job_items[i].flags & ITEM_REQUIRED) {
820 if (!bit_is_set(i, job->hdr.item_present)) {
821 Jmsg(NULL, M_ERROR_TERM, 0, _("\"%s\" directive in Job \"%s\" resource is required, but not found.\n"),
822 job_items[i].name, job->name());
826 /* If this triggers, take a look at lib/parse_conf.h */
827 if (i >= MAX_RES_ITEMS) {
828 Emsg0(M_ERROR_TERM, 0, _("Too many items in Job resource\n"));
831 if (!job->storage && !job->pool->storage) {
832 Jmsg(NULL, M_FATAL, 0, _("No storage specified in Job \"%s\" nor in Pool.\n"),
836 } /* End loop over Job res */
839 /* Loop over Consoles */
841 foreach_res(cons, R_CONSOLE) {
842 /* tls_require implies tls_enable */
843 if (cons->tls_require) {
845 cons->tls_enable = true;
847 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
853 need_tls = cons->tls_enable || cons->tls_authenticate;
855 if (!cons->tls_certfile && need_tls) {
856 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Console \"%s\" in %s.\n"),
857 cons->name(), configfile);
861 if (!cons->tls_keyfile && need_tls) {
862 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Console \"%s\" in %s.\n"),
863 cons->name(), configfile);
867 if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir)
868 && need_tls && cons->tls_verify_peer) {
869 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
870 " Certificate Dir\" are defined for Console \"%s\" in %s."
871 " At least one CA certificate store is required"
872 " when using \"TLS Verify Peer\".\n"),
873 cons->name(), configfile);
876 /* If everything is well, attempt to initialize our per-resource TLS context */
877 if (OK && (need_tls || cons->tls_require)) {
878 /* Initialize TLS context:
879 * Args: CA certfile, CA certdir, Certfile, Keyfile,
880 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
881 cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
882 cons->tls_ca_certdir, cons->tls_certfile,
883 cons->tls_keyfile, NULL, NULL, cons->tls_dhfile, cons->tls_verify_peer);
885 if (!cons->tls_ctx) {
886 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
887 cons->name(), configfile);
894 /* Loop over Clients */
896 foreach_res(client, R_CLIENT) {
897 /* tls_require implies tls_enable */
898 if (client->tls_require) {
900 client->tls_enable = true;
902 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
907 need_tls = client->tls_enable || client->tls_authenticate;
908 if ((!client->tls_ca_certfile && !client->tls_ca_certdir) && need_tls) {
909 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
910 " or \"TLS CA Certificate Dir\" are defined for File daemon \"%s\" in %s.\n"),
911 client->name(), configfile);
915 /* If everything is well, attempt to initialize our per-resource TLS context */
916 if (OK && (need_tls || client->tls_require)) {
917 /* Initialize TLS context:
918 * Args: CA certfile, CA certdir, Certfile, Keyfile,
919 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
920 client->tls_ctx = new_tls_context(client->tls_ca_certfile,
921 client->tls_ca_certdir, client->tls_certfile,
922 client->tls_keyfile, NULL, NULL, NULL,
925 if (!client->tls_ctx) {
926 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
927 client->name(), configfile);
933 /* Loop over Storages */
935 foreach_res(store, R_STORAGE) {
936 /* tls_require implies tls_enable */
937 if (store->tls_require) {
939 store->tls_enable = true;
941 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
947 need_tls = store->tls_enable || store->tls_authenticate;
949 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
950 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
951 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
952 store->name(), configfile);
956 /* If everything is well, attempt to initialize our per-resource TLS context */
957 if (OK && (need_tls || store->tls_require)) {
958 /* Initialize TLS context:
959 * Args: CA certfile, CA certdir, Certfile, Keyfile,
960 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
961 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
962 store->tls_ca_certdir, store->tls_certfile,
963 store->tls_keyfile, NULL, NULL, NULL, true);
965 if (!store->tls_ctx) {
966 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
967 store->name(), configfile);
975 close_msg(NULL); /* close temp message handler */
976 init_msg(NULL, director->messages); /* open daemon message handler */
983 * - we can check the connection (mode=CHECK_CONNECTION)
984 * - we can synchronize the catalog with the configuration (mode=UPDATE_CATALOG)
985 * - we can synchronize, and fix old job records (mode=UPDATE_AND_FIX)
987 static bool check_catalog(cat_op mode)
991 /* Loop over databases */
993 foreach_res(catalog, R_CATALOG) {
996 * Make sure we can open catalog, otherwise print a warning
997 * message because the server is probably not running.
999 db = db_init_database(NULL, catalog->db_driver, catalog->db_name, catalog->db_user,
1000 catalog->db_password, catalog->db_address,
1001 catalog->db_port, catalog->db_socket,
1002 catalog->mult_db_connections,
1003 catalog->disable_batch_insert);
1004 if (!db || !db_open_database(NULL, db)) {
1005 Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1006 catalog->name(), catalog->db_name);
1007 Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1008 catalog->name(), catalog->db_name);
1010 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
1011 Pmsg1(000, "%s", db_strerror(db));
1012 db_close_database(NULL, db);
1018 /* Display a message if the db max_connections is too low */
1019 if (!db_check_max_connections(NULL, db, director->MaxConcurrentJobs)) {
1020 Pmsg1(000, "Warning, settings problem for Catalog=%s\n", catalog->name());
1021 Pmsg1(000, "%s", db_strerror(db));
1024 /* we are in testing mode, so don't touch anything in the catalog */
1025 if (mode == CHECK_CONNECTION) {
1026 db_close_database(NULL, db);
1030 /* Loop over all pools, defining/updating them in each database */
1032 foreach_res(pool, R_POOL) {
1034 * If the Pool has a catalog resource create the pool only
1037 if (!pool->catalog || pool->catalog == catalog) {
1038 create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
1042 /* Once they are created, we can loop over them again, updating
1043 * references (RecyclePool)
1045 foreach_res(pool, R_POOL) {
1047 * If the Pool has a catalog resource update the pool only
1050 if (!pool->catalog || pool->catalog == catalog) {
1051 update_pool_references(NULL, db, pool);
1055 /* Ensure basic client record is in DB */
1057 foreach_res(client, R_CLIENT) {
1059 /* Create clients only if they use the current catalog */
1060 if (client->catalog != catalog) {
1061 Dmsg3(500, "Skip client=%s with cat=%s not catalog=%s\n",
1062 client->name(), client->catalog->name(), catalog->name());
1065 Dmsg2(500, "create cat=%s for client=%s\n",
1066 client->catalog->name(), client->name());
1067 memset(&cr, 0, sizeof(cr));
1068 bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
1069 db_create_client_record(NULL, db, &cr);
1072 /* Ensure basic storage record is in DB */
1074 foreach_res(store, R_STORAGE) {
1077 memset(&sr, 0, sizeof(sr));
1078 memset(&mtr, 0, sizeof(mtr));
1079 if (store->media_type) {
1080 bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType));
1082 db_create_mediatype_record(NULL, db, &mtr);
1084 mtr.MediaTypeId = 0;
1086 bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
1087 sr.AutoChanger = store->autochanger;
1088 if (!db_create_storage_record(NULL, db, &sr)) {
1089 Jmsg(NULL, M_FATAL, 0, _("Could not create storage record for %s\n"),
1093 store->StorageId = sr.StorageId; /* set storage Id */
1094 if (!sr.created) { /* if not created, update it */
1095 sr.AutoChanger = store->autochanger;
1096 if (!db_update_storage_record(NULL, db, &sr)) {
1097 Jmsg(NULL, M_FATAL, 0, _("Could not update storage record for %s\n"),
1104 /* Loop over all counters, defining them in each database */
1105 /* Set default value in all counters */
1107 foreach_res(counter, R_COUNTER) {
1108 /* Write to catalog? */
1109 if (!counter->created && counter->Catalog == catalog) {
1111 bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
1112 cr.MinValue = counter->MinValue;
1113 cr.MaxValue = counter->MaxValue;
1114 cr.CurrentValue = counter->MinValue;
1115 if (counter->WrapCounter) {
1116 bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
1118 cr.WrapCounter[0] = 0; /* empty string */
1120 if (db_create_counter_record(NULL, db, &cr)) {
1121 counter->CurrentValue = cr.CurrentValue;
1122 counter->created = true;
1123 Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
1126 if (!counter->created) {
1127 counter->CurrentValue = counter->MinValue; /* default value */
1130 /* cleanup old job records */
1131 if (mode == UPDATE_AND_FIX) {
1132 db_sql_query(db, cleanup_created_job, NULL, NULL);
1133 db_sql_query(db, cleanup_running_job, NULL, NULL);
1136 /* Set type in global for debugging */
1137 set_db_type(db_get_type(db));
1139 db_close_database(NULL, db);
1144 static void cleanup_old_files()
1147 struct dirent *entry, *result;
1149 int my_name_len = strlen(my_name);
1150 int len = strlen(director->working_directory);
1151 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
1152 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
1155 const int nmatch = 30;
1156 regmatch_t pmatch[nmatch];
1159 /* Exclude spaces and look for .mail or .restore.xx.bsr files */
1160 const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail)$";
1162 /* Setup working directory prefix */
1163 pm_strcpy(basename, director->working_directory);
1164 if (len > 0 && !IsPathSeparator(director->working_directory[len-1])) {
1165 pm_strcat(basename, "/");
1168 /* Compile regex expressions */
1169 rc = regcomp(&preg1, pat1, REG_EXTENDED);
1171 regerror(rc, &preg1, prbuf, sizeof(prbuf));
1172 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
1177 name_max = pathconf(".", _PC_NAME_MAX);
1178 if (name_max < 1024) {
1182 if (!(dp = opendir(director->working_directory))) {
1184 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
1185 director->working_directory, be.bstrerror());
1190 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
1192 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
1195 /* Exclude any name with ., .., not my_name or containing a space */
1196 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
1197 strncmp(result->d_name, my_name, my_name_len) != 0) {
1198 Dmsg1(500, "Skipped: %s\n", result->d_name);
1202 /* Unlink files that match regexes */
1203 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
1204 pm_strcpy(cleanup, basename);
1205 pm_strcat(cleanup, result->d_name);
1206 Dmsg1(100, "Unlink: %s\n", cleanup);
1213 /* Be careful to free up the correct resources */
1217 free_pool_memory(cleanup);
1218 free_pool_memory(basename);