18 struct CitContext *ContextList = NULL;
19 int ScheduledShutdown = 0;
23 * record an entry in the call log
25 void rec_log(unsigned int lrtype, char *name)
27 struct calllog record;
32 record.CLflags = lrtype;
33 strncpy(record.CLfullname,name,29);
35 begin_critical_section(S_CALLLOG);
36 file=open("calllog.pos",O_RDWR);
38 read(file,&a,sizeof(int));
39 b=a; ++a; if (a>=CALLLOG) a=0;
41 write(file,&a,sizeof(int));
43 file2=open("calllog",O_RDWR);
44 lseek(file2,(long)(b*sizeof(struct calllog)),0);
45 write(file2,(char *)&record,sizeof(struct calllog));
49 end_critical_section(S_CALLLOG);
53 * Cleanup routine to be called when the server is shutting down.
55 void master_cleanup(void) {
57 /* Cancel all running sessions */
58 lprintf(7, "Cancelling running sessions...\n");
59 while (ContextList != NULL) {
60 kill_session(ContextList->cs_pid);
63 /* Do system-dependent stuff */
64 sysdep_master_cleanup();
68 lprintf(3, "citserver: exiting.\n");
74 * Gracefully terminate the session and thread.
75 * (This is called as a cleanup handler by the thread library.)
79 struct ExpressMessage *emptr;
81 lprintf(9, "cleanup_stuff() called\n");
83 lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
86 rec_log(CL_TERMINATE,CC->curr_user);
88 lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
89 hook_end_session(CC->cs_pid);
90 syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
92 /* Deallocate any unsent express messages */
93 begin_critical_section(S_SESSION_TABLE);
94 while (CC->FirstExpressMessage != NULL) {
95 emptr = CC->FirstExpressMessage;
96 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
99 end_critical_section(S_SESSION_TABLE);
101 /* Now get rid of the session and context */
102 lprintf(7, "cleanup_stuff() is calling RemoveContext(%d)\n", CC->cs_pid);
105 /* While we still have an extra thread with no user attached to it,
106 * take the opportunity to do some housekeeping before exiting.
113 * set_wtmpsupp() - alter the session listing
115 void set_wtmpsupp(char *newtext)
117 strncpy(CC->cs_room,newtext,19);
119 time(&CC->cs_lastupdt);
120 hook_room_name(CC->cs_pid, CC->cs_room);
125 * cmd_info() - tell the client about this server
127 void cmd_info(void) {
128 cprintf("%d Server info:\n",LISTING_FOLLOWS);
129 cprintf("%d\n",CC->cs_pid);
130 cprintf("%s\n",config.c_nodename);
131 cprintf("%s\n",config.c_humannode);
132 cprintf("%s\n",config.c_fqdn);
133 cprintf("%s\n",CITADEL);
134 cprintf("%d\n",REV_LEVEL);
135 cprintf("%s\n",config.c_bbs_city);
136 cprintf("%s\n",config.c_sysadm);
137 cprintf("%d\n",SERVER_TYPE);
138 cprintf("%s\n",config.c_moreprompt);
139 cprintf("1\n"); /* 1 = yes, this system supports floors */
143 void cmd_rchg(char *newroomname)
145 if ((newroomname) && (newroomname[0]))
147 bzero(CC->fake_roomname, 20);
148 strncpy(CC->fake_roomname, newroomname, 19);
151 CC->fake_roomname[0] = '\0';
155 void cmd_hchg(char *newhostname)
157 if ((newhostname) && (newhostname[0]))
159 bzero(CC->fake_hostname, 25);
160 strncpy(CC->fake_hostname, newhostname, 24);
163 CC->fake_hostname[0] = '\0';
167 void cmd_uchg(char *newusername)
169 if (CC->usersupp.axlevel < 6)
171 cprintf("%d You must be an Aide to use UCHG.\n",
172 ERROR+HIGHER_ACCESS_REQUIRED);
175 if ((newusername) && (newusername[0]))
177 CC->cs_flags &= ~CS_STEALTH;
178 bzero(CC->fake_username, 32);
179 if (struncmp(newusername, CC->curr_user, strlen(CC->curr_user)))
180 strncpy(CC->fake_username, newusername, 31);
184 CC->fake_username[0] = '\0';
185 CC->cs_flags |= CS_STEALTH;
196 cprintf("%d|%ld\n", OK, tv);
200 * check a hostname against the public_clients file
202 int is_public_client(char *where)
207 if (!strucmp(where,"localhost")) return(1);
208 if (!strucmp(where,config.c_fqdn)) return(1);
210 fp = fopen("public_clients","r");
211 if (fp == NULL) return(0);
213 while (fgets(buf,256,fp)!=NULL) {
214 while (isspace((buf[strlen(buf)-1])))
215 buf[strlen(buf)-1] = 0;
216 if (!strucmp(buf,where)) {
228 * the client is identifying itself to the server
230 void cmd_iden(char *argbuf)
238 if (num_parms(argbuf)<4) {
239 cprintf("%d usage error\n",ERROR);
243 dev_code = extract_int(argbuf,0);
244 cli_code = extract_int(argbuf,1);
245 rev_level = extract_int(argbuf,2);
246 extract(desc,argbuf,3);
248 strcpy(from_host,config.c_fqdn);
249 if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
251 CC->cs_clientdev = dev_code;
252 CC->cs_clienttyp = cli_code;
253 CC->cs_clientver = rev_level;
254 strncpy(CC->cs_clientname,desc,31);
255 CC->cs_clientname[31] = 0;
257 if ((strlen(from_host)>0) &&
258 (is_public_client(CC->cs_host))) {
259 strncpy(CC->cs_host,from_host,24);
262 set_wtmpsupp(CC->quickroom.QRname);
264 syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
270 cprintf("%d Ok\n",OK);
275 * enter or exit "stealth mode"
277 void cmd_stel(char *cmdbuf)
281 requested_mode = extract_int(cmdbuf,0);
282 if (requested_mode !=0) requested_mode = 1;
284 if (!CC->logged_in) {
285 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
289 if (CC->usersupp.axlevel < 6) {
290 cprintf("%d You must be an Aide to use stealth mode.\n",
291 ERROR+HIGHER_ACCESS_REQUIRED);
295 if (CC->cs_flags & CS_STEALTH) {
296 if (requested_mode == 0)
297 CC->cs_flags = CC->cs_flags-CS_STEALTH;
300 if (requested_mode == 1)
301 CC->cs_flags = CC->cs_flags|CS_STEALTH;
304 set_wtmpsupp(CC->quickroom.QRname);
305 cprintf("%d Ok\n",OK);
312 * display system messages or help
314 void cmd_mesg(char *mname)
321 extract(buf,mname,0);
326 strcpy(dirs[0],"messages");
327 strcpy(dirs[1],"help");
328 mesg_locate(targ,buf,2,dirs);
333 if (strlen(targ)==0) {
334 cprintf("%d '%s' not found.\n",ERROR,mname);
338 mfp = fopen(targ,"r");
340 cprintf("%d Cannot open '%s': %s\n",
341 ERROR,targ,strerror(errno));
344 cprintf("%d %s\n",LISTING_FOLLOWS,buf);
346 while (fgets(buf,255,mfp)!=NULL) {
347 buf[strlen(buf)-1] = 0;
358 * enter system messages or help
360 void cmd_emsg(char *mname)
368 if (CC->usersupp.axlevel < 6) {
369 cprintf("%d You must be an Aide to edit system messages.\n",
370 ERROR+HIGHER_ACCESS_REQUIRED);
374 extract(buf,mname,0);
375 for (a=0; a<strlen(buf); ++a) { /* security measure */
376 if (buf[a] == '/') buf[a] = '.';
381 strcpy(dirs[0],"messages");
382 strcpy(dirs[1],"help");
383 mesg_locate(targ,buf,2,dirs);
387 if (strlen(targ)==0) {
388 sprintf(targ, "./help/%s", buf);
391 mfp = fopen(targ,"w");
393 cprintf("%d Cannot open '%s': %s\n",
394 ERROR,targ,strerror(errno));
397 cprintf("%d %s\n", SEND_LISTING, targ);
399 while (client_gets(buf), strcmp(buf, "000")) {
400 fprintf(mfp, "%s\n", buf);
410 void cmd_rwho(void) {
411 struct CitContext *cptr;
414 char un[40], room[40], host[40], flags[5];
416 aide = CC->usersupp.axlevel >= 6;
417 cprintf("%d\n",LISTING_FOLLOWS);
419 for (cptr = ContextList; cptr != NULL; cptr = cptr->next)
424 if (cptr->cs_flags & CS_POSTING)
429 if (cptr->fake_username[0])
431 strcpy(un, cptr->fake_username);
435 strcpy(un, cptr->curr_user);
437 if (cptr->fake_hostname[0])
439 strcpy(host, cptr->fake_hostname);
443 strcpy(host, cptr->cs_host);
445 if (cptr->fake_roomname[0])
447 strcpy(room, cptr->fake_roomname);
451 strcpy(room, cptr->cs_room);
454 if ((aide) && (spoofed))
457 if ((cptr->cs_flags & CS_STEALTH) && (aide))
460 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
462 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
463 cptr->cs_pid, un, room,
464 host, cptr->cs_clientname,
465 (long)(cptr->lastidle),
466 cptr->lastcmdname, flags);
468 if ((spoofed) && (aide))
470 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
471 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
472 cptr->cs_host, cptr->cs_clientname,
473 (long)(cptr->lastidle),
474 cptr->lastcmdname, flags);
483 * Terminate another running session
485 void cmd_term(char *cmdbuf)
488 struct CitContext *ccptr;
490 if (!CC->logged_in) {
491 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
495 if (CC->usersupp.axlevel < 6) {
496 cprintf("%d You must be an Aide to terminate sessions.\n",
497 ERROR+HIGHER_ACCESS_REQUIRED);
501 session_num = extract_int(cmdbuf, 0);
502 if (session_num == CC->cs_pid) {
503 cprintf("%d You can't kill your own session.\n", ERROR);
507 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
508 if (session_num == ccptr->cs_pid) {
509 kill_session(ccptr->cs_pid);
510 cprintf("%d Session terminated.\n", OK);
515 cprintf("%d No such session.\n", ERROR);
523 * get the paginator prompt
525 void cmd_more(void) {
526 cprintf("%d %s\n",OK,config.c_moreprompt);
532 void cmd_echo(char *etext)
534 cprintf("%d %s\n",OK,etext);
540 * identify as internal program
542 void cmd_ipgm(char *argbuf)
546 secret = extract_int(argbuf, 0);
547 if (secret == config.c_ipgm_secret) {
548 CC->internal_pgm = 1;
549 strcpy(CC->curr_user, "<internal program>");
550 CC->cs_flags = CC->cs_flags|CS_STEALTH;
551 cprintf("%d Authenticated as an internal program.\n",OK);
554 cprintf("%d Authentication failed.\n",ERROR);
560 * Shut down the server
562 void cmd_down(void) {
563 if (!CC->logged_in) {
564 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
568 if (CC->usersupp.axlevel < 6) {
569 cprintf("%d You must be an Aide to shut down the server.\n",
570 ERROR+HIGHER_ACCESS_REQUIRED);
574 cprintf("%d Shutting down server. Goodbye.\n", OK);
579 * Shut down the server
581 void cmd_scdn(char *argbuf)
585 if (!CC->logged_in) {
586 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
590 if (CC->usersupp.axlevel < 6) {
591 cprintf("%d You must be an Aide to schedule a shutdown.\n",
592 ERROR+HIGHER_ACCESS_REQUIRED);
596 new_state = extract_int(argbuf, 0);
597 if ((new_state == 0) || (new_state == 1)) {
598 ScheduledShutdown = new_state;
600 cprintf("%d %d\n", OK, ScheduledShutdown);
606 void *context_loop(struct CitContext *con)
612 * Wedge our way into the context table.
617 * Initialize some variables specific to our context.
621 CC->internal_pgm = 0;
622 CC->download_fp = NULL;
623 CC->upload_fp = NULL;
624 CC->cs_pid = con->client_socket; /* not necessarily portable */
625 CC->FirstExpressMessage = NULL;
628 strcpy(CC->lastcmdname, " ");
629 strcpy(CC->cs_clientname, "(unknown)");
630 strcpy(CC->curr_user,"");
631 strcpy(CC->net_node,"");
632 sprintf(CC->temp,"/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
633 strcpy(CC->cs_room, "");
634 strcpy(CC->cs_host, config.c_fqdn);
635 locate_host(CC->cs_host);
637 CC->upload_type = UPL_FILE;
640 session_num = session_count();
642 if ((config.c_maxsessions > 0)&&(session_num >= config.c_maxsessions))
645 if (CC->nologin==1) {
646 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
647 ERROR+MAX_SESSIONS_EXCEEDED,
648 config.c_nodename,config.c_maxsessions);
651 cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
654 lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
655 hook_start_session(CC->cs_pid);
659 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
660 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
661 hook_command_received(CC->cs_pid, cmdbuf);
664 * Let other clients see the last command we executed, but
665 * exclude NOOP because that would be boring.
667 if (struncmp(cmdbuf, "NOOP", 4)) {
668 strcpy(CC->lastcmdname, " ");
669 strncpy(CC->lastcmdname, cmdbuf, 4);
673 if ((struncmp(cmdbuf, "ENT0", 4)) && (struncmp(cmdbuf, "MESG", 4)) && (struncmp(cmdbuf, "MSGS", 4)))
675 CC->cs_flags &= ~CS_POSTING;
679 * This loop recognizes all server commands.
682 if (!struncmp(cmdbuf,"NOOP",4)) {
683 cprintf("%d%cok\n",OK,check_express());
686 else if (!struncmp(cmdbuf,"QUIT",4)) {
687 cprintf("%d Goodbye.\n",OK);
690 else if (!struncmp(cmdbuf,"LOUT",4)) {
691 if (CC->logged_in) logout(CC);
692 cprintf("%d logged out.\n",OK);
695 else if (!struncmp(cmdbuf,"USER",4)) {
696 cmd_user(&cmdbuf[5]);
699 else if (!struncmp(cmdbuf,"PASS",4)) {
700 cmd_pass(&cmdbuf[5]);
703 else if (!struncmp(cmdbuf,"NEWU",4)) {
704 cmd_newu(&cmdbuf[5]);
707 else if (!struncmp(cmdbuf,"SETP",4)) {
708 cmd_setp(&cmdbuf[5]);
711 else if (!struncmp(cmdbuf,"LRMS",4)) {
712 cmd_lrms(&cmdbuf[5]);
715 else if (!struncmp(cmdbuf,"LKRA",4)) {
716 cmd_lkra(&cmdbuf[5]);
719 else if (!struncmp(cmdbuf,"LKRN",4)) {
720 cmd_lkrn(&cmdbuf[5]);
723 else if (!struncmp(cmdbuf,"LKRO",4)) {
724 cmd_lkro(&cmdbuf[5]);
727 else if (!struncmp(cmdbuf,"LZRM",4)) {
728 cmd_lzrm(&cmdbuf[5]);
731 else if (!struncmp(cmdbuf,"GETU",4)) {
735 else if (!struncmp(cmdbuf,"SETU",4)) {
736 cmd_setu(&cmdbuf[5]);
739 else if (!struncmp(cmdbuf,"GOTO",4)) {
740 cmd_goto(&cmdbuf[5]);
743 else if (!struncmp(cmdbuf,"MSGS",4)) {
744 cmd_msgs(&cmdbuf[5]);
747 else if (!struncmp(cmdbuf,"WHOK",4)) {
751 else if (!struncmp(cmdbuf,"RDIR",4)) {
755 else if (!struncmp(cmdbuf,"MSG0",4)) {
756 cmd_msg0(&cmdbuf[5]);
759 else if (!struncmp(cmdbuf,"MSG2",4)) {
760 cmd_msg2(&cmdbuf[5]);
763 else if (!struncmp(cmdbuf,"MSG3",4)) {
764 cmd_msg3(&cmdbuf[5]);
767 else if (!struncmp(cmdbuf,"INFO",4)) {
771 else if (!struncmp(cmdbuf,"SLRP",4)) {
772 cmd_slrp(&cmdbuf[5]);
775 else if (!struncmp(cmdbuf,"INVT",4)) {
776 cmd_invt_kick(&cmdbuf[5],1);
779 else if (!struncmp(cmdbuf,"KICK",4)) {
780 cmd_invt_kick(&cmdbuf[5],0);
783 else if (!struncmp(cmdbuf,"GETR",4)) {
787 else if (!struncmp(cmdbuf,"SETR",4)) {
788 cmd_setr(&cmdbuf[5]);
791 else if (!struncmp(cmdbuf,"GETA",4)) {
795 else if (!struncmp(cmdbuf,"SETA",4)) {
796 cmd_seta(&cmdbuf[5]);
799 else if (!struncmp(cmdbuf,"ENT0",4)) {
800 cmd_ent0(&cmdbuf[5]);
803 else if (!struncmp(cmdbuf,"ENT3",4)) {
804 cmd_ent3(&cmdbuf[5]);
807 else if (!struncmp(cmdbuf,"RINF",4)) {
811 else if (!struncmp(cmdbuf,"DELE",4)) {
812 cmd_dele(&cmdbuf[5]);
815 else if (!struncmp(cmdbuf,"KILL",4)) {
816 cmd_kill(&cmdbuf[5]);
819 else if (!struncmp(cmdbuf,"CRE8",4)) {
820 cmd_cre8(&cmdbuf[5]);
823 else if (!struncmp(cmdbuf,"MOVE",4)) {
824 cmd_move(&cmdbuf[5]);
827 else if (!struncmp(cmdbuf,"FORG",4)) {
831 else if (!struncmp(cmdbuf,"MESG",4)) {
832 cmd_mesg(&cmdbuf[5]);
835 else if (!struncmp(cmdbuf,"EMSG",4)) {
836 cmd_emsg(&cmdbuf[5]);
839 else if (!struncmp(cmdbuf,"GNUR",4)) {
843 else if (!struncmp(cmdbuf,"GREG",4)) {
844 cmd_greg(&cmdbuf[5]);
847 else if (!struncmp(cmdbuf,"VALI",4)) {
848 cmd_vali(&cmdbuf[5]);
851 else if (!struncmp(cmdbuf,"EINF",4)) {
852 cmd_einf(&cmdbuf[5]);
855 else if (!struncmp(cmdbuf,"LIST",4)) {
859 else if (!struncmp(cmdbuf,"REGI",4)) {
863 else if (!struncmp(cmdbuf,"CHEK",4)) {
867 else if (!struncmp(cmdbuf,"DELF",4)) {
868 cmd_delf(&cmdbuf[5]);
871 else if (!struncmp(cmdbuf,"MOVF",4)) {
872 cmd_movf(&cmdbuf[5]);
875 else if (!struncmp(cmdbuf,"NETF",4)) {
876 cmd_netf(&cmdbuf[5]);
879 else if (!struncmp(cmdbuf,"RWHO",4)) {
883 else if (!struncmp(cmdbuf,"OPEN",4)) {
884 cmd_open(&cmdbuf[5]);
887 else if (!struncmp(cmdbuf,"CLOS",4)) {
891 else if (!struncmp(cmdbuf,"UOPN",4)) {
892 cmd_uopn(&cmdbuf[5]);
895 else if (!struncmp(cmdbuf,"UCLS",4)) {
896 cmd_ucls(&cmdbuf[5]);
899 else if (!struncmp(cmdbuf,"READ",4)) {
900 cmd_read(&cmdbuf[5]);
903 else if (!struncmp(cmdbuf,"WRIT",4)) {
904 cmd_writ(&cmdbuf[5]);
907 else if (!struncmp(cmdbuf,"QUSR",4)) {
908 cmd_qusr(&cmdbuf[5]);
911 else if (!struncmp(cmdbuf,"ECHO",4)) {
912 cmd_echo(&cmdbuf[5]);
915 else if (!struncmp(cmdbuf,"OIMG",4)) {
916 cmd_oimg(&cmdbuf[5]);
919 else if (!struncmp(cmdbuf,"MORE",4)) {
923 else if (!struncmp(cmdbuf,"NETP",4)) {
924 cmd_netp(&cmdbuf[5]);
927 else if (!struncmp(cmdbuf,"NDOP",4)) {
928 cmd_ndop(&cmdbuf[5]);
931 else if (!struncmp(cmdbuf,"NUOP",4)) {
932 cmd_nuop(&cmdbuf[5]);
935 else if (!struncmp(cmdbuf,"LFLR",4)) {
939 else if (!struncmp(cmdbuf,"CFLR",4)) {
940 cmd_cflr(&cmdbuf[5]);
943 else if (!struncmp(cmdbuf,"KFLR",4)) {
944 cmd_kflr(&cmdbuf[5]);
947 else if (!struncmp(cmdbuf,"EFLR",4)) {
948 cmd_eflr(&cmdbuf[5]);
951 else if (!struncmp(cmdbuf,"IDEN",4)) {
952 cmd_iden(&cmdbuf[5]);
955 else if (!struncmp(cmdbuf,"IPGM",4)) {
956 cmd_ipgm(&cmdbuf[5]);
959 else if (!struncmp(cmdbuf,"CHAT",4)) {
960 cmd_chat(&cmdbuf[5]);
963 else if (!struncmp(cmdbuf,"PEXP",4)) {
967 else if (!struncmp(cmdbuf,"SEXP",4)) {
968 cmd_sexp(&cmdbuf[5]);
971 else if (!struncmp(cmdbuf,"EBIO",4)) {
975 else if (!struncmp(cmdbuf,"RBIO",4)) {
976 cmd_rbio(&cmdbuf[5]);
979 else if (!struncmp(cmdbuf,"LBIO",4)) {
983 else if (!struncmp(cmdbuf,"STEL",4)) {
984 cmd_stel(&cmdbuf[5]);
987 else if (!struncmp(cmdbuf,"TERM",4)) {
988 cmd_term(&cmdbuf[5]);
991 else if (!struncmp(cmdbuf,"DOWN",4)) {
995 else if (!struncmp(cmdbuf,"SCDN",4)) {
996 cmd_scdn(&cmdbuf[5]);
999 else if (!struncmp(cmdbuf, "NSET", 4)) {
1000 cmd_nset(&cmdbuf[5]);
1003 else if (!struncmp(cmdbuf, "UIMG", 4)) {
1004 cmd_uimg(&cmdbuf[5]);
1006 else if (!struncmp(cmdbuf, "UCHG", 4)) {
1007 cmd_uchg(&cmdbuf[5]);
1009 else if (!struncmp(cmdbuf, "TIME", 4)) {
1010 cmd_time(&cmdbuf[5]);
1012 else if (!struncmp(cmdbuf, "HCHG", 4)) {
1013 cmd_hchg(&cmdbuf[5]);
1015 else if (!struncmp(cmdbuf, "RCHG", 4)) {
1016 cmd_rchg(&cmdbuf[5]);
1019 cprintf("%d Unrecognized or unsupported command.\n",
1023 } while(struncmp(cmdbuf,"QUIT",4));
1025 cleanup(EXIT_NORMAL);