xref: /titanic_41/usr/src/cmd/ypcmd/rpc_bootstrap.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /*	  All Rights Reserved   */
28 
29 /*
30  * Portions of this source code were derived from Berkeley
31  * under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <sys/types.h>
38 #include <sys/file.h>
39 #include <stdlib.h>
40 #include <ctype.h>
41 #include <string.h>
42 #include <tiuser.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <sys/socket.h>
46 #include <netdir.h>
47 #include <netdb.h>
48 #include <rpc/rpc.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpcsvc/nis.h>
51 
52 CLIENT *__clnt_tp_create_bootstrap();
53 int __rpcb_getaddr_bootstrap();
54 struct hostent *__files_gethostbyname();
55 
56 extern int hostNotKnownLocally;
57 
58 static char *__map_addr();
59 
60 /*
61  * __clnt_tp_create_bootstrap()
62  *
63  * This routine is NOT TRANSPORT INDEPENDENT.
64  *
65  * It relies on the local /etc/hosts file for hostname to address
66  * translation and does it itself instead of calling netdir_getbyname
67  * thereby avoids recursion.
68  */
69 CLIENT *
70 __clnt_tp_create_bootstrap(hostname, prog, vers, nconf)
71 	char *hostname;
72 	u_long prog, vers;
73 	struct netconfig    *nconf;
74 {
75 	CLIENT *cl;
76 	struct netbuf	*svc_taddr;
77 	struct sockaddr_in6	*sa;
78 	int fd;
79 
80 	if (nconf == (struct netconfig *)NULL) {
81 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
82 		return (NULL);
83 	}
84 	if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) == -1) {
85 		rpc_createerr.cf_stat = RPC_TLIERROR;
86 		return (NULL);
87 	}
88 	svc_taddr = (struct netbuf *) malloc(sizeof (struct netbuf));
89 	if (! svc_taddr) {
90 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
91 		t_close(fd);
92 		return (NULL);
93 	}
94 	sa = (struct sockaddr_in6 *)calloc(1, sizeof (*sa));
95 	if (! sa) {
96 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
97 		t_close(fd);
98 		free(svc_taddr);
99 		return (NULL);
100 	}
101 	svc_taddr->maxlen = svc_taddr->len = sizeof (*sa);
102 	svc_taddr->buf = (char *)sa;
103 	if (__rpcb_getaddr_bootstrap(prog,
104 		vers, nconf, svc_taddr, hostname) == FALSE) {
105 		t_close(fd);
106 		free(svc_taddr);
107 		free(sa);
108 		return (NULL);
109 	}
110 	rpc_createerr.cf_stat = RPC_SUCCESS;
111 	cl = __nis_clnt_create(fd, nconf, 0, svc_taddr, 0, prog, vers, 0, 0);
112 	if (cl == 0) {
113 		if (rpc_createerr.cf_stat == RPC_SUCCESS)
114 			rpc_createerr.cf_stat = RPC_TLIERROR;
115 		t_close(fd);
116 	}
117 	free(svc_taddr);
118 	free(sa);
119 	return (cl);
120 }
121 
122 /*
123  * __rpcb_getaddr_bootstrap()
124  *
125  * This is our internal function that replaces rpcb_getaddr(). We
126  * build our own to prevent calling netdir_getbyname() which could
127  * recurse to the nameservice.
128  */
129 int
130 __rpcb_getaddr_bootstrap(program, version, nconf, address, hostname)
131 	u_long program;
132 	u_long version;
133 	struct netconfig *nconf;
134 	struct netbuf *address; /* populate with the taddr of the service */
135 	char *hostname;
136 {
137 	char *svc_uaddr;
138 	struct hostent *hent;
139 	struct sockaddr_in *sa;
140 	struct sockaddr_in6 *sa6;
141 	struct netbuf rpcb_taddr;
142 	struct sockaddr_in local_sa;
143 	struct sockaddr_in6 local_sa6;
144 	in_port_t inport;
145 	int p1, p2;
146 	char *ipaddr, *port;
147 	int i, ipaddrlen;
148 
149 	if (strcmp(nconf->nc_protofmly, NC_INET) != 0 &&
150 		strcmp(nconf->nc_protofmly, NC_INET6) != 0) {
151 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
152 		return (FALSE);
153 	}
154 
155 	/* Get the address of the RPCBIND at hostname */
156 	hent = __files_gethostbyname(hostname, nconf->nc_protofmly);
157 	if (hent == (struct hostent *)NULL) {
158 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
159 		hostNotKnownLocally = 1;
160 		return (FALSE);
161 	}
162 
163 	switch (hent->h_addrtype) {
164 	case AF_INET:
165 		local_sa.sin_family = AF_INET;
166 		local_sa.sin_port = htons(111); /* RPCBIND port */
167 		memcpy((char *)&(local_sa.sin_addr.s_addr),
168 			hent->h_addr_list[0], hent->h_length);
169 		rpcb_taddr.buf = (char *)&local_sa;
170 		rpcb_taddr.maxlen = sizeof (local_sa);
171 		rpcb_taddr.len = rpcb_taddr.maxlen;
172 		break;
173 	case AF_INET6:
174 		local_sa6.sin6_family = AF_INET6;
175 		local_sa6.sin6_port = htons(111); /* RPCBIND port */
176 		memcpy((char *)&(local_sa6.sin6_addr.s6_addr),
177 			hent->h_addr_list[0], hent->h_length);
178 		rpcb_taddr.buf = (char *)&local_sa6;
179 		rpcb_taddr.maxlen = sizeof (local_sa6);
180 		rpcb_taddr.len = rpcb_taddr.maxlen;
181 		break;
182 	default:
183 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
184 		return (FALSE);
185 	}
186 
187 	svc_uaddr = __map_addr(nconf, &rpcb_taddr, program, version);
188 	if (! svc_uaddr)
189 		return (FALSE);
190 
191 /* do a local uaddr2taddr and stuff in the memory supplied by the caller */
192 	ipaddr = svc_uaddr;
193 	ipaddrlen = strlen(ipaddr);
194 	/* Look for the first '.' starting from the end */
195 	for (i = ipaddrlen-1; i >= 0; i--)
196 		if (ipaddr[i] == '.') break;
197 	/* Find the second dot (still counting from the end) */
198 	for (i--; i >= 0; i--)
199 		if (ipaddr[i] == '.') break;
200 	/* If we didn't find it, the uaddr has a syntax error */
201 	if (i < 0) {
202 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
203 		return (FALSE);
204 	}
205 	port = &ipaddr[i+1];
206 	ipaddr[i] = '\0';
207 	sscanf(port, "%d.%d", &p1, &p2);
208 	inport = (p1 << 8) + p2;
209 	if (hent->h_addrtype == AF_INET) {
210 		sa = (struct sockaddr_in *)address->buf;
211 		address->len = sizeof (*sa);
212 		if (inet_pton(AF_INET, ipaddr, &sa->sin_addr) != 1) {
213 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
214 			return (FALSE);
215 		}
216 		sa->sin_port = htons(inport);
217 		sa->sin_family = AF_INET;
218 	} else {
219 		sa6 = (struct sockaddr_in6 *)address->buf;
220 		address->len = sizeof (*sa6);
221 		if (inet_pton(AF_INET6, ipaddr, &sa6->sin6_addr) != 1) {
222 			rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
223 			return (FALSE);
224 		}
225 		sa6->sin6_port = htons(inport);
226 		sa6->sin6_family = AF_INET6;
227 	}
228 	return (TRUE);
229 }
230 
231 /*
232  * __map_addr()
233  *
234  */
235 static char *
236 __map_addr(nc, rpcb_taddr, prog, ver)
237 	struct netconfig	*nc;		/* Our transport	*/
238 	struct netbuf	*rpcb_taddr; /* RPCBIND address */
239 	u_long			prog, ver;	/* Name service Prog/vers */
240 {
241 	register CLIENT *client;
242 	RPCB 		parms;		/* Parameters for RPC binder	  */
243 	enum clnt_stat	clnt_st;	/* Result from the rpc call	  */
244 	int		fd;		/* Stream file descriptor	  */
245 	char 		*ua = NULL;	/* Universal address of service	  */
246 	struct timeval	tv;		/* Timeout for our rpcb call	  */
247 
248 	/*
249 	 * First we open a connection to the remote rpcbind process.
250 	 */
251 	if ((fd = t_open(nc->nc_device, O_RDWR, NULL)) == -1) {
252 		rpc_createerr.cf_stat = RPC_TLIERROR;
253 		return (NULL);
254 	}
255 
256 	client = __nis_clnt_create(fd, nc, 0, rpcb_taddr, 0,
257 					RPCBPROG, RPCBVERS, 0, 0);
258 	if (! client) {
259 		t_close(fd);
260 		rpc_createerr.cf_stat = RPC_TLIERROR;
261 		return (NULL);
262 	}
263 
264 	/*
265 	 * Now make the call to get the NIS service address.
266 	 */
267 	tv.tv_sec = 10;
268 	tv.tv_usec = 0;
269 	parms.r_prog = prog;
270 	parms.r_vers = ver;
271 	parms.r_netid = nc->nc_netid;	/* not needed */
272 	parms.r_addr = "";	/* not needed; just for xdring */
273 	parms.r_owner = "";	/* not needed; just for xdring */
274 	clnt_st = clnt_call(client, RPCBPROC_GETADDR, xdr_rpcb, (char *)&parms,
275 					    xdr_wrapstring, (char *)&ua, tv);
276 
277 	rpc_createerr.cf_stat = clnt_st;
278 	if (clnt_st == RPC_SUCCESS) {
279 
280 		clnt_destroy(client);
281 		t_close(fd);
282 		if (*ua == '\0') {
283 			xdr_free(xdr_wrapstring, (char *)&ua);
284 			return (NULL);
285 		}
286 		return (ua);
287 	} else if (((clnt_st == RPC_PROGVERSMISMATCH) ||
288 			(clnt_st == RPC_PROGUNAVAIL) ||
289 			(clnt_st == RPC_TIMEDOUT)) &&
290 			(strcmp(nc->nc_protofmly, NC_INET) == 0)) {
291 		/*
292 		 * version 3 not available. Try version 2
293 		 * The assumption here is that the netbuf
294 		 * is arranged in the sockaddr_in
295 		 * style for IP cases.
296 		 */
297 		u_short	port;
298 		struct sockaddr_in	*sa;
299 		struct netbuf 		remote;
300 		int		protocol;
301 		char	buf[32];
302 		char	*res;
303 
304 		clnt_control(client, CLGET_SVC_ADDR, (char *) &remote);
305 		sa = (struct sockaddr_in *)(remote.buf);
306 		protocol = strcmp(nc->nc_proto, NC_TCP) ?
307 				IPPROTO_UDP : IPPROTO_TCP;
308 		port = (u_short) pmap_getport(sa, prog, ver, protocol);
309 
310 		if (port != 0) {
311 			/* print s_addr (and port) in host byte order */
312 			sa->sin_addr.s_addr = ntohl(sa->sin_addr.s_addr);
313 			sprintf(buf, "%d.%d.%d.%d.%d.%d",
314 				(sa->sin_addr.s_addr >> 24) & 0xff,
315 				(sa->sin_addr.s_addr >> 16) & 0xff,
316 				(sa->sin_addr.s_addr >>  8) & 0xff,
317 				(sa->sin_addr.s_addr) & 0xff,
318 				(port >> 8) & 0xff,
319 				port & 0xff);
320 			res = strdup(buf);
321 			if (res != 0) {
322 				rpc_createerr.cf_stat = RPC_SUCCESS;
323 			} else {
324 				rpc_createerr.cf_stat = RPC_SYSTEMERROR;
325 			}
326 		} else {
327 			rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
328 			res = NULL;
329 		}
330 		clnt_destroy(client);
331 		t_close(fd);
332 		return (res);
333 	}
334 	clnt_destroy(client);
335 	t_close(fd);
336 	return (NULL);
337 }
338 
339 #define	bcmp(s1, s2, len)	memcmp(s1, s2, len)
340 #define	bcopy(s1, s2, len)	memcpy(s2, s1, len)
341 
342 #define	MAXALIASES	35
343 
344 static char line[BUFSIZ+1];
345 static char hostaddr[sizeof (struct in6_addr)];
346 static struct hostent host;
347 static char *host_aliases[MAXALIASES];
348 static char *host_addrs[] = {
349 	hostaddr,
350 	NULL
351 };
352 
353 static char *_hosts6[] = { "/etc/inet/ipnodes", 0 };
354 static char *_hosts4[] = { "/etc/inet/ipnodes", "/etc/hosts", 0 };
355 
356 static char *any();
357 
358 static struct hostent *__files_gethostent();
359 
360 struct hostent *
361 __files_gethostbyname(char *nam, char *fmly)
362 {
363 	register struct hostent *hp;
364 	register char **cp;
365 	char **file;
366 	FILE *hostf;
367 	sa_family_t af;
368 
369 	if (strcmp(fmly, NC_INET) == 0) {
370 		af = AF_INET;
371 		file = _hosts4;
372 	} else if (strcmp(fmly, NC_INET6) == 0) {
373 		af = AF_INET6;
374 		file = _hosts6;
375 	} else {
376 		return (0);
377 	}
378 
379 	for (; *file != 0; file++) {
380 
381 		if ((hostf = fopen(*file, "r")) == 0)
382 			continue;
383 
384 		while (hp = __files_gethostent(hostf)) {
385 			if (hp->h_addrtype != af)
386 				continue;
387 			if (strcasecmp(hp->h_name, nam) == 0) {
388 				(void) fclose(hostf);
389 				return (hp);
390 			}
391 			for (cp = hp->h_aliases; cp != 0 && *cp != 0; cp++)
392 				if (strcasecmp(*cp, nam) == 0) {
393 					(void) fclose(hostf);
394 					return (hp);
395 				}
396 		}
397 
398 		(void) fclose(hostf);
399 	}
400 
401 	return (0);
402 }
403 
404 #define	isV6Addr(s)	(strchr(s, (int)':') != 0)
405 
406 static struct hostent *
407 __files_gethostent(FILE *hostf)
408 {
409 	char *p;
410 	register char *cp, **q;
411 	struct in6_addr in6;
412 	struct in_addr in4;
413 	void *addr;
414 	sa_family_t af;
415 	int len;
416 
417 	if (hostf == NULL)
418 		return (NULL);
419 again:
420 	if ((p = fgets(line, BUFSIZ, hostf)) == NULL)
421 		return (NULL);
422 	if (*p == '#')
423 		goto again;
424 	cp = any(p, "#\n");
425 	if (cp == NULL)
426 		goto again;
427 	*cp = '\0';
428 	cp = any(p, " \t");
429 	if (cp == NULL)
430 		goto again;
431 	*cp++ = '\0';
432 	/* THIS STUFF IS INTERNET SPECIFIC */
433 	host.h_addr_list = host_addrs;
434 	if (isV6Addr(p)) {
435 		af = AF_INET6;
436 		addr = (void *)&in6;
437 		len = sizeof (in6);
438 	} else {
439 		af = AF_INET;
440 		addr = (void *)&in4;
441 		len = sizeof (in4);
442 	}
443 	if (inet_pton(af, p, addr) != 1)
444 		goto again;
445 	bcopy(addr, host.h_addr_list[0], len);
446 	host.h_length = len;
447 	host.h_addrtype = af;
448 	while (*cp == ' ' || *cp == '\t')
449 		cp++;
450 	host.h_name = cp;
451 	q = host.h_aliases = host_aliases;
452 	cp = any(cp, " \t");
453 	if (cp != NULL)
454 		*cp++ = '\0';
455 	while (cp && *cp) {
456 		if (*cp == ' ' || *cp == '\t') {
457 			cp++;
458 			continue;
459 		}
460 		if (q < &host_aliases[MAXALIASES - 1])
461 			*q++ = cp;
462 		cp = any(cp, " \t");
463 		if (cp != NULL)
464 			*cp++ = '\0';
465 	}
466 	*q = NULL;
467 	return (&host);
468 }
469 
470 static char *
471 any(cp, match)
472 	register char *cp;
473 	char *match;
474 {
475 	register char *mp, c;
476 
477 	while (c = *cp) {
478 		for (mp = match; *mp; mp++)
479 			if (*mp == c)
480 				return (cp);
481 		cp++;
482 	}
483 	return ((char *)0);
484 }
485