xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/tsol_gettpent.c (revision 1676:37f4a3e2bd99)
1*1676Sjpk /*
2*1676Sjpk  * CDDL HEADER START
3*1676Sjpk  *
4*1676Sjpk  * The contents of this file are subject to the terms of the
5*1676Sjpk  * Common Development and Distribution License (the "License").
6*1676Sjpk  * You may not use this file except in compliance with the License.
7*1676Sjpk  *
8*1676Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1676Sjpk  * or http://www.opensolaris.org/os/licensing.
10*1676Sjpk  * See the License for the specific language governing permissions
11*1676Sjpk  * and limitations under the License.
12*1676Sjpk  *
13*1676Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
14*1676Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1676Sjpk  * If applicable, add the following below this CDDL HEADER, with the
16*1676Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
17*1676Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1676Sjpk  *
19*1676Sjpk  * CDDL HEADER END
20*1676Sjpk  */
21*1676Sjpk /*
22*1676Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*1676Sjpk  * Use is subject to license terms.
24*1676Sjpk  */
25*1676Sjpk 
26*1676Sjpk #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*1676Sjpk 
28*1676Sjpk #include "ldap_common.h"
29*1676Sjpk #include <sys/tsol/tndb.h>
30*1676Sjpk 
31*1676Sjpk /* tnrhtp attributes filters */
32*1676Sjpk #define	_TNRHTP_NAME		"ipTnetTemplateName"
33*1676Sjpk #define	_TNRHTP_ATTRS		"SolarisAttrKeyValue"
34*1676Sjpk #define	_F_GETTNTPBYNAME	"(&(objectClass=ipTnetTemplate)"\
35*1676Sjpk 				"(ipTnetTemplateName=%s))"
36*1676Sjpk #define	_F_GETTNTPBYNAME_SSD	"(&(%%s)(ipTnetTemplateName=%s))"
37*1676Sjpk 
38*1676Sjpk static const char *tnrhtp_attrs[] = {
39*1676Sjpk 	_TNRHTP_NAME,
40*1676Sjpk 	_TNRHTP_ATTRS,
41*1676Sjpk 	NULL
42*1676Sjpk };
43*1676Sjpk 
44*1676Sjpk static int
45*1676Sjpk _nss_ldap_tnrhtp2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
46*1676Sjpk {
47*1676Sjpk 	int			i, nss_result;
48*1676Sjpk 	int			len = 0;
49*1676Sjpk 	int			buflen = 0;
50*1676Sjpk 	char			*buffer = NULL;
51*1676Sjpk 	char			*ceiling = NULL;
52*1676Sjpk 	ns_ldap_attr_t		*attrptr;
53*1676Sjpk 	ns_ldap_result_t	*result = be->result;
54*1676Sjpk 	tsol_tpstr_t		*tpstrp;
55*1676Sjpk 
56*1676Sjpk 	buffer = argp->buf.buffer;
57*1676Sjpk 	buflen = argp->buf.buflen;
58*1676Sjpk 	if (argp->buf.result == NULL) {
59*1676Sjpk 		nss_result = (int)NSS_STR_PARSE_ERANGE;
60*1676Sjpk 		goto result_tnrhtp2ent;
61*1676Sjpk 	}
62*1676Sjpk 	tpstrp = (tsol_tpstr_t *)(argp->buf.result);
63*1676Sjpk 	tpstrp->template = tpstrp->attrs = NULL;
64*1676Sjpk 	ceiling = buffer + buflen;
65*1676Sjpk 	(void) memset(argp->buf.buffer, 0, buflen);
66*1676Sjpk 	attrptr = getattr(result, 0);
67*1676Sjpk 	if (attrptr == NULL) {
68*1676Sjpk 		nss_result = NSS_STR_PARSE_PARSE;
69*1676Sjpk 		goto result_tnrhtp2ent;
70*1676Sjpk 	}
71*1676Sjpk 	for (i = 0; i < result->entry->attr_count; i++) {
72*1676Sjpk 		attrptr = getattr(result, i);
73*1676Sjpk 		if (attrptr == NULL) {
74*1676Sjpk 			nss_result = (int)NSS_STR_PARSE_PARSE;
75*1676Sjpk 			goto result_tnrhtp2ent;
76*1676Sjpk 		}
77*1676Sjpk #ifdef	DEBUG
78*1676Sjpk 		(void) fprintf(stdout,
79*1676Sjpk 		    "\n[tsol_gettpent.c: _nss_ldap_tnrhtp2ent %d]\n", i);
80*1676Sjpk 		(void) fprintf(stdout, "      entry value count %d: %s:%s\n",
81*1676Sjpk 		    attrptr->value_count,
82*1676Sjpk 		    attrptr->attrname ? attrptr->attrname : "NULL",
83*1676Sjpk 		    attrptr->attrvalue[0] ? attrptr->attrvalue[0] : "NULL");
84*1676Sjpk #endif	/* DEBUG */
85*1676Sjpk 		if (strcasecmp(attrptr->attrname, _TNRHTP_NAME) == 0) {
86*1676Sjpk 			len = strlen(attrptr->attrvalue[0]);
87*1676Sjpk 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
88*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_PARSE;
89*1676Sjpk 				goto result_tnrhtp2ent;
90*1676Sjpk 			}
91*1676Sjpk 			tpstrp->template = buffer;
92*1676Sjpk 			buffer += len + 1;
93*1676Sjpk 			if (buffer >= ceiling) {
94*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_ERANGE;
95*1676Sjpk 				goto result_tnrhtp2ent;
96*1676Sjpk 			}
97*1676Sjpk 			(void) strcpy(tpstrp->template, attrptr->attrvalue[0]);
98*1676Sjpk 			continue;
99*1676Sjpk 		}
100*1676Sjpk 		if (strcasecmp(attrptr->attrname, _TNRHTP_ATTRS) == 0) {
101*1676Sjpk 			len = strlen(attrptr->attrvalue[0]);
102*1676Sjpk 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
103*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_PARSE;
104*1676Sjpk 				goto result_tnrhtp2ent;
105*1676Sjpk 			}
106*1676Sjpk 			tpstrp->attrs = buffer;
107*1676Sjpk 			buffer += len + 1;
108*1676Sjpk 			if (buffer >= ceiling) {
109*1676Sjpk 				nss_result = (int)NSS_STR_PARSE_PARSE;
110*1676Sjpk 				goto result_tnrhtp2ent;
111*1676Sjpk 			}
112*1676Sjpk 			(void) strcpy(tpstrp->attrs, attrptr->attrvalue[0]);
113*1676Sjpk 			continue;
114*1676Sjpk 		}
115*1676Sjpk 	}
116*1676Sjpk 	if (tpstrp->attrs == NULL)
117*1676Sjpk 		nss_result = NSS_STR_PARSE_PARSE;
118*1676Sjpk 	else
119*1676Sjpk 		nss_result = NSS_STR_PARSE_SUCCESS;
120*1676Sjpk 
121*1676Sjpk #ifdef	DEBUG
122*1676Sjpk 	(void) fprintf(stdout, "\n[tsol_gettpent.c: _nss_ldap_tnrhtp2ent]\n");
123*1676Sjpk 	(void) fprintf(stdout, "      template: [%s]\n",
124*1676Sjpk 	    tpstrp->template ? tpstrp->template : "NULL");
125*1676Sjpk 	(void) fprintf(stdout, "      attrs: [%s]\n",
126*1676Sjpk 	    tpstrp->attrs ? tpstrp->attrs : "NULL");
127*1676Sjpk #endif	/* DEBUG */
128*1676Sjpk 
129*1676Sjpk result_tnrhtp2ent:
130*1676Sjpk 	(void) __ns_ldap_freeResult(&be->result);
131*1676Sjpk 	return (nss_result);
132*1676Sjpk }
133*1676Sjpk 
134*1676Sjpk 
135*1676Sjpk static nss_status_t
136*1676Sjpk getbyname(ldap_backend_ptr be, void *a)
137*1676Sjpk {
138*1676Sjpk 	char		searchfilter[SEARCHFILTERLEN];
139*1676Sjpk 	char		userdata[SEARCHFILTERLEN];
140*1676Sjpk 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
141*1676Sjpk 
142*1676Sjpk #ifdef	DEBUG
143*1676Sjpk 	(void) fprintf(stdout, "\n[tsol_gettpent.c: getbyname]\n");
144*1676Sjpk #endif	/* DEBUG */
145*1676Sjpk 
146*1676Sjpk 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETTNTPBYNAME,
147*1676Sjpk 	    argp->key.name) < 0)
148*1676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
149*1676Sjpk 
150*1676Sjpk 	if (snprintf(userdata, sizeof (userdata), _F_GETTNTPBYNAME_SSD,
151*1676Sjpk 	    argp->key.name) < 0)
152*1676Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
153*1676Sjpk 
154*1676Sjpk 	return (_nss_ldap_lookup(be, argp, _TNRHTP, searchfilter, NULL,
155*1676Sjpk 	    _merge_SSD_filter, userdata));
156*1676Sjpk }
157*1676Sjpk 
158*1676Sjpk 
159*1676Sjpk static ldap_backend_op_t tnrhtp_ops[] = {
160*1676Sjpk 	_nss_ldap_destr,
161*1676Sjpk 	_nss_ldap_endent,
162*1676Sjpk 	_nss_ldap_setent,
163*1676Sjpk 	_nss_ldap_getent,
164*1676Sjpk 	getbyname
165*1676Sjpk };
166*1676Sjpk 
167*1676Sjpk 
168*1676Sjpk nss_backend_t *
169*1676Sjpk _nss_ldap_tnrhtp_constr(const char *dummy1,
170*1676Sjpk     const char *dummy2,
171*1676Sjpk     const char *dummy3,
172*1676Sjpk     const char *dummy4,
173*1676Sjpk     const char *dummy5)
174*1676Sjpk {
175*1676Sjpk #ifdef	DEBUG
176*1676Sjpk 	(void) fprintf(stdout,
177*1676Sjpk 	    "\n[gettnrhtpattr.c: _nss_ldap_tnrhtp_constr]\n");
178*1676Sjpk #endif
179*1676Sjpk 	return ((nss_backend_t *)_nss_ldap_constr(tnrhtp_ops,
180*1676Sjpk 		sizeof (tnrhtp_ops)/sizeof (tnrhtp_ops[0]), _TNRHTP,
181*1676Sjpk 		tnrhtp_attrs, _nss_ldap_tnrhtp2ent));
182*1676Sjpk }
183