]> sjero.net Git - wget/blobdiff - src/ftp-basic.c
[svn] No longer include INET headers in ftp-basic.c.
[wget] / src / ftp-basic.c
index 8409579394b57ec4654e8739fd9f73ca0227bcb1..6a1c045390dcfb4c0105b1fe40a7d6c079df6ae9 100644 (file)
@@ -44,17 +44,6 @@ so, delete this exception statement from your version.  */
 #endif
 #include <sys/types.h>
 
-/* For inet_ntop. */
-#ifndef WINDOWS
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#endif
-
-#ifdef WINDOWS
-# include <winsock.h>
-#endif
-
 #include "wget.h"
 #include "utils.h"
 #include "rbuf.h"
@@ -134,10 +123,6 @@ ftp_request (const char *command, const char *value)
   return res;
 }
 
-#ifdef USE_OPIE
-const char *calculate_skey_response PARAMS ((int, const char *, const char *));
-#endif
-
 /* Sends the USER and PASS commands to the server, to control
    connection socket csock.  */
 uerr_t
@@ -162,7 +147,7 @@ ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
   xfree (respline);
   /* Send USER username.  */
   request = ftp_request ("USER", acc);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -195,41 +180,41 @@ ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
       "331 opiekey "
     };
     int i;
+    const char *seed = NULL;
 
     for (i = 0; i < countof (skey_head); i++)
       {
-        if (strncasecmp (skey_head[i], respline, strlen (skey_head[i])) == 0)
-          break;
+       int l = strlen (skey_head[i]);
+        if (0 == strncasecmp (skey_head[i], respline, l))
+         {
+           seed = respline + l;
+           break;
+         }
       }
-    if (i < countof (skey_head))
+    if (seed)
       {
-        const char *cp;
         int skey_sequence = 0;
 
-        for (cp = respline + strlen (skey_head[i]);
-             '0' <= *cp && *cp <= '9';
-             cp++)
-          {
-            skey_sequence = skey_sequence * 10 + *cp - '0';
-          }
-        if (*cp == ' ')
-          cp++;
+       /* Extract the sequence from SEED.  */
+       for (; ISDIGIT (*seed); seed++)
+         skey_sequence = 10 * skey_sequence + *seed - '0';
+       if (*seed == ' ')
+         ++seed;
         else
           {
-          bad:
             xfree (respline);
             return FTPLOGREFUSED;
           }
-        if ((cp = calculate_skey_response (skey_sequence, cp, pass)) == 0)
-          goto bad;
-        pass = cp;
+       /* Replace the password with the SKEY response to the
+          challenge.  */
+        pass = skey_response (skey_sequence, seed, pass);
       }
   }
 #endif /* USE_OPIE */
   xfree (respline);
   /* Send PASS password.  */
   request = ftp_request ("PASS", pass);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -254,7 +239,7 @@ ftp_login (struct rbuf *rbuf, const char *acc, const char *pass)
 }
 
 static void
-ip_address_to_port_repr (const ip_address *addr, unsigned short port, char *buf, 
+ip_address_to_port_repr (const ip_address *addr, int port, char *buf, 
                          size_t buflen)
 {
   unsigned char *ptr;
@@ -267,7 +252,7 @@ ip_address_to_port_repr (const ip_address *addr, unsigned short port, char *buf,
 
   ptr = ADDRESS_IPV4_DATA (addr);
   snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d", ptr[0], ptr[1],
-            ptr[2], ptr[3], (unsigned) (port & 0xff00) >> 8, port & 0xff);
+            ptr[2], ptr[3], (port & 0xff00) >> 8, port & 0xff);
   buf[buflen - 1] = '\0';
 }
 
