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 (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <netdb.h>
2N/A#include <sys/types.h>
2N/A#include <sys/socket.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <nss.h>
2N/A#include "ns_internal.h"
2N/A#include "ldap_common.h"
2N/A
2N/A/* host attributes filters */
2N/A
2N/A/* probably some change in the ipHostNumber field */
2N/A
2N/A#define _H_DN "dn"
2N/A#define _H_NAME "cn"
2N/A#define _H_ADDR "iphostnumber"
2N/A#define _F_GETHOSTS6BYNAME "(&(objectClass=ipHost)(cn=%s))"
2N/A#define _F_GETHOSTS6BYNAME_SSD "(&(%%s)(cn=%s))"
2N/A#define _F_GETHOSTS6DOTTEDBYNAME \
2N/A "(&(objectClass=ipHost)(|(cn=%s)(cn=%s)))"
2N/A#define _F_GETHOSTS6DOTTEDBYNAME_SSD \
2N/A "(&(%%s)(|(cn=%s)(cn=%s)))"
2N/A#define _F_GETHOSTS6BYADDR "(&(objectClass=ipHost)(ipHostNumber=%s))"
2N/A#define _F_GETHOSTS6BYADDR_SSD "(&(%%s)(ipHostNumber=%s))"
2N/A
2N/Astatic const char *ipnodes_attrs[] = {
2N/A _H_NAME,
2N/A _H_ADDR,
2N/A (char *)NULL
2N/A};
2N/A
2N/Aextern int
2N/A_nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp);
2N/A
2N/A/*
2N/A * _nss_ldap_hosts2str is the data marshaling method for the ipnodes getXbyY
2N/A * system call gethostbyname() and gethostbyaddr.
2N/A * This method is called after a successful search has been performed.
2N/A * This method will parse the search results into the file format.
2N/A * e.g.
2N/A *
2N/A * fe80::a00:20ff:fec4:f2b6 ipnodes_1
2N/A *
2N/A */
2N/Astatic int
2N/A_nss_ldap_hosts2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) {
2N/A return (_nss_ldap_hosts2str_int(AF_INET6, be, argp));
2N/A}
2N/A
2N/A/*
2N/A * getbyname gets a struct hostent by hostname. This function constructs
2N/A * an ldap search filter using the name invocation parameter and the
2N/A * gethostbyname search filter defined. Once the filter is constructed,
2N/A * we search for a matching entry and marshal the data results into
2N/A * struct hostent for the frontend process. Host name searches will be
2N/A * on fully qualified host names (foo.bar.sun.com)
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyname(ldap_backend_ptr be, void *a)
2N/A{
2N/A char hostname[3 * MAXHOSTNAMELEN];
2N/A char realdomain[BUFSIZ];
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A nss_status_t lstat;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A int rc;
2N/A
2N/A if (_ldap_filter_name(hostname, argp->key.ipnode.name,
2N/A sizeof (hostname)) != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETHOSTS6BYNAME, hostname);
2N/A if (rc >= sizeof (searchfilter) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A rc = snprintf(userdata, sizeof (userdata),
2N/A _F_GETHOSTS6BYNAME_SSD, hostname);
2N/A if (rc >= sizeof (userdata) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A /* get the domain we are in */
2N/A rc = sysinfo(SI_SRPC_DOMAIN, realdomain, BUFSIZ);
2N/A if (rc <= 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A /* Is this a request for a host.domain */
2N/A if (DOTTEDSUBDOMAIN(hostname)) {
2N/A char host[MAXHOSTNAMELEN];
2N/A char domain[MAXHOSTNAMELEN];
2N/A char hname[3 * MAXHOSTNAMELEN];
2N/A
2N/A /* separate host and domain. this function */
2N/A /* will munge hname, so use argp->keyname */
2N/A /* from here on for original string */
2N/A
2N/A (void) strcpy(hname, hostname);
2N/A if (chophostdomain(hname, host, domain) == -1) {
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A }
2N/A
2N/A /* if domain is a proper subset of realdomain */
2N/A /* ie. domain = "foo" and realdomain */
2N/A /* = "foor.bar.sun.com", we try to lookup both" */
2N/A /* host.domain and host */
2N/A
2N/A if (propersubdomain(realdomain, domain) == 1) {
2N/A /* yes, it is a proper domain */
2N/A rc = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETHOSTS6DOTTEDBYNAME, hostname, host);
2N/A if (rc >= sizeof (searchfilter) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = snprintf(userdata, sizeof (userdata),
2N/A _F_GETHOSTS6DOTTEDBYNAME_SSD, hostname, host);
2N/A if (rc >= sizeof (userdata) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A } else {
2N/A /* it is not a proper domain, so only try to look up */
2N/A /* host.domain */
2N/A rc = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETHOSTS6BYNAME, hostname);
2N/A if (rc >= sizeof (searchfilter) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = snprintf(userdata, sizeof (userdata),
2N/A _F_GETHOSTS6BYNAME_SSD, hostname);
2N/A if (rc >= sizeof (userdata) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A }
2N/A } else {
2N/A rc = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETHOSTS6BYNAME, hostname);
2N/A if (rc >= sizeof (searchfilter) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = snprintf(userdata, sizeof (userdata),
2N/A _F_GETHOSTS6BYNAME_SSD, hostname);
2N/A if (rc >= sizeof (userdata) || rc < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A }
2N/A lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS,
2N/A searchfilter, NULL, _merge_SSD_filter, userdata);
2N/A if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
2N/A return ((nss_status_t)NSS_SUCCESS);
2N/A
2N/A argp->h_errno = __nss2herrno(lstat);
2N/A return ((nss_status_t)lstat);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbyaddr gets a struct hostent by host address. This function
2N/A * constructs an ldap search filter using the host address invocation
2N/A * parameter and the gethostbyaddr search filter defined. Once the
2N/A * filter is constructed, we search for a matching entry and marshal
2N/A * the data results into struct hostent for the frontend process.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyaddr(ldap_backend_ptr be, void *a)
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A struct in6_addr addr;
2N/A char addrbuf[INET6_ADDRSTRLEN + 1];
2N/A nss_status_t lstat;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A argp->h_errno = 0;
2N/A if ((argp->key.hostaddr.type != AF_INET6) ||
2N/A (argp->key.hostaddr.len != sizeof (addr)))
2N/A return (NSS_NOTFOUND);
2N/A
2N/A (void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
2N/A if (IN6_IS_ADDR_V4MAPPED(&addr)) {
2N/A if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
2N/A (void *)addrbuf, INET_ADDRSTRLEN) == NULL) {
2N/A return (NSS_NOTFOUND);
2N/A }
2N/A } else {
2N/A if (inet_ntop(AF_INET6, (void *)&addr, (void *)addrbuf,
2N/A INET6_ADDRSTRLEN) == NULL)
2N/A return (NSS_NOTFOUND);
2N/A }
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETHOSTS6BYADDR, addrbuf);
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_GETHOSTS6BYADDR_SSD, addrbuf);
2N/A if (ret >= sizeof (userdata) || ret < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A lstat = (nss_status_t)_nss_ldap_lookup(be, argp, _HOSTS6,
2N/A searchfilter, NULL, _merge_SSD_filter, userdata);
2N/A if (lstat == (nss_status_t)NS_LDAP_SUCCESS)
2N/A return ((nss_status_t)NSS_SUCCESS);
2N/A
2N/A argp->h_errno = __nss2herrno(lstat);
2N/A return ((nss_status_t)lstat);
2N/A}
2N/A
2N/Astatic ldap_backend_op_t ipnodes_ops[] = {
2N/A _nss_ldap_destr,
2N/A 0,
2N/A 0,
2N/A 0,
2N/A getbyname,
2N/A getbyaddr
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_hosts_constr is where life begins. This function calls the generic
2N/A * ldap constructor function to define and build the abstract data types
2N/A * required to support ldap operations.
2N/A */
2N/A
2N/A/*ARGSUSED0*/
2N/Anss_backend_t *
2N/A_nss_ldap_ipnodes_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(ipnodes_ops,
2N/A sizeof (ipnodes_ops)/sizeof (ipnodes_ops[0]), _HOSTS6,
2N/A ipnodes_attrs, _nss_ldap_hosts2str));
2N/A}