2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
48 char ftp_last_respline[128];
51 /* Get the response of FTP server and allocate enough room to handle
52 it. <CR> and <LF> characters are stripped from the line, and the
53 line is 0-terminated. All the response lines but the last one are
54 skipped. The last line is determined as described in RFC959.
56 If the line is successfully read, FTPOK is returned, and *ret_line
57 is assigned a freshly allocated line. Otherwise, FTPRERR is
58 returned, and the value of *ret_line should be ignored. */
61 ftp_response (int fd, char **ret_line)
66 char *line = fd_read_line (fd);
70 /* Strip trailing CRLF before printing the line, so that
71 quotting doesn't include bogus \012 and \015. */
72 p = strchr (line, '\0');
73 if (p > line && p[-1] == '\n')
75 if (p > line && p[-1] == '\r')
78 if (opt.server_response)
79 logprintf (LOG_NOTQUIET, "%s\n",
80 quotearg_style (escape_quoting_style, line));
82 DEBUGP (("%s\n", quotearg_style (escape_quoting_style, line)));
84 /* The last line of output is the one that begins with "ddd ". */
85 if (c_isdigit (line[0]) && c_isdigit (line[1]) && c_isdigit (line[2])
88 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
89 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
97 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
98 it if printing is required. If VALUE is NULL, just use
101 ftp_request (const char *command, const char *value)
106 /* Check for newlines in VALUE (possibly injected by the %0A URL
107 escape) making the callers inadvertently send multiple FTP
108 commands at once. Without this check an attacker could
109 intentionally redirect to ftp://server/fakedir%0Acommand.../
110 and execute arbitrary FTP command on a remote FTP server. */
111 if (strpbrk (value, "\r\n"))
113 /* Copy VALUE to the stack and modify CR/LF to space. */
115 STRDUP_ALLOCA (defanged, value);
116 for (p = defanged; *p; p++)
117 if (*p == '\r' || *p == '\n')
119 DEBUGP (("\nDetected newlines in %s \"%s\"; changing to %s \"%s\"\n",
120 command, quotearg_style (escape_quoting_style, value),
121 command, quotearg_style (escape_quoting_style, defanged)));
122 /* Make VALUE point to the defanged copy of the string. */
125 res = concat_strings (command, " ", value, "\r\n", (char *) 0);
128 res = concat_strings (command, "\r\n", (char *) 0);
129 if (opt.server_response)
131 /* Hack: don't print out password. */
132 if (strncmp (res, "PASS", 4) != 0)
133 logprintf (LOG_ALWAYS, "--> %s\n", res);
135 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
138 DEBUGP (("\n--> %s\n", res));
142 /* Sends the USER and PASS commands to the server, to control
143 connection socket csock. */
145 ftp_login (int csock, const char *acc, const char *pass)
148 char *request, *respline;
152 err = ftp_response (csock, &respline);
155 if (*respline != '2')
161 /* Send USER username. */
162 request = ftp_request ("USER", acc);
163 nwritten = fd_write (csock, request, strlen (request), -1);
170 /* Get appropriate response. */
171 err = ftp_response (csock, &respline);
174 /* An unprobable possibility of logging without a password. */
175 if (*respline == '2')
180 /* Else, only response 3 is appropriate. */
181 if (*respline != '3')
184 return FTPLOGREFUSED;
188 static const char *skey_head[] = {
193 const char *seed = NULL;
195 for (i = 0; i < countof (skey_head); i++)
197 int l = strlen (skey_head[i]);
198 if (0 == strncasecmp (skey_head[i], respline, l))
206 int skey_sequence = 0;
208 /* Extract the sequence from SEED. */
209 for (; c_isdigit (*seed); seed++)
210 skey_sequence = 10 * skey_sequence + *seed - '0';
216 return FTPLOGREFUSED;
218 /* Replace the password with the SKEY response to the
220 pass = skey_response (skey_sequence, seed, pass);
223 #endif /* ENABLE_OPIE */
225 /* Send PASS password. */
226 request = ftp_request ("PASS", pass);
227 nwritten = fd_write (csock, request, strlen (request), -1);
234 /* Get appropriate response. */
235 err = ftp_response (csock, &respline);
238 if (*respline != '2')
249 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
254 assert (addr->family == AF_INET);
255 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
256 assert (buflen >= 6 * 4);
258 ptr = IP_INADDR_DATA (addr);
259 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
260 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
261 buf[buflen - 1] = '\0';
264 /* Bind a port and send the appropriate PORT command to the FTP
265 server. Use acceptport after RETR, to get the socket of data
268 ftp_port (int csock, int *local_sock)
271 char *request, *respline;
275 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
276 char bytes[6 * 4 + 1];
278 /* Get the address of this side of the connection. */
279 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
282 assert (addr.family == AF_INET);
284 /* Setting port to 0 lets the system choose a free port. */
288 *local_sock = bind_local (&addr, &port);
292 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
293 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
295 /* Send PORT request. */
296 request = ftp_request ("PORT", bytes);
297 nwritten = fd_write (csock, request, strlen (request), -1);
301 fd_close (*local_sock);
306 /* Get appropriate response. */
307 err = ftp_response (csock, &respline);
310 fd_close (*local_sock);
313 if (*respline != '2')
316 fd_close (*local_sock);
325 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
328 unsigned char *ptr = IP_INADDR_DATA (addr);
330 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
331 assert (buflen >= 21 * 4);
333 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
334 switch (addr->family)
337 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
338 ptr[0], ptr[1], ptr[2], ptr[3], 2,
339 (port & 0xff00) >> 8, port & 0xff);
342 snprintf (buf, buflen,
343 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
345 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
346 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15],
347 2, (port & 0xff00) >> 8, port & 0xff);
354 /* Bind a port and send the appropriate PORT command to the FTP
355 server. Use acceptport after RETR, to get the socket of data
358 ftp_lprt (int csock, int *local_sock)
361 char *request, *respline;
365 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
366 char bytes[21 * 4 + 1];
368 /* Get the address of this side of the connection. */
369 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
372 assert (addr.family == AF_INET || addr.family == AF_INET6);
374 /* Setting port to 0 lets the system choose a free port. */
378 *local_sock = bind_local (&addr, &port);
382 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
383 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
385 /* Send PORT request. */
386 request = ftp_request ("LPRT", bytes);
387 nwritten = fd_write (csock, request, strlen (request), -1);
391 fd_close (*local_sock);
395 /* Get appropriate response. */
396 err = ftp_response (csock, &respline);
399 fd_close (*local_sock);
402 if (*respline != '2')
405 fd_close (*local_sock);
413 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
418 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
419 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
420 * 1 char for af (1-2) and 5 chars for port (0-65535) */
421 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
423 /* Construct the argument of EPRT (of the form |af|addr|port|). */
424 afnum = (addr->family == AF_INET ? 1 : 2);
425 snprintf (buf, buflen, "|%d|%s|%d|", afnum, print_address (addr), port);
426 buf[buflen - 1] = '\0';
429 /* Bind a port and send the appropriate PORT command to the FTP
430 server. Use acceptport after RETR, to get the socket of data
433 ftp_eprt (int csock, int *local_sock)
436 char *request, *respline;
440 /* Must contain the argument of EPRT (of the form |af|addr|port|).
441 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
442 * 1 char for af (1-2) and 5 chars for port (0-65535) */
443 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
445 /* Get the address of this side of the connection. */
446 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
449 /* Setting port to 0 lets the system choose a free port. */
453 *local_sock = bind_local (&addr, &port);
457 /* Construct the argument of EPRT (of the form |af|addr|port|). */
458 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
460 /* Send PORT request. */
461 request = ftp_request ("EPRT", bytes);
462 nwritten = fd_write (csock, request, strlen (request), -1);
466 fd_close (*local_sock);
470 /* Get appropriate response. */
471 err = ftp_response (csock, &respline);
474 fd_close (*local_sock);
477 if (*respline != '2')
480 fd_close (*local_sock);
488 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
489 transfer. Reads the response from server and parses it. Reads the
490 host and port addresses and returns them. */
492 ftp_pasv (int csock, ip_address *addr, int *port)
494 char *request, *respline, *s;
497 unsigned char tmp[6];
499 assert (addr != NULL);
500 assert (port != NULL);
504 /* Form the request. */
505 request = ftp_request ("PASV", NULL);
507 nwritten = fd_write (csock, request, strlen (request), -1);
514 /* Get the server response. */
515 err = ftp_response (csock, &respline);
518 if (*respline != '2')
523 /* Parse the request. */
525 for (s += 4; *s && !c_isdigit (*s); s++)
529 for (i = 0; i < 6; i++)
532 for (; c_isdigit (*s); s++)
533 tmp[i] = (*s - '0') + 10 * tmp[i];
538 /* When on the last number, anything can be a terminator. */
545 addr->family = AF_INET;
546 memcpy (IP_INADDR_DATA (addr), tmp, 4);
547 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
553 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
554 transfer. Reads the response from server and parses it. Reads the
555 host and port addresses and returns them. */
557 ftp_lpsv (int csock, ip_address *addr, int *port)
559 char *request, *respline, *s;
560 int nwritten, i, af, addrlen, portlen;
562 unsigned char tmp[16];
563 unsigned char tmpprt[2];
565 assert (addr != NULL);
566 assert (port != NULL);
570 /* Form the request. */
571 request = ftp_request ("LPSV", NULL);
574 nwritten = fd_write (csock, request, strlen (request), -1);
582 /* Get the server response. */
583 err = ftp_response (csock, &respline);
586 if (*respline != '2')
592 /* Parse the response. */
594 for (s += 4; *s && !c_isdigit (*s); s++)
599 /* First, get the address family */
601 for (; c_isdigit (*s); s++)
602 af = (*s - '0') + 10 * af;
604 if (af != 4 && af != 6)
610 if (!*s || *s++ != ',')
616 /* Then, get the address length */
618 for (; c_isdigit (*s); s++)
619 addrlen = (*s - '0') + 10 * addrlen;
621 if (!*s || *s++ != ',')
633 if ((af == 4 && addrlen != 4)
634 || (af == 6 && addrlen != 16))
640 /* Now, we get the actual address */
641 for (i = 0; i < addrlen; i++)
644 for (; c_isdigit (*s); s++)
645 tmp[i] = (*s - '0') + 10 * tmp[i];
655 /* Now, get the port length */
657 for (; c_isdigit (*s); s++)
658 portlen = (*s - '0') + 10 * portlen;
660 if (!*s || *s++ != ',')
672 /* Finally, we get the port number */
674 for (; c_isdigit (*s); s++)
675 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
677 if (!*s || *s++ != ',')
684 for (; c_isdigit (*s); s++)
685 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
691 addr->family = AF_INET;
692 memcpy (IP_INADDR_DATA (addr), tmp, 4);
693 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
694 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
695 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
696 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
697 DEBUGP (("*port is: %d\n", *port));
702 addr->family = AF_INET6;
703 memcpy (IP_INADDR_DATA (addr), tmp, 16);
704 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
705 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
706 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
707 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
708 DEBUGP (("*port is: %d\n", *port));
715 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
716 transfer. Reads the response from server and parses it. Reads the
717 host and port addresses and returns them. */
719 ftp_epsv (int csock, ip_address *ip, int *port)
721 char *request, *respline, *start, delim, *s;
727 assert (port != NULL);
729 /* IP already contains the IP address of the control connection's
730 peer, so we don't need to call socket_ip_address here. */
732 /* Form the request. */
733 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
734 request = ftp_request ("EPSV", (ip->family == AF_INET ? "1" : "2"));
737 nwritten = fd_write (csock, request, strlen (request), -1);
745 /* Get the server response. */
746 err = ftp_response (csock, &respline);
749 if (*respline != '2')
755 assert (respline != NULL);
757 DEBUGP(("respline is %s\n", respline));
759 /* Skip the useless stuff and get what's inside the parentheses */
760 start = strchr (respline, '(');
767 /* Skip the first two void fields */
770 if (delim < 33 || delim > 126)
776 for (i = 0; i < 2; i++)
785 /* Finally, get the port number */
787 for (i = 1; c_isdigit (*s); s++)
794 tport = (*s - '0') + 10 * tport;
797 /* Make sure that the response terminates correcty */
817 /* Sends the TYPE request to the server. */
819 ftp_type (int csock, int type)
821 char *request, *respline;
826 /* Construct argument. */
829 /* Send TYPE request. */
830 request = ftp_request ("TYPE", stype);
831 nwritten = fd_write (csock, request, strlen (request), -1);
838 /* Get appropriate response. */
839 err = ftp_response (csock, &respline);
842 if (*respline != '2')
845 return FTPUNKNOWNTYPE;
852 /* Changes the working directory by issuing a CWD command to the
855 ftp_cwd (int csock, const char *dir)
857 char *request, *respline;
861 /* Send CWD request. */
862 request = ftp_request ("CWD", dir);
863 nwritten = fd_write (csock, request, strlen (request), -1);
870 /* Get appropriate response. */
871 err = ftp_response (csock, &respline);
874 if (*respline == '5')
879 if (*respline != '2')
889 /* Sends REST command to the FTP server. */
891 ftp_rest (int csock, wgint offset)
893 char *request, *respline;
897 request = ftp_request ("REST", number_to_static_string (offset));
898 nwritten = fd_write (csock, request, strlen (request), -1);
905 /* Get appropriate response. */
906 err = ftp_response (csock, &respline);
909 if (*respline != '3')
919 /* Sends RETR command to the FTP server. */
921 ftp_retr (int csock, const char *file)
923 char *request, *respline;
927 /* Send RETR request. */
928 request = ftp_request ("RETR", file);
929 nwritten = fd_write (csock, request, strlen (request), -1);
936 /* Get appropriate response. */
937 err = ftp_response (csock, &respline);
940 if (*respline == '5')
945 if (*respline != '1')
955 /* Sends the LIST command to the server. If FILE is NULL, send just
956 `LIST' (no space). */
958 ftp_list (int csock, const char *file, enum stype rs)
960 char *request, *respline;
965 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
966 const char *list_commands[] = { "LIST -a",
969 /* 2008-01-29 SMS. For a VMS FTP server, where "LIST -a" may not
970 fail, but will never do what is desired here, skip directly to the
971 simple "LIST" command (assumed to be the last one in the list).
974 i = countof (list_commands)- 1;
978 request = ftp_request (list_commands[i], file);
979 nwritten = fd_write (csock, request, strlen (request), -1);
986 /* Get appropriate response. */
987 err = ftp_response (csock, &respline);
990 if (*respline == '5')
994 else if (*respline == '1')
1006 } while (i < countof (list_commands) && !ok);
1011 /* Sends the SYST command to the server. */
1013 ftp_syst (int csock, enum stype *server_type)
1015 char *request, *respline;
1019 /* Send SYST request. */
1020 request = ftp_request ("SYST", NULL);
1021 nwritten = fd_write (csock, request, strlen (request), -1);
1029 /* Get appropriate response. */
1030 err = ftp_response (csock, &respline);
1033 if (*respline == '5')
1039 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1040 strtok (respline, " ");
1042 /* Which system type has been reported (we are interested just in the
1043 first word of the server response)? */
1044 request = strtok (NULL, " ");
1046 if (request == NULL)
1047 *server_type = ST_OTHER;
1048 else if (!strcasecmp (request, "VMS"))
1049 *server_type = ST_VMS;
1050 else if (!strcasecmp (request, "UNIX"))
1051 *server_type = ST_UNIX;
1052 else if (!strcasecmp (request, "WINDOWS_NT")
1053 || !strcasecmp (request, "WINDOWS2000"))
1054 *server_type = ST_WINNT;
1055 else if (!strcasecmp (request, "MACOS"))
1056 *server_type = ST_MACOS;
1057 else if (!strcasecmp (request, "OS/400"))
1058 *server_type = ST_OS400;
1060 *server_type = ST_OTHER;
1067 /* Sends the PWD command to the server. */
1069 ftp_pwd (int csock, char **pwd)
1071 char *request, *respline;
1075 /* Send PWD request. */
1076 request = ftp_request ("PWD", NULL);
1077 nwritten = fd_write (csock, request, strlen (request), -1);
1084 /* Get appropriate response. */
1085 err = ftp_response (csock, &respline);
1088 if (*respline == '5')
1095 /* Skip the number (257), leading citation mark, trailing citation mark
1096 and everything following it. */
1097 strtok (respline, "\"");
1098 request = strtok (NULL, "\"");
1100 /* Treat the malformed response as an error, which the caller has
1101 to handle gracefully anyway. */
1104 /* Has the `pwd' been already allocated? Free! */
1107 *pwd = xstrdup (request);
1114 /* Sends the SIZE command to the server, and returns the value in 'size'.
1115 * If an error occurs, size is set to zero. */
1117 ftp_size (int csock, const char *file, wgint *size)
1119 char *request, *respline;
1123 /* Send PWD request. */
1124 request = ftp_request ("SIZE", file);
1125 nwritten = fd_write (csock, request, strlen (request), -1);
1133 /* Get appropriate response. */
1134 err = ftp_response (csock, &respline);
1140 if (*respline == '5')
1143 * Probably means SIZE isn't supported on this server.
1144 * Error is nonfatal since SIZE isn't in RFC 959
1152 *size = str_to_wgint (respline + 4, NULL, 10);
1156 * Couldn't parse the response for some reason. On the (few)
1157 * tests I've done, the response is 213 <SIZE> with nothing else -
1158 * maybe something a bit more resilient is necessary. It's not a
1159 * fatal error, however.
1171 /* If URL's params are of the form "type=X", return character X.
1172 Otherwise, return 'I' (the default type). */
1174 ftp_process_type (const char *params)
1177 && 0 == strncasecmp (params, "type=", 5)
1178 && params[5] != '\0')
1179 return c_toupper (params[5]);