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. */
34 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <arpa/inet.h>
53 char ftp_last_respline[128];
56 /* Get the response of FTP server and allocate enough room to handle
57 it. <CR> and <LF> characters are stripped from the line, and the
58 line is 0-terminated. All the response lines but the last one are
59 skipped. The last line is determined as described in RFC959. */
61 ftp_response (struct rbuf *rbuf, char **line)
66 *line = (char *)xmalloc (bufsize);
73 *line = (char *)xrealloc (*line, (bufsize <<= 1));
74 res = RBUF_READCHAR (rbuf, *line + i);
75 /* RES is number of bytes read. */
78 if ((*line)[i] == '\n')
82 if (i > 0 && (*line)[i - 1] == '\r')
83 (*line)[i - 1] = '\0';
90 if (opt.server_response)
91 logprintf (LOG_ALWAYS, "%s\n", *line);
93 DEBUGP (("%s\n", *line));
95 while (!(i >= 3 && ISDIGIT (**line) && ISDIGIT ((*line)[1]) &&
96 ISDIGIT ((*line)[2]) && (*line)[3] == ' '));
97 strncpy (ftp_last_respline, *line, sizeof (ftp_last_respline));
98 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)
108 char *res = (char *)xmalloc (strlen (command)
109 + (value ? (1 + strlen (value)) : 0)
111 sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
112 if (opt.server_response)
114 /* Hack: don't print out password. */
115 if (strncmp (res, "PASS", 4) != 0)
116 logprintf (LOG_ALWAYS, "--> %s\n", res);
118 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n");
121 DEBUGP (("\n--> %s\n", res));
126 const char *calculate_skey_response PARAMS ((int, const char *, const char *));
129 /* Sends the USER and PASS commands to the server, to control
130 connection socket csock. */
132 ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
135 char *request, *respline;
139 err = ftp_response (rbuf, &respline);
145 if (*respline != '2')
151 /* Send USER username. */
152 request = ftp_request ("USER", acc);
153 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
160 /* Get appropriate response. */
161 err = ftp_response (rbuf, &respline);
167 /* An unprobable possibility of logging without a password. */
168 if (*respline == '2')
173 /* Else, only response 3 is appropriate. */
174 if (*respline != '3')
177 return FTPLOGREFUSED;
181 static const char *skey_head[] = {
187 for (i = 0; i < ARRAY_SIZE (skey_head); i++)
189 if (strncasecmp (skey_head[i], respline, strlen (skey_head[i])) == 0)
192 if (i < ARRAY_SIZE (skey_head))
195 int skey_sequence = 0;
197 for (cp = respline + strlen (skey_head[i]);
198 '0' <= *cp && *cp <= '9';
201 skey_sequence = skey_sequence * 10 + *cp - '0';
209 return FTPLOGREFUSED;
211 if ((cp = calculate_skey_response (skey_sequence, cp, pass)) == 0)
216 #endif /* USE_OPIE */
218 /* Send PASS password. */
219 request = ftp_request ("PASS", pass);
220 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
227 /* Get appropriate response. */
228 err = ftp_response (rbuf, &respline);
234 if (*respline != '2')
246 ftp_eprt (struct rbuf *rbuf)
250 char *request, *respline;
254 char ipv6 [8 * (4 * 3 + 3) + 8];
257 /* Setting port to 0 lets the system choose a free port. */
259 err = bindport (&port, ip_default_family);
260 if (err != BINDOK) /* Bind the port. */
263 /* Get the address of this side of the connection. */
264 if (!conaddr (RBUF_FD (rbuf), &in_addr))
265 /* Huh? This is not BINDERR! */
267 inet_ntop (AF_INET6, &in_addr, ipv6, sizeof (ipv6));
269 /* Construct the argument of EPRT (of the form |2|IPv6.ascii|PORT.ascii|). */
270 bytes = alloca (3 + strlen (ipv6) + 1 + numdigit (port) + 1 + 1);
271 sprintf (bytes, "|2|%s|%u|", ipv6, port);
272 /* Send PORT request. */
273 request = ftp_request ("EPRT", bytes);
274 if (0 > iwrite (RBUF_FD (rbuf), request, strlen (request)))
281 /* Get appropriate response. */
282 err = ftp_response (rbuf, &respline);
289 if (*respline != '2')
300 /* Bind a port and send the appropriate PORT command to the FTP
301 server. Use acceptport after RETR, to get the socket of data
304 ftp_port (struct rbuf *rbuf)
307 char *request, *respline;
308 char bytes[6 * 4 +1];
311 ip4_address in_addr_4;
312 unsigned char *in_addr4_ptr = (unsigned char *)&in_addr_4;
318 Only try the Extented Version if we actually use IPv6
320 if (ip_default_family == AF_INET6)
322 err = ftp_eprt (rbuf);
327 /* Setting port to 0 lets the system choose a free port. */
330 err = bindport (&port, AF_INET);
334 /* Get the address of this side of the connection and convert it
336 if (!conaddr (RBUF_FD (rbuf), &in_addr))
337 /* Huh? This is not BINDERR! */
339 if (!map_ip_to_ipv4 (&in_addr, &in_addr_4))
342 /* Construct the argument of PORT (of the form a,b,c,d,e,f). Port
343 is unsigned short so (unsigned) (port & 0xff000) >> 8 is the same
346 sprintf (bytes, "%d,%d,%d,%d,%d,%d",
347 in_addr4_ptr[0], in_addr4_ptr[1], in_addr4_ptr[2], in_addr4_ptr[3],
348 port >> 8, port & 0xff);
349 /* Send PORT request. */
350 request = ftp_request ("PORT", bytes);
351 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
358 /* Get appropriate response. */
359 err = ftp_response (rbuf, &respline);
365 if (*respline != '2')
376 ftp_epsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port,
381 char *request = ftp_request ("EPSV", typ);
382 if (0 > iwrite (RBUF_FD (rbuf), request, strlen (request)))
387 /* Get the server response. */
388 err = ftp_response (rbuf, &respline);
394 if (*respline != '2')
399 /* Parse the request. */
401 /* respline::=229 Entering Extended Passive Mode (|||6446|) */
402 for (s += 4; *s && !ISDIGIT (*s); s++);
406 for (; ISDIGIT (*s); s++)
407 *port = (*s - '0') + 10 * (*port);
409 /* Now we have the port but we need the IPv6 :-( */
411 wget_sockaddr remote;
412 int len = sizeof (remote);
413 struct sockaddr_in *ipv4_sock = ( struct sockaddr_in *)&remote;
414 getpeername (RBUF_FD (rbuf), (struct sockaddr*)&remote, &len);
415 switch(remote.sa.sa_family)
418 memcpy (addr, &remote.sin6.sin6_addr, 16);
421 map_ipv4_to_ip ((ip4_address *)&ipv4_sock->sin_addr, addr);
434 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
435 transfer. Reads the response from server and parses it. Reads the
436 host and port addresses and returns them. */
438 ftp_pasv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
440 char *request, *respline, *s;
443 unsigned char addr4[4];
446 if (ip_default_family == AF_INET6)
448 err = ftp_epsv (rbuf, addr, port, "2"); /* try IPv6 with EPSV */
451 err = ftp_epsv (rbuf, addr, port, "1"); /* try IPv4 with EPSV */
456 /* Form the request. */
457 request = ftp_request ("PASV", NULL);
459 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
466 /* Get the server response. */
467 err = ftp_response (rbuf, &respline);
473 if (*respline != '2')
478 /* Parse the request. */
479 /* respline::=227 Entering Passive Mode (h1,h2,h3,h4,p1,p2). */
481 for (s += 4; *s && !ISDIGIT (*s); s++);
484 for (i = 0; i < 4; i++)
487 for (; ISDIGIT (*s); s++)
488 addr4[i] = (*s - '0') + 10 * addr4[i];
498 /* Eventually make an IPv4 in IPv6 adress if needed */
499 map_ipv4_to_ip ((ip4_address *)addr4, addr);
502 for (; ISDIGIT (*s); s++)
503 *port = (*s - '0') + 10 * (*port);
513 unsigned short port2 = 0;
514 for (; ISDIGIT (*s); s++)
515 port2 = (*s - '0') + 10 * port2;
516 *port = (*port) * 256 + port2;
522 /* Sends the TYPE request to the server. */
524 ftp_type (struct rbuf *rbuf, int type)
526 char *request, *respline;
531 /* Construct argument. */
534 /* Send TYPE request. */
535 request = ftp_request ("TYPE", stype);
536 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
543 /* Get appropriate response. */
544 err = ftp_response (rbuf, &respline);
550 if (*respline != '2')
553 return FTPUNKNOWNTYPE;
560 /* Changes the working directory by issuing a CWD command to the
563 ftp_cwd (struct rbuf *rbuf, const char *dir)
565 char *request, *respline;
569 /* Send CWD request. */
570 request = ftp_request ("CWD", dir);
571 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
578 /* Get appropriate response. */
579 err = ftp_response (rbuf, &respline);
585 if (*respline == '5')
590 if (*respline != '2')
600 /* Sends REST command to the FTP server. */
602 ftp_rest (struct rbuf *rbuf, long offset)
604 char *request, *respline;
607 static char numbuf[24]; /* Buffer for the number */
609 number_to_string (numbuf, offset);
610 request = ftp_request ("REST", numbuf);
611 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
618 /* Get appropriate response. */
619 err = ftp_response (rbuf, &respline);
625 if (*respline != '3')
635 /* Sends RETR command to the FTP server. */
637 ftp_retr (struct rbuf *rbuf, const char *file)
639 char *request, *respline;
643 /* Send RETR request. */
644 request = ftp_request ("RETR", file);
645 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
652 /* Get appropriate response. */
653 err = ftp_response (rbuf, &respline);
659 if (*respline == '5')
664 if (*respline != '1')
674 /* Sends the LIST command to the server. If FILE is NULL, send just
675 `LIST' (no space). */
677 ftp_list (struct rbuf *rbuf, const char *file)
679 char *request, *respline;
683 /* Send LIST request. */
684 request = ftp_request ("LIST", file);
685 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
692 /* Get appropriate respone. */
693 err = ftp_response (rbuf, &respline);
699 if (*respline == '5')
704 if (*respline != '1')
714 /* Sends the SYST command to the server. */
716 ftp_syst (struct rbuf *rbuf, enum stype *server_type)
718 char *request, *respline;
722 /* Send SYST request. */
723 request = ftp_request ("SYST", NULL);
724 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
732 /* Get appropriate response. */
733 err = ftp_response (rbuf, &respline);
739 if (*respline == '5')
745 /* Skip the number (215, but 200 (!!!) in case of VMS) */
746 strtok (respline, " ");
748 /* Which system type has been reported (we are interested just in the
749 first word of the server response)? */
750 request = strtok (NULL, " ");
752 if (!strcasecmp (request, "VMS"))
753 *server_type = ST_VMS;
754 else if (!strcasecmp (request, "UNIX"))
755 *server_type = ST_UNIX;
756 else if (!strcasecmp (request, "WINDOWS_NT"))
757 *server_type = ST_WINNT;
758 else if (!strcasecmp (request, "MACOS"))
759 *server_type = ST_MACOS;
761 *server_type = ST_OTHER;
768 /* Sends the PWD command to the server. */
770 ftp_pwd (struct rbuf *rbuf, char **pwd)
772 char *request, *respline;
776 /* Send PWD request. */
777 request = ftp_request ("PWD", NULL);
778 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
785 /* Get appropriate response. */
786 err = ftp_response (rbuf, &respline);
792 if (*respline == '5')
798 /* Skip the number (257), leading citation mark, trailing citation mark
799 and everything following it. */
800 strtok (respline, "\"");
801 request = strtok (NULL, "\"");
803 /* Has the `pwd' been already allocated? Free! */
806 *pwd = xstrdup (request);
813 /* Sends the SIZE command to the server, and returns the value in 'size'.
814 * If an error occurs, size is set to zero. */
816 ftp_size (struct rbuf *rbuf, const char *file, long int *size)
818 char *request, *respline;
822 /* Send PWD request. */
823 request = ftp_request ("SIZE", file);
824 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
832 /* Get appropriate response. */
833 err = ftp_response (rbuf, &respline);
840 if (*respline == '5')
843 * Probably means SIZE isn't supported on this server.
844 * Error is nonfatal since SIZE isn't in RFC 959
852 *size = strtol (respline + 4, NULL, 0);
856 * Couldn't parse the response for some reason. On the (few)
857 * tests I've done, the response is 213 <SIZE> with nothing else -
858 * maybe something a bit more resilient is necessary. It's not a
859 * fatal error, however.
871 /* If URL's params are of the form "type=X", return character X.
872 Otherwise, return 'I' (the default type). */
874 ftp_process_type (const char *params)
877 && 0 == strncasecmp (params, "type=", 5)
878 && params[5] != '\0')
879 return TOUPPER (params[5]);