-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmailbox.c
1776 lines (1668 loc) · 40.6 KB
/
mailbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* There are only two functions in this mailbox code that depend on the
* underlying protocol, namely mbx_getname() and dochat(). All the other
* functions can hopefully be used without modification on other stream
* oriented protocols than AX.25 or NET/ROM.
*
* SM0RGV 890506, most work done previously by W9NK
*
*** Changed 900114 by KA9Q to use newline mapping features in stream socket
* interface code; everything here uses C eol convention (\n)
*
* Numerous new commands and other changes by SM0RGV, 900120
*/
#include "top.h"
#include "lib/std/stdio.h"
#include <time.h>
#include <ctype.h>
#include "lib/std/errno.h"
#ifdef UNIX
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "global.h"
#include "config.h"
#include "core/timer.h"
#include "core/proc.h"
#include "core/socket.h"
#include "core/usock.h"
#include "core/session.h"
#include "lib/std/dirutil.h"
#include "telnet.h"
#include "commands.h"
#include "files.h"
#include "bm.h"
#include "mailbox.h"
#include "lib/util/cmdparse.h"
#include "lib/inet/netuser.h"
#include "net/ax25/ax25.h"
#include "net/ax25/ax25mail.h"
#include "net/netrom/nr4mail.h"
#include "service/smtp/smtp.h"
#include "service/ftp/ftp.h"
#include "service/ftp/ftpserv.h"
/*
#define MBDEBUG
*/
struct mbx *Mbox[NUMMBX];
static char *Motd = NULL;
static int Attended = TRUE; /* default to attended mode */
unsigned Maxlet = BM_NLET;
char Noperm[] = "Permission denied.\n";
char Nosock[] = "Can't create socket\n";
static char Mbbanner[] = "[NET-H$]\nWelcome %s to the %s TCP/IP Mailbox (%s)\n%s";
static char Mbmenu[] = "Current msg# %d : A,B,C,D,E,F,G,H,I,J,K,L,N,R,S,T,U,V,W,Z,? >\n";
static char Longmenu1[] = "(?)help (A)rea (B)ye (C)hat (D)ownload (E)scape (F)inger\n";
static char Longmenu2[] = "(G)ateway (H)elp (I)nfo (J)heard (K)ill (L)ist (N)etrom\n";
static char Longmenu3[] = "(R)ead (S)end (T)elnet (U)pload (V)erbose (W)hat (Z)ap\n";
static char Loginbanner[] = "\nKA9Q NOS (%s)\n\n";
static char Howtoend[] = "Terminate with /EX or ^Z in first column (^A aborts):\n";
static int doarea(int argc,char *argv[],void *p);
static int mbx_getname(struct mbx *m);
/************************************************************************/
/* C O M M A N D S */
/************************************************************************/
static int doattend(int argc,char *argv[],void *p);
static int domaxmsg(int argc,char *argv[],void *p);
static int domotd(int argc,char *argv[],void *p);
static int dotimeout(int argc,char *argv[],void *p);
/* mbox subcommand table */
static struct cmds Mbtab[] = {
{ "attend", doattend, 0, 0, NULL },
#ifdef AX25
{ "kick", dombkick, 0, 0, NULL },
#endif
{ "maxmsg", domaxmsg, 0, 0, NULL },
{ "motd", domotd, 0, 0, NULL },
{ "status", domboxdisplay, 0, 0, NULL },
#ifdef AX25
{ "timer", dombtimer, 0, 0, NULL },
#endif
{ "tiptimeout", dotimeout, 0, 0, NULL },
{ NULL },
};
int
dombox(argc,argv,p)
int argc;
char *argv[];
void *p;
{
if(argc == 1)
return domboxdisplay(argc,argv,p);
return subcmd(Mbtab,argc,argv,p);
}
/* if unattended mode is set, ax25, telnet and maybe other sessions will
* be restricted.
*/
static int
doattend(argc,argv,p)
int argc;
char *argv[];
void *p;
{
return setbool(&Attended,"Attended flag",argc,argv);
}
static int
domaxmsg(argc,argv,p)
int argc;
char *argv[];
void *p;
{
return setuns(&Maxlet,"Maximum messages per area",argc,argv);
}
static int
domotd(argc,argv,p)
int argc;
char *argv[];
void *p;
{
if(argc > 2) {
kprintf("Usage: mbox motd \"<your message>\"\n");
return 0;
}
if(argc < 2) {
if(Motd != NULL)
kputs(Motd);
}
else {
if(Motd != NULL){
free(Motd);
Motd = NULL; /* reset the pointer */
}
if(!strlen(argv[1]))
return 0; /* clearing the buffer */
Motd = mallocw(strlen(argv[1])+5);/* allow for the EOL char */
strcpy(Motd, argv[1]);
strcat(Motd, "\n"); /* add the EOL char */
}
return 0;
}
int
domboxdisplay(argc,argv,p)
int argc;
char *argv[];
void *p;
{
int i, j, len;
struct mbx *m;
struct ksockaddr fsocket;
static char *states[] = {"LOGIN","CMD","SUBJ","DATA","REVFWD",
"TRYING","FORWARD"};
kprintf("User State S# Where\n");
for (i = 0; i < NUMMBX; i++){
if((m = Mbox[i]) != NULL){
len = MAXSOCKSIZE;
j = kgetpeername(kfileno(m->user),&fsocket,&len);
kprintf("%-11s%-9s%-6u%s\n",m->name,
states[m->state],kfileno(m->user),
j != -1 ? psocket(&fsocket): "");
}
}
return 0;
}
static int
dotimeout(argc,argv,p)
int argc;
char *argv[];
void *p;
{
return setuns(&Tiptimeout,"Tip connection timeout",argc,argv);
}
/**********************************************************************/
void
listusers(network)
kFILE *network;
{
kFILE *outsave;
kfprintf(network,"\nCurrent remote users:\n");
outsave = kstdout;
kstdout = network;
domboxdisplay(0,NULL,NULL);
kstdout = outsave;
}
struct mbx *
newmbx()
{
int i;
struct mbx *m;
for(i = 0; i < NUMMBX; i++){
if(Mbox[i] == NULL){
m = Mbox[i] = (struct mbx *)callocw(1,sizeof(struct mbx));
m->mbnum = i;
return m;
}
}
/* If we get here, there are no free mailbox sessions */
return NULL;
}
static int
mbx_getname(m)
struct mbx *m;
{
#ifdef AX25
char *cp;
#endif
union sp sp;
struct ksockaddr tmp;
char buf[MBXLINE];
int len = MAXSOCKSIZE;
int anony = 0;
int oldmode;
sp.sa = &tmp;
sp.sa->sa_family = kAF_LOCAL; /* default to AF_LOCAL */
kgetpeername(kfileno(m->user),&tmp,&len);
m->path = mallocw(MBXLINE);
/* This is one of the two parts of the mbox code that depends on the
* underlying protocol. We have to figure out the name of the
* calling station. This is only practical when AX.25 or NET/ROM is
* used. Telnet users have to identify themselves by a login procedure.
*/
switch(sp.sa->sa_family){
#ifdef AX25
case kAF_NETROM:
case kAF_AX25:
/* NETROM and AX25 socket address structures are "compatible" */
pax25(m->name,sp.ax->ax25_addr);
cp = strchr(m->name,'-');
if(cp != NULL) /* get rid of SSID */
*cp = '\0';
/* SMTP wants the name to be in lower case */
cp = m->name;
while(*cp){
if(isupper(*cp))
*cp = tolower(*cp);
++cp;
}
anony = 1;
/* Attempt authentication with blank password */
buf[0] = '\0';
/* Try to find the privileges of this user from the userfile */
if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony)) == -1)
if((m->privs = userlogin("bbs",buf,&m->path,MBXLINE,&anony)) == -1)
if((m->privs = userlogin("anonymous",buf,&m->path,MBXLINE,
&anony)) == -1){
m->privs = 0;
free(m->path);
m->path = NULL;
}
if(m->privs & EXCLUDED_CMD)
return -1;
return 0;
#endif
case kAF_LOCAL:
case kAF_INET:
m->state = MBX_LOGIN;
kprintf(Loginbanner,Hostname);
for(;;){
kfputs("login: ",kstdout);
if(mbxrecvline(m->user,m->name,sizeof(m->name),-1) == kEOF)
return -1;
if(*m->name == '\0')
continue;
kprintf("Password: %c%c%c",IAC,WILL,TN_ECHO);
oldmode = kfmode(m->user,STREAM_BINARY);
if(mbxrecvline(m->user,buf,MBXLINE,-1) == kEOF)
return -1;
kprintf("%c%c%c",IAC,WONT,TN_ECHO);
kfmode(m->user,oldmode);
kputchar('\n');
#ifdef notdef
/* This is needed if the password was send before the
* telnet no-echo options were received. We neeed to
* flush the eold sequence from the input buffers, sigh
*/
if(socklen(kfileno(m->user),0))/* discard any remaining input */
recv_mbuf(kfileno(m->user),NULL,0,NULL,0);
#endif
if((m->privs = userlogin(m->name,buf,&m->path,MBXLINE,&anony))
!= -1){
if(anony)
logmsg(kfileno(m->user),"MBOX login: %s Password: %s",m->name,buf);
else
logmsg(kfileno(m->user),"MBOX login: %s",m->name);
if(m->privs & EXCLUDED_CMD)
return -1;
return 0;
}
kprintf("Login incorrect\n");
*m->name = '\0'; /* wipe any garbage */
}
}
return 0;
}
/* Incoming mailbox session */
void
mbx_incom(s,t,p)
int s;
void *t;
void *p;
{
struct mbx *m;
struct usock *up;
char *buf[3];
int rval;
kFILE *network;
sockowner(s,Curproc); /* We own it now */
if(p == NULL)
network = kfdopen(s,"r+t");
else
network = (kFILE *)p;
/* Secede from the parent's sockets, and use the network socket that
* was passed to us for both input and output. The reference
* count on this socket will still be 1; this allows the domboxbye()
* command to work by closing that socket with a single call.
* If we return, the socket will be closed automatically.
*/
kfclose(kstdin);
kstdin = kfdup(network);
kfclose(kstdout);
kstdout = kfdup(network);
logmsg(kfileno(network),"open MBOX");
if((m = newmbx()) == NULL){
kprintf("Too many mailbox sessions\n");
return;
}
m->user = network;
m->escape = 24; /* default escape character is Ctrl-X */
m->type = (int) t;
/* get the name of the remote station */
if(mbx_getname(m) == -1) {
exitbbs(m);
return;
}
m->state = MBX_CMD; /* start in command state */
/* Now say hi */
kprintf(Mbbanner,m->name,Hostname,Version,
Motd != NULL ? Motd : "");
/* Enable our local message area */
buf[1] = m->name;
doarea(2,buf,m);
kprintf(Mbmenu,m->current);
while(mbxrecvline(network,m->line,MBXLINE,-1) != kEOF){
if((rval = mbx_parse(m)) == -2)
break;
if(rval == 1)
kprintf("Bad syntax.\n");
if(!(m->sid & MBX_SID) && isnewprivmail(m) > 0L)
kprintf("You have new mail.\n");
scanmail(m);
kprintf((m->sid & MBX_SID) ? ">\n" : Mbmenu, m->current);
m->state = MBX_CMD;
}
exitbbs(m);
/* nasty hack! we may have screwed up reference count */
/* by invoking newproc("smtp_send",....); Fudge it! */
if((up = itop(kfileno(kstdout))) != NULL)
up->refcnt = 1;
kfclose(kstdout);
}
void
exitbbs(m)
struct mbx *m;
{
closenotes(m);
free(m->to);
free(m->tofrom);
free(m->origto);
free(m->tomsgid);
free(m->path);
free(m->mbox);
Mbox[m->mbnum] = NULL;
free(m);
}
/**********************************************************************/
static int dochat(int argc,char *argv[],void *p);
static int dodownload(int argc,char *argv[],void *p);
static int dombupload(int argc,char *argv[],void *p);
static int dowhat(int argc,char *argv[],void *p);
static int dozap(int argc,char *argv[],void *p);
static int dosend(int argc,char *argv[],void *p);
static int dosid(int argc,char *argv[],void *p);
static int dosysop(int argc,char *argv[],void *p);
static int dologin(int argc, char *argv[],void *p);
static int dostars(int argc,char *argv[],void *p);
static int dombhelp(int argc,char *argv[],void *p);
static int dombtelnet(int argc,char *argv[],void *p);
static int dombfinger(int argc,char *argv[],void *p);
static void gw_alarm(void *p);
static void gw_input(int s,void *notused,void *p);
static void gw_superv(int null,void *proc,void *p);
static int mbx_to(int argc,char *argv[],void *p);
static int mbx_data(struct mbx *m,struct list *cclist,char *extra);
static int msgidcheck(char *string);
static int uuencode(kFILE *infile,kFILE *outfile,char *infilename);
static struct cmds Mbcmds[] = {
{ "", doreadnext, 0, 0, NULL },
{ "area", doarea, 0, 0, NULL },
{ "send", dosend, 0, 0, NULL },
{ "read", doreadmsg, 0, 2, "R numbers" },
{ "verbose", doreadmsg, 0, 2, "V numbers" },
#ifdef AX25
{ "jheard", doaxheard, 0, 0, NULL },
#endif
{ "kill", dodelmsg, 0, 2, "K numbers" },
{ "list", dolistnotes, 0, 0, NULL },
{ "escape", dombescape, 0, 0, NULL },
{ "download", dodownload, 0, 2, "D[U] filename" },
{ "upload", dombupload, 0, 2, "U filename" },
{ "what", dowhat, 0, 0, NULL },
{ "zap", dozap, 0, 2, "Z filename" },
#ifdef AX25
{ "gateway", dogateway, 0, 3, "G interface callsigns" },
#endif
{ "telnet", dombtelnet, 0, 2, "T hostname" },
{ "finger", dombfinger, 0, 0, NULL },
#ifdef NETROM
{ "netrom", dombnetrom, 0, 0, NULL },
#endif
{ "chat", dochat, 0, 0, NULL },
{ "bye", domboxbye, 0, 0, NULL },
{ "help", dombhelp, 0, 0, NULL },
{ "info", dombhelp, 0, 0, NULL },
{ "?", dombhelp, 0, 0, NULL },
{ "[", dosid, 0, 0, NULL },
#ifdef AX25
{ "f>", dorevfwd, 0, 0, NULL },
#endif
{ "@", dosysop, 0, 0, NULL },
{ "***", dostars, 0, 0, NULL },
{ "login", dologin, 0, 0, NULL },
{ NULL, NULL, 0, 0, "Huh?" },
};
/* "twocmds" defines the MBL/RLI two-letter commands, eg. "SB", "SP" and so on.
* They have to be treated specially since cmdparse() wants a space between
* the actual command and its arguments.
* "SP FOO" is converted to "s foo" and the second command letter is saved
* in m->stype. Longer commands like "SEND" are unaffected, except for
* commands starting with "[", i.e. the SID, since we don't know what it will
* look like.
*/
static char twocmds[] = "slrd["; /* S,L,R,D are two-letter commands */
int
mbx_parse(m)
struct mbx *m;
{
char *cp;
int i;
char *newargv[2];
/* Translate entire buffer to lower case */
for (cp = m->line; *cp != '\0'; ++cp)
if(isupper(*cp))
*cp = tolower(*cp);
/* Skip any spaces at the begining */
for(cp = m->line;isspace(*cp);++cp)
;
m->stype = ' ';
if(*cp != '\0' && *(cp+1) != '\0')
for(i=0; i<strlen(twocmds); ++i){
if(*cp == twocmds[i] && (isspace(*(cp+2)) || *(cp+2) == '\0'
|| *cp == '[')){
if(islower(*(++cp)))
m->stype = toupper(*cp); /* Save the second character */
else
m->stype = *cp;
*cp = ' ';
break;
}
}
/* See if the input line consists solely of digits */
cp = m->line;
for(cp = m->line;isspace(*cp);++cp)
;
newargv[1] = cp;
for(;*cp != '\0' && isdigit(*cp);++cp)
;
if(*cp == '\0' && strlen(newargv[1]) > 0) {
newargv[0] = "read";
return doreadmsg(2,newargv,(void *)m);
}
else
return cmdparse(Mbcmds,m->line,(void *)m);
}
/* This works like recvline(), but telnet options are answered and the
* terminating newline character is not put into the buffer. If the
* incoming character equals the value of escape, any queued input is
* flushed and -2 returned.
*/
int
mbxrecvline(network,buf,len,escape)
kFILE *network;
char *buf;
int len;
int escape;
{
int c, cnt = 0, opt;
if(buf == NULL)
return 0;
kfflush(kstdout);
while((c = kgetc(network)) != kEOF){
if(c == IAC){ /* Telnet command escape */
if((c = kgetc(network)) == kEOF)
break;
if(c > 250 && c < 255 && (opt = kgetc(network)) != kEOF){
#ifdef foo
switch(c){
case WILL:
kprintf("%c%c%c",IAC,DONT,opt);
break;
case WONT:
kprintf("%c%c%c",IAC,DONT,opt);
break;
case DO:
kprintf("%c%c%c",IAC,WONT,opt);
break;
case DONT:
kprintf("%c%c%c",IAC,WONT,opt);
}
#endif
/* to be fixed fflush(stdout);*/
continue;
}
if(c != IAC && (c = kfgetc(network)) == kEOF)
break;
}
/* ordinary character */
if(c == '\r' || c == '\n')
break;
if(c == escape){
if(socklen(kfileno(network),0)) /* discard any remaining input */
recv_mbuf(kfileno(network),NULL,0,NULL,0);
cnt = -2;
break;
}
*buf++ = c;
++cnt;
if(cnt == len - 1)
break;
}
if(c == kEOF && cnt == 0)
return -1;
*buf = '\0';
return cnt;
}
int
domboxbye(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
m = (struct mbx *)p;
/* Now say goodbye */
kprintf("Thank you %s, for calling the %s Tcp/Ip Mailbox.\n",m->name,
Hostname);
if(m->type == TIP)
kprintf("Please hang up now.\n");
return -2; /* signal that exitbbs() should be called */
}
static int
dombhelp(argc,argv,p)
int argc;
char *argv[];
void *p;
{
char buf[255];
int i;
kFILE *fp;
if(*argv[0] == '?') {
kfputs(Longmenu1,kstdout);
kfputs(Longmenu2,kstdout);
kfputs(Longmenu3,kstdout);
return 0;
}
buf[0] = '\0';
if(argc > 1)
for(i=0; Mbcmds[i].name != NULL; ++i)
if(!strncmp(Mbcmds[i].name,argv[1],strlen(argv[1]))) {
sprintf(buf,"%s/%s.hlp",Helpdir,Mbcmds[i].name);
break;
}
if(buf[0] == '\0') {
if(*argv[0] == 'i') { /* INFO command */
sprintf(buf,"%s/info.hlp",Helpdir);
} else {
sprintf(buf,"%s/help.hlp",Helpdir);
}
}
if((fp = kfopen(buf,READ_TEXT)) != NULL) {
ksendfile(fp,Curproc->output,ASCII_TYPE,0);
kfclose(fp);
}
else
kprintf("No help available. (%s not found)\n",buf);
return 0;
}
static int
dochat(argc,argv,p)
int argc;
char *argv[];
void *p;
{
char buf[8], *newargv[3];
if(Attended){
newargv[0] = "telnet";
newargv[1] = Hostname;
sprintf(buf,"%d",IPPORT_TTYLINK);
newargv[2] = buf;
return dombtelnet(3,newargv,p);
}
else {
kprintf("Sorry - the system is unattended.\007\n");
}
/* It returns only after a disconnect or refusal */
return 0;
}
static int
dosend(argc,argv,p)
int argc;
char *argv[];
void *p;
{
int cccnt = 0, fail = 0;
char *host, *cp, fullfrom[MBXLINE], sigwork[LINELEN], *rhdr = NULL;
struct list *ap, *cclist = NULL;
struct mbx *m;
kFILE *fp;
m = (struct mbx *)p;
if((m->stype != 'R' || (m->sid & MBX_SID)) && mbx_to(argc,argv,m)
== -1){
if(m->sid & MBX_SID)
kprintf("NO - syntax error\n");
else {
kprintf("S command syntax error - format is:\n");
kprintf(" S[F] name [@ host] [< from_addr] [$bulletin_id]\n");
kprintf(" SR [number]\n");
}
return 0;
}
if(m->stype != 'R' && msgidcheck(m->tomsgid)) {
if(m->sid & MBX_SID)
kfputs("NO - ",kstdout);
kprintf("Already have %s\n",m->tomsgid);
return 0;
}
if(m->stype == 'R' && !(m->sid & MBX_SID) &&
mbx_reply(argc,argv,m,&cclist,&rhdr) == -1)
return 0;
if((cp = rewrite_address(m->to)) != NULL) {
if(strcmp(m->to,cp) != 0){
m->origto = m->to;
m->to = cp;
} else {
free(cp);
}
}
if((m->origto != NULL || m->stype == 'R') && !(m->sid & MBX_SID))
kprintf("To: %s\n", m->to);
if(validate_address(m->to) == 0){
if(m->sid & MBX_SID)
kprintf("NO - bad address\n");
else
kprintf("Bad user or host name\n");
free(rhdr);
del_list(cclist);
/* We don't free any more buffers here. They are freed upon
* the next call to mbx_to() or to domboxbye()
*/
return 0;
}
/* Display the Cc: line (during SR command) */
for(ap = cclist; ap != NULL; ap = ap->next) {
if(cccnt == 0){
kprintf("%s",Hdrs[CC]);
cccnt = 4;
}
else {
kfputs(", ",kstdout);
cccnt += 2;
}
if(cccnt + strlen(ap->val) > 80 - 3) {
kfputs("\n ",kstdout);
cccnt = 4;
}
kfputs(ap->val,kstdout);
cccnt += strlen(ap->val);
}
if(cccnt)
kputchar('\n');
m->state = MBX_SUBJ;
if(m->stype != 'R' || (m->sid & MBX_SID) != 0) {
kprintf((m->sid & MBX_SID) ? "OK\n" : "Subject: ");
if(mbxrecvline(m->user,m->line,MBXLINE,-1) == -1)
return 0;
}
else /* Replying to a message */
kprintf("Subject: %s\n",m->line);
if(mbx_data(m,cclist,rhdr) == -1){
free(rhdr);
del_list(cclist);
kputs("Can't create temp file for mail");
return 0;
}
free(rhdr);
m->state = MBX_DATA;
if((m->sid & MBX_SID) == 0 && m->stype != 'F')
kprintf("Enter message. %s",Howtoend);
if(m->stype != 'F' || (m->sid & MBX_SID) != 0)
while(mbxrecvline(m->user,m->line,MBXLINE,-1) != -1){
if(m->line[0] == 0x01){ /* CTRL-A */
kfclose(m->tfile);
kputs("Aborted.");
del_list(cclist);
return 0;
}
if(m->line[0] != CTLZ && STRICMP(m->line, "/ex"))
kfprintf(m->tfile,"%s\n",m->line);
else
break; /* all done */
}
else {
kfprintf(m->tfile,"----- Forwarded message -----\n\n");
msgtofile(m,m->current,m->tfile,0);
kfprintf(m->tfile,"----- End of forwarded message -----\n");
}
/* Insert customised signature if one is found */
if(!(m->sid & MBX_SID)) { /* not a forwarding BBS */
sprintf(sigwork,"%s/%s.sig",Signature,
m->tofrom ? m->tofrom : m->name);
if((fp = kfopen(sigwork,READ_TEXT)) != NULL){
while(kfgets(sigwork,LINELEN,fp) != NULL)
kfputs(sigwork,m->tfile);
kfclose(fp);
}
}
if((host = strrchr(m->to,'@')) == NULL) {
host = Hostname; /* use our hostname */
if(m->origto != NULL) {
/* rewrite_address() will be called again by our
* SMTP server, so revert to the original address.
*/
free(m->to);
m->to = m->origto;
m->origto = NULL;
}
}
else
host++; /* use the host part of address */
/* make up full from name for work file */
if(m->tofrom != NULL)
sprintf(fullfrom,"%s%%%s.bbs@%s",m->tofrom, m->name, Hostname);
else
sprintf(fullfrom,"%s@%s",m->name,Hostname);
if(cclist != NULL && STRICMP(host,Hostname) != 0) {
kfseek(m->tfile,0L,0); /* reset to beginning */
fail = queuejob(m->tfile,Hostname,cclist,fullfrom);
del_list(cclist);
cclist = NULL;
}
addlist(&cclist,m->to,0);
kfseek(m->tfile,0L,0);
fail += queuejob(m->tfile,host,cclist,fullfrom);
del_list(cclist);
kfclose(m->tfile);
if(fail)
kputs("Couldn't queue message for delivery");
else
if(m->tomsgid != NULL &&
(fp = kfopen(Historyfile,APPEND_TEXT)) != NULL) {
kfprintf(fp,"%s\n",m->tomsgid); /* Save BID in history file */
kfclose(fp);
}
smtptick(0L); /* wake SMTP to send that mail */
return 0;
}
static int
dosid(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
char *cp;
m = (struct mbx *)p;
if(argc == 1)
return 1;
if(argv[1][strlen(argv[1]) - 1] != ']') /* must be an SID */
return 1;
m->sid = MBX_SID;
/* Now check to see if this is an RLI board.
* As usual, Hank does it a bit differently from
* the rest of the world.
*/
if(m->stype == 'R' && strncmp(argv[1],"li",2) == 0)/* [RLI] at a minimum */
m->sid |= MBX_RLI_SID;
/* Check to see if the BBS supports a kludge called "hierarchical
* routing designators."
*
* No need to check for ']' -- it must be there or this is not
* a valid mbox id -- it is checked earlier (fix de OH3LKU)
*/
if((cp = strchr(argv[1],'-')) != NULL
&& (cp=strchr(cp+1,'h')) != NULL
&& strchr(cp+1,'$'))
m->sid |= MBX_HIER_SID;
return 0;
}
int
dombescape(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
m = (struct mbx *)p;
if(argc < 2){
kprintf("The escape character is: ");
if(m->escape < 32)
kprintf("CTRL-%c\n",m->escape+'A'-1);
else
kprintf("'%c'\n",m->escape);
return 0;
}
if(strlen(argv[1]) > 1)
if(isdigit(*argv[1]))
m->escape = (char) atoi(argv[1]);
else
return 1;
else
m->escape = *argv[1];
return 0;
}
static int
dodownload(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
kFILE *fp;
char *file;
m = (struct mbx *)p;
file = pathname(m->path,argv[1]);
if(!permcheck(m->path,m->privs,RETR_CMD,file)){
kprintf(Noperm);
return 0;
}
if((fp = kfopen(file,READ_TEXT)) == NULL)
kprintf("Can't open \"%s\": %s\n",file,ksys_errlist[kerrno]);
else
if(m->stype == 'U'){ /* uuencode ? */
kfclose(fp);
fp = kfopen(file,READ_BINARY); /* assume non-ascii */
uuencode(fp,m->user,file);
} else
ksendfile(fp,m->user,ASCII_TYPE,0);
free(file);
kfclose(fp);
return 0;
}
static int
dombupload(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
kFILE *fp;
char *file, buf[LINELEN];
m = (struct mbx *)p;
file = pathname(m->path,argv[1]);
if(!permcheck(m->path,m->privs,STOR_CMD,file)){
kprintf(Noperm);
return 0;
}
if((fp = kfopen(file,WRITE_TEXT)) == NULL){
kprintf("Can't create \"%s\": %s\n",file,ksys_errlist[kerrno]);
free(file);
return 0;
}
logmsg(kfileno(m->user),"MBOX upload: %s",file);
kprintf("Send file, %s",Howtoend);
for(;;){
if(mbxrecvline(m->user,buf,LINELEN,-1) == -1){
unlink(file);
break;
}
if(buf[0] == 0x01){ /* CTRL-A */
unlink(file);
kprintf("Aborted.\n");
break;
}
if(buf[0] == CTLZ || !STRICMP("/ex",buf))
break;
kfputs(buf,fp);
#if !defined(UNIX) && !defined(__TURBOC__) && !defined(AMIGA)
/* Needed only if the OS uses a CR/LF
* convention and putc doesn't do
* an automatic translation
*/
if(kputc('\r',fp) == kEOF)
break;
#endif
if(kputc('\n',fp) == kEOF)
break;
}
free(file);
kfclose(fp);
return 0;
}
static int
dowhat(argc,argv,p)
int argc;
char *argv[];
void *p;
{
struct mbx *m;
kFILE *fp;
char *file;
m = (struct mbx *)p;
if(argc < 2)
file = strdup(m->path);
else
file = pathname(m->path,argv[1]);
if(!permcheck(m->path,m->privs,RETR_CMD,file)){
kprintf(Noperm);
return 0;
}
if((fp = dir(file,1)) == NULL)
kprintf("Can't read directory: \"%s\": %s\n",file,ksys_errlist[kerrno]);
else
ksendfile(fp,m->user,ASCII_TYPE,0);
free(file);
kfclose(fp);
return 0;
}
static int
dozap(argc,argv,p)