@@ -275,13 +260,13 @@ ip_address_to_port_repr (const ip_address *addr, unsigned short port, char *buf,
    server.  Use acceptport after RETR, to get the socket of data
    connection.  */
 uerr_t
-ftp_port (struct rbuf *rbuf)
+ftp_port (struct rbuf *rbuf, int *local_sock)
 {
   uerr_t err;
   char *request, *respline;
   ip_address addr;
   int nwritten;
-  unsigned short port;
+  int port;
   /* Must contain the argument of PORT (of the form a,b,c,d,e,f). */
   char bytes[6 * 4 + 1];
 
@@ -289,8 +274,8 @@ ftp_port (struct rbuf *rbuf)
   assert (rbuf_initialized_p (rbuf));
 
   /* Get the address of this side of the connection. */
-  if (!conaddr (RBUF_FD (rbuf), &addr))
-    return BINDERR;
+  if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
+    return FTPSYSERR;
 
   assert (addr.type == IPV4_ADDRESS);
 
@@ -298,20 +283,20 @@ ftp_port (struct rbuf *rbuf)
   port = 0;
 
   /* Bind the port.  */
-  err = bindport (&addr, &port);
-  if (err != BINDOK)
-    return err;
+  *local_sock = bind_local (&addr, &port);
+  if (*local_sock < 0)
+    return FTPSYSERR;
 
   /* Construct the argument of PORT (of the form a,b,c,d,e,f). */
   ip_address_to_port_repr (&addr, port, bytes, sizeof (bytes));
 
   /* Send PORT request.  */
   request = ftp_request ("PORT", bytes);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
-      closeport (-1);
+      xclose (*local_sock);
       return WRITEFAILED;
     }
   xfree (request);
@@ -321,13 +306,13 @@ ftp_port (struct rbuf *rbuf)
   if (err != FTPOK)
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return err;
     }
   if (*respline != '2')
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return FTPPORTERR;
     }
   xfree (respline);
@@ -336,7 +321,7 @@ ftp_port (struct rbuf *rbuf)
 
 #ifdef ENABLE_IPV6
 static void
-ip_address_to_lprt_repr (const ip_address *addr, unsigned short port, char *buf, 
+ip_address_to_lprt_repr (const ip_address *addr, int port, char *buf, 
                          size_t buflen)
 {
   unsigned char *ptr;
@@ -354,7 +339,7 @@ ip_address_to_lprt_repr (const ip_address *addr, unsigned short port, char *buf,
        ptr = ADDRESS_IPV4_DATA (addr);
         snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d", 4, 4, 
                   ptr[0], ptr[1], ptr[2], ptr[3], 2,
-                  (unsigned) (port & 0xff00) >> 8, port & 0xff);
+                  (port & 0xff00) >> 8, port & 0xff);
         buf[buflen - 1] = '\0';
         break;
       case IPV6_ADDRESS: 
@@ -362,7 +347,7 @@ ip_address_to_lprt_repr (const ip_address *addr, unsigned short port, char *buf,
        snprintf (buf, buflen, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
                  6, 16, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7], 
                  ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15], 2,
-                 (unsigned) (port & 0xff00) >> 8, port & 0xff);
+                 (port & 0xff00) >> 8, port & 0xff);
        buf[buflen - 1] = '\0';
        break;
     }
@@ -372,13 +357,13 @@ ip_address_to_lprt_repr (const ip_address *addr, unsigned short port, char *buf,
    server.  Use acceptport after RETR, to get the socket of data
    connection.  */
 uerr_t
-ftp_lprt (struct rbuf *rbuf)
+ftp_lprt (struct rbuf *rbuf, int *local_sock)
 {
   uerr_t err;
   char *request, *respline;
   ip_address addr;
   int nwritten;
-  unsigned short port;
+  int port;
   /* Must contain the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
   char bytes[21 * 4 + 1];
 
@@ -386,8 +371,8 @@ ftp_lprt (struct rbuf *rbuf)
   assert (rbuf_initialized_p (rbuf));
 
   /* Get the address of this side of the connection. */
-  if (!conaddr (RBUF_FD (rbuf), &addr))
-    return BINDERR;
+  if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
+    return FTPSYSERR;
 
   assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
 
@@ -395,20 +380,20 @@ ftp_lprt (struct rbuf *rbuf)
   port = 0;
 
   /* Bind the port.  */
-  err = bindport (&addr, &port);
-  if (err != BINDOK)
-    return err;
+  *local_sock = bind_local (&addr, &port);
+  if (*local_sock < 0)
+    return FTPSYSERR;
 
   /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
   ip_address_to_lprt_repr (&addr, port, bytes, sizeof (bytes));
 
   /* Send PORT request.  */
   request = ftp_request ("LPRT", bytes);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
-      closeport (-1);
+      xclose (*local_sock);
       return WRITEFAILED;
     }
   xfree (request);
@@ -417,13 +402,13 @@ ftp_lprt (struct rbuf *rbuf)
   if (err != FTPOK)
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return err;
     }
   if (*respline != '2')
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return FTPPORTERR;
     }
   xfree (respline);
