]> code.citadel.org Git - citadel.git/blob - citadel/citserver.c
* Finished removing all the "dynamic session data" stuff in order to
[citadel.git] / citadel / citserver.c
1 /* 
2  * $Id$
3  *
4  * Main source module for the Citadel server
5  *
6  */
7
8 #ifdef DLL_EXPORT
9 #define IN_LIBCIT
10 #endif
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <ctype.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <limits.h>
37 /* #include <dlfcn.h> */
38 #include <netdb.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
43 #include "citadel.h"
44 #include "server.h"
45 #include "serv_extensions.h"
46 #include "sysdep_decls.h"
47 #include "citserver.h"
48 #include "config.h"
49 #include "database.h"
50 #include "housekeeping.h"
51 #include "user_ops.h"
52 #include "msgbase.h"
53 #include "support.h"
54 #include "locate_host.h"
55 #include "room_ops.h"
56 #include "file_ops.h"
57 #include "policy.h"
58 #include "control.h"
59 #include "tools.h"
60
61 #ifndef HAVE_SNPRINTF
62 #include "snprintf.h"
63 #endif
64
65 struct CitContext *ContextList = NULL;
66 struct CitContext* next_session = NULL;
67 char *unique_session_numbers;
68 int ScheduledShutdown = 0;
69 int do_defrag = 0;
70 time_t server_startup_time;
71 char pid_file_name[PATH_MAX];
72
73 /*
74  * Various things that need to be initialized at startup
75  */
76 void master_startup(void) {
77         struct timeval tv;
78         unsigned int seed;
79         FILE *urandom;
80         struct ctdlroom qrbuf;
81         FILE *pidfile_fp;
82         
83         lprintf(CTDL_DEBUG, "master_startup() started\n");
84         time(&server_startup_time);
85
86         /* pid file.  If we go FSSTND this should end up in 'localstatedir' */
87         snprintf(pid_file_name, sizeof pid_file_name, "./citadel.pid");
88         pidfile_fp = fopen(pid_file_name, "w");
89         if (pidfile_fp != NULL) {
90                 fprintf(pidfile_fp, "%d\n", (int)getpid());
91                 fclose(pidfile_fp);
92         }
93
94         lprintf(CTDL_INFO, "Opening databases\n");
95         open_databases();
96
97         if (do_defrag) {
98                 defrag_databases();
99         }
100
101         check_ref_counts();
102
103         lprintf(CTDL_INFO, "Creating base rooms (if necessary)\n");
104         create_room(BASEROOM,           0, "", 0, 1, 0, VIEW_BBS);
105         create_room(AIDEROOM,           3, "", 0, 1, 0, VIEW_BBS);
106         create_room(SYSCONFIGROOM,      3, "", 0, 1, 0, VIEW_BBS);
107         create_room(config.c_twitroom,  0, "", 0, 1, 0, VIEW_BBS);
108
109         /* The "Local System Configuration" room doesn't need to be visible */
110         if (lgetroom(&qrbuf, SYSCONFIGROOM) == 0) {
111                 qrbuf.QRflags2 |= QR2_SYSTEM;
112                 lputroom(&qrbuf);
113         }
114
115         lprintf(CTDL_INFO, "Seeding the pseudo-random number generator...\n");
116         urandom = fopen("/dev/urandom", "r");
117         if (urandom != NULL) {
118                 fread(&seed, sizeof seed, 1, urandom);
119                 fclose(urandom);
120         }
121         else {
122                 gettimeofday(&tv, NULL);
123                 seed = tv.tv_usec;
124         }
125         srandom(seed);
126
127         lprintf(CTDL_INFO, "Initializing ipgm secret\n");
128         get_config();
129         config.c_ipgm_secret = rand();
130         put_config();
131
132         lprintf(CTDL_DEBUG, "master_startup() finished\n");
133 }
134
135
136 /*
137  * Cleanup routine to be called when the server is shutting down.
138  */
139 void master_cleanup(int exitcode) {
140         struct CleanupFunctionHook *fcn;
141         static int already_cleaning_up = 0;
142
143         if (already_cleaning_up) while(1) sleep(1);
144         already_cleaning_up = 1;
145
146         /* Run any cleanup routines registered by loadable modules */
147         for (fcn = CleanupHookTable; fcn != NULL; fcn = fcn->next) {
148                 (*fcn->h_function_pointer)();
149         }
150
151         /* Close databases */
152         lprintf(CTDL_INFO, "Closing databases\n");
153         close_databases();
154
155         /* Do system-dependent stuff */
156         sysdep_master_cleanup();
157
158 #ifdef DEBUG_MEMORY_LEAKS
159         dump_heap();
160 #endif
161
162         /* Now go away. */
163         lprintf(CTDL_NOTICE, "citserver: Exiting with status %d\n", exitcode);
164         fflush(stdout); fflush(stderr);
165
166         unlink(pid_file_name);
167         exit(exitcode);
168 }
169
170
171
172 /*
173  * Terminate a session and remove its context data structure.
174  */
175 void RemoveContext (struct CitContext *con)
176 {
177         if (con==NULL) {
178                 lprintf(CTDL_ERR, "WARNING: RemoveContext() called with NULL!\n");
179                 return;
180         }
181         lprintf(CTDL_DEBUG, "RemoveContext() called\n");
182
183         /* Remove the context from the global context list.  This needs
184          * to get done FIRST to avoid concurrency problems.  It is *vitally*
185          * important to keep num_sessions accurate!!
186          */
187         lprintf(CTDL_DEBUG, "Removing context for session %d\n", con->cs_pid);
188         begin_critical_section(S_SESSION_TABLE);
189         if (con->prev) {
190                 con->prev->next = con->next;
191         }
192         else {
193                 ContextList = con->next;
194         }
195         if (con->next) {
196                 con->next->prev = con->prev;
197         }
198         --num_sessions;
199         end_critical_section(S_SESSION_TABLE);
200
201         /* Run any cleanup routines registered by loadable modules.
202          * Note: We have to "become_session()" because the cleanup functions
203          *       might make references to "CC" assuming it's the right one.
204          */
205         become_session(con);
206         PerformSessionHooks(EVT_STOP);
207         become_session(NULL);
208
209         /* Now handle all of the administrivia. */
210         lprintf(CTDL_DEBUG, "Calling logout(%d)\n", con->cs_pid);
211         logout(con);
212
213         unlink(con->temp);
214         lprintf(CTDL_NOTICE, "[%3d] Session ended.\n", con->cs_pid);
215
216         /* If the client is still connected, blow 'em away. */
217         lprintf(CTDL_DEBUG, "Closing socket %d\n", con->client_socket);
218         close(con->client_socket);
219
220         /* This is where we used to check for scheduled shutdowns. */
221
222         /* Free up the memory used by this context */
223         free(con);
224
225         lprintf(CTDL_DEBUG, "Done with RemoveContext()\n");
226 }
227
228
229
230
231
232 /*
233  * cmd_info()  -  tell the client about this server
234  */
235 void cmd_info(void) {
236         cprintf("%d Server info:\n", LISTING_FOLLOWS);
237         cprintf("%d\n", CC->cs_pid);
238         cprintf("%s\n", config.c_nodename);
239         cprintf("%s\n", config.c_humannode);
240         cprintf("%s\n", config.c_fqdn);
241         cprintf("%s\n", CITADEL);
242         cprintf("%d\n", REV_LEVEL);
243         cprintf("%s\n", config.c_bbs_city);
244         cprintf("%s\n", config.c_sysadm);
245         cprintf("%d\n", SERVER_TYPE);
246         cprintf("%s\n", config.c_moreprompt);
247         cprintf("1\n"); /* 1 = yes, this system supports floors */
248         cprintf("1\n"); /* 1 = we support the extended paging options */
249         cprintf("%s\n", CC->cs_nonce);
250         cprintf("1\n"); /* 1 = yes, this system supports the QNOP command */
251 #ifdef HAVE_LDAP
252         cprintf("1\n"); /* 1 = yes, this server is LDAP-enabled */
253 #else
254         cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
255 #endif
256         cprintf("000\n");
257 }
258
259
260 /*
261  * returns an asterisk if there are any instant messages waiting,
262  * space otherwise.
263  */
264 char CtdlCheckExpress(void) {
265         if (CC->FirstExpressMessage == NULL) {
266                 return(' ');
267         }
268         else {
269                 return('*');
270         }
271 }
272
273 void cmd_time(void)
274 {
275    time_t tv;
276    struct tm tmp;
277    
278    tv = time(NULL);
279    localtime_r(&tv, &tmp);
280    
281    /* timezone and daylight global variables are not portable. */
282 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
283    cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst);
284 #else
285    cprintf("%d %ld|%ld|%d\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst);
286 #endif
287 }
288
289
290 /*
291  * Check originating host against the public_clients file.  This determines
292  * whether the client is allowed to change the hostname for this session
293  * (for example, to show the location of the user rather than the location
294  * of the client).
295  */
296 int is_public_client(void)
297 {
298         char buf[SIZ];
299         char addrbuf[SIZ];
300         FILE *fp;
301         int i;
302         struct stat statbuf;
303         static time_t pc_timestamp = 0;
304         static char public_clients[SIZ];
305
306 #define PUBLIC_CLIENTS "./public_clients"
307
308         /*
309          * Check the time stamp on the public_clients file.  If it's been
310          * updated since the last time we were here (or if this is the first
311          * time we've been through the loop), read its contents and learn
312          * the IP addresses of the listed hosts.
313          */
314         if (stat(PUBLIC_CLIENTS, &statbuf) != 0) {
315                 /* No public_clients file exists, so bail out */
316                 lprintf(CTDL_WARNING, "Warning: '%s' does not exist\n", PUBLIC_CLIENTS);
317                 return(0);
318         }
319
320         if (statbuf.st_mtime > pc_timestamp) {
321                 begin_critical_section(S_PUBLIC_CLIENTS);
322                 lprintf(CTDL_INFO, "Loading %s\n", PUBLIC_CLIENTS);
323
324                 strcpy(public_clients, "127.0.0.1");
325                 if (hostname_to_dotted_quad(addrbuf, config.c_fqdn) == 0) {
326                         strcat(public_clients, "|");
327                         strcat(public_clients, addrbuf);
328                 }
329
330                 fp = fopen("public_clients", "r");
331                 if (fp != NULL) while (fgets(buf, sizeof buf, fp)!=NULL) {
332                         for (i=0; i<strlen(buf); ++i) {
333                                 if (buf[i] == '#') buf[i] = 0;
334                         }
335                         while (isspace((buf[strlen(buf)-1]))) {
336                                 buf[strlen(buf)-1] = 0;
337                         }
338                         if (hostname_to_dotted_quad(addrbuf, buf) == 0) {
339                                 if ((strlen(public_clients) +
340                                    strlen(addrbuf) + 2)
341                                    < sizeof(public_clients)) {
342                                         strcat(public_clients, addrbuf);
343                                 }
344                         }
345                 }
346                 fclose(fp);
347                 pc_timestamp = time(NULL);
348                 end_critical_section(S_PUBLIC_CLIENTS);
349         }
350
351         lprintf(CTDL_DEBUG, "Checking whether %s is a local or public client\n",
352                 CC->cs_addr);
353         for (i=0; i<num_parms(public_clients); ++i) {
354                 extract(addrbuf, public_clients, i);
355                 if (!strcasecmp(CC->cs_addr, addrbuf)) {
356                         lprintf(CTDL_DEBUG, "... yes it is.\n");
357                         return(1);
358                 }
359         }
360
361         /* No hits.  This is not a public client. */
362         lprintf(CTDL_DEBUG, "... no it isn't.\n");
363         return(0);
364 }
365
366
367 /*
368  * the client is identifying itself to the server
369  */
370 void cmd_iden(char *argbuf)
371 {
372         int dev_code;
373         int cli_code;
374         int rev_level;
375         char desc[SIZ];
376         char from_host[SIZ];
377         struct in_addr addr;
378         int do_lookup = 0;
379
380         if (num_parms(argbuf)<4) {
381                 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
382                 return;
383         }
384
385         dev_code = extract_int(argbuf,0);
386         cli_code = extract_int(argbuf,1);
387         rev_level = extract_int(argbuf,2);
388         extract(desc,argbuf,3);
389
390         safestrncpy(from_host, config.c_fqdn, sizeof from_host);
391         from_host[sizeof from_host - 1] = 0;
392         if (num_parms(argbuf)>=5) extract(from_host,argbuf,4);
393
394         CC->cs_clientdev = dev_code;
395         CC->cs_clienttyp = cli_code;
396         CC->cs_clientver = rev_level;
397         safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
398         CC->cs_clientname[31] = 0;
399
400         if (strlen(from_host) > 0) {
401                 if (CC->is_local_socket) do_lookup = 1;
402                 else if (is_public_client()) do_lookup = 1;
403         }
404
405         if (do_lookup) {
406                 lprintf(CTDL_DEBUG, "Looking up hostname '%s'\n", from_host);
407                 if ((addr.s_addr = inet_addr(from_host)) != -1) {
408                         locate_host(CC->cs_host, sizeof CC->cs_host,
409                                 NULL, 0,
410                                 &addr);
411                 }
412                 else {
413                         safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
414                         CC->cs_host[sizeof CC->cs_host - 1] = 0;
415                 }
416         }
417
418         lprintf(CTDL_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
419                 dev_code,
420                 cli_code,
421                 (rev_level / 100),
422                 (rev_level % 100),
423                 desc,
424                 CC->cs_host);
425         cprintf("%d Ok\n",CIT_OK);
426 }
427
428
429 /*
430  * display system messages or help
431  */
432 void cmd_mesg(char *mname)
433 {
434         FILE *mfp;
435         char targ[SIZ];
436         char buf[SIZ];
437         char buf2[SIZ];
438         char *dirs[2];
439
440         extract(buf,mname,0);
441
442         dirs[0]=malloc(64);
443         dirs[1]=malloc(64);
444         strcpy(dirs[0],"messages");
445         strcpy(dirs[1],"help");
446         snprintf(buf2, sizeof buf2, "%s.%d.%d", buf, CC->cs_clientdev, CC->cs_clienttyp);
447         mesg_locate(targ,sizeof targ,buf2,2,(const char **)dirs);
448         if (strlen(targ) == 0) {
449                 snprintf(buf2, sizeof buf2, "%s.%d", buf, CC->cs_clientdev);
450                 mesg_locate(targ,sizeof targ,buf2,2,(const char **)dirs);
451                 if (strlen(targ) == 0) {
452                         mesg_locate(targ,sizeof targ,buf,2,(const char **)dirs);
453                 }       
454         }
455         free(dirs[0]);
456         free(dirs[1]);
457
458         if (strlen(targ)==0) {
459                 cprintf("%d '%s' not found.\n",ERROR + FILE_NOT_FOUND, mname);
460                 return;
461         }
462
463         mfp = fopen(targ,"r");
464         if (mfp==NULL) {
465                 cprintf("%d Cannot open '%s': %s\n",
466                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
467                 return;
468         }
469         cprintf("%d %s\n",LISTING_FOLLOWS,buf);
470
471         while (fgets(buf, (SIZ-1), mfp)!=NULL) {
472                 buf[strlen(buf)-1] = 0;
473                 do_help_subst(buf);
474                 cprintf("%s\n",buf);
475         }
476
477         fclose(mfp);
478         cprintf("000\n");
479 }
480
481
482 /*
483  * enter system messages or help
484  */
485 void cmd_emsg(char *mname)
486 {
487         FILE *mfp;
488         char targ[SIZ];
489         char buf[SIZ];
490         char *dirs[2];
491         int a;
492
493         unbuffer_output();
494
495         if (CtdlAccessCheck(ac_aide)) return;
496
497         extract(buf,mname,0);
498         for (a=0; a<strlen(buf); ++a) {         /* security measure */
499                 if (buf[a] == '/') buf[a] = '.';
500         }
501
502         dirs[0]=malloc(64);
503         dirs[1]=malloc(64);
504         strcpy(dirs[0],"messages");
505         strcpy(dirs[1],"help");
506         mesg_locate(targ,sizeof targ,buf,2,(const char**)dirs);
507         free(dirs[0]);
508         free(dirs[1]);
509
510         if (strlen(targ)==0) {
511                 snprintf(targ, sizeof targ, "./help/%s", buf);
512         }
513
514         mfp = fopen(targ,"w");
515         if (mfp==NULL) {
516                 cprintf("%d Cannot open '%s': %s\n",
517                         ERROR + INTERNAL_ERROR, targ, strerror(errno));
518                 return;
519         }
520         cprintf("%d %s\n", SEND_LISTING, targ);
521
522         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
523                 fprintf(mfp, "%s\n", buf);
524         }
525
526         fclose(mfp);
527 }
528
529
530 /* Don't show the names of private rooms unless the viewing
531  * user also knows the rooms.
532  */
533 void GenerateRoomDisplay(char *real_room,
534                         struct CitContext *viewed,
535                         struct CitContext *viewer) {
536
537         int ra;
538
539         strcpy(real_room, viewed->room.QRname);
540         if (viewed->room.QRflags & QR_MAILBOX) {
541                 strcpy(real_room, &real_room[11]);
542         }
543         if (viewed->room.QRflags & QR_PRIVATE) {
544                 CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
545                 if ( (ra & UA_KNOWN) == 0) {
546                         strcpy(real_room, "<private room>");
547                 }
548         }
549
550         if (viewed->cs_flags & CS_CHAT) {
551                 while (strlen(real_room) < 14)
552                         strcat(real_room, " ");
553
554                 strcpy(&real_room[14], "<chat>");
555         }
556
557 }
558
559 /*
560  * Convenience function.
561  */
562 int CtdlAccessCheck(int required_level) {
563
564         if (CC->internal_pgm) return(0);
565         if (required_level >= ac_internal) {
566                 cprintf("%d This is not a user-level command.\n",
567                         ERROR + HIGHER_ACCESS_REQUIRED);
568                 return(-1);
569         }
570
571         if ((required_level >= ac_logged_in) && (CC->logged_in == 0)) {
572                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
573                 return(-1);
574         }
575
576         if (CC->user.axlevel >= 6) return(0);
577         if (required_level >= ac_aide) {
578                 cprintf("%d This command requires Aide access.\n",
579                         ERROR + HIGHER_ACCESS_REQUIRED);
580                 return(-1);
581         }
582
583         if (is_room_aide()) return(0);
584         if (required_level >= ac_room_aide) {
585                 cprintf("%d This command requires Aide or Room Aide access.\n",
586                         ERROR + HIGHER_ACCESS_REQUIRED);
587                 return(-1);
588         }
589
590         /* shhh ... succeed quietly */
591         return(0);
592 }
593
594
595
596 /*
597  * Terminate another running session
598  */
599 void cmd_term(char *cmdbuf)
600 {
601         int session_num;
602         struct CitContext *ccptr;
603         int found_it = 0;
604         int allowed = 0;
605
606         session_num = extract_int(cmdbuf, 0);
607         if (session_num == CC->cs_pid) {
608                 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
609                 return;
610         }
611
612         lprintf(CTDL_DEBUG, "Locating session to kill\n");
613         begin_critical_section(S_SESSION_TABLE);
614         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
615                 if (session_num == ccptr->cs_pid) {
616                         found_it = 1;
617                         if ((ccptr->user.usernum == CC->user.usernum)
618                            || (CC->user.axlevel >= 6)) {
619                                 allowed = 1;
620                                 ccptr->kill_me = 1;
621                         }
622                         else {
623                                 allowed = 0;
624                         }
625                 }
626         }
627         end_critical_section(S_SESSION_TABLE);
628
629         if (found_it) {
630                 if (allowed) {
631                         cprintf("%d Session terminated.\n", CIT_OK);
632                 }
633                 else {
634                         cprintf("%d You are not allowed to do that.\n",
635                                 ERROR + HIGHER_ACCESS_REQUIRED);
636                 }
637         }
638         else {
639                 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
640         }
641 }
642
643
644
645
646
647 /* 
648  * get the paginator prompt
649  */
650 void cmd_more(void) {
651         cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
652 }
653
654 /*
655  * echo 
656  */
657 void cmd_echo(char *etext)
658 {
659         cprintf("%d %s\n", CIT_OK, etext);
660 }
661
662
663
664 /* 
665  * identify as internal program
666  */
667 void cmd_ipgm(char *argbuf)
668 {
669         int secret;
670
671         secret = extract_int(argbuf, 0);
672
673         /* For security reasons, we do NOT allow this command to run
674          * over the network.  Local sockets only.
675          */
676         if (!CC->is_local_socket) {
677                 sleep(5);
678                 cprintf("%d Authentication failed.\n",
679                         ERROR + PASSWORD_REQUIRED);
680         }
681         else if (secret == config.c_ipgm_secret) {
682                 CC->internal_pgm = 1;
683                 strcpy(CC->curr_user, "<internal program>");
684                 CC->cs_flags = CC->cs_flags|CS_STEALTH;
685                 cprintf("%d Authenticated as an internal program.\n", CIT_OK);
686         }
687         else {
688                 sleep(5);
689                 cprintf("%d Authentication failed.\n",
690                         ERROR + PASSWORD_REQUIRED);
691                 lprintf(CTDL_ERR, "Warning: ipgm authentication failed.\n");
692                 CC->kill_me = 1;
693         }
694
695         /* Now change the ipgm secret for the next round.
696          * (Disabled because it breaks concurrent scripts.  The fact that
697          * we no longer accept IPGM over the network should be sufficient
698          * to prevent brute-force attacks.  If you don't agree, uncomment
699          * this block.)
700         get_config();
701         config.c_ipgm_secret = rand();
702         put_config();
703         */
704 }
705
706
707 /*
708  * Shut down the server
709  */
710 void cmd_down(void) {
711
712         if (CtdlAccessCheck(ac_aide)) return;
713
714         cprintf("%d Shutting down server.  Goodbye.\n", CIT_OK);
715         time_to_die = 1;
716 }
717
718 /*
719  * Schedule or cancel a server shutdown
720  */
721 void cmd_scdn(char *argbuf)
722 {
723         int new_state;
724
725         if (CtdlAccessCheck(ac_aide)) return;
726
727         new_state = extract_int(argbuf, 0);
728         if ((new_state == 0) || (new_state == 1)) {
729                 ScheduledShutdown = new_state;
730         }
731         cprintf("%d %d\n", CIT_OK, ScheduledShutdown);
732 }
733
734
735 /*
736  * Set or unset asynchronous protocol mode
737  */
738 void cmd_asyn(char *argbuf)
739 {
740         int new_state;
741
742         new_state = extract_int(argbuf, 0);
743         if ((new_state == 0) || (new_state == 1)) {
744                 CC->is_async = new_state;
745         }
746         cprintf("%d %d\n", CIT_OK, CC->is_async);
747 }
748
749
750 /*
751  * Generate a "nonce" for APOP-style authentication.
752  *
753  * RFC 1725 et al specify a PID to be placed in front of the nonce.
754  * Quoth BTX: That would be stupid.
755  */
756 void generate_nonce(struct CitContext *con) {
757         struct timeval tv;
758
759         memset(con->cs_nonce, NONCE_SIZE, 0);
760         gettimeofday(&tv, NULL);
761         memset(con->cs_nonce, NONCE_SIZE, 0);
762         snprintf(con->cs_nonce, NONCE_SIZE, "<%d%ld@%s>",
763                 rand(), (long)tv.tv_usec, config.c_fqdn);
764 }
765
766
767
768
769 /*
770  * Back-end function for starting a session
771  */
772 void begin_session(struct CitContext *con)
773 {
774         int len;
775         struct sockaddr_in sin;
776
777         /* 
778          * Initialize some variables specific to our context.
779          */
780         con->logged_in = 0;
781         con->internal_pgm = 0;
782         con->download_fp = NULL;
783         con->upload_fp = NULL;
784         con->FirstExpressMessage = NULL;
785         time(&con->lastcmd);
786         time(&con->lastidle);
787         strcpy(con->lastcmdname, "    ");
788         strcpy(con->cs_clientname, "(unknown)");
789         strcpy(con->curr_user, NLI);
790         strcpy(con->net_node,"");
791         strcpy(con->fake_username, "");
792         strcpy(con->fake_postname, "");
793         strcpy(con->fake_hostname, "");
794         strcpy(con->fake_roomname, "");
795         generate_nonce(con);
796         safestrncpy(con->temp, tmpnam(NULL), sizeof con->temp);
797         safestrncpy(con->cs_host, config.c_fqdn, sizeof con->cs_host);
798         safestrncpy(con->cs_addr, "", sizeof con->cs_addr);
799         con->cs_host[sizeof con->cs_host - 1] = 0;
800         len = sizeof sin;
801         if (!CC->is_local_socket) {
802                 if (!getpeername(con->client_socket,
803                    (struct sockaddr *) &sin, &len))     /* should be socklen_t but doesn't work on Macintosh */
804                         locate_host(con->cs_host, sizeof con->cs_host,
805                                 con->cs_addr, sizeof con->cs_addr,
806                                 &sin.sin_addr);
807         }
808         else {
809                 strcpy(con->cs_host, "");
810         }
811         con->cs_flags = 0;
812         con->upload_type = UPL_FILE;
813         con->dl_is_net = 0;
814
815         con->nologin = 0;
816         if ((config.c_maxsessions > 0)&&(num_sessions > config.c_maxsessions))
817                 con->nologin = 1;
818
819         lprintf(CTDL_NOTICE, "Session started.\n");
820
821         /* Run any session startup routines registered by loadable modules */
822         PerformSessionHooks(EVT_START);
823 }
824
825
826 void citproto_begin_session() {
827         if (CC->nologin==1) {
828                 cprintf("%d %s: Too many users are already online "
829                         "(maximum is %d)\n",
830                         ERROR + MAX_SESSIONS_EXCEEDED,
831                         config.c_nodename, config.c_maxsessions);
832         }
833         else {
834                 cprintf("%d %s Citadel server ready.\n",
835                         CIT_OK, config.c_nodename);
836         }
837 }
838
839
840
841
842 /*
843  * This loop recognizes all server commands.
844  */
845 void do_command_loop(void) {
846         char cmdbuf[SIZ];
847
848         time(&CC->lastcmd);
849         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
850         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
851                 lprintf(CTDL_ERR, "Client socket is broken; ending session\n");
852                 CC->kill_me = 1;
853                 return;
854         }
855
856         /* Log the server command, but don't show passwords... */
857         if ( (strncasecmp(cmdbuf, "PASS", 4))
858            && (strncasecmp(cmdbuf, "SETP", 4)) ) {
859                 lprintf(CTDL_INFO, "%s\n", cmdbuf);
860         }
861         else {
862                 lprintf(CTDL_INFO, "<password command sent>\n");
863         }
864
865         buffer_output();
866
867         /*
868          * Let other clients see the last command we executed, and
869          * update the idle time, but not NOOP, QNOP, PEXP, or GEXP.
870          */
871         if ( (strncasecmp(cmdbuf, "NOOP", 4))
872            && (strncasecmp(cmdbuf, "QNOP", 4))
873            && (strncasecmp(cmdbuf, "PEXP", 4))
874            && (strncasecmp(cmdbuf, "GEXP", 4)) ) {
875                 strcpy(CC->lastcmdname, "    ");
876                 safestrncpy(CC->lastcmdname, cmdbuf, 
877                         sizeof(CC->lastcmdname) );
878                 time(&CC->lastidle);
879         }
880                 
881         if ((strncasecmp(cmdbuf, "ENT0", 4))
882            && (strncasecmp(cmdbuf, "MESG", 4))
883            && (strncasecmp(cmdbuf, "MSGS", 4)))
884         {
885            CC->cs_flags &= ~CS_POSTING;
886         }
887                    
888         if (!strncasecmp(cmdbuf,"NOOP",4)) {
889                 cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
890         }
891         
892         else if (!strncasecmp(cmdbuf,"QNOP",4)) {
893                 /* do nothing, this command returns no response */
894         }
895
896         else if (!strncasecmp(cmdbuf,"QUIT",4)) {
897                 cprintf("%d Goodbye.\n", CIT_OK);
898                 CC->kill_me = 1;
899         }
900
901         else if (!strncasecmp(cmdbuf,"ASYN",4)) {
902                 cmd_asyn(&cmdbuf[5]);
903         }
904
905         else if (!strncasecmp(cmdbuf,"LOUT",4)) {
906                 if (CC->logged_in) logout(CC);
907                 cprintf("%d logged out.\n", CIT_OK);
908         }
909
910         else if (!strncasecmp(cmdbuf,"USER",4)) {
911                 cmd_user(&cmdbuf[5]);
912         }
913
914         else if (!strncasecmp(cmdbuf,"PASS",4)) {
915                 cmd_pass(&cmdbuf[5]);
916         }
917
918         else if (!strncasecmp(cmdbuf,"NEWU",4)) {
919                 cmd_newu(&cmdbuf[5]);
920         }
921
922         else if (!strncasecmp(cmdbuf,"CREU",4)) {
923                 cmd_creu(&cmdbuf[5]);
924         }
925
926         else if (!strncasecmp(cmdbuf,"SETP",4)) {
927                 cmd_setp(&cmdbuf[5]);
928         }
929
930         else if (!strncasecmp(cmdbuf,"LRMS",4)) {
931                 cmd_lrms(&cmdbuf[5]);
932         }
933
934         else if (!strncasecmp(cmdbuf,"LKRA",4)) {
935                 cmd_lkra(&cmdbuf[5]);
936         }
937
938         else if (!strncasecmp(cmdbuf,"LKRN",4)) {
939                 cmd_lkrn(&cmdbuf[5]);
940         }
941
942         else if (!strncasecmp(cmdbuf,"LKRO",4)) {
943                 cmd_lkro(&cmdbuf[5]);
944         }
945
946         else if (!strncasecmp(cmdbuf,"LZRM",4)) {
947                 cmd_lzrm(&cmdbuf[5]);
948         }
949
950         else if (!strncasecmp(cmdbuf,"LPRM",4)) {
951                 cmd_lprm(&cmdbuf[5]);
952         }
953
954         else if (!strncasecmp(cmdbuf,"GETU",4)) {
955                 cmd_getu();
956         }
957
958         else if (!strncasecmp(cmdbuf,"SETU",4)) {
959                 cmd_setu(&cmdbuf[5]);
960         }
961
962         else if (!strncasecmp(cmdbuf,"GOTO",4)) {
963                 cmd_goto(&cmdbuf[5]);
964         }
965
966         else if (!strncasecmp(cmdbuf,"MSGS",4)) {
967                 cmd_msgs(&cmdbuf[5]);
968         }
969
970         else if (!strncasecmp(cmdbuf,"WHOK",4)) {
971                 cmd_whok();
972         }
973
974         else if (!strncasecmp(cmdbuf,"RDIR",4)) {
975                 cmd_rdir();
976         }
977
978         else if (!strncasecmp(cmdbuf,"MSG0",4)) {
979                 cmd_msg0(&cmdbuf[5]);
980         }
981
982         else if (!strncasecmp(cmdbuf,"MSG2",4)) {
983                 cmd_msg2(&cmdbuf[5]);
984         }
985
986         else if (!strncasecmp(cmdbuf,"MSG3",4)) {
987                 cmd_msg3(&cmdbuf[5]);
988         }
989
990         else if (!strncasecmp(cmdbuf,"MSG4",4)) {
991                 cmd_msg4(&cmdbuf[5]);
992         }
993
994         else if (!strncasecmp(cmdbuf,"MSGP",4)) {
995                 cmd_msgp(&cmdbuf[5]);
996         }
997
998         else if (!strncasecmp(cmdbuf,"OPNA",4)) {
999                 cmd_opna(&cmdbuf[5]);
1000         }
1001
1002         else if (!strncasecmp(cmdbuf,"INFO",4)) {
1003                 cmd_info();
1004         }
1005
1006         else if (!strncasecmp(cmdbuf,"SLRP",4)) {
1007                 cmd_slrp(&cmdbuf[5]);
1008         }
1009
1010         else if (!strncasecmp(cmdbuf,"INVT",4)) {
1011                 cmd_invt_kick(&cmdbuf[5],1);
1012         }
1013
1014         else if (!strncasecmp(cmdbuf,"KICK",4)) {
1015                 cmd_invt_kick(&cmdbuf[5],0);
1016         }
1017
1018         else if (!strncasecmp(cmdbuf,"GETR",4)) {
1019                 cmd_getr();
1020         }
1021
1022         else if (!strncasecmp(cmdbuf,"SETR",4)) {
1023                 cmd_setr(&cmdbuf[5]);
1024         }
1025
1026         else if (!strncasecmp(cmdbuf,"GETA",4)) {
1027                 cmd_geta();
1028         }
1029
1030         else if (!strncasecmp(cmdbuf,"SETA",4)) {
1031                 cmd_seta(&cmdbuf[5]);
1032         }
1033
1034         else if (!strncasecmp(cmdbuf,"ENT0",4)) {
1035                 cmd_ent0(&cmdbuf[5]);
1036         }
1037
1038         else if (!strncasecmp(cmdbuf,"RINF",4)) {
1039                 cmd_rinf();
1040         }
1041
1042         else if (!strncasecmp(cmdbuf,"DELE",4)) {
1043                 cmd_dele(&cmdbuf[5]);
1044         }
1045
1046         else if (!strncasecmp(cmdbuf,"KILL",4)) {
1047                 cmd_kill(&cmdbuf[5]);
1048         }
1049
1050         else if (!strncasecmp(cmdbuf,"CRE8",4)) {
1051                 cmd_cre8(&cmdbuf[5]);
1052         }
1053
1054         else if (!strncasecmp(cmdbuf,"MOVE",4)) {
1055                 cmd_move(&cmdbuf[5]);
1056         }
1057
1058         else if (!strncasecmp(cmdbuf,"FORG",4)) {
1059                 cmd_forg();
1060         }
1061
1062         else if (!strncasecmp(cmdbuf,"MESG",4)) {
1063                 cmd_mesg(&cmdbuf[5]);
1064         }
1065
1066         else if (!strncasecmp(cmdbuf,"EMSG",4)) {
1067                 cmd_emsg(&cmdbuf[5]);
1068         }
1069
1070         else if (!strncasecmp(cmdbuf,"GNUR",4)) {
1071                 cmd_gnur();
1072         }
1073
1074         else if (!strncasecmp(cmdbuf,"VALI",4)) {
1075                 cmd_vali(&cmdbuf[5]);
1076         }
1077
1078         else if (!strncasecmp(cmdbuf,"EINF",4)) {
1079                 cmd_einf(&cmdbuf[5]);
1080         }
1081
1082         else if (!strncasecmp(cmdbuf,"LIST",4)) {
1083                 cmd_list();
1084         }
1085
1086         else if (!strncasecmp(cmdbuf,"CHEK",4)) {
1087                 cmd_chek();
1088         }
1089
1090         else if (!strncasecmp(cmdbuf,"DELF",4)) {
1091                 cmd_delf(&cmdbuf[5]);
1092         }
1093
1094         else if (!strncasecmp(cmdbuf,"MOVF",4)) {
1095                 cmd_movf(&cmdbuf[5]);
1096         }
1097
1098         else if (!strncasecmp(cmdbuf,"NETF",4)) {
1099                 cmd_netf(&cmdbuf[5]);
1100         }
1101
1102         else if (!strncasecmp(cmdbuf,"OPEN",4)) {
1103                 cmd_open(&cmdbuf[5]);
1104         }
1105
1106         else if (!strncasecmp(cmdbuf,"CLOS",4)) {
1107                 cmd_clos();
1108         }
1109
1110         else if (!strncasecmp(cmdbuf,"UOPN",4)) {
1111                 cmd_uopn(&cmdbuf[5]);
1112         }
1113
1114         else if (!strncasecmp(cmdbuf,"UCLS",4)) {
1115                 cmd_ucls(&cmdbuf[5]);
1116         }
1117
1118         else if (!strncasecmp(cmdbuf,"READ",4)) {
1119                 cmd_read(&cmdbuf[5]);
1120         }
1121
1122         else if (!strncasecmp(cmdbuf,"WRIT",4)) {
1123                 cmd_writ(&cmdbuf[5]);
1124         }
1125
1126         else if (!strncasecmp(cmdbuf,"QUSR",4)) {
1127                 cmd_qusr(&cmdbuf[5]);
1128         }
1129
1130         else if (!strncasecmp(cmdbuf,"ECHO",4)) {
1131                 cmd_echo(&cmdbuf[5]);
1132         }
1133
1134         else if (!strncasecmp(cmdbuf,"OIMG",4)) {
1135                 cmd_oimg(&cmdbuf[5]);
1136         }
1137
1138         else if (!strncasecmp(cmdbuf,"MORE",4)) {
1139                 cmd_more();
1140         }
1141
1142         else if (!strncasecmp(cmdbuf,"NDOP",4)) {
1143                 cmd_ndop(&cmdbuf[5]);
1144         }
1145
1146         else if (!strncasecmp(cmdbuf,"NUOP",4)) {
1147                 cmd_nuop(&cmdbuf[5]);
1148         }
1149
1150         else if (!strncasecmp(cmdbuf,"LFLR",4)) {
1151                 cmd_lflr();
1152         }
1153
1154         else if (!strncasecmp(cmdbuf,"CFLR",4)) {
1155                 cmd_cflr(&cmdbuf[5]);
1156         }
1157
1158         else if (!strncasecmp(cmdbuf,"KFLR",4)) {
1159                 cmd_kflr(&cmdbuf[5]);
1160         }
1161
1162         else if (!strncasecmp(cmdbuf,"EFLR",4)) {
1163                 cmd_eflr(&cmdbuf[5]);
1164         }
1165
1166         else if (!strncasecmp(cmdbuf,"IDEN",4)) {
1167                 cmd_iden(&cmdbuf[5]);
1168         }
1169
1170         else if (!strncasecmp(cmdbuf,"IPGM",4)) {
1171                 cmd_ipgm(&cmdbuf[5]);
1172         }
1173
1174         else if (!strncasecmp(cmdbuf,"TERM",4)) {
1175                 cmd_term(&cmdbuf[5]);
1176         }
1177
1178         else if (!strncasecmp(cmdbuf,"DOWN",4)) {
1179                 cmd_down();
1180         }
1181
1182         else if (!strncasecmp(cmdbuf,"SCDN",4)) {
1183                 cmd_scdn(&cmdbuf[5]);
1184         }
1185
1186         else if (!strncasecmp(cmdbuf, "UIMG", 4)) {
1187                 cmd_uimg(&cmdbuf[5]);
1188         }
1189
1190         else if (!strncasecmp(cmdbuf, "TIME", 4)) {
1191                 cmd_time();
1192         }
1193
1194         else if (!strncasecmp(cmdbuf, "AGUP", 4)) {
1195                 cmd_agup(&cmdbuf[5]);
1196         }
1197
1198         else if (!strncasecmp(cmdbuf, "ASUP", 4)) {
1199                 cmd_asup(&cmdbuf[5]);
1200         }
1201
1202         else if (!strncasecmp(cmdbuf, "GPEX", 4)) {
1203                 cmd_gpex(&cmdbuf[5]);
1204         }
1205
1206         else if (!strncasecmp(cmdbuf, "SPEX", 4)) {
1207                 cmd_spex(&cmdbuf[5]);
1208         }
1209
1210         else if (!strncasecmp(cmdbuf, "CONF", 4)) {
1211                 cmd_conf(&cmdbuf[5]);
1212         }
1213
1214         else if (!strncasecmp(cmdbuf, "SEEN", 4)) {
1215                 cmd_seen(&cmdbuf[5]);
1216         }
1217
1218         else if (!strncasecmp(cmdbuf, "GTSN", 4)) {
1219                 cmd_gtsn(&cmdbuf[5]);
1220         }
1221
1222         else if (!strncasecmp(cmdbuf, "VIEW", 4)) {
1223                 cmd_view(&cmdbuf[5]);
1224         }
1225
1226         else if (!strncasecmp(cmdbuf, "ISME", 4)) {
1227                 cmd_isme(&cmdbuf[5]);
1228         }
1229
1230         else if (!DLoader_Exec_Cmd(cmdbuf)) {
1231                 cprintf("%d Unrecognized or unsupported command.\n",
1232                         ERROR + CMD_NOT_SUPPORTED);
1233                }
1234
1235         unbuffer_output();
1236
1237         /* Run any after-each-command routines registered by modules */
1238         PerformSessionHooks(EVT_CMD);
1239 }
1240
1241
1242 /*
1243  * This loop performs all asynchronous functions.
1244  */
1245 void do_async_loop(void) {
1246         PerformSessionHooks(EVT_ASYNC);
1247 }