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