2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2014 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;
38 * print the actual stack frame.
40 void cit_backtrace(void)
43 void *stack_frames[50];
47 const char *p = IOSTR;
48 if (p == NULL) p = "";
49 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
50 strings = backtrace_symbols(stack_frames, size);
51 for (i = 0; i < size; i++) {
53 syslog(LOG_ALERT, "%s %s\n", p, strings[i]);
55 syslog(LOG_ALERT, "%s %p\n", p, stack_frames[i]);
61 void cit_oneline_backtrace(void)
64 void *stack_frames[50];
69 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
70 strings = backtrace_symbols(stack_frames, size);
74 for (i = 1; i < size; i++) {
76 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
78 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
81 syslog(LOG_ALERT, "%s %s\n", IOSTR, ChrPtr(Buf));
88 * print the actual stack frame.
90 void cit_panic_backtrace(int SigNum)
93 void *stack_frames[10];
97 printf("caught signal 11\n");
98 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
99 strings = backtrace_symbols(stack_frames, size);
100 for (i = 0; i < size; i++) {
102 syslog(LOG_ALERT, "%s\n", strings[i]);
104 syslog(LOG_ALERT, "%p\n", stack_frames[i]);
112 * Various things that need to be initialized at startup
114 void master_startup(void) {
118 struct ctdlroom qrbuf;
121 syslog(LOG_DEBUG, "master_startup() started\n");
122 time(&server_startup_time);
125 syslog(LOG_INFO, "Opening databases\n");
129 syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
130 CtdlCreateRoom(config.c_baseroom, 0, "", 0, 1, 0, VIEW_BBS);
131 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
132 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
133 CtdlCreateRoom(config.c_twitroom, 0, "", 0, 1, 0, VIEW_BBS);
135 /* The "Local System Configuration" room doesn't need to be visible */
136 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
137 qrbuf.QRflags2 |= QR2_SYSTEM;
138 CtdlPutRoomLock(&qrbuf);
141 /* Aide needs to be public postable, else we're not RFC conformant. */
142 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
143 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
144 CtdlPutRoomLock(&qrbuf);
147 syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
148 urandom = fopen("/dev/urandom", "r");
149 if (urandom != NULL) {
150 rv = fread(&seed, sizeof seed, 1, urandom);
152 syslog(LOG_EMERG, "failed to read random seed: %s\n",
157 gettimeofday(&tv, NULL);
165 syslog(LOG_DEBUG, "master_startup() finished\n");
170 * Cleanup routine to be called when the server is shutting down.
172 void master_cleanup(int exitcode) {
173 struct CleanupFunctionHook *fcn;
174 static int already_cleaning_up = 0;
176 if (already_cleaning_up) while(1) usleep(1000000);
177 already_cleaning_up = 1;
179 /* Run any cleanup routines registered by loadable modules */
180 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
181 (*fcn->h_function_pointer)();
184 /* Close the AdjRefCount queue file */
187 /* Do system-dependent stuff */
188 sysdep_master_cleanup();
190 /* Close databases */
191 syslog(LOG_INFO, "Closing databases\n");
194 /* If the operator requested a halt but not an exit, halt here. */
195 if (shutdown_and_halt) {
196 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
197 fflush(stdout); fflush(stderr);
206 syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
207 fflush(stdout); fflush(stderr);
209 if (restart_server != 0)
211 if ((running_as_daemon != 0) && ((exitcode == 0) ))
212 exitcode = CTDLEXIT_SHUTDOWN;
219 * returns an asterisk if there are any instant messages waiting,
222 char CtdlCheckExpress(void) {
223 if (CC->FirstExpressMessage == NULL) {
233 * Check originating host against the public_clients file. This determines
234 * whether the client is allowed to change the hostname for this session
235 * (for example, to show the location of the user rather than the location
238 int CtdlIsPublicClient(void)
244 char *public_clientspos;
245 char *public_clientsend;
248 static time_t pc_timestamp = 0;
249 static char public_clients[SIZ];
250 static char public_clients_file[SIZ];
252 #define LOCALHOSTSTR "127.0.0.1"
254 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
257 * Check the time stamp on the public_clients file. If it's been
258 * updated since the last time we were here (or if this is the first
259 * time we've been through the loop), read its contents and learn
260 * the IP addresses of the listed hosts.
262 if (stat(public_clients_file, &statbuf) != 0) {
263 /* No public_clients file exists, so bail out */
264 syslog(LOG_WARNING, "Warning: '%s' does not exist\n",
265 public_clients_file);
269 if (statbuf.st_mtime > pc_timestamp) {
270 begin_critical_section(S_PUBLIC_CLIENTS);
271 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
273 public_clientspos = &public_clients[0];
274 public_clientsend = public_clientspos + SIZ;
275 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
276 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
278 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
279 *(public_clientspos++) = '|';
281 while (!IsEmptyStr (paddr) &&
282 (public_clientspos < public_clientsend))
283 *(public_clientspos++) = *(paddr++);
286 fp = fopen(public_clients_file, "r");
288 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
289 (public_clientspos < public_clientsend)){
292 while (!IsEmptyStr(ptr)) {
300 while (ptr>buf && isspace(*ptr)) {
303 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
304 *(public_clientspos++) = '|';
306 while (!IsEmptyStr(paddr) &&
307 (public_clientspos < public_clientsend)){
308 *(public_clientspos++) = *(paddr++);
312 if (fp != NULL) fclose(fp);
313 pc_timestamp = time(NULL);
314 end_critical_section(S_PUBLIC_CLIENTS);
317 syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
319 for (i=0; i<num_parms(public_clients); ++i) {
320 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
321 if (!strcasecmp(CC->cs_addr, addrbuf)) {
322 syslog(LOG_DEBUG, "... yes its local.\n");
327 /* No hits. This is not a public client. */
328 syslog(LOG_DEBUG, "... no it isn't.\n");
336 void citproto_begin_session() {
337 if (CC->nologin==1) {
338 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
339 ERROR + MAX_SESSIONS_EXCEEDED,
340 config.c_nodename, config.c_maxsessions
342 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
345 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
346 CC->can_receive_im = 1;
351 void citproto_begin_admin_session() {
352 CC->internal_pgm = 1;
353 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
360 * This loop recognizes all server commands.
362 void do_command_loop(void) {
366 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
367 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
368 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
369 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
373 /* Log the server command, but don't show passwords... */
374 if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
375 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
376 CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
380 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
381 CC->cs_pid, CC->curr_user, CC->user.usernum
388 * Let other clients see the last command we executed, and
389 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
391 if ( (strncasecmp(cmdbuf, "NOOP", 4))
392 && (strncasecmp(cmdbuf, "QNOP", 4))
393 && (strncasecmp(cmdbuf, "PEXP", 4))
394 && (strncasecmp(cmdbuf, "GEXP", 4))
395 && (strncasecmp(cmdbuf, "RWHO", 4))
396 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
397 strcpy(CC->lastcmdname, " ");
398 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
402 if ((strncasecmp(cmdbuf, "ENT0", 4))
403 && (strncasecmp(cmdbuf, "MESG", 4))
404 && (strncasecmp(cmdbuf, "MSGS", 4)))
406 CC->cs_flags &= ~CS_POSTING;
409 if (!DLoader_Exec_Cmd(cmdbuf)) {
410 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
415 /* Run any after-each-command routines registered by modules */
416 PerformSessionHooks(EVT_CMD);
421 * This loop performs all asynchronous functions.
423 void do_async_loop(void) {
424 PerformSessionHooks(EVT_ASYNC);