]> Pileus Git - ~andy/fetchmail/blobdiff - env.c
Merge branch 'legacy_63'
[~andy/fetchmail] / env.c
diff --git a/env.c b/env.c
index d832811da19be3b8d745d529dcae641c82d27867..7d03ae170a463502476d59b13bfae242fbd49a54 100644 (file)
--- a/env.c
+++ b/env.c
@@ -8,35 +8,29 @@
 #include "config.h"
 #include <stdio.h>
 #include <ctype.h>
-#if defined(STDC_HEADERS)
 #include <stdlib.h>
-#endif
-#if defined(HAVE_UNISTD_H)
 #include <unistd.h>
-#endif
 #include <pwd.h>
 #include <string.h>
-#ifdef HAVE_NET_SOCKET_H
-#include <net/socket.h>
-#endif
-#ifdef HAVE_GETHOSTBYNAME
 #include <netdb.h>
-#endif /* HAVE_GETHOSTBYNAME */
-#include  <sys/types.h>
+#include <sys/types.h>
+#include <sys/socket.h>
 #include "fetchmail.h"
+#include "getaddrinfo.h"
 
-#include "i18n.h"
-#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
+#include "gettext.h"
+#if defined(ENABLE_NLS)
 #include <locale.h>
 #endif
 
-extern char *getenv(const char *);     /* needed on sysV68 R3V7.1. */
-
 void envquery(int argc, char **argv)
 /* set up basic stuff from the environment (including the rc file name) */
 {
     struct passwd by_name, by_uid, *pwp;
 
+    (void)argc;
+
+    (void)argc;
     if (!(user = getenv("FETCHMAILUSER")))
     {
        if (!(user = getenv("LOGNAME")))
@@ -102,10 +96,12 @@ void envquery(int argc, char **argv)
        user = xstrdup(pwp->pw_name);
     }
 
+    endpwent();
+
     /* compute user's home directory */
     home = getenv("HOME_ETC");
     if (!home && !(home = getenv("HOME")))
-       home = pwp->pw_dir;
+       home = xstrdup(pwp->pw_dir);
 
     /* compute fetchmail's home directory */
     if (!(fmhome = getenv("FETCHMAILHOME")))
@@ -132,10 +128,10 @@ void envquery(int argc, char **argv)
     strcat(rcfile, RCFILE_NAME);
 }
 
-char *host_fqdn(void)
-/* get the FQDN of the machine we're running */
+char *host_fqdn(int required)
 {
-    char       tmpbuf[HOSTLEN+1];
+    char tmpbuf[HOSTLEN+1];
+    char *result;
 
     if (gethostname(tmpbuf, sizeof(tmpbuf)))
     {
@@ -143,26 +139,43 @@ char *host_fqdn(void)
                program_name);
        exit(PS_DNS);
     }
-#ifdef HAVE_GETHOSTBYNAME
-    /* if we got a . in the hostname assume it is a FQDN */
+
+    /* if we got no . in the hostname, try to canonicalize it,
+     * else assume it is a FQDN */
     if (strchr(tmpbuf, '.') == NULL)
     {
-       struct hostent *hp;
+       /* if we got a basename without dots, as we often do in Linux,
+        * look up canonical name (make a FQDN of it) */
+       struct addrinfo hints, *res;
+       int e;
 
-       /* if we got a basename (as we do in Linux) make a FQDN of it */
-       hp = gethostbyname(tmpbuf);
-       if (hp == (struct hostent *) NULL)
-       {
+       memset(&hints, 0, sizeof hints);
+       hints.ai_family = AF_UNSPEC;
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_flags = AI_CANONNAME;
+
+       e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res);
+       if (e) {
            /* exit with error message */
            fprintf(stderr,
                    GT_("gethostbyname failed for %s\n"), tmpbuf);
-           exit(PS_DNS);
+           fprintf(stderr, "%s", gai_strerror(e));
+           fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
+           if (required)
+               exit(PS_DNS);
+           else {
+               fprintf(stderr, GT_("Trying to continue with unqualified hostname.\nDO NOT report broken Received: headers, HELO/EHLO lines or similar problems!\nDO repair your /etc/hosts, DNS, NIS or LDAP instead.\n"));
+               return xstrdup(tmpbuf);
+           }
        }
-       return(xstrdup(hp->h_name));
+
+       result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf);
+       fm_freeaddrinfo(res);
     }
     else
