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