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