xref: /titanic_50/usr/src/lib/nsswitch/ldap/common/getuserattr.c (revision 25cf1a301a396c38e8adf52c15f537b80d2483f7)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <secdb.h>
30 #include <user_attr.h>
31 #include "ldap_common.h"
32 
33 
34 /* user_attr attributes filters */
35 #define	_USER_NAME		"uid"
36 #define	_USER_QUALIFIER		"SolarisUserQualifier"
37 #define	_USER_RES1		"SolarisAttrReserved1"
38 #define	_USER_RES2		"SolarisAttrReserved2"
39 #define	_USER_ATTRS		"SolarisAttrKeyValue"
40 #define	_USER_GETUSERNAME	\
41 	"(&(objectClass=SolarisUserAttr)(uid=%s))"
42 #define	_USER_GETUSERNAME_SSD	"(&(%%s)(uid=%s))"
43 
44 
45 static const char *user_attrs[] = {
46 	_USER_NAME,
47 	_USER_QUALIFIER,
48 	_USER_RES1,
49 	_USER_RES2,
50 	_USER_ATTRS,
51 	(char *)NULL
52 };
53 
54 
55 static int
56 _nss_ldap_user2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
57 {
58 	int			i, nss_result;
59 	int			buflen = (int)0;
60 	unsigned long		len = 0L;
61 	char			*nullstring = (char *)NULL;
62 	char			*buffer = (char *)NULL;
63 	char			*ceiling = (char *)NULL;
64 	userstr_t		*user = (userstr_t *)NULL;
65 	ns_ldap_attr_t		*attrptr;
66 	ns_ldap_result_t	*result = be->result;
67 
68 	buffer = argp->buf.buffer;
69 	buflen = (size_t)argp->buf.buflen;
70 	if (!argp->buf.result) {
71 		nss_result = (int)NSS_STR_PARSE_ERANGE;
72 		goto result_user2ent;
73 	}
74 	user = (userstr_t *)(argp->buf.result);
75 	ceiling = buffer + buflen;
76 	user->name = (char *)NULL;
77 	user->qualifier = (char *)NULL;
78 	user->res1 = (char *)NULL;
79 	user->res2 = (char *)NULL;
80 	user->attr = (char *)NULL;
81 	nss_result = (int)NSS_STR_PARSE_SUCCESS;
82 	(void) memset(argp->buf.buffer, 0, buflen);
83 
84 	attrptr = getattr(result, 0);
85 	if (attrptr == NULL) {
86 		nss_result = (int)NSS_STR_PARSE_PARSE;
87 		goto result_user2ent;
88 	}
89 
90 	for (i = 0; i < result->entry->attr_count; i++) {
91 		attrptr = getattr(result, i);
92 		if (attrptr == NULL) {
93 			nss_result = (int)NSS_STR_PARSE_PARSE;
94 			goto result_user2ent;
95 		}
96 		if (strcasecmp(attrptr->attrname, _USER_NAME) == 0) {
97 			if ((attrptr->attrvalue[0] == NULL) ||
98 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
99 				nss_result = (int)NSS_STR_PARSE_PARSE;
100 				goto result_user2ent;
101 			}
102 			user->name = buffer;
103 			buffer += len + 1;
104 			if (buffer >= ceiling) {
105 				nss_result = (int)NSS_STR_PARSE_ERANGE;
106 				goto result_user2ent;
107 			}
108 			(void) strcpy(user->name, attrptr->attrvalue[0]);
109 			continue;
110 		}
111 		if (strcasecmp(attrptr->attrname, _USER_QUALIFIER) == 0) {
112 			if ((attrptr->attrvalue[0] == NULL) ||
113 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
114 				user->qualifier = nullstring;
115 			} else {
116 				user->qualifier = buffer;
117 				buffer += len + 1;
118 				if (buffer >= ceiling) {
119 					nss_result = (int)NSS_STR_PARSE_ERANGE;
120 					goto result_user2ent;
121 				}
122 				(void) strcpy(user->qualifier,
123 				    attrptr->attrvalue[0]);
124 			}
125 			continue;
126 		}
127 		if (strcasecmp(attrptr->attrname, _USER_RES1) == 0) {
128 			if ((attrptr->attrvalue[0] == NULL) ||
129 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
130 				user->res1 = nullstring;
131 			} else {
132 				user->res1 = buffer;
133 				buffer += len + 1;
134 				if (buffer >= ceiling) {
135 					nss_result = (int)NSS_STR_PARSE_ERANGE;
136 					goto result_user2ent;
137 				}
138 				(void) strcpy(user->res1,
139 				    attrptr->attrvalue[0]);
140 			}
141 			continue;
142 		}
143 		if (strcasecmp(attrptr->attrname, _USER_RES2) == 0) {
144 			if ((attrptr->attrvalue[0] == NULL) ||
145 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
146 				user->res2 = nullstring;
147 			} else {
148 				user->res2 = buffer;
149 				buffer += len + 1;
150 				if (buffer >= ceiling) {
151 					nss_result = (int)NSS_STR_PARSE_ERANGE;
152 					goto result_user2ent;
153 				}
154 				(void) strcpy(user->res2,
155 				    attrptr->attrvalue[0]);
156 			}
157 			continue;
158 		}
159 		if (strcasecmp(attrptr->attrname, _USER_ATTRS) == 0) {
160 			if ((attrptr->attrvalue[0] == NULL) ||
161 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
162 				user->attr = nullstring;
163 			} else {
164 				user->attr = buffer;
165 				buffer += len + 1;
166 				if (buffer >= ceiling) {
167 					nss_result = (int)NSS_STR_PARSE_ERANGE;
168 					goto result_user2ent;
169 				}
170 				(void) strcpy(user->attr,
171 				    attrptr->attrvalue[0]);
172 			}
173 			continue;
174 		}
175 	}
176 
177 #ifdef	DEBUG
178 	(void) fprintf(stdout, "\n[getuserattr.c: _nss_ldap_user2ent]\n");
179 	(void) fprintf(stdout, "      user-name: [%s]\n", user->name);
180 	if (user->qualifier != (char *)NULL) {
181 		(void) fprintf(stdout, "      qualifier: [%s]\n",
182 		    user->qualifier);
183 	}
184 	if (user->res1 != (char *)NULL) {
185 		(void) fprintf(stdout, "      res1: [%s]\n", user->res1);
186 	}
187 	if (user->res2 != (char *)NULL) {
188 		(void) fprintf(stdout, "      res2: [%s]\n", user->res2);
189 	}
190 	if (user->attr != (char *)NULL) {
191 		(void) fprintf(stdout, "      attr: [%s]\n", user->attr);
192 	}
193 #endif	/* DEBUG */
194 
195 result_user2ent:
196 	(void) __ns_ldap_freeResult(&be->result);
197 	return ((int)nss_result);
198 }
199 
200 
201 static nss_status_t
202 getbyname(ldap_backend_ptr be, void *a)
203 {
204 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
205 	char		searchfilter[SEARCHFILTERLEN];
206 	char		userdata[SEARCHFILTERLEN];
207 	char		name[SEARCHFILTERLEN];
208 	int		ret;
209 
210 #ifdef	DEBUG
211 	(void) fprintf(stdout, "\n[getuserattr.c: getbyname]\n");
212 #endif	/* DEBUG */
213 
214 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
215 		return ((nss_status_t)NSS_NOTFOUND);
216 
217 	ret = snprintf(searchfilter, sizeof (searchfilter),
218 	    _USER_GETUSERNAME, name);
219 	if (ret >= sizeof (userdata) || ret < 0)
220 		return ((nss_status_t)NSS_NOTFOUND);
221 
222 	ret = snprintf(userdata, sizeof (userdata),
223 	    _USER_GETUSERNAME_SSD, name);
224 	if (ret >= sizeof (userdata) || ret < 0)
225 		return ((nss_status_t)NSS_NOTFOUND);
226 
227 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
228 	    _USERATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
229 }
230 
231 
232 static ldap_backend_op_t userattr_ops[] = {
233 	_nss_ldap_destr,
234 	_nss_ldap_endent,
235 	_nss_ldap_setent,
236 	_nss_ldap_getent,
237 	getbyname
238 };
239 
240 
241 /*ARGSUSED0*/
242 nss_backend_t *
243 _nss_ldap_user_attr_constr(const char *dummy1,
244     const char *dummy2,
245     const char *dummy3,
246     const char *dummy4,
247     const char *dummy5)
248 {
249 #ifdef	DEBUG
250 	(void) fprintf(stdout,
251 	    "\n[getuserattr.c: _nss_ldap_user_attr_constr]\n");
252 #endif
253 	return ((nss_backend_t *)_nss_ldap_constr(userattr_ops,
254 		sizeof (userattr_ops)/sizeof (userattr_ops[0]), _USERATTR,
255 		user_attrs, _nss_ldap_user2ent));
256 }
257