17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*cb5caa98Sdjl * Common Development and Distribution License (the "License"). 6*cb5caa98Sdjl * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*cb5caa98Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <ctype.h> 297c478bd9Sstevel@tonic-gate #include <netdb.h> 307c478bd9Sstevel@tonic-gate #include "ns_internal.h" 317c478bd9Sstevel@tonic-gate #include "ldap_common.h" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* protocols attributes filters */ 347c478bd9Sstevel@tonic-gate #define _P_NAME "cn" 357c478bd9Sstevel@tonic-gate #define _P_PROTO "ipprotocolnumber" 367c478bd9Sstevel@tonic-gate #define _F_GETPROTOBYNAME "(&(objectClass=ipProtocol)(cn=%s))" 377c478bd9Sstevel@tonic-gate #define _F_GETPROTOBYNAME_SSD "(&(%%s)(cn=%s))" 387c478bd9Sstevel@tonic-gate #define _F_GETPROTOBYNUMBER \ 397c478bd9Sstevel@tonic-gate "(&(objectClass=ipProtocol)(ipProtocolNumber=%d))" 407c478bd9Sstevel@tonic-gate #define _F_GETPROTOBYNUMBER_SSD \ 417c478bd9Sstevel@tonic-gate "(&(%%s)(ipProtocolNumber=%d))" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static const char *protocols_attrs[] = { 447c478bd9Sstevel@tonic-gate _P_NAME, 457c478bd9Sstevel@tonic-gate _P_PROTO, 467c478bd9Sstevel@tonic-gate (char *)NULL 477c478bd9Sstevel@tonic-gate }; 487c478bd9Sstevel@tonic-gate 49*cb5caa98Sdjl typedef struct protocol_alias { 50*cb5caa98Sdjl char *protocol; 51*cb5caa98Sdjl char *alias; 52*cb5caa98Sdjl } protocol_alias_t; 53*cb5caa98Sdjl 54*cb5caa98Sdjl static const protocol_alias_t ip_aliases[10] = { 55*cb5caa98Sdjl { "ip", "IP" }, 56*cb5caa98Sdjl { "ipip", "IP-IP" }, 57*cb5caa98Sdjl { "ipcomp", "IPComp" }, 58*cb5caa98Sdjl { "ipv6", "IPv6" }, 59*cb5caa98Sdjl { "ipv6-route", "IPv6-Route" }, 60*cb5caa98Sdjl { "ipv6-frag", "IPv6-Frag" }, 61*cb5caa98Sdjl { "ipv6-icmp", "IPv6-ICMP" }, 62*cb5caa98Sdjl { "ipv6-nonxt", "IPv6-NoNxt" }, 63*cb5caa98Sdjl { "ipv6-opts", "IPv6-Opts" }, 64*cb5caa98Sdjl { NULL, NULL } 65*cb5caa98Sdjl }; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* 68*cb5caa98Sdjl * When the data is imported by ldapaddent, it does not save the aliase in the 69*cb5caa98Sdjl * "cn" that is same as the canonical name but only different in case. 707c478bd9Sstevel@tonic-gate * e.g. 717c478bd9Sstevel@tonic-gate * icmp 1 ICMP 727c478bd9Sstevel@tonic-gate * 737c478bd9Sstevel@tonic-gate * is saved as 747c478bd9Sstevel@tonic-gate * 757c478bd9Sstevel@tonic-gate * dn: cn=icmp, ... 767c478bd9Sstevel@tonic-gate * ... 777c478bd9Sstevel@tonic-gate * cn: icmp 787c478bd9Sstevel@tonic-gate * ... 797c478bd9Sstevel@tonic-gate * 80*cb5caa98Sdjl * So it needs to replicate the canonical name as an alias of upper case. 81*cb5caa98Sdjl * But some protocol does have different aliases. 82*cb5caa98Sdjl * 837c478bd9Sstevel@tonic-gate * e.g. 84*cb5caa98Sdjl * dn: cn=ospf, ... 857c478bd9Sstevel@tonic-gate * ... 867c478bd9Sstevel@tonic-gate * cn: ospf 877c478bd9Sstevel@tonic-gate * cn: OSPFIGP 887c478bd9Sstevel@tonic-gate * ... 897c478bd9Sstevel@tonic-gate * 90*cb5caa98Sdjl * For many ip* protocols, the aliases are mixed cased. Maybe it's case 91*cb5caa98Sdjl * insensitive. But this fucntion tries to restore the aliases to the original 92*cb5caa98Sdjl * form as much as possible. If the alias can't be found in the aliases table, 93*cb5caa98Sdjl * it assumes the alias is all upper case. 947c478bd9Sstevel@tonic-gate * 957c478bd9Sstevel@tonic-gate */ 96*cb5caa98Sdjl static char * 97*cb5caa98Sdjl get_alias(char *protocol) { 98*cb5caa98Sdjl int i; 99*cb5caa98Sdjl char *cp; 100*cb5caa98Sdjl 101*cb5caa98Sdjl if (strncmp(protocol, "ip", 2) == 0) { 102*cb5caa98Sdjl for (i = 0; ip_aliases[i].protocol != NULL; i++) { 103*cb5caa98Sdjl if (strcmp(protocol, ip_aliases[i].protocol) == 0) 104*cb5caa98Sdjl return (ip_aliases[i].alias); 105*cb5caa98Sdjl } 106*cb5caa98Sdjl /* 107*cb5caa98Sdjl * No aliase in the table. Return an all upper case aliase 108*cb5caa98Sdjl */ 109*cb5caa98Sdjl for (cp = protocol; *cp; cp++) 1107c478bd9Sstevel@tonic-gate *cp = toupper(*cp); 111*cb5caa98Sdjl 112*cb5caa98Sdjl return (protocol); 113*cb5caa98Sdjl } else { 114*cb5caa98Sdjl /* Return an all upper case aliase */ 115*cb5caa98Sdjl for (cp = protocol; *cp; cp++) 116*cb5caa98Sdjl *cp = toupper(*cp); 117*cb5caa98Sdjl 118*cb5caa98Sdjl return (protocol); 1197c478bd9Sstevel@tonic-gate } 120*cb5caa98Sdjl 1217c478bd9Sstevel@tonic-gate } 122*cb5caa98Sdjl /* 123*cb5caa98Sdjl * _nss_ldap_protocols2str is the data marshaling method for the protocols 124*cb5caa98Sdjl * getXbyY * (e.g., getbyname(), getbynumber(), getent()) backend processes. 125*cb5caa98Sdjl * This method is called after a successful ldap search has been performed. 126*cb5caa98Sdjl * This method will parse the ldap search values into a file format. 127*cb5caa98Sdjl * e.g. 128*cb5caa98Sdjl * idrp 45 IDRP 129*cb5caa98Sdjl * or 130*cb5caa98Sdjl * ospf 89 OSPFIGP 131*cb5caa98Sdjl */ 132*cb5caa98Sdjl 133*cb5caa98Sdjl static int 134*cb5caa98Sdjl _nss_ldap_protocols2str(ldap_backend_ptr be, nss_XbyY_args_t *argp) 135*cb5caa98Sdjl { 136*cb5caa98Sdjl uint_t i; 137*cb5caa98Sdjl int nss_result; 138*cb5caa98Sdjl int buflen = 0, len; 139*cb5caa98Sdjl char *cname = NULL; 140*cb5caa98Sdjl char *buffer = NULL, **number, *alias; 141*cb5caa98Sdjl ns_ldap_result_t *result = be->result; 142*cb5caa98Sdjl ns_ldap_attr_t *names; 143*cb5caa98Sdjl 144*cb5caa98Sdjl if (result == NULL) 145*cb5caa98Sdjl return (NSS_STR_PARSE_PARSE); 146*cb5caa98Sdjl 147*cb5caa98Sdjl buflen = argp->buf.buflen; 148*cb5caa98Sdjl if (argp->buf.result != NULL) { 149*cb5caa98Sdjl if ((be->buffer = calloc(1, buflen)) == NULL) { 150*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 151*cb5caa98Sdjl goto result_pls2str; 1527c478bd9Sstevel@tonic-gate } 153*cb5caa98Sdjl buffer = be->buffer; 154*cb5caa98Sdjl } else 155*cb5caa98Sdjl buffer = argp->buf.buffer; 156*cb5caa98Sdjl 157*cb5caa98Sdjl nss_result = NSS_STR_PARSE_SUCCESS; 158*cb5caa98Sdjl (void) memset(argp->buf.buffer, 0, buflen); 159*cb5caa98Sdjl 160*cb5caa98Sdjl names = __ns_ldap_getAttrStruct(result->entry, _P_NAME); 161*cb5caa98Sdjl if (names == NULL || names->attrvalue == NULL) { 162*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 163*cb5caa98Sdjl goto result_pls2str; 1647c478bd9Sstevel@tonic-gate } 165*cb5caa98Sdjl /* Get the canonical name */ 166*cb5caa98Sdjl cname = __s_api_get_canonical_name(result->entry, names, 1); 167*cb5caa98Sdjl if (cname == NULL || (len = strlen(cname)) < 1) { 168*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 169*cb5caa98Sdjl goto result_pls2str; 1707c478bd9Sstevel@tonic-gate } 171*cb5caa98Sdjl number = __ns_ldap_getAttr(result->entry, _P_PROTO); 172*cb5caa98Sdjl if (number == NULL || number[0] == NULL || 173*cb5caa98Sdjl (len = strlen(number[0])) < 1) { 174*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 175*cb5caa98Sdjl goto result_pls2str; 1767c478bd9Sstevel@tonic-gate } 177*cb5caa98Sdjl len = snprintf(buffer, buflen, "%s %s", cname, number[0]); 178*cb5caa98Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_pls2str); 179*cb5caa98Sdjl /* Append aliases */ 180*cb5caa98Sdjl if (names->value_count == 1) { 181*cb5caa98Sdjl /* create an aliase from protocol name */ 182*cb5caa98Sdjl alias = get_alias(cname); 183*cb5caa98Sdjl len = snprintf(buffer, buflen, " %s", alias); 184*cb5caa98Sdjl TEST_AND_ADJUST(len, buffer, buflen, result_pls2str); 185*cb5caa98Sdjl 186*cb5caa98Sdjl } else { 187*cb5caa98Sdjl for (i = 0; i < names->value_count; i++) { 188*cb5caa98Sdjl if (names->attrvalue[i] == NULL) { 189*cb5caa98Sdjl nss_result = NSS_STR_PARSE_PARSE; 190*cb5caa98Sdjl goto result_pls2str; 191*cb5caa98Sdjl } 192*cb5caa98Sdjl /* Skip the canonical name */ 193*cb5caa98Sdjl if (strcasecmp(names->attrvalue[i], cname) != 0) { 194*cb5caa98Sdjl len = snprintf(buffer, buflen, " %s", 195*cb5caa98Sdjl names->attrvalue[i]); 196*cb5caa98Sdjl TEST_AND_ADJUST(len, buffer, buflen, 197*cb5caa98Sdjl result_pls2str); 198*cb5caa98Sdjl } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 202*cb5caa98Sdjl /* The front end marshaller doesn't need to copy trailing nulls */ 203*cb5caa98Sdjl if (argp->buf.result != NULL) 204*cb5caa98Sdjl be->buflen = strlen(be->buffer); 2057c478bd9Sstevel@tonic-gate 206*cb5caa98Sdjl result_pls2str: 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate (void) __ns_ldap_freeResult(&be->result); 2097c478bd9Sstevel@tonic-gate return ((int)nss_result); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * getbyname gets struct protoent values by protocol name. This 2157c478bd9Sstevel@tonic-gate * function constructs an ldap search filter using the protocol 2167c478bd9Sstevel@tonic-gate * name invocation parameter and the getprotobyname search filter 2177c478bd9Sstevel@tonic-gate * defined. Once the filter is constructed, we search for a matching 2187c478bd9Sstevel@tonic-gate * entry and marshal the data results into *proto = (struct * 2197c478bd9Sstevel@tonic-gate * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent 2207c478bd9Sstevel@tonic-gate * performs the data marshaling. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate static nss_status_t 2247c478bd9Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a) 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 2277c478bd9Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 2287c478bd9Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 2297c478bd9Sstevel@tonic-gate char name[SEARCHFILTERLEN]; 2307c478bd9Sstevel@tonic-gate int ret; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0) 2337c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 2367c478bd9Sstevel@tonic-gate _F_GETPROTOBYNAME, name); 2377c478bd9Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 2387c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 2417c478bd9Sstevel@tonic-gate _F_GETPROTOBYNAME_SSD, name); 2427c478bd9Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 2437c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 2467c478bd9Sstevel@tonic-gate _PROTOCOLS, searchfilter, NULL, 2477c478bd9Sstevel@tonic-gate _merge_SSD_filter, userdata)); 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * getbynumber gets struct protoent values by protocol number. This 2537c478bd9Sstevel@tonic-gate * function constructs an ldap search filter using the protocol 2547c478bd9Sstevel@tonic-gate * name invocation parameter and the getprotobynumber search filter 2557c478bd9Sstevel@tonic-gate * defined. Once the filter is constructed, we search for a matching 2567c478bd9Sstevel@tonic-gate * entry and marshal the data results into *proto = (struct * 2577c478bd9Sstevel@tonic-gate * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent 2587c478bd9Sstevel@tonic-gate * performs the data marshaling. 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate static nss_status_t 2627c478bd9Sstevel@tonic-gate getbynumber(ldap_backend_ptr be, void *a) 2637c478bd9Sstevel@tonic-gate { 2647c478bd9Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 2657c478bd9Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 2667c478bd9Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 2677c478bd9Sstevel@tonic-gate int ret; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 2707c478bd9Sstevel@tonic-gate _F_GETPROTOBYNUMBER, argp->key.number); 2717c478bd9Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 2727c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 2757c478bd9Sstevel@tonic-gate _F_GETPROTOBYNUMBER_SSD, argp->key.number); 2767c478bd9Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 2777c478bd9Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 2807c478bd9Sstevel@tonic-gate _PROTOCOLS, searchfilter, NULL, 2817c478bd9Sstevel@tonic-gate _merge_SSD_filter, userdata)); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate static ldap_backend_op_t proto_ops[] = { 2857c478bd9Sstevel@tonic-gate _nss_ldap_destr, 2867c478bd9Sstevel@tonic-gate _nss_ldap_endent, 2877c478bd9Sstevel@tonic-gate _nss_ldap_setent, 2887c478bd9Sstevel@tonic-gate _nss_ldap_getent, 2897c478bd9Sstevel@tonic-gate getbyname, 2907c478bd9Sstevel@tonic-gate getbynumber 2917c478bd9Sstevel@tonic-gate }; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate /* 2957c478bd9Sstevel@tonic-gate * _nss_ldap_protocols_constr is where life begins. This function calls 2967c478bd9Sstevel@tonic-gate * the generic ldap constructor function to define and build the abstract 2977c478bd9Sstevel@tonic-gate * data types required to support ldap operations. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate /*ARGSUSED0*/ 3017c478bd9Sstevel@tonic-gate nss_backend_t * 3027c478bd9Sstevel@tonic-gate _nss_ldap_protocols_constr(const char *dummy1, const char *dummy2, 3037c478bd9Sstevel@tonic-gate const char *dummy3) 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate return ((nss_backend_t *)_nss_ldap_constr(proto_ops, 3077c478bd9Sstevel@tonic-gate sizeof (proto_ops)/sizeof (proto_ops[0]), _PROTOCOLS, 308*cb5caa98Sdjl protocols_attrs, _nss_ldap_protocols2str)); 3097c478bd9Sstevel@tonic-gate } 310