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.
28 * $FreeBSD: src/lib/libfetch/ftp.c,v 1.102 2008/02/08 09:48:48 des Exp $
32 * Portions of this code were taken from or based on ftpio.c:
34 * ----------------------------------------------------------------------------
35 * "THE BEER-WARE LICENSE" (Revision 42):
36 * <phk@FreeBSD.org> wrote this file. As long as you retain this notice you
37 * can do whatever you want with this stuff. If we meet some day, and you think
38 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
39 * ----------------------------------------------------------------------------
43 * Dag-Erling Coïdan Smørgrav
46 * Incorporated into libfetch
51 * Turned inside out. Now returns xfers as new file ids, not as a special
54 * $ftpioId: ftpio.c,v 1.30 1998/04/11 07:28:53 phk Exp $
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
79 #define FTP_ANONYMOUS_USER "anonymous"
81 #define FTP_CONNECTION_ALREADY_OPEN 125
82 #define FTP_OPEN_DATA_CONNECTION 150
84 #define FTP_FILE_STATUS 213
85 #define FTP_SERVICE_READY 220
86 #define FTP_TRANSFER_COMPLETE 226
87 #define FTP_PASSIVE_MODE 227
88 #define FTP_LPASSIVE_MODE 228
89 #define FTP_EPASSIVE_MODE 229
90 #define FTP_LOGGED_IN 230
91 #define FTP_FILE_ACTION_OK 250
92 #define FTP_DIRECTORY_CREATED 257 /* multiple meanings */
93 #define FTP_FILE_CREATED 257 /* multiple meanings */
94 #define FTP_WORKING_DIRECTORY 257 /* multiple meanings */
95 #define FTP_NEED_PASSWORD 331
96 #define FTP_NEED_ACCOUNT 332
97 #define FTP_FILE_OK 350
98 #define FTP_SYNTAX_ERROR 500
99 #define FTP_PROTOCOL_ERROR 999
101 static int ftp_cmd(conn_t *, const char *, ...) __printflike(2, 3);
102 static struct url cached_host;
103 static conn_t *cached_connection;
105 #define isftpreply(foo) \
106 (isdigit((unsigned char)foo[0]) && \
107 isdigit((unsigned char)foo[1]) && \
108 isdigit((unsigned char)foo[2]) && \
109 (foo[3] == ' ' || foo[3] == '\0'))
110 #define isftpinfo(foo) \
111 (isdigit((unsigned char)foo[0]) && \
112 isdigit((unsigned char)foo[1]) && \
113 isdigit((unsigned char)foo[2]) && \
117 * Translate IPv4 mapped IPv6 address to IPv4 address
120 unmappedaddr(struct sockaddr_in6 *sin6)
122 struct sockaddr_in *sin4;
126 if (sin6->sin6_family != AF_INET6 ||
127 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
129 sin4 = (struct sockaddr_in *)sin6;
130 addr = *(u_int32_t *)&sin6->sin6_addr.s6_addr[12];
131 port = sin6->sin6_port;
132 memset(sin4, 0, sizeof(struct sockaddr_in));
133 sin4->sin_addr.s_addr = addr;
134 sin4->sin_port = port;
135 sin4->sin_family = AF_INET;
136 sin4->sin_len = sizeof(struct sockaddr_in);
140 * Get server response
143 ftp_chkerr(conn_t *conn)
145 if (fetch_getln(conn) == -1) {
149 if (isftpinfo(conn->buf)) {
150 while (conn->buflen && !isftpreply(conn->buf)) {
151 if (fetch_getln(conn) == -1) {
158 while (conn->buflen &&
159 isspace((unsigned char)conn->buf[conn->buflen - 1]))
161 conn->buf[conn->buflen] = '\0';
163 if (!isftpreply(conn->buf)) {
164 ftp_seterr(FTP_PROTOCOL_ERROR);
168 conn->err = (conn->buf[0] - '0') * 100
169 + (conn->buf[1] - '0') * 10
170 + (conn->buf[2] - '0');
176 * Send a command and check reply
179 ftp_cmd(conn_t *conn, const char *fmt, ...)
187 len = vasprintf(&msg, fmt, ap);
196 r = fetch_putln(conn, msg, len);
204 return (ftp_chkerr(conn));
208 * Return a pointer to the filename part of a path
211 ftp_filename(const char *file, int *len, int *type)
215 if ((s = strrchr(file, '/')) == NULL)
220 if (*len > 7 && strncmp(s + *len - 7, ";type=", 6) == 0) {
230 * Get current working directory from the reply to a CWD, PWD or CDUP
234 ftp_pwd(conn_t *conn, char *pwd, size_t pwdlen)
236 char *src, *dst, *end;
239 if (conn->err != FTP_WORKING_DIRECTORY &&
240 conn->err != FTP_FILE_ACTION_OK)
241 return (FTP_PROTOCOL_ERROR);
242 end = conn->buf + conn->buflen;
244 if (src >= end || *src++ != '"')
245 return (FTP_PROTOCOL_ERROR);
246 for (q = 0, dst = pwd; src < end && pwdlen--; ++src) {
247 if (!q && *src == '"')
249 else if (q && *src != '"')
257 return (FTP_PROTOCOL_ERROR);
260 DEBUG(fprintf(stderr, "pwd: [%s]\n", pwd));
266 * Change working directory to the directory that contains the specified
270 ftp_cwd(conn_t *conn, const char *file)
272 const char *beg, *end;
276 /* If no slashes in name, no need to change dirs. */
277 if ((end = strrchr(file, '/')) == NULL)
279 if ((e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
280 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
287 /* Look for a common prefix between PWD and dir to fetch. */
288 for (i = 0; i <= len && i <= end - file; ++i)
289 if (pwd[i] != file[i])
292 DEBUG(fprintf(stderr, "have: [%.*s|%s]\n", i, pwd, pwd + i));
293 DEBUG(fprintf(stderr, "want: [%.*s|%s]\n", i, file, file + i));
295 /* Keep going up a dir until we have a matching prefix. */
296 if (pwd[i] == '\0' && (file[i - 1] == '/' || file[i] == '/'))
298 if ((e = ftp_cmd(conn, "CDUP")) != FTP_FILE_ACTION_OK ||
299 (e = ftp_cmd(conn, "PWD")) != FTP_WORKING_DIRECTORY ||
300 (e = ftp_pwd(conn, pwd, sizeof(pwd))) != FTP_OK) {
306 #ifdef FTP_COMBINE_CWDS
307 /* Skip leading slashes, even "////". */
308 for (beg = file + i; beg < end && *beg == '/'; ++beg, ++i)
311 /* If there is no trailing dir, we're already there. */
315 /* Change to the directory all in one chunk (e.g., foo/bar/baz). */
316 e = ftp_cmd(conn, "CWD %.*s", (int)(end - beg), beg);
317 if (e == FTP_FILE_ACTION_OK)
319 #endif /* FTP_COMBINE_CWDS */
321 /* That didn't work so go back to legacy behavior (multiple CWDs). */
322 for (beg = file + i; beg < end; beg = file + i + 1) {
325 for (++i; file + i < end && file[i] != '/'; ++i)
327 e = ftp_cmd(conn, "CWD %.*s", (int)(file + i - beg), beg);
328 if (e != FTP_FILE_ACTION_OK) {
337 * Set transfer mode and data type
340 ftp_mode_type(conn_t *conn, int mode, int type)
351 return (FTP_PROTOCOL_ERROR);
353 if ((e = ftp_cmd(conn, "MODE %c", mode)) != FTP_OK) {
356 * Stream mode is supposed to be the default - so
357 * much so that some servers not only do not
358 * support any other mode, but do not support the
359 * MODE command at all.
361 * If "MODE S" fails, it is unlikely that we
362 * previously succeeded in setting a different
363 * mode. Therefore, we simply hope that the
364 * server is already in the correct mode, and
365 * silently ignore the failure.
385 /* can't handle yet */
387 return (FTP_PROTOCOL_ERROR);
389 if ((e = ftp_cmd(conn, "TYPE %c", type)) != FTP_OK)
396 * Request and parse file stats
399 ftp_stat(conn_t *conn, const char *file, struct url_stat *us)
402 const char *filename;
403 int filenamelen, type;
409 us->atime = us->mtime = 0;
411 filename = ftp_filename(file, &filenamelen, &type);
413 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK) {
418 e = ftp_cmd(conn, "SIZE %.*s", filenamelen, filename);
419 if (e != FTP_FILE_STATUS) {
423 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
425 for (us->size = 0; *ln && isdigit((unsigned char)*ln); ln++)
426 us->size = us->size * 10 + *ln - '0';
427 if (*ln && !isspace((unsigned char)*ln)) {
428 ftp_seterr(FTP_PROTOCOL_ERROR);
434 DEBUG(fprintf(stderr, "size: [%lld]\n", (long long)us->size));
436 e = ftp_cmd(conn, "MDTM %.*s", filenamelen, filename);
437 if (e != FTP_FILE_STATUS) {
441 for (ln = conn->buf + 4; *ln && isspace((unsigned char)*ln); ln++)
443 switch (strspn(ln, "0123456789")) {
452 ftp_seterr(FTP_PROTOCOL_ERROR);
455 if (sscanf(ln, "%04d%02d%02d%02d%02d%02d",
456 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
457 &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
458 ftp_seterr(FTP_PROTOCOL_ERROR);
469 DEBUG(fprintf(stderr,
470 "last modified: [%04d-%02d-%02d %02d:%02d:%02d]\n",
471 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
472 tm.tm_hour, tm.tm_min, tm.tm_sec));
477 * I/O functions for FTP
480 conn_t *cconn; /* Control connection */
481 conn_t *dconn; /* Data connection */
482 int dir; /* Direction */
483 int eof; /* EOF reached */
484 int err; /* Error code */
487 static int ftp_readfn(void *, char *, int);
488 static int ftp_writefn(void *, const char *, int);
489 static fpos_t ftp_seekfn(void *, fpos_t, int);
490 static int ftp_closefn(void *);
493 ftp_readfn(void *v, char *buf, int len)
498 io = (struct ftpio *)v;
503 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_WRONLY) {
513 r = fetch_read(io->dconn, buf, len);
526 ftp_writefn(void *v, const char *buf, int len)
531 io = (struct ftpio *)v;
536 if (io->cconn == NULL || io->dconn == NULL || io->dir == O_RDONLY) {
544 w = fetch_write(io->dconn, buf, len);
553 ftp_seekfn(void *v, fpos_t pos __unused, int whence __unused)
557 io = (struct ftpio *)v;
572 io = (struct ftpio *)v;
579 if (io->cconn == NULL || io->dconn == NULL) {
583 fetch_close(io->dconn);
586 DEBUG(fprintf(stderr, "Waiting for final status\n"));
587 r = ftp_chkerr(io->cconn);
588 if (io->cconn == cached_connection && io->cconn->ref == 1)
589 cached_connection = NULL;
590 fetch_close(io->cconn);
592 return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
596 ftp_setup(conn_t *cconn, conn_t *dconn, int mode)
601 if (cconn == NULL || dconn == NULL)
603 if ((io = malloc(sizeof(*io))) == NULL)
608 io->eof = io->err = 0;
609 f = funopen(io, ftp_readfn, ftp_writefn, ftp_seekfn, ftp_closefn);
619 ftp_transfer(conn_t *conn, const char *oper, const char *file,
620 int mode, off_t offset, const char *flags)
622 struct sockaddr_storage sa;
623 struct sockaddr_in6 *sin6;
624 struct sockaddr_in *sin4;
625 const char *bindaddr;
626 const char *filename;
627 int filenamelen, type;
628 int low, pasv, verbose;
635 low = CHECK_FLAG('l');
636 pasv = CHECK_FLAG('p');
637 verbose = CHECK_FLAG('v');
641 pasv = ((s = getenv("FTP_PASSIVE_MODE")) != NULL &&
642 strncasecmp(s, "no", 2) != 0);
644 /* isolate filename */
645 filename = ftp_filename(file, &filenamelen, &type);
647 /* set transfer mode and data type */
648 if ((e = ftp_mode_type(conn, 0, type)) != FTP_OK)
651 /* find our own address, bind, and listen */
653 if (getsockname(conn->sd, (struct sockaddr *)&sa, &l) == -1)
655 if (sa.ss_family == AF_INET6)
656 unmappedaddr((struct sockaddr_in6 *)&sa);
658 /* open data socket */
659 if ((sd = socket(sa.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) {
670 /* send PASV command */
672 fetch_info("setting passive mode");
673 switch (sa.ss_family) {
675 if ((e = ftp_cmd(conn, "PASV")) != FTP_PASSIVE_MODE)
679 if ((e = ftp_cmd(conn, "EPSV")) != FTP_EPASSIVE_MODE) {
682 if ((e = ftp_cmd(conn, "LPSV")) !=
688 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
693 * Find address and port number. The reply to the PASV command
694 * is IMHO the one and only weak point in the FTP protocol.
698 case FTP_PASSIVE_MODE:
699 case FTP_LPASSIVE_MODE:
700 for (p = ln + 3; *p && !isdigit((unsigned char)*p); p++)
703 e = FTP_PROTOCOL_ERROR;
706 l = (e == FTP_PASSIVE_MODE ? 6 : 21);
707 for (i = 0; *p && i < l; i++, p++)
708 addr[i] = strtol(p, &p, 10);
710 e = FTP_PROTOCOL_ERROR;
714 case FTP_EPASSIVE_MODE:
715 for (p = ln + 3; *p && *p != '('; p++)
718 e = FTP_PROTOCOL_ERROR;
722 if (sscanf(p, "%c%c%c%d%c", &addr[0], &addr[1], &addr[2],
723 &port, &addr[3]) != 5 ||
724 addr[0] != addr[1] ||
725 addr[0] != addr[2] || addr[0] != addr[3]) {
726 e = FTP_PROTOCOL_ERROR;
732 /* seek to required offset */
734 if (ftp_cmd(conn, "REST %lu", (u_long)offset) != FTP_FILE_OK)
737 /* construct sockaddr for data socket */
739 if (getpeername(conn->sd, (struct sockaddr *)&sa, &l) == -1)
741 if (sa.ss_family == AF_INET6)
742 unmappedaddr((struct sockaddr_in6 *)&sa);
743 switch (sa.ss_family) {
745 sin6 = (struct sockaddr_in6 *)&sa;
746 if (e == FTP_EPASSIVE_MODE)
747 sin6->sin6_port = htons(port);
749 memcpy(&sin6->sin6_addr, addr + 2, 16);
750 memcpy(&sin6->sin6_port, addr + 19, 2);
754 sin4 = (struct sockaddr_in *)&sa;
755 if (e == FTP_EPASSIVE_MODE)
756 sin4->sin_port = htons(port);
758 memcpy(&sin4->sin_addr, addr, 4);
759 memcpy(&sin4->sin_port, addr + 4, 2);
763 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
767 /* connect to data port */
769 fetch_info("opening data connection");
770 bindaddr = getenv("FETCH_BIND_ADDRESS");
771 if (bindaddr != NULL && *bindaddr != '\0' &&
772 fetch_bind(sd, sa.ss_family, bindaddr) != 0)
774 if (connect(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
777 /* make the server initiate the transfer */
779 fetch_info("initiating transfer");
780 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
781 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
789 char hname[INET6_ADDRSTRLEN];
791 switch (sa.ss_family) {
793 ((struct sockaddr_in6 *)&sa)->sin6_port = 0;
794 #ifdef IPV6_PORTRANGE
795 arg = low ? IPV6_PORTRANGE_DEFAULT : IPV6_PORTRANGE_HIGH;
796 if (setsockopt(sd, IPPROTO_IPV6, IPV6_PORTRANGE,
797 (char *)&arg, sizeof(arg)) == -1)
802 ((struct sockaddr_in *)&sa)->sin_port = 0;
803 arg = low ? IP_PORTRANGE_DEFAULT : IP_PORTRANGE_HIGH;
804 if (setsockopt(sd, IPPROTO_IP, IP_PORTRANGE,
805 (char *)&arg, sizeof(arg)) == -1)
810 fetch_info("binding data socket");
811 if (bind(sd, (struct sockaddr *)&sa, sa.ss_len) == -1)
813 if (listen(sd, 1) == -1)
816 /* find what port we're on and tell the server */
817 if (getsockname(sd, (struct sockaddr *)&sa, &l) == -1)
819 switch (sa.ss_family) {
821 sin4 = (struct sockaddr_in *)&sa;
822 a = ntohl(sin4->sin_addr.s_addr);
823 p = ntohs(sin4->sin_port);
824 e = ftp_cmd(conn, "PORT %d,%d,%d,%d,%d,%d",
825 (a >> 24) & 0xff, (a >> 16) & 0xff,
826 (a >> 8) & 0xff, a & 0xff,
827 (p >> 8) & 0xff, p & 0xff);
830 #define UC(b) (((int)b)&0xff)
832 sin6 = (struct sockaddr_in6 *)&sa;
833 sin6->sin6_scope_id = 0;
834 if (getnameinfo((struct sockaddr *)&sa, sa.ss_len,
835 hname, sizeof(hname),
836 NULL, 0, NI_NUMERICHOST) == 0) {
837 e = ftp_cmd(conn, "EPRT |%d|%s|%d|", 2, hname,
838 htons(sin6->sin6_port));
843 ap = (char *)&sin6->sin6_addr;
845 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
847 UC(ap[0]), UC(ap[1]), UC(ap[2]), UC(ap[3]),
848 UC(ap[4]), UC(ap[5]), UC(ap[6]), UC(ap[7]),
849 UC(ap[8]), UC(ap[9]), UC(ap[10]), UC(ap[11]),
850 UC(ap[12]), UC(ap[13]), UC(ap[14]), UC(ap[15]),
852 (ntohs(sin6->sin6_port) >> 8) & 0xff,
853 ntohs(sin6->sin6_port) & 0xff);
857 e = FTP_PROTOCOL_ERROR; /* XXX: error code should be prepared */
863 /* seek to required offset */
865 if (ftp_cmd(conn, "REST %ju", (uintmax_t)offset) != FTP_FILE_OK)
868 /* make the server initiate the transfer */
870 fetch_info("initiating transfer");
871 e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
872 if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
875 /* accept the incoming connection and go to town */
876 if ((d = accept(sd, NULL, NULL)) == -1)
882 if ((df = ftp_setup(conn, fetch_reopen(sd), mode)) == NULL)
904 ftp_authenticate(conn_t *conn, struct url *url, struct url *purl)
906 const char *user, *pwd, *logname;
907 char pbuf[MAXHOSTNAMELEN + MAXLOGNAME + 1];
910 /* XXX FTP_AUTH, and maybe .netrc */
912 /* send user name and password */
913 if (url->user[0] == '\0')
914 fetch_netrc_auth(url);
917 user = getenv("FTP_LOGIN");
918 if (user == NULL || *user == '\0')
919 user = FTP_ANONYMOUS_USER;
920 if (purl && url->port == fetch_default_port(url->scheme))
921 e = ftp_cmd(conn, "USER %s@%s", user, url->host);
923 e = ftp_cmd(conn, "USER %s@%s@%d", user, url->host, url->port);
925 e = ftp_cmd(conn, "USER %s", user);
927 /* did the server request a password? */
928 if (e == FTP_NEED_PASSWORD) {
931 pwd = getenv("FTP_PASSWORD");
932 if (pwd == NULL || *pwd == '\0') {
933 if ((logname = getlogin()) == NULL)
934 logname = FTP_ANONYMOUS_USER;
935 if ((len = snprintf(pbuf, MAXLOGNAME + 1, "%s@", logname)) < 0)
937 else if (len > MAXLOGNAME)
939 gethostname(pbuf + len, sizeof(pbuf) - len);
942 e = ftp_cmd(conn, "PASS %s", pwd);
949 * Log on to FTP server
952 ftp_connect(struct url *url, struct url *purl, const char *flags)
955 int e, direct, verbose;
962 direct = CHECK_FLAG('d');
963 verbose = CHECK_FLAG('v');
966 else if (CHECK_FLAG('6'))
972 /* check for proxy */
974 /* XXX proxy authentication! */
975 conn = fetch_connect(purl->host, purl->port, af, verbose);
977 /* no proxy, go straight to target */
978 conn = fetch_connect(url->host, url->port, af, verbose);
982 /* check connection */
984 /* fetch_connect() has already set an error code */
987 /* expect welcome message */
988 if ((e = ftp_chkerr(conn)) != FTP_SERVICE_READY)
992 if ((e = ftp_authenticate(conn, url, purl)) != FTP_LOGGED_IN)
995 /* TODO: Request extended features supported, if any (RFC 3659). */
1008 * Disconnect from server
1011 ftp_disconnect(conn_t *conn)
1013 (void)ftp_cmd(conn, "QUIT");
1014 if (conn == cached_connection && conn->ref == 1)
1015 cached_connection = NULL;
1020 * Check if we're already connected
1023 ftp_isconnected(struct url *url)
1025 return (cached_connection
1026 && (strcmp(url->host, cached_host.host) == 0)
1027 && (strcmp(url->user, cached_host.user) == 0)
1028 && (strcmp(url->pwd, cached_host.pwd) == 0)
1029 && (url->port == cached_host.port));
1033 * Check the cache, reconnect if no luck
1036 ftp_cached_connect(struct url *url, struct url *purl, const char *flags)
1041 /* set default port */
1043 url->port = fetch_default_port(url->scheme);
1045 /* try to use previously cached connection */
1046 if (ftp_isconnected(url)) {
1047 e = ftp_cmd(cached_connection, "NOOP");
1048 if (e == FTP_OK || e == FTP_SYNTAX_ERROR)
1049 return (fetch_ref(cached_connection));
1052 /* connect to server */
1053 if ((conn = ftp_connect(url, purl, flags)) == NULL)
1055 if (cached_connection)
1056 ftp_disconnect(cached_connection);
1057 cached_connection = fetch_ref(conn);
1058 memcpy(&cached_host, url, sizeof(*url));
1063 * Check the proxy settings
1066 ftp_get_proxy(struct url * url, const char *flags)
1071 if (flags != NULL && strchr(flags, 'd') != NULL)
1073 if (fetch_no_proxy_match(url->host))
1075 if (((p = getenv("FTP_PROXY")) || (p = getenv("ftp_proxy")) ||
1076 (p = getenv("HTTP_PROXY")) || (p = getenv("http_proxy"))) &&
1077 *p && (purl = fetchParseURL(p)) != NULL) {
1078 if (!*purl->scheme) {
1079 if (getenv("FTP_PROXY") || getenv("ftp_proxy"))
1080 strcpy(purl->scheme, SCHEME_FTP);
1082 strcpy(purl->scheme, SCHEME_HTTP);
1085 purl->port = fetch_default_proxy_port(purl->scheme);
1086 if (strcasecmp(purl->scheme, SCHEME_FTP) == 0 ||
1087 strcasecmp(purl->scheme, SCHEME_HTTP) == 0)
1095 * Process an FTP request
1098 ftp_request(struct url *url, const char *op, struct url_stat *us,
1099 struct url *purl, const char *flags)
1104 /* check if we should use HTTP instead */
1105 if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
1106 if (strcmp(op, "STAT") == 0)
1107 return (http_request(url, "HEAD", us, purl, flags));
1108 else if (strcmp(op, "RETR") == 0)
1109 return (http_request(url, "GET", us, purl, flags));
1111 * Our HTTP code doesn't support PUT requests yet, so try
1112 * a direct connection.
1116 /* connect to server */
1117 conn = ftp_cached_connect(url, purl, flags);
1123 /* change directory */
1124 if (ftp_cwd(conn, url->doc) == -1)
1128 if (us && ftp_stat(conn, url->doc, us) == -1
1129 && fetchLastErrCode != FETCH_PROTO
1130 && fetchLastErrCode != FETCH_UNAVAIL)
1134 if (strcmp(op, "STAT") == 0)
1135 return (FILE *)1; /* bogus return value */
1136 if (strcmp(op, "STOR") == 0 || strcmp(op, "APPE") == 0)
1141 /* initiate the transfer */
1142 return (ftp_transfer(conn, op, url->doc, oflag, url->offset, flags));
1149 fetchXGetFTP(struct url *url, struct url_stat *us, const char *flags)
1151 return (ftp_request(url, "RETR", us, ftp_get_proxy(url, flags), flags));
1158 fetchGetFTP(struct url *url, const char *flags)
1160 return (fetchXGetFTP(url, NULL, flags));
1167 fetchPutFTP(struct url *url, const char *flags)
1169 return (ftp_request(url, CHECK_FLAG('a') ? "APPE" : "STOR", NULL,
1170 ftp_get_proxy(url, flags), flags));
1177 fetchStatFTP(struct url *url, struct url_stat *us, const char *flags)
1181 f = ftp_request(url, "STAT", us, ftp_get_proxy(url, flags), flags);
1185 * When op is "STAT", ftp_request() will return either NULL or
1186 * (FILE *)1, never a valid FILE *, so we mustn't fclose(f) before
1187 * returning, as it would cause a segfault.
1196 fetchListFTP(struct url *url __unused, const char *flags __unused)
1198 warnx("fetchListFTP(): not implemented");