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
5cb5caa98Sdjl * Common Development and Distribution License (the "License").
6cb5caa98Sdjl * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21e8031f0aSraf
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*ff3aea39SRobert Mustacchi * Copyright (c) 2014, Joyent, Inc. All rights reserved.
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 /*
427c478bd9Sstevel@tonic-gate * All routines necessary to deal the "ethers" database. The sources
437c478bd9Sstevel@tonic-gate * contain mappings between 48 bit ethernet addresses and corresponding
447c478bd9Sstevel@tonic-gate * hosts names. The addresses have an ascii representation of the form
457c478bd9Sstevel@tonic-gate * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the
467c478bd9Sstevel@tonic-gate * bytes are always in network order.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #include <stdio.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <string.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <sys/types.h>
547c478bd9Sstevel@tonic-gate #include <thread.h>
55cb620785Sraf #include <pthread.h>
567c478bd9Sstevel@tonic-gate #include <sys/socket.h>
577c478bd9Sstevel@tonic-gate #include <net/if.h>
587c478bd9Sstevel@tonic-gate #include <netinet/in.h>
597c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
607c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
617c478bd9Sstevel@tonic-gate
62cb5caa98Sdjl int str2ether(const char *, int, void *, char *, int);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root);
657c478bd9Sstevel@tonic-gate
66cb5caa98Sdjl void
_nss_initf_ethers(nss_db_params_t * p)677c478bd9Sstevel@tonic-gate _nss_initf_ethers(nss_db_params_t *p)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_ETHERS;
707c478bd9Sstevel@tonic-gate p->default_config = NSS_DEFCONF_ETHERS;
717c478bd9Sstevel@tonic-gate }
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * Given a host's name, this routine finds the corresponding 48 bit
757c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
767c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate int
ether_hostton(const char * host,struct ether_addr * e)797c478bd9Sstevel@tonic-gate ether_hostton(
807c478bd9Sstevel@tonic-gate const char *host, /* function input */
817c478bd9Sstevel@tonic-gate struct ether_addr *e /* function output */
827c478bd9Sstevel@tonic-gate )
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg;
857c478bd9Sstevel@tonic-gate nss_status_t res;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether);
917c478bd9Sstevel@tonic-gate arg.key.name = host;
927c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers,
937257d1b4Sraf NSS_DBOP_ETHERS_HOSTTON, &arg);
947c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg);
957c478bd9Sstevel@tonic-gate return (arg.status = res);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * Given a 48 bit ethernet address, it finds the corresponding hostname
1007c478bd9Sstevel@tonic-gate * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
1017c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise.
1027c478bd9Sstevel@tonic-gate */
1037c478bd9Sstevel@tonic-gate int
ether_ntohost(char * host,const struct ether_addr * e)1047c478bd9Sstevel@tonic-gate ether_ntohost(
1057c478bd9Sstevel@tonic-gate char *host, /* function output */
1067c478bd9Sstevel@tonic-gate const struct ether_addr *e /* function input */
1077c478bd9Sstevel@tonic-gate )
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg;
1107c478bd9Sstevel@tonic-gate nss_status_t res;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate * let the backend do the allocation to store stuff for parsing.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether);
1167c478bd9Sstevel@tonic-gate arg.key.ether = (void *)e;
1177c478bd9Sstevel@tonic-gate res = nss_search(&db_root, _nss_initf_ethers,
1187257d1b4Sraf NSS_DBOP_ETHERS_NTOHOST, &arg);
1197c478bd9Sstevel@tonic-gate /* memcpy(host, ether_res.host, strlen(ether_res.host)); */
1207c478bd9Sstevel@tonic-gate (void) NSS_XbyY_FINI(&arg);
1217c478bd9Sstevel@tonic-gate return (arg.status = res);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components. The line has
1267c478bd9Sstevel@tonic-gate * the form 8:0:20:1:17:c8 krypton
1277c478bd9Sstevel@tonic-gate * where the first part is a 48 bit ethernet address and the second is
1287c478bd9Sstevel@tonic-gate * the corresponding hosts name.
1297c478bd9Sstevel@tonic-gate * Returns zero if successful, non-zero otherwise.
1307c478bd9Sstevel@tonic-gate */
1317c478bd9Sstevel@tonic-gate int
ether_line(const char * s,struct ether_addr * e,char * hostname)1327c478bd9Sstevel@tonic-gate ether_line(
1337c478bd9Sstevel@tonic-gate const char *s, /* the string to be parsed */
1347c478bd9Sstevel@tonic-gate struct ether_addr *e, /* ethernet address struct to be filled in */
1357c478bd9Sstevel@tonic-gate char *hostname /* hosts name to be set */
1367c478bd9Sstevel@tonic-gate )
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate int i;
1397c478bd9Sstevel@tonic-gate uint_t t[6];
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
1427c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
1437c478bd9Sstevel@tonic-gate if (i != 7) {
1447c478bd9Sstevel@tonic-gate return (7 - i);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++)
1477c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i];
1487c478bd9Sstevel@tonic-gate return (0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Parses a line from "ethers" database into its components.
1537c478bd9Sstevel@tonic-gate * Useful for the vile purposes of the backends that
1547c478bd9Sstevel@tonic-gate * expect a str2ether() format.
1557c478bd9Sstevel@tonic-gate *
1567c478bd9Sstevel@tonic-gate * This function, after parsing the instr line, will
1577c478bd9Sstevel@tonic-gate * place the resulting struct ether_addr in b->buf.result only if
1587c478bd9Sstevel@tonic-gate * b->buf.result is initialized (not NULL). I.e. it always happens
1597c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and
1607c478bd9Sstevel@tonic-gate * then do a match for the ether key) and happens for "nis"
1617c478bd9Sstevel@tonic-gate * backend only if the call was ether_hostton.
1627c478bd9Sstevel@tonic-gate *
1637c478bd9Sstevel@tonic-gate * Also, it will place the resulting hostname into b->buf.buffer
1647c478bd9Sstevel@tonic-gate * only if b->buf.buffer is initialized. I.e. it always happens
1657c478bd9Sstevel@tonic-gate * for "files" backend (that needs to parse input line and
1667c478bd9Sstevel@tonic-gate * then do a match for the host key) and happens for "nis"
1677c478bd9Sstevel@tonic-gate * backend only if the call was ether_ntohost.
1687c478bd9Sstevel@tonic-gate *
1697c478bd9Sstevel@tonic-gate * Cannot use the sscanf() technique for parsing because instr
1707c478bd9Sstevel@tonic-gate * is a read-only, not necessarily null-terminated, buffer.
1717c478bd9Sstevel@tonic-gate *
1727c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ...
1737c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space
1747c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if
1757c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas.
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate #define DIGIT(x) (isdigit(x) ? (x) - '0' : \
1787c478bd9Sstevel@tonic-gate islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
1797c478bd9Sstevel@tonic-gate #define lisalnum(x) (isdigit(x) || \
1807c478bd9Sstevel@tonic-gate ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
1817c478bd9Sstevel@tonic-gate /* ARGSUSED */
182cb5caa98Sdjl int
str2ether(const char * instr,int lenstr,void * ent,char * buffer,int buflen)1837c478bd9Sstevel@tonic-gate str2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate uchar_t *ether = (uchar_t *)ent;
1867c478bd9Sstevel@tonic-gate char *host = buffer;
1877c478bd9Sstevel@tonic-gate const char *p, *limit, *start;
1887c478bd9Sstevel@tonic-gate ptrdiff_t i;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate p = instr;
1917c478bd9Sstevel@tonic-gate limit = p + lenstr;
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /* skip beginning whitespace, if any */
1947c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p))
1957c478bd9Sstevel@tonic-gate p++;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate if (ether) { /* parse ether */
1987c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++) {
1997c478bd9Sstevel@tonic-gate int j = 0, n = 0;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate start = p;
2027c478bd9Sstevel@tonic-gate while (p < limit && lisalnum(start[j])) {
2037c478bd9Sstevel@tonic-gate /* don't worry about overflow here */
2047c478bd9Sstevel@tonic-gate n = 16 * n + DIGIT(start[j]);
2057c478bd9Sstevel@tonic-gate j++;
2067c478bd9Sstevel@tonic-gate p++;
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate if (*p != ':' && i < 5) {
2097c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE);
2107c478bd9Sstevel@tonic-gate } else {
2117c478bd9Sstevel@tonic-gate p++;
2127c478bd9Sstevel@tonic-gate *(ether + i) = (uchar_t)n;
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate } else { /* skip ether */
2167c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p))
2177c478bd9Sstevel@tonic-gate p++;
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate if (host) { /* parse host */
2207c478bd9Sstevel@tonic-gate while (p < limit && isspace(*p)) /* skip whitespace */
2217c478bd9Sstevel@tonic-gate p++;
2227c478bd9Sstevel@tonic-gate start = p;
2237c478bd9Sstevel@tonic-gate while (p < limit && !isspace(*p)) /* skip hostname */
2247c478bd9Sstevel@tonic-gate p++;
2257c478bd9Sstevel@tonic-gate if ((i = (p - start)) < MAXHOSTNAMELEN) {
2267c478bd9Sstevel@tonic-gate (void) memcpy(host, start, i);
2277c478bd9Sstevel@tonic-gate host[i] = '\0';
2287c478bd9Sstevel@tonic-gate } else
2297c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); /* failure */
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate typedef struct {
2357c478bd9Sstevel@tonic-gate char ea_string[18];
2367c478bd9Sstevel@tonic-gate struct ether_addr ea_addr;
2377c478bd9Sstevel@tonic-gate } eabuf_t;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate static eabuf_t *
ea_buf(void)2407c478bd9Sstevel@tonic-gate ea_buf(void)
2417c478bd9Sstevel@tonic-gate {
242cb620785Sraf static thread_key_t key = THR_ONCE_KEY;
2437c478bd9Sstevel@tonic-gate static eabuf_t ea_main;
244cb620785Sraf eabuf_t *eabuf;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate if (thr_main())
2477c478bd9Sstevel@tonic-gate return (&ea_main);
2487c478bd9Sstevel@tonic-gate
249cb620785Sraf if (thr_keycreate_once(&key, free) != 0)
250cb620785Sraf return (NULL);
251cb620785Sraf eabuf = pthread_getspecific(key);
2527c478bd9Sstevel@tonic-gate if (eabuf == NULL) {
2537c478bd9Sstevel@tonic-gate eabuf = malloc(sizeof (eabuf_t));
2547c478bd9Sstevel@tonic-gate (void) thr_setspecific(key, eabuf);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate return (eabuf);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate /*
260*ff3aea39SRobert Mustacchi * Converts a 48 bit ethernet number to its string representation using a user
261*ff3aea39SRobert Mustacchi * defined buffer.
262*ff3aea39SRobert Mustacchi */
263*ff3aea39SRobert Mustacchi char *
ether_ntoa_r(const struct ether_addr * e,char * buf)264*ff3aea39SRobert Mustacchi ether_ntoa_r(const struct ether_addr *e, char *buf)
265*ff3aea39SRobert Mustacchi {
266*ff3aea39SRobert Mustacchi (void) sprintf(buf, "%x:%x:%x:%x:%x:%x",
267*ff3aea39SRobert Mustacchi e->ether_addr_octet[0], e->ether_addr_octet[1],
268*ff3aea39SRobert Mustacchi e->ether_addr_octet[2], e->ether_addr_octet[3],
269*ff3aea39SRobert Mustacchi e->ether_addr_octet[4], e->ether_addr_octet[5]);
270*ff3aea39SRobert Mustacchi return (buf);
271*ff3aea39SRobert Mustacchi }
272*ff3aea39SRobert Mustacchi
273*ff3aea39SRobert Mustacchi /*
274*ff3aea39SRobert Mustacchi * Converts a 48 bit ethernet number to its string representation using a
275*ff3aea39SRobert Mustacchi * per-thread buffer.
2767c478bd9Sstevel@tonic-gate */
2777c478bd9Sstevel@tonic-gate char *
ether_ntoa(const struct ether_addr * e)2787c478bd9Sstevel@tonic-gate ether_ntoa(const struct ether_addr *e)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate eabuf_t *eabuf;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if ((eabuf = ea_buf()) == NULL)
2837c478bd9Sstevel@tonic-gate return (NULL);
284*ff3aea39SRobert Mustacchi return (ether_ntoa_r(e, eabuf->ea_string));
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate /*
288*ff3aea39SRobert Mustacchi * Converts an ethernet address representation back into its 48 bits using a
289*ff3aea39SRobert Mustacchi * user defined buffer.
2907c478bd9Sstevel@tonic-gate */
2917c478bd9Sstevel@tonic-gate struct ether_addr *
ether_aton_r(const char * s,struct ether_addr * e)292*ff3aea39SRobert Mustacchi ether_aton_r(const char *s, struct ether_addr *e)
2937c478bd9Sstevel@tonic-gate {
2947c478bd9Sstevel@tonic-gate int i;
2957c478bd9Sstevel@tonic-gate uint_t t[6];
2967c478bd9Sstevel@tonic-gate i = sscanf(s, " %x:%x:%x:%x:%x:%x",
2977c478bd9Sstevel@tonic-gate &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
2987c478bd9Sstevel@tonic-gate if (i != 6)
2997257d1b4Sraf return (NULL);
3007c478bd9Sstevel@tonic-gate for (i = 0; i < 6; i++)
3017c478bd9Sstevel@tonic-gate e->ether_addr_octet[i] = (uchar_t)t[i];
3027c478bd9Sstevel@tonic-gate return (e);
3037c478bd9Sstevel@tonic-gate }
304*ff3aea39SRobert Mustacchi
305*ff3aea39SRobert Mustacchi /*
306*ff3aea39SRobert Mustacchi * Converts an ethernet address representation back into its 48 bits using a
307*ff3aea39SRobert Mustacchi * per-thread buffer.
308*ff3aea39SRobert Mustacchi */
309*ff3aea39SRobert Mustacchi struct ether_addr *
ether_aton(const char * s)310*ff3aea39SRobert Mustacchi ether_aton(const char *s)
311*ff3aea39SRobert Mustacchi {
312*ff3aea39SRobert Mustacchi eabuf_t *eabuf;
313*ff3aea39SRobert Mustacchi
314*ff3aea39SRobert Mustacchi if ((eabuf = ea_buf()) == NULL)
315*ff3aea39SRobert Mustacchi return (NULL);
316*ff3aea39SRobert Mustacchi return (ether_aton_r(s, &eabuf->ea_addr));
317*ff3aea39SRobert Mustacchi }
318