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