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*3864Sraf * Copyright 2007 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 501219Sraf #include "c_synonyms.h" 510Sstevel@tonic-gate #include <stdio.h> 520Sstevel@tonic-gate #include <ctype.h> 530Sstevel@tonic-gate #include <string.h> 540Sstevel@tonic-gate #include <stdlib.h> 550Sstevel@tonic-gate #include <sys/types.h> 560Sstevel@tonic-gate #include <thread.h> 57*3864Sraf #include <pthread.h> 580Sstevel@tonic-gate #include <sys/socket.h> 590Sstevel@tonic-gate #include <net/if.h> 600Sstevel@tonic-gate #include <netinet/in.h> 610Sstevel@tonic-gate #include <netinet/if_ether.h> 620Sstevel@tonic-gate #include <nss_dbdefs.h> 630Sstevel@tonic-gate 642830Sdjl int str2ether(const char *, int, void *, char *, int); 650Sstevel@tonic-gate 660Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 670Sstevel@tonic-gate 682830Sdjl void 690Sstevel@tonic-gate _nss_initf_ethers(nss_db_params_t *p) 700Sstevel@tonic-gate { 710Sstevel@tonic-gate p->name = NSS_DBNAM_ETHERS; 720Sstevel@tonic-gate p->default_config = NSS_DEFCONF_ETHERS; 730Sstevel@tonic-gate } 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* 760Sstevel@tonic-gate * Given a host's name, this routine finds the corresponding 48 bit 770Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 780Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate int 810Sstevel@tonic-gate ether_hostton( 820Sstevel@tonic-gate const char *host, /* function input */ 830Sstevel@tonic-gate struct ether_addr *e /* function output */ 840Sstevel@tonic-gate ) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate nss_XbyY_args_t arg; 870Sstevel@tonic-gate nss_status_t res; 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether); 930Sstevel@tonic-gate arg.key.name = host; 940Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 950Sstevel@tonic-gate NSS_DBOP_ETHERS_HOSTTON, &arg); 960Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 970Sstevel@tonic-gate return (arg.status = res); 980Sstevel@tonic-gate } 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* 1010Sstevel@tonic-gate * Given a 48 bit ethernet address, it finds the corresponding hostname 1020Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf. 1030Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1040Sstevel@tonic-gate */ 1050Sstevel@tonic-gate int 1060Sstevel@tonic-gate ether_ntohost( 1070Sstevel@tonic-gate char *host, /* function output */ 1080Sstevel@tonic-gate const struct ether_addr *e /* function input */ 1090Sstevel@tonic-gate ) 1100Sstevel@tonic-gate { 1110Sstevel@tonic-gate nss_XbyY_args_t arg; 1120Sstevel@tonic-gate nss_status_t res; 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing. 1160Sstevel@tonic-gate */ 1170Sstevel@tonic-gate NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether); 1180Sstevel@tonic-gate arg.key.ether = (void *)e; 1190Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers, 1200Sstevel@tonic-gate NSS_DBOP_ETHERS_NTOHOST, &arg); 1210Sstevel@tonic-gate /* memcpy(host, ether_res.host, strlen(ether_res.host)); */ 1220Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg); 1230Sstevel@tonic-gate return (arg.status = res); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * Parses a line from "ethers" database into its components. The line has 1280Sstevel@tonic-gate * the form 8:0:20:1:17:c8 krypton 1290Sstevel@tonic-gate * where the first part is a 48 bit ethernet address and the second is 1300Sstevel@tonic-gate * the corresponding hosts name. 1310Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate int 1340Sstevel@tonic-gate ether_line( 1350Sstevel@tonic-gate const char *s, /* the string to be parsed */ 1360Sstevel@tonic-gate struct ether_addr *e, /* ethernet address struct to be filled in */ 1370Sstevel@tonic-gate char *hostname /* hosts name to be set */ 1380Sstevel@tonic-gate ) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate int i; 1410Sstevel@tonic-gate uint_t t[6]; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", 1440Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname); 1450Sstevel@tonic-gate if (i != 7) { 1460Sstevel@tonic-gate return (7 - i); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate for (i = 0; i < 6; i++) 1490Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 1500Sstevel@tonic-gate return (0); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* 1540Sstevel@tonic-gate * Parses a line from "ethers" database into its components. 1550Sstevel@tonic-gate * Useful for the vile purposes of the backends that 1560Sstevel@tonic-gate * expect a str2ether() format. 1570Sstevel@tonic-gate * 1580Sstevel@tonic-gate * This function, after parsing the instr line, will 1590Sstevel@tonic-gate * place the resulting struct ether_addr in b->buf.result only if 1600Sstevel@tonic-gate * b->buf.result is initialized (not NULL). I.e. it always happens 1610Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1620Sstevel@tonic-gate * then do a match for the ether key) and happens for "nis" 1630Sstevel@tonic-gate * backend only if the call was ether_hostton. 1640Sstevel@tonic-gate * 1650Sstevel@tonic-gate * Also, it will place the resulting hostname into b->buf.buffer 1660Sstevel@tonic-gate * only if b->buf.buffer is initialized. I.e. it always happens 1670Sstevel@tonic-gate * for "files" backend (that needs to parse input line and 1680Sstevel@tonic-gate * then do a match for the host key) and happens for "nis" 1690Sstevel@tonic-gate * backend only if the call was ether_ntohost. 1700Sstevel@tonic-gate * 1710Sstevel@tonic-gate * Cannot use the sscanf() technique for parsing because instr 1720Sstevel@tonic-gate * is a read-only, not necessarily null-terminated, buffer. 1730Sstevel@tonic-gate * 1740Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 1750Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 1760Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 1770Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 1780Sstevel@tonic-gate */ 1790Sstevel@tonic-gate #define DIGIT(x) (isdigit(x) ? (x) - '0' : \ 1800Sstevel@tonic-gate islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 1810Sstevel@tonic-gate #define lisalnum(x) (isdigit(x) || \ 1820Sstevel@tonic-gate ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z')) 1830Sstevel@tonic-gate /* ARGSUSED */ 1842830Sdjl int 1850Sstevel@tonic-gate str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate uchar_t *ether = (uchar_t *)ent; 1880Sstevel@tonic-gate char *host = buffer; 1890Sstevel@tonic-gate const char *p, *limit, *start; 1900Sstevel@tonic-gate ptrdiff_t i; 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate p = instr; 1930Sstevel@tonic-gate limit = p + lenstr; 1940Sstevel@tonic-gate 1950Sstevel@tonic-gate /* skip beginning whitespace, if any */ 1960Sstevel@tonic-gate while (p < limit && isspace(*p)) 1970Sstevel@tonic-gate p++; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate if (ether) { /* parse ether */ 2000Sstevel@tonic-gate for (i = 0; i < 6; i++) { 2010Sstevel@tonic-gate int j = 0, n = 0; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate start = p; 2040Sstevel@tonic-gate while (p < limit && lisalnum(start[j])) { 2050Sstevel@tonic-gate /* don't worry about overflow here */ 2060Sstevel@tonic-gate n = 16 * n + DIGIT(start[j]); 2070Sstevel@tonic-gate j++; 2080Sstevel@tonic-gate p++; 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate if (*p != ':' && i < 5) { 2110Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 2120Sstevel@tonic-gate } else { 2130Sstevel@tonic-gate p++; 2140Sstevel@tonic-gate *(ether + i) = (uchar_t)n; 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate } else { /* skip ether */ 2180Sstevel@tonic-gate while (p < limit && !isspace(*p)) 2190Sstevel@tonic-gate p++; 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate if (host) { /* parse host */ 2220Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip whitespace */ 2230Sstevel@tonic-gate p++; 2240Sstevel@tonic-gate start = p; 2250Sstevel@tonic-gate while (p < limit && !isspace(*p)) /* skip hostname */ 2260Sstevel@tonic-gate p++; 2270Sstevel@tonic-gate if ((i = (p - start)) < MAXHOSTNAMELEN) { 2280Sstevel@tonic-gate (void) memcpy(host, start, i); 2290Sstevel@tonic-gate host[i] = '\0'; 2300Sstevel@tonic-gate } else 2310Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); /* failure */ 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate typedef struct { 2370Sstevel@tonic-gate char ea_string[18]; 2380Sstevel@tonic-gate struct ether_addr ea_addr; 2390Sstevel@tonic-gate } eabuf_t; 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate static eabuf_t * 2420Sstevel@tonic-gate ea_buf(void) 2430Sstevel@tonic-gate { 244*3864Sraf static thread_key_t key = THR_ONCE_KEY; 2450Sstevel@tonic-gate static eabuf_t ea_main; 246*3864Sraf eabuf_t *eabuf; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if (thr_main()) 2490Sstevel@tonic-gate return (&ea_main); 2500Sstevel@tonic-gate 251*3864Sraf if (thr_keycreate_once(&key, free) != 0) 252*3864Sraf return (NULL); 253*3864Sraf eabuf = pthread_getspecific(key); 2540Sstevel@tonic-gate if (eabuf == NULL) { 2550Sstevel@tonic-gate eabuf = malloc(sizeof (eabuf_t)); 2560Sstevel@tonic-gate (void) thr_setspecific(key, eabuf); 2570Sstevel@tonic-gate } 2580Sstevel@tonic-gate return (eabuf); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* 2620Sstevel@tonic-gate * Converts a 48 bit ethernet number to its string representation. 2630Sstevel@tonic-gate */ 2640Sstevel@tonic-gate char * 2650Sstevel@tonic-gate ether_ntoa(const struct ether_addr *e) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate eabuf_t *eabuf; 2680Sstevel@tonic-gate char *s; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 2710Sstevel@tonic-gate return (NULL); 2720Sstevel@tonic-gate s = eabuf->ea_string; 2730Sstevel@tonic-gate (void) sprintf(s, "%x:%x:%x:%x:%x:%x", 2740Sstevel@tonic-gate e->ether_addr_octet[0], e->ether_addr_octet[1], 2750Sstevel@tonic-gate e->ether_addr_octet[2], e->ether_addr_octet[3], 2760Sstevel@tonic-gate e->ether_addr_octet[4], e->ether_addr_octet[5]); 2770Sstevel@tonic-gate return (s); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* 2810Sstevel@tonic-gate * Converts an ethernet address representation back into its 48 bits. 2820Sstevel@tonic-gate */ 2830Sstevel@tonic-gate struct ether_addr * 2840Sstevel@tonic-gate ether_aton(const char *s) 2850Sstevel@tonic-gate { 2860Sstevel@tonic-gate eabuf_t *eabuf; 2870Sstevel@tonic-gate struct ether_addr *e; 2880Sstevel@tonic-gate int i; 2890Sstevel@tonic-gate uint_t t[6]; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL) 2920Sstevel@tonic-gate return (NULL); 2930Sstevel@tonic-gate e = &eabuf->ea_addr; 2940Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x", 2950Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]); 2960Sstevel@tonic-gate if (i != 6) 2970Sstevel@tonic-gate return (NULL); 2980Sstevel@tonic-gate for (i = 0; i < 6; i++) 2990Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i]; 3000Sstevel@tonic-gate return (e); 3010Sstevel@tonic-gate } 302