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 "ldap_common.h"
2N/A
2N/A/* netmasks attributes filters */
2N/A#define _N_NETWORK "ipnetworknumber"
2N/A#define _N_NETMASK "ipnetmasknumber"
2N/A
2N/A#define _F_GETMASKBYNET "(&(objectClass=ipNetwork)(ipNetworkNumber=%s))"
2N/A#define _F_GETMASKBYNET_SSD "(&(%%s)(ipNetworkNumber=%s))"
2N/A
2N/Astatic const char *netmasks_attrs[] = {
2N/A _N_NETWORK,
2N/A _N_NETMASK,
2N/A (char *)NULL
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_netmasks2str is the data marshaling method for the netmasks
2N/A * getXbyY * (e.g., getnetmaskby[net|addr]()) 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 *
2N/A * getnetmaskbykey set argp->buf.buffer to NULL and argp->buf.buflen to 0
2N/A * and argp->buf.result to non-NULL.
2N/A * The front end marshaller str2add expects "netmask" only
2N/A *
2N/A * e.g.
2N/A *
2N/A * 255.255.255.0
2N/A *
2N/A *
2N/A */
2N/A
2N/Astatic int
2N/A_nss_ldap_netmasks2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
2N/A{
2N/A int nss_result, len;
2N/A ns_ldap_result_t *result = be->result;
2N/A char *buffer, **netmask;
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A nss_result = NSS_STR_PARSE_SUCCESS;
2N/A
2N/A netmask = __ns_ldap_getAttr(result->entry, _N_NETMASK);
2N/A if (netmask == NULL || netmask[0] == NULL ||
2N/A (strlen(netmask[0]) < 1)) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_nmks2str;
2N/A }
2N/A /* Add a trailing null for debugging purpose */
2N/A len = strlen(netmask[0]) + 1;
2N/A if (argp->buf.result != NULL) {
2N/A if ((be->buffer = calloc(1, len)) == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_nmks2str;
2N/A }
2N/A be->buflen = len - 1;
2N/A buffer = be->buffer;
2N/A } else
2N/A buffer = argp->buf.buffer;
2N/A
2N/A
2N/A (void) snprintf(buffer, len, "%s", netmask[0]);
2N/A
2N/Aresult_nmks2str:
2N/A
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return ((int)nss_result);
2N/A}
2N/A
2N/A/*
2N/A * getbynet gets a network mask by address. This function constructs an
2N/A * ldap search filter using the netmask name invocation parameter and the
2N/A * getmaskbynet 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 * in_addr for the frontend process. The function _nss_ldap_netmasks2ent
2N/A * performs the data marshaling.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbynet(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 netnumber[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A if (_ldap_filter_name(netnumber, argp->key.name, sizeof (netnumber))
2N/A != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETMASKBYNET, netnumber);
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_GETMASKBYNET_SSD, netnumber);
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 _NETMASKS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/A
2N/Astatic ldap_backend_op_t netmasks_ops[] = {
2N/A _nss_ldap_destr,
2N/A getbynet
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_netmasks_constr is where life begins. This function calls
2N/A * the 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_netmasks_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(netmasks_ops,
2N/A sizeof (netmasks_ops)/sizeof (netmasks_ops[0]), _NETMASKS,
2N/A netmasks_attrs, _nss_ldap_netmasks2str));
2N/A}