2 * env.c -- small service routines
4 * Copyright 1998 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include "fetchmail.h"
19 #include "getaddrinfo.h"
22 #if defined(ENABLE_NLS)
26 void envquery(int argc, char **argv)
27 /* set up basic stuff from the environment (including the rc file name) */
29 struct passwd by_name, by_uid, *pwp;
34 if (!(user = getenv("FETCHMAILUSER")))
36 if (!(user = getenv("LOGNAME")))
38 user = getenv("USER");
42 if ((program_name = strrchr(argv[0], '/')) != NULL)
45 program_name = argv[0];
47 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
50 GT_("%s: The QMAILINJECT environment variable is set.\n"
51 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
52 "tamper with your From: or Message-ID: headers.\n"
53 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
55 program_name, program_name, program_name);
59 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
62 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
63 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
64 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
65 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
67 program_name, program_name, program_name);
71 if (!(pwp = getpwuid(getuid())))
74 GT_("%s: You don't exist. Go away.\n"),
80 memcpy(&by_uid, pwp, sizeof(struct passwd));
81 if (!user || !(pwp = getpwnam(user)))
86 * This logic is needed to handle gracefully the possibility
87 * that multiple names might be mapped to one UID.
89 memcpy(&by_name, pwp, sizeof(struct passwd));
91 if (by_name.pw_uid == by_uid.pw_uid)
96 user = xstrdup(pwp->pw_name);
101 /* compute user's home directory */
102 home = getenv("HOME_ETC");
103 if (!home && !(home = getenv("HOME")))
104 home = xstrdup(pwp->pw_dir);
106 /* compute fetchmail's home directory */
107 if (!(fmhome = getenv("FETCHMAILHOME")))
110 #define RCFILE_NAME "fetchmailrc"
112 * The (fmhome==home) leaves an extra character for a . at the
113 * beginning of the rc file's name, iff fetchmail is using $HOME
114 * for its files. We don't want to do that if fetchmail has its
115 * own home ($FETCHMAILHOME), however.
117 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
118 /* avoid //.fetchmailrc */
119 if (strcmp(fmhome, "/") != 0)
120 strcpy(rcfile, fmhome);
124 if (rcfile[strlen(rcfile) - 1] != '/')
128 strcat(rcfile, RCFILE_NAME);
131 char *host_fqdn(int required)
133 char tmpbuf[HOSTLEN+1];
136 if (gethostname(tmpbuf, sizeof(tmpbuf)))
138 fprintf(stderr, GT_("%s: can't determine your host!"),
143 /* if we got no . in the hostname, try to canonicalize it,
144 * else assume it is a FQDN */
145 if (strchr(tmpbuf, '.') == NULL)
147 /* if we got a basename without dots, as we often do in Linux,
148 * look up canonical name (make a FQDN of it) */
149 struct addrinfo hints, *res;
152 memset(&hints, 0, sizeof hints);
153 hints.ai_family = AF_UNSPEC;
154 hints.ai_socktype = SOCK_STREAM;
155 hints.ai_flags = AI_CANONNAME;
157 e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res);
159 /* exit with error message */
161 GT_("gethostbyname failed for %s\n"), tmpbuf);
162 fprintf(stderr, "%s", gai_strerror(e));
163 fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n"));
167 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"));
168 return xstrdup(tmpbuf);
172 result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf);
173 fm_freeaddrinfo(res);
176 result = xstrdup(tmpbuf);
181 static char *tzoffset(time_t *now)
182 /* calculate timezone offset */
184 static char offset_string[6];
191 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
192 if (lt->tm_year < gmt.tm_year)
194 else if (lt->tm_year > gmt.tm_year)
196 else if (lt->tm_yday < gmt.tm_yday)
198 else if (lt->tm_yday > gmt.tm_yday)
204 if (off >= 24 * 60) /* should be impossible */
205 off = 23 * 60 + 59; /* if not, insert silly value */
206 snprintf(offset_string, sizeof(offset_string),
207 "%c%02d%02d", sign, off / 60, off % 60);
208 return (offset_string);
211 char *rfc822timestamp(void)
212 /* return a timestamp in RFC822 form */
219 * Conform to RFC822. We generate a 4-digit year here, avoiding
220 * Y2K hassles. Max length of this timestamp in an English locale
221 * should be 29 chars. The only things that should vary by locale
222 * are the day and month abbreviations. The set_locale calls prevent
223 * weird multibyte i18n characters (such as kanji) from showing up
224 * in your Received headers.
226 #if defined(ENABLE_NLS)
227 setlocale (LC_TIME, "C");
229 strftime(buf, sizeof(buf)-1,
230 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
231 #if defined(ENABLE_NLS)
232 setlocale (LC_TIME, "");
234 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
238 const char *showproto(int proto)
239 /* protocol index to protocol name mapping */
243 case P_AUTO: return("auto");
245 case P_POP3: return("POP3");
246 #endif /* POP3_ENABLE */
248 case P_IMAP: return("IMAP");
249 #endif /* IMAP_ENABLE */
251 case P_ETRN: return("ETRN");
252 #endif /* ETRN_ENABLE */
254 case P_ODMR: return("ODMR");
255 #endif /* ODMR_ENABLE */
256 default: return("unknown?!?");
260 char *visbuf(const char *buf)
261 /* visibilize a given string */
268 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
270 if (!vbuf || needed > vbufs) {
272 vbuf = (char *)xrealloc(vbuf, vbufs);
279 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
280 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
281 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
282 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
283 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
284 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
285 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
286 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
287 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
288 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
291 const char hex[] = "0123456789abcdef";
292 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
293 *tp++ = hex[*buf >> 4];
294 *tp++ = hex[*buf & 0xf];
301 /* env.c ends here */