forked from MidnightCommander/mc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftpfs.c
2037 lines (1757 loc) · 48.9 KB
/
ftpfs.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
/* Virtual File System: FTP file system.
Copyright (C) 1995 The Free Software Foundation
Written by: 1995 Ching Hui
1995 Jakub Jelinek
1995, 1996, 1997 Miguel de Icaza
1997 Norbert Warmuth
1998 Pavel Machek
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* FTPfs TODO:
- make it more robust - all the connects etc. should handle EADDRINUSE and
ERETRY (have I spelled these names correctly?)
- make the user able to flush a connection - all the caches will get empty
etc., (tarfs as well), we should give there a user selectable timeout
and assign a key sequence.
- use hash table instead of linklist to cache ftpfs directory.
What to do with this?
* NOTE: Usage of tildes is deprecated, consider:
* cd /#ftp:pavel@hobit
* cd ~
* And now: what do I want to do? Do I want to go to /home/pavel or to
* /#ftp:hobit/home/pavel? I think first has better sense...
*
{
int f = !strcmp( remote_path, "/~" );
if (f || !strncmp( remote_path, "/~/", 3 )) {
char *s;
s = concat_dir_and_file( qhome (*bucket), remote_path +3-f );
g_free (remote_path);
remote_path = s;
}
}
*/
/* Namespace pollution: horrible */
#include <config.h>
#include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
#include <netdb.h> /* struct hostent */
#include <sys/socket.h> /* AF_INET */
#include <netinet/in.h> /* struct in_addr */
#ifdef HAVE_SETSOCKOPT
# include <netinet/ip.h> /* IP options */
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <arpa/ftp.h>
#include <arpa/telnet.h>
#include <sys/param.h>
#include "utilvfs.h"
#include "xdirentry.h"
#include "vfs.h"
#include "tcputil.h"
#include "../src/dialog.h"
#include "../src/setup.h" /* for load_anon_passwd */
#include "container.h"
#include "ftpfs.h"
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 64
#endif
#define UPLOAD_ZERO_LENGTH_FILE
#define SUP super->u.ftp
#define FH_SOCK fh->u.ftp.sock
static int my_errno;
static int code;
/* Delay to retry a connection */
int ftpfs_retry_seconds = 30;
/* Method to use to connect to ftp sites */
int ftpfs_use_passive_connections = 1;
/* Method used to get directory listings:
* 1: try 'LIST -la <path>', if it fails
* fall back to CWD <path>; LIST
* 0: always use CWD <path>; LIST
*/
int ftpfs_use_unix_list_options = 1;
/* First "CWD <path>", then "LIST -la ." */
int ftpfs_first_cd_then_ls;
/* Use the ~/.netrc */
int use_netrc = 1;
extern char *home_dir;
/* Anonymous setup */
char *ftpfs_anonymous_passwd = NULL;
int ftpfs_directory_timeout = 900;
/* Proxy host */
char *ftpfs_proxy_host = NULL;
/* wether we have to use proxy by default? */
int ftpfs_always_use_proxy;
/* source routing host */
extern int source_route;
/* Where we store the transactions */
static FILE *logfile = NULL;
/* If true, the directory cache is forced to reload */
static int force_expiration = 0;
#ifdef FIXME_LATER_ALIGATOR
static struct linklist *connections_list;
#endif
/* command wait_flag: */
#define NONE 0x00
#define WAIT_REPLY 0x01
#define WANT_STRING 0x02
static char reply_str [80];
/* char *translate_path (struct ftpfs_connection *bucket, char *remote_path)
Translate a Unix path, i.e. MC's internal path representation (e.g.
/somedir/somefile) to a path valid for the remote server. Every path
transfered to the remote server has to be mangled by this function
right prior to sending it.
Currently only Amiga ftp servers are handled in a special manner.
When the remote server is an amiga:
a) strip leading slash if necesarry
b) replace first occurance of ":/" with ":"
c) strip trailing "/."
*/
static char *ftpfs_get_current_directory (vfs *me, vfs_s_super *super);
static int ftpfs_chdir_internal (vfs *me, vfs_s_super *super, char *remote_path);
static int command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...)
__attribute__ ((format (printf, 4, 5)));
static int ftpfs_open_socket (vfs *me, vfs_s_super *super);
static int login_server (vfs *me, vfs_s_super *super, const char *netrcpass);
static int lookup_netrc (const char *host, char **login, char **pass);
static char *
translate_path (vfs *me, vfs_s_super *super, const char *remote_path)
{
if (!SUP.remote_is_amiga)
return g_strdup (remote_path);
else {
char *ret, *p;
if (logfile) {
fprintf (logfile, "MC -- translate_path: %s\n", remote_path);
fflush (logfile);
}
/* strip leading slash(es) */
while (*remote_path == '/')
remote_path++;
/*
* Don't change "/" into "", e.g. "CWD " would be
* invalid.
*/
if (*remote_path == '\0')
return g_strdup (".");
ret = g_strdup (remote_path);
/* replace first occurance of ":/" with ":" */
if ((p = strchr (ret, ':')) && *(p + 1) == '/')
strcpy (p + 1, p + 2);
/* strip trailing "/." */
if ((p = strrchr (ret, '/')) && *(p + 1) == '.' && *(p + 2) == '\0')
*p = '\0';
return ret;
}
}
/* Extract the hostname and username from the path */
/*
* path is in the form: [user@]hostname:port/remote-dir, e.g.:
* ftp://sunsite.unc.edu/pub/linux
* ftp://[email protected]/c/nc
* ftp://tsx-11.mit.edu:8192/
* ftp://[email protected]:11321/private
* If the user is empty, e.g. ftp://@roxanne/private, then your login name
* is supplied.
*
*/
#define FTP_COMMAND_PORT 21
static void
ftp_split_url(char *path, char **host, char **user, int *port, char **pass)
{
char *p;
p = vfs_split_url (path, host, user, port, pass, FTP_COMMAND_PORT,
URL_ALLOW_ANON);
if (!*user) {
/* Look up user and password in netrc */
if (use_netrc)
lookup_netrc (*host, user, pass);
if (!*user)
*user = g_strdup ("anonymous");
}
/* Look up password in netrc for known user */
if (use_netrc && *user && pass && !*pass) {
char *new_user;
lookup_netrc (*host, &new_user, pass);
/* If user is different, remove password */
if (new_user && strcmp (*user, new_user)) {
g_free (*pass);
*pass = NULL;
}
g_free (new_user);
}
if (p)
g_free (p);
}
/* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
static int
get_reply (vfs *me, int sock, char *string_buf, int string_len)
{
char answer[BUF_1K];
int i;
for (;;) {
if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n')){
if (string_buf)
*string_buf = 0;
code = 421;
return 4;
}
switch (sscanf(answer, "%d", &code)){
case 0:
if (string_buf) {
strncpy (string_buf, answer, string_len - 1);
*(string_buf + string_len - 1) = 0;
}
code = 500;
return 5;
case 1:
if (answer[3] == '-') {
while (1) {
if (!vfs_s_get_line (me, sock, answer, sizeof(answer), '\n')){
if (string_buf)
*string_buf = 0;
code = 421;
return 4;
}
if ((sscanf (answer, "%d", &i) > 0) &&
(code == i) && (answer[3] == ' '))
break;
}
}
if (string_buf){
strncpy (string_buf, answer, string_len - 1);
*(string_buf + string_len - 1) = 0;
}
return code / 100;
}
}
}
static int
reconnect (vfs *me, vfs_s_super *super)
{
int sock = ftpfs_open_socket (me, super);
if (sock != -1){
char *cwdir = SUP.cwdir;
close (SUP.sock);
SUP.sock = sock;
SUP.cwdir = NULL;
if (login_server (me, super, SUP.password)){
if (!cwdir)
return 1;
sock = ftpfs_chdir_internal (me, super, cwdir);
g_free (cwdir);
return sock == COMPLETE;
}
SUP.cwdir = cwdir;
}
return 0;
}
static int
command (vfs *me, vfs_s_super *super, int wait_reply, char *fmt, ...)
{
va_list ap;
char *str, *fmt_str;
int status;
int sock = SUP.sock;
va_start (ap, fmt);
fmt_str = g_strdup_vprintf (fmt, ap);
va_end (ap);
status = strlen (fmt_str);
str = g_realloc (fmt_str, status + 3);
strcpy (str + status, "\r\n");
if (logfile){
if (strncmp (str, "PASS ", 5) == 0){
fputs ("PASS <Password not logged>\r\n", logfile);
} else
fwrite (str, status + 2, 1, logfile);
fflush (logfile);
}
got_sigpipe = 0;
enable_interrupt_key ();
status = write (SUP.sock, str, status + 2);
if (status < 0){
code = 421;
if (errno == EPIPE){ /* Remote server has closed connection */
static int level = 0; /* login_server() use command() */
if (level == 0){
level = 1;
status = reconnect (me, super);
level = 0;
if (status && write (SUP.sock, str, status + 2) > 0)
goto ok;
}
got_sigpipe = 1;
}
g_free (str);
disable_interrupt_key ();
return TRANSIENT;
}
ok:
g_free (str);
disable_interrupt_key ();
if (wait_reply)
return get_reply (me, sock, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
return COMPLETE;
}
static void
free_archive (vfs *me, vfs_s_super *super)
{
if (SUP.sock != -1){
print_vfs_message (_("ftpfs: Disconnecting from %s"), SUP.host);
command(me, super, NONE, "QUIT");
close(SUP.sock);
}
g_free (SUP.host);
g_free (SUP.user);
g_free (SUP.cwdir);
g_free (SUP.password);
}
/* some defines only used by changetype */
/* These two are valid values for the second parameter */
#define TYPE_ASCII 0
#define TYPE_BINARY 1
/* This one is only used to initialize bucket->isbinary, don't use it as
second parameter to changetype. */
#define TYPE_UNKNOWN -1
static int
changetype (vfs *me, vfs_s_super *super, int binary)
{
if (binary != SUP.isbinary) {
if (command (me, super, WAIT_REPLY, "TYPE %c", binary ? 'I' : 'A') != COMPLETE)
ERRNOR (EIO, -1);
SUP.isbinary = binary;
}
return binary;
}
/* This routine logs the user in */
static int
login_server (vfs *me, vfs_s_super *super, const char *netrcpass)
{
char *pass;
char *op;
char *name; /* login user name */
int anon = 0;
char reply_string[BUF_MEDIUM];
SUP.isbinary = TYPE_UNKNOWN;
if (netrcpass)
op = g_strdup (netrcpass);
else {
if (!strcmp (SUP.user, "anonymous") ||
!strcmp (SUP.user, "ftp")) {
if (!ftpfs_anonymous_passwd)
ftpfs_init_passwd();
op = g_strdup (ftpfs_anonymous_passwd);
anon = 1;
} else {
char *p;
if (!SUP.password){
p = g_strconcat (_(" FTP: Password required for "), SUP.user,
" ", NULL);
op = vfs_get_password (p);
g_free (p);
if (op == NULL)
ERRNOR (EPERM, 0);
SUP.password = g_strdup (op);
} else
op = g_strdup (SUP.password);
}
}
if (!anon || logfile)
pass = op;
else {
pass = g_strconcat ("-", op, NULL);
wipe_password (op);
}
/* Proxy server accepts: username@host-we-want-to-connect*/
if (SUP.proxy){
name = g_strconcat (SUP.user, "@",
SUP.host[0] == '!' ? SUP.host+1 : SUP.host, NULL);
} else
name = g_strdup (SUP.user);
if (get_reply (me, SUP.sock, reply_string, sizeof (reply_string) - 1) == COMPLETE) {
g_strup (reply_string);
SUP.remote_is_amiga = strstr (reply_string, "AMIGA") != 0;
if (logfile) {
fprintf (logfile, "MC -- remote_is_amiga = %d\n", SUP.remote_is_amiga);
fflush (logfile);
}
print_vfs_message (_("ftpfs: sending login name"));
code = command (me, super, WAIT_REPLY, "USER %s", name);
switch (code){
case CONTINUE:
print_vfs_message (_("ftpfs: sending user password"));
if (command (me, super, WAIT_REPLY, "PASS %s", pass) != COMPLETE)
break;
case COMPLETE:
print_vfs_message (_("ftpfs: logged in"));
wipe_password (pass);
g_free (name);
return 1;
default:
SUP.failed_on_login = 1;
/* my_errno = E; */
if (SUP.password)
wipe_password (SUP.password);
SUP.password = 0;
goto login_fail;
}
}
message_2s (1, MSG_ERROR, _("ftpfs: Login incorrect for user %s "), SUP.user);
login_fail:
wipe_password (pass);
g_free (name);
ERRNOR (EPERM, 0);
}
#ifdef HAVE_SETSOCKOPT
static void
setup_source_route (int socket, int dest)
{
char buffer [20];
char *ptr = buffer;
if (!source_route)
return;
memset (buffer, 0, sizeof (buffer));
*ptr++ = IPOPT_LSRR;
*ptr++ = 3 + 8;
*ptr++ = 4; /* pointer */
/* First hop */
memcpy (ptr, (char *) &source_route, sizeof (int));
ptr += 4;
/* Second hop (ie, final destination) */
memcpy (ptr, (char *) &dest, sizeof (int));
ptr += 4;
while ((ptr - buffer) & 3)
ptr++;
if (setsockopt (socket, IPPROTO_IP, IP_OPTIONS,
buffer, ptr - buffer) < 0)
message_2s (1, MSG_ERROR, _(" Could not set source routing (%s)"), unix_error_string (errno));
}
#else
#define setup_source_route(x,y)
#endif
static struct no_proxy_entry {
char *domain;
void *next;
} *no_proxy;
static void
load_no_proxy_list (void)
{
/* FixMe: shouldn't be hardcoded!!! */
char s[BUF_LARGE]; /* provide for BUF_LARGE characters */
struct no_proxy_entry *np, *current = 0;
FILE *npf;
int c;
char *p;
static char *mc_file;
if (mc_file)
return;
mc_file = concat_dir_and_file (mc_home, "mc.no_proxy");
if (exist_file (mc_file) &&
(npf = fopen (mc_file, "r"))) {
while (fgets (s, sizeof(s), npf) || !(feof (npf) || ferror (npf))) {
if (!(p = strchr (s, '\n'))) { /* skip bogus entries */
while ((c = fgetc (npf)) != EOF && c != '\n')
;
continue;
}
if (p == s)
continue;
*p = '\0';
np = g_new (struct no_proxy_entry, 1);
np->domain = g_strdup (s);
np->next = NULL;
if (no_proxy)
current->next = np;
else
no_proxy = np;
current = np;
}
fclose (npf);
}
g_free (mc_file);
}
static int
ftpfs_check_proxy (const char *host)
{
struct no_proxy_entry *npe;
if (!ftpfs_proxy_host || !*ftpfs_proxy_host || !host || !*host)
return 0; /* sanity check */
if (*host == '!')
return 1;
if (!ftpfs_always_use_proxy)
return 0;
if (!strchr (host, '.'))
return 0;
load_no_proxy_list ();
for (npe = no_proxy; npe; npe=npe->next) {
char *domain = npe->domain;
if (domain[0] == '.') {
int ld = strlen (domain);
int lh = strlen (host);
while (ld && lh && host[lh - 1] == domain[ld - 1]) {
ld--;
lh--;
}
if (!ld)
return 0;
} else
if (!g_strcasecmp (host, domain))
return 0;
}
return 1;
}
static void
ftpfs_get_proxy_host_and_port (char *proxy, char **host, int *port)
{
char *user, *dir;
dir =
vfs_split_url (proxy, host, &user, port, 0, FTP_COMMAND_PORT,
URL_ALLOW_ANON);
g_free (user);
g_free (dir);
}
static int
ftpfs_open_socket (vfs *me, vfs_s_super *super)
{
struct sockaddr_in server_address;
struct hostent *hp;
int my_socket;
char *host;
int port = SUP.port;
int free_host = 0;
/* Use a proxy host? */
host = SUP.host;
if (!host || !*host){
print_vfs_message (_("ftpfs: Invalid host name."));
my_errno = EINVAL;
return -1;
}
/* Hosts to connect to that start with a ! should use proxy */
if (SUP.proxy){
ftpfs_get_proxy_host_and_port (ftpfs_proxy_host, &host, &port);
free_host = 1;
}
/* Get host address */
memset ((char *) &server_address, 0, sizeof (server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr (host);
if (server_address.sin_addr.s_addr != -1)
server_address.sin_family = AF_INET;
else {
hp = gethostbyname (host);
if (hp == NULL){
print_vfs_message (_("ftpfs: Invalid host address."));
my_errno = EINVAL;
if (free_host)
g_free (host);
return -1;
}
server_address.sin_family = hp->h_addrtype;
/* We copy only 4 bytes, we can not trust hp->h_length, as it comes from the DNS */
memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr, 4);
}
server_address.sin_port = htons (port);
/* Connect */
if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
my_errno = errno;
if (free_host)
g_free (host);
return -1;
}
setup_source_route (my_socket, server_address.sin_addr.s_addr);
print_vfs_message (_("ftpfs: making connection to %s"), host);
if (free_host)
g_free (host);
enable_interrupt_key (); /* clear the interrupt flag */
if (connect (my_socket, (struct sockaddr *) &server_address,
sizeof (server_address)) < 0){
my_errno = errno;
if (errno == EINTR && got_interrupt ())
print_vfs_message (_("ftpfs: connection interrupted by user"));
else
print_vfs_message (_("ftpfs: connection to server failed: %s"),
unix_error_string(errno));
disable_interrupt_key();
close (my_socket);
return -1;
}
disable_interrupt_key();
return my_socket;
}
static int
open_archive_int (vfs *me, vfs_s_super *super)
{
int retry_seconds, count_down;
/* We do not want to use the passive if we are using proxies */
if (SUP.proxy)
SUP.use_passive_connection = 0;
retry_seconds = 0;
do {
SUP.failed_on_login = 0;
SUP.sock = ftpfs_open_socket (me, super);
if (SUP.sock == -1)
return -1;
if (login_server (me, super, NULL)) {
/* Logged in, no need to retry the connection */
break;
} else {
if (SUP.failed_on_login){
/* Close only the socket descriptor */
close (SUP.sock);
} else {
return -1;
}
if (ftpfs_retry_seconds){
retry_seconds = ftpfs_retry_seconds;
enable_interrupt_key ();
for (count_down = retry_seconds; count_down; count_down--){
print_vfs_message (_("Waiting to retry... %d (Control-C to cancel)"), count_down);
sleep (1);
if (got_interrupt ()){
/* my_errno = E; */
disable_interrupt_key ();
return 0;
}
}
disable_interrupt_key ();
}
}
} while (retry_seconds);
SUP.cwdir = ftpfs_get_current_directory (me, super);
if (!SUP.cwdir)
SUP.cwdir = g_strdup (PATH_SEP_STR);
return 0;
}
static int
open_archive (vfs *me, vfs_s_super *super, char *archive_name, char *op)
{
char *host, *user, *password;
int port;
ftp_split_url (strchr (op, ':') + 1, &host, &user, &port, &password);
SUP.host = host;
SUP.user = user;
SUP.port = port;
SUP.cwdir = NULL;
SUP.proxy = 0;
if (ftpfs_check_proxy (host))
SUP.proxy = ftpfs_proxy_host;
SUP.password = password;
SUP.use_passive_connection = ftpfs_use_passive_connections | source_route;
SUP.use_source_route = source_route;
SUP.strict = ftpfs_use_unix_list_options ? RFC_AUTODETECT : RFC_STRICT;
SUP.isbinary = TYPE_UNKNOWN;
SUP.remote_is_amiga = 0;
super->name = g_strdup("/");
super->root = vfs_s_new_inode (me, super, vfs_s_default_stat(me, S_IFDIR | 0755));
return open_archive_int (me, super);
}
static int
archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *cookie)
{
char *host, *user;
int port;
ftp_split_url (strchr(op, ':') + 1, &host, &user, &port, 0);
port = ((strcmp (host, SUP.host) == 0) &&
(strcmp (user, SUP.user) == 0) &&
(port == SUP.port));
g_free (host);
g_free (user);
return port;
}
void
ftpfs_flushdir (void)
{
force_expiration = 1;
}
static int
dir_uptodate(vfs *me, vfs_s_inode *ino)
{
struct timeval tim;
if (force_expiration) {
force_expiration = 0;
return 0;
}
gettimeofday(&tim, NULL);
if (tim.tv_sec < ino->u.ftp.timestamp.tv_sec)
return 1;
return 0;
}
/* The returned directory should always contain a trailing slash */
static char *
ftpfs_get_current_directory (vfs *me, vfs_s_super *super)
{
char buf[BUF_8K], *bufp, *bufq;
if (command (me, super, NONE, "PWD") == COMPLETE &&
get_reply(me, SUP.sock, buf, sizeof(buf)) == COMPLETE) {
bufp = NULL;
for (bufq = buf; *bufq; bufq++)
if (*bufq == '"') {
if (!bufp) {
bufp = bufq + 1;
} else {
*bufq = 0;
if (*bufp) {
if (*(bufq - 1) != '/') {
*bufq++ = '/';
*bufq = 0;
}
if (*bufp == '/')
return g_strdup (bufp);
else {
/* If the remote server is an Amiga a leading slash
might be missing. MC needs it because it is used
as seperator between hostname and path internally. */
return g_strconcat( "/", bufp, 0);
}
} else {
my_errno = EIO;
return NULL;
}
}
}
}
my_errno = EIO;
return NULL;
}
/* Setup Passive ftp connection, we use it for source routed connections */
static int
setup_passive (vfs *me, vfs_s_super *super, int my_socket, struct sockaddr_in *sa)
{
int xa, xb, xc, xd, xe, xf;
char n [6];
char *c = reply_str;
if (command (me, super, WAIT_REPLY | WANT_STRING, "PASV") != COMPLETE)
return 0;
/* Parse remote parameters */
for (c = reply_str + 4; (*c) && (!isdigit ((unsigned char) *c)); c++)
;
if (!*c)
return 0;
if (!isdigit ((unsigned char) *c))
return 0;
if (sscanf (c, "%d,%d,%d,%d,%d,%d", &xa, &xb, &xc, &xd, &xe, &xf) != 6)
return 0;
n [0] = (unsigned char) xa;
n [1] = (unsigned char) xb;
n [2] = (unsigned char) xc;
n [3] = (unsigned char) xd;
n [4] = (unsigned char) xe;
n [5] = (unsigned char) xf;
memcpy (&(sa->sin_addr.s_addr), (void *)n, 4);
memcpy (&(sa->sin_port), (void *)&n[4], 2);
setup_source_route (my_socket, sa->sin_addr.s_addr);
if (connect (my_socket, (struct sockaddr *) sa, sizeof (struct sockaddr_in)) < 0)
return 0;
return 1;
}
static int
initconn (vfs *me, vfs_s_super *super)
{
struct sockaddr_in data_addr;
int data, len = sizeof(data_addr);
struct protoent *pe;
getsockname(SUP.sock, (struct sockaddr *) &data_addr, &len);
data_addr.sin_port = 0;
pe = getprotobyname("tcp");
if (pe == NULL)
ERRNOR (EIO, -1);
data = socket (AF_INET, SOCK_STREAM, pe->p_proto);
if (data < 0)
ERRNOR (EIO, -1);
if (SUP.use_passive_connection){
if ((SUP.use_passive_connection = setup_passive (me, super, data, &data_addr)))
return data;
SUP.use_source_route = 0;
SUP.use_passive_connection = 0;
print_vfs_message (_("ftpfs: could not setup passive mode"));
}
/* If passive setup fails, fallback to active connections */
/* Active FTP connection */
if ((bind (data, (struct sockaddr *)&data_addr, len) == 0) &&
(getsockname (data, (struct sockaddr *) &data_addr, &len) == 0) &&
(listen (data, 1) == 0))
{
unsigned char *a = (unsigned char *)&data_addr.sin_addr;
unsigned char *p = (unsigned char *)&data_addr.sin_port;
if (command (me, super, WAIT_REPLY, "PORT %d,%d,%d,%d,%d,%d", a[0], a[1],
a[2], a[3], p[0], p[1]) == COMPLETE)
return data;
}
close(data);
my_errno = EIO;
return -1;
}
static int
open_data_connection (vfs *me, vfs_s_super *super, char *cmd, char *remote,
int isbinary, int reget)
{
struct sockaddr_in from;
int s, j, data, fromlen = sizeof(from);
if ((s = initconn (me, super)) == -1)
return -1;
if (changetype (me, super, isbinary) == -1)
return -1;
if (reget > 0){
j = command (me, super, WAIT_REPLY, "REST %d", reget);
if (j != CONTINUE)
return -1;
}
if (remote) {
char * remote_path = translate_path (me, super, remote);
j = command (me, super, WAIT_REPLY, "%s /%s", cmd,
/* WarFtpD can't STORE //filename */
(*remote_path == '/') ? remote_path + 1 : remote_path);
g_free (remote_path);
} else
j = command (me, super, WAIT_REPLY, "%s", cmd);
if (j != PRELIM)
ERRNOR (EPERM, -1);
enable_interrupt_key();
if (SUP.use_passive_connection)
data = s;
else {
data = accept (s, (struct sockaddr *)&from, &fromlen);
close(s);
if (data < 0) {
my_errno = errno;
return -1;
}
}
disable_interrupt_key();
return data;
}
static void
linear_abort (vfs *me, vfs_s_fh *fh)
{
vfs_s_super *super = FH_SUPER;
static unsigned char const ipbuf[3] = { IAC, IP, IAC };
fd_set mask;
char buf[1024];
int dsock = FH_SOCK;
FH_SOCK = -1;
SUP.control_connection_buzy = 0;
print_vfs_message(_("ftpfs: aborting transfer."));
if (send(SUP.sock, ipbuf, sizeof(ipbuf), MSG_OOB) != sizeof(ipbuf)) {
print_vfs_message(_("ftpfs: abort error: %s"), unix_error_string(errno));
return;
}
if (command(me, super, NONE, "%cABOR", DM) != COMPLETE){