-#endif /* HAVE_GETHOSTBYNAME */
-       return(xstrdup(tmpbuf));
+       result = xstrdup(tmpbuf);
+
+    return result;
 }
 
 static char *tzoffset(time_t *now)
@@ -190,7 +203,8 @@ static char *tzoffset(time_t *now)
     }
     if (off >= 24 * 60)                        /* should be impossible */
        off = 23 * 60 + 59;             /* if not, insert silly value */
-    sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
+    snprintf(offset_string, sizeof(offset_string),
+           "%c%02d%02d", sign, off / 60, off % 60);
     return (offset_string);
 }
 
@@ -201,7 +215,6 @@ char *rfc822timestamp(void)
     static char buf[50];
 
     time(&now);
-#ifdef HAVE_STRFTIME
     /*
      * Conform to RFC822.  We generate a 4-digit year here, avoiding
      * Y2K hassles.  Max length of this timestamp in an English locale
@@ -210,25 +223,15 @@ char *rfc822timestamp(void)
      * weird multibyte i18n characters (such as kanji) from showing up
      * in your Received headers.
      */
-#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
+#if defined(ENABLE_NLS)
     setlocale (LC_TIME, "C");
 #endif
     strftime(buf, sizeof(buf)-1, 
             "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
-#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
+#if defined(ENABLE_NLS)
     setlocale (LC_TIME, "");
 #endif
     strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
-#else
-    /*
-     * This is really just a portability fallback, as the
-     * date format ctime(3) emits is not RFC822
-     * conformant.
-     */
-    strcpy(buf, ctime(&now));
-    buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
-#endif /* HAVE_STRFTIME */
-
     return(buf);
 }
 
@@ -238,13 +241,8 @@ const char *showproto(int proto)
     switch (proto)
     {
     case P_AUTO: return("auto");
-#ifdef POP2_ENABLE
-    case P_POP2: return("POP2");
-#endif /* POP2_ENABLE */
 #ifdef POP3_ENABLE
     case P_POP3: return("POP3");
-    case P_APOP: return("APOP");
-    case P_RPOP: return("RPOP");
 #endif /* POP3_ENABLE */
 #ifdef IMAP_ENABLE
     case P_IMAP: return("IMAP");
@@ -262,51 +260,42 @@ const char *showproto(int proto)
 char *visbuf(const char *buf)
 /* visibilize a given string */
 {
-    static char vbuf[BUFSIZ];
-    char *tp = vbuf;
+    static char *vbuf;
+    static size_t vbufs;
+    char *tp;
+    size_t needed;
+
+    needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
+
+    if (!vbuf || needed > vbufs) {
+       vbufs = needed;
+       vbuf = (char *)xrealloc(vbuf, vbufs);
+    }
+
+    tp = vbuf;
 
     while (*buf)
     {
-       if (*buf == '"')
-       {
-           *tp++ = '\\'; *tp++ = '"';
-           buf++;
-       }
-       else if (*buf == '\\')
-       {
-           *tp++ = '\\'; *tp++ = '\\';
-           buf++;
-       }
-       else if (isprint((unsigned char)*buf) || *buf == ' ')
-           *tp++ = *buf++;
-       else if (*buf == '\n')
-       {
-           *tp++ = '\\'; *tp++ = 'n';
-           buf++;
-       }
-       else if (*buf == '\r')
-       {
-           *tp++ = '\\'; *tp++ = 'r';
-           buf++;
-       }
-       else if (*buf == '\b')
-       {
-           *tp++ = '\\'; *tp++ = 'b';
-           buf++;
-       }
-       else if (*buf < ' ')
-       {
-           *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
-           buf++;
-       }
+            if (*buf == '"')  { *tp++ = '\\'; *tp++ = '"'; buf++; }
+       else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
+       else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
+       else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
+       else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
+       else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
+       else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
+       else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
+       else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
+       else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
        else
        {
-           (void) sprintf(tp, "\\0x%02x", *buf++);
-           tp += strlen(tp);
+           const char hex[] = "0123456789abcdef";
+           *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
+           *tp++ = hex[*buf >> 4];
+           *tp++ = hex[*buf & 0xf];
+           buf++;
        }
     }
-    *tp++ = '\0';
+    *tp = '\0';
     return(vbuf);
 }
-
 /* env.c ends here */