open_databases() and close_databases() are now cdb_open_databases() and cdb_close_dat...
[citadel.git] / citadel / server / citserver.c
1 // Main source module for the Citadel server
2 //
3 // Copyright (c) 1987-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <sys/stat.h>
12 #include "sysdep.h"
13 #include <time.h>
14 #include <libcitadel.h>
15
16 #include "ctdl_module.h"
17 #include "housekeeping.h"
18 #include "locate_host.h"
19 #include "citserver.h"
20 #include "user_ops.h"
21 #include "control.h"
22 #include "config.h"
23
24 char *unique_session_numbers;
25 int ScheduledShutdown = 0;
26 time_t server_startup_time;
27 int panic_fd;
28
29
30 // We need pseudo-random numbers for a few things.  Seed generously.
31 void seed_random_number_generator(void) {
32         FILE *urandom;
33         struct timeval tv;
34         unsigned int seed;
35
36         syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
37         urandom = fopen("/dev/urandom", "r");
38         if (urandom != NULL) {
39                 if (fread(&seed, sizeof seed, 1, urandom) == -1) {
40                         syslog(LOG_ERR, "citserver: failed to read random seed: %m");
41                 }
42                 fclose(urandom);
43         }
44         else {
45                 gettimeofday(&tv, NULL);
46                 seed = tv.tv_usec;
47         }
48         srand(seed);
49         srandom(seed);
50 }
51
52
53 // Various things that need to be initialized at startup
54 void master_startup(void) {
55         struct ctdlroom qrbuf;
56         struct passwd *pw;
57         gid_t gid;
58
59         syslog(LOG_DEBUG, "master_startup() started");
60         time(&server_startup_time);
61
62         syslog(LOG_INFO, "Checking directory access");
63         if ((pw = getpwuid(ctdluid)) == NULL) {
64                 gid = getgid();
65         }
66         else {
67                 gid = pw->pw_gid;
68         }
69
70         if (create_run_directories(CTDLUID, gid) != 0) {
71                 syslog(LOG_ERR, "citserver: failed to access and create directories");
72                 exit(1);
73         }
74
75         syslog(LOG_DEBUG, "citserver: ctdl_message_dir is %s", ctdl_message_dir);
76         syslog(LOG_DEBUG, "citserver: ctdl_file_dir is %s", ctdl_file_dir);
77         syslog(LOG_DEBUG, "citserver: ctdl_key_dir is %s", ctdl_key_dir);
78         syslog(LOG_DEBUG, "citserver: ctdl_run_dir is %s", ctdl_run_dir);
79
80         syslog(LOG_INFO, "Opening databases");
81         cdb_open_databases();
82
83         // Load site-specific configuration
84         seed_random_number_generator();                                 // must be done before config system
85         syslog(LOG_INFO, "Initializing configuration system");
86         initialize_config_system();
87         validate_config();
88         migrate_legacy_control_record();
89
90         // If we have an existing database that is older than version 928, reindex the user records.
91         // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
92         int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
93         if ( (existing_db > 0) && (existing_db < 928) ) {
94                 ForEachUser(reindex_user_928, NULL);
95         }
96
97         // Check floor reference counts
98         check_ref_counts();
99
100         syslog(LOG_INFO, "Creating base rooms (if necessary)");
101         CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
102         CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
103         CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
104         CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
105
106         // The "Local System Configuration" room doesn't need to be visible
107         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
108                 qrbuf.QRflags2 |= QR2_SYSTEM;
109                 CtdlPutRoomLock(&qrbuf);
110         }
111
112         // Aide needs to be public postable, else we're not RFC conformant.
113         if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
114                 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
115                 CtdlPutRoomLock(&qrbuf);
116         }
117
118         syslog(LOG_DEBUG, "master_startup() finished");
119 }
120
121
122 // Cleanup routine to be called when the server is shutting down.  Returns the needed exit code.
123 int master_cleanup(int exitcode) {
124         static int already_cleaning_up = 0;
125
126         if (already_cleaning_up) {
127                 while (1) {
128                         usleep(1000000);
129                 }
130         }
131         already_cleaning_up = 1;
132
133         // Do system-dependent stuff
134         sysdep_master_cleanup();
135
136         // Close the configuration system
137         shutdown_config_system();
138
139         // Close databases
140         syslog(LOG_INFO, "citserver: closing databases");
141         cdb_close_databases();
142
143         // If the operator requested a halt but not an exit, halt here.
144         if (shutdown_and_halt) {
145                 syslog(LOG_ERR, "citserver: Halting server without exiting.");
146                 fflush(stdout);
147                 fflush(stderr);
148                 while (1) {
149                         sleep(32767);
150                 }
151         }
152
153         // Now go away.
154         syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
155         fflush(stdout);
156         fflush(stderr);
157
158         if (restart_server != 0) {
159                 exitcode = 1;
160         }
161         else if ((running_as_daemon != 0) && ((exitcode == 0))) {
162                 exitcode = CTDLEXIT_SHUTDOWN;
163         }
164         return (exitcode);
165 }
166
167
168 // returns an asterisk if there are any instant messages waiting, space otherwise.
169 char CtdlCheckExpress(void) {
170         if (CC->FirstExpressMessage == NULL) {
171                 return (' ');
172         }
173         else {
174                 return ('*');
175         }
176 }
177
178
179 void citproto_begin_session() {
180         if (CC->nologin == 1) {
181                 cprintf("%d Too many users are already online (maximum is %d)\n",
182                         ERROR + MAX_SESSIONS_EXCEEDED, CtdlGetConfigInt("c_maxsessions")
183                 );
184                 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
185         }
186         else {
187                 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
188                 CC->can_receive_im = 1;
189         }
190 }
191
192
193 void citproto_begin_admin_session() {
194         CC->internal_pgm = 1;
195         cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
196 }
197
198
199 // This loop performs all asynchronous functions.
200 void do_async_loop(void) {
201         PerformSessionHooks(EVT_ASYNC);
202 }