2 Copyright (C) 1996-2005 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 != NULL);
254 assert (addr->type == IPV4_ADDRESS);
255 assert (buf != NULL);
256 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
257 assert (buflen >= 6 * 4);
259 ptr = ADDRESS_IPV4_DATA (addr);
260 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
261 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
262 buf[buflen - 1] = '\0';
265 /* Bind a port and send the appropriate PORT command to the FTP
266 server. Use acceptport after RETR, to get the socket of data
269 ftp_port (int csock, int *local_sock)
272 char *request, *respline;
276 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
277 char bytes[6 * 4 + 1];
279 /* Get the address of this side of the connection. */
280 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
283 assert (addr.type == IPV4_ADDRESS);
285 /* Setting port to 0 lets the system choose a free port. */
289 *local_sock = bind_local (&addr, &port);
293 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
294 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
296 /* Send PORT request. */
297 request = ftp_request ("PORT", bytes);
298 nwritten = fd_write (csock, request, strlen (request), -1);
302 fd_close (*local_sock);
307 /* Get appropriate response. */
308 err = ftp_response (csock, &respline);
311 fd_close (*local_sock);
314 if (*respline != '2')
317 fd_close (*local_sock);
326 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
331 assert (addr != NULL);
332 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
333 assert (buf != NULL);
334 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
335 assert (buflen >= 21 * 4);
337 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
341 ptr = ADDRESS_IPV4_DATA (addr);
342 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
343 ptr[0], ptr[1], ptr[2], ptr[3], 2,
344 (port & 0xff00) >> 8, port & 0xff);
345 buf[buflen - 1] = '\0';
348 ptr = ADDRESS_IPV6_DATA (addr);
349 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
350 6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
351 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
352 (port & 0xff00) >> 8, port & 0xff);
353 buf[buflen - 1] = '\0';
358 /* Bind a port and send the appropriate PORT command to the FTP
359 server. Use acceptport after RETR, to get the socket of data
362 ftp_lprt (int csock, int *local_sock)
365 char *request, *respline;
369 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
370 char bytes[21 * 4 + 1];
372 /* Get the address of this side of the connection. */
373 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
376 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
378 /* Setting port to 0 lets the system choose a free port. */
382 *local_sock = bind_local (&addr, &port);
386 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
387 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
389 /* Send PORT request. */
390 request = ftp_request ("LPRT", bytes);
391 nwritten = fd_write (csock, request, strlen (request), -1);
395 fd_close (*local_sock);
399 /* Get appropriate response. */
400 err = ftp_response (csock, &respline);
403 fd_close (*local_sock);
406 if (*respline != '2')
409 fd_close (*local_sock);
417 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
422 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
423 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
424 * 1 char for af (1-2) and 5 chars for port (0-65535) */
425 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
427 /* Construct the argument of EPRT (of the form |af|addr|port|). */
428 afnum = (addr->type == IPV4_ADDRESS ? 1 : 2);
429 snprintf (buf, buflen, "|%d|%s|%d|", afnum, print_address (addr), port);
430 buf[buflen - 1] = '\0';
433 /* Bind a port and send the appropriate PORT command to the FTP
434 server. Use acceptport after RETR, to get the socket of data
437 ftp_eprt (int csock, int *local_sock)
440 char *request, *respline;
444 /* Must contain the argument of EPRT (of the form |af|addr|port|).
445 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
446 * 1 char for af (1-2) and 5 chars for port (0-65535) */
447 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
449 /* Get the address of this side of the connection. */
450 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
453 /* Setting port to 0 lets the system choose a free port. */
457 *local_sock = bind_local (&addr, &port);
461 /* Construct the argument of EPRT (of the form |af|addr|port|). */
462 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
464 /* Send PORT request. */
465 request = ftp_request ("EPRT", bytes);
466 nwritten = fd_write (csock, request, strlen (request), -1);
470 fd_close (*local_sock);
474 /* Get appropriate response. */
475 err = ftp_response (csock, &respline);
478 fd_close (*local_sock);
481 if (*respline != '2')
484 fd_close (*local_sock);
492 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
493 transfer. Reads the response from server and parses it. Reads the
494 host and port addresses and returns them. */
496 ftp_pasv (int csock, ip_address *addr, int *port)
498 char *request, *respline, *s;
501 unsigned char tmp[6];
503 assert (addr != NULL);
504 assert (port != NULL);
508 /* Form the request. */
509 request = ftp_request ("PASV", NULL);
511 nwritten = fd_write (csock, request, strlen (request), -1);
518 /* Get the server response. */
519 err = ftp_response (csock, &respline);
522 if (*respline != '2')
527 /* Parse the request. */
529 for (s += 4; *s && !ISDIGIT (*s); s++)
533 for (i = 0; i < 6; i++)
536 for (; ISDIGIT (*s); s++)
537 tmp[i] = (*s - '0') + 10 * tmp[i];
542 /* When on the last number, anything can be a terminator. */
549 addr->type = IPV4_ADDRESS;
550 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
551 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
557 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
558 transfer. Reads the response from server and parses it. Reads the
559 host and port addresses and returns them. */
561 ftp_lpsv (int csock, ip_address *addr, int *port)
563 char *request, *respline, *s;
564 int nwritten, i, af, addrlen, portlen;
566 unsigned char tmp[16];
567 unsigned char tmpprt[2];
569 assert (addr != NULL);
570 assert (port != NULL);
574 /* Form the request. */
575 request = ftp_request ("LPSV", NULL);
578 nwritten = fd_write (csock, request, strlen (request), -1);
586 /* Get the server response. */
587 err = ftp_response (csock, &respline);
590 if (*respline != '2')
596 /* Parse the response. */
598 for (s += 4; *s && !ISDIGIT (*s); s++)
603 /* First, get the address family */
605 for (; ISDIGIT (*s); s++)
606 af = (*s - '0') + 10 * af;
608 if (af != 4 && af != 6)
614 if (!*s || *s++ != ',')
620 /* Then, get the address length */
622 for (; ISDIGIT (*s); s++)
623 addrlen = (*s - '0') + 10 * addrlen;
625 if (!*s || *s++ != ',')
637 if ((af == 4 && addrlen != 4)
638 || (af == 6 && addrlen != 16))
644 /* Now, we get the actual address */
645 for (i = 0; i < addrlen; i++)
648 for (; ISDIGIT (*s); s++)
649 tmp[i] = (*s - '0') + 10 * tmp[i];
659 /* Now, get the port length */
661 for (; ISDIGIT (*s); s++)
662 portlen = (*s - '0') + 10 * portlen;
664 if (!*s || *s++ != ',')
676 /* Finally, we get the port number */
678 for (; ISDIGIT (*s); s++)
679 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
681 if (!*s || *s++ != ',')
688 for (; ISDIGIT (*s); s++)
689 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
695 addr->type = IPV4_ADDRESS;
696 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
697 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
698 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
699 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
700 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
701 DEBUGP (("*port is: %d\n", *port));
706 addr->type = IPV6_ADDRESS;
707 memcpy (ADDRESS_IPV6_DATA (addr), tmp, 16);
708 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
709 DEBUGP (("lpsv addr is: %s\n", print_address(addr)));
710 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
711 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
712 DEBUGP (("*port is: %d\n", *port));
719 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
720 transfer. Reads the response from server and parses it. Reads the
721 host and port addresses and returns them. */
723 ftp_epsv (int csock, ip_address *ip, int *port)
725 char *request, *respline, *start, delim, *s;
731 assert (port != NULL);
733 /* IP already contains the IP address of the control connection's
734 peer, so we don't need to call socket_ip_address here. */
736 /* Form the request. */
737 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
738 request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
741 nwritten = fd_write (csock, request, strlen (request), -1);
749 /* Get the server response. */
750 err = ftp_response (csock, &respline);
753 if (*respline != '2')
759 assert (respline != NULL);
761 DEBUGP(("respline is %s\n", respline));
763 /* Parse the response. */
766 /* Skip the useless stuff and get what's inside the parentheses */
767 start = strchr (respline, '(');
774 /* Skip the first two void fields */
777 if (delim < 33 || delim > 126)
783 for (i = 0; i < 2; i++)
792 /* Finally, get the port number */
794 for (i = 1; ISDIGIT (*s); s++)
801 tport = (*s - '0') + 10 * tport;
804 /* Make sure that the response terminates correcty */
824 /* Sends the TYPE request to the server. */
826 ftp_type (int csock, int type)
828 char *request, *respline;
833 /* Construct argument. */
836 /* Send TYPE request. */
837 request = ftp_request ("TYPE", stype);
838 nwritten = fd_write (csock, request, strlen (request), -1);
845 /* Get appropriate response. */
846 err = ftp_response (csock, &respline);
849 if (*respline != '2')
852 return FTPUNKNOWNTYPE;
859 /* Changes the working directory by issuing a CWD command to the
862 ftp_cwd (int csock, const char *dir)
864 char *request, *respline;
868 /* Send CWD request. */
869 request = ftp_request ("CWD", dir);
870 nwritten = fd_write (csock, request, strlen (request), -1);
877 /* Get appropriate response. */
878 err = ftp_response (csock, &respline);
881 if (*respline == '5')
886 if (*respline != '2')
896 /* Sends REST command to the FTP server. */
898 ftp_rest (int csock, wgint offset)
900 char *request, *respline;
904 request = ftp_request ("REST", number_to_static_string (offset));
905 nwritten = fd_write (csock, request, strlen (request), -1);
912 /* Get appropriate response. */
913 err = ftp_response (csock, &respline);
916 if (*respline != '3')
926 /* Sends RETR command to the FTP server. */
928 ftp_retr (int csock, const char *file)
930 char *request, *respline;
934 /* Send RETR request. */
935 request = ftp_request ("RETR", file);
936 nwritten = fd_write (csock, request, strlen (request), -1);
943 /* Get appropriate response. */
944 err = ftp_response (csock, &respline);
947 if (*respline == '5')
952 if (*respline != '1')
962 /* Sends the LIST command to the server. If FILE is NULL, send just
963 `LIST' (no space). */
965 ftp_list (int csock, const char *file)
967 char *request, *respline;
971 /* Send LIST request. */
972 request = ftp_request ("LIST", file);
973 nwritten = fd_write (csock, request, strlen (request), -1);
980 /* Get appropriate respone. */
981 err = ftp_response (csock, &respline);
984 if (*respline == '5')
989 if (*respline != '1')
999 /* Sends the SYST command to the server. */
1001 ftp_syst (int csock, enum stype *server_type)
1003 char *request, *respline;
1007 /* Send SYST request. */
1008 request = ftp_request ("SYST", NULL);
1009 nwritten = fd_write (csock, request, strlen (request), -1);
1017 /* Get appropriate response. */
1018 err = ftp_response (csock, &respline);
1021 if (*respline == '5')
1027 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1028 strtok (respline, " ");
1030 /* Which system type has been reported (we are interested just in the
1031 first word of the server response)? */
1032 request = strtok (NULL, " ");
1034 if (!strcasecmp (request, "VMS"))
1035 *server_type = ST_VMS;
1036 else if (!strcasecmp (request, "UNIX"))
1037 *server_type = ST_UNIX;
1038 else if (!strcasecmp (request, "WINDOWS_NT")
1039 || !strcasecmp (request, "WINDOWS2000"))
1040 *server_type = ST_WINNT;
1041 else if (!strcasecmp (request, "MACOS"))
1042 *server_type = ST_MACOS;
1043 else if (!strcasecmp (request, "OS/400"))
1044 *server_type = ST_OS400;
1046 *server_type = ST_OTHER;
1053 /* Sends the PWD command to the server. */
1055 ftp_pwd (int csock, char **pwd)
1057 char *request, *respline;
1061 /* Send PWD request. */
1062 request = ftp_request ("PWD", NULL);
1063 nwritten = fd_write (csock, request, strlen (request), -1);
1070 /* Get appropriate response. */
1071 err = ftp_response (csock, &respline);
1074 if (*respline == '5')
1081 /* Skip the number (257), leading citation mark, trailing citation mark
1082 and everything following it. */
1083 strtok (respline, "\"");
1084 request = strtok (NULL, "\"");
1086 /* Treat the malformed response as an error, which the caller has
1087 to handle gracefully anyway. */
1090 /* Has the `pwd' been already allocated? Free! */
1093 *pwd = xstrdup (request);
1100 /* Sends the SIZE command to the server, and returns the value in 'size'.
1101 * If an error occurs, size is set to zero. */
1103 ftp_size (int csock, const char *file, wgint *size)
1105 char *request, *respline;
1109 /* Send PWD request. */
1110 request = ftp_request ("SIZE", file);
1111 nwritten = fd_write (csock, request, strlen (request), -1);
1119 /* Get appropriate response. */
1120 err = ftp_response (csock, &respline);
1126 if (*respline == '5')
1129 * Probably means SIZE isn't supported on this server.
1130 * Error is nonfatal since SIZE isn't in RFC 959
1138 *size = str_to_wgint (respline + 4, NULL, 10);
1142 * Couldn't parse the response for some reason. On the (few)
1143 * tests I've done, the response is 213 <SIZE> with nothing else -
1144 * maybe something a bit more resilient is necessary. It's not a
1145 * fatal error, however.
1157 /* If URL's params are of the form "type=X", return character X.
1158 Otherwise, return 'I' (the default type). */
1160 ftp_process_type (const char *params)
1163 && 0 == strncasecmp (params, "type=", 5)
1164 && params[5] != '\0')
1165 return TOUPPER (params[5]);