1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28#include <rpc/rpcent.h>
29#include "ns_internal.h"
30#include "ldap_common.h"
31
32/* rpc attributes filters */
33#define	_R_NAME			"cn"
34#define	_R_NUMBER		"oncrpcnumber"
35
36
37#define	_F_GETRPCBYNAME		"(&(objectClass=oncRpc)(cn=%s))"
38#define	_F_GETRPCBYNAME_SSD	"(&(%%s)(cn=%s))"
39#define	_F_GETRPCBYNUMBER	"(&(objectClass=oncRpc)(oncRpcNumber=%d))"
40#define	_F_GETRPCBYNUMBER_SSD	"(&(%%s)(oncRpcNumber=%d))"
41
42static const char *rpc_attrs[] = {
43	_R_NAME,
44	_R_NUMBER,
45	(char *)NULL
46};
47
48/*
49 * _nss_ldap_rpc2str is the data marshaling method for the rpc getXbyY
50 * (e.g., getbyname(), getbynumber(), getrpcent()) backend processes.
51 * This method is called after a successful ldap search has been performed.
52 * This method will parse the ldap search values into the file format.
53 * e.g.
54 *
55 * nfs_acl 100227
56 * snmp 100122  na.snmp snmp-cmc snmp-synoptics snmp-unisys snmp-utk
57 */
58static int
59_nss_ldap_rpc2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
60{
61	uint_t		i;
62	int		nss_result;
63	int		buflen = 0, len;
64	char		*cname = NULL;
65	char		*buffer = NULL;
66	ns_ldap_result_t	*result = be->result;
67	ns_ldap_attr_t	*names;
68	char		**rpcnumber;
69
70	if (result == NULL)
71		return (NSS_STR_PARSE_PARSE);
72	nss_result = NSS_STR_PARSE_SUCCESS;
73	(void) memset(argp->buf.buffer, 0, buflen);
74
75	buflen = argp->buf.buflen;
76	if (argp->buf.result != NULL) {
77		if ((be->buffer = calloc(1, buflen)) == NULL) {
78			nss_result = NSS_STR_PARSE_PARSE;
79			goto result_rpc2str;
80		}
81		buffer = be->buffer;
82	} else
83		buffer = argp->buf.buffer;
84
85
86	names = __ns_ldap_getAttrStruct(result->entry, _R_NAME);
87	if (names == NULL || names->attrvalue == NULL) {
88		nss_result = NSS_STR_PARSE_PARSE;
89		goto result_rpc2str;
90	}
91	/* Get the canonical rpc name */
92	cname = __s_api_get_canonical_name(result->entry, names, 1);
93	if (cname == NULL || (len = strlen(cname)) < 1) {
94		nss_result = NSS_STR_PARSE_PARSE;
95		goto result_rpc2str;
96	}
97	rpcnumber = __ns_ldap_getAttr(result->entry, _R_NUMBER);
98	if (rpcnumber == NULL || rpcnumber[0] == NULL ||
99			(len = strlen(rpcnumber[0])) < 1) {
100		nss_result = NSS_STR_PARSE_PARSE;
101		goto result_rpc2str;
102	}
103	len = snprintf(buffer, buflen,  "%s %s", cname, rpcnumber[0]);
104	TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str);
105	/* Append aliases */
106	for (i = 0; i < names->value_count; i++) {
107		if (names->attrvalue[i] == NULL) {
108			nss_result = NSS_STR_PARSE_PARSE;
109			goto result_rpc2str;
110		}
111		/* Skip the canonical name */
112		if (strcasecmp(names->attrvalue[i], cname) != 0) {
113			len = snprintf(buffer, buflen,  " %s",
114					names->attrvalue[i]);
115			TEST_AND_ADJUST(len, buffer, buflen, result_rpc2str);
116		}
117	}
118
119	/* The front end marshaller doesn't need to copy trailing nulls */
120	if (argp->buf.result != NULL)
121		be->buflen = strlen(be->buffer);
122
123result_rpc2str:
124
125	(void) __ns_ldap_freeResult(&be->result);
126	return (nss_result);
127}
128
129/*
130 * getbyname gets struct rpcent values by rpc name. This function
131 * constructs an ldap search filter using the rpc name invocation
132 * parameter and the getrpcbyname search filter defined. Once the
133 * filter is constructed, we search for a matching entry and marshal
134 * the data results into *rpc = (struct rpcent *)argp->buf.result.
135 * The function _nss_ldap_rpc2ent performs the data marshaling.
136 */
137
138static nss_status_t
139getbyname(ldap_backend_ptr be, void *a)
140{
141	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
142	char		searchfilter[SEARCHFILTERLEN];
143	char		userdata[SEARCHFILTERLEN];
144	char		name[SEARCHFILTERLEN];
145	int		ret;
146
147	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
148		return ((nss_status_t)NSS_NOTFOUND);
149
150	ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETRPCBYNAME,
151	    name);
152	if (ret >= sizeof (searchfilter) || ret < 0)
153		return ((nss_status_t)NSS_NOTFOUND);
154
155	ret = snprintf(userdata, sizeof (userdata), _F_GETRPCBYNAME_SSD, name);
156	if (ret >= sizeof (userdata) || ret < 0)
157		return ((nss_status_t)NSS_NOTFOUND);
158
159	return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter,
160		NULL, _merge_SSD_filter, userdata));
161}
162
163
164/*
165 * getbynumber gets struct rpcent values by rpc number. This function
166 * constructs an ldap search filter using the rpc number invocation
167 * parameter and the getrpcbynumber search filter defined. Once the
168 * filter is constructed, we search for a matching entry and marshal
169 * the data results into *rpc = (struct rpcent *)argp->buf.result.
170 * The function _nss_ldap_rpc2ent performs the data marshaling.
171 */
172
173static nss_status_t
174getbynumber(ldap_backend_ptr be, void *a)
175{
176	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
177	char		searchfilter[SEARCHFILTERLEN];
178	char		userdata[SEARCHFILTERLEN];
179	int		ret;
180
181	ret = snprintf(searchfilter, sizeof (searchfilter),
182	    _F_GETRPCBYNUMBER, argp->key.number);
183	if (ret >= sizeof (searchfilter) || ret < 0)
184		return ((nss_status_t)NSS_NOTFOUND);
185
186	ret = snprintf(userdata, sizeof (userdata),
187	    _F_GETRPCBYNUMBER_SSD, argp->key.number);
188	if (ret >= sizeof (userdata) || ret < 0)
189		return ((nss_status_t)NSS_NOTFOUND);
190
191	return ((nss_status_t)_nss_ldap_lookup(be, argp, _RPC, searchfilter,
192		NULL, _merge_SSD_filter, userdata));
193}
194
195
196static ldap_backend_op_t rpc_ops[] = {
197	_nss_ldap_destr,
198	_nss_ldap_endent,
199	_nss_ldap_setent,
200	_nss_ldap_getent,
201	getbyname,
202	getbynumber
203};
204
205
206/*
207 * _nss_ldap_rpc_constr is where life begins. This function calls the generic
208 * ldap constructor function to define and build the abstract data types
209 * required to support ldap operations.
210 */
211
212/*ARGSUSED0*/
213nss_backend_t *
214_nss_ldap_rpc_constr(const char *dummy1, const char *dummy2,
215			const char *dummy3)
216{
217
218	return ((nss_backend_t *)_nss_ldap_constr(rpc_ops,
219		sizeof (rpc_ops)/sizeof (rpc_ops[0]),
220		_RPC, rpc_attrs, _nss_ldap_rpc2str));
221}
222