| 1 |
/* |
| 2 |
* env.c -- small service routines |
| 3 |
* |
| 4 |
* Copyright 1998 by Eric S. Raymond |
| 5 |
* For license terms, see the file COPYING in this directory. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "config.h" |
| 9 |
#include <stdio.h> |
| 10 |
#include <ctype.h> |
| 11 |
#if defined(STDC_HEADERS) |
| 12 |
#include <stdlib.h> |
| 13 |
#endif |
| 14 |
#if defined(HAVE_UNISTD_H) |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
#include <pwd.h> |
| 18 |
#include <string.h> |
| 19 |
#ifdef HAVE_NET_SOCKET_H |
| 20 |
#include <net/socket.h> |
| 21 |
#endif |
| 22 |
#include <netdb.h> |
| 23 |
#include <sys/types.h> |
| 24 |
#include <sys/socket.h> |
| 25 |
#include "fetchmail.h" |
| 26 |
#include "getaddrinfo.h" |
| 27 |
|
| 28 |
#include "i18n.h" |
| 29 |
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME) |
| 30 |
#include <locale.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifndef HAVE_DECL_GETENV |
| 34 |
extern char *getenv(const char *); /* needed on sysV68 R3V7.1. */ |
| 35 |
#endif |
| 36 |
|
| 37 |
void envquery(int argc, char **argv) |
| 38 |
/* set up basic stuff from the environment (including the rc file name) */ |
| 39 |
{ |
| 40 |
struct passwd by_name, by_uid, *pwp; |
| 41 |
|
| 42 |
(void)argc; |
| 43 |
|
| 44 |
(void)argc; |
| 45 |
if (!(user = getenv("FETCHMAILUSER"))) |
| 46 |
{ |
| 47 |
if (!(user = getenv("LOGNAME"))) |
| 48 |
{ |
| 49 |
user = getenv("USER"); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ((program_name = strrchr(argv[0], '/')) != NULL) |
| 54 |
++program_name; |
| 55 |
else |
| 56 |
program_name = argv[0]; |
| 57 |
|
| 58 |
if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), "")) |
| 59 |
{ |
| 60 |
fprintf(stderr, |
| 61 |
GT_("%s: The QMAILINJECT environment variable is set.\n" |
| 62 |
"This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n" |
| 63 |
"tamper with your From: or Message-ID: headers.\n" |
| 64 |
"Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n" |
| 65 |
"%s: Abort.\n"), |
| 66 |
program_name, program_name, program_name); |
| 67 |
exit(PS_UNDEFINED); |
| 68 |
} |
| 69 |
|
| 70 |
if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), "")) |
| 71 |
{ |
| 72 |
fprintf(stderr, |
| 73 |
GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n" |
| 74 |
"This is dangerous as it can make nullmailer-inject or nullmailer's\n" |
| 75 |
"sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n" |
| 76 |
"Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n" |
| 77 |
"%s: Abort.\n"), |
| 78 |
program_name, program_name, program_name); |
| 79 |
exit(PS_UNDEFINED); |
| 80 |
} |
| 81 |
|
| 82 |
if (!(pwp = getpwuid(getuid()))) |
| 83 |
{ |
| 84 |
fprintf(stderr, |
| 85 |
GT_("%s: You don't exist. Go away.\n"), |
| 86 |
program_name); |
| 87 |
exit(PS_UNDEFINED); |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
memcpy(&by_uid, pwp, sizeof(struct passwd)); |
| 92 |
if (!user || !(pwp = getpwnam(user))) |
| 93 |
pwp = &by_uid; |
| 94 |
else |
| 95 |
{ |
| 96 |
/* |
| 97 |
* This logic is needed to handle gracefully the possibility |
| 98 |
* that multiple names might be mapped to one UID. |
| 99 |
*/ |
| 100 |
memcpy(&by_name, pwp, sizeof(struct passwd)); |
| 101 |
|
| 102 |
if (by_name.pw_uid == by_uid.pw_uid) |
| 103 |
pwp = &by_name; |
| 104 |
else |
| 105 |
pwp = &by_uid; |
| 106 |
} |
| 107 |
user = xstrdup(pwp->pw_name); |
| 108 |
} |
| 109 |
|
| 110 |
endpwent(); |
| 111 |
|
| 112 |
/* compute user's home directory */ |
| 113 |
home = getenv("HOME_ETC"); |
| 114 |
if (!home && !(home = getenv("HOME"))) |
| 115 |
home = xstrdup(pwp->pw_dir); |
| 116 |
|
| 117 |
/* compute fetchmail's home directory */ |
| 118 |
if (!(fmhome = getenv("FETCHMAILHOME"))) |
| 119 |
fmhome = home; |
| 120 |
|
| 121 |
#define RCFILE_NAME "fetchmailrc" |
| 122 |
/* |
| 123 |
* The (fmhome==home) leaves an extra character for a . at the |
| 124 |
* beginning of the rc file's name, iff fetchmail is using $HOME |
| 125 |
* for its files. We don't want to do that if fetchmail has its |
| 126 |
* own home ($FETCHMAILHOME), however. |
| 127 |
*/ |
| 128 |
rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2); |
| 129 |
/* avoid //.fetchmailrc */ |
| 130 |
if (strcmp(fmhome, "/") != 0) |
| 131 |
strcpy(rcfile, fmhome); |
| 132 |
else |
| 133 |
*rcfile = '\0'; |
| 134 |
|
| 135 |
if (rcfile[strlen(rcfile) - 1] != '/') |
| 136 |
strcat(rcfile, "/"); |
| 137 |
if (fmhome==home) |
| 138 |
strcat(rcfile, "."); |
| 139 |
strcat(rcfile, RCFILE_NAME); |
| 140 |
} |
| 141 |
|
| 142 |
char *host_fqdn(int required) |
| 143 |
{ |
| 144 |
char tmpbuf[HOSTLEN+1]; |
| 145 |
char *result; |
| 146 |
|
| 147 |
if (gethostname(tmpbuf, sizeof(tmpbuf))) |
| 148 |
{ |
| 149 |
fprintf(stderr, GT_("%s: can't determine your host!"), |
| 150 |
program_name); |
| 151 |
exit(PS_DNS); |
| 152 |
} |
| 153 |
|
| 154 |
/* if we got no . in the hostname, try to canonicalize it, |
| 155 |
* else assume it is a FQDN */ |
| 156 |
if (strchr(tmpbuf, '.') == NULL) |
| 157 |
{ |
| 158 |
/* if we got a basename without dots, as we often do in Linux, |
| 159 |
* look up canonical name (make a FQDN of it) */ |
| 160 |
struct addrinfo hints, *res; |
| 161 |
int e; |
| 162 |
|
| 163 |
memset(&hints, 0, sizeof hints); |
| 164 |
hints.ai_family = AF_UNSPEC; |
| 165 |
hints.ai_socktype = SOCK_STREAM; |
| 166 |
hints.ai_flags = AI_CANONNAME; |
| 167 |
|
| 168 |
e = fm_getaddrinfo(tmpbuf, NULL, &hints, &res); |
| 169 |
if (e) { |
| 170 |
/* exit with error message */ |
| 171 |
fprintf(stderr, |
| 172 |
GT_("gethostbyname failed for %s\n"), tmpbuf); |
| 173 |
fprintf(stderr, "%s", gai_strerror(e)); |
| 174 |
fprintf(stderr, GT_("Cannot find my own host in hosts database to qualify it!\n")); |
| 175 |
if (required) |
| 176 |
exit(PS_DNS); |
| 177 |
else { |
| 178 |
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")); |
| 179 |
return xstrdup(tmpbuf); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
result = xstrdup(res->ai_canonname ? res->ai_canonname : tmpbuf); |
| 184 |
fm_freeaddrinfo(res); |
| 185 |
} |
| 186 |
else |
| 187 |
result = xstrdup(tmpbuf); |
| 188 |
|
| 189 |
return result; |
| 190 |
} |
| 191 |
|
| 192 |
static char *tzoffset(time_t *now) |
| 193 |
/* calculate timezone offset */ |
| 194 |
{ |
| 195 |
static char offset_string[6]; |
| 196 |
struct tm gmt, *lt; |
| 197 |
int off; |
| 198 |
char sign = '+'; |
| 199 |
|
| 200 |
gmt = *gmtime(now); |
| 201 |
lt = localtime(now); |
| 202 |
off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min; |
| 203 |
if (lt->tm_year < gmt.tm_year) |
| 204 |
off -= 24 * 60; |
| 205 |
else if (lt->tm_year > gmt.tm_year) |
| 206 |
off += 24 * 60; |
| 207 |
else if (lt->tm_yday < gmt.tm_yday) |
| 208 |
off -= 24 * 60; |
| 209 |
else if (lt->tm_yday > gmt.tm_yday) |
| 210 |
off += 24 * 60; |
| 211 |
if (off < 0) { |
| 212 |
sign = '-'; |
| 213 |
off = -off; |
| 214 |
} |
| 215 |
if (off >= 24 * 60) /* should be impossible */ |
| 216 |
off = 23 * 60 + 59; /* if not, insert silly value */ |
| 217 |
snprintf(offset_string, sizeof(offset_string), |
| 218 |
"%c%02d%02d", sign, off / 60, off % 60); |
| 219 |
return (offset_string); |
| 220 |
} |
| 221 |
|
| 222 |
char *rfc822timestamp(void) |
| 223 |
/* return a timestamp in RFC822 form */ |
| 224 |
{ |
| 225 |
time_t now; |
| 226 |
static char buf[50]; |
| 227 |
|
| 228 |
time(&now); |
| 229 |
#ifdef HAVE_STRFTIME |
| 230 |
/* |
| 231 |
* Conform to RFC822. We generate a 4-digit year here, avoiding |
| 232 |
* Y2K hassles. Max length of this timestamp in an English locale |
| 233 |
* should be 29 chars. The only things that should vary by locale |
| 234 |
* are the day and month abbreviations. The set_locale calls prevent |
| 235 |
* weird multibyte i18n characters (such as kanji) from showing up |
| 236 |
* in your Received headers. |
| 237 |
*/ |
| 238 |
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) |
| 239 |
setlocale (LC_TIME, "C"); |
| 240 |
#endif |
| 241 |
strftime(buf, sizeof(buf)-1, |
| 242 |
"%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now)); |
| 243 |
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) |
| 244 |
setlocale (LC_TIME, ""); |
| 245 |
#endif |
| 246 |
strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5); |
| 247 |
#else |
| 248 |
/* |
| 249 |
* This is really just a portability fallback, as the |
| 250 |
* date format ctime(3) emits is not RFC822 |
| 251 |
* conformant. |
| 252 |
*/ |
| 253 |
strlcpy(buf, ctime(&now), sizeof(buf)); |
| 254 |
buf[strlen(buf)-1] = '\0'; /* remove trailing \n */ |
| 255 |
#endif /* HAVE_STRFTIME */ |
| 256 |
|
| 257 |
return(buf); |
| 258 |
} |
| 259 |
|
| 260 |
const char *showproto(int proto) |
| 261 |
/* protocol index to protocol name mapping */ |
| 262 |
{ |
| 263 |
switch (proto) |
| 264 |
{ |
| 265 |
case P_AUTO: return("auto"); |
| 266 |
#ifdef POP2_ENABLE |
| 267 |
case P_POP2: return("POP2"); |
| 268 |
#endif /* POP2_ENABLE */ |
| 269 |
#ifdef POP3_ENABLE |
| 270 |
case P_POP3: return("POP3"); |
| 271 |
case P_APOP: return("APOP"); |
| 272 |
case P_RPOP: return("RPOP"); |
| 273 |
#endif /* POP3_ENABLE */ |
| 274 |
#ifdef IMAP_ENABLE |
| 275 |
case P_IMAP: return("IMAP"); |
| 276 |
#endif /* IMAP_ENABLE */ |
| 277 |
#ifdef ETRN_ENABLE |
| 278 |
case P_ETRN: return("ETRN"); |
| 279 |
#endif /* ETRN_ENABLE */ |
| 280 |
#ifdef ODMR_ENABLE |
| 281 |
case P_ODMR: return("ODMR"); |
| 282 |
#endif /* ODMR_ENABLE */ |
| 283 |
default: return("unknown?!?"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
char *visbuf(const char *buf) |
| 288 |
/* visibilize a given string */ |
| 289 |
{ |
| 290 |
static char *vbuf; |
| 291 |
static size_t vbufs; |
| 292 |
char *tp; |
| 293 |
size_t needed; |
| 294 |
|
| 295 |
needed = strlen(buf) * 5 + 1; /* worst case: HEX, plus NUL byte */ |
| 296 |
|
| 297 |
if (!vbuf || needed > vbufs) { |
| 298 |
vbufs = needed; |
| 299 |
vbuf = (char *)xrealloc(vbuf, vbufs); |
| 300 |
} |
| 301 |
|
| 302 |
tp = vbuf; |
| 303 |
|
| 304 |
while (*buf) |
| 305 |
{ |
| 306 |
if (*buf == '"') { *tp++ = '\\'; *tp++ = '"'; buf++; } |
| 307 |
else if (*buf == '\\') { *tp++ = '\\'; *tp++ = '\\'; buf++; } |
| 308 |
else if (isprint((unsigned char)*buf) || *buf == ' ') *tp++ = *buf++; |
| 309 |
else if (*buf == '\a') { *tp++ = '\\'; *tp++ = 'a'; buf++; } |
| 310 |
else if (*buf == '\b') { *tp++ = '\\'; *tp++ = 'b'; buf++; } |
| 311 |
else if (*buf == '\f') { *tp++ = '\\'; *tp++ = 'f'; buf++; } |
| 312 |
else if (*buf == '\n') { *tp++ = '\\'; *tp++ = 'n'; buf++; } |
| 313 |
else if (*buf == '\r') { *tp++ = '\\'; *tp++ = 'r'; buf++; } |
| 314 |
else if (*buf == '\t') { *tp++ = '\\'; *tp++ = 't'; buf++; } |
| 315 |
else if (*buf == '\v') { *tp++ = '\\'; *tp++ = 'v'; buf++; } |
| 316 |
else |
| 317 |
{ |
| 318 |
const char hex[] = "0123456789abcdef"; |
| 319 |
*tp++ = '\\'; *tp++ = '0'; *tp++ = 'x'; |
| 320 |
*tp++ = hex[*buf >> 4]; |
| 321 |
*tp++ = hex[*buf & 0xf]; |
| 322 |
buf++; |
| 323 |
} |
| 324 |
} |
| 325 |
*tp = '\0'; |
| 326 |
return(vbuf); |
| 327 |
} |
| 328 |
/* env.c ends here */ |