]> code.citadel.org Git - citadel.git/blob - citadel/citserver.c
* Stabilize, dammit!!
[citadel.git] / citadel / citserver.c
1 /* 
2  * $Id$
3  *
4  * Main source module for the Citadel server
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <time.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <syslog.h>
20 /* #include <dlfcn.h> */
21 #include <netdb.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include "citadel.h"
28 #include "server.h"
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "config.h"
32 #include "database.h"
33 #include "housekeeping.h"
34 #include "user_ops.h"
35 #include "logging.h"
36 #include "msgbase.h"
37 #include "support.h"
38 #include "locate_host.h"
39 #include "room_ops.h"
40 #include "file_ops.h"
41 #include "dynloader.h"
42 #include "policy.h"
43 #include "control.h"
44 #include "tools.h"
45
46 struct CitContext *ContextList = NULL;
47 char *unique_session_numbers;
48 int ScheduledShutdown = 0;
49 int do_defrag = 0;
50
51 /*
52  * Various things that need to be initialized at startup
53  */
54 void master_startup(void) {
55         struct timeval tv;
56         
57         lprintf(9, "master_startup() started\n");
58         lprintf(7, "Opening databases\n");
59         open_databases();
60         cdb_begin_transaction();
61
62         if (do_defrag) {
63                 defrag_databases();
64         }
65
66         check_ref_counts();
67
68         lprintf(7, "Creating base rooms (if necessary)\n");
69         create_room(BASEROOM,           0, "", 0);
70         create_room(AIDEROOM,           3, "", 0);
71         create_room(SYSCONFIGROOM,      3, "", 0);
72         create_room(config.c_twitroom,  0, "", 0);
73
74         lprintf(7, "Seeding the pseudo-random number generator...\n");
75         gettimeofday(&tv, NULL);
76         srand(tv.tv_usec);
77         lprintf(9, "master_startup() finished\n");
78 }
79
80
81
82 /*
83  * Cleanup routine to be called when the server is shutting down.
84  */
85 void master_cleanup(void) {
86         struct CleanupFunctionHook *fcn;
87
88         /* Cancel all running sessions */
89         lprintf(7, "Cancelling running sessions...\n");
90
91 /* FIXME do something here
92         while (ContextList != NULL) {
93                 }
94  */
95
96         /* Run any cleanup routines registered by loadable modules */
97         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
98                 (*fcn->h_function_pointer)();
99         }
100
101         /* Close databases */
102         lprintf(7, "Closing databases\n");
103         cdb_end_transaction();
104         close_databases();
105
106         /* Do system-dependent stuff */
107         sysdep_master_cleanup();
108
109         /* Now go away. */
110         lprintf(3, "citserver: exiting.\n");
111         fflush(stdout); fflush(stderr);
112         exit(0);
113 }
114
115
116 /*
117  * Free any per-session data allocated by modules or whatever
118  */
119 void deallocate_user_data(struct CitContext *con)
120 {
121         struct CtdlSessData *ptr;
122
123         begin_critical_section(S_SESSION_TABLE);
124         while (con->FirstSessData != NULL) {
125                 lprintf(9, "Deallocating user data symbol %ld\n",
126                         con->FirstSessData->sym_id);
127                 if (con->FirstSessData->sym_data != NULL)
128                         phree(con->FirstSessData->sym_data);
129                 ptr = con->FirstSessData->next;
130                 phree(con->FirstSessData);
131                 con->FirstSessData = ptr;
132         }
133         end_critical_section(S_SESSION_TABLE);
134 }
135
136
137
138
139 /*
140  * Terminate a session and remove its context data structure.
141  */
142 void RemoveContext (struct CitContext *con)
143 {
144         struct CitContext *ptr = NULL;
145         struct CitContext *ToFree = NULL;
146
147         lprintf(9, "RemoveContext() called\n");
148         if (con==NULL) {
149                 lprintf(5, "WARNING: RemoveContext() called with NULL!\n");
150                 return;
151                 }
152
153         /* Remove the context from the global context list.  This needs
154          * to get done FIRST to avoid concurrency problems.  It is *vitally*
155          * important to keep num_sessions accurate!!
156          */
157         lprintf(7, "Removing context for session %d\n", con->cs_pid);
158         begin_critical_section(S_SESSION_TABLE);
159         if (ContextList == con) {
160                 ToFree = ContextList;
161                 ContextList = ContextList->next;
162                 --num_sessions;
163                 }
164         else {
165                 for (ptr = ContextList; ptr != NULL; ptr = ptr->next) {
166                         if (ptr->next == con) {
167                                 ToFree = ptr->next;
168                                 ptr->next = ptr->next->next;
169                                 --num_sessions;
170                                 }
171                         }
172                 }
173         end_critical_section(S_SESSION_TABLE);
174
175         if (ToFree == NULL) {
176                 lprintf(9, "RemoveContext() found nothing to remove\n");
177                 return;
178         }
179
180         /* Run any cleanup routines registered by loadable modules.
181          * Note 1: This must occur *before* deallocate_user_data() because the
182          *         cleanup functions might touch dynamic session data.
183          * Note 2: We have to "become_session()" because the cleanup functions
184          *         might make references to "CC" assuming it's the right one.
185          */
186         become_session(con);
187         PerformSessionHooks(EVT_STOP);
188         become_session(NULL);
189
190         /* Now handle all of the administrivia. */
191         lprintf(7, "Calling logout(%d)\n", con->cs_pid);
192         logout(con);
193
194         rec_log(CL_TERMINATE, con->curr_user);
195         unlink(con->temp);
196         lprintf(3, "citserver[%3d]: ended.\n", con->cs_pid);
197         
198
199         syslog(LOG_NOTICE,"session %d: ended", con->cs_pid);
200         
201         /* Deallocate any user-data attached to this session */
202         deallocate_user_data(con);
203
204         /* If the client is still connected, blow 'em away. */
205         lprintf(7, "Closing socket %d\n", con->client_socket);
206         close(con->client_socket);
207
208         /* This is where we used to check for scheduled shutdowns. */
209
210         /* Free up the memory used by this context */
211         phree(con);
212
213         lprintf(7, "Done with RemoveContext()\n");
214 }
215
216
217
218 /*
219  * Get a dynamic symbol number for per-session user data.
220  * This API call should be made only ONCE per symbol per citserver run.
221  */
222 int CtdlGetDynamicSymbol() 
223 {
224         static unsigned int next_symbol = SYM_MAX;
225         return ++next_symbol;
226 }
227
228
229
230 /*
231  * Return a pointer to some generic per-session user data.
232  * (This function returns NULL if the requested symbol is not allocated.)
233  *
234  * NOTE: we use critical sections for allocating and de-allocating these,
235  *       but not for locating one.
236  */
237 void *CtdlGetUserData(unsigned long requested_sym) 
238 {
239         struct CtdlSessData *ptr;
240
241         for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)
242                 if (ptr->sym_id == requested_sym)
243                         return(ptr->sym_data);
244
245         lprintf(2, "ERROR! CtdlGetUserData(%ld) symbol not allocated\n",
246                 requested_sym);
247         return NULL;
248 }
249
250
251 /*
252  * Allocate some generic per-session user data.
253  */
254 void CtdlAllocUserData(unsigned long requested_sym, size_t num_bytes)
255 {
256         struct CtdlSessData *ptr;
257
258         lprintf(9, "CtdlAllocUserData(%ld) called\n", requested_sym);
259
260         /* Fail silently if the symbol is already registered. */
261         for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)  {
262                 if (ptr->sym_id == requested_sym) {
263                         return;
264                 }
265         }
266
267         /* Grab us some memory!  Dem's good eatin' !!  */
268         ptr = mallok(sizeof(struct CtdlSessData));
269         ptr->sym_id = requested_sym;
270         ptr->sym_data = mallok(num_bytes);
271         memset(ptr->sym_data, 0, num_bytes);
272
273         begin_critical_section(S_SESSION_TABLE);
274         ptr->next = CC->FirstSessData;
275         CC->FirstSessData = ptr;
276         end_critical_section(S_SESSION_TABLE);
277
278         lprintf(9, "CtdlAllocUserData(%ld) finished\n", requested_sym);
279 }
280
281
282 /* 
283  * Change the size of a buffer allocated with CtdlAllocUserData()
284  */
285 void CtdlReallocUserData(unsigned long requested_sym, size_t num_bytes)
286 {
287         struct CtdlSessData *ptr;
288
289         for (ptr = CC->FirstSessData; ptr != NULL; ptr = ptr->next)  {
290                 if (ptr->sym_id == requested_sym) {
291                         ptr->sym_data = reallok(ptr->sym_data, num_bytes);
292                         return;
293                 }
294         }
295
296         lprintf(2, "CtdlReallocUserData() ERROR: symbol %ld not found!\n",
297                 requested_sym);
298 }
299
300
301
302
303
304
305 /*
306  * cmd_info()  -  tell the client about this server
307  */
308 void cmd_info(void) {
309         cprintf("%d Server info:\n", LISTING_FOLLOWS);
310         cprintf("%d\n", CC->cs_pid);
311         cprintf("%s\n", config.c_nodename);
312         cprintf("%s\n", config.c_humannode);
313         cprintf("%s\n", config.c_fqdn);
314         cprintf("%s\n", CITADEL);
315         cprintf("%d\n", REV_LEVEL);
316         cprintf("%s\n", config.c_bbs_city);
317         cprintf("%s\n", config.c_sysadm);
318         cprintf("%d\n", SERVER_TYPE);
319         cprintf("%s\n", config.c_moreprompt);
320         cprintf("1\n"); /* 1 = yes, this system supports floors */
321         cprintf("1\n"); /* 1 = we support the extended paging options */
322         cprintf("%s\n", CC->cs_nonce);
323         cprintf("000\n");
324 }
325
326
327 /*
328  * returns an asterisk if there are any express messages waiting,
329  * space otherwise.
330  */
331 char CtdlCheckExpress(void) {
332         if (CC->FirstExpressMessage == NULL) {
333                 return(' ');
334                 }
335         else {
336                 return('*');
337                 }
338         }
339
340 void cmd_time(void)
341 {
342    time_t tv;
343    
344    tv = time(NULL);
345    
346    cprintf("%d %ld\n", OK, tv);
347 }
348
349 /*
350  * Check whether two hostnames match.
351  * "Realname" should be an actual name of a client that is trying to connect;
352  * "testname" should be the value we are comparing it with. The idea is that we
353  * want to compare with both the abbreviated and fully-qualified versions of
354  * "testname;" some people define "localhost" as "localhost.foo.com," etc.
355  */
356 static int hostnames_match(const char *realname, const char *testname) {
357         struct hostent *he;
358         int retval = 0;
359
360         if (!strcasecmp(realname, testname))
361                 return 1;
362
363 #ifdef HAVE_NONREENTRANT_NETDB
364         begin_critical_section(S_NETDB);
365 #endif
366
367         if ((he = gethostbyname(testname)) != NULL)
368                 if (!strcasecmp(realname, he->h_name))
369                         retval = 1;
370
371 #ifdef HAVE_NONREENTRANT_NETDB
372         end_critical_section(S_NETDB);
373 #endif
374
375         return retval;
376         }
377
378 /*
379  * check a hostname against the public_clients file
380  */
381 int is_public_client(char *where)
382 {
383         char buf[256];
384         FILE *fp;
385
386         lprintf(9, "Checking whether %s is a public client\n", where);
387
388         if (hostnames_match(where, "localhost")) return(1);
389         if (hostnames_match(where, config.c_fqdn)) return(1);
390
391         fp = fopen("public_clients","r");
392         if (fp == NULL) return(0);
393
394         while (fgets(buf,256,fp)!=NULL) {
395                 while (isspace((buf[strlen(buf)-1]))) 
396                         buf[strlen(buf)-1] = 0;
397                 if (hostnames_match(where,buf)) {
398                         fclose(fp);
399                         return(1);
400                         }
401                 }
402
403         fclose(fp);
404         return(0);
405         }
406
407
408 /*
409  * the client is identifying itself to the server
410  */
411 void cmd_iden(char *argbuf)
412 {
413         int dev_code;
414         int cli_code;
415         int rev_level;
416         char desc[256];
417         char from_host[256];
418         struct in_addr addr;
419         int do_lookup = 0;
420
421         if (num_parms(argbuf)<4) {
422                 cprintf("%d usage error\n",ERROR);
423                 return;
424         }
425
426         dev_code = extract_int(argbuf,0);
427         cli_code = extract_int(argbuf,1);
428         rev_level = extract_int(argbuf,2);
429         extract(desc,argbuf,3);
430
431         safestrncpy(from_host, config.c_fqdn, sizeof from_host);
432         from_host[sizeof from_host - 1] = 0;
433         if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
434
435         CC->cs_clientdev = dev_code;
436         CC->cs_clienttyp = cli_code;
437         CC->cs_clientver = rev_level;
438         safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
439         CC->cs_clientname[31] = 0;
440
441         if (strlen(from_host) > 0) {
442                 if (CC->is_local_socket) do_lookup = 1;
443                 else if (is_public_client(CC->cs_host)) do_lookup = 1;
444         }
445
446         if (do_lookup) {
447                 lprintf(9, "Looking up hostname '%s'\n", from_host);
448                 if ((addr.s_addr = inet_addr(from_host)) != -1) {
449                         locate_host(CC->cs_host, &addr);
450                 }
451                 else {
452                         safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
453                         CC->cs_host[24] = 0;
454                 }
455         }
456
457         lprintf(7, "client %d/%d/%01d.%02d (%s)\n",
458                 dev_code,
459                 cli_code,
460                 (rev_level / 100),
461                 (rev_level % 100),
462                 desc);
463
464         syslog(LOG_NOTICE,"session %d: client %d/%d/%01d.%02d (%s) from %s\n",
465                 CC->cs_pid,
466                 dev_code,
467                 cli_code,
468                 (rev_level / 100),
469                 (rev_level % 100),
470                 desc,
471                 CC->cs_host);
472         cprintf("%d Ok\n",OK);
473 }
474
475
476 /*
477  * display system messages or help
478  */
479 void cmd_mesg(char *mname)
480 {
481         FILE *mfp;
482         char targ[256];
483         char buf[256];
484         char *dirs[2];
485
486         extract(buf,mname,0);
487
488
489         dirs[0]=mallok(64);
490         dirs[1]=mallok(64);
491         strcpy(dirs[0],"messages");
492         strcpy(dirs[1],"help");
493         mesg_locate(targ,buf,2,dirs);
494         phree(dirs[0]);
495         phree(dirs[1]);
496
497
498         if (strlen(targ)==0) {
499                 cprintf("%d '%s' not found.\n",ERROR,mname);
500                 return;
501                 }
502
503         mfp = fopen(targ,"r");
504         if (mfp==NULL) {
505                 cprintf("%d Cannot open '%s': %s\n",
506                         ERROR,targ,strerror(errno));
507                 return;
508                 }
509         cprintf("%d %s\n",LISTING_FOLLOWS,buf);
510
511         while (fgets(buf,255,mfp)!=NULL) {
512                 buf[strlen(buf)-1] = 0;
513                 do_help_subst(buf);
514                 cprintf("%s\n",buf);
515                 }
516
517         fclose(mfp);
518         cprintf("000\n");
519         }
520
521
522 /*
523  * enter system messages or help
524  */
525 void cmd_emsg(char *mname)
526 {
527         FILE *mfp;
528         char targ[256];
529         char buf[256];
530         char *dirs[2];
531         int a;
532
533         if (CtdlAccessCheck(ac_aide)) return;
534
535         extract(buf,mname,0);
536         for (a=0; a<strlen(buf); ++a) {         /* security measure */
537                 if (buf[a] == '/') buf[a] = '.';
538                 }
539
540         dirs[0]=mallok(64);
541         dirs[1]=mallok(64);
542         strcpy(dirs[0],"messages");
543         strcpy(dirs[1],"help");
544         mesg_locate(targ,buf,2,dirs);
545         phree(dirs[0]);
546         phree(dirs[1]);
547
548         if (strlen(targ)==0) {
549                 snprintf(targ, sizeof targ, "./help/%s", buf);
550                 }
551
552         mfp = fopen(targ,"w");
553         if (mfp==NULL) {
554                 cprintf("%d Cannot open '%s': %s\n",
555                         ERROR,targ,strerror(errno));
556                 return;
557                 }
558         cprintf("%d %s\n", SEND_LISTING, targ);
559
560         while (client_gets(buf), strcmp(buf, "000")) {
561                 fprintf(mfp, "%s\n", buf);
562                 }
563
564         fclose(mfp);
565         }
566
567
568 /* Don't show the names of private rooms unless the viewing
569  * user also knows the rooms.
570  */
571 void GenerateRoomDisplay(char *real_room,
572                         struct CitContext *viewed,
573                         struct CitContext *viewer) {
574
575         strcpy(real_room, viewed->quickroom.QRname);
576         if (viewed->quickroom.QRflags & QR_MAILBOX) {
577                 strcpy(real_room, &real_room[11]);
578         }
579         if (viewed->quickroom.QRflags & QR_PRIVATE) {
580                 if ( (CtdlRoomAccess(&viewed->quickroom, &viewer->usersupp)
581                    & UA_KNOWN) == 0) {
582                         strcpy(real_room, "<private room>");
583                 }
584         }
585
586         if (viewed->cs_flags & CS_CHAT) {
587                 while (strlen(real_room) < 14)
588                         strcat(real_room, " ");
589
590                 strcpy(&real_room[14], "<chat>");
591         }
592
593 }
594
595 /*
596  * Convenience function.
597  */
598 int CtdlAccessCheck(int required_level) {
599
600         if (CC->internal_pgm) return(0);
601         if (required_level >= ac_internal) {
602                 cprintf("%d This is not a user-level command.\n",
603                         ERROR+HIGHER_ACCESS_REQUIRED);
604                 return(-1);
605         }
606
607         if (CC->usersupp.axlevel >= 6) return(0);
608         if (required_level >= ac_aide) {
609                 cprintf("%d This command requires Aide access.\n",
610                         ERROR+HIGHER_ACCESS_REQUIRED);
611                 return(-1);
612         }
613
614         if (is_room_aide()) return(0);
615         if (required_level >= ac_room_aide) {
616                 cprintf("%d This command requires Aide or Room Aide access.\n",
617                         ERROR + HIGHER_ACCESS_REQUIRED);
618                 return(-1);
619         }
620
621         if (CC->logged_in) return(0);
622         if (required_level >= ac_logged_in) {
623                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
624                 return(-1);
625         }
626
627         /* shhh ... succeed quietly */
628         return(0);
629 }
630
631
632
633 /*
634  * Terminate another running session
635  */
636 void cmd_term(char *cmdbuf)
637 {
638         int session_num;
639         struct CitContext *ccptr;
640         int found_it = 0;
641
642         if (CtdlAccessCheck(ac_aide)) return;
643
644         session_num = extract_int(cmdbuf, 0);
645         if (session_num == CC->cs_pid) {
646                 cprintf("%d You can't kill your own session.\n", ERROR);
647                 return;
648                 }
649
650         lprintf(9, "Locating session to kill\n");
651         begin_critical_section(S_SESSION_TABLE);
652         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
653                 if (session_num == ccptr->cs_pid) {
654                         ccptr->kill_me = 1;
655                         found_it = 1;
656                         }
657                 }
658         end_critical_section(S_SESSION_TABLE);
659
660         if (found_it) {
661                 cprintf("%d Session terminated.\n", OK);
662                 }
663         else {
664                 cprintf("%d No such session.\n", ERROR);
665                 }
666         }
667
668
669
670
671
672 /* 
673  * get the paginator prompt
674  */
675 void cmd_more(void) {
676         cprintf("%d %s\n",OK,config.c_moreprompt);
677         }
678
679 /*
680  * echo 
681  */
682 void cmd_echo(char *etext)
683 {
684         cprintf("%d %s\n",OK,etext);
685         }
686
687
688
689 /* 
690  * identify as internal program
691  */
692 void cmd_ipgm(char *argbuf)
693 {
694         int secret;
695
696         secret = extract_int(argbuf, 0);
697         if (secret == config.c_ipgm_secret) {
698                 CC->internal_pgm = 1;
699                 strcpy(CC->curr_user, "<internal program>");
700                 CC->cs_flags = CC->cs_flags|CS_STEALTH;
701                 cprintf("%d Authenticated as an internal program.\n",OK);
702                 }
703         else {
704                 cprintf("%d Authentication failed.\n",ERROR);
705                 lprintf(3, "Warning: ipgm authentication failed.\n");
706                 }
707         }
708
709
710 /*
711  * Shut down the server
712  */
713 void cmd_down(void) {
714
715         if (CtdlAccessCheck(ac_aide)) return;
716
717         cprintf("%d Shutting down server.  Goodbye.\n", OK);
718         master_cleanup();
719         }
720
721 /*
722  * Schedule or cancel a server shutdown
723  */
724 void cmd_scdn(char *argbuf)
725 {
726         int new_state;
727
728         if (CtdlAccessCheck(ac_aide)) return;
729
730         new_state = extract_int(argbuf, 0);
731         if ((new_state == 0) || (new_state == 1)) {
732                 ScheduledShutdown = new_state;
733                 }
734         cprintf("%d %d\n", OK, ScheduledShutdown);
735         }
736
737
738 /*
739  * Generate a "nonce" for APOP-style authentication.
740  *
741  * RFC 1725 et al specify a PID to be placed in front of the nonce.
742  * Quoth BTX: That would be stupid.
743  */
744 void generate_nonce(struct CitContext *con) {
745         struct timeval tv;
746
747         memset(con->cs_nonce, NONCE_SIZE, 0);
748         gettimeofday(&tv, NULL);
749         memset(con->cs_nonce, NONCE_SIZE, 0);
750         snprintf(con->cs_nonce, NONCE_SIZE, "<%d%ld@%s>",
751                 rand(), tv.tv_usec, config.c_fqdn);
752 }
753
754
755
756
757 /*
758  * Back-end function for starting a session
759  */
760 void begin_session(struct CitContext *con)
761 {
762         int len;
763         struct sockaddr_in sin;
764
765         /* 
766          * Initialize some variables specific to our context.
767          */
768         con->logged_in = 0;
769         con->internal_pgm = 0;
770         con->download_fp = NULL;
771         con->upload_fp = NULL;
772         con->FirstExpressMessage = NULL;
773         time(&con->lastcmd);
774         time(&con->lastidle);
775         strcpy(con->lastcmdname, "    ");
776         strcpy(con->cs_clientname, "(unknown)");
777         strcpy(con->curr_user, NLI);
778         strcpy(con->net_node,"");
779         strcpy(con->fake_username, "");
780         strcpy(con->fake_postname, "");
781         strcpy(con->fake_hostname, "");
782         strcpy(con->fake_roomname, "");
783         generate_nonce(con);
784         snprintf(con->temp, sizeof con->temp, tmpnam(NULL));
785         safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
786         con->cs_host[sizeof con->cs_host - 1] = 0;
787         len = sizeof sin;
788         if (!CC->is_local_socket) {
789                 if (!getpeername(con->client_socket,
790                    (struct sockaddr *) &sin, &len))
791                         locate_host(con->cs_host, &sin.sin_addr);
792         }
793         else {
794                 strcpy(con->cs_host, "");
795         }
796         con->cs_flags = 0;
797         con->upload_type = UPL_FILE;
798         con->dl_is_net = 0;
799         con->FirstSessData = NULL;
800
801         con->nologin = 0;
802         if ((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions))
803                 con->nologin = 1;
804
805         lprintf(3, "citserver[%3d]: started.\n", con->cs_pid);
806
807         /* Run any session startup routines registered by loadable modules */
808         PerformSessionHooks(EVT_START);
809
810         rec_log(CL_CONNECT, "");
811 }
812
813
814 void citproto_begin_session() {
815         if (CC->nologin==1) {
816                 cprintf("%d %s: Too many users are already online "
817                         "(maximum is %d)\n",
818                         ERROR+MAX_SESSIONS_EXCEEDED,
819                         config.c_nodename, config.c_maxsessions);
820                 }
821         else {
822                 cprintf("%d %s Citadel/UX server ready.\n",
823                         OK, config.c_nodename);
824                 }
825 }
826
827
828
829
830 /*
831  * This loop recognizes all server commands.
832  */
833 void do_command_loop(void) {
834         char cmdbuf[256];
835
836         time(&CC->lastcmd);
837         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
838         if (client_gets(cmdbuf) < 1) {
839                 lprintf(3, "Client socket is broken.  Ending session.\n");
840                 CC->kill_me = 1;
841                 return;
842         }
843         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
844
845         /*
846          * Let other clients see the last command we executed, and
847          * update the idle time, but not NOOP, PEXP, or GEXP.
848          */
849         if ( (strncasecmp(cmdbuf, "NOOP", 4))
850            && (strncasecmp(cmdbuf, "PEXP", 4))
851            && (strncasecmp(cmdbuf, "GEXP", 4)) ) {
852                 strcpy(CC->lastcmdname, "    ");
853                 safestrncpy(CC->lastcmdname, cmdbuf, 
854                         sizeof(CC->lastcmdname) );
855                 time(&CC->lastidle);
856                 }
857                 
858         if ((strncasecmp(cmdbuf, "ENT0", 4))
859            && (strncasecmp(cmdbuf, "MESG", 4))
860            && (strncasecmp(cmdbuf, "MSGS", 4)))
861         {
862            CC->cs_flags &= ~CS_POSTING;
863         }
864                    
865         if (!strncasecmp(cmdbuf,"NOOP",4)) {
866                 cprintf("%d%cok\n",OK,CtdlCheckExpress());
867                 }
868
869         else if (!strncasecmp(cmdbuf,"QUIT",4)) {
870                 cprintf("%d Goodbye.\n",OK);
871                 CC->kill_me = 1;
872                 }
873
874         else if (!strncasecmp(cmdbuf,"LOUT",4)) {
875                 if (CC->logged_in) logout(CC);
876                 cprintf("%d logged out.\n",OK);
877                 }
878
879         else if (!strncasecmp(cmdbuf,"USER",4)) {
880                 cmd_user(&cmdbuf[5]);
881                 }
882
883         else if (!strncasecmp(cmdbuf,"PASS",4)) {
884                 cmd_pass(&cmdbuf[5]);
885                 }
886
887         else if (!strncasecmp(cmdbuf,"NEWU",4)) {
888                 cmd_newu(&cmdbuf[5]);
889                 }
890
891         else if (!strncasecmp(cmdbuf,"SETP",4)) {
892                 cmd_setp(&cmdbuf[5]);
893                 }
894
895         else if (!strncasecmp(cmdbuf,"LRMS",4)) {
896                 cmd_lrms(&cmdbuf[5]);
897                 }
898
899         else if (!strncasecmp(cmdbuf,"LKRA",4)) {
900                 cmd_lkra(&cmdbuf[5]);
901                 }
902
903         else if (!strncasecmp(cmdbuf,"LKRN",4)) {
904                 cmd_lkrn(&cmdbuf[5]);
905                 }
906
907         else if (!strncasecmp(cmdbuf,"LKRO",4)) {
908                 cmd_lkro(&cmdbuf[5]);
909                 }
910
911         else if (!strncasecmp(cmdbuf,"LZRM",4)) {
912                 cmd_lzrm(&cmdbuf[5]);
913                 }
914
915         else if (!strncasecmp(cmdbuf,"GETU",4)) {
916                 cmd_getu();
917                 }
918
919         else if (!strncasecmp(cmdbuf,"SETU",4)) {
920                 cmd_setu(&cmdbuf[5]);
921                 }
922
923         else if (!strncasecmp(cmdbuf,"GOTO",4)) {
924                 cmd_goto(&cmdbuf[5]);
925                 }
926
927         else if (!strncasecmp(cmdbuf,"MSGS",4)) {
928                 cmd_msgs(&cmdbuf[5]);
929                 }
930
931         else if (!strncasecmp(cmdbuf,"WHOK",4)) {
932                 cmd_whok();
933                 }
934
935         else if (!strncasecmp(cmdbuf,"RDIR",4)) {
936                 cmd_rdir();
937                 }
938
939         else if (!strncasecmp(cmdbuf,"MSG0",4)) {
940                 cmd_msg0(&cmdbuf[5]);
941                 }
942
943         else if (!strncasecmp(cmdbuf,"MSG2",4)) {
944                 cmd_msg2(&cmdbuf[5]);
945                 }
946
947         else if (!strncasecmp(cmdbuf,"MSG3",4)) {
948                 cmd_msg3(&cmdbuf[5]);
949                 }
950
951         else if (!strncasecmp(cmdbuf,"MSG4",4)) {
952                 cmd_msg4(&cmdbuf[5]);
953                 }
954
955         else if (!strncasecmp(cmdbuf,"OPNA",4)) {
956                 cmd_opna(&cmdbuf[5]);
957                 }
958
959         else if (!strncasecmp(cmdbuf,"INFO",4)) {
960                 cmd_info();
961                 }
962
963         else if (!strncasecmp(cmdbuf,"SLRP",4)) {
964                 cmd_slrp(&cmdbuf[5]);
965                 }
966
967         else if (!strncasecmp(cmdbuf,"INVT",4)) {
968                 cmd_invt_kick(&cmdbuf[5],1);
969                 }
970
971         else if (!strncasecmp(cmdbuf,"KICK",4)) {
972                 cmd_invt_kick(&cmdbuf[5],0);
973                 }
974
975         else if (!strncasecmp(cmdbuf,"GETR",4)) {
976                 cmd_getr();
977                 }
978
979         else if (!strncasecmp(cmdbuf,"SETR",4)) {
980                 cmd_setr(&cmdbuf[5]);
981                 }
982
983         else if (!strncasecmp(cmdbuf,"GETA",4)) {
984                 cmd_geta();
985                 }
986
987         else if (!strncasecmp(cmdbuf,"SETA",4)) {
988                 cmd_seta(&cmdbuf[5]);
989                 }
990
991         else if (!strncasecmp(cmdbuf,"ENT0",4)) {
992                 cmd_ent0(&cmdbuf[5]);
993                 }
994
995         else if (!strncasecmp(cmdbuf,"ENT3",4)) {
996                 cmd_ent3(&cmdbuf[5]);
997                 }
998
999         else if (!strncasecmp(cmdbuf,"RINF",4)) {
1000                 cmd_rinf();
1001                 }
1002
1003         else if (!strncasecmp(cmdbuf,"DELE",4)) {
1004                 cmd_dele(&cmdbuf[5]);
1005                 }
1006
1007         else if (!strncasecmp(cmdbuf,"KILL",4)) {
1008                 cmd_kill(&cmdbuf[5]);
1009                 }
1010
1011         else if (!strncasecmp(cmdbuf,"CRE8",4)) {
1012                 cmd_cre8(&cmdbuf[5]);
1013                 }
1014
1015         else if (!strncasecmp(cmdbuf,"MOVE",4)) {
1016                 cmd_move(&cmdbuf[5]);
1017                 }
1018
1019         else if (!strncasecmp(cmdbuf,"FORG",4)) {
1020                 cmd_forg();
1021                 }
1022
1023         else if (!strncasecmp(cmdbuf,"MESG",4)) {
1024                 cmd_mesg(&cmdbuf[5]);
1025                 }
1026
1027         else if (!strncasecmp(cmdbuf,"EMSG",4)) {
1028                 cmd_emsg(&cmdbuf[5]);
1029                 }
1030
1031         else if (!strncasecmp(cmdbuf,"GNUR",4)) {
1032                 cmd_gnur();
1033                 }
1034
1035         else if (!strncasecmp(cmdbuf,"VALI",4)) {
1036                 cmd_vali(&cmdbuf[5]);
1037                 }
1038
1039         else if (!strncasecmp(cmdbuf,"EINF",4)) {
1040                 cmd_einf(&cmdbuf[5]);
1041                 }
1042
1043         else if (!strncasecmp(cmdbuf,"LIST",4)) {
1044                 cmd_list();
1045                 }
1046
1047         else if (!strncasecmp(cmdbuf,"CHEK",4)) {
1048                 cmd_chek();
1049                 }
1050
1051         else if (!strncasecmp(cmdbuf,"DELF",4)) {
1052                 cmd_delf(&cmdbuf[5]);
1053                 }
1054
1055         else if (!strncasecmp(cmdbuf,"MOVF",4)) {
1056                 cmd_movf(&cmdbuf[5]);
1057                 }
1058
1059         else if (!strncasecmp(cmdbuf,"NETF",4)) {
1060                 cmd_netf(&cmdbuf[5]);
1061                 }
1062
1063         else if (!strncasecmp(cmdbuf,"OPEN",4)) {
1064                 cmd_open(&cmdbuf[5]);
1065                 }
1066
1067         else if (!strncasecmp(cmdbuf,"CLOS",4)) {
1068                 cmd_clos();
1069                 }
1070
1071         else if (!strncasecmp(cmdbuf,"UOPN",4)) {
1072                 cmd_uopn(&cmdbuf[5]);
1073                 }
1074
1075         else if (!strncasecmp(cmdbuf,"UCLS",4)) {
1076                 cmd_ucls(&cmdbuf[5]);
1077                 }
1078
1079         else if (!strncasecmp(cmdbuf,"READ",4)) {
1080                 cmd_read(&cmdbuf[5]);
1081                 }
1082
1083         else if (!strncasecmp(cmdbuf,"WRIT",4)) {
1084                 cmd_writ(&cmdbuf[5]);
1085                 }
1086
1087         else if (!strncasecmp(cmdbuf,"QUSR",4)) {
1088                 cmd_qusr(&cmdbuf[5]);
1089                 }
1090
1091         else if (!strncasecmp(cmdbuf,"ECHO",4)) {
1092                 cmd_echo(&cmdbuf[5]);
1093                 }
1094
1095         else if (!strncasecmp(cmdbuf,"OIMG",4)) {
1096                 cmd_oimg(&cmdbuf[5]);
1097                 }
1098
1099         else if (!strncasecmp(cmdbuf,"MORE",4)) {
1100                 cmd_more();
1101                 }
1102
1103         else if (!strncasecmp(cmdbuf,"NETP",4)) {
1104                 cmd_netp(&cmdbuf[5]);
1105                 }
1106
1107         else if (!strncasecmp(cmdbuf,"NDOP",4)) {
1108                 cmd_ndop(&cmdbuf[5]);
1109                 }
1110
1111         else if (!strncasecmp(cmdbuf,"NUOP",4)) {
1112                 cmd_nuop(&cmdbuf[5]);
1113                 }
1114
1115         else if (!strncasecmp(cmdbuf,"LFLR",4)) {
1116                 cmd_lflr();
1117                 }
1118
1119         else if (!strncasecmp(cmdbuf,"CFLR",4)) {
1120                 cmd_cflr(&cmdbuf[5]);
1121                 }
1122
1123         else if (!strncasecmp(cmdbuf,"KFLR",4)) {
1124                 cmd_kflr(&cmdbuf[5]);
1125                 }
1126
1127         else if (!strncasecmp(cmdbuf,"EFLR",4)) {
1128                 cmd_eflr(&cmdbuf[5]);
1129                 }
1130
1131         else if (!strncasecmp(cmdbuf,"IDEN",4)) {
1132                 cmd_iden(&cmdbuf[5]);
1133                 }
1134
1135         else if (!strncasecmp(cmdbuf,"IPGM",4)) {
1136                 cmd_ipgm(&cmdbuf[5]);
1137                 }
1138
1139         else if (!strncasecmp(cmdbuf,"TERM",4)) {
1140                 cmd_term(&cmdbuf[5]);
1141                 }
1142
1143         else if (!strncasecmp(cmdbuf,"DOWN",4)) {
1144                 cmd_down();
1145                 }
1146
1147         else if (!strncasecmp(cmdbuf,"SCDN",4)) {
1148                 cmd_scdn(&cmdbuf[5]);
1149                 }
1150
1151         else if (!strncasecmp(cmdbuf, "NSET", 4)) {
1152                 cmd_nset(&cmdbuf[5]);
1153                 }
1154
1155         else if (!strncasecmp(cmdbuf, "UIMG", 4)) {
1156                 cmd_uimg(&cmdbuf[5]);
1157                 }
1158
1159         else if (!strncasecmp(cmdbuf, "TIME", 4)) {
1160                 cmd_time();
1161                 }
1162
1163         else if (!strncasecmp(cmdbuf, "AGUP", 4)) {
1164                 cmd_agup(&cmdbuf[5]);
1165                 }
1166
1167         else if (!strncasecmp(cmdbuf, "ASUP", 4)) {
1168                 cmd_asup(&cmdbuf[5]);
1169                 }
1170
1171         else if (!strncasecmp(cmdbuf, "GPEX", 4)) {
1172                 cmd_gpex(&cmdbuf[5]);
1173                 }
1174
1175         else if (!strncasecmp(cmdbuf, "SPEX", 4)) {
1176                 cmd_spex(&cmdbuf[5]);
1177                 }
1178
1179         else if (!strncasecmp(cmdbuf, "CONF", 4)) {
1180                 cmd_conf(&cmdbuf[5]);
1181                 }
1182
1183 #ifdef DEBUG_MEMORY_LEAKS
1184         else if (!strncasecmp(cmdbuf, "LEAK", 4)) {
1185                 dump_tracked();
1186                 }
1187 #endif
1188
1189         else if (!DLoader_Exec_Cmd(cmdbuf))
1190                 {
1191                    cprintf("%d Unrecognized or unsupported command.\n",
1192                             ERROR);
1193                 }
1194
1195         /* Run any after-each-command outines registered by modules */
1196         PerformSessionHooks(EVT_CMD);
1197 }