]> code.delx.au - gnu-emacs/blobdiff - lib-src/pop.c
merge emacs-23
[gnu-emacs] / lib-src / pop.c
index df9e41f64578f765ad21c6a15d18ac7eee6b6cc3..64488e7e3eae663ee08051d26dd5ad4faaeda5c8 100644 (file)
@@ -1,14 +1,15 @@
 /* pop.c: client routines for talking to a POP3-protocol post-office server
    Copyright (C) 1991, 1993, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007  Free Software Foundation, Inc.
-   Written by Jonathan Kamens, jik@security.ov.com.
+                 2005, 2006, 2007, 2008, 2009, 2010  Free Software Foundation, Inc.
+
+Author: Jonathan Kamens <jik@security.ov.com>
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -16,12 +17,10 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
+
 
 #ifdef HAVE_CONFIG_H
-#define NO_SHORTNAMES  /* Tell config not to load remap.h */
 #include <config.h>
 #else
 #define MAIL_USE_POP
@@ -352,6 +351,7 @@ pop_stat (server, count, size)
      int *size;
 {
   char *fromserver;
+  char *end_ptr;
 
   if (server->in_multi)
     {
@@ -377,18 +377,26 @@ pop_stat (server, count, size)
       return (-1);
     }
 
-  *count = atoi (&fromserver[4]);
-
-  fromserver = index (&fromserver[4], ' ');
-  if (! fromserver)
+  errno = 0;
+  *count = strtol (&fromserver[4], &end_ptr, 10);
+  /* Check validity of string-to-integer conversion. */
+  if (fromserver + 4 == end_ptr || *end_ptr != ' ' || errno)
     {
-      strcpy (pop_error,
-             "Badly formatted response from server in pop_stat");
+      strcpy (pop_error, "Unexpected response from POP server in pop_stat");
       pop_trash (server);
       return (-1);
     }
 
-  *size = atoi (fromserver + 1);
+  fromserver = end_ptr;
+
+  errno = 0;
+  *size = strtol (fromserver + 1, &end_ptr, 10);
+  if (fromserver + 1 == end_ptr || errno)
+    {
+      strcpy (pop_error, "Unexpected response from POP server in pop_stat");
+      pop_trash (server);
+      return (-1);
+    }
 
   return (0);
 }
@@ -913,7 +921,17 @@ pop_last (server)
     }
   else
     {
-      return (atoi (&fromserver[4]));
+      char *end_ptr;
+      int count;
+      errno = 0;
+      count = strtol (&fromserver[4], &end_ptr, 10);
+      if (fromserver + 4 == end_ptr || errno)
+       {
+         strcpy (pop_error, "Unexpected response from server in pop_last");
+         pop_trash (server);
+         return (-1);
+       }
+      return count;
     }
 }
 
@@ -980,8 +998,7 @@ pop_quit (server)
       close (server->file);
     }
 
-  if (server->buffer)
-    free (server->buffer);
+  free (server->buffer);
   free ((char *) server);
 
   return (ret);
@@ -1022,6 +1039,7 @@ socket_connection (host, flags)
   char found_port = 0;
   char *service;
   int sock;
+  char *realhost;
 #ifdef KERBEROS
 #ifdef KERBEROS5
   krb5_error_code rem;
@@ -1037,7 +1055,6 @@ socket_connection (host, flags)
   CREDENTIALS cred;
   Key_schedule schedule;
   int rem;
-  char *realhost;
 #endif /* KERBEROS5 */
 #endif /* KERBEROS */
 
@@ -1107,7 +1124,7 @@ socket_connection (host, flags)
 #ifdef HAVE_GETADDRINFO
   memset (&hints, 0, sizeof(hints));
   hints.ai_socktype = SOCK_STREAM;
-  hints.ai_flags = AI_ADDRCONFIG;
+  hints.ai_flags = AI_CANONNAME;
   hints.ai_family = AF_INET;
   do
     {
@@ -1136,6 +1153,11 @@ socket_connection (host, flags)
           it = it->ai_next;
         }
       connect_ok = it != NULL;
+      if (connect_ok)
+        {
+          realhost = alloca (strlen (it->ai_canonname) + 1);
+          strcpy (realhost, it->ai_canonname);
+        }
       freeaddrinfo (res);
     }
 #else /* !HAVE_GETADDRINFO */
@@ -1159,6 +1181,12 @@ socket_connection (host, flags)
       hostent->h_addr_list++;
     }
   connect_ok = *hostent->h_addr_list != NULL;
+  if (! connect_ok)
+    {
+      realhost = alloca (strlen (hostent->h_name) + 1);
+      strcpy (realhost, hostent->h_name);
+    }
+
 #endif /* !HAVE_GETADDRINFO */
 
 #define CONNECT_ERROR "Could not connect to POP server: "
@@ -1175,9 +1203,6 @@ socket_connection (host, flags)
 
 #ifdef KERBEROS
 
-  realhost = alloca (strlen (hostent->h_name) + 1);
-  strcpy (realhost, hostent->h_name);
-
 #define KRB_ERROR "Kerberos error connecting to POP server: "
   if (! (flags & POP_NO_KERBEROS))
     {
@@ -1229,11 +1254,12 @@ socket_connection (host, flags)
       krb5_free_principal (kcontext, server);
       if (rem)
        {
+         strcpy (pop_error, KRB_ERROR);
+         strncat (pop_error, error_message (rem),
+                  ERROR_MAX - sizeof (KRB_ERROR));
+#if defined HAVE_KRB5_ERROR_TEXT
          if (err_ret && err_ret->text.length)
            {
-             strcpy (pop_error, KRB_ERROR);
-             strncat (pop_error, error_message (rem),
-                      ERROR_MAX - sizeof (KRB_ERROR));
              strncat (pop_error, " [server says '",
                       ERROR_MAX - strlen (pop_error) - 1);
              strncat (pop_error, err_ret->text.data,
@@ -1242,12 +1268,17 @@ socket_connection (host, flags)
              strncat (pop_error, "']",
                       ERROR_MAX - strlen (pop_error) - 1);
            }
-         else
+#elif defined HAVE_KRB5_ERROR_E_TEXT
+         if (err_ret && err_ret->e_text && strlen(*err_ret->e_text))
            {
-             strcpy (pop_error, KRB_ERROR);
-             strncat (pop_error, error_message (rem),
-                      ERROR_MAX - sizeof (KRB_ERROR));
+             strncat (pop_error, " [server says '",
+                      ERROR_MAX - strlen (pop_error) - 1);
+             strncat (pop_error, *err_ret->e_text,
+                      ERROR_MAX - strlen (pop_error) - 1);
+             strncat (pop_error, "']",
+                      ERROR_MAX - strlen (pop_error) - 1);
            }
+#endif
          if (err_ret)
            krb5_free_error (kcontext, err_ret);
          krb5_auth_con_free (kcontext, auth_context);
@@ -1603,7 +1634,7 @@ pop_close (server)
  * Function: pop_trash
  *
  * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
- *     memory associated with the server.  It is legal to call
+ *     memory associated with the server.  It is valid to call
  *     pop_close or pop_quit after this function has been called.
  */
 static void