2 * env.c -- small service routines
4 * Copyright 1998 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
18 #include <sys/types.h>
19 #include <sys/socket.h>
22 #if defined(ENABLE_NLS)
27 void envquery(int argc, char **argv)
28 /* set up basic stuff from the environment (including the rc file name) */
30 struct passwd by_name, by_uid, *pwp;
35 if (!(user = getenv("FETCHMAILUSER")))
37 if (!(user = getenv("LOGNAME")))
39 user = getenv("USER");
43 if ((program_name = strrchr(argv[0], '/')) != NULL)
46 program_name = argv[0];
48 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
51 GT_("%s: The QMAILINJECT environment variable is set.\n"
52 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
53 "tamper with your From: or Message-ID: headers.\n"
54 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
56 program_name, program_name, program_name);
60 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
63 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
64 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
65 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
66 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
68 program_name, program_name, program_name);
72 if (!(pwp = getpwuid(getuid())))
75 GT_("%s: You don't exist. Go away.\n"),
81 memcpy(&by_uid, pwp, sizeof(struct passwd));
82 if (!user || !(pwp = getpwnam(user)))
87 * This logic is needed to handle gracefully the possibility
88 * that multiple names might be mapped to one UID.
90 memcpy(&by_name, pwp, sizeof(struct passwd));
92 if (by_name.pw_uid == by_uid.pw_uid)
97 user = xstrdup(pwp->pw_name);
102 /* This documentation added to 6.4.1,
103 * and manual page corrected.
105 * here's the home directory logic:
106 * 1. we derive home. it is taken from:
109 * c. the user's passwd entry
110 * 2. we derive fmhome. It is normally
112 * b. It can be overridden from FETCHMAILHOME.
113 * If and only if fmhome != home, then the
114 * default configuration files will be expected
115 * without leading dots.
118 /* compute user's home directory */
119 home = getenv("HOME_ETC");
121 home = getenv("HOME");
123 home = xstrdup(pwp->pw_dir);
124 /* and make it an absolute path, so we
125 * can optionally chdir("/") later in daemonize()
126 * without changing behaviour.
129 static char *_home_abs;
131 if (_home_abs) free(_home_abs), _home_abs = 0;
132 tmp = fm_realpath(home);
134 home = _home_abs = tmp;
136 report(stderr, GT_("Cannot find absolute path for user's home directory.\n"));
141 /* compute fetchmail's home directory */
142 fmhome = getenv("FETCHMAILHOME");
143 if (NULL == fmhome) {
147 /* and make it an absolute path, so we
148 * can optionally chdir("/") later in daemonize()
149 * without changing behaviour.
150 * This is to fix Debian Bug#941129 by Alex Andreotti.
153 static char *_fmhome_abs;
155 if (_fmhome_abs) free(_fmhome_abs), _fmhome_abs = 0;
156 tmp = fm_realpath(fmhome);
158 fmhome = _fmhome_abs = tmp;
160 report(stderr, GT_("Cannot find absolute path for fetchmail's home directory.\n"));
165 #define RCFILE_NAME "fetchmailrc"
167 * The (fmhome==home) leaves an extra character for a . at the
168 * beginning of the rc file's name, iff fetchmail is using $HOME
169 * for its files. We don't want to do that if fetchmail has its
170 * own home ($FETCHMAILHOME), however.
172 rcfile = (char *)xmalloc(strlen(fmhome) + sizeof(RCFILE_NAME) + 3);
173 /* avoid //.fetchmailrc */
174 if (strcmp(fmhome, "/") != 0)
175 strcpy(rcfile, fmhome);
179 if (rcfile[strlen(rcfile) - 1] != '/')
183 strcat(rcfile, RCFILE_NAME);
186 char *host_fqdn(int required)
188 char tmpbuf[HOSTLEN+1];
191 if (gethostname(tmpbuf, sizeof(tmpbuf)))
193 fprintf(stderr, GT_("%s: can't determine your host!"),
198 /* if we got no . in the hostname, try to canonicalize it,
199 * else assume it is a FQDN */
200 if (strchr(tmpbuf, '.') == NULL)
202 /* if we got a basename without dots, as we often do in Linux,
203 * look up canonical name (make a FQDN of it) */
204 struct addrinfo hints, *res;
207 memset(&hints, 0, sizeof hints);
208 hints.ai_family = AF_UNSPEC;
209 hints.ai_socktype = SOCK_STREAM;
210 hints.ai_flags = AI_CANONNAME;
212 e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res);
214 /* exit with error message */
216 GT_("getaddrinfo failed for %s\n"), tmpbuf);
217 fprintf(stderr, "%s", gai_strerror(e));
218 fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
222 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"));
223 return xstrdup(tmpbuf);
227 result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf);
228 fm_freeaddrinfo(res);
231 result = xstrdup(tmpbuf);
236 static char *tzoffset(time_t *now)
237 /* calculate timezone offset */
239 static char offset_string[6];
246 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
247 if (lt->tm_year < gmt.tm_year)
249 else if (lt->tm_year > gmt.tm_year)
251 else if (lt->tm_yday < gmt.tm_yday)
253 else if (lt->tm_yday > gmt.tm_yday)
259 if (off >= 24 * 60) /* should be impossible */
260 off = 23 * 60 + 59; /* if not, insert silly value */
261 snprintf(offset_string, sizeof(offset_string),
262 "%c%02d%02d", sign, off / 60, off % 60);
263 return (offset_string);
266 char *rfc822timestamp(void)
267 /* return a timestamp in RFC822 form */
274 * Conform to RFC822. We generate a 4-digit year here, avoiding
275 * Y2K hassles. Max length of this timestamp in an English locale
276 * should be 29 chars. The only things that should vary by locale
277 * are the day and month abbreviations. The set_locale calls prevent
278 * weird multibyte i18n characters (such as kanji) from showing up
279 * in your Received headers.
281 #if defined(ENABLE_NLS)
282 setlocale (LC_TIME, "C");
284 strftime(buf, sizeof(buf)-1,
285 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
286 #if defined(ENABLE_NLS)
287 setlocale (LC_TIME, "");
289 memcpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
294 const char *showproto(int proto)
295 /* protocol index to protocol name mapping */
299 case P_AUTO: return("auto");
301 case P_POP2: return("POP2");
302 #endif /* POP2_ENABLE */
304 case P_POP3: return("POP3");
305 case P_APOP: return("APOP");
306 case P_RPOP: return("RPOP");
307 #endif /* POP3_ENABLE */
309 case P_IMAP: return("IMAP");
310 #endif /* IMAP_ENABLE */
312 case P_ETRN: return("ETRN");
313 #endif /* ETRN_ENABLE */
315 case P_ODMR: return("ODMR");
316 #endif /* ODMR_ENABLE */
317 default: return("unknown?!?");
321 char *visbuf(const char *buf)
322 /* visibilize a given string */
329 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
331 if (!vbuf || needed > vbufs) {
333 vbuf = (char *)xrealloc(vbuf, vbufs);
340 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
341 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
342 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
343 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
344 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
345 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
346 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
347 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
348 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
349 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
352 const char hex[] = "0123456789abcdef";
353 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
354 *tp++ = hex[*buf >> 4];
355 *tp++ = hex[*buf & 0xf];
362 /* env.c ends here */