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