xref: /titanic_53/usr/src/lib/nsswitch/ldap/common/gethostent6.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <netdb.h>
29*cb5caa98Sdjl #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
31*cb5caa98Sdjl #include <netinet/in.h>
32*cb5caa98Sdjl #include <arpa/inet.h>
337c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
347c478bd9Sstevel@tonic-gate #include "ns_internal.h"
357c478bd9Sstevel@tonic-gate #include "ldap_common.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /* host attributes filters */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* probably some change in the ipHostNumber field */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define	_H_DN			"dn"
427c478bd9Sstevel@tonic-gate #define	_H_NAME			"cn"
437c478bd9Sstevel@tonic-gate #define	_H_ADDR			"iphostnumber"
447c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6BYNAME	"(&(objectClass=ipHost)(cn=%s))"
457c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6BYNAME_SSD	"(&(%%s)(cn=%s))"
467c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6DOTTEDBYNAME \
477c478bd9Sstevel@tonic-gate 				"(&(objectClass=ipHost)(|(cn=%s)(cn=%s)))"
487c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6DOTTEDBYNAME_SSD \
497c478bd9Sstevel@tonic-gate 				"(&(%%s)(|(cn=%s)(cn=%s)))"
507c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6BYADDR	"(&(objectClass=ipHost)(ipHostNumber=%s))"
517c478bd9Sstevel@tonic-gate #define	_F_GETHOSTS6BYADDR_SSD	"(&(%%s)(ipHostNumber=%s))"
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static const char *ipnodes_attrs[] = {
547c478bd9Sstevel@tonic-gate 	_H_NAME,
557c478bd9Sstevel@tonic-gate 	_H_ADDR,
567c478bd9Sstevel@tonic-gate 	(char *)NULL
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
59*cb5caa98Sdjl extern int
60*cb5caa98Sdjl _nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
63*cb5caa98Sdjl  * _nss_ldap_hosts2str is the data marshaling method for the ipnodes getXbyY
64*cb5caa98Sdjl  * system call gethostbyname() and gethostbyaddr.
65*cb5caa98Sdjl  * This method is called after a successful search has been performed.
66*cb5caa98Sdjl  * This method will parse the search results into the file format.
67*cb5caa98Sdjl  * e.g.
68*cb5caa98Sdjl  *
69*cb5caa98Sdjl  * fe80::a00:20ff:fec4:f2b6 ipnodes_1
70*cb5caa98Sdjl  *
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate static int
_nss_ldap_hosts2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)73*cb5caa98Sdjl _nss_ldap_hosts2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) {
74*cb5caa98Sdjl 	return (_nss_ldap_hosts2str_int(AF_INET6, be, argp));
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * getbyname gets a struct hostent by hostname. This function constructs
797c478bd9Sstevel@tonic-gate  * an ldap search filter using the name invocation parameter and the
807c478bd9Sstevel@tonic-gate  * gethostbyname search filter defined. Once the filter is constructed,
817c478bd9Sstevel@tonic-gate  * we search for a matching entry and marshal the data results into
827c478bd9Sstevel@tonic-gate  * struct hostent for the frontend process.  Host name searches will be
837c478bd9Sstevel@tonic-gate  * on fully qualified host names (foo.bar.sun.com)
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)877c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
907c478bd9Sstevel@tonic-gate 	char		realdomain[BUFSIZ];
917c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
927c478bd9Sstevel@tonic-gate 	nss_status_t	lstat;
937c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
947c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
957c478bd9Sstevel@tonic-gate 	int		rc;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.ipnode.name,
987c478bd9Sstevel@tonic-gate 			sizeof (hostname)) != 0)
997c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	rc = snprintf(searchfilter, sizeof (searchfilter),
1027c478bd9Sstevel@tonic-gate 	    _F_GETHOSTS6BYNAME, hostname);
1037c478bd9Sstevel@tonic-gate 	if (rc >= sizeof (searchfilter) || rc < 0)
1047c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1057c478bd9Sstevel@tonic-gate 	rc = snprintf(userdata, sizeof (userdata),
1067c478bd9Sstevel@tonic-gate 	    _F_GETHOSTS6BYNAME_SSD, hostname);
1077c478bd9Sstevel@tonic-gate 	if (rc >= sizeof (userdata) || rc < 0)
1087c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* get the domain we are in */
1117c478bd9Sstevel@tonic-gate 	rc = sysinfo(SI_SRPC_DOMAIN, realdomain, BUFSIZ);
1127c478bd9Sstevel@tonic-gate 	if (rc <= 0)
1137c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* Is this a request for a host.domain */
1167c478bd9Sstevel@tonic-gate 	if (DOTTEDSUBDOMAIN(hostname)) {
1177c478bd9Sstevel@tonic-gate 		char	host[MAXHOSTNAMELEN];
1187c478bd9Sstevel@tonic-gate 		char	domain[MAXHOSTNAMELEN];
1197c478bd9Sstevel@tonic-gate 		char	hname[3 * MAXHOSTNAMELEN];
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/* separate host and domain.  this function */
1227c478bd9Sstevel@tonic-gate 		/* will munge hname, so use argp->keyname */
1237c478bd9Sstevel@tonic-gate 		/* from here on for original string */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		(void) strcpy(hname, hostname);
1267c478bd9Sstevel@tonic-gate 		if (chophostdomain(hname, host, domain) == -1) {
1277c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 		/* if domain is a proper subset of realdomain */
1317c478bd9Sstevel@tonic-gate 		/* ie. domain = "foo" and realdomain */
1327c478bd9Sstevel@tonic-gate 		/* = "foor.bar.sun.com", we try to lookup both" */
1337c478bd9Sstevel@tonic-gate 		/* host.domain and host */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		if (propersubdomain(realdomain, domain) == 1) {
1367c478bd9Sstevel@tonic-gate 			/* yes, it is a proper domain */
1377c478bd9Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
1387c478bd9Sstevel@tonic-gate 			    _F_GETHOSTS6DOTTEDBYNAME, hostname, host);
1397c478bd9Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
1407c478bd9Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
1437c478bd9Sstevel@tonic-gate 			    _F_GETHOSTS6DOTTEDBYNAME_SSD, hostname, host);
1447c478bd9Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
1457c478bd9Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1467c478bd9Sstevel@tonic-gate 		} else {
1477c478bd9Sstevel@tonic-gate 			/* it is not a proper domain, so only try to look up */
1487c478bd9Sstevel@tonic-gate 			/* host.domain */
1497c478bd9Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
1507c478bd9Sstevel@tonic-gate 			    _F_GETHOSTS6BYNAME, hostname);
1517c478bd9Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
1527c478bd9Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
1557c478bd9Sstevel@tonic-gate 			    _F_GETHOSTS6BYNAME_SSD, hostname);
1567c478bd9Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
1577c478bd9Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	} else {
1607c478bd9Sstevel@tonic-gate 		rc = snprintf(searchfilter, sizeof (searchfilter),
1617c478bd9Sstevel@tonic-gate 		    _F_GETHOSTS6BYNAME, hostname);
1627c478bd9Sstevel@tonic-gate 		if (rc >= sizeof (searchfilter) || rc < 0)
1637c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 		rc = snprintf(userdata, sizeof (userdata),
1667c478bd9Sstevel@tonic-gate 		    _F_GETHOSTS6BYNAME_SSD, hostname);
1677c478bd9Sstevel@tonic-gate 		if (rc >= sizeof (userdata) || rc < 0)
1687c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS,
1717c478bd9Sstevel@tonic-gate 		searchfilter, NULL,
1727c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata);
1737c478bd9Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
1747c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
1777c478bd9Sstevel@tonic-gate 	return ((nss_status_t)lstat);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * getbyaddr gets a struct hostent by host address. This function
1837c478bd9Sstevel@tonic-gate  * constructs an ldap search filter using the host address invocation
1847c478bd9Sstevel@tonic-gate  * parameter and the gethostbyaddr search filter defined. Once the
1857c478bd9Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
1867c478bd9Sstevel@tonic-gate  * the data results into struct hostent for the frontend process.
1877c478bd9Sstevel@tonic-gate  */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)1907c478bd9Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1937c478bd9Sstevel@tonic-gate 	struct in6_addr	addr;
1947c478bd9Sstevel@tonic-gate 	char		addrbuf[INET6_ADDRSTRLEN + 1];
1957c478bd9Sstevel@tonic-gate 	nss_status_t	lstat;
1967c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1977c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1987c478bd9Sstevel@tonic-gate 	int		ret;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	argp->h_errno = 0;
2017c478bd9Sstevel@tonic-gate 	if ((argp->key.hostaddr.type != AF_INET6) ||
2027c478bd9Sstevel@tonic-gate 	    (argp->key.hostaddr.len != sizeof (addr)))
2037c478bd9Sstevel@tonic-gate 		return (NSS_NOTFOUND);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
2067c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
2077c478bd9Sstevel@tonic-gate 		if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
2087c478bd9Sstevel@tonic-gate 				(void *)addrbuf, INET_ADDRSTRLEN) == NULL) {
2097c478bd9Sstevel@tonic-gate 			return (NSS_NOTFOUND);
2107c478bd9Sstevel@tonic-gate 		}
2117c478bd9Sstevel@tonic-gate 	} else {
2127c478bd9Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, (void *)&addr, (void *)addrbuf,
2137c478bd9Sstevel@tonic-gate 				INET6_ADDRSTRLEN) == NULL)
2147c478bd9Sstevel@tonic-gate 			return (NSS_NOTFOUND);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2177c478bd9Sstevel@tonic-gate 	    _F_GETHOSTS6BYADDR, addrbuf);
2187c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2197c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2227c478bd9Sstevel@tonic-gate 	    _F_GETHOSTS6BYADDR_SSD, addrbuf);
2237c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2247c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp,
2277c478bd9Sstevel@tonic-gate 		_HOSTS6, searchfilter, NULL,
2287c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata);
2297c478bd9Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
2307c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
2337c478bd9Sstevel@tonic-gate 	return ((nss_status_t)lstat);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static ldap_backend_op_t ipnodes_ops[] = {
2377c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
2387c478bd9Sstevel@tonic-gate 	0,
2397c478bd9Sstevel@tonic-gate 	0,
2407c478bd9Sstevel@tonic-gate 	0,
2417c478bd9Sstevel@tonic-gate 	getbyname,
2427c478bd9Sstevel@tonic-gate 	getbyaddr
2437c478bd9Sstevel@tonic-gate };
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * _nss_ldap_hosts_constr is where life begins. This function calls the generic
2487c478bd9Sstevel@tonic-gate  * ldap constructor function to define and build the abstract data types
2497c478bd9Sstevel@tonic-gate  * required to support ldap operations.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2537c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_ldap_ipnodes_constr(const char * dummy1,const char * dummy2,const char * dummy3)2547c478bd9Sstevel@tonic-gate _nss_ldap_ipnodes_constr(const char *dummy1, const char *dummy2,
2557c478bd9Sstevel@tonic-gate 			const char *dummy3)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(ipnodes_ops,
2597c478bd9Sstevel@tonic-gate 		sizeof (ipnodes_ops)/sizeof (ipnodes_ops[0]), _HOSTS6,
260*cb5caa98Sdjl 		ipnodes_attrs, _nss_ldap_hosts2str));
2617c478bd9Sstevel@tonic-gate }
262