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