xref: /titanic_51/usr/src/lib/nsswitch/ldap/common/getprojent.c (revision 8bd1bae7b80c19127d25a15440e161140bcaacea)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*8bd1bae7Smj162486  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <project.h>
297c478bd9Sstevel@tonic-gate #include "ldap_common.h"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* Project attributes filters */
327c478bd9Sstevel@tonic-gate #define	_PROJ_NAME	"SolarisProjectName"
337c478bd9Sstevel@tonic-gate #define	_PROJ_PROJID	"SolarisProjectID"
347c478bd9Sstevel@tonic-gate #define	_PROJ_DESCR	"description"
357c478bd9Sstevel@tonic-gate #define	_PROJ_USERS	"memberUid"
367c478bd9Sstevel@tonic-gate #define	_PROJ_GROUPS	"memberGid"
377c478bd9Sstevel@tonic-gate #define	_PROJ_ATTR	"SolarisProjectAttr"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	_F_GETPROJNAME	"(&(objectClass=SolarisProject)(SolarisProjectName=%s))"
407c478bd9Sstevel@tonic-gate #define	_F_GETPROJID	"(&(objectClass=SolarisProject)(SolarisProjectID=%ld))"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static const char *project_attrs[] = {
437c478bd9Sstevel@tonic-gate 	_PROJ_NAME,
447c478bd9Sstevel@tonic-gate 	_PROJ_PROJID,
457c478bd9Sstevel@tonic-gate 	_PROJ_DESCR,
467c478bd9Sstevel@tonic-gate 	_PROJ_USERS,
477c478bd9Sstevel@tonic-gate 	_PROJ_GROUPS,
487c478bd9Sstevel@tonic-gate 	_PROJ_ATTR,
497c478bd9Sstevel@tonic-gate 	(char *)NULL
507c478bd9Sstevel@tonic-gate };
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
53cb5caa98Sdjl  * _nss_ldap_proj2str is the data marshalling method for the project getXbyY
547c478bd9Sstevel@tonic-gate  * (getprojbyname, getprojbyid, getprojent) backend processes. This method
557c478bd9Sstevel@tonic-gate  * is called after a successful ldap search has been performed. This method
56cb5caa98Sdjl  * will parse the ldap search values into the file format.
57cb5caa98Sdjl  * e.g.
58cb5caa98Sdjl  *
59cb5caa98Sdjl  * system:0:System:::
60cb5caa98Sdjl  *
61cb5caa98Sdjl  * beatles:100:The Beatles:john,paul,george,ringo::task.max-lwps=
62cb5caa98Sdjl  * 	(privileged,100,signal=SIGTERM),(privileged,110,deny)
63cb5caa98Sdjl  *
64cb5caa98Sdjl  * (All in one line)
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static int
67cb5caa98Sdjl _nss_ldap_proj2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
687c478bd9Sstevel@tonic-gate {
69*8bd1bae7Smj162486 	int			i;
70*8bd1bae7Smj162486 	int			nss_result;
71*8bd1bae7Smj162486 	int			buflen = 0, len;
72*8bd1bae7Smj162486 	int			firsttime;
73*8bd1bae7Smj162486 	char			*buffer, *comment, *attr_str;
747c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
75*8bd1bae7Smj162486 	char			**name, **id, **descr, **attr;
76*8bd1bae7Smj162486 	ns_ldap_attr_t		*users, *groups;
777c478bd9Sstevel@tonic-gate 
78cb5caa98Sdjl 	if (result == NULL)
79cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
80cb5caa98Sdjl 	buflen = argp->buf.buflen;
81cb5caa98Sdjl 
82*8bd1bae7Smj162486 	if (argp->buf.result != NULL) {
83*8bd1bae7Smj162486 		/* In all cases it must be deallocated by caller */
84*8bd1bae7Smj162486 		if ((be->buffer = calloc(1, buflen)) == NULL) {
85*8bd1bae7Smj162486 			nss_result = NSS_STR_PARSE_PARSE;
86*8bd1bae7Smj162486 			goto result_proj2str;
87*8bd1bae7Smj162486 		}
88*8bd1bae7Smj162486 		buffer = be->buffer;
89*8bd1bae7Smj162486 	} else
90*8bd1bae7Smj162486 		buffer = argp->buf.buffer;
91*8bd1bae7Smj162486 
927c478bd9Sstevel@tonic-gate 	nss_result = NSS_STR_PARSE_SUCCESS;
93*8bd1bae7Smj162486 	(void) memset(buffer, 0, buflen);
947c478bd9Sstevel@tonic-gate 
95cb5caa98Sdjl 	name = __ns_ldap_getAttr(result->entry, _PROJ_NAME);
96cb5caa98Sdjl 	if (name == NULL || name[0] == NULL || (strlen(name[0]) < 1)) {
97cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
98cb5caa98Sdjl 		goto result_proj2str;
99cb5caa98Sdjl 	}
100cb5caa98Sdjl 	id = __ns_ldap_getAttr(result->entry, _PROJ_PROJID);
101cb5caa98Sdjl 	if (id == NULL || id[0] == NULL || (strlen(id[0]) < 1)) {
102cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
103cb5caa98Sdjl 		goto result_proj2str;
104cb5caa98Sdjl 	}
105cb5caa98Sdjl 	descr = __ns_ldap_getAttr(result->entry, _PROJ_DESCR);
106cb5caa98Sdjl 	if (descr == NULL || descr[0] == NULL || (strlen(descr[0]) < 1))
107cb5caa98Sdjl 		comment = _NO_VALUE;
108cb5caa98Sdjl 	else
109cb5caa98Sdjl 		comment = descr[0];
110*8bd1bae7Smj162486 	len = snprintf(buffer, buflen, "%s:%s:%s:", name[0], id[0],
111*8bd1bae7Smj162486 	    comment);
112*8bd1bae7Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
113cb5caa98Sdjl 
114*8bd1bae7Smj162486 	users = __ns_ldap_getAttrStruct(result->entry, _PROJ_USERS);
115*8bd1bae7Smj162486 	if (!(users == NULL || users->attrvalue == NULL)) {
116*8bd1bae7Smj162486 		firsttime = 1;
117*8bd1bae7Smj162486 		for (i = 0; i < users->value_count; i++) {
118*8bd1bae7Smj162486 			if (users->attrvalue[i] == NULL) {
119*8bd1bae7Smj162486 				nss_result = NSS_STR_PARSE_PARSE;
120*8bd1bae7Smj162486 				goto result_proj2str;
121*8bd1bae7Smj162486 			}
122*8bd1bae7Smj162486 			if (firsttime) {
123*8bd1bae7Smj162486 				len = snprintf(buffer, buflen, "%s",
124*8bd1bae7Smj162486 				    users->attrvalue[i]);
125*8bd1bae7Smj162486 				firsttime = 0;
126*8bd1bae7Smj162486 			} else {
127*8bd1bae7Smj162486 				len = snprintf(buffer, buflen, ",%s",
128*8bd1bae7Smj162486 				    users->attrvalue[i]);
129*8bd1bae7Smj162486 			}
130*8bd1bae7Smj162486 			TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
131*8bd1bae7Smj162486 		}
132*8bd1bae7Smj162486 	}
133*8bd1bae7Smj162486 	len = snprintf(buffer, buflen, ":");
134*8bd1bae7Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
135cb5caa98Sdjl 
136*8bd1bae7Smj162486 	groups = __ns_ldap_getAttrStruct(result->entry, _PROJ_GROUPS);
137*8bd1bae7Smj162486 	if (!(groups == NULL || groups->attrvalue == NULL)) {
138*8bd1bae7Smj162486 		firsttime = 1;
139*8bd1bae7Smj162486 		for (i = 0; i < groups->value_count; i++) {
140*8bd1bae7Smj162486 			if (groups->attrvalue[i] == NULL) {
141*8bd1bae7Smj162486 				nss_result = NSS_STR_PARSE_PARSE;
142*8bd1bae7Smj162486 				goto result_proj2str;
143*8bd1bae7Smj162486 			}
144*8bd1bae7Smj162486 			if (firsttime) {
145*8bd1bae7Smj162486 				len = snprintf(buffer, buflen, "%s",
146*8bd1bae7Smj162486 				    groups->attrvalue[i]);
147*8bd1bae7Smj162486 				firsttime = 0;
148*8bd1bae7Smj162486 			} else {
149*8bd1bae7Smj162486 				len = snprintf(buffer, buflen, ",%s",
150*8bd1bae7Smj162486 				    groups->attrvalue[i]);
151*8bd1bae7Smj162486 			}
152*8bd1bae7Smj162486 			TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
153*8bd1bae7Smj162486 		}
154*8bd1bae7Smj162486 	}
155cb5caa98Sdjl 
156cb5caa98Sdjl 	attr = __ns_ldap_getAttr(result->entry, _PROJ_ATTR);
157cb5caa98Sdjl 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
158cb5caa98Sdjl 		attr_str = _NO_VALUE;
159cb5caa98Sdjl 
160cb5caa98Sdjl 	else
161cb5caa98Sdjl 		attr_str = attr[0];
162*8bd1bae7Smj162486 	len = snprintf(buffer, buflen, ":%s", attr_str);
163*8bd1bae7Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
164cb5caa98Sdjl 
165*8bd1bae7Smj162486 	/* The front end marshaller doesn't need the trailing nulls */
166*8bd1bae7Smj162486 	if (argp->buf.result != NULL)
167*8bd1bae7Smj162486 		be->buflen = strlen(be->buffer);
168cb5caa98Sdjl result_proj2str:
1697c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1707c478bd9Sstevel@tonic-gate 	return ((int)nss_result);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * getbyname gets a project entry by name. This function constructs an ldap
1767c478bd9Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojname search
1777c478bd9Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1787c478bd9Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
1797c478bd9Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate static nss_status_t
1827c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1857c478bd9Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
1887c478bd9Sstevel@tonic-gate 	    _F_GETPROJNAME, argp->key.name) < 0)
1897c478bd9Sstevel@tonic-gate 		return (NSS_NOTFOUND);
190*8bd1bae7Smj162486 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL, NULL,
191*8bd1bae7Smj162486 	    NULL));
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * getbyprojid gets a project entry by number. This function constructs an ldap
1977c478bd9Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojid search
1987c478bd9Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1997c478bd9Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
2007c478bd9Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate static nss_status_t
2037c478bd9Sstevel@tonic-gate getbyprojid(ldap_backend_ptr be, void *a)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2067c478bd9Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
2077c478bd9Sstevel@tonic-gate 
208*8bd1bae7Smj162486 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETPROJID,
209*8bd1bae7Smj162486 	    (long)argp->key.projid) < 0)
2107c478bd9Sstevel@tonic-gate 		return (NSS_NOTFOUND);
211*8bd1bae7Smj162486 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL, NULL,
212*8bd1bae7Smj162486 	    NULL));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static ldap_backend_op_t project_ops[] = {
2167c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
2177c478bd9Sstevel@tonic-gate 	_nss_ldap_endent,
2187c478bd9Sstevel@tonic-gate 	_nss_ldap_setent,
2197c478bd9Sstevel@tonic-gate 	_nss_ldap_getent,
2207c478bd9Sstevel@tonic-gate 	getbyname,
2217c478bd9Sstevel@tonic-gate 	getbyprojid
2227c478bd9Sstevel@tonic-gate };
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
2267c478bd9Sstevel@tonic-gate nss_backend_t *
2277c478bd9Sstevel@tonic-gate _nss_ldap_project_constr(const char *dummy1, const char *dummy2,
2287c478bd9Sstevel@tonic-gate     const char *dummy3)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	return (_nss_ldap_constr(project_ops,
2317c478bd9Sstevel@tonic-gate 	    sizeof (project_ops) / sizeof (project_ops[0]),
232cb5caa98Sdjl 	    _PROJECT, project_attrs, _nss_ldap_proj2str));
2337c478bd9Sstevel@tonic-gate }
234