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.
24 #include <libcitadel.h>
26 #include "ctdl_module.h"
27 #include "housekeeping.h"
28 #include "locate_host.h"
29 #include "citserver.h"
34 char *unique_session_numbers;
35 int ScheduledShutdown = 0;
36 time_t server_startup_time;
38 int openid_level_supported = 0;
41 * print the actual stack frame.
43 void cit_backtrace(void)
46 void *stack_frames[50];
50 const char *p = IOSTR;
51 if (p == NULL) p = "";
52 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
53 strings = backtrace_symbols(stack_frames, size);
54 for (i = 0; i < size; i++) {
55 if (strings != NULL) {
56 syslog(LOG_DEBUG, "citserver: %s %s", p, strings[i]);
59 syslog(LOG_DEBUG, "citserver: %s %p", p, stack_frames[i]);
66 void cit_oneline_backtrace(void)
69 void *stack_frames[50];
74 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
75 strings = backtrace_symbols(stack_frames, size);
79 for (i = 1; i < size; i++) {
81 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
83 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
86 syslog(LOG_DEBUG, "citserver: %s %s", IOSTR, ChrPtr(Buf));
94 * print the actual stack frame.
96 void cit_panic_backtrace(int SigNum)
99 void *stack_frames[10];
103 printf("caught signal 11\n");
104 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
105 strings = backtrace_symbols(stack_frames, size);
106 for (i = 0; i < size; i++) {
107 if (strings != NULL) {
108 syslog(LOG_DEBUG, "%s", strings[i]);
111 syslog(LOG_DEBUG, "%p", stack_frames[i]);
120 * Various things that need to be initialized at startup
122 void master_startup(void) {
126 struct ctdlroom qrbuf;
131 syslog(LOG_DEBUG, "master_startup() started");
132 time(&server_startup_time);
134 syslog(LOG_INFO, "Checking directory access");
135 if ((pw = getpwuid(ctdluid)) == NULL) {
141 if (create_run_directories(CTDLUID, gid) != 0) {
142 syslog(LOG_EMERG, "failed to access & create directories");
145 syslog(LOG_INFO, "Opening databases");
148 /* Load site-specific configuration */
149 syslog(LOG_INFO, "Initializing configuration system");
150 initialize_config_system();
152 migrate_legacy_control_record();
154 /* Check floor reference counts */
157 syslog(LOG_INFO, "Creating base rooms (if necessary)");
158 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
159 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
160 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
161 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
163 /* The "Local System Configuration" room doesn't need to be visible */
164 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
165 qrbuf.QRflags2 |= QR2_SYSTEM;
166 CtdlPutRoomLock(&qrbuf);
169 /* Aide needs to be public postable, else we're not RFC conformant. */
170 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
171 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
172 CtdlPutRoomLock(&qrbuf);
175 syslog(LOG_INFO, "Seeding the pseudo-random number generator...");
176 urandom = fopen("/dev/urandom", "r");
177 if (urandom != NULL) {
178 rv = fread(&seed, sizeof seed, 1, urandom);
180 syslog(LOG_ERR, "citserver: failed to read random seed: %m");
185 gettimeofday(&tv, NULL);
191 syslog(LOG_DEBUG, "master_startup() finished");
196 * Cleanup routine to be called when the server is shutting down. Returns the needed exit code.
198 int master_cleanup(int exitcode) {
199 struct CleanupFunctionHook *fcn;
200 static int already_cleaning_up = 0;
202 if (already_cleaning_up) while(1) usleep(1000000);
203 already_cleaning_up = 1;
205 /* Run any cleanup routines registered by loadable modules */
206 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
207 (*fcn->h_function_pointer)();
210 /* Close the AdjRefCount queue file */
213 /* Do system-dependent stuff */
214 sysdep_master_cleanup();
216 /* Close the configuration system */
217 shutdown_config_system();
219 /* Close databases */
220 syslog(LOG_INFO, "Closing databases\n");
223 /* If the operator requested a halt but not an exit, halt here. */
224 if (shutdown_and_halt) {
225 syslog(LOG_ERR, "citserver: Halting server without exiting.");
226 fflush(stdout); fflush(stderr);
233 syslog(LOG_ERR, "citserver: Exiting with status %d", exitcode);
234 fflush(stdout); fflush(stderr);
236 if (restart_server != 0) {
239 else if ((running_as_daemon != 0) && ((exitcode == 0) )) {
240 exitcode = CTDLEXIT_SHUTDOWN;
248 * returns an asterisk if there are any instant messages waiting,
251 char CtdlCheckExpress(void) {
252 if (CC->FirstExpressMessage == NULL) {
262 * Check originating host against the public_clients file. This determines
263 * whether the client is allowed to change the hostname for this session
264 * (for example, to show the location of the user rather than the location
267 int CtdlIsPublicClient(void)
273 char *public_clientspos;
274 char *public_clientsend;
277 static time_t pc_timestamp = 0;
278 static char public_clients[SIZ];
279 static char public_clients_file[SIZ];
281 #define LOCALHOSTSTR "127.0.0.1"
283 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
286 * Check the time stamp on the public_clients file. If it's been
287 * updated since the last time we were here (or if this is the first
288 * time we've been through the loop), read its contents and learn
289 * the IP addresses of the listed hosts.
291 if (stat(public_clients_file, &statbuf) != 0) {
292 /* No public_clients file exists, so bail out */
293 syslog(LOG_WARNING, "Warning: '%s' does not exist", public_clients_file);
297 if (statbuf.st_mtime > pc_timestamp) {
298 begin_critical_section(S_PUBLIC_CLIENTS);
299 syslog(LOG_INFO, "Loading %s", public_clients_file);
301 public_clientspos = &public_clients[0];
302 public_clientsend = public_clientspos + SIZ;
303 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
304 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
306 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
307 *(public_clientspos++) = '|';
309 while (!IsEmptyStr (paddr) &&
310 (public_clientspos < public_clientsend))
311 *(public_clientspos++) = *(paddr++);
314 fp = fopen(public_clients_file, "r");
316 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
317 (public_clientspos < public_clientsend)){
320 while (!IsEmptyStr(ptr)) {
328 while (ptr>buf && isspace(*ptr)) {
331 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
332 *(public_clientspos++) = '|';
334 while (!IsEmptyStr(paddr) &&
335 (public_clientspos < public_clientsend)){
336 *(public_clientspos++) = *(paddr++);
340 if (fp != NULL) fclose(fp);
341 pc_timestamp = time(NULL);
342 end_critical_section(S_PUBLIC_CLIENTS);
345 syslog(LOG_DEBUG, "Checking whether %s is a local or public client", CC->cs_addr);
346 for (i=0; i<num_parms(public_clients); ++i) {
347 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
348 if (!strcasecmp(CC->cs_addr, addrbuf)) {
349 syslog(LOG_DEBUG, "... yes its local.");
354 /* No hits. This is not a public client. */
355 syslog(LOG_DEBUG, "... no it isn't.");
363 void citproto_begin_session() {
364 if (CC->nologin==1) {
365 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
366 ERROR + MAX_SESSIONS_EXCEEDED,
367 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
369 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
372 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
373 CC->can_receive_im = 1;
378 void citproto_begin_admin_session() {
379 CC->internal_pgm = 1;
380 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
385 * This loop performs all asynchronous functions.
387 void do_async_loop(void) {
388 PerformSessionHooks(EVT_ASYNC);