]> Pileus Git - ~andy/fetchmail/blob - env.c
e69fea0f817bbac9698225660ed188e9774d9145
[~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
74 or nullmailer's\n" 
75                     "sendmail wrapper tamper with your From:, Message-ID: or Return-Path: headers.\n"
76                     "Try \"env NULLMAILER_FLAGS= %s YOUR ARGUMENTS HERE\"\n"
77                     "%s: Abort.\n"), 
78                 program_name, program_name, program_name);
79         exit(PS_UNDEFINED);
80     }
81
82     if (!(pwp = getpwuid(getuid())))
83     {
84         fprintf(stderr,
85                 GT_("%s: You don't exist.  Go away.\n"),
86                 program_name);
87         exit(PS_UNDEFINED);
88     }
89     else
90     {
91         memcpy(&by_uid, pwp, sizeof(struct passwd));
92         if (!user || !(pwp = getpwnam(user)))
93             pwp = &by_uid;
94         else
95         {
96             /*
97              * This logic is needed to handle gracefully the possibility
98              * that multiple names might be mapped to one UID.
99              */
100             memcpy(&by_name, pwp, sizeof(struct passwd));
101
102             if (by_name.pw_uid == by_uid.pw_uid)
103                 pwp = &by_name;
104             else
105                 pwp = &by_uid;
106         }
107         user = xstrdup(pwp->pw_name);
108     }
109
110     /* compute user's home directory */
111     if (!(home = getenv("HOME")))
112         home = pwp->pw_dir;
113
114     /* compute fetchmail's home directory */
115     if (!(fmhome = getenv("FETCHMAILHOME")))
116         fmhome = home;
117
118 #define RCFILE_NAME     "fetchmailrc"
119     /*
120      * The (fmhome==home) leaves an extra character for a . at the
121      * beginning of the rc file's name, iff fetchmail is using $HOME
122      * for its files. We don't want to do that if fetchmail has its
123      * own home ($FETCHMAILHOME), however.
124      */
125     rcfile = (char *)xmalloc(strlen(fmhome)+sizeof(RCFILE_NAME)+(fmhome==home)+2);
126     /* avoid //.fetchmailrc */
127     if (strcmp(fmhome, "/") != 0)
128         strcpy(rcfile, fmhome);
129     else
130         *rcfile = '\0';
131
132     if (rcfile[strlen(rcfile) - 1] != '/')
133         strcat(rcfile, "/");
134     if (fmhome==home)
135         strcat(rcfile, ".");
136     strcat(rcfile, RCFILE_NAME);
137 }
138
139 char *host_fqdn(void)
140 /* get the FQDN of the machine we're running */
141 {
142     char        tmpbuf[HOSTLEN+1];
143
144     if (gethostname(tmpbuf, sizeof(tmpbuf)))
145     {
146         fprintf(stderr, GT_("%s: can't determine your host!"),
147                 program_name);
148         exit(PS_DNS);
149     }
150 #ifdef HAVE_GETHOSTBYNAME
151     /* if we got a . in the hostname assume it is a FQDN */
152     if (strchr(tmpbuf, '.') == NULL)
153     {
154         struct hostent *hp;
155
156         /* if we got a basename (as we do in Linux) make a FQDN of it */
157         hp = gethostbyname(tmpbuf);
158         if (hp == (struct hostent *) NULL)
159         {
160             /* exit with error message */
161             fprintf(stderr,
162                     GT_("gethostbyname failed for %s\n"), tmpbuf);
163             exit(PS_DNS);
164         }
165         return(xstrdup(hp->h_name));
166     }
167     else
168 #endif /* HAVE_GETHOSTBYNAME */
169         return(xstrdup(tmpbuf));
170 }
171
172 static char *tzoffset(time_t *now)
173 /* calculate timezone offset */
174 {
175     static char offset_string[6];
176     struct tm gmt, *lt;
177     int off;
178     char sign = '+';
179
180     gmt = *gmtime(now);
181     lt = localtime(now);
182     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
183     if (lt->tm_year < gmt.tm_year)
184         off -= 24 * 60;
185     else if (lt->tm_year > gmt.tm_year)
186         off += 24 * 60;
187     else if (lt->tm_yday < gmt.tm_yday)
188         off -= 24 * 60;
189     else if (lt->tm_yday > gmt.tm_yday)
190         off += 24 * 60;
191     if (off < 0) {
192         sign = '-';
193         off = -off;
194     }
195     if (off >= 24 * 60)                 /* should be impossible */
196         off = 23 * 60 + 59;             /* if not, insert silly value */
197     sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
198     return (offset_string);
199 }
200
201 char *rfc822timestamp(void)
202 /* return a timestamp in RFC822 form */
203 {
204     time_t      now;
205     static char buf[50];
206
207     time(&now);
208 #ifdef HAVE_STRFTIME
209     /*
210      * Conform to RFC822.  We generate a 4-digit year here, avoiding
211      * Y2K hassles.  Max length of this timestamp in an English locale
212      * should be 29 chars.  The only things that should vary by locale
213      * are the day and month abbreviations.  The set_locale calls prevent
214      * weird multibyte i18n characters (such as kanji) from showing up
215      * in your Received headers.
216      */
217 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
218     setlocale (LC_TIME, "C");
219 #endif
220     strftime(buf, sizeof(buf)-1, 
221              "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
222 #if defined(HAVE_SETLOCALE) && defined(ENABLE_NLS) && defined(HAVE_STRFTIME)
223     setlocale (LC_TIME, "");
224 #endif
225     strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
226 #else
227     /*
228      * This is really just a portability fallback, as the
229      * date format ctime(3) emits is not RFC822
230      * conformant.
231      */
232     strcpy(buf, ctime(&now));
233     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
234 #endif /* HAVE_STRFTIME */
235
236     return(buf);
237 }
238
239 const char *showproto(int proto)
240 /* protocol index to protocol name mapping */
241 {
242     switch (proto)
243     {
244     case P_AUTO: return("auto");
245 #ifdef POP2_ENABLE
246     case P_POP2: return("POP2");
247 #endif /* POP2_ENABLE */
248 #ifdef POP3_ENABLE
249     case P_POP3: return("POP3");
250     case P_APOP: return("APOP");
251     case P_RPOP: return("RPOP");
252 #endif /* POP3_ENABLE */
253 #ifdef IMAP_ENABLE
254     case P_IMAP: return("IMAP");
255 #endif /* IMAP_ENABLE */
256 #ifdef ETRN_ENABLE
257     case P_ETRN: return("ETRN");
258 #endif /* ETRN_ENABLE */
259 #ifdef ODMR_ENABLE
260     case P_ODMR: return("ODMR");
261 #endif /* ODMR_ENABLE */
262     default: return("unknown?!?");
263     }
264 }
265
266 char *visbuf(const char *buf)
267 /* visibilize a given string */
268 {
269     static char vbuf[BUFSIZ];
270     char *tp = vbuf;
271
272     while (*buf)
273     {
274         if (*buf == '"')
275         {
276             *tp++ = '\\'; *tp++ = '"';
277             buf++;
278         }
279         else if (*buf == '\\')
280         {
281             *tp++ = '\\'; *tp++ = '\\';
282             buf++;
283         }
284         else if (isprint(*buf) || *buf == ' ')
285             *tp++ = *buf++;
286         else if (*buf == '\n')
287         {
288             *tp++ = '\\'; *tp++ = 'n';
289             buf++;
290         }
291         else if (*buf == '\r')
292         {
293             *tp++ = '\\'; *tp++ = 'r';
294             buf++;
295         }
296         else if (*buf == '\b')
297         {
298             *tp++ = '\\'; *tp++ = 'b';
299             buf++;
300         }
301         else if (*buf < ' ')
302         {
303             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
304             buf++;
305         }
306         else
307         {
308             (void) sprintf(tp, "\\0x%02x", *buf++);
309             tp += strlen(tp);
310         }
311     }
312     *tp++ = '\0';
313     return(vbuf);
314 }
315
316 /* env.c ends here */