-
Notifications
You must be signed in to change notification settings - Fork 514
/
ftpfs.c
2784 lines (2278 loc) · 79.8 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-2024
Free Software Foundation, Inc.
Written by:
Ching Hui, 1995
Jakub Jelinek, 1995
Miguel de Icaza, 1995, 1996, 1997
Norbert Warmuth, 1997
Pavel Machek, 1998
Yury V. Zaytsev, 2010
Slava Zanko <slavazanko@gmail.com>, 2010, 2013
Andrew Borodin <aborodin@vmail.ru>, 2010-2022
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file
* \brief Source: Virtual File System: FTP file system
* \author Ching Hui
* \author Jakub Jelinek
* \author Miguel de Icaza
* \author Norbert Warmuth
* \author Pavel Machek
* \date 1995, 1997, 1998
*
* \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:
* \verbatim
cd ftp//:pavel@hobit
cd ~
\endverbatim
* 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...
*
\verbatim
{
int f = !strcmp( remote_path, "/~" );
if (f || !strncmp( remote_path, "/~/", 3 )) {
char *s;
s = mc_build_filename ( qhome (*bucket), remote_path +3-f, (char *) NULL );
g_free (remote_path);
remote_path = s;
}
}
\endverbatim
*/
/* \todo Fix: Namespace pollution: horrible */
#include <config.h>
#include <stdio.h> /* sscanf() */
#include <stdlib.h> /* atoi() */
#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_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <arpa/ftp.h>
#include <arpa/telnet.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <inttypes.h> /* uintmax_t */
#include "lib/global.h"
#include "lib/file-entry.h"
#include "lib/util.h"
#include "lib/strutil.h" /* str_move() */
#include "lib/mcconfig.h"
#include "lib/tty/tty.h" /* enable/disable interrupt key */
#include "lib/widget.h" /* message() */
#include "src/history.h"
#include "src/setup.h" /* for load_anon_passwd */
#include "lib/vfs/vfs.h"
#include "lib/vfs/utilvfs.h"
#include "lib/vfs/netutil.h"
#include "lib/vfs/xdirentry.h"
#include "lib/vfs/gc.h" /* vfs_stamp_create */
#include "ftpfs.h"
/*** global variables ****************************************************************************/
/* Delay to retry a connection */
int ftpfs_retry_seconds = 30;
/* Method to use to connect to ftp sites */
gboolean ftpfs_use_passive_connections = TRUE;
gboolean ftpfs_use_passive_connections_over_proxy = FALSE;
/* 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
*/
gboolean ftpfs_use_unix_list_options = TRUE;
/* First "CWD <path>", then "LIST -la ." */
gboolean ftpfs_first_cd_then_ls = TRUE;
/* Use the ~/.netrc */
gboolean ftpfs_use_netrc = TRUE;
/* Anonymous setup */
char *ftpfs_anonymous_passwd = NULL;
int ftpfs_directory_timeout = 900;
/* Proxy host */
char *ftpfs_proxy_host = NULL;
/* whether we have to use proxy by default? */
gboolean ftpfs_always_use_proxy = FALSE;
gboolean ftpfs_ignore_chattr_errors = TRUE;
/*** file scope macro definitions ****************************************************************/
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#define FTP_SUPER(super) ((ftp_super_t *) (super))
#define FTP_FILE_HANDLER(fh) ((ftp_file_handler_t *) (fh))
#define FH_SOCK FTP_FILE_HANDLER(fh)->sock
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
#define RFC_AUTODETECT 0
#define RFC_DARING 1
#define RFC_STRICT 2
/* ftpfs_command wait_flag: */
#define NONE 0x00
#define WAIT_REPLY 0x01
#define WANT_STRING 0x02
#define FTP_COMMAND_PORT 21
/* some defines only used by ftpfs_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 ftpfs_changetype. */
#define TYPE_UNKNOWN -1
#define ABORT_TIMEOUT (5 * G_USEC_PER_SEC)
/*** file scope type declarations ****************************************************************/
#ifndef HAVE_SOCKLEN_T
typedef int socklen_t;
#endif
/* This should match the keywords[] array below */
typedef enum
{
NETRC_NONE = 0,
NETRC_DEFAULT,
NETRC_MACHINE,
NETRC_LOGIN,
NETRC_PASSWORD,
NETRC_PASSWD,
NETRC_ACCOUNT,
NETRC_MACDEF,
NETRC_UNKNOWN
} keyword_t;
typedef struct
{
struct vfs_s_super base; /* base class */
int sock;
char *proxy; /* proxy server, NULL if no proxy */
gboolean failed_on_login; /* used to pass the failure reason to upper levels */
gboolean use_passive_connection;
gboolean remote_is_amiga; /* No leading slash allowed for AmiTCP (Amiga) */
int isbinary;
gboolean cwd_deferred; /* current_directory was changed but CWD command hasn't
been sent yet */
int strict; /* ftp server doesn't understand
* "LIST -la <path>"; use "CWD <path>"/
* "LIST" instead
*/
gboolean ctl_connection_busy;
char *current_dir;
} ftp_super_t;
typedef struct
{
vfs_file_handler_t base; /* base class */
int sock;
gboolean append;
} ftp_file_handler_t;
/*** forward declarations (file scope functions) *************************************************/
static char *ftpfs_get_current_directory (struct vfs_class *me, struct vfs_s_super *super);
static int ftpfs_chdir_internal (struct vfs_class *me, struct vfs_s_super *super,
const char *remote_path);
static int ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super);
static gboolean ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super,
const char *netrcpass);
static gboolean ftpfs_netrc_lookup (const char *host, char **login, char **pass);
/*** file scope variables ************************************************************************/
static int code;
static char reply_str[80];
static struct vfs_s_subclass ftpfs_subclass;
static struct vfs_class *vfs_ftpfs_ops = VFS_CLASS (&ftpfs_subclass);
static GSList *no_proxy = NULL;
static char buffer[BUF_MEDIUM];
static char *netrc = NULL;
static const char *netrcp;
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
ftpfs_set_blksize (struct stat *s)
{
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
/* redefine block size */
s->st_blksize = 64 * 1024; /* FIXME */
#endif
}
/* --------------------------------------------------------------------------------------------- */
static struct stat *
ftpfs_default_stat (struct vfs_class *me)
{
struct stat *s;
s = vfs_s_default_stat (me, S_IFDIR | 0755);
ftpfs_set_blksize (s);
vfs_adjust_stat (s);
return s;
}
/* --------------------------------------------------------------------------------------------- */
/* 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
transferred 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 necessary
b) replace first occurrence of ":/" with ":"
c) strip trailing "/."
*/
static char *
ftpfs_translate_path (struct vfs_class *me, struct vfs_s_super *super, const char *remote_path)
{
char *ret, *p;
if (!FTP_SUPER (super)->remote_is_amiga)
return g_strdup (remote_path);
if (me->logfile != NULL)
{
fprintf (me->logfile, "MC -- ftpfs_translate_path: %s\n", remote_path);
fflush (me->logfile);
}
/* strip leading slash(es) */
while (IS_PATH_SEP (*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 occurrence of ":/" with ":" */
p = strchr (ret, ':');
if (p != NULL && IS_PATH_SEP (p[1]))
str_move (p + 1, p + 2);
/* strip trailing "/." */
p = strrchr (ret, PATH_SEP);
if ((p != NULL) && (*(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://miguel@sphinx.nuclecu.unam.mx/c/nc
* ftp://tsx-11.mit.edu:8192/
* ftp://joe@foo.edu:11321/private
* If the user is empty, e.g. ftp://@roxanne/private, then your login name
* is supplied.
*/
static vfs_path_element_t *
ftpfs_correct_url_parameters (const vfs_path_element_t *velement)
{
vfs_path_element_t *path_element = vfs_path_element_clone (velement);
if (path_element->port == 0)
path_element->port = FTP_COMMAND_PORT;
if (path_element->user == NULL)
{
/* Look up user and password in netrc */
if (ftpfs_use_netrc)
ftpfs_netrc_lookup (path_element->host, &path_element->user, &path_element->password);
}
if (path_element->user == NULL)
path_element->user = g_strdup ("anonymous");
/* Look up password in netrc for known user */
if (ftpfs_use_netrc && path_element->password == NULL)
{
char *new_user = NULL;
char *new_passwd = NULL;
ftpfs_netrc_lookup (path_element->host, &new_user, &new_passwd);
/* If user is different, remove password */
if (new_user != NULL && strcmp (path_element->user, new_user) != 0)
MC_PTR_FREE (path_element->password);
g_free (new_user);
g_free (new_passwd);
}
return path_element;
}
/* --------------------------------------------------------------------------------------------- */
/* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
static int
ftpfs_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
{
while (TRUE)
{
char answer[BUF_1K];
if (vfs_s_get_line (me, sock, answer, sizeof (answer), '\n') == 0)
{
if (string_buf != NULL)
*string_buf = '\0';
code = 421;
return 4;
}
/* cppcheck-suppress invalidscanf */
switch (sscanf (answer, "%d", &code))
{
case 0:
if (string_buf != NULL)
g_strlcpy (string_buf, answer, string_len);
code = 500;
return 5;
case 1:
if (answer[3] == '-')
{
while (TRUE)
{
int i;
if (vfs_s_get_line (me, sock, answer, sizeof (answer), '\n') == 0)
{
if (string_buf != NULL)
*string_buf = '\0';
code = 421;
return 4;
}
/* cppcheck-suppress invalidscanf */
if ((sscanf (answer, "%d", &i) > 0) && (code == i) && (answer[3] == ' '))
break;
}
}
if (string_buf != NULL)
g_strlcpy (string_buf, answer, string_len);
return code / 100;
default:
break;
}
}
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
ftpfs_reconnect (struct vfs_class *me, struct vfs_s_super *super)
{
ftp_super_t *ftp_super = FTP_SUPER (super);
int sock;
sock = ftpfs_open_socket (me, super);
if (sock != -1)
{
char *cwdir = ftp_super->current_dir;
close (ftp_super->sock);
ftp_super->sock = sock;
ftp_super->current_dir = NULL;
if (ftpfs_login_server (me, super, super->path_element->password))
{
if (cwdir == NULL)
return TRUE;
sock = ftpfs_chdir_internal (me, super, cwdir);
g_free (cwdir);
return (sock == COMPLETE);
}
ftp_super->current_dir = cwdir;
}
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
static int
G_GNUC_PRINTF (4, 5)
ftpfs_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt,
...)
{
ftp_super_t *ftp_super = FTP_SUPER (super);
va_list ap;
GString *cmdstr;
int status;
static gboolean retry = FALSE;
static int level = 0; /* ftpfs_login_server() use ftpfs_command() */
cmdstr = g_string_sized_new (32);
va_start (ap, fmt);
g_string_vprintf (cmdstr, fmt, ap);
va_end (ap);
g_string_append (cmdstr, "\r\n");
if (me->logfile != NULL)
{
if (strncmp (cmdstr->str, "PASS ", 5) == 0)
fputs ("PASS <Password not logged>\r\n", me->logfile);
else
{
size_t ret;
ret = fwrite (cmdstr->str, cmdstr->len, 1, me->logfile);
(void) ret;
}
fflush (me->logfile);
}
got_sigpipe = 0;
tty_enable_interrupt_key ();
status = write (ftp_super->sock, cmdstr->str, cmdstr->len);
if (status < 0)
{
code = 421;
if (errno == EPIPE)
{ /* Remote server has closed connection */
if (level == 0)
{
level = 1;
status = ftpfs_reconnect (me, super) ? 1 : 0;
level = 0;
if (status != 0 && (write (ftp_super->sock, cmdstr->str, cmdstr->len) > 0))
goto ok;
}
got_sigpipe = 1;
}
g_string_free (cmdstr, TRUE);
tty_disable_interrupt_key ();
return TRANSIENT;
}
retry = FALSE;
ok:
tty_disable_interrupt_key ();
if (wait_reply != NONE)
{
status = ftpfs_get_reply (me, ftp_super->sock,
(wait_reply & WANT_STRING) != 0 ? reply_str : NULL,
sizeof (reply_str) - 1);
if ((wait_reply & WANT_STRING) != 0 && !retry && level == 0 && code == 421)
{
retry = TRUE;
level = 1;
status = ftpfs_reconnect (me, super) ? 1 : 0;
level = 0;
if (status != 0 && (write (ftp_super->sock, cmdstr->str, cmdstr->len) > 0))
goto ok;
}
retry = FALSE;
g_string_free (cmdstr, TRUE);
return status;
}
g_string_free (cmdstr, TRUE);
return COMPLETE;
}
/* --------------------------------------------------------------------------------------------- */
static struct vfs_s_super *
ftpfs_new_archive (struct vfs_class *me)
{
ftp_super_t *arch;
arch = g_new0 (ftp_super_t, 1);
arch->base.me = me;
arch->base.name = g_strdup (PATH_SEP_STR);
arch->sock = -1;
arch->use_passive_connection = ftpfs_use_passive_connections;
arch->strict = ftpfs_use_unix_list_options ? RFC_AUTODETECT : RFC_STRICT;
arch->isbinary = TYPE_UNKNOWN;
return VFS_SUPER (arch);
}
/* --------------------------------------------------------------------------------------------- */
static void
ftpfs_free_archive (struct vfs_class *me, struct vfs_s_super *super)
{
ftp_super_t *ftp_super = FTP_SUPER (super);
if (ftp_super->sock != -1)
{
vfs_print_message (_("ftpfs: Disconnecting from %s"), super->path_element->host);
ftpfs_command (me, super, NONE, "%s", "QUIT");
close (ftp_super->sock);
}
g_free (ftp_super->current_dir);
}
/* --------------------------------------------------------------------------------------------- */
static int
ftpfs_changetype (struct vfs_class *me, struct vfs_s_super *super, int binary)
{
if (binary != FTP_SUPER (super)->isbinary)
{
if (ftpfs_command (me, super, WAIT_REPLY, "TYPE %c", binary ? 'I' : 'A') != COMPLETE)
ERRNOR (EIO, -1);
FTP_SUPER (super)->isbinary = binary;
}
return binary;
}
/* --------------------------------------------------------------------------------------------- */
/* This routine logs the user in */
static int
ftpfs_login_server (struct vfs_class *me, struct vfs_s_super *super, const char *netrcpass)
{
ftp_super_t *ftp_super = FTP_SUPER (super);
char *pass;
char *op;
char *name; /* login user name */
gboolean anon = FALSE;
char reply_string[BUF_MEDIUM];
ftp_super->isbinary = TYPE_UNKNOWN;
if (super->path_element->password != NULL) /* explicit password */
op = g_strdup (super->path_element->password);
else if (netrcpass != NULL) /* password from netrc */
op = g_strdup (netrcpass);
else if (strcmp (super->path_element->user, "anonymous") == 0
|| strcmp (super->path_element->user, "ftp") == 0)
{
if (ftpfs_anonymous_passwd == NULL) /* default anonymous password */
ftpfs_init_passwd ();
op = g_strdup (ftpfs_anonymous_passwd);
anon = TRUE;
}
else
{ /* ask user */
char *p;
p = g_strdup_printf (_("FTP: Password required for %s"), super->path_element->user);
op = vfs_get_password (p);
g_free (p);
if (op == NULL)
ERRNOR (EPERM, 0);
super->path_element->password = g_strdup (op);
}
if (!anon || me->logfile != NULL)
pass = op;
else
{
pass = g_strconcat ("-", op, (char *) NULL);
wipe_password (op);
}
/* Proxy server accepts: username@host-we-want-to-connect */
if (ftp_super->proxy != NULL)
name =
g_strconcat (super->path_element->user, "@",
super->path_element->host[0] ==
'!' ? super->path_element->host + 1 : super->path_element->host,
(char *) NULL);
else
name = g_strdup (super->path_element->user);
if (ftpfs_get_reply (me, ftp_super->sock, reply_string, sizeof (reply_string) - 1) == COMPLETE)
{
char *reply_up;
reply_up = g_ascii_strup (reply_string, -1);
ftp_super->remote_is_amiga = strstr (reply_up, "AMIGA") != NULL;
if (strstr (reply_up, " SPFTP/1.0.0000 SERVER ") != NULL) /* handles `LIST -la` in a weird way */
ftp_super->strict = RFC_STRICT;
g_free (reply_up);
if (me->logfile != NULL)
{
fprintf (me->logfile, "MC -- remote_is_amiga = %s\n",
ftp_super->remote_is_amiga ? "yes" : "no");
fflush (me->logfile);
}
vfs_print_message ("%s", _("ftpfs: sending login name"));
switch (ftpfs_command (me, super, WAIT_REPLY, "USER %s", name))
{
case CONTINUE:
vfs_print_message ("%s", _("ftpfs: sending user password"));
code = ftpfs_command (me, super, WAIT_REPLY, "PASS %s", pass);
if (code == CONTINUE)
{
char *p;
p = g_strdup_printf (_("FTP: Account required for user %s"),
super->path_element->user);
op = input_dialog (p, _("Account:"), MC_HISTORY_FTPFS_ACCOUNT, "",
INPUT_COMPLETE_USERNAMES);
g_free (p);
if (op == NULL)
ERRNOR (EPERM, 0);
vfs_print_message ("%s", _("ftpfs: sending user account"));
code = ftpfs_command (me, super, WAIT_REPLY, "ACCT %s", op);
g_free (op);
}
if (code != COMPLETE)
break;
MC_FALLTHROUGH;
case COMPLETE:
vfs_print_message ("%s", _("ftpfs: logged in"));
wipe_password (pass);
g_free (name);
return TRUE;
default:
ftp_super->failed_on_login = TRUE;
wipe_password (super->path_element->password);
super->path_element->password = NULL;
goto login_fail;
}
}
message (D_ERROR, MSG_ERROR, _("ftpfs: Login incorrect for user %s "),
super->path_element->user);
login_fail:
wipe_password (pass);
g_free (name);
ERRNOR (EPERM, FALSE);
}
/* --------------------------------------------------------------------------------------------- */
static void
ftpfs_load_no_proxy_list (void)
{
/* FixMe: shouldn't be hardcoded!!! */
char *mc_file;
mc_file = g_build_filename (mc_global.sysconfig_dir, "mc.no_proxy", (char *) NULL);
if (exist_file (mc_file))
{
FILE *npf;
npf = fopen (mc_file, "r");
if (npf != NULL)
{
char s[BUF_LARGE]; /* provide for BUF_LARGE characters */
while (fgets (s, sizeof (s), npf) != NULL)
{
char *p;
p = strchr (s, '\n');
if (p == NULL) /* skip bogus entries */
{
int c;
while ((c = fgetc (npf)) != EOF && c != '\n')
;
}
else if (p != s)
{
*p = '\0';
no_proxy = g_slist_prepend (no_proxy, g_strdup (s));
}
}
fclose (npf);
}
}
g_free (mc_file);
}
/* --------------------------------------------------------------------------------------------- */
/* Return TRUE if FTP proxy should be used for this host, FALSE otherwise */
static gboolean
ftpfs_check_proxy (const char *host)
{
if (ftpfs_proxy_host == NULL || *ftpfs_proxy_host == '\0' || host == NULL || *host == '\0')
return FALSE; /* sanity check */
if (*host == '!')
return TRUE;
if (!ftpfs_always_use_proxy)
return FALSE;
if (strchr (host, '.') == NULL)
return FALSE;
if (no_proxy == NULL)
{
GSList *npe;
ftpfs_load_no_proxy_list ();
for (npe = no_proxy; npe != NULL; npe = g_slist_next (npe))
{
const char *domain = (const char *) npe->data;
if (domain[0] == '.')
{
size_t ld, lh;
ld = strlen (domain);
lh = strlen (host);
while (ld != 0 && lh != 0 && host[lh - 1] == domain[ld - 1])
{
ld--;
lh--;
}
if (ld == 0)
return FALSE;
}
else if (g_ascii_strcasecmp (host, domain) == 0)
return FALSE;
}
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
static void
ftpfs_get_proxy_host_and_port (const char *proxy, char **host, int *port)
{
vfs_path_element_t *path_element;
path_element = vfs_url_split (proxy, FTP_COMMAND_PORT, URL_USE_ANONYMOUS);
*host = path_element->host;
path_element->host = NULL;
*port = path_element->port;
vfs_path_element_free (path_element);
}
/* --------------------------------------------------------------------------------------------- */
static int
ftpfs_open_socket (struct vfs_class *me, struct vfs_s_super *super)
{
struct addrinfo hints, *res, *curr_res;
int my_socket = 0;
char *host = NULL;
char port[8];
int tmp_port = 0;
int e;
(void) me;
if (super->path_element->host == NULL || *super->path_element->host == '\0')
{
vfs_print_message ("%s", _("ftpfs: Invalid host name."));
me->verrno = EINVAL;
return (-1);
}
/* Use a proxy host? */
/* Hosts to connect to that start with a ! should use proxy */
if (FTP_SUPER (super)->proxy != NULL)
ftpfs_get_proxy_host_and_port (ftpfs_proxy_host, &host, &tmp_port);
else
{
host = g_strdup (super->path_element->host);
tmp_port = super->path_element->port;
}
g_snprintf (port, sizeof (port), "%hu", (unsigned short) tmp_port);
tty_enable_interrupt_key (); /* clear the interrupt flag */
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
#ifdef AI_ADDRCONFIG
/* By default, only look up addresses using address types for
* which a local interface is configured (i.e. no IPv6 if no IPv6
* interfaces, likewise for IPv4 (see RFC 3493 for details). */
hints.ai_flags = AI_ADDRCONFIG;
#endif
e = getaddrinfo (host, port, &hints, &res);
#ifdef AI_ADDRCONFIG
if (e == EAI_BADFLAGS)
{
/* Retry with no flags if AI_ADDRCONFIG was rejected. */
hints.ai_flags = 0;
e = getaddrinfo (host, port, &hints, &res);
}
#endif
*port = '\0';
if (e != 0)
{
tty_disable_interrupt_key ();
vfs_print_message (_("ftpfs: %s"), gai_strerror (e));
g_free (host);
me->verrno = EINVAL;
return (-1);
}
for (curr_res = res; curr_res != NULL; curr_res = curr_res->ai_next)
{
my_socket = socket (curr_res->ai_family, curr_res->ai_socktype, curr_res->ai_protocol);
if (my_socket < 0)
{
if (curr_res->ai_next != NULL)
continue;
tty_disable_interrupt_key ();
vfs_print_message (_("ftpfs: %s"), unix_error_string (errno));
g_free (host);
freeaddrinfo (res);
me->verrno = errno;
return (-1);
}
vfs_print_message (_("ftpfs: making connection to %s"), host);
MC_PTR_FREE (host);
if (connect (my_socket, curr_res->ai_addr, curr_res->ai_addrlen) >= 0)
break;
me->verrno = errno;
close (my_socket);
if (errno == EINTR && tty_got_interrupt ())
vfs_print_message ("%s", _("ftpfs: connection interrupted by user"));
else if (res->ai_next == NULL)
vfs_print_message (_("ftpfs: connection to server failed: %s"),
unix_error_string (errno));
else
continue;
freeaddrinfo (res);
tty_disable_interrupt_key ();
return (-1);
}
freeaddrinfo (res);
tty_disable_interrupt_key ();
return my_socket;
}
/* --------------------------------------------------------------------------------------------- */
static int
ftpfs_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
{
ftp_super_t *ftp_super = FTP_SUPER (super);
int retry_seconds = 0;
/* We do not want to use the passive if we are using proxies */
if (ftp_super->proxy != NULL)
ftp_super->use_passive_connection = ftpfs_use_passive_connections_over_proxy;
do
{
ftp_super->failed_on_login = FALSE;
ftp_super->sock = ftpfs_open_socket (me, super);
if (ftp_super->sock == -1)
return (-1);
if (ftpfs_login_server (me, super, NULL))
{
/* Logged in, no need to retry the connection */
break;
}
if (!ftp_super->failed_on_login)
return (-1);
/* Close only the socket descriptor */
close (ftp_super->sock);
if (ftpfs_retry_seconds != 0)
{
int count_down;
retry_seconds = ftpfs_retry_seconds;
tty_enable_interrupt_key ();
for (count_down = retry_seconds; count_down != 0; count_down--)
{
vfs_print_message (_("Waiting to retry... %d (Control-G to cancel)"), count_down);
sleep (1);
if (tty_got_interrupt ())
{
/* me->verrno = E; */