xref: /titanic_54/usr/src/lib/nsswitch/ldap/common/getbootparams.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * 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*cb5caa98Sdjl  * Copyright 2006 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 "ldap_common.h"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* bootparams attributes filters */
317c478bd9Sstevel@tonic-gate #define	_B_HOSTNAME		"cn"
327c478bd9Sstevel@tonic-gate #define	_B_PARAMETER		"bootparameter"
337c478bd9Sstevel@tonic-gate #define	_F_GETBOOTPARAMBYNAME	"(&(objectClass=bootableDevice)(cn=%s))"
347c478bd9Sstevel@tonic-gate #define	_F_GETBOOTPARAMBYNAME_SSD "(&(%%s)(cn=%s))"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static const char *bootparams_attrs[] = {
377c478bd9Sstevel@tonic-gate 	_B_HOSTNAME,
387c478bd9Sstevel@tonic-gate 	_B_PARAMETER,
397c478bd9Sstevel@tonic-gate 	(char *)NULL
407c478bd9Sstevel@tonic-gate };
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
43*cb5caa98Sdjl  * _nss_ldap_bootparams2str is the data marshaling method for the
44*cb5caa98Sdjl  * bootparams bootparams_getbyname backend processes.
45*cb5caa98Sdjl  * This method is called after a successful ldap search has been performed.
46*cb5caa98Sdjl  * This method will parse the ldap search values into the file format.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * A host's bootparameters are returned on one line separated by white
49*cb5caa98Sdjl  * space. The LDAP server stores each boot parameter as a separate entry.
50*cb5caa98Sdjl  * If more than one bootparameter is available, a white space separated buffer
517c478bd9Sstevel@tonic-gate  * must be constructed and returned.
52*cb5caa98Sdjl  *
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int
_nss_ldap_bootparams2str(ldap_backend_ptr be,nss_XbyY_args_t * argp)56*cb5caa98Sdjl _nss_ldap_bootparams2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
577c478bd9Sstevel@tonic-gate {
58*cb5caa98Sdjl 	uint_t		i;
59*cb5caa98Sdjl 	int		buflen = 0, len = 0;
60*cb5caa98Sdjl 	int		nss_result, firsttime;
61*cb5caa98Sdjl 	ns_ldap_attr_t	*bparams;
62*cb5caa98Sdjl 	char		*buffer, **names;
637c478bd9Sstevel@tonic-gate 	ns_ldap_result_t	*result = be->result;
647c478bd9Sstevel@tonic-gate 
65*cb5caa98Sdjl 	if (result == NULL)
66*cb5caa98Sdjl 		return (NSS_STR_PARSE_PARSE);
67*cb5caa98Sdjl 	buflen = argp->buf.buflen;
68*cb5caa98Sdjl 	if (argp->buf.result != NULL) {
69*cb5caa98Sdjl 		if ((be->buffer = calloc(1, buflen)) == NULL) {
70*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
71*cb5caa98Sdjl 			goto result_bp2str;
72*cb5caa98Sdjl 		}
73*cb5caa98Sdjl 		buffer = be->buffer;
74*cb5caa98Sdjl 	} else
757c478bd9Sstevel@tonic-gate 		buffer = argp->buf.buffer;
767c478bd9Sstevel@tonic-gate 
77*cb5caa98Sdjl 	nss_result = NSS_STR_PARSE_SUCCESS;
78*cb5caa98Sdjl 	(void) memset(argp->buf.buffer, 0, buflen);
797c478bd9Sstevel@tonic-gate 
80*cb5caa98Sdjl 	names = __ns_ldap_getAttr(result->entry, _B_HOSTNAME);
81*cb5caa98Sdjl 	if (names == NULL || names[0] == NULL ||
82*cb5caa98Sdjl 			(strlen(names[0]) < 1)) {
83*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
84*cb5caa98Sdjl 		goto result_bp2str;
857c478bd9Sstevel@tonic-gate 	}
86*cb5caa98Sdjl 	bparams = __ns_ldap_getAttrStruct(result->entry, _B_PARAMETER);
87*cb5caa98Sdjl 	if (bparams == NULL || bparams->attrvalue == NULL) {
88*cb5caa98Sdjl 		nss_result = NSS_STR_PARSE_PARSE;
89*cb5caa98Sdjl 		goto result_bp2str;
90*cb5caa98Sdjl 	}
91*cb5caa98Sdjl 	firsttime = 1;
92*cb5caa98Sdjl 	for (i = 0; i < bparams->value_count; i++) {
93*cb5caa98Sdjl 		if (bparams->attrvalue[i] == NULL) {
94*cb5caa98Sdjl 			nss_result = NSS_STR_PARSE_PARSE;
95*cb5caa98Sdjl 			goto result_bp2str;
96*cb5caa98Sdjl 		}
97*cb5caa98Sdjl 		/*
98*cb5caa98Sdjl 		 * Skip client host name. The early version of ldapaddent
99*cb5caa98Sdjl 		 * adds hostname as a boot param and it should be filtered.
100*cb5caa98Sdjl 		 */
101*cb5caa98Sdjl 		if (strcasecmp(names[0], bparams->attrvalue[i]) != 0) {
102*cb5caa98Sdjl 			if (firsttime) {
103*cb5caa98Sdjl 				firsttime = 0;
104*cb5caa98Sdjl 				len = snprintf(buffer, buflen, "%s",
105*cb5caa98Sdjl 					bparams->attrvalue[i]);
106*cb5caa98Sdjl 			} else
107*cb5caa98Sdjl 				len = snprintf(buffer, buflen, " %s",
108*cb5caa98Sdjl 					bparams->attrvalue[i]);
109*cb5caa98Sdjl 			TEST_AND_ADJUST(len, buffer, buflen, result_bp2str);
110*cb5caa98Sdjl 		}
111*cb5caa98Sdjl 	}
112*cb5caa98Sdjl 	/* The front end marshaller doesn't need to copy trailing nulls */
113*cb5caa98Sdjl 	if (argp->buf.result != NULL)
114*cb5caa98Sdjl 		be->buflen = strlen(be->buffer);
1157c478bd9Sstevel@tonic-gate 
116*cb5caa98Sdjl result_bp2str:
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	(void) __ns_ldap_freeResult(&be->result);
119*cb5caa98Sdjl 	return (nss_result);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * getbyname gets bootparameters by host name. This function constructs an
1247c478bd9Sstevel@tonic-gate  * ldap search filter using the host name invocation parameter and the
1257c478bd9Sstevel@tonic-gate  * getbootparambyname search filter defined. Once the filter is
1267c478bd9Sstevel@tonic-gate  * constructed, we search for matching entries and marshal the data
1277c478bd9Sstevel@tonic-gate  * results into argp->buf.buffer for the frontend process. The function
1287c478bd9Sstevel@tonic-gate  * _nss_ldap_bootparams2ent performs the data marshaling.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * RFC 2307, An Approach for Using LDAP as a Network Information Service,
1317c478bd9Sstevel@tonic-gate  * indicates that dn's be fully qualified. Host name searches will be on
1327c478bd9Sstevel@tonic-gate  * fully qualified host names (e.g., foo.bar.sun.com).
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(ldap_backend_ptr be,void * a)1367c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	char		hostname[3 * MAXHOSTNAMELEN];
1397c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
1407c478bd9Sstevel@tonic-gate 	char		searchfilter[SEARCHFILTERLEN];
1417c478bd9Sstevel@tonic-gate 	char		userdata[SEARCHFILTERLEN];
1427c478bd9Sstevel@tonic-gate 	int		ret;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
1457c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	ret = snprintf(searchfilter, sizeof (searchfilter),
1487c478bd9Sstevel@tonic-gate 	    _F_GETBOOTPARAMBYNAME, hostname);
1497c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (searchfilter) || ret < 0)
1507c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	ret = snprintf(userdata, sizeof (userdata),
1537c478bd9Sstevel@tonic-gate 	    _F_GETBOOTPARAMBYNAME_SSD, hostname);
1547c478bd9Sstevel@tonic-gate 	if (ret >= sizeof (userdata) || ret < 0)
1557c478bd9Sstevel@tonic-gate 		return ((nss_status_t)NSS_NOTFOUND);
1567c478bd9Sstevel@tonic-gate 	return ((nss_status_t)_nss_ldap_lookup(be, argp,
1577c478bd9Sstevel@tonic-gate 		_BOOTPARAMS, searchfilter, NULL,
1587c478bd9Sstevel@tonic-gate 		_merge_SSD_filter, userdata));
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate static ldap_backend_op_t bootparams_ops[] = {
1637c478bd9Sstevel@tonic-gate 	_nss_ldap_destr,
1647c478bd9Sstevel@tonic-gate 	getbyname
1657c478bd9Sstevel@tonic-gate };
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * _nss_ldap_bootparams_constr is where life begins. This function calls
1707c478bd9Sstevel@tonic-gate  * the generic ldap constructor function to define and build the abstract
1717c478bd9Sstevel@tonic-gate  * data types required to support ldap operations.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*ARGSUSED0*/
1757c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_ldap_bootparams_constr(const char * dummy1,const char * dummy2,const char * dummy3)1767c478bd9Sstevel@tonic-gate _nss_ldap_bootparams_constr(const char *dummy1, const char *dummy2,
1777c478bd9Sstevel@tonic-gate 			const char *dummy3)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)_nss_ldap_constr(bootparams_ops,
1817c478bd9Sstevel@tonic-gate 		sizeof (bootparams_ops)/sizeof (bootparams_ops[0]),
182*cb5caa98Sdjl 		_BOOTPARAMS, bootparams_attrs, _nss_ldap_bootparams2str));
1837c478bd9Sstevel@tonic-gate }
184