xref: /openbsd-src/lib/libc/net/ethers.c (revision f1a075da3e4bb4f84a5bb83783c6b1943dc3da49)
1 /*	$OpenBSD: ethers.c,v 1.12 2001/06/27 00:58:54 lebel 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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * ethers(3) a la Sun.
32  * Originally Written by Roland McGrath <roland@frob.com> 10/14/93.
33  * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com>
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char rcsid[] = "$OpenBSD: ethers.c,v 1.12 2001/06/27 00:58:54 lebel Exp $";
38 #endif /* LIBC_SCCS and not lint */
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45 #include <sys/param.h>
46 #include <paths.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <ctype.h>
52 #ifdef YP
53 #include <rpcsvc/ypclnt.h>
54 #endif
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 || l < 0)
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 #ifdef YP
129 	char trybuf[sizeof("xx:xx:xx:xx:xx:xx")];
130 	int trylen;
131 #endif
132 
133 	if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF ||
134 	    e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF ||
135 	    e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) {
136 		errno = EINVAL;
137 		return (-1);
138 	}
139 
140 #ifdef YP
141 	sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
142 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
143 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
144 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
145 	trylen = strlen(trybuf);
146 #endif
147 
148 	f = fopen(_PATH_ETHERS, "r");
149 	if (f == NULL)
150 		return (-1);
151 	while ((p = fgetln(f, &len)) != NULL) {
152 		if (p[len-1] == '\n')
153 			len--;
154 		if (len > sizeof(buf) - 2)
155 			continue;
156 		(void)memcpy(buf, p, len);
157 		buf[len] = '\n';	/* code assumes newlines later on */
158 		buf[len+1] = '\0';
159 #ifdef YP
160 		/* A + in the file means try YP now.  */
161 		if (!strncmp(buf, "+\n", sizeof(buf))) {
162 			char *ypbuf, *ypdom;
163 			int ypbuflen;
164 
165 			if (yp_get_default_domain(&ypdom))
166 				continue;
167 			if (yp_match(ypdom, "ethers.byaddr", trybuf,
168 			    trylen, &ypbuf, &ypbuflen))
169 				continue;
170 			if (ether_line(ypbuf, &try, hostname) == 0) {
171 				free(ypbuf);
172 				(void)fclose(f);
173 				return (0);
174 			}
175 			free(ypbuf);
176 			continue;
177 		}
178 #endif
179 		if (ether_line(buf, &try, hostname) == 0 &&
180 		    memcmp((void *)&try, (void *)e, sizeof(try)) == 0) {
181 			(void)fclose(f);
182 			return (0);
183 		}
184 	}
185 	(void)fclose(f);
186 	errno = ENOENT;
187 	return (-1);
188 }
189 
190 int
191 ether_hostton(hostname, e)
192 	char *hostname;
193 	struct ether_addr *e;
194 {
195 	FILE *f;
196 	char buf[BUFSIZ+1], *p;
197 	char try[MAXHOSTNAMELEN];
198 	size_t len;
199 #ifdef YP
200 	int hostlen = strlen(hostname);
201 #endif
202 
203 	f = fopen(_PATH_ETHERS, "r");
204 	if (f==NULL)
205 		return (-1);
206 
207 	while ((p = fgetln(f, &len)) != NULL) {
208 		if (p[len-1] == '\n')
209 			len--;
210 		if (len > sizeof(buf) - 2)
211 			continue;
212 		memcpy(buf, p, len);
213 		buf[len] = '\n';	/* code assumes newlines later on */
214 		buf[len+1] = '\0';
215 #ifdef YP
216 		/* A + in the file means try YP now.  */
217 		if (!strncmp(buf, "+\n", sizeof(buf))) {
218 			char *ypbuf, *ypdom;
219 			int ypbuflen;
220 
221 			if (yp_get_default_domain(&ypdom))
222 				continue;
223 			if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
224 			    &ypbuf, &ypbuflen))
225 				continue;
226 			if (ether_line(ypbuf, e, try) == 0) {
227 				free(ypbuf);
228 				(void)fclose(f);
229 				return (0);
230 			}
231 			free(ypbuf);
232 			continue;
233 		}
234 #endif
235 		if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
236 			(void)fclose(f);
237 			return (0);
238 		}
239 	}
240 	(void)fclose(f);
241 	errno = ENOENT;
242 	return (-1);
243 }
244 
245 int
246 ether_line(line, e, hostname)
247 	char *line;
248 	struct ether_addr *e;
249 	char *hostname;
250 {
251 	char *p;
252 	size_t n;
253 
254 	/* Parse "xx:xx:xx:xx:xx:xx" */
255 	if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
256 		goto bad;
257 
258 	/* Now get the hostname */
259 	while (isspace(*p))
260 		p++;
261 	if (*p == '\0')
262 		goto bad;
263 	n = strcspn(p, " \t\n");
264 	if (n >= MAXHOSTNAMELEN)
265 		goto bad;
266 	strlcpy(hostname, p, n + 1);
267 	return (0);
268 
269 bad:
270 	errno = EINVAL;
271 	return (-1);
272 }
273