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