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