forked from bakagirl/Arachne-WWW-browser
/
FTP.C
648 lines (570 loc) · 16.4 KB
/
FTP.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
// ========================================================================
// Arachne WWW browser FTP functions
// (c)1997-1999 Michael Polak, Arachne Labs (xChaos software)
// ========================================================================
#include "arachne.h"
#include "internet.h"
#define BUFLEN 512
int ftpsession(struct Url *url,struct HTTPrecord *cache,char *uploadfile)
{
longword host;
char str[256];
char buffer[BUFLEN+2];
tcp_Socket datasocket;
word dataport=0;
int rv=0,len;
char *ptr,*datahostptr,datahost[80];
char isdir=0,retry=0;//,ascii=0;
long total=0;
//!!glennmcc: Nov 11, 2007 -- for 'dblp code' below
int dblp=0;
//!!glennmcc: end
//!!glennmcc: Nov 13, 2007 -- for EZNOS2 fix below
int eznos2=0;
//!!glennmcc: end
int log;
if(!tcpip)return 0;
free_socket();
if((!url->file[0] || url->file[strlen(url->file)-1]=='/') && !uploadfile)
isdir=1;
sprintf(str,msg_askdns,url->host);
outs(str);
GlobalLogoStyle=0; //SDL set resolve animation
host=resolve_fn( url->host, (sockfunct_t) TcpIdleFunc ); //SDL
// host=resolve( url->host );
if(!host)
{
DNSerr(url->host);
return 0;
}
// if(!uploadfile) //!glennmcc: Oct 20, 2012 - commented-out to make upload log more verbose
log=a_open("FTP.LOG",O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
GlobalLogoStyle=2; //SDL set connect animation
if (!tcp_open( socket, locport(), host, url->port, NULL ))
{
sprintf(str,msg_errcon,url->host);
outs(str);
return 0;
}
sprintf(str,msg_con,url->host,url->port);
outs(str);
write(log,str,strlen(str));
write(log,"\r\n",2);
sock_wait_established(socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status); //SDL
GlobalLogoStyle=1; //SDL set data animation
sock_mode( socket, TCP_MODE_ASCII );
outs(MSG_LOGIN);
do
{
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
if ( *buffer != '2' && buffer[0]!=' ') goto quit;
}
while(buffer[3]=='-' || buffer[0]==' '); //continued message!
if(!url->user[0])
ptr="anonymous";
else
ptr=url->user;
sprintf( str, "USER %s", ptr);
write(log,str,strlen(str));
write(log,"\r\n",2);
sock_puts(socket,(unsigned char *)str);
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
//!!glennmcc: May 11, 2005
//removed due to the fact that not all sites use '3'
//see additional info below with respect to anonymous password
// if ( *buffer != '3' ) goto quit;
//!!glennmcc: end
//open cache filename:
//!glennmcc: Oct 20, 2012 - commented-out to make upload log more verbose
// if(uploadfile)
// strcpy(cache->locname,"FTP.LOG");
// else
//!glennmcc end: Oct 20, 2012
{
//!!glennmcc: Oct 22, 2008 -- strchr() was preventing the use of 'CachePath .\cache\'
// ptr=strchr(cache->locname,'.');
ptr=strrchr(cache->locname,'.');
//!!glennmcc: end
if(ptr)
{
strcpy(&ptr[1],"FTP");
strcpy(cache->rawname,cache->locname);
}
}
cache->handle=a_open(cache->locname,O_BINARY|O_WRONLY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE);
if(cache->handle<0)
goto quit;
strcpy(cache->mime,"text/plain");
if(url->password[0])
ptr=url->password;
else
{
if(url->user[0] && !strcmp(url->host,AUTHENTICATION->host)
&& !strcmp(AUTHENTICATION->realm,"$ftp"))
ptr=AUTHENTICATION->password;
else
{
ptr=configvariable(&ARACHNEcfg,"FakeFTPeMail",NULL);
if(!ptr || !strchr(ptr,'@'))
{
ptr=configvariable(&ARACHNEcfg,"eMail",NULL);
if(!ptr)
ptr="@";
}
}
}
//!!glennmcc: May 11, 2005
//some sites do not require a password after 'anonymous'
//therefer, this entire block is now within this 'if()' so that
//the password (email address), will only be sent if asked for
//!!glennmcc: Nov 13, 2007 -- EZNOS2 says "Enter PASS command"
if (strstr(buffer,"sword") || strstr(buffer,"Enter PASS command"))
//if (strstr(buffer,"sword"))//original line
{
sprintf( str, "PASS %s", ptr);
sock_puts(socket,(unsigned char *)str);
write(log,str,strlen(str));
write(log,"\r\n",2);
}//!!glennmcc: inserted Mar 02, 2008
//Some servers need the following 'do/while' section,
//therefore, only the section above for sending the password needs to be
//'blocked' when no password was requested.
do
{
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
if (strstr(buffer,"Enter PASS command")) eznos2=1;
if (*buffer != '2' && buffer[0]!=' ' && !eznos2)
{
write(cache->handle,buffer,strlen(buffer));
rv=1;
goto quit;
}
else if ((buffer[3]=='-' || buffer[0]==' ') && (isdir || uploadfile))
{
strcat(buffer,"\r\n");
rv=1;
write(cache->handle,buffer,strlen(buffer));
}
}
while(buffer[3]=='-' || buffer[0]==' ' || buffer[0]=='3'); //continued message!
//}//!!glennmcc: end May 11, 2005 -- removed on Mar 02, 2008
//ask server where we have to connect:
sock_puts(socket,(unsigned char *)"PASV");
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
//2xx Entering passive mode (a,b,c,d,x,y)
if ( *buffer != '2' ) goto quit;
datahostptr=strchr(buffer,'(');
//!!glennmcc: Nov 13, 2007 -- EZNOS2 doesn't enclose the info in ()
//therefore, if '(' is not found... look for the last 'space'.
if(!datahostptr) {datahostptr=strrchr(buffer,' '); eznos2=1;}
//if that still fails... 'quit'
//!!glennmcc: end
if(!datahostptr) goto quit;//original line
ptr=++datahostptr;
{
int carka=0;
char *portptr=NULL;
while(*ptr)
{
if(*ptr==',')
{
carka++;
if(carka<4)
*ptr='.';
else if(carka==4)
{
*ptr='\0';
portptr=ptr+1;
}
else if (carka==5)
{
*ptr='\0';
dataport=256*(word)atoi(portptr); // ,x,y -> 256*x+y
portptr=ptr+1;
//!!glennmcc: Nov 13, 2007 -- part of above fix for EZNO2 info not in ()
if(eznos2) dataport+=atoi(portptr); // ,x,y -> 256*x+y
//!!glennmcc: end
}
}
else if(*ptr==')' && portptr)
{
//!!glennmcc: Nov 11, 2007 -- some servers have double ')'
// at the end of the port address info...
// this 'dblp code' will prevent that from adding the final set twice
//eg: (99,167,219,186,234,255)) instead of.... (99,167,219,186,234,255)
//without this fix ... 255 gets added a 2nd time and
//we end-up-with... port 60414 instead of the correct port of 60159
*ptr='\0';
if(!dblp)//!!glennmcc: Nov 11, 2007
dataport+=atoi(portptr); // ,x,y -> 256*x+y
dblp=1;//!!glennmcc: Nov 11, 2007
}
ptr++;
}
}
if(!dataport)
goto quit;
//!!glennmcc: Aug 31, 2009
//EZNOS2 sends the IP of the machine on a router as datahost
//therefore we need to go back to the original host
if(eznos2)
{
makestr(datahost,url->host,79);
outs(datahost);
Piip();
}
else
//!!glennmcc:end
makestr(datahost,datahostptr,79);//original line
retry:
if(isdir)
{
if(url->file[0])
{
//!!glennmcc: Oct 15, 2007 -- fix problems with CWD on FTP servers
//which interpret the leading '/' as an attempted CD to 'root'
if(url->file[0]=='/')
sprintf( str, "CWD %s", url->file+1);
else
//!!glennmcc: end Oct 15, 2007
sprintf( str, "CWD %s", url->file);
sock_puts(socket,(unsigned char *)str);
do
{
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
//!!glennmcc: Apr 08, 2005 -- commented-out this block
// to fix the problem of 'broken dir listing' when the
// 'FTP welcome message' contains linefeeds
// such as at ftp://ftp.cdrom.com/.2/simtelnet/
/*
if ( *buffer != '2' && buffer[0]!=' ')
{
write(cache->handle,buffer,strlen(buffer));
rv=1;
goto quit;
}
else if (buffer[3]=='-' || buffer[0]==' ')
*/
//!!glennmcc: end
{
strcat(buffer,"\r\n");
rv=1;
write(cache->handle,buffer,strlen(buffer));
}
}
//!!glennmcc: Apr 08, 2005 -- added a test for !=' ' which is also
// needed for the same fix at ftp://ftp.cdrom.com/.2/simtelnet/
while(buffer[3]=='-' || buffer[3]!=' ' || buffer[0]==' '); //continued message!
// while(buffer[3]=='-' || buffer[0]==' '); //continued message!
//!!glennmcc: end
}
strcpy(cache->mime,"ftp/list");
sprintf( str, "LIST");
}
else
{
char *fnameptr;
char mimestr[80]="ftp/binary";
fnameptr=strrchr(url->file,'/');
if(!fnameptr)
{
fnameptr=strrchr(url->file,'\\');
if(!fnameptr)
fnameptr=url->file;
else
fnameptr++;
}
else
fnameptr++;
sprintf( str, "TYPE I");
if(fnameptr)
{
char ext[5];
strcpy(mimestr,"file/");
strncat(mimestr,fnameptr,70);
mimestr[79]='\0';
get_extension(mimestr,ext);
if(!strncmpi(ext,"TXT",3) || !strncmpi(ext,"HTM",3))
{
//!!glennmcc: begin June 09, 2002
//optionally upload TXT and HTM in binary mode
ptr=configvariable(&ARACHNEcfg,"UseBinaryFTP",NULL);
if(!ptr || toupper(*ptr)=='N')
//!!glennmcc: end
sprintf( str, "TYPE A");
// ascii=1;
}
}
strcpy(cache->mime,mimestr);
sock_puts(socket,(unsigned char *)str);
write(log,str,strlen(str));
write(log,"\r\n",2);
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
if ( *buffer != '2' || uploadfile)
{
strcat(buffer,"\n");
write(cache->handle,buffer,strlen(buffer));
}
if ( *buffer != '2' )
{
rv=1;
goto quit;
}
if(!uploadfile)
//!!glennmcc: Oct 17, 2007 -- fix problems with FTP servers
//which interpret the leading '/' as an attempted CD to 'root'
if(url->file[0]=='/')
sprintf( str, "RETR %s", url->file+1);
else
sprintf( str, "RETR %s", url->file);
//original single line above this comment
//!!glennmcc: end
else
sprintf( str, "STOR %s", url->file);
}
sock_puts(socket,(unsigned char *)str);
write(log,str,strlen(str));
write(log,"\r\n",2);
//!!glennmcc: Oct 19, 2008 -- back to original fix
//!!glennmcc: Nov 15, 2007 -- always 'close' the connection when done
//with both dir listings and file downloads
//Apr 10, 2007 fix did it only for dir listings
if(isdir || strstr(str,"RETR"))
//if(isdir)
sock_puts(socket,(unsigned char *)"QUIT");//!!glennmcc: Apr 10, 2007
//!!glennmcc: end
if(!retry)
{
//get file using datahost & dataport
GlobalLogoStyle=0; //SDL set resolve animation
host=resolve_fn( datahost, (sockfunct_t) TcpIdleFunc ); //SDL
// host=resolve( datahost );
if(!host)
goto quit;
GlobalLogoStyle=2; //SDL set connect animation
if (!tcp_open( &datasocket, locport(), host, dataport, NULL ))
{
sprintf(str,msg_errcon,datahost);
outs(str);
goto quit;
}
sprintf(str,msg_con,datahost,dataport);
outs(str);
write(log,str,strlen(str));
write(log,"\r\n",2);
//wait for datasocket to open:
sock_wait_established(&datasocket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status); //SDL
//!!glennmcc: Sep 27, 2008 -- increase D/L speed on cable & DSL
//many thanks to 'mik' for pointing me in the right direction. :)
{
#ifdef DEBUG
char sp[80];
sprintf(sp,"Available stack = %u bytes",_SP);
outs(sp);
Piip(); Piip();
#endif
if(_SP>(1024*SETBUFSIZE))
{
char setbuf[1024*SETBUFSIZE];
sock_setbuf(&datasocket, (unsigned char *)setbuf, 1024*SETBUFSIZE);
}
}
//!!glennmcc: end
GlobalLogoStyle=1; //SDL set data animation
}
//wait for "110 openning connection" (or "550 ....error....")
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
write(log,buffer,strlen(buffer));
write(log,"\r\n",2);
// printf("FTP daemon said>");
// puts(buffer);
if ( *buffer != '1' || uploadfile)
{
strcat(buffer,"\n");
write(cache->handle,buffer,strlen(buffer));
}
if ( *buffer != '1' )
{
if(!strncmp(buffer,"550",3) && !retry)
{
retry=1;
isdir=1-isdir;
if(isdir)
strcat(url->file,"/");
else
{
int i=strlen(url->file);
if(i>0 && url->file[i-1]=='/')
url->file[i-1]='\0';
}
goto retry;
}
strcpy(cache->mime,"text/plain");
rv=1;
goto dataquit;
}
if(!uploadfile) //-------------------------------------- download ---------------
{
while ( 1 )
{
xChLogoTICK(1);
if(GUITICK())
if(GLOBAL.gotolocation || GLOBAL.abort)
goto dataquit;
if (sock_dataready( &datasocket ))
{
len = sock_fastread( &datasocket, (unsigned char*)buffer, BUFLEN );
write(cache->handle,buffer,len);
total+=len;
sprintf(str,MSG_BYTESR,MSG_DOWNLD,total);
outs(str);
}
else
sock_tick( &datasocket, &status ); //shift TCP/IP
}
}
else //-------------------------------------- upload ------------------
{
int f,lenread,done;
long length;
char pom[256];
/* if(ascii)
f=a_sopen(uploadfile,O_RDONLY|O_TEXT, SH_DENYNO, S_IREAD);
else*/
f=a_sopen(uploadfile,O_RDONLY|O_BINARY, SH_DENYNO, S_IREAD);
if(f<0)
goto dataquit;
lenread=done=0;
length=0l;
{
long filel=a_filelength(f);
while(1)
{
sprintf(pom,MSG_UPLOAD,length,filel);
outs(pom);
//!!glennmcc:Oct 23, 2008 -- 'reversed the logic'
// to keep from overflowing at 21megs
if(filel>100)
percentbar((int)(length/(filel/100)));
// percentbar((int)(100*length/filel));
lenread=a_read(f,buffer,BUFLEN);
length+=lenread;
if(lenread<=0)
done=1;
//wait until we can write to socket:
while(sock_tbleft(&datasocket)<lenread) //SDL
// while( datasocket.datalen > 1024)
{
sock_tick(&datasocket,&status);
xChLogoTICK(1); // animation of logo
if(GUITICK())
goto dataquit;
}
if(done)
{
sock_close( &datasocket );
a_close(f);
goto dataclose;
}
sock_fastwrite(&datasocket,(unsigned char *)buffer,lenread);
sock_tick(&datasocket,&status);
}//loop
}
}
dataquit:
//!!glennmcc: Nov 15, 2007 -- removed sock_abort because it was
// sometimes preventing the connection from being closed,
// therefore preventing access to several files within a short time
// due to too many connections open from the same IP
// sock_abort( &datasocket );//original line
//!!glennmcc: end
dataclose:
outs(MSG_CLOSE);
sock_puts(socket,(unsigned char *)"QUIT");//!!glennmcc: Dec 04, 2006
sock_wait_closed( &datasocket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
if(uploadfile)
{
sock_wait_input( socket, sock_delay, (sockfunct_t) TcpIdleFunc,
&status ); //SDL
sock_gets( socket, (unsigned char *)buffer, sizeof( buffer ));
outs(buffer);
}
quit:
sock_puts(socket,(unsigned char *)"QUIT");
sock_close( socket );
closing[socknum]=1;
sock_keepalive[socknum][0]='\0';
// sock_wait_closed( socket, sock_delay, NULL, &status );
//we will better wait because we are about to deallocate datasocket
sock_err:
switch (status) {
case 1 : /* foreign host closed */
write(log,MSG_CLOSE,strlen(MSG_CLOSE));
write(log,"\r\n",2);
close(log);
break;
case -1: /* timeout */
sprintf(str,MSG_TCPERR, sockerr(socket));
outs(str);
write(log,str,strlen(str));
write(log,"\r\n",2);
close(log);
break;
}
if(total)
{
cache->knowsize=1;
cache->size=total;
rv=1;
}
if(cache->handle>=0)
{
a_close(cache->handle);
rv=1;
}
return rv;
}