#include "config.h"
#include <stdio.h>
#include <ctype.h>
#if defined(STDC_HEADERS)
#include <stdlib.h>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include <pwd.h>
#include <string.h>
#ifdef HAVE_NET_SOCKET_H
#include <net/socket.h>
#endif
#ifdef HAVE_GETHOSTBYNAME
#include <netdb.h>
#endif
#include <sys/types.h>
#include <time.h>
#include "fetchmail.h"
#include "i18n.h"
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
#include <time.h>
#include <locale.h>
#endif
extern char *getenv();
extern char *program_name;
void envquery(int argc, char **argv)
{
struct passwd by_name, by_uid, *pwp;
if (!(user = getenv("FETCHMAILUSER")))
{
if (!(user = getenv("LOGNAME")))
{
user = getenv("USER");
}
}
if ((program_name = strrchr(argv[0], '/')) != NULL)
++program_name;
else
program_name = argv[0];
if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
{
fprintf(stderr,
GT_("%s: The QMAILINJECT environment variable is set.\n"
"This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"
"tamper with your From: or Message-ID: headers.\n"
"Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
"%s: Abort.\n"),
program_name, program_name, program_name);
exit(PS_UNDEFINED);
}
if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
{
fprintf(stderr,
GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
"This is dangerous as it can make nullmailer-inject or nullmailer's\n"
"sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
"Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
"%s: Abort.\n"),
program_name, program_name, program_name);
exit(PS_UNDEFINED);
}
if (!(pwp = getpwuid(getuid())))
{
fprintf(stderr,
GT_("%s: You don't exist. Go away.\n"),
program_name);
exit(PS_UNDEFINED);
}
else
{
memcpy(&by_uid, pwp, sizeof(struct passwd));
if (!user || !(pwp = getpwnam(user)))
pwp = &by_uid;
else
{
memcpy(&by_name, pwp, sizeof(struct passwd));
if (by_name.pw_uid == by_uid.pw_uid)
pwp = &by_name;
else
pwp = &by_uid;
}
user = xstrdup(pwp->pw_name);
}
if (!(home = getenv("HOME")))
home = pwp->pw_dir;
if (!(fmhome = getenv("FETCHMAILHOME")))
fmhome = home;
#define RCFILE_NAME "fetchmailrc"
rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
if (strcmp(fmhome, "/") != 0)
strcpy(rcfile, fmhome);
else
*rcfile = '\0';
if (rcfile[strlen(rcfile) - 1] != '/')
strcat(rcfile, "/");
if (fmhome==home)
strcat(rcfile, ".");
strcat(rcfile, RCFILE_NAME);
}
char *host_fqdn(void)
{
char tmpbuf[HOSTLEN+1];
if (gethostname(tmpbuf, sizeof(tmpbuf)))
{
fprintf(stderr, GT_("%s: can't determine your host!"),
program_name);
exit(PS_DNS);
}
#ifdef HAVE_GETHOSTBYNAME
if (strchr(tmpbuf, '.') == NULL)
{
struct hostent *hp;
hp = gethostbyname(tmpbuf);
if (hp == (struct hostent *) NULL)
{
fprintf(stderr,
GT_("gethostbyname failed for %s\n"), tmpbuf);
exit(PS_DNS);
}
return(xstrdup(hp->h_name));
}
else
#endif
return(xstrdup(tmpbuf));
}
static char *tzoffset(time_t *now)
{
static char offset_string[6];
struct tm gmt, *lt;
int off;
char sign = '+';
gmt = *gmtime(now);
lt = localtime(now);
off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
if (lt->tm_year < gmt.tm_year)
off -= 24 * 60;
else if (lt->tm_year > gmt.tm_year)
off += 24 * 60;
else if (lt->tm_yday < gmt.tm_yday)
off -= 24 * 60;
else if (lt->tm_yday > gmt.tm_yday)
off += 24 * 60;
if (off < 0) {
sign = '-';
off = -off;
}
if (off >= 24 * 60)
off = 23 * 60 + 59;
sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
return (offset_string);
}
char *rfc822timestamp(void)
{
time_t now;
static char buf[50];
time(&now);
#ifdef HAVE_STRFTIME
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
setlocale (LC_TIME, "C");
#endif
strftime(buf, sizeof(buf)-1,
"%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
#if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS)
setlocale (LC_TIME, "");
#endif
strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
#else
strcpy(buf, ctime(&now));
buf[strlen(buf)-1] = '\0';
#endif
return(buf);
}
const char *showproto(int proto)
{
switch (proto)
{
case P_AUTO: return("auto");
#ifdef POP2_ENABLE
case P_POP2: return("POP2");
#endif
#ifdef POP3_ENABLE
case P_POP3: return("POP3");
case P_APOP: return("APOP");
case P_RPOP: return("RPOP");
#endif
#ifdef IMAP_ENABLE
case P_IMAP: return("IMAP");
#endif
#ifdef ETRN_ENABLE
case P_ETRN: return("ETRN");
#endif
#ifdef ODMR_ENABLE
case P_ODMR: return("ODMR");
#endif
default: return("unknown?!?");
}
}
char *visbuf(const char *buf)
{
static char vbuf[BUFSIZ];
char *tp = vbuf;
while (*buf)
{
if (*buf == '"')
{
*tp++ = '\\'; *tp++ = '"';
buf++;
}
else if (*buf == '\\')
{
*tp++ = '\\'; *tp++ = '\\';
buf++;
}
else if (isprint(*buf) || *buf == ' ')
*tp++ = *buf++;
else if (*buf == '\n')
{
*tp++ = '\\'; *tp++ = 'n';
buf++;
}
else if (*buf == '\r')
{
*tp++ = '\\'; *tp++ = 'r';
buf++;
}
else if (*buf == '\b')
{
*tp++ = '\\'; *tp++ = 'b';
buf++;
}
else if (*buf < ' ')
{
*tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
buf++;
}
else
{
(void) sprintf(tp, "\\0x%02x", *buf++);
tp += strlen(tp);
}
}
*tp++ = '\0';
return(vbuf);
}