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