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 5*2830Sdjl * Common Development and Distribution License (the "License"). 6*2830Sdjl * 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 */ 210Sstevel@tonic-gate /* 22*2830Sdjl * files/ether_addr.c -- "files" backend for nsswitch "ethers" database 230Sstevel@tonic-gate * 24*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 250Sstevel@tonic-gate * Use is subject to license terms. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * All routines necessary to deal with the file /etc/ethers. The file 320Sstevel@tonic-gate * contains mappings from 48 bit ethernet addresses to their corresponding 330Sstevel@tonic-gate * hosts names. The addresses have an ascii representation of the form 340Sstevel@tonic-gate * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the 350Sstevel@tonic-gate * bytes are always in network order. 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <sys/types.h> 390Sstevel@tonic-gate #include <sys/socket.h> 400Sstevel@tonic-gate #include <net/if.h> 410Sstevel@tonic-gate #include <netinet/in.h> 420Sstevel@tonic-gate #include <netinet/if_ether.h> 430Sstevel@tonic-gate #include <nss_dbdefs.h> 440Sstevel@tonic-gate #include "files_common.h" 450Sstevel@tonic-gate #include <strings.h> 46*2830Sdjl #include <ctype.h> 470Sstevel@tonic-gate 480Sstevel@tonic-gate #define _PATH_ETHERS "/etc/ethers" 49*2830Sdjl #define DIGIT(x) \ 50*2830Sdjl (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A') 510Sstevel@tonic-gate 520Sstevel@tonic-gate static int 53*2830Sdjl check_host(nss_XbyY_args_t *argp, const char *line, int linelen) 540Sstevel@tonic-gate { 55*2830Sdjl const char *limit, *linep, *keyp; 56*2830Sdjl linep = line; 57*2830Sdjl limit = line + linelen; 58*2830Sdjl 59*2830Sdjl /* skip leading spaces */ 60*2830Sdjl while (linep < limit && isspace(*linep)) 61*2830Sdjl linep++; 62*2830Sdjl /* skip mac address */ 63*2830Sdjl while (linep < limit && !isspace(*linep)) 64*2830Sdjl linep++; 65*2830Sdjl /* skip the delimiting spaces */ 66*2830Sdjl while (linep < limit && isspace(*linep)) 67*2830Sdjl linep++; 68*2830Sdjl if (linep == limit) 69*2830Sdjl return (0); 70*2830Sdjl 71*2830Sdjl /* compare the host name */ 72*2830Sdjl keyp = argp->key.name; 73*2830Sdjl while (*keyp != '\0' && linep < limit && *keyp == *linep) { 74*2830Sdjl keyp++; 75*2830Sdjl linep++; 76*2830Sdjl } 77*2830Sdjl return (*keyp == '\0' && linep == limit); 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate static nss_status_t 810Sstevel@tonic-gate getbyhost(be, a) 820Sstevel@tonic-gate files_backend_ptr_t be; 830Sstevel@tonic-gate void *a; 840Sstevel@tonic-gate { 850Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 860Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN]; 870Sstevel@tonic-gate nss_status_t res; 880Sstevel@tonic-gate 89*2830Sdjl /* 90*2830Sdjl * use the buffer passed in if result is to be returned 91*2830Sdjl * in /etc file format 92*2830Sdjl */ 93*2830Sdjl if (argp->buf.result != NULL) { 94*2830Sdjl argp->buf.buffer = hostname; 95*2830Sdjl argp->buf.buflen = MAXHOSTNAMELEN; 96*2830Sdjl } 970Sstevel@tonic-gate 980Sstevel@tonic-gate res = _nss_files_XY_all(be, argp, 0, argp->key.name, check_host); 990Sstevel@tonic-gate 100*2830Sdjl if (argp->buf.result != NULL) { 101*2830Sdjl argp->buf.buffer = NULL; 102*2830Sdjl argp->buf.buflen = 0; 103*2830Sdjl } 104*2830Sdjl 1050Sstevel@tonic-gate return (res); 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate static int 109*2830Sdjl check_ether(nss_XbyY_args_t *argp, const char *line, int linelen) 1100Sstevel@tonic-gate { 111*2830Sdjl 112*2830Sdjl const char *limit, *linep; 113*2830Sdjl uchar_t ether[6]; 114*2830Sdjl ptrdiff_t i; 115*2830Sdjl int n; 116*2830Sdjl 117*2830Sdjl linep = line; 118*2830Sdjl limit = line + linelen; 119*2830Sdjl 120*2830Sdjl /* skip leading spaces */ 121*2830Sdjl while (linep < limit && isspace(*linep)) 122*2830Sdjl linep++; 123*2830Sdjl 124*2830Sdjl for (i = 0; i < 6; i++) { 125*2830Sdjl n = 0; 126*2830Sdjl while (linep < limit && isxdigit(*linep)) { 127*2830Sdjl n = 16 * n + DIGIT(*linep); 128*2830Sdjl linep++; 129*2830Sdjl } 130*2830Sdjl if (*linep != ':' && i < 5) { 131*2830Sdjl return (0); 132*2830Sdjl } else if (*linep == ':' && i == 5) { 133*2830Sdjl return (0); 134*2830Sdjl } else { 135*2830Sdjl linep++; 136*2830Sdjl ether[i] = (uchar_t)n; 137*2830Sdjl } 138*2830Sdjl } 139*2830Sdjl return (ether_cmp((void *)ether, (void *)argp->key.ether) == 0); 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate static nss_status_t 1430Sstevel@tonic-gate getbyether(be, a) 1440Sstevel@tonic-gate files_backend_ptr_t be; 1450Sstevel@tonic-gate void *a; 1460Sstevel@tonic-gate { 1470Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a; 1480Sstevel@tonic-gate struct ether_addr etheraddr; 1490Sstevel@tonic-gate nss_status_t res; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate argp->buf.result = ðeraddr; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate res = _nss_files_XY_all(be, argp, 0, NULL, check_ether); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate argp->buf.result = NULL; 1560Sstevel@tonic-gate return (res); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static files_backend_op_t ethers_ops[] = { 1600Sstevel@tonic-gate _nss_files_destr, 1610Sstevel@tonic-gate getbyhost, 1620Sstevel@tonic-gate getbyether 1630Sstevel@tonic-gate }; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /*ARGSUSED*/ 1660Sstevel@tonic-gate nss_backend_t * 1670Sstevel@tonic-gate _nss_files_ethers_constr(dummy1, dummy2, dummy3) 1680Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3; 1690Sstevel@tonic-gate { 1700Sstevel@tonic-gate return (_nss_files_constr(ethers_ops, 1710Sstevel@tonic-gate sizeof (ethers_ops) / sizeof (ethers_ops[0]), 1720Sstevel@tonic-gate _PATH_ETHERS, 1730Sstevel@tonic-gate NSS_LINELEN_ETHERS, 1740Sstevel@tonic-gate NULL)); 1750Sstevel@tonic-gate } 176