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