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