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