2 Copyright (C) 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program 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 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
33 #include <sys/types.h>
52 char ftp_last_respline[128];
55 /* Get the response of FTP server and allocate enough room to handle
56 it. <CR> and <LF> characters are stripped from the line, and the
57 line is 0-terminated. All the response lines but the last one are
58 skipped. The last line is determined as described in RFC959. */
60 ftp_response (struct rbuf *rbuf, char **line)
65 *line = (char *)xmalloc (bufsize);
72 *line = (char *)xrealloc (*line, (bufsize <<= 1));
73 res = RBUF_READCHAR (rbuf, *line + i);
74 /* RES is number of bytes read. */
77 if ((*line)[i] == '\n')
81 if (i > 0 && (*line)[i - 1] == '\r')
82 (*line)[i - 1] = '\0';
89 if (opt.server_response)
90 logprintf (LOG_ALWAYS, "%s\n", *line);
92 DEBUGP (("%s\n", *line));
94 while (!(i >= 3 && ISDIGIT (**line) && ISDIGIT ((*line)[1]) &&
95 ISDIGIT ((*line)[2]) && (*line)[3] == ' '));
96 strncpy (ftp_last_respline, *line, sizeof (ftp_last_respline));
97 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
101 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
102 it if printing is required. If VALUE is NULL, just use
105 ftp_request (const char *command, const char *value)
107 char *res = (char *)xmalloc (strlen (command)
108 + (value ? (1 + strlen (value)) : 0)
110 sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
111 if (opt.server_response)
113 /* Hack: don't print out password. */
114 if (strncmp (res, "PASS", 4) != 0)
115 logprintf (LOG_ALWAYS, "--> %s\n", res);
117 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n");
120 DEBUGP (("\n--> %s\n", res));
125 const char *calculate_skey_response PARAMS ((int, const char *, const char *));
128 /* Sends the USER and PASS commands to the server, to control
129 connection socket csock. */
131 ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
134 char *request, *respline;
138 err = ftp_response (rbuf, &respline);
144 if (*respline != '2')
150 /* Send USER username. */
151 request = ftp_request ("USER", acc);
152 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
159 /* Get appropriate response. */
160 err = ftp_response (rbuf, &respline);
166 /* An unprobable possibility of logging without a password. */
167 if (*respline == '2')
172 /* Else, only response 3 is appropriate. */
173 if (*respline != '3')
176 return FTPLOGREFUSED;
180 static const char *skey_head[] = {
186 for (i = 0; i < ARRAY_SIZE (skey_head); i++)
188 if (strncasecmp (skey_head[i], respline, strlen (skey_head[i])) == 0)
191 if (i < ARRAY_SIZE (skey_head))
194 int skey_sequence = 0;
196 for (cp = respline + strlen (skey_head[i]);
197 '0' <= *cp && *cp <= '9';
200 skey_sequence = skey_sequence * 10 + *cp - '0';
208 return FTPLOGREFUSED;
210 if ((cp = calculate_skey_response (skey_sequence, cp, pass)) == 0)
215 #endif /* USE_OPIE */
217 /* Send PASS password. */
218 request = ftp_request ("PASS", pass);
219 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
226 /* Get appropriate response. */
227 err = ftp_response (rbuf, &respline);
233 if (*respline != '2')
243 /* Bind a port and send the appropriate PORT command to the FTP
244 server. Use acceptport after RETR, to get the socket of data
247 ftp_port (struct rbuf *rbuf)
250 char *request, *respline, *bytes;
251 unsigned char *in_addr;
255 /* Setting port to 0 lets the system choose a free port. */
258 err = bindport (&port);
261 /* Get the address of this side of the connection. */
262 if (!(in_addr = conaddr (RBUF_FD (rbuf))))
264 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
265 bytes = (char *)alloca (6 * 4 + 1);
266 sprintf (bytes, "%d,%d,%d,%d,%d,%d", in_addr[0], in_addr[1],
267 in_addr[2], in_addr[3], (unsigned) (port & 0xff00) >> 8,
269 /* Send PORT request. */
270 request = ftp_request ("PORT", bytes);
271 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
278 /* Get appropriate response. */
279 err = ftp_response (rbuf, &respline);
285 if (*respline != '2')
294 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
295 transfer. Reads the response from server and parses it. Reads the
296 host and port addresses and returns them. */
298 ftp_pasv (struct rbuf *rbuf, unsigned char *addr)
300 char *request, *respline, *s;
304 /* Form the request. */
305 request = ftp_request ("PASV", NULL);
307 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
314 /* Get the server response. */
315 err = ftp_response (rbuf, &respline);
321 if (*respline != '2')
326 /* Parse the request. */
328 for (s += 4; *s && !ISDIGIT (*s); s++);
331 for (i = 0; i < 6; i++)
334 for (; ISDIGIT (*s); s++)
335 addr[i] = (*s - '0') + 10 * addr[i];
340 /* When on the last number, anything can be a terminator. */
349 /* Sends the TYPE request to the server. */
351 ftp_type (struct rbuf *rbuf, int type)
353 char *request, *respline;
358 /* Construct argument. */
361 /* Send TYPE request. */
362 request = ftp_request ("TYPE", stype);
363 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
370 /* Get appropriate response. */
371 err = ftp_response (rbuf, &respline);
377 if (*respline != '2')
380 return FTPUNKNOWNTYPE;
387 /* Changes the working directory by issuing a CWD command to the
390 ftp_cwd (struct rbuf *rbuf, const char *dir)
392 char *request, *respline;
396 /* Send CWD request. */
397 request = ftp_request ("CWD", dir);
398 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
405 /* Get appropriate response. */
406 err = ftp_response (rbuf, &respline);
412 if (*respline == '5')
417 if (*respline != '2')
427 /* Sends REST command to the FTP server. */
429 ftp_rest (struct rbuf *rbuf, long offset)
431 char *request, *respline;
434 static char numbuf[20]; /* Buffer for the number */
436 long_to_string (numbuf, offset);
437 request = ftp_request ("REST", numbuf);
438 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
445 /* Get appropriate response. */
446 err = ftp_response (rbuf, &respline);
452 if (*respline != '3')
462 /* Sends RETR command to the FTP server. */
464 ftp_retr (struct rbuf *rbuf, const char *file)
466 char *request, *respline;
470 /* Send RETR request. */
471 request = ftp_request ("RETR", file);
472 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
479 /* Get appropriate response. */
480 err = ftp_response (rbuf, &respline);
486 if (*respline == '5')
491 if (*respline != '1')
501 /* Sends the LIST command to the server. If FILE is NULL, send just
502 `LIST' (no space). */
504 ftp_list (struct rbuf *rbuf, const char *file)
506 char *request, *respline;
510 /* Send LIST request. */
511 request = ftp_request ("LIST", file);
512 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
519 /* Get appropriate respone. */
520 err = ftp_response (rbuf, &respline);
526 if (*respline == '5')
531 if (*respline != '1')