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