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