2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <net/if.h>
2N/A#include <netinet/in.h>
2N/A#include <net/if_arp.h>
2N/A#include <netinet/if_ether.h>
2N/A#include "ldap_common.h"
2N/A
2N/A/* ether attributes filters */
2N/A#define _E_HOSTNAME "cn"
2N/A#define _E_MACADDRESS "macaddress"
2N/A#define _F_GETETHERBYHOST "(&(objectClass=ieee802Device)(cn=%s))"
2N/A#define _F_GETETHERBYHOST_SSD "(&(%%s)(cn=%s))"
2N/A#define _F_GETETHERBYETHER "(&(objectClass=ieee802Device)(macAddress=%s))"
2N/A#define _F_GETETHERBYETHER_SSD "(&(%%s)(macAddress=%s))"
2N/A
2N/Astatic const char *ethers_attrs[] = {
2N/A _E_HOSTNAME,
2N/A _E_MACADDRESS,
2N/A (char *)NULL
2N/A};
2N/A
2N/A/*
2N/A * _nss_ldap_ethers2str is the data marshaling method for the ethers
2N/A * ether_hostton/ether_ntohost backend processes.
2N/A * This method is called after a successful ldap search has been performed.
2N/A * This method will parse the ldap search values into the file format.
2N/A * e.g.
2N/A *
2N/A * 8:0:20:8e:eb:8a8 borealis
2N/A *
2N/A * The front end marshaller str2ether uses argp->buf.result for a different
2N/A * purpose so a flag be->db_type is set to work around this oddity.
2N/A *
2N/A */
2N/A/*ARGSUSED0*/
2N/Astatic int
2N/A_nss_ldap_ethers2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
2N/A{
2N/A int nss_result;
2N/A ns_ldap_result_t *result = be->result;
2N/A char **host, **macaddress;
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A nss_result = NSS_STR_PARSE_SUCCESS;
2N/A
2N/A host = __ns_ldap_getAttr(result->entry, _E_HOSTNAME);
2N/A if (host == NULL || host[0] == NULL || (strlen(host[0]) < 1)) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_ea2str;
2N/A }
2N/A macaddress = __ns_ldap_getAttr(result->entry, _E_MACADDRESS);
2N/A if (macaddress == NULL || macaddress[0] == NULL ||
2N/A (strlen(macaddress[0]) < 1)) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_ea2str;
2N/A }
2N/A be->buflen = strlen(host[0]) + strlen(macaddress[0]) + 1; /* ' ' */
2N/A /* Add a trailing null for easy debug */
2N/A be->buffer = calloc(1, be->buflen + 1);
2N/A if (be->buffer == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_ea2str;
2N/A }
2N/A
2N/A (void) snprintf(be->buffer, be->buflen + 1, "%s %s",
2N/A macaddress[0], host[0]);
2N/A be->db_type = NSS_LDAP_DB_ETHERS;
2N/A
2N/Aresult_ea2str:
2N/A
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return (nss_result);
2N/A}
2N/A
2N/A/*
2N/A * getbyhost gets an ethernet address by hostname. This function
2N/A * constructs an ldap search filter using the hostname invocation
2N/A * parameter and the getetherbyhost search filter defined. Once
2N/A * the filter is constructed, we search for a matching entry and
2N/A * marshal the data results into uchar_t *ether for the frontend
2N/A * process. The function _nss_ldap_ethers2ent performs the data
2N/A * marshaling.
2N/A *
2N/A * RFC 2307, An Approach for Using LDAP as a Network Information Service,
2N/A * indicates that dn's be fully qualified. Host name searches will be on
2N/A * fully qualified host names (e.g., foo.bar.sun.com).
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyhost(ldap_backend_ptr be, void *a)
2N/A{
2N/A char hostname[3 * MAXHOSTNAMELEN];
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A int ret;
2N/A nss_status_t rc;
2N/A
2N/A if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETETHERBYHOST, hostname);
2N/A
2N/A if (ret >= sizeof (searchfilter) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(userdata, sizeof (userdata),
2N/A _F_GETETHERBYHOST_SSD, hostname);
2N/A
2N/A if (ret >= sizeof (userdata) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = (nss_status_t)_nss_ldap_lookup(be, argp,
2N/A _ETHERS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbyether gets an ethernet address by ethernet address. This
2N/A * function constructs an ldap search filter using the ASCII
2N/A * ethernet address invocation parameter and the getetherbyether
2N/A * search filter defined. Once the filter is constructed, we
2N/A * search for a matching entry and marshal the data results into
2N/A * uchar_t *ether for the frontend process. The function
2N/A * _nss_ldap_ethers2ent performs the data marshaling.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyether(ldap_backend_ptr be, void *a)
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A char etherstr[18];
2N/A uchar_t *e = argp->key.ether;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A ret = snprintf(etherstr, sizeof (etherstr), "%x:%x:%x:%x:%x:%x",
2N/A *e, *(e + 1), *(e + 2), *(e + 3), *(e + 4), *(e + 5));
2N/A if (ret >= sizeof (etherstr) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETETHERBYETHER, etherstr);
2N/A if (ret >= sizeof (searchfilter) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(userdata, sizeof (userdata),
2N/A _F_GETETHERBYETHER_SSD, etherstr);
2N/A if (ret >= sizeof (userdata) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A return ((nss_status_t)_nss_ldap_lookup(be, argp,
2N/A _ETHERS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/A
2N/Astatic ldap_backend_op_t ethers_ops[] = {
2N/A _nss_ldap_destr,
2N/A getbyhost,
2N/A getbyether
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_ethers_constr is where life begins. This function calls the
2N/A * generic ldap constructor function to define and build the abstract
2N/A * data types required to support ldap operations.
2N/A */
2N/A
2N/A/*ARGSUSED0*/
2N/Anss_backend_t *
2N/A_nss_ldap_ethers_constr(const char *dummy1, const char *dummy2,
2N/A const char *dummy3)
2N/A{
2N/A
2N/A return ((nss_backend_t *)_nss_ldap_constr(ethers_ops,
2N/A sizeof (ethers_ops)/sizeof (ethers_ops[0]), _ETHERS,
2N/A ethers_attrs, _nss_ldap_ethers2str));
2N/A}