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