xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getprofattr.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 <prof_attr.h>
300Sstevel@tonic-gate #include "ldap_common.h"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* prof_attr attributes filters */
340Sstevel@tonic-gate #define	_PROF_NAME		"cn"
350Sstevel@tonic-gate #define	_PROF_RES1		"SolarisAttrReserved1"
360Sstevel@tonic-gate #define	_PROF_RES2		"SolarisAttrReserved2"
370Sstevel@tonic-gate #define	_PROF_DESC		"SolarisAttrLongDesc"
380Sstevel@tonic-gate #define	_PROF_ATTRS		"SolarisAttrKeyValue"
39*2830Sdjl /* Negate an exec_attr attribute to exclude exec_attr entries */
40*2830Sdjl #define	_PROF_GETPROFNAME \
41*2830Sdjl "(&(objectClass=SolarisProfAttr)(!(SolarisKernelSecurityPolicy=*))(cn=%s))"
420Sstevel@tonic-gate #define	_PROF_GETPROFNAME_SSD	"(&(%%s)(cn=%s))"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static const char *prof_attrs[] = {
450Sstevel@tonic-gate 	_PROF_NAME,
460Sstevel@tonic-gate 	_PROF_RES1,
470Sstevel@tonic-gate 	_PROF_RES2,
480Sstevel@tonic-gate 	_PROF_DESC,
490Sstevel@tonic-gate 	_PROF_ATTRS,
500Sstevel@tonic-gate 	(char *)NULL
510Sstevel@tonic-gate };
52*2830Sdjl /*
53*2830Sdjl  * _nss_ldap_prof2str is the data marshaling method for the prof_attr
54*2830Sdjl  * system call getprofattr, getprofnam and getproflist.
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  * All:::Execute any command as the user or role:help=RtAll.html
60*2830Sdjl  *
61*2830Sdjl  */
620Sstevel@tonic-gate static int
_nss_ldap_prof2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)63*2830Sdjl _nss_ldap_prof2str(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, **des, **attr;
71*2830Sdjl 	char			*res1_str, *res2_str, *des_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, _PROF_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_prof2str;
85*2830Sdjl 	}
86*2830Sdjl 	res1 = __ns_ldap_getAttr(result->entry, _PROF_RES1);
87*2830Sdjl 	if (res1 == NULL || res1[0] == NULL || (strlen(res1[0]) < 1))
88*2830Sdjl 		res1_str = _NO_VALUE;
89*2830Sdjl 	else
90*2830Sdjl 		res1_str = res1[0];
91*2830Sdjl 
92*2830Sdjl 	res2 = __ns_ldap_getAttr(result->entry, _PROF_RES2);
93*2830Sdjl 	if (res2 == NULL || res2[0] == NULL || (strlen(res2[0]) < 1))
94*2830Sdjl 		res2_str = _NO_VALUE;
95*2830Sdjl 	else
96*2830Sdjl 		res2_str = res2[0];
97*2830Sdjl 
98*2830Sdjl 	des = __ns_ldap_getAttr(result->entry, _PROF_DESC);
99*2830Sdjl 	if (des == NULL || des[0] == NULL || (strlen(des[0]) < 1))
100*2830Sdjl 		des_str = _NO_VALUE;
101*2830Sdjl 	else
102*2830Sdjl 		des_str = des[0];
103*2830Sdjl 
104*2830Sdjl 	attr = __ns_ldap_getAttr(result->entry, _PROF_ATTRS);
105*2830Sdjl 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
106*2830Sdjl 		attr_str = _NO_VALUE;
107*2830Sdjl 	else
108*2830Sdjl 		attr_str = attr[0];
109*2830Sdjl 	/* 5 = 4 ':' + 1 '\0' */
110*2830Sdjl 	len = strlen(name[0]) + strlen(res1_str) + strlen(res2_str) +
111*2830Sdjl 		strlen(des_str) + strlen(attr_str) + 6;
112*2830Sdjl 	if (len > buflen) {
113*2830Sdjl 		nss_result = NSS_STR_PARSE_ERANGE;
114*2830Sdjl 		goto result_prof2str;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
117*2830Sdjl 	if (argp->buf.result != NULL) {
118*2830Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
119*2830Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
120*2830Sdjl 			goto result_prof2str;
1210Sstevel@tonic-gate 		}
122*2830Sdjl 		buffer = be->buffer;
123*2830Sdjl 	} else
124*2830Sdjl 		buffer = argp->buf.buffer;
125*2830Sdjl 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s",
126*2830Sdjl 			name[0], res1_str, res2_str, des_str, attr_str);
127*2830Sdjl 	/* The front end marshaller doesn't need the trailing null */
128*2830Sdjl 	if (argp->buf.result != NULL)
129*2830Sdjl 		be->buflen = strlen(be->buffer);
1300Sstevel@tonic-gate 
131*2830Sdjl result_prof2str:
1320Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
133*2830Sdjl 	return (nss_result);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1380Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1410Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1420Sstevel@tonic-gate 	char		name[SEARCHFILTERLEN];
1430Sstevel@tonic-gate 	int		ret;
1440Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
1470Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1500Sstevel@tonic-gate 	    _PROF_GETPROFNAME, name);
1510Sstevel@tonic-gate 	if (ret < 0 || ret >= sizeof (searchfilter))
1520Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1550Sstevel@tonic-gate 	    _PROF_GETPROFNAME_SSD, name);
1560Sstevel@tonic-gate 	if (ret < 0 || ret >= sizeof (userdata))
1570Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	return (_nss_ldap_lookup(be, argp,
1600Sstevel@tonic-gate 	    _PROFATTR, searchfilter, NULL, _merge_SSD_filter, userdata));
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate static ldap_backend_op_t profattr_ops[] = {
1650Sstevel@tonic-gate 	_nss_ldap_destr,
1660Sstevel@tonic-gate 	_nss_ldap_endent,
1670Sstevel@tonic-gate 	_nss_ldap_setent,
1680Sstevel@tonic-gate 	_nss_ldap_getent,
1690Sstevel@tonic-gate 	getbyname
1700Sstevel@tonic-gate };
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*ARGSUSED0*/
1740Sstevel@tonic-gate nss_backend_t *
_nss_ldap_prof_attr_constr(const char * dummy1,const char * dummy2,const char * dummy3,const char * dummy4,const char * dummy5)1750Sstevel@tonic-gate _nss_ldap_prof_attr_constr(const char *dummy1,
1760Sstevel@tonic-gate     const char *dummy2,
1770Sstevel@tonic-gate     const char *dummy3,
1780Sstevel@tonic-gate     const char *dummy4,
1790Sstevel@tonic-gate     const char *dummy5)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(profattr_ops,
1820Sstevel@tonic-gate 		sizeof (profattr_ops)/sizeof (profattr_ops[0]), _PROFATTR,
183*2830Sdjl 		prof_attrs, _nss_ldap_prof2str));
1840Sstevel@tonic-gate }
185