2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 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. */
50 char ftp_last_respline[128];
53 /* Get the response of FTP server and allocate enough room to handle
54 it. <CR> and <LF> characters are stripped from the line, and the
55 line is 0-terminated. All the response lines but the last one are
56 skipped. The last line is determined as described in RFC959.
58 If the line is successfully read, FTPOK is returned, and *ret_line
59 is assigned a freshly allocated line. Otherwise, FTPRERR is
60 returned, and the value of *ret_line should be ignored. */
63 ftp_response (int fd, char **ret_line)
68 char *line = fd_read_line (fd);
72 /* Strip trailing CRLF before printing the line, so that
73 escnonprint doesn't include bogus \012 and \015. */
74 p = strchr (line, '\0');
75 if (p > line && p[-1] == '\n')
77 if (p > line && p[-1] == '\r')
80 if (opt.server_response)
81 logprintf (LOG_NOTQUIET, "%s\n", escnonprint (line));
83 DEBUGP (("%s\n", escnonprint (line)));
85 /* The last line of output is the one that begins with "ddd ". */
86 if (ISDIGIT (line[0]) && ISDIGIT (line[1]) && ISDIGIT (line[2])
89 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
90 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
98 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
99 it if printing is required. If VALUE is NULL, just use
102 ftp_request (const char *command, const char *value)
107 /* Check for newlines in VALUE (possibly injected by the %0A URL
108 escape) making the callers inadvertently send multiple FTP
109 commands at once. Without this check an attacker could
110 intentionally redirect to ftp://server/fakedir%0Acommand.../
111 and execute arbitrary FTP command on a remote FTP server. */
112 if (strpbrk (value, "\r\n"))
114 /* Copy VALUE to the stack and modify CR/LF to space. */
116 STRDUP_ALLOCA (defanged, value);
117 for (p = defanged; *p; p++)
118 if (*p == '\r' || *p == '\n')
120 DEBUGP (("\nDetected newlines in %s \"%s\"; changing to %s \"%s\"\n",
121 command, escnonprint (value), command, escnonprint (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 (; 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 && !ISDIGIT (*s); s++)
529 for (i = 0; i < 6; i++)
532 for (; 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 && !ISDIGIT (*s); s++)
599 /* First, get the address family */
601 for (; 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 (; 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 (; ISDIGIT (*s); s++)
645 tmp[i] = (*s - '0') + 10 * tmp[i];
655 /* Now, get the port length */
657 for (; ISDIGIT (*s); s++)
658 portlen = (*s - '0') + 10 * portlen;
660 if (!*s || *s++ != ',')
672 /* Finally, we get the port number */
674 for (; ISDIGIT (*s); s++)
675 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
677 if (!*s || *s++ != ',')
684 for (; 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 /* Parse the response. */
762 /* Skip the useless stuff and get what's inside the parentheses */
763 start = strchr (respline, '(');
770 /* Skip the first two void fields */
773 if (delim < 33 || delim > 126)
779 for (i = 0; i < 2; i++)
788 /* Finally, get the port number */
790 for (i = 1; ISDIGIT (*s); s++)
797 tport = (*s - '0') + 10 * tport;
800 /* Make sure that the response terminates correcty */
820 /* Sends the TYPE request to the server. */
822 ftp_type (int csock, int type)
824 char *request, *respline;
829 /* Construct argument. */
832 /* Send TYPE request. */
833 request = ftp_request ("TYPE", stype);
834 nwritten = fd_write (csock, request, strlen (request), -1);
841 /* Get appropriate response. */
842 err = ftp_response (csock, &respline);
845 if (*respline != '2')
848 return FTPUNKNOWNTYPE;
855 /* Changes the working directory by issuing a CWD command to the
858 ftp_cwd (int csock, const char *dir)
860 char *request, *respline;
864 /* Send CWD request. */
865 request = ftp_request ("CWD", dir);
866 nwritten = fd_write (csock, request, strlen (request), -1);
873 /* Get appropriate response. */
874 err = ftp_response (csock, &respline);
877 if (*respline == '5')
882 if (*respline != '2')
892 /* Sends REST command to the FTP server. */
894 ftp_rest (int csock, wgint offset)
896 char *request, *respline;
900 request = ftp_request ("REST", number_to_static_string (offset));
901 nwritten = fd_write (csock, request, strlen (request), -1);
908 /* Get appropriate response. */
909 err = ftp_response (csock, &respline);
912 if (*respline != '3')
922 /* Sends RETR command to the FTP server. */
924 ftp_retr (int csock, const char *file)
926 char *request, *respline;
930 /* Send RETR request. */
931 request = ftp_request ("RETR", file);
932 nwritten = fd_write (csock, request, strlen (request), -1);
939 /* Get appropriate response. */
940 err = ftp_response (csock, &respline);
943 if (*respline == '5')
948 if (*respline != '1')
958 /* Sends the LIST command to the server. If FILE is NULL, send just
959 `LIST' (no space). */
961 ftp_list (int csock, const char *file)
963 char *request, *respline;
968 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
969 const char *list_commands[] = { "LIST -a",
974 request = ftp_request (list_commands[i], file);
975 nwritten = fd_write (csock, request, strlen (request), -1);
982 /* Get appropriate response. */
983 err = ftp_response (csock, &respline);
986 if (*respline == '5')
990 else if (*respline == '1')
1002 } while (i < countof (list_commands) && !ok);
1007 /* Sends the SYST command to the server. */
1009 ftp_syst (int csock, enum stype *server_type)
1011 char *request, *respline;
1015 /* Send SYST request. */
1016 request = ftp_request ("SYST", NULL);
1017 nwritten = fd_write (csock, request, strlen (request), -1);
1025 /* Get appropriate response. */
1026 err = ftp_response (csock, &respline);
1029 if (*respline == '5')
1035 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1036 strtok (respline, " ");
1038 /* Which system type has been reported (we are interested just in the
1039 first word of the server response)? */
1040 request = strtok (NULL, " ");
1042 if (request == NULL)
1043 *server_type = ST_OTHER;
1044 else if (!strcasecmp (request, "VMS"))
1045 *server_type = ST_VMS;
1046 else if (!strcasecmp (request, "UNIX"))
1047 *server_type = ST_UNIX;
1048 else if (!strcasecmp (request, "WINDOWS_NT")
1049 || !strcasecmp (request, "WINDOWS2000"))
1050 *server_type = ST_WINNT;
1051 else if (!strcasecmp (request, "MACOS"))
1052 *server_type = ST_MACOS;
1053 else if (!strcasecmp (request, "OS/400"))
1054 *server_type = ST_OS400;
1056 *server_type = ST_OTHER;
1063 /* Sends the PWD command to the server. */
1065 ftp_pwd (int csock, char **pwd)
1067 char *request, *respline;
1071 /* Send PWD request. */
1072 request = ftp_request ("PWD", NULL);
1073 nwritten = fd_write (csock, request, strlen (request), -1);
1080 /* Get appropriate response. */
1081 err = ftp_response (csock, &respline);
1084 if (*respline == '5')
1091 /* Skip the number (257), leading citation mark, trailing citation mark
1092 and everything following it. */
1093 strtok (respline, "\"");
1094 request = strtok (NULL, "\"");
1096 /* Treat the malformed response as an error, which the caller has
1097 to handle gracefully anyway. */
1100 /* Has the `pwd' been already allocated? Free! */
1103 *pwd = xstrdup (request);
1110 /* Sends the SIZE command to the server, and returns the value in 'size'.
1111 * If an error occurs, size is set to zero. */
1113 ftp_size (int csock, const char *file, wgint *size)
1115 char *request, *respline;
1119 /* Send PWD request. */
1120 request = ftp_request ("SIZE", file);
1121 nwritten = fd_write (csock, request, strlen (request), -1);
1129 /* Get appropriate response. */
1130 err = ftp_response (csock, &respline);
1136 if (*respline == '5')
1139 * Probably means SIZE isn't supported on this server.
1140 * Error is nonfatal since SIZE isn't in RFC 959
1148 *size = str_to_wgint (respline + 4, NULL, 10);
1152 * Couldn't parse the response for some reason. On the (few)
1153 * tests I've done, the response is 213 <SIZE> with nothing else -
1154 * maybe something a bit more resilient is necessary. It's not a
1155 * fatal error, however.
1167 /* If URL's params are of the form "type=X", return character X.
1168 Otherwise, return 'I' (the default type). */
1170 ftp_process_type (const char *params)
1173 && 0 == strncasecmp (params, "type=", 5)
1174 && params[5] != '\0')
1175 return TOUPPER (params[5]);