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.)
62 * All NON-system-dependent stuff is done in this function.
63 * System-dependent session/thread cleanup is in cleanup() in sysdep.c
67 struct ExpressMessage *emptr;
69 lprintf(9, "cleanup_stuff() called\n");
71 lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
74 rec_log(CL_TERMINATE,CC->curr_user);
76 lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
77 hook_end_session(CC->cs_pid);
78 syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
80 /* Deallocate any unsent express messages */
81 begin_critical_section(S_SESSION_TABLE);
82 while (CC->FirstExpressMessage != NULL) {
83 emptr = CC->FirstExpressMessage;
84 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
87 end_critical_section(S_SESSION_TABLE);
89 /* Deallocate any message list we might have in memory */
90 if (CC->msglist != NULL) free(CC->msglist);
92 /* Now get rid of the session and context */
93 lprintf(7, "cleanup_stuff() is calling RemoveContext(%d)\n", CC->cs_pid);
96 /* While we still have an extra thread with no user attached to it,
97 * take the opportunity to do some housekeeping before exiting.
104 * set_wtmpsupp() - alter the session listing
106 void set_wtmpsupp(char *newtext)
108 strncpy(CC->cs_room,newtext,19);
110 time(&CC->cs_lastupdt);
111 hook_room_name(CC->cs_pid, CC->cs_room);
116 * cmd_info() - tell the client about this server
118 void cmd_info(void) {
119 cprintf("%d Server info:\n",LISTING_FOLLOWS);
120 cprintf("%d\n",CC->cs_pid);
121 cprintf("%s\n",config.c_nodename);
122 cprintf("%s\n",config.c_humannode);
123 cprintf("%s\n",config.c_fqdn);
124 cprintf("%s\n",CITADEL);
125 cprintf("%d\n",REV_LEVEL);
126 cprintf("%s\n",config.c_bbs_city);
127 cprintf("%s\n",config.c_sysadm);
128 cprintf("%d\n",SERVER_TYPE);
129 cprintf("%s\n",config.c_moreprompt);
130 cprintf("1\n"); /* 1 = yes, this system supports floors */
134 void cmd_rchg(char *newroomname)
136 if ((newroomname) && (newroomname[0]))
138 bzero(CC->fake_roomname, 20);
139 strncpy(CC->fake_roomname, newroomname, 19);
142 CC->fake_roomname[0] = '\0';
146 void cmd_hchg(char *newhostname)
148 if ((newhostname) && (newhostname[0]))
150 bzero(CC->fake_hostname, 25);
151 strncpy(CC->fake_hostname, newhostname, 24);
154 CC->fake_hostname[0] = '\0';
158 void cmd_uchg(char *newusername)
160 if (CC->usersupp.axlevel < 6)
162 cprintf("%d You must be an Aide to use UCHG.\n",
163 ERROR+HIGHER_ACCESS_REQUIRED);
166 if ((newusername) && (newusername[0]))
168 CC->cs_flags &= ~CS_STEALTH;
169 bzero(CC->fake_username, 32);
170 if (struncmp(newusername, CC->curr_user, strlen(CC->curr_user)))
171 strncpy(CC->fake_username, newusername, 31);
175 CC->fake_username[0] = '\0';
176 CC->cs_flags |= CS_STEALTH;
187 cprintf("%d|%ld\n", OK, tv);
191 * check a hostname against the public_clients file
193 int is_public_client(char *where)
198 if (!strucmp(where,"localhost")) return(1);
199 if (!strucmp(where,config.c_fqdn)) return(1);
201 fp = fopen("public_clients","r");
202 if (fp == NULL) return(0);
204 while (fgets(buf,256,fp)!=NULL) {
205 while (isspace((buf[strlen(buf)-1])))
206 buf[strlen(buf)-1] = 0;
207 if (!strucmp(buf,where)) {
219 * the client is identifying itself to the server
221 void cmd_iden(char *argbuf)
229 if (num_parms(argbuf)<4) {
230 cprintf("%d usage error\n",ERROR);
234 dev_code = extract_int(argbuf,0);
235 cli_code = extract_int(argbuf,1);
236 rev_level = extract_int(argbuf,2);
237 extract(desc,argbuf,3);
239 strcpy(from_host,config.c_fqdn);
240 if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
242 CC->cs_clientdev = dev_code;
243 CC->cs_clienttyp = cli_code;
244 CC->cs_clientver = rev_level;
245 strncpy(CC->cs_clientname,desc,31);
246 CC->cs_clientname[31] = 0;
248 if ((strlen(from_host)>0) &&
249 (is_public_client(CC->cs_host))) {
250 strncpy(CC->cs_host,from_host,24);
253 set_wtmpsupp(CC->quickroom.QRname);
255 syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
261 cprintf("%d Ok\n",OK);
266 * enter or exit "stealth mode"
268 void cmd_stel(char *cmdbuf)
272 requested_mode = extract_int(cmdbuf,0);
273 if (requested_mode !=0) requested_mode = 1;
275 if (!CC->logged_in) {
276 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
280 if (CC->usersupp.axlevel < 6) {
281 cprintf("%d You must be an Aide to use stealth mode.\n",
282 ERROR+HIGHER_ACCESS_REQUIRED);
286 if (CC->cs_flags & CS_STEALTH) {
287 if (requested_mode == 0)
288 CC->cs_flags = CC->cs_flags-CS_STEALTH;
291 if (requested_mode == 1)
292 CC->cs_flags = CC->cs_flags|CS_STEALTH;
295 set_wtmpsupp(CC->quickroom.QRname);
296 cprintf("%d Ok\n",OK);
303 * display system messages or help
305 void cmd_mesg(char *mname)
312 extract(buf,mname,0);
317 strcpy(dirs[0],"messages");
318 strcpy(dirs[1],"help");
319 mesg_locate(targ,buf,2,dirs);
324 if (strlen(targ)==0) {
325 cprintf("%d '%s' not found.\n",ERROR,mname);
329 mfp = fopen(targ,"r");
331 cprintf("%d Cannot open '%s': %s\n",
332 ERROR,targ,strerror(errno));
335 cprintf("%d %s\n",LISTING_FOLLOWS,buf);
337 while (fgets(buf,255,mfp)!=NULL) {
338 buf[strlen(buf)-1] = 0;
349 * enter system messages or help
351 void cmd_emsg(char *mname)
359 if (CC->usersupp.axlevel < 6) {
360 cprintf("%d You must be an Aide to edit system messages.\n",
361 ERROR+HIGHER_ACCESS_REQUIRED);
365 extract(buf,mname,0);
366 for (a=0; a<strlen(buf); ++a) { /* security measure */
367 if (buf[a] == '/') buf[a] = '.';
372 strcpy(dirs[0],"messages");
373 strcpy(dirs[1],"help");
374 mesg_locate(targ,buf,2,dirs);
378 if (strlen(targ)==0) {
379 sprintf(targ, "./help/%s", buf);
382 mfp = fopen(targ,"w");
384 cprintf("%d Cannot open '%s': %s\n",
385 ERROR,targ,strerror(errno));
388 cprintf("%d %s\n", SEND_LISTING, targ);
390 while (client_gets(buf), strcmp(buf, "000")) {
391 fprintf(mfp, "%s\n", buf);
401 void cmd_rwho(void) {
402 struct CitContext *cptr;
405 char un[40], room[40], host[40], flags[5];
407 aide = CC->usersupp.axlevel >= 6;
408 cprintf("%d\n",LISTING_FOLLOWS);
410 for (cptr = ContextList; cptr != NULL; cptr = cptr->next)
415 if (cptr->cs_flags & CS_POSTING)
420 if (cptr->fake_username[0])
422 strcpy(un, cptr->fake_username);
426 strcpy(un, cptr->curr_user);
428 if (cptr->fake_hostname[0])
430 strcpy(host, cptr->fake_hostname);
434 strcpy(host, cptr->cs_host);
436 if (cptr->fake_roomname[0])
438 strcpy(room, cptr->fake_roomname);
442 strcpy(room, cptr->cs_room);
445 if ((aide) && (spoofed))
448 if ((cptr->cs_flags & CS_STEALTH) && (aide))
451 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
453 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
454 cptr->cs_pid, un, room,
455 host, cptr->cs_clientname,
456 (long)(cptr->lastidle),
457 cptr->lastcmdname, flags);
459 if ((spoofed) && (aide))
461 cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
462 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
463 cptr->cs_host, cptr->cs_clientname,
464 (long)(cptr->lastidle),
465 cptr->lastcmdname, flags);
474 * Terminate another running session
476 void cmd_term(char *cmdbuf)
479 struct CitContext *ccptr;
481 if (!CC->logged_in) {
482 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
486 if (CC->usersupp.axlevel < 6) {
487 cprintf("%d You must be an Aide to terminate sessions.\n",
488 ERROR+HIGHER_ACCESS_REQUIRED);
492 session_num = extract_int(cmdbuf, 0);
493 if (session_num == CC->cs_pid) {
494 cprintf("%d You can't kill your own session.\n", ERROR);
498 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
499 if (session_num == ccptr->cs_pid) {
500 kill_session(ccptr->cs_pid);
501 cprintf("%d Session terminated.\n", OK);
506 cprintf("%d No such session.\n", ERROR);
514 * get the paginator prompt
516 void cmd_more(void) {
517 cprintf("%d %s\n",OK,config.c_moreprompt);
523 void cmd_echo(char *etext)
525 cprintf("%d %s\n",OK,etext);
531 * identify as internal program
533 void cmd_ipgm(char *argbuf)
537 secret = extract_int(argbuf, 0);
538 if (secret == config.c_ipgm_secret) {
539 CC->internal_pgm = 1;
540 strcpy(CC->curr_user, "<internal program>");
541 CC->cs_flags = CC->cs_flags|CS_STEALTH;
542 cprintf("%d Authenticated as an internal program.\n",OK);
545 cprintf("%d Authentication failed.\n",ERROR);
551 * Shut down the server
553 void cmd_down(void) {
554 if (!CC->logged_in) {
555 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
559 if (CC->usersupp.axlevel < 6) {
560 cprintf("%d You must be an Aide to shut down the server.\n",
561 ERROR+HIGHER_ACCESS_REQUIRED);
565 cprintf("%d Shutting down server. Goodbye.\n", OK);
570 * Shut down the server
572 void cmd_scdn(char *argbuf)
576 if (!CC->logged_in) {
577 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
581 if (CC->usersupp.axlevel < 6) {
582 cprintf("%d You must be an Aide to schedule a shutdown.\n",
583 ERROR+HIGHER_ACCESS_REQUIRED);
587 new_state = extract_int(argbuf, 0);
588 if ((new_state == 0) || (new_state == 1)) {
589 ScheduledShutdown = new_state;
591 cprintf("%d %d\n", OK, ScheduledShutdown);
597 void *context_loop(struct CitContext *con)
603 * Wedge our way into the context table.
608 * Initialize some variables specific to our context.
612 CC->internal_pgm = 0;
613 CC->download_fp = NULL;
614 CC->upload_fp = NULL;
615 CC->cs_pid = con->client_socket; /* not necessarily portable */
616 CC->FirstExpressMessage = NULL;
621 strcpy(CC->lastcmdname, " ");
622 strcpy(CC->cs_clientname, "(unknown)");
623 strcpy(CC->curr_user,"");
624 strcpy(CC->net_node,"");
625 sprintf(CC->temp,"/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
626 strcpy(CC->cs_room, "");
627 strcpy(CC->cs_host, config.c_fqdn);
628 locate_host(CC->cs_host);
630 CC->upload_type = UPL_FILE;
633 session_num = session_count();
635 if ((config.c_maxsessions > 0)&&(session_num >= config.c_maxsessions))
638 if (CC->nologin==1) {
639 cprintf("%d %s: Too many users are already online (maximum is %d)\n",
640 ERROR+MAX_SESSIONS_EXCEEDED,
641 config.c_nodename,config.c_maxsessions);
644 cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
647 lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
648 hook_start_session(CC->cs_pid);
649 rec_log(CL_CONNECT, "");
653 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
654 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
655 hook_command_received(CC->cs_pid, cmdbuf);
658 * Let other clients see the last command we executed, but
659 * exclude NOOP because that would be boring.
661 if (struncmp(cmdbuf, "NOOP", 4)) {
662 strcpy(CC->lastcmdname, " ");
663 strncpy(CC->lastcmdname, cmdbuf, 4);
667 if ((struncmp(cmdbuf, "ENT0", 4)) && (struncmp(cmdbuf, "MESG", 4)) && (struncmp(cmdbuf, "MSGS", 4)))
669 CC->cs_flags &= ~CS_POSTING;
673 * This loop recognizes all server commands.
676 if (!struncmp(cmdbuf,"NOOP",4)) {
677 cprintf("%d%cok\n",OK,check_express());
680 else if (!struncmp(cmdbuf,"QUIT",4)) {
681 cprintf("%d Goodbye.\n",OK);
684 else if (!struncmp(cmdbuf,"LOUT",4)) {
685 if (CC->logged_in) logout(CC);
686 cprintf("%d logged out.\n",OK);
689 else if (!struncmp(cmdbuf,"USER",4)) {
690 cmd_user(&cmdbuf[5]);
693 else if (!struncmp(cmdbuf,"PASS",4)) {
694 cmd_pass(&cmdbuf[5]);
697 else if (!struncmp(cmdbuf,"NEWU",4)) {
698 cmd_newu(&cmdbuf[5]);
701 else if (!struncmp(cmdbuf,"SETP",4)) {
702 cmd_setp(&cmdbuf[5]);
705 else if (!struncmp(cmdbuf,"LRMS",4)) {
706 cmd_lrms(&cmdbuf[5]);
709 else if (!struncmp(cmdbuf,"LKRA",4)) {
710 cmd_lkra(&cmdbuf[5]);
713 else if (!struncmp(cmdbuf,"LKRN",4)) {
714 cmd_lkrn(&cmdbuf[5]);
717 else if (!struncmp(cmdbuf,"LKRO",4)) {
718 cmd_lkro(&cmdbuf[5]);
721 else if (!struncmp(cmdbuf,"LZRM",4)) {
722 cmd_lzrm(&cmdbuf[5]);
725 else if (!struncmp(cmdbuf,"GETU",4)) {
729 else if (!struncmp(cmdbuf,"SETU",4)) {
730 cmd_setu(&cmdbuf[5]);
733 else if (!struncmp(cmdbuf,"GOTO",4)) {
734 cmd_goto(&cmdbuf[5]);
737 else if (!struncmp(cmdbuf,"MSGS",4)) {
738 cmd_msgs(&cmdbuf[5]);
741 else if (!struncmp(cmdbuf,"WHOK",4)) {
745 else if (!struncmp(cmdbuf,"RDIR",4)) {
749 else if (!struncmp(cmdbuf,"MSG0",4)) {
750 cmd_msg0(&cmdbuf[5]);
753 else if (!struncmp(cmdbuf,"MSG2",4)) {
754 cmd_msg2(&cmdbuf[5]);
757 else if (!struncmp(cmdbuf,"MSG3",4)) {
758 cmd_msg3(&cmdbuf[5]);
761 else if (!struncmp(cmdbuf,"INFO",4)) {
765 else if (!struncmp(cmdbuf,"SLRP",4)) {
766 cmd_slrp(&cmdbuf[5]);
769 else if (!struncmp(cmdbuf,"INVT",4)) {
770 cmd_invt_kick(&cmdbuf[5],1);
773 else if (!struncmp(cmdbuf,"KICK",4)) {
774 cmd_invt_kick(&cmdbuf[5],0);
777 else if (!struncmp(cmdbuf,"GETR",4)) {
781 else if (!struncmp(cmdbuf,"SETR",4)) {
782 cmd_setr(&cmdbuf[5]);
785 else if (!struncmp(cmdbuf,"GETA",4)) {
789 else if (!struncmp(cmdbuf,"SETA",4)) {
790 cmd_seta(&cmdbuf[5]);
793 else if (!struncmp(cmdbuf,"ENT0",4)) {
794 cmd_ent0(&cmdbuf[5]);
797 else if (!struncmp(cmdbuf,"ENT3",4)) {
798 cmd_ent3(&cmdbuf[5]);
801 else if (!struncmp(cmdbuf,"RINF",4)) {
805 else if (!struncmp(cmdbuf,"DELE",4)) {
806 cmd_dele(&cmdbuf[5]);
809 else if (!struncmp(cmdbuf,"KILL",4)) {
810 cmd_kill(&cmdbuf[5]);
813 else if (!struncmp(cmdbuf,"CRE8",4)) {
814 cmd_cre8(&cmdbuf[5]);
817 else if (!struncmp(cmdbuf,"MOVE",4)) {
818 cmd_move(&cmdbuf[5]);
821 else if (!struncmp(cmdbuf,"FORG",4)) {
825 else if (!struncmp(cmdbuf,"MESG",4)) {
826 cmd_mesg(&cmdbuf[5]);
829 else if (!struncmp(cmdbuf,"EMSG",4)) {
830 cmd_emsg(&cmdbuf[5]);
833 else if (!struncmp(cmdbuf,"GNUR",4)) {
837 else if (!struncmp(cmdbuf,"GREG",4)) {
838 cmd_greg(&cmdbuf[5]);
841 else if (!struncmp(cmdbuf,"VALI",4)) {
842 cmd_vali(&cmdbuf[5]);
845 else if (!struncmp(cmdbuf,"EINF",4)) {
846 cmd_einf(&cmdbuf[5]);
849 else if (!struncmp(cmdbuf,"LIST",4)) {
853 else if (!struncmp(cmdbuf,"REGI",4)) {
857 else if (!struncmp(cmdbuf,"CHEK",4)) {
861 else if (!struncmp(cmdbuf,"DELF",4)) {
862 cmd_delf(&cmdbuf[5]);
865 else if (!struncmp(cmdbuf,"MOVF",4)) {
866 cmd_movf(&cmdbuf[5]);
869 else if (!struncmp(cmdbuf,"NETF",4)) {
870 cmd_netf(&cmdbuf[5]);
873 else if (!struncmp(cmdbuf,"RWHO",4)) {
877 else if (!struncmp(cmdbuf,"OPEN",4)) {
878 cmd_open(&cmdbuf[5]);
881 else if (!struncmp(cmdbuf,"CLOS",4)) {
885 else if (!struncmp(cmdbuf,"UOPN",4)) {
886 cmd_uopn(&cmdbuf[5]);
889 else if (!struncmp(cmdbuf,"UCLS",4)) {
890 cmd_ucls(&cmdbuf[5]);
893 else if (!struncmp(cmdbuf,"READ",4)) {
894 cmd_read(&cmdbuf[5]);
897 else if (!struncmp(cmdbuf,"WRIT",4)) {
898 cmd_writ(&cmdbuf[5]);
901 else if (!struncmp(cmdbuf,"QUSR",4)) {
902 cmd_qusr(&cmdbuf[5]);
905 else if (!struncmp(cmdbuf,"ECHO",4)) {
906 cmd_echo(&cmdbuf[5]);
909 else if (!struncmp(cmdbuf,"OIMG",4)) {
910 cmd_oimg(&cmdbuf[5]);
913 else if (!struncmp(cmdbuf,"MORE",4)) {
917 else if (!struncmp(cmdbuf,"NETP",4)) {
918 cmd_netp(&cmdbuf[5]);
921 else if (!struncmp(cmdbuf,"NDOP",4)) {
922 cmd_ndop(&cmdbuf[5]);
925 else if (!struncmp(cmdbuf,"NUOP",4)) {
926 cmd_nuop(&cmdbuf[5]);
929 else if (!struncmp(cmdbuf,"LFLR",4)) {
933 else if (!struncmp(cmdbuf,"CFLR",4)) {
934 cmd_cflr(&cmdbuf[5]);
937 else if (!struncmp(cmdbuf,"KFLR",4)) {
938 cmd_kflr(&cmdbuf[5]);
941 else if (!struncmp(cmdbuf,"EFLR",4)) {
942 cmd_eflr(&cmdbuf[5]);
945 else if (!struncmp(cmdbuf,"IDEN",4)) {
946 cmd_iden(&cmdbuf[5]);
949 else if (!struncmp(cmdbuf,"IPGM",4)) {
950 cmd_ipgm(&cmdbuf[5]);
953 else if (!struncmp(cmdbuf,"CHAT",4)) {
954 cmd_chat(&cmdbuf[5]);
957 else if (!struncmp(cmdbuf,"PEXP",4)) {
961 else if (!struncmp(cmdbuf,"SEXP",4)) {
962 cmd_sexp(&cmdbuf[5]);
965 else if (!struncmp(cmdbuf,"EBIO",4)) {
969 else if (!struncmp(cmdbuf,"RBIO",4)) {
970 cmd_rbio(&cmdbuf[5]);
973 else if (!struncmp(cmdbuf,"LBIO",4)) {
977 else if (!struncmp(cmdbuf,"STEL",4)) {
978 cmd_stel(&cmdbuf[5]);
981 else if (!struncmp(cmdbuf,"TERM",4)) {
982 cmd_term(&cmdbuf[5]);
985 else if (!struncmp(cmdbuf,"DOWN",4)) {
989 else if (!struncmp(cmdbuf,"SCDN",4)) {
990 cmd_scdn(&cmdbuf[5]);
993 else if (!struncmp(cmdbuf, "NSET", 4)) {
994 cmd_nset(&cmdbuf[5]);
997 else if (!struncmp(cmdbuf, "UIMG", 4)) {
998 cmd_uimg(&cmdbuf[5]);
1000 else if (!struncmp(cmdbuf, "UCHG", 4)) {
1001 cmd_uchg(&cmdbuf[5]);
1003 else if (!struncmp(cmdbuf, "TIME", 4)) {
1004 cmd_time(&cmdbuf[5]);
1006 else if (!struncmp(cmdbuf, "HCHG", 4)) {
1007 cmd_hchg(&cmdbuf[5]);
1009 else if (!struncmp(cmdbuf, "RCHG", 4)) {
1010 cmd_rchg(&cmdbuf[5]);
1013 cprintf("%d Unrecognized or unsupported command.\n",
1017 } while(struncmp(cmdbuf,"QUIT",4));
1019 cleanup(EXIT_NORMAL);