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