@@ -431,7 +416,7 @@ ftp_lprt (struct rbuf *rbuf)
 }
 
 static void
-ip_address_to_eprt_repr (const ip_address *addr, unsigned short port, char *buf, 
+ip_address_to_eprt_repr (const ip_address *addr, int port, char *buf, 
                          size_t buflen)
 {
   int afnum;
@@ -454,13 +439,13 @@ ip_address_to_eprt_repr (const ip_address *addr, unsigned short port, char *buf,
    server.  Use acceptport after RETR, to get the socket of data
    connection.  */
 uerr_t
-ftp_eprt (struct rbuf *rbuf)
+ftp_eprt (struct rbuf *rbuf, int *local_sock)
 {
   uerr_t err;
   char *request, *respline;
   ip_address addr;
   int nwritten;
-  unsigned short port;
+  int port;
   /* Must contain the argument of EPRT (of the form |af|addr|port|). 
    * 4 chars for the | separators, ENABLE_IPV6_ADDRSTRLEN chars for addr  
    * 1 char for af (1-2) and 5 chars for port (0-65535) */
@@ -470,8 +455,8 @@ ftp_eprt (struct rbuf *rbuf)
   assert (rbuf_initialized_p(rbuf));
 
   /* Get the address of this side of the connection. */
-  if (!conaddr (RBUF_FD (rbuf), &addr))
-    return BINDERR;
+  if (!socket_ip_address (RBUF_FD (rbuf), &addr, ENDPOINT_LOCAL))
+    return FTPSYSERR;
 
   assert (addr.type == IPV4_ADDRESS || addr.type == IPV6_ADDRESS);
 
@@ -479,20 +464,20 @@ ftp_eprt (struct rbuf *rbuf)
   port = 0;
 
   /* Bind the port.  */
-  err = bindport (&addr, &port);
-  if (err != BINDOK)
-    return err;
+  *local_sock = bind_local (&addr, &port);
+  if (*local_sock < 0)
+    return FTPSYSERR;
 
   /* Construct the argument of LPRT (of the form af,n,h1,h2,...,hn,p1,p2). */
   ip_address_to_eprt_repr (&addr, port, bytes, sizeof (bytes));
 
   /* Send PORT request.  */
   request = ftp_request ("EPRT", bytes);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
-      closeport (-1);
+      xclose (*local_sock);
       return WRITEFAILED;
     }
   xfree (request);
@@ -501,13 +486,13 @@ ftp_eprt (struct rbuf *rbuf)
   if (err != FTPOK)
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return err;
     }
   if (*respline != '2')
     {
       xfree (respline);
-      closeport (-1);
+      xclose (*local_sock);
       return FTPPORTERR;
     }
   xfree (respline);
@@ -519,7 +504,7 @@ ftp_eprt (struct rbuf *rbuf)
    transfer.  Reads the response from server and parses it.  Reads the
    host and port addresses and returns them.  */
 uerr_t
-ftp_pasv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
+ftp_pasv (struct rbuf *rbuf, ip_address *addr, int *port)
 {
   char *request, *respline, *s;
   int nwritten, i;
@@ -536,7 +521,7 @@ ftp_pasv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
   /* Form the request.  */
   request = ftp_request ("PASV", NULL);
   /* And send it.  */
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -588,7 +573,7 @@ ftp_pasv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
    transfer.  Reads the response from server and parses it.  Reads the
    host and port addresses and returns them.  */
 uerr_t
-ftp_lpsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
+ftp_lpsv (struct rbuf *rbuf, ip_address *addr, int *port)
 {
   char *request, *respline, *s;
   int nwritten, i, af, addrlen, portlen;
@@ -607,7 +592,7 @@ ftp_lpsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
   request = ftp_request ("LPSV", NULL);
 
   /* And send it.  */
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -754,36 +739,27 @@ ftp_lpsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
    transfer.  Reads the response from server and parses it.  Reads the
    host and port addresses and returns them.  */
 uerr_t
-ftp_epsv (struct rbuf *rbuf, ip_address *addr, unsigned short *port)
+ftp_epsv (struct rbuf *rbuf, ip_address *ip, int *port)
 {
   char *request, *respline, *start, delim, *s;
   int nwritten, i;
   uerr_t err;
-  unsigned short tport;
-  socklen_t addrlen;
-  struct sockaddr_storage ss;
-  struct sockaddr *sa = (struct sockaddr *)&ss;
+  int tport;
 
   assert (rbuf != NULL);
   assert (rbuf_initialized_p(rbuf));
-  assert (addr != NULL);
+  assert (ip != NULL);
   assert (port != NULL);
 
-  addrlen = sizeof (ss);
-  if (getpeername (rbuf->fd, sa, &addrlen) < 0)
-    /* Mauro Tortonesi: HOW DO WE HANDLE THIS ERROR? */
-    return CONPORTERR;
-
-  assert (sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
-
-  sockaddr_get_address (sa, NULL, addr);
+  /* IP already contains the IP address of the control connection's
+     peer, so we don't need to call socket_ip_address here.  */
 
   /* Form the request.  */
   /* EPSV 1 means that we ask for IPv4 and EPSV 2 means that we ask for IPv6. */
-  request = ftp_request ("EPSV", (sa->sa_family == AF_INET ? "1" : "2"));
+  request = ftp_request ("EPSV", (ip->type == IPV4_ADDRESS ? "1" : "2"));
 
   /* And send it.  */
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -883,7 +859,7 @@ ftp_type (struct rbuf *rbuf, int type)
   stype[1] = 0;
   /* Send TYPE request.  */
   request = ftp_request ("TYPE", stype);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -918,7 +894,7 @@ ftp_cwd (struct rbuf *rbuf, const char *dir)
 
   /* Send CWD request.  */
   request = ftp_request ("CWD", dir);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -958,7 +934,7 @@ ftp_rest (struct rbuf *rbuf, long offset)
 
   number_to_string (numbuf, offset);
   request = ftp_request ("REST", numbuf);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -992,7 +968,7 @@ ftp_retr (struct rbuf *rbuf, const char *file)
 
   /* Send RETR request.  */
   request = ftp_request ("RETR", file);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -1032,7 +1008,7 @@ ftp_list (struct rbuf *rbuf, const char *file)
 
   /* Send LIST request.  */
   request = ftp_request ("LIST", file);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -1071,7 +1047,7 @@ ftp_syst (struct rbuf *rbuf, enum stype *server_type)
 
   /* Send SYST request.  */
   request = ftp_request ("SYST", NULL);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -1103,7 +1079,8 @@ ftp_syst (struct rbuf *rbuf, enum stype *server_type)
     *server_type = ST_VMS;
   else if (!strcasecmp (request, "UNIX"))
     *server_type = ST_UNIX;
-  else if (!strcasecmp (request, "WINDOWS_NT"))
+  else if (!strcasecmp (request, "WINDOWS_NT")
+          || !strcasecmp (request, "WINDOWS2000"))
     *server_type = ST_WINNT;
   else if (!strcasecmp (request, "MACOS"))
     *server_type = ST_MACOS;
@@ -1127,7 +1104,7 @@ ftp_pwd (struct rbuf *rbuf, char **pwd)
 
   /* Send PWD request.  */
   request = ftp_request ("PWD", NULL);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);
@@ -1153,7 +1130,7 @@ ftp_pwd (struct rbuf *rbuf, char **pwd)
   request = strtok (NULL, "\"");
 
   /* Has the `pwd' been already allocated?  Free! */
-  FREE_MAYBE (*pwd);
+  xfree_null (*pwd);
 
   *pwd = xstrdup (request);
 
@@ -1173,7 +1150,7 @@ ftp_size (struct rbuf *rbuf, const char *file, long int *size)
 
   /* Send PWD request.  */
   request = ftp_request ("SIZE", file);
-  nwritten = iwrite (RBUF_FD (rbuf), request, strlen (request));
+  nwritten = xwrite (RBUF_FD (rbuf), request, strlen (request), -1);
   if (nwritten < 0)
     {
       xfree (request);