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