xref: /titanic_52/usr/src/lib/nsswitch/ldap/common/getservent.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 <ctype.h>
297c478bd9Sstevel@tonic-gate #include <netdb.h>
307c478bd9Sstevel@tonic-gate #include "ns_internal.h"
317c478bd9Sstevel@tonic-gate #include "ldap_common.h"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* services attributes filters */
347c478bd9Sstevel@tonic-gate #define	_S_NAME			"cn"
357c478bd9Sstevel@tonic-gate #define	_S_PORT			"ipserviceport"
367c478bd9Sstevel@tonic-gate #define	_S_PROTOCOL		"ipserviceprotocol"
377c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYNAME	"(&(objectClass=ipService)(cn=%s))"
387c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYNAME_SSD	"(&(%%s)(cn=%s))"
397c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYNAMEPROTO	\
407c478bd9Sstevel@tonic-gate 	"(&(objectClass=ipService)(cn=%s)(ipServiceProtocol=%s))"
417c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYNAMEPROTO_SSD	\
427c478bd9Sstevel@tonic-gate 	"(&(%%s)(cn=%s)(ipServiceProtocol=%s))"
437c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYPORT	"(&(objectClass=ipService)(ipServicePort=%ld))"
447c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYPORT_SSD	"(&(%%s)(ipServicePort=%ld))"
457c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYPORTPROTO	\
467c478bd9Sstevel@tonic-gate 	"(&(objectClass=ipService)(ipServicePort=%ld)(ipServiceProtocol=%s))"
477c478bd9Sstevel@tonic-gate #define	_F_GETSERVBYPORTPROTO_SSD	\
487c478bd9Sstevel@tonic-gate 	"(&(%%s)(ipServicePort=%ld)(ipServiceProtocol=%s))"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate typedef struct _nss_services_cookie {
517c478bd9Sstevel@tonic-gate 	int			index;	/* index of ipserviceprotocol */
527c478bd9Sstevel@tonic-gate 	char			*cname;	/* canonical name, don't free it */
537c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result;
547c478bd9Sstevel@tonic-gate } _nss_services_cookie_t;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static const char *services_attrs[] = {
577c478bd9Sstevel@tonic-gate 	_S_NAME,
587c478bd9Sstevel@tonic-gate 	_S_PORT,
597c478bd9Sstevel@tonic-gate 	_S_PROTOCOL,
607c478bd9Sstevel@tonic-gate 	(char *)NULL
617c478bd9Sstevel@tonic-gate };
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate void
647c478bd9Sstevel@tonic-gate _nss_services_cookie_free(void **ckP) {
657c478bd9Sstevel@tonic-gate 	_nss_services_cookie_t **cookieP = (_nss_services_cookie_t **)ckP;
667c478bd9Sstevel@tonic-gate 	if (cookieP && *cookieP) {
677c478bd9Sstevel@tonic-gate 		if ((*cookieP)->result)
687c478bd9Sstevel@tonic-gate 			(void) __ns_ldap_freeResult(&(*cookieP)->result);
697c478bd9Sstevel@tonic-gate 		free(*cookieP);
707c478bd9Sstevel@tonic-gate 		*cookieP = NULL;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static _nss_services_cookie_t *
757c478bd9Sstevel@tonic-gate _nss_services_cookie_new(ns_ldap_result_t *result, int index, char *cname) {
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	_nss_services_cookie_t	*cookie;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	if ((cookie = calloc(1, sizeof (*cookie))) == NULL)
807c478bd9Sstevel@tonic-gate 		return (NULL);
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * result has been allocated either by __ns_ldap_firstEntry
847c478bd9Sstevel@tonic-gate 	 * or __ns_ldap_nextEntry.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	cookie->result = result;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	cookie->index = index;
897c478bd9Sstevel@tonic-gate 	cookie->cname = cname;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	return (cookie);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate /*
94*cb5caa98Sdjl  * _nss_ldap_services2str is the data marshaling method for the services
957c478bd9Sstevel@tonic-gate  * getXbyY * (e.g., getbyname(), getbyport(), getent()) backend processes.
967c478bd9Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
97*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
98*cb5caa98Sdjl  * e.g.
99*cb5caa98Sdjl  *
100*cb5caa98Sdjl  * nfsd 2049/udp nfs
101*cb5caa98Sdjl  * nfsd 2049/tcp nfs
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * In section 5.5 of RFC 2307, it specifies that a "services" LDAP entry
1047c478bd9Sstevel@tonic-gate  * containing multiple ipserviceprotocol values should be able to be mapped
1057c478bd9Sstevel@tonic-gate  * to multiple "services" entities. Code has been added to support
1067c478bd9Sstevel@tonic-gate  * this one to many mapping feature.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static int
110*cb5caa98Sdjl _nss_ldap_services2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
1117c478bd9Sstevel@tonic-gate {
112*cb5caa98Sdjl 	uint_t		i, k;
1137c478bd9Sstevel@tonic-gate 	int		nss_result;
114*cb5caa98Sdjl 	int		buflen = 0, len;
115*cb5caa98Sdjl 	char		**ipport, *cname = NULL, *protoval = NULL;
116*cb5caa98Sdjl 	char		*buffer = NULL;
1177c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result;
118*cb5caa98Sdjl 	ns_ldap_attr_t	*names = NULL, *protocol = NULL;
1197c478bd9Sstevel@tonic-gate 	_nss_services_cookie_t	*cookie = (_nss_services_cookie_t *)
1207c478bd9Sstevel@tonic-gate 						be->services_cookie;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if (cookie) {
1237c478bd9Sstevel@tonic-gate 		/*
1247c478bd9Sstevel@tonic-gate 		 * getservent_r with multiple protocol values and the entry
1257c478bd9Sstevel@tonic-gate 		 * is enumerated 2nd time or beyond
1267c478bd9Sstevel@tonic-gate 		 */
1277c478bd9Sstevel@tonic-gate 		result =  cookie->result;
1287c478bd9Sstevel@tonic-gate 		cname = cookie->cname;
1297c478bd9Sstevel@tonic-gate 	} else {
1307c478bd9Sstevel@tonic-gate 		/*
1317c478bd9Sstevel@tonic-gate 		 * getservbyname_r, getservbyport_r or
1327c478bd9Sstevel@tonic-gate 		 * getservent_r with single protocol value or multiple values
1337c478bd9Sstevel@tonic-gate 		 * and the entry is enumerated 1st time
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		result = be->result;
1367c478bd9Sstevel@tonic-gate 	}
137*cb5caa98Sdjl 	if (result == NULL) {
138*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
139*cb5caa98Sdjl 		goto result_srvs2str;
140*cb5caa98Sdjl 	}
1417c478bd9Sstevel@tonic-gate 
142*cb5caa98Sdjl 	buflen = argp->buf.buflen;
143*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
144*cb5caa98Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
145*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
146*cb5caa98Sdjl 			goto result_srvs2str;
147*cb5caa98Sdjl 		}
148*cb5caa98Sdjl 		buffer = be->buffer;
149*cb5caa98Sdjl 	} else
150*cb5caa98Sdjl 		buffer = argp->buf.buffer;
151*cb5caa98Sdjl 
152*cb5caa98Sdjl 
153*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
1547c478bd9Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
1557c478bd9Sstevel@tonic-gate 
156*cb5caa98Sdjl 	/* Get services names */
157*cb5caa98Sdjl 	names = __ns_ldap_getAttrStruct(result->entry, _S_NAME);
158*cb5caa98Sdjl 	if (names == NULL || names->attrvalue == NULL) {
159*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
160*cb5caa98Sdjl 		goto result_srvs2str;
1617c478bd9Sstevel@tonic-gate 	}
162*cb5caa98Sdjl 	/* Get canonical services name */
1637c478bd9Sstevel@tonic-gate 	if (cname == NULL) {
164*cb5caa98Sdjl 	    cname = __s_api_get_canonical_name(result->entry, names, 1);
165*cb5caa98Sdjl 	    if (cname == NULL || (len = strlen(cname)) < 1) {
166*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
167*cb5caa98Sdjl 		goto result_srvs2str;
1687c478bd9Sstevel@tonic-gate 	    }
169*cb5caa98Sdjl 	}
170*cb5caa98Sdjl 	/* Get port */
171*cb5caa98Sdjl 	ipport = __ns_ldap_getAttr(result->entry, _S_PORT);
172*cb5caa98Sdjl 	if (ipport == NULL || ipport[0] == NULL ||
1737c478bd9Sstevel@tonic-gate 			(len = strlen(cname)) < 1) {
174*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
175*cb5caa98Sdjl 		goto result_srvs2str;
1767c478bd9Sstevel@tonic-gate 	}
177*cb5caa98Sdjl 	/* Set services name and port and '/' */
178*cb5caa98Sdjl 	len = snprintf(buffer, buflen, "%s %s/", cname, ipport[0]);
179*cb5caa98Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
1807c478bd9Sstevel@tonic-gate 
181*cb5caa98Sdjl 	/* Get protocol */
182*cb5caa98Sdjl 	protocol = __ns_ldap_getAttrStruct(result->entry, _S_PROTOCOL);
183*cb5caa98Sdjl 	if (protocol == NULL || protocol->attrvalue == NULL) {
184*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
185*cb5caa98Sdjl 		goto result_srvs2str;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (cookie) {
1897c478bd9Sstevel@tonic-gate 		/*
1907c478bd9Sstevel@tonic-gate 		 * getservent_r
1917c478bd9Sstevel@tonic-gate 		 * Get current value then increment index
1927c478bd9Sstevel@tonic-gate 		 */
193*cb5caa98Sdjl 		protoval = protocol->attrvalue[cookie->index++];
194*cb5caa98Sdjl 	} else if (protocol->value_count > 1 && be->setcalled == 0 &&
1957c478bd9Sstevel@tonic-gate 			argp->key.serv.proto) {
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * getserverbyname_r and getservbyport_r
1987c478bd9Sstevel@tonic-gate 		 *
1997c478bd9Sstevel@tonic-gate 		 * If there are more than one value and
2007c478bd9Sstevel@tonic-gate 		 * it needs to match protocol too,
2017c478bd9Sstevel@tonic-gate 		 * iterate each value to find matching one.
2027c478bd9Sstevel@tonic-gate 		 */
203*cb5caa98Sdjl 		for (k = 0; k < protocol->value_count; k++) {
204*cb5caa98Sdjl 			if (protocol->attrvalue[k] == NULL) {
205*cb5caa98Sdjl 				nss_result = NSS_STR_PARSE_PARSE;
206*cb5caa98Sdjl 				goto result_srvs2str;
2077c478bd9Sstevel@tonic-gate 			}
208*cb5caa98Sdjl 			if (strcmp(protocol->attrvalue[k],
2097c478bd9Sstevel@tonic-gate 				argp->key.serv.proto) == 0) {
210*cb5caa98Sdjl 				protoval = protocol->attrvalue[k];
2117c478bd9Sstevel@tonic-gate 				break;
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 	} else {
2157c478bd9Sstevel@tonic-gate 		/*
2167c478bd9Sstevel@tonic-gate 		 * 1. getserverbyname_r and getservbyport_r
2177c478bd9Sstevel@tonic-gate 		 *
2187c478bd9Sstevel@tonic-gate 		 * It does not need to match protocol or
2197c478bd9Sstevel@tonic-gate 		 * ipserviceprotocol has single value,
220*cb5caa98Sdjl 		 * return the first one.
2217c478bd9Sstevel@tonic-gate 		 *
222*cb5caa98Sdjl 		 * 2. getservent_r with single ipserviceprotocol value
2237c478bd9Sstevel@tonic-gate 		 * or multiple values and the entry is
224*cb5caa98Sdjl 		 * enumerated 1st time,  return the first one.
2257c478bd9Sstevel@tonic-gate 		 *
2267c478bd9Sstevel@tonic-gate 		 */
227*cb5caa98Sdjl 		protoval = protocol->attrvalue[0];
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (protoval == NULL || (len = strlen(protoval)) < 1) {
231*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
232*cb5caa98Sdjl 		goto result_srvs2str;
2337c478bd9Sstevel@tonic-gate 	}
234*cb5caa98Sdjl 
235*cb5caa98Sdjl 	/* Set protocol */
236*cb5caa98Sdjl 	len = snprintf(buffer, buflen, "%s", protoval);
237*cb5caa98Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
238*cb5caa98Sdjl 
239*cb5caa98Sdjl 	/* Append aliases */
240*cb5caa98Sdjl 	for (i = 0; i < names->value_count; i++) {
241*cb5caa98Sdjl 		if (names->attrvalue[i] == NULL) {
242*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
243*cb5caa98Sdjl 			goto result_srvs2str;
2447c478bd9Sstevel@tonic-gate 		}
245*cb5caa98Sdjl 		/* Skip the canonical name */
246*cb5caa98Sdjl 		if (strcmp(cname, names->attrvalue[i]) != 0) {
247*cb5caa98Sdjl 			len = snprintf(buffer, buflen, " %s",
248*cb5caa98Sdjl 					names->attrvalue[i]);
249*cb5caa98Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_srvs2str);
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 
253*cb5caa98Sdjl 
2547c478bd9Sstevel@tonic-gate 	if (be->enumcookie != NULL && cookie == NULL &&
2557c478bd9Sstevel@tonic-gate 			protocol->value_count > 1) {
2567c478bd9Sstevel@tonic-gate 		/*
2577c478bd9Sstevel@tonic-gate 		 * getservent_r with multiple ipserviceprotocol values
2587c478bd9Sstevel@tonic-gate 		 * and the entry is enumerated 1st time
2597c478bd9Sstevel@tonic-gate 		 *
2607c478bd9Sstevel@tonic-gate 		 * Create cookie and save result in the cookie
2617c478bd9Sstevel@tonic-gate 		 * "attrvalue[0]" of ipserviceprotocol is returned,
2627c478bd9Sstevel@tonic-gate 		 * so it starts with index 1. Also save the canonical name.
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		be->services_cookie =
2657c478bd9Sstevel@tonic-gate 			(void *)_nss_services_cookie_new(be->result, 1, cname);
2667c478bd9Sstevel@tonic-gate 		if (be->services_cookie == NULL) {
2677c478bd9Sstevel@tonic-gate 			nss_result = NSS_STR_PARSE_PARSE;
268*cb5caa98Sdjl 			goto result_srvs2str;
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		/* reset be->result so it won't get freed later */
2727c478bd9Sstevel@tonic-gate 		be->result = NULL;
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
275*cb5caa98Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
276*cb5caa98Sdjl 	if (argp->buf.result != NULL)
277*cb5caa98Sdjl 		be->buflen = strlen(be->buffer);
2787c478bd9Sstevel@tonic-gate 
279*cb5caa98Sdjl result_srvs2str:
2807c478bd9Sstevel@tonic-gate 	if (cookie) {
2817c478bd9Sstevel@tonic-gate 		/*
2827c478bd9Sstevel@tonic-gate 		 * getservent_r with multiple ipserviceprotocol values and
2837c478bd9Sstevel@tonic-gate 		 * the entry is enumerated 2nd time or beyond
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if (nss_result != NSS_STR_PARSE_SUCCESS ||
2867c478bd9Sstevel@tonic-gate 			cookie->index >= protocol->value_count) {
2877c478bd9Sstevel@tonic-gate 			/*
2887c478bd9Sstevel@tonic-gate 			 * If it's an error case or it has iterated all
2897c478bd9Sstevel@tonic-gate 			 * ipservicesprotocol value(s) then free cookie and
2907c478bd9Sstevel@tonic-gate 			 * set it to NULL
2917c478bd9Sstevel@tonic-gate 			 *
2927c478bd9Sstevel@tonic-gate 			 */
2937c478bd9Sstevel@tonic-gate 			_nss_services_cookie_free(
2947c478bd9Sstevel@tonic-gate 				(void **)&be->services_cookie);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	} else {
2977c478bd9Sstevel@tonic-gate 		/*
2987c478bd9Sstevel@tonic-gate 		 * getservbyname_r, getservbyport_r, or
2997c478bd9Sstevel@tonic-gate 		 * getservent_r with single value or can't create cookie
3007c478bd9Sstevel@tonic-gate 		 */
3017c478bd9Sstevel@tonic-gate 		(void) __ns_ldap_freeResult(&be->result);
3027c478bd9Sstevel@tonic-gate 	}
303*cb5caa98Sdjl 	return (nss_result);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * getbyname gets struct servent values by service name. This
3087c478bd9Sstevel@tonic-gate  * function constructs an ldap search filter using the service
3097c478bd9Sstevel@tonic-gate  * name invocation parameter and the getservbyname search filter
3107c478bd9Sstevel@tonic-gate  * defined. Once the filter is constructed, we search for a matching
3117c478bd9Sstevel@tonic-gate  * entry and marshal the data results into *serv = (struct servent *)
3127c478bd9Sstevel@tonic-gate  * argp->buf.result. The function _nss_ldap_services2ent performs
3137c478bd9Sstevel@tonic-gate  * the data marshaling.
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate static nss_status_t
3177c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
3207c478bd9Sstevel@tonic-gate 	const char	*proto = argp->key.serv.proto;
3217c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
3227c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
3237c478bd9Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
3247c478bd9Sstevel@tonic-gate 	char		protocol[SEARCHFILTERLEN];
3257c478bd9Sstevel@tonic-gate 	int		ret;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.serv.serv.name, sizeof (name))
3287c478bd9Sstevel@tonic-gate 			!= 0)
3297c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	if (proto == NULL) {
3327c478bd9Sstevel@tonic-gate 		ret = snprintf(searchfilter, sizeof (searchfilter),
3337c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYNAME, name);
3347c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (searchfilter) || ret < 0)
3357c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		ret = snprintf(userdata, sizeof (userdata),
3387c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYNAME_SSD, name);
3397c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (userdata) || ret < 0)
3407c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3417c478bd9Sstevel@tonic-gate 	} else {
3427c478bd9Sstevel@tonic-gate 		if (_ldap_filter_name(protocol, proto, sizeof (protocol)) != 0)
3437c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		ret = snprintf(searchfilter, sizeof (searchfilter),
3467c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYNAMEPROTO, name, protocol);
3477c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (searchfilter) || ret < 0)
3487c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		ret = snprintf(userdata, sizeof (userdata),
3517c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYNAMEPROTO_SSD, name, protocol);
3527c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (userdata) || ret < 0)
3537c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
3577c478bd9Sstevel@tonic-gate 		_SERVICES, searchfilter, NULL,
3587c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /*
3637c478bd9Sstevel@tonic-gate  * getbyport gets struct servent values by service port. This
3647c478bd9Sstevel@tonic-gate  * function constructs an ldap search filter using the service
3657c478bd9Sstevel@tonic-gate  * name invocation parameter and the getservbyport search filter
3667c478bd9Sstevel@tonic-gate  * defined. Once the filter is constructed, we search for a matching
3677c478bd9Sstevel@tonic-gate  * entry and marshal the data results into *serv = (struct servent *)
3687c478bd9Sstevel@tonic-gate  * argp->buf.result. The function _nss_ldap_services2ent performs
3697c478bd9Sstevel@tonic-gate  * the data marshaling.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate static nss_status_t
3737c478bd9Sstevel@tonic-gate getbyport(ldap_backend_ptr be, void *a)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
3767c478bd9Sstevel@tonic-gate 	const char	*proto = argp->key.serv.proto;
3777c478bd9Sstevel@tonic-gate 	char		portstr[12];
3787c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
3797c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
3807c478bd9Sstevel@tonic-gate 	char		protocol[SEARCHFILTERLEN];
3817c478bd9Sstevel@tonic-gate 	int		ret;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	ret = snprintf(portstr, sizeof (portstr), " %d",
3847c478bd9Sstevel@tonic-gate 	    ntohs((ushort_t)argp->key.serv.serv.port));
3857c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (portstr) || ret < 0)
3867c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (proto == NULL) {
3897c478bd9Sstevel@tonic-gate 		ret = snprintf(searchfilter, sizeof (searchfilter),
3907c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYPORT, strtol(portstr, (char **)NULL, 10));
3917c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (searchfilter) || ret < 0)
3927c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		ret = snprintf(userdata, sizeof (userdata),
3957c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYPORT_SSD, strtol(portstr, (char **)NULL, 10));
3967c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (userdata) || ret < 0)
3977c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
3987c478bd9Sstevel@tonic-gate 	} else {
3997c478bd9Sstevel@tonic-gate 		if (_ldap_filter_name(protocol, proto, sizeof (protocol)) != 0)
4007c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		ret = snprintf(searchfilter, sizeof (searchfilter),
4037c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYPORTPROTO,
4047c478bd9Sstevel@tonic-gate 		    strtol(portstr, (char **)NULL, 10), protocol);
4057c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (searchfilter) || ret < 0)
4067c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		ret = snprintf(userdata, sizeof (userdata),
4097c478bd9Sstevel@tonic-gate 		    _F_GETSERVBYPORTPROTO_SSD,
4107c478bd9Sstevel@tonic-gate 		    strtol(portstr, (char **)NULL, 10), protocol);
4117c478bd9Sstevel@tonic-gate 		if (ret >= sizeof (userdata) || ret < 0)
4127c478bd9Sstevel@tonic-gate 			return ((nss_status_t)NSS_NOTFOUND);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
4167c478bd9Sstevel@tonic-gate 		_SERVICES, searchfilter, NULL,
4177c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate static ldap_backend_op_t serv_ops[] = {
4217c478bd9Sstevel@tonic-gate     _nss_ldap_destr,
4227c478bd9Sstevel@tonic-gate     _nss_ldap_endent,
4237c478bd9Sstevel@tonic-gate     _nss_ldap_setent,
4247c478bd9Sstevel@tonic-gate     _nss_ldap_getent,
4257c478bd9Sstevel@tonic-gate     getbyname,
4267c478bd9Sstevel@tonic-gate     getbyport
4277c478bd9Sstevel@tonic-gate };
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * _nss_ldap_services_constr is where life begins. This function calls
4327c478bd9Sstevel@tonic-gate  * the generic ldap constructor function to define and build the
4337c478bd9Sstevel@tonic-gate  * abstract data types required to support ldap operations.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
4377c478bd9Sstevel@tonic-gate nss_backend_t *
4387c478bd9Sstevel@tonic-gate _nss_ldap_services_constr(const char *dummy1, const char *dummy2,
4397c478bd9Sstevel@tonic-gate 			const char *dummy3)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(serv_ops,
4437c478bd9Sstevel@tonic-gate 		sizeof (serv_ops)/sizeof (serv_ops[0]), _SERVICES,
444*cb5caa98Sdjl 		services_attrs, _nss_ldap_services2str));
4457c478bd9Sstevel@tonic-gate }
446