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