2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2015 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;
123 syslog(LOG_DEBUG, "master_startup() started\n");
124 time(&server_startup_time);
126 syslog(LOG_INFO, "Checking directory access");
127 if ((pw = getpwuid(ctdluid)) == NULL) {
133 if (create_run_directories(CTDLUID, gid) != 0) {
134 syslog(LOG_EMERG, "failed to access & create directories");
137 syslog(LOG_INFO, "Opening databases");
140 /* Load site-specific configuration */
141 syslog(LOG_INFO, "Initializing configuration system");
142 initialize_config_system();
144 migrate_legacy_control_record();
146 /* Check floor reference counts */
149 syslog(LOG_INFO, "Creating base rooms (if necessary)\n");
150 CtdlCreateRoom(CtdlGetConfigStr("c_baseroom"), 0, "", 0, 1, 0, VIEW_BBS);
151 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
152 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
153 CtdlCreateRoom(CtdlGetConfigStr("c_twitroom"), 0, "", 0, 1, 0, VIEW_BBS);
155 /* The "Local System Configuration" room doesn't need to be visible */
156 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
157 qrbuf.QRflags2 |= QR2_SYSTEM;
158 CtdlPutRoomLock(&qrbuf);
161 /* Aide needs to be public postable, else we're not RFC conformant. */
162 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
163 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
164 CtdlPutRoomLock(&qrbuf);
167 syslog(LOG_INFO, "Seeding the pseudo-random number generator...\n");
168 urandom = fopen("/dev/urandom", "r");
169 if (urandom != NULL) {
170 rv = fread(&seed, sizeof seed, 1, urandom);
172 syslog(LOG_EMERG, "failed to read random seed: %s\n",
177 gettimeofday(&tv, NULL);
183 syslog(LOG_DEBUG, "master_startup() finished\n");
188 * Cleanup routine to be called when the server is shutting down.
190 void master_cleanup(int exitcode) {
191 struct CleanupFunctionHook *fcn;
192 static int already_cleaning_up = 0;
194 if (already_cleaning_up) while(1) usleep(1000000);
195 already_cleaning_up = 1;
197 /* Run any cleanup routines registered by loadable modules */
198 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
199 (*fcn->h_function_pointer)();
202 /* Close the AdjRefCount queue file */
205 /* Do system-dependent stuff */
206 sysdep_master_cleanup();
208 /* Close the configuration system */
209 shutdown_config_system();
211 /* Close databases */
212 syslog(LOG_INFO, "Closing databases\n");
215 /* If the operator requested a halt but not an exit, halt here. */
216 if (shutdown_and_halt) {
217 syslog(LOG_NOTICE, "citserver: Halting server without exiting.\n");
218 fflush(stdout); fflush(stderr);
225 syslog(LOG_NOTICE, "citserver: Exiting with status %d\n", exitcode);
226 fflush(stdout); fflush(stderr);
228 if (restart_server != 0)
230 if ((running_as_daemon != 0) && ((exitcode == 0) ))
231 exitcode = CTDLEXIT_SHUTDOWN;
238 * returns an asterisk if there are any instant messages waiting,
241 char CtdlCheckExpress(void) {
242 if (CC->FirstExpressMessage == NULL) {
252 * Check originating host against the public_clients file. This determines
253 * whether the client is allowed to change the hostname for this session
254 * (for example, to show the location of the user rather than the location
257 int CtdlIsPublicClient(void)
263 char *public_clientspos;
264 char *public_clientsend;
267 static time_t pc_timestamp = 0;
268 static char public_clients[SIZ];
269 static char public_clients_file[SIZ];
271 #define LOCALHOSTSTR "127.0.0.1"
273 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
276 * Check the time stamp on the public_clients file. If it's been
277 * updated since the last time we were here (or if this is the first
278 * time we've been through the loop), read its contents and learn
279 * the IP addresses of the listed hosts.
281 if (stat(public_clients_file, &statbuf) != 0) {
282 /* No public_clients file exists, so bail out */
283 syslog(LOG_WARNING, "Warning: '%s' does not exist\n",
284 public_clients_file);
288 if (statbuf.st_mtime > pc_timestamp) {
289 begin_critical_section(S_PUBLIC_CLIENTS);
290 syslog(LOG_INFO, "Loading %s\n", public_clients_file);
292 public_clientspos = &public_clients[0];
293 public_clientsend = public_clientspos + SIZ;
294 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
295 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
297 if (hostname_to_dotted_quad(addrbuf, CtdlGetConfigStr("c_fqdn")) == 0) {
298 *(public_clientspos++) = '|';
300 while (!IsEmptyStr (paddr) &&
301 (public_clientspos < public_clientsend))
302 *(public_clientspos++) = *(paddr++);
305 fp = fopen(public_clients_file, "r");
307 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
308 (public_clientspos < public_clientsend)){
311 while (!IsEmptyStr(ptr)) {
319 while (ptr>buf && isspace(*ptr)) {
322 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
323 *(public_clientspos++) = '|';
325 while (!IsEmptyStr(paddr) &&
326 (public_clientspos < public_clientsend)){
327 *(public_clientspos++) = *(paddr++);
331 if (fp != NULL) fclose(fp);
332 pc_timestamp = time(NULL);
333 end_critical_section(S_PUBLIC_CLIENTS);
336 syslog(LOG_DEBUG, "Checking whether %s is a local or public client\n",
338 for (i=0; i<num_parms(public_clients); ++i) {
339 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
340 if (!strcasecmp(CC->cs_addr, addrbuf)) {
341 syslog(LOG_DEBUG, "... yes its local.\n");
346 /* No hits. This is not a public client. */
347 syslog(LOG_DEBUG, "... no it isn't.\n");
355 void citproto_begin_session() {
356 if (CC->nologin==1) {
357 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
358 ERROR + MAX_SESSIONS_EXCEEDED,
359 CtdlGetConfigStr("c_nodename"), CtdlGetConfigInt("c_maxsessions")
361 CC->kill_me = KILLME_MAX_SESSIONS_EXCEEDED;
364 cprintf("%d %s Citadel server ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
365 CC->can_receive_im = 1;
370 void citproto_begin_admin_session() {
371 CC->internal_pgm = 1;
372 cprintf("%d %s Citadel server ADMIN CONNECTION ready.\n", CIT_OK, CtdlGetConfigStr("c_nodename"));
379 * This loop recognizes all server commands.
381 void do_command_loop(void) {
385 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
386 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
387 syslog(LOG_ERR, "Citadel client disconnected: ending session.\n");
388 CC->kill_me = KILLME_CLIENT_DISCONNECTED;
392 /* Log the server command, but don't show passwords... */
393 if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
394 syslog(LOG_INFO, "[%d][%s(%ld)] %s",
395 CC->cs_pid, CC->curr_user, CC->user.usernum, cmdbuf
399 syslog(LOG_INFO, "[%d][%s(%ld)] <password command hidden from log>",
400 CC->cs_pid, CC->curr_user, CC->user.usernum
407 * Let other clients see the last command we executed, and
408 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
410 if ( (strncasecmp(cmdbuf, "NOOP", 4))
411 && (strncasecmp(cmdbuf, "QNOP", 4))
412 && (strncasecmp(cmdbuf, "PEXP", 4))
413 && (strncasecmp(cmdbuf, "GEXP", 4))
414 && (strncasecmp(cmdbuf, "RWHO", 4))
415 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
416 strcpy(CC->lastcmdname, " ");
417 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
421 if ((strncasecmp(cmdbuf, "ENT0", 4))
422 && (strncasecmp(cmdbuf, "MESG", 4))
423 && (strncasecmp(cmdbuf, "MSGS", 4)))
425 CC->cs_flags &= ~CS_POSTING;
428 if (!DLoader_Exec_Cmd(cmdbuf)) {
429 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
434 /* Run any after-each-command routines registered by modules */
435 PerformSessionHooks(EVT_CMD);
440 * This loop performs all asynchronous functions.
442 void do_async_loop(void) {
443 PerformSessionHooks(EVT_ASYNC);