1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 33*7c478bd9Sstevel@tonic-gate * All Rights Reserved 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*7c478bd9Sstevel@tonic-gate * contributors. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate /* 43*7c478bd9Sstevel@tonic-gate * All routines necessary to deal the "ethers" database. The sources 44*7c478bd9Sstevel@tonic-gate * contain mappings between 48 bit ethernet addresses and corresponding 45*7c478bd9Sstevel@tonic-gate * hosts names. The addresses have an ascii representation of the form 46*7c478bd9Sstevel@tonic-gate * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the 47*7c478bd9Sstevel@tonic-gate * bytes are always in network order. 48*7c478bd9Sstevel@tonic-gate */ 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #include "mtlib.h" 51*7c478bd9Sstevel@tonic-gate #include <stdio.h> 52*7c478bd9Sstevel@tonic-gate #include <ctype.h> 53*7c478bd9Sstevel@tonic-gate #include <string.h> 54*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 55*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 56*7c478bd9Sstevel@tonic-gate #include <thread.h> 57*7c478bd9Sstevel@tonic-gate #include <sys/socket.h> 58*7c478bd9Sstevel@tonic-gate #include <net/if.h> 59*7c478bd9Sstevel@tonic-gate #include <netinet/in.h> 60*7c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h> 61*7c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static int str2ether(const char *, int, void *, char *, int); 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate static void 68*7c478bd9Sstevel@tonic-gate _nss_initf_ethers(nss_db_params_t *p) 69*7c478bd9Sstevel@tonic-gate { 70*7c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_ETHERS; 71*7c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_ETHERS; 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate /* 75*7c478bd9Sstevel@tonic-gate * Given a host's name, this routine finds the corresponding 48 bit 76*7c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 77*7c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate int 80*7c478bd9Sstevel@tonic-gate ether_hostton( 81*7c478bd9Sstevel@tonic-gate const char *host, /* function input */ 82*7c478bd9Sstevel@tonic-gate struct ether_addr *e /* function output */ 83*7c478bd9Sstevel@tonic-gate ) 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 86*7c478bd9Sstevel@tonic-gate nss_status_t res; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate /* 89*7c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 90*7c478bd9Sstevel@tonic-gate */ 91*7c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether); 92*7c478bd9Sstevel@tonic-gate arg.key.name = host; 93*7c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 94*7c478bd9Sstevel@tonic-gate NSS_DBOP_ETHERS_HOSTTON, &arg); 95*7c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 96*7c478bd9Sstevel@tonic-gate return (arg.status = res); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * Given a 48 bit ethernet address, it finds the corresponding hostname 101*7c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 102*7c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 103*7c478bd9Sstevel@tonic-gate */ 104*7c478bd9Sstevel@tonic-gate int 105*7c478bd9Sstevel@tonic-gate ether_ntohost( 106*7c478bd9Sstevel@tonic-gate char *host, /* function output */ 107*7c478bd9Sstevel@tonic-gate const struct ether_addr *e /* function input */ 108*7c478bd9Sstevel@tonic-gate ) 109*7c478bd9Sstevel@tonic-gate { 110*7c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 111*7c478bd9Sstevel@tonic-gate nss_status_t res; 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate /* 114*7c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 115*7c478bd9Sstevel@tonic-gate */ 116*7c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether); 117*7c478bd9Sstevel@tonic-gate arg.key.ether = (void *)e; 118*7c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 119*7c478bd9Sstevel@tonic-gate NSS_DBOP_ETHERS_NTOHOST, &arg); 120*7c478bd9Sstevel@tonic-gate /* memcpy(host, ether_res.host, strlen(ether_res.host)); */ 121*7c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 122*7c478bd9Sstevel@tonic-gate return (arg.status = res); 123*7c478bd9Sstevel@tonic-gate } 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate /* 126*7c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components. The line has 127*7c478bd9Sstevel@tonic-gate * the form 8:0:20:1:17:c8 krypton 128*7c478bd9Sstevel@tonic-gate * where the first part is a 48 bit ethernet address and the second is 129*7c478bd9Sstevel@tonic-gate * the corresponding hosts name. 130*7c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 131*7c478bd9Sstevel@tonic-gate */ 132*7c478bd9Sstevel@tonic-gate int 133*7c478bd9Sstevel@tonic-gate ether_line( 134*7c478bd9Sstevel@tonic-gate const char *s, /* the string to be parsed */ 135*7c478bd9Sstevel@tonic-gate struct ether_addr *e, /* ethernet address struct to be filled in */ 136*7c478bd9Sstevel@tonic-gate char *hostname /* hosts name to be set */ 137*7c478bd9Sstevel@tonic-gate ) 138*7c478bd9Sstevel@tonic-gate { 139*7c478bd9Sstevel@tonic-gate int i; 140*7c478bd9Sstevel@tonic-gate uint_t t[6]; 141*7c478bd9Sstevel@tonic-gate 142*7c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", 143*7c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname); 144*7c478bd9Sstevel@tonic-gate if (i != 7) { 145*7c478bd9Sstevel@tonic-gate return (7 - i); 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 148*7c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 149*7c478bd9Sstevel@tonic-gate return (0); 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* 153*7c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components. 154*7c478bd9Sstevel@tonic-gate * Useful for the vile purposes of the backends that 155*7c478bd9Sstevel@tonic-gate * expect a str2ether() format. 156*7c478bd9Sstevel@tonic-gate * 157*7c478bd9Sstevel@tonic-gate * This function, after parsing the instr line, will 158*7c478bd9Sstevel@tonic-gate * place the resulting struct ether_addr in b->buf.result only if 159*7c478bd9Sstevel@tonic-gate * b->buf.result is initialized (not NULL). I.e. it always happens 160*7c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 161*7c478bd9Sstevel@tonic-gate * then do a match for the ether key) and happens for "nis" 162*7c478bd9Sstevel@tonic-gate * backend only if the call was ether_hostton. 163*7c478bd9Sstevel@tonic-gate * 164*7c478bd9Sstevel@tonic-gate * Also, it will place the resulting hostname into b->buf.buffer 165*7c478bd9Sstevel@tonic-gate * only if b->buf.buffer is initialized. I.e. it always happens 166*7c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 167*7c478bd9Sstevel@tonic-gate * then do a match for the host key) and happens for "nis" 168*7c478bd9Sstevel@tonic-gate * backend only if the call was ether_ntohost. 169*7c478bd9Sstevel@tonic-gate * 170*7c478bd9Sstevel@tonic-gate * Cannot use the sscanf() technique for parsing because instr 171*7c478bd9Sstevel@tonic-gate * is a read-only, not necessarily null-terminated, buffer. 172*7c478bd9Sstevel@tonic-gate * 173*7c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 174*7c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 175*7c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 176*7c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 179*7c478bd9Sstevel@tonic-gate islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 180*7c478bd9Sstevel@tonic-gate #define lisalnum(x) (isdigit(x) || \ 181*7c478bd9Sstevel@tonic-gate ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 182*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 183*7c478bd9Sstevel@tonic-gate static int 184*7c478bd9Sstevel@tonic-gate str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 185*7c478bd9Sstevel@tonic-gate { 186*7c478bd9Sstevel@tonic-gate uchar_t *ether = (uchar_t *)ent; 187*7c478bd9Sstevel@tonic-gate char *host = buffer; 188*7c478bd9Sstevel@tonic-gate const char *p, *limit, *start; 189*7c478bd9Sstevel@tonic-gate ptrdiff_t i; 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate p = instr; 192*7c478bd9Sstevel@tonic-gate limit = p + lenstr; 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate /* skip beginning whitespace, if any */ 195*7c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) 196*7c478bd9Sstevel@tonic-gate p++; 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate if (ether) { /* parse ether */ 199*7c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 200*7c478bd9Sstevel@tonic-gate int j = 0, n = 0; 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate start = p; 203*7c478bd9Sstevel@tonic-gate while (p < limit && lisalnum(start[j])) { 204*7c478bd9Sstevel@tonic-gate /* don't worry about overflow here */ 205*7c478bd9Sstevel@tonic-gate n = 16 * n + DIGIT(start[j]); 206*7c478bd9Sstevel@tonic-gate j++; 207*7c478bd9Sstevel@tonic-gate p++; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate if (*p != ':' && i < 5) { 210*7c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 211*7c478bd9Sstevel@tonic-gate } else { 212*7c478bd9Sstevel@tonic-gate p++; 213*7c478bd9Sstevel@tonic-gate *(ether + i) = (uchar_t)n; 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate } else { /* skip ether */ 217*7c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p)) 218*7c478bd9Sstevel@tonic-gate p++; 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate if (host) { /* parse host */ 221*7c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip whitespace */ 222*7c478bd9Sstevel@tonic-gate p++; 223*7c478bd9Sstevel@tonic-gate start = p; 224*7c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p)) /* skip hostname */ 225*7c478bd9Sstevel@tonic-gate p++; 226*7c478bd9Sstevel@tonic-gate if ((i = (p - start)) < MAXHOSTNAMELEN) { 227*7c478bd9Sstevel@tonic-gate (void) memcpy(host, start, i); 228*7c478bd9Sstevel@tonic-gate host[i] = '\0'; 229*7c478bd9Sstevel@tonic-gate } else 230*7c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); /* failure */ 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate typedef struct { 236*7c478bd9Sstevel@tonic-gate char ea_string[18]; 237*7c478bd9Sstevel@tonic-gate struct ether_addr ea_addr; 238*7c478bd9Sstevel@tonic-gate } eabuf_t; 239*7c478bd9Sstevel@tonic-gate 240*7c478bd9Sstevel@tonic-gate static eabuf_t * 241*7c478bd9Sstevel@tonic-gate ea_buf(void) 242*7c478bd9Sstevel@tonic-gate { 243*7c478bd9Sstevel@tonic-gate static thread_key_t key; 244*7c478bd9Sstevel@tonic-gate static int key_once = 0; 245*7c478bd9Sstevel@tonic-gate static mutex_t tsd_lock = DEFAULTMUTEX; 246*7c478bd9Sstevel@tonic-gate static eabuf_t ea_main; 247*7c478bd9Sstevel@tonic-gate eabuf_t *eabuf = NULL; 248*7c478bd9Sstevel@tonic-gate 249*7c478bd9Sstevel@tonic-gate if (thr_main()) 250*7c478bd9Sstevel@tonic-gate return (&ea_main); 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate if (key_once == 0) { 253*7c478bd9Sstevel@tonic-gate (void) mutex_lock(&tsd_lock); 254*7c478bd9Sstevel@tonic-gate if (key_once == 0) { 255*7c478bd9Sstevel@tonic-gate if (thr_keycreate(&key, free) != 0) { 256*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tsd_lock); 257*7c478bd9Sstevel@tonic-gate return (NULL); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate key_once = 1; 260*7c478bd9Sstevel@tonic-gate } 261*7c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tsd_lock); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate (void) thr_getspecific(key, (void **)&eabuf); 264*7c478bd9Sstevel@tonic-gate if (eabuf == NULL) { 265*7c478bd9Sstevel@tonic-gate eabuf = malloc(sizeof (eabuf_t)); 266*7c478bd9Sstevel@tonic-gate (void) thr_setspecific(key, eabuf); 267*7c478bd9Sstevel@tonic-gate } 268*7c478bd9Sstevel@tonic-gate return (eabuf); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate 271*7c478bd9Sstevel@tonic-gate /* 272*7c478bd9Sstevel@tonic-gate * Converts a 48 bit ethernet number to its string representation. 273*7c478bd9Sstevel@tonic-gate */ 274*7c478bd9Sstevel@tonic-gate char * 275*7c478bd9Sstevel@tonic-gate ether_ntoa(const struct ether_addr *e) 276*7c478bd9Sstevel@tonic-gate { 277*7c478bd9Sstevel@tonic-gate eabuf_t *eabuf; 278*7c478bd9Sstevel@tonic-gate char *s; 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 281*7c478bd9Sstevel@tonic-gate return (NULL); 282*7c478bd9Sstevel@tonic-gate s = eabuf->ea_string; 283*7c478bd9Sstevel@tonic-gate (void) sprintf(s, "%x:%x:%x:%x:%x:%x", 284*7c478bd9Sstevel@tonic-gate e->ether_addr_octet[0], e->ether_addr_octet[1], 285*7c478bd9Sstevel@tonic-gate e->ether_addr_octet[2], e->ether_addr_octet[3], 286*7c478bd9Sstevel@tonic-gate e->ether_addr_octet[4], e->ether_addr_octet[5]); 287*7c478bd9Sstevel@tonic-gate return (s); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate /* 291*7c478bd9Sstevel@tonic-gate * Converts an ethernet address representation back into its 48 bits. 292*7c478bd9Sstevel@tonic-gate */ 293*7c478bd9Sstevel@tonic-gate struct ether_addr * 294*7c478bd9Sstevel@tonic-gate ether_aton(const char *s) 295*7c478bd9Sstevel@tonic-gate { 296*7c478bd9Sstevel@tonic-gate eabuf_t *eabuf; 297*7c478bd9Sstevel@tonic-gate struct ether_addr *e; 298*7c478bd9Sstevel@tonic-gate int i; 299*7c478bd9Sstevel@tonic-gate uint_t t[6]; 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 302*7c478bd9Sstevel@tonic-gate return (NULL); 303*7c478bd9Sstevel@tonic-gate e = &eabuf->ea_addr; 304*7c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x", 305*7c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]); 306*7c478bd9Sstevel@tonic-gate if (i != 6) 307*7c478bd9Sstevel@tonic-gate return (NULL); 308*7c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 309*7c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 310*7c478bd9Sstevel@tonic-gate return (e); 311*7c478bd9Sstevel@tonic-gate } 312