xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getrpcent.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 <rpc/rpcent.h>
290Sstevel@tonic-gate #include "ns_internal.h"
300Sstevel@tonic-gate #include "ldap_common.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /* rpc attributes filters */
330Sstevel@tonic-gate #define	_R_NAME			"cn"
340Sstevel@tonic-gate #define	_R_NUMBER		"oncrpcnumber"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	_F_GETRPCBYNAME		"(&(objectClass=oncRpc)(cn=%s))"
380Sstevel@tonic-gate #define	_F_GETRPCBYNAME_SSD	"(&(%%s)(cn=%s))"
390Sstevel@tonic-gate #define	_F_GETRPCBYNUMBER	"(&(objectClass=oncRpc)(oncRpcNumber=%d))"
400Sstevel@tonic-gate #define	_F_GETRPCBYNUMBER_SSD	"(&(%%s)(oncRpcNumber=%d))"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static const char *rpc_attrs[] = {
430Sstevel@tonic-gate 	_R_NAME,
440Sstevel@tonic-gate 	_R_NUMBER,
450Sstevel@tonic-gate 	(char *)NULL
460Sstevel@tonic-gate };
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
49*2830Sdjl  * _nss_ldap_rpc2str is the data marshaling method for the rpc getXbyY
500Sstevel@tonic-gate  * (e.g., getbyname(), getbynumber(), getrpcent()) backend processes.
510Sstevel@tonic-gate  * This method is called after a successful ldap search has been performed.
52*2830Sdjl  * This method will parse the ldap search values into the file format.
53*2830Sdjl  * e.g.
54*2830Sdjl  *
55*2830Sdjl  * nfs_acl 100227
56*2830Sdjl  * snmp 100122  na.snmp snmp-cmc snmp-synoptics snmp-unisys snmp-utk
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static int
_nss_ldap_rpc2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)59*2830Sdjl _nss_ldap_rpc2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
600Sstevel@tonic-gate {
61*2830Sdjl 	uint_t		i;
620Sstevel@tonic-gate 	int		nss_result;
63*2830Sdjl 	int		buflen = 0, len;
64*2830Sdjl 	char		*cname = NULL;
65*2830Sdjl 	char		*buffer = NULL;
660Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
67*2830Sdjl 	ns_ldap_attr_t	*names;
68*2830Sdjl 	char		**rpcnumber;
690Sstevel@tonic-gate 
70*2830Sdjl 	if (result == NULL)
71*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
72*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
730Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
740Sstevel@tonic-gate 
75*2830Sdjl 	buflen = argp->buf.buflen;
76*2830Sdjl 	if (argp->buf.result != NULL) {
77*2830Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
78*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
79*2830Sdjl 			goto result_rpc2str;
800Sstevel@tonic-gate 		}
81*2830Sdjl 		buffer = be->buffer;
82*2830Sdjl 	} else
83*2830Sdjl 		buffer = argp->buf.buffer;
84*2830Sdjl 
85*2830Sdjl 
86*2830Sdjl 	names = __ns_ldap_getAttrStruct(result->entry, _R_NAME);
87*2830Sdjl 	if (names == NULL || names->attrvalue == NULL) {
88*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
89*2830Sdjl 		goto result_rpc2str;
90*2830Sdjl 	}
91*2830Sdjl 	/* Get the canonical rpc name */
92*2830Sdjl 	cname = __s_api_get_canonical_name(result->entry, names, 1);
93*2830Sdjl 	if (cname == NULL || (len = strlen(cname)) < 1) {
94*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
95*2830Sdjl 		goto result_rpc2str;
96*2830Sdjl 	}
97*2830Sdjl 	rpcnumber = __ns_ldap_getAttr(result->entry, _R_NUMBER);
98*2830Sdjl 	if (rpcnumber == NULL || rpcnumber[0] == NULL ||
99*2830Sdjl 			(len = strlen(rpcnumber[0])) < 1) {
100*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
101*2830Sdjl 		goto result_rpc2str;
102*2830Sdjl 	}
103*2830Sdjl 	len = snprintf(buffer, buflen,  "%s %s", cname, rpcnumber[0]);
104*2830Sdjl 	TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str);
105*2830Sdjl 	/* Append aliases */
106*2830Sdjl 	for (i = 0; i < names->value_count; i++) {
107*2830Sdjl 		if (names->attrvalue[i] == NULL) {
108*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
109*2830Sdjl 			goto result_rpc2str;
1100Sstevel@tonic-gate 		}
111*2830Sdjl 		/* Skip the canonical name */
112*2830Sdjl 		if (strcasecmp(names->attrvalue[i], cname) != 0) {
113*2830Sdjl 			len = snprintf(buffer, buflen,  " %s",
114*2830Sdjl 					names->attrvalue[i]);
115*2830Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str);
1160Sstevel@tonic-gate 		}
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
119*2830Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
120*2830Sdjl 	if (argp->buf.result != NULL)
121*2830Sdjl 		be->buflen = strlen(be->buffer);
1220Sstevel@tonic-gate 
123*2830Sdjl result_rpc2str:
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
126*2830Sdjl 	return (nss_result);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * getbyname gets struct rpcent values by rpc name. This function
1310Sstevel@tonic-gate  * constructs an ldap search filter using the rpc name invocation
1320Sstevel@tonic-gate  * parameter and the getrpcbyname search filter defined. Once the
1330Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
1340Sstevel@tonic-gate  * the data results into *rpc = (struct rpcent *)argp->buf.result.
1350Sstevel@tonic-gate  * The function _nss_ldap_rpc2ent performs the data marshaling.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1390Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1420Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1430Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1440Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
1450Sstevel@tonic-gate 	int		ret;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
1480Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETRPCBYNAME,
1510Sstevel@tonic-gate 	    name);
1520Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1530Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata), _F_GETRPCBYNAME_SSD, name);
1560Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1570Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter,
1600Sstevel@tonic-gate 		NULL, _merge_SSD_filter, userdata));
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate  * getbynumber gets struct rpcent values by rpc number. This function
1660Sstevel@tonic-gate  * constructs an ldap search filter using the rpc number invocation
1670Sstevel@tonic-gate  * parameter and the getrpcbynumber search filter defined. Once the
1680Sstevel@tonic-gate  * filter is constructed, we search for a matching entry and marshal
1690Sstevel@tonic-gate  * the data results into *rpc = (struct rpcent *)argp->buf.result.
1700Sstevel@tonic-gate  * The function _nss_ldap_rpc2ent performs the data marshaling.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static nss_status_t
getbynumber(ldap_backend_ptr be,void * a)1740Sstevel@tonic-gate getbynumber(ldap_backend_ptr be, void *a)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1770Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1780Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1790Sstevel@tonic-gate 	int		ret;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1820Sstevel@tonic-gate 	    _F_GETRPCBYNUMBER, argp->key.number);
1830Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1840Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1870Sstevel@tonic-gate 	    _F_GETRPCBYNUMBER_SSD, argp->key.number);
1880Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1890Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter,
1920Sstevel@tonic-gate 		NULL, _merge_SSD_filter, userdata));
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static ldap_backend_op_t rpc_ops[] = {
1970Sstevel@tonic-gate 	_nss_ldap_destr,
1980Sstevel@tonic-gate 	_nss_ldap_endent,
1990Sstevel@tonic-gate 	_nss_ldap_setent,
2000Sstevel@tonic-gate 	_nss_ldap_getent,
2010Sstevel@tonic-gate 	getbyname,
2020Sstevel@tonic-gate 	getbynumber
2030Sstevel@tonic-gate };
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * _nss_ldap_rpc_constr is where life begins. This function calls the generic
2080Sstevel@tonic-gate  * ldap constructor function to define and build the abstract data types
2090Sstevel@tonic-gate  * required to support ldap operations.
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*ARGSUSED0*/
2130Sstevel@tonic-gate nss_backend_t *
_nss_ldap_rpc_constr(const char * dummy1,const char * dummy2,const char * dummy3)2140Sstevel@tonic-gate _nss_ldap_rpc_constr(const char *dummy1, const char *dummy2,
2150Sstevel@tonic-gate 			const char *dummy3)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(rpc_ops,
2190Sstevel@tonic-gate 		sizeof (rpc_ops)/sizeof (rpc_ops[0]),
220*2830Sdjl 		_RPC, rpc_attrs, _nss_ldap_rpc2str));
2210Sstevel@tonic-gate }
222