]> Pileus Git - ~andy/fetchmail/blob - env.c
86531b96ee96742c79b6f821158cadd7a6d0bfa6
[~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 #ifdef HAVE_GETHOSTBYNAME
23 #include <netdb.h>
24 #endif /* HAVE_GETHOSTBYNAME */
25 #include  <sys/types.h>
26 #include  <time.h>
27 #include "fetchmail.h"
28
29 #include "i18n.h"
30 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
31 #include <time.h>
32 #include <locale.h>
33 #endif
34
35 extern char *getenv();  /* needed on sysV68 R3V7.1. */
36
37 extern char *program_name;
38
39 void envquery(int argc, char **argv)
40 /* set up basic stuff from the environment (including the rc file name) */
41 {
42     struct passwd by_name, by_uid, *pwp;
43
44     if (!(user = getenv("FETCHMAILUSER")))
45     {
46         if (!(user = getenv("LOGNAME")))
47         {
48             user = getenv("USER");
49         }
50     }
51
52     if ((program_name = strrchr(argv[0], '/')) != NULL)
53         ++program_name;
54     else
55         program_name = argv[0];
56
57     if (getenv("QMAILINJECT") && strcmp(getenv("QMAILINJECT"), ""))
58     {
59         fprintf(stderr,
60                 GT_("%s: The QMAILINJECT environment variable is set.\n"
61                     "This is dangerous as it can make qmail-inject or qmail's sendmail wrapper\n"  
62                     "tamper with your From: or Message-ID: headers.\n"
63                     "Try \"env QMAILINJECT= %s YOUR ARGUMENTS HERE\"\n"
64                     "%s: Abort.\n"), 
65                 program_name, program_name, program_name);
66         exit(PS_UNDEFINED);
67     }
68
69     if (getenv("NULLMAILER_FLAGS") && strcmp(getenv("NULLMAILER_FLAGS"), ""))
70     {
71         fprintf(stderr,
72                 GT_("%s: The NULLMAILER_FLAGS environment variable is set.\n"
73                     "This is dangerous as it can make nullmailer-inject or nullmailer's\n" 
74                     "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
75                     "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
76                     "%s: Abort.\n"), 
77                 program_name, program_name, program_name);
78         exit(PS_UNDEFINED);
79     }
80
81     if (!(pwp = getpwuid(getuid())))
82     {
83         fprintf(stderr,
84                 GT_("%s: You don't exist.  Go away.\n"),
85                 program_name);
86         exit(PS_UNDEFINED);
87     }
88     else
89     {
90         memcpy(&by_uid, pwp, sizeof(struct passwd));
91         if (!user || !(pwp = getpwnam(user)))
92             pwp = &by_uid;
93         else
94         {
95             /*
96              * This logic is needed to handle gracefully the possibility
97              * that multiple names might be mapped to one UID.
98              */
99             memcpy(&by_name, pwp, sizeof(struct passwd));
100
101             if (by_name.pw_uid == by_uid.pw_uid)
102                 pwp = &by_name;
103             else
104                 pwp = &by_uid;
105         }
106         user = xstrdup(pwp->pw_name);
107     }
108
109     /* compute user's home directory */
110     if (!(home = getenv("HOME")))
111         home = 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(void)
139 /* get the FQDN of the machine we're running */
140 {
141     char        tmpbuf[HOSTLEN+1];
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 #ifdef HAVE_GETHOSTBYNAME
150     /* if we got a . in the hostname assume it is a FQDN */
151     if (strchr(tmpbuf, '.') == NULL)
152     {
153         struct hostent *hp;
154
155         /* if we got a basename (as we do in Linux) make a FQDN of it */
156         hp = gethostbyname(tmpbuf);
157         if (hp == (struct hostent *) NULL)
158         {
159             /* exit with error message */
160             fprintf(stderr,
161                     GT_("gethostbyname failed for %s\n"), tmpbuf);
162             exit(PS_DNS);
163         }
164         return(xstrdup(hp->h_name));
165     }
166     else
167 #endif /* HAVE_GETHOSTBYNAME */
168         return(xstrdup(tmpbuf));
169 }
170
171 static char *tzoffset(time_t *now)
172 /* calculate timezone offset */
173 {
174     static char offset_string[6];
175     struct tm gmt, *lt;
176     int off;
177     char sign = '+';
178
179     gmt = *gmtime(now);
180     lt = localtime(now);
181     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
182     if (lt->tm_year < gmt.tm_year)
183         off -= 24 * 60;
184     else if (lt->tm_year > gmt.tm_year)
185         off += 24 * 60;
186     else if (lt->tm_yday < gmt.tm_yday)
187         off -= 24 * 60;
188     else if (lt->tm_yday > gmt.tm_yday)
189         off += 24 * 60;
190     if (off < 0) {
191         sign = '-';
192         off = -off;
193     }
194     if (off >= 24 * 60)                 /* should be impossible */
195         off = 23 * 60 + 59;             /* if not, insert silly value */
196     sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
197     return (offset_string);
198 }
199
200 char *rfc822timestamp(void)
201 /* return a timestamp in RFC822 form */
202 {
203     time_t      now;
204     static char buf[50];
205
206     time(&now);
207 #ifdef HAVE_STRFTIME
208     /*
209      * Conform to RFC822.  We generate a 4-digit year here, avoiding
210      * Y2K hassles.  Max length of this timestamp in an English locale
211      * should be 29 chars.  The only things that should vary by locale
212      * are the day and month abbreviations.  The set_locale calls prevent
213      * weird multibyte i18n characters (such as kanji) from showing up
214      * in your Received headers.
215      */
216 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
217     setlocale (LC_TIME, "C");
218 #endif
219     strftime(buf, sizeof(buf)-1, 
220              "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
221 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
222     setlocale (LC_TIME, "");
223 #endif
224     strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
225 #else
226     /*
227      * This is really just a portability fallback, as the
228      * date format ctime(3) emits is not RFC822
229      * conformant.
230      */
231     strcpy(buf, ctime(&now));
232     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
233 #endif /* HAVE_STRFTIME */
234
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 POP2_ENABLE
245     case P_POP2: return("POP2");
246 #endif /* POP2_ENABLE */
247 #ifdef POP3_ENABLE
248     case P_POP3: return("POP3");
249     case P_APOP: return("APOP");
250     case P_RPOP: return("RPOP");
251 #endif /* POP3_ENABLE */
252 #ifdef IMAP_ENABLE
253     case P_IMAP: return("IMAP");
254 #endif /* IMAP_ENABLE */
255 #ifdef ETRN_ENABLE
256     case P_ETRN: return("ETRN");
257 #endif /* ETRN_ENABLE */
258 #ifdef ODMR_ENABLE
259     case P_ODMR: return("ODMR");
260 #endif /* ODMR_ENABLE */
261     default: return("unknown?!?");
262     }
263 }
264
265 char *visbuf(const char *buf)
266 /* visibilize a given string */
267 {
268     static char vbuf[BUFSIZ];
269     char *tp = vbuf;
270
271     while (*buf)
272     {
273         if (*buf == '"')
274         {
275             *tp++ = '\\'; *tp++ = '"';
276             buf++;
277         }
278         else if (*buf == '\\')
279         {
280             *tp++ = '\\'; *tp++ = '\\';
281             buf++;
282         }
283         else if (isprint(*buf) || *buf == ' ')
284             *tp++ = *buf++;
285         else if (*buf == '\n')
286         {
287             *tp++ = '\\'; *tp++ = 'n';
288             buf++;
289         }
290         else if (*buf == '\r')
291         {
292             *tp++ = '\\'; *tp++ = 'r';
293             buf++;
294         }
295         else if (*buf == '\b')
296         {
297             *tp++ = '\\'; *tp++ = 'b';
298             buf++;
299         }
300         else if (*buf < ' ')
301         {
302             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
303             buf++;
304         }
305         else
306         {
307             (void) sprintf(tp, "\\0x%02x", *buf++);
308             tp += strlen(tp);
309         }
310     }
311     *tp++ = '\0';
312     return(vbuf);
313 }
314
315 /* env.c ends here */