xref: /onnv-gate/usr/src/lib/nsswitch/ldap/common/getprojent.c (revision 6258:eb57169ca8ba)
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
52830Sdjl  * Common Development and Distribution License (the "License").
62830Sdjl  * 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*6258Smj162486  * Copyright 2008 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 /*
532830Sdjl  * _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
562830Sdjl  * will parse the ldap search values into the file format.
572830Sdjl  * e.g.
582830Sdjl  *
592830Sdjl  * system:0:System:::
602830Sdjl  *
612830Sdjl  * beatles:100:The Beatles:john,paul,george,ringo::task.max-lwps=
622830Sdjl  * 	(privileged,100,signal=SIGTERM),(privileged,110,deny)
632830Sdjl  *
642830Sdjl  * (All in one line)
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static int
_nss_ldap_proj2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)672830Sdjl _nss_ldap_proj2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
680Sstevel@tonic-gate {
69*6258Smj162486 	int			i;
70*6258Smj162486 	int			nss_result;
71*6258Smj162486 	int			buflen = 0, len;
72*6258Smj162486 	int			firsttime;
73*6258Smj162486 	char			*buffer, *comment, *attr_str;
74*6258Smj162486 	ns_ldap_result_t	*result = be->result;
75*6258Smj162486 	char			**name, **id, **descr, **attr;
76*6258Smj162486 	ns_ldap_attr_t		*users, *groups;
772830Sdjl 
782830Sdjl 	if (result == NULL)
792830Sdjl 		return (NSS_STR_PARSE_PARSE);
802830Sdjl 	buflen = argp->buf.buflen;
812830Sdjl 
82*6258Smj162486 	if (argp->buf.result != NULL) {
83*6258Smj162486 		/* In all cases it must be deallocated by caller */
84*6258Smj162486 		if ((be->buffer = calloc(1, buflen)) == NULL) {
85*6258Smj162486 			nss_result = NSS_STR_PARSE_PARSE;
86*6258Smj162486 			goto result_proj2str;
87*6258Smj162486 		}
88*6258Smj162486 		buffer = be->buffer;
89*6258Smj162486 	} else
90*6258Smj162486 		buffer = argp->buf.buffer;
91*6258Smj162486 
922830Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
93*6258Smj162486 	(void) memset(buffer, 0, buflen);
940Sstevel@tonic-gate 
952830Sdjl 	name = __ns_ldap_getAttr(result->entry, _PROJ_NAME);
962830Sdjl 	if (name == NULL || name[0] == NULL || (strlen(name[0]) < 1)) {
972830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
982830Sdjl 		goto result_proj2str;
992830Sdjl 	}
1002830Sdjl 	id = __ns_ldap_getAttr(result->entry, _PROJ_PROJID);
1012830Sdjl 	if (id == NULL || id[0] == NULL || (strlen(id[0]) < 1)) {
1022830Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
1032830Sdjl 		goto result_proj2str;
1040Sstevel@tonic-gate 	}
1052830Sdjl 	descr = __ns_ldap_getAttr(result->entry, _PROJ_DESCR);
1062830Sdjl 	if (descr == NULL || descr[0] == NULL || (strlen(descr[0]) < 1))
1072830Sdjl 		comment = _NO_VALUE;
1082830Sdjl 	else
1092830Sdjl 		comment = descr[0];
110*6258Smj162486 	len = snprintf(buffer, buflen, "%s:%s:%s:", name[0], id[0],
111*6258Smj162486 	    comment);
112*6258Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
1132830Sdjl 
114*6258Smj162486 	users = __ns_ldap_getAttrStruct(result->entry, _PROJ_USERS);
115*6258Smj162486 	if (!(users == NULL || users->attrvalue == NULL)) {
116*6258Smj162486 		firsttime = 1;
117*6258Smj162486 		for (i = 0; i < users->value_count; i++) {
118*6258Smj162486 			if (users->attrvalue[i] == NULL) {
119*6258Smj162486 				nss_result = NSS_STR_PARSE_PARSE;
120*6258Smj162486 				goto result_proj2str;
121*6258Smj162486 			}
122*6258Smj162486 			if (firsttime) {
123*6258Smj162486 				len = snprintf(buffer, buflen, "%s",
124*6258Smj162486 				    users->attrvalue[i]);
125*6258Smj162486 				firsttime = 0;
126*6258Smj162486 			} else {
127*6258Smj162486 				len = snprintf(buffer, buflen, ",%s",
128*6258Smj162486 				    users->attrvalue[i]);
129*6258Smj162486 			}
130*6258Smj162486 			TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
131*6258Smj162486 		}
132*6258Smj162486 	}
133*6258Smj162486 	len = snprintf(buffer, buflen, ":");
134*6258Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
1352830Sdjl 
136*6258Smj162486 	groups = __ns_ldap_getAttrStruct(result->entry, _PROJ_GROUPS);
137*6258Smj162486 	if (!(groups == NULL || groups->attrvalue == NULL)) {
138*6258Smj162486 		firsttime = 1;
139*6258Smj162486 		for (i = 0; i < groups->value_count; i++) {
140*6258Smj162486 			if (groups->attrvalue[i] == NULL) {
141*6258Smj162486 				nss_result = NSS_STR_PARSE_PARSE;
142*6258Smj162486 				goto result_proj2str;
143*6258Smj162486 			}
144*6258Smj162486 			if (firsttime) {
145*6258Smj162486 				len = snprintf(buffer, buflen, "%s",
146*6258Smj162486 				    groups->attrvalue[i]);
147*6258Smj162486 				firsttime = 0;
148*6258Smj162486 			} else {
149*6258Smj162486 				len = snprintf(buffer, buflen, ",%s",
150*6258Smj162486 				    groups->attrvalue[i]);
151*6258Smj162486 			}
152*6258Smj162486 			TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
153*6258Smj162486 		}
154*6258Smj162486 	}
1552830Sdjl 
1562830Sdjl 	attr = __ns_ldap_getAttr(result->entry, _PROJ_ATTR);
1572830Sdjl 	if (attr == NULL || attr[0] == NULL || (strlen(attr[0]) < 1))
1582830Sdjl 		attr_str = _NO_VALUE;
1592830Sdjl 
1602830Sdjl 	else
1612830Sdjl 		attr_str = attr[0];
162*6258Smj162486 	len = snprintf(buffer, buflen, ":%s", attr_str);
163*6258Smj162486 	TEST_AND_ADJUST(len, buffer, buflen, result_proj2str);
1642830Sdjl 
165*6258Smj162486 	/* The front end marshaller doesn't need the trailing nulls */
166*6258Smj162486 	if (argp->buf.result != NULL)
167*6258Smj162486 		be->buflen = strlen(be->buffer);
1682830Sdjl result_proj2str:
1690Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
1700Sstevel@tonic-gate 	return ((int)nss_result);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * getbyname gets a project entry by name. This function constructs an ldap
1760Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojname search
1770Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1780Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
1790Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
1800Sstevel@tonic-gate  */
1810Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1820Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1850Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	if (snprintf(searchfilter, SEARCHFILTERLEN,
188*6258Smj162486 	    _F_GETPROJNAME, argp->key.name) < 0)
1890Sstevel@tonic-gate 		return (NSS_NOTFOUND);
190*6258Smj162486 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL, NULL,
191*6258Smj162486 	    NULL));
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * getbyprojid gets a project entry by number. This function constructs an ldap
1970Sstevel@tonic-gate  * search filter using the name invocation parameter and the getprojid search
1980Sstevel@tonic-gate  * filter defined. Once the filter is constructed, we search for a matching
1990Sstevel@tonic-gate  * entry and marshal the data results into struct project for the frontend
2000Sstevel@tonic-gate  * process. The function _nss_ldap_proj2ent performs the data marshaling.
2010Sstevel@tonic-gate  */
2020Sstevel@tonic-gate static nss_status_t
getbyprojid(ldap_backend_ptr be,void * a)2030Sstevel@tonic-gate getbyprojid(ldap_backend_ptr be, void *a)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
2060Sstevel@tonic-gate 	char searchfilter[SEARCHFILTERLEN];
2070Sstevel@tonic-gate 
208*6258Smj162486 	if (snprintf(searchfilter, SEARCHFILTERLEN, _F_GETPROJID,
209*6258Smj162486 	    (long)argp->key.projid) < 0)
2100Sstevel@tonic-gate 		return (NSS_NOTFOUND);
211*6258Smj162486 	return (_nss_ldap_lookup(be, argp, _PROJECT, searchfilter, NULL, NULL,
212*6258Smj162486 	    NULL));
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate static ldap_backend_op_t project_ops[] = {
2160Sstevel@tonic-gate 	_nss_ldap_destr,
2170Sstevel@tonic-gate 	_nss_ldap_endent,
2180Sstevel@tonic-gate 	_nss_ldap_setent,
2190Sstevel@tonic-gate 	_nss_ldap_getent,
2200Sstevel@tonic-gate 	getbyname,
2210Sstevel@tonic-gate 	getbyprojid
2220Sstevel@tonic-gate };
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /*ARGSUSED0*/
2260Sstevel@tonic-gate nss_backend_t *
_nss_ldap_project_constr(const char * dummy1,const char * dummy2,const char * dummy3)2270Sstevel@tonic-gate _nss_ldap_project_constr(const char *dummy1, const char *dummy2,
2280Sstevel@tonic-gate     const char *dummy3)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	return (_nss_ldap_constr(project_ops,
2310Sstevel@tonic-gate 	    sizeof (project_ops) / sizeof (project_ops[0]),
2322830Sdjl 	    _PROJECT, project_attrs, _nss_ldap_proj2str));
2330Sstevel@tonic-gate }
234