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)
34 void envquery(int argc, char **argv)
35 /* set up basic stuff from the environment (including the rc file name) */
37 struct passwd by_name, by_uid, *pwp;
42 if (!(user = getenv("FETCHMAILUSER")))
44 if (!(user = getenv("LOGNAME")))
46 user = getenv("USER");
50 if ((program_name = strrchr(argv[0], '/')) != NULL)
53 program_name = argv[0];
55 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
58 GT_("%s: The QMAILINJECT environment variable is set.\n"
59 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
60 "tamper with your From: or Message-ID: headers.\n"
61 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
63 program_name, program_name, program_name);
67 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
70 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
71 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
72 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
73 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
75 program_name, program_name, program_name);
79 if (!(pwp = getpwuid(getuid())))
82 GT_("%s: You don't exist. Go away.\n"),
88 memcpy(&by_uid, pwp, sizeof(struct passwd));
89 if (!user || !(pwp = getpwnam(user)))
94 * This logic is needed to handle gracefully the possibility
95 * that multiple names might be mapped to one UID.
97 memcpy(&by_name, pwp, sizeof(struct passwd));
99 if (by_name.pw_uid == by_uid.pw_uid)
104 user = xstrdup(pwp->pw_name);
109 /* This documentation added to 6.4.1,
110 * and manual page corrected.
112 * here's the home directory logic:
113 * 1. we derive home. it is taken from:
116 * c. the user's passwd entry
117 * 2. we derive fmhome. It is normally
119 * b. It can be overridden from FETCHMAILHOME.
120 * If and only if fmhome != home, then the
121 * default configuration files will be expected
122 * without leading dots.
125 /* compute user's home directory */
126 home = getenv("HOME_ETC");
128 home = getenv("HOME");
130 home = xstrdup(pwp->pw_dir);
131 /* and make it an absolute path, so we
132 * can optionally chdir("/") later in daemonize()
133 * without changing behaviour.
136 static char *_home_abs;
138 if (_home_abs) free(_home_abs), _home_abs = 0;
139 tmp = realpath(home, NULL);
141 home = _home_abs = tmp;
143 report(stderr, GT_("Cannot find absolute path for user's home directory.\n"));
148 /* compute fetchmail's home directory */
149 fmhome = getenv("FETCHMAILHOME");
150 if (NULL == fmhome) {
154 /* and make it an absolute path, so we
155 * can optionally chdir("/") later in daemonize()
156 * without changing behaviour.
157 * This is to fix Debian Bug#941129 by Alex Andreotti.
160 static char *_fmhome_abs;
162 if (_fmhome_abs) free(_fmhome_abs), _fmhome_abs = 0;
163 tmp = realpath(fmhome, NULL);
165 fmhome = _fmhome_abs = tmp;
167 report(stderr, GT_("Cannot find absolute path for fetchmail's home directory.\n"));
172 #define RCFILE_NAME "fetchmailrc"
174 * The (fmhome==home) leaves an extra character for a . at the
175 * beginning of the rc file's name, iff fetchmail is using $HOME
176 * for its files. We don't want to do that if fetchmail has its
177 * own home ($FETCHMAILHOME), however.
179 rcfile = (char *)xmalloc(strlen(fmhome) + sizeof(RCFILE_NAME) + 3);
180 /* avoid //.fetchmailrc */
181 if (strcmp(fmhome, "/") != 0)
182 strcpy(rcfile, fmhome);
186 if (rcfile[strlen(rcfile) - 1] != '/')
190 strcat(rcfile, RCFILE_NAME);
193 char *host_fqdn(int required)
195 char tmpbuf[HOSTLEN+1];
198 if (gethostname(tmpbuf, sizeof(tmpbuf)))
200 fprintf(stderr, GT_("%s: can't determine your host!"),
205 /* if we got no . in the hostname, try to canonicalize it,
206 * else assume it is a FQDN */
207 if (strchr(tmpbuf, '.') == NULL)
209 /* if we got a basename without dots, as we often do in Linux,
210 * look up canonical name (make a FQDN of it) */
211 struct addrinfo hints, *res;
214 memset(&hints, 0, sizeof hints);
215 hints.ai_family = AF_UNSPEC;
216 hints.ai_socktype = SOCK_STREAM;
217 hints.ai_flags = AI_CANONNAME;
219 e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res);
221 /* exit with error message */
223 GT_("getaddrinfo failed for %s\n"), tmpbuf);
224 fprintf(stderr, "%s", gai_strerror(e));
225 fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
229 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"));
230 return xstrdup(tmpbuf);
234 result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf);
235 fm_freeaddrinfo(res);
238 result = xstrdup(tmpbuf);
243 static char *tzoffset(time_t *now)
244 /* calculate timezone offset */
246 static char offset_string[6];
253 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
254 if (lt->tm_year < gmt.tm_year)
256 else if (lt->tm_year > gmt.tm_year)
258 else if (lt->tm_yday < gmt.tm_yday)
260 else if (lt->tm_yday > gmt.tm_yday)
266 if (off >= 24 * 60) /* should be impossible */
267 off = 23 * 60 + 59; /* if not, insert silly value */
268 snprintf(offset_string, sizeof(offset_string),
269 "%c%02d%02d", sign, off / 60, off % 60);
270 return (offset_string);
273 char *rfc822timestamp(void)
274 /* return a timestamp in RFC822 form */
282 * Conform to RFC822. We generate a 4-digit year here, avoiding
283 * Y2K hassles. Max length of this timestamp in an English locale
284 * should be 29 chars. The only things that should vary by locale
285 * are the day and month abbreviations. The set_locale calls prevent
286 * weird multibyte i18n characters (such as kanji) from showing up
287 * in your Received headers.
289 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
290 setlocale (LC_TIME, "C");
292 strftime(buf, sizeof(buf)-1,
293 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
294 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
295 setlocale (LC_TIME, "");
297 memcpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
300 * This is really just a portability fallback, as the
301 * date format ctime(3) emits is not RFC822
304 strlcpy(buf, ctime(&now), sizeof(buf));
305 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
306 #endif /* HAVE_STRFTIME */
311 const char *showproto(int proto)
312 /* protocol index to protocol name mapping */
316 case P_AUTO: return("auto");
318 case P_POP2: return("POP2");
319 #endif /* POP2_ENABLE */
321 case P_POP3: return("POP3");
322 case P_APOP: return("APOP");
323 case P_RPOP: return("RPOP");
324 #endif /* POP3_ENABLE */
326 case P_IMAP: return("IMAP");
327 #endif /* IMAP_ENABLE */
329 case P_ETRN: return("ETRN");
330 #endif /* ETRN_ENABLE */
332 case P_ODMR: return("ODMR");
333 #endif /* ODMR_ENABLE */
334 default: return("unknown?!?");
338 char *visbuf(const char *buf)
339 /* visibilize a given string */
346 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
348 if (!vbuf || needed > vbufs) {
350 vbuf = (char *)xrealloc(vbuf, vbufs);
357 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
358 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
359 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
360 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
361 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
362 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
363 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
364 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
365 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
366 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
369 const char hex[] = "0123456789abcdef";
370 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
371 *tp++ = hex[*buf >> 4];
372 *tp++ = hex[*buf & 0xf];
379 /* env.c ends here */