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. */
46 char ftp_last_respline[128];
49 /* Get the response of FTP server and allocate enough room to handle
50 it. <CR> and <LF> characters are stripped from the line, and the
51 line is 0-terminated. All the response lines but the last one are
52 skipped. The last line is determined as described in RFC959.
54 If the line is successfully read, FTPOK is returned, and *ret_line
55 is assigned a freshly allocated line. Otherwise, FTPRERR is
56 returned, and the value of *ret_line should be ignored. */
59 ftp_response (int fd, char **ret_line)
64 char *line = fd_read_line (fd);
68 /* Strip trailing CRLF before printing the line, so that
69 quotting doesn't include bogus \012 and \015. */
70 p = strchr (line, '\0');
71 if (p > line && p[-1] == '\n')
73 if (p > line && p[-1] == '\r')
76 if (opt.server_response)
77 logprintf (LOG_NOTQUIET, "%s\n",
78 quotearg_style (escape_quoting_style, line));
80 DEBUGP (("%s\n", quotearg_style (escape_quoting_style, line)));
82 /* The last line of output is the one that begins with "ddd ". */
83 if (c_isdigit (line[0]) && c_isdigit (line[1]) && c_isdigit (line[2])
86 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
87 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
95 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
96 it if printing is required. If VALUE is NULL, just use
99 ftp_request (const char *command, const char *value)
104 /* Check for newlines in VALUE (possibly injected by the %0A URL
105 escape) making the callers inadvertently send multiple FTP
106 commands at once. Without this check an attacker could
107 intentionally redirect to ftp://server/fakedir%0Acommand.../
108 and execute arbitrary FTP command on a remote FTP server. */
109 if (strpbrk (value, "\r\n"))
111 /* Copy VALUE to the stack and modify CR/LF to space. */
113 STRDUP_ALLOCA (defanged, value);
114 for (p = defanged; *p; p++)
115 if (*p == '\r' || *p == '\n')
117 DEBUGP (("\nDetected newlines in %s \"%s\"; changing to %s \"%s\"\n",
118 command, quotearg_style (escape_quoting_style, value),
119 command, quotearg_style (escape_quoting_style, defanged)));
120 /* Make VALUE point to the defanged copy of the string. */
123 res = concat_strings (command, " ", value, "\r\n", (char *) 0);
126 res = concat_strings (command, "\r\n", (char *) 0);
127 if (opt.server_response)
129 /* Hack: don't print out password. */
130 if (strncmp (res, "PASS", 4) != 0)
131 logprintf (LOG_ALWAYS, "--> %s\n", res);
133 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
136 DEBUGP (("\n--> %s\n", res));
140 /* Sends the USER and PASS commands to the server, to control
141 connection socket csock. */
143 ftp_login (int csock, const char *acc, const char *pass)
146 char *request, *respline;
150 err = ftp_response (csock, &respline);
153 if (*respline != '2')
159 /* Send USER username. */
160 request = ftp_request ("USER", acc);
161 nwritten = fd_write (csock, request, strlen (request), -1);
168 /* Get appropriate response. */
169 err = ftp_response (csock, &respline);
172 /* An unprobable possibility of logging without a password. */
173 if (*respline == '2')
178 /* Else, only response 3 is appropriate. */
179 if (*respline != '3')
182 return FTPLOGREFUSED;
186 static const char *skey_head[] = {
191 const char *seed = NULL;
193 for (i = 0; i < countof (skey_head); i++)
195 int l = strlen (skey_head[i]);
196 if (0 == strncasecmp (skey_head[i], respline, l))
204 int skey_sequence = 0;
206 /* Extract the sequence from SEED. */
207 for (; c_isdigit (*seed); seed++)
208 skey_sequence = 10 * skey_sequence + *seed - '0';
214 return FTPLOGREFUSED;
216 /* Replace the password with the SKEY response to the
218 pass = skey_response (skey_sequence, seed, pass);
221 #endif /* ENABLE_OPIE */
223 /* Send PASS password. */
224 request = ftp_request ("PASS", pass);
225 nwritten = fd_write (csock, request, strlen (request), -1);
232 /* Get appropriate response. */
233 err = ftp_response (csock, &respline);
236 if (*respline != '2')
247 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
252 assert (addr->family == AF_INET);
253 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
254 assert (buflen >= 6 * 4);
256 ptr = IP_INADDR_DATA (addr);
257 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
258 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
259 buf[buflen - 1] = '\0';
262 /* Bind a port and send the appropriate PORT command to the FTP
263 server. Use acceptport after RETR, to get the socket of data
266 ftp_port (int csock, int *local_sock)
269 char *request, *respline;
273 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
274 char bytes[6 * 4 + 1];
276 /* Get the address of this side of the connection. */
277 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
280 assert (addr.family == AF_INET);
282 /* Setting port to 0 lets the system choose a free port. */
286 *local_sock = bind_local (&addr, &port);
290 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
291 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
293 /* Send PORT request. */
294 request = ftp_request ("PORT", bytes);
295 nwritten = fd_write (csock, request, strlen (request), -1);
299 fd_close (*local_sock);
304 /* Get appropriate response. */
305 err = ftp_response (csock, &respline);
308 fd_close (*local_sock);
311 if (*respline != '2')
314 fd_close (*local_sock);
323 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
326 unsigned char *ptr = IP_INADDR_DATA (addr);
328 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
329 assert (buflen >= 21 * 4);
331 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
332 switch (addr->family)
335 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
336 ptr[0], ptr[1], ptr[2], ptr[3], 2,
337 (port & 0xff00) >> 8, port & 0xff);
340 snprintf (buf, buflen,
341 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
343 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
344 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15],
345 2, (port & 0xff00) >> 8, port & 0xff);
352 /* Bind a port and send the appropriate PORT command to the FTP
353 server. Use acceptport after RETR, to get the socket of data
356 ftp_lprt (int csock, int *local_sock)
359 char *request, *respline;
363 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
364 char bytes[21 * 4 + 1];
366 /* Get the address of this side of the connection. */
367 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
370 assert (addr.family == AF_INET || addr.family == AF_INET6);
372 /* Setting port to 0 lets the system choose a free port. */
376 *local_sock = bind_local (&addr, &port);
380 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
381 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
383 /* Send PORT request. */
384 request = ftp_request ("LPRT", bytes);
385 nwritten = fd_write (csock, request, strlen (request), -1);
389 fd_close (*local_sock);
393 /* Get appropriate response. */
394 err = ftp_response (csock, &respline);
397 fd_close (*local_sock);
400 if (*respline != '2')
403 fd_close (*local_sock);
411 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
416 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
417 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
418 * 1 char for af (1-2) and 5 chars for port (0-65535) */
419 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
421 /* Construct the argument of EPRT (of the form |af|addr|port|). */
422 afnum = (addr->family == AF_INET ? 1 : 2);
423 snprintf (buf, buflen, "|%d|%s|%d|", afnum, print_address (addr), port);
424 buf[buflen - 1] = '\0';
427 /* Bind a port and send the appropriate PORT command to the FTP
428 server. Use acceptport after RETR, to get the socket of data
431 ftp_eprt (int csock, int *local_sock)
434 char *request, *respline;
438 /* Must contain the argument of EPRT (of the form |af|addr|port|).
439 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
440 * 1 char for af (1-2) and 5 chars for port (0-65535) */
441 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
443 /* Get the address of this side of the connection. */
444 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
447 /* Setting port to 0 lets the system choose a free port. */
451 *local_sock = bind_local (&addr, &port);
455 /* Construct the argument of EPRT (of the form |af|addr|port|). */
456 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
458 /* Send PORT request. */
459 request = ftp_request ("EPRT", bytes);
460 nwritten = fd_write (csock, request, strlen (request), -1);
464 fd_close (*local_sock);
468 /* Get appropriate response. */
469 err = ftp_response (csock, &respline);
472 fd_close (*local_sock);
475 if (*respline != '2')
478 fd_close (*local_sock);
486 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
487 transfer. Reads the response from server and parses it. Reads the
488 host and port addresses and returns them. */
490 ftp_pasv (int csock, ip_address *addr, int *port)
492 char *request, *respline, *s;
495 unsigned char tmp[6];
497 assert (addr != NULL);
498 assert (port != NULL);
502 /* Form the request. */
503 request = ftp_request ("PASV", NULL);
505 nwritten = fd_write (csock, request, strlen (request), -1);
512 /* Get the server response. */
513 err = ftp_response (csock, &respline);
516 if (*respline != '2')
521 /* Parse the request. */
523 for (s += 4; *s && !c_isdigit (*s); s++)
527 for (i = 0; i < 6; i++)
530 for (; c_isdigit (*s); s++)
531 tmp[i] = (*s - '0') + 10 * tmp[i];
536 /* When on the last number, anything can be a terminator. */
543 addr->family = AF_INET;
544 memcpy (IP_INADDR_DATA (addr), tmp, 4);
545 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
551 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
552 transfer. Reads the response from server and parses it. Reads the
553 host and port addresses and returns them. */
555 ftp_lpsv (int csock, ip_address *addr, int *port)
557 char *request, *respline, *s;
558 int nwritten, i, af, addrlen, portlen;
560 unsigned char tmp[16];
561 unsigned char tmpprt[2];
563 assert (addr != NULL);
564 assert (port != NULL);
568 /* Form the request. */
569 request = ftp_request ("LPSV", NULL);
572 nwritten = fd_write (csock, request, strlen (request), -1);
580 /* Get the server response. */
581 err = ftp_response (csock, &respline);
584 if (*respline != '2')
590 /* Parse the response. */
592 for (s += 4; *s && !c_isdigit (*s); s++)
597 /* First, get the address family */
599 for (; c_isdigit (*s); s++)
600 af = (*s - '0') + 10 * af;
602 if (af != 4 && af != 6)
608 if (!*s || *s++ != ',')
614 /* Then, get the address length */
616 for (; c_isdigit (*s); s++)
617 addrlen = (*s - '0') + 10 * addrlen;
619 if (!*s || *s++ != ',')
631 if ((af == 4 && addrlen != 4)
632 || (af == 6 && addrlen != 16))
638 /* Now, we get the actual address */
639 for (i = 0; i < addrlen; i++)
642 for (; c_isdigit (*s); s++)
643 tmp[i] = (*s - '0') + 10 * tmp[i];
653 /* Now, get the port length */
655 for (; c_isdigit (*s); s++)
656 portlen = (*s - '0') + 10 * portlen;
658 if (!*s || *s++ != ',')
670 /* Finally, we get the port number */
672 for (; c_isdigit (*s); s++)
673 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
675 if (!*s || *s++ != ',')
682 for (; c_isdigit (*s); s++)
683 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
689 addr->family = AF_INET;
690 memcpy (IP_INADDR_DATA (addr), tmp, 4);
691 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
692 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
693 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
694 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
695 DEBUGP (("*port is: %d\n", *port));
700 addr->family = AF_INET6;
701 memcpy (IP_INADDR_DATA (addr), tmp, 16);
702 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
703 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
704 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
705 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
706 DEBUGP (("*port is: %d\n", *port));
713 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
714 transfer. Reads the response from server and parses it. Reads the
715 host and port addresses and returns them. */
717 ftp_epsv (int csock, ip_address *ip, int *port)
719 char *request, *respline, *start, delim, *s;
725 assert (port != NULL);
727 /* IP already contains the IP address of the control connection's
728 peer, so we don't need to call socket_ip_address here. */
730 /* Form the request. */
731 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
732 request = ftp_request ("EPSV", (ip->family == AF_INET ? "1" : "2"));
735 nwritten = fd_write (csock, request, strlen (request), -1);
743 /* Get the server response. */
744 err = ftp_response (csock, &respline);
747 if (*respline != '2')
753 assert (respline != NULL);
755 DEBUGP(("respline is %s\n", respline));
757 /* Skip the useless stuff and get what's inside the parentheses */
758 start = strchr (respline, '(');
765 /* Skip the first two void fields */
768 if (delim < 33 || delim > 126)
774 for (i = 0; i < 2; i++)
783 /* Finally, get the port number */
785 for (i = 1; c_isdigit (*s); s++)
792 tport = (*s - '0') + 10 * tport;
795 /* Make sure that the response terminates correcty */
815 /* Sends the TYPE request to the server. */
817 ftp_type (int csock, int type)
819 char *request, *respline;
824 /* Construct argument. */
827 /* Send TYPE request. */
828 request = ftp_request ("TYPE", stype);
829 nwritten = fd_write (csock, request, strlen (request), -1);
836 /* Get appropriate response. */
837 err = ftp_response (csock, &respline);
840 if (*respline != '2')
843 return FTPUNKNOWNTYPE;
850 /* Changes the working directory by issuing a CWD command to the
853 ftp_cwd (int csock, const char *dir)
855 char *request, *respline;
859 /* Send CWD request. */
860 request = ftp_request ("CWD", dir);
861 nwritten = fd_write (csock, request, strlen (request), -1);
868 /* Get appropriate response. */
869 err = ftp_response (csock, &respline);
872 if (*respline == '5')
877 if (*respline != '2')
887 /* Sends REST command to the FTP server. */
889 ftp_rest (int csock, wgint offset)
891 char *request, *respline;
895 request = ftp_request ("REST", number_to_static_string (offset));
896 nwritten = fd_write (csock, request, strlen (request), -1);
903 /* Get appropriate response. */
904 err = ftp_response (csock, &respline);
907 if (*respline != '3')
917 /* Sends RETR command to the FTP server. */
919 ftp_retr (int csock, const char *file)
921 char *request, *respline;
925 /* Send RETR request. */
926 request = ftp_request ("RETR", file);
927 nwritten = fd_write (csock, request, strlen (request), -1);
934 /* Get appropriate response. */
935 err = ftp_response (csock, &respline);
938 if (*respline == '5')
943 if (*respline != '1')
953 /* Sends the LIST command to the server. If FILE is NULL, send just
954 `LIST' (no space). */
956 ftp_list (int csock, const char *file, enum stype rs)
958 char *request, *respline;
963 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
964 const char *list_commands[] = { "LIST -a",
967 /* 2008-01-29 SMS. For a VMS FTP server, where "LIST -a" may not
968 fail, but will never do what is desired here, skip directly to the
969 simple "LIST" command (assumed to be the last one in the list).
972 i = countof (list_commands)- 1;
976 request = ftp_request (list_commands[i], file);
977 nwritten = fd_write (csock, request, strlen (request), -1);
984 /* Get appropriate response. */
985 err = ftp_response (csock, &respline);
988 if (*respline == '5')
992 else if (*respline == '1')
1004 } while (i < countof (list_commands) && !ok);
1009 /* Sends the SYST command to the server. */
1011 ftp_syst (int csock, enum stype *server_type)
1013 char *request, *respline;
1017 /* Send SYST request. */
1018 request = ftp_request ("SYST", NULL);
1019 nwritten = fd_write (csock, request, strlen (request), -1);
1027 /* Get appropriate response. */
1028 err = ftp_response (csock, &respline);
1031 if (*respline == '5')
1037 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1038 strtok (respline, " ");
1040 /* Which system type has been reported (we are interested just in the
1041 first word of the server response)? */
1042 request = strtok (NULL, " ");
1044 if (request == NULL)
1045 *server_type = ST_OTHER;
1046 else if (!strcasecmp (request, "VMS"))
1047 *server_type = ST_VMS;
1048 else if (!strcasecmp (request, "UNIX"))
1049 *server_type = ST_UNIX;
1050 else if (!strcasecmp (request, "WINDOWS_NT")
1051 || !strcasecmp (request, "WINDOWS2000"))
1052 *server_type = ST_WINNT;
1053 else if (!strcasecmp (request, "MACOS"))
1054 *server_type = ST_MACOS;
1055 else if (!strcasecmp (request, "OS/400"))
1056 *server_type = ST_OS400;
1058 *server_type = ST_OTHER;
1065 /* Sends the PWD command to the server. */
1067 ftp_pwd (int csock, char **pwd)
1069 char *request, *respline;
1073 /* Send PWD request. */
1074 request = ftp_request ("PWD", NULL);
1075 nwritten = fd_write (csock, request, strlen (request), -1);
1082 /* Get appropriate response. */
1083 err = ftp_response (csock, &respline);
1086 if (*respline == '5')
1093 /* Skip the number (257), leading citation mark, trailing citation mark
1094 and everything following it. */
1095 strtok (respline, "\"");
1096 request = strtok (NULL, "\"");
1098 /* Treat the malformed response as an error, which the caller has
1099 to handle gracefully anyway. */
1102 /* Has the `pwd' been already allocated? Free! */
1105 *pwd = xstrdup (request);
1112 /* Sends the SIZE command to the server, and returns the value in 'size'.
1113 * If an error occurs, size is set to zero. */
1115 ftp_size (int csock, const char *file, wgint *size)
1117 char *request, *respline;
1121 /* Send PWD request. */
1122 request = ftp_request ("SIZE", file);
1123 nwritten = fd_write (csock, request, strlen (request), -1);
1131 /* Get appropriate response. */
1132 err = ftp_response (csock, &respline);
1138 if (*respline == '5')
1141 * Probably means SIZE isn't supported on this server.
1142 * Error is nonfatal since SIZE isn't in RFC 959
1150 *size = str_to_wgint (respline + 4, NULL, 10);
1154 * Couldn't parse the response for some reason. On the (few)
1155 * tests I've done, the response is 213 <SIZE> with nothing else -
1156 * maybe something a bit more resilient is necessary. It's not a
1157 * fatal error, however.
1169 /* If URL's params are of the form "type=X", return character X.
1170 Otherwise, return 'I' (the default type). */
1172 ftp_process_type (const char *params)
1175 && 0 == strncasecmp (params, "type=", 5)
1176 && params[5] != '\0')
1177 return c_toupper (params[5]);