Merge from vendor branch OPENSSL:
[dragonfly.git] / contrib / tcp_wrappers / scaffold.c
1  /*
2   * Routines for testing only. Not really industrial strength.
3   * 
4   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5   *
6   * $FreeBSD: src/contrib/tcp_wrappers/scaffold.c,v 1.2.2.1 2000/07/18 08:34:55 ume Exp $
7   * $DragonFly: src/contrib/tcp_wrappers/scaffold.c,v 1.2 2003/06/17 04:24:06 dillon Exp $
8   */
9
10 #ifndef lint
11 static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
12 #endif
13
14 /* System libraries. */
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <stdio.h>
23 #include <syslog.h>
24 #include <setjmp.h>
25 #include <string.h>
26
27 #ifndef INADDR_NONE
28 #define INADDR_NONE     (-1)            /* XXX should be 0xffffffff */
29 #endif
30
31 #ifndef INET6
32 extern char *malloc();
33 #endif
34
35 /* Application-specific. */
36
37 #include "tcpd.h"
38 #include "scaffold.h"
39
40  /*
41   * These are referenced by the options module and by rfc931.c.
42   */
43 int     allow_severity = SEVERITY;
44 int     deny_severity = LOG_WARNING;
45 int     rfc931_timeout = RFC931_TIMEOUT;
46
47 #ifndef INET6
48 /* dup_hostent - create hostent in one memory block */
49
50 static struct hostent *dup_hostent(hp)
51 struct hostent *hp;
52 {
53     struct hostent_block {
54         struct hostent host;
55         char   *addr_list[1];
56     };
57     struct hostent_block *hb;
58     int     count;
59     char   *data;
60     char   *addr;
61
62     for (count = 0; hp->h_addr_list[count] != 0; count++)
63          /* void */ ;
64
65     if ((hb = (struct hostent_block *) malloc(sizeof(struct hostent_block)
66                          + (hp->h_length + sizeof(char *)) * count)) == 0) {
67         fprintf(stderr, "Sorry, out of memory\n");
68         exit(1);
69     }
70     memset((char *) &hb->host, 0, sizeof(hb->host));
71     hb->host.h_length = hp->h_length;
72     hb->host.h_addr_list = hb->addr_list;
73     hb->host.h_addr_list[count] = 0;
74     data = (char *) (hb->host.h_addr_list + count + 1);
75
76     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
77         hb->host.h_addr_list[count] = data + hp->h_length * count;
78         memcpy(hb->host.h_addr_list[count], addr, hp->h_length);
79     }
80     return (&hb->host);
81 }
82 #endif
83
84 /* find_inet_addr - find all addresses for this host, result to free() */
85
86 #ifdef INET6
87 struct addrinfo *find_inet_addr(host)
88 char   *host;
89 {
90     struct addrinfo hints, *res;
91
92     memset(&hints, 0, sizeof(hints));
93     hints.ai_family = PF_UNSPEC;
94     hints.ai_socktype = SOCK_STREAM;
95     hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
96     if (getaddrinfo(host, NULL, &hints, &res) == 0)
97         return (res);
98
99     memset(&hints, 0, sizeof(hints));
100     hints.ai_family = PF_UNSPEC;
101     hints.ai_socktype = SOCK_STREAM;
102     hints.ai_flags = AI_PASSIVE | AI_CANONNAME;
103     if (getaddrinfo(host, NULL, &hints, &res) != 0) {
104         tcpd_warn("%s: host not found", host);
105         return (0);
106     }
107     if (res->ai_family != AF_INET6 && res->ai_family != AF_INET) {
108         tcpd_warn("%d: not an internet host", res->ai_family);
109         freeaddrinfo(res);
110         return (0);
111     }
112     if (!res->ai_canonname) {
113         tcpd_warn("%s: hostname alias", host);
114         tcpd_warn("(cannot obtain official name)", res->ai_canonname);
115     } else if (STR_NE(host, res->ai_canonname)) {
116         tcpd_warn("%s: hostname alias", host);
117         tcpd_warn("(official name: %.*s)", STRING_LENGTH, res->ai_canonname);
118     }
119     return (res);
120 }
121 #else
122 struct hostent *find_inet_addr(host)
123 char   *host;
124 {
125     struct in_addr addr;
126     struct hostent *hp;
127     static struct hostent h;
128     static char *addr_list[2];
129
130     /*
131      * Host address: translate it to internal form.
132      */
133     if ((addr.s_addr = dot_quad_addr(host)) != INADDR_NONE) {
134         h.h_addr_list = addr_list;
135         h.h_addr_list[0] = (char *) &addr;
136         h.h_length = sizeof(addr);
137         return (dup_hostent(&h));
138     }
139
140     /*
141      * Map host name to a series of addresses. Watch out for non-internet
142      * forms or aliases. The NOT_INADDR() is here in case gethostbyname() has
143      * been "enhanced" to accept numeric addresses. Make a copy of the
144      * address list so that later gethostbyXXX() calls will not clobber it.
145      */
146     if (NOT_INADDR(host) == 0) {
147         tcpd_warn("%s: not an internet address", host);
148         return (0);
149     }
150     if ((hp = gethostbyname(host)) == 0) {
151         tcpd_warn("%s: host not found", host);
152         return (0);
153     }
154     if (hp->h_addrtype != AF_INET) {
155         tcpd_warn("%d: not an internet host", hp->h_addrtype);
156         return (0);
157     }
158     if (STR_NE(host, hp->h_name)) {
159         tcpd_warn("%s: hostname alias", host);
160         tcpd_warn("(official name: %.*s)", STRING_LENGTH, hp->h_name);
161     }
162     return (dup_hostent(hp));
163 }
164 #endif
165
166 /* check_dns - give each address thorough workout, return address count */
167
168 int     check_dns(host)
169 char   *host;
170 {
171     struct request_info request;
172 #ifdef INET6
173     struct sockaddr_storage sin;
174     struct addrinfo *hp, *res;
175 #else
176     struct sockaddr_in sin;
177     struct hostent *hp;
178 #endif
179     int     count;
180     char   *addr;
181
182     if ((hp = find_inet_addr(host)) == 0)
183         return (0);
184     request_init(&request, RQ_CLIENT_SIN, &sin, 0);
185     sock_methods(&request);
186 #ifndef INET6
187     memset((char *) &sin, 0, sizeof(sin));
188     sin.sin_family = AF_INET;
189 #endif
190
191 #ifdef INET6
192     for (res = hp, count = 0; res; res = res->ai_next, count++) {
193         memcpy(&sin, res->ai_addr, res->ai_addrlen);
194 #else
195     for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
196         memcpy((char *) &sin.sin_addr, addr, sizeof(sin.sin_addr));
197 #endif
198
199         /*
200          * Force host name and address conversions. Use the request structure
201          * as a cache. Detect hostname lookup problems. Any name/name or
202          * name/address conflicts will be reported while eval_hostname() does
203          * its job.
204          */
205         request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
206         if (STR_EQ(eval_hostname(request.client), unknown))
207             tcpd_warn("host address %s->name lookup failed",
208                       eval_hostaddr(request.client));
209     }
210 #ifdef INET6
211     freeaddrinfo(hp);
212 #else
213     free((char *) hp);
214 #endif
215     return (count);
216 }
217
218 /* dummy function to intercept the real shell_cmd() */
219
220 /* ARGSUSED */
221
222 void    shell_cmd(command)
223 char   *command;
224 {
225     if (hosts_access_verbose)
226         printf("command: %s", command);
227 }
228
229 /* dummy function  to intercept the real clean_exit() */
230
231 /* ARGSUSED */
232
233 void    clean_exit(request)
234 struct request_info *request;
235 {
236     exit(0);
237 }
238
239 /* dummy function  to intercept the real rfc931() */
240
241 /* ARGSUSED */
242
243 void    rfc931(request)
244 struct request_info *request;
245 {
246     strcpy(request->user, unknown);
247 }
248
249 /* check_path - examine accessibility */
250
251 int     check_path(path, st)
252 char   *path;
253 struct stat *st;
254 {
255     struct stat stbuf;
256     char    buf[BUFSIZ];
257
258     if (stat(path, st) < 0)
259         return (-1);
260 #ifdef notdef
261     if (st->st_uid != 0)
262         tcpd_warn("%s: not owned by root", path);
263     if (st->st_mode & 020)
264         tcpd_warn("%s: group writable", path);
265 #endif
266     if (st->st_mode & 002)
267         tcpd_warn("%s: world writable", path);
268     if (path[0] == '/' && path[1] != 0) {
269         strrchr(strcpy(buf, path), '/')[0] = 0;
270         (void) check_path(buf[0] ? buf : "/", &stbuf);
271     }
272     return (0);
273 }