2 Copyright (C) 1996-2007 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
49 char ftp_last_respline[128];
52 /* Get the response of FTP server and allocate enough room to handle
53 it. <CR> and <LF> characters are stripped from the line, and the
54 line is 0-terminated. All the response lines but the last one are
55 skipped. The last line is determined as described in RFC959.
57 If the line is successfully read, FTPOK is returned, and *ret_line
58 is assigned a freshly allocated line. Otherwise, FTPRERR is
59 returned, and the value of *ret_line should be ignored. */
62 ftp_response (int fd, char **ret_line)
67 char *line = fd_read_line (fd);
71 /* Strip trailing CRLF before printing the line, so that
72 escnonprint doesn't include bogus \012 and \015. */
73 p = strchr (line, '\0');
74 if (p > line && p[-1] == '\n')
76 if (p > line && p[-1] == '\r')
79 if (opt.server_response)
80 logprintf (LOG_NOTQUIET, "%s\n", escnonprint (line));
82 DEBUGP (("%s\n", escnonprint (line)));
84 /* The last line of output is the one that begins with "ddd ". */
85 if (ISDIGIT (line[0]) && ISDIGIT (line[1]) && 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, escnonprint (value), command, escnonprint (defanged)));
121 /* Make VALUE point to the defanged copy of the string. */
124 res = concat_strings (command, " ", value, "\r\n", (char *) 0);
127 res = concat_strings (command, "\r\n", (char *) 0);
128 if (opt.server_response)
130 /* Hack: don't print out password. */
131 if (strncmp (res, "PASS", 4) != 0)
132 logprintf (LOG_ALWAYS, "--> %s\n", res);
134 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
137 DEBUGP (("\n--> %s\n", res));
141 /* Sends the USER and PASS commands to the server, to control
142 connection socket csock. */
144 ftp_login (int csock, const char *acc, const char *pass)
147 char *request, *respline;
151 err = ftp_response (csock, &respline);
154 if (*respline != '2')
160 /* Send USER username. */
161 request = ftp_request ("USER", acc);
162 nwritten = fd_write (csock, request, strlen (request), -1);
169 /* Get appropriate response. */
170 err = ftp_response (csock, &respline);
173 /* An unprobable possibility of logging without a password. */
174 if (*respline == '2')
179 /* Else, only response 3 is appropriate. */
180 if (*respline != '3')
183 return FTPLOGREFUSED;
187 static const char *skey_head[] = {
192 const char *seed = NULL;
194 for (i = 0; i < countof (skey_head); i++)
196 int l = strlen (skey_head[i]);
197 if (0 == strncasecmp (skey_head[i], respline, l))
205 int skey_sequence = 0;
207 /* Extract the sequence from SEED. */
208 for (; ISDIGIT (*seed); seed++)
209 skey_sequence = 10 * skey_sequence + *seed - '0';
215 return FTPLOGREFUSED;
217 /* Replace the password with the SKEY response to the
219 pass = skey_response (skey_sequence, seed, pass);
222 #endif /* ENABLE_OPIE */
224 /* Send PASS password. */
225 request = ftp_request ("PASS", pass);
226 nwritten = fd_write (csock, request, strlen (request), -1);
233 /* Get appropriate response. */
234 err = ftp_response (csock, &respline);
237 if (*respline != '2')
248 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
253 assert (addr->family == AF_INET);
254 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
255 assert (buflen >= 6 * 4);
257 ptr = IP_INADDR_DATA (addr);
258 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
259 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
260 buf[buflen - 1] = '\0';
263 /* Bind a port and send the appropriate PORT command to the FTP
264 server. Use acceptport after RETR, to get the socket of data
267 ftp_port (int csock, int *local_sock)
270 char *request, *respline;
274 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
275 char bytes[6 * 4 + 1];
277 /* Get the address of this side of the connection. */
278 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
281 assert (addr.family == AF_INET);
283 /* Setting port to 0 lets the system choose a free port. */
287 *local_sock = bind_local (&addr, &port);
291 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
292 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
294 /* Send PORT request. */
295 request = ftp_request ("PORT", bytes);
296 nwritten = fd_write (csock, request, strlen (request), -1);
300 fd_close (*local_sock);
305 /* Get appropriate response. */
306 err = ftp_response (csock, &respline);
309 fd_close (*local_sock);
312 if (*respline != '2')
315 fd_close (*local_sock);
324 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
327 unsigned char *ptr = IP_INADDR_DATA (addr);
329 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
330 assert (buflen >= 21 * 4);
332 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
333 switch (addr->family)
336 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
337 ptr[0], ptr[1], ptr[2], ptr[3], 2,
338 (port & 0xff00) >> 8, port & 0xff);
341 snprintf (buf, buflen,
342 "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
344 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
345 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15],
346 2, (port & 0xff00) >> 8, port & 0xff);
353 /* Bind a port and send the appropriate PORT command to the FTP
354 server. Use acceptport after RETR, to get the socket of data
357 ftp_lprt (int csock, int *local_sock)
360 char *request, *respline;
364 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
365 char bytes[21 * 4 + 1];
367 /* Get the address of this side of the connection. */
368 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
371 assert (addr.family == AF_INET || addr.family == AF_INET6);
373 /* Setting port to 0 lets the system choose a free port. */
377 *local_sock = bind_local (&addr, &port);
381 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
382 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
384 /* Send PORT request. */
385 request = ftp_request ("LPRT", bytes);
386 nwritten = fd_write (csock, request, strlen (request), -1);
390 fd_close (*local_sock);
394 /* Get appropriate response. */
395 err = ftp_response (csock, &respline);
398 fd_close (*local_sock);
401 if (*respline != '2')
404 fd_close (*local_sock);
412 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
417 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
418 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
419 * 1 char for af (1-2) and 5 chars for port (0-65535) */
420 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
422 /* Construct the argument of EPRT (of the form |af|addr|port|). */
423 afnum = (addr->family == AF_INET ? 1 : 2);
424 snprintf (buf, buflen, "|%d|%s|%d|", afnum, print_address (addr), port);
425 buf[buflen - 1] = '\0';
428 /* Bind a port and send the appropriate PORT command to the FTP
429 server. Use acceptport after RETR, to get the socket of data
432 ftp_eprt (int csock, int *local_sock)
435 char *request, *respline;
439 /* Must contain the argument of EPRT (of the form |af|addr|port|).
440 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
441 * 1 char for af (1-2) and 5 chars for port (0-65535) */
442 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
444 /* Get the address of this side of the connection. */
445 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
448 /* Setting port to 0 lets the system choose a free port. */
452 *local_sock = bind_local (&addr, &port);
456 /* Construct the argument of EPRT (of the form |af|addr|port|). */
457 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
459 /* Send PORT request. */
460 request = ftp_request ("EPRT", bytes);
461 nwritten = fd_write (csock, request, strlen (request), -1);
465 fd_close (*local_sock);
469 /* Get appropriate response. */
470 err = ftp_response (csock, &respline);
473 fd_close (*local_sock);
476 if (*respline != '2')
479 fd_close (*local_sock);
487 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
488 transfer. Reads the response from server and parses it. Reads the
489 host and port addresses and returns them. */
491 ftp_pasv (int csock, ip_address *addr, int *port)
493 char *request, *respline, *s;
496 unsigned char tmp[6];
498 assert (addr != NULL);
499 assert (port != NULL);
503 /* Form the request. */
504 request = ftp_request ("PASV", NULL);
506 nwritten = fd_write (csock, request, strlen (request), -1);
513 /* Get the server response. */
514 err = ftp_response (csock, &respline);
517 if (*respline != '2')
522 /* Parse the request. */
524 for (s += 4; *s && !ISDIGIT (*s); s++)
528 for (i = 0; i < 6; i++)
531 for (; ISDIGIT (*s); s++)
532 tmp[i] = (*s - '0') + 10 * tmp[i];
537 /* When on the last number, anything can be a terminator. */
544 addr->family = AF_INET;
545 memcpy (IP_INADDR_DATA (addr), tmp, 4);
546 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
552 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
553 transfer. Reads the response from server and parses it. Reads the
554 host and port addresses and returns them. */
556 ftp_lpsv (int csock, ip_address *addr, int *port)
558 char *request, *respline, *s;
559 int nwritten, i, af, addrlen, portlen;
561 unsigned char tmp[16];
562 unsigned char tmpprt[2];
564 assert (addr != NULL);
565 assert (port != NULL);
569 /* Form the request. */
570 request = ftp_request ("LPSV", NULL);
573 nwritten = fd_write (csock, request, strlen (request), -1);
581 /* Get the server response. */
582 err = ftp_response (csock, &respline);
585 if (*respline != '2')
591 /* Parse the response. */
593 for (s += 4; *s && !ISDIGIT (*s); s++)
598 /* First, get the address family */
600 for (; ISDIGIT (*s); s++)
601 af = (*s - '0') + 10 * af;
603 if (af != 4 && af != 6)
609 if (!*s || *s++ != ',')
615 /* Then, get the address length */
617 for (; ISDIGIT (*s); s++)
618 addrlen = (*s - '0') + 10 * addrlen;
620 if (!*s || *s++ != ',')
632 if ((af == 4 && addrlen != 4)
633 || (af == 6 && addrlen != 16))
639 /* Now, we get the actual address */
640 for (i = 0; i < addrlen; i++)
643 for (; ISDIGIT (*s); s++)
644 tmp[i] = (*s - '0') + 10 * tmp[i];
654 /* Now, get the port length */
656 for (; ISDIGIT (*s); s++)
657 portlen = (*s - '0') + 10 * portlen;
659 if (!*s || *s++ != ',')
671 /* Finally, we get the port number */
673 for (; ISDIGIT (*s); s++)
674 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
676 if (!*s || *s++ != ',')
683 for (; ISDIGIT (*s); s++)
684 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
690 addr->family = AF_INET;
691 memcpy (IP_INADDR_DATA (addr), tmp, 4);
692 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
693 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
694 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
695 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
696 DEBUGP (("*port is: %d\n", *port));
701 addr->family = AF_INET6;
702 memcpy (IP_INADDR_DATA (addr), tmp, 16);
703 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
704 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
705 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
706 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
707 DEBUGP (("*port is: %d\n", *port));
714 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
715 transfer. Reads the response from server and parses it. Reads the
716 host and port addresses and returns them. */
718 ftp_epsv (int csock, ip_address *ip, int *port)
720 char *request, *respline, *start, delim, *s;
726 assert (port != NULL);
728 /* IP already contains the IP address of the control connection's
729 peer, so we don't need to call socket_ip_address here. */
731 /* Form the request. */
732 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
733 request = ftp_request ("EPSV", (ip->family == AF_INET ? "1" : "2"));
736 nwritten = fd_write (csock, request, strlen (request), -1);
744 /* Get the server response. */
745 err = ftp_response (csock, &respline);
748 if (*respline != '2')
754 assert (respline != NULL);
756 DEBUGP(("respline is %s\n", respline));
758 /* Parse the response. */
761 /* Skip the useless stuff and get what's inside the parentheses */
762 start = strchr (respline, '(');
769 /* Skip the first two void fields */
772 if (delim < 33 || delim > 126)
778 for (i = 0; i < 2; i++)
787 /* Finally, get the port number */
789 for (i = 1; ISDIGIT (*s); s++)
796 tport = (*s - '0') + 10 * tport;
799 /* Make sure that the response terminates correcty */
819 /* Sends the TYPE request to the server. */
821 ftp_type (int csock, int type)
823 char *request, *respline;
828 /* Construct argument. */
831 /* Send TYPE request. */
832 request = ftp_request ("TYPE", stype);
833 nwritten = fd_write (csock, request, strlen (request), -1);
840 /* Get appropriate response. */
841 err = ftp_response (csock, &respline);
844 if (*respline != '2')
847 return FTPUNKNOWNTYPE;
854 /* Changes the working directory by issuing a CWD command to the
857 ftp_cwd (int csock, const char *dir)
859 char *request, *respline;
863 /* Send CWD request. */
864 request = ftp_request ("CWD", dir);
865 nwritten = fd_write (csock, request, strlen (request), -1);
872 /* Get appropriate response. */
873 err = ftp_response (csock, &respline);
876 if (*respline == '5')
881 if (*respline != '2')
891 /* Sends REST command to the FTP server. */
893 ftp_rest (int csock, wgint offset)
895 char *request, *respline;
899 request = ftp_request ("REST", number_to_static_string (offset));
900 nwritten = fd_write (csock, request, strlen (request), -1);
907 /* Get appropriate response. */
908 err = ftp_response (csock, &respline);
911 if (*respline != '3')
921 /* Sends RETR command to the FTP server. */
923 ftp_retr (int csock, const char *file)
925 char *request, *respline;
929 /* Send RETR request. */
930 request = ftp_request ("RETR", file);
931 nwritten = fd_write (csock, request, strlen (request), -1);
938 /* Get appropriate response. */
939 err = ftp_response (csock, &respline);
942 if (*respline == '5')
947 if (*respline != '1')
957 /* Sends the LIST command to the server. If FILE is NULL, send just
958 `LIST' (no space). */
960 ftp_list (int csock, const char *file)
962 char *request, *respline;
967 /* Try `LIST -a' first and revert to `LIST' in case of failure. */
968 const char *list_commands[] = { "LIST -a",
973 request = ftp_request (list_commands[i], file);
974 nwritten = fd_write (csock, request, strlen (request), -1);
981 /* Get appropriate response. */
982 err = ftp_response (csock, &respline);
985 if (*respline == '5')
989 else if (*respline == '1')
1001 } while (i < countof (list_commands) && !ok);
1006 /* Sends the SYST command to the server. */
1008 ftp_syst (int csock, enum stype *server_type)
1010 char *request, *respline;
1014 /* Send SYST request. */
1015 request = ftp_request ("SYST", NULL);
1016 nwritten = fd_write (csock, request, strlen (request), -1);
1024 /* Get appropriate response. */
1025 err = ftp_response (csock, &respline);
1028 if (*respline == '5')
1034 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1035 strtok (respline, " ");
1037 /* Which system type has been reported (we are interested just in the
1038 first word of the server response)? */
1039 request = strtok (NULL, " ");
1041 if (request == NULL)
1042 *server_type = ST_OTHER;
1043 else if (!strcasecmp (request, "VMS"))
1044 *server_type = ST_VMS;
1045 else if (!strcasecmp (request, "UNIX"))
1046 *server_type = ST_UNIX;
1047 else if (!strcasecmp (request, "WINDOWS_NT")
1048 || !strcasecmp (request, "WINDOWS2000"))
1049 *server_type = ST_WINNT;
1050 else if (!strcasecmp (request, "MACOS"))
1051 *server_type = ST_MACOS;
1052 else if (!strcasecmp (request, "OS/400"))
1053 *server_type = ST_OS400;
1055 *server_type = ST_OTHER;
1062 /* Sends the PWD command to the server. */
1064 ftp_pwd (int csock, char **pwd)
1066 char *request, *respline;
1070 /* Send PWD request. */
1071 request = ftp_request ("PWD", NULL);
1072 nwritten = fd_write (csock, request, strlen (request), -1);
1079 /* Get appropriate response. */
1080 err = ftp_response (csock, &respline);
1083 if (*respline == '5')
1090 /* Skip the number (257), leading citation mark, trailing citation mark
1091 and everything following it. */
1092 strtok (respline, "\"");
1093 request = strtok (NULL, "\"");
1095 /* Treat the malformed response as an error, which the caller has
1096 to handle gracefully anyway. */
1099 /* Has the `pwd' been already allocated? Free! */
1102 *pwd = xstrdup (request);
1109 /* Sends the SIZE command to the server, and returns the value in 'size'.
1110 * If an error occurs, size is set to zero. */
1112 ftp_size (int csock, const char *file, wgint *size)
1114 char *request, *respline;
1118 /* Send PWD request. */
1119 request = ftp_request ("SIZE", file);
1120 nwritten = fd_write (csock, request, strlen (request), -1);
1128 /* Get appropriate response. */
1129 err = ftp_response (csock, &respline);
1135 if (*respline == '5')
1138 * Probably means SIZE isn't supported on this server.
1139 * Error is nonfatal since SIZE isn't in RFC 959
1147 *size = str_to_wgint (respline + 4, NULL, 10);
1151 * Couldn't parse the response for some reason. On the (few)
1152 * tests I've done, the response is 213 <SIZE> with nothing else -
1153 * maybe something a bit more resilient is necessary. It's not a
1154 * fatal error, however.
1166 /* If URL's params are of the form "type=X", return character X.
1167 Otherwise, return 'I' (the default type). */
1169 ftp_process_type (const char *params)
1172 && 0 == strncasecmp (params, "type=", 5)
1173 && params[5] != '\0')
1174 return TOUPPER (params[5]);