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