Makefile: restructured so that backends can be compiled into utilities
[citadel.git] / citadel / server / backends / common / database.c
1 // Copyright (c) 1987-2023 by the citadel.org team
2 // This program is open source software.  Use, duplication, or disclosure
3 // are subject to the terms of the GNU General Public License, version 3.
4
5 // The functions in this file handle the selection and activation of a storage backend for Citadel Server.
6 // Right now, it simply activates Berkeley DB because that's the only one we have.
7
8 #include "../../sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <dirent.h>
14 #include <libcitadel.h>
15 #include "../../ctdl_module.h"
16 #include "../../control.h"
17 #include "../../citserver.h"
18 #include "../../config.h"
19
20 // Header files for all available backends must be included here.
21 #include "../berkeley_db/berkeley_db.h"
22
23 // Backends must include implementations of all these functions, but with their own prefix instead of "cdb_".
24 // The initialization function of the selected backend will map them.
25 void                    (*cdb_open_databases)(void)                             = NULL;
26 void                    (*cdb_close_databases)(void)                            = NULL;
27 struct cdbdata          (*cdb_fetch)(int, const void *, int)                    = NULL;
28 int                     (*cdb_store)(int, const void *, int, void *, int)       = NULL;
29 int                     (*cdb_delete)(int, void *, int)                         = NULL;
30 struct cdbkeyval        (*cdb_next_item)(int)                                   = NULL;
31 void                    (*cdb_close_cursor)(int)                                = NULL;
32 void                    (*cdb_begin_transaction)(void)                          = NULL;
33 void                    (*cdb_end_transaction)(void)                            = NULL;
34 void                    (*cdb_check_handles)(void)                              = NULL;
35 void                    (*cdb_trunc)(int)                                       = NULL;
36 void                    (*check_handles)(void *)                                = NULL;
37 void                    (*cdb_compact)(void)                                    = NULL;
38 void                    (*cdb_checkpoint)(void)                                 = NULL;
39 void                    (*cdb_rewind)(int)                                      = NULL;
40
41 // This function is responsible for choosing and initializing a back end.
42 void cdb_init_backends(void) {
43         bdb_init_backend();             // for now, this is the only one, so we select it always.
44 }
45
46
47 // Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
48 void cdb_chmod_data(void) {
49         DIR *dp;
50         struct dirent *d;
51         char filename[PATH_MAX];
52
53         // Silently try to create the database subdirectory.  If it's already there, no problem.
54         if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) {
55                 syslog(LOG_ERR, "bdb: database directory [%s] does not exist and could not be created: %m", ctdl_db_dir);
56                 exit(CTDLEXIT_DB);
57         }
58         if (chmod(ctdl_db_dir, 0700) != 0) {
59                 syslog(LOG_ERR, "bdb: unable to set database directory permissions [%s]: %m", ctdl_db_dir);
60                 exit(CTDLEXIT_DB);
61         }
62         if (chown(ctdl_db_dir, ctdluid, (-1)) != 0) {
63                 syslog(LOG_ERR, "bdb: unable to set the owner for [%s]: %m", ctdl_db_dir);
64                 exit(CTDLEXIT_DB);
65         }
66         dp = opendir(ctdl_db_dir);
67         if (dp != NULL) {
68                 while (d = readdir(dp), d != NULL) {
69                         if (d->d_name[0] != '.') {
70                                 snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name);
71                                 syslog(LOG_DEBUG, "bdb: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
72                                 syslog(LOG_DEBUG, "bdb: chown(%s, ctdluid, -1) returned %d", filename, chown(filename, ctdluid, (-1)));
73                         }
74                 }
75                 closedir(dp);
76         }
77 }
78
79