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);
54 * Various things that need to be initialized at startup
56 void master_startup() {
57 lprintf(7, "Opening databases\n");
60 lprintf(7, "Checking floor reference counts\n");
65 * Cleanup routine to be called when the server is shutting down.
67 void master_cleanup() {
69 /* Cancel all running sessions */
70 lprintf(7, "Cancelling running sessions...\n");
71 while (ContextList != NULL) {
72 kill_session(ContextList->cs_pid);
76 lprintf(7, "Closing databases\n");
79 /* Do system-dependent stuff */
80 sysdep_master_cleanup();
84 lprintf(3, "citserver: exiting.\n");
90 * Gracefully terminate the session and thread.
91 * (This is called as a cleanup handler by the thread library.)
95 struct ExpressMessage *emptr;
97 lprintf(9, "cleanup_stuff() called\n");
99 lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
102 rec_log(CL_TERMINATE,CC->curr_user);
104 lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
105 hook_end_session(CC->cs_pid);
106 syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
108 /* Deallocate any unsent express messages */
109 begin_critical_section(S_SESSION_TABLE);
110 while (CC->FirstExpressMessage != NULL) {
111 emptr = CC->FirstExpressMessage;
112 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
115 end_critical_section(S_SESSION_TABLE);
117 /* Now get rid of the session and context */
118 lprintf(7, "cleanup_stuff() is calling RemoveContext(%d)\n", CC->cs_pid);
121 /* While we still have an extra thread with no user attached to it,
122 * take the opportunity to do some housekeeping before exiting.
129 * set_wtmpsupp() - alter the session listing
131 void set_wtmpsupp(char *newtext)
133 strncpy(CC->cs_room,newtext,19);
135 time(&CC->cs_lastupdt);
136 hook_room_name(CC->cs_pid, CC->cs_room);
141 * cmd_info() - tell the client about this server
143 void cmd_info(void) {
144 cprintf("%d Server info:\n",LISTING_FOLLOWS);
145 cprintf("%d\n",CC->cs_pid);
146 cprintf("%s\n",config.c_nodename);
147 cprintf("%s\n",config.c_humannode);
148 cprintf("%s\n",config.c_fqdn);
149 cprintf("%s\n",CITADEL);
150 cprintf("%d\n",REV_LEVEL);
151 cprintf("%s\n",config.c_bbs_city);
152 cprintf("%s\n",config.c_sysadm);
153 cprintf("%d\n",SERVER_TYPE);
154 cprintf("%s\n",config.c_moreprompt);
155 cprintf("1\n"); /* 1 = yes, this system supports floors */
159 void cmd_rchg(char *newroomname)
161 if ((newroomname) && (newroomname[0]))
163 bzero(CC->fake_roomname, 20);
164 strncpy(CC->fake_roomname, newroomname, 19);
167 CC->fake_roomname[0] = '\0';
171 void cmd_hchg(char *newhostname)
173 if ((newhostname) && (newhostname[0]))
175 bzero(CC->fake_hostname, 25);
176 strncpy(CC->fake_hostname, newhostname, 24);
179 CC->fake_hostname[0] = '\0';
183 void cmd_uchg(char *newusername)
185 if (CC->usersupp.axlevel < 6)
187 cprintf("%d You must be an Aide to use UCHG.\n",
188 ERROR+HIGHER_ACCESS_REQUIRED);
191 if ((newusername) && (newusername[0]))
193 CC->cs_flags &= ~CS_STEALTH;
194 bzero(CC->fake_username, 32);
195 if (struncmp(newusername, CC->curr_user, strlen(CC->curr_user)))
196 strncpy(CC->fake_username, newusername, 31);
200 CC->fake_username[0] = '\0';
201 CC->cs_flags |= CS_STEALTH;
212 cprintf("%d|%ld\n", OK, tv);
216 * check a hostname against the public_clients file
218 int is_public_client(char *where)
223 if (!strucmp(where,"localhost")) return(1);
224 if (!strucmp(where,config.c_fqdn)) return(1);
226 fp = fopen("public_clients","r");
227 if (fp == NULL) return(0);
229 while (fgets(buf,256,fp)!=NULL) {
230 while (isspace((buf[strlen(buf)-1])))
231 buf[strlen(buf)-1] = 0;
232 if (!strucmp(buf,where)) {
244 * the client is identifying itself to the server
246 void cmd_iden(char *argbuf)
254 if (num_parms(argbuf)<4) {
255 cprintf("%d usage error\n",ERROR);
259 dev_code = extract_int(argbuf,0);
260 cli_code = extract_int(argbuf,1);
261 rev_level = extract_int(argbuf,2);
262 extract(desc,argbuf,3);
264 strcpy(from_host,config.c_fqdn);
265 if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
267 CC->cs_clientdev = dev_code;
268 CC->cs_clienttyp = cli_code;
269 CC->cs_clientver = rev_level;
270 strncpy(CC->cs_clientname,desc,31);
271 CC->cs_clientname[31] = 0;
273 if ((strlen(from_host)>0) &&
274 (is_public_client(CC->cs_host))) {
275 strncpy(CC->cs_host,from_host,24);
278 set_wtmpsupp(CC->quickroom.QRname);
280 syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
286 cprintf("%d Ok\n",OK);
291 * enter or exit "stealth mode"
293 void cmd_stel(char *cmdbuf)
297 requested_mode = extract_int(cmdbuf,0);
298 if (requested_mode !=0) requested_mode = 1;
300 if (!CC->logged_in) {
301 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
305 if (CC->usersupp.axlevel < 6) {
306 cprintf("%d You must be an Aide to use stealth mode.\n",
307 ERROR+HIGHER_ACCESS_REQUIRED);
311 if (CC->cs_flags & CS_STEALTH) {
312 if (requested_mode == 0)
313 CC->cs_flags = CC->cs_flags-CS_STEALTH;
316 if (requested_mode == 1)
317 CC->cs_flags = CC->cs_flags|CS_STEALTH;
320 set_wtmpsupp(CC->quickroom.QRname);
321 cprintf("%d Ok\n",OK);
328 * display system messages or help
330 void cmd_mesg(char *mname)
337 extract(buf,mname,0);
342 strcpy(dirs[0],"messages");
343 strcpy(dirs[1],"help");
344 mesg_locate(targ,buf,2,dirs);
349 if (strlen(targ)==0) {
350 cprintf("%d '%s' not found.\n",ERROR,mname);
354 mfp = fopen(targ,"r");
356 cprintf("%d Cannot open '%s': %s\n",
357 ERROR,targ,strerror(errno));
360 cprintf("%d %s\n",LISTING_FOLLOWS,buf);
362 while (fgets(buf,255,mfp)!=NULL) {
363 buf[strlen(buf)-1] = 0;
374 * enter system messages or help
376 void cmd_emsg(char *mname)
384 if (CC->usersupp.axlevel < 6) {
385 cprintf("%d You must be an Aide to edit system messages.\n",
386 ERROR+HIGHER_ACCESS_REQUIRED);
390 extract(buf,mname,0);
391 for (a=0; a<strlen(buf); ++a) { /* security measure */
392 if (buf[a] == '/') buf[a] = '.';
397 strcpy(dirs[0],"messages");
398 strcpy(dirs[1],"help");
399 mesg_locate(targ,buf,2,dirs);
403 if (strlen(targ)==0) {
404 sprintf(targ, "./help/%s", buf);
407 mfp = fopen(targ,"w");
409 cprintf("%d Cannot open '%s': %s\n",
410 ERROR,targ,strerror(errno));
413 cprintf("%d %s\n", SEND_LISTING, targ);
415 while (client_gets(buf), strcmp(buf, "000")) {
416 fprintf(mfp, "%s\n", buf);
426 void cmd_rwho(void) {
427 struct CitContext *cptr;
430 char un[40], room[40], host[40], flags[5];
432 aide = CC->usersupp.axlevel >= 6;
433 cprintf("%d\n",LISTING_FOLLOWS);
435 for (cptr = ContextList; cptr != NULL; cptr = cptr->next)
440 if (cptr->cs_flags & CS_POSTING)
445 if (cptr->fake_username[0])
447 strcpy(un, cptr->fake_username);
451 strcpy(un, cptr->curr_user);
453 if (cptr->fake_hostname[0])
455 strcpy(host, cptr->fake_hostname);
459 strcpy(host, cptr->cs_host);
461 if (cptr->fake_roomname[0])
463 strcpy(room, cptr->fake_roomname);
467 strcpy(room, cptr->cs_room);
470 if ((aide) && (spoofed))
473 if ((cptr->cs_flags & CS_STEALTH) && (aide))
476 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
478 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
479 cptr->cs_pid, un, room,
480 host, cptr->cs_clientname,
481 (long)(cptr->lastidle),
482 cptr->lastcmdname, flags);
484 if ((spoofed) && (aide))
486 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
487 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
488 cptr->cs_host, cptr->cs_clientname,
489 (long)(cptr->lastidle),
490 cptr->lastcmdname, flags);
499 * Terminate another running session
501 void cmd_term(char *cmdbuf)
504 struct CitContext *ccptr;
506 if (!CC->logged_in) {
507 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
511 if (CC->usersupp.axlevel < 6) {
512 cprintf("%d You must be an Aide to terminate sessions.\n",
513 ERROR+HIGHER_ACCESS_REQUIRED);
517 session_num = extract_int(cmdbuf, 0);
518 if (session_num == CC->cs_pid) {
519 cprintf("%d You can't kill your own session.\n", ERROR);
523 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
524 if (session_num == ccptr->cs_pid) {
525 kill_session(ccptr->cs_pid);
526 cprintf("%d Session terminated.\n", OK);
531 cprintf("%d No such session.\n", ERROR);
539 * get the paginator prompt
541 void cmd_more(void) {
542 cprintf("%d %s\n",OK,config.c_moreprompt);
548 void cmd_echo(char *etext)
550 cprintf("%d %s\n",OK,etext);
556 * identify as internal program
558 void cmd_ipgm(char *argbuf)
562 secret = extract_int(argbuf, 0);
563 if (secret == config.c_ipgm_secret) {
564 CC->internal_pgm = 1;
565 strcpy(CC->curr_user, "<internal program>");
566 CC->cs_flags = CC->cs_flags|CS_STEALTH;
567 cprintf("%d Authenticated as an internal program.\n",OK);
570 cprintf("%d Authentication failed.\n",ERROR);
576 * Shut down the server
578 void cmd_down(void) {
579 if (!CC->logged_in) {
580 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
584 if (CC->usersupp.axlevel < 6) {
585 cprintf("%d You must be an Aide to shut down the server.\n",
586 ERROR+HIGHER_ACCESS_REQUIRED);
590 cprintf("%d Shutting down server. Goodbye.\n", OK);
595 * Shut down the server
597 void cmd_scdn(char *argbuf)
601 if (!CC->logged_in) {
602 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
606 if (CC->usersupp.axlevel < 6) {
607 cprintf("%d You must be an Aide to schedule a shutdown.\n",
608 ERROR+HIGHER_ACCESS_REQUIRED);
612 new_state = extract_int(argbuf, 0);
613 if ((new_state == 0) || (new_state == 1)) {
614 ScheduledShutdown = new_state;
616 cprintf("%d %d\n", OK, ScheduledShutdown);
622 void *context_loop(struct CitContext *con)
628 * Wedge our way into the context table.
633 * Initialize some variables specific to our context.
637 CC->internal_pgm = 0;
638 CC->download_fp = NULL;
639 CC->upload_fp = NULL;
640 CC->cs_pid = con->client_socket; /* not necessarily portable */
641 CC->FirstExpressMessage = NULL;
644 strcpy(CC->lastcmdname, " ");
645 strcpy(CC->cs_clientname, "(unknown)");
646 strcpy(CC->curr_user,"");
647 strcpy(CC->net_node,"");
648 sprintf(CC->temp,"/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
649 strcpy(CC->cs_room, "");
650 strcpy(CC->cs_host, config.c_fqdn);
651 locate_host(CC->cs_host);
653 CC->upload_type = UPL_FILE;
656 session_num = session_count();
658 if ((config.c_maxsessions > 0)&&(session_num >= config.c_maxsessions))
661 if (CC->nologin==1) {
662 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
663 ERROR+MAX_SESSIONS_EXCEEDED,
664 config.c_nodename,config.c_maxsessions);
667 cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
670 lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
671 hook_start_session(CC->cs_pid);
675 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
676 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
677 hook_command_received(CC->cs_pid, cmdbuf);
680 * Let other clients see the last command we executed, but
681 * exclude NOOP because that would be boring.
683 if (struncmp(cmdbuf, "NOOP", 4)) {
684 strcpy(CC->lastcmdname, " ");
685 strncpy(CC->lastcmdname, cmdbuf, 4);
689 if ((struncmp(cmdbuf, "ENT0", 4)) && (struncmp(cmdbuf, "MESG", 4)) && (struncmp(cmdbuf, "MSGS", 4)))
691 CC->cs_flags &= ~CS_POSTING;
695 * This loop recognizes all server commands.
698 if (!struncmp(cmdbuf,"NOOP",4)) {
699 cprintf("%d%cok\n",OK,check_express());
702 else if (!struncmp(cmdbuf,"QUIT",4)) {
703 cprintf("%d Goodbye.\n",OK);
706 else if (!struncmp(cmdbuf,"LOUT",4)) {
707 if (CC->logged_in) logout(CC);
708 cprintf("%d logged out.\n",OK);
711 else if (!struncmp(cmdbuf,"USER",4)) {
712 cmd_user(&cmdbuf[5]);
715 else if (!struncmp(cmdbuf,"PASS",4)) {
716 cmd_pass(&cmdbuf[5]);
719 else if (!struncmp(cmdbuf,"NEWU",4)) {
720 cmd_newu(&cmdbuf[5]);
723 else if (!struncmp(cmdbuf,"SETP",4)) {
724 cmd_setp(&cmdbuf[5]);
727 else if (!struncmp(cmdbuf,"LRMS",4)) {
728 cmd_lrms(&cmdbuf[5]);
731 else if (!struncmp(cmdbuf,"LKRA",4)) {
732 cmd_lkra(&cmdbuf[5]);
735 else if (!struncmp(cmdbuf,"LKRN",4)) {
736 cmd_lkrn(&cmdbuf[5]);
739 else if (!struncmp(cmdbuf,"LKRO",4)) {
740 cmd_lkro(&cmdbuf[5]);
743 else if (!struncmp(cmdbuf,"LZRM",4)) {
744 cmd_lzrm(&cmdbuf[5]);
747 else if (!struncmp(cmdbuf,"GETU",4)) {
751 else if (!struncmp(cmdbuf,"SETU",4)) {
752 cmd_setu(&cmdbuf[5]);
755 else if (!struncmp(cmdbuf,"GOTO",4)) {
756 cmd_goto(&cmdbuf[5]);
759 else if (!struncmp(cmdbuf,"MSGS",4)) {
760 cmd_msgs(&cmdbuf[5]);
763 else if (!struncmp(cmdbuf,"WHOK",4)) {
767 else if (!struncmp(cmdbuf,"RDIR",4)) {
771 else if (!struncmp(cmdbuf,"MSG0",4)) {
772 cmd_msg0(&cmdbuf[5]);
775 else if (!struncmp(cmdbuf,"MSG2",4)) {
776 cmd_msg2(&cmdbuf[5]);
779 else if (!struncmp(cmdbuf,"MSG3",4)) {
780 cmd_msg3(&cmdbuf[5]);
783 else if (!struncmp(cmdbuf,"INFO",4)) {
787 else if (!struncmp(cmdbuf,"SLRP",4)) {
788 cmd_slrp(&cmdbuf[5]);
791 else if (!struncmp(cmdbuf,"INVT",4)) {
792 cmd_invt_kick(&cmdbuf[5],1);
795 else if (!struncmp(cmdbuf,"KICK",4)) {
796 cmd_invt_kick(&cmdbuf[5],0);
799 else if (!struncmp(cmdbuf,"GETR",4)) {
803 else if (!struncmp(cmdbuf,"SETR",4)) {
804 cmd_setr(&cmdbuf[5]);
807 else if (!struncmp(cmdbuf,"GETA",4)) {
811 else if (!struncmp(cmdbuf,"SETA",4)) {
812 cmd_seta(&cmdbuf[5]);
815 else if (!struncmp(cmdbuf,"ENT0",4)) {
816 cmd_ent0(&cmdbuf[5]);
819 else if (!struncmp(cmdbuf,"ENT3",4)) {
820 cmd_ent3(&cmdbuf[5]);
823 else if (!struncmp(cmdbuf,"RINF",4)) {
827 else if (!struncmp(cmdbuf,"DELE",4)) {
828 cmd_dele(&cmdbuf[5]);
831 else if (!struncmp(cmdbuf,"KILL",4)) {
832 cmd_kill(&cmdbuf[5]);
835 else if (!struncmp(cmdbuf,"CRE8",4)) {
836 cmd_cre8(&cmdbuf[5]);
839 else if (!struncmp(cmdbuf,"MOVE",4)) {
840 cmd_move(&cmdbuf[5]);
843 else if (!struncmp(cmdbuf,"FORG",4)) {
847 else if (!struncmp(cmdbuf,"MESG",4)) {
848 cmd_mesg(&cmdbuf[5]);
851 else if (!struncmp(cmdbuf,"EMSG",4)) {
852 cmd_emsg(&cmdbuf[5]);
855 else if (!struncmp(cmdbuf,"GNUR",4)) {
859 else if (!struncmp(cmdbuf,"GREG",4)) {
860 cmd_greg(&cmdbuf[5]);
863 else if (!struncmp(cmdbuf,"VALI",4)) {
864 cmd_vali(&cmdbuf[5]);
867 else if (!struncmp(cmdbuf,"EINF",4)) {
868 cmd_einf(&cmdbuf[5]);
871 else if (!struncmp(cmdbuf,"LIST",4)) {
875 else if (!struncmp(cmdbuf,"REGI",4)) {
879 else if (!struncmp(cmdbuf,"CHEK",4)) {
883 else if (!struncmp(cmdbuf,"DELF",4)) {
884 cmd_delf(&cmdbuf[5]);
887 else if (!struncmp(cmdbuf,"MOVF",4)) {
888 cmd_movf(&cmdbuf[5]);
891 else if (!struncmp(cmdbuf,"NETF",4)) {
892 cmd_netf(&cmdbuf[5]);
895 else if (!struncmp(cmdbuf,"RWHO",4)) {
899 else if (!struncmp(cmdbuf,"OPEN",4)) {
900 cmd_open(&cmdbuf[5]);
903 else if (!struncmp(cmdbuf,"CLOS",4)) {
907 else if (!struncmp(cmdbuf,"UOPN",4)) {
908 cmd_uopn(&cmdbuf[5]);
911 else if (!struncmp(cmdbuf,"UCLS",4)) {
912 cmd_ucls(&cmdbuf[5]);
915 else if (!struncmp(cmdbuf,"READ",4)) {
916 cmd_read(&cmdbuf[5]);
919 else if (!struncmp(cmdbuf,"WRIT",4)) {
920 cmd_writ(&cmdbuf[5]);
923 else if (!struncmp(cmdbuf,"QUSR",4)) {
924 cmd_qusr(&cmdbuf[5]);
927 else if (!struncmp(cmdbuf,"ECHO",4)) {
928 cmd_echo(&cmdbuf[5]);
931 else if (!struncmp(cmdbuf,"OIMG",4)) {
932 cmd_oimg(&cmdbuf[5]);
935 else if (!struncmp(cmdbuf,"MORE",4)) {
939 else if (!struncmp(cmdbuf,"NETP",4)) {
940 cmd_netp(&cmdbuf[5]);
943 else if (!struncmp(cmdbuf,"NDOP",4)) {
944 cmd_ndop(&cmdbuf[5]);
947 else if (!struncmp(cmdbuf,"NUOP",4)) {
948 cmd_nuop(&cmdbuf[5]);
951 else if (!struncmp(cmdbuf,"LFLR",4)) {
955 else if (!struncmp(cmdbuf,"CFLR",4)) {
956 cmd_cflr(&cmdbuf[5]);
959 else if (!struncmp(cmdbuf,"KFLR",4)) {
960 cmd_kflr(&cmdbuf[5]);
963 else if (!struncmp(cmdbuf,"EFLR",4)) {
964 cmd_eflr(&cmdbuf[5]);
967 else if (!struncmp(cmdbuf,"IDEN",4)) {
968 cmd_iden(&cmdbuf[5]);
971 else if (!struncmp(cmdbuf,"IPGM",4)) {
972 cmd_ipgm(&cmdbuf[5]);
975 else if (!struncmp(cmdbuf,"CHAT",4)) {
976 cmd_chat(&cmdbuf[5]);
979 else if (!struncmp(cmdbuf,"PEXP",4)) {
983 else if (!struncmp(cmdbuf,"SEXP",4)) {
984 cmd_sexp(&cmdbuf[5]);
987 else if (!struncmp(cmdbuf,"EBIO",4)) {
991 else if (!struncmp(cmdbuf,"RBIO",4)) {
992 cmd_rbio(&cmdbuf[5]);
995 else if (!struncmp(cmdbuf,"LBIO",4)) {
999 else if (!struncmp(cmdbuf,"STEL",4)) {
1000 cmd_stel(&cmdbuf[5]);
1003 else if (!struncmp(cmdbuf,"TERM",4)) {
1004 cmd_term(&cmdbuf[5]);
1007 else if (!struncmp(cmdbuf,"DOWN",4)) {
1011 else if (!struncmp(cmdbuf,"SCDN",4)) {
1012 cmd_scdn(&cmdbuf[5]);
1015 else if (!struncmp(cmdbuf, "NSET", 4)) {
1016 cmd_nset(&cmdbuf[5]);
1019 else if (!struncmp(cmdbuf, "UIMG", 4)) {
1020 cmd_uimg(&cmdbuf[5]);
1022 else if (!struncmp(cmdbuf, "UCHG", 4)) {
1023 cmd_uchg(&cmdbuf[5]);
1025 else if (!struncmp(cmdbuf, "TIME", 4)) {
1026 cmd_time(&cmdbuf[5]);
1028 else if (!struncmp(cmdbuf, "HCHG", 4)) {
1029 cmd_hchg(&cmdbuf[5]);
1031 else if (!struncmp(cmdbuf, "RCHG", 4)) {
1032 cmd_rchg(&cmdbuf[5]);
1035 cprintf("%d Unrecognized or unsupported command.\n",
1039 } while(struncmp(cmdbuf,"QUIT",4));
1041 cleanup(EXIT_NORMAL);