2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2017 by the citadel.org team
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.
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.
21 #include <libcitadel.h>
23 #include "ctdl_module.h"
24 #include "housekeeping.h"
25 #include "locate_host.h"
26 #include "citserver.h"
31 char *unique_session_numbers;
32 int ScheduledShutdown = 0;
33 time_t server_startup_time;
35 int openid_level_supported = 0;
39 * Various things that need to be initialized at startup
41 void master_startup(void)
46 struct ctdlroom qrbuf;
51 syslog(LOG_DEBUG, "master_startup() started");
52 time(&server_startup_time);
54 syslog(LOG_INFO, "Checking directory access");
55 if ((pw = getpwuid(ctdluid)) == NULL) {
61 if (create_run_directories(CTDLUID, gid) != 0) {
62 syslog(LOG_EMERG, "failed to access & create directories");
65 syslog(LOG_INFO, "Opening databases");
68 /* Load site-specific configuration */
69 syslog(LOG_INFO, "Initializing configuration system");
70 initialize_config_system();
72 migrate_legacy_control_record();
74 /* Check floor reference counts */
77 syslog(LOG_INFO, "Creating base rooms (if necessary)");
78 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
79 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
80 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
81 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
83 /* The "Local System Configuration" room doesn't need to be visible */
84 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
85 qrbuf.QRflags2 |= QR2_SYSTEM;
86 CtdlPutRoomLock(&qrbuf);
89 /* Aide needs to be public postable, else we're not RFC conformant. */
90 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
91 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
92 CtdlPutRoomLock(&qrbuf);
95 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
96 urandom = fopen("/dev/urandom", "r");
97 if (urandom != NULL) {
98 rv = fread(&seed, sizeof seed, 1, urandom);
100 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
104 gettimeofday(&tv, NULL);
110 syslog(LOG_DEBUG, "master_startup() finished");
115 * Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
117 int master_cleanup(int exitcode)
119 struct CleanupFunctionHook *fcn;
120 static int already_cleaning_up = 0;
122 if (already_cleaning_up)
125 already_cleaning_up = 1;
127 /* Run any cleanup routines registered by loadable modules */
128 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
129 (*fcn->h_function_pointer) ();
132 /* Close the AdjRefCount queue file */
135 /* Do system-dependent stuff */
136 sysdep_master_cleanup();
138 /* Close the configuration system */
139 shutdown_config_system();
141 /* Close databases */
142 syslog(LOG_INFO, "Closing databases\n");
145 /* If the operator requested a halt but not an exit, halt here. */
146 if (shutdown_and_halt) {
147 syslog(LOG_ERR, "citserver: Halting server without exiting.");
156 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
160 if (restart_server != 0) {
162 } else if ((running_as_daemon != 0) && ((exitcode == 0))) {
163 exitcode = CTDLEXIT_SHUTDOWN;
171 * returns an asterisk if there are any instant messages waiting,
174 char CtdlCheckExpress(void)
176 if (CC->FirstExpressMessage == NULL) {
185 * Check originating host against the public_clients file. This determines
186 * whether the client is allowed to change the hostname for this session
187 * (for example, to show the location of the user rather than the location
190 int CtdlIsPublicClient(void)
196 char *public_clientspos;
197 char *public_clientsend;
200 static time_t pc_timestamp = 0;
201 static char public_clients[SIZ];
202 static char public_clients_file[SIZ];
204 #define LOCALHOSTSTR "127.0.0.1"
206 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
209 * Check the time stamp on the public_clients file. If it's been
210 * updated since the last time we were here (or if this is the first
211 * time we've been through the loop), read its contents and learn
212 * the IP addresses of the listed hosts.
214 if (stat(public_clients_file, &statbuf) != 0) {
215 /* No public_clients file exists, so bail out */
216 syslog(LOG_WARNING, "Warning: '%s' does not exist", public_clients_file);
220 if (statbuf.st_mtime > pc_timestamp) {
221 begin_critical_section(S_PUBLIC_CLIENTS);
222 syslog(LOG_INFO, "Loading %s", public_clients_file);
224 public_clientspos = &public_clients[0];
225 public_clientsend = public_clientspos + SIZ;
226 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
227 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
229 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
230 *(public_clientspos++) = '|';
232 while (!IsEmptyStr(paddr) && (public_clientspos < public_clientsend))
233 *(public_clientspos++) = *(paddr++);
236 fp = fopen(public_clients_file, "r");
238 while ((fgets(buf, sizeof buf, fp) != NULL) && (public_clientspos < public_clientsend)) {
241 while (!IsEmptyStr(ptr)) {
249 while (ptr > buf && isspace(*ptr)) {
252 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
253 *(public_clientspos++) = '|';
255 while (!IsEmptyStr(paddr) && (public_clientspos < public_clientsend)) {
256 *(public_clientspos++) = *(paddr++);
262 pc_timestamp = time(NULL);
263 end_critical_section(S_PUBLIC_CLIENTS);
266 syslog(LOG_DEBUG, "Checking whether %s is a local or public client", CC->cs_addr);
267 for (i = 0; i < num_parms(public_clients); ++i) {
268 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
269 if (!strcasecmp(CC->cs_addr, addrbuf)) {
270 syslog(LOG_DEBUG, "... yes its local.");
275 /* No hits. This is not a public client. */
276 syslog(LOG_DEBUG, "... no it isn't.");
284 void citproto_begin_session()
286 if (CC->nologin == 1) {
287 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
288 ERROR + MAX_SESSIONS_EXCEEDED, CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
290 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
292 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
293 CC->can_receive_im = 1;
298 void citproto_begin_admin_session()
300 CC->internal_pgm = 1;
301 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
306 * This loop performs all asynchronous functions.
308 void do_async_loop(void)
310 PerformSessionHooks(EVT_ASYNC);