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