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