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