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 <auth_attr.h>
31*0Sstevel@tonic-gate #include "ldap_common.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /* auth_attr attributes filters */
35*0Sstevel@tonic-gate #define	_AUTH_NAME		"cn"
36*0Sstevel@tonic-gate #define	_AUTH_RES1		"SolarisAttrReserved1"
37*0Sstevel@tonic-gate #define	_AUTH_RES2		"SolarisAttrReserved2"
38*0Sstevel@tonic-gate #define	_AUTH_SHORTDES		"SolarisAttrShortDesc"
39*0Sstevel@tonic-gate #define	_AUTH_LONGDES		"SolarisAttrLongDesc"
40*0Sstevel@tonic-gate #define	_AUTH_ATTRS		"SolarisAttrKeyValue"
41*0Sstevel@tonic-gate #define	_AUTH_GETAUTHNAME 	"(&(objectClass=SolarisAuthAttr)(cn=%s))"
42*0Sstevel@tonic-gate #define	_AUTH_GETAUTHNAME_SSD 	"(&(%%s)(cn=%s))"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate static const char *auth_attrs[] = {
46*0Sstevel@tonic-gate 	_AUTH_NAME,
47*0Sstevel@tonic-gate 	_AUTH_RES1,
48*0Sstevel@tonic-gate 	_AUTH_RES2,
49*0Sstevel@tonic-gate 	_AUTH_SHORTDES,
50*0Sstevel@tonic-gate 	_AUTH_LONGDES,
51*0Sstevel@tonic-gate 	_AUTH_ATTRS,
52*0Sstevel@tonic-gate 	(char *)NULL
53*0Sstevel@tonic-gate };
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static int
57*0Sstevel@tonic-gate _nss_ldap_auth2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	int			i, nss_result;
60*0Sstevel@tonic-gate 	int			buflen = (int)0;
61*0Sstevel@tonic-gate 	unsigned long		len = 0L;
62*0Sstevel@tonic-gate 	char			*nullstring = (char *)NULL;
63*0Sstevel@tonic-gate 	char			*buffer = (char *)NULL;
64*0Sstevel@tonic-gate 	char			*ceiling = (char *)NULL;
65*0Sstevel@tonic-gate 	authstr_t		*auth = (authstr_t *)NULL;
66*0Sstevel@tonic-gate 	ns_ldap_attr_t		*attrptr;
67*0Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	buffer = argp->buf.buffer;
70*0Sstevel@tonic-gate 	buflen = (size_t)argp->buf.buflen;
71*0Sstevel@tonic-gate 	if (!argp->buf.result) {
72*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_ERANGE;
73*0Sstevel@tonic-gate 		goto result_auth2ent;
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	auth = (authstr_t *)(argp->buf.result);
76*0Sstevel@tonic-gate 	ceiling = buffer + buflen;
77*0Sstevel@tonic-gate 	auth->name = (char *)NULL;
78*0Sstevel@tonic-gate 	auth->res1 = (char *)NULL;
79*0Sstevel@tonic-gate 	auth->res2 = (char *)NULL;
80*0Sstevel@tonic-gate 	auth->short_desc = (char *)NULL;
81*0Sstevel@tonic-gate 	auth->long_desc = (char *)NULL;
82*0Sstevel@tonic-gate 	auth->attr = (char *)NULL;
83*0Sstevel@tonic-gate 	nss_result = (int)NSS_STR_PARSE_SUCCESS;
84*0Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	attrptr = getattr(result, 0);
87*0Sstevel@tonic-gate 	if (attrptr == NULL) {
88*0Sstevel@tonic-gate 		nss_result = (int)NSS_STR_PARSE_PARSE;
89*0Sstevel@tonic-gate 		goto result_auth2ent;
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	for (i = 0; i < result->entry->attr_count; i++) {
93*0Sstevel@tonic-gate 		attrptr = getattr(result, i);
94*0Sstevel@tonic-gate 		if (attrptr == NULL) {
95*0Sstevel@tonic-gate 			nss_result = (int)NSS_STR_PARSE_PARSE;
96*0Sstevel@tonic-gate 			goto result_auth2ent;
97*0Sstevel@tonic-gate 		}
98*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_NAME) == 0) {
99*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
100*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
101*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_PARSE;
102*0Sstevel@tonic-gate 				goto result_auth2ent;
103*0Sstevel@tonic-gate 			}
104*0Sstevel@tonic-gate 			auth->name = buffer;
105*0Sstevel@tonic-gate 			buffer += len + 1;
106*0Sstevel@tonic-gate 			if (buffer >= ceiling) {
107*0Sstevel@tonic-gate 				nss_result = (int)NSS_STR_PARSE_ERANGE;
108*0Sstevel@tonic-gate 				goto result_auth2ent;
109*0Sstevel@tonic-gate 			}
110*0Sstevel@tonic-gate 			(void) strcpy(auth->name, attrptr->attrvalue[0]);
111*0Sstevel@tonic-gate 			continue;
112*0Sstevel@tonic-gate 		}
113*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_RES1) == 0) {
114*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
115*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
116*0Sstevel@tonic-gate 				auth->res1 = nullstring;
117*0Sstevel@tonic-gate 			} else {
118*0Sstevel@tonic-gate 				auth->res1 = buffer;
119*0Sstevel@tonic-gate 				buffer += len + 1;
120*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
121*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
122*0Sstevel@tonic-gate 					goto result_auth2ent;
123*0Sstevel@tonic-gate 				}
124*0Sstevel@tonic-gate 				(void) strcpy(auth->res1,
125*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 			continue;
128*0Sstevel@tonic-gate 		}
129*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_RES2) == 0) {
130*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
131*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
132*0Sstevel@tonic-gate 				auth->res2 = nullstring;
133*0Sstevel@tonic-gate 			} else {
134*0Sstevel@tonic-gate 				auth->res2 = buffer;
135*0Sstevel@tonic-gate 				buffer += len + 1;
136*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
137*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
138*0Sstevel@tonic-gate 					goto result_auth2ent;
139*0Sstevel@tonic-gate 				}
140*0Sstevel@tonic-gate 				(void) strcpy(auth->res2,
141*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 			continue;
144*0Sstevel@tonic-gate 		}
145*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_SHORTDES) == 0) {
146*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
147*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
148*0Sstevel@tonic-gate 				auth->short_desc = nullstring;
149*0Sstevel@tonic-gate 			} else {
150*0Sstevel@tonic-gate 				auth->short_desc = buffer;
151*0Sstevel@tonic-gate 				buffer += len + 1;
152*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
153*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
154*0Sstevel@tonic-gate 					goto result_auth2ent;
155*0Sstevel@tonic-gate 				}
156*0Sstevel@tonic-gate 				(void) strcpy(auth->short_desc,
157*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
158*0Sstevel@tonic-gate 			}
159*0Sstevel@tonic-gate 			continue;
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_LONGDES) == 0) {
162*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
163*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
164*0Sstevel@tonic-gate 				auth->long_desc = nullstring;
165*0Sstevel@tonic-gate 			} else {
166*0Sstevel@tonic-gate 				auth->long_desc = buffer;
167*0Sstevel@tonic-gate 				buffer += len + 1;
168*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
169*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
170*0Sstevel@tonic-gate 					goto result_auth2ent;
171*0Sstevel@tonic-gate 				}
172*0Sstevel@tonic-gate 				(void) strcpy(auth->long_desc,
173*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
174*0Sstevel@tonic-gate 			}
175*0Sstevel@tonic-gate 			continue;
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		if (strcasecmp(attrptr->attrname, _AUTH_ATTRS) == 0) {
178*0Sstevel@tonic-gate 			if ((attrptr->attrvalue[0] == NULL) ||
179*0Sstevel@tonic-gate 			    (len = strlen(attrptr->attrvalue[0])) < 1) {
180*0Sstevel@tonic-gate 				auth->attr = nullstring;
181*0Sstevel@tonic-gate 			} else {
182*0Sstevel@tonic-gate 				auth->attr = buffer;
183*0Sstevel@tonic-gate 				buffer += len + 1;
184*0Sstevel@tonic-gate 				if (buffer >= ceiling) {
185*0Sstevel@tonic-gate 					nss_result = (int)NSS_STR_PARSE_ERANGE;
186*0Sstevel@tonic-gate 					goto result_auth2ent;
187*0Sstevel@tonic-gate 				}
188*0Sstevel@tonic-gate 				(void) strcpy(auth->attr,
189*0Sstevel@tonic-gate 				    attrptr->attrvalue[0]);
190*0Sstevel@tonic-gate 			}
191*0Sstevel@tonic-gate 			continue;
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate #ifdef	DEBUG
196*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getauthattr.c: _nss_ldap_auth2ent]\n");
197*0Sstevel@tonic-gate 	(void) fprintf(stdout, "      auth-name: [%s]\n", auth->name);
198*0Sstevel@tonic-gate 	if (auth->res1 != (char *)NULL) {
199*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      res1: [%s]\n", auth->res1);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 	if (auth->res2 != (char *)NULL) {
202*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      res2: [%s]\n", auth->res2);
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 	if (auth->short_desc != (char *)NULL) {
205*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      short_desc: [%s]\n",
206*0Sstevel@tonic-gate 		    auth->short_desc);
207*0Sstevel@tonic-gate 	}
208*0Sstevel@tonic-gate 	if (auth->long_desc != (char *)NULL) {
209*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      long_desc: [%s]\n",
210*0Sstevel@tonic-gate 		    auth->long_desc);
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	if (auth->attr != (char *)NULL) {
213*0Sstevel@tonic-gate 		(void) fprintf(stdout, "      attr: [%s]\n", auth->attr);
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate #endif	/* DEBUG */
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate result_auth2ent:
218*0Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
219*0Sstevel@tonic-gate 	return ((int)nss_result);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate static nss_status_t
224*0Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
227*0Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
228*0Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
229*0Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
230*0Sstevel@tonic-gate 	int		ret;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate #ifdef	DEBUG
233*0Sstevel@tonic-gate 	(void) fprintf(stdout, "\n[getauthattr.c: getbyname]\n");
234*0Sstevel@tonic-gate #endif	/* DEBUG */
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
237*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	ret = snprintf(searchfilter, SEARCHFILTERLEN,
240*0Sstevel@tonic-gate 	    _AUTH_GETAUTHNAME, name);
241*0Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
242*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
245*0Sstevel@tonic-gate 	    _AUTH_GETAUTHNAME_SSD, name);
246*0Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
247*0Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
250*0Sstevel@tonic-gate 	    _AUTHATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
251*0Sstevel@tonic-gate }
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate static ldap_backend_op_t authattr_ops[] = {
255*0Sstevel@tonic-gate 	_nss_ldap_destr,
256*0Sstevel@tonic-gate 	_nss_ldap_endent,
257*0Sstevel@tonic-gate 	_nss_ldap_setent,
258*0Sstevel@tonic-gate 	_nss_ldap_getent,
259*0Sstevel@tonic-gate 	getbyname
260*0Sstevel@tonic-gate };
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate /*ARGSUSED0*/
264*0Sstevel@tonic-gate nss_backend_t *
265*0Sstevel@tonic-gate _nss_ldap_auth_attr_constr(const char *dummy1,
266*0Sstevel@tonic-gate     const char *dummy2,
267*0Sstevel@tonic-gate     const char *dummy3,
268*0Sstevel@tonic-gate     const char *dummy4,
269*0Sstevel@tonic-gate     const char *dummy5)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate #ifdef	DEBUG
272*0Sstevel@tonic-gate 	(void) fprintf(stdout,
273*0Sstevel@tonic-gate 	    "\n[getauthattr.c: _nss_ldap_auth_attr_constr]\n");
274*0Sstevel@tonic-gate #endif
275*0Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(authattr_ops,
276*0Sstevel@tonic-gate 		sizeof (authattr_ops)/sizeof (authattr_ops[0]), _AUTHATTR,
277*0Sstevel@tonic-gate 		auth_attrs, _nss_ldap_auth2ent));
278*0Sstevel@tonic-gate }
279