xref: /illumos-gate/usr/src/cmd/rpcsvc/rpc.bootparamd/bootparam_subr.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1999-2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Subroutines that implement the bootparam services.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <rpcsvc/bootparam_prot.h>
337c478bd9Sstevel@tonic-gate #include <netdb.h>
347c478bd9Sstevel@tonic-gate #include <nlist.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <nsswitch.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/file.h>
427c478bd9Sstevel@tonic-gate #include <sys/socket.h>
437c478bd9Sstevel@tonic-gate #define	KERNEL		/* to get RTHASHSIZ */
447c478bd9Sstevel@tonic-gate #include <sys/stream.h>
457c478bd9Sstevel@tonic-gate #include <net/route.h>
467c478bd9Sstevel@tonic-gate #undef	KERNEL
477c478bd9Sstevel@tonic-gate #include <net/if.h>			/* for structs ifnet and ifaddr */
487c478bd9Sstevel@tonic-gate #include <netinet/in.h>
497c478bd9Sstevel@tonic-gate #include <netinet/in_var.h>		/* for struct in_ifaddr */
507c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include "bootparam_private.h"
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #define	LINESIZE	1024
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern int getdomainname(char *, int);
577c478bd9Sstevel@tonic-gate extern int bootparams_getbyname(char *, char *, int);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static char *wildcard = "*";
607c478bd9Sstevel@tonic-gate static char domainkey[] = "domain=";
617c478bd9Sstevel@tonic-gate static void getf_printres(bp_getfile_res *);
627c478bd9Sstevel@tonic-gate static void copydomain(char *, char *, int);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Whoami turns a client address into a client name
667c478bd9Sstevel@tonic-gate  * and suggested route machine.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
697c478bd9Sstevel@tonic-gate bp_whoami_res *
bootparamproc_whoami_1(bp_whoami_arg * argp,CLIENT * cl)707c478bd9Sstevel@tonic-gate bootparamproc_whoami_1(bp_whoami_arg *argp, CLIENT *cl)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	static bp_whoami_res res;
737c478bd9Sstevel@tonic-gate 	struct in_addr clnt_addr;
747c478bd9Sstevel@tonic-gate 	struct in_addr route_addr;
757c478bd9Sstevel@tonic-gate 	struct hostent *hp;
767c478bd9Sstevel@tonic-gate 	static char clnt_entry[LINESIZE];
777c478bd9Sstevel@tonic-gate 	static char domain[MAX_MACHINE_NAME];
787c478bd9Sstevel@tonic-gate 	char *cp;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (argp->client_address.address_type != IP_ADDR_TYPE) {
817c478bd9Sstevel@tonic-gate 		if (debug) {
827c478bd9Sstevel@tonic-gate 			msgout("Whoami failed: unknown address type %d",
837c478bd9Sstevel@tonic-gate 				argp->client_address.address_type);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		return (NULL);
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	(void) memcpy(&clnt_addr, &argp->client_address.bp_address_u.ip_addr,
887c478bd9Sstevel@tonic-gate 	    sizeof (clnt_addr));
897c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr((char *)&clnt_addr, sizeof (clnt_addr), AF_INET);
907c478bd9Sstevel@tonic-gate 	if (hp == NULL) {
917c478bd9Sstevel@tonic-gate 		if (debug) {
927c478bd9Sstevel@tonic-gate 			msgout("Whoami failed: gethostbyaddr for %s.",
937c478bd9Sstevel@tonic-gate 				inet_ntoa(clnt_addr));
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 		return (NULL);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/*
997c478bd9Sstevel@tonic-gate 	 * We only answer requests from clients listed in the database.
1007c478bd9Sstevel@tonic-gate 	 */
1017c478bd9Sstevel@tonic-gate 	if ((bootparams_getbyname(hp->h_name, clnt_entry,
1027c478bd9Sstevel@tonic-gate 	    sizeof (clnt_entry)) != __NSW_SUCCESS) &&
1037c478bd9Sstevel@tonic-gate 	    (bootparams_getbyname(wildcard, clnt_entry,
1047c478bd9Sstevel@tonic-gate 	    sizeof (clnt_entry)) != __NSW_SUCCESS))
1057c478bd9Sstevel@tonic-gate 		return (NULL);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	res.client_name = hp->h_name;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/*
1107c478bd9Sstevel@tonic-gate 	 * The algorithm for determining the client's domain name is:
1117c478bd9Sstevel@tonic-gate 	 * 	1) look for "domain=" in the client's bootparams line.
1127c478bd9Sstevel@tonic-gate 	 *	   If found, use its value.
1137c478bd9Sstevel@tonic-gate 	 *	2) look for a "domain=" entry in the wildcard bootparams
1147c478bd9Sstevel@tonic-gate 	 *	   line (if any).  If found, use its value.  Otherwise,
1157c478bd9Sstevel@tonic-gate 	 * 	3) return the domain name of the server answering the
1167c478bd9Sstevel@tonic-gate 	 *	   request.
1177c478bd9Sstevel@tonic-gate 	 */
1187c478bd9Sstevel@tonic-gate 	if (cp = strstr(clnt_entry, domainkey)) {
1197c478bd9Sstevel@tonic-gate 		copydomain(cp + sizeof (domainkey) - 1, domain,
1207c478bd9Sstevel@tonic-gate 		    sizeof (domain));
1217c478bd9Sstevel@tonic-gate 	} else {
1227c478bd9Sstevel@tonic-gate 		/* "domain=" not found - try for wildcard */
1237c478bd9Sstevel@tonic-gate 		if ((bootparams_getbyname(wildcard, clnt_entry,
1247c478bd9Sstevel@tonic-gate 			sizeof (clnt_entry)) == __NSW_SUCCESS) &&
1257c478bd9Sstevel@tonic-gate 			(cp = strstr(clnt_entry, domainkey))) {
1267c478bd9Sstevel@tonic-gate 			copydomain(cp + sizeof (domainkey) - 1, domain,
1277c478bd9Sstevel@tonic-gate 			    sizeof (domain));
1287c478bd9Sstevel@tonic-gate 		} else {
1297c478bd9Sstevel@tonic-gate 			(void) getdomainname(domain, sizeof (domain));
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	res.domain_name = domain;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	res.router_address.address_type = IP_ADDR_TYPE;
1357c478bd9Sstevel@tonic-gate 	route_addr.s_addr = get_ip_route(clnt_addr);
1367c478bd9Sstevel@tonic-gate 	(void) memcpy(&res.router_address.bp_address_u.ip_addr,
1377c478bd9Sstevel@tonic-gate 	    &route_addr,
1387c478bd9Sstevel@tonic-gate 	    sizeof (res.router_address.bp_address_u.ip_addr));
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if (debug) {
1417c478bd9Sstevel@tonic-gate 		struct in_addr	in;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		(void) memcpy(&in.s_addr,
1447c478bd9Sstevel@tonic-gate 		    &res.router_address.bp_address_u.ip_addr,
1457c478bd9Sstevel@tonic-gate 		    sizeof (in.s_addr));
1467c478bd9Sstevel@tonic-gate 		msgout("Whoami returning name = %s, router address = %s",
1477c478bd9Sstevel@tonic-gate 		    res.client_name,
1487c478bd9Sstevel@tonic-gate 		    inet_ntoa(in));
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	return (&res);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Getfile gets the client name and the key and returns its server
1557c478bd9Sstevel@tonic-gate  * and the pathname for that key.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
1587c478bd9Sstevel@tonic-gate bp_getfile_res *
bootparamproc_getfile_1(bp_getfile_arg * argp,CLIENT * cl)1597c478bd9Sstevel@tonic-gate bootparamproc_getfile_1(bp_getfile_arg *argp, CLIENT *cl)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	static bp_getfile_res res;
1627c478bd9Sstevel@tonic-gate 	static char clnt_entry[LINESIZE];
1637c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1647c478bd9Sstevel@tonic-gate 	char *cp;
1657c478bd9Sstevel@tonic-gate 	char filekey[LINESIZE];
1667c478bd9Sstevel@tonic-gate 	char *server_hostname;
1677c478bd9Sstevel@tonic-gate 	char *path_on_server;
1687c478bd9Sstevel@tonic-gate 	int do_wildcard = 0;
1697c478bd9Sstevel@tonic-gate 	static char *zero_len_string = "";
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	/*
1727c478bd9Sstevel@tonic-gate 	 * The bootparams_getbyname() library function looks up a
1737c478bd9Sstevel@tonic-gate 	 * "client entry" using using the client's hostname as the
1747c478bd9Sstevel@tonic-gate 	 * key.  A client entry consists of a string of "file entries"
1757c478bd9Sstevel@tonic-gate 	 * separated by white space.  Each file entry is of the form:
1767c478bd9Sstevel@tonic-gate 	 *
1777c478bd9Sstevel@tonic-gate 	 *	file_key=server_hostname:path_on_server
1787c478bd9Sstevel@tonic-gate 	 *
179*48bbca81SDaniel Hoffman 	 * In the getfile RPC call, the client gives us its hostname
180*48bbca81SDaniel Hoffman 	 * and a file_key.  We lookup its client entry, then locate a
1817c478bd9Sstevel@tonic-gate 	 * file entry matching that file_key.  We then parse out the
1827c478bd9Sstevel@tonic-gate 	 * server_hostname and path_on_server from the file entry, map
1837c478bd9Sstevel@tonic-gate 	 * the server_hostname to an IP address, and return both the
1847c478bd9Sstevel@tonic-gate 	 * IP address and path_on_server back to the client.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/* make the client's file key int a string we can use for matching */
1887c478bd9Sstevel@tonic-gate 	(void) strncpy(filekey, argp->file_id, sizeof (filekey) - 2);
1897c478bd9Sstevel@tonic-gate 	filekey[sizeof (filekey) - 2] = '\0';
1907c478bd9Sstevel@tonic-gate 	(void) strcat(filekey, "=");
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (bootparams_getbyname(argp->client_name, clnt_entry,
1937c478bd9Sstevel@tonic-gate 	    sizeof (clnt_entry)) == __NSW_SUCCESS) {
1947c478bd9Sstevel@tonic-gate 		/* locate the file_key in the client's entry */
1957c478bd9Sstevel@tonic-gate 		cp = strstr(clnt_entry, filekey);
1967c478bd9Sstevel@tonic-gate 		if (cp == NULL)
1977c478bd9Sstevel@tonic-gate 			do_wildcard++;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	} else
2007c478bd9Sstevel@tonic-gate 		do_wildcard++;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (do_wildcard) {
2037c478bd9Sstevel@tonic-gate 		if (bootparams_getbyname(wildcard, clnt_entry,
2047c478bd9Sstevel@tonic-gate 		    sizeof (clnt_entry)) != __NSW_SUCCESS)
2057c478bd9Sstevel@tonic-gate 			return (NULL);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		/* locate the file_key in the client's entry */
2087c478bd9Sstevel@tonic-gate 		cp = strstr(clnt_entry, filekey);
2097c478bd9Sstevel@tonic-gate 		if (cp == NULL)
2107c478bd9Sstevel@tonic-gate 			return (NULL);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/* locate the "data" part of file entry (r.h.s. of "=") */
2147c478bd9Sstevel@tonic-gate 	cp = strchr(cp, '=');
2157c478bd9Sstevel@tonic-gate 	if (cp == NULL)
2167c478bd9Sstevel@tonic-gate 		return (NULL);
2177c478bd9Sstevel@tonic-gate 	cp++;
2187c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
2197c478bd9Sstevel@tonic-gate 		return (NULL);
2207c478bd9Sstevel@tonic-gate 	server_hostname = cp;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* null-terminate server_hostname and parse path_on_server */
2237c478bd9Sstevel@tonic-gate 	cp = strchr(server_hostname, ':');
2247c478bd9Sstevel@tonic-gate 	if (cp == NULL)
2257c478bd9Sstevel@tonic-gate 		return (NULL);
2267c478bd9Sstevel@tonic-gate 	*cp = '\0';
2277c478bd9Sstevel@tonic-gate 	cp++;
2287c478bd9Sstevel@tonic-gate 	/* strtok() will null-terminate path_on_server */
2297c478bd9Sstevel@tonic-gate 	path_on_server = strtok(cp, "\t\n ");
2307c478bd9Sstevel@tonic-gate 	if (path_on_server == NULL)
2317c478bd9Sstevel@tonic-gate 		path_on_server = zero_len_string;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	res.server_name = server_hostname;
2347c478bd9Sstevel@tonic-gate 	res.server_path = path_on_server;
2357c478bd9Sstevel@tonic-gate 	if (*res.server_name == 0) {
2367c478bd9Sstevel@tonic-gate 		res.server_address.address_type = IP_ADDR_TYPE;
2377c478bd9Sstevel@tonic-gate 		(void) memset(&res.server_address.bp_address_u.ip_addr, 0,
2387c478bd9Sstevel@tonic-gate 		    sizeof (res.server_address.bp_address_u.ip_addr));
2397c478bd9Sstevel@tonic-gate 	} else {
2407c478bd9Sstevel@tonic-gate 		in_addr_t addr;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		if ((hp = gethostbyname(server_hostname)) != NULL) {
2437c478bd9Sstevel@tonic-gate 			addr = find_best_server_int(hp->h_addr_list,
2447c478bd9Sstevel@tonic-gate 			    argp->client_name);
2457c478bd9Sstevel@tonic-gate 		} else {
2467c478bd9Sstevel@tonic-gate 			addr = inet_addr(server_hostname);
2477c478bd9Sstevel@tonic-gate 			if (addr == INADDR_BROADCAST) {
2487c478bd9Sstevel@tonic-gate 				if (debug) {
2497c478bd9Sstevel@tonic-gate 					msgout("getfile_1: gethostbyname(%s) "
2507c478bd9Sstevel@tonic-gate 					    "failed", res.server_name);
2517c478bd9Sstevel@tonic-gate 				}
2527c478bd9Sstevel@tonic-gate 				return (NULL);
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 		res.server_address.address_type = IP_ADDR_TYPE;
2567c478bd9Sstevel@tonic-gate 		(void) memcpy(&res.server_address.bp_address_u.ip_addr,
2577c478bd9Sstevel@tonic-gate 		    &addr, sizeof (res.server_address.bp_address_u.ip_addr));
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	if (debug) {
2607c478bd9Sstevel@tonic-gate 		getf_printres(&res);
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	return (&res);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate void
getf_printres(bp_getfile_res * res)2667c478bd9Sstevel@tonic-gate getf_printres(bp_getfile_res *res)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	struct in_addr	in;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	(void) memcpy(&in.s_addr, &res->server_address.bp_address_u.ip_addr,
2717c478bd9Sstevel@tonic-gate 	    sizeof (in.s_addr));
2727c478bd9Sstevel@tonic-gate 	msgout("getfile_1: file is \"%s\" %s \"%s\"",
2737c478bd9Sstevel@tonic-gate 	    res->server_name,
2747c478bd9Sstevel@tonic-gate 	    inet_ntoa(in),
2757c478bd9Sstevel@tonic-gate 	    res->server_path);
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Used when we've found a "domain=" key, this function copies characters
2807c478bd9Sstevel@tonic-gate  * from source to target until we come upon either a NULL or whitespace is
2817c478bd9Sstevel@tonic-gate  * found in the source string, or we run out of room in the target.
2827c478bd9Sstevel@tonic-gate  *
2837c478bd9Sstevel@tonic-gate  */
2847c478bd9Sstevel@tonic-gate void
copydomain(char * source,char * target,int len)2857c478bd9Sstevel@tonic-gate copydomain(char *source, char *target, int len)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int n;			/* number of characters copies */;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	len--;			/* leave room for terminating '\0' */
2907c478bd9Sstevel@tonic-gate 	if (source)
2917c478bd9Sstevel@tonic-gate 		for (n = 0; *source != '\0' && n < len; n++)
2927c478bd9Sstevel@tonic-gate 			if (isspace((int)*source))
2937c478bd9Sstevel@tonic-gate 				break;
2947c478bd9Sstevel@tonic-gate 			else
2957c478bd9Sstevel@tonic-gate 				*target++ = *source++;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	*target = '\0';
2987c478bd9Sstevel@tonic-gate }
299