10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52830Sdjl * Common Development and Distribution License (the "License"). 62830Sdjl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211219Sraf 220Sstevel@tonic-gate /* 23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 320Sstevel@tonic-gate * The Regents of the University of California 330Sstevel@tonic-gate * All Rights Reserved 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 370Sstevel@tonic-gate * contributors. 380Sstevel@tonic-gate */ 390Sstevel@tonic-gate 400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 410Sstevel@tonic-gate 420Sstevel@tonic-gate /* 430Sstevel@tonic-gate * All routines necessary to deal the "ethers" database. The sources 440Sstevel@tonic-gate * contain mappings between 48 bit ethernet addresses and corresponding 450Sstevel@tonic-gate * hosts names. The addresses have an ascii representation of the form 460Sstevel@tonic-gate * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the 470Sstevel@tonic-gate * bytes are always in network order. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <stdio.h> 510Sstevel@tonic-gate #include <ctype.h> 520Sstevel@tonic-gate #include <string.h> 530Sstevel@tonic-gate #include <stdlib.h> 540Sstevel@tonic-gate #include <sys/types.h> 550Sstevel@tonic-gate #include <thread.h> 563864Sraf #include <pthread.h> 570Sstevel@tonic-gate #include <sys/socket.h> 580Sstevel@tonic-gate #include <net/if.h> 590Sstevel@tonic-gate #include <netinet/in.h> 600Sstevel@tonic-gate #include <netinet/if_ether.h> 610Sstevel@tonic-gate #include <nss_dbdefs.h> 620Sstevel@tonic-gate 632830Sdjl int str2ether(const char *, int, void *, char *, int); 640Sstevel@tonic-gate 650Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 660Sstevel@tonic-gate 672830Sdjl void 680Sstevel@tonic-gate _nss_initf_ethers(nss_db_params_t *p) 690Sstevel@tonic-gate { 700Sstevel@tonic-gate p->name = NSS_DBNAM_ETHERS; 710Sstevel@tonic-gate p->default_config = NSS_DEFCONF_ETHERS; 720Sstevel@tonic-gate } 730Sstevel@tonic-gate 740Sstevel@tonic-gate /* 750Sstevel@tonic-gate * Given a host's name, this routine finds the corresponding 48 bit 760Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 770Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate int 800Sstevel@tonic-gate ether_hostton( 810Sstevel@tonic-gate const char *host, /* function input */ 820Sstevel@tonic-gate struct ether_addr *e /* function output */ 830Sstevel@tonic-gate ) 840Sstevel@tonic-gate { 850Sstevel@tonic-gate nss_XbyY_args_t arg; 860Sstevel@tonic-gate nss_status_t res; 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 900Sstevel@tonic-gate */ 910Sstevel@tonic-gate NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether); 920Sstevel@tonic-gate arg.key.name = host; 930Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 94*6812Sraf NSS_DBOP_ETHERS_HOSTTON, &arg); 950Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 960Sstevel@tonic-gate return (arg.status = res); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * Given a 48 bit ethernet address, it finds the corresponding hostname 1010Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 1020Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1030Sstevel@tonic-gate */ 1040Sstevel@tonic-gate int 1050Sstevel@tonic-gate ether_ntohost( 1060Sstevel@tonic-gate char *host, /* function output */ 1070Sstevel@tonic-gate const struct ether_addr *e /* function input */ 1080Sstevel@tonic-gate ) 1090Sstevel@tonic-gate { 1100Sstevel@tonic-gate nss_XbyY_args_t arg; 1110Sstevel@tonic-gate nss_status_t res; 1120Sstevel@tonic-gate 1130Sstevel@tonic-gate /* 1140Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 1150Sstevel@tonic-gate */ 1160Sstevel@tonic-gate NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether); 1170Sstevel@tonic-gate arg.key.ether = (void *)e; 1180Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 119*6812Sraf NSS_DBOP_ETHERS_NTOHOST, &arg); 1200Sstevel@tonic-gate /* memcpy(host, ether_res.host, strlen(ether_res.host)); */ 1210Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 1220Sstevel@tonic-gate return (arg.status = res); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate /* 1260Sstevel@tonic-gate * Parses a line from "ethers" database into its components. The line has 1270Sstevel@tonic-gate * the form 8:0:20:1:17:c8 krypton 1280Sstevel@tonic-gate * where the first part is a 48 bit ethernet address and the second is 1290Sstevel@tonic-gate * the corresponding hosts name. 1300Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1310Sstevel@tonic-gate */ 1320Sstevel@tonic-gate int 1330Sstevel@tonic-gate ether_line( 1340Sstevel@tonic-gate const char *s, /* the string to be parsed */ 1350Sstevel@tonic-gate struct ether_addr *e, /* ethernet address struct to be filled in */ 1360Sstevel@tonic-gate char *hostname /* hosts name to be set */ 1370Sstevel@tonic-gate ) 1380Sstevel@tonic-gate { 1390Sstevel@tonic-gate int i; 1400Sstevel@tonic-gate uint_t t[6]; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", 1430Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname); 1440Sstevel@tonic-gate if (i != 7) { 1450Sstevel@tonic-gate return (7 - i); 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate for (i = 0; i < 6; i++) 1480Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 1490Sstevel@tonic-gate return (0); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate /* 1530Sstevel@tonic-gate * Parses a line from "ethers" database into its components. 1540Sstevel@tonic-gate * Useful for the vile purposes of the backends that 1550Sstevel@tonic-gate * expect a str2ether() format. 1560Sstevel@tonic-gate * 1570Sstevel@tonic-gate * This function, after parsing the instr line, will 1580Sstevel@tonic-gate * place the resulting struct ether_addr in b->buf.result only if 1590Sstevel@tonic-gate * b->buf.result is initialized (not NULL). I.e. it always happens 1600Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1610Sstevel@tonic-gate * then do a match for the ether key) and happens for "nis" 1620Sstevel@tonic-gate * backend only if the call was ether_hostton. 1630Sstevel@tonic-gate * 1640Sstevel@tonic-gate * Also, it will place the resulting hostname into b->buf.buffer 1650Sstevel@tonic-gate * only if b->buf.buffer is initialized. I.e. it always happens 1660Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1670Sstevel@tonic-gate * then do a match for the host key) and happens for "nis" 1680Sstevel@tonic-gate * backend only if the call was ether_ntohost. 1690Sstevel@tonic-gate * 1700Sstevel@tonic-gate * Cannot use the sscanf() technique for parsing because instr 1710Sstevel@tonic-gate * is a read-only, not necessarily null-terminated, buffer. 1720Sstevel@tonic-gate * 1730Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 1740Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 1750Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 1760Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 1790Sstevel@tonic-gate islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 1800Sstevel@tonic-gate #define lisalnum(x) (isdigit(x) || \ 1810Sstevel@tonic-gate ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 1820Sstevel@tonic-gate /* ARGSUSED */ 1832830Sdjl int 1840Sstevel@tonic-gate str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 1850Sstevel@tonic-gate { 1860Sstevel@tonic-gate uchar_t *ether = (uchar_t *)ent; 1870Sstevel@tonic-gate char *host = buffer; 1880Sstevel@tonic-gate const char *p, *limit, *start; 1890Sstevel@tonic-gate ptrdiff_t i; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate p = instr; 1920Sstevel@tonic-gate limit = p + lenstr; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* skip beginning whitespace, if any */ 1950Sstevel@tonic-gate while (p < limit && isspace(*p)) 1960Sstevel@tonic-gate p++; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (ether) { /* parse ether */ 1990Sstevel@tonic-gate for (i = 0; i < 6; i++) { 2000Sstevel@tonic-gate int j = 0, n = 0; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate start = p; 2030Sstevel@tonic-gate while (p < limit && lisalnum(start[j])) { 2040Sstevel@tonic-gate /* don't worry about overflow here */ 2050Sstevel@tonic-gate n = 16 * n + DIGIT(start[j]); 2060Sstevel@tonic-gate j++; 2070Sstevel@tonic-gate p++; 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate if (*p != ':' && i < 5) { 2100Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2110Sstevel@tonic-gate } else { 2120Sstevel@tonic-gate p++; 2130Sstevel@tonic-gate *(ether + i) = (uchar_t)n; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate } else { /* skip ether */ 2170Sstevel@tonic-gate while (p < limit && !isspace(*p)) 2180Sstevel@tonic-gate p++; 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate if (host) { /* parse host */ 2210Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip whitespace */ 2220Sstevel@tonic-gate p++; 2230Sstevel@tonic-gate start = p; 2240Sstevel@tonic-gate while (p < limit && !isspace(*p)) /* skip hostname */ 2250Sstevel@tonic-gate p++; 2260Sstevel@tonic-gate if ((i = (p - start)) < MAXHOSTNAMELEN) { 2270Sstevel@tonic-gate (void) memcpy(host, start, i); 2280Sstevel@tonic-gate host[i] = '\0'; 2290Sstevel@tonic-gate } else 2300Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); /* failure */ 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate typedef struct { 2360Sstevel@tonic-gate char ea_string[18]; 2370Sstevel@tonic-gate struct ether_addr ea_addr; 2380Sstevel@tonic-gate } eabuf_t; 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate static eabuf_t * 2410Sstevel@tonic-gate ea_buf(void) 2420Sstevel@tonic-gate { 2433864Sraf static thread_key_t key = THR_ONCE_KEY; 2440Sstevel@tonic-gate static eabuf_t ea_main; 2453864Sraf eabuf_t *eabuf; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate if (thr_main()) 2480Sstevel@tonic-gate return (&ea_main); 2490Sstevel@tonic-gate 2503864Sraf if (thr_keycreate_once(&key, free) != 0) 2513864Sraf return (NULL); 2523864Sraf eabuf = pthread_getspecific(key); 2530Sstevel@tonic-gate if (eabuf == NULL) { 2540Sstevel@tonic-gate eabuf = malloc(sizeof (eabuf_t)); 2550Sstevel@tonic-gate (void) thr_setspecific(key, eabuf); 2560Sstevel@tonic-gate } 2570Sstevel@tonic-gate return (eabuf); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate /* 2610Sstevel@tonic-gate * Converts a 48 bit ethernet number to its string representation. 2620Sstevel@tonic-gate */ 2630Sstevel@tonic-gate char * 2640Sstevel@tonic-gate ether_ntoa(const struct ether_addr *e) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate eabuf_t *eabuf; 2670Sstevel@tonic-gate char *s; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 2700Sstevel@tonic-gate return (NULL); 2710Sstevel@tonic-gate s = eabuf->ea_string; 2720Sstevel@tonic-gate (void) sprintf(s, "%x:%x:%x:%x:%x:%x", 2730Sstevel@tonic-gate e->ether_addr_octet[0], e->ether_addr_octet[1], 2740Sstevel@tonic-gate e->ether_addr_octet[2], e->ether_addr_octet[3], 2750Sstevel@tonic-gate e->ether_addr_octet[4], e->ether_addr_octet[5]); 2760Sstevel@tonic-gate return (s); 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* 2800Sstevel@tonic-gate * Converts an ethernet address representation back into its 48 bits. 2810Sstevel@tonic-gate */ 2820Sstevel@tonic-gate struct ether_addr * 2830Sstevel@tonic-gate ether_aton(const char *s) 2840Sstevel@tonic-gate { 2850Sstevel@tonic-gate eabuf_t *eabuf; 2860Sstevel@tonic-gate struct ether_addr *e; 2870Sstevel@tonic-gate int i; 2880Sstevel@tonic-gate uint_t t[6]; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 2910Sstevel@tonic-gate return (NULL); 2920Sstevel@tonic-gate e = &eabuf->ea_addr; 2930Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x", 2940Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]); 2950Sstevel@tonic-gate if (i != 6) 296*6812Sraf return (NULL); 2970Sstevel@tonic-gate for (i = 0; i < 6; i++) 2980Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 2990Sstevel@tonic-gate return (e); 3000Sstevel@tonic-gate } 301