/* 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.
+ 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Written by 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
it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Emacs is distributed in the hope that it will be useful,
Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
-#define NO_SHORTNAMES /* Tell config not to load remap.h */
#include <config.h>
#else
#define MAIL_USE_POP
int *size;
{
char *fromserver;
+ char *end_ptr;
if (server->in_multi)
{
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);
}
}
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;
}
}
char *host;
int flags;
{
+#ifdef HAVE_GETADDRINFO
+ struct addrinfo *res, *it;
+ struct addrinfo hints;
+ int ret;
+#else /* !HAVE_GETADDRINFO */
struct hostent *hostent;
+#endif
struct servent *servent;
struct sockaddr_in addr;
char found_port = 0;
char *service;
int sock;
+ char *realhost;
#ifdef KERBEROS
#ifdef KERBEROS5
krb5_error_code rem;
CREDENTIALS cred;
Key_schedule schedule;
int rem;
- char *realhost;
#endif /* KERBEROS5 */
#endif /* KERBEROS */
int try_count = 0;
+ int connect_ok;
#ifdef WINDOWSNT
{
}
+#ifdef HAVE_GETADDRINFO
+ memset (&hints, 0, sizeof(hints));
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+ hints.ai_family = AF_INET;
+ do
+ {
+ ret = getaddrinfo (host, service, &hints, &res);
+ try_count++;
+ if (ret != 0 && (ret != EAI_AGAIN || try_count == 5))
+ {
+ strcpy (pop_error, "Could not determine POP server's address");
+ return (-1);
+ }
+ } while (ret != 0);
+
+ if (ret == 0)
+ {
+ it = res;
+ while (it)
+ {
+ if (it->ai_addrlen == sizeof (addr))
+ {
+ struct sockaddr_in *in_a = (struct sockaddr_in *) it->ai_addr;
+ bcopy (&in_a->sin_addr, (char *) &addr.sin_addr,
+ sizeof (addr.sin_addr));
+ if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
+ break;
+ }
+ 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 */
do
{
hostent = gethostbyname (host);
break;
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: "
- if (! *hostent->h_addr_list)
+ if (! connect_ok)
{
CLOSESOCKET (sock);
strcpy (pop_error, CONNECT_ERROR);
}
#ifdef KERBEROS
+
#define KRB_ERROR "Kerberos error connecting to POP server: "
if (! (flags & POP_NO_KERBEROS))
{
if (rem = krb5_cc_get_principal (kcontext, ccdef, &client))
goto krb5error;
- for (cp = hostent->h_name; *cp; cp++)
+ for (cp = realhost; *cp; cp++)
{
if (isupper (*cp))
{
}
}
- if (rem = krb5_sname_to_principal (kcontext, hostent->h_name,
+ if (rem = krb5_sname_to_principal (kcontext, realhost,
POP_SERVICE, FALSE, &server))
goto krb5error;
}
#else /* ! KERBEROS5 */
ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
- realhost = strdup (hostent->h_name);
rem = krb_sendauth (0L, sock, ticket, "pop", realhost,
(char *) krb_realmofhost (realhost),
(unsigned long) 0, &msg_data, &cred, schedule,
(struct sockaddr_in *) 0,
"KPOPV0.1");
free ((char *) ticket);
- free (realhost);
if (rem != KSUCCESS)
{
strcpy (pop_error, KRB_ERROR);
* 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