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