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