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.
21 #include <sys/types.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <libcitadel.h>
51 #include "sysdep_decls.h"
53 #include "citserver.h"
56 #include "housekeeping.h"
60 #include "locate_host.h"
63 #include "euidindex.h"
65 #include "svn_revision.h"
66 #include "ctdl_module.h"
68 char *unique_session_numbers;
69 int ScheduledShutdown = 0;
70 time_t server_startup_time;
72 int openid_level_supported = 0;
75 * print the actual stack frame.
77 void cit_backtrace(void)
80 void *stack_frames[50];
85 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
86 strings = backtrace_symbols(stack_frames, size);
87 for (i = 0; i < size; i++) {
89 syslog(LOG_ALERT, "%s\n", strings[i]);
91 syslog(LOG_ALERT, "%p\n", stack_frames[i]);
97 void cit_oneline_backtrace(void)
100 void *stack_frames[50];
105 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
106 strings = backtrace_symbols(stack_frames, size);
110 for (i = 1; i < size; i++) {
112 StrBufAppendPrintf(Buf, "%s : ", strings[i]);
114 StrBufAppendPrintf(Buf, "%p : ", stack_frames[i]);
117 syslog(LOG_ALERT, "%s\n", ChrPtr(Buf));
124 * print the actual stack frame.
126 void cit_panic_backtrace(int SigNum)
128 #ifdef HAVE_BACKTRACE
129 void *stack_frames[10];
133 printf("caught signal 11\n");
134 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
135 strings = backtrace_symbols(stack_frames, size);
136 for (i = 0; i < size; i++) {
138 syslog(LOG_ALERT, "%s\n", strings[i]);
140 syslog(LOG_ALERT, "%p\n", stack_frames[i]);
148 * Various things that need to be initialized at startup
150 void master_startup(void) {
154 struct ctdlroom qrbuf;
157 syslog(LOG_DEBUG, "master_startup() started\n");
158 time(&server_startup_time);
161 syslog(LOG_INFO, "Opening databases\n");
165 syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
166 CtdlCreateRoom(config.c_baseroom, 0, "", 0, 1, 0, VIEW_BBS);
167 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
168 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
169 CtdlCreateRoom(config.c_twitroom, 0, "", 0, 1, 0, VIEW_BBS);
171 /* The "Local System Configuration" room doesn't need to be visible */
172 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
173 qrbuf.QRflags2 |= QR2_SYSTEM;
174 CtdlPutRoomLock(&qrbuf);
177 /* Aide needs to be public postable, else we're not RFC conformant. */
178 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
179 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
180 CtdlPutRoomLock(&qrbuf);
183 syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
184 urandom = fopen("/dev/urandom", "r");
185 if (urandom != NULL) {
186 rv = fread(&seed, sizeof seed, 1, urandom);
188 syslog(LOG_EMERG, "failed to read random seed: %s\n",
193 gettimeofday(&tv, NULL);
201 syslog(LOG_DEBUG, "master_startup() finished\n");
206 * Cleanup routine to be called when the server is shutting down.
208 void master_cleanup(int exitcode) {
209 struct CleanupFunctionHook *fcn;
210 static int already_cleaning_up = 0;
212 if (already_cleaning_up) while(1) usleep(1000000);
213 already_cleaning_up = 1;
215 /* Run any cleanup routines registered by loadable modules */
216 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
217 (*fcn->h_function_pointer)();
220 /* Close the AdjRefCount queue file */
223 /* Do system-dependent stuff */
224 sysdep_master_cleanup();
226 /* Close databases */
227 syslog(LOG_INFO, "Closing databases\n");
230 /* If the operator requested a halt but not an exit, halt here. */
231 if (shutdown_and_halt) {
232 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
233 fflush(stdout); fflush(stderr);
242 syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
243 fflush(stdout); fflush(stderr);
245 if (restart_server != 0)
247 if ((running_as_daemon != 0) && ((exitcode == 0) ))
248 exitcode = CTDLEXIT_SHUTDOWN;
255 * returns an asterisk if there are any instant messages waiting,
258 char CtdlCheckExpress(void) {
259 if (CC->FirstExpressMessage == NULL) {
269 * Check originating host against the public_clients file. This determines
270 * whether the client is allowed to change the hostname for this session
271 * (for example, to show the location of the user rather than the location
274 int CtdlIsPublicClient(void)
280 char *public_clientspos;
281 char *public_clientsend;
284 static time_t pc_timestamp = 0;
285 static char public_clients[SIZ];
286 static char public_clients_file[SIZ];
288 #define LOCALHOSTSTR "127.0.0.1"
290 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
293 * Check the time stamp on the public_clients file. If it's been
294 * updated since the last time we were here (or if this is the first
295 * time we've been through the loop), read its contents and learn
296 * the IP addresses of the listed hosts.
298 if (stat(public_clients_file, &statbuf) != 0) {
299 /* No public_clients file exists, so bail out */
300 syslog(LOG_WARNING, "Warning: '%s' does not exist\n",
301 public_clients_file);
305 if (statbuf.st_mtime > pc_timestamp) {
306 begin_critical_section(S_PUBLIC_CLIENTS);
307 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
309 public_clientspos = &public_clients[0];
310 public_clientsend = public_clientspos + SIZ;
311 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
312 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
314 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
315 *(public_clientspos++) = '|';
317 while (!IsEmptyStr (paddr) &&
318 (public_clientspos < public_clientsend))
319 *(public_clientspos++) = *(paddr++);
322 fp = fopen(public_clients_file, "r");
324 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
325 (public_clientspos < public_clientsend)){
328 while (!IsEmptyStr(ptr)) {
336 while (ptr>buf && isspace(*ptr)) {
339 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
340 *(public_clientspos++) = '|';
342 while (!IsEmptyStr(paddr) &&
343 (public_clientspos < public_clientsend)){
344 *(public_clientspos++) = *(paddr++);
348 if (fp != NULL) fclose(fp);
349 pc_timestamp = time(NULL);
350 end_critical_section(S_PUBLIC_CLIENTS);
353 syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
355 for (i=0; i<num_parms(public_clients); ++i) {
356 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
357 if (!strcasecmp(CC->cs_addr, addrbuf)) {
358 syslog(LOG_DEBUG, "... yes its local.\n");
363 /* No hits. This is not a public client. */
364 syslog(LOG_DEBUG, "... no it isn't.\n");
372 void citproto_begin_session() {
373 if (CC->nologin==1) {
374 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
375 ERROR + MAX_SESSIONS_EXCEEDED,
376 config.c_nodename, config.c_maxsessions
378 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
381 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
382 CC->can_receive_im = 1;
387 void citproto_begin_admin_session() {
388 CC->internal_pgm = 1;
389 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, config.c_nodename);
396 * This loop recognizes all server commands.
398 void do_command_loop(void) {
402 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
403 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
404 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
405 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
409 /* Log the server command, but don't show passwords... */
410 if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
411 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
412 CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
416 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
417 CC->cs_pid, CC->curr_user, CC->user.usernum
424 * Let other clients see the last command we executed, and
425 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
427 if ( (strncasecmp(cmdbuf, "NOOP", 4))
428 && (strncasecmp(cmdbuf, "QNOP", 4))
429 && (strncasecmp(cmdbuf, "PEXP", 4))
430 && (strncasecmp(cmdbuf, "GEXP", 4))
431 && (strncasecmp(cmdbuf, "RWHO", 4))
432 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
433 strcpy(CC->lastcmdname, " ");
434 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
438 if ((strncasecmp(cmdbuf, "ENT0", 4))
439 && (strncasecmp(cmdbuf, "MESG", 4))
440 && (strncasecmp(cmdbuf, "MSGS", 4)))
442 CC->cs_flags &= ~CS_POSTING;
445 if (!DLoader_Exec_Cmd(cmdbuf)) {
446 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
451 /* Run any after-each-command routines registered by modules */
452 PerformSessionHooks(EVT_CMD);
457 * This loop performs all asynchronous functions.
459 void do_async_loop(void) {
460 PerformSessionHooks(EVT_ASYNC);