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 extern char *getenv(const char *); /* needed on sysV68 R3V7.1. */
35 void envquery(int argc, char **argv)
36 /* set up basic stuff from the environment (including the rc file name) */
38 struct passwd by_name, by_uid, *pwp;
43 if (!(user = getenv("FETCHMAILUSER")))
45 if (!(user = getenv("LOGNAME")))
47 user = getenv("USER");
51 if ((program_name = strrchr(argv[0], '/')) != NULL)
54 program_name = argv[0];
56 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
59 GT_("%s: The QMAILINJECT environment variable is set.\n"
60 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
61 "tamper with your From: or Message-ID: headers.\n"
62 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
64 program_name, program_name, program_name);
68 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
71 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
72 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
73 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
74 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
76 program_name, program_name, program_name);
80 if (!(pwp = getpwuid(getuid())))
83 GT_("%s: You don't exist. Go away.\n"),
89 memcpy(&by_uid, pwp, sizeof(struct passwd));
90 if (!user || !(pwp = getpwnam(user)))
95 * This logic is needed to handle gracefully the possibility
96 * that multiple names might be mapped to one UID.
98 memcpy(&by_name, pwp, sizeof(struct passwd));
100 if (by_name.pw_uid == by_uid.pw_uid)
105 user = xstrdup(pwp->pw_name);
108 /* compute user's home directory */
109 home = getenv("HOME_ETC");
110 if (!home && !(home = getenv("HOME")))
113 /* compute fetchmail's home directory */
114 if (!(fmhome = getenv("FETCHMAILHOME")))
117 #define RCFILE_NAME "fetchmailrc"
119 * The (fmhome==home) leaves an extra character for a . at the
120 * beginning of the rc file's name, iff fetchmail is using $HOME
121 * for its files. We don't want to do that if fetchmail has its
122 * own home ($FETCHMAILHOME), however.
124 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
125 /* avoid //.fetchmailrc */
126 if (strcmp(fmhome, "/") != 0)
127 strcpy(rcfile, fmhome);
131 if (rcfile[strlen(rcfile) - 1] != '/')
135 strcat(rcfile, RCFILE_NAME);
138 char *host_fqdn(int required)
140 char tmpbuf[HOSTLEN+1];
143 if (gethostname(tmpbuf, sizeof(tmpbuf)))
145 fprintf(stderr, GT_("%s: can't determine your host!"),
150 /* if we got a . in the hostname assume it is a FQDN */
151 if (strchr(tmpbuf, '.') == NULL)
153 /* if we got a basename (as we do in Linux) make a FQDN of it */
154 struct addrinfo hints, *res;
157 memset(&hints, 0, sizeof hints);
158 hints.ai_family = AF_UNSPEC;
159 hints.ai_socktype = SOCK_STREAM;
160 hints.ai_flags=AI_CANONNAME;
162 e = getaddrinfo(tmpbuf, NULL, &hints, &res);
164 /* exit with error message */
166 GT_("gethostbyname failed for %s\n"), tmpbuf);
167 fprintf(stderr, "%s", gai_strerror(e));
168 fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
172 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"));
173 return xstrdup(tmpbuf);
177 result = xstrdup(res->ai_canonname);
181 result = xstrdup(tmpbuf);
186 static char *tzoffset(time_t *now)
187 /* calculate timezone offset */
189 static char offset_string[6];
196 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
197 if (lt->tm_year < gmt.tm_year)
199 else if (lt->tm_year > gmt.tm_year)
201 else if (lt->tm_yday < gmt.tm_yday)
203 else if (lt->tm_yday > gmt.tm_yday)
209 if (off >= 24 * 60) /* should be impossible */
210 off = 23 * 60 + 59; /* if not, insert silly value */
211 snprintf(offset_string, sizeof(offset_string),
212 "%c%02d%02d", sign, off / 60, off % 60);
213 return (offset_string);
216 char *rfc822timestamp(void)
217 /* return a timestamp in RFC822 form */
225 * Conform to RFC822. We generate a 4-digit year here, avoiding
226 * Y2K hassles. Max length of this timestamp in an English locale
227 * should be 29 chars. The only things that should vary by locale
228 * are the day and month abbreviations. The set_locale calls prevent
229 * weird multibyte i18n characters (such as kanji) from showing up
230 * in your Received headers.
232 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
233 setlocale (LC_TIME, "C");
235 strftime(buf, sizeof(buf)-1,
236 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
237 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
238 setlocale (LC_TIME, "");
240 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
243 * This is really just a portability fallback, as the
244 * date format ctime(3) emits is not RFC822
247 strlcpy(buf, ctime(&now), sizeof(buf));
248 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
249 #endif /* HAVE_STRFTIME */
254 const char *showproto(int proto)
255 /* protocol index to protocol name mapping */
259 case P_AUTO: return("auto");
261 case P_POP2: return("POP2");
262 #endif /* POP2_ENABLE */
264 case P_POP3: return("POP3");
265 case P_APOP: return("APOP");
266 case P_RPOP: return("RPOP");
267 #endif /* POP3_ENABLE */
269 case P_IMAP: return("IMAP");
270 #endif /* IMAP_ENABLE */
272 case P_ETRN: return("ETRN");
273 #endif /* ETRN_ENABLE */
275 case P_ODMR: return("ODMR");
276 #endif /* ODMR_ENABLE */
277 default: return("unknown?!?");
281 char *visbuf(const char *buf)
282 /* visibilize a given string */
289 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
291 if (needed > vbufs) {
293 vbuf = xrealloc(vbuf, vbufs);
300 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
301 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
302 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
303 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
304 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
305 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
306 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
307 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
308 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
309 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
312 const char hex[] = "0123456789abcdef";
313 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
314 *tp++ = hex[*buf >> 4];
315 *tp++ = hex[*buf & 0xf];
322 /* env.c ends here */