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);
644 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
645 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
646 hook_command_received(CC->cs_pid, cmdbuf);
649 * Let other clients see the last command we executed, but
650 * exclude NOOP because that would be boring.
652 if (struncmp(cmdbuf, "NOOP", 4)) {
653 strcpy(CC->lastcmdname, " ");
654 strncpy(CC->lastcmdname, cmdbuf, 4);
658 if ((struncmp(cmdbuf, "ENT0", 4)) && (struncmp(cmdbuf, "MESG", 4)) && (struncmp(cmdbuf, "MSGS", 4)))
660 CC->cs_flags &= ~CS_POSTING;
664 * This loop recognizes all server commands.
667 if (!struncmp(cmdbuf,"NOOP",4)) {
668 cprintf("%d%cok\n",OK,check_express());
671 else if (!struncmp(cmdbuf,"QUIT",4)) {
672 cprintf("%d Goodbye.\n",OK);
675 else if (!struncmp(cmdbuf,"LOUT",4)) {
676 if (CC->logged_in) logout(CC);
677 cprintf("%d logged out.\n",OK);
680 else if (!struncmp(cmdbuf,"USER",4)) {
681 cmd_user(&cmdbuf[5]);
684 else if (!struncmp(cmdbuf,"PASS",4)) {
685 cmd_pass(&cmdbuf[5]);
688 else if (!struncmp(cmdbuf,"NEWU",4)) {
689 cmd_newu(&cmdbuf[5]);
692 else if (!struncmp(cmdbuf,"SETP",4)) {
693 cmd_setp(&cmdbuf[5]);
696 else if (!struncmp(cmdbuf,"LRMS",4)) {
697 cmd_lrms(&cmdbuf[5]);
700 else if (!struncmp(cmdbuf,"LKRA",4)) {
701 cmd_lkra(&cmdbuf[5]);
704 else if (!struncmp(cmdbuf,"LKRN",4)) {
705 cmd_lkrn(&cmdbuf[5]);
708 else if (!struncmp(cmdbuf,"LKRO",4)) {
709 cmd_lkro(&cmdbuf[5]);
712 else if (!struncmp(cmdbuf,"LZRM",4)) {
713 cmd_lzrm(&cmdbuf[5]);
716 else if (!struncmp(cmdbuf,"GETU",4)) {
720 else if (!struncmp(cmdbuf,"SETU",4)) {
721 cmd_setu(&cmdbuf[5]);
724 else if (!struncmp(cmdbuf,"GOTO",4)) {
725 cmd_goto(&cmdbuf[5]);
728 else if (!struncmp(cmdbuf,"MSGS",4)) {
729 cmd_msgs(&cmdbuf[5]);
732 else if (!struncmp(cmdbuf,"WHOK",4)) {
736 else if (!struncmp(cmdbuf,"RDIR",4)) {
740 else if (!struncmp(cmdbuf,"MSG0",4)) {
741 cmd_msg0(&cmdbuf[5]);
744 else if (!struncmp(cmdbuf,"MSG2",4)) {
745 cmd_msg2(&cmdbuf[5]);
748 else if (!struncmp(cmdbuf,"MSG3",4)) {
749 cmd_msg3(&cmdbuf[5]);
752 else if (!struncmp(cmdbuf,"INFO",4)) {
756 else if (!struncmp(cmdbuf,"SLRP",4)) {
757 cmd_slrp(&cmdbuf[5]);
760 else if (!struncmp(cmdbuf,"INVT",4)) {
761 cmd_invt_kick(&cmdbuf[5],1);
764 else if (!struncmp(cmdbuf,"KICK",4)) {
765 cmd_invt_kick(&cmdbuf[5],0);
768 else if (!struncmp(cmdbuf,"GETR",4)) {
772 else if (!struncmp(cmdbuf,"SETR",4)) {
773 cmd_setr(&cmdbuf[5]);
776 else if (!struncmp(cmdbuf,"GETA",4)) {
780 else if (!struncmp(cmdbuf,"SETA",4)) {
781 cmd_seta(&cmdbuf[5]);
784 else if (!struncmp(cmdbuf,"ENT0",4)) {
785 cmd_ent0(&cmdbuf[5]);
788 else if (!struncmp(cmdbuf,"ENT3",4)) {
789 cmd_ent3(&cmdbuf[5]);
792 else if (!struncmp(cmdbuf,"RINF",4)) {
796 else if (!struncmp(cmdbuf,"DELE",4)) {
797 cmd_dele(&cmdbuf[5]);
800 else if (!struncmp(cmdbuf,"KILL",4)) {
801 cmd_kill(&cmdbuf[5]);
804 else if (!struncmp(cmdbuf,"CRE8",4)) {
805 cmd_cre8(&cmdbuf[5]);
808 else if (!struncmp(cmdbuf,"MOVE",4)) {
809 cmd_move(&cmdbuf[5]);
812 else if (!struncmp(cmdbuf,"FORG",4)) {
816 else if (!struncmp(cmdbuf,"MESG",4)) {
817 cmd_mesg(&cmdbuf[5]);
820 else if (!struncmp(cmdbuf,"EMSG",4)) {
821 cmd_emsg(&cmdbuf[5]);
824 else if (!struncmp(cmdbuf,"GNUR",4)) {
828 else if (!struncmp(cmdbuf,"GREG",4)) {
829 cmd_greg(&cmdbuf[5]);
832 else if (!struncmp(cmdbuf,"VALI",4)) {
833 cmd_vali(&cmdbuf[5]);
836 else if (!struncmp(cmdbuf,"EINF",4)) {
837 cmd_einf(&cmdbuf[5]);
840 else if (!struncmp(cmdbuf,"LIST",4)) {
844 else if (!struncmp(cmdbuf,"REGI",4)) {
848 else if (!struncmp(cmdbuf,"CHEK",4)) {
852 else if (!struncmp(cmdbuf,"DELF",4)) {
853 cmd_delf(&cmdbuf[5]);
856 else if (!struncmp(cmdbuf,"MOVF",4)) {
857 cmd_movf(&cmdbuf[5]);
860 else if (!struncmp(cmdbuf,"NETF",4)) {
861 cmd_netf(&cmdbuf[5]);
864 else if (!struncmp(cmdbuf,"RWHO",4)) {
868 else if (!struncmp(cmdbuf,"OPEN",4)) {
869 cmd_open(&cmdbuf[5]);
872 else if (!struncmp(cmdbuf,"CLOS",4)) {
876 else if (!struncmp(cmdbuf,"UOPN",4)) {
877 cmd_uopn(&cmdbuf[5]);
880 else if (!struncmp(cmdbuf,"UCLS",4)) {
881 cmd_ucls(&cmdbuf[5]);
884 else if (!struncmp(cmdbuf,"READ",4)) {
885 cmd_read(&cmdbuf[5]);
888 else if (!struncmp(cmdbuf,"WRIT",4)) {
889 cmd_writ(&cmdbuf[5]);
892 else if (!struncmp(cmdbuf,"QUSR",4)) {
893 cmd_qusr(&cmdbuf[5]);
896 else if (!struncmp(cmdbuf,"ECHO",4)) {
897 cmd_echo(&cmdbuf[5]);
900 else if (!struncmp(cmdbuf,"OIMG",4)) {
901 cmd_oimg(&cmdbuf[5]);
904 else if (!struncmp(cmdbuf,"MORE",4)) {
908 else if (!struncmp(cmdbuf,"NETP",4)) {
909 cmd_netp(&cmdbuf[5]);
912 else if (!struncmp(cmdbuf,"NDOP",4)) {
913 cmd_ndop(&cmdbuf[5]);
916 else if (!struncmp(cmdbuf,"NUOP",4)) {
917 cmd_nuop(&cmdbuf[5]);
920 else if (!struncmp(cmdbuf,"LFLR",4)) {
924 else if (!struncmp(cmdbuf,"CFLR",4)) {
925 cmd_cflr(&cmdbuf[5]);
928 else if (!struncmp(cmdbuf,"KFLR",4)) {
929 cmd_kflr(&cmdbuf[5]);
932 else if (!struncmp(cmdbuf,"EFLR",4)) {
933 cmd_eflr(&cmdbuf[5]);
936 else if (!struncmp(cmdbuf,"IDEN",4)) {
937 cmd_iden(&cmdbuf[5]);
940 else if (!struncmp(cmdbuf,"IPGM",4)) {
941 cmd_ipgm(&cmdbuf[5]);
944 else if (!struncmp(cmdbuf,"CHAT",4)) {
945 cmd_chat(&cmdbuf[5]);
948 else if (!struncmp(cmdbuf,"PEXP",4)) {
952 else if (!struncmp(cmdbuf,"SEXP",4)) {
953 cmd_sexp(&cmdbuf[5]);
956 else if (!struncmp(cmdbuf,"EBIO",4)) {
960 else if (!struncmp(cmdbuf,"RBIO",4)) {
961 cmd_rbio(&cmdbuf[5]);
964 else if (!struncmp(cmdbuf,"LBIO",4)) {
968 else if (!struncmp(cmdbuf,"STEL",4)) {
969 cmd_stel(&cmdbuf[5]);
972 else if (!struncmp(cmdbuf,"TERM",4)) {
973 cmd_term(&cmdbuf[5]);
976 else if (!struncmp(cmdbuf,"DOWN",4)) {
980 else if (!struncmp(cmdbuf,"SCDN",4)) {
981 cmd_scdn(&cmdbuf[5]);
984 else if (!struncmp(cmdbuf, "NSET", 4)) {
985 cmd_nset(&cmdbuf[5]);
988 else if (!struncmp(cmdbuf, "UIMG", 4)) {
989 cmd_uimg(&cmdbuf[5]);
991 else if (!struncmp(cmdbuf, "UCHG", 4)) {
992 cmd_uchg(&cmdbuf[5]);
994 else if (!struncmp(cmdbuf, "TIME", 4)) {
995 cmd_time(&cmdbuf[5]);
997 else if (!struncmp(cmdbuf, "HCHG", 4)) {
998 cmd_hchg(&cmdbuf[5]);
1000 else if (!struncmp(cmdbuf, "RCHG", 4)) {
1001 cmd_rchg(&cmdbuf[5]);
1004 cprintf("%d Unrecognized or unsupported command.\n",
1008 } while(struncmp(cmdbuf,"QUIT",4));
1010 cleanup(EXIT_NORMAL);