]> code.citadel.org Git - citadel.git/blobdiff - citadel/server/backends/common/database.c
Makefile: restructured so that backends can be compiled into utilities
[citadel.git] / citadel / server / backends / common / database.c
diff --git a/citadel/server/backends/common/database.c b/citadel/server/backends/common/database.c
new file mode 100644 (file)
index 0000000..ac1ebba
--- /dev/null
@@ -0,0 +1,79 @@
+// Copyright (c) 1987-2023 by the citadel.org team
+// This program is open source software.  Use, duplication, or disclosure
+// are subject to the terms of the GNU General Public License, version 3.
+
+// The functions in this file handle the selection and activation of a storage backend for Citadel Server.
+// Right now, it simply activates Berkeley DB because that's the only one we have.
+
+#include "../../sysdep.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <libcitadel.h>
+#include "../../ctdl_module.h"
+#include "../../control.h"
+#include "../../citserver.h"
+#include "../../config.h"
+
+// Header files for all available backends must be included here.
+#include "../berkeley_db/berkeley_db.h"
+
+// Backends must include implementations of all these functions, but with their own prefix instead of "cdb_".
+// The initialization function of the selected backend will map them.
+void                   (*cdb_open_databases)(void)                             = NULL;
+void                   (*cdb_close_databases)(void)                            = NULL;
+struct cdbdata                 (*cdb_fetch)(int, const void *, int)                    = NULL;
+int                    (*cdb_store)(int, const void *, int, void *, int)       = NULL;
+int                    (*cdb_delete)(int, void *, int)                         = NULL;
+struct cdbkeyval       (*cdb_next_item)(int)                                   = NULL;
+void                   (*cdb_close_cursor)(int)                                = NULL;
+void                   (*cdb_begin_transaction)(void)                          = NULL;
+void                   (*cdb_end_transaction)(void)                            = NULL;
+void                   (*cdb_check_handles)(void)                              = NULL;
+void                   (*cdb_trunc)(int)                                       = NULL;
+void                   (*check_handles)(void *)                                = NULL;
+void                   (*cdb_compact)(void)                                    = NULL;
+void                   (*cdb_checkpoint)(void)                                 = NULL;
+void                   (*cdb_rewind)(int)                                      = NULL;
+
+// This function is responsible for choosing and initializing a back end.
+void cdb_init_backends(void) {
+       bdb_init_backend();             // for now, this is the only one, so we select it always.
+}
+
+
+// Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
+void cdb_chmod_data(void) {
+       DIR *dp;
+       struct dirent *d;
+       char filename[PATH_MAX];
+
+       // Silently try to create the database subdirectory.  If it's already there, no problem.
+       if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) {
+               syslog(LOG_ERR, "bdb: database directory [%s] does not exist and could not be created: %m", ctdl_db_dir);
+               exit(CTDLEXIT_DB);
+       }
+       if (chmod(ctdl_db_dir, 0700) != 0) {
+               syslog(LOG_ERR, "bdb: unable to set database directory permissions [%s]: %m", ctdl_db_dir);
+               exit(CTDLEXIT_DB);
+       }
+       if (chown(ctdl_db_dir, ctdluid, (-1)) != 0) {
+               syslog(LOG_ERR, "bdb: unable to set the owner for [%s]: %m", ctdl_db_dir);
+               exit(CTDLEXIT_DB);
+       }
+       dp = opendir(ctdl_db_dir);
+       if (dp != NULL) {
+               while (d = readdir(dp), d != NULL) {
+                       if (d->d_name[0] != '.') {
+                               snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name);
+                               syslog(LOG_DEBUG, "bdb: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
+                               syslog(LOG_DEBUG, "bdb: chown(%s, ctdluid, -1) returned %d", filename, chown(filename, ctdluid, (-1)));
+                       }
+               }
+               closedir(dp);
+       }
+}
+
+