* Added function CtdlGetDynamicSymbol() for dynamic symbol allocation
[citadel.git] / citadel / citserver.c
1 /* $Id$ */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <time.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <limits.h>
13 #ifdef HAVE_PTHREAD_H
14 #include <pthread.h>
15 #endif
16 #include <syslog.h>
17 #include <dlfcn.h>
18 #include <netdb.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include "sysdep_decls.h"
25 #include "citserver.h"
26 #include "config.h"
27 #include "database.h"
28 #include "housekeeping.h"
29 #include "user_ops.h"
30 #include "logging.h"
31 #include "support.h"
32 #include "msgbase.h"
33 #include "locate_host.h"
34 #include "room_ops.h"
35 #include "file_ops.h"
36 #include "dynloader.h"
37 #include "policy.h"
38 #include "control.h"
39 #include "tools.h"
40
41 struct CitContext *ContextList = NULL;
42 int ScheduledShutdown = 0;
43
44 /*
45  * Various things that need to be initialized at startup
46  */
47 void master_startup(void) {
48         lprintf(7, "Opening databases\n");
49         open_databases();
50
51         lprintf(7, "Checking floor reference counts\n");
52         check_ref_counts();
53
54         lprintf(7, "Creating base rooms (if necessary)\n");
55         create_room(BASEROOM, 0, "", 0);
56         create_room(AIDEROOM, 4, "", 0);
57         create_room(config.c_twitroom, 0, "", 0);
58         }
59
60 /*
61  * Cleanup routine to be called when the server is shutting down.
62  */
63 void master_cleanup(void) {
64         struct CleanupFunctionHook *fcn;
65
66         /* Cancel all running sessions */
67         lprintf(7, "Cancelling running sessions...\n");
68         while (ContextList != NULL) {
69                 kill_session(ContextList->cs_pid);
70                 }
71
72         /* Run any cleanup routines registered by loadable modules */
73         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
74                 (*fcn->h_function_pointer)();
75                 }
76
77         /* Close databases */
78         lprintf(7, "Closing databases\n");
79         close_databases();
80
81         /* Do system-dependent stuff */
82         sysdep_master_cleanup();
83
84         /* Now go away. */
85         lprintf(3, "citserver: exiting.\n");
86         fflush(stdout); fflush(stderr);
87         exit(0);
88         }
89
90
91 /*
92  * Free any per-session data allocated by modules or whatever
93  */
94 void deallocate_user_data(struct CitContext *con)
95 {
96         struct CtdlSessData *ptr;
97
98         begin_critical_section(S_SESSION_TABLE);
99         while (con->FirstSessData != NULL) {
100                 lprintf(9, "Deallocating user data symbol %ld\n",
101                         con->FirstSessData->sym_id);
102                 if (con->FirstSessData->sym_data != NULL)
103                         phree(con->FirstSessData->sym_data);
104                 ptr = con->FirstSessData->next;
105                 phree(con->FirstSessData);
106                 con->FirstSessData = ptr;
107         }
108         end_critical_section(S_SESSION_TABLE);
109 }
110
111
112
113
114 /*
115  * Gracefully terminate the session and thread.
116  * (This is called as a cleanup handler by the thread library.)
117  *
118  * All NON-system-dependent stuff is done in this function.
119  * System-dependent session/thread cleanup is in cleanup() in sysdep.c
120  */
121 void cleanup_stuff(void *arg)
122 {
123
124         lprintf(9, "cleanup_stuff() called\n");
125
126         lprintf(7, "Calling logout(%d)\n", CC->cs_pid);
127         logout(CC);
128
129         rec_log(CL_TERMINATE,CC->curr_user);
130         unlink(CC->temp);
131         lprintf(3, "citserver[%3d]: ended.\n",CC->cs_pid);
132         
133         /* Run any cleanup routines registered by loadable modules */
134         PerformSessionHooks(EVT_STOP);
135
136         syslog(LOG_NOTICE,"session %d ended", CC->cs_pid);
137         
138         /* Deallocate any message list we might have in memory */
139         if (CC->msglist != NULL) phree(CC->msglist);
140
141         /* Deallocate any user-data attached to this session */
142         deallocate_user_data(CC);
143
144         /* Now get rid of the session and context */
145         lprintf(7, "cleanup_stuff() calling RemoveContext(%d)\n", CC->cs_pid);
146         RemoveContext(CC);
147
148         /* While we still have an extra thread with no user attached to it,
149          * take the opportunity to do some housekeeping before exiting.
150          */
151         do_housekeeping();
152         }
153
154
155 /*
156  * Get a dynamic symbol number for per-session user data.
157  * This API call should be made only ONCE per symbol per citserver run.
158  */
159 int CtdlGetDynamicSymbol() 
160 {
161         static unsigned int next_symbol = SYM_MAX;
162         return ++next_symbol;
163 }
164
165
166
167 /*
168  * Return a pointer to some generic per-session user data.
169  * (This function returns NULL if the requested symbol is not allocated.)
170  *
171  * NOTE: we use critical sections for allocating and de-allocating these,
172  *       but not for locating one.
173  */
174 void *CtdlGetUserData(unsigned long requested_sym) 
175 {
176         struct CtdlSessData *ptr;
177
178         for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)
179                 if (ptr->sym_id == requested_sym)
180                         return(ptr->sym_data);
181
182         lprintf(2, "ERROR! CtdlGetUserData(%ld) symbol not allocated\n",
183                 requested_sym);
184         return NULL;
185 }
186
187
188 /*
189  * Allocate some generic per-session user data.
190  */
191 void CtdlAllocUserData(unsigned long requested_sym, size_t num_bytes)
192 {
193         struct CtdlSessData *ptr;
194
195         lprintf(9, "CtdlAllocUserData(%ld) called\n", requested_sym);
196
197         for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)  {
198                 if (ptr->sym_id == requested_sym) {
199                         lprintf(2, "ERROR: CtdlAllocUserData() requested for"
200                                 " symbol id %ld already registered\n", 
201                                 requested_sym);
202                         return;
203                 }
204         }
205
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         if ((strlen(from_host)>0) && 
436            (is_public_client(CC->cs_host))) {
437                 if (inet_aton(from_host, &addr))
438                         locate_host(CC->cs_host, &addr);
439                 else {
440                         strncpy(CC->cs_host,from_host,24);
441                         CC->cs_host[24] = 0;
442                         }
443                 }
444         set_wtmpsupp_to_current_room();
445
446         syslog(LOG_NOTICE,"client %d/%d/%01d.%02d (%s)\n",
447                 dev_code,
448                 cli_code,
449                 (rev_level / 100),
450                 (rev_level % 100),
451                 desc);
452                 cprintf("%d Ok\n",OK);
453         }
454
455
456 /*
457  * enter or exit "stealth mode"
458  */
459 void cmd_stel(char *cmdbuf)
460 {
461         int requested_mode;
462
463         requested_mode = extract_int(cmdbuf,0);
464         if (requested_mode !=0) requested_mode = 1;
465
466         if (!CC->logged_in) {
467                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
468                 return;
469                 }
470
471         if (CC->usersupp.axlevel < 6) {
472                 cprintf("%d You must be an Aide to use stealth mode.\n",
473                         ERROR+HIGHER_ACCESS_REQUIRED);
474                 return;
475                 }
476
477         if (CC->cs_flags & CS_STEALTH) {
478                 if (requested_mode == 0)
479                         CC->cs_flags = CC->cs_flags-CS_STEALTH;
480                 }
481         else {
482                 if (requested_mode == 1)
483                         CC->cs_flags = CC->cs_flags|CS_STEALTH;
484                 }
485
486         set_wtmpsupp_to_current_room();
487         cprintf("%d Ok\n",OK);
488         }
489
490
491
492
493 /*
494  * display system messages or help
495  */
496 void cmd_mesg(char *mname)
497 {
498         FILE *mfp;
499         char targ[256];
500         char buf[256];
501         char *dirs[2];
502
503         extract(buf,mname,0);
504
505
506         dirs[0]=mallok(64);
507         dirs[1]=mallok(64);
508         strcpy(dirs[0],"messages");
509         strcpy(dirs[1],"help");
510         mesg_locate(targ,buf,2,dirs);
511         phree(dirs[0]);
512         phree(dirs[1]);
513
514
515         if (strlen(targ)==0) {
516                 cprintf("%d '%s' not found.\n",ERROR,mname);
517                 return;
518                 }
519
520         mfp = fopen(targ,"r");
521         if (mfp==NULL) {
522                 cprintf("%d Cannot open '%s': %s\n",
523                         ERROR,targ,strerror(errno));
524                 return;
525                 }
526         cprintf("%d %s\n",LISTING_FOLLOWS,buf);
527
528         while (fgets(buf,255,mfp)!=NULL) {
529                 buf[strlen(buf)-1] = 0;
530                 do_help_subst(buf);
531                 cprintf("%s\n",buf);
532                 }
533
534         fclose(mfp);
535         cprintf("000\n");
536         }
537
538
539 /*
540  * enter system messages or help
541  */
542 void cmd_emsg(char *mname)
543 {
544         FILE *mfp;
545         char targ[256];
546         char buf[256];
547         char *dirs[2];
548         int a;
549
550         if (CC->usersupp.axlevel < 6) {
551                 cprintf("%d You must be an Aide to edit system messages.\n",
552                         ERROR+HIGHER_ACCESS_REQUIRED);
553                 return;
554                 }
555
556         extract(buf,mname,0);
557         for (a=0; a<strlen(buf); ++a) {         /* security measure */
558                 if (buf[a] == '/') buf[a] = '.';
559                 }
560
561         dirs[0]=mallok(64);
562         dirs[1]=mallok(64);
563         strcpy(dirs[0],"messages");
564         strcpy(dirs[1],"help");
565         mesg_locate(targ,buf,2,dirs);
566         phree(dirs[0]);
567         phree(dirs[1]);
568
569         if (strlen(targ)==0) {
570                 snprintf(targ, sizeof targ, "./help/%s", buf);
571                 }
572
573         mfp = fopen(targ,"w");
574         if (mfp==NULL) {
575                 cprintf("%d Cannot open '%s': %s\n",
576                         ERROR,targ,strerror(errno));
577                 return;
578                 }
579         cprintf("%d %s\n", SEND_LISTING, targ);
580
581         while (client_gets(buf), strcmp(buf, "000")) {
582                 fprintf(mfp, "%s\n", buf);
583                 }
584
585         fclose(mfp);
586         }
587
588
589 /*
590  * who's online
591  */
592 void cmd_rwho(void) {
593         struct CitContext *cptr;
594         int spoofed = 0;
595         int aide;
596         char un[40], room[40], host[40], flags[5];
597         
598         aide = CC->usersupp.axlevel >= 6;
599         cprintf("%d\n",LISTING_FOLLOWS);
600         
601         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) 
602         {
603                 flags[0] = '\0';
604                 spoofed = 0;
605                 
606                 if (cptr->cs_flags & CS_POSTING)
607                    strcat(flags, "*");
608                 else
609                    strcat(flags, ".");
610                    
611                 if (cptr->fake_username[0])
612                 {
613                    strcpy(un, cptr->fake_username);
614                    spoofed = 1;
615                 }
616                 else
617                    strcpy(un, cptr->curr_user);
618                    
619                 if (cptr->fake_hostname[0])
620                 {
621                    strcpy(host, cptr->fake_hostname);
622                    spoofed = 1;
623                 }
624                 else
625                    strcpy(host, cptr->cs_host);
626
627                 if (cptr->fake_roomname[0])
628                 {
629                    strcpy(room, cptr->fake_roomname);
630                    spoofed = 1;
631                 }
632                 else
633                    strcpy(room, cptr->cs_room);
634                    
635                 
636                 if ((aide) && (spoofed))
637                    strcat(flags, "+");
638                 
639                 if ((cptr->cs_flags & CS_STEALTH) && (aide))
640                    strcat(flags, "-");
641                 
642                 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
643                 {
644                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
645                                 cptr->cs_pid, un, room,
646                                 host, cptr->cs_clientname,
647                                 (long)(cptr->lastidle),
648                                 cptr->lastcmdname, flags);
649                 }
650                 if ((spoofed) && (aide))
651                 {
652                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
653                                 cptr->cs_pid, cptr->curr_user, cptr->cs_room,
654                                 cptr->cs_host, cptr->cs_clientname,
655                                 (long)(cptr->lastidle),
656                                 cptr->lastcmdname, flags);
657                 
658                 }
659         }
660         cprintf("000\n");
661         }
662
663
664 /*
665  * Terminate another running session
666  */
667 void cmd_term(char *cmdbuf)
668 {
669         int session_num;
670         struct CitContext *ccptr;
671         int session_to_kill = 0;
672
673         if (!CC->logged_in) {
674                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
675                 return;
676                 }
677
678         if (CC->usersupp.axlevel < 6) {
679                 cprintf("%d You must be an Aide to terminate sessions.\n",
680                         ERROR+HIGHER_ACCESS_REQUIRED);
681                 return;
682                 }
683
684         session_num = extract_int(cmdbuf, 0);
685         if (session_num == CC->cs_pid) {
686                 cprintf("%d You can't kill your own session.\n", ERROR);
687                 return;
688                 }
689
690         lprintf(9, "Locating session to kill\n");
691         begin_critical_section(S_SESSION_TABLE);
692         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
693                 if (session_num == ccptr->cs_pid) {
694                         session_to_kill = ccptr->cs_pid;
695                         }
696                 }
697         end_critical_section(S_SESSION_TABLE);
698         lprintf(9, "session_to_kill == %d\n", session_to_kill);
699
700         if (session_to_kill > 0) {
701                 lprintf(9, "calling kill_session()\n");
702                 kill_session(session_to_kill);
703                 cprintf("%d Session terminated.\n", OK);
704                 }
705         else {
706                 cprintf("%d No such session.\n", ERROR);
707                 }
708         }
709
710
711
712
713
714 /* 
715  * get the paginator prompt
716  */
717 void cmd_more(void) {
718         cprintf("%d %s\n",OK,config.c_moreprompt);
719         }
720
721 /*
722  * echo 
723  */
724 void cmd_echo(char *etext)
725 {
726         cprintf("%d %s\n",OK,etext);
727         }
728
729
730
731 /* 
732  * identify as internal program
733  */
734 void cmd_ipgm(char *argbuf)
735 {
736         int secret;
737
738         secret = extract_int(argbuf, 0);
739         if (secret == config.c_ipgm_secret) {
740                 CC->internal_pgm = 1;
741                 strcpy(CC->curr_user, "<internal program>");
742                 CC->cs_flags = CC->cs_flags|CS_STEALTH;
743                 cprintf("%d Authenticated as an internal program.\n",OK);
744                 }
745         else {
746                 cprintf("%d Authentication failed.\n",ERROR);
747                 }
748         }
749
750
751 /*
752  * Shut down the server
753  */
754 void cmd_down(void) {
755         if (!CC->logged_in) {
756                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
757                 return;
758                 }
759
760         if (CC->usersupp.axlevel < 6) {
761                 cprintf("%d You must be an Aide to shut down the server.\n",
762                         ERROR+HIGHER_ACCESS_REQUIRED);
763                 return;
764                 }
765
766         cprintf("%d Shutting down server.  Goodbye.\n", OK);
767         master_cleanup();
768         }
769
770 /*
771  * Schedule or cancel a server shutdown
772  */
773 void cmd_scdn(char *argbuf)
774 {
775         int new_state;
776
777         if (!CC->logged_in) {
778                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
779                 return;
780                 }
781
782         if (CC->usersupp.axlevel < 6) {
783                 cprintf("%d You must be an Aide to schedule a shutdown.\n",
784                         ERROR+HIGHER_ACCESS_REQUIRED);
785                 return;
786                 }
787
788         new_state = extract_int(argbuf, 0);
789         if ((new_state == 0) || (new_state == 1)) {
790                 ScheduledShutdown = new_state;
791                 }
792         cprintf("%d %d\n", OK, ScheduledShutdown);
793         }
794
795
796 /*
797  * main context loop
798  */
799 void *context_loop(struct CitContext *con)
800 {
801         char cmdbuf[256];
802         int num_sessions, len;
803         struct sockaddr_in sin;
804
805         /*
806          * Wedge our way into the context table.
807          */
808         InitMyContext(con);
809
810         /* 
811          * Initialize some variables specific to our context.
812          */
813         CC->logged_in = 0;
814         CC->internal_pgm = 0;
815         CC->download_fp = NULL;
816         CC->upload_fp = NULL;
817         CC->cs_pid = con->client_socket;        /* not necessarily portable */
818         CC->FirstExpressMessage = NULL;
819         CC->msglist = NULL;
820         CC->num_msgs = 0;
821         time(&CC->lastcmd);
822         time(&CC->lastidle);
823         strcpy(CC->lastcmdname, "    ");
824         strcpy(CC->cs_clientname, "(unknown)");
825         strcpy(CC->curr_user,"(not logged in)");
826         strcpy(CC->net_node,"");
827         snprintf(CC->temp, sizeof CC->temp, "/tmp/CitServer.%d.%d", getpid(), CC->cs_pid);
828         strcpy(CC->cs_room, "(no room)");
829         strncpy(CC->cs_host, config.c_fqdn, sizeof CC->cs_host);
830         CC->cs_host[sizeof CC->cs_host - 1] = 0;
831         len = sizeof sin;
832         if (!getpeername(CC->client_socket, (struct sockaddr *) &sin, &len))
833                 locate_host(CC->cs_host, &sin.sin_addr);
834         CC->cs_flags = 0;
835         CC->upload_type = UPL_FILE;
836         CC->dl_is_net = 0;
837         CC->FirstSessData = NULL;
838
839         num_sessions = session_count();
840         CC->nologin = 0;
841         if ((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions))
842                 CC->nologin = 1;
843
844         if (CC->nologin==1) {
845            cprintf("%d %s: Too many users are already online (maximum is %d)\n",
846                 ERROR+MAX_SESSIONS_EXCEEDED,
847                 config.c_nodename,config.c_maxsessions);
848                 }
849         else {
850            cprintf("%d %s Citadel/UX server ready.\n",OK,config.c_nodename);
851                 }
852
853         lprintf(3, "citserver[%3d]: started.\n", CC->cs_pid);
854
855         /* Run any session startup routines registered by loadable modules */
856         PerformSessionHooks(EVT_START);
857
858         rec_log(CL_CONNECT, "");
859
860         do {
861                 time(&CC->lastcmd);
862                 memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
863                 if (client_gets(cmdbuf) < 1) cleanup(EXIT_NULL);
864                 lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
865
866                 /*
867                  * Let other clients see the last command we executed, but
868                  * exclude NOOP because that would be boring.
869                  */
870                 if (strncasecmp(cmdbuf, "NOOP", 4)) {
871                         strcpy(CC->lastcmdname, "    ");
872                         strncpy(CC->lastcmdname, cmdbuf, 4);
873                         time(&CC->lastidle);
874                         }
875                         
876                 if ((strncasecmp(cmdbuf, "ENT0", 4)) && (strncasecmp(cmdbuf, "MESG", 4)) && (strncasecmp(cmdbuf, "MSGS", 4)))
877                 {
878                    CC->cs_flags &= ~CS_POSTING;
879                 }
880                    
881 /*
882  * This loop recognizes all server commands.
883  */
884
885                 if (!strncasecmp(cmdbuf,"NOOP",4)) {
886                         cprintf("%d%cok\n",OK,check_express());
887                         }
888
889                 else if (!strncasecmp(cmdbuf,"QUIT",4)) {
890                         cprintf("%d Goodbye.\n",OK);
891                         }
892
893                 else if (!strncasecmp(cmdbuf,"LOUT",4)) {
894                         if (CC->logged_in) logout(CC);
895                         cprintf("%d logged out.\n",OK);
896                         }
897
898                 else if (!strncasecmp(cmdbuf,"USER",4)) {
899                         cmd_user(&cmdbuf[5]);
900                         }
901
902                 else if (!strncasecmp(cmdbuf,"PASS",4)) {
903                         cmd_pass(&cmdbuf[5]);
904                         }
905
906                 else if (!strncasecmp(cmdbuf,"NEWU",4)) {
907                         cmd_newu(&cmdbuf[5]);
908                         }
909
910                 else if (!strncasecmp(cmdbuf,"SETP",4)) {
911                         cmd_setp(&cmdbuf[5]);
912                         }
913
914                 else if (!strncasecmp(cmdbuf,"LRMS",4)) {
915                         cmd_lrms(&cmdbuf[5]);
916                         }
917
918                 else if (!strncasecmp(cmdbuf,"LKRA",4)) {
919                         cmd_lkra(&cmdbuf[5]);
920                         }
921
922                 else if (!strncasecmp(cmdbuf,"LKRN",4)) {
923                         cmd_lkrn(&cmdbuf[5]);
924                         }
925
926                 else if (!strncasecmp(cmdbuf,"LKRO",4)) {
927                         cmd_lkro(&cmdbuf[5]);
928                         }
929
930                 else if (!strncasecmp(cmdbuf,"LZRM",4)) {
931                         cmd_lzrm(&cmdbuf[5]);
932                         }
933
934                 else if (!strncasecmp(cmdbuf,"GETU",4)) {
935                         cmd_getu();
936                         }
937
938                 else if (!strncasecmp(cmdbuf,"SETU",4)) {
939                         cmd_setu(&cmdbuf[5]);
940                         }
941
942                 else if (!strncasecmp(cmdbuf,"GOTO",4)) {
943                         cmd_goto(&cmdbuf[5]);
944                         }
945
946                 else if (!strncasecmp(cmdbuf,"MSGS",4)) {
947                         cmd_msgs(&cmdbuf[5]);
948                         }
949
950                 else if (!strncasecmp(cmdbuf,"WHOK",4)) {
951                         cmd_whok();
952                         }
953
954                 else if (!strncasecmp(cmdbuf,"RDIR",4)) {
955                         cmd_rdir();
956                         }
957
958                 else if (!strncasecmp(cmdbuf,"MSG0",4)) {
959                         cmd_msg0(&cmdbuf[5]);
960                         }
961
962                 else if (!strncasecmp(cmdbuf,"MSG2",4)) {
963                         cmd_msg2(&cmdbuf[5]);
964                         }
965
966                 else if (!strncasecmp(cmdbuf,"MSG3",4)) {
967                         cmd_msg3(&cmdbuf[5]);
968                         }
969
970                 else if (!strncasecmp(cmdbuf,"MSG4",4)) {
971                         cmd_msg4(&cmdbuf[5]);
972                         }
973
974                 else if (!strncasecmp(cmdbuf,"OPNA",4)) {
975                         cmd_opna(&cmdbuf[5]);
976                         }
977
978                 else if (!strncasecmp(cmdbuf,"INFO",4)) {
979                         cmd_info();
980                         }
981
982                 else if (!strncasecmp(cmdbuf,"SLRP",4)) {
983                         cmd_slrp(&cmdbuf[5]);
984                         }
985
986                 else if (!strncasecmp(cmdbuf,"INVT",4)) {
987                         cmd_invt_kick(&cmdbuf[5],1);
988                         }
989
990                 else if (!strncasecmp(cmdbuf,"KICK",4)) {
991                         cmd_invt_kick(&cmdbuf[5],0);
992                         }
993
994                 else if (!strncasecmp(cmdbuf,"GETR",4)) {
995                         cmd_getr();
996                         }
997
998                 else if (!strncasecmp(cmdbuf,"SETR",4)) {
999                         cmd_setr(&cmdbuf[5]);
1000                         }
1001
1002                 else if (!strncasecmp(cmdbuf,"GETA",4)) {
1003                         cmd_geta();
1004                         }
1005
1006                 else if (!strncasecmp(cmdbuf,"SETA",4)) {
1007                         cmd_seta(&cmdbuf[5]);
1008                         }
1009
1010                 else if (!strncasecmp(cmdbuf,"ENT0",4)) {
1011                         cmd_ent0(&cmdbuf[5]);
1012                         }
1013
1014                 else if (!strncasecmp(cmdbuf,"ENT3",4)) {
1015                         cmd_ent3(&cmdbuf[5]);
1016                         }
1017
1018                 else if (!strncasecmp(cmdbuf,"RINF",4)) {
1019                         cmd_rinf();
1020                         }
1021
1022                 else if (!strncasecmp(cmdbuf,"DELE",4)) {
1023                         cmd_dele(&cmdbuf[5]);
1024                         }
1025
1026                 else if (!strncasecmp(cmdbuf,"KILL",4)) {
1027                         cmd_kill(&cmdbuf[5]);
1028                         }
1029
1030                 else if (!strncasecmp(cmdbuf,"CRE8",4)) {
1031                         cmd_cre8(&cmdbuf[5]);
1032                         }
1033
1034                 else if (!strncasecmp(cmdbuf,"MOVE",4)) {
1035                         cmd_move(&cmdbuf[5]);
1036                         }
1037
1038                 else if (!strncasecmp(cmdbuf,"FORG",4)) {
1039                         cmd_forg();
1040                         }
1041
1042                 else if (!strncasecmp(cmdbuf,"MESG",4)) {
1043                         cmd_mesg(&cmdbuf[5]);
1044                         }
1045
1046                 else if (!strncasecmp(cmdbuf,"EMSG",4)) {
1047                         cmd_emsg(&cmdbuf[5]);
1048                         }
1049
1050                 else if (!strncasecmp(cmdbuf,"GNUR",4)) {
1051                         cmd_gnur();
1052                         }
1053
1054                 else if (!strncasecmp(cmdbuf,"GREG",4)) {
1055                         cmd_greg(&cmdbuf[5]);
1056                         }
1057
1058                 else if (!strncasecmp(cmdbuf,"VALI",4)) {
1059                         cmd_vali(&cmdbuf[5]);
1060                         }
1061
1062                 else if (!strncasecmp(cmdbuf,"EINF",4)) {
1063                         cmd_einf(&cmdbuf[5]);
1064                         }
1065
1066                 else if (!strncasecmp(cmdbuf,"LIST",4)) {
1067                         cmd_list();
1068                         }
1069
1070                 else if (!strncasecmp(cmdbuf,"REGI",4)) {
1071                         cmd_regi();
1072                         }
1073
1074                 else if (!strncasecmp(cmdbuf,"CHEK",4)) {
1075                         cmd_chek();
1076                         }
1077
1078                 else if (!strncasecmp(cmdbuf,"DELF",4)) {
1079                         cmd_delf(&cmdbuf[5]);
1080                         }
1081
1082                 else if (!strncasecmp(cmdbuf,"MOVF",4)) {
1083                         cmd_movf(&cmdbuf[5]);
1084                         }
1085
1086                 else if (!strncasecmp(cmdbuf,"NETF",4)) {
1087                         cmd_netf(&cmdbuf[5]);
1088                         }
1089
1090                 else if (!strncasecmp(cmdbuf,"RWHO",4)) {
1091                         cmd_rwho();
1092                         }
1093
1094                 else if (!strncasecmp(cmdbuf,"OPEN",4)) {
1095                         cmd_open(&cmdbuf[5]);
1096                         }
1097
1098                 else if (!strncasecmp(cmdbuf,"CLOS",4)) {
1099                         cmd_clos();
1100                         }
1101
1102                 else if (!strncasecmp(cmdbuf,"UOPN",4)) {
1103                         cmd_uopn(&cmdbuf[5]);
1104                         }
1105
1106                 else if (!strncasecmp(cmdbuf,"UCLS",4)) {
1107                         cmd_ucls(&cmdbuf[5]);
1108                         }
1109
1110                 else if (!strncasecmp(cmdbuf,"READ",4)) {
1111                         cmd_read(&cmdbuf[5]);
1112                         }
1113
1114                 else if (!strncasecmp(cmdbuf,"WRIT",4)) {
1115                         cmd_writ(&cmdbuf[5]);
1116                         }
1117
1118                 else if (!strncasecmp(cmdbuf,"QUSR",4)) {
1119                         cmd_qusr(&cmdbuf[5]);
1120                         }
1121
1122                 else if (!strncasecmp(cmdbuf,"ECHO",4)) {
1123                         cmd_echo(&cmdbuf[5]);
1124                         }
1125
1126                 else if (!strncasecmp(cmdbuf,"OIMG",4)) {
1127                         cmd_oimg(&cmdbuf[5]);
1128                         }
1129
1130                 else if (!strncasecmp(cmdbuf,"MORE",4)) {
1131                         cmd_more();
1132                         }
1133
1134                 else if (!strncasecmp(cmdbuf,"NETP",4)) {
1135                         cmd_netp(&cmdbuf[5]);
1136                         }
1137
1138                 else if (!strncasecmp(cmdbuf,"NDOP",4)) {
1139                         cmd_ndop(&cmdbuf[5]);
1140                         }
1141
1142                 else if (!strncasecmp(cmdbuf,"NUOP",4)) {
1143                         cmd_nuop(&cmdbuf[5]);
1144                         }
1145
1146                 else if (!strncasecmp(cmdbuf,"LFLR",4)) {
1147                         cmd_lflr();
1148                         }
1149
1150                 else if (!strncasecmp(cmdbuf,"CFLR",4)) {
1151                         cmd_cflr(&cmdbuf[5]);
1152                         }
1153
1154                 else if (!strncasecmp(cmdbuf,"KFLR",4)) {
1155                         cmd_kflr(&cmdbuf[5]);
1156                         }
1157
1158                 else if (!strncasecmp(cmdbuf,"EFLR",4)) {
1159                         cmd_eflr(&cmdbuf[5]);
1160                         }
1161
1162                 else if (!strncasecmp(cmdbuf,"IDEN",4)) {
1163                         cmd_iden(&cmdbuf[5]);
1164                         }
1165
1166                 else if (!strncasecmp(cmdbuf,"IPGM",4)) {
1167                         cmd_ipgm(&cmdbuf[5]);
1168                         }
1169
1170                 else if (!strncasecmp(cmdbuf,"EBIO",4)) {
1171                         cmd_ebio();
1172                         }
1173
1174                 else if (!strncasecmp(cmdbuf,"RBIO",4)) {
1175                         cmd_rbio(&cmdbuf[5]);
1176                         }
1177
1178                 else if (!strncasecmp(cmdbuf,"LBIO",4)) {
1179                         cmd_lbio();
1180                         }
1181
1182                 else if (!strncasecmp(cmdbuf,"STEL",4)) {
1183                         cmd_stel(&cmdbuf[5]);
1184                         }
1185
1186                 else if (!strncasecmp(cmdbuf,"TERM",4)) {
1187                         cmd_term(&cmdbuf[5]);
1188                         }
1189
1190                 else if (!strncasecmp(cmdbuf,"DOWN",4)) {
1191                         cmd_down();
1192                         }
1193
1194                 else if (!strncasecmp(cmdbuf,"SCDN",4)) {
1195                         cmd_scdn(&cmdbuf[5]);
1196                         }
1197
1198                 else if (!strncasecmp(cmdbuf, "NSET", 4)) {
1199                         cmd_nset(&cmdbuf[5]);
1200                         }
1201
1202                 else if (!strncasecmp(cmdbuf, "UIMG", 4)) {
1203                         cmd_uimg(&cmdbuf[5]);
1204                         }
1205
1206                 else if (!strncasecmp(cmdbuf, "UCHG", 4)) {
1207                         cmd_uchg(&cmdbuf[5]);
1208                         }
1209
1210                 else if (!strncasecmp(cmdbuf, "TIME", 4)) {
1211                         cmd_time();
1212                         }
1213
1214                 else if (!strncasecmp(cmdbuf, "HCHG", 4)) {
1215                         cmd_hchg(&cmdbuf[5]);
1216                         }
1217
1218                 else if (!strncasecmp(cmdbuf, "RCHG", 4)) {
1219                         cmd_rchg(&cmdbuf[5]);
1220                         }
1221
1222                 else if (!strncasecmp(cmdbuf, "AGUP", 4)) {
1223                         cmd_agup(&cmdbuf[5]);
1224                         }
1225
1226                 else if (!strncasecmp(cmdbuf, "ASUP", 4)) {
1227                         cmd_asup(&cmdbuf[5]);
1228                         }
1229
1230                 else if (!strncasecmp(cmdbuf, "GPEX", 4)) {
1231                         cmd_gpex(&cmdbuf[5]);
1232                         }
1233
1234                 else if (!strncasecmp(cmdbuf, "SPEX", 4)) {
1235                         cmd_spex(&cmdbuf[5]);
1236                         }
1237
1238                 else if (!strncasecmp(cmdbuf, "CONF", 4)) {
1239                         cmd_conf(&cmdbuf[5]);
1240                         }
1241
1242 #ifdef DEBUG_MEMORY_LEAKS
1243                 else if (!strncasecmp(cmdbuf, "LEAK", 4)) {
1244                         dump_tracked();
1245                         }
1246 #endif
1247
1248                 else if (!DLoader_Exec_Cmd(cmdbuf))
1249                         {
1250                            cprintf("%d Unrecognized or unsupported command.\n",
1251                                     ERROR);
1252                         }
1253
1254                 } while(strncasecmp(cmdbuf, "QUIT", 4));
1255
1256         cleanup(EXIT_NORMAL);
1257         return(NULL);
1258         }