1 /* $OpenBSD: ethers.c,v 1.5 1998/03/17 06:16:55 millert Exp $ */ 2 3 /* 4 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Todd C. Miller. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 23 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 24 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * ethers(3) a la Sun. 35 * Originally Written by Roland McGrath <roland@frob.com> 10/14/93. 36 * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com> 37 */ 38 39 #if defined(LIBC_SCCS) && !defined(lint) 40 static char rcsid[] = "$OpenBSD: ethers.c,v 1.5 1998/03/17 06:16:55 millert Exp $"; 41 #endif /* LIBC_SCCS and not lint */ 42 43 #include <sys/types.h> 44 #include <sys/socket.h> 45 #include <net/if.h> 46 #include <netinet/in.h> 47 #include <netinet/if_ether.h> 48 #include <sys/param.h> 49 #include <paths.h> 50 #include <errno.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <ctype.h> 55 56 #ifndef _PATH_ETHERS 57 #define _PATH_ETHERS "/etc/ethers" 58 #endif 59 60 static char * _ether_aton __P((char *, struct ether_addr *)); 61 62 char * 63 ether_ntoa(e) 64 struct ether_addr *e; 65 { 66 static char a[] = "xx:xx:xx:xx:xx:xx"; 67 68 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || 69 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || 70 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) 71 return (NULL); 72 73 (void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", 74 e->ether_addr_octet[0], e->ether_addr_octet[1], 75 e->ether_addr_octet[2], e->ether_addr_octet[3], 76 e->ether_addr_octet[4], e->ether_addr_octet[5]); 77 78 return (a); 79 } 80 81 static char * 82 _ether_aton(s, e) 83 char *s; 84 struct ether_addr *e; 85 { 86 int i; 87 long l; 88 char *pp; 89 90 while (isspace(*s)) 91 s++; 92 93 /* expect 6 hex octets separated by ':' or space/NUL if last octet */ 94 for (i = 0; i < 6; i++) { 95 l = strtol(s, &pp, 16); 96 if (pp == s || l > 0xFF) 97 return (NULL); 98 if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) 99 return (NULL); 100 e->ether_addr_octet[i] = (u_char)l; 101 s = pp + 1; 102 } 103 104 /* return character after the octets ala strtol(3) */ 105 return (pp); 106 } 107 108 struct ether_addr * 109 ether_aton(s) 110 char *s; 111 { 112 static struct ether_addr n; 113 114 return (_ether_aton(s, &n) ? &n : NULL); 115 } 116 117 int 118 ether_ntohost(hostname, e) 119 char *hostname; 120 struct ether_addr *e; 121 { 122 FILE *f; 123 char buf[BUFSIZ+1], *p; 124 size_t len; 125 struct ether_addr try; 126 127 if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || 128 e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || 129 e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) 130 return (NULL); 131 132 #ifdef YP 133 char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; 134 int trylen; 135 136 sprintf(trybuf, "%x:%x:%x:%x:%x:%x", 137 e->ether_addr_octet[0], e->ether_addr_octet[1], 138 e->ether_addr_octet[2], e->ether_addr_octet[3], 139 e->ether_addr_octet[4], e->ether_addr_octet[5]); 140 trylen = strlen(trybuf); 141 #endif 142 143 f = fopen(_PATH_ETHERS, "r"); 144 if (f == NULL) 145 return (-1); 146 while ((p = fgetln(f, &len)) != NULL) { 147 if (p[len-1] == '\n') 148 len--; 149 if (len > sizeof(buf) - 2) 150 continue; 151 (void)memcpy(buf, p, len); 152 buf[len] = '\n'; /* code assumes newlines later on */ 153 buf[len+1] = '\0'; 154 #ifdef YP 155 /* A + in the file means try YP now. */ 156 if (!strncmp(buf, "+\n", sizeof(buf))) { 157 char *ypbuf, *ypdom; 158 int ypbuflen; 159 160 if (yp_get_default_domain(&ypdom)) 161 continue; 162 if (yp_match(ypdom, "ethers.byaddr", trybuf, 163 trylen, &ypbuf, &ypbuflen)) 164 continue; 165 if (ether_line(ypbuf, &try, hostname) == 0) { 166 free(ypbuf); 167 (void)fclose(f); 168 return (0); 169 } 170 free(ypbuf); 171 continue; 172 } 173 #endif 174 if (ether_line(buf, &try, hostname) == 0 && 175 memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { 176 (void)fclose(f); 177 return (0); 178 } 179 } 180 (void)fclose(f); 181 errno = ENOENT; 182 return (-1); 183 } 184 185 int 186 ether_hostton(hostname, e) 187 char *hostname; 188 struct ether_addr *e; 189 { 190 FILE *f; 191 char buf[BUFSIZ+1], *p; 192 char try[MAXHOSTNAMELEN]; 193 size_t len; 194 #ifdef YP 195 int hostlen = strlen(hostname); 196 #endif 197 198 f = fopen(_PATH_ETHERS, "r"); 199 if (f==NULL) 200 return (-1); 201 202 while ((p = fgetln(f, &len)) != NULL) { 203 if (p[len-1] == '\n') 204 len--; 205 if (len > sizeof(buf) - 2) 206 continue; 207 memcpy(buf, p, len); 208 buf[len] = '\n'; /* code assumes newlines later on */ 209 buf[len+1] = '\0'; 210 #ifdef YP 211 /* A + in the file means try YP now. */ 212 if (!strncmp(buf, "+\n", sizeof(buf))) { 213 char *ypbuf, *ypdom; 214 int ypbuflen; 215 216 if (yp_get_default_domain(&ypdom)) 217 continue; 218 if (yp_match(ypdom, "ethers.byname", hostname, hostlen, 219 &ypbuf, &ypbuflen)) 220 continue; 221 if (ether_line(ypbuf, e, try) == 0) { 222 free(ypbuf); 223 (void)fclose(f); 224 return (0); 225 } 226 free(ypbuf); 227 continue; 228 } 229 #endif 230 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { 231 (void)fclose(f); 232 return (0); 233 } 234 } 235 (void)fclose(f); 236 errno = ENOENT; 237 return (-1); 238 } 239 240 int 241 ether_line(line, e, hostname) 242 char *line; 243 struct ether_addr *e; 244 char *hostname; 245 { 246 char *p; 247 size_t n; 248 249 /* Parse "xx:xx:xx:xx:xx:xx" */ 250 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) 251 goto bad; 252 253 /* Now get the hostname */ 254 while (isspace(*p)) 255 p++; 256 if (*p == '\0') 257 goto bad; 258 n = strcspn(p, " \t\n"); 259 if (n >= MAXHOSTNAMELEN) 260 goto bad; 261 (void)strncpy(hostname, p, n); 262 hostname[n] = '\0'; 263 return (0); 264 265 bad: 266 errno = EINVAL; 267 return (-1); 268 } 269