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 cleanup_old_files();
70 /* Exported subroutines */
71 extern "C" void reload_config(int sig);
72 extern void invalidate_schedules();
73 extern bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code);
75 /* Imported subroutines */
76 JCR *wait_for_next_job(char *runjob);
77 void term_scheduler();
78 void term_ua_server();
79 void start_UA_server(dlist *addrs);
80 void init_job_server(int max_workers);
81 void term_job_server();
82 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
83 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
84 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
85 void store_migtype(LEX *lc, RES_ITEM *item, int index, int pass);
86 void init_device_resources();
88 static char *runjob = NULL;
89 static bool background = true;
90 static void init_reload(void);
91 static CONFIG *config;
93 /* Globals Exported */
94 DIRRES *director; /* Director resource */
97 char *configfile = NULL;
100 /* Globals Imported */
101 extern RES_ITEM job_items[];
102 #if defined(_MSC_VER)
103 extern "C" { // work around visual compiler mangling variables
111 CHECK_CONNECTION, /* Check catalog connection */
112 UPDATE_CATALOG, /* Ensure that catalog is ok with conf */
113 UPDATE_AND_FIX /* Ensure that catalog is ok, and fix old jobs */
115 static bool check_catalog(cat_op mode);
117 #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */
120 * This allows the message handler to operate on the database
121 * by using a pointer to this function. The pointer is
122 * needed because the other daemons do not have access
123 * to the database. If the pointer is
124 * not defined (other daemons), then writing the database
127 static bool dir_sql_query(JCR *jcr, const char *cmd)
129 if (!jcr || !jcr->db || !jcr->db->is_connected()) {
133 return db_sql_query(jcr->db, cmd);
136 static bool dir_sql_escape(JCR *jcr, B_DB *mdb, char *snew, char *old, int len)
138 if (!jcr || !jcr->db || !jcr->db->is_connected()) {
142 db_escape_string(jcr, mdb, snew, old, len);
150 "\nVersion: %s (%s)\n\n"
151 "Usage: bacula-dir [-f -s] [-c config_file] [-d debug_level] [config_file]\n"
152 " -c <file> set configuration file to file\n"
153 " -d <nn> set debug level to <nn>\n"
154 " -dt print timestamp in debug output\n"
155 " -f run in foreground (for debugging)\n"
157 " -m print kaboom output (for debugging)\n"
158 " -r <job> run <job> now\n"
160 " -t test - read configuration and exit\n"
162 " -v verbose user messages\n"
163 " -? print this message.\n"
164 "\n"), 2000, VERSION, BDATE);
170 /*********************************************************************
172 * Main Bacula Director Server program
175 #if defined(HAVE_WIN32)
176 /* For Win32 main() is in src/win32 code ... */
177 #define main BaculaMain
180 int main (int argc, char *argv[])
184 bool no_signals = false;
185 bool test_config = false;
189 init_python_interpreter_args python_args;
190 #endif /* HAVE_PYTHON */
192 start_heap = sbrk(0);
193 setlocale(LC_ALL, "");
194 bindtextdomain("bacula", LOCALEDIR);
195 textdomain("bacula");
198 my_name_is(argc, argv, "bacula-dir");
199 init_msg(NULL, NULL); /* initialize message handler */
201 daemon_start_time = time(NULL);
203 console_command = run_console_command;
205 while ((ch = getopt(argc, argv, "c:d:fg:mr:stu:v?")) != -1) {
207 case 'c': /* specify config file */
208 if (configfile != NULL) {
211 configfile = bstrdup(optarg);
214 case 'd': /* set debug level */
215 if (*optarg == 't') {
216 dbg_timestamp = true;
218 debug_level = atoi(optarg);
219 if (debug_level <= 0) {
223 Dmsg1(10, "Debug level = %d\n", debug_level);
226 case 'f': /* run in foreground */
230 case 'g': /* set group id */
234 case 'm': /* print kaboom output */
238 case 'r': /* run job */
239 if (runjob != NULL) {
243 runjob = bstrdup(optarg);
247 case 's': /* turn off signals */
251 case 't': /* test config */
255 case 'u': /* set uid */
259 case 'v': /* verbose */
273 init_signals(terminate_dird);
277 if (configfile != NULL) {
280 configfile = bstrdup(*argv);
288 if (configfile == NULL) {
289 configfile = bstrdup(CONFIG_FILE);
292 config = new_config_parser();
293 parse_dir_config(config, configfile, M_ERROR_TERM);
295 if (init_crypto() != 0) {
296 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
299 if (!check_resources()) {
300 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
303 if (!test_config) { /* we don't need to do this block in test mode */
306 init_stack_dump(); /* grab new pid */
308 /* Create pid must come after we are a daemon -- so we have our final pid */
309 create_pid_file(director->pid_directory, "bacula-dir",
310 get_first_port_host_order(director->DIRaddrs));
311 read_state_file(director->working_directory, "bacula-dir",
312 get_first_port_host_order(director->DIRaddrs));
315 set_jcr_in_tsd(INVALID_JCR);
316 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
317 4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */);
318 lmgr_init_thread(); /* initialize the lockmanager stack */
320 load_dir_plugins(director->plugin_directory);
322 drop(uid, gid, false); /* reduce privileges if requested */
324 /* If we are in testing mode, we don't try to fix the catalog */
325 cat_op mode=(test_config)?CHECK_CONNECTION:UPDATE_AND_FIX;
327 if (!check_catalog(mode)) {
328 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
335 my_name_is(0, NULL, director->name()); /* set user defined name */
339 /* Plug database interface for library routines */
340 p_sql_query = (sql_query_func)dir_sql_query;
341 p_sql_escape = (sql_escape_func)dir_sql_escape;
343 FDConnectTimeout = (int)director->FDConnectTimeout;
344 SDConnectTimeout = (int)director->SDConnectTimeout;
346 #if !defined(HAVE_WIN32)
347 signal(SIGHUP, reload_config);
350 init_console_msg(working_directory);
353 python_args.progname = director->name();
354 python_args.scriptdir = director->scripts_directory;
355 python_args.modulename = "DirStartUp";
356 python_args.configfile = configfile;
357 python_args.workingdir = director->working_directory;
358 python_args.job_getattr = job_getattr;
359 python_args.job_setattr = job_setattr;
361 init_python_interpreter(&python_args);
362 #endif /* HAVE_PYTHON */
364 Dmsg0(200, "Start UA server\n");
365 start_UA_server(director->DIRaddrs);
367 start_watchdog(); /* start network watchdog thread */
369 init_jcr_subsystem(); /* start JCR watchdogs etc. */
371 init_job_server(director->MaxConcurrentJobs);
373 dbg_jcr_add_hook(db_debug_print); /* used to debug B_DB connexion after fatal signal */
375 // init_device_resources();
377 Dmsg0(200, "wait for next job\n");
378 /* Main loop -- call scheduler to get next job to run */
379 while ( (jcr = wait_for_next_job(runjob)) ) {
380 run_job(jcr); /* run job */
381 free_jcr(jcr); /* release jcr */
382 set_jcr_in_tsd(INVALID_JCR);
383 if (runjob) { /* command line, run a single job? */
384 break; /* yes, terminate */
393 /* Cleanup and then exit */
394 void terminate_dird(int sig)
396 static bool already_here = false;
398 if (already_here) { /* avoid recursive temination problems */
399 bmicrosleep(2, 0); /* yield */
403 debug_level = 0; /* turn off debug */
405 generate_daemon_event(NULL, "Exit");
407 write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
408 delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
414 if (configfile != NULL) {
417 if (debug_level > 5) {
418 print_memory_pool_stats();
421 config->free_resources();
426 term_msg(); /* terminate message handler */
428 close_memory_pool(); /* release free memory in pool */
434 struct RELOAD_TABLE {
439 static const int max_reloads = 32;
440 static RELOAD_TABLE reload_table[max_reloads];
442 static void init_reload(void)
444 for (int i=0; i < max_reloads; i++) {
445 reload_table[i].job_count = 0;
446 reload_table[i].res_table = NULL;
450 static void free_saved_resources(int table)
452 int num = r_last - r_first + 1;
453 RES **res_tab = reload_table[table].res_table;
455 Dmsg1(100, "res_tab for table %d already released.\n", table);
458 Dmsg1(100, "Freeing resources for table %d\n", table);
459 for (int j=0; j<num; j++) {
460 free_resource(res_tab[j], r_first + j);
463 reload_table[table].job_count = 0;
464 reload_table[table].res_table = NULL;
468 * Called here at the end of every job that was
469 * hooked decrementing the active job_count. When
470 * it goes to zero, no one is using the associated
471 * resource table, so free it.
473 static void reload_job_end_cb(JCR *jcr, void *ctx)
475 int reload_id = (int)((intptr_t)ctx);
476 Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
477 reload_id, reload_table[reload_id].job_count);
480 if (--reload_table[reload_id].job_count <= 0) {
481 free_saved_resources(reload_id);
487 static int find_free_reload_table_entry()
490 for (int i=0; i < max_reloads; i++) {
491 if (reload_table[i].res_table == NULL) {
500 * If we get here, we have received a SIGHUP, which means to
501 * reread our configuration file.
503 * The algorithm used is as follows: we count how many jobs are
504 * running and mark the running jobs to make a callback on
505 * exiting. The old config is saved with the reload table
506 * id in a reload table. The new config file is read. Now, as
507 * each job exits, it calls back to the reload_job_end_cb(), which
508 * decrements the count of open jobs for the given reload table.
509 * When the count goes to zero, we release those resources.
510 * This allows us to have pointers into the resource table (from
511 * jobs), and once they exit and all the pointers are released, we
512 * release the old table. Note, if no new jobs are running since the
513 * last reload, then the old resources will be immediately release.
514 * A console is considered a job because it may have pointers to
515 * resources, but a SYSTEM job is not since it *should* not have any
516 * permanent pointers to jobs.
519 void reload_config(int sig)
521 static bool already_here = false;
522 #if !defined(HAVE_WIN32)
526 int njobs = 0; /* number of running jobs */
531 abort(); /* Oops, recursion -> die */
535 #if !defined(HAVE_WIN32)
537 sigaddset(&set, SIGHUP);
538 sigprocmask(SIG_BLOCK, &set, NULL);
544 table = find_free_reload_table_entry();
546 Jmsg(NULL, M_ERROR, 0, _("Too many open reload requests. Request ignored.\n"));
550 Dmsg1(100, "Reload_config njobs=%d\n", njobs);
551 reload_table[table].res_table = config->save_resources();
552 Dmsg1(100, "Saved old config in table %d\n", table);
554 ok = parse_dir_config(config, configfile, M_ERROR);
556 Dmsg0(100, "Reloaded config file\n");
557 if (!ok || !check_resources() || !check_catalog(UPDATE_CATALOG)) {
558 rtable = find_free_reload_table_entry(); /* save new, bad table */
560 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
561 Jmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
563 Jmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
564 Jmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
566 reload_table[rtable].res_table = config->save_resources();
567 /* Now restore old resoure values */
568 int num = r_last - r_first + 1;
569 RES **res_tab = reload_table[table].res_table;
570 for (int i=0; i<num; i++) {
571 res_head[i] = res_tab[i];
573 table = rtable; /* release new, bad, saved table below */
575 invalidate_schedules();
577 * Hook all active jobs so that they release this table
580 if (jcr->getJobType() != JT_SYSTEM) {
581 reload_table[table].job_count++;
582 job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
590 set_working_directory(director->working_directory);
591 FDConnectTimeout = director->FDConnectTimeout;
592 SDConnectTimeout = director->SDConnectTimeout;
593 Dmsg0(10, "Director's configuration file reread.\n");
595 /* Now release saved resources, if no jobs using the resources */
597 free_saved_resources(table);
603 #if !defined(HAVE_WIN32)
604 sigprocmask(SIG_UNBLOCK, &set, NULL);
605 signal(SIGHUP, reload_config);
607 already_here = false;
611 * Make a quick check to see that we have all the
614 * **** FIXME **** this routine could be a lot more
615 * intelligent and comprehensive.
617 static bool check_resources()
625 job = (JOB *)GetNextRes(R_JOB, NULL);
626 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
628 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
629 "Without that I don't know who I am :-(\n"), configfile);
632 set_working_directory(director->working_directory);
633 if (!director->messages) { /* If message resource not specified */
634 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
635 if (!director->messages) {
636 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
640 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
641 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
645 /* tls_require implies tls_enable */
646 if (director->tls_require) {
648 director->tls_enable = true;
650 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
655 need_tls = director->tls_enable || director->tls_authenticate;
657 if (!director->tls_certfile && need_tls) {
658 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
659 director->name(), configfile);
663 if (!director->tls_keyfile && need_tls) {
664 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
665 director->name(), configfile);
669 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) &&
670 need_tls && director->tls_verify_peer) {
671 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
672 " Certificate Dir\" are defined for Director \"%s\" in %s."
673 " At least one CA certificate store is required"
674 " when using \"TLS Verify Peer\".\n"),
675 director->name(), configfile);
679 /* If everything is well, attempt to initialize our per-resource TLS context */
680 if (OK && (need_tls || director->tls_require)) {
681 /* Initialize TLS context:
682 * Args: CA certfile, CA certdir, Certfile, Keyfile,
683 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
684 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
685 director->tls_ca_certdir, director->tls_certfile,
686 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
687 director->tls_verify_peer);
689 if (!director->tls_ctx) {
690 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
691 director->name(), configfile);
698 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
701 foreach_res(job, R_JOB) {
705 /* Handle Storage alists specifically */
706 JOB *jobdefs = job->jobdefs;
707 if (jobdefs->storage && !job->storage) {
709 job->storage = New(alist(10, not_owned_by_alist));
710 foreach_alist(st, jobdefs->storage) {
711 job->storage->append(st);
714 /* Handle RunScripts alists specifically */
715 if (jobdefs->RunScripts) {
718 if (!job->RunScripts) {
719 job->RunScripts = New(alist(10, not_owned_by_alist));
722 foreach_alist(rs, jobdefs->RunScripts) {
723 elt = copy_runscript(rs);
724 job->RunScripts->append(elt); /* we have to free it */
728 /* Transfer default items from JobDefs Resource */
729 for (i=0; job_items[i].name; i++) {
730 char **def_svalue, **svalue; /* string value */
731 uint32_t *def_ivalue, *ivalue; /* integer value */
732 bool *def_bvalue, *bvalue; /* bool value */
733 int64_t *def_lvalue, *lvalue; /* 64 bit values */
736 Dmsg4(1400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
737 job->name(), job_items[i].name,
738 bit_is_set(i, job->hdr.item_present),
739 bit_is_set(i, job->jobdefs->hdr.item_present));
741 if (!bit_is_set(i, job->hdr.item_present) &&
742 bit_is_set(i, job->jobdefs->hdr.item_present)) {
743 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
744 job->name(), job_items[i].name);
745 offset = (char *)(job_items[i].value) - (char *)&res_all;
747 * Handle strings and directory strings
749 if (job_items[i].handler == store_str ||
750 job_items[i].handler == store_dir) {
751 def_svalue = (char **)((char *)(job->jobdefs) + offset);
752 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
753 job->name(), job_items[i].name, *def_svalue, i, offset);
754 svalue = (char **)((char *)job + offset);
756 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
758 *svalue = bstrdup(*def_svalue);
759 set_bit(i, job->hdr.item_present);
763 } else if (job_items[i].handler == store_res) {
764 def_svalue = (char **)((char *)(job->jobdefs) + offset);
765 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
766 job->name(), job_items[i].name, i, offset);
767 svalue = (char **)((char *)job + offset);
769 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
771 *svalue = *def_svalue;
772 set_bit(i, job->hdr.item_present);
774 * Handle alist resources
776 } else if (job_items[i].handler == store_alist_res) {
777 if (bit_is_set(i, job->jobdefs->hdr.item_present)) {
778 set_bit(i, job->hdr.item_present);
781 * Handle integer fields
782 * Note, our store_bit does not handle bitmaped fields
784 } else if (job_items[i].handler == store_bit ||
785 job_items[i].handler == store_pint32 ||
786 job_items[i].handler == store_jobtype ||
787 job_items[i].handler == store_level ||
788 job_items[i].handler == store_int32 ||
789 job_items[i].handler == store_size32 ||
790 job_items[i].handler == store_migtype ||
791 job_items[i].handler == store_replace) {
792 def_ivalue = (uint32_t *)((char *)(job->jobdefs) + offset);
793 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
794 job->name(), job_items[i].name, *def_ivalue, i, offset);
795 ivalue = (uint32_t *)((char *)job + offset);
796 *ivalue = *def_ivalue;
797 set_bit(i, job->hdr.item_present);
799 * Handle 64 bit integer fields
801 } else if (job_items[i].handler == store_time ||
802 job_items[i].handler == store_size64 ||
803 job_items[i].handler == store_int64) {
804 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
805 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
806 job->name(), job_items[i].name, *def_lvalue, i, offset);
807 lvalue = (int64_t *)((char *)job + offset);
808 *lvalue = *def_lvalue;
809 set_bit(i, job->hdr.item_present);
813 } else if (job_items[i].handler == store_bool) {
814 def_bvalue = (bool *)((char *)(job->jobdefs) + offset);
815 Dmsg5(400, "Job \"%s\", field \"%s\" def_bvalue=%d item %d offset=%u\n",
816 job->name(), job_items[i].name, *def_bvalue, i, offset);
817 bvalue = (bool *)((char *)job + offset);
818 *bvalue = *def_bvalue;
819 set_bit(i, job->hdr.item_present);
825 * Ensure that all required items are present
827 for (i=0; job_items[i].name; i++) {
828 if (job_items[i].flags & ITEM_REQUIRED) {
829 if (!bit_is_set(i, job->hdr.item_present)) {
830 Jmsg(NULL, M_ERROR_TERM, 0, _("\"%s\" directive in Job \"%s\" resource is required, but not found.\n"),
831 job_items[i].name, job->name());
835 /* If this triggers, take a look at lib/parse_conf.h */
836 if (i >= MAX_RES_ITEMS) {
837 Emsg0(M_ERROR_TERM, 0, _("Too many items in Job resource\n"));
840 if (!job->storage && !job->pool->storage) {
841 Jmsg(NULL, M_FATAL, 0, _("No storage specified in Job \"%s\" nor in Pool.\n"),
845 } /* End loop over Job res */
848 /* Loop over Consoles */
850 foreach_res(cons, R_CONSOLE) {
851 /* tls_require implies tls_enable */
852 if (cons->tls_require) {
854 cons->tls_enable = true;
856 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
862 need_tls = cons->tls_enable || cons->tls_authenticate;
864 if (!cons->tls_certfile && need_tls) {
865 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Console \"%s\" in %s.\n"),
866 cons->name(), configfile);
870 if (!cons->tls_keyfile && need_tls) {
871 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Console \"%s\" in %s.\n"),
872 cons->name(), configfile);
876 if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir)
877 && need_tls && cons->tls_verify_peer) {
878 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
879 " Certificate Dir\" are defined for Console \"%s\" in %s."
880 " At least one CA certificate store is required"
881 " when using \"TLS Verify Peer\".\n"),
882 cons->name(), configfile);
885 /* If everything is well, attempt to initialize our per-resource TLS context */
886 if (OK && (need_tls || cons->tls_require)) {
887 /* Initialize TLS context:
888 * Args: CA certfile, CA certdir, Certfile, Keyfile,
889 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
890 cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
891 cons->tls_ca_certdir, cons->tls_certfile,
892 cons->tls_keyfile, NULL, NULL, cons->tls_dhfile, cons->tls_verify_peer);
894 if (!cons->tls_ctx) {
895 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
896 cons->name(), configfile);
903 /* Loop over Clients */
905 foreach_res(client, R_CLIENT) {
906 /* tls_require implies tls_enable */
907 if (client->tls_require) {
909 client->tls_enable = true;
911 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
916 need_tls = client->tls_enable || client->tls_authenticate;
917 if ((!client->tls_ca_certfile && !client->tls_ca_certdir) && need_tls) {
918 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
919 " or \"TLS CA Certificate Dir\" are defined for File daemon \"%s\" in %s.\n"),
920 client->name(), configfile);
924 /* If everything is well, attempt to initialize our per-resource TLS context */
925 if (OK && (need_tls || client->tls_require)) {
926 /* Initialize TLS context:
927 * Args: CA certfile, CA certdir, Certfile, Keyfile,
928 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
929 client->tls_ctx = new_tls_context(client->tls_ca_certfile,
930 client->tls_ca_certdir, client->tls_certfile,
931 client->tls_keyfile, NULL, NULL, NULL,
934 if (!client->tls_ctx) {
935 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
936 client->name(), configfile);
942 /* Loop over Storages */
944 foreach_res(store, R_STORAGE) {
945 /* tls_require implies tls_enable */
946 if (store->tls_require) {
948 store->tls_enable = true;
950 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
956 need_tls = store->tls_enable || store->tls_authenticate;
958 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
959 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
960 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
961 store->name(), configfile);
965 /* If everything is well, attempt to initialize our per-resource TLS context */
966 if (OK && (need_tls || store->tls_require)) {
967 /* Initialize TLS context:
968 * Args: CA certfile, CA certdir, Certfile, Keyfile,
969 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
970 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
971 store->tls_ca_certdir, store->tls_certfile,
972 store->tls_keyfile, NULL, NULL, NULL, true);
974 if (!store->tls_ctx) {
975 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
976 store->name(), configfile);
984 close_msg(NULL); /* close temp message handler */
985 init_msg(NULL, director->messages); /* open daemon message handler */
992 * - we can check the connection (mode=CHECK_CONNECTION)
993 * - we can synchronize the catalog with the configuration (mode=UPDATE_CATALOG)
994 * - we can synchronize, and fix old job records (mode=UPDATE_AND_FIX)
996 static bool check_catalog(cat_op mode)
1000 /* Loop over databases */
1002 foreach_res(catalog, R_CATALOG) {
1005 * Make sure we can open catalog, otherwise print a warning
1006 * message because the server is probably not running.
1008 db = db_init_database(NULL, catalog->db_driver, catalog->db_name, catalog->db_user,
1009 catalog->db_password, catalog->db_address,
1010 catalog->db_port, catalog->db_socket,
1011 catalog->mult_db_connections,
1012 catalog->disable_batch_insert);
1013 if (!db || !db_open_database(NULL, db)) {
1014 Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1015 catalog->name(), catalog->db_name);
1016 Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1017 catalog->name(), catalog->db_name);
1019 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
1020 Pmsg1(000, "%s", db_strerror(db));
1021 db_close_database(NULL, db);
1027 /* Display a message if the db max_connections is too low */
1028 if (!db_check_max_connections(NULL, db, director->MaxConcurrentJobs)) {
1029 Pmsg1(000, "Warning, settings problem for Catalog=%s\n", catalog->name());
1030 Pmsg1(000, "%s", db_strerror(db));
1033 /* we are in testing mode, so don't touch anything in the catalog */
1034 if (mode == CHECK_CONNECTION) {
1035 db_close_database(NULL, db);
1039 /* Loop over all pools, defining/updating them in each database */
1041 foreach_res(pool, R_POOL) {
1043 * If the Pool has a catalog resource create the pool only
1046 if (!pool->catalog || pool->catalog == catalog) {
1047 create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
1051 /* Once they are created, we can loop over them again, updating
1052 * references (RecyclePool)
1054 foreach_res(pool, R_POOL) {
1056 * If the Pool has a catalog resource update the pool only
1059 if (!pool->catalog || pool->catalog == catalog) {
1060 update_pool_references(NULL, db, pool);
1064 /* Ensure basic client record is in DB */
1066 foreach_res(client, R_CLIENT) {
1068 /* Create clients only if they use the current catalog */
1069 if (client->catalog != catalog) {
1070 Dmsg3(500, "Skip client=%s with cat=%s not catalog=%s\n",
1071 client->name(), client->catalog->name(), catalog->name());
1074 Dmsg2(500, "create cat=%s for client=%s\n",
1075 client->catalog->name(), client->name());
1076 memset(&cr, 0, sizeof(cr));
1077 bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
1078 db_create_client_record(NULL, db, &cr);
1081 /* Ensure basic storage record is in DB */
1083 foreach_res(store, R_STORAGE) {
1086 memset(&sr, 0, sizeof(sr));
1087 memset(&mtr, 0, sizeof(mtr));
1088 if (store->media_type) {
1089 bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType));
1091 db_create_mediatype_record(NULL, db, &mtr);
1093 mtr.MediaTypeId = 0;
1095 bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
1096 sr.AutoChanger = store->autochanger;
1097 if (!db_create_storage_record(NULL, db, &sr)) {
1098 Jmsg(NULL, M_FATAL, 0, _("Could not create storage record for %s\n"),
1102 store->StorageId = sr.StorageId; /* set storage Id */
1103 if (!sr.created) { /* if not created, update it */
1104 sr.AutoChanger = store->autochanger;
1105 if (!db_update_storage_record(NULL, db, &sr)) {
1106 Jmsg(NULL, M_FATAL, 0, _("Could not update storage record for %s\n"),
1113 /* Loop over all counters, defining them in each database */
1114 /* Set default value in all counters */
1116 foreach_res(counter, R_COUNTER) {
1117 /* Write to catalog? */
1118 if (!counter->created && counter->Catalog == catalog) {
1120 bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
1121 cr.MinValue = counter->MinValue;
1122 cr.MaxValue = counter->MaxValue;
1123 cr.CurrentValue = counter->MinValue;
1124 if (counter->WrapCounter) {
1125 bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
1127 cr.WrapCounter[0] = 0; /* empty string */
1129 if (db_create_counter_record(NULL, db, &cr)) {
1130 counter->CurrentValue = cr.CurrentValue;
1131 counter->created = true;
1132 Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
1135 if (!counter->created) {
1136 counter->CurrentValue = counter->MinValue; /* default value */
1139 /* cleanup old job records */
1140 if (mode == UPDATE_AND_FIX) {
1141 db_sql_query(db, cleanup_created_job, NULL, NULL);
1142 db_sql_query(db, cleanup_running_job, NULL, NULL);
1145 /* Set type in global for debugging */
1146 set_db_type(db_get_type(db));
1148 db_close_database(NULL, db);
1153 static void cleanup_old_files()
1156 struct dirent *entry, *result;
1158 int my_name_len = strlen(my_name);
1159 int len = strlen(director->working_directory);
1160 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
1161 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
1164 const int nmatch = 30;
1165 regmatch_t pmatch[nmatch];
1168 /* Exclude spaces and look for .mail or .restore.xx.bsr files */
1169 const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail)$";
1171 /* Setup working directory prefix */
1172 pm_strcpy(basename, director->working_directory);
1173 if (len > 0 && !IsPathSeparator(director->working_directory[len-1])) {
1174 pm_strcat(basename, "/");
1177 /* Compile regex expressions */
1178 rc = regcomp(&preg1, pat1, REG_EXTENDED);
1180 regerror(rc, &preg1, prbuf, sizeof(prbuf));
1181 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
1186 name_max = pathconf(".", _PC_NAME_MAX);
1187 if (name_max < 1024) {
1191 if (!(dp = opendir(director->working_directory))) {
1193 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
1194 director->working_directory, be.bstrerror());
1199 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
1201 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
1204 /* Exclude any name with ., .., not my_name or containing a space */
1205 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
1206 strncmp(result->d_name, my_name, my_name_len) != 0) {
1207 Dmsg1(500, "Skipped: %s\n", result->d_name);
1211 /* Unlink files that match regexes */
1212 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
1213 pm_strcpy(cleanup, basename);
1214 pm_strcat(cleanup, result->d_name);
1215 Dmsg1(100, "Unlink: %s\n", cleanup);
1222 /* Be careful to free up the correct resources */
1226 free_pool_memory(cleanup);
1227 free_pool_memory(basename);