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>
46 char ftp_last_respline[128];
49 /* Get the response of FTP server and allocate enough room to handle
50 it. <CR> and <LF> characters are stripped from the line, and the
51 line is 0-terminated. All the response lines but the last one are
52 skipped. The last line is determined as described in RFC959. */
54 ftp_response (struct rbuf *rbuf, char **line)
59 *line = (char *)xmalloc (bufsize);
66 *line = (char *)xrealloc (*line, (bufsize <<= 1));
67 res = RBUF_READCHAR (rbuf, *line + i);
68 /* RES is number of bytes read. */
71 if ((*line)[i] == '\n')
75 if (i > 0 && (*line)[i - 1] == '\r')
76 (*line)[i - 1] = '\0';
83 if (opt.server_response)
84 logprintf (LOG_ALWAYS, "%s\n", *line);
86 DEBUGP (("%s\n", *line));
88 while (!(i >= 3 && ISDIGIT (**line) && ISDIGIT ((*line)[1]) &&
89 ISDIGIT ((*line)[2]) && (*line)[3] == ' '));
90 strncpy (ftp_last_respline, *line, sizeof (ftp_last_respline));
91 ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
95 /* Returns the malloc-ed FTP request, ending with <CR><LF>, printing
96 it if printing is required. If VALUE is NULL, just use
99 ftp_request (const char *command, const char *value)
101 char *res = (char *)xmalloc (strlen (command)
102 + (value ? (1 + strlen (value)) : 0)
104 sprintf (res, "%s%s%s\r\n", command, value ? " " : "", value ? value : "");
105 if (opt.server_response)
107 /* Hack: don't print out password. */
108 if (strncmp (res, "PASS", 4) != 0)
109 logprintf (LOG_ALWAYS, "--> %s\n", res);
111 logputs (LOG_ALWAYS, "--> PASS Turtle Power!\n");
114 DEBUGP (("\n--> %s\n", res));
119 const char *calculate_skey_response PARAMS ((int, const char *, const char *));
122 /* Sends the USER and PASS commands to the server, to control
123 connection socket csock. */
125 ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
128 char *request, *respline;
132 err = ftp_response (rbuf, &respline);
138 if (*respline != '2')
144 /* Send USER username. */
145 request = ftp_request ("USER", acc);
146 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
153 /* Get appropriate response. */
154 err = ftp_response (rbuf, &respline);
160 /* An unprobable possibility of logging without a password. */
161 if (*respline == '2')
166 /* Else, only response 3 is appropriate. */
167 if (*respline != '3')
170 return FTPLOGREFUSED;
174 static const char *skey_head[] = {
180 for (i = 0; i < ARRAY_SIZE (skey_head); i++)
182 if (strncasecmp (skey_head[i], respline, strlen (skey_head[i])) == 0)
185 if (i < ARRAY_SIZE (skey_head))
188 int skey_sequence = 0;
190 for (cp = respline + strlen (skey_head[i]);
191 '0' <= *cp && *cp <= '9';
194 skey_sequence = skey_sequence * 10 + *cp - '0';
202 return FTPLOGREFUSED;
204 if ((cp = calculate_skey_response (skey_sequence, cp, pass)) == 0)
209 #endif /* USE_OPIE */
211 /* Send PASS password. */
212 request = ftp_request ("PASS", pass);
213 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
220 /* Get appropriate response. */
221 err = ftp_response (rbuf, &respline);
227 if (*respline != '2')
237 /* Bind a port and send the appropriate PORT command to the FTP
238 server. Use acceptport after RETR, to get the socket of data
241 ftp_port (struct rbuf *rbuf)
244 char *request, *respline, *bytes;
245 unsigned char *in_addr;
249 /* Setting port to 0 lets the system choose a free port. */
252 err = bindport (&port);
255 /* Get the address of this side of the connection. */
256 if (!(in_addr = conaddr (RBUF_FD (rbuf))))
258 /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
259 bytes = (char *)alloca (6 * 4 + 1);
260 sprintf (bytes, "%d,%d,%d,%d,%d,%d", in_addr[0], in_addr[1],
261 in_addr[2], in_addr[3], (unsigned) (port & 0xff00) >> 8,
263 /* Send PORT request. */
264 request = ftp_request ("PORT", bytes);
265 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
272 /* Get appropriate response. */
273 err = ftp_response (rbuf, &respline);
279 if (*respline != '2')
288 /* Similar to ftp_port, but uses `PASV' to initiate the passive FTP
289 transfer. Reads the response from server and parses it. Reads the
290 host and port addresses and returns them. */
292 ftp_pasv (struct rbuf *rbuf, unsigned char *addr)
294 char *request, *respline, *s;
298 /* Form the request. */
299 request = ftp_request ("PASV", NULL);
301 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
308 /* Get the server response. */
309 err = ftp_response (rbuf, &respline);
315 if (*respline != '2')
320 /* Parse the request. */
322 for (s += 4; *s && !ISDIGIT (*s); s++);
325 for (i = 0; i < 6; i++)
328 for (; ISDIGIT (*s); s++)
329 addr[i] = (*s - '0') + 10 * addr[i];
334 /* When on the last number, anything can be a terminator. */
343 /* Sends the TYPE request to the server. */
345 ftp_type (struct rbuf *rbuf, int type)
347 char *request, *respline;
352 /* Construct argument. */
355 /* Send TYPE request. */
356 request = ftp_request ("TYPE", stype);
357 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
364 /* Get appropriate response. */
365 err = ftp_response (rbuf, &respline);
371 if (*respline != '2')
374 return FTPUNKNOWNTYPE;
381 /* Changes the working directory by issuing a CWD command to the
384 ftp_cwd (struct rbuf *rbuf, const char *dir)
386 char *request, *respline;
390 /* Send CWD request. */
391 request = ftp_request ("CWD", dir);
392 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
399 /* Get appropriate response. */
400 err = ftp_response (rbuf, &respline);
406 if (*respline == '5')
411 if (*respline != '2')
421 /* Sends REST command to the FTP server. */
423 ftp_rest (struct rbuf *rbuf, long offset)
425 char *request, *respline;
428 static char numbuf[20]; /* Buffer for the number */
430 long_to_string (numbuf, offset);
431 request = ftp_request ("REST", numbuf);
432 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
439 /* Get appropriate response. */
440 err = ftp_response (rbuf, &respline);
446 if (*respline != '3')
456 /* Sends RETR command to the FTP server. */
458 ftp_retr (struct rbuf *rbuf, const char *file)
460 char *request, *respline;
464 /* Send RETR request. */
465 request = ftp_request ("RETR", file);
466 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
473 /* Get appropriate response. */
474 err = ftp_response (rbuf, &respline);
480 if (*respline == '5')
485 if (*respline != '1')
495 /* Sends the LIST command to the server. If FILE is NULL, send just
496 `LIST' (no space). */
498 ftp_list (struct rbuf *rbuf, const char *file)
500 char *request, *respline;
504 /* Send LIST request. */
505 request = ftp_request ("LIST", file);
506 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
513 /* Get appropriate respone. */
514 err = ftp_response (rbuf, &respline);
520 if (*respline == '5')
525 if (*respline != '1')
535 /* Sends the SYST command to the server. */
537 ftp_syst (struct rbuf *rbuf, enum stype *server_type)
539 char *request, *respline;
543 /* Send SYST request. */
544 request = ftp_request ("SYST", NULL);
545 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
552 /* Get appropriate response. */
553 err = ftp_response (rbuf, &respline);
559 if (*respline == '5')
565 /* Skip the number (215, but 200 (!!!) in case of VMS) */
566 strtok (respline, " ");
568 /* Which system type has been reported (we are interested just in the
569 first word of the server response)? */
570 request = strtok (NULL, " ");
572 if (!strcasecmp (request, "VMS"))
573 *server_type = ST_VMS;
575 if (!strcasecmp (request, "UNIX"))
576 *server_type = ST_UNIX;
578 if (!strcasecmp (request, "WINDOWS_NT"))
579 *server_type = ST_WINNT;
581 if (!strcasecmp (request, "MACOS"))
582 *server_type = ST_MACOS;
584 *server_type = ST_OTHER;
591 /* Sends the PWD command to the server. */
593 ftp_pwd (struct rbuf *rbuf, char **pwd)
595 char *request, *respline;
599 /* Send PWD request. */
600 request = ftp_request ("PWD", NULL);
601 nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
608 /* Get appropriate response. */
609 err = ftp_response (rbuf, &respline);
615 if (*respline == '5')
621 /* Skip the number (257), leading citation mark, trailing citation mark
622 and everything following it. */
623 strtok (respline, "\"");
624 request = strtok (NULL, "\"");
626 /* Has the `pwd' been already allocated? Free! */
629 *pwd = xstrdup (request);