2 * env.c -- small service routines
4 * Copyright 1998 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
14 #if defined(HAVE_UNISTD_H)
19 #ifdef HAVE_NET_SOCKET_H
20 #include <net/socket.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include "fetchmail.h"
26 #include "getaddrinfo.h"
29 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
33 #ifndef HAVE_DECL_GETENV
34 extern char *getenv(const char *); /* needed on sysV68 R3V7.1. */
37 void envquery(int argc, char **argv)
38 /* set up basic stuff from the environment (including the rc file name) */
40 struct passwd by_name, by_uid, *pwp;
45 if (!(user = getenv("FETCHMAILUSER")))
47 if (!(user = getenv("LOGNAME")))
49 user = getenv("USER");
53 if ((program_name = strrchr(argv[0], '/')) != NULL)
56 program_name = argv[0];
58 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
61 GT_("%s: The QMAILINJECT environment variable is set.\n"
62 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
63 "tamper with your From: or Message-ID: headers.\n"
64 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
66 program_name, program_name, program_name);
70 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
73 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
74 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
75 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
76 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
78 program_name, program_name, program_name);
82 if (!(pwp = getpwuid(getuid())))
85 GT_("%s: You don't exist. Go away.\n"),
91 memcpy(&by_uid, pwp, sizeof(struct passwd));
92 if (!user || !(pwp = getpwnam(user)))
97 * This logic is needed to handle gracefully the possibility
98 * that multiple names might be mapped to one UID.
100 memcpy(&by_name, pwp, sizeof(struct passwd));
102 if (by_name.pw_uid == by_uid.pw_uid)
107 user = xstrdup(pwp->pw_name);
110 /* compute user's home directory */
111 home = getenv("HOME_ETC");
112 if (!home && !(home = getenv("HOME")))
113 home = xstrdup(pwp->pw_dir);
115 /* compute fetchmail's home directory */
116 if (!(fmhome = getenv("FETCHMAILHOME")))
119 #define RCFILE_NAME "fetchmailrc"
121 * The (fmhome==home) leaves an extra character for a . at the
122 * beginning of the rc file's name, iff fetchmail is using $HOME
123 * for its files. We don't want to do that if fetchmail has its
124 * own home ($FETCHMAILHOME), however.
126 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
127 /* avoid //.fetchmailrc */
128 if (strcmp(fmhome, "/") != 0)
129 strcpy(rcfile, fmhome);
133 if (rcfile[strlen(rcfile) - 1] != '/')
137 strcat(rcfile, RCFILE_NAME);
140 char *host_fqdn(int required)
142 char tmpbuf[HOSTLEN+1];
145 if (gethostname(tmpbuf, sizeof(tmpbuf)))
147 fprintf(stderr, GT_("%s: can't determine your host!"),
152 /* if we got no . in the hostname, try to canonicalize it,
153 * else assume it is a FQDN */
154 if (strchr(tmpbuf, '.') == NULL)
156 /* if we got a basename without dots, as we often do in Linux,
157 * look up canonical name (make a FQDN of it) */
158 struct addrinfo hints, *res;
161 memset(&hints, 0, sizeof hints);
162 hints.ai_family = AF_UNSPEC;
163 hints.ai_socktype = SOCK_STREAM;
164 hints.ai_flags = AI_CANONNAME;
166 e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res);
168 /* exit with error message */
170 GT_("gethostbyname failed for %s\n"), tmpbuf);
171 fprintf(stderr, "%s", gai_strerror(e));
172 fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
176 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"));
177 return xstrdup(tmpbuf);
181 result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf);
182 fm_freeaddrinfo(res);
185 result = xstrdup(tmpbuf);
190 static char *tzoffset(time_t *now)
191 /* calculate timezone offset */
193 static char offset_string[6];
200 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
201 if (lt->tm_year < gmt.tm_year)
203 else if (lt->tm_year > gmt.tm_year)
205 else if (lt->tm_yday < gmt.tm_yday)
207 else if (lt->tm_yday > gmt.tm_yday)
213 if (off >= 24 * 60) /* should be impossible */
214 off = 23 * 60 + 59; /* if not, insert silly value */
215 snprintf(offset_string, sizeof(offset_string),
216 "%c%02d%02d", sign, off / 60, off % 60);
217 return (offset_string);
220 char *rfc822timestamp(void)
221 /* return a timestamp in RFC822 form */
229 * Conform to RFC822. We generate a 4-digit year here, avoiding
230 * Y2K hassles. Max length of this timestamp in an English locale
231 * should be 29 chars. The only things that should vary by locale
232 * are the day and month abbreviations. The set_locale calls prevent
233 * weird multibyte i18n characters (such as kanji) from showing up
234 * in your Received headers.
236 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
237 setlocale (LC_TIME, "C");
239 strftime(buf, sizeof(buf)-1,
240 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
241 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
242 setlocale (LC_TIME, "");
244 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
247 * This is really just a portability fallback, as the
248 * date format ctime(3) emits is not RFC822
251 strlcpy(buf, ctime(&now), sizeof(buf));
252 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
253 #endif /* HAVE_STRFTIME */
258 const char *showproto(int proto)
259 /* protocol index to protocol name mapping */
263 case P_AUTO: return("auto");
265 case P_POP2: return("POP2");
266 #endif /* POP2_ENABLE */
268 case P_POP3: return("POP3");
269 case P_APOP: return("APOP");
270 case P_RPOP: return("RPOP");
271 #endif /* POP3_ENABLE */
273 case P_IMAP: return("IMAP");
274 #endif /* IMAP_ENABLE */
276 case P_ETRN: return("ETRN");
277 #endif /* ETRN_ENABLE */
279 case P_ODMR: return("ODMR");
280 #endif /* ODMR_ENABLE */
281 default: return("unknown?!?");
285 char *visbuf(const char *buf)
286 /* visibilize a given string */
293 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
295 if (!vbuf || needed > vbufs) {
297 vbuf = (char *)xrealloc(vbuf, vbufs);
304 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
305 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
306 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
307 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
308 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
309 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
310 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
311 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
312 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
313 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
316 const char hex[] = "0123456789abcdef";
317 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
318 *tp++ = hex[*buf >> 4];
319 *tp++ = hex[*buf & 0xf];
326 /* env.c ends here */