xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getuserattr.c (revision 2830:5228d1267a01)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <secdb.h>
290Sstevel@tonic-gate #include <user_attr.h>
300Sstevel@tonic-gate #include "ldap_common.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* user_attr attributes filters */
340Sstevel@tonic-gate #define	_USER_NAME		"uid"
350Sstevel@tonic-gate #define	_USER_QUALIFIER		"SolarisUserQualifier"
360Sstevel@tonic-gate #define	_USER_RES1		"SolarisAttrReserved1"
370Sstevel@tonic-gate #define	_USER_RES2		"SolarisAttrReserved2"
380Sstevel@tonic-gate #define	_USER_ATTRS		"SolarisAttrKeyValue"
390Sstevel@tonic-gate #define	_USER_GETUSERNAME	\
400Sstevel@tonic-gate 	"(&(objectClass=SolarisUserAttr)(uid=%s))"
410Sstevel@tonic-gate #define	_USER_GETUSERNAME_SSD	"(&(%%s)(uid=%s))"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static const char *user_attrs[] = {
450Sstevel@tonic-gate 	_USER_NAME,
460Sstevel@tonic-gate 	_USER_QUALIFIER,
470Sstevel@tonic-gate 	_USER_RES1,
480Sstevel@tonic-gate 	_USER_RES2,
490Sstevel@tonic-gate 	_USER_ATTRS,
500Sstevel@tonic-gate 	(char *)NULL
510Sstevel@tonic-gate };
52*2830Sdjl /*
53*2830Sdjl  * _nss_ldap_user2str is the data marshaling method for the user_attr
54*2830Sdjl  * system call getuserattr, getusernam and getuseruid.
55*2830Sdjl  * This method is called after a successful search has been performed.
56*2830Sdjl  * This method will parse the search results into the file format.
57*2830Sdjl  * e.g.
58*2830Sdjl  *
59*2830Sdjl  * adm::::profiles=Log Management
60*2830Sdjl  *
61*2830Sdjl  */
620Sstevel@tonic-gate static int
_nss_ldap_user2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)63*2830Sdjl _nss_ldap_user2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
640Sstevel@tonic-gate {
65*2830Sdjl 	int			nss_result;
66*2830Sdjl 	int			buflen = 0;
670Sstevel@tonic-gate 	unsigned long		len = 0L;
68*2830Sdjl 	char			*buffer = NULL;
690Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
70*2830Sdjl 	char			**name, **res1, **res2, **qu, **attr;
71*2830Sdjl 	char			*res1_str, *res2_str, *qu_str, *attr_str;
720Sstevel@tonic-gate 
73*2830Sdjl 	if (result == NULL)
74*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
75*2830Sdjl 
76*2830Sdjl 	buflen = argp->buf.buflen;
77*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
780Sstevel@tonic-gate 	(void) memset(argp->buf.buffer, 0, buflen);
790Sstevel@tonic-gate 
80*2830Sdjl 	name = __ns_ldap_getAttr(result->entry, _USER_NAME);
81*2830Sdjl 	if (name == NULL || name[0] == NULL ||
82*2830Sdjl 			(strlen(name[0]) < 1)) {
83*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
84*2830Sdjl 		goto result_user2str;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
87*2830Sdjl 	qu = __ns_ldap_getAttr(result->entry, _USER_QUALIFIER);
88*2830Sdjl 	if (qu == NULL || qu[0] == NULL || (strlen(qu[0]) < 1))
89*2830Sdjl 		qu_str = _NO_VALUE;
90*2830Sdjl 	else
91*2830Sdjl 		qu_str = qu[0];
92*2830Sdjl 
93*2830Sdjl 	res1 = __ns_ldap_getAttr(result->entry, _USER_RES2);
94*2830Sdjl 	if (res1 == NULL || res1[0] == NULL || (strlen(res1[0]) < 1))
95*2830Sdjl 		res1_str = _NO_VALUE;
96*2830Sdjl 	else
97*2830Sdjl 		res1_str = res1[0];
98*2830Sdjl 
99*2830Sdjl 	res2 = __ns_ldap_getAttr(result->entry, _USER_RES2);
100*2830Sdjl 	if (res2 == NULL || res2[0] == NULL || (strlen(res2[0]) < 1))
101*2830Sdjl 		res2_str = _NO_VALUE;
102*2830Sdjl 	else
103*2830Sdjl 		res2_str = res2[0];
104*2830Sdjl 
105*2830Sdjl 	attr = __ns_ldap_getAttr(result->entry, _USER_ATTRS);
106*2830Sdjl 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
107*2830Sdjl 		attr_str = _NO_VALUE;
108*2830Sdjl 	else
109*2830Sdjl 		attr_str = attr[0];
110*2830Sdjl 	/* 5 = 4 ':' + 1 '\0' */
111*2830Sdjl 	len = strlen(name[0]) + strlen(res1_str) + strlen(res2_str) +
112*2830Sdjl 		strlen(qu_str) + strlen(attr_str) + 5;
113*2830Sdjl 	if (len > buflen) {
114*2830Sdjl 		nss_result = NSS_STR_PARSE_ERANGE;
115*2830Sdjl 		goto result_user2str;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
118*2830Sdjl 	if (argp->buf.result != NULL) {
119*2830Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
120*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
121*2830Sdjl 			goto result_user2str;
122*2830Sdjl 		}
123*2830Sdjl 		buffer = be->buffer;
124*2830Sdjl 	} else
125*2830Sdjl 		buffer = argp->buf.buffer;
126*2830Sdjl 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s",
127*2830Sdjl 			name[0], qu_str, res1_str, res2_str, attr_str);
128*2830Sdjl 	/* The front end marshaller doesn't need the trailing null */
129*2830Sdjl 	if (argp->buf.result != NULL)
130*2830Sdjl 		be->buflen = strlen(be->buffer);
1310Sstevel@tonic-gate 
132*2830Sdjl result_user2str:
1330Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1340Sstevel@tonic-gate 	return ((int)nss_result);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1390Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1420Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1430Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1440Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
1450Sstevel@tonic-gate 	int		ret;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
1480Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1510Sstevel@tonic-gate 	    _USER_GETUSERNAME, name);
1520Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1530Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1560Sstevel@tonic-gate 	    _USER_GETUSERNAME_SSD, name);
1570Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1580Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
1610Sstevel@tonic-gate 	    _USERATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static ldap_backend_op_t userattr_ops[] = {
1660Sstevel@tonic-gate 	_nss_ldap_destr,
1670Sstevel@tonic-gate 	_nss_ldap_endent,
1680Sstevel@tonic-gate 	_nss_ldap_setent,
1690Sstevel@tonic-gate 	_nss_ldap_getent,
1700Sstevel@tonic-gate 	getbyname
1710Sstevel@tonic-gate };
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*ARGSUSED0*/
1750Sstevel@tonic-gate nss_backend_t *
_nss_ldap_user_attr_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)1760Sstevel@tonic-gate _nss_ldap_user_attr_constr(const char *dummy1,
1770Sstevel@tonic-gate     const char *dummy2,
1780Sstevel@tonic-gate     const char *dummy3,
1790Sstevel@tonic-gate     const char *dummy4,
1800Sstevel@tonic-gate     const char *dummy5)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(userattr_ops,
1830Sstevel@tonic-gate 		sizeof (userattr_ops)/sizeof (userattr_ops[0]), _USERATTR,
184*2830Sdjl 		user_attrs, _nss_ldap_user2str));
1850Sstevel@tonic-gate }
186