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