2 * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: src/lib/libfetch/ftp.c,v 1.91.2.2 2007/05/16 07:04:02 njl Exp $");
35 * Portions of this code were taken from or based on ftpio.c:
37 * ----------------------------------------------------------------------------
38 * "THE BEER-WARE LICENSE" (Revision 42):
39 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
40 * can do whatever you want with this stuff. If we meet some day, and you think
41 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
42 * ----------------------------------------------------------------------------
46 * Dag-Erling Coïdan Smørgrav
49 * Incorporated into libfetch
54 * Turned inside out. Now returns xfers as new file ids, not as a special
57 * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
61 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
82 #define FTP_ANONYMOUS_USER "anonymous"
84 #define FTP_CONNECTION_ALREADY_OPEN 125
85 #define FTP_OPEN_DATA_CONNECTION 150
87 #define FTP_FILE_STATUS 213
88 #define FTP_SERVICE_READY 220
89 #define FTP_TRANSFER_COMPLETE 226
90 #define FTP_PASSIVE_MODE 227
91 #define FTP_LPASSIVE_MODE 228
92 #define FTP_EPASSIVE_MODE 229
93 #define FTP_LOGGED_IN 230
94 #define FTP_FILE_ACTION_OK 250
95 #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */
96 #define FTP_FILE_CREATED 257 /* multiple meanings */
97 #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */
98 #define FTP_NEED_PASSWORD 331
99 #define FTP_NEED_ACCOUNT 332
100 #define FTP_FILE_OK 350
101 #define FTP_SYNTAX_ERROR 500
102 #define FTP_PROTOCOL_ERROR 999
104 static struct url cached_host
;
105 static conn_t
*cached_connection
;
107 #define isftpreply(foo) (isdigit((unsigned)foo[0]) && \
108 isdigit((unsigned)foo[1]) && \
109 isdigit((unsigned)foo[2]) && \
110 (foo[3] == ' ' || foo[3] == '\0'))
111 #define isftpinfo(foo) (isdigit((unsigned)foo[0]) && \
112 isdigit((unsigned)foo[1]) && \
113 isdigit((unsigned)foo[2]) && foo[3] == '-')
116 * Translate IPv4 mapped IPv6 address to IPv4 address
119 unmappedaddr(struct sockaddr_in6
*sin6
)
121 struct sockaddr_in
*sin4
;
125 if (sin6
->sin6_family
!= AF_INET6
||
126 !IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
128 sin4
= (struct sockaddr_in
*)(void *)sin6
;
129 addr
= *(u_int32_t
*)(void *)&sin6
->sin6_addr
.s6_addr
[12];
130 port
= sin6
->sin6_port
;
131 memset(sin4
, 0, sizeof(struct sockaddr_in
));
132 sin4
->sin_addr
.s_addr
= addr
;
133 sin4
->sin_port
= port
;
134 sin4
->sin_family
= AF_INET
;
135 sin4
->sin_len
= sizeof(struct sockaddr_in
);
139 * Get server response
142 _ftp_chkerr(conn_t
*conn
)
144 if (_fetch_getln(conn
) == -1) {
148 if (isftpinfo(conn
->buf
)) {
149 while (conn
->buflen
&& !isftpreply(conn
->buf
)) {
150 if (_fetch_getln(conn
) == -1) {
157 while (conn
->buflen
&& isspace((unsigned)conn
->buf
[conn
->buflen
- 1]))
159 conn
->buf
[conn
->buflen
] = '\0';
161 if (!isftpreply(conn
->buf
)) {
162 _ftp_seterr(FTP_PROTOCOL_ERROR
);
166 conn
->err
= (conn
->buf
[0] - '0') * 100
167 + (conn
->buf
[1] - '0') * 10
168 + (conn
->buf
[2] - '0');
174 * Send a command and check reply
177 _ftp_cmd(conn_t
*conn
, const char *fmt
, ...)
185 len
= vasprintf(&msg
, fmt
, ap
);
194 r
= _fetch_putln(conn
, msg
, len
);
202 return (_ftp_chkerr(conn
));
206 * Return a pointer to the filename part of a path
209 _ftp_filename(const char *file
, int *len
, int *type
)
213 if ((s
= strrchr(file
, '/')) == NULL
)
218 if (*len
> 7 && strncmp(s
+ *len
- 7, ";type=", 6) == 0) {
228 * Get current working directory from the reply to a CWD, PWD or CDUP
232 _ftp_pwd(conn_t
*conn
, char *pwd
, size_t pwdlen
)
234 char *src
, *dst
, *end
;
237 if (conn
->err
!= FTP_WORKING_DIRECTORY
&&
238 conn
->err
!= FTP_FILE_ACTION_OK
)
239 return (FTP_PROTOCOL_ERROR
);
240 end
= conn
->buf
+ conn
->buflen
;
242 if (src
>= end
|| *src
++ != '"')
243 return (FTP_PROTOCOL_ERROR
);
244 for (q
= 0, dst
= pwd
; src
< end
&& pwdlen
--; ++src
) {
245 if (!q
&& *src
== '"')
247 else if (q
&& *src
!= '"')
255 return (FTP_PROTOCOL_ERROR
);
258 DEBUG(fprintf(stderr
, "pwd: [%s]\n", pwd
));
264 * Change working directory to the directory that contains the specified
268 _ftp_cwd(conn_t
*conn
, const char *file
)
270 const char *beg
, *end
;
274 /* If no slashes in name, no need to change dirs. */
275 if ((end
= strrchr(file
, '/')) == NULL
)
277 if ((e
= _ftp_cmd(conn
, "PWD")) != FTP_WORKING_DIRECTORY
||
278 (e
= _ftp_pwd(conn
, pwd
, sizeof(pwd
))) != FTP_OK
) {
285 /* Look for a common prefix between PWD and dir to fetch. */
286 for (i
= 0; i
<= len
&& i
<= end
- file
; ++i
)
287 if (pwd
[i
] != file
[i
])
290 DEBUG(fprintf(stderr
, "have: [%.*s|%s]\n", i
, pwd
, pwd
+ i
));
291 DEBUG(fprintf(stderr
, "want: [%.*s|%s]\n", i
, file
, file
+ i
));
293 /* Keep going up a dir until we have a matching prefix. */
294 if (pwd
[i
] == '\0' && (file
[i
- 1] == '/' || file
[i
] == '/'))
296 if ((e
= _ftp_cmd(conn
, "CDUP")) != FTP_FILE_ACTION_OK
||
297 (e
= _ftp_cmd(conn
, "PWD")) != FTP_WORKING_DIRECTORY
||
298 (e
= _ftp_pwd(conn
, pwd
, sizeof(pwd
))) != FTP_OK
) {
304 #ifdef FTP_COMBINE_CWDS
305 /* Skip leading slashes, even "////". */
306 for (beg
= file
+ i
; beg
< end
&& *beg
== '/'; ++beg
, ++i
)
309 /* If there is no trailing dir, we're already there. */
313 /* Change to the directory all in one chunk (e.g., foo/bar/baz). */
314 e
= _ftp_cmd(conn
, "CWD %.*s", (int)(end
- beg
), beg
);
315 if (e
== FTP_FILE_ACTION_OK
)
317 #endif /* FTP_COMBINE_CWDS */
319 /* That didn't work so go back to legacy behavior (multiple CWDs). */
320 for (beg
= file
+ i
; beg
< end
; beg
= file
+ i
+ 1) {
323 for (++i
; file
+ i
< end
&& file
[i
] != '/'; ++i
)
325 e
= _ftp_cmd(conn
, "CWD %.*s", file
+ i
- beg
, beg
);
326 if (e
!= FTP_FILE_ACTION_OK
) {
335 * Set transfer mode and data type
338 _ftp_mode_type(conn_t
*conn
, int mode
, int type
)
350 return (FTP_PROTOCOL_ERROR
);
352 if ((e
= _ftp_cmd(conn
, "MODE %c", mode
)) != FTP_OK
) {
355 * Stream mode is supposed to be the default - so
356 * much so that some servers not only do not
357 * support any other mode, but do not support the
358 * MODE command at all.
360 * If "MODE S" fails, it is unlikely that we
361 * previously succeeded in setting a different
362 * mode. Therefore, we simply hope that the
363 * server is already in the correct mode, and
364 * silently ignore the failure.
387 /* can't handle yet */
389 return (FTP_PROTOCOL_ERROR
);
391 if ((e
= _ftp_cmd(conn
, "TYPE %c", type
)) != FTP_OK
)
398 * Request and parse file stats
401 _ftp_stat(conn_t
*conn
, const char *file
, struct url_stat
*us
)
404 const char *filename
;
405 int filenamelen
, type
;
411 us
->atime
= us
->mtime
= 0;
413 filename
= _ftp_filename(file
, &filenamelen
, &type
);
415 if ((e
= _ftp_mode_type(conn
, 0, type
)) != FTP_OK
) {
420 e
= _ftp_cmd(conn
, "SIZE %.*s", filenamelen
, filename
);
421 if (e
!= FTP_FILE_STATUS
) {
425 for (ln
= conn
->buf
+ 4; *ln
&& isspace((unsigned)*ln
); ln
++)
427 for (us
->size
= 0; *ln
&& isdigit((unsigned)*ln
); ln
++)
428 us
->size
= us
->size
* 10 + *ln
- '0';
429 if (*ln
&& !isspace((unsigned)*ln
)) {
430 _ftp_seterr(FTP_PROTOCOL_ERROR
);
436 DEBUG(fprintf(stderr
, "size: [%lld]\n", (long long)us
->size
));
438 e
= _ftp_cmd(conn
, "MDTM %.*s", filenamelen
, filename
);
439 if (e
!= FTP_FILE_STATUS
) {
443 for (ln
= conn
->buf
+ 4; *ln
&& isspace((unsigned)*ln
); ln
++)
445 switch (strspn(ln
, "0123456789")) {
454 _ftp_seterr(FTP_PROTOCOL_ERROR
);
457 if (sscanf(ln
, "%04d%02d%02d%02d%02d%02d",
458 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
,
459 &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) != 6) {
460 _ftp_seterr(FTP_PROTOCOL_ERROR
);
471 DEBUG(fprintf(stderr
,
472 "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
473 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
474 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
));
479 * I/O functions for FTP
482 conn_t
*cconn
; /* Control connection */
483 conn_t
*dconn
; /* Data connection */
484 int dir
; /* Direction */
485 int eof
; /* EOF reached */
486 int err
; /* Error code */
489 static int _ftp_readfn(void *, char *, int);
490 static int _ftp_writefn(void *, const char *, int);
491 static fpos_t _ftp_seekfn(void *, fpos_t, int);
492 static int _ftp_closefn(void *);
495 _ftp_readfn(void *v
, char *buf
, int len
)
500 io
= (struct ftpio
*)v
;
505 if (io
->cconn
== NULL
|| io
->dconn
== NULL
|| io
->dir
== O_WRONLY
) {
515 r
= _fetch_read(io
->dconn
, buf
, (unsigned)len
);
528 _ftp_writefn(void *v
, const char *buf
, int len
)
533 io
= (struct ftpio
*)v
;
538 if (io
->cconn
== NULL
|| io
->dconn
== NULL
|| io
->dir
== O_RDONLY
) {
546 w
= _fetch_write(io
->dconn
, buf
, (unsigned)len
);
556 _ftp_seekfn(void *v
, fpos_t pos __unused
, int whence __unused
)
560 io
= (struct ftpio
*)v
;
570 _ftp_closefn(void *v
)
575 io
= (struct ftpio
*)v
;
582 if (io
->cconn
== NULL
|| io
->dconn
== NULL
) {
586 _fetch_close(io
->dconn
);
589 DEBUG(fprintf(stderr
, "Waiting for final status\n"));
590 r
= _ftp_chkerr(io
->cconn
);
591 if (io
->cconn
== cached_connection
&& io
->cconn
->ref
== 1)
592 cached_connection
= NULL
;
593 _fetch_close(io
->cconn
);
595 return (r
== FTP_TRANSFER_COMPLETE
) ? 0 : -1;
599 _ftp_setup(conn_t
*cconn
, conn_t
*dconn
, int mode
)
604 if (cconn
== NULL
|| dconn
== NULL
)
606 if ((io
= malloc(sizeof(*io
))) == NULL
)
611 io
->eof
= io
->err
= 0;
612 f
= funopen(io
, _ftp_readfn
, _ftp_writefn
, _ftp_seekfn
, _ftp_closefn
);
622 _ftp_transfer(conn_t
*conn
, const char *oper
, const char *file
,
623 int mode
, off_t offset
, const char *flags
)
625 struct sockaddr_storage sa
;
626 struct sockaddr_in6
*sin6
;
627 struct sockaddr_in
*sin4
;
628 const char *bindaddr
;
629 const char *filename
;
630 int filenamelen
, type
;
631 int low
, pasv
, verbose
;
638 low
= CHECK_FLAG('l');
639 pasv
= CHECK_FLAG('p');
640 verbose
= CHECK_FLAG('v');
644 pasv
= ((s
= getenv("FTP_PASSIVE_MODE")) != NULL
&&
645 strncasecmp(s
, "no", 2) != 0);
647 /* isolate filename */
648 filename
= _ftp_filename(file
, &filenamelen
, &type
);
650 /* set transfer mode and data type */
651 if ((e
= _ftp_mode_type(conn
, 0, type
)) != FTP_OK
)
654 /* find our own address, bind, and listen */
656 if (getsockname(conn
->sd
, (struct sockaddr
*)(void *)&sa
, &l
) == -1)
658 if (sa
.ss_family
== AF_INET6
)
659 unmappedaddr((struct sockaddr_in6
*)(void *)&sa
);
661 /* open data socket */
662 if ((sd
= socket(sa
.ss_family
, SOCK_STREAM
, IPPROTO_TCP
)) == -1) {
673 /* send PASV command */
675 _fetch_info("setting passive mode");
676 switch (sa
.ss_family
) {
678 if ((e
= _ftp_cmd(conn
, "PASV")) != FTP_PASSIVE_MODE
)
682 if ((e
= _ftp_cmd(conn
, "EPSV")) != FTP_EPASSIVE_MODE
) {
685 if ((e
= _ftp_cmd(conn
, "LPSV")) !=
691 e
= FTP_PROTOCOL_ERROR
; /* XXX: error code should be prepared */
696 * Find address and port number. The reply to the PASV command
697 * is IMHO the one and only weak point in the FTP protocol.
701 case FTP_PASSIVE_MODE
:
702 case FTP_LPASSIVE_MODE
:
703 for (p
= ln
+ 3; *p
&& !isdigit((unsigned)*p
); p
++)
706 e
= FTP_PROTOCOL_ERROR
;
709 l
= (e
== FTP_PASSIVE_MODE
? 6 : 21);
710 for (i
= 0; *p
&& i
< l
; i
++, p
++)
711 addr
[i
] = (unsigned char) strtol(p
, &p
, 10);
713 e
= FTP_PROTOCOL_ERROR
;
717 case FTP_EPASSIVE_MODE
:
718 for (p
= ln
+ 3; *p
&& *p
!= '('; p
++)
721 e
= FTP_PROTOCOL_ERROR
;
725 if (sscanf(p
, "%c%c%c%d%c", &addr
[0], &addr
[1], &addr
[2],
726 &port
, &addr
[3]) != 5 ||
727 addr
[0] != addr
[1] ||
728 addr
[0] != addr
[2] || addr
[0] != addr
[3]) {
729 e
= FTP_PROTOCOL_ERROR
;
735 /* seek to required offset */
737 if (_ftp_cmd(conn
, "REST %lu", (u_long
)offset
) != FTP_FILE_OK
)
740 /* construct sockaddr for data socket */
742 if (getpeername(conn
->sd
, (struct sockaddr
*)(void *)&sa
, &l
) == -1)
744 if (sa
.ss_family
== AF_INET6
)
745 unmappedaddr((struct sockaddr_in6
*)(void *)&sa
);
746 switch (sa
.ss_family
) {
748 sin6
= (struct sockaddr_in6
*)(void *)&sa
;
749 if (e
== FTP_EPASSIVE_MODE
)
750 sin6
->sin6_port
= htons(port
);
752 bcopy(addr
+ 2, (char *)(void *)&sin6
->sin6_addr
, 16);
753 bcopy(addr
+ 19, (char *)(void *)&sin6
->sin6_port
, 2);
757 sin4
= (struct sockaddr_in
*)(void *)&sa
;
758 if (e
== FTP_EPASSIVE_MODE
)
759 sin4
->sin_port
= htons(port
);
761 bcopy(addr
, (char *)(void *)&sin4
->sin_addr
, 4);
762 bcopy(addr
+ 4, (char *)(void *)&sin4
->sin_port
, 2);
766 e
= FTP_PROTOCOL_ERROR
; /* XXX: error code should be prepared */
770 /* connect to data port */
772 _fetch_info("opening data connection");
773 bindaddr
= getenv("FETCH_BIND_ADDRESS");
774 if (bindaddr
!= NULL
&& *bindaddr
!= '\0' &&
775 _fetch_bind(sd
, sa
.ss_family
, bindaddr
) != 0)
777 if (connect(sd
, (struct sockaddr
*)(void *)&sa
, (unsigned)sa
.ss_len
) == -1)
780 /* make the server initiate the transfer */
782 _fetch_info("initiating transfer");
783 e
= _ftp_cmd(conn
, "%s %.*s", oper
, filenamelen
, filename
);
784 if (e
!= FTP_CONNECTION_ALREADY_OPEN
&& e
!= FTP_OPEN_DATA_CONNECTION
)
792 char hname
[INET6_ADDRSTRLEN
];
794 switch (sa
.ss_family
) {
796 ((struct sockaddr_in6
*)(void *)&sa
)->sin6_port
= 0;
797 #ifdef IPV6_PORTRANGE
798 arg
= low
? IPV6_PORTRANGE_DEFAULT
: IPV6_PORTRANGE_HIGH
;
799 if (setsockopt(sd
, IPPROTO_IPV6
, IPV6_PORTRANGE
,
800 (char *)(void *)&arg
, sizeof(arg
)) == -1)
805 ((struct sockaddr_in
*)(void *)&sa
)->sin_port
= 0;
806 arg
= low
? IP_PORTRANGE_DEFAULT
: IP_PORTRANGE_HIGH
;
807 if (setsockopt(sd
, IPPROTO_IP
, IP_PORTRANGE
,
808 (char *)(void *)&arg
, sizeof(arg
)) == -1)
813 _fetch_info("binding data socket");
814 if (bind(sd
, (struct sockaddr
*)(void *)&sa
, (unsigned) sa
.ss_len
) == -1)
816 if (listen(sd
, 1) == -1)
819 /* find what port we're on and tell the server */
820 if (getsockname(sd
, (struct sockaddr
*)(void *)&sa
, &l
) == -1)
822 switch (sa
.ss_family
) {
824 sin4
= (struct sockaddr_in
*)(void *)&sa
;
825 a
= ntohl(sin4
->sin_addr
.s_addr
);
826 p
= ntohs(sin4
->sin_port
);
827 e
= _ftp_cmd(conn
, "PORT %d,%d,%d,%d,%d,%d",
828 (a
>> 24) & 0xff, (a
>> 16) & 0xff,
829 (a
>> 8) & 0xff, a
& 0xff,
830 ((unsigned)p
>> 8) & 0xff, p
& 0xff);
833 #define UC(b) (((int)b)&0xff)
835 sin6
= (struct sockaddr_in6
*)(void *)&sa
;
836 sin6
->sin6_scope_id
= 0;
837 if (getnameinfo((struct sockaddr
*)(void *)&sa
,
839 hname
, sizeof(hname
),
840 NULL
, 0, NI_NUMERICHOST
) == 0) {
841 e
= _ftp_cmd(conn
, "EPRT |%d|%s|%d|", 2, hname
,
842 htons(sin6
->sin6_port
));
847 ap
= (char *)(void *)&sin6
->sin6_addr
;
849 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
851 UC(ap
[0]), UC(ap
[1]), UC(ap
[2]), UC(ap
[3]),
852 UC(ap
[4]), UC(ap
[5]), UC(ap
[6]), UC(ap
[7]),
853 UC(ap
[8]), UC(ap
[9]), UC(ap
[10]), UC(ap
[11]),
854 UC(ap
[12]), UC(ap
[13]), UC(ap
[14]), UC(ap
[15]),
856 ((unsigned)ntohs(sin6
->sin6_port
) >> 8) & 0xff,
857 ntohs(sin6
->sin6_port
) & 0xff);
861 e
= FTP_PROTOCOL_ERROR
; /* XXX: error code should be prepared */
867 /* seek to required offset */
869 if (_ftp_cmd(conn
, "REST %ju", (uintmax_t)offset
) != FTP_FILE_OK
)
872 /* make the server initiate the transfer */
874 _fetch_info("initiating transfer");
875 e
= _ftp_cmd(conn
, "%s %.*s", oper
, filenamelen
, filename
);
876 if (e
!= FTP_CONNECTION_ALREADY_OPEN
&& e
!= FTP_OPEN_DATA_CONNECTION
)
879 /* accept the incoming connection and go to town */
880 if ((d
= accept(sd
, NULL
, NULL
)) == -1)
886 if ((df
= _ftp_setup(conn
, _fetch_reopen(sd
), mode
)) == NULL
)
908 _ftp_authenticate(conn_t
*conn
, struct url
*url
, struct url
*purl
)
910 const char *user
, *pwd
, *logname
;
911 char pbuf
[MAXHOSTNAMELEN
+ MAXLOGNAME
+ 1];
914 /* XXX FTP_AUTH, and maybe .netrc */
916 /* send user name and password */
917 if (url
->user
[0] == '\0')
918 _fetch_netrc_auth(url
);
921 user
= getenv("FTP_LOGIN");
922 if (user
== NULL
|| *user
== '\0')
923 user
= FTP_ANONYMOUS_USER
;
924 if (purl
&& url
->port
== _fetch_default_port(url
->scheme
))
925 e
= _ftp_cmd(conn
, "USER %s@%s", user
, url
->host
);
927 e
= _ftp_cmd(conn
, "USER %s@%s@%d", user
, url
->host
, url
->port
);
929 e
= _ftp_cmd(conn
, "USER %s", user
);
931 /* did the server request a password? */
932 if (e
== FTP_NEED_PASSWORD
) {
935 pwd
= getenv("FTP_PASSWORD");
936 if (pwd
== NULL
|| *pwd
== '\0') {
937 if ((logname
= getlogin()) == 0)
938 logname
= FTP_ANONYMOUS_USER
;
939 if ((len
= snprintf(pbuf
, MAXLOGNAME
+ 1, "%s@", logname
)) < 0)
941 else if (len
> MAXLOGNAME
)
943 gethostname(pbuf
+ len
, sizeof(pbuf
) - len
);
946 e
= _ftp_cmd(conn
, "PASS %s", pwd
);
953 * Log on to FTP server
956 _ftp_connect(struct url
*url
, struct url
*purl
, const char *flags
)
959 int e
, direct
, verbose
;
966 direct
= CHECK_FLAG('d');
967 verbose
= CHECK_FLAG('v');
970 else if (CHECK_FLAG('6'))
976 /* check for proxy */
978 /* XXX proxy authentication! */
979 conn
= _fetch_connect(purl
->host
, purl
->port
, af
, verbose
);
981 /* no proxy, go straight to target */
982 conn
= _fetch_connect(url
->host
, url
->port
, af
, verbose
);
986 /* check connection */
988 /* _fetch_connect() has already set an error code */
991 /* expect welcome message */
992 if ((e
= _ftp_chkerr(conn
)) != FTP_SERVICE_READY
)
996 if ((e
= _ftp_authenticate(conn
, url
, purl
)) != FTP_LOGGED_IN
)
999 /* TODO: Request extended features supported, if any (RFC 3659). */
1012 * Disconnect from server
1015 _ftp_disconnect(conn_t
*conn
)
1017 (void)_ftp_cmd(conn
, "QUIT");
1018 if (conn
== cached_connection
&& conn
->ref
== 1)
1019 cached_connection
= NULL
;
1024 * Check if we're already connected
1027 _ftp_isconnected(struct url
*url
)
1029 return (cached_connection
1030 && (strcmp(url
->host
, cached_host
.host
) == 0)
1031 && (strcmp(url
->user
, cached_host
.user
) == 0)
1032 && (strcmp(url
->pwd
, cached_host
.pwd
) == 0)
1033 && (url
->port
== cached_host
.port
));
1037 * Check the cache, reconnect if no luck
1040 _ftp_cached_connect(struct url
*url
, struct url
*purl
, const char *flags
)
1045 /* set default port */
1047 url
->port
= _fetch_default_port(url
->scheme
);
1049 /* try to use previously cached connection */
1050 if (_ftp_isconnected(url
)) {
1051 e
= _ftp_cmd(cached_connection
, "NOOP");
1052 if (e
== FTP_OK
|| e
== FTP_SYNTAX_ERROR
)
1053 return (_fetch_ref(cached_connection
));
1056 /* connect to server */
1057 if ((conn
= _ftp_connect(url
, purl
, flags
)) == NULL
)
1059 if (cached_connection
)
1060 _ftp_disconnect(cached_connection
);
1061 cached_connection
= _fetch_ref(conn
);
1062 memcpy(&cached_host
, url
, sizeof(*url
));
1067 * Check the proxy settings
1070 _ftp_get_proxy(const char *flags
)
1075 if (flags
!= NULL
&& strchr(flags
, 'd') != NULL
)
1077 if (((p
= getenv("FTP_PROXY")) || (p
= getenv("ftp_proxy")) ||
1078 (p
= getenv("HTTP_PROXY")) || (p
= getenv("http_proxy"))) &&
1079 *p
&& (purl
= fetchParseURL(p
)) != NULL
) {
1080 if (!*purl
->scheme
) {
1081 if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1082 strcpy(purl
->scheme
, SCHEME_FTP
);
1084 strcpy(purl
->scheme
, SCHEME_HTTP
);
1087 purl
->port
= _fetch_default_proxy_port(purl
->scheme
);
1088 if (strcasecmp(purl
->scheme
, SCHEME_FTP
) == 0 ||
1089 strcasecmp(purl
->scheme
, SCHEME_HTTP
) == 0)
1097 * Process an FTP request
1100 _ftp_request(struct url
*url
, const char *op
, struct url_stat
*us
,
1101 struct url
*purl
, const char *flags
)
1106 /* check if we should use HTTP instead */
1107 if (purl
&& strcasecmp(purl
->scheme
, SCHEME_HTTP
) == 0) {
1108 if (strcmp(op
, "STAT") == 0)
1109 return (_http_request(url
, "HEAD", us
, purl
, flags
));
1110 else if (strcmp(op
, "RETR") == 0)
1111 return (_http_request(url
, "GET", us
, purl
, flags
));
1113 * Our HTTP code doesn't support PUT requests yet, so try
1114 * a direct connection.
1118 /* connect to server */
1119 conn
= _ftp_cached_connect(url
, purl
, flags
);
1125 /* change directory */
1126 if (_ftp_cwd(conn
, url
->doc
) == -1)
1130 if (us
&& _ftp_stat(conn
, url
->doc
, us
) == -1
1131 && fetchLastErrCode
!= FETCH_PROTO
1132 && fetchLastErrCode
!= FETCH_UNAVAIL
)
1136 if (strcmp(op
, "STAT") == 0)
1137 return (FILE *)1; /* bogus return value */
1138 if (strcmp(op
, "STOR") == 0 || strcmp(op
, "APPE") == 0)
1143 /* initiate the transfer */
1144 return (_ftp_transfer(conn
, op
, url
->doc
, oflag
, url
->offset
, flags
));
1151 fetchXGetFTP(struct url
*url
, struct url_stat
*us
, const char *flags
)
1153 return (_ftp_request(url
, "RETR", us
, _ftp_get_proxy(flags
), flags
));
1160 fetchGetFTP(struct url
*url
, const char *flags
)
1162 return (fetchXGetFTP(url
, NULL
, flags
));
1169 fetchPutFTP(struct url
*url
, const char *flags
)
1172 return (_ftp_request(url
, CHECK_FLAG('a') ? "APPE" : "STOR", NULL
,
1173 _ftp_get_proxy(flags
), flags
));
1180 fetchStatFTP(struct url
*url
, struct url_stat
*us
, const char *flags
)
1184 f
= _ftp_request(url
, "STAT", us
, _ftp_get_proxy(flags
), flags
);
1195 fetchListFTP(struct url
*url __unused
, const char *flags __unused
)
1197 warnx("fetchListFTP(): not implemented");