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
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * 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 /*
222830Sdjl  * 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>
290Sstevel@tonic-gate #include <arpa/inet.h>
300Sstevel@tonic-gate #include <netinet/in.h>
310Sstevel@tonic-gate #include <sys/socket.h>
320Sstevel@tonic-gate #include <syslog.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 #define	_H_DN			"dn"
390Sstevel@tonic-gate #define	_H_NAME			"cn"
400Sstevel@tonic-gate #define	_H_ADDR			"iphostnumber"
410Sstevel@tonic-gate #define	_F_GETHOSTBYNAME	"(&(objectClass=ipHost)(cn=%s))"
420Sstevel@tonic-gate #define	_F_GETHOSTBYNAME_SSD	"(&(%%s)(cn=%s))"
430Sstevel@tonic-gate #define	_F_GETHOSTDOTTEDBYNAME	"(&(objectClass=ipHost)(|(cn=%s)(cn=%s)))"
440Sstevel@tonic-gate #define	_F_GETHOSTDOTTEDBYNAME_SSD "(&(%%s)(|(cn=%s)(cn=%s)))"
450Sstevel@tonic-gate #define	_F_GETHOSTBYADDR	"(&(objectClass=ipHost)(ipHostNumber=%s))"
460Sstevel@tonic-gate #define	_F_GETHOSTBYADDR_SSD	"(&(%%s)(ipHostNumber=%s))"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static const char *hosts_attrs[] = {
490Sstevel@tonic-gate 	_H_NAME,
500Sstevel@tonic-gate 	_H_ADDR,
510Sstevel@tonic-gate 	(char *)NULL
520Sstevel@tonic-gate };
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
552830Sdjl  * _nss_ldap_hosts2str is the data marshaling method for the hosts getXbyY
562830Sdjl  * system call gethostbyname() and gethostbyaddr.
572830Sdjl  * This method is called after a successful search has been performed.
582830Sdjl  * This method will parse the search results into the file format.
592830Sdjl  * e.g.
602830Sdjl  *
612830Sdjl  * 9.9.9.9 jurassic jurassic1 jurassic2
622830Sdjl  * 10.10.10.10 puppy
632830Sdjl  *
640Sstevel@tonic-gate  */
652830Sdjl int
662830Sdjl _nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp)
670Sstevel@tonic-gate {
682830Sdjl 	uint_t			i;
690Sstevel@tonic-gate 	int			nss_result;
702830Sdjl 	int			buflen, buflen1, buflen2, len;
712830Sdjl 	int			firstimedn   = 1, first_entry;
722830Sdjl 	int			validaddress = 0, copy_cname;
732830Sdjl 	char			*cname = NULL, *h_name = NULL;
742830Sdjl 	char			*buffer = NULL;
752830Sdjl 	char			*name;
760Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
772830Sdjl 	ns_ldap_attr_t		*names;
780Sstevel@tonic-gate 	ns_ldap_entry_t		*entry;
792830Sdjl 	char			**ips = NULL, **dns = NULL;
802830Sdjl 	char			*first_host = NULL, *other_hosts = NULL;
812830Sdjl 	char			*buf1, *buf2;
822830Sdjl 
832830Sdjl 	if (result == NULL)
842830Sdjl 		return (NSS_STR_PARSE_PARSE);
852830Sdjl 	buflen = buflen1 = buflen2 = argp->buf.buflen;
860Sstevel@tonic-gate 
872830Sdjl 	if (argp->buf.result != NULL) {
882830Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
892830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
902830Sdjl 			goto result_host2str;
912830Sdjl 		}
922830Sdjl 		buffer = be->buffer;
932830Sdjl 	} else
942830Sdjl 		buffer = argp->buf.buffer;
952830Sdjl 	if ((first_host = calloc(1, buflen1)) == NULL) {
962830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
972830Sdjl 		goto result_host2str;
980Sstevel@tonic-gate 	}
992830Sdjl 	if ((other_hosts = calloc(1, buflen2)) == NULL) {
1002830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
1012830Sdjl 		goto result_host2str;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1042830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
1052830Sdjl 	(void) memset(argp->buf.buffer, 0, buflen);
1062830Sdjl 	/*
1072830Sdjl 	 * Multiple lines return will be sepereated by newlines
1082830Sdjl 	 * Single line return or last line does not have newline
1092830Sdjl 	 * e.g.
1102830Sdjl 	 *
1112830Sdjl 	 * 8.8.8.8 hostname
1122830Sdjl 	 *
1132830Sdjl 	 * or the search for hostname h1 returns 3 entries
1142830Sdjl 	 *
1152830Sdjl 	 * 9.9.9.9 h1
1162830Sdjl 	 * 10.10.10.10 h1 xx
1172830Sdjl 	 * 20.20.20.20 h1 yyy
1182830Sdjl 	 *
1192830Sdjl 	 * str2hostent expects all name/aliases in the first entry
1202830Sdjl 	 * so the string is organized as
1212830Sdjl 	 *
1222830Sdjl 	 * "9.9.9.9 h1 xx yy\n10.10.10.10 \n20.20.20.20 "
1232830Sdjl 	 *
1242830Sdjl 	 * Use first_host to hold "9.9.9.9 h1 xx yy" and other_hosts to hold
1252830Sdjl 	 * "\n10.10.10.10 \n20.20.20.20 "
1262830Sdjl 	 *
1272830Sdjl 	 */
1282830Sdjl 	buf1 = first_host;
1292830Sdjl 	buf2 = other_hosts;
1302830Sdjl 	first_entry = 1;
1310Sstevel@tonic-gate 	for (entry = result->entry; entry != NULL; entry = entry->next) {
1322830Sdjl 	    if (firstimedn) {
1332830Sdjl 		    dns =  __ns_ldap_getAttr(entry, _H_DN);
1342830Sdjl 		    if (dns == NULL || dns[0] == NULL || strlen(dns[0]) < 1) {
1352830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
1362830Sdjl 			goto result_host2str;
1370Sstevel@tonic-gate 		    }
1382830Sdjl 		    /* get domain name associated with this dn */
1392830Sdjl 		    be->toglue = _get_domain_name(dns[0]);
1402830Sdjl 		    firstimedn = 0;
1412830Sdjl 	    }
1422830Sdjl 
1432830Sdjl 	    /* Get IP */
1442830Sdjl 	    ips = __ns_ldap_getAttr(entry, _H_ADDR);
1452830Sdjl 	    if (ips == NULL || ips[0] == NULL || strlen(ips[0]) < 1) {
1462830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
1472830Sdjl 		goto result_host2str;
1482830Sdjl 	    }
1492830Sdjl 	    /* Skip IPV6 address in AF_INET mode */
1502830Sdjl 	    if (af == AF_INET &&
1512830Sdjl 		(inet_addr(_strip_quotes(ips[0])) == (in_addr_t)-1))
1522830Sdjl 		    continue;
1532830Sdjl 
1542830Sdjl 	    /* A valid address for either af mode */
1552830Sdjl 	    validaddress++;
1562830Sdjl 
1572830Sdjl 	    if (first_entry) {
1582830Sdjl 		len = snprintf(buf1, buflen1, "%s", ips[0]);
1592830Sdjl 		TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
1602830Sdjl 	    } else {
1612830Sdjl 		len = snprintf(buf2, buflen2, "\n%s ", ips[0]);
1622830Sdjl 		TEST_AND_ADJUST(len, buf2, buflen2, result_host2str);
1632830Sdjl 	    }
1642830Sdjl 
1652830Sdjl 	    /* Get host names */
1662830Sdjl 	    names = __ns_ldap_getAttrStruct(entry, _H_NAME);
1672830Sdjl 	    if (names == NULL || names->attrvalue == NULL) {
1682830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
1692830Sdjl 		goto result_host2str;
1702830Sdjl 	    }
1712830Sdjl 
1722830Sdjl 	    /* Get canonical name of each entry */
1732830Sdjl 	    cname = __s_api_get_canonical_name(entry,
1742830Sdjl 			names, 1);
1752830Sdjl 	    if (cname == NULL || strlen(cname) < 1) {
1762830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
1772830Sdjl 		goto result_host2str;
1782830Sdjl 	    }
1792830Sdjl 
1802830Sdjl 	    /* Filter cname that's identical to h_name */
1812830Sdjl 	    if (first_entry) {
1822830Sdjl 		    h_name = cname;
1832830Sdjl 		    first_entry = 0;
1842830Sdjl 		    copy_cname = 1;
1852830Sdjl 	    } else if (strcasecmp(cname, h_name) != 0) {
1862830Sdjl 		    copy_cname = 1;
1872830Sdjl 	    } else
1882830Sdjl 		    copy_cname = 0;
1892830Sdjl 
1902830Sdjl 	    if (copy_cname) {
1912830Sdjl 		/* Use the canonical name as the host name */
1922830Sdjl 		if (DOTTEDSUBDOMAIN(cname))
1932830Sdjl 			len = snprintf(buf1, buflen1, " %s", cname);
1942830Sdjl 		else
1952830Sdjl 			/* append domain name */
1962830Sdjl 			len = snprintf(buf1, buflen1, " %s.%s", cname,
1972830Sdjl 					be->toglue);
1982830Sdjl 
1992830Sdjl 		TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
2002830Sdjl 	    }
2012830Sdjl 
2022830Sdjl 	    /* Append aliases */
2032830Sdjl 	    for (i = 0; i < names->value_count; i++) {
2042830Sdjl 		name = names->attrvalue[i];
2052830Sdjl 		if (name == NULL) {
2062830Sdjl 		    nss_result = NSS_STR_PARSE_PARSE;
2072830Sdjl 		    goto result_host2str;
2080Sstevel@tonic-gate 		}
2092830Sdjl 		/* Skip the canonical name and h_name */
2102830Sdjl 		if (strcasecmp(name, cname) != 0 &&
2112830Sdjl 				strcasecmp(name, h_name) != 0) {
2122830Sdjl 		    if (DOTTEDSUBDOMAIN(name))
2132830Sdjl 			len = snprintf(buf1, buflen1, " %s", name);
2142830Sdjl 		    else
2152830Sdjl 			/* append domain name */
2162830Sdjl 			len = snprintf(buf1, buflen1, " %s.%s",
2172830Sdjl 					    name, be->toglue);
2182830Sdjl 		    TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	    }
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	if (validaddress == 0) {
2242830Sdjl 	/*
2252830Sdjl 	 * For AF_INET mode, it found an IPv6 address and skipped it.
2262830Sdjl 	 */
2272830Sdjl 	    nss_result = NSS_STR_PARSE_NO_ADDR;
2282830Sdjl 	    goto result_host2str;
2290Sstevel@tonic-gate 	}
2302830Sdjl 	/* Combine 2 strings */
2312830Sdjl 	len = snprintf(buffer, buflen, "%s%s", first_host, other_hosts);
2322830Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_host2str);
2332830Sdjl 
2342830Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
2352830Sdjl 	if (argp->buf.result != NULL)
2362830Sdjl 		be->buflen = strlen(be->buffer);
2370Sstevel@tonic-gate 
2382830Sdjl result_host2str:
2392830Sdjl 	if (first_host)
2402830Sdjl 		free(first_host);
2412830Sdjl 	if (other_hosts)
2422830Sdjl 		free(other_hosts);
243*3178Schinlong 	if (be->toglue) {
244*3178Schinlong 		free(be->toglue);
245*3178Schinlong 		be->toglue = NULL;
246*3178Schinlong 	}
2470Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
2482830Sdjl 	return (nss_result);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2512830Sdjl static int
2522830Sdjl _nss_ldap_hosts2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) {
2532830Sdjl 	return (_nss_ldap_hosts2str_int(AF_INET, be, argp));
2542830Sdjl }
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate /*
2570Sstevel@tonic-gate  * getbyname gets a struct hostent by hostname. This function constructs
2580Sstevel@tonic-gate  * an ldap search filter using the name invocation parameter and the
2590Sstevel@tonic-gate  * gethostbyname search filter defined. Once the filter is constructed,
2600Sstevel@tonic-gate  * we search for a matching entry and marshal the data results into
2610Sstevel@tonic-gate  * struct hostent for the frontend process.  Host name searches will be
2620Sstevel@tonic-gate  * on fully qualified host names (foo.bar.sun.com)
2630Sstevel@tonic-gate  */
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate static nss_status_t
2660Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
2690Sstevel@tonic-gate 	char		realdomain[BUFSIZ];
2700Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2710Sstevel@tonic-gate 	nss_status_t	lstat;
2720Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2730Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2740Sstevel@tonic-gate 	int		rc;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
2770Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	rc = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYNAME,
2800Sstevel@tonic-gate 	    hostname);
2810Sstevel@tonic-gate 	if (rc >= sizeof (searchfilter) || rc < 0)
2820Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	rc = snprintf(userdata, sizeof (userdata), _F_GETHOSTBYNAME_SSD,
2850Sstevel@tonic-gate 	    hostname);
2860Sstevel@tonic-gate 	if (rc >= sizeof (userdata) || rc < 0)
2870Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	/* get the domain we are in */
2900Sstevel@tonic-gate 	rc = sysinfo(SI_SRPC_DOMAIN, realdomain, BUFSIZ);
2910Sstevel@tonic-gate 	if (rc <= 0)
2920Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	/* Is this a request for a host.domain */
2950Sstevel@tonic-gate 	if (DOTTEDSUBDOMAIN(hostname)) {
2960Sstevel@tonic-gate 		char	host[MAXHOSTNAMELEN];
2970Sstevel@tonic-gate 		char	domain[MAXHOSTNAMELEN];
2980Sstevel@tonic-gate 		char	hname[3 * MAXHOSTNAMELEN];
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		/* separate host and domain.  this function */
3010Sstevel@tonic-gate 		/* will munge hname, so use argp->keyname */
3020Sstevel@tonic-gate 		/* from here on for original string */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		(void) strcpy(hname, hostname);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		if (chophostdomain(hname, host, domain) == -1) {
3070Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3080Sstevel@tonic-gate 		}
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 		/* if domain is a proper subset of realdomain */
3110Sstevel@tonic-gate 		/* ie. domain = "eng" and realdomain */
3120Sstevel@tonic-gate 		/* = "eng.wiz.com", we try to lookup both" */
3130Sstevel@tonic-gate 		/* host.domain and host */
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 		if (propersubdomain(realdomain, domain) == 1) {
3160Sstevel@tonic-gate 			/* yes, it is a proper domain */
3170Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
3180Sstevel@tonic-gate 			    _F_GETHOSTDOTTEDBYNAME, hostname, host);
3190Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
3200Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
3230Sstevel@tonic-gate 			    _F_GETHOSTDOTTEDBYNAME_SSD, hostname, host);
3240Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
3250Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3260Sstevel@tonic-gate 		} else {
3270Sstevel@tonic-gate 			/* it is not a proper domain, so only try to look up */
3280Sstevel@tonic-gate 			/* host.domain */
3290Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
3300Sstevel@tonic-gate 			    _F_GETHOSTBYNAME, hostname);
3310Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
3320Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
3350Sstevel@tonic-gate 			    _F_GETHOSTBYNAME_SSD, hostname);
3360Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
3370Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3380Sstevel@tonic-gate 		}
3390Sstevel@tonic-gate 	} else {
3400Sstevel@tonic-gate 		rc = snprintf(searchfilter, sizeof (searchfilter),
3410Sstevel@tonic-gate 		    _F_GETHOSTBYNAME, hostname);
3420Sstevel@tonic-gate 		if (rc >= sizeof (searchfilter) || rc < 0)
3430Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		rc = snprintf(userdata, sizeof (userdata),
3460Sstevel@tonic-gate 		    _F_GETHOSTBYNAME_SSD, hostname);
3470Sstevel@tonic-gate 		if (rc >= sizeof (userdata) || rc < 0)
3480Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS,
3510Sstevel@tonic-gate 		searchfilter, NULL, _merge_SSD_filter,
3520Sstevel@tonic-gate 		userdata);
3530Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
3540Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
3570Sstevel@tonic-gate 	return ((nss_status_t)lstat);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate  * getbyaddr gets a struct hostent by host address. This function
3630Sstevel@tonic-gate  * constructs an ldap search filter using the host address invocation
3640Sstevel@tonic-gate  * parameter and the gethostbyaddr search filter defined. Once the
3650Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
3660Sstevel@tonic-gate  * the data results into struct hostent for the frontend process.
3670Sstevel@tonic-gate  *
3680Sstevel@tonic-gate  * extern char *inet_ntoa_r() not an advertised function from libnsl.
3690Sstevel@tonic-gate  * There is no man page and no prototype.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static nss_status_t
3730Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
3760Sstevel@tonic-gate 	struct in_addr	addr;
3770Sstevel@tonic-gate 	char		buf[18];
3780Sstevel@tonic-gate 	nss_status_t	lstat;
3790Sstevel@tonic-gate 	extern char	*inet_ntoa_r();
3800Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
3810Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
3820Sstevel@tonic-gate 	int		ret;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	argp->h_errno = 0;
3850Sstevel@tonic-gate 	if ((argp->key.hostaddr.type != AF_INET) ||
3860Sstevel@tonic-gate 	    (argp->key.hostaddr.len != sizeof (addr)))
3870Sstevel@tonic-gate 		return (NSS_NOTFOUND);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
3900Sstevel@tonic-gate 	(void) inet_ntoa_r(addr, buf);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYADDR,
3930Sstevel@tonic-gate 	    buf);
3940Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
3950Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata), _F_GETHOSTBYADDR_SSD, buf);
3980Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
3990Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp,
4020Sstevel@tonic-gate 		_HOSTS, searchfilter, NULL, _merge_SSD_filter, userdata);
4030Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
4040Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
4070Sstevel@tonic-gate 	return ((nss_status_t)lstat);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate static ldap_backend_op_t hosts_ops[] = {
4110Sstevel@tonic-gate 	_nss_ldap_destr,
4120Sstevel@tonic-gate 	_nss_ldap_endent,
4130Sstevel@tonic-gate 	_nss_ldap_setent,
4140Sstevel@tonic-gate 	_nss_ldap_getent,
4150Sstevel@tonic-gate 	getbyname,
4160Sstevel@tonic-gate 	getbyaddr
4170Sstevel@tonic-gate };
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * _nss_ldap_hosts_constr is where life begins. This function calls the generic
4220Sstevel@tonic-gate  * ldap constructor function to define and build the abstract data types
4230Sstevel@tonic-gate  * required to support ldap operations.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*ARGSUSED0*/
4270Sstevel@tonic-gate nss_backend_t *
4280Sstevel@tonic-gate _nss_ldap_hosts_constr(const char *dummy1, const char *dummy2,
4290Sstevel@tonic-gate 			const char *dummy3)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(hosts_ops,
4330Sstevel@tonic-gate 		sizeof (hosts_ops)/sizeof (hosts_ops[0]), _HOSTS,
4342830Sdjl 		hosts_attrs, _nss_ldap_hosts2str));
4350Sstevel@tonic-gate }
436