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