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