]> Pileus Git - ~andy/fetchmail/blob - env.c
FETCHMAILHOME
[~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         strcat(rcfile, fmhome);
102     } else {
103         *rcfile = '\0';
104     }
105     strcat(rcfile, "/");
106     if (fmhome==home)
107         strcat(rcfile, ".");
108     strcat(rcfile, RCFILE_NAME);
109 }
110
111 char *host_fqdn(void)
112 /* get the FQDN of the machine we're running */
113 {
114     char        tmpbuf[HOSTLEN+1];
115
116     if (gethostname(tmpbuf, sizeof(tmpbuf)))
117     {
118         fprintf(stderr, _("%s: can't determine your host!"),
119                 program_name);
120         exit(PS_DNS);
121     }
122 #ifdef HAVE_GETHOSTBYNAME
123     /* if we got a . in the hostname assume it is a FQDN */
124     if (strchr(tmpbuf, '.') == NULL)
125     {
126         struct hostent *hp;
127
128         /* if we got a basename (as we do in Linux) make a FQDN of it */
129         hp = gethostbyname(tmpbuf);
130         if (hp == (struct hostent *) NULL)
131         {
132             /* exit with error message */
133             fprintf(stderr,
134                     _("gethostbyname failed for %s\n"), tmpbuf);
135             exit(PS_DNS);
136         }
137         return(xstrdup(hp->h_name));
138     }
139     else
140 #endif /* HAVE_GETHOSTBYNAME */
141         return(xstrdup(tmpbuf));
142 }
143
144 static char *tzoffset(time_t *now)
145 /* calculate timezone offset */
146 {
147     static char offset_string[6];
148     struct tm gmt, *lt;
149     int off;
150     char sign = '+';
151
152     gmt = *gmtime(now);
153     lt = localtime(now);
154     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
155     if (lt->tm_year < gmt.tm_year)
156         off -= 24 * 60;
157     else if (lt->tm_year > gmt.tm_year)
158         off += 24 * 60;
159     else if (lt->tm_yday < gmt.tm_yday)
160         off -= 24 * 60;
161     else if (lt->tm_yday > gmt.tm_yday)
162         off += 24 * 60;
163     if (off < 0) {
164         sign = '-';
165         off = -off;
166     }
167     if (off >= 24 * 60)                 /* should be impossible */
168         off = 23 * 60 + 59;             /* if not, insert silly value */
169     sprintf(offset_string, "%c%02d%02d", sign, off / 60, off % 60);
170     return (offset_string);
171 }
172
173 char *rfc822timestamp(void)
174 /* return a timestamp in RFC822 form */
175 {
176     time_t      now;
177     static char buf[50];
178
179     time(&now);
180 #ifdef HAVE_STRFTIME
181     /*
182      * Conform to RFC822.  We generate a 4-digit year here, avoiding
183      * Y2K hassles.  Max length of this timestamp in an English locale
184      * should be 29 chars.  The only things that should vary by locale
185      * are the day and month abbreviations.
186      */
187     strftime(buf, sizeof(buf)-1, 
188              "%a, %d %b %Y %H:%M:%S XXXXX (%Z)", localtime(&now));
189     strncpy(strstr(buf, "XXXXX"), tzoffset(&now), 5);
190 #else
191     /*
192      * This is really just a portability fallback, as the
193      * date format ctime(3) emits is not RFC822
194      * conformant.
195      */
196     strcpy(buf, ctime(&now));
197     buf[strlen(buf)-1] = '\0';  /* remove trailing \n */
198 #endif /* HAVE_STRFTIME */
199
200     return(buf);
201 }
202
203 const char *showproto(int proto)
204 /* protocol index to protocol name mapping */
205 {
206     switch (proto)
207     {
208     case P_AUTO: return("auto");
209 #ifdef POP2_ENABLE
210     case P_POP2: return("POP2");
211 #endif /* POP2_ENABLE */
212     case P_POP3: return("POP3");
213     case P_IMAP: return("IMAP");
214     case P_IMAP_K4: return("IMAP-K4");
215 #ifdef GSSAPI
216     case P_IMAP_GSS: return("IMAP-GSS");
217 #endif /* GSSAPI */
218     case P_IMAP_CRAM_MD5: return("IMAP-LOGIN");
219     case P_IMAP_LOGIN: return("IMAP-LOGIN");
220     case P_APOP: return("APOP");
221     case P_RPOP: return("RPOP");
222     case P_ETRN: return("ETRN");
223     default: return("unknown?!?");
224     }
225 }
226
227 char *visbuf(const char *buf)
228 /* visibilize a given string */
229 {
230     static char vbuf[BUFSIZ];
231     char *tp = vbuf;
232
233     while (*buf)
234     {
235         if (*buf == '"')
236         {
237             *tp++ = '\\'; *tp++ = '"';
238             buf++;
239         }
240         else if (*buf == '\\')
241         {
242             *tp++ = '\\'; *tp++ = '\\';
243             buf++;
244         }
245         else if (isprint(*buf) || *buf == ' ')
246             *tp++ = *buf++;
247         else if (*buf == '\n')
248         {
249             *tp++ = '\\'; *tp++ = 'n';
250             buf++;
251         }
252         else if (*buf == '\r')
253         {
254             *tp++ = '\\'; *tp++ = 'r';
255             buf++;
256         }
257         else if (*buf == '\b')
258         {
259             *tp++ = '\\'; *tp++ = 'b';
260             buf++;
261         }
262         else if (*buf < ' ')
263         {
264             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
265             buf++;
266         }
267         else
268         {
269             (void) sprintf(tp, "\\0x%02x", *buf++);
270             tp += strlen(tp);
271         }
272     }
273     *tp++ = '\0';
274     return(vbuf);
275 }
276
277 /* env.c ends here */