CtdlCheckExpress() now accepts a session context.
[citadel.git] / citadel / server / citserver.c
1 // Main source module for the Citadel server
2 //
3 // Copyright (c) 1987-2024 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 <sys/time.h>
15 #include <libcitadel.h>
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_init_backends();
82         cdb_open_databases();
83
84         // Load site-specific configuration
85         seed_random_number_generator();                                 // must be done before config system
86         syslog(LOG_INFO, "Initializing configuration system");
87         initialize_config_system();
88         validate_config();
89         migrate_legacy_control_record();
90
91         // If we have an existing database that is older than version 928, reindex the user records.
92         // Unfortunately we cannot do this in serv_upgrade.c because it needs to happen VERY early during startup.
93         int existing_db = CtdlGetConfigInt("MM_hosted_upgrade_level");
94         if ( (existing_db > 0) && (existing_db < 928) ) {
95                 ForEachUser(reindex_user_928, NULL);
96         }
97
98         // Check floor reference counts
99         check_ref_counts();
100
101         syslog(LOG_INFO, "Creating base rooms (if necessary)");
102         CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
103         CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
104         CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
105         CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
106
107         // The "Local System Configuration" room doesn't need to be visible
108         if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
109                 qrbuf.QRflags2 |= QR2_SYSTEM;
110                 CtdlPutRoomLock(&qrbuf);
111         }
112
113         // Aide needs to be public postable, else we're not RFC conformant.
114         if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
115                 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
116                 CtdlPutRoomLock(&qrbuf);
117         }
118
119         syslog(LOG_DEBUG, "master_startup() finished");
120 }
121
122
123 // Cleanup routine to be called when the server is shutting down.  Returns the needed exit code.
124 void master_cleanup(int exitcode) {
125         static int already_cleaning_up = 0;
126
127         if (already_cleaning_up) {
128                 while (1) {
129                         usleep(1000000);
130                 }
131         }
132         already_cleaning_up = 1;
133
134         // Close the sockets
135         context_cleanup();
136
137         // Close the configuration system
138         shutdown_config_system();
139
140         // Close databases
141         syslog(LOG_INFO, "citserver: closing databases");
142         cdb_close_databases();
143
144         // If the operator requested a halt but not an exit, halt here.
145         if (shutdown_and_halt) {
146                 syslog(LOG_ERR, "citserver: Halting server without exiting.");
147                 fflush(stdout);
148                 fflush(stderr);
149                 while (1) {
150                         sleep(32767);
151                 }
152         }
153
154         // Now go away.
155         syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
156         fflush(stdout);
157         fflush(stderr);
158
159         if ((running_as_daemon != 0) && ((exitcode == 0))) {
160                 exitcode = CTDLEXIT_SHUTDOWN;
161         }
162
163         ctdl_lockfile(0);
164         exit(exitcode);
165 }
166
167
168 // returns an asterisk if there are any instant messages waiting, space otherwise.
169 char CtdlCheckExpress(struct CitContext *con) {
170         if (con->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                 strcpy(CC->cs_clientname, "Citadel client protocol");
189                 CC->can_receive_im = 1;
190         }
191 }
192
193
194 void citproto_begin_admin_session() {
195         CC->internal_pgm = 1;
196         cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_fqdn"));
197 }
198
199
200 // This loop performs all asynchronous functions.
201 void do_async_loop(void) {
202         PerformSessionHooks(EVT_ASYNC);
203 }