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