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 #include "ldap_common.h"
27 #include <sys/tsol/tndb.h>
28 
29 /* tnrhtp attributes filters */
30 #define	_TNRHTP_NAME		"ipTnetTemplateName"
31 #define	_TNRHTP_ATTRS		"SolarisAttrKeyValue"
32 #define	_F_GETTNTPBYNAME	"(&(objectClass=ipTnetTemplate)"\
33 				"(!(objectClass=ipTnetHost))" \
34 				"(ipTnetTemplateName=%s))"
35 #define	_F_GETTNTPBYNAME_SSD	"(&(%%s)(ipTnetTemplateName=%s))"
36 
37 static const char *tnrhtp_attrs[] = {
38 	_TNRHTP_NAME,
39 	_TNRHTP_ATTRS,
40 	NULL
41 };
42 
43 /*
44  * _nss_ldap_tnrhtp2str is the data marshaling method for the tnrhtp
45  * (tsol_gettpbyaddr()/tsol_gettpent()) backend processes.
46  * This method is called after a successful ldap search has been performed.
47  * This method will parse the ldap search values into the file format.
48  *
49  * e.g.
50  *
51  * admin_low:host_type=unlabeled;def_label=[0x0000000000000000000000000000000000
52  * 0000000000000000000000000000000000];min_sl=0x00000000000000000000000000000000
53  * 000000000000000000000000000000000000;max_sl=0x7ffffffffffffffffffffffffffffff
54  * fffffffffffffffffffffffffffffffffffff;doi=0;
55  */
56 static int
_nss_ldap_tnrhtp2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)57 _nss_ldap_tnrhtp2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
58 {
59 	int			nss_result = NSS_STR_PARSE_SUCCESS;
60 	int			len = 0;
61 	char			*buffer = NULL;
62 	char			**attrs, **template;
63 	ns_ldap_result_t	*result = be->result;
64 
65 	if (result == NULL)
66 		return (NSS_STR_PARSE_PARSE);
67 
68 	template = __ns_ldap_getAttr(result->entry, _TNRHTP_NAME);
69 	if (template == NULL || template[0] == NULL ||
70 			(strlen(template[0]) < 1)) {
71 		nss_result = NSS_STR_PARSE_PARSE;
72 		goto result_tnrhtp2str;
73 	}
74 	attrs = __ns_ldap_getAttr(result->entry, _TNRHTP_ATTRS);
75 	if (attrs == NULL || attrs[0] == NULL || (strlen(attrs[0]) < 1)) {
76 		nss_result = NSS_STR_PARSE_PARSE;
77 		goto result_tnrhtp2str;
78 	}
79 
80 	/* "template:attrs" */
81 	len = strlen(template[0]) + strlen(attrs[0]) + 2;
82 
83 	if (argp->buf.result != NULL) {
84 		if ((be->buffer = calloc(1, len)) == NULL) {
85 			nss_result = NSS_STR_PARSE_PARSE;
86 			goto result_tnrhtp2str;
87 		}
88 		be->buflen = len - 1;
89 		buffer = be->buffer;
90 	} else
91 		buffer = argp->buf.buffer;
92 
93 	(void) snprintf(buffer, len, "%s:%s", template[0], attrs[0]);
94 
95 result_tnrhtp2str:
96 	(void) __ns_ldap_freeResult(&be->result);
97 	return (nss_result);
98 }
99 
100 static nss_status_t
getbyname(ldap_backend_ptr be,void * a)101 getbyname(ldap_backend_ptr be, void *a)
102 {
103 	char		searchfilter[SEARCHFILTERLEN];
104 	char		userdata[SEARCHFILTERLEN];
105 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
106 
107 	if (argp->key.name == NULL)
108 		return (NSS_NOTFOUND);
109 
110 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETTNTPBYNAME,
111 	    argp->key.name) < 0)
112 		return ((nss_status_t)NSS_NOTFOUND);
113 
114 	if (snprintf(userdata, sizeof (userdata), _F_GETTNTPBYNAME_SSD,
115 	    argp->key.name) < 0)
116 		return ((nss_status_t)NSS_NOTFOUND);
117 
118 	return (_nss_ldap_lookup(be, argp, _TNRHTP, searchfilter, NULL,
119 	    _merge_SSD_filter, userdata));
120 }
121 
122 
123 static ldap_backend_op_t tnrhtp_ops[] = {
124 	_nss_ldap_destr,
125 	_nss_ldap_endent,
126 	_nss_ldap_setent,
127 	_nss_ldap_getent,
128 	getbyname
129 };
130 
131 /* ARGSUSED */
132 nss_backend_t *
_nss_ldap_tnrhtp_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)133 _nss_ldap_tnrhtp_constr(const char *dummy1,
134     const char *dummy2,
135     const char *dummy3,
136     const char *dummy4,
137     const char *dummy5)
138 {
139 	return ((nss_backend_t *)_nss_ldap_constr(tnrhtp_ops,
140 		sizeof (tnrhtp_ops)/sizeof (tnrhtp_ops[0]), _TNRHTP,
141 		tnrhtp_attrs, _nss_ldap_tnrhtp2str));
142 }
143