1 // Main source module for the Citadel server
3 // Copyright (c) 1987-2022 by the citadel.org team
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.
14 #include <libcitadel.h>
16 #include "ctdl_module.h"
17 #include "housekeeping.h"
18 #include "locate_host.h"
19 #include "citserver.h"
24 char *unique_session_numbers;
25 int ScheduledShutdown = 0;
26 time_t server_startup_time;
30 // We need pseudo-random numbers for a few things. Seed generously.
31 void seed_random_number_generator(void) {
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");
45 gettimeofday(&tv, NULL);
53 // Various things that need to be initialized at startup
54 void master_startup(void) {
55 struct ctdlroom qrbuf;
59 syslog(LOG_DEBUG, "master_startup() started");
60 time(&server_startup_time);
62 syslog(LOG_INFO, "Checking directory access");
63 if ((pw = getpwuid(ctdluid)) == NULL) {
70 if (create_run_directories(CTDLUID, gid) != 0) {
71 syslog(LOG_ERR, "citserver: failed to access and create directories");
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);
80 syslog(LOG_INFO, "Opening databases");
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();
88 migrate_legacy_control_record();
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);
97 // Check floor reference counts
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);
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);
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);
118 syslog(LOG_DEBUG, "master_startup() finished");
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;
126 if (already_cleaning_up) {
131 already_cleaning_up = 1;
133 // Do system-dependent stuff
134 sysdep_master_cleanup();
136 // Close the configuration system
137 shutdown_config_system();
140 syslog(LOG_INFO, "citserver: closing databases");
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.");
154 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
158 if (restart_server != 0) {
161 else if ((running_as_daemon != 0) && ((exitcode == 0))) {
162 exitcode = CTDLEXIT_SHUTDOWN;
168 // returns an asterisk if there are any instant messages waiting, space otherwise.
169 char CtdlCheckExpress(void) {
170 if (CC->FirstExpressMessage == NULL) {
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")
184 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
187 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
188 CC->can_receive_im = 1;
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"));
199 // This loop performs all asynchronous functions.
200 void do_async_loop(void) {
201 PerformSessionHooks(EVT_ASYNC);