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