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 DELE command to the FTP server. */
892 ftp_dele (int csock, const char *file)
894 char *request, *respline;
898 /* Send DELE request. */
899 request = ftp_request ("DELE", file);
900 nwritten = fd_write (csock, request, strlen (request), -1.0);
907 /* Get appropriate response. */
908 err = ftp_response (csock, &respline);
910 return err; /* Return with early bad status. */
912 /* All OK, so far. */
913 if (*respline == '5')
915 err = FTPNSFOD; /* Permanent Negative Completion. */
917 else if (*respline != '2') /* Success might be 226 or 250 (or ???). */
919 err = FTPRERR; /* Not Positive Completion. */
922 xfree (respline); /* Free "respline" storage. */
923 return err; /* Return response-based status code. */
926 /* Sends REST command to the FTP server. */
928 ftp_rest (int csock, wgint offset)
930 char *request, *respline;
934 request = ftp_request ("REST", number_to_static_string (offset));
935 nwritten = fd_write (csock, request, strlen (request), -1);
942 /* Get appropriate response. */
943 err = ftp_response (csock, &respline);
946 if (*respline != '3')
956 /* Sends RETR command to the FTP server. */
958 ftp_retr (int csock, const char *file)
960 char *request, *respline;
964 /* Send RETR request. */
965 request = ftp_request ("RETR", file);
966 nwritten = fd_write (csock, request, strlen (request), -1);
973 /* Get appropriate response. */
974 err = ftp_response (csock, &respline);
977 if (*respline == '5')
982 if (*respline != '1')
992 /* Sends the LIST command to the server. If FILE is NULL, send just
993 `LIST' (no space). */
995 ftp_list (int csock, const char *file, enum stype rs)
997 char *request, *respline;
1002 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
1003 const char *list_commands[] = { "LIST -a",
1006 /* 2008-01-29 SMS. For a VMS FTP server, where "LIST -a" may not
1007 fail, but will never do what is desired here, skip directly to the
1008 simple "LIST" command (assumed to be the last one in the list).
1011 i = countof (list_commands)- 1;
1015 request = ftp_request (list_commands[i], file);
1016 nwritten = fd_write (csock, request, strlen (request), -1);
1023 /* Get appropriate response. */
1024 err = ftp_response (csock, &respline);
1027 if (*respline == '5')
1031 else if (*respline == '1')
1043 } while (i < countof (list_commands) && !ok);
1048 /* Sends the SYST command to the server. */
1050 ftp_syst (int csock, enum stype *server_type)
1052 char *request, *respline;
1056 /* Send SYST request. */
1057 request = ftp_request ("SYST", NULL);
1058 nwritten = fd_write (csock, request, strlen (request), -1);
1066 /* Get appropriate response. */
1067 err = ftp_response (csock, &respline);
1070 if (*respline == '5')
1076 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1077 strtok (respline, " ");
1079 /* Which system type has been reported (we are interested just in the
1080 first word of the server response)? */
1081 request = strtok (NULL, " ");
1083 if (request == NULL)
1084 *server_type = ST_OTHER;
1085 else if (!strcasecmp (request, "VMS"))
1086 *server_type = ST_VMS;
1087 else if (!strcasecmp (request, "UNIX"))
1088 *server_type = ST_UNIX;
1089 else if (!strcasecmp (request, "WINDOWS_NT")
1090 || !strcasecmp (request, "WINDOWS2000"))
1091 *server_type = ST_WINNT;
1092 else if (!strcasecmp (request, "MACOS"))
1093 *server_type = ST_MACOS;
1094 else if (!strcasecmp (request, "OS/400"))
1095 *server_type = ST_OS400;
1097 *server_type = ST_OTHER;
1104 /* Sends the PWD command to the server. */
1106 ftp_pwd (int csock, char **pwd)
1108 char *request, *respline;
1112 /* Send PWD request. */
1113 request = ftp_request ("PWD", NULL);
1114 nwritten = fd_write (csock, request, strlen (request), -1);
1121 /* Get appropriate response. */
1122 err = ftp_response (csock, &respline);
1125 if (*respline == '5')
1132 /* Skip the number (257), leading citation mark, trailing citation mark
1133 and everything following it. */
1134 strtok (respline, "\"");
1135 request = strtok (NULL, "\"");
1137 /* Treat the malformed response as an error, which the caller has
1138 to handle gracefully anyway. */
1141 /* Has the `pwd' been already allocated? Free! */
1144 *pwd = xstrdup (request);
1151 /* Sends the SIZE command to the server, and returns the value in 'size'.
1152 * If an error occurs, size is set to zero. */
1154 ftp_size (int csock, const char *file, wgint *size)
1156 char *request, *respline;
1160 /* Send PWD request. */
1161 request = ftp_request ("SIZE", file);
1162 nwritten = fd_write (csock, request, strlen (request), -1);
1170 /* Get appropriate response. */
1171 err = ftp_response (csock, &respline);
1177 if (*respline == '5')
1180 * Probably means SIZE isn't supported on this server.
1181 * Error is nonfatal since SIZE isn't in RFC 959
1189 *size = str_to_wgint (respline + 4, NULL, 10);
1193 * Couldn't parse the response for some reason. On the (few)
1194 * tests I've done, the response is 213 <SIZE> with nothing else -
1195 * maybe something a bit more resilient is necessary. It's not a
1196 * fatal error, however.
1208 /* If URL's params are of the form "type=X", return character X.
1209 Otherwise, return 'I' (the default type). */
1211 ftp_process_type (const char *params)
1214 && 0 == strncasecmp (params, "type=", 5)
1215 && params[5] != '\0')
1216 return c_toupper (params[5]);