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 <arpa/inet.h>
2N/A#include <netinet/in.h>
2N/A#include <sys/socket.h>
2N/A#include <syslog.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#define _H_DN "dn"
2N/A#define _H_NAME "cn"
2N/A#define _H_ADDR "iphostnumber"
2N/A#define _F_GETHOSTBYNAME "(&(objectClass=ipHost)(cn=%s))"
2N/A#define _F_GETHOSTBYNAME_SSD "(&(%%s)(cn=%s))"
2N/A#define _F_GETHOSTDOTTEDBYNAME "(&(objectClass=ipHost)(|(cn=%s)(cn=%s)))"
2N/A#define _F_GETHOSTDOTTEDBYNAME_SSD "(&(%%s)(|(cn=%s)(cn=%s)))"
2N/A#define _F_GETHOSTBYADDR "(&(objectClass=ipHost)(ipHostNumber=%s))"
2N/A#define _F_GETHOSTBYADDR_SSD "(&(%%s)(ipHostNumber=%s))"
2N/A
2N/Astatic const char *hosts_attrs[] = {
2N/A _H_NAME,
2N/A _H_ADDR,
2N/A (char *)NULL
2N/A};
2N/A
2N/A/*
2N/A * _nss_ldap_hosts2str is the data marshaling method for the hosts 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 * 9.9.9.9 jurassic jurassic1 jurassic2
2N/A * 10.10.10.10 puppy
2N/A *
2N/A */
2N/Aint
2N/A_nss_ldap_hosts2str_int(int af, ldap_backend_ptr be, nss_XbyY_args_t *argp)
2N/A{
2N/A uint_t i;
2N/A int nss_result;
2N/A int buflen, buflen1, buflen2, len;
2N/A int firstimedn = 1, first_entry;
2N/A int validaddress = 0, copy_cname;
2N/A char *cname = NULL, *h_name = NULL;
2N/A char *buffer = NULL;
2N/A char *name;
2N/A ns_ldap_result_t *result = be->result;
2N/A ns_ldap_attr_t *names;
2N/A ns_ldap_entry_t *entry;
2N/A char **ips = NULL, **dns = NULL;
2N/A char *first_host = NULL, *other_hosts = NULL;
2N/A char *buf1, *buf2;
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A buflen = buflen1 = buflen2 = argp->buf.buflen;
2N/A
2N/A if (argp->buf.result != NULL) {
2N/A if ((be->buffer = calloc(1, buflen)) == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A buffer = be->buffer;
2N/A } else
2N/A buffer = argp->buf.buffer;
2N/A if ((first_host = calloc(1, buflen1)) == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A if ((other_hosts = calloc(1, buflen2)) == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A
2N/A nss_result = NSS_STR_PARSE_SUCCESS;
2N/A (void) memset(argp->buf.buffer, 0, buflen);
2N/A /*
2N/A * Multiple lines return will be sepereated by newlines
2N/A * Single line return or last line does not have newline
2N/A * e.g.
2N/A *
2N/A * 8.8.8.8 hostname
2N/A *
2N/A * or the search for hostname h1 returns 3 entries
2N/A *
2N/A * 9.9.9.9 h1
2N/A * 10.10.10.10 h1 xx
2N/A * 20.20.20.20 h1 yyy
2N/A *
2N/A * str2hostent expects all name/aliases in the first entry
2N/A * so the string is organized as
2N/A *
2N/A * "9.9.9.9 h1 xx yy\n10.10.10.10 \n20.20.20.20 "
2N/A *
2N/A * Use first_host to hold "9.9.9.9 h1 xx yy" and other_hosts to hold
2N/A * "\n10.10.10.10 \n20.20.20.20 "
2N/A *
2N/A */
2N/A buf1 = first_host;
2N/A buf2 = other_hosts;
2N/A first_entry = 1;
2N/A for (entry = result->entry; entry != NULL; entry = entry->next) {
2N/A if (firstimedn) {
2N/A dns = __ns_ldap_getAttr(entry, _H_DN);
2N/A if (dns == NULL || dns[0] == NULL || strlen(dns[0])
2N/A < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A /* get domain name associated with this dn */
2N/A be->toglue = _get_domain_name(dns[0]);
2N/A firstimedn = 0;
2N/A }
2N/A
2N/A /* Get IP */
2N/A ips = __ns_ldap_getAttr(entry, _H_ADDR);
2N/A if (ips == NULL || ips[0] == NULL || strlen(ips[0]) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A /* Skip IPV6 address in AF_INET mode */
2N/A if (af == AF_INET &&
2N/A (inet_addr(_strip_quotes(ips[0])) == (in_addr_t)-1))
2N/A continue;
2N/A
2N/A /* A valid address for either af mode */
2N/A validaddress++;
2N/A
2N/A if (first_entry) {
2N/A len = snprintf(buf1, buflen1, "%s", ips[0]);
2N/A TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
2N/A } else {
2N/A len = snprintf(buf2, buflen2, "\n%s ", ips[0]);
2N/A TEST_AND_ADJUST(len, buf2, buflen2, result_host2str);
2N/A }
2N/A
2N/A /* Get host names */
2N/A names = __ns_ldap_getAttrStruct(entry, _H_NAME);
2N/A if (names == NULL || names->attrvalue == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A
2N/A /* Get canonical name of each entry */
2N/A cname = __s_api_get_canonical_name(entry, names, 1);
2N/A if (cname == NULL || strlen(cname) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A
2N/A /* Filter cname that's identical to h_name */
2N/A if (first_entry) {
2N/A h_name = cname;
2N/A first_entry = 0;
2N/A copy_cname = 1;
2N/A } else if (strcasecmp(cname, h_name) != 0) {
2N/A copy_cname = 1;
2N/A } else
2N/A copy_cname = 0;
2N/A
2N/A if (copy_cname) {
2N/A /* Use the canonical name as the host name */
2N/A if (be->toglue == NULL || DOTTEDSUBDOMAIN(cname))
2N/A len = snprintf(buf1, buflen1, " %s", cname);
2N/A else
2N/A /* append domain name */
2N/A len = snprintf(buf1, buflen1, " %s.%s", cname,
2N/A be->toglue);
2N/A
2N/A TEST_AND_ADJUST(len, buf1, buflen1, result_host2str);
2N/A }
2N/A
2N/A /* Append aliases */
2N/A for (i = 0; i < names->value_count; i++) {
2N/A name = names->attrvalue[i];
2N/A if (name == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_host2str;
2N/A }
2N/A /* Skip the canonical name and h_name */
2N/A if (strcasecmp(name, cname) != 0 &&
2N/A strcasecmp(name, h_name) != 0) {
2N/A if (be->toglue == NULL || DOTTEDSUBDOMAIN(name))
2N/A len = snprintf(buf1, buflen1, " %s",
2N/A name);
2N/A else
2N/A /* append domain name */
2N/A len = snprintf(buf1, buflen1, " %s.%s",
2N/A name, be->toglue);
2N/A TEST_AND_ADJUST(len, buf1, buflen1,
2N/A result_host2str);
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (validaddress == 0) {
2N/A /*
2N/A * For AF_INET mode, it found an IPv6 address and skipped it.
2N/A */
2N/A nss_result = NSS_STR_PARSE_NO_ADDR;
2N/A goto result_host2str;
2N/A }
2N/A /* Combine 2 strings */
2N/A len = snprintf(buffer, buflen, "%s%s", first_host, other_hosts);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_host2str);
2N/A
2N/A /* The front end marshaller doesn't need to copy trailing nulls */
2N/A if (argp->buf.result != NULL)
2N/A be->buflen = strlen(be->buffer);
2N/A
2N/Aresult_host2str:
2N/A if (first_host)
2N/A free(first_host);
2N/A if (other_hosts)
2N/A free(other_hosts);
2N/A if (be->toglue) {
2N/A free(be->toglue);
2N/A be->toglue = NULL;
2N/A }
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return (nss_result);
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_INET, 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.name, sizeof (hostname)) != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A rc = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYNAME,
2N/A 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), _F_GETHOSTBYNAME_SSD,
2N/A 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
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 = "eng" and realdomain */
2N/A /* = "eng.wiz.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_GETHOSTDOTTEDBYNAME, 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_GETHOSTDOTTEDBYNAME_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_GETHOSTBYNAME, 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_GETHOSTBYNAME_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_GETHOSTBYNAME, 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_GETHOSTBYNAME_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, searchfilter,
2N/A 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 * extern char *inet_ntoa_r() not an advertised function from libnsl.
2N/A * There is no man page and no prototype.
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 in_addr addr;
2N/A char buf[18];
2N/A nss_status_t lstat;
2N/A extern char *inet_ntoa_r();
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_INET) ||
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 (void) inet_ntoa_r(addr, buf);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter), _F_GETHOSTBYADDR,
2N/A buf);
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), _F_GETHOSTBYADDR_SSD, buf);
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, _HOSTS, searchfilter,
2N/A 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 hosts_ops[] = {
2N/A _nss_ldap_destr,
2N/A _nss_ldap_endent,
2N/A _nss_ldap_setent,
2N/A _nss_ldap_getent,
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_hosts_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(hosts_ops,
2N/A sizeof (hosts_ops)/sizeof (hosts_ops[0]), _HOSTS,
2N/A hosts_attrs, _nss_ldap_hosts2str));
2N/A}