xref: /openbsd-src/lib/libc/net/ethers.c (revision c044b6961acf719aabe3a2bcaa957d2fa5a2c166)
1 /*	$OpenBSD: ethers.c,v 1.7 1998/03/17 06:22:00 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.7 1998/03/17 06:22:00 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 		errno = EINVAL;
72 		return (NULL);
73 	}
74 
75 	(void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
76 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
77 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
78 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
79 
80 	return (a);
81 }
82 
83 static char *
84 _ether_aton(s, e)
85 	char *s;
86 	struct ether_addr *e;
87 {
88 	int i;
89 	long l;
90 	char *pp;
91 
92 	while (isspace(*s))
93 		s++;
94 
95 	/* expect 6 hex octets separated by ':' or space/NUL if last octet */
96 	for (i = 0; i < 6; i++) {
97 		l = strtol(s, &pp, 16);
98 		if (pp == s || l > 0xFF)
99 			return (NULL);
100 		if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0'))))
101 			return (NULL);
102 		e->ether_addr_octet[i] = (u_char)l;
103 		s = pp + 1;
104 	}
105 
106 	/* return character after the octets ala strtol(3) */
107 	return (pp);
108 }
109 
110 struct ether_addr *
111 ether_aton(s)
112 	char *s;
113 {
114 	static struct ether_addr n;
115 
116 	return (_ether_aton(s, &n) ? &n : NULL);
117 }
118 
119 int
120 ether_ntohost(hostname, e)
121 	char *hostname;
122 	struct ether_addr *e;
123 {
124 	FILE *f;
125 	char buf[BUFSIZ+1], *p;
126 	size_t len;
127 	struct ether_addr try;
128 
129 	if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
130 	    e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
131 	    e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
132 		errno = EINVAL;
133 		return (-1);
134 	}
135 
136 #ifdef YP
137 	char trybuf[sizeof("xx:xx:xx:xx:xx:xx")];
138 	int trylen;
139 
140 	sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
141 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
142 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
143 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
144 	trylen = strlen(trybuf);
145 #endif
146 
147 	f = fopen(_PATH_ETHERS, "r");
148 	if (f == NULL)
149 		return (-1);
150 	while ((p = fgetln(f, &len)) != NULL) {
151 		if (p[len-1] == '\n')
152 			len--;
153 		if (len > sizeof(buf) - 2)
154 			continue;
155 		(void)memcpy(buf, p, len);
156 		buf[len] = '\n';	/* code assumes newlines later on */
157 		buf[len+1] = '\0';
158 #ifdef YP
159 		/* A + in the file means try YP now.  */
160 		if (!strncmp(buf, "+\n", sizeof(buf))) {
161 			char *ypbuf, *ypdom;
162 			int ypbuflen;
163 
164 			if (yp_get_default_domain(&ypdom))
165 				continue;
166 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
167 			    trylen, &ypbuf, &ypbuflen))
168 				continue;
169 			if (ether_line(ypbuf, &try, hostname) == 0) {
170 				free(ypbuf);
171 				(void)fclose(f);
172 				return (0);
173 			}
174 			free(ypbuf);
175 			continue;
176 		}
177 #endif
178 		if (ether_line(buf, &try, hostname) == 0 &&
179 		    memcmp((void *)&try, (void *)e, sizeof(try)) == 0) {
180 			(void)fclose(f);
181 			return (0);
182 		}
183 	}
184 	(void)fclose(f);
185 	errno = ENOENT;
186 	return (-1);
187 }
188 
189 int
190 ether_hostton(hostname, e)
191 	char *hostname;
192 	struct ether_addr *e;
193 {
194 	FILE *f;
195 	char buf[BUFSIZ+1], *p;
196 	char try[MAXHOSTNAMELEN];
197 	size_t len;
198 #ifdef YP
199 	int hostlen = strlen(hostname);
200 #endif
201 
202 	f = fopen(_PATH_ETHERS, "r");
203 	if (f==NULL)
204 		return (-1);
205 
206 	while ((p = fgetln(f, &len)) != NULL) {
207 		if (p[len-1] == '\n')
208 			len--;
209 		if (len > sizeof(buf) - 2)
210 			continue;
211 		memcpy(buf, p, len);
212 		buf[len] = '\n';	/* code assumes newlines later on */
213 		buf[len+1] = '\0';
214 #ifdef YP
215 		/* A + in the file means try YP now.  */
216 		if (!strncmp(buf, "+\n", sizeof(buf))) {
217 			char *ypbuf, *ypdom;
218 			int ypbuflen;
219 
220 			if (yp_get_default_domain(&ypdom))
221 				continue;
222 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
223 			    &ypbuf, &ypbuflen))
224 				continue;
225 			if (ether_line(ypbuf, e, try) == 0) {
226 				free(ypbuf);
227 				(void)fclose(f);
228 				return (0);
229 			}
230 			free(ypbuf);
231 			continue;
232 		}
233 #endif
234 		if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
235 			(void)fclose(f);
236 			return (0);
237 		}
238 	}
239 	(void)fclose(f);
240 	errno = ENOENT;
241 	return (-1);
242 }
243 
244 int
245 ether_line(line, e, hostname)
246 	char *line;
247 	struct ether_addr *e;
248 	char *hostname;
249 {
250 	char *p;
251 	size_t n;
252 
253 	/* Parse "xx:xx:xx:xx:xx:xx" */
254 	if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
255 		goto bad;
256 
257 	/* Now get the hostname */
258 	while (isspace(*p))
259 		p++;
260 	if (*p == '\0')
261 		goto bad;
262 	n = strcspn(p, " \t\n");
263 	if (n >= MAXHOSTNAMELEN)
264 		goto bad;
265 	(void)strncpy(hostname, p, n);
266 	hostname[n] = '\0';
267 	return (0);
268 
269 bad:
270 	errno = EINVAL;
271 	return (-1);
272 }
273