5e685dbd97543d984f416e4728d59806f34bebbb
[citadel.git] / citadel / citserver.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <time.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <limits.h>
13 #ifdef HAVE_PTHREAD_H
14 #include <pthread.h>
15 #endif
16 #include <syslog.h>
17 #include <dlfcn.h>
18 #include <netdb.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include "sysdep_decls.h"
25 #include "citserver.h"
26 #include "config.h"
27 #include "database.h"
28 #include "housekeeping.h"
29 #include "user_ops.h"
30 #include "logging.h"
31 #include "support.h"
32 #include "msgbase.h"
33 #include "locate_host.h"
34 #include "room_ops.h"
35 #include "file_ops.h"
36 #include "dynloader.h"
37 #include "policy.h"
38 #include "control.h"
39 #include "tools.h"
40
41 struct CitContext *ContextList = NULL;
42 int ScheduledShutdown = 0;
43
44 /*
45  * Various things that need to be initialized at startup
46  */
47 void master_startup(void) {
48         lprintf(7, "Opening databases\n");
49         open_databases();
50
51         lprintf(7, "Checking floor reference counts\n");
52         check_ref_counts();
53
54         lprintf(7, "Creating base rooms (if necessary)\n");
55         create_room(BASEROOM, 0, "", 0);
56         create_room(AIDEROOM, 4, "", 0);
57         create_room(config.c_twitroom, 0, "", 0);
58         }
59
60 /*
61  * Cleanup routine to be called when the server is shutting down.
62  */
63 void master_cleanup(void) {
64         struct CleanupFunctionHook *fcn;
65
66         /* Cancel all running sessions */
67         lprintf(7, "Cancelling running sessions...\n");
68         while (ContextList != NULL) {
69                 kill_session(ContextList->cs_pid);
70                 }
71
72         /* Run any cleanup routines registered by loadable modules */
73         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
74                 (*fcn->h_function_pointer)();
75                 }
76
77         /* Close databases */
78         lprintf(7, "Closing databases\n");
79         close_databases();
80
81         /* Do system-dependent stuff */
82         sysdep_master_cleanup();
83
84         /* Now go away. */
85         lprintf(3, "citserver: exiting.\n");
86         fflush(stdout); fflush(stderr);
87         exit(0);
88         }
89
90
91 /*
92  * Gracefully terminate the session and thread.
93  * (This is called as a cleanup handler by the thread library.)
94  *
95  * All NON-system-dependent stuff is done in this function.
96  * System-dependent session/thread cleanup is in cleanup() in sysdep.c
97  */
98 void cleanup_stuff(void *arg)
99 {
100
101         lprintf(9, "cleanup_stuff() called\n");
102
103         lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
104         logout(CC);
105
106         rec_log(CL_TERMINATE,CC->curr_user);
107         unlink(CC->temp);
108         lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
109         
110         /* Run any cleanup routines registered by loadable modules */
111         PerformSessionHooks(EVT_STOP);
112
113         syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
114         
115         /* Deallocate any message list we might have in memory */
116         if (CC->msglist != NULL) phree(CC->msglist);
117
118         /* Now get rid of the session and context */
119         lprintf(7, "cleanup_stuff() calling RemoveContext(%d)\n", CC->cs_pid);
120         RemoveContext(CC);
121
122         /* While we still have an extra thread with no user attached to it,
123          * take the opportunity to do some housekeeping before exiting.
124          */
125         do_housekeeping();
126         }
127
128
129 /*
130  * set_wtmpsupp()  -  alter the session listing
131  */
132 void set_wtmpsupp(char *newtext)
133 {
134         strncpy(CC->cs_room,newtext,sizeof CC->cs_room);
135         CC->cs_room[sizeof CC->cs_room - 1] = 0;
136         time(&CC->cs_lastupdt);
137
138         /* Run any routines registered by loadable modules */
139         PerformSessionHooks(EVT_NEWROOM);
140         }
141
142
143 /*
144  * call set_wtmpsupp() with the name of the current room, modified a bit...
145  */
146 void set_wtmpsupp_to_current_room() {
147         if (CC->quickroom.QRflags & QR_PRIVATE) {
148                 set_wtmpsupp("<private room>");
149                 }
150         else if (CC->quickroom.QRflags & QR_MAILBOX) {
151                 set_wtmpsupp(&CC->quickroom.QRname[11]);
152                 }
153         else {
154                 set_wtmpsupp(CC->quickroom.QRname);
155                 }
156         }
157
158
159
160 /*
161  * cmd_info()  -  tell the client about this server
162  */
163 void cmd_info(void) {
164         cprintf("%d Server info:\n",LISTING_FOLLOWS);
165         cprintf("%d\n",CC->cs_pid);
166         cprintf("%s\n",config.c_nodename);
167         cprintf("%s\n",config.c_humannode);
168         cprintf("%s\n",config.c_fqdn);
169         cprintf("%s\n",CITADEL);
170         cprintf("%d\n",REV_LEVEL);
171         cprintf("%s\n",config.c_bbs_city);
172         cprintf("%s\n",config.c_sysadm);
173         cprintf("%d\n",SERVER_TYPE);
174         cprintf("%s\n",config.c_moreprompt);
175         cprintf("1\n"); /* 1 = yes, this system supports floors */
176         cprintf("1\n"); /* 1 = we support the extended paging options */
177         cprintf("000\n");
178         }
179
180 void cmd_rchg(char *argbuf)
181 {
182         char newroomname[256]; /* set to 256 to prevent buffer overruns <dme>*/
183
184         extract(newroomname, argbuf, 0);
185         newroomname[ROOMNAMELEN] = 0;
186         if (strlen(newroomname) > 0) {
187                 strncpy(CC->fake_roomname, newroomname, ROOMNAMELEN);
188                 CC->fake_roomname[ROOMNAMELEN - 1] = 0;
189                 }
190         else {
191                 strcpy(CC->fake_roomname, "");
192                 }
193         cprintf("%d OK\n", OK);
194 }
195
196 void cmd_hchg(char *newhostname)
197 {
198    if ((newhostname) && (newhostname[0]))
199    {
200       memset(CC->fake_hostname, 0, 25);
201       strncpy(CC->fake_hostname, newhostname, 24);
202    }
203    else
204       strcpy(CC->fake_hostname, "");
205    cprintf("%d OK\n",OK);
206 }
207
208 void cmd_uchg(char *newusername)
209 {
210    if (CC->usersupp.axlevel < 6) 
211    {
212       cprintf("%d You must be an Aide to use UCHG.\n",
213                 ERROR+HIGHER_ACCESS_REQUIRED);
214       return;
215    }
216    if ((newusername) && (newusername[0]))
217    {
218       CC->cs_flags &= ~CS_STEALTH;
219       memset(CC->fake_username, 0, 32);
220       if (strncasecmp(newusername, CC->curr_user, strlen(CC->curr_user)))
221          strncpy(CC->fake_username, newusername, 31);
222    }
223    else
224    {
225       CC->fake_username[0] = '\0';
226       CC->cs_flags |= CS_STEALTH;
227    }
228    cprintf("%d\n",OK);
229 }
230
231 /*
232  * returns an asterisk if there are any express messages waiting,
233  * space otherwise.
234  */
235 char check_express(void) {
236         if (CC->FirstExpressMessage == NULL) {
237                 return(' ');
238                 }
239         else {
240                 return('*');
241                 }
242         }
243
244 void cmd_time(void)
245 {
246    time_t tv;
247    
248    tv = time(NULL);
249    
250    cprintf("%d %ld\n", OK, tv);
251 }
252
253 /*
254  * Check whether two hostnames match.
255  * "Realname" should be an actual name of a client that is trying to connect;
256  * "testname" should be the value we are comparing it with. The idea is that we
257  * want to compare with both the abbreviated and fully-qualified versions of
258  * "testname;" some people define "localhost" as "localhost.foo.com," etc.
259  */
260 static int hostnames_match(const char *realname, const char *testname) {
261         struct hostent *he;
262         int retval = 0;
263
264         if (!strcasecmp(realname, testname))
265                 return 1;
266
267 #ifdef HAVE_NONREENTRANT_NETDB
268         begin_critical_section(S_NETDB);
269 #endif
270
271         if ((he = gethostbyname(testname)) != NULL)
272                 if (!strcasecmp(realname, he->h_name))
273                         retval = 1;
274
275 #ifdef HAVE_NONREENTRANT_NETDB
276         end_critical_section(S_NETDB);
277 #endif
278
279         return retval;
280         }
281
282 /*
283  * check a hostname against the public_clients file
284  */
285 int is_public_client(char *where)
286 {
287         char buf[256];
288         FILE *fp;
289
290         if (hostnames_match(where,"localhost")) return(1);
291         if (hostnames_match(where,config.c_fqdn)) return(1);
292
293         fp = fopen("public_clients","r");
294         if (fp == NULL) return(0);
295
296         while (fgets(buf,256,fp)!=NULL) {
297                 while (isspace((buf[strlen(buf)-1]))) 
298                         buf[strlen(buf)-1] = 0;
299                 if (hostnames_match(where,buf)) {
300                         fclose(fp);
301                         return(1);
302                         }
303                 }
304
305         fclose(fp);
306         return(0);
307         }
308
309
310 /*
311  * the client is identifying itself to the server
312  */
313 void cmd_iden(char *argbuf)
314 {
315         int dev_code;
316         int cli_code;
317         int rev_level;
318         char desc[256];
319         char from_host[256];
320         struct in_addr addr;
321
322         if (num_parms(argbuf)<4) {
323                 cprintf("%d usage error\n",ERROR);
324                 return;
325                 }
326
327         dev_code = extract_int(argbuf,0);
328         cli_code = extract_int(argbuf,1);
329         rev_level = extract_int(argbuf,2);
330         extract(desc,argbuf,3);
331
332         strncpy(from_host,config.c_fqdn,sizeof from_host);
333         from_host[sizeof from_host - 1] = 0;
334         if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
335
336         CC->cs_clientdev = dev_code;
337         CC->cs_clienttyp = cli_code;
338         CC->cs_clientver = rev_level;
339         strncpy(CC->cs_clientname,desc,31);
340         CC->cs_clientname[31] = 0;
341
342         if ((strlen(from_host)>0) && 
343            (is_public_client(CC->cs_host))) {
344                 if (inet_aton(from_host, &addr))
345                         locate_host(CC->cs_host, &addr);
346                 else {
347                         strncpy(CC->cs_host,from_host,24);
348                         CC->cs_host[24] = 0;
349                         }
350                 }
351         set_wtmpsupp_to_current_room();
352
353         syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
354                 dev_code,
355                 cli_code,
356                 (rev_level / 100),
357                 (rev_level % 100),
358                 desc);
359                 cprintf("%d Ok\n",OK);
360         }
361
362
363 /*
364  * enter or exit "stealth mode"
365  */
366 void cmd_stel(char *cmdbuf)
367 {
368         int requested_mode;
369
370         requested_mode = extract_int(cmdbuf,0);
371         if (requested_mode !=0) requested_mode = 1;
372
373         if (!CC->logged_in) {
374                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
375                 return;
376                 }
377
378         if (CC->usersupp.axlevel < 6) {
379                 cprintf("%d You must be an Aide to use stealth mode.\n",
380                         ERROR+HIGHER_ACCESS_REQUIRED);
381                 return;
382                 }
383
384         if (CC->cs_flags & CS_STEALTH) {
385                 if (requested_mode == 0)
386                         CC->cs_flags = CC->cs_flags-CS_STEALTH;
387                 }
388         else {
389                 if (requested_mode == 1)
390                         CC->cs_flags = CC->cs_flags|CS_STEALTH;
391                 }
392
393         set_wtmpsupp_to_current_room();
394         cprintf("%d Ok\n",OK);
395         }
396
397
398
399
400 /*
401  * display system messages or help
402  */
403 void cmd_mesg(char *mname)
404 {
405         FILE *mfp;
406         char targ[256];
407         char buf[256];
408         char *dirs[2];
409
410         extract(buf,mname,0);
411
412
413         dirs[0]=mallok(64);
414         dirs[1]=mallok(64);
415         strcpy(dirs[0],"messages");
416         strcpy(dirs[1],"help");
417         mesg_locate(targ,buf,2,dirs);
418         phree(dirs[0]);
419         phree(dirs[1]);
420
421
422         if (strlen(targ)==0) {
423                 cprintf("%d '%s' not found.\n",ERROR,mname);
424                 return;
425                 }
426
427         mfp = fopen(targ,"r");
428         if (mfp==NULL) {
429                 cprintf("%d Cannot open '%s': %s\n",
430                         ERROR,targ,strerror(errno));
431                 return;
432                 }
433         cprintf("%d %s\n",LISTING_FOLLOWS,buf);
434
435         while (fgets(buf,255,mfp)!=NULL) {
436                 buf[strlen(buf)-1] = 0;
437                 do_help_subst(buf);
438                 cprintf("%s\n",buf);
439                 }
440
441         fclose(mfp);
442         cprintf("000\n");
443         }
444
445
446 /*
447  * enter system messages or help
448  */
449 void cmd_emsg(char *mname)
450 {
451         FILE *mfp;
452         char targ[256];
453         char buf[256];
454         char *dirs[2];
455         int a;
456
457         if (CC->usersupp.axlevel < 6) {
458                 cprintf("%d You must be an Aide to edit system messages.\n",
459                         ERROR+HIGHER_ACCESS_REQUIRED);
460                 return;
461                 }
462
463         extract(buf,mname,0);
464         for (a=0; a<strlen(buf); ++a) {         /* security measure */
465                 if (buf[a] == '/') buf[a] = '.';
466                 }
467
468         dirs[0]=mallok(64);
469         dirs[1]=mallok(64);
470         strcpy(dirs[0],"messages");
471         strcpy(dirs[1],"help");
472         mesg_locate(targ,buf,2,dirs);
473         phree(dirs[0]);
474         phree(dirs[1]);
475
476         if (strlen(targ)==0) {
477                 snprintf(targ, sizeof targ, "./help/%s", buf);
478                 }
479
480         mfp = fopen(targ,"w");
481         if (mfp==NULL) {
482                 cprintf("%d Cannot open '%s': %s\n",
483                         ERROR,targ,strerror(errno));
484                 return;
485                 }
486         cprintf("%d %s\n", SEND_LISTING, targ);
487
488         while (client_gets(buf), strcmp(buf, "000")) {
489                 fprintf(mfp, "%s\n", buf);
490                 }
491
492         fclose(mfp);
493         }
494
495
496 /*
497  * who's online
498  */
499 void cmd_rwho(void) {
500         struct CitContext *cptr;
501         int spoofed = 0;
502         int aide;
503         char un[40], room[40], host[40], flags[5];
504         
505         aide = CC->usersupp.axlevel >= 6;
506         cprintf("%d\n",LISTING_FOLLOWS);
507         
508         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) 
509         {
510                 flags[0] = '\0';
511                 spoofed = 0;
512                 
513                 if (cptr->cs_flags & CS_POSTING)
514                    strcat(flags, "*");
515                 else
516                    strcat(flags, ".");
517                    
518                 if (cptr->fake_username[0])
519                 {
520                    strcpy(un, cptr->fake_username);
521                    spoofed = 1;
522                 }
523                 else
524                    strcpy(un, cptr->curr_user);
525                    
526                 if (cptr->fake_hostname[0])
527                 {
528                    strcpy(host, cptr->fake_hostname);
529                    spoofed = 1;
530                 }
531                 else
532                    strcpy(host, cptr->cs_host);
533
534                 if (cptr->fake_roomname[0])
535                 {
536                    strcpy(room, cptr->fake_roomname);
537                    spoofed = 1;
538                 }
539                 else
540                    strcpy(room, cptr->cs_room);
541                    
542                 
543                 if ((aide) && (spoofed))
544                    strcat(flags, "+");
545                 
546                 if ((cptr->cs_flags & CS_STEALTH) && (aide))
547                    strcat(flags, "-");
548                 
549                 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
550                 {
551                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
552                                 cptr->cs_pid, un, room,
553                                 host, cptr->cs_clientname,
554                                 (long)(cptr->lastidle),
555                                 cptr->lastcmdname, flags);
556                 }
557                 if ((spoofed) && (aide))
558                 {
559                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
560                                 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
561                                 cptr->cs_host, cptr->cs_clientname,
562                                 (long)(cptr->lastidle),
563                                 cptr->lastcmdname, flags);
564                 
565                 }
566         }
567         cprintf("000\n");
568         }
569
570
571 /*
572  * Terminate another running session
573  */
574 void cmd_term(char *cmdbuf)
575 {
576         int session_num;
577         struct CitContext *ccptr;
578         int session_to_kill = 0;
579
580         if (!CC->logged_in) {
581                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
582                 return;
583                 }
584
585         if (CC->usersupp.axlevel < 6) {
586                 cprintf("%d You must be an Aide to terminate sessions.\n",
587                         ERROR+HIGHER_ACCESS_REQUIRED);
588                 return;
589                 }
590
591         session_num = extract_int(cmdbuf, 0);
592         if (session_num == CC->cs_pid) {
593                 cprintf("%d You can't kill your own session.\n", ERROR);
594                 return;
595                 }
596
597         lprintf(9, "Locating session to kill\n");
598         begin_critical_section(S_SESSION_TABLE);
599         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
600                 if (session_num == ccptr->cs_pid) {
601                         session_to_kill = ccptr->cs_pid;
602                         }
603                 }
604         end_critical_section(S_SESSION_TABLE);
605         lprintf(9, "session_to_kill == %d\n", session_to_kill);
606
607         if (session_to_kill > 0) {
608                 lprintf(9, "calling kill_session()\n");
609                 kill_session(session_to_kill);
610                 cprintf("%d Session terminated.\n", OK);
611                 }
612         else {
613                 cprintf("%d No such session.\n", ERROR);
614                 }
615         }
616
617
618
619
620
621 /* 
622  * get the paginator prompt
623  */
624 void cmd_more(void) {
625         cprintf("%d %s\n",OK,config.c_moreprompt);
626         }
627
628 /*
629  * echo 
630  */
631 void cmd_echo(char *etext)
632 {
633         cprintf("%d %s\n",OK,etext);
634         }
635
636
637
638 /* 
639  * identify as internal program
640  */
641 void cmd_ipgm(char *argbuf)
642 {
643         int secret;
644
645         secret = extract_int(argbuf, 0);
646         if (secret == config.c_ipgm_secret) {
647                 CC->internal_pgm = 1;
648                 strcpy(CC->curr_user, "<internal program>");
649                 CC->cs_flags = CC->cs_flags|CS_STEALTH;
650                 cprintf("%d Authenticated as an internal program.\n",OK);
651                 }
652         else {
653                 cprintf("%d Authentication failed.\n",ERROR);
654                 }
655         }
656
657
658 /*
659  * Shut down the server
660  */
661 void cmd_down(void) {
662         if (!CC->logged_in) {
663                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
664                 return;
665                 }
666
667         if (CC->usersupp.axlevel < 6) {
668                 cprintf("%d You must be an Aide to shut down the server.\n",
669                         ERROR+HIGHER_ACCESS_REQUIRED);
670                 return;
671                 }
672
673         cprintf("%d Shutting down server.  Goodbye.\n", OK);
674         master_cleanup();
675         }
676
677 /*
678  * Schedule or cancel a server shutdown
679  */
680 void cmd_scdn(char *argbuf)
681 {
682         int new_state;
683
684         if (!CC->logged_in) {
685                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
686                 return;
687                 }
688
689         if (CC->usersupp.axlevel < 6) {
690                 cprintf("%d You must be an Aide to schedule a shutdown.\n",
691                         ERROR+HIGHER_ACCESS_REQUIRED);
692                 return;
693                 }
694
695         new_state = extract_int(argbuf, 0);
696         if ((new_state == 0) || (new_state == 1)) {
697                 ScheduledShutdown = new_state;
698                 }
699         cprintf("%d %d\n", OK, ScheduledShutdown);
700         }
701
702
703 /*
704  * main context loop
705  */
706 void *context_loop(struct CitContext *con)
707 {
708         char cmdbuf[256];
709         int num_sessions, len;
710         struct sockaddr_in sin;
711
712         /*
713          * Wedge our way into the context table.
714          */
715         InitMyContext(con);
716
717         /* 
718          * Initialize some variables specific to our context.
719          */
720         CC->logged_in = 0;
721         CC->internal_pgm = 0;
722         CC->download_fp = NULL;
723         CC->upload_fp = NULL;
724         CC->cs_pid = con->client_socket;        /* not necessarily portable */
725         CC->FirstExpressMessage = NULL;
726         CC->msglist = NULL;
727         CC->num_msgs = 0;
728         time(&CC->lastcmd);
729         time(&CC->lastidle);
730         strcpy(CC->lastcmdname, "    ");
731         strcpy(CC->cs_clientname, "(unknown)");
732         strcpy(CC->curr_user,"(not logged in)");
733         strcpy(CC->net_node,"");
734         snprintf(CC->temp, sizeof CC->temp, "/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
735         strcpy(CC->cs_room, "(no room)");
736         strncpy(CC->cs_host, config.c_fqdn, sizeof CC->cs_host);
737         CC->cs_host[sizeof CC->cs_host - 1] = 0;
738         len = sizeof sin;
739         if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, &len))
740                 locate_host(CC->cs_host, &sin.sin_addr);
741         CC->cs_flags = 0;
742         CC->upload_type = UPL_FILE;
743         CC->dl_is_net = 0;
744
745         num_sessions = session_count();
746         CC->nologin = 0;
747         if ((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions))
748                 CC->nologin = 1;
749
750         if (CC->nologin==1) {
751            cprintf("%d %s: Too many users are already online (maximum is %d)\n",
752                 ERROR+MAX_SESSIONS_EXCEEDED,
753                 config.c_nodename,config.c_maxsessions);
754                 }
755         else {
756            cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
757                 }
758
759         lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
760
761         /* Run any session startup routines registered by loadable modules */
762         PerformSessionHooks(EVT_START);
763
764         rec_log(CL_CONNECT, "");
765
766         do {
767                 time(&CC->lastcmd);
768                 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
769                 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
770                 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
771
772                 /*
773                  * Let other clients see the last command we executed, but
774                  * exclude NOOP because that would be boring.
775                  */
776                 if (strncasecmp(cmdbuf, "NOOP", 4)) {
777                         strcpy(CC->lastcmdname, "    ");
778                         strncpy(CC->lastcmdname, cmdbuf, 4);
779                         time(&CC->lastidle);
780                         }
781                         
782                 if ((strncasecmp(cmdbuf, "ENT0", 4)) && (strncasecmp(cmdbuf, "MESG", 4)) && (strncasecmp(cmdbuf, "MSGS", 4)))
783                 {
784                    CC->cs_flags &= ~CS_POSTING;
785                 }
786                    
787 /*
788  * This loop recognizes all server commands.
789  */
790
791                 if (!strncasecmp(cmdbuf,"NOOP",4)) {
792                         cprintf("%d%cok\n",OK,check_express());
793                         }
794
795                 else if (!strncasecmp(cmdbuf,"QUIT",4)) {
796                         cprintf("%d Goodbye.\n",OK);
797                         }
798
799                 else if (!strncasecmp(cmdbuf,"LOUT",4)) {
800                         if (CC->logged_in) logout(CC);
801                         cprintf("%d logged out.\n",OK);
802                         }
803
804                 else if (!strncasecmp(cmdbuf,"USER",4)) {
805                         cmd_user(&cmdbuf[5]);
806                         }
807
808                 else if (!strncasecmp(cmdbuf,"PASS",4)) {
809                         cmd_pass(&cmdbuf[5]);
810                         }
811
812                 else if (!strncasecmp(cmdbuf,"NEWU",4)) {
813                         cmd_newu(&cmdbuf[5]);
814                         }
815
816                 else if (!strncasecmp(cmdbuf,"SETP",4)) {
817                         cmd_setp(&cmdbuf[5]);
818                         }
819
820                 else if (!strncasecmp(cmdbuf,"LRMS",4)) {
821                         cmd_lrms(&cmdbuf[5]);
822                         }
823
824                 else if (!strncasecmp(cmdbuf,"LKRA",4)) {
825                         cmd_lkra(&cmdbuf[5]);
826                         }
827
828                 else if (!strncasecmp(cmdbuf,"LKRN",4)) {
829                         cmd_lkrn(&cmdbuf[5]);
830                         }
831
832                 else if (!strncasecmp(cmdbuf,"LKRO",4)) {
833                         cmd_lkro(&cmdbuf[5]);
834                         }
835
836                 else if (!strncasecmp(cmdbuf,"LZRM",4)) {
837                         cmd_lzrm(&cmdbuf[5]);
838                         }
839
840                 else if (!strncasecmp(cmdbuf,"GETU",4)) {
841                         cmd_getu();
842                         }
843
844                 else if (!strncasecmp(cmdbuf,"SETU",4)) {
845                         cmd_setu(&cmdbuf[5]);
846                         }
847
848                 else if (!strncasecmp(cmdbuf,"GOTO",4)) {
849                         cmd_goto(&cmdbuf[5]);
850                         }
851
852                 else if (!strncasecmp(cmdbuf,"MSGS",4)) {
853                         cmd_msgs(&cmdbuf[5]);
854                         }
855
856                 else if (!strncasecmp(cmdbuf,"WHOK",4)) {
857                         cmd_whok();
858                         }
859
860                 else if (!strncasecmp(cmdbuf,"RDIR",4)) {
861                         cmd_rdir();
862                         }
863
864                 else if (!strncasecmp(cmdbuf,"MSG0",4)) {
865                         cmd_msg0(&cmdbuf[5]);
866                         }
867
868                 else if (!strncasecmp(cmdbuf,"MSG2",4)) {
869                         cmd_msg2(&cmdbuf[5]);
870                         }
871
872                 else if (!strncasecmp(cmdbuf,"MSG3",4)) {
873                         cmd_msg3(&cmdbuf[5]);
874                         }
875
876                 else if (!strncasecmp(cmdbuf,"MSG4",4)) {
877                         cmd_msg4(&cmdbuf[5]);
878                         }
879
880                 else if (!strncasecmp(cmdbuf,"OPNA",4)) {
881                         cmd_opna(&cmdbuf[5]);
882                         }
883
884                 else if (!strncasecmp(cmdbuf,"INFO",4)) {
885                         cmd_info();
886                         }
887
888                 else if (!strncasecmp(cmdbuf,"SLRP",4)) {
889                         cmd_slrp(&cmdbuf[5]);
890                         }
891
892                 else if (!strncasecmp(cmdbuf,"INVT",4)) {
893                         cmd_invt_kick(&cmdbuf[5],1);
894                         }
895
896                 else if (!strncasecmp(cmdbuf,"KICK",4)) {
897                         cmd_invt_kick(&cmdbuf[5],0);
898                         }
899
900                 else if (!strncasecmp(cmdbuf,"GETR",4)) {
901                         cmd_getr();
902                         }
903
904                 else if (!strncasecmp(cmdbuf,"SETR",4)) {
905                         cmd_setr(&cmdbuf[5]);
906                         }
907
908                 else if (!strncasecmp(cmdbuf,"GETA",4)) {
909                         cmd_geta();
910                         }
911
912                 else if (!strncasecmp(cmdbuf,"SETA",4)) {
913                         cmd_seta(&cmdbuf[5]);
914                         }
915
916                 else if (!strncasecmp(cmdbuf,"ENT0",4)) {
917                         cmd_ent0(&cmdbuf[5]);
918                         }
919
920                 else if (!strncasecmp(cmdbuf,"ENT3",4)) {
921                         cmd_ent3(&cmdbuf[5]);
922                         }
923
924                 else if (!strncasecmp(cmdbuf,"RINF",4)) {
925                         cmd_rinf();
926                         }
927
928                 else if (!strncasecmp(cmdbuf,"DELE",4)) {
929                         cmd_dele(&cmdbuf[5]);
930                         }
931
932                 else if (!strncasecmp(cmdbuf,"KILL",4)) {
933                         cmd_kill(&cmdbuf[5]);
934                         }
935
936                 else if (!strncasecmp(cmdbuf,"CRE8",4)) {
937                         cmd_cre8(&cmdbuf[5]);
938                         }
939
940                 else if (!strncasecmp(cmdbuf,"MOVE",4)) {
941                         cmd_move(&cmdbuf[5]);
942                         }
943
944                 else if (!strncasecmp(cmdbuf,"FORG",4)) {
945                         cmd_forg();
946                         }
947
948                 else if (!strncasecmp(cmdbuf,"MESG",4)) {
949                         cmd_mesg(&cmdbuf[5]);
950                         }
951
952                 else if (!strncasecmp(cmdbuf,"EMSG",4)) {
953                         cmd_emsg(&cmdbuf[5]);
954                         }
955
956                 else if (!strncasecmp(cmdbuf,"GNUR",4)) {
957                         cmd_gnur();
958                         }
959
960                 else if (!strncasecmp(cmdbuf,"GREG",4)) {
961                         cmd_greg(&cmdbuf[5]);
962                         }
963
964                 else if (!strncasecmp(cmdbuf,"VALI",4)) {
965                         cmd_vali(&cmdbuf[5]);
966                         }
967
968                 else if (!strncasecmp(cmdbuf,"EINF",4)) {
969                         cmd_einf(&cmdbuf[5]);
970                         }
971
972                 else if (!strncasecmp(cmdbuf,"LIST",4)) {
973                         cmd_list();
974                         }
975
976                 else if (!strncasecmp(cmdbuf,"REGI",4)) {
977                         cmd_regi();
978                         }
979
980                 else if (!strncasecmp(cmdbuf,"CHEK",4)) {
981                         cmd_chek();
982                         }
983
984                 else if (!strncasecmp(cmdbuf,"DELF",4)) {
985                         cmd_delf(&cmdbuf[5]);
986                         }
987
988                 else if (!strncasecmp(cmdbuf,"MOVF",4)) {
989                         cmd_movf(&cmdbuf[5]);
990                         }
991
992                 else if (!strncasecmp(cmdbuf,"NETF",4)) {
993                         cmd_netf(&cmdbuf[5]);
994                         }
995
996                 else if (!strncasecmp(cmdbuf,"RWHO",4)) {
997                         cmd_rwho();
998                         }
999
1000                 else if (!strncasecmp(cmdbuf,"OPEN",4)) {
1001                         cmd_open(&cmdbuf[5]);
1002                         }
1003
1004                 else if (!strncasecmp(cmdbuf,"CLOS",4)) {
1005                         cmd_clos();
1006                         }
1007
1008                 else if (!strncasecmp(cmdbuf,"UOPN",4)) {
1009                         cmd_uopn(&cmdbuf[5]);
1010                         }
1011
1012                 else if (!strncasecmp(cmdbuf,"UCLS",4)) {
1013                         cmd_ucls(&cmdbuf[5]);
1014                         }
1015
1016                 else if (!strncasecmp(cmdbuf,"READ",4)) {
1017                         cmd_read(&cmdbuf[5]);
1018                         }
1019
1020                 else if (!strncasecmp(cmdbuf,"WRIT",4)) {
1021                         cmd_writ(&cmdbuf[5]);
1022                         }
1023
1024                 else if (!strncasecmp(cmdbuf,"QUSR",4)) {
1025                         cmd_qusr(&cmdbuf[5]);
1026                         }
1027
1028                 else if (!strncasecmp(cmdbuf,"ECHO",4)) {
1029                         cmd_echo(&cmdbuf[5]);
1030                         }
1031
1032                 else if (!strncasecmp(cmdbuf,"OIMG",4)) {
1033                         cmd_oimg(&cmdbuf[5]);
1034                         }
1035
1036                 else if (!strncasecmp(cmdbuf,"MORE",4)) {
1037                         cmd_more();
1038                         }
1039
1040                 else if (!strncasecmp(cmdbuf,"NETP",4)) {
1041                         cmd_netp(&cmdbuf[5]);
1042                         }
1043
1044                 else if (!strncasecmp(cmdbuf,"NDOP",4)) {
1045                         cmd_ndop(&cmdbuf[5]);
1046                         }
1047
1048                 else if (!strncasecmp(cmdbuf,"NUOP",4)) {
1049                         cmd_nuop(&cmdbuf[5]);
1050                         }
1051
1052                 else if (!strncasecmp(cmdbuf,"LFLR",4)) {
1053                         cmd_lflr();
1054                         }
1055
1056                 else if (!strncasecmp(cmdbuf,"CFLR",4)) {
1057                         cmd_cflr(&cmdbuf[5]);
1058                         }
1059
1060                 else if (!strncasecmp(cmdbuf,"KFLR",4)) {
1061                         cmd_kflr(&cmdbuf[5]);
1062                         }
1063
1064                 else if (!strncasecmp(cmdbuf,"EFLR",4)) {
1065                         cmd_eflr(&cmdbuf[5]);
1066                         }
1067
1068                 else if (!strncasecmp(cmdbuf,"IDEN",4)) {
1069                         cmd_iden(&cmdbuf[5]);
1070                         }
1071
1072                 else if (!strncasecmp(cmdbuf,"IPGM",4)) {
1073                         cmd_ipgm(&cmdbuf[5]);
1074                         }
1075
1076                 else if (!strncasecmp(cmdbuf,"EBIO",4)) {
1077                         cmd_ebio();
1078                         }
1079
1080                 else if (!strncasecmp(cmdbuf,"RBIO",4)) {
1081                         cmd_rbio(&cmdbuf[5]);
1082                         }
1083
1084                 else if (!strncasecmp(cmdbuf,"LBIO",4)) {
1085                         cmd_lbio();
1086                         }
1087
1088                 else if (!strncasecmp(cmdbuf,"STEL",4)) {
1089                         cmd_stel(&cmdbuf[5]);
1090                         }
1091
1092                 else if (!strncasecmp(cmdbuf,"TERM",4)) {
1093                         cmd_term(&cmdbuf[5]);
1094                         }
1095
1096                 else if (!strncasecmp(cmdbuf,"DOWN",4)) {
1097                         cmd_down();
1098                         }
1099
1100                 else if (!strncasecmp(cmdbuf,"SCDN",4)) {
1101                         cmd_scdn(&cmdbuf[5]);
1102                         }
1103
1104                 else if (!strncasecmp(cmdbuf, "NSET", 4)) {
1105                         cmd_nset(&cmdbuf[5]);
1106                         }
1107
1108                 else if (!strncasecmp(cmdbuf, "UIMG", 4)) {
1109                         cmd_uimg(&cmdbuf[5]);
1110                         }
1111
1112                 else if (!strncasecmp(cmdbuf, "UCHG", 4)) {
1113                         cmd_uchg(&cmdbuf[5]);
1114                         }
1115
1116                 else if (!strncasecmp(cmdbuf, "TIME", 4)) {
1117                         cmd_time();
1118                         }
1119
1120                 else if (!strncasecmp(cmdbuf, "HCHG", 4)) {
1121                         cmd_hchg(&cmdbuf[5]);
1122                         }
1123
1124                 else if (!strncasecmp(cmdbuf, "RCHG", 4)) {
1125                         cmd_rchg(&cmdbuf[5]);
1126                         }
1127
1128                 else if (!strncasecmp(cmdbuf, "AGUP", 4)) {
1129                         cmd_agup(&cmdbuf[5]);
1130                         }
1131
1132                 else if (!strncasecmp(cmdbuf, "ASUP", 4)) {
1133                         cmd_asup(&cmdbuf[5]);
1134                         }
1135
1136                 else if (!strncasecmp(cmdbuf, "GPEX", 4)) {
1137                         cmd_gpex(&cmdbuf[5]);
1138                         }
1139
1140                 else if (!strncasecmp(cmdbuf, "SPEX", 4)) {
1141                         cmd_spex(&cmdbuf[5]);
1142                         }
1143
1144                 else if (!strncasecmp(cmdbuf, "CONF", 4)) {
1145                         cmd_conf(&cmdbuf[5]);
1146                         }
1147
1148 #ifdef DEBUG_MEMORY_LEAKS
1149                 else if (!strncasecmp(cmdbuf, "LEAK", 4)) {
1150                         dump_tracked();
1151                         }
1152 #endif
1153
1154                 else if (!DLoader_Exec_Cmd(cmdbuf))
1155                         {
1156                            cprintf("%d Unrecognized or unsupported command.\n",
1157                                     ERROR);
1158                         }
1159
1160                 } while(strncasecmp(cmdbuf, "QUIT", 4));
1161
1162         cleanup(EXIT_NORMAL);
1163         return(NULL);
1164         }