]> Pileus Git - ~andy/fetchmail/blob - env.c
Compute FQDN when Kerberos is in use.
[~andy/fetchmail] / env.c
1 /*
2  * env.c -- small service routines
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8 #include "fetchmail.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
24 extern char *getenv();  /* needed on sysV68 R3V7.1. */
25
26 extern char *program_name;
27
28 void envquery(int argc, char **argv)
29 /* set up basic stuff from the environment (including the rc file name) */
30 {
31     char tmpbuf[BUFSIZ]; 
32     struct passwd *pw;
33
34     if ((program_name = strrchr(argv[0], '/')) != NULL)
35         ++program_name;
36     else
37         program_name = argv[0];
38
39     if ((user = getenv("USER")) == (char *)NULL)
40         user = getenv("LOGNAME");
41
42     if ((user == (char *)NULL) || (home = getenv("HOME")) == (char *)NULL)
43     {
44         if ((pw = getpwuid(getuid())) != NULL)
45         {
46             user = pw->pw_name;
47             home = pw->pw_dir;
48         }
49         else
50         {
51             fprintf(stderr,
52                     "%s: can't find your name and home directory!\n",
53                     program_name);
54             exit(PS_UNDEFINED);
55         }
56     }
57
58 #define RCFILE_NAME     ".fetchmailrc"
59     rcfile = (char *) xmalloc(strlen(home)+strlen(RCFILE_NAME)+2);
60     /* avoid //.fetchmailrc */
61     if (strcmp(home, "/") != 0) {
62         strcpy(rcfile, home);
63     } else {
64         *rcfile = '\0';
65     }
66     strcat(rcfile, "/");
67     strcat(rcfile, RCFILE_NAME);
68 }
69
70 char *host_fqdn(void)
71 /* get the FQDN of the machine we're running */
72 {
73     char        tmpbuf[HOSTLEN+1];
74
75     if (gethostname(tmpbuf, sizeof(tmpbuf)))
76     {
77         fprintf(stderr, "%s: can't determine your host!",
78                 program_name);
79         exit(PS_DNS);
80     }
81 #ifdef HAVE_GETHOSTBYNAME
82     /* if we got a . in the hostname assume it is a FQDN */
83     if (strchr(tmpbuf, '.') == NULL)
84     {
85         struct hostent *hp;
86
87         /* if we got a basename (as we do in Linux) make a FQDN of it */
88         hp = gethostbyname(tmpbuf);
89         if (hp == (struct hostent *) NULL)
90         {
91             /* exit with error message */
92             fprintf(stderr,
93                     "gethostbyname failed for %s\n", tmpbuf);
94             exit(PS_DNS);
95         }
96         return(xstrdup(hp->h_name));
97     }
98     else
99 #endif /* HAVE_GETHOSTBYNAME */
100         return(xstrdup(tmpbuf));
101 }
102
103
104 char *showproto(int proto)
105 /* protocol index to protocol name mapping */
106 {
107     switch (proto)
108     {
109     case P_AUTO: return("auto"); break;
110 #ifdef POP2_ENABLE
111     case P_POP2: return("POP2"); break;
112 #endif /* POP2_ENABLE */
113     case P_POP3: return("POP3"); break;
114     case P_IMAP: return("IMAP"); break;
115     case P_IMAP_K4: return("IMAP-K4"); break;
116 #ifdef GSSAPI
117     case P_IMAP_GSS: return("IMAP-GSS"); break;
118 #endif /* GSSAPI */
119     case P_APOP: return("APOP"); break;
120     case P_RPOP: return("RPOP"); break;
121     case P_ETRN: return("ETRN"); break;
122     default: return("unknown?!?"); break;
123     }
124 }
125
126 char *visbuf(const char *buf)
127 /* visibilize a given string */
128 {
129     static char vbuf[BUFSIZ];
130     char *tp = vbuf;
131
132     while (*buf)
133     {
134         if (*buf == '"')
135         {
136             *tp++ = '\\'; *tp++ = '"';
137             buf++;
138         }
139         else if (isprint(*buf) || *buf == ' ')
140             *tp++ = *buf++;
141         else if (*buf == '\n')
142         {
143             *tp++ = '\\'; *tp++ = 'n';
144             buf++;
145         }
146         else if (*buf == '\r')
147         {
148             *tp++ = '\\'; *tp++ = 'r';
149             buf++;
150         }
151         else if (*buf == '\b')
152         {
153             *tp++ = '\\'; *tp++ = 'b';
154             buf++;
155         }
156         else if (*buf < ' ')
157         {
158             *tp++ = '\\'; *tp++ = '^'; *tp++ = '@' + *buf;
159             buf++;
160         }
161         else
162         {
163             (void) sprintf(tp, "\\0x%02x", *buf++);
164             tp += strlen(tp);
165         }
166     }
167     *tp++ = '\0';
168     return(vbuf);
169 }
170
171 /* env.c ends here */