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