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>
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')
244 /* Bind a port and send the appropriate PORT command to the FTP
245 server. Use acceptport after RETR, to get the socket of data
248 ftp_port (struct rbuf *rbuf)
251 char *request, *respline, *bytes;
252 unsigned char *in_addr;
256 /* Setting port to 0 lets the system choose a free port. */
259 err = bindport (&port);
262 /* Get the address of this side of the connection. */
263 if (!(in_addr = conaddr (RBUF_FD (rbuf))))
265 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
266 bytes = (char *)alloca (6 * 4 + 1);
267 sprintf (bytes, "%d,%d,%d,%d,%d,%d", in_addr[0], in_addr[1],
268 in_addr[2], in_addr[3], (unsigned) (port & 0xff00) >> 8,
270 /* Send PORT request. */
271 request = ftp_request ("PORT", bytes);
272 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
279 /* Get appropriate response. */
280 err = ftp_response (rbuf, &respline);
286 if (*respline != '2')
295 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
296 transfer. Reads the response from server and parses it. Reads the
297 host and port addresses and returns them. */
299 ftp_pasv (struct rbuf *rbuf, unsigned char *addr)
301 char *request, *respline, *s;
305 /* Form the request. */
306 request = ftp_request ("PASV", NULL);
308 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
315 /* Get the server response. */
316 err = ftp_response (rbuf, &respline);
322 if (*respline != '2')
327 /* Parse the request. */
329 for (s += 4; *s && !ISDIGIT (*s); s++);
332 for (i = 0; i < 6; i++)
335 for (; ISDIGIT (*s); s++)
336 addr[i] = (*s - '0') + 10 * addr[i];
341 /* When on the last number, anything can be a terminator. */
350 /* Sends the TYPE request to the server. */
352 ftp_type (struct rbuf *rbuf, int type)
354 char *request, *respline;
359 /* Construct argument. */
362 /* Send TYPE request. */
363 request = ftp_request ("TYPE", stype);
364 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
371 /* Get appropriate response. */
372 err = ftp_response (rbuf, &respline);
378 if (*respline != '2')
381 return FTPUNKNOWNTYPE;
388 /* Changes the working directory by issuing a CWD command to the
391 ftp_cwd (struct rbuf *rbuf, const char *dir)
393 char *request, *respline;
397 /* Send CWD request. */
398 request = ftp_request ("CWD", dir);
399 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
406 /* Get appropriate response. */
407 err = ftp_response (rbuf, &respline);
413 if (*respline == '5')
418 if (*respline != '2')
428 /* Sends REST command to the FTP server. */
430 ftp_rest (struct rbuf *rbuf, long offset)
432 char *request, *respline;
435 static char numbuf[20]; /* Buffer for the number */
437 long_to_string (numbuf, offset);
438 request = ftp_request ("REST", numbuf);
439 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
446 /* Get appropriate response. */
447 err = ftp_response (rbuf, &respline);
453 if (*respline != '3')
463 /* Sends RETR command to the FTP server. */
465 ftp_retr (struct rbuf *rbuf, const char *file)
467 char *request, *respline;
471 /* Send RETR request. */
472 request = ftp_request ("RETR", file);
473 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
480 /* Get appropriate response. */
481 err = ftp_response (rbuf, &respline);
487 if (*respline == '5')
492 if (*respline != '1')
502 /* Sends the LIST command to the server. If FILE is NULL, send just
503 `LIST' (no space). */
505 ftp_list (struct rbuf *rbuf, const char *file)
507 char *request, *respline;
511 /* Send LIST request. */
512 request = ftp_request ("LIST", file);
513 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
520 /* Get appropriate respone. */
521 err = ftp_response (rbuf, &respline);
527 if (*respline == '5')
532 if (*respline != '1')
542 /* Sends the SYST command to the server. */
544 ftp_syst (struct rbuf *rbuf, enum stype *server_type)
546 char *request, *respline;
550 /* Send SYST request. */
551 request = ftp_request ("SYST", NULL);
552 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
559 /* Get appropriate response. */
560 err = ftp_response (rbuf, &respline);
566 if (*respline == '5')
572 /* Skip the number (215, but 200 (!!!) in case of VMS) */
573 strtok (respline, " ");
575 /* Which system type has been reported (we are interested just in the
576 first word of the server response)? */
577 request = strtok (NULL, " ");
579 if (!strcasecmp (request, "VMS"))
580 *server_type = ST_VMS;
582 if (!strcasecmp (request, "UNIX"))
583 *server_type = ST_UNIX;
585 if (!strcasecmp (request, "WINDOWS_NT"))
586 *server_type = ST_WINNT;
588 *server_type = ST_OTHER;
595 /* Sends the PWD command to the server. */
597 ftp_pwd (struct rbuf *rbuf, char **pwd)
599 char *request, *respline;
603 /* Send PWD request. */
604 request = ftp_request ("PWD", NULL);
605 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
612 /* Get appropriate response. */
613 err = ftp_response (rbuf, &respline);
619 if (*respline == '5')
625 /* Skip the number (257), leading citation mark, trailing citation mark
626 and everything following it. */
627 strtok (respline, "\"");
628 request = strtok (NULL, "\"");
630 /* Has the `pwd' been already allocated? Free! */
633 *pwd = xstrdup (request);