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