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