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