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, escnonprint (line));
72 DEBUGP (("%s", escnonprint (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)
98 res = concat_strings (command, " ", value, "\r\n", (char *) 0);
100 res = concat_strings (command, "\r\n", (char *) 0);
101 if (opt.server_response)
103 /* Hack: don't print out password. */
104 if (strncmp (res, "PASS", 4) != 0)
105 logprintf (LOG_ALWAYS, "--> %s\n", res);
107 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n\n");
110 DEBUGP (("\n--> %s\n", res));
114 /* Sends the USER and PASS commands to the server, to control
115 connection socket csock. */
117 ftp_login (int csock, const char *acc, const char *pass)
120 char *request, *respline;
124 err = ftp_response (csock, &respline);
130 if (*respline != '2')
136 /* Send USER username. */
137 request = ftp_request ("USER", acc);
138 nwritten = fd_write (csock, request, strlen (request), -1);
145 /* Get appropriate response. */
146 err = ftp_response (csock, &respline);
152 /* An unprobable possibility of logging without a password. */
153 if (*respline == '2')
158 /* Else, only response 3 is appropriate. */
159 if (*respline != '3')
162 return FTPLOGREFUSED;
166 static const char *skey_head[] = {
171 const char *seed = NULL;
173 for (i = 0; i < countof (skey_head); i++)
175 int l = strlen (skey_head[i]);
176 if (0 == strncasecmp (skey_head[i], respline, l))
184 int skey_sequence = 0;
186 /* Extract the sequence from SEED. */
187 for (; ISDIGIT (*seed); seed++)
188 skey_sequence = 10 * skey_sequence + *seed - '0';
194 return FTPLOGREFUSED;
196 /* Replace the password with the SKEY response to the
198 pass = skey_response (skey_sequence, seed, pass);
201 #endif /* USE_OPIE */
203 /* Send PASS password. */
204 request = ftp_request ("PASS", pass);
205 nwritten = fd_write (csock, request, strlen (request), -1);
212 /* Get appropriate response. */
213 err = ftp_response (csock, &respline);
219 if (*respline != '2')
230 ip_address_to_port_repr (const ip_address *addr, int port, char *buf,
235 assert (addr != NULL);
236 assert (addr->type == IPV4_ADDRESS);
237 assert (buf != NULL);
238 /* buf must contain the argument of PORT (of the form a,b,c,d,e,f). */
239 assert (buflen >= 6 * 4);
241 ptr = ADDRESS_IPV4_DATA (addr);
242 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
243 ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
244 buf[buflen - 1] = '\0';
247 /* Bind a port and send the appropriate PORT command to the FTP
248 server. Use acceptport after RETR, to get the socket of data
251 ftp_port (int csock, int *local_sock)
254 char *request, *respline;
258 /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
259 char bytes[6 * 4 + 1];
261 /* Get the address of this side of the connection. */
262 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
265 assert (addr.type == IPV4_ADDRESS);
267 /* Setting port to 0 lets the system choose a free port. */
271 *local_sock = bind_local (&addr, &port);
275 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
276 ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
278 /* Send PORT request. */
279 request = ftp_request ("PORT", bytes);
280 nwritten = fd_write (csock, request, strlen (request), -1);
284 fd_close (*local_sock);
289 /* Get appropriate response. */
290 err = ftp_response (csock, &respline);
294 fd_close (*local_sock);
297 if (*respline != '2')
300 fd_close (*local_sock);
309 ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf,
314 assert (addr != NULL);
315 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
316 assert (buf != NULL);
317 /* buf must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
318 assert (buflen >= 21 * 4);
320 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
324 ptr = ADDRESS_IPV4_DATA (addr);
325 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4,
326 ptr[0], ptr[1], ptr[2], ptr[3], 2,
327 (port & 0xff00) >> 8, port & 0xff);
328 buf[buflen - 1] = '\0';
331 ptr = ADDRESS_IPV6_DATA (addr);
332 snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
333 6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
334 ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
335 (port & 0xff00) >> 8, port & 0xff);
336 buf[buflen - 1] = '\0';
341 /* Bind a port and send the appropriate PORT command to the FTP
342 server. Use acceptport after RETR, to get the socket of data
345 ftp_lprt (int csock, int *local_sock)
348 char *request, *respline;
352 /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
353 char bytes[21 * 4 + 1];
355 /* Get the address of this side of the connection. */
356 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
359 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
361 /* Setting port to 0 lets the system choose a free port. */
365 *local_sock = bind_local (&addr, &port);
369 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
370 ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
372 /* Send PORT request. */
373 request = ftp_request ("LPRT", bytes);
374 nwritten = fd_write (csock, request, strlen (request), -1);
378 fd_close (*local_sock);
382 /* Get appropriate response. */
383 err = ftp_response (csock, &respline);
387 fd_close (*local_sock);
390 if (*respline != '2')
393 fd_close (*local_sock);
401 ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf,
406 assert (addr != NULL);
407 assert (addr->type == IPV4_ADDRESS || addr->type == IPV6_ADDRESS);
408 assert (buf != NULL);
409 /* buf must contain the argument of EPRT (of the form |af|addr|port|).
410 * 4 chars for the | separators, INET6_ADDRSTRLEN chars for addr
411 * 1 char for af (1-2) and 5 chars for port (0-65535) */
412 assert (buflen >= 4 + INET6_ADDRSTRLEN + 1 + 5);
414 /* Construct the argument of EPRT (of the form |af|addr|port|). */
415 afnum = (addr->type == IPV4_ADDRESS ? 1 : 2);
416 snprintf (buf, buflen, "|%d|%s|%d|", afnum, pretty_print_address (addr), port);
417 buf[buflen - 1] = '\0';
420 /* Bind a port and send the appropriate PORT command to the FTP
421 server. Use acceptport after RETR, to get the socket of data
424 ftp_eprt (int csock, int *local_sock)
427 char *request, *respline;
431 /* Must contain the argument of EPRT (of the form |af|addr|port|).
432 * 4 chars for the | separators, ENABLE_IPV6_ADDRSTRLEN chars for addr
433 * 1 char for af (1-2) and 5 chars for port (0-65535) */
434 char bytes[4 + INET6_ADDRSTRLEN + 1 + 5 + 1];
436 /* Get the address of this side of the connection. */
437 if (!socket_ip_address (csock, &addr, ENDPOINT_LOCAL))
440 assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
442 /* Setting port to 0 lets the system choose a free port. */
446 *local_sock = bind_local (&addr, &port);
450 /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
451 ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
453 /* Send PORT request. */
454 request = ftp_request ("EPRT", bytes);
455 nwritten = fd_write (csock, request, strlen (request), -1);
459 fd_close (*local_sock);
463 /* Get appropriate response. */
464 err = ftp_response (csock, &respline);
468 fd_close (*local_sock);
471 if (*respline != '2')
474 fd_close (*local_sock);
482 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
483 transfer. Reads the response from server and parses it. Reads the
484 host and port addresses and returns them. */
486 ftp_pasv (int csock, ip_address *addr, int *port)
488 char *request, *respline, *s;
491 unsigned char tmp[6];
493 assert (addr != NULL);
494 assert (port != NULL);
496 memset (addr, 0, sizeof (ip_address));
498 /* Form the request. */
499 request = ftp_request ("PASV", NULL);
501 nwritten = fd_write (csock, request, strlen (request), -1);
508 /* Get the server response. */
509 err = ftp_response (csock, &respline);
515 if (*respline != '2')
520 /* Parse the request. */
522 for (s += 4; *s && !ISDIGIT (*s); s++);
525 for (i = 0; i < 6; i++)
528 for (; ISDIGIT (*s); s++)
529 tmp[i] = (*s - '0') + 10 * tmp[i];
534 /* When on the last number, anything can be a terminator. */
541 addr->type = IPV4_ADDRESS;
542 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
543 *port = ((tmp[4] << 8) & 0xff00) + tmp[5];
549 /* Similar to ftp_lprt, but uses `LPSV' to initiate the passive FTP
550 transfer. Reads the response from server and parses it. Reads the
551 host and port addresses and returns them. */
553 ftp_lpsv (int csock, ip_address *addr, int *port)
555 char *request, *respline, *s;
556 int nwritten, i, af, addrlen, portlen;
558 unsigned char tmp[16];
559 unsigned char tmpprt[2];
561 assert (addr != NULL);
562 assert (port != NULL);
564 memset (addr, 0, sizeof (ip_address));
566 /* Form the request. */
567 request = ftp_request ("LPSV", NULL);
570 nwritten = fd_write (csock, request, strlen (request), -1);
578 /* Get the server response. */
579 err = ftp_response (csock, &respline);
585 if (*respline != '2')
591 /* Parse the response. */
593 for (s += 4; *s && !ISDIGIT (*s); s++);
597 /* First, get the address family */
599 for (; ISDIGIT (*s); s++)
600 af = (*s - '0') + 10 * af;
602 if (af != 4 && af != 6)
608 if (!*s || *s++ != ',')
614 /* Then, get the address length */
616 for (; ISDIGIT (*s); s++)
617 addrlen = (*s - '0') + 10 * addrlen;
619 if (!*s || *s++ != ',')
631 if ((af == 4 && addrlen != 4)
632 || (af == 6 && addrlen != 16))
638 /* Now, we get the actual address */
639 for (i = 0; i < addrlen; i++)
642 for (; ISDIGIT (*s); s++)
643 tmp[i] = (*s - '0') + 10 * tmp[i];
653 /* Now, get the port length */
655 for (; ISDIGIT (*s); s++)
656 portlen = (*s - '0') + 10 * portlen;
658 if (!*s || *s++ != ',')
670 /* Finally, we get the port number */
672 for (; ISDIGIT (*s); s++)
673 tmpprt[0] = (*s - '0') + 10 * tmpprt[0];
675 if (!*s || *s++ != ',')
682 for (; ISDIGIT (*s); s++)
683 tmpprt[1] = (*s - '0') + 10 * tmpprt[1];
689 addr->type = IPV4_ADDRESS;
690 memcpy (ADDRESS_IPV4_DATA (addr), tmp, 4);
691 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
692 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
693 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
694 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
695 DEBUGP (("*port is: %d\n", *port));
700 addr->type = IPV6_ADDRESS;
701 memcpy (ADDRESS_IPV6_DATA (addr), tmp, 16);
702 *port = ((tmpprt[0] << 8) & 0xff00) + tmpprt[1];
703 DEBUGP (("lpsv addr is: %s\n", pretty_print_address(addr)));
704 DEBUGP (("tmpprt[0] is: %d\n", tmpprt[0]));
705 DEBUGP (("tmpprt[1] is: %d\n", tmpprt[1]));
706 DEBUGP (("*port is: %d\n", *port));
713 /* Similar to ftp_eprt, but uses `EPSV' to initiate the passive FTP
714 transfer. Reads the response from server and parses it. Reads the
715 host and port addresses and returns them. */
717 ftp_epsv (int csock, ip_address *ip, int *port)
719 char *request, *respline, *start, delim, *s;
725 assert (port != NULL);
727 /* IP already contains the IP address of the control connection's
728 peer, so we don't need to call socket_ip_address here. */
730 /* Form the request. */
731 /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
732 request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
735 nwritten = fd_write (csock, request, strlen (request), -1);
743 /* Get the server response. */
744 err = ftp_response (csock, &respline);
750 if (*respline != '2')
756 assert (respline != NULL);
758 DEBUGP(("respline is %s\n", respline));
760 /* Parse the response. */
763 /* Skip the useless stuff and get what's inside the parentheses */
764 start = strchr (respline, '(');
771 /* Skip the first two void fields */
774 if (delim < 33 || delim > 126)
780 for (i = 0; i < 2; i++)
789 /* Finally, get the port number */
791 for (i = 1; ISDIGIT (*s); s++)
798 tport = (*s - '0') + 10 * tport;
801 /* Make sure that the response terminates correcty */
821 /* Sends the TYPE request to the server. */
823 ftp_type (int csock, int type)
825 char *request, *respline;
830 /* Construct argument. */
833 /* Send TYPE request. */
834 request = ftp_request ("TYPE", stype);
835 nwritten = fd_write (csock, request, strlen (request), -1);
842 /* Get appropriate response. */
843 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);
884 if (*respline == '5')
889 if (*respline != '2')
899 /* Sends REST command to the FTP server. */
901 ftp_rest (int csock, wgint offset)
903 char *request, *respline;
907 request = ftp_request ("REST", number_to_static_string (offset));
908 nwritten = fd_write (csock, request, strlen (request), -1);
915 /* Get appropriate response. */
916 err = ftp_response (csock, &respline);
922 if (*respline != '3')
932 /* Sends RETR command to the FTP server. */
934 ftp_retr (int csock, const char *file)
936 char *request, *respline;
940 /* Send RETR request. */
941 request = ftp_request ("RETR", file);
942 nwritten = fd_write (csock, request, strlen (request), -1);
949 /* Get appropriate response. */
950 err = ftp_response (csock, &respline);
956 if (*respline == '5')
961 if (*respline != '1')
971 /* Sends the LIST command to the server. If FILE is NULL, send just
972 `LIST' (no space). */
974 ftp_list (int csock, const char *file)
976 char *request, *respline;
980 /* Send LIST request. */
981 request = ftp_request ("LIST", file);
982 nwritten = fd_write (csock, request, strlen (request), -1);
989 /* Get appropriate respone. */
990 err = ftp_response (csock, &respline);
996 if (*respline == '5')
1001 if (*respline != '1')
1011 /* Sends the SYST command to the server. */
1013 ftp_syst (int csock, enum stype *server_type)
1015 char *request, *respline;
1019 /* Send SYST request. */
1020 request = ftp_request ("SYST", NULL);
1021 nwritten = fd_write (csock, request, strlen (request), -1);
1029 /* Get appropriate response. */
1030 err = ftp_response (csock, &respline);
1036 if (*respline == '5')
1042 /* Skip the number (215, but 200 (!!!) in case of VMS) */
1043 strtok (respline, " ");
1045 /* Which system type has been reported (we are interested just in the
1046 first word of the server response)? */
1047 request = strtok (NULL, " ");
1049 if (!strcasecmp (request, "VMS"))
1050 *server_type = ST_VMS;
1051 else if (!strcasecmp (request, "UNIX"))
1052 *server_type = ST_UNIX;
1053 else if (!strcasecmp (request, "WINDOWS_NT")
1054 || !strcasecmp (request, "WINDOWS2000"))
1055 *server_type = ST_WINNT;
1056 else if (!strcasecmp (request, "MACOS"))
1057 *server_type = ST_MACOS;
1058 else if (!strcasecmp (request, "OS/400"))
1059 *server_type = ST_OS400;
1061 *server_type = ST_OTHER;
1068 /* Sends the PWD command to the server. */
1070 ftp_pwd (int csock, char **pwd)
1072 char *request, *respline;
1076 /* Send PWD request. */
1077 request = ftp_request ("PWD", NULL);
1078 nwritten = fd_write (csock, request, strlen (request), -1);
1085 /* Get appropriate response. */
1086 err = ftp_response (csock, &respline);
1092 if (*respline == '5')
1098 /* Skip the number (257), leading citation mark, trailing citation mark
1099 and everything following it. */
1100 strtok (respline, "\"");
1101 request = strtok (NULL, "\"");
1103 /* Has the `pwd' been already allocated? Free! */
1106 *pwd = xstrdup (request);
1113 /* Sends the SIZE command to the server, and returns the value in 'size'.
1114 * If an error occurs, size is set to zero. */
1116 ftp_size (int csock, const char *file, wgint *size)
1118 char *request, *respline;
1122 /* Send PWD request. */
1123 request = ftp_request ("SIZE", file);
1124 nwritten = fd_write (csock, request, strlen (request), -1);
1132 /* Get appropriate response. */
1133 err = ftp_response (csock, &respline);
1140 if (*respline == '5')
1143 * Probably means SIZE isn't supported on this server.
1144 * Error is nonfatal since SIZE isn't in RFC 959
1152 *size = str_to_wgint (respline + 4, NULL, 10);
1156 * Couldn't parse the response for some reason. On the (few)
1157 * tests I've done, the response is 213 <SIZE> with nothing else -
1158 * maybe something a bit more resilient is necessary. It's not a
1159 * fatal error, however.
1171 /* If URL's params are of the form "type=X", return character X.
1172 Otherwise, return 'I' (the default type). */
1174 ftp_process_type (const char *params)
1177 && 0 == strncasecmp (params, "type=", 5)
1178 && params[5] != '\0')
1179 return TOUPPER (params[5]);