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.
7 // The program is distributed without any warranty, expressed or implied.
15 #include <libcitadel.h>
17 #include "ctdl_module.h"
18 #include "housekeeping.h"
19 #include "locate_host.h"
20 #include "citserver.h"
25 char *unique_session_numbers;
26 int ScheduledShutdown = 0;
27 time_t server_startup_time;
29 int openid_level_supported = 0;
32 // We need pseudo-random numbers for a few things. Seed generously.
33 void seed_random_number_generator(void) {
38 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
39 urandom = fopen("/dev/urandom", "r");
40 if (urandom != NULL) {
41 if (fread(&seed, sizeof seed, 1, urandom) == -1) {
42 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
47 gettimeofday(&tv, NULL);
55 // Various things that need to be initialized at startup
56 void master_startup(void) {
57 struct ctdlroom qrbuf;
61 syslog(LOG_DEBUG, "master_startup() started");
62 time(&server_startup_time);
64 syslog(LOG_INFO, "Checking directory access");
65 if ((pw = getpwuid(ctdluid)) == NULL) {
72 if (create_run_directories(CTDLUID, gid) != 0) {
73 syslog(LOG_ERR, "citserver: failed to access and create directories");
77 syslog(LOG_DEBUG, "citserver: ctdl_message_dir is %s", ctdl_message_dir);
78 syslog(LOG_DEBUG, "citserver: ctdl_file_dir is %s", ctdl_file_dir);
79 syslog(LOG_DEBUG, "citserver: ctdl_key_dir is %s", ctdl_key_dir);
80 syslog(LOG_DEBUG, "citserver: ctdl_run_dir is %s", ctdl_run_dir);
82 syslog(LOG_INFO, "Opening databases");
85 // Load site-specific configuration
86 seed_random_number_generator(); // must be done before config system
87 syslog(LOG_INFO, "Initializing configuration system");
88 initialize_config_system();
90 migrate_legacy_control_record();
92 // If we have an existing database that is older than version 928, reindex the user records.
93 // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
94 int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
95 if ( (existing_db > 0) && (existing_db < 928) ) {
96 ForEachUser(reindex_user_928, NULL);
99 // Check floor reference counts
102 syslog(LOG_INFO, "Creating base rooms (if necessary)");
103 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
104 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
105 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
106 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
108 // The "Local System Configuration" room doesn't need to be visible
109 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
110 qrbuf.QRflags2 |= QR2_SYSTEM;
111 CtdlPutRoomLock(&qrbuf);
114 // Aide needs to be public postable, else we're not RFC conformant.
115 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
116 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
117 CtdlPutRoomLock(&qrbuf);
120 syslog(LOG_DEBUG, "master_startup() finished");
124 // Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
125 int master_cleanup(int exitcode) {
126 static int already_cleaning_up = 0;
128 if (already_cleaning_up) {
133 already_cleaning_up = 1;
135 // Do system-dependent stuff
136 sysdep_master_cleanup();
138 // Close the configuration system
139 shutdown_config_system();
142 syslog(LOG_INFO, "citserver: closing databases");
145 // If the operator requested a halt but not an exit, halt here.
146 if (shutdown_and_halt) {
147 syslog(LOG_ERR, "citserver: Halting server without exiting.");
156 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
160 if (restart_server != 0) {
163 else if ((running_as_daemon != 0) && ((exitcode == 0))) {
164 exitcode = CTDLEXIT_SHUTDOWN;
170 // returns an asterisk if there are any instant messages waiting, space otherwise.
171 char CtdlCheckExpress(void) {
172 if (CC->FirstExpressMessage == NULL) {
181 void citproto_begin_session() {
182 if (CC->nologin == 1) {
183 cprintf("%d Too many users are already online (maximum is %d)\n",
184 ERROR + MAX_SESSIONS_EXCEEDED, CtdlGetConfigInt("c_maxsessions")
186 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
189 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
190 CC->can_receive_im = 1;
195 void citproto_begin_admin_session() {
196 CC->internal_pgm = 1;
197 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
201 // This loop performs all asynchronous functions.
202 void do_async_loop(void) {
203 PerformSessionHooks(EVT_ASYNC);