2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 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 escnonprint 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", escnonprint (line));
81 DEBUGP (("%s\n", escnonprint (line)));
83 /* The last line of output is the one that begins with "ddd ". */
84 if (c_isdigit (line[0]) && c_isdigit (line[1]) && c_isdigit (line[2])
87 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
88 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
96 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
97 it if printing is required. If VALUE is NULL, just use
100 ftp_request (const char *command, const char *value)
105 /* Check for newlines in VALUE (possibly injected by the %0A URL
106 escape) making the callers inadvertently send multiple FTP
107 commands at once. Without this check an attacker could
108 intentionally redirect to ftp://server/fakedir%0Acommand.../
109 and execute arbitrary FTP command on a remote FTP server. */
110 if (strpbrk (value, "\r\n"))
112 /* Copy VALUE to the stack and modify CR/LF to space. */
114 STRDUP_ALLOCA (defanged, value);
115 for (p = defanged; *p; p++)
116 if (*p == '\r' || *p == '\n')
118 DEBUGP (("\nDetected newlines in %s \"%s\"; changing to %s \"%s\"\n",
119 command, escnonprint (value), command, escnonprint (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 /* Parse the response. */
760 /* Skip the useless stuff and get what's inside the parentheses */
761 start = strchr (respline, '(');
768 /* Skip the first two void fields */
771 if (delim < 33 || delim > 126)
777 for (i = 0; i < 2; i++)
786 /* Finally, get the port number */
788 for (i = 1; c_isdigit (*s); s++)
795 tport = (*s - '0') + 10 * tport;
798 /* Make sure that the response terminates correcty */
818 /* Sends the TYPE request to the server. */
820 ftp_type (int csock, int type)
822 char *request, *respline;
827 /* Construct argument. */
830 /* Send TYPE request. */
831 request = ftp_request ("TYPE", stype);
832 nwritten = fd_write (csock, request, strlen (request), -1);
839 /* Get appropriate response. */
840 err = ftp_response (csock, &respline);
843 if (*respline != '2')
846 return FTPUNKNOWNTYPE;
853 /* Changes the working directory by issuing a CWD command to the
856 ftp_cwd (int csock, const char *dir)
858 char *request, *respline;
862 /* Send CWD request. */
863 request = ftp_request ("CWD", dir);
864 nwritten = fd_write (csock, request, strlen (request), -1);
871 /* Get appropriate response. */
872 err = ftp_response (csock, &respline);
875 if (*respline == '5')
880 if (*respline != '2')
890 /* Sends REST command to the FTP server. */
892 ftp_rest (int csock, wgint offset)
894 char *request, *respline;
898 request = ftp_request ("REST", number_to_static_string (offset));
899 nwritten = fd_write (csock, request, strlen (request), -1);
906 /* Get appropriate response. */
907 err = ftp_response (csock, &respline);
910 if (*respline != '3')
920 /* Sends RETR command to the FTP server. */
922 ftp_retr (int csock, const char *file)
924 char *request, *respline;
928 /* Send RETR request. */
929 request = ftp_request ("RETR", file);
930 nwritten = fd_write (csock, request, strlen (request), -1);
937 /* Get appropriate response. */
938 err = ftp_response (csock, &respline);
941 if (*respline == '5')
946 if (*respline != '1')
956 /* Sends the LIST command to the server. If FILE is NULL, send just
957 `LIST' (no space). */
959 ftp_list (int csock, const char *file)
961 char *request, *respline;
966 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
967 const char *list_commands[] = { "LIST -a",
972 request = ftp_request (list_commands[i], file);
973 nwritten = fd_write (csock, request, strlen (request), -1);
980 /* Get appropriate response. */
981 err = ftp_response (csock, &respline);
984 if (*respline == '5')
988 else if (*respline == '1')
1000 } while (i < countof (list_commands) && !ok);
1005 /* Sends the SYST command to the server. */
1007 ftp_syst (int csock, enum stype *server_type)
1009 char *request, *respline;
1013 /* Send SYST request. */
1014 request = ftp_request ("SYST", NULL);
1015 nwritten = fd_write (csock, request, strlen (request), -1);
1023 /* Get appropriate response. */
1024 err = ftp_response (csock, &respline);
1027 if (*respline == '5')
1033 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1034 strtok (respline, " ");
1036 /* Which system type has been reported (we are interested just in the
1037 first word of the server response)? */
1038 request = strtok (NULL, " ");
1040 if (request == NULL)
1041 *server_type = ST_OTHER;
1042 else if (!strcasecmp (request, "VMS"))
1043 *server_type = ST_VMS;
1044 else if (!strcasecmp (request, "UNIX"))
1045 *server_type = ST_UNIX;
1046 else if (!strcasecmp (request, "WINDOWS_NT")
1047 || !strcasecmp (request, "WINDOWS2000"))
1048 *server_type = ST_WINNT;
1049 else if (!strcasecmp (request, "MACOS"))
1050 *server_type = ST_MACOS;
1051 else if (!strcasecmp (request, "OS/400"))
1052 *server_type = ST_OS400;
1054 *server_type = ST_OTHER;
1061 /* Sends the PWD command to the server. */
1063 ftp_pwd (int csock, char **pwd)
1065 char *request, *respline;
1069 /* Send PWD request. */
1070 request = ftp_request ("PWD", NULL);
1071 nwritten = fd_write (csock, request, strlen (request), -1);
1078 /* Get appropriate response. */
1079 err = ftp_response (csock, &respline);
1082 if (*respline == '5')
1089 /* Skip the number (257), leading citation mark, trailing citation mark
1090 and everything following it. */
1091 strtok (respline, "\"");
1092 request = strtok (NULL, "\"");
1094 /* Treat the malformed response as an error, which the caller has
1095 to handle gracefully anyway. */
1098 /* Has the `pwd' been already allocated? Free! */
1101 *pwd = xstrdup (request);
1108 /* Sends the SIZE command to the server, and returns the value in 'size'.
1109 * If an error occurs, size is set to zero. */
1111 ftp_size (int csock, const char *file, wgint *size)
1113 char *request, *respline;
1117 /* Send PWD request. */
1118 request = ftp_request ("SIZE", file);
1119 nwritten = fd_write (csock, request, strlen (request), -1);
1127 /* Get appropriate response. */
1128 err = ftp_response (csock, &respline);
1134 if (*respline == '5')
1137 * Probably means SIZE isn't supported on this server.
1138 * Error is nonfatal since SIZE isn't in RFC 959
1146 *size = str_to_wgint (respline + 4, NULL, 10);
1150 * Couldn't parse the response for some reason. On the (few)
1151 * tests I've done, the response is 213 <SIZE> with nothing else -
1152 * maybe something a bit more resilient is necessary. It's not a
1153 * fatal error, however.
1165 /* If URL's params are of the form "type=X", return character X.
1166 Otherwise, return 'I' (the default type). */
1168 ftp_process_type (const char *params)
1171 && 0 == strncasecmp (params, "type=", 5)
1172 && params[5] != '\0')
1173 return c_toupper (params[5]);