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