+
+static bool check_catalog()
+{
+ bool OK = true;
+ bool need_tls;
+
+ /* Loop over databases */
+ CAT *catalog;
+ foreach_res(catalog, R_CATALOG) {
+ B_DB *db;
+ /*
+ * Make sure we can open catalog, otherwise print a warning
+ * message because the server is probably not running.
+ */
+ db = db_init(NULL, catalog->db_driver, catalog->db_name, catalog->db_user,
+ catalog->db_password, catalog->db_address,
+ catalog->db_port, catalog->db_socket,
+ catalog->mult_db_connections);
+ if (!db || !db_open_database(NULL, db)) {
+ Pmsg2(000, _("Could not open Catalog \"%s\", database \"%s\".\n"),
+ catalog->name(), catalog->db_name);
+ Jmsg(NULL, M_FATAL, 0, _("Could not open Catalog \"%s\", database \"%s\".\n"),
+ catalog->name(), catalog->db_name);
+ if (db) {
+ Jmsg(NULL, M_FATAL, 0, _("%s"), db_strerror(db));
+ Pmsg1(000, "%s", db_strerror(db));
+ db_close_database(NULL, db);
+ }
+ OK = false;
+ continue;
+ }
+
+ /* Loop over all pools, defining/updating them in each database */
+ POOL *pool;
+ foreach_res(pool, R_POOL) {
+ create_pool(NULL, db, pool, POOL_OP_UPDATE); /* update request */
+ }
+
+ /* Loop over all pools for updating RecyclePool */
+ foreach_res(pool, R_POOL) {
+ update_pool_recyclepool(NULL, db, pool);
+ }
+
+ STORE *store;
+ foreach_res(store, R_STORAGE) {
+ STORAGE_DBR sr;
+ MEDIATYPE_DBR mr;
+ if (store->media_type) {
+ bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
+ mr.ReadOnly = 0;
+ db_create_mediatype_record(NULL, db, &mr);
+ } else {
+ mr.MediaTypeId = 0;
+ }
+ bstrncpy(sr.Name, store->name(), sizeof(sr.Name));
+ sr.AutoChanger = store->autochanger;
+ db_create_storage_record(NULL, db, &sr);
+ store->StorageId = sr.StorageId; /* set storage Id */
+ if (!sr.created) { /* if not created, update it */
+ db_update_storage_record(NULL, db, &sr);
+ }
+
+ /* tls_require implies tls_enable */
+ if (store->tls_require) {
+ if (have_tls) {
+ store->tls_enable = true;
+ } else {
+ Jmsg(NULL, M_FATAL, 0, _("TLS required but not configured in Bacula.\n"));
+ OK = false;
+ }
+ }
+
+ need_tls = store->tls_enable || store->tls_authenticate;
+
+ if ((!store->tls_ca_certfile && !store->tls_ca_certdir) && need_tls) {
+ Jmsg(NULL, M_FATAL, 0, _("Neither \"TLS CA Certificate\""
+ " or \"TLS CA Certificate Dir\" are defined for Storage \"%s\" in %s.\n"),
+ store->name(), configfile);
+ OK = false;
+ }
+
+ /* If everything is well, attempt to initialize our per-resource TLS context */
+ if (OK && (need_tls || store->tls_require)) {
+ /* Initialize TLS context:
+ * Args: CA certfile, CA certdir, Certfile, Keyfile,
+ * Keyfile PEM Callback, Keyfile CB Userdata, DHfile, Verify Peer */
+ store->tls_ctx = new_tls_context(store->tls_ca_certfile,
+ store->tls_ca_certdir, store->tls_certfile,
+ store->tls_keyfile, NULL, NULL, NULL, true);
+
+ if (!store->tls_ctx) {
+ Jmsg(NULL, M_FATAL, 0, _("Failed to initialize TLS context for Storage \"%s\" in %s.\n"),
+ store->name(), configfile);
+ OK = false;
+ }
+ }
+ }
+
+ /* Loop over all counters, defining them in each database */
+ /* Set default value in all counters */
+ COUNTER *counter;
+ foreach_res(counter, R_COUNTER) {
+ /* Write to catalog? */
+ if (!counter->created && counter->Catalog == catalog) {
+ COUNTER_DBR cr;
+ bstrncpy(cr.Counter, counter->name(), sizeof(cr.Counter));
+ cr.MinValue = counter->MinValue;
+ cr.MaxValue = counter->MaxValue;
+ cr.CurrentValue = counter->MinValue;
+ if (counter->WrapCounter) {
+ bstrncpy(cr.WrapCounter, counter->WrapCounter->name(), sizeof(cr.WrapCounter));
+ } else {
+ cr.WrapCounter[0] = 0; /* empty string */
+ }
+ if (db_create_counter_record(NULL, db, &cr)) {
+ counter->CurrentValue = cr.CurrentValue;
+ counter->created = true;
+ Dmsg2(100, "Create counter %s val=%d\n", counter->name(), counter->CurrentValue);
+ }
+ }
+ if (!counter->created) {
+ counter->CurrentValue = counter->MinValue; /* default value */
+ }
+ }
+ db_close_database(NULL, db);
+ }
+ /* Set type in global for debugging */
+ set_db_type(db_get_type());
+ return OK;
+}