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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*e8031f0aSraf 237c478bd9Sstevel@tonic-gate /* 24*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 337c478bd9Sstevel@tonic-gate * The Regents of the University of California 347c478bd9Sstevel@tonic-gate * All Rights Reserved 357c478bd9Sstevel@tonic-gate * 367c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 377c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 387c478bd9Sstevel@tonic-gate * contributors. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * All routines necessary to deal the "ethers" database. The sources 457c478bd9Sstevel@tonic-gate * contain mappings between 48 bit ethernet addresses and corresponding 467c478bd9Sstevel@tonic-gate * hosts names. The addresses have an ascii representation of the form 477c478bd9Sstevel@tonic-gate * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the 487c478bd9Sstevel@tonic-gate * bytes are always in network order. 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate 51*e8031f0aSraf #include "c_synonyms.h" 527c478bd9Sstevel@tonic-gate #include <stdio.h> 537c478bd9Sstevel@tonic-gate #include <ctype.h> 547c478bd9Sstevel@tonic-gate #include <string.h> 557c478bd9Sstevel@tonic-gate #include <stdlib.h> 567c478bd9Sstevel@tonic-gate #include <sys/types.h> 577c478bd9Sstevel@tonic-gate #include <thread.h> 587c478bd9Sstevel@tonic-gate #include <sys/socket.h> 597c478bd9Sstevel@tonic-gate #include <net/if.h> 607c478bd9Sstevel@tonic-gate #include <netinet/in.h> 617c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h> 627c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static int str2ether(const char *, int, void *, char *, int); 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static void 697c478bd9Sstevel@tonic-gate _nss_initf_ethers(nss_db_params_t *p) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_ETHERS; 727c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_ETHERS; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate /* 767c478bd9Sstevel@tonic-gate * Given a host's name, this routine finds the corresponding 48 bit 777c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 787c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate int 817c478bd9Sstevel@tonic-gate ether_hostton( 827c478bd9Sstevel@tonic-gate const char *host, /* function input */ 837c478bd9Sstevel@tonic-gate struct ether_addr *e /* function output */ 847c478bd9Sstevel@tonic-gate ) 857c478bd9Sstevel@tonic-gate { 867c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 877c478bd9Sstevel@tonic-gate nss_status_t res; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether); 937c478bd9Sstevel@tonic-gate arg.key.name = host; 947c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 957c478bd9Sstevel@tonic-gate NSS_DBOP_ETHERS_HOSTTON, &arg); 967c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 977c478bd9Sstevel@tonic-gate return (arg.status = res); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* 1017c478bd9Sstevel@tonic-gate * Given a 48 bit ethernet address, it finds the corresponding hostname 1027c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 1037c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1047c478bd9Sstevel@tonic-gate */ 1057c478bd9Sstevel@tonic-gate int 1067c478bd9Sstevel@tonic-gate ether_ntohost( 1077c478bd9Sstevel@tonic-gate char *host, /* function output */ 1087c478bd9Sstevel@tonic-gate const struct ether_addr *e /* function input */ 1097c478bd9Sstevel@tonic-gate ) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 1127c478bd9Sstevel@tonic-gate nss_status_t res; 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate /* 1157c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 1167c478bd9Sstevel@tonic-gate */ 1177c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether); 1187c478bd9Sstevel@tonic-gate arg.key.ether = (void *)e; 1197c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 1207c478bd9Sstevel@tonic-gate NSS_DBOP_ETHERS_NTOHOST, &arg); 1217c478bd9Sstevel@tonic-gate /* memcpy(host, ether_res.host, strlen(ether_res.host)); */ 1227c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 1237c478bd9Sstevel@tonic-gate return (arg.status = res); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate /* 1277c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components. The line has 1287c478bd9Sstevel@tonic-gate * the form 8:0:20:1:17:c8 krypton 1297c478bd9Sstevel@tonic-gate * where the first part is a 48 bit ethernet address and the second is 1307c478bd9Sstevel@tonic-gate * the corresponding hosts name. 1317c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate int 1347c478bd9Sstevel@tonic-gate ether_line( 1357c478bd9Sstevel@tonic-gate const char *s, /* the string to be parsed */ 1367c478bd9Sstevel@tonic-gate struct ether_addr *e, /* ethernet address struct to be filled in */ 1377c478bd9Sstevel@tonic-gate char *hostname /* hosts name to be set */ 1387c478bd9Sstevel@tonic-gate ) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate int i; 1417c478bd9Sstevel@tonic-gate uint_t t[6]; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", 1447c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname); 1457c478bd9Sstevel@tonic-gate if (i != 7) { 1467c478bd9Sstevel@tonic-gate return (7 - i); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 1497c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 1507c478bd9Sstevel@tonic-gate return (0); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components. 1557c478bd9Sstevel@tonic-gate * Useful for the vile purposes of the backends that 1567c478bd9Sstevel@tonic-gate * expect a str2ether() format. 1577c478bd9Sstevel@tonic-gate * 1587c478bd9Sstevel@tonic-gate * This function, after parsing the instr line, will 1597c478bd9Sstevel@tonic-gate * place the resulting struct ether_addr in b->buf.result only if 1607c478bd9Sstevel@tonic-gate * b->buf.result is initialized (not NULL). I.e. it always happens 1617c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1627c478bd9Sstevel@tonic-gate * then do a match for the ether key) and happens for "nis" 1637c478bd9Sstevel@tonic-gate * backend only if the call was ether_hostton. 1647c478bd9Sstevel@tonic-gate * 1657c478bd9Sstevel@tonic-gate * Also, it will place the resulting hostname into b->buf.buffer 1667c478bd9Sstevel@tonic-gate * only if b->buf.buffer is initialized. I.e. it always happens 1677c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1687c478bd9Sstevel@tonic-gate * then do a match for the host key) and happens for "nis" 1697c478bd9Sstevel@tonic-gate * backend only if the call was ether_ntohost. 1707c478bd9Sstevel@tonic-gate * 1717c478bd9Sstevel@tonic-gate * Cannot use the sscanf() technique for parsing because instr 1727c478bd9Sstevel@tonic-gate * is a read-only, not necessarily null-terminated, buffer. 1737c478bd9Sstevel@tonic-gate * 1747c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 1757c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 1767c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 1777c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 1807c478bd9Sstevel@tonic-gate islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 1817c478bd9Sstevel@tonic-gate #define lisalnum(x) (isdigit(x) || \ 1827c478bd9Sstevel@tonic-gate ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 1837c478bd9Sstevel@tonic-gate /* ARGSUSED */ 1847c478bd9Sstevel@tonic-gate static int 1857c478bd9Sstevel@tonic-gate str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 1867c478bd9Sstevel@tonic-gate { 1877c478bd9Sstevel@tonic-gate uchar_t *ether = (uchar_t *)ent; 1887c478bd9Sstevel@tonic-gate char *host = buffer; 1897c478bd9Sstevel@tonic-gate const char *p, *limit, *start; 1907c478bd9Sstevel@tonic-gate ptrdiff_t i; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate p = instr; 1937c478bd9Sstevel@tonic-gate limit = p + lenstr; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* skip beginning whitespace, if any */ 1967c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) 1977c478bd9Sstevel@tonic-gate p++; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate if (ether) { /* parse ether */ 2007c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) { 2017c478bd9Sstevel@tonic-gate int j = 0, n = 0; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate start = p; 2047c478bd9Sstevel@tonic-gate while (p < limit && lisalnum(start[j])) { 2057c478bd9Sstevel@tonic-gate /* don't worry about overflow here */ 2067c478bd9Sstevel@tonic-gate n = 16 * n + DIGIT(start[j]); 2077c478bd9Sstevel@tonic-gate j++; 2087c478bd9Sstevel@tonic-gate p++; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate if (*p != ':' && i < 5) { 2117c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2127c478bd9Sstevel@tonic-gate } else { 2137c478bd9Sstevel@tonic-gate p++; 2147c478bd9Sstevel@tonic-gate *(ether + i) = (uchar_t)n; 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate } else { /* skip ether */ 2187c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p)) 2197c478bd9Sstevel@tonic-gate p++; 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate if (host) { /* parse host */ 2227c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip whitespace */ 2237c478bd9Sstevel@tonic-gate p++; 2247c478bd9Sstevel@tonic-gate start = p; 2257c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p)) /* skip hostname */ 2267c478bd9Sstevel@tonic-gate p++; 2277c478bd9Sstevel@tonic-gate if ((i = (p - start)) < MAXHOSTNAMELEN) { 2287c478bd9Sstevel@tonic-gate (void) memcpy(host, start, i); 2297c478bd9Sstevel@tonic-gate host[i] = '\0'; 2307c478bd9Sstevel@tonic-gate } else 2317c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); /* failure */ 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate typedef struct { 2377c478bd9Sstevel@tonic-gate char ea_string[18]; 2387c478bd9Sstevel@tonic-gate struct ether_addr ea_addr; 2397c478bd9Sstevel@tonic-gate } eabuf_t; 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate static eabuf_t * 2427c478bd9Sstevel@tonic-gate ea_buf(void) 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate static thread_key_t key; 2457c478bd9Sstevel@tonic-gate static int key_once = 0; 2467c478bd9Sstevel@tonic-gate static mutex_t tsd_lock = DEFAULTMUTEX; 2477c478bd9Sstevel@tonic-gate static eabuf_t ea_main; 2487c478bd9Sstevel@tonic-gate eabuf_t *eabuf = NULL; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate if (thr_main()) 2517c478bd9Sstevel@tonic-gate return (&ea_main); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate if (key_once == 0) { 2547c478bd9Sstevel@tonic-gate (void) mutex_lock(&tsd_lock); 2557c478bd9Sstevel@tonic-gate if (key_once == 0) { 2567c478bd9Sstevel@tonic-gate if (thr_keycreate(&key, free) != 0) { 2577c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tsd_lock); 2587c478bd9Sstevel@tonic-gate return (NULL); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate key_once = 1; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate (void) mutex_unlock(&tsd_lock); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate (void) thr_getspecific(key, (void **)&eabuf); 2657c478bd9Sstevel@tonic-gate if (eabuf == NULL) { 2667c478bd9Sstevel@tonic-gate eabuf = malloc(sizeof (eabuf_t)); 2677c478bd9Sstevel@tonic-gate (void) thr_setspecific(key, eabuf); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate return (eabuf); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* 2737c478bd9Sstevel@tonic-gate * Converts a 48 bit ethernet number to its string representation. 2747c478bd9Sstevel@tonic-gate */ 2757c478bd9Sstevel@tonic-gate char * 2767c478bd9Sstevel@tonic-gate ether_ntoa(const struct ether_addr *e) 2777c478bd9Sstevel@tonic-gate { 2787c478bd9Sstevel@tonic-gate eabuf_t *eabuf; 2797c478bd9Sstevel@tonic-gate char *s; 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 2827c478bd9Sstevel@tonic-gate return (NULL); 2837c478bd9Sstevel@tonic-gate s = eabuf->ea_string; 2847c478bd9Sstevel@tonic-gate (void) sprintf(s, "%x:%x:%x:%x:%x:%x", 2857c478bd9Sstevel@tonic-gate e->ether_addr_octet[0], e->ether_addr_octet[1], 2867c478bd9Sstevel@tonic-gate e->ether_addr_octet[2], e->ether_addr_octet[3], 2877c478bd9Sstevel@tonic-gate e->ether_addr_octet[4], e->ether_addr_octet[5]); 2887c478bd9Sstevel@tonic-gate return (s); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * Converts an ethernet address representation back into its 48 bits. 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate struct ether_addr * 2957c478bd9Sstevel@tonic-gate ether_aton(const char *s) 2967c478bd9Sstevel@tonic-gate { 2977c478bd9Sstevel@tonic-gate eabuf_t *eabuf; 2987c478bd9Sstevel@tonic-gate struct ether_addr *e; 2997c478bd9Sstevel@tonic-gate int i; 3007c478bd9Sstevel@tonic-gate uint_t t[6]; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 3037c478bd9Sstevel@tonic-gate return (NULL); 3047c478bd9Sstevel@tonic-gate e = &eabuf->ea_addr; 3057c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x", 3067c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]); 3077c478bd9Sstevel@tonic-gate if (i != 6) 3087c478bd9Sstevel@tonic-gate return (NULL); 3097c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) 3107c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 3117c478bd9Sstevel@tonic-gate return (e); 3127c478bd9Sstevel@tonic-gate } 313