summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/ethers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/ethers.c')
-rw-r--r--src/lib/libc/net/ethers.c204
1 files changed, 149 insertions, 55 deletions
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c
index 0f32b9b71b..858ac5b3f3 100644
--- a/src/lib/libc/net/ethers.c
+++ b/src/lib/libc/net/ethers.c
@@ -1,12 +1,42 @@
1/* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ 1/* $OpenBSD: ethers.c,v 1.14 2002/05/24 21:22:37 deraadt Exp $ */
2 2
3/* 3/*
4 * ethers(3N) a la Sun. 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.
5 * 17 *
6 * Written by Roland McGrath <roland@frob.com> 10/14/93. 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
7 * Public domain. 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>
8 */ 34 */
9 35
36#if defined(LIBC_SCCS) && !defined(lint)
37static char rcsid[] = "$OpenBSD: ethers.c,v 1.14 2002/05/24 21:22:37 deraadt Exp $";
38#endif /* LIBC_SCCS and not lint */
39
10#include <sys/types.h> 40#include <sys/types.h>
11#include <sys/socket.h> 41#include <sys/socket.h>
12#include <net/if.h> 42#include <net/if.h>
@@ -18,22 +48,63 @@
18#include <stdio.h> 48#include <stdio.h>
19#include <stdlib.h> 49#include <stdlib.h>
20#include <string.h> 50#include <string.h>
51#include <ctype.h>
52#ifdef YP
53#include <rpcsvc/ypclnt.h>
54#endif
21 55
22#ifndef _PATH_ETHERS 56#ifndef _PATH_ETHERS
23#define _PATH_ETHERS "/etc/ethers" 57#define _PATH_ETHERS "/etc/ethers"
24#endif 58#endif
25 59
60static char * _ether_aton(char *, struct ether_addr *);
61
26char * 62char *
27ether_ntoa(e) 63ether_ntoa(e)
28 struct ether_addr *e; 64 struct ether_addr *e;
29{ 65{
30 static char a[] = "xx:xx:xx:xx:xx:xx"; 66 static char a[] = "xx:xx:xx:xx:xx:xx";
31 67
32 sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", 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)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
33 e->ether_addr_octet[0], e->ether_addr_octet[1], 76 e->ether_addr_octet[0], e->ether_addr_octet[1],
34 e->ether_addr_octet[2], e->ether_addr_octet[3], 77 e->ether_addr_octet[2], e->ether_addr_octet[3],
35 e->ether_addr_octet[4], e->ether_addr_octet[5]); 78 e->ether_addr_octet[4], e->ether_addr_octet[5]);
36 return a; 79
80 return (a);
81}
82
83static 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);
37} 108}
38 109
39struct ether_addr * 110struct ether_addr *
@@ -41,34 +112,33 @@ ether_aton(s)
41 char *s; 112 char *s;
42{ 113{
43 static struct ether_addr n; 114 static struct ether_addr n;
44 u_int i[6]; 115
45 116 return (_ether_aton(s, &n) ? &n : NULL);
46 if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
47 &i[2], &i[3], &i[4], &i[5]) == 6) {
48 n.ether_addr_octet[0] = (u_char)i[0];
49 n.ether_addr_octet[1] = (u_char)i[1];
50 n.ether_addr_octet[2] = (u_char)i[2];
51 n.ether_addr_octet[3] = (u_char)i[3];
52 n.ether_addr_octet[4] = (u_char)i[4];
53 n.ether_addr_octet[5] = (u_char)i[5];
54 return &n;
55 }
56 return NULL;
57} 117}
58 118
119int
59ether_ntohost(hostname, e) 120ether_ntohost(hostname, e)
60 char *hostname; 121 char *hostname;
61 struct ether_addr *e; 122 struct ether_addr *e;
62{ 123{
63 FILE *f; 124 FILE *f;
64 char buf[BUFSIZ]; 125 char buf[BUFSIZ+1], *p;
126 size_t len;
65 struct ether_addr try; 127 struct ether_addr try;
66
67#ifdef YP 128#ifdef YP
68 char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; 129 char trybuf[sizeof("xx:xx:xx:xx:xx:xx")];
69 int trylen; 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 }
70 139
71 sprintf(trybuf, "%x:%x:%x:%x:%x:%x", 140#ifdef YP
141 snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x",
72 e->ether_addr_octet[0], e->ether_addr_octet[1], 142 e->ether_addr_octet[0], e->ether_addr_octet[1],
73 e->ether_addr_octet[2], e->ether_addr_octet[3], 143 e->ether_addr_octet[2], e->ether_addr_octet[3],
74 e->ether_addr_octet[4], e->ether_addr_octet[5]); 144 e->ether_addr_octet[4], e->ether_addr_octet[5]);
@@ -76,12 +146,19 @@ ether_ntohost(hostname, e)
76#endif 146#endif
77 147
78 f = fopen(_PATH_ETHERS, "r"); 148 f = fopen(_PATH_ETHERS, "r");
79 if (f==NULL) 149 if (f == NULL)
80 return -1; 150 return (-1);
81 while (fgets(buf, sizeof buf, f)) { 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';
82#ifdef YP 159#ifdef YP
83 /* A + in the file means try YP now. */ 160 /* A + in the file means try YP now. */
84 if (!strncmp(buf, "+\n", sizeof buf)) { 161 if (!strncmp(buf, "+\n", sizeof(buf))) {
85 char *ypbuf, *ypdom; 162 char *ypbuf, *ypdom;
86 int ypbuflen; 163 int ypbuflen;
87 164
@@ -93,42 +170,51 @@ ether_ntohost(hostname, e)
93 if (ether_line(ypbuf, &try, hostname) == 0) { 170 if (ether_line(ypbuf, &try, hostname) == 0) {
94 free(ypbuf); 171 free(ypbuf);
95 (void)fclose(f); 172 (void)fclose(f);
96 return 0; 173 return (0);
97 } 174 }
98 free(ypbuf); 175 free(ypbuf);
99 continue; 176 continue;
100 } 177 }
101#endif 178#endif
102 if (ether_line(buf, &try, hostname) == 0 && 179 if (ether_line(buf, &try, hostname) == 0 &&
103 bcmp((char *)&try, (char *)e, sizeof try) == 0) { 180 memcmp((void *)&try, (void *)e, sizeof(try)) == 0) {
104 (void)fclose(f); 181 (void)fclose(f);
105 return 0; 182 return (0);
106 } 183 }
107 } 184 }
108 (void)fclose(f); 185 (void)fclose(f);
109 errno = ENOENT; 186 errno = ENOENT;
110 return -1; 187 return (-1);
111} 188}
112 189
190int
113ether_hostton(hostname, e) 191ether_hostton(hostname, e)
114 char *hostname; 192 char *hostname;
115 struct ether_addr *e; 193 struct ether_addr *e;
116{ 194{
117 FILE *f; 195 FILE *f;
118 char buf[BUFSIZ]; 196 char buf[BUFSIZ+1], *p;
119 char try[MAXHOSTNAMELEN]; 197 char try[MAXHOSTNAMELEN];
198 size_t len;
120#ifdef YP 199#ifdef YP
121 int hostlen = strlen(hostname); 200 int hostlen = strlen(hostname);
122#endif 201#endif
123 202
124 f = fopen(_PATH_ETHERS, "r"); 203 f = fopen(_PATH_ETHERS, "r");
125 if (f==NULL) 204 if (f==NULL)
126 return -1; 205 return (-1);
127 206
128 while (fgets(buf, sizeof buf, f)) { 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';
129#ifdef YP 215#ifdef YP
130 /* A + in the file means try YP now. */ 216 /* A + in the file means try YP now. */
131 if (!strncmp(buf, "+\n", sizeof buf)) { 217 if (!strncmp(buf, "+\n", sizeof(buf))) {
132 char *ypbuf, *ypdom; 218 char *ypbuf, *ypdom;
133 int ypbuflen; 219 int ypbuflen;
134 220
@@ -140,7 +226,7 @@ ether_hostton(hostname, e)
140 if (ether_line(ypbuf, e, try) == 0) { 226 if (ether_line(ypbuf, e, try) == 0) {
141 free(ypbuf); 227 free(ypbuf);
142 (void)fclose(f); 228 (void)fclose(f);
143 return 0; 229 return (0);
144 } 230 }
145 free(ypbuf); 231 free(ypbuf);
146 continue; 232 continue;
@@ -148,31 +234,39 @@ ether_hostton(hostname, e)
148#endif 234#endif
149 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { 235 if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
150 (void)fclose(f); 236 (void)fclose(f);
151 return 0; 237 return (0);
152 } 238 }
153 } 239 }
154 (void)fclose(f); 240 (void)fclose(f);
155 errno = ENOENT; 241 errno = ENOENT;
156 return -1; 242 return (-1);
157} 243}
158 244
159ether_line(l, e, hostname) 245int
160 char *l; 246ether_line(line, e, hostname)
247 char *line;
161 struct ether_addr *e; 248 struct ether_addr *e;
162 char *hostname; 249 char *hostname;
163{ 250{
164 u_int i[6]; 251 char *p;
165 252 size_t n;
166 if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], 253
167 &i[2], &i[3], &i[4], &i[5], hostname) == 7) { 254 /* Parse "xx:xx:xx:xx:xx:xx" */
168 e->ether_addr_octet[0] = (u_char)i[0]; 255 if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t'))
169 e->ether_addr_octet[1] = (u_char)i[1]; 256 goto bad;
170 e->ether_addr_octet[2] = (u_char)i[2]; 257
171 e->ether_addr_octet[3] = (u_char)i[3]; 258 /* Now get the hostname */
172 e->ether_addr_octet[4] = (u_char)i[4]; 259 while (isspace(*p))
173 e->ether_addr_octet[5] = (u_char)i[5]; 260 p++;
174 return 0; 261 if (*p == '\0')
175 } 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
269bad:
176 errno = EINVAL; 270 errno = EINVAL;
177 return -1; 271 return (-1);
178} 272}