xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/gethostent6.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <netdb.h>
29*2830Sdjl #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/socket.h>
31*2830Sdjl #include <netinet/in.h>
32*2830Sdjl #include <arpa/inet.h>
330Sstevel@tonic-gate #include <sys/systeminfo.h>
340Sstevel@tonic-gate #include "ns_internal.h"
350Sstevel@tonic-gate #include "ldap_common.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /* host attributes filters */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /* probably some change in the ipHostNumber field */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	_H_DN			"dn"
420Sstevel@tonic-gate #define	_H_NAME			"cn"
430Sstevel@tonic-gate #define	_H_ADDR			"iphostnumber"
440Sstevel@tonic-gate #define	_F_GETHOSTS6BYNAME	"(&(objectClass=ipHost)(cn=%s))"
450Sstevel@tonic-gate #define	_F_GETHOSTS6BYNAME_SSD	"(&(%%s)(cn=%s))"
460Sstevel@tonic-gate #define	_F_GETHOSTS6DOTTEDBYNAME \
470Sstevel@tonic-gate 				"(&(objectClass=ipHost)(|(cn=%s)(cn=%s)))"
480Sstevel@tonic-gate #define	_F_GETHOSTS6DOTTEDBYNAME_SSD \
490Sstevel@tonic-gate 				"(&(%%s)(|(cn=%s)(cn=%s)))"
500Sstevel@tonic-gate #define	_F_GETHOSTS6BYADDR	"(&(objectClass=ipHost)(ipHostNumber=%s))"
510Sstevel@tonic-gate #define	_F_GETHOSTS6BYADDR_SSD	"(&(%%s)(ipHostNumber=%s))"
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static const char *ipnodes_attrs[] = {
540Sstevel@tonic-gate 	_H_NAME,
550Sstevel@tonic-gate 	_H_ADDR,
560Sstevel@tonic-gate 	(char *)NULL
570Sstevel@tonic-gate };
580Sstevel@tonic-gate 
59*2830Sdjl extern int
60*2830Sdjl _nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /*
63*2830Sdjl  * _nss_ldap_hosts2str is the data marshaling method for the ipnodes getXbyY
64*2830Sdjl  * system call gethostbyname() and gethostbyaddr.
65*2830Sdjl  * This method is called after a successful search has been performed.
66*2830Sdjl  * This method will parse the search results into the file format.
67*2830Sdjl  * e.g.
68*2830Sdjl  *
69*2830Sdjl  * fe80::a00:20ff:fec4:f2b6 ipnodes_1
70*2830Sdjl  *
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate static int
_nss_ldap_hosts2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)73*2830Sdjl _nss_ldap_hosts2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) {
74*2830Sdjl 	return (_nss_ldap_hosts2str_int(AF_INET6, be, argp));
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * getbyname gets a struct hostent by hostname. This function constructs
790Sstevel@tonic-gate  * an ldap search filter using the name invocation parameter and the
800Sstevel@tonic-gate  * gethostbyname search filter defined. Once the filter is constructed,
810Sstevel@tonic-gate  * we search for a matching entry and marshal the data results into
820Sstevel@tonic-gate  * struct hostent for the frontend process.  Host name searches will be
830Sstevel@tonic-gate  * on fully qualified host names (foo.bar.sun.com)
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)870Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
900Sstevel@tonic-gate 	char		realdomain[BUFSIZ];
910Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
920Sstevel@tonic-gate 	nss_status_t	lstat;
930Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
940Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
950Sstevel@tonic-gate 	int		rc;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.ipnode.name,
980Sstevel@tonic-gate 			sizeof (hostname)) != 0)
990Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	rc = snprintf(searchfilter, sizeof (searchfilter),
1020Sstevel@tonic-gate 	    _F_GETHOSTS6BYNAME, hostname);
1030Sstevel@tonic-gate 	if (rc >= sizeof (searchfilter) || rc < 0)
1040Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1050Sstevel@tonic-gate 	rc = snprintf(userdata, sizeof (userdata),
1060Sstevel@tonic-gate 	    _F_GETHOSTS6BYNAME_SSD, hostname);
1070Sstevel@tonic-gate 	if (rc >= sizeof (userdata) || rc < 0)
1080Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/* get the domain we are in */
1110Sstevel@tonic-gate 	rc = sysinfo(SI_SRPC_DOMAIN, realdomain, BUFSIZ);
1120Sstevel@tonic-gate 	if (rc <= 0)
1130Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/* Is this a request for a host.domain */
1160Sstevel@tonic-gate 	if (DOTTEDSUBDOMAIN(hostname)) {
1170Sstevel@tonic-gate 		char	host[MAXHOSTNAMELEN];
1180Sstevel@tonic-gate 		char	domain[MAXHOSTNAMELEN];
1190Sstevel@tonic-gate 		char	hname[3 * MAXHOSTNAMELEN];
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 		/* separate host and domain.  this function */
1220Sstevel@tonic-gate 		/* will munge hname, so use argp->keyname */
1230Sstevel@tonic-gate 		/* from here on for original string */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		(void) strcpy(hname, hostname);
1260Sstevel@tonic-gate 		if (chophostdomain(hname, host, domain) == -1) {
1270Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 		/* if domain is a proper subset of realdomain */
1310Sstevel@tonic-gate 		/* ie. domain = "foo" and realdomain */
1320Sstevel@tonic-gate 		/* = "foor.bar.sun.com", we try to lookup both" */
1330Sstevel@tonic-gate 		/* host.domain and host */
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 		if (propersubdomain(realdomain, domain) == 1) {
1360Sstevel@tonic-gate 			/* yes, it is a proper domain */
1370Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
1380Sstevel@tonic-gate 			    _F_GETHOSTS6DOTTEDBYNAME, hostname, host);
1390Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
1400Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
1430Sstevel@tonic-gate 			    _F_GETHOSTS6DOTTEDBYNAME_SSD, hostname, host);
1440Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
1450Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1460Sstevel@tonic-gate 		} else {
1470Sstevel@tonic-gate 			/* it is not a proper domain, so only try to look up */
1480Sstevel@tonic-gate 			/* host.domain */
1490Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
1500Sstevel@tonic-gate 			    _F_GETHOSTS6BYNAME, hostname);
1510Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
1520Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
1550Sstevel@tonic-gate 			    _F_GETHOSTS6BYNAME_SSD, hostname);
1560Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
1570Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	} else {
1600Sstevel@tonic-gate 		rc = snprintf(searchfilter, sizeof (searchfilter),
1610Sstevel@tonic-gate 		    _F_GETHOSTS6BYNAME, hostname);
1620Sstevel@tonic-gate 		if (rc >= sizeof (searchfilter) || rc < 0)
1630Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 		rc = snprintf(userdata, sizeof (userdata),
1660Sstevel@tonic-gate 		    _F_GETHOSTS6BYNAME_SSD, hostname);
1670Sstevel@tonic-gate 		if (rc >= sizeof (userdata) || rc < 0)
1680Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS,
1710Sstevel@tonic-gate 		searchfilter, NULL,
1720Sstevel@tonic-gate 		_merge_SSD_filter, userdata);
1730Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
1740Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
1770Sstevel@tonic-gate 	return ((nss_status_t)lstat);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * getbyaddr gets a struct hostent by host address. This function
1830Sstevel@tonic-gate  * constructs an ldap search filter using the host address invocation
1840Sstevel@tonic-gate  * parameter and the gethostbyaddr search filter defined. Once the
1850Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
1860Sstevel@tonic-gate  * the data results into struct hostent for the frontend process.
1870Sstevel@tonic-gate  */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static nss_status_t
getbyaddr(ldap_backend_ptr be,void * a)1900Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1930Sstevel@tonic-gate 	struct in6_addr	addr;
1940Sstevel@tonic-gate 	char		addrbuf[INET6_ADDRSTRLEN + 1];
1950Sstevel@tonic-gate 	nss_status_t	lstat;
1960Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1970Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1980Sstevel@tonic-gate 	int		ret;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	argp->h_errno = 0;
2010Sstevel@tonic-gate 	if ((argp->key.hostaddr.type != AF_INET6) ||
2020Sstevel@tonic-gate 	    (argp->key.hostaddr.len != sizeof (addr)))
2030Sstevel@tonic-gate 		return (NSS_NOTFOUND);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
2060Sstevel@tonic-gate 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
2070Sstevel@tonic-gate 		if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
2080Sstevel@tonic-gate 				(void *)addrbuf, INET_ADDRSTRLEN) == NULL) {
2090Sstevel@tonic-gate 			return (NSS_NOTFOUND);
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 	} else {
2120Sstevel@tonic-gate 		if (inet_ntop(AF_INET6, (void *)&addr, (void *)addrbuf,
2130Sstevel@tonic-gate 				INET6_ADDRSTRLEN) == NULL)
2140Sstevel@tonic-gate 			return (NSS_NOTFOUND);
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
2170Sstevel@tonic-gate 	    _F_GETHOSTS6BYADDR, addrbuf);
2180Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
2190Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
2220Sstevel@tonic-gate 	    _F_GETHOSTS6BYADDR_SSD, addrbuf);
2230Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
2240Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp,
2270Sstevel@tonic-gate 		_HOSTS6, searchfilter, NULL,
2280Sstevel@tonic-gate 		_merge_SSD_filter, userdata);
2290Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
2300Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
2330Sstevel@tonic-gate 	return ((nss_status_t)lstat);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static ldap_backend_op_t ipnodes_ops[] = {
2370Sstevel@tonic-gate 	_nss_ldap_destr,
2380Sstevel@tonic-gate 	0,
2390Sstevel@tonic-gate 	0,
2400Sstevel@tonic-gate 	0,
2410Sstevel@tonic-gate 	getbyname,
2420Sstevel@tonic-gate 	getbyaddr
2430Sstevel@tonic-gate };
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * _nss_ldap_hosts_constr is where life begins. This function calls the generic
2480Sstevel@tonic-gate  * ldap constructor function to define and build the abstract data types
2490Sstevel@tonic-gate  * required to support ldap operations.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*ARGSUSED0*/
2530Sstevel@tonic-gate nss_backend_t *
_nss_ldap_ipnodes_constr(const char * dummy1,const char * dummy2,const char * dummy3)2540Sstevel@tonic-gate _nss_ldap_ipnodes_constr(const char *dummy1, const char *dummy2,
2550Sstevel@tonic-gate 			const char *dummy3)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(ipnodes_ops,
2590Sstevel@tonic-gate 		sizeof (ipnodes_ops)/sizeof (ipnodes_ops[0]), _HOSTS6,
260*2830Sdjl 		ipnodes_attrs, _nss_ldap_hosts2str));
2610Sstevel@tonic-gate }
262