18 struct CitContext *ContextList = NULL;
19 int ScheduledShutdown = 0;
23 * Various things that need to be initialized at startup
25 void master_startup() {
26 lprintf(7, "Opening databases\n");
29 lprintf(7, "Checking floor reference counts\n");
34 * Cleanup routine to be called when the server is shutting down.
36 void master_cleanup() {
38 /* Cancel all running sessions */
39 lprintf(7, "Cancelling running sessions...\n");
40 while (ContextList != NULL) {
41 kill_session(ContextList->cs_pid);
45 lprintf(7, "Closing databases\n");
48 /* Do system-dependent stuff */
49 sysdep_master_cleanup();
53 lprintf(3, "citserver: exiting.\n");
59 * Gracefully terminate the session and thread.
60 * (This is called as a cleanup handler by the thread library.)
64 struct ExpressMessage *emptr;
66 lprintf(9, "cleanup_stuff() called\n");
68 lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
71 rec_log(CL_TERMINATE,CC->curr_user);
73 lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
74 hook_end_session(CC->cs_pid);
75 syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
77 /* Deallocate any unsent express messages */
78 begin_critical_section(S_SESSION_TABLE);
79 while (CC->FirstExpressMessage != NULL) {
80 emptr = CC->FirstExpressMessage;
81 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
84 end_critical_section(S_SESSION_TABLE);
86 /* Now get rid of the session and context */
87 lprintf(7, "cleanup_stuff() is calling RemoveContext(%d)\n", CC->cs_pid);
90 /* While we still have an extra thread with no user attached to it,
91 * take the opportunity to do some housekeeping before exiting.
98 * set_wtmpsupp() - alter the session listing
100 void set_wtmpsupp(char *newtext)
102 strncpy(CC->cs_room,newtext,19);
104 time(&CC->cs_lastupdt);
105 hook_room_name(CC->cs_pid, CC->cs_room);
110 * cmd_info() - tell the client about this server
112 void cmd_info(void) {
113 cprintf("%d Server info:\n",LISTING_FOLLOWS);
114 cprintf("%d\n",CC->cs_pid);
115 cprintf("%s\n",config.c_nodename);
116 cprintf("%s\n",config.c_humannode);
117 cprintf("%s\n",config.c_fqdn);
118 cprintf("%s\n",CITADEL);
119 cprintf("%d\n",REV_LEVEL);
120 cprintf("%s\n",config.c_bbs_city);
121 cprintf("%s\n",config.c_sysadm);
122 cprintf("%d\n",SERVER_TYPE);
123 cprintf("%s\n",config.c_moreprompt);
124 cprintf("1\n"); /* 1 = yes, this system supports floors */
128 void cmd_rchg(char *newroomname)
130 if ((newroomname) && (newroomname[0]))
132 bzero(CC->fake_roomname, 20);
133 strncpy(CC->fake_roomname, newroomname, 19);
136 CC->fake_roomname[0] = '\0';
140 void cmd_hchg(char *newhostname)
142 if ((newhostname) && (newhostname[0]))
144 bzero(CC->fake_hostname, 25);
145 strncpy(CC->fake_hostname, newhostname, 24);
148 CC->fake_hostname[0] = '\0';
152 void cmd_uchg(char *newusername)
154 if (CC->usersupp.axlevel < 6)
156 cprintf("%d You must be an Aide to use UCHG.\n",
157 ERROR+HIGHER_ACCESS_REQUIRED);
160 if ((newusername) && (newusername[0]))
162 CC->cs_flags &= ~CS_STEALTH;
163 bzero(CC->fake_username, 32);
164 if (struncmp(newusername, CC->curr_user, strlen(CC->curr_user)))
165 strncpy(CC->fake_username, newusername, 31);
169 CC->fake_username[0] = '\0';
170 CC->cs_flags |= CS_STEALTH;
181 cprintf("%d|%ld\n", OK, tv);
185 * check a hostname against the public_clients file
187 int is_public_client(char *where)
192 if (!strucmp(where,"localhost")) return(1);
193 if (!strucmp(where,config.c_fqdn)) return(1);
195 fp = fopen("public_clients","r");
196 if (fp == NULL) return(0);
198 while (fgets(buf,256,fp)!=NULL) {
199 while (isspace((buf[strlen(buf)-1])))
200 buf[strlen(buf)-1] = 0;
201 if (!strucmp(buf,where)) {
213 * the client is identifying itself to the server
215 void cmd_iden(char *argbuf)
223 if (num_parms(argbuf)<4) {
224 cprintf("%d usage error\n",ERROR);
228 dev_code = extract_int(argbuf,0);
229 cli_code = extract_int(argbuf,1);
230 rev_level = extract_int(argbuf,2);
231 extract(desc,argbuf,3);
233 strcpy(from_host,config.c_fqdn);
234 if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
236 CC->cs_clientdev = dev_code;
237 CC->cs_clienttyp = cli_code;
238 CC->cs_clientver = rev_level;
239 strncpy(CC->cs_clientname,desc,31);
240 CC->cs_clientname[31] = 0;
242 if ((strlen(from_host)>0) &&
243 (is_public_client(CC->cs_host))) {
244 strncpy(CC->cs_host,from_host,24);
247 set_wtmpsupp(CC->quickroom.QRname);
249 syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
255 cprintf("%d Ok\n",OK);
260 * enter or exit "stealth mode"
262 void cmd_stel(char *cmdbuf)
266 requested_mode = extract_int(cmdbuf,0);
267 if (requested_mode !=0) requested_mode = 1;
269 if (!CC->logged_in) {
270 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
274 if (CC->usersupp.axlevel < 6) {
275 cprintf("%d You must be an Aide to use stealth mode.\n",
276 ERROR+HIGHER_ACCESS_REQUIRED);
280 if (CC->cs_flags & CS_STEALTH) {
281 if (requested_mode == 0)
282 CC->cs_flags = CC->cs_flags-CS_STEALTH;
285 if (requested_mode == 1)
286 CC->cs_flags = CC->cs_flags|CS_STEALTH;
289 set_wtmpsupp(CC->quickroom.QRname);
290 cprintf("%d Ok\n",OK);
297 * display system messages or help
299 void cmd_mesg(char *mname)
306 extract(buf,mname,0);
311 strcpy(dirs[0],"messages");
312 strcpy(dirs[1],"help");
313 mesg_locate(targ,buf,2,dirs);
318 if (strlen(targ)==0) {
319 cprintf("%d '%s' not found.\n",ERROR,mname);
323 mfp = fopen(targ,"r");
325 cprintf("%d Cannot open '%s': %s\n",
326 ERROR,targ,strerror(errno));
329 cprintf("%d %s\n",LISTING_FOLLOWS,buf);
331 while (fgets(buf,255,mfp)!=NULL) {
332 buf[strlen(buf)-1] = 0;
343 * enter system messages or help
345 void cmd_emsg(char *mname)
353 if (CC->usersupp.axlevel < 6) {
354 cprintf("%d You must be an Aide to edit system messages.\n",
355 ERROR+HIGHER_ACCESS_REQUIRED);
359 extract(buf,mname,0);
360 for (a=0; a<strlen(buf); ++a) { /* security measure */
361 if (buf[a] == '/') buf[a] = '.';
366 strcpy(dirs[0],"messages");
367 strcpy(dirs[1],"help");
368 mesg_locate(targ,buf,2,dirs);
372 if (strlen(targ)==0) {
373 sprintf(targ, "./help/%s", buf);
376 mfp = fopen(targ,"w");
378 cprintf("%d Cannot open '%s': %s\n",
379 ERROR,targ,strerror(errno));
382 cprintf("%d %s\n", SEND_LISTING, targ);
384 while (client_gets(buf), strcmp(buf, "000")) {
385 fprintf(mfp, "%s\n", buf);
395 void cmd_rwho(void) {
396 struct CitContext *cptr;
399 char un[40], room[40], host[40], flags[5];
401 aide = CC->usersupp.axlevel >= 6;
402 cprintf("%d\n",LISTING_FOLLOWS);
404 for (cptr = ContextList; cptr != NULL; cptr = cptr->next)
409 if (cptr->cs_flags & CS_POSTING)
414 if (cptr->fake_username[0])
416 strcpy(un, cptr->fake_username);
420 strcpy(un, cptr->curr_user);
422 if (cptr->fake_hostname[0])
424 strcpy(host, cptr->fake_hostname);
428 strcpy(host, cptr->cs_host);
430 if (cptr->fake_roomname[0])
432 strcpy(room, cptr->fake_roomname);
436 strcpy(room, cptr->cs_room);
439 if ((aide) && (spoofed))
442 if ((cptr->cs_flags & CS_STEALTH) && (aide))
445 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
447 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
448 cptr->cs_pid, un, room,
449 host, cptr->cs_clientname,
450 (long)(cptr->lastidle),
451 cptr->lastcmdname, flags);
453 if ((spoofed) && (aide))
455 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
456 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
457 cptr->cs_host, cptr->cs_clientname,
458 (long)(cptr->lastidle),
459 cptr->lastcmdname, flags);
468 * Terminate another running session
470 void cmd_term(char *cmdbuf)
473 struct CitContext *ccptr;
475 if (!CC->logged_in) {
476 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
480 if (CC->usersupp.axlevel < 6) {
481 cprintf("%d You must be an Aide to terminate sessions.\n",
482 ERROR+HIGHER_ACCESS_REQUIRED);
486 session_num = extract_int(cmdbuf, 0);
487 if (session_num == CC->cs_pid) {
488 cprintf("%d You can't kill your own session.\n", ERROR);
492 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
493 if (session_num == ccptr->cs_pid) {
494 kill_session(ccptr->cs_pid);
495 cprintf("%d Session terminated.\n", OK);
500 cprintf("%d No such session.\n", ERROR);
508 * get the paginator prompt
510 void cmd_more(void) {
511 cprintf("%d %s\n",OK,config.c_moreprompt);
517 void cmd_echo(char *etext)
519 cprintf("%d %s\n",OK,etext);
525 * identify as internal program
527 void cmd_ipgm(char *argbuf)
531 secret = extract_int(argbuf, 0);
532 if (secret == config.c_ipgm_secret) {
533 CC->internal_pgm = 1;
534 strcpy(CC->curr_user, "<internal program>");
535 CC->cs_flags = CC->cs_flags|CS_STEALTH;
536 cprintf("%d Authenticated as an internal program.\n",OK);
539 cprintf("%d Authentication failed.\n",ERROR);
545 * Shut down the server
547 void cmd_down(void) {
548 if (!CC->logged_in) {
549 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
553 if (CC->usersupp.axlevel < 6) {
554 cprintf("%d You must be an Aide to shut down the server.\n",
555 ERROR+HIGHER_ACCESS_REQUIRED);
559 cprintf("%d Shutting down server. Goodbye.\n", OK);
564 * Shut down the server
566 void cmd_scdn(char *argbuf)
570 if (!CC->logged_in) {
571 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
575 if (CC->usersupp.axlevel < 6) {
576 cprintf("%d You must be an Aide to schedule a shutdown.\n",
577 ERROR+HIGHER_ACCESS_REQUIRED);
581 new_state = extract_int(argbuf, 0);
582 if ((new_state == 0) || (new_state == 1)) {
583 ScheduledShutdown = new_state;
585 cprintf("%d %d\n", OK, ScheduledShutdown);
591 void *context_loop(struct CitContext *con)
597 * Wedge our way into the context table.
602 * Initialize some variables specific to our context.
606 CC->internal_pgm = 0;
607 CC->download_fp = NULL;
608 CC->upload_fp = NULL;
609 CC->cs_pid = con->client_socket; /* not necessarily portable */
610 CC->FirstExpressMessage = NULL;
613 strcpy(CC->lastcmdname, " ");
614 strcpy(CC->cs_clientname, "(unknown)");
615 strcpy(CC->curr_user,"");
616 strcpy(CC->net_node,"");
617 sprintf(CC->temp,"/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
618 strcpy(CC->cs_room, "");
619 strcpy(CC->cs_host, config.c_fqdn);
620 locate_host(CC->cs_host);
622 CC->upload_type = UPL_FILE;
625 session_num = session_count();
627 if ((config.c_maxsessions > 0)&&(session_num >= config.c_maxsessions))
630 if (CC->nologin==1) {
631 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
632 ERROR+MAX_SESSIONS_EXCEEDED,
633 config.c_nodename,config.c_maxsessions);
636 cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
639 lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
640 hook_start_session(CC->cs_pid);
641 rec_log(CL_CONNECT, "");
645 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
646 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
647 hook_command_received(CC->cs_pid, cmdbuf);
650 * Let other clients see the last command we executed, but
651 * exclude NOOP because that would be boring.
653 if (struncmp(cmdbuf, "NOOP", 4)) {
654 strcpy(CC->lastcmdname, " ");
655 strncpy(CC->lastcmdname, cmdbuf, 4);
659 if ((struncmp(cmdbuf, "ENT0", 4)) && (struncmp(cmdbuf, "MESG", 4)) && (struncmp(cmdbuf, "MSGS", 4)))
661 CC->cs_flags &= ~CS_POSTING;
665 * This loop recognizes all server commands.
668 if (!struncmp(cmdbuf,"NOOP",4)) {
669 cprintf("%d%cok\n",OK,check_express());
672 else if (!struncmp(cmdbuf,"QUIT",4)) {
673 cprintf("%d Goodbye.\n",OK);
676 else if (!struncmp(cmdbuf,"LOUT",4)) {
677 if (CC->logged_in) logout(CC);
678 cprintf("%d logged out.\n",OK);
681 else if (!struncmp(cmdbuf,"USER",4)) {
682 cmd_user(&cmdbuf[5]);
685 else if (!struncmp(cmdbuf,"PASS",4)) {
686 cmd_pass(&cmdbuf[5]);
689 else if (!struncmp(cmdbuf,"NEWU",4)) {
690 cmd_newu(&cmdbuf[5]);
693 else if (!struncmp(cmdbuf,"SETP",4)) {
694 cmd_setp(&cmdbuf[5]);
697 else if (!struncmp(cmdbuf,"LRMS",4)) {
698 cmd_lrms(&cmdbuf[5]);
701 else if (!struncmp(cmdbuf,"LKRA",4)) {
702 cmd_lkra(&cmdbuf[5]);
705 else if (!struncmp(cmdbuf,"LKRN",4)) {
706 cmd_lkrn(&cmdbuf[5]);
709 else if (!struncmp(cmdbuf,"LKRO",4)) {
710 cmd_lkro(&cmdbuf[5]);
713 else if (!struncmp(cmdbuf,"LZRM",4)) {
714 cmd_lzrm(&cmdbuf[5]);
717 else if (!struncmp(cmdbuf,"GETU",4)) {
721 else if (!struncmp(cmdbuf,"SETU",4)) {
722 cmd_setu(&cmdbuf[5]);
725 else if (!struncmp(cmdbuf,"GOTO",4)) {
726 cmd_goto(&cmdbuf[5]);
729 else if (!struncmp(cmdbuf,"MSGS",4)) {
730 cmd_msgs(&cmdbuf[5]);
733 else if (!struncmp(cmdbuf,"WHOK",4)) {
737 else if (!struncmp(cmdbuf,"RDIR",4)) {
741 else if (!struncmp(cmdbuf,"MSG0",4)) {
742 cmd_msg0(&cmdbuf[5]);
745 else if (!struncmp(cmdbuf,"MSG2",4)) {
746 cmd_msg2(&cmdbuf[5]);
749 else if (!struncmp(cmdbuf,"MSG3",4)) {
750 cmd_msg3(&cmdbuf[5]);
753 else if (!struncmp(cmdbuf,"INFO",4)) {
757 else if (!struncmp(cmdbuf,"SLRP",4)) {
758 cmd_slrp(&cmdbuf[5]);
761 else if (!struncmp(cmdbuf,"INVT",4)) {
762 cmd_invt_kick(&cmdbuf[5],1);
765 else if (!struncmp(cmdbuf,"KICK",4)) {
766 cmd_invt_kick(&cmdbuf[5],0);
769 else if (!struncmp(cmdbuf,"GETR",4)) {
773 else if (!struncmp(cmdbuf,"SETR",4)) {
774 cmd_setr(&cmdbuf[5]);
777 else if (!struncmp(cmdbuf,"GETA",4)) {
781 else if (!struncmp(cmdbuf,"SETA",4)) {
782 cmd_seta(&cmdbuf[5]);
785 else if (!struncmp(cmdbuf,"ENT0",4)) {
786 cmd_ent0(&cmdbuf[5]);
789 else if (!struncmp(cmdbuf,"ENT3",4)) {
790 cmd_ent3(&cmdbuf[5]);
793 else if (!struncmp(cmdbuf,"RINF",4)) {
797 else if (!struncmp(cmdbuf,"DELE",4)) {
798 cmd_dele(&cmdbuf[5]);
801 else if (!struncmp(cmdbuf,"KILL",4)) {
802 cmd_kill(&cmdbuf[5]);
805 else if (!struncmp(cmdbuf,"CRE8",4)) {
806 cmd_cre8(&cmdbuf[5]);
809 else if (!struncmp(cmdbuf,"MOVE",4)) {
810 cmd_move(&cmdbuf[5]);
813 else if (!struncmp(cmdbuf,"FORG",4)) {
817 else if (!struncmp(cmdbuf,"MESG",4)) {
818 cmd_mesg(&cmdbuf[5]);
821 else if (!struncmp(cmdbuf,"EMSG",4)) {
822 cmd_emsg(&cmdbuf[5]);
825 else if (!struncmp(cmdbuf,"GNUR",4)) {
829 else if (!struncmp(cmdbuf,"GREG",4)) {
830 cmd_greg(&cmdbuf[5]);
833 else if (!struncmp(cmdbuf,"VALI",4)) {
834 cmd_vali(&cmdbuf[5]);
837 else if (!struncmp(cmdbuf,"EINF",4)) {
838 cmd_einf(&cmdbuf[5]);
841 else if (!struncmp(cmdbuf,"LIST",4)) {
845 else if (!struncmp(cmdbuf,"REGI",4)) {
849 else if (!struncmp(cmdbuf,"CHEK",4)) {
853 else if (!struncmp(cmdbuf,"DELF",4)) {
854 cmd_delf(&cmdbuf[5]);
857 else if (!struncmp(cmdbuf,"MOVF",4)) {
858 cmd_movf(&cmdbuf[5]);
861 else if (!struncmp(cmdbuf,"NETF",4)) {
862 cmd_netf(&cmdbuf[5]);
865 else if (!struncmp(cmdbuf,"RWHO",4)) {
869 else if (!struncmp(cmdbuf,"OPEN",4)) {
870 cmd_open(&cmdbuf[5]);
873 else if (!struncmp(cmdbuf,"CLOS",4)) {
877 else if (!struncmp(cmdbuf,"UOPN",4)) {
878 cmd_uopn(&cmdbuf[5]);
881 else if (!struncmp(cmdbuf,"UCLS",4)) {
882 cmd_ucls(&cmdbuf[5]);
885 else if (!struncmp(cmdbuf,"READ",4)) {
886 cmd_read(&cmdbuf[5]);
889 else if (!struncmp(cmdbuf,"WRIT",4)) {
890 cmd_writ(&cmdbuf[5]);
893 else if (!struncmp(cmdbuf,"QUSR",4)) {
894 cmd_qusr(&cmdbuf[5]);
897 else if (!struncmp(cmdbuf,"ECHO",4)) {
898 cmd_echo(&cmdbuf[5]);
901 else if (!struncmp(cmdbuf,"OIMG",4)) {
902 cmd_oimg(&cmdbuf[5]);
905 else if (!struncmp(cmdbuf,"MORE",4)) {
909 else if (!struncmp(cmdbuf,"NETP",4)) {
910 cmd_netp(&cmdbuf[5]);
913 else if (!struncmp(cmdbuf,"NDOP",4)) {
914 cmd_ndop(&cmdbuf[5]);
917 else if (!struncmp(cmdbuf,"NUOP",4)) {
918 cmd_nuop(&cmdbuf[5]);
921 else if (!struncmp(cmdbuf,"LFLR",4)) {
925 else if (!struncmp(cmdbuf,"CFLR",4)) {
926 cmd_cflr(&cmdbuf[5]);
929 else if (!struncmp(cmdbuf,"KFLR",4)) {
930 cmd_kflr(&cmdbuf[5]);
933 else if (!struncmp(cmdbuf,"EFLR",4)) {
934 cmd_eflr(&cmdbuf[5]);
937 else if (!struncmp(cmdbuf,"IDEN",4)) {
938 cmd_iden(&cmdbuf[5]);
941 else if (!struncmp(cmdbuf,"IPGM",4)) {
942 cmd_ipgm(&cmdbuf[5]);
945 else if (!struncmp(cmdbuf,"CHAT",4)) {
946 cmd_chat(&cmdbuf[5]);
949 else if (!struncmp(cmdbuf,"PEXP",4)) {
953 else if (!struncmp(cmdbuf,"SEXP",4)) {
954 cmd_sexp(&cmdbuf[5]);
957 else if (!struncmp(cmdbuf,"EBIO",4)) {
961 else if (!struncmp(cmdbuf,"RBIO",4)) {
962 cmd_rbio(&cmdbuf[5]);
965 else if (!struncmp(cmdbuf,"LBIO",4)) {
969 else if (!struncmp(cmdbuf,"STEL",4)) {
970 cmd_stel(&cmdbuf[5]);
973 else if (!struncmp(cmdbuf,"TERM",4)) {
974 cmd_term(&cmdbuf[5]);
977 else if (!struncmp(cmdbuf,"DOWN",4)) {
981 else if (!struncmp(cmdbuf,"SCDN",4)) {
982 cmd_scdn(&cmdbuf[5]);
985 else if (!struncmp(cmdbuf, "NSET", 4)) {
986 cmd_nset(&cmdbuf[5]);
989 else if (!struncmp(cmdbuf, "UIMG", 4)) {
990 cmd_uimg(&cmdbuf[5]);
992 else if (!struncmp(cmdbuf, "UCHG", 4)) {
993 cmd_uchg(&cmdbuf[5]);
995 else if (!struncmp(cmdbuf, "TIME", 4)) {
996 cmd_time(&cmdbuf[5]);
998 else if (!struncmp(cmdbuf, "HCHG", 4)) {
999 cmd_hchg(&cmdbuf[5]);
1001 else if (!struncmp(cmdbuf, "RCHG", 4)) {
1002 cmd_rchg(&cmdbuf[5]);
1005 cprintf("%d Unrecognized or unsupported command.\n",
1009 } while(struncmp(cmdbuf,"QUIT",4));
1011 cleanup(EXIT_NORMAL);