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