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