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 "ldap_common.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/tsol/tndb.h>
2N/A
2N/A/* tnrhdb attributes filters */
2N/A#define _TNRHDB_ADDR "ipTnetNumber"
2N/A#define _TNRHDB_TNAME "ipTnetTemplateName"
2N/A#define _F_GETTNDBBYADDR "(&(objectClass=ipTnetHost)(ipTnetNumber=%s))"
2N/A#define _F_GETTNDBBYADDR_SSD "(&(%%s)(ipTnetNumber=%s))"
2N/A
2N/Astatic const char *tnrhdb_attrs[] = {
2N/A _TNRHDB_ADDR,
2N/A _TNRHDB_TNAME,
2N/A NULL
2N/A};
2N/A
2N/Astatic void
2N/Aescape_colon(char *in, char *out) {
2N/A int i, j;
2N/A for (i = 0, j = 0; in[i] != '\0'; i++) {
2N/A if (in[i] == ':') {
2N/A out[j++] = '\\';
2N/A out[j++] = in[i];
2N/A } else
2N/A out[j++] = in[i];
2N/A }
2N/A out[j] = '\0';
2N/A}
2N/A
2N/A/*
2N/A * _nss_ldap_tnrhdb2str is the data marshaling method for the tnrhdb
2N/A * (tsol_getrhbyaddr()/tsol_getrhent()) 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 * e.g.
2N/A *
2N/A * 192.168.120.6:public
2N/A * fec0\:\:a00\:20ff\:fea0\:21f7:cipso
2N/A *
2N/A */
2N/Astatic int
2N/A_nss_ldap_tnrhdb2str(ldap_backend_ptr be, nss_XbyY_args_t *argp)
2N/A{
2N/A int nss_result = NSS_STR_PARSE_SUCCESS;
2N/A int len = 0;
2N/A char *buffer = NULL;
2N/A char **addr, **template, *addr_out;
2N/A ns_ldap_result_t *result = be->result;
2N/A char addr6[INET6_ADDRSTRLEN + 5]; /* 5 '\' for ':' at most */
2N/A
2N/A if (result == NULL)
2N/A return (NSS_STR_PARSE_PARSE);
2N/A
2N/A addr = __ns_ldap_getAttr(result->entry, _TNRHDB_ADDR);
2N/A if (addr == NULL || addr[0] == NULL || (strlen(addr[0]) < 1)) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_tnrhdb2str;
2N/A }
2N/A
2N/A /*
2N/A * Escape ':' in IPV6.
2N/A * The value is stored in LDAP directory without escape charaters.
2N/A */
2N/A if (strchr(addr[0], ':') != NULL) {
2N/A escape_colon(addr[0], addr6);
2N/A addr_out = addr6;
2N/A } else
2N/A addr_out = addr[0];
2N/A
2N/A template = __ns_ldap_getAttr(result->entry, _TNRHDB_TNAME);
2N/A if (template == NULL || template[0] == NULL ||
2N/A (strlen(template[0]) < 1)) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_tnrhdb2str;
2N/A }
2N/A /* "addr:template" */
2N/A len = strlen(addr_out) + strlen(template[0]) + 2;
2N/A
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_tnrhdb2str;
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 (void) snprintf(buffer, len, "%s:%s", addr_out, template[0]);
2N/A
2N/Aresult_tnrhdb2str:
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return (nss_result);
2N/A}
2N/A
2N/A
2N/Astatic nss_status_t
2N/Agetbyaddr(ldap_backend_ptr be, void *a)
2N/A{
2N/A char searchfilter[SEARCHFILTERLEN];
2N/A char userdata[SEARCHFILTERLEN];
2N/A nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
2N/A
2N/A if (argp->key.hostaddr.addr == NULL ||
2N/A (argp->key.hostaddr.type != AF_INET &&
2N/A argp->key.hostaddr.type != AF_INET6))
2N/A return (NSS_NOTFOUND);
2N/A if (strchr(argp->key.hostaddr.addr, ':') != NULL) {
2N/A /* IPV6 */
2N/A if (argp->key.hostaddr.type == AF_INET)
2N/A return (NSS_NOTFOUND);
2N/A } else {
2N/A /* IPV4 */
2N/A if (argp->key.hostaddr.type == AF_INET6)
2N/A return (NSS_NOTFOUND);
2N/A }
2N/A
2N/A /*
2N/A * The IPV6 addresses are saved in the directory without '\'s.
2N/A * So don't need to escape colons in IPV6 addresses.
2N/A */
2N/A if (snprintf(searchfilter, sizeof (searchfilter), _F_GETTNDBBYADDR,
2N/A argp->key.hostaddr.addr) < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A if (snprintf(userdata, sizeof (userdata), _F_GETTNDBBYADDR_SSD,
2N/A argp->key.hostaddr.addr) < 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A return (_nss_ldap_lookup(be, argp, _TNRHDB, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/A
2N/Astatic ldap_backend_op_t tnrhdb_ops[] = {
2N/A _nss_ldap_destr,
2N/A _nss_ldap_endent,
2N/A _nss_ldap_setent,
2N/A _nss_ldap_getent,
2N/A getbyaddr
2N/A};
2N/A
2N/A
2N/A/* ARGSUSED */
2N/Anss_backend_t *
2N/A_nss_ldap_tnrhdb_constr(const char *dummy1,
2N/A const char *dummy2,
2N/A const char *dummy3,
2N/A const char *dummy4,
2N/A const char *dummy5)
2N/A{
2N/A return ((nss_backend_t *)_nss_ldap_constr(tnrhdb_ops,
2N/A sizeof (tnrhdb_ops)/sizeof (tnrhdb_ops[0]), _TNRHDB,
2N/A tnrhdb_attrs, _nss_ldap_tnrhdb2str));
2N/A}