xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getprojent.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 <project.h>
290Sstevel@tonic-gate #include "ldap_common.h"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /* Project attributes filters */
320Sstevel@tonic-gate #define	_PROJ_NAME	"SolarisProjectName"
330Sstevel@tonic-gate #define	_PROJ_PROJID	"SolarisProjectID"
340Sstevel@tonic-gate #define	_PROJ_DESCR	"description"
350Sstevel@tonic-gate #define	_PROJ_USERS	"memberUid"
360Sstevel@tonic-gate #define	_PROJ_GROUPS	"memberGid"
370Sstevel@tonic-gate #define	_PROJ_ATTR	"SolarisProjectAttr"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #define	_F_GETPROJNAME	"(&(objectClass=SolarisProject)(SolarisProjectName=%s))"
400Sstevel@tonic-gate #define	_F_GETPROJID	"(&(objectClass=SolarisProject)(SolarisProjectID=%ld))"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate static const char *project_attrs[] = {
430Sstevel@tonic-gate 	_PROJ_NAME,
440Sstevel@tonic-gate 	_PROJ_PROJID,
450Sstevel@tonic-gate 	_PROJ_DESCR,
460Sstevel@tonic-gate 	_PROJ_USERS,
470Sstevel@tonic-gate 	_PROJ_GROUPS,
480Sstevel@tonic-gate 	_PROJ_ATTR,
490Sstevel@tonic-gate 	(char *)NULL
500Sstevel@tonic-gate };
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
53*2830Sdjl  * _nss_ldap_proj2str is the data marshalling method for the project getXbyY
540Sstevel@tonic-gate  * (getprojbyname, getprojbyid, getprojent) backend processes. This method
550Sstevel@tonic-gate  * is called after a successful ldap search has been performed. This method
56*2830Sdjl  * will parse the ldap search values into the file format.
57*2830Sdjl  * e.g.
58*2830Sdjl  *
59*2830Sdjl  * system:0:System:::
60*2830Sdjl  *
61*2830Sdjl  * beatles:100:The Beatles:john,paul,george,ringo::task.max-lwps=
62*2830Sdjl  * 	(privileged,100,signal=SIGTERM),(privileged,110,deny)
63*2830Sdjl  *
64*2830Sdjl  * (All in one line)
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static int
67*2830Sdjl _nss_ldap_proj2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
680Sstevel@tonic-gate {
69*2830Sdjl 	int nss_result, buflen;
700Sstevel@tonic-gate 	unsigned long len = 0;
71*2830Sdjl 	char *buffer, *comment, *user_str, *group_str, *attr_str;
720Sstevel@tonic-gate 	ns_ldap_result_t *result = be->result;
73*2830Sdjl 	char **name, **id, **descr, **users, **groups, **attr;
74*2830Sdjl 
75*2830Sdjl 	if (result == NULL)
76*2830Sdjl 		return (NSS_STR_PARSE_PARSE);
77*2830Sdjl 	buflen = argp->buf.buflen;
78*2830Sdjl 
79*2830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
80*2830Sdjl 	(void) memset(argp->buf.buffer, 0, buflen);
810Sstevel@tonic-gate 
82*2830Sdjl 	name = __ns_ldap_getAttr(result->entry, _PROJ_NAME);
83*2830Sdjl 	if (name == NULL || name[0] == NULL || (strlen(name[0]) < 1)) {
84*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
85*2830Sdjl 		goto result_proj2str;
86*2830Sdjl 	}
87*2830Sdjl 	id = __ns_ldap_getAttr(result->entry, _PROJ_PROJID);
88*2830Sdjl 	if (id == NULL || id[0] == NULL || (strlen(id[0]) < 1)) {
89*2830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
90*2830Sdjl 		goto result_proj2str;
910Sstevel@tonic-gate 	}
92*2830Sdjl 	descr = __ns_ldap_getAttr(result->entry, _PROJ_DESCR);
93*2830Sdjl 	if (descr == NULL || descr[0] == NULL || (strlen(descr[0]) < 1))
94*2830Sdjl 		comment = _NO_VALUE;
95*2830Sdjl 
96*2830Sdjl 	else
97*2830Sdjl 		comment = descr[0];
98*2830Sdjl 
99*2830Sdjl 	users = __ns_ldap_getAttr(result->entry, _PROJ_USERS);
100*2830Sdjl 	if (users == NULL || users[0] == NULL || (strlen(users[0]) < 1))
101*2830Sdjl 		user_str = _NO_VALUE;
102*2830Sdjl 
103*2830Sdjl 	else
104*2830Sdjl 		user_str = users[0];
105*2830Sdjl 
106*2830Sdjl 	groups = __ns_ldap_getAttr(result->entry, _PROJ_GROUPS);
107*2830Sdjl 	if (groups == NULL || groups[0] == NULL || (strlen(groups[0]) < 1))
108*2830Sdjl 		group_str = _NO_VALUE;
109*2830Sdjl 
110*2830Sdjl 	else
111*2830Sdjl 		group_str = groups[0];
112*2830Sdjl 
113*2830Sdjl 	attr = __ns_ldap_getAttr(result->entry, _PROJ_ATTR);
114*2830Sdjl 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
115*2830Sdjl 		attr_str = _NO_VALUE;
116*2830Sdjl 
117*2830Sdjl 	else
118*2830Sdjl 		attr_str = attr[0];
119*2830Sdjl 
120*2830Sdjl 	/* 6 = 5 ':' + 1 '\0' */
121*2830Sdjl 	len = strlen(name[0]) + strlen(id[0]) + strlen(comment) +
122*2830Sdjl 		strlen(user_str) + strlen(group_str) + strlen(attr_str) + 6;
123*2830Sdjl 	if (len >= buflen) {
124*2830Sdjl 		nss_result = NSS_STR_PARSE_ERANGE;
125*2830Sdjl 		goto result_proj2str;
1260Sstevel@tonic-gate 	}
127*2830Sdjl 	if (argp->buf.result != NULL) {
128*2830Sdjl 		if ((be->buffer = calloc(1, len)) == NULL) {
1290Sstevel@tonic-gate 			nss_result = NSS_STR_PARSE_PARSE;
130*2830Sdjl 			goto result_proj2str;
1310Sstevel@tonic-gate 		}
132*2830Sdjl 		buffer = be->buffer;
133*2830Sdjl 		/* The front end marshaller does not need trailing nulls */
134*2830Sdjl 		be->buflen = len - 1;
135*2830Sdjl 	} else
136*2830Sdjl 		buffer = argp->buf.buffer;
1370Sstevel@tonic-gate 
138*2830Sdjl 	(void) snprintf(buffer, len, "%s:%s:%s:%s:%s:%s", name[0], id[0],
139*2830Sdjl 			comment, user_str, group_str, attr_str);
140*2830Sdjl 
141*2830Sdjl result_proj2str:
1420Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1430Sstevel@tonic-gate 	return ((int)nss_result);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate /*
1480Sstevel@tonic-gate  * getbyname gets a project entry by name. This function constructs an ldap
1490Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojname search
1500Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1510Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
1520Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate static nss_status_t
1550Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1580Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
1610Sstevel@tonic-gate 		_F_GETPROJNAME, argp->key.name) < 0)
1620Sstevel@tonic-gate 		return (NSS_NOTFOUND);
1630Sstevel@tonic-gate 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL,
1640Sstevel@tonic-gate 			NULL, NULL));
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * getbyprojid gets a project entry by number. This function constructs an ldap
1700Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojid search
1710Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1720Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
1730Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate static nss_status_t
1760Sstevel@tonic-gate getbyprojid(ldap_backend_ptr be, void *a)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1790Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
1820Sstevel@tonic-gate 		_F_GETPROJID, (long)argp->key.projid) < 0)
1830Sstevel@tonic-gate 		return (NSS_NOTFOUND);
1840Sstevel@tonic-gate 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL,
1850Sstevel@tonic-gate 			NULL, NULL));
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate static ldap_backend_op_t project_ops[] = {
1890Sstevel@tonic-gate 	_nss_ldap_destr,
1900Sstevel@tonic-gate 	_nss_ldap_endent,
1910Sstevel@tonic-gate 	_nss_ldap_setent,
1920Sstevel@tonic-gate 	_nss_ldap_getent,
1930Sstevel@tonic-gate 	getbyname,
1940Sstevel@tonic-gate 	getbyprojid
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*ARGSUSED0*/
1990Sstevel@tonic-gate nss_backend_t *
2000Sstevel@tonic-gate _nss_ldap_project_constr(const char *dummy1, const char *dummy2,
2010Sstevel@tonic-gate     const char *dummy3)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	return (_nss_ldap_constr(project_ops,
2040Sstevel@tonic-gate 	    sizeof (project_ops) / sizeof (project_ops[0]),
205*2830Sdjl 	    _PROJECT, project_attrs, _nss_ldap_proj2str));
2060Sstevel@tonic-gate }
207