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 <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/socket.h> 31*0Sstevel@tonic-gate #include <net/if.h> 32*0Sstevel@tonic-gate #include <netinet/in.h> 33*0Sstevel@tonic-gate #include <net/if_arp.h> 34*0Sstevel@tonic-gate #include <netinet/if_ether.h> 35*0Sstevel@tonic-gate #include "ldap_common.h" 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* ether attributes filters */ 38*0Sstevel@tonic-gate #define _E_HOSTNAME "cn" 39*0Sstevel@tonic-gate #define _E_MACADDRESS "macaddress" 40*0Sstevel@tonic-gate #define _F_GETETHERBYHOST "(&(objectClass=ieee802Device)(cn=%s))" 41*0Sstevel@tonic-gate #define _F_GETETHERBYHOST_SSD "(&(%%s)(cn=%s))" 42*0Sstevel@tonic-gate #define _F_GETETHERBYETHER "(&(objectClass=ieee802Device)(macAddress=%s))" 43*0Sstevel@tonic-gate #define _F_GETETHERBYETHER_SSD "(&(%%s)(macAddress=%s))" 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate static const char *ethers_attrs[] = { 46*0Sstevel@tonic-gate _E_HOSTNAME, 47*0Sstevel@tonic-gate _E_MACADDRESS, 48*0Sstevel@tonic-gate (char *)NULL 49*0Sstevel@tonic-gate }; 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* 53*0Sstevel@tonic-gate * _nss_ldap_ethers2ent is the data marshaling method for the ethers 54*0Sstevel@tonic-gate * getXbyY * (e.g., getbyhost(), getbyether()) backend processes. This 55*0Sstevel@tonic-gate * method is called after a successful ldap search has been performed. 56*0Sstevel@tonic-gate * This method will parse the ldap search values into uchar_t *ether 57*0Sstevel@tonic-gate * = argp->buf.buffer which the frontend process expects. Three error 58*0Sstevel@tonic-gate * conditions are expected and returned to nsswitch. 59*0Sstevel@tonic-gate * 60*0Sstevel@tonic-gate * Place the resulting struct ether_addr from the ldap query into 61*0Sstevel@tonic-gate * argp->buf.result only if argp->buf.result is initialized (not NULL). 62*0Sstevel@tonic-gate * e.g., it happens for the call ether_hostton. 63*0Sstevel@tonic-gate * 64*0Sstevel@tonic-gate * Place the resulting hostname into argp->buf.buffer only if 65*0Sstevel@tonic-gate * argp->buf.buffer is initialized. I.e. it happens for the call 66*0Sstevel@tonic-gate * ether_ntohost. 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * argp->buf.buflen does not make sense for ethers. It is always set 69*0Sstevel@tonic-gate * to 0 by the frontend. The caller only passes a hostname pointer in 70*0Sstevel@tonic-gate * case of ether_ntohost, that is assumed to be big enough. For 71*0Sstevel@tonic-gate * ether_hostton, the struct ether_addr passed is a fixed size. 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * The interface does not let the caller specify how long is the buffer 74*0Sstevel@tonic-gate * pointed by host. We make a safe assumption that the callers will 75*0Sstevel@tonic-gate * always give MAXHOSTNAMELEN. In any case, it is the only finite number 76*0Sstevel@tonic-gate * we can lay our hands on in case of runaway strings, memory corruption etc. 77*0Sstevel@tonic-gate */ 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate static int 80*0Sstevel@tonic-gate _nss_ldap_ethers2ent(ldap_backend_ptr be, nss_XbyY_args_t *argp) 81*0Sstevel@tonic-gate { 82*0Sstevel@tonic-gate int i, ip; 83*0Sstevel@tonic-gate int nss_result; 84*0Sstevel@tonic-gate int buflen = (int)0; 85*0Sstevel@tonic-gate unsigned int t[ETHERADDRL]; 86*0Sstevel@tonic-gate unsigned long len = 0L; 87*0Sstevel@tonic-gate char *host = NULL; 88*0Sstevel@tonic-gate struct ether_addr *ether = NULL; 89*0Sstevel@tonic-gate ns_ldap_result_t *result = be->result; 90*0Sstevel@tonic-gate ns_ldap_attr_t *attrptr; 91*0Sstevel@tonic-gate int etherflag = 0, hostflag = 0; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate if (argp->buf.buffer) { 94*0Sstevel@tonic-gate hostflag = 1; 95*0Sstevel@tonic-gate host = argp->buf.buffer; 96*0Sstevel@tonic-gate } 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate buflen = (size_t)argp->buf.buflen; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate if (argp->buf.result) { 101*0Sstevel@tonic-gate etherflag = 1; 102*0Sstevel@tonic-gate ether = (struct ether_addr *)argp->buf.result; 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_SUCCESS; 106*0Sstevel@tonic-gate (void) memset(argp->buf.buffer, 0, buflen); 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate attrptr = getattr(result, 0); 109*0Sstevel@tonic-gate if (attrptr == NULL) { 110*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 111*0Sstevel@tonic-gate goto result_ea2ent; 112*0Sstevel@tonic-gate } 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate for (i = 0; i < result->entry->attr_count; i++) { 115*0Sstevel@tonic-gate attrptr = getattr(result, i); 116*0Sstevel@tonic-gate if (attrptr == NULL) { 117*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 118*0Sstevel@tonic-gate goto result_ea2ent; 119*0Sstevel@tonic-gate } 120*0Sstevel@tonic-gate if (hostflag) { 121*0Sstevel@tonic-gate if (strcasecmp(attrptr->attrname, _E_HOSTNAME) == 0) { 122*0Sstevel@tonic-gate if ((attrptr->attrvalue[0] == NULL) || 123*0Sstevel@tonic-gate (len = strlen(attrptr->attrvalue[0])) < 1) { 124*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 125*0Sstevel@tonic-gate goto result_ea2ent; 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate if (len > MAXHOSTNAMELEN) { 128*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_ERANGE; 129*0Sstevel@tonic-gate goto result_ea2ent; 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate (void) strcpy(host, attrptr->attrvalue[0]); 132*0Sstevel@tonic-gate continue; 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate if (etherflag) { 136*0Sstevel@tonic-gate if (strcasecmp(attrptr->attrname, _E_MACADDRESS) == 0) { 137*0Sstevel@tonic-gate if ((attrptr->attrvalue[0] == NULL) || 138*0Sstevel@tonic-gate (len = strlen(attrptr->attrvalue[0])) < 1) { 139*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 140*0Sstevel@tonic-gate goto result_ea2ent; 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate ip = (int)sscanf(attrptr->attrvalue[0], 143*0Sstevel@tonic-gate "%x:%x:%x:%x:%x:%x", &t[0], &t[1], 144*0Sstevel@tonic-gate &t[2], &t[3], &t[4], &t[5]); 145*0Sstevel@tonic-gate if (ip != ETHERADDRL) { 146*0Sstevel@tonic-gate nss_result = (int)NSS_STR_PARSE_PARSE; 147*0Sstevel@tonic-gate goto result_ea2ent; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate for (ip = 0; ip < ETHERADDRL; ip++) 150*0Sstevel@tonic-gate ether->ether_addr_octet[ip] = 151*0Sstevel@tonic-gate (uchar_t)t[ip]; 152*0Sstevel@tonic-gate continue; 153*0Sstevel@tonic-gate } 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate #ifdef DEBUG 158*0Sstevel@tonic-gate (void) fprintf(stdout, "\n[ether_addr.c: _nss_ldap_ethers2ent]\n"); 159*0Sstevel@tonic-gate if (host != NULL) 160*0Sstevel@tonic-gate (void) fprintf(stdout, " hostname: [%s]\n", host); 161*0Sstevel@tonic-gate if (ether != NULL) 162*0Sstevel@tonic-gate (void) fprintf(stdout, 163*0Sstevel@tonic-gate " ether_addr: [%x:%x:%x:%x:%x:%x]\n", 164*0Sstevel@tonic-gate ether->ether_addr_octet[0], 165*0Sstevel@tonic-gate ether->ether_addr_octet[1], 166*0Sstevel@tonic-gate ether->ether_addr_octet[2], 167*0Sstevel@tonic-gate ether->ether_addr_octet[3], 168*0Sstevel@tonic-gate ether->ether_addr_octet[4], 169*0Sstevel@tonic-gate ether->ether_addr_octet[5]); 170*0Sstevel@tonic-gate #endif /* DEBUG */ 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate result_ea2ent: 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate (void) __ns_ldap_freeResult(&be->result); 175*0Sstevel@tonic-gate return ((int)nss_result); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate /* 179*0Sstevel@tonic-gate * getbyhost gets an ethernet address by hostname. This function 180*0Sstevel@tonic-gate * constructs an ldap search filter using the hostname invocation 181*0Sstevel@tonic-gate * parameter and the getetherbyhost search filter defined. Once 182*0Sstevel@tonic-gate * the filter is constructed, we search for a matching entry and 183*0Sstevel@tonic-gate * marshal the data results into uchar_t *ether for the frontend 184*0Sstevel@tonic-gate * process. The function _nss_ldap_ethers2ent performs the data 185*0Sstevel@tonic-gate * marshaling. 186*0Sstevel@tonic-gate * 187*0Sstevel@tonic-gate * RFC 2307, An Approach for Using LDAP as a Network Information Service, 188*0Sstevel@tonic-gate * indicates that dn's be fully qualified. Host name searches will be on 189*0Sstevel@tonic-gate * fully qualified host names (e.g., foo.bar.sun.com). 190*0Sstevel@tonic-gate */ 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate static nss_status_t 193*0Sstevel@tonic-gate getbyhost(ldap_backend_ptr be, void *a) 194*0Sstevel@tonic-gate { 195*0Sstevel@tonic-gate char hostname[3 * MAXHOSTNAMELEN]; 196*0Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 197*0Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 198*0Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 199*0Sstevel@tonic-gate int ret; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate if (_ldap_filter_name(hostname, argp->key.name, sizeof (hostname)) != 0) 202*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 205*0Sstevel@tonic-gate _F_GETETHERBYHOST, hostname); 206*0Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 207*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 210*0Sstevel@tonic-gate _F_GETETHERBYHOST_SSD, hostname); 211*0Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 212*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 215*0Sstevel@tonic-gate _ETHERS, searchfilter, NULL, 216*0Sstevel@tonic-gate _merge_SSD_filter, userdata)); 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate /* 221*0Sstevel@tonic-gate * getbyether gets an ethernet address by ethernet address. This 222*0Sstevel@tonic-gate * function constructs an ldap search filter using the ASCII 223*0Sstevel@tonic-gate * ethernet address invocation parameter and the getetherbyether 224*0Sstevel@tonic-gate * search filter defined. Once the filter is constructed, we 225*0Sstevel@tonic-gate * search for a matching entry and marshal the data results into 226*0Sstevel@tonic-gate * uchar_t *ether for the frontend process. The function 227*0Sstevel@tonic-gate * _nss_ldap_ethers2ent performs the data marshaling. 228*0Sstevel@tonic-gate */ 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate static nss_status_t 231*0Sstevel@tonic-gate getbyether(ldap_backend_ptr be, void *a) 232*0Sstevel@tonic-gate { 233*0Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 234*0Sstevel@tonic-gate char etherstr[18]; 235*0Sstevel@tonic-gate uchar_t *e = argp->key.ether; 236*0Sstevel@tonic-gate char searchfilter[SEARCHFILTERLEN]; 237*0Sstevel@tonic-gate char userdata[SEARCHFILTERLEN]; 238*0Sstevel@tonic-gate int ret; 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate ret = snprintf(etherstr, sizeof (etherstr), "%x:%x:%x:%x:%x:%x", 241*0Sstevel@tonic-gate *e, *(e + 1), *(e + 2), *(e + 3), *(e + 4), *(e + 5)); 242*0Sstevel@tonic-gate if (ret >= sizeof (etherstr) || ret < 0) 243*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate ret = snprintf(searchfilter, sizeof (searchfilter), 246*0Sstevel@tonic-gate _F_GETETHERBYETHER, etherstr); 247*0Sstevel@tonic-gate if (ret >= sizeof (searchfilter) || ret < 0) 248*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate ret = snprintf(userdata, sizeof (userdata), 251*0Sstevel@tonic-gate _F_GETETHERBYETHER_SSD, etherstr); 252*0Sstevel@tonic-gate if (ret >= sizeof (userdata) || ret < 0) 253*0Sstevel@tonic-gate return ((nss_status_t)NSS_NOTFOUND); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate return ((nss_status_t)_nss_ldap_lookup(be, argp, 256*0Sstevel@tonic-gate _ETHERS, searchfilter, NULL, 257*0Sstevel@tonic-gate _merge_SSD_filter, userdata)); 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate static ldap_backend_op_t ethers_ops[] = { 262*0Sstevel@tonic-gate _nss_ldap_destr, 263*0Sstevel@tonic-gate getbyhost, 264*0Sstevel@tonic-gate getbyether 265*0Sstevel@tonic-gate }; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate /* 269*0Sstevel@tonic-gate * _nss_ldap_ethers_constr is where life begins. This function calls the 270*0Sstevel@tonic-gate * generic ldap constructor function to define and build the abstract 271*0Sstevel@tonic-gate * data types required to support ldap operations. 272*0Sstevel@tonic-gate */ 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate /*ARGSUSED0*/ 275*0Sstevel@tonic-gate nss_backend_t * 276*0Sstevel@tonic-gate _nss_ldap_ethers_constr(const char *dummy1, const char *dummy2, 277*0Sstevel@tonic-gate const char *dummy3) 278*0Sstevel@tonic-gate { 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate return ((nss_backend_t *)_nss_ldap_constr(ethers_ops, 281*0Sstevel@tonic-gate sizeof (ethers_ops)/sizeof (ethers_ops[0]), _ETHERS, 282*0Sstevel@tonic-gate ethers_attrs, _nss_ldap_ethers2ent)); 283*0Sstevel@tonic-gate } 284