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 <netdb.h>
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <sys/socket.h>
2N/A#include "ns_internal.h"
2N/A#include "ldap_common.h"
2N/A
2N/A/* networks attributes filters */
2N/A#define _N_NAME "cn"
2N/A#define _N_NETWORK "ipnetworknumber"
2N/A#define _F_GETNETBYNAME "(&(objectClass=ipNetwork)(cn=%s))"
2N/A#define _F_GETNETBYNAME_SSD "(&(%%s)(cn=%s))"
2N/A#define _F_GETNETBYADDR "(&(objectClass=ipNetwork)(|(ipNetworkNumber=%s)" \
2N/A "(ipNetworkNumber=%s)))"
2N/A#define _F_GETNETBYADDR_SSD "(&(%%s)(|(ipNetworkNumber=%s)" \
2N/A "(ipNetworkNumber=%s)))"
2N/A
2N/Astatic const char *networks_attrs[] = {
2N/A _N_NAME,
2N/A _N_NETWORK,
2N/A (char *)NULL
2N/A};
2N/A
2N/A/*
2N/A * _nss_ldap_networks2str is the data marshaling method for the networks
2N/A * getXbyY * (e.g., getbyname(), getbyaddr(), getnetent() 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 * SunRay-ce2 10.34.96.0 SunRay
2N/A *
2N/A */
2N/Astatic int
2N/A_nss_ldap_networks2str(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 = 0, len;
2N/A char **network, *cname = NULL;
2N/A char *buffer = NULL;
2N/A ns_ldap_result_t *result = be->result;
2N/A ns_ldap_attr_t *names;
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A buflen = 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_net2str;
2N/A }
2N/A buffer = be->buffer;
2N/A } else
2N/A buffer = argp->buf.buffer;
2N/A
2N/A nss_result = NSS_STR_PARSE_SUCCESS;
2N/A (void) memset(argp->buf.buffer, 0, buflen);
2N/A
2N/A names = __ns_ldap_getAttrStruct(result->entry, _N_NAME);
2N/A if (names == NULL || names->attrvalue == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_net2str;
2N/A }
2N/A /* Get the canonical name */
2N/A cname = __s_api_get_canonical_name(result->entry, names, 1);
2N/A /*
2N/A * The definition of the object class "ipNetwork" has a
2N/A * discrepency between RFC 2307 and 2307bis.
2N/A * In 2307, "cn" is a MUST attribute. In 2307bis, "cn" is a
2N/A * MAY attribute.
2N/A * If "cn" is a MAY attribute, it does not appear in RDN and can't
2N/A * be derived from RDN as a canonical "cn" name. In that case, use 1st
2N/A * "cn" value as the official name.
2N/A */
2N/A if (cname == NULL)
2N/A /* 2307bis case */
2N/A cname = names->attrvalue[0];
2N/A if (cname == NULL || (len = strlen(cname)) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_net2str;
2N/A }
2N/A network = __ns_ldap_getAttr(result->entry, _N_NETWORK);
2N/A if (network == NULL || network[0] == NULL ||
2N/A (len = strlen(network[0])) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_net2str;
2N/A }
2N/A len = snprintf(buffer, buflen, "%s %s", cname, network[0]);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
2N/A /* Append aliases */
2N/A for (i = 0; i < names->value_count; i++) {
2N/A if (names->attrvalue[i] == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_net2str;
2N/A }
2N/A /* Skip the canonical name */
2N/A if (strcasecmp(names->attrvalue[i], cname) != 0) {
2N/A len = snprintf(buffer, buflen, " %s",
2N/A names->attrvalue[i]);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_net2str);
2N/A }
2N/A }
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_net2str:
2N/A
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return (nss_result);
2N/A}
2N/A
2N/A/*
2N/A * Takes an unsigned integer in host order, and returns a printable
2N/A * string for it as a network number. To allow for the possibility of
2N/A * naming subnets, only trailing dot-zeros are truncated.
2N/A * buf2 is untruncated version.
2N/A */
2N/A
2N/Astatic int nettoa(int anet, char *buf, char *buf2, int buflen)
2N/A{
2N/A int addr;
2N/A char *p;
2N/A struct in_addr in;
2N/A
2N/A if (buf == NULL || buf2 == NULL)
2N/A return ((int)1);
2N/A
2N/A in = inet_makeaddr(anet, INADDR_ANY);
2N/A addr = in.s_addr;
2N/A if (inet_ntop(AF_INET, (const void *)&in, buf2, INET_ADDRSTRLEN)
2N/A == NULL)
2N/A return ((int)1);
2N/A if (strlcpy(buf, buf2, buflen) >= buflen)
2N/A return ((int)1);
2N/A if ((IN_CLASSA_HOST & htonl(addr)) == 0) {
2N/A p = strchr(buf, '.');
2N/A if (p == NULL)
2N/A return ((int)1);
2N/A *p = 0;
2N/A } else if ((IN_CLASSB_HOST & htonl(addr)) == 0) {
2N/A p = strchr(buf, '.');
2N/A if (p == NULL)
2N/A return ((int)1);
2N/A p = strchr(p + 1, '.');
2N/A if (p == NULL)
2N/A return ((int)1);
2N/A *p = 0;
2N/A } else if ((IN_CLASSC_HOST & htonl(addr)) == 0) {
2N/A p = strrchr(buf, '.');
2N/A if (p == NULL)
2N/A return ((int)1);
2N/A *p = 0;
2N/A }
2N/A
2N/A return ((int)0);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbyname gets a network entry by name. This function constructs an
2N/A * ldap search filter using the network name invocation parameter and the
2N/A * getnetbyname search filter defined. Once the filter is constructed, we
2N/A * search for a matching entry and marshal the data results into struct
2N/A * netent for the frontend process. The function _nss_ldap_networks2ent
2N/A * performs the data marshaling.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbyname(ldap_backend_ptr be, void *a)
2N/A{
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A char netname[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A if (_ldap_filter_name(netname, argp->key.name, sizeof (netname)) != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETNETBYNAME, netname);
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_GETNETBYNAME_SSD, netname);
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 _NETWORKS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbyaddr gets a network entry by ip address. This function constructs an
2N/A * ldap search filter using the name invocation parameter and the getnetbyaddr
2N/A * search filter defined. Once the filter is constructed, we search for a
2N/A * matching entry and marshal the data results into struct netent for the
2N/A * frontend process. The function _nss_ldap_networks2ent performs the data
2N/A * marshaling.
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 char addrstr[INET_ADDRSTRLEN], addrstr2[INET_ADDRSTRLEN];
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A if (nettoa((int)argp->key.netaddr.net, addrstr, addrstr2,
2N/A INET_ADDRSTRLEN) != 0)
2N/A return ((nss_status_t)NSS_UNAVAIL);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETNETBYADDR, addrstr, addrstr2);
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_GETNETBYADDR_SSD, addrstr, addrstr2);
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 _NETWORKS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/Astatic ldap_backend_op_t net_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_networks_constr is where life begins. This function calls the
2N/A * generic ldap constructor function to define and build the abstract data
2N/A * types required to support ldap operations.
2N/A */
2N/A
2N/A/*ARGSUSED0*/
2N/Anss_backend_t *
2N/A_nss_ldap_networks_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(net_ops,
2N/A sizeof (net_ops)/sizeof (net_ops[0]), _NETWORKS,
2N/A networks_attrs, _nss_ldap_networks2str));
2N/A}