2 * Main source module for the Citadel server
4 * Copyright (c) 1987-2010 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <libcitadel.h>
58 #include "sysdep_decls.h"
60 #include "citserver.h"
63 #include "housekeeping.h"
67 #include "locate_host.h"
71 #include "euidindex.h"
73 #include "svn_revision.h"
79 #include "ctdl_module.h"
81 char *unique_session_numbers;
82 int ScheduledShutdown = 0;
83 time_t server_startup_time;
87 * print the actual stack frame.
89 void cit_backtrace(void)
92 void *stack_frames[50];
97 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
98 strings = backtrace_symbols(stack_frames, size);
99 for (i = 0; i < size; i++) {
101 CtdlLogPrintf(1, "%s\n", strings[i]);
103 CtdlLogPrintf(1, "%p\n", stack_frames[i]);
110 * print the actual stack frame.
112 void cit_panic_backtrace(int SigNum)
114 #ifdef HAVE_BACKTRACE
115 void *stack_frames[10];
119 printf("caught signal 11\n");
120 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
121 strings = backtrace_symbols(stack_frames, size);
122 for (i = 0; i < size; i++) {
124 CtdlLogPrintf(1, "%s\n", strings[i]);
126 CtdlLogPrintf(1, "%p\n", stack_frames[i]);
134 * Various things that need to be initialized at startup
136 void master_startup(void) {
140 struct ctdlroom qrbuf;
143 CtdlLogPrintf(CTDL_DEBUG, "master_startup() started\n");
144 time(&server_startup_time);
146 CtdlLogPrintf(CTDL_INFO, "Opening databases\n");
149 ctdl_thread_internal_init_tsd();
151 CtdlThreadAllocTSD();
155 CtdlLogPrintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
156 CtdlCreateRoom(config.c_baseroom, 0, "", 0, 1, 0, VIEW_BBS);
157 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
158 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
159 CtdlCreateRoom(config.c_twitroom, 0, "", 0, 1, 0, VIEW_BBS);
161 /* The "Local System Configuration" room doesn't need to be visible */
162 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
163 qrbuf.QRflags2 |= QR2_SYSTEM;
164 CtdlPutRoomLock(&qrbuf);
167 /* Aide needs to be public postable, else we're not RFC conformant. */
168 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
169 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
170 CtdlPutRoomLock(&qrbuf);
173 CtdlLogPrintf(CTDL_INFO, "Seeding the pseudo-random number generator...\n");
174 urandom = fopen("/dev/urandom", "r");
175 if (urandom != NULL) {
176 rv = fread(&seed, sizeof seed, 1, urandom);
180 gettimeofday(&tv, NULL);
186 CtdlLogPrintf(CTDL_INFO, "Initializing ipgm secret\n");
188 config.c_ipgm_secret = rand();
191 CtdlLogPrintf(CTDL_DEBUG, "master_startup() finished\n");
196 * Cleanup routine to be called when the server is shutting down.
198 void master_cleanup(int exitcode) {
199 struct CleanupFunctionHook *fcn;
200 static int already_cleaning_up = 0;
202 if (already_cleaning_up) while(1) sleep(1);
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 databases */
217 CtdlLogPrintf(CTDL_INFO, "Closing databases\n");
220 #ifdef DEBUG_MEMORY_LEAKS
224 /* If the operator requested a halt but not an exit, halt here. */
225 if (shutdown_and_halt) {
226 CtdlLogPrintf(CTDL_NOTICE, "citserver: Halting server without exiting.\n");
227 fflush(stdout); fflush(stderr);
236 CtdlLogPrintf(CTDL_NOTICE, "citserver: Exiting with status %d\n", exitcode);
237 fflush(stdout); fflush(stderr);
239 if (restart_server != 0)
241 if ((running_as_daemon != 0) && ((exitcode == 0) ))
242 exitcode = CTDLEXIT_SHUTDOWN;
249 * cmd_info() - tell the client about this server
251 void cmd_info(char *cmdbuf) {
252 cprintf("%d Server info:\n", LISTING_FOLLOWS);
253 cprintf("%d\n", CC->cs_pid);
254 cprintf("%s\n", config.c_nodename);
255 cprintf("%s\n", config.c_humannode);
256 cprintf("%s\n", config.c_fqdn);
257 cprintf("%s\n", CITADEL);
258 cprintf("%d\n", REV_LEVEL);
259 cprintf("%s\n", config.c_site_location);
260 cprintf("%s\n", config.c_sysadm);
261 cprintf("%d\n", SERVER_TYPE);
262 cprintf("%s\n", config.c_moreprompt);
263 cprintf("1\n"); /* 1 = yes, this system supports floors */
264 cprintf("1\n"); /* 1 = we support the extended paging options */
265 cprintf("%s\n", CC->cs_nonce);
266 cprintf("1\n"); /* 1 = yes, this system supports the QNOP command */
269 cprintf("1\n"); /* 1 = yes, this server is LDAP-enabled */
271 cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
274 if (config.c_auth_mode == AUTHMODE_NATIVE) {
275 cprintf("%d\n", config.c_disable_newu);
278 cprintf("1\n"); /* "create new user" does not work with non-native auth modes */
281 cprintf("%s\n", config.c_default_cal_zone);
283 /* Output load averages */
284 cprintf("%f\n", CtdlThreadLoadAvg);
285 cprintf("%f\n", CtdlThreadWorkerAvg);
286 cprintf("%d\n", CtdlThreadGetCount());
288 cprintf("1\n"); /* yes, Sieve mail filtering is supported */
289 cprintf("%d\n", config.c_enable_fulltext);
290 cprintf("%s\n", svn_revision());
292 if (config.c_auth_mode == AUTHMODE_NATIVE) {
293 cprintf("1\n"); /* OpenID is enabled when using native auth */
296 cprintf("0\n"); /* OpenID is disabled when using non-native auth */
299 cprintf("%d\n", config.c_guest_logins);
306 * returns an asterisk if there are any instant messages waiting,
309 char CtdlCheckExpress(void) {
310 if (CC->FirstExpressMessage == NULL) {
318 void cmd_time(char *argbuf)
324 localtime_r(&tv, &tmp);
326 /* timezone and daylight global variables are not portable. */
327 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
328 cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
330 cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
336 * Check originating host against the public_clients file. This determines
337 * whether the client is allowed to change the hostname for this session
338 * (for example, to show the location of the user rather than the location
341 int is_public_client(void)
347 char *public_clientspos;
348 char *public_clientsend;
351 static time_t pc_timestamp = 0;
352 static char public_clients[SIZ];
353 static char public_clients_file[SIZ];
355 #define LOCALHOSTSTR "127.0.0.1"
357 snprintf(public_clients_file, sizeof public_clients_file, "%s/public_clients", ctdl_etc_dir);
360 * Check the time stamp on the public_clients file. If it's been
361 * updated since the last time we were here (or if this is the first
362 * time we've been through the loop), read its contents and learn
363 * the IP addresses of the listed hosts.
365 if (stat(public_clients_file, &statbuf) != 0) {
366 /* No public_clients file exists, so bail out */
367 CtdlLogPrintf(CTDL_WARNING, "Warning: '%s' does not exist\n",
368 public_clients_file);
372 if (statbuf.st_mtime > pc_timestamp) {
373 begin_critical_section(S_PUBLIC_CLIENTS);
374 CtdlLogPrintf(CTDL_INFO, "Loading %s\n", public_clients_file);
376 public_clientspos = &public_clients[0];
377 public_clientsend = public_clientspos + SIZ;
378 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
379 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
381 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
382 *(public_clientspos++) = '|';
384 while (!IsEmptyStr (paddr) &&
385 (public_clientspos < public_clientsend))
386 *(public_clientspos++) = *(paddr++);
389 fp = fopen(public_clients_file, "r");
391 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
392 (public_clientspos < public_clientsend)){
395 while (!IsEmptyStr(ptr)) {
403 while (ptr>buf && isspace(*ptr)) {
406 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
407 *(public_clientspos++) = '|';
409 while (!IsEmptyStr(paddr) &&
410 (public_clientspos < public_clientsend)){
411 *(public_clientspos++) = *(paddr++);
416 pc_timestamp = time(NULL);
417 end_critical_section(S_PUBLIC_CLIENTS);
420 CtdlLogPrintf(CTDL_DEBUG, "Checking whether %s is a local or public client\n",
422 for (i=0; i<num_parms(public_clients); ++i) {
423 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
424 if (!strcasecmp(CC->cs_addr, addrbuf)) {
425 CtdlLogPrintf(CTDL_DEBUG, "... yes it is.\n");
430 /* No hits. This is not a public client. */
431 CtdlLogPrintf(CTDL_DEBUG, "... no it isn't.\n");
437 * the client is identifying itself to the server
439 void cmd_iden(char *argbuf)
447 if (num_parms(argbuf)<4) {
448 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
452 dev_code = extract_int(argbuf,0);
453 cli_code = extract_int(argbuf,1);
454 rev_level = extract_int(argbuf,2);
455 extract_token(desc, argbuf, 3, '|', sizeof desc);
457 safestrncpy(from_host, config.c_fqdn, sizeof from_host);
458 from_host[sizeof from_host - 1] = 0;
459 if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
461 CC->cs_clientdev = dev_code;
462 CC->cs_clienttyp = cli_code;
463 CC->cs_clientver = rev_level;
464 safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
465 CC->cs_clientname[31] = 0;
467 /* For local sockets and public clients, trust the hostname supplied by the client */
468 if ( (CC->is_local_socket) || (is_public_client()) ) {
469 safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
470 CC->cs_host[sizeof CC->cs_host - 1] = 0;
474 CtdlLogPrintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
482 cprintf("%d Ok\n",CIT_OK);
487 * display system messages or help
489 void cmd_mesg(char *mname)
499 extract_token(buf, mname, 0, '|', sizeof buf);
501 dirs[0] = strdup(ctdl_message_dir);
502 dirs[1] = strdup(ctdl_hlp_dir);
504 snprintf(buf2, sizeof buf2, "%s.%d.%d",
505 buf, CC->cs_clientdev, CC->cs_clienttyp);
507 /* If the client requested "?" then produce a listing */
508 if (!strcmp(buf, "?")) {
509 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
510 dp = opendir(dirs[1]);
512 while (d = readdir(dp), d != NULL) {
513 if (d->d_name[0] != '.') {
514 cprintf(" %s\n", d->d_name);
525 /* Otherwise, look for the requested file by name. */
527 mesg_locate(targ, sizeof targ, buf2, 2, (const char **)dirs);
528 if (IsEmptyStr(targ)) {
529 snprintf(buf2, sizeof buf2, "%s.%d",
530 buf, CC->cs_clientdev);
531 mesg_locate(targ, sizeof targ, buf2, 2,
532 (const char **)dirs);
533 if (IsEmptyStr(targ)) {
534 mesg_locate(targ, sizeof targ, buf, 2,
535 (const char **)dirs);
543 if (IsEmptyStr(targ)) {
544 cprintf("%d '%s' not found. (Searching in %s and %s)\n",
545 ERROR + FILE_NOT_FOUND,
553 mfp = fopen(targ, "r");
555 cprintf("%d Cannot open '%s': %s\n",
556 ERROR + INTERNAL_ERROR, targ, strerror(errno));
559 cprintf("%d %s\n", LISTING_FOLLOWS,buf);
561 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
562 buf[strlen(buf)-1] = 0;
573 * enter system messages or help
575 void cmd_emsg(char *mname)
585 if (CtdlAccessCheck(ac_aide)) return;
587 extract_token(buf, mname, 0, '|', sizeof buf);
588 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
589 if (buf[a] == '/') buf[a] = '.';
592 dirs[0] = strdup(ctdl_message_dir);
593 dirs[1] = strdup(ctdl_hlp_dir);
595 mesg_locate(targ, sizeof targ, buf, 2, (const char**)dirs);
599 if (IsEmptyStr(targ)) {
600 snprintf(targ, sizeof targ,
605 mfp = fopen(targ,"w");
607 cprintf("%d Cannot open '%s': %s\n",
608 ERROR + INTERNAL_ERROR, targ, strerror(errno));
611 cprintf("%d %s\n", SEND_LISTING, targ);
613 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
614 fprintf(mfp, "%s\n", buf);
621 /* Don't show the names of private rooms unless the viewing
622 * user also knows the rooms.
624 void GenerateRoomDisplay(char *real_room,
626 CitContext *viewer) {
630 strcpy(real_room, viewed->room.QRname);
631 if (viewed->room.QRflags & QR_MAILBOX) {
632 strcpy(real_room, &real_room[11]);
634 if (viewed->room.QRflags & QR_PRIVATE) {
635 CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
636 if ( (ra & UA_KNOWN) == 0) {
637 strcpy(real_room, "<private room>");
641 if (viewed->cs_flags & CS_CHAT) {
642 while (strlen(real_room) < 14) {
643 strcat(real_room, " ");
645 strcpy(&real_room[14], "<chat>");
651 * Convenience function.
653 int CtdlAccessCheck(int required_level) {
655 if (CC->internal_pgm) return(0);
656 if (required_level >= ac_internal) {
657 cprintf("%d This is not a user-level command.\n",
658 ERROR + HIGHER_ACCESS_REQUIRED);
662 if ((required_level >= ac_logged_in_or_guest) && (CC->logged_in == 0) && (!config.c_guest_logins)) {
663 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
667 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
668 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
672 if (CC->user.axlevel >= AxAideU) return(0);
673 if (required_level >= ac_aide) {
674 cprintf("%d This command requires Aide access.\n",
675 ERROR + HIGHER_ACCESS_REQUIRED);
679 if (is_room_aide()) return(0);
680 if (required_level >= ac_room_aide) {
681 cprintf("%d This command requires Aide or Room Aide access.\n",
682 ERROR + HIGHER_ACCESS_REQUIRED);
686 /* shhh ... succeed quietly */
693 * Terminate another running session
695 void cmd_term(char *cmdbuf)
700 session_num = extract_int(cmdbuf, 0);
702 terminated = CtdlTerminateOtherSession(session_num);
704 if (terminated < 0) {
705 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
709 if (terminated & TERM_FOUND) {
710 if (terminated == TERM_KILLED) {
711 cprintf("%d Session terminated.\n", CIT_OK);
714 cprintf("%d You are not allowed to do that.\n",
715 ERROR + HIGHER_ACCESS_REQUIRED);
719 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
725 * get the paginator prompt
727 void cmd_more(char *argbuf) {
728 cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
735 void cmd_echo(char *etext)
737 cprintf("%d %s\n", CIT_OK, etext);
742 * Perform privilege escalation for an internal program
744 void cmd_ipgm(char *argbuf)
748 secret = extract_int(argbuf, 0);
750 /* For security reasons, we do NOT allow this command to run
751 * over the network. Local sockets only.
753 if (!CC->is_local_socket) {
755 cprintf("%d Authentication failed.\n", ERROR + PASSWORD_REQUIRED);
757 else if (secret == config.c_ipgm_secret) {
758 CC->internal_pgm = 1;
759 strcpy(CC->curr_user, "<internal program>");
760 CC->cs_flags = CC->cs_flags|CS_STEALTH;
761 cprintf("%d Authenticated as an internal program.\n", CIT_OK);
765 cprintf("%d Authentication failed.\n", ERROR + PASSWORD_REQUIRED);
766 CtdlLogPrintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
773 * Shut down the server
775 void cmd_down(char *argbuf) {
776 char *Reply ="%d Shutting down server. Goodbye.\n";
778 if (CtdlAccessCheck(ac_aide)) return;
780 if (!IsEmptyStr(argbuf))
783 restart_server = extract_int(argbuf, 0);
785 if (restart_server > 0)
787 Reply = "%d citserver will now shut down and automatically restart.\n";
789 if ((restart_server > 0) && !running_as_daemon)
791 CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
792 Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
795 cprintf(Reply, state);
799 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN);
801 CC->kill_me = 1; /* Even the DOWN command has to follow correct proceedure when disconecting */
807 * Halt the server without exiting the server process.
809 void cmd_halt(char *argbuf) {
811 if (CtdlAccessCheck(ac_aide)) return;
813 cprintf("%d Halting server. Goodbye.\n", CIT_OK);
815 shutdown_and_halt = 1;
820 * Schedule or cancel a server shutdown
822 void cmd_scdn(char *argbuf)
826 char *Reply = "%d %d\n";
828 if (CtdlAccessCheck(ac_aide)) return;
830 new_state = extract_int(argbuf, 0);
831 if ((new_state == 2) || (new_state == 3))
834 if (!running_as_daemon)
836 CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
837 Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
841 restart_server = extract_int(argbuf, 0);
844 if ((new_state == 0) || (new_state == 1)) {
845 ScheduledShutdown = new_state;
847 cprintf(Reply, state, ScheduledShutdown);
852 * Set or unset asynchronous protocol mode
854 void cmd_asyn(char *argbuf)
858 new_state = extract_int(argbuf, 0);
859 if ((new_state == 0) || (new_state == 1)) {
860 CC->is_async = new_state;
862 cprintf("%d %d\n", CIT_OK, CC->is_async);
867 * Generate a "nonce" for APOP-style authentication.
869 * RFC 1725 et al specify a PID to be placed in front of the nonce.
870 * Quoth BTX: That would be stupid.
872 void generate_nonce(CitContext *con) {
875 memset(con->cs_nonce, NONCE_SIZE, 0);
876 gettimeofday(&tv, NULL);
877 memset(con->cs_nonce, NONCE_SIZE, 0);
878 snprintf(con->cs_nonce, NONCE_SIZE, "<%d%ld@%s>",
879 rand(), (long)tv.tv_usec, config.c_fqdn);
884 * Back-end function for starting a session
886 void begin_session(CitContext *con)
889 struct sockaddr_in sin;
892 * Initialize some variables specific to our context.
895 con->internal_pgm = 0;
896 con->download_fp = NULL;
897 con->upload_fp = NULL;
898 con->FirstExpressMessage = NULL;
900 time(&con->lastidle);
901 strcpy(con->lastcmdname, " ");
902 strcpy(con->cs_clientname, "(unknown)");
903 strcpy(con->curr_user, NLI);
904 *con->net_node = '\0';
905 *con->fake_username = '\0';
906 *con->fake_hostname = '\0';
907 *con->fake_roomname = '\0';
909 safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
910 safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
911 con->cs_UDSclientUID = -1;
912 con->cs_host[sizeof con->cs_host - 1] = 0;
914 if (!CC->is_local_socket) {
915 locate_host(con->cs_host, sizeof con->cs_host,
916 con->cs_addr, sizeof con->cs_addr,
923 #ifdef HAVE_STRUCT_UCRED
925 /* as http://www.wsinnovations.com/softeng/articles/uds.html told us... */
926 struct ucred credentials;
927 socklen_t ucred_length = sizeof(struct ucred);
929 /*fill in the user data structure */
930 if(getsockopt(con->client_socket, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
931 CtdlLogPrintf(CTDL_NOTICE, "could obtain credentials from unix domain socket");
935 /* the process ID of the process on the other side of the socket */
936 /* credentials.pid; */
938 /* the effective UID of the process on the other side of the socket */
939 con->cs_UDSclientUID = credentials.uid;
941 /* the effective primary GID of the process on the other side of the socket */
942 /* credentials.gid; */
944 /* To get supplemental groups, we will have to look them up in our account
945 database, after a reverse lookup on the UID to get the account name.
946 We can take this opportunity to check to see if this is a legit account.
953 con->upload_type = UPL_FILE;
957 if (((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions)) || CtdlWantSingleUser()) {
961 if (!CC->is_local_socket) {
962 CtdlLogPrintf(CTDL_NOTICE, "Session (%s) started from %s (%s).\n", con->ServiceName, con->cs_host, con->cs_addr);
965 CtdlLogPrintf(CTDL_NOTICE, "Session (%s) started via local socket UID:%d.\n", con->ServiceName, con->cs_UDSclientUID);
968 /* Run any session startup routines registered by loadable modules */
969 PerformSessionHooks(EVT_START);
973 void citproto_begin_session() {
974 if (CC->nologin==1) {
975 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
976 ERROR + MAX_SESSIONS_EXCEEDED,
977 config.c_nodename, config.c_maxsessions
982 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
983 CC->can_receive_im = 1;
988 void cmd_noop(char *argbuf)
990 cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
994 void cmd_qnop(char *argbuf)
996 /* do nothing, this command returns no response */
1000 void cmd_quit(char *argbuf)
1002 cprintf("%d Goodbye.\n", CIT_OK);
1007 void cmd_lout(char *argbuf)
1011 cprintf("%d logged out.\n", CIT_OK);
1016 * This loop recognizes all server commands.
1018 void do_command_loop(void) {
1020 const char *old_name = NULL;
1022 old_name = CtdlThreadName("do_command_loop");
1025 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1026 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1027 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\n");
1029 CtdlThreadName(old_name);
1033 /* Log the server command, but don't show passwords... */
1034 if ( (strncasecmp(cmdbuf, "PASS", 4)) && (strncasecmp(cmdbuf, "SETP", 4)) ) {
1035 CtdlLogPrintf(CTDL_INFO, "CtdlCommand [%s] [%s] %s\n", CTDLUSERIP, CC->curr_user, cmdbuf);
1038 CtdlLogPrintf(CTDL_INFO, "CtdlCommand [%s] [%s] <password command hidden from log>\n", CTDLUSERIP, CC->curr_user);
1044 * Let other clients see the last command we executed, and
1045 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
1047 if ( (strncasecmp(cmdbuf, "NOOP", 4))
1048 && (strncasecmp(cmdbuf, "QNOP", 4))
1049 && (strncasecmp(cmdbuf, "PEXP", 4))
1050 && (strncasecmp(cmdbuf, "GEXP", 4))
1051 && (strncasecmp(cmdbuf, "RWHO", 4))
1052 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
1053 strcpy(CC->lastcmdname, " ");
1054 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
1055 time(&CC->lastidle);
1058 CtdlThreadName(cmdbuf);
1060 if ((strncasecmp(cmdbuf, "ENT0", 4))
1061 && (strncasecmp(cmdbuf, "MESG", 4))
1062 && (strncasecmp(cmdbuf, "MSGS", 4)))
1064 CC->cs_flags &= ~CS_POSTING;
1067 if (!DLoader_Exec_Cmd(cmdbuf)) {
1068 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
1073 /* Run any after-each-command routines registered by modules */
1074 PerformSessionHooks(EVT_CMD);
1075 CtdlThreadName(old_name);
1080 * This loop performs all asynchronous functions.
1082 void do_async_loop(void) {
1083 PerformSessionHooks(EVT_ASYNC);
1087 /*****************************************************************************/
1088 /* MODULE INITIALIZATION STUFF */
1089 /*****************************************************************************/
1091 CTDL_MODULE_INIT(citserver)
1094 CtdlRegisterProtoHook(cmd_noop, "NOOP", "no operation");
1095 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "no operation with no response");
1096 CtdlRegisterProtoHook(cmd_quit, "QUIT", "log out and disconnect from server");
1097 CtdlRegisterProtoHook(cmd_lout, "LOUT", "log out but do not disconnect from server");
1098 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "enable asynchronous server responses");
1099 CtdlRegisterProtoHook(cmd_info, "INFO", "fetch server capabilities and configuration");
1100 CtdlRegisterProtoHook(cmd_mesg, "MESG", "fetch system banners");
1101 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "submit system banners");
1102 CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
1103 CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
1104 CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
1105 CtdlRegisterProtoHook(cmd_ipgm, "IPGM", "perform privilege escalation for internal programs");
1106 CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
1107 CtdlRegisterProtoHook(cmd_down, "DOWN", "perform a server shutdown");
1108 CtdlRegisterProtoHook(cmd_halt, "HALT", "halt the server without exiting the server process");
1109 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "schedule or cancel a server shutdown");
1110 CtdlRegisterProtoHook(cmd_time, "TIME", "fetch the date and time from the server");
1112 /* return our id for the Log */