4 * Main source module for the Citadel server
6 * Copyright (c) 1987-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <sys/types.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
58 #include <libcitadel.h>
61 #include "sysdep_decls.h"
63 #include "citserver.h"
66 #include "housekeeping.h"
70 #include "locate_host.h"
75 #include "euidindex.h"
76 #include "svn_revision.h"
82 #include "ctdl_module.h"
85 struct CitContext *ContextList = NULL;
86 struct CitContext* next_session = NULL;
87 char *unique_session_numbers;
88 int ScheduledShutdown = 0;
89 time_t server_startup_time;
93 * \brief print the actual stack frame.
95 void cit_backtrace(void)
98 void *stack_frames[50];
103 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
104 strings = backtrace_symbols(stack_frames, size);
105 for (i = 0; i < size; i++) {
107 CtdlLogPrintf(1, "%s\n", strings[i]);
109 CtdlLogPrintf(1, "%p\n", stack_frames[i]);
116 * \brief print the actual stack frame.
118 void cit_panic_backtrace(int SigNum)
120 #ifdef HAVE_BACKTRACE
121 void *stack_frames[10];
125 printf("caught signal 11\n");
126 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
127 strings = backtrace_symbols(stack_frames, size);
128 for (i = 0; i < size; i++) {
130 CtdlLogPrintf(1, "%s\n", strings[i]);
132 CtdlLogPrintf(1, "%p\n", stack_frames[i]);
140 * Various things that need to be initialized at startup
142 void master_startup(void) {
146 struct ctdlroom qrbuf;
149 CtdlLogPrintf(CTDL_DEBUG, "master_startup() started\n");
150 time(&server_startup_time);
152 CtdlLogPrintf(CTDL_INFO, "Opening databases\n");
155 ctdl_thread_internal_init_tsd();
157 CtdlThreadAllocTSD();
161 CtdlLogPrintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
162 CtdlCreateRoom(config.c_baseroom, 0, "", 0, 1, 0, VIEW_BBS);
163 CtdlCreateRoom(AIDEROOM, 3, "", 0, 1, 0, VIEW_BBS);
164 CtdlCreateRoom(SYSCONFIGROOM, 3, "", 0, 1, 0, VIEW_BBS);
165 CtdlCreateRoom(config.c_twitroom, 0, "", 0, 1, 0, VIEW_BBS);
167 /* The "Local System Configuration" room doesn't need to be visible */
168 if (CtdlGetRoomLock(&qrbuf, SYSCONFIGROOM) == 0) {
169 qrbuf.QRflags2 |= QR2_SYSTEM;
170 CtdlPutRoomLock(&qrbuf);
173 /* Aide needs to be public postable, else we're not RFC conformant. */
174 if (CtdlGetRoomLock(&qrbuf, AIDEROOM) == 0) {
175 qrbuf.QRflags2 |= QR2_SMTP_PUBLIC;
176 CtdlPutRoomLock(&qrbuf);
179 CtdlLogPrintf(CTDL_INFO, "Seeding the pseudo-random number generator...\n");
180 urandom = fopen("/dev/urandom", "r");
181 if (urandom != NULL) {
182 rv = fread(&seed, sizeof seed, 1, urandom);
186 gettimeofday(&tv, NULL);
192 CtdlLogPrintf(CTDL_INFO, "Initializing ipgm secret\n");
194 config.c_ipgm_secret = rand();
197 CtdlLogPrintf(CTDL_DEBUG, "master_startup() finished\n");
202 * Cleanup routine to be called when the server is shutting down.
204 void master_cleanup(int exitcode) {
205 struct CleanupFunctionHook *fcn;
206 static int already_cleaning_up = 0;
208 if (already_cleaning_up) while(1) sleep(1);
209 already_cleaning_up = 1;
211 /* Run any cleanup routines registered by loadable modules */
212 for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
213 (*fcn->h_function_pointer)();
216 /* Close the AdjRefCount queue file */
219 /* Do system-dependent stuff */
220 sysdep_master_cleanup();
222 /* Close databases */
223 CtdlLogPrintf(CTDL_INFO, "Closing databases\n");
226 #ifdef DEBUG_MEMORY_LEAKS
230 /* If the operator requested a halt but not an exit, halt here. */
231 if (shutdown_and_halt) {
232 CtdlLogPrintf(CTDL_NOTICE, "citserver: Halting server without exiting.\n");
233 fflush(stdout); fflush(stderr);
242 CtdlLogPrintf(CTDL_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 * Terminate a session.
257 void RemoveContext (struct CitContext *con)
260 CtdlLogPrintf(CTDL_ERR,
261 "WARNING: RemoveContext() called with NULL!\n");
264 CtdlLogPrintf(CTDL_DEBUG, "RemoveContext() session %d\n", con->cs_pid);
266 /* Run any cleanup routines registered by loadable modules.
267 * Note: We have to "become_session()" because the cleanup functions
268 * might make references to "CC" assuming it's the right one.
272 PerformSessionHooks(EVT_STOP);
273 become_session(NULL);
275 CtdlLogPrintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
277 /* If the client is still connected, blow 'em away. */
278 CtdlLogPrintf(CTDL_DEBUG, "Closing socket %d\n", con->client_socket);
279 close(con->client_socket);
281 /* If using AUTHMODE_LDAP, free the DN */
287 CtdlLogPrintf(CTDL_DEBUG, "Done with RemoveContext()\n");
295 * cmd_info() - tell the client about this server
297 void cmd_info(char *cmdbuf) {
298 cprintf("%d Server info:\n", LISTING_FOLLOWS);
299 cprintf("%d\n", CC->cs_pid);
300 cprintf("%s\n", config.c_nodename);
301 cprintf("%s\n", config.c_humannode);
302 cprintf("%s\n", config.c_fqdn);
303 cprintf("%s\n", CITADEL);
304 cprintf("%d\n", REV_LEVEL);
305 cprintf("%s\n", config.c_site_location);
306 cprintf("%s\n", config.c_sysadm);
307 cprintf("%d\n", SERVER_TYPE);
308 cprintf("%s\n", config.c_moreprompt);
309 cprintf("1\n"); /* 1 = yes, this system supports floors */
310 cprintf("1\n"); /* 1 = we support the extended paging options */
311 cprintf("%s\n", CC->cs_nonce);
312 cprintf("1\n"); /* 1 = yes, this system supports the QNOP command */
315 cprintf("1\n"); /* 1 = yes, this server is LDAP-enabled */
317 cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
320 if (config.c_auth_mode == AUTHMODE_NATIVE) {
321 cprintf("%d\n", config.c_disable_newu);
324 cprintf("1\n"); /* "create new user" does not work with non-native auth modes */
327 cprintf("%s\n", config.c_default_cal_zone);
329 /* Output load averages */
330 cprintf("%f\n", CtdlThreadLoadAvg);
331 cprintf("%f\n", CtdlThreadWorkerAvg);
332 cprintf("%d\n", CtdlThreadGetCount());
334 cprintf("1\n"); /* yes, Sieve mail filtering is supported */
335 cprintf("%d\n", config.c_enable_fulltext);
336 cprintf("%s\n", svn_revision());
338 if (config.c_auth_mode == AUTHMODE_NATIVE) {
339 cprintf("1\n"); /* OpenID is enabled when using native auth */
342 cprintf("0\n"); /* OpenID is disabled when using non-native auth */
350 * returns an asterisk if there are any instant messages waiting,
353 char CtdlCheckExpress(void) {
354 if (CC->FirstExpressMessage == NULL) {
362 void cmd_time(char *argbuf)
368 localtime_r(&tv, &tmp);
370 /* timezone and daylight global variables are not portable. */
371 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
372 cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
374 cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
380 * Check originating host against the public_clients file. This determines
381 * whether the client is allowed to change the hostname for this session
382 * (for example, to show the location of the user rather than the location
385 int is_public_client(void)
391 char *public_clientspos;
392 char *public_clientsend;
395 static time_t pc_timestamp = 0;
396 static char public_clients[SIZ];
397 static char public_clients_file[SIZ];
399 #define LOCALHOSTSTR "127.0.0.1"
401 snprintf(public_clients_file,
402 sizeof public_clients_file,
407 * Check the time stamp on the public_clients file. If it's been
408 * updated since the last time we were here (or if this is the first
409 * time we've been through the loop), read its contents and learn
410 * the IP addresses of the listed hosts.
412 if (stat(public_clients_file, &statbuf) != 0) {
413 /* No public_clients file exists, so bail out */
414 CtdlLogPrintf(CTDL_WARNING, "Warning: '%s' does not exist\n",
415 public_clients_file);
419 if (statbuf.st_mtime > pc_timestamp) {
420 begin_critical_section(S_PUBLIC_CLIENTS);
421 CtdlLogPrintf(CTDL_INFO, "Loading %s\n", public_clients_file);
423 public_clientspos = &public_clients[0];
424 public_clientsend = public_clientspos + SIZ;
425 safestrncpy(public_clientspos, LOCALHOSTSTR, sizeof public_clients);
426 public_clientspos += sizeof(LOCALHOSTSTR) - 1;
428 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
429 *(public_clientspos++) = '|';
431 while (!IsEmptyStr (paddr) &&
432 (public_clientspos < public_clientsend))
433 *(public_clientspos++) = *(paddr++);
436 fp = fopen(public_clients_file, "r");
438 while ((fgets(buf, sizeof buf, fp)!=NULL) &&
439 (public_clientspos < public_clientsend)){
442 while (!IsEmptyStr(ptr)) {
450 while (ptr>buf && isspace(*ptr)) {
453 if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
454 *(public_clientspos++) = '|';
456 while (!IsEmptyStr(paddr) &&
457 (public_clientspos < public_clientsend)){
458 *(public_clientspos++) = *(paddr++);
463 pc_timestamp = time(NULL);
464 end_critical_section(S_PUBLIC_CLIENTS);
467 CtdlLogPrintf(CTDL_DEBUG, "Checking whether %s is a local or public client\n",
469 for (i=0; i<num_parms(public_clients); ++i) {
470 extract_token(addrbuf, public_clients, i, '|', sizeof addrbuf);
471 if (!strcasecmp(CC->cs_addr, addrbuf)) {
472 CtdlLogPrintf(CTDL_DEBUG, "... yes it is.\n");
477 /* No hits. This is not a public client. */
478 CtdlLogPrintf(CTDL_DEBUG, "... no it isn't.\n");
484 * the client is identifying itself to the server
486 void cmd_iden(char *argbuf)
496 if (num_parms(argbuf)<4) {
497 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
501 dev_code = extract_int(argbuf,0);
502 cli_code = extract_int(argbuf,1);
503 rev_level = extract_int(argbuf,2);
504 extract_token(desc, argbuf, 3, '|', sizeof desc);
506 safestrncpy(from_host, config.c_fqdn, sizeof from_host);
507 from_host[sizeof from_host - 1] = 0;
508 if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
510 CC->cs_clientdev = dev_code;
511 CC->cs_clienttyp = cli_code;
512 CC->cs_clientver = rev_level;
513 safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
514 CC->cs_clientname[31] = 0;
516 if (!IsEmptyStr(from_host)) {
517 if (CC->is_local_socket) do_lookup = 1;
518 else if (is_public_client()) do_lookup = 1;
522 CtdlLogPrintf(CTDL_DEBUG, "Looking up hostname '%s'\n", from_host);
523 if ((addr.s_addr = inet_addr(from_host)) != -1) {
524 locate_host(CC->cs_host, sizeof CC->cs_host,
525 CC->cs_addr, sizeof CC->cs_addr,
529 safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
530 CC->cs_host[sizeof CC->cs_host - 1] = 0;
534 CtdlLogPrintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
541 cprintf("%d Ok\n",CIT_OK);
546 * display system messages or help
548 void cmd_mesg(char *mname)
558 extract_token(buf, mname, 0, '|', sizeof buf);
560 dirs[0] = strdup(ctdl_message_dir);
561 dirs[1] = strdup(ctdl_hlp_dir);
563 snprintf(buf2, sizeof buf2, "%s.%d.%d",
564 buf, CC->cs_clientdev, CC->cs_clienttyp);
566 /* If the client requested "?" then produce a listing */
567 if (!strcmp(buf, "?")) {
568 cprintf("%d %s\n", LISTING_FOLLOWS, buf);
569 dp = opendir(dirs[1]);
571 while (d = readdir(dp), d != NULL) {
572 if (d->d_name[0] != '.') {
573 cprintf(" %s\n", d->d_name);
584 /* Otherwise, look for the requested file by name. */
586 mesg_locate(targ, sizeof targ, buf2, 2, (const char **)dirs);
587 if (IsEmptyStr(targ)) {
588 snprintf(buf2, sizeof buf2, "%s.%d",
589 buf, CC->cs_clientdev);
590 mesg_locate(targ, sizeof targ, buf2, 2,
591 (const char **)dirs);
592 if (IsEmptyStr(targ)) {
593 mesg_locate(targ, sizeof targ, buf, 2,
594 (const char **)dirs);
602 if (IsEmptyStr(targ)) {
603 cprintf("%d '%s' not found. (Searching in %s and %s)\n",
604 ERROR + FILE_NOT_FOUND,
612 mfp = fopen(targ, "r");
614 cprintf("%d Cannot open '%s': %s\n",
615 ERROR + INTERNAL_ERROR, targ, strerror(errno));
618 cprintf("%d %s\n", LISTING_FOLLOWS,buf);
620 while (fgets(buf, (sizeof buf - 1), mfp) != NULL) {
621 buf[strlen(buf)-1] = 0;
632 * enter system messages or help
634 void cmd_emsg(char *mname)
644 if (CtdlAccessCheck(ac_aide)) return;
646 extract_token(buf, mname, 0, '|', sizeof buf);
647 for (a=0; !IsEmptyStr(&buf[a]); ++a) { /* security measure */
648 if (buf[a] == '/') buf[a] = '.';
651 dirs[0] = strdup(ctdl_message_dir);
652 dirs[1] = strdup(ctdl_hlp_dir);
654 mesg_locate(targ, sizeof targ, buf, 2, (const char**)dirs);
658 if (IsEmptyStr(targ)) {
659 snprintf(targ, sizeof targ,
664 mfp = fopen(targ,"w");
666 cprintf("%d Cannot open '%s': %s\n",
667 ERROR + INTERNAL_ERROR, targ, strerror(errno));
670 cprintf("%d %s\n", SEND_LISTING, targ);
672 while (client_getln(buf, sizeof buf) >=0 && strcmp(buf, "000")) {
673 fprintf(mfp, "%s\n", buf);
680 /* Don't show the names of private rooms unless the viewing
681 * user also knows the rooms.
683 void GenerateRoomDisplay(char *real_room,
684 struct CitContext *viewed,
685 struct CitContext *viewer) {
689 strcpy(real_room, viewed->room.QRname);
690 if (viewed->room.QRflags & QR_MAILBOX) {
691 strcpy(real_room, &real_room[11]);
693 if (viewed->room.QRflags & QR_PRIVATE) {
694 CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
695 if ( (ra & UA_KNOWN) == 0) {
696 strcpy(real_room, "<private room>");
700 if (viewed->cs_flags & CS_CHAT) {
701 while (strlen(real_room) < 14) {
702 strcat(real_room, " ");
704 strcpy(&real_room[14], "<chat>");
710 * Convenience function.
712 int CtdlAccessCheck(int required_level) {
714 if (CC->internal_pgm) return(0);
715 if (required_level >= ac_internal) {
716 cprintf("%d This is not a user-level command.\n",
717 ERROR + HIGHER_ACCESS_REQUIRED);
721 if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
722 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
726 if (CC->user.axlevel >= 6) return(0);
727 if (required_level >= ac_aide) {
728 cprintf("%d This command requires Aide access.\n",
729 ERROR + HIGHER_ACCESS_REQUIRED);
733 if (is_room_aide()) return(0);
734 if (required_level >= ac_room_aide) {
735 cprintf("%d This command requires Aide or Room Aide access.\n",
736 ERROR + HIGHER_ACCESS_REQUIRED);
740 /* shhh ... succeed quietly */
747 * Terminate another running session
749 void cmd_term(char *cmdbuf)
752 struct CitContext *ccptr;
756 session_num = extract_int(cmdbuf, 0);
757 if (session_num == CC->cs_pid) {
758 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
762 CtdlLogPrintf(CTDL_DEBUG, "Locating session to kill\n");
763 begin_critical_section(S_SESSION_TABLE);
764 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
765 if (session_num == ccptr->cs_pid) {
767 if ((ccptr->user.usernum == CC->user.usernum)
768 || (CC->user.axlevel >= 6)) {
777 end_critical_section(S_SESSION_TABLE);
781 cprintf("%d Session terminated.\n", CIT_OK);
784 cprintf("%d You are not allowed to do that.\n",
785 ERROR + HIGHER_ACCESS_REQUIRED);
789 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
798 * get the paginator prompt
800 void cmd_more(char *argbuf) {
801 cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
807 void cmd_echo(char *etext)
809 cprintf("%d %s\n", CIT_OK, etext);
815 * identify as internal program
817 void cmd_ipgm(char *argbuf)
821 secret = extract_int(argbuf, 0);
823 /* For security reasons, we do NOT allow this command to run
824 * over the network. Local sockets only.
826 if (!CC->is_local_socket) {
828 cprintf("%d Authentication failed.\n",
829 ERROR + PASSWORD_REQUIRED);
831 else if (secret == config.c_ipgm_secret) {
832 CC->internal_pgm = 1;
833 strcpy(CC->curr_user, "<internal program>");
834 CC->cs_flags = CC->cs_flags|CS_STEALTH;
835 cprintf("%d Authenticated as an internal program.\n", CIT_OK);
839 cprintf("%d Authentication failed.\n",
840 ERROR + PASSWORD_REQUIRED);
841 CtdlLogPrintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
848 * Shut down the server
850 void cmd_down(char *argbuf) {
851 char *Reply ="%d Shutting down server. Goodbye.\n";
853 if (CtdlAccessCheck(ac_aide)) return;
855 if (!IsEmptyStr(argbuf))
858 restart_server = extract_int(argbuf, 0);
860 if (restart_server > 0)
862 Reply = "%d citserver will now shut down and automatically restart.\n";
864 if ((restart_server > 0) && !running_as_daemon)
866 CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as daemon! Geronimooooooo!\n");
867 Reply = "%d Warning: citserver is not running in daemon mode and is therefore unlikely to restart automatically.\n";
870 cprintf(Reply, state);
874 cprintf(Reply, CIT_OK + SERVER_SHUTTING_DOWN);
880 * Halt the server without exiting the server process.
882 void cmd_halt(char *argbuf) {
884 if (CtdlAccessCheck(ac_aide)) return;
886 cprintf("%d Halting server. Goodbye.\n", CIT_OK);
888 shutdown_and_halt = 1;
892 * Schedule or cancel a server shutdown
894 void cmd_scdn(char *argbuf)
898 char *Reply = "%d %d\n";
900 if (CtdlAccessCheck(ac_aide)) return;
902 new_state = extract_int(argbuf, 0);
903 if ((new_state == 2) || (new_state == 3))
906 if (!running_as_daemon)
908 CtdlLogPrintf(CTDL_ERR, "The user requested restart, but not running as deamon! Geronimooooooo!\n");
909 Reply = "%d %d Warning, not running in deamon mode. maybe we will come up again, but don't lean on it.\n";
913 restart_server = extract_int(argbuf, 0);
916 if ((new_state == 0) || (new_state == 1)) {
917 ScheduledShutdown = new_state;
919 cprintf(Reply, state, ScheduledShutdown);
924 * Set or unset asynchronous protocol mode
926 void cmd_asyn(char *argbuf)
930 new_state = extract_int(argbuf, 0);
931 if ((new_state == 0) || (new_state == 1)) {
932 CC->is_async = new_state;
934 cprintf("%d %d\n", CIT_OK, CC->is_async);
939 * Generate a "nonce" for APOP-style authentication.
941 * RFC 1725 et al specify a PID to be placed in front of the nonce.
942 * Quoth BTX: That would be stupid.
944 void generate_nonce(struct CitContext *con) {
947 memset(con->cs_nonce, NONCE_SIZE, 0);
948 gettimeofday(&tv, NULL);
949 memset(con->cs_nonce, NONCE_SIZE, 0);
950 snprintf(con->cs_nonce, NONCE_SIZE, "<%d%ld@%s>",
951 rand(), (long)tv.tv_usec, config.c_fqdn);
958 * Back-end function for starting a session
960 void begin_session(struct CitContext *con)
963 struct sockaddr_in sin;
966 * Initialize some variables specific to our context.
969 con->internal_pgm = 0;
970 con->download_fp = NULL;
971 con->upload_fp = NULL;
972 con->FirstExpressMessage = NULL;
974 time(&con->lastidle);
975 strcpy(con->lastcmdname, " ");
976 strcpy(con->cs_clientname, "(unknown)");
977 strcpy(con->curr_user, NLI);
978 *con->net_node = '\0';
979 *con->fake_username = '\0';
980 *con->fake_hostname = '\0';
981 *con->fake_roomname = '\0';
983 safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
984 safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
985 con->cs_UDSclientUID = -1;
986 con->cs_host[sizeof con->cs_host - 1] = 0;
988 if (!CC->is_local_socket) {
989 if (!getpeername(con->client_socket, (struct sockaddr *) &sin, &len)) {
990 locate_host(con->cs_host, sizeof con->cs_host,
991 con->cs_addr, sizeof con->cs_addr,
997 strcpy(con->cs_host, "");
998 #ifdef HAVE_STRUCT_UCRED
1000 /* as http://www.wsinnovations.com/softeng/articles/uds.html told us... */
1001 struct ucred credentials;
1002 socklen_t ucred_length = sizeof(struct ucred);
1004 /*fill in the user data structure */
1005 if(getsockopt(con->client_socket, SOL_SOCKET, SO_PEERCRED, &credentials, &ucred_length)) {
1006 CtdlLogPrintf(CTDL_NOTICE, "could obtain credentials from unix domain socket");
1010 /* the process ID of the process on the other side of the socket */
1011 /* credentials.pid; */
1013 /* the effective UID of the process on the other side of the socket */
1014 con->cs_UDSclientUID = credentials.uid;
1016 /* the effective primary GID of the process on the other side of the socket */
1017 /* credentials.gid; */
1019 /* To get supplemental groups, we will have to look them up in our account
1020 database, after a reverse lookup on the UID to get the account name.
1021 We can take this opportunity to check to see if this is a legit account.
1028 con->upload_type = UPL_FILE;
1032 if (((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions)) || CtdlWantSingleUser()) {
1036 if (!CC->is_local_socket) {
1037 CtdlLogPrintf(CTDL_NOTICE, "Session started from %s [%s].\n", con->cs_host, con->cs_addr);
1040 CtdlLogPrintf(CTDL_NOTICE, "Session started via local socket.\n");
1043 /* Run any session startup routines registered by loadable modules */
1044 PerformSessionHooks(EVT_START);
1048 void citproto_begin_session() {
1049 if (CC->nologin==1) {
1050 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
1051 ERROR + MAX_SESSIONS_EXCEEDED,
1052 config.c_nodename, config.c_maxsessions
1057 cprintf("%d %s Citadel server ready.\n", CIT_OK, config.c_nodename);
1058 CC->can_receive_im = 1;
1064 void cmd_noop(char *argbuf)
1066 cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
1069 void cmd_qnop(char *argbuf)
1071 /* do nothing, this command returns no response */
1074 void cmd_quit(char *argbuf)
1076 cprintf("%d Goodbye.\n", CIT_OK);
1080 void cmd_lout(char *argbuf)
1084 cprintf("%d logged out.\n", CIT_OK);
1088 * This loop recognizes all server commands.
1090 void do_command_loop(void) {
1092 const char *old_name = NULL;
1094 old_name = CtdlThreadName("do_command_loop");
1097 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1098 if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1099 CtdlLogPrintf(CTDL_ERR, "Client disconnected: ending session.\n");
1101 CtdlThreadName(old_name);
1105 /* Log the server command, but don't show passwords... */
1106 if ( (strncasecmp(cmdbuf, "PASS", 4))
1107 && (strncasecmp(cmdbuf, "SETP", 4)) ) {
1108 CtdlLogPrintf(CTDL_INFO, "%s\n", cmdbuf);
1111 CtdlLogPrintf(CTDL_INFO, "<password command sent>\n");
1117 * Let other clients see the last command we executed, and
1118 * update the idle time, but not NOOP, QNOP, PEXP, GEXP, RWHO, or TIME.
1120 if ( (strncasecmp(cmdbuf, "NOOP", 4))
1121 && (strncasecmp(cmdbuf, "QNOP", 4))
1122 && (strncasecmp(cmdbuf, "PEXP", 4))
1123 && (strncasecmp(cmdbuf, "GEXP", 4))
1124 && (strncasecmp(cmdbuf, "RWHO", 4))
1125 && (strncasecmp(cmdbuf, "TIME", 4)) ) {
1126 strcpy(CC->lastcmdname, " ");
1127 safestrncpy(CC->lastcmdname, cmdbuf, sizeof(CC->lastcmdname));
1128 time(&CC->lastidle);
1131 CtdlThreadName(cmdbuf);
1133 if ((strncasecmp(cmdbuf, "ENT0", 4))
1134 && (strncasecmp(cmdbuf, "MESG", 4))
1135 && (strncasecmp(cmdbuf, "MSGS", 4)))
1137 CC->cs_flags &= ~CS_POSTING;
1140 if (!DLoader_Exec_Cmd(cmdbuf)) {
1141 cprintf("%d Unrecognized or unsupported command.\n", ERROR + CMD_NOT_SUPPORTED);
1146 /* Run any after-each-command routines registered by modules */
1147 PerformSessionHooks(EVT_CMD);
1148 CtdlThreadName(old_name);
1153 * This loop performs all asynchronous functions.
1155 void do_async_loop(void) {
1156 PerformSessionHooks(EVT_ASYNC);
1165 /*****************************************************************************/
1166 /* MODULE INITIALIZATION STUFF */
1167 /*****************************************************************************/
1169 CTDL_MODULE_INIT(citserver)
1172 CtdlRegisterProtoHook(cmd_noop, "NOOP", "Autoconverted. TODO: document me.");
1173 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "Autoconverted. TODO: document me.");
1174 CtdlRegisterProtoHook(cmd_quit, "QUIT", "Autoconverted. TODO: document me.");
1175 CtdlRegisterProtoHook(cmd_lout, "LOUT", "Autoconverted. TODO: document me.");
1176 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "Autoconverted. TODO: document me.");
1177 CtdlRegisterProtoHook(cmd_info, "INFO", "Autoconverted. TODO: document me.");
1178 CtdlRegisterProtoHook(cmd_mesg, "MESG", "Autoconverted. TODO: document me.");
1179 CtdlRegisterProtoHook(cmd_emsg, "EMSG", "Autoconverted. TODO: document me.");
1180 CtdlRegisterProtoHook(cmd_echo, "ECHO", "Autoconverted. TODO: document me.");
1181 CtdlRegisterProtoHook(cmd_more, "MORE", "Autoconverted. TODO: document me.");
1182 CtdlRegisterProtoHook(cmd_iden, "IDEN", "Autoconverted. TODO: document me.");
1183 CtdlRegisterProtoHook(cmd_ipgm, "IPGM", "Autoconverted. TODO: document me.");
1184 CtdlRegisterProtoHook(cmd_term, "TERM", "Autoconverted. TODO: document me.");
1185 CtdlRegisterProtoHook(cmd_down, "DOWN", "Autoconverted. TODO: document me.");
1186 CtdlRegisterProtoHook(cmd_halt, "HALT", "Autoconverted. TODO: document me.");
1187 CtdlRegisterProtoHook(cmd_scdn, "SCDN", "Autoconverted. TODO: document me.");
1188 CtdlRegisterProtoHook(cmd_time, "TIME", "Autoconverted. TODO: document me.");
1190 /* return our Subversion id for the Log */