xref: /titanic_41/usr/src/lib/nsswitch/ldap/common/getuserattr.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
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 <user_attr.h>
30 #include "ldap_common.h"
31 
32 
33 /* user_attr attributes filters */
34 #define	_USER_NAME		"uid"
35 #define	_USER_QUALIFIER		"SolarisUserQualifier"
36 #define	_USER_RES1		"SolarisAttrReserved1"
37 #define	_USER_RES2		"SolarisAttrReserved2"
38 #define	_USER_ATTRS		"SolarisAttrKeyValue"
39 #define	_USER_GETUSERNAME	\
40 	"(&(objectClass=SolarisUserAttr)(uid=%s))"
41 #define	_USER_GETUSERNAME_SSD	"(&(%%s)(uid=%s))"
42 
43 
44 static const char *user_attrs[] = {
45 	_USER_NAME,
46 	_USER_QUALIFIER,
47 	_USER_RES1,
48 	_USER_RES2,
49 	_USER_ATTRS,
50 	(char *)NULL
51 };
52 /*
53  * _nss_ldap_user2str is the data marshaling method for the user_attr
54  * system call getuserattr, getusernam and getuseruid.
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  * adm::::profiles=Log Management
60  *
61  */
62 static int
63 _nss_ldap_user2str(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, **qu, **attr;
71 	char			*res1_str, *res2_str, *qu_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, _USER_NAME);
81 	if (name == NULL || name[0] == NULL ||
82 			(strlen(name[0]) < 1)) {
83 		nss_result = NSS_STR_PARSE_PARSE;
84 		goto result_user2str;
85 	}
86 
87 	qu = __ns_ldap_getAttr(result->entry, _USER_QUALIFIER);
88 	if (qu == NULL || qu[0] == NULL || (strlen(qu[0]) < 1))
89 		qu_str = _NO_VALUE;
90 	else
91 		qu_str = qu[0];
92 
93 	res1 = __ns_ldap_getAttr(result->entry, _USER_RES2);
94 	if (res1 == NULL || res1[0] == NULL || (strlen(res1[0]) < 1))
95 		res1_str = _NO_VALUE;
96 	else
97 		res1_str = res1[0];
98 
99 	res2 = __ns_ldap_getAttr(result->entry, _USER_RES2);
100 	if (res2 == NULL || res2[0] == NULL || (strlen(res2[0]) < 1))
101 		res2_str = _NO_VALUE;
102 	else
103 		res2_str = res2[0];
104 
105 	attr = __ns_ldap_getAttr(result->entry, _USER_ATTRS);
106 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
107 		attr_str = _NO_VALUE;
108 	else
109 		attr_str = attr[0];
110 	/* 5 = 4 ':' + 1 '\0' */
111 	len = strlen(name[0]) + strlen(res1_str) + strlen(res2_str) +
112 		strlen(qu_str) + strlen(attr_str) + 5;
113 	if (len > buflen) {
114 		nss_result = NSS_STR_PARSE_ERANGE;
115 		goto result_user2str;
116 	}
117 
118 	if (argp->buf.result != NULL) {
119 		if ((be->buffer = calloc(1, len)) == NULL) {
120 			nss_result = NSS_STR_PARSE_PARSE;
121 			goto result_user2str;
122 		}
123 		buffer = be->buffer;
124 	} else
125 		buffer = argp->buf.buffer;
126 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s",
127 			name[0], qu_str, res1_str, res2_str, attr_str);
128 	/* The front end marshaller doesn't need the trailing null */
129 	if (argp->buf.result != NULL)
130 		be->buflen = strlen(be->buffer);
131 
132 result_user2str:
133 	(void) __ns_ldap_freeResult(&be->result);
134 	return ((int)nss_result);
135 }
136 
137 
138 static nss_status_t
139 getbyname(ldap_backend_ptr be, void *a)
140 {
141 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
142 	char		searchfilter[SEARCHFILTERLEN];
143 	char		userdata[SEARCHFILTERLEN];
144 	char		name[SEARCHFILTERLEN];
145 	int		ret;
146 
147 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
148 		return ((nss_status_t)NSS_NOTFOUND);
149 
150 	ret = snprintf(searchfilter, sizeof (searchfilter),
151 	    _USER_GETUSERNAME, name);
152 	if (ret >= sizeof (userdata) || ret < 0)
153 		return ((nss_status_t)NSS_NOTFOUND);
154 
155 	ret = snprintf(userdata, sizeof (userdata),
156 	    _USER_GETUSERNAME_SSD, name);
157 	if (ret >= sizeof (userdata) || ret < 0)
158 		return ((nss_status_t)NSS_NOTFOUND);
159 
160 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
161 	    _USERATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
162 }
163 
164 
165 static ldap_backend_op_t userattr_ops[] = {
166 	_nss_ldap_destr,
167 	_nss_ldap_endent,
168 	_nss_ldap_setent,
169 	_nss_ldap_getent,
170 	getbyname
171 };
172 
173 
174 /*ARGSUSED0*/
175 nss_backend_t *
176 _nss_ldap_user_attr_constr(const char *dummy1,
177     const char *dummy2,
178     const char *dummy3,
179     const char *dummy4,
180     const char *dummy5)
181 {
182 	return ((nss_backend_t *)_nss_ldap_constr(userattr_ops,
183 		sizeof (userattr_ops)/sizeof (userattr_ops[0]), _USERATTR,
184 		user_attrs, _nss_ldap_user2str));
185 }
186