xref: /titanic_52/usr/src/lib/nsswitch/ldap/common/getprofattr.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
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 <secdb.h>
29 #include <prof_attr.h>
30 #include "ldap_common.h"
31 
32 
33 /* prof_attr attributes filters */
34 #define	_PROF_NAME		"cn"
35 #define	_PROF_RES1		"SolarisAttrReserved1"
36 #define	_PROF_RES2		"SolarisAttrReserved2"
37 #define	_PROF_DESC		"SolarisAttrLongDesc"
38 #define	_PROF_ATTRS		"SolarisAttrKeyValue"
39 /* Negate an exec_attr attribute to exclude exec_attr entries */
40 #define	_PROF_GETPROFNAME \
41 "(&(objectClass=SolarisProfAttr)(!(SolarisKernelSecurityPolicy=*))(cn=%s))"
42 #define	_PROF_GETPROFNAME_SSD	"(&(%%s)(cn=%s))"
43 
44 static const char *prof_attrs[] = {
45 	_PROF_NAME,
46 	_PROF_RES1,
47 	_PROF_RES2,
48 	_PROF_DESC,
49 	_PROF_ATTRS,
50 	(char *)NULL
51 };
52 /*
53  * _nss_ldap_prof2str is the data marshaling method for the prof_attr
54  * system call getprofattr, getprofnam and getproflist.
55  * This method is called after a successful search has been performed.
56  * This method will parse the search results into the file format.
57  * e.g.
58  *
59  * All:::Execute any command as the user or role:help=RtAll.html
60  *
61  */
62 static int
63 _nss_ldap_prof2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
64 {
65 	int			nss_result;
66 	int			buflen = 0;
67 	unsigned long		len = 0L;
68 	char			*buffer = NULL;
69 	ns_ldap_result_t	*result = be->result;
70 	char			**name, **res1, **res2, **des, **attr;
71 	char			*res1_str, *res2_str, *des_str, *attr_str;
72 
73 	if (result == NULL)
74 		return (NSS_STR_PARSE_PARSE);
75 
76 	buflen = argp->buf.buflen;
77 	nss_result = NSS_STR_PARSE_SUCCESS;
78 	(void) memset(argp->buf.buffer, 0, buflen);
79 
80 	name = __ns_ldap_getAttr(result->entry, _PROF_NAME);
81 	if (name == NULL || name[0] == NULL ||
82 			(strlen(name[0]) < 1)) {
83 		nss_result = NSS_STR_PARSE_PARSE;
84 		goto result_prof2str;
85 	}
86 	res1 = __ns_ldap_getAttr(result->entry, _PROF_RES1);
87 	if (res1 == NULL || res1[0] == NULL || (strlen(res1[0]) < 1))
88 		res1_str = _NO_VALUE;
89 	else
90 		res1_str = res1[0];
91 
92 	res2 = __ns_ldap_getAttr(result->entry, _PROF_RES2);
93 	if (res2 == NULL || res2[0] == NULL || (strlen(res2[0]) < 1))
94 		res2_str = _NO_VALUE;
95 	else
96 		res2_str = res2[0];
97 
98 	des = __ns_ldap_getAttr(result->entry, _PROF_DESC);
99 	if (des == NULL || des[0] == NULL || (strlen(des[0]) < 1))
100 		des_str = _NO_VALUE;
101 	else
102 		des_str = des[0];
103 
104 	attr = __ns_ldap_getAttr(result->entry, _PROF_ATTRS);
105 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
106 		attr_str = _NO_VALUE;
107 	else
108 		attr_str = attr[0];
109 	/* 5 = 4 ':' + 1 '\0' */
110 	len = strlen(name[0]) + strlen(res1_str) + strlen(res2_str) +
111 		strlen(des_str) + strlen(attr_str) + 6;
112 	if (len > buflen) {
113 		nss_result = NSS_STR_PARSE_ERANGE;
114 		goto result_prof2str;
115 	}
116 
117 	if (argp->buf.result != NULL) {
118 		if ((be->buffer = calloc(1, len)) == NULL) {
119 			nss_result = NSS_STR_PARSE_PARSE;
120 			goto result_prof2str;
121 		}
122 		buffer = be->buffer;
123 	} else
124 		buffer = argp->buf.buffer;
125 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s",
126 			name[0], res1_str, res2_str, des_str, attr_str);
127 	/* The front end marshaller doesn't need the trailing null */
128 	if (argp->buf.result != NULL)
129 		be->buflen = strlen(be->buffer);
130 
131 result_prof2str:
132 	(void) __ns_ldap_freeResult(&be->result);
133 	return (nss_result);
134 }
135 
136 
137 static nss_status_t
138 getbyname(ldap_backend_ptr be, void *a)
139 {
140 	char		searchfilter[SEARCHFILTERLEN];
141 	char		userdata[SEARCHFILTERLEN];
142 	char		name[SEARCHFILTERLEN];
143 	int		ret;
144 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
145 
146 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
147 		return ((nss_status_t)NSS_NOTFOUND);
148 
149 	ret = snprintf(searchfilter, sizeof (searchfilter),
150 	    _PROF_GETPROFNAME, name);
151 	if (ret < 0 || ret >= sizeof (searchfilter))
152 		return ((nss_status_t)NSS_NOTFOUND);
153 
154 	ret = snprintf(userdata, sizeof (userdata),
155 	    _PROF_GETPROFNAME_SSD, name);
156 	if (ret < 0 || ret >= sizeof (userdata))
157 		return ((nss_status_t)NSS_NOTFOUND);
158 
159 	return (_nss_ldap_lookup(be, argp,
160 	    _PROFATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
161 }
162 
163 
164 static ldap_backend_op_t profattr_ops[] = {
165 	_nss_ldap_destr,
166 	_nss_ldap_endent,
167 	_nss_ldap_setent,
168 	_nss_ldap_getent,
169 	getbyname
170 };
171 
172 
173 /*ARGSUSED0*/
174 nss_backend_t *
175 _nss_ldap_prof_attr_constr(const char *dummy1,
176     const char *dummy2,
177     const char *dummy3,
178     const char *dummy4,
179     const char *dummy5)
180 {
181 	return ((nss_backend_t *)_nss_ldap_constr(profattr_ops,
182 		sizeof (profattr_ops)/sizeof (profattr_ops[0]), _PROFATTR,
183 		prof_attrs, _nss_ldap_prof2str));
184 }
185