145916cd2Sjpk /*
245916cd2Sjpk  * CDDL HEADER START
345916cd2Sjpk  *
445916cd2Sjpk  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
745916cd2Sjpk  *
845916cd2Sjpk  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945916cd2Sjpk  * or http://www.opensolaris.org/os/licensing.
1045916cd2Sjpk  * See the License for the specific language governing permissions
1145916cd2Sjpk  * and limitations under the License.
1245916cd2Sjpk  *
1345916cd2Sjpk  * When distributing Covered Code, include this CDDL HEADER in each
1445916cd2Sjpk  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545916cd2Sjpk  * If applicable, add the following below this CDDL HEADER, with the
1645916cd2Sjpk  * fields enclosed by brackets "[]" replaced with your own identifying
1745916cd2Sjpk  * information: Portions Copyright [yyyy] [name of copyright owner]
1845916cd2Sjpk  *
1945916cd2Sjpk  * CDDL HEADER END
2045916cd2Sjpk  */
2145916cd2Sjpk /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2345916cd2Sjpk  * Use is subject to license terms.
2445916cd2Sjpk  */
2545916cd2Sjpk 
2645916cd2Sjpk #include "ldap_common.h"
2745916cd2Sjpk #include <sys/tsol/tndb.h>
2845916cd2Sjpk 
2945916cd2Sjpk /* tnrhtp attributes filters */
3045916cd2Sjpk #define	_TNRHTP_NAME		"ipTnetTemplateName"
3145916cd2Sjpk #define	_TNRHTP_ATTRS		"SolarisAttrKeyValue"
3245916cd2Sjpk #define	_F_GETTNTPBYNAME	"(&(objectClass=ipTnetTemplate)"\
33*cb5caa98Sdjl 				"(!(objectClass=ipTnetHost))" \
3445916cd2Sjpk 				"(ipTnetTemplateName=%s))"
3545916cd2Sjpk #define	_F_GETTNTPBYNAME_SSD	"(&(%%s)(ipTnetTemplateName=%s))"
3645916cd2Sjpk 
3745916cd2Sjpk static const char *tnrhtp_attrs[] = {
3845916cd2Sjpk 	_TNRHTP_NAME,
3945916cd2Sjpk 	_TNRHTP_ATTRS,
4045916cd2Sjpk 	NULL
4145916cd2Sjpk };
4245916cd2Sjpk 
43*cb5caa98Sdjl /*
44*cb5caa98Sdjl  * _nss_ldap_tnrhtp2str is the data marshaling method for the tnrhtp
45*cb5caa98Sdjl  * (tsol_gettpbyaddr()/tsol_gettpent()) backend processes.
46*cb5caa98Sdjl  * This method is called after a successful ldap search has been performed.
47*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
48*cb5caa98Sdjl  *
49*cb5caa98Sdjl  * e.g.
50*cb5caa98Sdjl  *
51*cb5caa98Sdjl  * admin_low:host_type=unlabeled;def_label=[0x0000000000000000000000000000000000
52*cb5caa98Sdjl  * 0000000000000000000000000000000000];min_sl=0x00000000000000000000000000000000
53*cb5caa98Sdjl  * 000000000000000000000000000000000000;max_sl=0x7ffffffffffffffffffffffffffffff
54*cb5caa98Sdjl  * fffffffffffffffffffffffffffffffffffff;doi=0;
55*cb5caa98Sdjl  */
5645916cd2Sjpk static int
_nss_ldap_tnrhtp2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)57*cb5caa98Sdjl _nss_ldap_tnrhtp2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
5845916cd2Sjpk {
59*cb5caa98Sdjl 	int			nss_result = NSS_STR_PARSE_SUCCESS;
6045916cd2Sjpk 	int			len = 0;
6145916cd2Sjpk 	char			*buffer = NULL;
62*cb5caa98Sdjl 	char			**attrs, **template;
6345916cd2Sjpk 	ns_ldap_result_t	*result = be->result;
6445916cd2Sjpk 
65*cb5caa98Sdjl 	if (result == NULL)
66*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
67*cb5caa98Sdjl 
68*cb5caa98Sdjl 	template = __ns_ldap_getAttr(result->entry, _TNRHTP_NAME);
69*cb5caa98Sdjl 	if (template == NULL || template[0] == NULL ||
70*cb5caa98Sdjl 			(strlen(template[0]) < 1)) {
71*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
72*cb5caa98Sdjl 		goto result_tnrhtp2str;
7345916cd2Sjpk 	}
74*cb5caa98Sdjl 	attrs = __ns_ldap_getAttr(result->entry, _TNRHTP_ATTRS);
75*cb5caa98Sdjl 	if (attrs == NULL || attrs[0] == NULL || (strlen(attrs[0]) < 1)) {
7645916cd2Sjpk 		nss_result = NSS_STR_PARSE_PARSE;
77*cb5caa98Sdjl 		goto result_tnrhtp2str;
7845916cd2Sjpk 	}
79*cb5caa98Sdjl 
80*cb5caa98Sdjl 	/* "template:attrs" */
81*cb5caa98Sdjl 	len = strlen(template[0]) + strlen(attrs[0]) + 2;
82*cb5caa98Sdjl 
83*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
84*cb5caa98Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
85*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
86*cb5caa98Sdjl 			goto result_tnrhtp2str;
8745916cd2Sjpk 		}
88*cb5caa98Sdjl 		be->buflen = len - 1;
89*cb5caa98Sdjl 		buffer = be->buffer;
90*cb5caa98Sdjl 	} else
91*cb5caa98Sdjl 		buffer = argp->buf.buffer;
92*cb5caa98Sdjl 
93*cb5caa98Sdjl 	(void) snprintf(buffer, len, "%s:%s", template[0], attrs[0]);
94*cb5caa98Sdjl 
95*cb5caa98Sdjl result_tnrhtp2str:
9645916cd2Sjpk 	(void) __ns_ldap_freeResult(&be->result);
9745916cd2Sjpk 	return (nss_result);
9845916cd2Sjpk }
9945916cd2Sjpk 
10045916cd2Sjpk static nss_status_t
getbyname(ldap_backend_ptr be,void * a)10145916cd2Sjpk getbyname(ldap_backend_ptr be, void *a)
10245916cd2Sjpk {
10345916cd2Sjpk 	char		searchfilter[SEARCHFILTERLEN];
10445916cd2Sjpk 	char		userdata[SEARCHFILTERLEN];
10545916cd2Sjpk 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
10645916cd2Sjpk 
107*cb5caa98Sdjl 	if (argp->key.name == NULL)
108*cb5caa98Sdjl 		return (NSS_NOTFOUND);
10945916cd2Sjpk 
11045916cd2Sjpk 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETTNTPBYNAME,
11145916cd2Sjpk 	    argp->key.name) < 0)
11245916cd2Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
11345916cd2Sjpk 
11445916cd2Sjpk 	if (snprintf(userdata, sizeof (userdata), _F_GETTNTPBYNAME_SSD,
11545916cd2Sjpk 	    argp->key.name) < 0)
11645916cd2Sjpk 		return ((nss_status_t)NSS_NOTFOUND);
11745916cd2Sjpk 
11845916cd2Sjpk 	return (_nss_ldap_lookup(be, argp, _TNRHTP, searchfilter, NULL,
11945916cd2Sjpk 	    _merge_SSD_filter, userdata));
12045916cd2Sjpk }
12145916cd2Sjpk 
12245916cd2Sjpk 
12345916cd2Sjpk static ldap_backend_op_t tnrhtp_ops[] = {
12445916cd2Sjpk 	_nss_ldap_destr,
12545916cd2Sjpk 	_nss_ldap_endent,
12645916cd2Sjpk 	_nss_ldap_setent,
12745916cd2Sjpk 	_nss_ldap_getent,
12845916cd2Sjpk 	getbyname
12945916cd2Sjpk };
13045916cd2Sjpk 
131*cb5caa98Sdjl /* ARGSUSED */
13245916cd2Sjpk nss_backend_t *
_nss_ldap_tnrhtp_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)13345916cd2Sjpk _nss_ldap_tnrhtp_constr(const char *dummy1,
13445916cd2Sjpk     const char *dummy2,
13545916cd2Sjpk     const char *dummy3,
13645916cd2Sjpk     const char *dummy4,
13745916cd2Sjpk     const char *dummy5)
13845916cd2Sjpk {
13945916cd2Sjpk 	return ((nss_backend_t *)_nss_ldap_constr(tnrhtp_ops,
14045916cd2Sjpk 		sizeof (tnrhtp_ops)/sizeof (tnrhtp_ops[0]), _TNRHTP,
141*cb5caa98Sdjl 		tnrhtp_attrs, _nss_ldap_tnrhtp2str));
14245916cd2Sjpk }
143