libuutil: move under bmake
[unleashed.git] / usr / src / cmd / tcpd / scaffold.c
blob7fb01375f96935c0cc0dc6f00cbb94da2066e0d1
1 /*
2 * Routines for testing only. Not really industrial strength.
3 *
4 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
5 */
7 static char sccs_id[] = "@(#) scaffold.c 1.6 97/03/21 19:27:24";
9 /* System libraries. */
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/socket.h>
14 #include <netinet/in.h>
15 #include <arpa/inet.h>
16 #include <netdb.h>
17 #include <stdio.h>
18 #include <syslog.h>
19 #include <setjmp.h>
20 #include <string.h>
22 #ifndef INADDR_NONE
23 #define INADDR_NONE (-1) /* XXX should be 0xffffffff */
24 #endif
26 extern char *malloc();
28 /* Application-specific. */
30 #include "tcpd.h"
31 #include "scaffold.h"
34 * These are referenced by the options module and by rfc931.c.
36 int allow_severity = SEVERITY;
37 int deny_severity = LOG_WARNING;
38 int rfc931_timeout = RFC931_TIMEOUT;
40 /* dup_hostent - create hostent in one memory block */
42 static struct hostent *dup_hostent(hp)
43 struct hostent *hp;
45 struct hostent_block {
46 struct hostent host;
47 char *addr_list[1];
49 struct hostent_block *hb;
50 int count;
51 char *data;
52 char *addr;
54 for (count = 0; hp->h_addr_list[count] != 0; count++)
55 /* void */ ;
57 if ((hb = (struct hostent_block *) malloc(sizeof(struct hostent_block)
58 + (hp->h_length + sizeof(char *)) * count)) == 0) {
59 fprintf(stderr, "Sorry, out of memory\n");
60 exit(1);
62 memset((char *) &hb->host, 0, sizeof(hb->host));
63 hb->host.h_addrtype = hp->h_addrtype;;
64 hb->host.h_length = hp->h_length;
65 hb->host.h_addr_list = hb->addr_list;
66 hb->host.h_addr_list[count] = 0;
67 data = (char *) (hb->host.h_addr_list + count + 1);
69 for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
70 hb->host.h_addr_list[count] = data + hp->h_length * count;
71 memcpy(hb->host.h_addr_list[count], addr, hp->h_length);
73 return (&hb->host);
76 /* find_inet_addr - find all addresses for this host, result to free() */
78 struct hostent *find_inet_addr(host)
79 char *host;
81 union gen_addr addr;
82 struct hostent *hp;
83 static struct hostent h;
84 static char *addr_list[2];
87 * Host address: translate it to internal form.
89 if (numeric_addr(host, &addr, &h.h_addrtype, &h.h_length) != -1) {
90 h.h_addr_list = addr_list;
91 h.h_addr_list[0] = (char *) &addr;
92 return (dup_hostent(&h));
96 * Map host name to a series of addresses. Watch out for non-internet
97 * forms or aliases. The NOT_INADDR() is here in case gethostbyname() has
98 * been "enhanced" to accept numeric addresses. Make a copy of the
99 * address list so that later gethostbyXXX() calls will not clobber it.
101 if (NOT_INADDR(host) == 0) {
102 tcpd_warn("%s: not an internet address", host);
103 return (0);
105 if ((hp = tcpd_gethostbyname(host, 0)) == 0) {
106 tcpd_warn("%s: host not found", host);
107 return (0);
109 if (!VALID_ADDRTYPE(hp->h_addrtype)) {
110 tcpd_warn("%d: not an internet host", hp->h_addrtype);
111 return (0);
113 if (STR_NE(host, hp->h_name)) {
114 tcpd_warn("%s: hostname alias", host);
115 tcpd_warn("(official name: %.*s)", STRING_LENGTH, hp->h_name);
117 return (dup_hostent(hp));
120 /* check_dns - give each address thorough workout, return address count */
122 int check_dns(host)
123 char *host;
125 struct request_info request;
126 struct sockaddr_gen sin;
127 struct hostent *hp;
128 int count;
129 char *addr;
131 if ((hp = find_inet_addr(host)) == 0)
132 return (0);
133 request_init(&request, RQ_CLIENT_SIN, &sin, 0);
134 sock_methods(&request);
135 memset((char *) &sin, 0, sizeof(sin));
136 sin.sg_family = hp->h_addrtype;
138 for (count = 0; (addr = hp->h_addr_list[count]) != 0; count++) {
139 memcpy((char *) SGADDRP(&sin), addr, SGADDRSZ(&sin));
142 * Force host name and address conversions. Use the request structure
143 * as a cache. Detect hostname lookup problems. Any name/name or
144 * name/address conflicts will be reported while eval_hostname() does
145 * its job.
147 request_set(&request, RQ_CLIENT_ADDR, "", RQ_CLIENT_NAME, "", 0);
148 if (STR_EQ(eval_hostname(request.client), unknown))
149 tcpd_warn("host address %s->name lookup failed",
150 eval_hostaddr(request.client));
152 free((char *) hp);
153 return (count);
156 /* dummy function to intercept the real shell_cmd() */
158 /* ARGSUSED */
160 void shell_cmd(command)
161 char *command;
163 if (hosts_access_verbose)
164 printf("command: %s", command);
167 /* dummy function to intercept the real clean_exit() */
169 /* ARGSUSED */
171 void clean_exit(request)
172 struct request_info *request;
174 exit(0);
177 /* dummy function to intercept the real rfc931() */
179 /* ARGSUSED */
181 void rfc931(request)
182 struct request_info *request;
184 strcpy(request->user, unknown);
187 /* check_path - examine accessibility */
189 int check_path(path, st)
190 char *path;
191 struct stat *st;
193 struct stat stbuf;
194 char buf[BUFSIZ];
196 if (stat(path, st) < 0)
197 return (-1);
198 #ifdef notdef
199 if (st->st_uid != 0)
200 tcpd_warn("%s: not owned by root", path);
201 if (st->st_mode & 020)
202 tcpd_warn("%s: group writable", path);
203 #endif
204 if (st->st_mode & 002)
205 tcpd_warn("%s: world writable", path);
206 if (path[0] == '/' && path[1] != 0) {
207 strrchr(strcpy(buf, path), '/')[0] = 0;
208 (void) check_path(buf[0] ? buf : "/", &stbuf);
210 return (0);