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