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 (int fd, char **ret_line)
66 char *line = fd_read_line (fd);
69 if (opt.server_response)
70 logputs (LOG_NOTQUIET, line);
72 DEBUGP (("%s", line));
73 if (ISDIGIT (line[0]) && ISDIGIT (line[1]) && ISDIGIT (line[2])
76 char *p = line + strlen (line);
77 if (p > line && p[-1] == '\n')
79 if (p > line && p[-1] == '\r')
81 strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
82 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
90 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
91 it if printing is required. If VALUE is NULL, just use
94 ftp_request (const char *command, const char *value)
96 char *res = (char *)xmalloc (strlen (command)
97 + (value ? (1 + strlen (value)) : 0)
99 sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
100 if (opt.server_response)
102 /* Hack: don't print out password. */
103 if (strncmp (res, "PASS", 4) != 0)
104 logprintf (LOG_ALWAYS, "--> %s\n", res);
106 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
109 DEBUGP (("\n--> %s\n", res));
113 /* Sends the USER and PASS commands to the server, to control
114 connection socket csock. */
116 ftp_login (int csock, const char *acc, const char *pass)
119 char *request, *respline;
123 err = ftp_response (csock, &respline);
129 if (*respline != '2')
135 /* Send USER username. */
136 request = ftp_request ("USER", acc);
137 nwritten = fd_write (csock, request, strlen (request), -1);
144 /* Get appropriate response. */
145 err = ftp_response (csock, &respline);
151 /* An unprobable possibility of logging without a password. */
152 if (*respline == '2')
157 /* Else, only response 3 is appropriate. */
158 if (*respline != '3')
161 return FTPLOGREFUSED;
165 static const char *skey_head[] = {
170 const char *seed = NULL;
172 for (i = 0; i < countof (skey_head); i++)
174 int l = strlen (skey_head[i]);
175 if (0 == strncasecmp (skey_head[i], respline, l))
183 int skey_sequence = 0;
185 /* Extract the sequence from SEED. */
186 for (; ISDIGIT (*seed); seed++)
187 skey_sequence = 10 * skey_sequence + *seed - '0';
193 return FTPLOGREFUSED;
195 /* Replace the password with the SKEY response to the
197 pass = skey_response (skey_sequence, seed, pass);
200 #endif /* USE_OPIE */
202 /* Send PASS password. */
203 request = ftp_request ("PASS", pass);
204 nwritten = fd_write (csock, request, strlen (request), -1);
211 /* Get appropriate response. */
212 err = ftp_response (csock, &respline);
218 if (*respline != '2')
229 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
234 assert (addr != NULL);
235 assert (addr->type == IPV4_ADDRESS);
236 assert (buf != NULL);
237 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
238 assert (buflen >= 6 * 4);
240 ptr = ADDRESS_IPV4_DATA (addr);
241 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
242 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
243 buf[buflen - 1] = '\0';
246 /* Bind a port and send the appropriate PORT command to the FTP
247 server. Use acceptport after RETR, to get the socket of data
250 ftp_port (int csock, int *local_sock)
253 char *request, *respline;
257 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
258 char bytes[6 * 4 + 1];
260 /* Get the address of this side of the connection. */
261 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
264 assert (addr.type == IPV4_ADDRESS);
266 /* Setting port to 0 lets the system choose a free port. */
270 *local_sock = bind_local (&addr, &port);
274 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
275 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
277 /* Send PORT request. */
278 request = ftp_request ("PORT", bytes);
279 nwritten = fd_write (csock, request, strlen (request), -1);
283 fd_close (*local_sock);
288 /* Get appropriate response. */
289 err = ftp_response (csock, &respline);
293 fd_close (*local_sock);
296 if (*respline != '2')
299 fd_close (*local_sock);
308 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
313 assert (addr != NULL);
314 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
315 assert (buf != NULL);
316 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
317 assert (buflen >= 21 * 4);
319 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
323 ptr = ADDRESS_IPV4_DATA (addr);
324 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
325 ptr[0], ptr[1], ptr[2], ptr[3], 2,
326 (port & 0xff00) >> 8, port & 0xff);
327 buf[buflen - 1] = '\0';
330 ptr = ADDRESS_IPV6_DATA (addr);
331 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
332 6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
333 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
334 (port & 0xff00) >> 8, port & 0xff);
335 buf[buflen - 1] = '\0';
340 /* Bind a port and send the appropriate PORT command to the FTP
341 server. Use acceptport after RETR, to get the socket of data
344 ftp_lprt (int csock, int *local_sock)
347 char *request, *respline;
351 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
352 char bytes[21 * 4 + 1];
354 /* Get the address of this side of the connection. */
355 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
358 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
360 /* Setting port to 0 lets the system choose a free port. */
364 *local_sock = bind_local (&addr, &port);
368 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
369 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
371 /* Send PORT request. */
372 request = ftp_request ("LPRT", bytes);
373 nwritten = fd_write (csock, request, strlen (request), -1);
377 fd_close (*local_sock);
381 /* Get appropriate response. */
382 err = ftp_response (csock, &respline);
386 fd_close (*local_sock);
389 if (*respline != '2')
392 fd_close (*local_sock);
400 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
405 assert (addr != NULL);
406 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
407 assert (buf != NULL);
408 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
409 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
410 * 1 char for af (1-2) and 5 chars for port (0-65535) */
411 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
413 /* Construct the argument of EPRT (of the form |af|addr|port|). */
414 afnum = (addr->type == IPV4_ADDRESS ? 1 : 2);
415 snprintf (buf, buflen, "|%d|%s|%d|", afnum, pretty_print_address (addr), port);
416 buf[buflen - 1] = '\0';
419 /* Bind a port and send the appropriate PORT command to the FTP
420 server. Use acceptport after RETR, to get the socket of data
423 ftp_eprt (int csock, int *local_sock)
426 char *request, *respline;
430 /* Must contain the argument of EPRT (of the form |af|addr|port|).
431 * 4 chars for the | separators, ENABLE_IPV6_ADDRSTRLEN chars for addr
432 * 1 char for af (1-2) and 5 chars for port (0-65535) */
433 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
435 /* Get the address of this side of the connection. */
436 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
439 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
441 /* Setting port to 0 lets the system choose a free port. */
445 *local_sock = bind_local (&addr, &port);
449 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
450 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
452 /* Send PORT request. */
453 request = ftp_request ("EPRT", bytes);
454 nwritten = fd_write (csock, request, strlen (request), -1);
458 fd_close (*local_sock);
462 /* Get appropriate response. */
463 err = ftp_response (csock, &respline);
467 fd_close (*local_sock);
470 if (*respline != '2')
473 fd_close (*local_sock);
481 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
482 transfer. Reads the response from server and parses it. Reads the
483 host and port addresses and returns them. */
485 ftp_pasv (int csock, ip_address *addr, int *port)
487 char *request, *respline, *s;
490 unsigned char tmp[6];
492 assert (addr != NULL);
493 assert (port != NULL);
495 memset (addr, 0, sizeof (ip_address));
497 /* Form the request. */
498 request = ftp_request ("PASV", NULL);
500 nwritten = fd_write (csock, request, strlen (request), -1);
507 /* Get the server response. */
508 err = ftp_response (csock, &respline);
514 if (*respline != '2')
519 /* Parse the request. */
521 for (s += 4; *s && !ISDIGIT (*s); s++);
524 for (i = 0; i < 6; i++)
527 for (; ISDIGIT (*s); s++)
528 tmp[i] = (*s - '0') + 10 * tmp[i];
533 /* When on the last number, anything can be a terminator. */
540 addr->type = IPV4_ADDRESS;
541 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
542 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
548 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
549 transfer. Reads the response from server and parses it. Reads the
550 host and port addresses and returns them. */
552 ftp_lpsv (int csock, ip_address *addr, int *port)
554 char *request, *respline, *s;
555 int nwritten, i, af, addrlen, portlen;
557 unsigned char tmp[16];
558 unsigned char tmpprt[2];
560 assert (addr != NULL);
561 assert (port != NULL);
563 memset (addr, 0, sizeof (ip_address));
565 /* Form the request. */
566 request = ftp_request ("LPSV", NULL);
569 nwritten = fd_write (csock, request, strlen (request), -1);
577 /* Get the server response. */
578 err = ftp_response (csock, &respline);
584 if (*respline != '2')
590 /* Parse the response. */
592 for (s += 4; *s && !ISDIGIT (*s); s++);
596 /* First, get the address family */
598 for (; ISDIGIT (*s); s++)
599 af = (*s - '0') + 10 * af;
601 if (af != 4 && af != 6)
607 if (!*s || *s++ != ',')
613 /* Then, get the address length */
615 for (; ISDIGIT (*s); s++)
616 addrlen = (*s - '0') + 10 * addrlen;
618 if (!*s || *s++ != ',')
630 if ((af == 4 && addrlen != 4)
631 || (af == 6 && addrlen != 16))
637 /* Now, we get the actual address */
638 for (i = 0; i < addrlen; i++)
641 for (; ISDIGIT (*s); s++)
642 tmp[i] = (*s - '0') + 10 * tmp[i];
652 /* Now, get the port length */
654 for (; ISDIGIT (*s); s++)
655 portlen = (*s - '0') + 10 * portlen;
657 if (!*s || *s++ != ',')
669 /* Finally, we get the port number */
671 for (; ISDIGIT (*s); s++)
672 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
674 if (!*s || *s++ != ',')
681 for (; ISDIGIT (*s); s++)
682 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
688 addr->type = IPV4_ADDRESS;
689 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
690 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
691 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
692 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
693 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
694 DEBUGP (("*port is: %d\n", *port));
699 addr->type = IPV6_ADDRESS;
700 memcpy (ADDRESS_IPV6_DATA (addr), tmp, 16);
701 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
702 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
703 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
704 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
705 DEBUGP (("*port is: %d\n", *port));
712 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
713 transfer. Reads the response from server and parses it. Reads the
714 host and port addresses and returns them. */
716 ftp_epsv (int csock, ip_address *ip, int *port)
718 char *request, *respline, *start, delim, *s;
724 assert (port != NULL);
726 /* IP already contains the IP address of the control connection's
727 peer, so we don't need to call socket_ip_address here. */
729 /* Form the request. */
730 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
731 request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
734 nwritten = fd_write (csock, request, strlen (request), -1);
742 /* Get the server response. */
743 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);
848 if (*respline != '2')
851 return FTPUNKNOWNTYPE;
858 /* Changes the working directory by issuing a CWD command to the
861 ftp_cwd (int csock, const char *dir)
863 char *request, *respline;
867 /* Send CWD request. */
868 request = ftp_request ("CWD", dir);
869 nwritten = fd_write (csock, request, strlen (request), -1);
876 /* Get appropriate response. */
877 err = ftp_response (csock, &respline);
883 if (*respline == '5')
888 if (*respline != '2')
898 /* Sends REST command to the FTP server. */
900 ftp_rest (int csock, long offset)
902 char *request, *respline;
905 static char numbuf[24]; /* Buffer for the number */
907 number_to_string (numbuf, offset);
908 request = ftp_request ("REST", numbuf);
909 nwritten = fd_write (csock, request, strlen (request), -1);
916 /* Get appropriate response. */
917 err = ftp_response (csock, &respline);
923 if (*respline != '3')
933 /* Sends RETR command to the FTP server. */
935 ftp_retr (int csock, const char *file)
937 char *request, *respline;
941 /* Send RETR request. */
942 request = ftp_request ("RETR", file);
943 nwritten = fd_write (csock, request, strlen (request), -1);
950 /* Get appropriate response. */
951 err = ftp_response (csock, &respline);
957 if (*respline == '5')
962 if (*respline != '1')
972 /* Sends the LIST command to the server. If FILE is NULL, send just
973 `LIST' (no space). */
975 ftp_list (int csock, const char *file)
977 char *request, *respline;
981 /* Send LIST request. */
982 request = ftp_request ("LIST", file);
983 nwritten = fd_write (csock, request, strlen (request), -1);
990 /* Get appropriate respone. */
991 err = ftp_response (csock, &respline);
997 if (*respline == '5')
1002 if (*respline != '1')
1012 /* Sends the SYST command to the server. */
1014 ftp_syst (int csock, enum stype *server_type)
1016 char *request, *respline;
1020 /* Send SYST request. */
1021 request = ftp_request ("SYST", NULL);
1022 nwritten = fd_write (csock, request, strlen (request), -1);
1030 /* Get appropriate response. */
1031 err = ftp_response (csock, &respline);
1037 if (*respline == '5')
1043 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1044 strtok (respline, " ");
1046 /* Which system type has been reported (we are interested just in the
1047 first word of the server response)? */
1048 request = strtok (NULL, " ");
1050 if (!strcasecmp (request, "VMS"))
1051 *server_type = ST_VMS;
1052 else if (!strcasecmp (request, "UNIX"))
1053 *server_type = ST_UNIX;
1054 else if (!strcasecmp (request, "WINDOWS_NT")
1055 || !strcasecmp (request, "WINDOWS2000"))
1056 *server_type = ST_WINNT;
1057 else if (!strcasecmp (request, "MACOS"))
1058 *server_type = ST_MACOS;
1059 else if (!strcasecmp (request, "OS/400"))
1060 *server_type = ST_OS400;
1062 *server_type = ST_OTHER;
1069 /* Sends the PWD command to the server. */
1071 ftp_pwd (int csock, char **pwd)
1073 char *request, *respline;
1077 /* Send PWD request. */
1078 request = ftp_request ("PWD", NULL);
1079 nwritten = fd_write (csock, request, strlen (request), -1);
1086 /* Get appropriate response. */
1087 err = ftp_response (csock, &respline);
1093 if (*respline == '5')
1099 /* Skip the number (257), leading citation mark, trailing citation mark
1100 and everything following it. */
1101 strtok (respline, "\"");
1102 request = strtok (NULL, "\"");
1104 /* Has the `pwd' been already allocated? Free! */
1107 *pwd = xstrdup (request);
1114 /* Sends the SIZE command to the server, and returns the value in 'size'.
1115 * If an error occurs, size is set to zero. */
1117 ftp_size (int csock, const char *file, long int *size)
1119 char *request, *respline;
1123 /* Send PWD request. */
1124 request = ftp_request ("SIZE", file);
1125 nwritten = fd_write (csock, request, strlen (request), -1);
1133 /* Get appropriate response. */
1134 err = ftp_response (csock, &respline);
1141 if (*respline == '5')
1144 * Probably means SIZE isn't supported on this server.
1145 * Error is nonfatal since SIZE isn't in RFC 959
1153 *size = strtol (respline + 4, NULL, 0);
1157 * Couldn't parse the response for some reason. On the (few)
1158 * tests I've done, the response is 213 <SIZE> with nothing else -
1159 * maybe something a bit more resilient is necessary. It's not a
1160 * fatal error, however.
1172 /* If URL's params are of the form "type=X", return character X.
1173 Otherwise, return 'I' (the default type). */
1175 ftp_process_type (const char *params)
1178 && 0 == strncasecmp (params, "type=", 5)
1179 && params[5] != '\0')
1180 return TOUPPER (params[5]);