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