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) {
45 struct ctdlroom qrbuf;
50 syslog(LOG_DEBUG, "master_startup() started");
51 time(&server_startup_time);
53 syslog(LOG_INFO, "Checking directory access");
54 if ((pw = getpwuid(ctdluid)) == NULL) {
60 if (create_run_directories(CTDLUID, gid) != 0) {
61 syslog(LOG_EMERG, "failed to access & create directories");
64 syslog(LOG_INFO, "Opening databases");
67 /* Load site-specific configuration */
68 syslog(LOG_INFO, "Initializing configuration system");
69 initialize_config_system();
71 migrate_legacy_control_record();
73 /* Check floor reference counts */
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);
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);
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);
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);
99 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) {
118 struct CleanupFunctionHook *fcn;
119 static int already_cleaning_up = 0;
121 if (already_cleaning_up) while(1) usleep(1000000);
122 already_cleaning_up = 1;
124 /* Run any cleanup routines registered by loadable modules */
125 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
126 (*fcn->h_function_pointer)();
129 /* Close the AdjRefCount queue file */
132 /* Do system-dependent stuff */
133 sysdep_master_cleanup();
135 /* Close the configuration system */
136 shutdown_config_system();
138 /* Close databases */
139 syslog(LOG_INFO, "Closing databases\n");
142 /* If the operator requested a halt but not an exit, halt here. */
143 if (shutdown_and_halt) {
144 syslog(LOG_ERR, "citserver: Halting server without exiting.");
145 fflush(stdout); fflush(stderr);
152 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
153 fflush(stdout); fflush(stderr);
155 if (restart_server != 0) {
158 else if ((running_as_daemon != 0) && ((exitcode == 0) )) {
159 exitcode = CTDLEXIT_SHUTDOWN;
167 * returns an asterisk if there are any instant messages waiting,
170 char CtdlCheckExpress(void) {
171 if (CC->FirstExpressMessage == NULL) {
181 * Check originating host against the public_clients file. This determines
182 * whether the client is allowed to change the hostname for this session
183 * (for example, to show the location of the user rather than the location
186 int CtdlIsPublicClient(void)
192 char *public_clientspos;
193 char *public_clientsend;
196 static time_t pc_timestamp = 0;
197 static char public_clients[SIZ];
198 static char public_clients_file[SIZ];
200 #define LOCALHOSTSTR "127.0.0.1"
202 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
205 * Check the time stamp on the public_clients file. If it's been
206 * updated since the last time we were here (or if this is the first
207 * time we've been through the loop), read its contents and learn
208 * the IP addresses of the listed hosts.
210 if (stat(public_clients_file, &statbuf) != 0) {
211 /* No public_clients file exists, so bail out */
212 syslog(LOG_WARNING, "Warning: '%s' does not exist", public_clients_file);
216 if (statbuf.st_mtime > pc_timestamp) {
217 begin_critical_section(S_PUBLIC_CLIENTS);
218 syslog(LOG_INFO, "Loading %s", public_clients_file);
220 public_clientspos = &public_clients[0];
221 public_clientsend = public_clientspos + SIZ;
222 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
223 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
225 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
226 *(public_clientspos++) = '|';
228 while (!IsEmptyStr (paddr) &&
229 (public_clientspos < public_clientsend))
230 *(public_clientspos++) = *(paddr++);
233 fp = fopen(public_clients_file, "r");
235 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
236 (public_clientspos < public_clientsend)){
239 while (!IsEmptyStr(ptr)) {
247 while (ptr>buf && isspace(*ptr)) {
250 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
251 *(public_clientspos++) = '|';
253 while (!IsEmptyStr(paddr) &&
254 (public_clientspos < public_clientsend)){
255 *(public_clientspos++) = *(paddr++);
259 if (fp != NULL) fclose(fp);
260 pc_timestamp = time(NULL);
261 end_critical_section(S_PUBLIC_CLIENTS);
264 syslog(LOG_DEBUG, "Checking whether %s is a local or public client", CC->cs_addr);
265 for (i=0; i<num_parms(public_clients); ++i) {
266 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
267 if (!strcasecmp(CC->cs_addr, addrbuf)) {
268 syslog(LOG_DEBUG, "... yes its local.");
273 /* No hits. This is not a public client. */
274 syslog(LOG_DEBUG, "... no it isn't.");
282 void citproto_begin_session() {
283 if (CC->nologin==1) {
284 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
285 ERROR + MAX_SESSIONS_EXCEEDED,
286 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
288 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
291 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
292 CC->can_receive_im = 1;
297 void citproto_begin_admin_session() {
298 CC->internal_pgm = 1;
299 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
304 * This loop performs all asynchronous functions.
306 void do_async_loop(void) {
307 PerformSessionHooks(EVT_ASYNC);