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