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>
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 /*
55*2830Sdjl  * _nss_ldap_hosts2str is the data marshaling method for the hosts getXbyY
56*2830Sdjl  * system call gethostbyname() and gethostbyaddr.
57*2830Sdjl  * This method is called after a successful search has been performed.
58*2830Sdjl  * This method will parse the search results into the file format.
59*2830Sdjl  * e.g.
60*2830Sdjl  *
61*2830Sdjl  * 9.9.9.9 jurassic jurassic1 jurassic2
62*2830Sdjl  * 10.10.10.10 puppy
63*2830Sdjl  *
640Sstevel@tonic-gate  */
65*2830Sdjl int
66*2830Sdjl _nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp)
670Sstevel@tonic-gate {
68*2830Sdjl 	uint_t			i;
690Sstevel@tonic-gate 	int			nss_result;
70*2830Sdjl 	int			buflen, buflen1, buflen2, len;
71*2830Sdjl 	int			firstimedn   = 1, first_entry;
72*2830Sdjl 	int			validaddress = 0, copy_cname;
73*2830Sdjl 	char			*cname = NULL, *h_name = NULL;
74*2830Sdjl 	char			*buffer = NULL;
75*2830Sdjl 	char			*name;
760Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
77*2830Sdjl 	ns_ldap_attr_t		*names;
780Sstevel@tonic-gate 	ns_ldap_entry_t		*entry;
79*2830Sdjl 	char			**ips = NULL, **dns = NULL;
80*2830Sdjl 	char			*first_host = NULL, *other_hosts = NULL;
81*2830Sdjl 	char			*buf1, *buf2;
82*2830Sdjl 
83*2830Sdjl 	if (result == NULL)
84*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
85*2830Sdjl 	buflen = buflen1 = buflen2 = argp->buf.buflen;
860Sstevel@tonic-gate 
87*2830Sdjl 	if (argp->buf.result != NULL) {
88*2830Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
89*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
90*2830Sdjl 			goto result_host2str;
91*2830Sdjl 		}
92*2830Sdjl 		buffer = be->buffer;
93*2830Sdjl 	} else
94*2830Sdjl 		buffer = argp->buf.buffer;
95*2830Sdjl 	if ((first_host = calloc(1, buflen1)) == NULL) {
96*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
97*2830Sdjl 		goto result_host2str;
980Sstevel@tonic-gate 	}
99*2830Sdjl 	if ((other_hosts = calloc(1, buflen2)) == NULL) {
100*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
101*2830Sdjl 		goto result_host2str;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
104*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
105*2830Sdjl 	(void) memset(argp->buf.buffer, 0, buflen);
106*2830Sdjl 	/*
107*2830Sdjl 	 * Multiple lines return will be sepereated by newlines
108*2830Sdjl 	 * Single line return or last line does not have newline
109*2830Sdjl 	 * e.g.
110*2830Sdjl 	 *
111*2830Sdjl 	 * 8.8.8.8 hostname
112*2830Sdjl 	 *
113*2830Sdjl 	 * or the search for hostname h1 returns 3 entries
114*2830Sdjl 	 *
115*2830Sdjl 	 * 9.9.9.9 h1
116*2830Sdjl 	 * 10.10.10.10 h1 xx
117*2830Sdjl 	 * 20.20.20.20 h1 yyy
118*2830Sdjl 	 *
119*2830Sdjl 	 * str2hostent expects all name/aliases in the first entry
120*2830Sdjl 	 * so the string is organized as
121*2830Sdjl 	 *
122*2830Sdjl 	 * "9.9.9.9 h1 xx yy\n10.10.10.10 \n20.20.20.20 "
123*2830Sdjl 	 *
124*2830Sdjl 	 * Use first_host to hold "9.9.9.9 h1 xx yy" and other_hosts to hold
125*2830Sdjl 	 * "\n10.10.10.10 \n20.20.20.20 "
126*2830Sdjl 	 *
127*2830Sdjl 	 */
128*2830Sdjl 	buf1 = first_host;
129*2830Sdjl 	buf2 = other_hosts;
130*2830Sdjl 	first_entry = 1;
1310Sstevel@tonic-gate 	for (entry = result->entry; entry != NULL; entry = entry->next) {
132*2830Sdjl 	    if (firstimedn) {
133*2830Sdjl 		    dns =  __ns_ldap_getAttr(entry, _H_DN);
134*2830Sdjl 		    if (dns == NULL || dns[0] == NULL || strlen(dns[0]) < 1) {
135*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
136*2830Sdjl 			goto result_host2str;
1370Sstevel@tonic-gate 		    }
138*2830Sdjl 		    /* get domain name associated with this dn */
139*2830Sdjl 		    be->toglue = _get_domain_name(dns[0]);
140*2830Sdjl 		    firstimedn = 0;
141*2830Sdjl 	    }
142*2830Sdjl 
143*2830Sdjl 	    /* Get IP */
144*2830Sdjl 	    ips = __ns_ldap_getAttr(entry, _H_ADDR);
145*2830Sdjl 	    if (ips == NULL || ips[0] == NULL || strlen(ips[0]) < 1) {
146*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
147*2830Sdjl 		goto result_host2str;
148*2830Sdjl 	    }
149*2830Sdjl 	    /* Skip IPV6 address in AF_INET mode */
150*2830Sdjl 	    if (af == AF_INET &&
151*2830Sdjl 		(inet_addr(_strip_quotes(ips[0])) == (in_addr_t)-1))
152*2830Sdjl 		    continue;
153*2830Sdjl 
154*2830Sdjl 	    /* A valid address for either af mode */
155*2830Sdjl 	    validaddress++;
156*2830Sdjl 
157*2830Sdjl 	    if (first_entry) {
158*2830Sdjl 		len = snprintf(buf1, buflen1, "%s", ips[0]);
159*2830Sdjl 		TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
160*2830Sdjl 	    } else {
161*2830Sdjl 		len = snprintf(buf2, buflen2, "\n%s ", ips[0]);
162*2830Sdjl 		TEST_AND_ADJUST(len, buf2, buflen2, result_host2str);
163*2830Sdjl 	    }
164*2830Sdjl 
165*2830Sdjl 	    /* Get host names */
166*2830Sdjl 	    names = __ns_ldap_getAttrStruct(entry, _H_NAME);
167*2830Sdjl 	    if (names == NULL || names->attrvalue == NULL) {
168*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
169*2830Sdjl 		goto result_host2str;
170*2830Sdjl 	    }
171*2830Sdjl 
172*2830Sdjl 	    /* Get canonical name of each entry */
173*2830Sdjl 	    cname = __s_api_get_canonical_name(entry,
174*2830Sdjl 			names, 1);
175*2830Sdjl 	    if (cname == NULL || strlen(cname) < 1) {
176*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
177*2830Sdjl 		goto result_host2str;
178*2830Sdjl 	    }
179*2830Sdjl 
180*2830Sdjl 	    /* Filter cname that's identical to h_name */
181*2830Sdjl 	    if (first_entry) {
182*2830Sdjl 		    h_name = cname;
183*2830Sdjl 		    first_entry = 0;
184*2830Sdjl 		    copy_cname = 1;
185*2830Sdjl 	    } else if (strcasecmp(cname, h_name) != 0) {
186*2830Sdjl 		    copy_cname = 1;
187*2830Sdjl 	    } else
188*2830Sdjl 		    copy_cname = 0;
189*2830Sdjl 
190*2830Sdjl 	    if (copy_cname) {
191*2830Sdjl 		/* Use the canonical name as the host name */
192*2830Sdjl 		if (DOTTEDSUBDOMAIN(cname))
193*2830Sdjl 			len = snprintf(buf1, buflen1, " %s", cname);
194*2830Sdjl 		else
195*2830Sdjl 			/* append domain name */
196*2830Sdjl 			len = snprintf(buf1, buflen1, " %s.%s", cname,
197*2830Sdjl 					be->toglue);
198*2830Sdjl 
199*2830Sdjl 		TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
200*2830Sdjl 	    }
201*2830Sdjl 
202*2830Sdjl 	    /* Append aliases */
203*2830Sdjl 	    for (i = 0; i < names->value_count; i++) {
204*2830Sdjl 		name = names->attrvalue[i];
205*2830Sdjl 		if (name == NULL) {
206*2830Sdjl 		    nss_result = NSS_STR_PARSE_PARSE;
207*2830Sdjl 		    goto result_host2str;
2080Sstevel@tonic-gate 		}
209*2830Sdjl 		/* Skip the canonical name and h_name */
210*2830Sdjl 		if (strcasecmp(name, cname) != 0 &&
211*2830Sdjl 				strcasecmp(name, h_name) != 0) {
212*2830Sdjl 		    if (DOTTEDSUBDOMAIN(name))
213*2830Sdjl 			len = snprintf(buf1, buflen1, " %s", name);
214*2830Sdjl 		    else
215*2830Sdjl 			/* append domain name */
216*2830Sdjl 			len = snprintf(buf1, buflen1, " %s.%s",
217*2830Sdjl 					    name, be->toglue);
218*2830Sdjl 		    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) {
224*2830Sdjl 	/*
225*2830Sdjl 	 * For AF_INET mode, it found an IPv6 address and skipped it.
226*2830Sdjl 	 */
227*2830Sdjl 	    nss_result = NSS_STR_PARSE_NO_ADDR;
228*2830Sdjl 	    goto result_host2str;
2290Sstevel@tonic-gate 	}
230*2830Sdjl 	/* Combine 2 strings */
231*2830Sdjl 	len = snprintf(buffer, buflen, "%s%s", first_host, other_hosts);
232*2830Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_host2str);
233*2830Sdjl 
234*2830Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
235*2830Sdjl 	if (argp->buf.result != NULL)
236*2830Sdjl 		be->buflen = strlen(be->buffer);
2370Sstevel@tonic-gate 
238*2830Sdjl result_host2str:
239*2830Sdjl 	if (first_host)
240*2830Sdjl 		free(first_host);
241*2830Sdjl 	if (other_hosts)
242*2830Sdjl 		free(other_hosts);
2430Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
244*2830Sdjl 	return (nss_result);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
247*2830Sdjl static int
248*2830Sdjl _nss_ldap_hosts2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) {
249*2830Sdjl 	return (_nss_ldap_hosts2str_int(AF_INET, be, argp));
250*2830Sdjl }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * getbyname gets a struct hostent by hostname. This function constructs
2540Sstevel@tonic-gate  * an ldap search filter using the name invocation parameter and the
2550Sstevel@tonic-gate  * gethostbyname search filter defined. Once the filter is constructed,
2560Sstevel@tonic-gate  * we search for a matching entry and marshal the data results into
2570Sstevel@tonic-gate  * struct hostent for the frontend process.  Host name searches will be
2580Sstevel@tonic-gate  * on fully qualified host names (foo.bar.sun.com)
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate static nss_status_t
2620Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
2650Sstevel@tonic-gate 	char		realdomain[BUFSIZ];
2660Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2670Sstevel@tonic-gate 	nss_status_t	lstat;
2680Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
2690Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
2700Sstevel@tonic-gate 	int		rc;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
2730Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	rc = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYNAME,
2760Sstevel@tonic-gate 	    hostname);
2770Sstevel@tonic-gate 	if (rc >= sizeof (searchfilter) || rc < 0)
2780Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	rc = snprintf(userdata, sizeof (userdata), _F_GETHOSTBYNAME_SSD,
2810Sstevel@tonic-gate 	    hostname);
2820Sstevel@tonic-gate 	if (rc >= sizeof (userdata) || rc < 0)
2830Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/* get the domain we are in */
2860Sstevel@tonic-gate 	rc = sysinfo(SI_SRPC_DOMAIN, realdomain, BUFSIZ);
2870Sstevel@tonic-gate 	if (rc <= 0)
2880Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	/* Is this a request for a host.domain */
2910Sstevel@tonic-gate 	if (DOTTEDSUBDOMAIN(hostname)) {
2920Sstevel@tonic-gate 		char	host[MAXHOSTNAMELEN];
2930Sstevel@tonic-gate 		char	domain[MAXHOSTNAMELEN];
2940Sstevel@tonic-gate 		char	hname[3 * MAXHOSTNAMELEN];
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 		/* separate host and domain.  this function */
2970Sstevel@tonic-gate 		/* will munge hname, so use argp->keyname */
2980Sstevel@tonic-gate 		/* from here on for original string */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		(void) strcpy(hname, hostname);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		if (chophostdomain(hname, host, domain) == -1) {
3030Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 		/* if domain is a proper subset of realdomain */
3070Sstevel@tonic-gate 		/* ie. domain = "eng" and realdomain */
3080Sstevel@tonic-gate 		/* = "eng.wiz.com", we try to lookup both" */
3090Sstevel@tonic-gate 		/* host.domain and host */
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 		if (propersubdomain(realdomain, domain) == 1) {
3120Sstevel@tonic-gate 			/* yes, it is a proper domain */
3130Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
3140Sstevel@tonic-gate 			    _F_GETHOSTDOTTEDBYNAME, hostname, host);
3150Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
3160Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
3190Sstevel@tonic-gate 			    _F_GETHOSTDOTTEDBYNAME_SSD, hostname, host);
3200Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
3210Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3220Sstevel@tonic-gate 		} else {
3230Sstevel@tonic-gate 			/* it is not a proper domain, so only try to look up */
3240Sstevel@tonic-gate 			/* host.domain */
3250Sstevel@tonic-gate 			rc = snprintf(searchfilter, sizeof (searchfilter),
3260Sstevel@tonic-gate 			    _F_GETHOSTBYNAME, hostname);
3270Sstevel@tonic-gate 			if (rc >= sizeof (searchfilter) || rc < 0)
3280Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 			rc = snprintf(userdata, sizeof (userdata),
3310Sstevel@tonic-gate 			    _F_GETHOSTBYNAME_SSD, hostname);
3320Sstevel@tonic-gate 			if (rc >= sizeof (userdata) || rc < 0)
3330Sstevel@tonic-gate 				return ((nss_status_t)NSS_NOTFOUND);
3340Sstevel@tonic-gate 		}
3350Sstevel@tonic-gate 	} else {
3360Sstevel@tonic-gate 		rc = snprintf(searchfilter, sizeof (searchfilter),
3370Sstevel@tonic-gate 		    _F_GETHOSTBYNAME, hostname);
3380Sstevel@tonic-gate 		if (rc >= sizeof (searchfilter) || rc < 0)
3390Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 		rc = snprintf(userdata, sizeof (userdata),
3420Sstevel@tonic-gate 		    _F_GETHOSTBYNAME_SSD, hostname);
3430Sstevel@tonic-gate 		if (rc >= sizeof (userdata) || rc < 0)
3440Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS,
3470Sstevel@tonic-gate 		searchfilter, NULL, _merge_SSD_filter,
3480Sstevel@tonic-gate 		userdata);
3490Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
3500Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
3530Sstevel@tonic-gate 	return ((nss_status_t)lstat);
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * getbyaddr gets a struct hostent by host address. This function
3590Sstevel@tonic-gate  * constructs an ldap search filter using the host address invocation
3600Sstevel@tonic-gate  * parameter and the gethostbyaddr search filter defined. Once the
3610Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
3620Sstevel@tonic-gate  * the data results into struct hostent for the frontend process.
3630Sstevel@tonic-gate  *
3640Sstevel@tonic-gate  * extern char *inet_ntoa_r() not an advertised function from libnsl.
3650Sstevel@tonic-gate  * There is no man page and no prototype.
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate static nss_status_t
3690Sstevel@tonic-gate getbyaddr(ldap_backend_ptr be, void *a)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
3720Sstevel@tonic-gate 	struct in_addr	addr;
3730Sstevel@tonic-gate 	char		buf[18];
3740Sstevel@tonic-gate 	nss_status_t	lstat;
3750Sstevel@tonic-gate 	extern char	*inet_ntoa_r();
3760Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
3770Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
3780Sstevel@tonic-gate 	int		ret;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	argp->h_errno = 0;
3810Sstevel@tonic-gate 	if ((argp->key.hostaddr.type != AF_INET) ||
3820Sstevel@tonic-gate 	    (argp->key.hostaddr.len != sizeof (addr)))
3830Sstevel@tonic-gate 		return (NSS_NOTFOUND);
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
3860Sstevel@tonic-gate 	(void) inet_ntoa_r(addr, buf);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYADDR,
3890Sstevel@tonic-gate 	    buf);
3900Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
3910Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata), _F_GETHOSTBYADDR_SSD, buf);
3940Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
3950Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	lstat = (nss_status_t)_nss_ldap_lookup(be, argp,
3980Sstevel@tonic-gate 		_HOSTS, searchfilter, NULL, _merge_SSD_filter, userdata);
3990Sstevel@tonic-gate 	if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
4000Sstevel@tonic-gate 		return ((nss_status_t)NSS_SUCCESS);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	argp->h_errno = __nss2herrno(lstat);
4030Sstevel@tonic-gate 	return ((nss_status_t)lstat);
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate static ldap_backend_op_t hosts_ops[] = {
4070Sstevel@tonic-gate 	_nss_ldap_destr,
4080Sstevel@tonic-gate 	_nss_ldap_endent,
4090Sstevel@tonic-gate 	_nss_ldap_setent,
4100Sstevel@tonic-gate 	_nss_ldap_getent,
4110Sstevel@tonic-gate 	getbyname,
4120Sstevel@tonic-gate 	getbyaddr
4130Sstevel@tonic-gate };
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * _nss_ldap_hosts_constr is where life begins. This function calls the generic
4180Sstevel@tonic-gate  * ldap constructor function to define and build the abstract data types
4190Sstevel@tonic-gate  * required to support ldap operations.
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*ARGSUSED0*/
4230Sstevel@tonic-gate nss_backend_t *
4240Sstevel@tonic-gate _nss_ldap_hosts_constr(const char *dummy1, const char *dummy2,
4250Sstevel@tonic-gate 			const char *dummy3)
4260Sstevel@tonic-gate {
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(hosts_ops,
4290Sstevel@tonic-gate 		sizeof (hosts_ops)/sizeof (hosts_ops[0]), _HOSTS,
430*2830Sdjl 		hosts_attrs, _nss_ldap_hosts2str));
4310Sstevel@tonic-gate }
432