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