1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <ctype.h> 30*0Sstevel@tonic-gate #include <netdb.h> 31*0Sstevel@tonic-gate #include "ns_internal.h" 32*0Sstevel@tonic-gate #include "ldap_common.h" 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate /* protocols attributes filters */ 35*0Sstevel@tonic-gate #define _P_NAME "cn" 36*0Sstevel@tonic-gate #define _P_PROTO "ipprotocolnumber" 37*0Sstevel@tonic-gate #define _F_GETPROTOBYNAME "(&(objectClass=ipProtocol)(cn=%s))" 38*0Sstevel@tonic-gate #define _F_GETPROTOBYNAME_SSD "(&(%%s)(cn=%s))" 39*0Sstevel@tonic-gate #define _F_GETPROTOBYNUMBER \ 40*0Sstevel@tonic-gate "(&(objectClass=ipProtocol)(ipProtocolNumber=%d))" 41*0Sstevel@tonic-gate #define _F_GETPROTOBYNUMBER_SSD \ 42*0Sstevel@tonic-gate "(&(%%s)(ipProtocolNumber=%d))" 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate static const char *protocols_attrs[] = { 45*0Sstevel@tonic-gate _P_NAME, 46*0Sstevel@tonic-gate _P_PROTO, 47*0Sstevel@tonic-gate (char *)NULL 48*0Sstevel@tonic-gate }; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* 52*0Sstevel@tonic-gate * _nss_ldap_protocols2ent is the data marshaling method for the protocols 53*0Sstevel@tonic-gate * getXbyY * (e.g., getbyname(), getbynumber(), getent()) backend processes. 54*0Sstevel@tonic-gate * This method is called after a successful ldap search has been performed. 55*0Sstevel@tonic-gate * This method will parse the ldap search values into *proto = (struct 56*0Sstevel@tonic-gate * protoent *)argp->buf.result which the frontend process expects. Three error 57*0Sstevel@tonic-gate * conditions are expected and returned to nsswitch. 58*0Sstevel@tonic-gate */ 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate static int 61*0Sstevel@tonic-gate _nss_ldap_protocols2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp) 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate int i, j; 64*0Sstevel@tonic-gate int nss_result; 65*0Sstevel@tonic-gate int buflen = (int)0; 66*0Sstevel@tonic-gate int firstime = (int)1; 67*0Sstevel@tonic-gate unsigned long len = 0L; 68*0Sstevel@tonic-gate char *cp, **mp, *cname = NULL; 69*0Sstevel@tonic-gate char *buffer = (char *)NULL; 70*0Sstevel@tonic-gate char *ceiling = (char *)NULL; 71*0Sstevel@tonic-gate struct protoent *proto = (struct protoent *)NULL; 72*0Sstevel@tonic-gate ns_ldap_result_t *result = be->result; 73*0Sstevel@tonic-gate ns_ldap_attr_t *attrptr; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate buffer = (char *)argp->buf.buffer; 76*0Sstevel@tonic-gate buflen = (size_t)argp->buf.buflen; 77*0Sstevel@tonic-gate if (!argp->buf.result) { 78*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_ERANGE; 79*0Sstevel@tonic-gate goto result_pls2ent; 80*0Sstevel@tonic-gate } 81*0Sstevel@tonic-gate proto = (struct protoent *)argp->buf.result; 82*0Sstevel@tonic-gate ceiling = buffer + buflen; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_SUCCESS; 85*0Sstevel@tonic-gate (void) memset(argp->buf.buffer, 0, buflen); 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate attrptr = getattr(result, 0); 88*0Sstevel@tonic-gate if (attrptr == NULL) { 89*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 90*0Sstevel@tonic-gate goto result_pls2ent; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate for (i = 0; i < result->entry->attr_count; i++) { 93*0Sstevel@tonic-gate attrptr = getattr(result, i); 94*0Sstevel@tonic-gate if (attrptr == NULL) { 95*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 96*0Sstevel@tonic-gate goto result_pls2ent; 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate if (strcasecmp(attrptr->attrname, _P_NAME) == 0) { 99*0Sstevel@tonic-gate for (j = 0; j < attrptr->value_count; j++) { 100*0Sstevel@tonic-gate if (firstime) { 101*0Sstevel@tonic-gate /* protocol name */ 102*0Sstevel@tonic-gate cname = __s_api_get_canonical_name( 103*0Sstevel@tonic-gate result->entry, attrptr, 1); 104*0Sstevel@tonic-gate if (cname == NULL || 105*0Sstevel@tonic-gate (len = strlen(cname)) < 1) { 106*0Sstevel@tonic-gate nss_result = 107*0Sstevel@tonic-gate NSS_STR_PARSE_PARSE; 108*0Sstevel@tonic-gate goto result_pls2ent; 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate proto->p_name = buffer; 111*0Sstevel@tonic-gate buffer += len + 1; 112*0Sstevel@tonic-gate if (buffer >= ceiling) { 113*0Sstevel@tonic-gate nss_result = 114*0Sstevel@tonic-gate (int)NSS_STR_PARSE_ERANGE; 115*0Sstevel@tonic-gate goto result_pls2ent; 116*0Sstevel@tonic-gate } 117*0Sstevel@tonic-gate (void) strcpy(proto->p_name, cname); 118*0Sstevel@tonic-gate mp = proto->p_aliases = 119*0Sstevel@tonic-gate (char **)ROUND_UP(buffer, 120*0Sstevel@tonic-gate sizeof (char **)); 121*0Sstevel@tonic-gate buffer = (char *)proto->p_aliases + 122*0Sstevel@tonic-gate sizeof (char *) * 123*0Sstevel@tonic-gate (attrptr->value_count + 1); 124*0Sstevel@tonic-gate buffer = (char *)ROUND_UP(buffer, 125*0Sstevel@tonic-gate sizeof (char **)); 126*0Sstevel@tonic-gate if (buffer >= ceiling) { 127*0Sstevel@tonic-gate nss_result = 128*0Sstevel@tonic-gate (int)NSS_STR_PARSE_ERANGE; 129*0Sstevel@tonic-gate goto result_pls2ent; 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate firstime = (int)0; 132*0Sstevel@tonic-gate } 133*0Sstevel@tonic-gate /* alias list */ 134*0Sstevel@tonic-gate if ((attrptr->attrvalue[j] == NULL) || 135*0Sstevel@tonic-gate (len = strlen(attrptr->attrvalue[j])) < 1) { 136*0Sstevel@tonic-gate nss_result = NSS_STR_PARSE_PARSE; 137*0Sstevel@tonic-gate goto result_pls2ent; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate /* 140*0Sstevel@tonic-gate * When the data is imported by ldapaddent, 141*0Sstevel@tonic-gate * it does not save the aliase in the "cn" 142*0Sstevel@tonic-gate * that is same as the canonical name but only 143*0Sstevel@tonic-gate * differnt in case. 144*0Sstevel@tonic-gate * e.g. 145*0Sstevel@tonic-gate * icmp 1 ICMP 146*0Sstevel@tonic-gate * 147*0Sstevel@tonic-gate * is saved as 148*0Sstevel@tonic-gate * 149*0Sstevel@tonic-gate * dn: cn=icmp, ... 150*0Sstevel@tonic-gate * ... 151*0Sstevel@tonic-gate * cn: icmp 152*0Sstevel@tonic-gate * ... 153*0Sstevel@tonic-gate * So it needs to replicate the canonical name 154*0Sstevel@tonic-gate * as an aliase of upper case. 155*0Sstevel@tonic-gate * 156*0Sstevel@tonic-gate * But in the case of 157*0Sstevel@tonic-gate * ospf 89 OSPFIGP 158*0Sstevel@tonic-gate * it creates a redundant aliase. 159*0Sstevel@tonic-gate * e.g. 160*0Sstevel@tonic-gate * dn: cn=icmp, ... 161*0Sstevel@tonic-gate * ... 162*0Sstevel@tonic-gate * cn: ospf 163*0Sstevel@tonic-gate * cn: OSPFIGP 164*0Sstevel@tonic-gate * ... 165*0Sstevel@tonic-gate * 166*0Sstevel@tonic-gate * getent services ospf 167*0Sstevel@tonic-gate * ==> ospf 89 ospf OSPFIGP 168*0Sstevel@tonic-gate * 169*0Sstevel@tonic-gate * Some condition check is added to handle this 170*0Sstevel@tonic-gate * scenario. Such check also works with 171*0Sstevel@tonic-gate * following scenario. 172*0Sstevel@tonic-gate * dn: cn=icmp, ... 173*0Sstevel@tonic-gate * ... 174*0Sstevel@tonic-gate * cn: icmp 175*0Sstevel@tonic-gate * cn: ICMP 176*0Sstevel@tonic-gate * ... 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate if (strcmp(proto->p_name, 179*0Sstevel@tonic-gate attrptr->attrvalue[j]) == 0) { 180*0Sstevel@tonic-gate if (attrptr->value_count > 1) 181*0Sstevel@tonic-gate /* Do not replicate */ 182*0Sstevel@tonic-gate continue; 183*0Sstevel@tonic-gate for (cp = attrptr->attrvalue[j]; 184*0Sstevel@tonic-gate *cp; cp++) 185*0Sstevel@tonic-gate *cp = toupper(*cp); 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate *mp = buffer; 188*0Sstevel@tonic-gate buffer += len + 1; 189*0Sstevel@tonic-gate if (buffer >= ceiling) { 190*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_ERANGE; 191*0Sstevel@tonic-gate goto result_pls2ent; 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate (void) strcpy(*mp++, attrptr->attrvalue[j]); 194*0Sstevel@tonic-gate continue; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate if (strcasecmp(attrptr->attrname, _P_PROTO) == 0) { 198*0Sstevel@tonic-gate if ((attrptr->attrvalue[0] == NULL) || 199*0Sstevel@tonic-gate (len = strlen(attrptr->attrvalue[0])) < 1) { 200*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 201*0Sstevel@tonic-gate goto result_pls2ent; 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate errno = 0; 204*0Sstevel@tonic-gate proto->p_proto = (int)strtol(attrptr->attrvalue[0], 205*0Sstevel@tonic-gate (char **)NULL, 10); 206*0Sstevel@tonic-gate if (errno != 0) { 207*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 208*0Sstevel@tonic-gate goto result_pls2ent; 209*0Sstevel@tonic-gate } 210*0Sstevel@tonic-gate continue; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate #ifdef DEBUG 215*0Sstevel@tonic-gate (void) fprintf(stdout, "\n[getprotoent.c: _nss_ldap_protocols2ent]\n"); 216*0Sstevel@tonic-gate (void) fprintf(stdout, " p_name: [%s]\n", proto->p_name); 217*0Sstevel@tonic-gate if (mp != NULL) { 218*0Sstevel@tonic-gate for (mp = proto->p_aliases; *mp != NULL; mp++) 219*0Sstevel@tonic-gate (void) fprintf(stdout, " p_aliases: [%s]\n", *mp); 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate (void) fprintf(stdout, " p_proto: [%d]\n", proto->p_proto); 222*0Sstevel@tonic-gate #endif /* DEBUG */ 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate result_pls2ent: 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate (void) __ns_ldap_freeResult(&be->result); 227*0Sstevel@tonic-gate return ((int)nss_result); 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* 232*0Sstevel@tonic-gate * getbyname gets struct protoent values by protocol name. This 233*0Sstevel@tonic-gate * function constructs an ldap search filter using the protocol 234*0Sstevel@tonic-gate * name invocation parameter and the getprotobyname search filter 235*0Sstevel@tonic-gate * defined. Once the filter is constructed, we search for a matching 236*0Sstevel@tonic-gate * entry and marshal the data results into *proto = (struct * 237*0Sstevel@tonic-gate * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent 238*0Sstevel@tonic-gate * performs the data marshaling. 239*0Sstevel@tonic-gate */ 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate static nss_status_t 242*0Sstevel@tonic-gate getbyname(ldap_backend_ptr be, void *a) 243*0Sstevel@tonic-gate { 244*0Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 245*0Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 246*0Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 247*0Sstevel@tonic-gate char name[SEARCHFILTERLEN]; 248*0Sstevel@tonic-gate int ret; 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if (_ldap_filter_name(name, argp->key.name, sizeof (name)) != 0) 251*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 254*0Sstevel@tonic-gate _F_GETPROTOBYNAME, name); 255*0Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 256*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 259*0Sstevel@tonic-gate _F_GETPROTOBYNAME_SSD, name); 260*0Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 261*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 264*0Sstevel@tonic-gate _PROTOCOLS, searchfilter, NULL, 265*0Sstevel@tonic-gate _merge_SSD_filter, userdata)); 266*0Sstevel@tonic-gate } 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate /* 270*0Sstevel@tonic-gate * getbynumber gets struct protoent values by protocol number. This 271*0Sstevel@tonic-gate * function constructs an ldap search filter using the protocol 272*0Sstevel@tonic-gate * name invocation parameter and the getprotobynumber search filter 273*0Sstevel@tonic-gate * defined. Once the filter is constructed, we search for a matching 274*0Sstevel@tonic-gate * entry and marshal the data results into *proto = (struct * 275*0Sstevel@tonic-gate * protoent *)argp->buf.result. The function _nss_ldap_protocols2ent 276*0Sstevel@tonic-gate * performs the data marshaling. 277*0Sstevel@tonic-gate */ 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate static nss_status_t 280*0Sstevel@tonic-gate getbynumber(ldap_backend_ptr be, void *a) 281*0Sstevel@tonic-gate { 282*0Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 283*0Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 284*0Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 285*0Sstevel@tonic-gate int ret; 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 288*0Sstevel@tonic-gate _F_GETPROTOBYNUMBER, argp->key.number); 289*0Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 290*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 293*0Sstevel@tonic-gate _F_GETPROTOBYNUMBER_SSD, argp->key.number); 294*0Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 295*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 298*0Sstevel@tonic-gate _PROTOCOLS, searchfilter, NULL, 299*0Sstevel@tonic-gate _merge_SSD_filter, userdata)); 300*0Sstevel@tonic-gate } 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gate static ldap_backend_op_t proto_ops[] = { 303*0Sstevel@tonic-gate _nss_ldap_destr, 304*0Sstevel@tonic-gate _nss_ldap_endent, 305*0Sstevel@tonic-gate _nss_ldap_setent, 306*0Sstevel@tonic-gate _nss_ldap_getent, 307*0Sstevel@tonic-gate getbyname, 308*0Sstevel@tonic-gate getbynumber 309*0Sstevel@tonic-gate }; 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate /* 313*0Sstevel@tonic-gate * _nss_ldap_protocols_constr is where life begins. This function calls 314*0Sstevel@tonic-gate * the generic ldap constructor function to define and build the abstract 315*0Sstevel@tonic-gate * data types required to support ldap operations. 316*0Sstevel@tonic-gate */ 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate /*ARGSUSED0*/ 319*0Sstevel@tonic-gate nss_backend_t * 320*0Sstevel@tonic-gate _nss_ldap_protocols_constr(const char *dummy1, const char *dummy2, 321*0Sstevel@tonic-gate const char *dummy3) 322*0Sstevel@tonic-gate { 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate return ((nss_backend_t *)_nss_ldap_constr(proto_ops, 325*0Sstevel@tonic-gate sizeof (proto_ops)/sizeof (proto_ops[0]), _PROTOCOLS, 326*0Sstevel@tonic-gate protocols_attrs, _nss_ldap_protocols2ent)); 327*0Sstevel@tonic-gate } 328