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>
22 #ifdef HAVE_GETHOSTBYNAME
24 #endif /* HAVE_GETHOSTBYNAME */
25 #include <sys/types.h>
26 #include "fetchmail.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;
40 if (!(user = getenv("FETCHMAILUSER")))
42 if (!(user = getenv("LOGNAME")))
44 user = getenv("USER");
48 if ((program_name = strrchr(argv[0], '/')) != NULL)
51 program_name = argv[0];
53 if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
56 GT_("%s: The QMAILINJECT environment variable is set.\n"
57 "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
58 "tamper with your From: or Message-ID: headers.\n"
59 "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
61 program_name, program_name, program_name);
65 if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
68 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
69 "This is dangerous as it can make nullmailer-inject or nullmailer's\n"
70 "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
71 "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
73 program_name, program_name, program_name);
77 if (!(pwp = getpwuid(getuid())))
80 GT_("%s: You don't exist. Go away.\n"),
86 memcpy(&by_uid, pwp, sizeof(struct passwd));
87 if (!user || !(pwp = getpwnam(user)))
92 * This logic is needed to handle gracefully the possibility
93 * that multiple names might be mapped to one UID.
95 memcpy(&by_name, pwp, sizeof(struct passwd));
97 if (by_name.pw_uid == by_uid.pw_uid)
102 user = xstrdup(pwp->pw_name);
105 /* compute user's home directory */
106 home = getenv("HOME_ETC");
107 if (!home && !(home = getenv("HOME")))
110 /* compute fetchmail's home directory */
111 if (!(fmhome = getenv("FETCHMAILHOME")))
114 #define RCFILE_NAME "fetchmailrc"
116 * The (fmhome==home) leaves an extra character for a . at the
117 * beginning of the rc file's name, iff fetchmail is using $HOME
118 * for its files. We don't want to do that if fetchmail has its
119 * own home ($FETCHMAILHOME), however.
121 rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
122 /* avoid //.fetchmailrc */
123 if (strcmp(fmhome, "/") != 0)
124 strcpy(rcfile, fmhome);
128 if (rcfile[strlen(rcfile) - 1] != '/')
132 strcat(rcfile, RCFILE_NAME);
135 char *host_fqdn(void)
136 /* get the FQDN of the machine we're running */
138 char tmpbuf[HOSTLEN+1];
140 if (gethostname(tmpbuf, sizeof(tmpbuf)))
142 fprintf(stderr, GT_("%s: can't determine your host!"),
146 #ifdef HAVE_GETHOSTBYNAME
147 /* if we got a . in the hostname assume it is a FQDN */
148 if (strchr(tmpbuf, '.') == NULL)
152 /** XXX FIXME: use getaddrinfo instead? */
153 /* if we got a basename (as we do in Linux) make a FQDN of it */
154 hp = gethostbyname(tmpbuf);
155 if (hp == (struct hostent *) NULL)
157 /* exit with error message */
159 GT_("gethostbyname failed for %s\n"), tmpbuf);
162 return(xstrdup(hp->h_name));
165 #endif /* HAVE_GETHOSTBYNAME */
166 return(xstrdup(tmpbuf));
169 static char *tzoffset(time_t *now)
170 /* calculate timezone offset */
172 static char offset_string[6];
179 off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
180 if (lt->tm_year < gmt.tm_year)
182 else if (lt->tm_year > gmt.tm_year)
184 else if (lt->tm_yday < gmt.tm_yday)
186 else if (lt->tm_yday > gmt.tm_yday)
192 if (off >= 24 * 60) /* should be impossible */
193 off = 23 * 60 + 59; /* if not, insert silly value */
194 snprintf(offset_string, sizeof(offset_string),
195 "%c%02d%02d", sign, off / 60, off % 60);
196 return (offset_string);
199 char *rfc822timestamp(void)
200 /* return a timestamp in RFC822 form */
208 * Conform to RFC822. We generate a 4-digit year here, avoiding
209 * Y2K hassles. Max length of this timestamp in an English locale
210 * should be 29 chars. The only things that should vary by locale
211 * are the day and month abbreviations. The set_locale calls prevent
212 * weird multibyte i18n characters (such as kanji) from showing up
213 * in your Received headers.
215 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
216 setlocale (LC_TIME, "C");
218 strftime(buf, sizeof(buf)-1,
219 "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
220 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
221 setlocale (LC_TIME, "");
223 strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
226 * This is really just a portability fallback, as the
227 * date format ctime(3) emits is not RFC822
230 strcpy(buf, ctime(&now));
231 buf[strlen(buf)-1] = '\0'; /* remove trailing \n */
232 #endif /* HAVE_STRFTIME */
237 const char *showproto(int proto)
238 /* protocol index to protocol name mapping */
242 case P_AUTO: return("auto");
244 case P_POP2: return("POP2");
245 #endif /* POP2_ENABLE */
247 case P_POP3: return("POP3");
248 case P_APOP: return("APOP");
249 case P_RPOP: return("RPOP");
250 #endif /* POP3_ENABLE */
252 case P_IMAP: return("IMAP");
253 #endif /* IMAP_ENABLE */
255 case P_ETRN: return("ETRN");
256 #endif /* ETRN_ENABLE */
258 case P_ODMR: return("ODMR");
259 #endif /* ODMR_ENABLE */
260 default: return("unknown?!?");
264 char *visbuf(const char *buf)
265 /* visibilize a given string */
272 needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */
274 if (needed > vbufs) {
276 vbuf = xrealloc(vbuf, vbufs);
283 if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; }
284 else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; }
285 else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++;
286 else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; }
287 else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; }
288 else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; }
289 else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; }
290 else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; }
291 else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; }
292 else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; }
295 const char hex[] = "0123456789abcdef";
296 *tp++ = '\\'; *tp++ = '0'; *tp++ = 'x';
297 *tp++ = hex[*buf >> 4];
298 *tp++ = hex[*buf & 0xf];
305 /* env.c ends here */