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 <ctype.h>
2N/A#include <netdb.h>
2N/A#include "ns_internal.h"
2N/A#include "ldap_common.h"
2N/A
2N/A/* protocols attributes filters */
2N/A#define _P_NAME "cn"
2N/A#define _P_PROTO "ipprotocolnumber"
2N/A#define _F_GETPROTOBYNAME "(&(objectClass=ipProtocol)(cn=%s))"
2N/A#define _F_GETPROTOBYNAME_SSD "(&(%%s)(cn=%s))"
2N/A#define _F_GETPROTOBYNUMBER \
2N/A "(&(objectClass=ipProtocol)(ipProtocolNumber=%d))"
2N/A#define _F_GETPROTOBYNUMBER_SSD \
2N/A "(&(%%s)(ipProtocolNumber=%d))"
2N/A
2N/Astatic const char *protocols_attrs[] = {
2N/A _P_NAME,
2N/A _P_PROTO,
2N/A (char *)NULL
2N/A};
2N/A
2N/Atypedef struct protocol_alias {
2N/A char *protocol;
2N/A char *alias;
2N/A} protocol_alias_t;
2N/A
2N/Astatic const protocol_alias_t ip_aliases[10] = {
2N/A { "ip", "IP" },
2N/A { "ipip", "IP-IP" },
2N/A { "ipcomp", "IPComp" },
2N/A { "ipv6", "IPv6" },
2N/A { "ipv6-route", "IPv6-Route" },
2N/A { "ipv6-frag", "IPv6-Frag" },
2N/A { "ipv6-icmp", "IPv6-ICMP" },
2N/A { "ipv6-nonxt", "IPv6-NoNxt" },
2N/A { "ipv6-opts", "IPv6-Opts" },
2N/A { NULL, NULL }
2N/A};
2N/A
2N/A/*
2N/A * When the data is imported by ldapaddent, it does not save the aliase in the
2N/A * "cn" that is same as the canonical name but only different in case.
2N/A * e.g.
2N/A * icmp 1 ICMP
2N/A *
2N/A * is saved as
2N/A *
2N/A * dn: cn=icmp, ...
2N/A * ...
2N/A * cn: icmp
2N/A * ...
2N/A *
2N/A * So it needs to replicate the canonical name as an alias of upper case.
2N/A * But some protocol does have different aliases.
2N/A *
2N/A * e.g.
2N/A * dn: cn=ospf, ...
2N/A * ...
2N/A * cn: ospf
2N/A * cn: OSPFIGP
2N/A * ...
2N/A *
2N/A * For many ip* protocols, the aliases are mixed cased. Maybe it's case
2N/A * insensitive. But this fucntion tries to restore the aliases to the original
2N/A * form as much as possible. If the alias can't be found in the aliases table,
2N/A * it assumes the alias is all upper case.
2N/A *
2N/A */
2N/Astatic char *
2N/Aget_alias(char *protocol) {
2N/A int i;
2N/A char *cp;
2N/A
2N/A if (strncmp(protocol, "ip", 2) == 0) {
2N/A for (i = 0; ip_aliases[i].protocol != NULL; i++) {
2N/A if (strcmp(protocol, ip_aliases[i].protocol) == 0)
2N/A return (ip_aliases[i].alias);
2N/A }
2N/A /*
2N/A * No aliase in the table. Return an all upper case aliase
2N/A */
2N/A for (cp = protocol; *cp; cp++)
2N/A *cp = toupper(*cp);
2N/A
2N/A return (protocol);
2N/A } else {
2N/A /* Return an all upper case aliase */
2N/A for (cp = protocol; *cp; cp++)
2N/A *cp = toupper(*cp);
2N/A
2N/A return (protocol);
2N/A }
2N/A
2N/A}
2N/A/*
2N/A * _nss_ldap_protocols2str is the data marshaling method for the protocols
2N/A * getXbyY * (e.g., getbyname(), getbynumber(), getent()) 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 a file format.
2N/A * e.g.
2N/A * idrp 45 IDRP
2N/A * or
2N/A * ospf 89 OSPFIGP
2N/A */
2N/A
2N/Astatic int
2N/A_nss_ldap_protocols2str(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 *cname = NULL;
2N/A char *buffer = NULL, **number, *alias;
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
2N/A buflen = argp->buf.buflen;
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_pls2str;
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, _P_NAME);
2N/A if (names == NULL || names->attrvalue == NULL) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_pls2str;
2N/A }
2N/A /* Get the canonical name */
2N/A cname = __s_api_get_canonical_name(result->entry, names, 1);
2N/A if (cname == NULL || (len = strlen(cname)) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_pls2str;
2N/A }
2N/A number = __ns_ldap_getAttr(result->entry, _P_PROTO);
2N/A if (number == NULL || number[0] == NULL ||
2N/A (len = strlen(number[0])) < 1) {
2N/A nss_result = NSS_STR_PARSE_PARSE;
2N/A goto result_pls2str;
2N/A }
2N/A len = snprintf(buffer, buflen, "%s %s", cname, number[0]);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_pls2str);
2N/A /* Append aliases */
2N/A if (names->value_count == 1) {
2N/A /* create an aliase from protocol name */
2N/A alias = get_alias(cname);
2N/A len = snprintf(buffer, buflen, " %s", alias);
2N/A TEST_AND_ADJUST(len, buffer, buflen, result_pls2str);
2N/A
2N/A } else {
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_pls2str;
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,
2N/A result_pls2str);
2N/A }
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_pls2str:
2N/A
2N/A (void) __ns_ldap_freeResult(&be->result);
2N/A return ((int)nss_result);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbyname gets struct protoent values by protocol name. This
2N/A * function constructs an ldap search filter using the protocol
2N/A * name invocation parameter and the getprotobyname search filter
2N/A * defined. Once the filter is constructed, we search for a matching
2N/A * entry and marshal the data results into *proto = (struct *
2N/A * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent
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 name[SEARCHFILTERLEN];
2N/A int ret;
2N/A
2N/A if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0)
2N/A return ((nss_status_t)NSS_NOTFOUND);
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETPROTOBYNAME, name);
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_GETPROTOBYNAME_SSD, name);
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 _PROTOCOLS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/A
2N/A/*
2N/A * getbynumber gets struct protoent values by protocol number. This
2N/A * function constructs an ldap search filter using the protocol
2N/A * name invocation parameter and the getprotobynumber search filter
2N/A * defined. Once the filter is constructed, we search for a matching
2N/A * entry and marshal the data results into *proto = (struct *
2N/A * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent
2N/A * performs the data marshaling.
2N/A */
2N/A
2N/Astatic nss_status_t
2N/Agetbynumber(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 int ret;
2N/A
2N/A ret = snprintf(searchfilter, sizeof (searchfilter),
2N/A _F_GETPROTOBYNUMBER, argp->key.number);
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_GETPROTOBYNUMBER_SSD, argp->key.number);
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 _PROTOCOLS, searchfilter, NULL,
2N/A _merge_SSD_filter, userdata));
2N/A}
2N/A
2N/Astatic ldap_backend_op_t proto_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 getbynumber
2N/A};
2N/A
2N/A
2N/A/*
2N/A * _nss_ldap_protocols_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_protocols_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(proto_ops,
2N/A sizeof (proto_ops)/sizeof (proto_ops[0]), _PROTOCOLS,
2N/A protocols_attrs, _nss_ldap_protocols2str));
2N/A}