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 "ldap_common.h"
29 #include <sys/tsol/tndb.h>
30 
31 /* tnrhtp attributes filters */
32 #define	_TNRHTP_NAME		"ipTnetTemplateName"
33 #define	_TNRHTP_ATTRS		"SolarisAttrKeyValue"
34 #define	_F_GETTNTPBYNAME	"(&(objectClass=ipTnetTemplate)"\
35 				"(ipTnetTemplateName=%s))"
36 #define	_F_GETTNTPBYNAME_SSD	"(&(%%s)(ipTnetTemplateName=%s))"
37 
38 static const char *tnrhtp_attrs[] = {
39 	_TNRHTP_NAME,
40 	_TNRHTP_ATTRS,
41 	NULL
42 };
43 
44 static int
45 _nss_ldap_tnrhtp2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
46 {
47 	int			i, nss_result;
48 	int			len = 0;
49 	int			buflen = 0;
50 	char			*buffer = NULL;
51 	char			*ceiling = NULL;
52 	ns_ldap_attr_t		*attrptr;
53 	ns_ldap_result_t	*result = be->result;
54 	tsol_tpstr_t		*tpstrp;
55 
56 	buffer = argp->buf.buffer;
57 	buflen = argp->buf.buflen;
58 	if (argp->buf.result == NULL) {
59 		nss_result = (int)NSS_STR_PARSE_ERANGE;
60 		goto result_tnrhtp2ent;
61 	}
62 	tpstrp = (tsol_tpstr_t *)(argp->buf.result);
63 	tpstrp->template = tpstrp->attrs = NULL;
64 	ceiling = buffer + buflen;
65 	(void) memset(argp->buf.buffer, 0, buflen);
66 	attrptr = getattr(result, 0);
67 	if (attrptr == NULL) {
68 		nss_result = NSS_STR_PARSE_PARSE;
69 		goto result_tnrhtp2ent;
70 	}
71 	for (i = 0; i < result->entry->attr_count; i++) {
72 		attrptr = getattr(result, i);
73 		if (attrptr == NULL) {
74 			nss_result = (int)NSS_STR_PARSE_PARSE;
75 			goto result_tnrhtp2ent;
76 		}
77 #ifdef	DEBUG
78 		(void) fprintf(stdout,
79 		    "\n[tsol_gettpent.c: _nss_ldap_tnrhtp2ent %d]\n", i);
80 		(void) fprintf(stdout, "      entry value count %d: %s:%s\n",
81 		    attrptr->value_count,
82 		    attrptr->attrname ? attrptr->attrname : "NULL",
83 		    attrptr->attrvalue[0] ? attrptr->attrvalue[0] : "NULL");
84 #endif	/* DEBUG */
85 		if (strcasecmp(attrptr->attrname, _TNRHTP_NAME) == 0) {
86 			len = strlen(attrptr->attrvalue[0]);
87 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
88 				nss_result = (int)NSS_STR_PARSE_PARSE;
89 				goto result_tnrhtp2ent;
90 			}
91 			tpstrp->template = buffer;
92 			buffer += len + 1;
93 			if (buffer >= ceiling) {
94 				nss_result = (int)NSS_STR_PARSE_ERANGE;
95 				goto result_tnrhtp2ent;
96 			}
97 			(void) strcpy(tpstrp->template, attrptr->attrvalue[0]);
98 			continue;
99 		}
100 		if (strcasecmp(attrptr->attrname, _TNRHTP_ATTRS) == 0) {
101 			len = strlen(attrptr->attrvalue[0]);
102 			if (len < 1 || (attrptr->attrvalue[0] == '\0')) {
103 				nss_result = (int)NSS_STR_PARSE_PARSE;
104 				goto result_tnrhtp2ent;
105 			}
106 			tpstrp->attrs = buffer;
107 			buffer += len + 1;
108 			if (buffer >= ceiling) {
109 				nss_result = (int)NSS_STR_PARSE_PARSE;
110 				goto result_tnrhtp2ent;
111 			}
112 			(void) strcpy(tpstrp->attrs, attrptr->attrvalue[0]);
113 			continue;
114 		}
115 	}
116 	if (tpstrp->attrs == NULL)
117 		nss_result = NSS_STR_PARSE_PARSE;
118 	else
119 		nss_result = NSS_STR_PARSE_SUCCESS;
120 
121 #ifdef	DEBUG
122 	(void) fprintf(stdout, "\n[tsol_gettpent.c: _nss_ldap_tnrhtp2ent]\n");
123 	(void) fprintf(stdout, "      template: [%s]\n",
124 	    tpstrp->template ? tpstrp->template : "NULL");
125 	(void) fprintf(stdout, "      attrs: [%s]\n",
126 	    tpstrp->attrs ? tpstrp->attrs : "NULL");
127 #endif	/* DEBUG */
128 
129 result_tnrhtp2ent:
130 	(void) __ns_ldap_freeResult(&be->result);
131 	return (nss_result);
132 }
133 
134 
135 static nss_status_t
136 getbyname(ldap_backend_ptr be, void *a)
137 {
138 	char		searchfilter[SEARCHFILTERLEN];
139 	char		userdata[SEARCHFILTERLEN];
140 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
141 
142 #ifdef	DEBUG
143 	(void) fprintf(stdout, "\n[tsol_gettpent.c: getbyname]\n");
144 #endif	/* DEBUG */
145 
146 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETTNTPBYNAME,
147 	    argp->key.name) < 0)
148 		return ((nss_status_t)NSS_NOTFOUND);
149 
150 	if (snprintf(userdata, sizeof (userdata), _F_GETTNTPBYNAME_SSD,
151 	    argp->key.name) < 0)
152 		return ((nss_status_t)NSS_NOTFOUND);
153 
154 	return (_nss_ldap_lookup(be, argp, _TNRHTP, searchfilter, NULL,
155 	    _merge_SSD_filter, userdata));
156 }
157 
158 
159 static ldap_backend_op_t tnrhtp_ops[] = {
160 	_nss_ldap_destr,
161 	_nss_ldap_endent,
162 	_nss_ldap_setent,
163 	_nss_ldap_getent,
164 	getbyname
165 };
166 
167 
168 nss_backend_t *
169 _nss_ldap_tnrhtp_constr(const char *dummy1,
170     const char *dummy2,
171     const char *dummy3,
172     const char *dummy4,
173     const char *dummy5)
174 {
175 #ifdef	DEBUG
176 	(void) fprintf(stdout,
177 	    "\n[gettnrhtpattr.c: _nss_ldap_tnrhtp_constr]\n");
178 #endif
179 	return ((nss_backend_t *)_nss_ldap_constr(tnrhtp_ops,
180 		sizeof (tnrhtp_ops)/sizeof (tnrhtp_ops[0]), _TNRHTP,
181 		tnrhtp_attrs, _nss_ldap_tnrhtp2ent));
182 }
183