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 ftp_response (struct rbuf *rbuf, char **line)
67 *line = (char *)xmalloc (bufsize);
74 *line = (char *)xrealloc (*line, (bufsize <<= 1));
75 res = RBUF_READCHAR (rbuf, *line + i);
76 /* RES is number of bytes read. */
79 if ((*line)[i] == '\n')
83 if (i > 0 && (*line)[i - 1] == '\r')
84 (*line)[i - 1] = '\0';
91 if (opt.server_response)
92 logprintf (LOG_ALWAYS, "%s\n", *line);
94 DEBUGP (("%s\n", *line));
96 while (!(i >= 3 && ISDIGIT (**line) && ISDIGIT ((*line)[1]) &&
97 ISDIGIT ((*line)[2]) && (*line)[3] == ' '));
98 strncpy (ftp_last_respline, *line, sizeof (ftp_last_respline));
99 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
103 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
104 it if printing is required. If VALUE is NULL, just use
107 ftp_request (const char *command, const char *value)
109 char *res = (char *)xmalloc (strlen (command)
110 + (value ? (1 + strlen (value)) : 0)
112 sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
113 if (opt.server_response)
115 /* Hack: don't print out password. */
116 if (strncmp (res, "PASS", 4) != 0)
117 logprintf (LOG_ALWAYS, "--> %s\n", res);
119 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
122 DEBUGP (("\n--> %s\n", res));
126 /* Sends the USER and PASS commands to the server, to control
127 connection socket csock. */
129 ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
132 char *request, *respline;
136 err = ftp_response (rbuf, &respline);
142 if (*respline != '2')
148 /* Send USER username. */
149 request = ftp_request ("USER", acc);
150 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
157 /* Get appropriate response. */
158 err = ftp_response (rbuf, &respline);
164 /* An unprobable possibility of logging without a password. */
165 if (*respline == '2')
170 /* Else, only response 3 is appropriate. */
171 if (*respline != '3')
174 return FTPLOGREFUSED;
178 static const char *skey_head[] = {
183 const char *seed = NULL;
185 for (i = 0; i < countof (skey_head); i++)
187 int l = strlen (skey_head[i]);
188 if (0 == strncasecmp (skey_head[i], respline, l))
196 int skey_sequence = 0;
198 /* Extract the sequence from SEED. */
199 for (; ISDIGIT (*seed); seed++)
200 skey_sequence = 10 * skey_sequence + *seed - '0';
206 return FTPLOGREFUSED;
208 /* Replace the password with the SKEY response to the
210 pass = skey_response (skey_sequence, seed, pass);
213 #endif /* USE_OPIE */
215 /* Send PASS password. */
216 request = ftp_request ("PASS", pass);
217 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
224 /* Get appropriate response. */
225 err = ftp_response (rbuf, &respline);
231 if (*respline != '2')
242 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
247 assert (addr != NULL);
248 assert (addr->type == IPV4_ADDRESS);
249 assert (buf != NULL);
250 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
251 assert (buflen >= 6 * 4);
253 ptr = ADDRESS_IPV4_DATA (addr);
254 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
255 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
256 buf[buflen - 1] = '\0';
259 /* Bind a port and send the appropriate PORT command to the FTP
260 server. Use acceptport after RETR, to get the socket of data
263 ftp_port (struct rbuf *rbuf, int *local_sock)
266 char *request, *respline;
270 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
271 char bytes[6 * 4 + 1];
273 assert (rbuf != NULL);
274 assert (rbuf_initialized_p (rbuf));
276 /* Get the address of this side of the connection. */
277 if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
280 assert (addr.type == IPV4_ADDRESS);
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 (RBUF_FD (rbuf), request, strlen (request), -1);
299 fd_close (*local_sock);
304 /* Get appropriate response. */
305 err = ftp_response (rbuf, &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,
329 assert (addr != NULL);
330 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
331 assert (buf != NULL);
332 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
333 assert (buflen >= 21 * 4);
335 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
339 ptr = ADDRESS_IPV4_DATA (addr);
340 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
341 ptr[0], ptr[1], ptr[2], ptr[3], 2,
342 (port & 0xff00) >> 8, port & 0xff);
343 buf[buflen - 1] = '\0';
346 ptr = ADDRESS_IPV6_DATA (addr);
347 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
348 6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
349 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
350 (port & 0xff00) >> 8, port & 0xff);
351 buf[buflen - 1] = '\0';
356 /* Bind a port and send the appropriate PORT command to the FTP
357 server. Use acceptport after RETR, to get the socket of data
360 ftp_lprt (struct rbuf *rbuf, int *local_sock)
363 char *request, *respline;
367 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
368 char bytes[21 * 4 + 1];
370 assert (rbuf != NULL);
371 assert (rbuf_initialized_p (rbuf));
373 /* Get the address of this side of the connection. */
374 if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
377 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
379 /* Setting port to 0 lets the system choose a free port. */
383 *local_sock = bind_local (&addr, &port);
387 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
388 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
390 /* Send PORT request. */
391 request = ftp_request ("LPRT", bytes);
392 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
396 fd_close (*local_sock);
400 /* Get appropriate response. */
401 err = ftp_response (rbuf, &respline);
405 fd_close (*local_sock);
408 if (*respline != '2')
411 fd_close (*local_sock);
419 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
424 assert (addr != NULL);
425 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
426 assert (buf != NULL);
427 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
428 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
429 * 1 char for af (1-2) and 5 chars for port (0-65535) */
430 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
432 /* Construct the argument of EPRT (of the form |af|addr|port|). */
433 afnum = (addr->type == IPV4_ADDRESS ? 1 : 2);
434 snprintf (buf, buflen, "|%d|%s|%d|", afnum, pretty_print_address (addr), port);
435 buf[buflen - 1] = '\0';
438 /* Bind a port and send the appropriate PORT command to the FTP
439 server. Use acceptport after RETR, to get the socket of data
442 ftp_eprt (struct rbuf *rbuf, int *local_sock)
445 char *request, *respline;
449 /* Must contain the argument of EPRT (of the form |af|addr|port|).
450 * 4 chars for the | separators, ENABLE_IPV6_ADDRSTRLEN chars for addr
451 * 1 char for af (1-2) and 5 chars for port (0-65535) */
452 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
454 assert (rbuf != NULL);
455 assert (rbuf_initialized_p(rbuf));
457 /* Get the address of this side of the connection. */
458 if (!socket_ip_address (RBUF_FD (rbuf), &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 LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
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 (RBUF_FD (rbuf), request, strlen (request), -1);
480 fd_close (*local_sock);
484 /* Get appropriate response. */
485 err = ftp_response (rbuf, &respline);
489 fd_close (*local_sock);
492 if (*respline != '2')
495 fd_close (*local_sock);
503 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
504 transfer. Reads the response from server and parses it. Reads the
505 host and port addresses and returns them. */
507 ftp_pasv (struct rbuf *rbuf, ip_address *addr, int *port)
509 char *request, *respline, *s;
512 unsigned char tmp[6];
514 assert (rbuf != NULL);
515 assert (rbuf_initialized_p (rbuf));
516 assert (addr != NULL);
517 assert (port != NULL);
519 memset (addr, 0, sizeof (ip_address));
521 /* Form the request. */
522 request = ftp_request ("PASV", NULL);
524 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
531 /* Get the server response. */
532 err = ftp_response (rbuf, &respline);
538 if (*respline != '2')
543 /* Parse the request. */
545 for (s += 4; *s && !ISDIGIT (*s); s++);
548 for (i = 0; i < 6; i++)
551 for (; ISDIGIT (*s); s++)
552 tmp[i] = (*s - '0') + 10 * tmp[i];
557 /* When on the last number, anything can be a terminator. */
564 addr->type = IPV4_ADDRESS;
565 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
566 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
572 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
573 transfer. Reads the response from server and parses it. Reads the
574 host and port addresses and returns them. */
576 ftp_lpsv (struct rbuf *rbuf, ip_address *addr, int *port)
578 char *request, *respline, *s;
579 int nwritten, i, af, addrlen, portlen;
581 unsigned char tmp[16];
582 unsigned char tmpprt[2];
584 assert (rbuf != NULL);
585 assert (rbuf_initialized_p(rbuf));
586 assert (addr != NULL);
587 assert (port != NULL);
589 memset (addr, 0, sizeof (ip_address));
591 /* Form the request. */
592 request = ftp_request ("LPSV", NULL);
595 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
603 /* Get the server response. */
604 err = ftp_response (rbuf, &respline);
610 if (*respline != '2')
616 /* Parse the response. */
618 for (s += 4; *s && !ISDIGIT (*s); s++);
622 /* First, get the address family */
624 for (; ISDIGIT (*s); s++)
625 af = (*s - '0') + 10 * af;
627 if (af != 4 && af != 6)
633 if (!*s || *s++ != ',')
639 /* Then, get the address length */
641 for (; ISDIGIT (*s); s++)
642 addrlen = (*s - '0') + 10 * addrlen;
644 if (!*s || *s++ != ',')
656 if ((af == 4 && addrlen != 4)
657 || (af == 6 && addrlen != 16))
663 /* Now, we get the actual address */
664 for (i = 0; i < addrlen; i++)
667 for (; ISDIGIT (*s); s++)
668 tmp[i] = (*s - '0') + 10 * tmp[i];
678 /* Now, get the port length */
680 for (; ISDIGIT (*s); s++)
681 portlen = (*s - '0') + 10 * portlen;
683 if (!*s || *s++ != ',')
695 /* Finally, we get the port number */
697 for (; ISDIGIT (*s); s++)
698 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
700 if (!*s || *s++ != ',')
707 for (; ISDIGIT (*s); s++)
708 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
714 addr->type = IPV4_ADDRESS;
715 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
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));
725 addr->type = IPV6_ADDRESS;
726 memcpy (ADDRESS_IPV6_DATA (addr), tmp, 16);
727 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
728 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
729 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
730 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
731 DEBUGP (("*port is: %d\n", *port));
738 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
739 transfer. Reads the response from server and parses it. Reads the
740 host and port addresses and returns them. */
742 ftp_epsv (struct rbuf *rbuf, ip_address *ip, int *port)
744 char *request, *respline, *start, delim, *s;
749 assert (rbuf != NULL);
750 assert (rbuf_initialized_p(rbuf));
752 assert (port != NULL);
754 /* IP already contains the IP address of the control connection's
755 peer, so we don't need to call socket_ip_address here. */
757 /* Form the request. */
758 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
759 request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
762 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
770 /* Get the server response. */
771 err = ftp_response (rbuf, &respline);
777 if (*respline != '2')
783 assert (respline != NULL);
785 DEBUGP(("respline is %s\n", respline));
787 /* Parse the response. */
790 /* Skip the useless stuff and get what's inside the parentheses */
791 start = strchr (respline, '(');
798 /* Skip the first two void fields */
801 if (delim < 33 || delim > 126)
807 for (i = 0; i < 2; i++)
816 /* Finally, get the port number */
818 for (i = 1; ISDIGIT (*s); s++)
825 tport = (*s - '0') + 10 * tport;
828 /* Make sure that the response terminates correcty */
848 /* Sends the TYPE request to the server. */
850 ftp_type (struct rbuf *rbuf, int type)
852 char *request, *respline;
857 /* Construct argument. */
860 /* Send TYPE request. */
861 request = ftp_request ("TYPE", stype);
862 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
869 /* Get appropriate response. */
870 err = ftp_response (rbuf, &respline);
876 if (*respline != '2')
879 return FTPUNKNOWNTYPE;
886 /* Changes the working directory by issuing a CWD command to the
889 ftp_cwd (struct rbuf *rbuf, const char *dir)
891 char *request, *respline;
895 /* Send CWD request. */
896 request = ftp_request ("CWD", dir);
897 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
904 /* Get appropriate response. */
905 err = ftp_response (rbuf, &respline);
911 if (*respline == '5')
916 if (*respline != '2')
926 /* Sends REST command to the FTP server. */
928 ftp_rest (struct rbuf *rbuf, long offset)
930 char *request, *respline;
933 static char numbuf[24]; /* Buffer for the number */
935 number_to_string (numbuf, offset);
936 request = ftp_request ("REST", numbuf);
937 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
944 /* Get appropriate response. */
945 err = ftp_response (rbuf, &respline);
951 if (*respline != '3')
961 /* Sends RETR command to the FTP server. */
963 ftp_retr (struct rbuf *rbuf, const char *file)
965 char *request, *respline;
969 /* Send RETR request. */
970 request = ftp_request ("RETR", file);
971 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
978 /* Get appropriate response. */
979 err = ftp_response (rbuf, &respline);
985 if (*respline == '5')
990 if (*respline != '1')
1000 /* Sends the LIST command to the server. If FILE is NULL, send just
1001 `LIST' (no space). */
1003 ftp_list (struct rbuf *rbuf, const char *file)
1005 char *request, *respline;
1009 /* Send LIST request. */
1010 request = ftp_request ("LIST", file);
1011 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
1018 /* Get appropriate respone. */
1019 err = ftp_response (rbuf, &respline);
1025 if (*respline == '5')
1030 if (*respline != '1')
1040 /* Sends the SYST command to the server. */
1042 ftp_syst (struct rbuf *rbuf, enum stype *server_type)
1044 char *request, *respline;
1048 /* Send SYST request. */
1049 request = ftp_request ("SYST", NULL);
1050 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
1058 /* Get appropriate response. */
1059 err = ftp_response (rbuf, &respline);
1065 if (*respline == '5')
1071 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1072 strtok (respline, " ");
1074 /* Which system type has been reported (we are interested just in the
1075 first word of the server response)? */
1076 request = strtok (NULL, " ");
1078 if (!strcasecmp (request, "VMS"))
1079 *server_type = ST_VMS;
1080 else if (!strcasecmp (request, "UNIX"))
1081 *server_type = ST_UNIX;
1082 else if (!strcasecmp (request, "WINDOWS_NT")
1083 || !strcasecmp (request, "WINDOWS2000"))
1084 *server_type = ST_WINNT;
1085 else if (!strcasecmp (request, "MACOS"))
1086 *server_type = ST_MACOS;
1087 else if (!strcasecmp (request, "OS/400"))
1088 *server_type = ST_OS400;
1090 *server_type = ST_OTHER;
1097 /* Sends the PWD command to the server. */
1099 ftp_pwd (struct rbuf *rbuf, char **pwd)
1101 char *request, *respline;
1105 /* Send PWD request. */
1106 request = ftp_request ("PWD", NULL);
1107 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
1114 /* Get appropriate response. */
1115 err = ftp_response (rbuf, &respline);
1121 if (*respline == '5')
1127 /* Skip the number (257), leading citation mark, trailing citation mark
1128 and everything following it. */
1129 strtok (respline, "\"");
1130 request = strtok (NULL, "\"");
1132 /* Has the `pwd' been already allocated? Free! */
1135 *pwd = xstrdup (request);
1142 /* Sends the SIZE command to the server, and returns the value in 'size'.
1143 * If an error occurs, size is set to zero. */
1145 ftp_size (struct rbuf *rbuf, const char *file, long int *size)
1147 char *request, *respline;
1151 /* Send PWD request. */
1152 request = ftp_request ("SIZE", file);
1153 nwritten = fd_write (RBUF_FD (rbuf), request, strlen (request), -1);
1161 /* Get appropriate response. */
1162 err = ftp_response (rbuf, &respline);
1169 if (*respline == '5')
1172 * Probably means SIZE isn't supported on this server.
1173 * Error is nonfatal since SIZE isn't in RFC 959
1181 *size = strtol (respline + 4, NULL, 0);
1185 * Couldn't parse the response for some reason. On the (few)
1186 * tests I've done, the response is 213 <SIZE> with nothing else -
1187 * maybe something a bit more resilient is necessary. It's not a
1188 * fatal error, however.
1200 /* If URL's params are of the form "type=X", return character X.
1201 Otherwise, return 'I' (the default type). */
1203 ftp_process_type (const char *params)
1206 && 0 == strncasecmp (params, "type=", 5)
1207 && params[5] != '\0')
1208 return TOUPPER (params[5]);