2 Copyright (C) 1995, 1996, 1997, 1998, 2000 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
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. */
45 #include <sys/types.h>
54 char ftp_last_respline[128];
57 /* Get the response of FTP server and allocate enough room to handle
58 it. <CR> and <LF> characters are stripped from the line, and the
59 line is 0-terminated. All the response lines but the last one are
60 skipped. The last line is determined as described in RFC959.
62 If the line is successfully read, FTPOK is returned, and *ret_line
63 is assigned a freshly allocated line. Otherwise, FTPRERR is
64 returned, and the value of *ret_line should be ignored. */
67 ftp_response (int fd, char **ret_line)
72 char *line = fd_read_line (fd);
76 /* Strip trailing CRLF before printing the line, so that
77 escnonprint doesn't include bogus \012 and \015. */
78 p = strchr (line, '\0');
79 if (p > line && p[-1] == '\n')
81 if (p > line && p[-1] == '\r')
84 if (opt.server_response)
85 logprintf (LOG_NOTQUIET, "%s\n", escnonprint (line));
87 DEBUGP (("%s\n", escnonprint (line)));
89 /* The last line of output is the one that begins with "ddd ". */
90 if (ISDIGIT (line[0]) && ISDIGIT (line[1]) && ISDIGIT (line[2])
93 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
94 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
102 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
103 it if printing is required. If VALUE is NULL, just use
106 ftp_request (const char *command, const char *value)
111 /* Check for newlines in VALUE (possibly injected by the %0A URL
112 escape) making the callers inadvertently send multiple FTP
113 commands at once. Without this check an attacker could
114 intentionally redirect to ftp://server/fakedir%0Acommand.../
115 and execute arbitrary FTP command on a remote FTP server. */
116 if (strpbrk (value, "\r\n"))
118 /* Copy VALUE to the stack and modify CR/LF to space. */
120 STRDUP_ALLOCA (defanged, value);
121 for (p = defanged; *p; p++)
122 if (*p == '\r' || *p == '\n')
124 DEBUGP (("\nDetected newlines in %s \"%s\"; changing to %s \"%s\"\n",
125 command, escnonprint (value), command, escnonprint (defanged)));
126 /* Make VALUE point to the defanged copy of the string. */
129 res = concat_strings (command, " ", value, "\r\n", (char *) 0);
132 res = concat_strings (command, "\r\n", (char *) 0);
133 if (opt.server_response)
135 /* Hack: don't print out password. */
136 if (strncmp (res, "PASS", 4) != 0)
137 logprintf (LOG_ALWAYS, "--> %s\n", res);
139 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
142 DEBUGP (("\n--> %s\n", res));
146 /* Sends the USER and PASS commands to the server, to control
147 connection socket csock. */
149 ftp_login (int csock, const char *acc, const char *pass)
152 char *request, *respline;
156 err = ftp_response (csock, &respline);
159 if (*respline != '2')
165 /* Send USER username. */
166 request = ftp_request ("USER", acc);
167 nwritten = fd_write (csock, request, strlen (request), -1.0);
174 /* Get appropriate response. */
175 err = ftp_response (csock, &respline);
178 /* An unprobable possibility of logging without a password. */
179 if (*respline == '2')
184 /* Else, only response 3 is appropriate. */
185 if (*respline != '3')
188 return FTPLOGREFUSED;
192 static const char *skey_head[] = {
197 const char *seed = NULL;
199 for (i = 0; i < countof (skey_head); i++)
201 int l = strlen (skey_head[i]);
202 if (0 == strncasecmp (skey_head[i], respline, l))
210 int skey_sequence = 0;
212 /* Extract the sequence from SEED. */
213 for (; ISDIGIT (*seed); seed++)
214 skey_sequence = 10 * skey_sequence + *seed - '0';
220 return FTPLOGREFUSED;
222 /* Replace the password with the SKEY response to the
224 pass = skey_response (skey_sequence, seed, pass);
227 #endif /* ENABLE_OPIE */
229 /* Send PASS password. */
230 request = ftp_request ("PASS", pass);
231 nwritten = fd_write (csock, request, strlen (request), -1.0);
238 /* Get appropriate response. */
239 err = ftp_response (csock, &respline);
242 if (*respline != '2')
253 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
258 assert (addr != NULL);
259 assert (addr->type == IPV4_ADDRESS);
260 assert (buf != NULL);
261 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
262 assert (buflen >= 6 * 4);
264 ptr = ADDRESS_IPV4_DATA (addr);
265 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
266 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
267 buf[buflen - 1] = '\0';
270 /* Bind a port and send the appropriate PORT command to the FTP
271 server. Use acceptport after RETR, to get the socket of data
274 ftp_port (int csock, int *local_sock)
277 char *request, *respline;
281 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
282 char bytes[6 * 4 + 1];
284 /* Get the address of this side of the connection. */
285 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
288 assert (addr.type == IPV4_ADDRESS);
290 /* Setting port to 0 lets the system choose a free port. */
294 *local_sock = bind_local (&addr, &port);
298 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
299 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
301 /* Send PORT request. */
302 request = ftp_request ("PORT", bytes);
303 nwritten = fd_write (csock, request, strlen (request), -1.0);
307 fd_close (*local_sock);
312 /* Get appropriate response. */
313 err = ftp_response (csock, &respline);
316 fd_close (*local_sock);
319 if (*respline != '2')
322 fd_close (*local_sock);
331 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
336 assert (addr != NULL);
337 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
338 assert (buf != NULL);
339 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
340 assert (buflen >= 21 * 4);
342 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
346 ptr = ADDRESS_IPV4_DATA (addr);
347 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
348 ptr[0], ptr[1], ptr[2], ptr[3], 2,
349 (port & 0xff00) >> 8, port & 0xff);
350 buf[buflen - 1] = '\0';
353 ptr = ADDRESS_IPV6_DATA (addr);
354 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
355 6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
356 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
357 (port & 0xff00) >> 8, port & 0xff);
358 buf[buflen - 1] = '\0';
363 /* Bind a port and send the appropriate PORT command to the FTP
364 server. Use acceptport after RETR, to get the socket of data
367 ftp_lprt (int csock, int *local_sock)
370 char *request, *respline;
374 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
375 char bytes[21 * 4 + 1];
377 /* Get the address of this side of the connection. */
378 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
381 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
383 /* Setting port to 0 lets the system choose a free port. */
387 *local_sock = bind_local (&addr, &port);
391 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
392 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
394 /* Send PORT request. */
395 request = ftp_request ("LPRT", bytes);
396 nwritten = fd_write (csock, request, strlen (request), -1.0);
400 fd_close (*local_sock);
404 /* Get appropriate response. */
405 err = ftp_response (csock, &respline);
408 fd_close (*local_sock);
411 if (*respline != '2')
414 fd_close (*local_sock);
422 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
427 assert (addr != NULL);
428 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
429 assert (buf != NULL);
430 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
431 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
432 * 1 char for af (1-2) and 5 chars for port (0-65535) */
433 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
435 /* Construct the argument of EPRT (of the form |af|addr|port|). */
436 afnum = (addr->type == IPV4_ADDRESS ? 1 : 2);
437 snprintf (buf, buflen, "|%d|%s|%d|", afnum, pretty_print_address (addr), port);
438 buf[buflen - 1] = '\0';
441 /* Bind a port and send the appropriate PORT command to the FTP
442 server. Use acceptport after RETR, to get the socket of data
445 ftp_eprt (int csock, int *local_sock)
448 char *request, *respline;
452 /* Must contain the argument of EPRT (of the form |af|addr|port|).
453 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
454 * 1 char for af (1-2) and 5 chars for port (0-65535) */
455 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
457 /* Get the address of this side of the connection. */
458 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
461 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
463 /* Setting port to 0 lets the system choose a free port. */
467 *local_sock = bind_local (&addr, &port);
471 /* Construct the argument of EPRT (of the form |af|addr|port|). */
472 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
474 /* Send PORT request. */
475 request = ftp_request ("EPRT", bytes);
476 nwritten = fd_write (csock, request, strlen (request), -1.0);
480 fd_close (*local_sock);
484 /* Get appropriate response. */
485 err = ftp_response (csock, &respline);
488 fd_close (*local_sock);
491 if (*respline != '2')
494 fd_close (*local_sock);
502 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
503 transfer. Reads the response from server and parses it. Reads the
504 host and port addresses and returns them. */
506 ftp_pasv (int csock, ip_address *addr, int *port)
508 char *request, *respline, *s;
511 unsigned char tmp[6];
513 assert (addr != NULL);
514 assert (port != NULL);
518 /* Form the request. */
519 request = ftp_request ("PASV", NULL);
521 nwritten = fd_write (csock, request, strlen (request), -1.0);
528 /* Get the server response. */
529 err = ftp_response (csock, &respline);
532 if (*respline != '2')
537 /* Parse the request. */
539 for (s += 4; *s && !ISDIGIT (*s); s++);
542 for (i = 0; i < 6; i++)
545 for (; ISDIGIT (*s); s++)
546 tmp[i] = (*s - '0') + 10 * tmp[i];
551 /* When on the last number, anything can be a terminator. */
558 addr->type = IPV4_ADDRESS;
559 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
560 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
566 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
567 transfer. Reads the response from server and parses it. Reads the
568 host and port addresses and returns them. */
570 ftp_lpsv (int csock, ip_address *addr, int *port)
572 char *request, *respline, *s;
573 int nwritten, i, af, addrlen, portlen;
575 unsigned char tmp[16];
576 unsigned char tmpprt[2];
578 assert (addr != NULL);
579 assert (port != NULL);
583 /* Form the request. */
584 request = ftp_request ("LPSV", NULL);
587 nwritten = fd_write (csock, request, strlen (request), -1.0);
595 /* Get the server response. */
596 err = ftp_response (csock, &respline);
599 if (*respline != '2')
605 /* Parse the response. */
607 for (s += 4; *s && !ISDIGIT (*s); s++);
611 /* First, get the address family */
613 for (; ISDIGIT (*s); s++)
614 af = (*s - '0') + 10 * af;
616 if (af != 4 && af != 6)
622 if (!*s || *s++ != ',')
628 /* Then, get the address length */
630 for (; ISDIGIT (*s); s++)
631 addrlen = (*s - '0') + 10 * addrlen;
633 if (!*s || *s++ != ',')
645 if ((af == 4 && addrlen != 4)
646 || (af == 6 && addrlen != 16))
652 /* Now, we get the actual address */
653 for (i = 0; i < addrlen; i++)
656 for (; ISDIGIT (*s); s++)
657 tmp[i] = (*s - '0') + 10 * tmp[i];
667 /* Now, get the port length */
669 for (; ISDIGIT (*s); s++)
670 portlen = (*s - '0') + 10 * portlen;
672 if (!*s || *s++ != ',')
684 /* Finally, we get the port number */
686 for (; ISDIGIT (*s); s++)
687 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
689 if (!*s || *s++ != ',')
696 for (; ISDIGIT (*s); s++)
697 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
703 addr->type = IPV4_ADDRESS;
704 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
705 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
706 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
707 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
708 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
709 DEBUGP (("*port is: %d\n", *port));
714 addr->type = IPV6_ADDRESS;
715 memcpy (ADDRESS_IPV6_DATA (addr), tmp, 16);
716 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
717 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
718 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
719 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
720 DEBUGP (("*port is: %d\n", *port));
727 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
728 transfer. Reads the response from server and parses it. Reads the
729 host and port addresses and returns them. */
731 ftp_epsv (int csock, ip_address *ip, int *port)
733 char *request, *respline, *start, delim, *s;
739 assert (port != NULL);
741 /* IP already contains the IP address of the control connection's
742 peer, so we don't need to call socket_ip_address here. */
744 /* Form the request. */
745 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
746 request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
749 nwritten = fd_write (csock, request, strlen (request), -1.0);
757 /* Get the server response. */
758 err = ftp_response (csock, &respline);
761 if (*respline != '2')
767 assert (respline != NULL);
769 DEBUGP(("respline is %s\n", respline));
771 /* Parse the response. */
774 /* Skip the useless stuff and get what's inside the parentheses */
775 start = strchr (respline, '(');
782 /* Skip the first two void fields */
785 if (delim < 33 || delim > 126)
791 for (i = 0; i < 2; i++)
800 /* Finally, get the port number */
802 for (i = 1; ISDIGIT (*s); s++)
809 tport = (*s - '0') + 10 * tport;
812 /* Make sure that the response terminates correcty */
832 /* Sends the TYPE request to the server. */
834 ftp_type (int csock, int type)
836 char *request, *respline;
841 /* Construct argument. */
844 /* Send TYPE request. */
845 request = ftp_request ("TYPE", stype);
846 nwritten = fd_write (csock, request, strlen (request), -1.0);
853 /* Get appropriate response. */
854 err = ftp_response (csock, &respline);
857 if (*respline != '2')
860 return FTPUNKNOWNTYPE;
867 /* Changes the working directory by issuing a CWD command to the
870 ftp_cwd (int csock, const char *dir)
872 char *request, *respline;
876 /* Send CWD request. */
877 request = ftp_request ("CWD", dir);
878 nwritten = fd_write (csock, request, strlen (request), -1.0);
885 /* Get appropriate response. */
886 err = ftp_response (csock, &respline);
889 if (*respline == '5')
894 if (*respline != '2')
904 /* Sends REST command to the FTP server. */
906 ftp_rest (int csock, wgint offset)
908 char *request, *respline;
912 request = ftp_request ("REST", number_to_static_string (offset));
913 nwritten = fd_write (csock, request, strlen (request), -1.0);
920 /* Get appropriate response. */
921 err = ftp_response (csock, &respline);
924 if (*respline != '3')
934 /* Sends RETR command to the FTP server. */
936 ftp_retr (int csock, const char *file)
938 char *request, *respline;
942 /* Send RETR request. */
943 request = ftp_request ("RETR", file);
944 nwritten = fd_write (csock, request, strlen (request), -1.0);
951 /* Get appropriate response. */
952 err = ftp_response (csock, &respline);
955 if (*respline == '5')
960 if (*respline != '1')
970 /* Sends the LIST command to the server. If FILE is NULL, send just
971 `LIST' (no space). */
973 ftp_list (int csock, const char *file)
975 char *request, *respline;
979 /* Send LIST request. */
980 request = ftp_request ("LIST", file);
981 nwritten = fd_write (csock, request, strlen (request), -1.0);
988 /* Get appropriate respone. */
989 err = ftp_response (csock, &respline);
992 if (*respline == '5')
997 if (*respline != '1')
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.0);
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 (!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.0);
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.0);
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 TOUPPER (params[5]);