2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Bacula Director daemon -- this is the main program
22 * Kern Sibbald, March MM
28 #include "lib/bregex.h"
34 #define NAMELEN(dirent) (strlen((dirent)->d_name))
36 #ifndef HAVE_READDIR_R
37 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
40 /* Forward referenced subroutines */
41 void terminate_dird(int sig);
42 static bool check_resources();
43 static void cleanup_old_files();
44 static void resize_reload(int nb);
46 /* Exported subroutines */
47 extern "C" void reload_config(int sig);
48 extern void invalidate_schedules();
49 extern bool parse_dir_config(CONFIG *config, const char *configfile, int exit_code);
51 /* Imported subroutines */
52 JCR *wait_for_next_job(char *runjob);
53 void term_scheduler();
54 void term_ua_server();
55 void start_UA_server(dlist *addrs);
56 void init_job_server(int max_workers);
57 void term_job_server();
58 void store_jobtype(LEX *lc, RES_ITEM *item, int index, int pass);
59 void store_level(LEX *lc, RES_ITEM *item, int index, int pass);
60 void store_replace(LEX *lc, RES_ITEM *item, int index, int pass);
61 void store_migtype(LEX *lc, RES_ITEM *item, int index, int pass);
62 void init_device_resources();
65 static char *runjob = NULL;
66 static bool foreground = false;
67 static bool make_pid_file = true; /* create pid file */
68 static void init_reload(void);
69 static CONFIG *config;
70 static bool test_config = false;
72 /* Globals Exported */
73 DIRRES *director; /* Director resource */
76 char *configfile = NULL;
78 utime_t last_reload_time = 0;
81 /* Globals Imported */
82 extern dlist client_globals;
83 extern dlist store_globals;
84 extern dlist job_globals;
85 extern dlist sched_globals;
86 extern RES_ITEM job_items[];
88 extern "C" { // work around visual compiler mangling variables
96 CHECK_CONNECTION, /* Check catalog connection */
97 UPDATE_CATALOG, /* Ensure that catalog is ok with conf */
98 UPDATE_AND_FIX /* Ensure that catalog is ok, and fix old jobs */
100 static bool check_catalog(cat_op mode);
102 #define CONFIG_FILE "bacula-dir.conf" /* default configuration file */
104 static bool dir_sql_query(JCR *jcr, const char *cmd)
106 if (jcr && jcr->db && jcr->db->is_connected()) {
107 return db_sql_query(jcr->db, cmd, NULL, NULL);
112 static bool dir_sql_escape(JCR *jcr, BDB *mdb, char *snew, char *sold, int len)
114 if (jcr && jcr->db && jcr->db->is_connected()) {
115 db_escape_string(jcr, mdb, snew, sold, len);
125 "\n%sVersion: %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>[,<tags>] set debug level to <nn>, debug tags to <tags>\n"
129 " -dt print timestamp in debug output\n"
131 " -f run in foreground (for debugging)\n"
133 " -m print kaboom output (for debugging)\n"
134 " -r <job> run <job> now\n"
135 " -P do not create pid file\n"
137 " -t test - read configuration and exit\n"
139 " -v verbose user messages\n"
140 " -? print this message.\n"
141 "\n"), 2000, "", VERSION, BDATE);
147 * !!! WARNING !!! Use this function only when bacula is stopped.
148 * ie, after a fatal signal and before exiting the program
149 * Print information about a JCR
151 static void dir_debug_print(JCR *jcr, FILE *fp)
153 fprintf(fp, "\twstore=%p rstore=%p wjcr=%p client=%p reschedule_count=%d SD_msg_chan_started=%d\n",
154 jcr->wstore, jcr->rstore, jcr->wjcr, jcr->client, jcr->reschedule_count, (int)jcr->SD_msg_chan_started);
157 /*********************************************************************
159 * Main Bacula Director Server program
162 #if defined(HAVE_WIN32)
163 /* For Win32 main() is in src/win32 code ... */
164 #define main BaculaMain
167 /* DELETE ME when bugs in MA1512, MA1632 MA1639 are fixed */
168 extern void (*MA1512_reload_job_end_cb)(JCR *,void *);
169 static void reload_job_end_cb(JCR *jcr, void *ctx);
171 int main (int argc, char *argv[])
175 bool no_signals = false;
179 /* DELETE ME when bugs in MA1512, MA1632 MA1639 are fixed */
180 MA1512_reload_job_end_cb = reload_job_end_cb;
182 start_heap = sbrk(0);
183 setlocale(LC_ALL, "");
184 bindtextdomain("bacula", LOCALEDIR);
185 textdomain("bacula");
188 my_name_is(argc, argv, "bacula-dir");
189 init_msg(NULL, NULL); /* initialize message handler */
191 daemon_start_time = time(NULL);
192 setup_daemon_message_queue();
193 console_command = run_console_command;
195 while ((ch = getopt(argc, argv, "c:d:fg:mPr:stu:v?T")) != -1) {
197 case 'c': /* specify config file */
198 if (configfile != NULL) {
201 configfile = bstrdup(optarg);
204 case 'd': /* set debug level */
205 if (*optarg == 't') {
206 dbg_timestamp = true;
209 /* We probably find a tag list -d 10,sql,bvfs */
210 if ((p = strchr(optarg, ',')) != NULL) {
213 debug_level = atoi(optarg);
214 if (debug_level <= 0) {
218 debug_parse_tags(p+1, &debug_level_tags);
221 Dmsg1(10, "Debug level = %lld\n", debug_level);
228 case 'f': /* run in foreground */
232 case 'g': /* set group id */
236 case 'm': /* print kaboom output */
240 case 'P': /* no pid file */
241 make_pid_file = false;
244 case 'r': /* run job */
245 if (runjob != NULL) {
249 runjob = bstrdup(optarg);
253 case 's': /* turn off signals */
257 case 't': /* test config */
261 case 'u': /* set uid */
265 case 'v': /* verbose */
279 if (configfile != NULL) {
282 configfile = bstrdup(*argv);
290 if (!foreground && !test_config) {
292 init_stack_dump(); /* grab new pid */
296 init_signals(terminate_dird);
299 if (configfile == NULL) {
300 configfile = bstrdup(CONFIG_FILE);
303 config = New(CONFIG());
304 parse_dir_config(config, configfile, M_ERROR_TERM);
306 if (init_crypto() != 0) {
307 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Cryptography library initialization failed.\n"));
310 if (!check_resources()) {
311 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
315 /* Create pid must come after we are a daemon -- so we have our final pid */
317 create_pid_file(director->pid_directory, "bacula-dir",
318 get_first_port_host_order(director->DIRaddrs));
320 read_state_file(director->working_directory, "bacula-dir",
321 get_first_port_host_order(director->DIRaddrs));
324 set_jcr_in_tsd(INVALID_JCR);
325 set_thread_concurrency(director->MaxConcurrentJobs * 2 +
326 4 /* UA */ + 5 /* sched+watchdog+jobsvr+misc */);
327 lmgr_init_thread(); /* initialize the lockmanager stack */
329 load_dir_plugins(director->plugin_directory);
331 drop(uid, gid, false); /* reduce privileges if requested */
333 /* If we are in testing mode, we don't try to fix the catalog */
334 cat_op mode=(test_config)?CHECK_CONNECTION:UPDATE_AND_FIX;
336 if (!check_catalog(mode)) {
337 Jmsg((JCR *)NULL, M_ERROR_TERM, 0, _("Please correct configuration file: %s\n"), configfile);
344 my_name_is(0, NULL, director->name()); /* set user defined name */
348 /* Plug database interface for library routines */
349 p_sql_query = (sql_query_call)dir_sql_query;
350 p_sql_escape = (sql_escape_call)dir_sql_escape;
352 FDConnectTimeout = (int)director->FDConnectTimeout;
353 SDConnectTimeout = (int)director->SDConnectTimeout;
355 resize_reload(director->MaxReload);
357 #if !defined(HAVE_WIN32)
358 signal(SIGHUP, reload_config);
361 init_console_msg(working_directory);
363 Dmsg0(200, "Start UA server\n");
364 start_UA_server(director->DIRaddrs);
366 start_watchdog(); /* start network watchdog thread */
368 init_jcr_subsystem(); /* start JCR watchdogs etc. */
370 init_job_server(director->MaxConcurrentJobs);
372 dbg_jcr_add_hook(dir_debug_print); /* used to director variables */
373 dbg_jcr_add_hook(bdb_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 struct RELOAD_TABLE {
398 static int max_reloads = 32;
399 static RELOAD_TABLE *reload_table=NULL;
401 static void resize_reload(int nb)
403 if (nb <= max_reloads) {
407 reload_table = (RELOAD_TABLE*)realloc(reload_table, nb * sizeof(RELOAD_TABLE));
408 for (int i=max_reloads; i < nb ; i++) {
409 reload_table[i].job_count = 0;
410 reload_table[i].res_head = NULL;
415 static void init_reload(void)
417 reload_table = (RELOAD_TABLE*)malloc(max_reloads * sizeof(RELOAD_TABLE));
418 for (int i=0; i < max_reloads; i++) {
419 reload_table[i].job_count = 0;
420 reload_table[i].res_head = NULL;
425 * This subroutine frees a saved resource table.
426 * It was saved when a new table was created with "reload"
428 static void free_saved_resources(int table)
431 int num = r_last - r_first + 1;
432 RES_HEAD **res_tab = reload_table[table].res_head;
434 if (res_tab == NULL) {
435 Dmsg1(100, "res_tab for table %d already released.\n", table);
438 Dmsg1(100, "Freeing resources for table %d\n", table);
439 for (int j=0; j<num; j++) {
441 next = res_tab[j]->first;
444 next = res->res_next;
445 free_resource(res, r_first + j);
447 free(res_tab[j]->res_list);
453 reload_table[table].job_count = 0;
454 reload_table[table].res_head = NULL;
458 * Called here at the end of every job that was
459 * hooked decrementing the active job_count. When
460 * it goes to zero, no one is using the associated
461 * resource table, so free it.
463 static void reload_job_end_cb(JCR *jcr, void *ctx)
465 int reload_id = (int)((intptr_t)ctx);
466 Dmsg3(100, "reload job_end JobId=%d table=%d cnt=%d\n", jcr->JobId,
467 reload_id, reload_table[reload_id].job_count);
470 if (--reload_table[reload_id].job_count <= 0) {
471 free_saved_resources(reload_id);
477 static int find_free_reload_table_entry()
480 for (int i=0; i < max_reloads; i++) {
481 if (reload_table[i].res_head == NULL) {
489 static pthread_mutex_t reload_mutex = PTHREAD_MUTEX_INITIALIZER;
492 * If we get here, we have received a SIGHUP, which means to
493 * reread our configuration file.
495 * The algorithm used is as follows: we count how many jobs are
496 * running and mark the running jobs to make a callback on
497 * exiting. The old config is saved with the reload table
498 * id in a reload table. The new config file is read. Now, as
499 * each job exits, it calls back to the reload_job_end_cb(), which
500 * decrements the count of open jobs for the given reload table.
501 * When the count goes to zero, we release those resources.
502 * This allows us to have pointers into the resource table (from
503 * jobs), and once they exit and all the pointers are released, we
504 * release the old table. Note, if no new jobs are running since the
505 * last reload, then the old resources will be immediately release.
506 * A console is considered a job because it may have pointers to
507 * resources, but a SYSTEM job is not since it *should* not have any
508 * permanent pointers to jobs.
511 void reload_config(int sig)
513 static bool already_here = false;
514 #if !defined(HAVE_WIN32)
518 int njobs = 0; /* number of running jobs */
523 /* Wait to do the reload */
529 Qmsg(NULL, M_INFO, 0, _("Already doing a reload request, "
530 "request ignored.\n"));
533 Dmsg0(10, "Already doing a reload request, waiting a bit\n");
542 #if !defined(HAVE_WIN32)
544 sigaddset(&set, SIGHUP);
545 sigprocmask(SIG_BLOCK, &set, NULL);
551 table = find_free_reload_table_entry();
553 Qmsg(NULL, M_ERROR, 0, _("Too many (%d) open reload requests. "
554 "Request ignored.\n"), max_reloads);
558 Dmsg1(100, "Reload_config njobs=%d\n", njobs);
559 /* Save current res_head */
560 reload_table[table].res_head = res_head;
561 Dmsg1(100, "Saved old config in table %d\n", table);
563 /* Create a new res_head and parse into it */
564 ok = parse_dir_config(config, configfile, M_ERROR);
566 Dmsg0(100, "Reloaded config file\n");
567 if (!ok || !check_resources() || !check_catalog(UPDATE_CATALOG)) {
569 * We got an error, save broken point, restore old one,
570 * then release everything from broken pointer.
572 rtable = find_free_reload_table_entry(); /* save new, bad table */
574 Qmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
575 Qmsg(NULL, M_ERROR_TERM, 0, _("Out of reload table entries. Giving up.\n"));
577 Qmsg(NULL, M_ERROR, 0, _("Please correct configuration file: %s\n"), configfile);
578 Qmsg(NULL, M_ERROR, 0, _("Resetting previous configuration.\n"));
580 /* Save broken res_head pointer */
581 reload_table[rtable].res_head = res_head;
583 /* Now restore old resource pointer */
584 res_head = reload_table[table].res_head;
585 table = rtable; /* release new, bad, saved table below */
587 invalidate_schedules();
589 * Hook all active jobs so that they release this table
592 if (jcr->getJobType() != JT_SYSTEM) {
593 reload_table[table].job_count++;
594 job_end_push(jcr, reload_job_end_cb, (void *)((long int)table));
600 * Now walk through globals tables and plug them into the
604 foreach_dlist(cg, &client_globals) {
606 client = GetClientResWithName(cg->name);
608 Qmsg(NULL, M_INFO, 0, _("Client=%s not found. Assuming it was removed!!!\n"), cg->name);
610 client->globals = cg; /* Set globals pointer */
614 foreach_dlist(sg, &store_globals) {
616 store = GetStoreResWithName(sg->name);
618 Qmsg(NULL, M_INFO, 0, _("Storage=%s not found. Assuming it was removed!!!\n"), sg->name);
620 store->globals = sg; /* set globals pointer */
621 Dmsg2(200, "Reload found numConcurrent=%ld for Store %s\n",
622 sg->NumConcurrentJobs, sg->name);
626 foreach_dlist(jg, &job_globals) {
628 job = GetJobResWithName(jg->name);
630 Qmsg(NULL, M_INFO, 0, _("Job=%s not found. Assuming it was removed!!!\n"), jg->name);
632 job->globals = jg; /* Set globals pointer */
636 foreach_dlist(schg, &sched_globals) {
638 sched = GetSchedResWithName(schg->name);
640 Qmsg(NULL, M_INFO, 0, _("Schedule=%s not found. Assuming it was removed!!!\n"), schg->name);
642 sched->globals = schg; /* Set globals pointer */
647 /* Reset other globals */
648 set_working_directory(director->working_directory);
649 FDConnectTimeout = director->FDConnectTimeout;
650 SDConnectTimeout = director->SDConnectTimeout;
651 Dmsg0(10, "Director's configuration file reread.\n");
653 /* Now release saved resources, if no jobs using the resources */
655 free_saved_resources(table);
661 #if !defined(HAVE_WIN32)
662 sigprocmask(SIG_UNBLOCK, &set, NULL);
663 signal(SIGHUP, reload_config);
665 already_here = false;
668 /* Cleanup and then exit */
669 void terminate_dird(int sig)
671 static bool already_here = false;
673 if (already_here) { /* avoid recursive temination problems */
674 bmicrosleep(2, 0); /* yield */
678 debug_level = 0; /* turn off debug */
680 generate_daemon_event(NULL, "Exit");
683 write_state_file(director->working_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
685 delete_pid_file(director->pid_directory, "bacula-dir", get_first_port_host_order(director->DIRaddrs));
693 if (configfile != NULL) {
697 print_memory_pool_stats();
704 term_msg(); /* terminate message handler */
707 free_daemon_message_queue();
715 * Now walk through resource globals tables and release them
718 foreach_dlist(cg, &client_globals) {
720 if (cg->SetIPaddress) {
721 free(cg->SetIPaddress);
726 foreach_dlist(sg, &store_globals) {
731 foreach_dlist(jg, &job_globals) {
735 close_memory_pool(); /* release free memory in pool */
742 * Make a quick check to see that we have all the
745 * **** FIXME **** this routine could be a lot more
746 * intelligent and comprehensive.
748 static bool check_resources()
756 job = (JOB *)GetNextRes(R_JOB, NULL);
757 director = (DIRRES *)GetNextRes(R_DIRECTOR, NULL);
759 Jmsg(NULL, M_FATAL, 0, _("No Director resource defined in %s\n"
760 "Without that I don't know who I am :-(\n"), configfile);
763 set_working_directory(director->working_directory);
764 if (!director->messages) { /* If message resource not specified */
765 director->messages = (MSGS *)GetNextRes(R_MSGS, NULL);
766 if (!director->messages) {
767 Jmsg(NULL, M_FATAL, 0, _("No Messages resource defined in %s\n"), configfile);
771 if (GetNextRes(R_DIRECTOR, (RES *)director) != NULL) {
772 Jmsg(NULL, M_FATAL, 0, _("Only one Director resource permitted in %s\n"),
776 /* tls_require implies tls_enable */
777 if (director->tls_require) {
779 director->tls_enable = true;
781 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
786 need_tls = director->tls_enable || director->tls_authenticate;
788 if (!director->tls_certfile && need_tls) {
789 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Director \"%s\" in %s.\n"),
790 director->name(), configfile);
794 if (!director->tls_keyfile && need_tls) {
795 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Director \"%s\" in %s.\n"),
796 director->name(), configfile);
800 if ((!director->tls_ca_certfile && !director->tls_ca_certdir) &&
801 need_tls && director->tls_verify_peer) {
802 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
803 " Certificate Dir\" are defined for Director \"%s\" in %s."
804 " At least one CA certificate store is required"
805 " when using \"TLS Verify Peer\".\n"),
806 director->name(), configfile);
810 /* If everything is well, attempt to initialize our per-resource TLS context */
811 if (OK && (need_tls || director->tls_require)) {
812 /* Initialize TLS context:
813 * Args: CA certfile, CA certdir, Certfile, Keyfile,
814 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
815 director->tls_ctx = new_tls_context(director->tls_ca_certfile,
816 director->tls_ca_certdir, director->tls_certfile,
817 director->tls_keyfile, NULL, NULL, director->tls_dhfile,
818 director->tls_verify_peer);
820 if (!director->tls_ctx) {
821 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Director \"%s\" in %s.\n"),
822 director->name(), configfile);
829 Jmsg(NULL, M_FATAL, 0, _("No Job records defined in %s\n"), configfile);
832 foreach_res(job, R_JOB) {
836 JOB *jobdefs = job->jobdefs;
837 /* Handle RunScripts alists specifically */
838 if (jobdefs->RunScripts) {
841 if (!job->RunScripts) {
842 job->RunScripts = New(alist(10, not_owned_by_alist));
845 foreach_alist(rs, jobdefs->RunScripts) {
846 elt = copy_runscript(rs);
847 job->RunScripts->append(elt); /* we have to free it */
851 /* Transfer default items from JobDefs Resource */
852 for (i=0; job_items[i].name; i++) {
853 char **def_svalue, **svalue; /* string value */
854 uint32_t *def_ivalue, *ivalue; /* integer value */
855 bool *def_bvalue, *bvalue; /* bool value */
856 int64_t *def_lvalue, *lvalue; /* 64 bit values */
857 alist **def_avalue, **avalue; /* alist values */
860 Dmsg4(1400, "Job \"%s\", field \"%s\" bit=%d def=%d\n",
861 job->name(), job_items[i].name,
862 bit_is_set(i, job->hdr.item_present),
863 bit_is_set(i, job->jobdefs->hdr.item_present));
865 if (!bit_is_set(i, job->hdr.item_present) &&
866 bit_is_set(i, job->jobdefs->hdr.item_present)) {
867 Dmsg2(400, "Job \"%s\", field \"%s\": getting default.\n",
868 job->name(), job_items[i].name);
869 offset = (char *)(job_items[i].value) - (char *)&res_all;
871 * Handle strings and directory strings
873 if (job_items[i].handler == store_str ||
874 job_items[i].handler == store_dir) {
875 def_svalue = (char **)((char *)(job->jobdefs) + offset);
876 Dmsg5(400, "Job \"%s\", field \"%s\" def_svalue=%s item %d offset=%u\n",
877 job->name(), job_items[i].name, *def_svalue, i, offset);
878 svalue = (char **)((char *)job + offset);
880 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
882 *svalue = bstrdup(*def_svalue);
883 set_bit(i, job->hdr.item_present);
887 } else if (job_items[i].handler == store_res) {
888 def_svalue = (char **)((char *)(job->jobdefs) + offset);
889 Dmsg4(400, "Job \"%s\", field \"%s\" item %d offset=%u\n",
890 job->name(), job_items[i].name, i, offset);
891 svalue = (char **)((char *)job + offset);
893 Pmsg1(000, _("Hey something is wrong. p=0x%lu\n"), *svalue);
895 *svalue = *def_svalue;
896 set_bit(i, job->hdr.item_present);
898 * Handle alist resources
900 } else if (job_items[i].handler == store_alist_res) {
903 def_avalue = (alist **)((char *)(job->jobdefs) + offset);
904 avalue = (alist **)((char *)job + offset);
906 *avalue = New(alist(10, not_owned_by_alist));
908 foreach_alist(elt, (*def_avalue)) {
909 (*avalue)->append(elt);
911 set_bit(i, job->hdr.item_present);
913 * Handle integer fields
914 * Note, our store_bit does not handle bitmaped fields
916 } else if (job_items[i].handler == store_bit ||
917 job_items[i].handler == store_pint32 ||
918 job_items[i].handler == store_jobtype ||
919 job_items[i].handler == store_level ||
920 job_items[i].handler == store_int32 ||
921 job_items[i].handler == store_size32 ||
922 job_items[i].handler == store_migtype ||
923 job_items[i].handler == store_replace) {
924 def_ivalue = (uint32_t *)((char *)(job->jobdefs) + offset);
925 Dmsg5(400, "Job \"%s\", field \"%s\" def_ivalue=%d item %d offset=%u\n",
926 job->name(), job_items[i].name, *def_ivalue, i, offset);
927 ivalue = (uint32_t *)((char *)job + offset);
928 *ivalue = *def_ivalue;
929 set_bit(i, job->hdr.item_present);
931 * Handle 64 bit integer fields
933 } else if (job_items[i].handler == store_time ||
934 job_items[i].handler == store_size64 ||
935 job_items[i].handler == store_speed ||
936 job_items[i].handler == store_int64) {
937 def_lvalue = (int64_t *)((char *)(job->jobdefs) + offset);
938 Dmsg5(400, "Job \"%s\", field \"%s\" def_lvalue=%" lld " item %d offset=%u\n",
939 job->name(), job_items[i].name, *def_lvalue, i, offset);
940 lvalue = (int64_t *)((char *)job + offset);
941 *lvalue = *def_lvalue;
942 set_bit(i, job->hdr.item_present);
946 } else if (job_items[i].handler == store_bool) {
947 def_bvalue = (bool *)((char *)(job->jobdefs) + offset);
948 Dmsg5(400, "Job \"%s\", field \"%s\" def_bvalue=%d item %d offset=%u\n",
949 job->name(), job_items[i].name, *def_bvalue, i, offset);
950 bvalue = (bool *)((char *)job + offset);
951 *bvalue = *def_bvalue;
952 set_bit(i, job->hdr.item_present);
958 * Ensure that all required items are present
960 for (i=0; job_items[i].name; i++) {
961 if (job_items[i].flags & ITEM_REQUIRED) {
962 if (!bit_is_set(i, job->hdr.item_present)) {
963 Jmsg(NULL, M_ERROR_TERM, 0, _("\"%s\" directive in Job \"%s\" resource is required, but not found.\n"),
964 job_items[i].name, job->name());
968 /* If this triggers, take a look at lib/parse_conf.h */
969 if (i >= MAX_RES_ITEMS) {
970 Emsg0(M_ERROR_TERM, 0, _("Too many items in Job resource\n"));
973 if (!job->storage && !job->pool->storage) {
974 Jmsg(NULL, M_FATAL, 0, _("No storage specified in Job \"%s\" nor in Pool.\n"),
979 /* Make sure the job doesn't use the Scratch Pool to start with */
981 if (!check_pool(job->JobType, job->JobLevel,
982 job->pool, job->next_pool, &name)) {
983 Jmsg(NULL, M_FATAL, 0,
984 _("%s \"Scratch\" not valid in Job \"%s\".\n"),
988 } /* End loop over Job res */
991 /* Loop over Consoles */
993 foreach_res(cons, R_CONSOLE) {
994 /* tls_require implies tls_enable */
995 if (cons->tls_require) {
997 cons->tls_enable = true;
999 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1005 need_tls = cons->tls_enable || cons->tls_authenticate;
1007 if (!cons->tls_certfile && need_tls) {
1008 Jmsg(NULL, M_FATAL, 0, _("\"TLS Certificate\" file not defined for Console \"%s\" in %s.\n"),
1009 cons->name(), configfile);
1013 if (!cons->tls_keyfile && need_tls) {
1014 Jmsg(NULL, M_FATAL, 0, _("\"TLS Key\" file not defined for Console \"%s\" in %s.\n"),
1015 cons->name(), configfile);
1019 if ((!cons->tls_ca_certfile && !cons->tls_ca_certdir)
1020 && need_tls && cons->tls_verify_peer) {
1021 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\" or \"TLS CA"
1022 " Certificate Dir\" are defined for Console \"%s\" in %s."
1023 " At least one CA certificate store is required"
1024 " when using \"TLS Verify Peer\".\n"),
1025 cons->name(), configfile);
1028 /* If everything is well, attempt to initialize our per-resource TLS context */
1029 if (OK && (need_tls || cons->tls_require)) {
1030 /* Initialize TLS context:
1031 * Args: CA certfile, CA certdir, Certfile, Keyfile,
1032 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1033 cons->tls_ctx = new_tls_context(cons->tls_ca_certfile,
1034 cons->tls_ca_certdir, cons->tls_certfile,
1035 cons->tls_keyfile, NULL, NULL, cons->tls_dhfile, cons->tls_verify_peer);
1037 if (!cons->tls_ctx) {
1038 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
1039 cons->name(), configfile);
1046 /* Loop over Clients */
1048 foreach_res(client, R_CLIENT) {
1049 /* tls_require implies tls_enable */
1050 if (client->tls_require) {
1052 client->tls_enable = true;
1054 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1059 need_tls = client->tls_enable || client->tls_authenticate;
1060 if ((!client->tls_ca_certfile && !client->tls_ca_certdir) && need_tls) {
1061 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
1062 " or \"TLS CA Certificate Dir\" are defined for File daemon \"%s\" in %s.\n"),
1063 client->name(), configfile);
1067 /* If everything is well, attempt to initialize our per-resource TLS context */
1068 if (OK && (need_tls || client->tls_require)) {
1069 /* Initialize TLS context:
1070 * Args: CA certfile, CA certdir, Certfile, Keyfile,
1071 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1072 client->tls_ctx = new_tls_context(client->tls_ca_certfile,
1073 client->tls_ca_certdir, client->tls_certfile,
1074 client->tls_keyfile, NULL, NULL, NULL,
1077 if (!client->tls_ctx) {
1078 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for File daemon \"%s\" in %s.\n"),
1079 client->name(), configfile);
1085 /* Loop over all pools, check PoolType */
1087 foreach_res(pool, R_POOL) {
1088 if (!pool->pool_type) {
1089 /* This case is checked by the parse engine, we should not */
1090 Jmsg(NULL, M_FATAL, 0, _("PoolType required in Pool resource \"%s\".\n"), pool->hdr.name);
1094 if ((strcasecmp(pool->pool_type, NT_("backup")) != 0) &&
1095 (strcasecmp(pool->pool_type, NT_("copy")) != 0) &&
1096 (strcasecmp(pool->pool_type, NT_("cloned")) != 0) &&
1097 (strcasecmp(pool->pool_type, NT_("archive")) != 0) &&
1098 (strcasecmp(pool->pool_type, NT_("migration")) != 0) &&
1099 (strcasecmp(pool->pool_type, NT_("scratch")) != 0))
1101 Jmsg(NULL, M_FATAL, 0, _("Invalid PoolType \"%s\" in Pool resource \"%s\".\n"), pool->pool_type, pool->hdr.name);
1105 if (pool->NextPool && strcmp(pool->NextPool->name(), "Scratch") == 0) {
1106 Jmsg(NULL, M_FATAL, 0,
1107 _("NextPool \"Scratch\" not valid in Pool \"%s\".\n"),
1115 close_msg(NULL); /* close temp message handler */
1116 init_msg(NULL, director->messages); /* open daemon message handler */
1117 last_reload_time = time(NULL);
1124 * - we can check the connection (mode=CHECK_CONNECTION)
1125 * - we can synchronize the catalog with the configuration (mode=UPDATE_CATALOG)
1126 * - we can synchronize, and fix old job records (mode=UPDATE_AND_FIX)
1127 * - we hook up the Autochange children with the parent, and
1128 * we hook the shared autochangers together.
1130 static bool check_catalog(cat_op mode)
1134 STORE *store, *ac_child;
1136 /* Loop over databases */
1138 foreach_res(catalog, R_CATALOG) {
1141 * Make sure we can open catalog, otherwise print a warning
1142 * message because the server is probably not running.
1144 db = db_init_database(NULL, catalog->db_driver, catalog->db_name,
1146 catalog->db_password, catalog->db_address,
1147 catalog->db_port, catalog->db_socket,
1148 catalog->db_ssl_mode, catalog->db_ssl_key,
1149 catalog->db_ssl_cert, catalog->db_ssl_ca,
1150 catalog->db_ssl_capath, catalog->db_ssl_cipher,
1151 catalog->mult_db_connections,
1152 catalog->disable_batch_insert);
1153 if (!db || !db_open_database(NULL, db)) {
1154 Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1155 catalog->name(), catalog->db_name);
1156 Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
1157 catalog->name(), catalog->db_name);
1159 Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
1160 Pmsg1(000, "%s", db_strerror(db));
1161 db_close_database(NULL, db);
1167 /* Display a message if the db max_connections is too low */
1168 if (!db_check_max_connections(NULL, db, director->MaxConcurrentJobs)) {
1169 Pmsg1(000, "Warning, settings problem for Catalog=%s\n", catalog->name());
1170 Pmsg1(000, "%s", db_strerror(db));
1173 /* we are in testing mode, so don't touch anything in the catalog */
1174 if (mode == CHECK_CONNECTION) {
1175 if (db) db_close_database(NULL, db);
1179 /* Loop over all pools, defining/updating them in each database */
1181 foreach_res(pool, R_POOL) {
1183 * If the Pool has a catalog resource create the pool only
1186 if (!pool->catalog || pool->catalog == catalog) {
1187 create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
1191 /* Once they are created, we can loop over them again, updating
1192 * references (RecyclePool)
1194 foreach_res(pool, R_POOL) {
1196 * If the Pool has a catalog resource update the pool only
1199 if (!pool->catalog || pool->catalog == catalog) {
1200 update_pool_references(NULL, db, pool);
1204 /* Ensure basic client record is in DB */
1206 foreach_res(client, R_CLIENT) {
1208 /* Create clients only if they use the current catalog */
1209 if (client->catalog != catalog) {
1210 Dmsg3(500, "Skip client=%s with cat=%s not catalog=%s\n",
1211 client->name(), client->catalog->name(), catalog->name());
1214 Dmsg2(500, "create cat=%s for client=%s\n",
1215 client->catalog->name(), client->name());
1216 memset(&cr, 0, sizeof(cr));
1217 bstrncpy(cr.Name, client->name(), sizeof(cr.Name));
1219 db_create_client_record(NULL, db, &cr);
1222 /* Ensure basic storage record is in DB */
1223 foreach_res(store, R_STORAGE) {
1226 memset(&sr, 0, sizeof(sr));
1227 memset(&mtr, 0, sizeof(mtr));
1228 if (store->media_type) {
1229 bstrncpy(mtr.MediaType, store->media_type, sizeof(mtr.MediaType));
1231 db_create_mediatype_record(NULL, db, &mtr);
1233 mtr.MediaTypeId = 0;
1235 bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
1236 sr.AutoChanger = store->autochanger;
1237 if (!db_create_storage_record(NULL, db, &sr)) {
1238 Jmsg(NULL, M_FATAL, 0, _("Could not create storage record for %s\n"),
1242 store->StorageId = sr.StorageId; /* set storage Id */
1243 if (!sr.created) { /* if not created, update it */
1244 sr.AutoChanger = store->autochanger;
1245 if (!db_update_storage_record(NULL, db, &sr)) {
1246 Jmsg(NULL, M_FATAL, 0, _("Could not update storage record for %s\n"),
1252 /* tls_require implies tls_enable */
1253 if (store->tls_require) {
1255 store->tls_enable = true;
1257 Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
1262 need_tls = store->tls_enable || store->tls_authenticate;
1264 if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
1265 Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
1266 " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
1267 store->name(), configfile);
1271 /* If everything is well, attempt to initialize our per-resource TLS context */
1272 if (OK && (need_tls || store->tls_require)) {
1273 /* Initialize TLS context:
1274 * Args: CA certfile, CA certdir, Certfile, Keyfile,
1275 * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
1276 store->tls_ctx = new_tls_context(store->tls_ca_certfile,
1277 store->tls_ca_certdir, store->tls_certfile,
1278 store->tls_keyfile, NULL, NULL, NULL, true);
1280 if (!store->tls_ctx) {
1281 Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
1282 store->name(), configfile);
1288 /* Link up all the children for each changer */
1289 foreach_res(store, R_STORAGE) {
1291 if (store->changer == store) { /* we are a real Autochanger */
1292 store->ac_group = get_pool_memory(PM_FNAME);
1293 store->ac_group[0] = 0;
1294 pm_strcat(store->ac_group, edit_int64(store->StorageId, sid));
1295 /* Now look for children who point to this storage */
1296 foreach_res(ac_child, R_STORAGE) {
1297 if (ac_child != store && ac_child->changer == store) {
1298 /* Found a child -- add StorageId */
1299 pm_strcat(store->ac_group, ",");
1300 pm_strcat(store->ac_group, edit_int64(ac_child->StorageId, sid));
1306 /* Link up all the shared storage devices */
1307 foreach_res(store, R_STORAGE) {
1308 if (store->ac_group) { /* we are a real Autochanger */
1309 /* Now look for Shared Storage who point to this storage */
1310 foreach_res(ac_child, R_STORAGE) {
1311 if (ac_child->shared_storage == store && ac_child->ac_group &&
1312 ac_child->shared_storage != ac_child) {
1313 pm_strcat(store->ac_group, ",");
1314 pm_strcat(store->ac_group, ac_child->ac_group);
1320 /* Loop over all counters, defining them in each database */
1321 /* Set default value in all counters */
1323 foreach_res(counter, R_COUNTER) {
1324 /* Write to catalog? */
1325 if (!counter->created && counter->Catalog == catalog) {
1327 bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
1328 cr.MinValue = counter->MinValue;
1329 cr.MaxValue = counter->MaxValue;
1330 cr.CurrentValue = counter->MinValue;
1331 if (counter->WrapCounter) {
1332 bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
1334 cr.WrapCounter[0] = 0; /* empty string */
1336 if (db_create_counter_record(NULL, db, &cr)) {
1337 counter->CurrentValue = cr.CurrentValue;
1338 counter->created = true;
1339 Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
1342 if (!counter->created) {
1343 counter->CurrentValue = counter->MinValue; /* default value */
1346 /* cleanup old job records */
1347 if (mode == UPDATE_AND_FIX) {
1348 db_sql_query(db, cleanup_created_job, NULL, NULL);
1349 db_sql_query(db, cleanup_running_job, NULL, NULL);
1352 /* Set SQL engine name in global for debugging */
1353 set_db_engine_name(db_get_engine_name(db));
1354 if (db) db_close_database(NULL, db);
1359 static void cleanup_old_files()
1362 struct dirent *entry, *result;
1364 int my_name_len = strlen(my_name);
1365 int len = strlen(director->working_directory);
1366 POOLMEM *cleanup = get_pool_memory(PM_MESSAGE);
1367 POOLMEM *basename = get_pool_memory(PM_MESSAGE);
1370 const int nmatch = 30;
1371 regmatch_t pmatch[nmatch];
1374 /* Exclude spaces and look for .mail, .tmp or .restore.xx.bsr files */
1375 const char *pat1 = "^[^ ]+\\.(restore\\.[^ ]+\\.bsr|mail|tmp)$";
1377 /* Setup working directory prefix */
1378 pm_strcpy(basename, director->working_directory);
1379 if (len > 0 && !IsPathSeparator(director->working_directory[len-1])) {
1380 pm_strcat(basename, "/");
1383 /* Compile regex expressions */
1384 rc = regcomp(&preg1, pat1, REG_EXTENDED);
1386 regerror(rc, &preg1, prbuf, sizeof(prbuf));
1387 Pmsg2(000, _("Could not compile regex pattern \"%s\" ERR=%s\n"),
1392 name_max = pathconf(".", _PC_NAME_MAX);
1393 if (name_max < 1024) {
1397 if (!(dp = opendir(director->working_directory))) {
1399 Pmsg2(000, "Failed to open working dir %s for cleanup: ERR=%s\n",
1400 director->working_directory, be.bstrerror());
1405 entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 1000);
1407 if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
1410 /* Exclude any name with ., .., not my_name or containing a space */
1411 if (strcmp(result->d_name, ".") == 0 || strcmp(result->d_name, "..") == 0 ||
1412 strncmp(result->d_name, my_name, my_name_len) != 0) {
1413 Dmsg1(500, "Skipped: %s\n", result->d_name);
1417 /* Unlink files that match regexes */
1418 if (regexec(&preg1, result->d_name, nmatch, pmatch, 0) == 0) {
1419 pm_strcpy(cleanup, basename);
1420 pm_strcat(cleanup, result->d_name);
1421 Dmsg1(100, "Unlink: %s\n", cleanup);
1428 /* Be careful to free up the correct resources */
1432 free_pool_memory(cleanup);
1433 free_pool_memory(basename);