1 /* $OpenBSD: ethers.c,v 1.6 1998/03/17 06:20:25 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.6 1998/03/17 06:20:25 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 errno = EINVAL; 131 return (-1); 132 } 133 134 #ifdef YP 135 char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; 136 int trylen; 137 138 sprintf(trybuf, "%x:%x:%x:%x:%x:%x", 139 e->ether_addr_octet[0], e->ether_addr_octet[1], 140 e->ether_addr_octet[2], e->ether_addr_octet[3], 141 e->ether_addr_octet[4], e->ether_addr_octet[5]); 142 trylen = strlen(trybuf); 143 #endif 144 145 f = fopen(_PATH_ETHERS, "r"); 146 if (f == NULL) 147 return (-1); 148 while ((p = fgetln(f, &len)) != NULL) { 149 if (p[len-1] == '\n') 150 len--; 151 if (len > sizeof(buf) - 2) 152 continue; 153 (void)memcpy(buf, p, len); 154 buf[len] = '\n'; /* code assumes newlines later on */ 155 buf[len+1] = '\0'; 156 #ifdef YP 157 /* A + in the file means try YP now. */ 158 if (!strncmp(buf, "+\n", sizeof(buf))) { 159 char *ypbuf, *ypdom; 160 int ypbuflen; 161 162 if (yp_get_default_domain(&ypdom)) 163 continue; 164 if (yp_match(ypdom, "ethers.byaddr", trybuf, 165 trylen, &ypbuf, &ypbuflen)) 166 continue; 167 if (ether_line(ypbuf, &try, hostname) == 0) { 168 free(ypbuf); 169 (void)fclose(f); 170 return (0); 171 } 172 free(ypbuf); 173 continue; 174 } 175 #endif 176 if (ether_line(buf, &try, hostname) == 0 && 177 memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { 178 (void)fclose(f); 179 return (0); 180 } 181 } 182 (void)fclose(f); 183 errno = ENOENT; 184 return (-1); 185 } 186 187 int 188 ether_hostton(hostname, e) 189 char *hostname; 190 struct ether_addr *e; 191 { 192 FILE *f; 193 char buf[BUFSIZ+1], *p; 194 char try[MAXHOSTNAMELEN]; 195 size_t len; 196 #ifdef YP 197 int hostlen = strlen(hostname); 198 #endif 199 200 f = fopen(_PATH_ETHERS, "r"); 201 if (f==NULL) 202 return (-1); 203 204 while ((p = fgetln(f, &len)) != NULL) { 205 if (p[len-1] == '\n') 206 len--; 207 if (len > sizeof(buf) - 2) 208 continue; 209 memcpy(buf, p, len); 210 buf[len] = '\n'; /* code assumes newlines later on */ 211 buf[len+1] = '\0'; 212 #ifdef YP 213 /* A + in the file means try YP now. */ 214 if (!strncmp(buf, "+\n", sizeof(buf))) { 215 char *ypbuf, *ypdom; 216 int ypbuflen; 217 218 if (yp_get_default_domain(&ypdom)) 219 continue; 220 if (yp_match(ypdom, "ethers.byname", hostname, hostlen, 221 &ypbuf, &ypbuflen)) 222 continue; 223 if (ether_line(ypbuf, e, try) == 0) { 224 free(ypbuf); 225 (void)fclose(f); 226 return (0); 227 } 228 free(ypbuf); 229 continue; 230 } 231 #endif 232 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { 233 (void)fclose(f); 234 return (0); 235 } 236 } 237 (void)fclose(f); 238 errno = ENOENT; 239 return (-1); 240 } 241 242 int 243 ether_line(line, e, hostname) 244 char *line; 245 struct ether_addr *e; 246 char *hostname; 247 { 248 char *p; 249 size_t n; 250 251 /* Parse "xx:xx:xx:xx:xx:xx" */ 252 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) 253 goto bad; 254 255 /* Now get the hostname */ 256 while (isspace(*p)) 257 p++; 258 if (*p == '\0') 259 goto bad; 260 n = strcspn(p, " \t\n"); 261 if (n >= MAXHOSTNAMELEN) 262 goto bad; 263 (void)strncpy(hostname, p, n); 264 hostname[n] = '\0'; 265 return (0); 266 267 bad: 268 errno = EINVAL; 269 return (-1); 270 } 271