2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2011 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.
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
31 #include <libcitadel.h>
33 #include "ctdl_module.h"
34 #include "housekeeping.h"
35 #include "locate_host.h"
36 #include "citserver.h"
41 char *unique_session_numbers;
42 int ScheduledShutdown = 0;
43 time_t server_startup_time;
45 int openid_level_supported = 0;
48 * print the actual stack frame.
50 void cit_backtrace(void)
53 void *stack_frames[50];
58 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
59 strings = backtrace_symbols(stack_frames, size);
60 for (i = 0; i < size; i++) {
62 syslog(LOG_ALERT, "%s\n", strings[i]);
64 syslog(LOG_ALERT, "%p\n", stack_frames[i]);
70 void cit_oneline_backtrace(void)
73 void *stack_frames[50];
78 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
79 strings = backtrace_symbols(stack_frames, size);
83 for (i = 1; i < size; i++) {
85 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
87 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
90 syslog(LOG_ALERT, "%s\n", ChrPtr(Buf));
97 * print the actual stack frame.
99 void cit_panic_backtrace(int SigNum)
101 #ifdef HAVE_BACKTRACE
102 void *stack_frames[10];
106 printf("caught signal 11\n");
107 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
108 strings = backtrace_symbols(stack_frames, size);
109 for (i = 0; i < size; i++) {
111 syslog(LOG_ALERT, "%s\n", strings[i]);
113 syslog(LOG_ALERT, "%p\n", stack_frames[i]);
121 * Various things that need to be initialized at startup
123 void master_startup(void) {
127 struct ctdlroom qrbuf;
130 syslog(LOG_DEBUG, "master_startup() started\n");
131 time(&server_startup_time);
134 syslog(LOG_INFO, "Opening databases\n");
138 syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
139 CtdlCreateRoom(config.c_baseroom, 0, "", 0, 1, 0, VIEW_BBS);
140 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
141 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
142 CtdlCreateRoom(config.c_twitroom, 0, "", 0, 1, 0, VIEW_BBS);
144 /* The "Local System Configuration" room doesn't need to be visible */
145 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
146 qrbuf.QRflags2 |= QR2_SYSTEM;
147 CtdlPutRoomLock(&qrbuf);
150 /* Aide needs to be public postable, else we're not RFC conformant. */
151 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
152 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
153 CtdlPutRoomLock(&qrbuf);
156 syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
157 urandom = fopen("/dev/urandom", "r");
158 if (urandom != NULL) {
159 rv = fread(&seed, sizeof seed, 1, urandom);
161 syslog(LOG_EMERG, "failed to read random seed: %s\n",
166 gettimeofday(&tv, NULL);
174 syslog(LOG_DEBUG, "master_startup() finished\n");
179 * Cleanup routine to be called when the server is shutting down.
181 void master_cleanup(int exitcode) {
182 struct CleanupFunctionHook *fcn;
183 static int already_cleaning_up = 0;
185 if (already_cleaning_up) while(1) usleep(1000000);
186 already_cleaning_up = 1;
188 /* Run any cleanup routines registered by loadable modules */
189 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
190 (*fcn->h_function_pointer)();
193 /* Close the AdjRefCount queue file */
196 /* Do system-dependent stuff */
197 sysdep_master_cleanup();
199 /* Close databases */
200 syslog(LOG_INFO, "Closing databases\n");
203 /* If the operator requested a halt but not an exit, halt here. */
204 if (shutdown_and_halt) {
205 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
206 fflush(stdout); fflush(stderr);
215 syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
216 fflush(stdout); fflush(stderr);
218 if (restart_server != 0)
220 if ((running_as_daemon != 0) && ((exitcode == 0) ))
221 exitcode = CTDLEXIT_SHUTDOWN;
228 * returns an asterisk if there are any instant messages waiting,
231 char CtdlCheckExpress(void) {
232 if (CC->FirstExpressMessage == NULL) {
242 * Check originating host against the public_clients file. This determines
243 * whether the client is allowed to change the hostname for this session
244 * (for example, to show the location of the user rather than the location
247 int CtdlIsPublicClient(void)
253 char *public_clientspos;
254 char *public_clientsend;
257 static time_t pc_timestamp = 0;
258 static char public_clients[SIZ];
259 static char public_clients_file[SIZ];
261 #define LOCALHOSTSTR "127.0.0.1"
263 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
266 * Check the time stamp on the public_clients file. If it's been
267 * updated since the last time we were here (or if this is the first
268 * time we've been through the loop), read its contents and learn
269 * the IP addresses of the listed hosts.
271 if (stat(public_clients_file, &statbuf) != 0) {
272 /* No public_clients file exists, so bail out */
273 syslog(LOG_WARNING, "Warning: '%s' does not exist\n",
274 public_clients_file);
278 if (statbuf.st_mtime > pc_timestamp) {
279 begin_critical_section(S_PUBLIC_CLIENTS);
280 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
282 public_clientspos = &public_clients[0];
283 public_clientsend = public_clientspos + SIZ;
284 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
285 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
287 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
288 *(public_clientspos++) = '|';
290 while (!IsEmptyStr (paddr) &&
291 (public_clientspos < public_clientsend))
292 *(public_clientspos++) = *(paddr++);
295 fp = fopen(public_clients_file, "r");
297 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
298 (public_clientspos < public_clientsend)){
301 while (!IsEmptyStr(ptr)) {
309 while (ptr>buf && isspace(*ptr)) {
312 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
313 *(public_clientspos++) = '|';
315 while (!IsEmptyStr(paddr) &&
316 (public_clientspos < public_clientsend)){
317 *(public_clientspos++) = *(paddr++);
321 if (fp != NULL) fclose(fp);
322 pc_timestamp = time(NULL);
323 end_critical_section(S_PUBLIC_CLIENTS);
326 syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
328 for (i=0; i<num_parms(public_clients); ++i) {
329 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
330 if (!strcasecmp(CC->cs_addr, addrbuf)) {
331 syslog(LOG_DEBUG, "... yes its local.\n");
336 /* No hits. This is not a public client. */
337 syslog(LOG_DEBUG, "... no it isn't.\n");
345 void citproto_begin_session() {
346 if (CC->nologin==1) {
347 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
348 ERROR + MAX_SESSIONS_EXCEEDED,
349 config.c_nodename, config.c_maxsessions
351 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
354 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
355 CC->can_receive_im = 1;
360 void citproto_begin_admin_session() {
361 CC->internal_pgm = 1;
362 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
369 * This loop recognizes all server commands.
371 void do_command_loop(void) {
375 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
376 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
377 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
378 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
382 /* Log the server command, but don't show passwords... */
383 if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
384 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
385 CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
389 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
390 CC->cs_pid, CC->curr_user, CC->user.usernum
397 * Let other clients see the last command we executed, and
398 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
400 if ( (strncasecmp(cmdbuf, "NOOP", 4))
401 && (strncasecmp(cmdbuf, "QNOP", 4))
402 && (strncasecmp(cmdbuf, "PEXP", 4))
403 && (strncasecmp(cmdbuf, "GEXP", 4))
404 && (strncasecmp(cmdbuf, "RWHO", 4))
405 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
406 strcpy(CC->lastcmdname, " ");
407 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
411 if ((strncasecmp(cmdbuf, "ENT0", 4))
412 && (strncasecmp(cmdbuf, "MESG", 4))
413 && (strncasecmp(cmdbuf, "MSGS", 4)))
415 CC->cs_flags &= ~CS_POSTING;
418 if (!DLoader_Exec_Cmd(cmdbuf)) {
419 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
424 /* Run any after-each-command routines registered by modules */
425 PerformSessionHooks(EVT_CMD);
430 * This loop performs all asynchronous functions.
432 void do_async_loop(void) {
433 PerformSessionHooks(EVT_ASYNC);