diff options
Diffstat (limited to 'src/lib/libc/net/ethers.c')
-rw-r--r-- | src/lib/libc/net/ethers.c | 200 |
1 files changed, 146 insertions, 54 deletions
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c index 0f32b9b71b..9df876b6f4 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.9 1998/06/21 22:13:44 millert 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) | ||
37 | static char rcsid[] = "$OpenBSD: ethers.c,v 1.9 1998/06/21 22:13:44 millert 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,60 @@ | |||
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> | ||
21 | 52 | ||
22 | #ifndef _PATH_ETHERS | 53 | #ifndef _PATH_ETHERS |
23 | #define _PATH_ETHERS "/etc/ethers" | 54 | #define _PATH_ETHERS "/etc/ethers" |
24 | #endif | 55 | #endif |
25 | 56 | ||
57 | static char * _ether_aton __P((char *, struct ether_addr *)); | ||
58 | |||
26 | char * | 59 | char * |
27 | ether_ntoa(e) | 60 | ether_ntoa(e) |
28 | struct ether_addr *e; | 61 | struct ether_addr *e; |
29 | { | 62 | { |
30 | static char a[] = "xx:xx:xx:xx:xx:xx"; | 63 | static char a[] = "xx:xx:xx:xx:xx:xx"; |
31 | 64 | ||
32 | sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | 65 | if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || |
66 | e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || | ||
67 | e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { | ||
68 | errno = EINVAL; | ||
69 | return (NULL); | ||
70 | } | ||
71 | |||
72 | (void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
33 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 73 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
34 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 74 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
35 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | 75 | e->ether_addr_octet[4], e->ether_addr_octet[5]); |
36 | return a; | 76 | |
77 | return (a); | ||
78 | } | ||
79 | |||
80 | static char * | ||
81 | _ether_aton(s, e) | ||
82 | char *s; | ||
83 | struct ether_addr *e; | ||
84 | { | ||
85 | int i; | ||
86 | long l; | ||
87 | char *pp; | ||
88 | |||
89 | while (isspace(*s)) | ||
90 | s++; | ||
91 | |||
92 | /* expect 6 hex octets separated by ':' or space/NUL if last octet */ | ||
93 | for (i = 0; i < 6; i++) { | ||
94 | l = strtol(s, &pp, 16); | ||
95 | if (pp == s || l > 0xFF) | ||
96 | return (NULL); | ||
97 | if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) | ||
98 | return (NULL); | ||
99 | e->ether_addr_octet[i] = (u_char)l; | ||
100 | s = pp + 1; | ||
101 | } | ||
102 | |||
103 | /* return character after the octets ala strtol(3) */ | ||
104 | return (pp); | ||
37 | } | 105 | } |
38 | 106 | ||
39 | struct ether_addr * | 107 | struct ether_addr * |
@@ -41,33 +109,32 @@ ether_aton(s) | |||
41 | char *s; | 109 | char *s; |
42 | { | 110 | { |
43 | static struct ether_addr n; | 111 | static struct ether_addr n; |
44 | u_int i[6]; | 112 | |
45 | 113 | 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 | } | 114 | } |
58 | 115 | ||
116 | int | ||
59 | ether_ntohost(hostname, e) | 117 | ether_ntohost(hostname, e) |
60 | char *hostname; | 118 | char *hostname; |
61 | struct ether_addr *e; | 119 | struct ether_addr *e; |
62 | { | 120 | { |
63 | FILE *f; | 121 | FILE *f; |
64 | char buf[BUFSIZ]; | 122 | char buf[BUFSIZ+1], *p; |
123 | size_t len; | ||
65 | struct ether_addr try; | 124 | struct ether_addr try; |
66 | |||
67 | #ifdef YP | 125 | #ifdef YP |
68 | char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; | 126 | char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; |
69 | int trylen; | 127 | int trylen; |
128 | #endif | ||
129 | |||
130 | if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || | ||
131 | e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || | ||
132 | e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { | ||
133 | errno = EINVAL; | ||
134 | return (-1); | ||
135 | } | ||
70 | 136 | ||
137 | #ifdef YP | ||
71 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", | 138 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", |
72 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 139 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
73 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 140 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
@@ -76,12 +143,19 @@ ether_ntohost(hostname, e) | |||
76 | #endif | 143 | #endif |
77 | 144 | ||
78 | f = fopen(_PATH_ETHERS, "r"); | 145 | f = fopen(_PATH_ETHERS, "r"); |
79 | if (f==NULL) | 146 | if (f == NULL) |
80 | return -1; | 147 | return (-1); |
81 | while (fgets(buf, sizeof buf, f)) { | 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'; | ||
82 | #ifdef YP | 156 | #ifdef YP |
83 | /* A + in the file means try YP now. */ | 157 | /* A + in the file means try YP now. */ |
84 | if (!strncmp(buf, "+\n", sizeof buf)) { | 158 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
85 | char *ypbuf, *ypdom; | 159 | char *ypbuf, *ypdom; |
86 | int ypbuflen; | 160 | int ypbuflen; |
87 | 161 | ||
@@ -93,42 +167,51 @@ ether_ntohost(hostname, e) | |||
93 | if (ether_line(ypbuf, &try, hostname) == 0) { | 167 | if (ether_line(ypbuf, &try, hostname) == 0) { |
94 | free(ypbuf); | 168 | free(ypbuf); |
95 | (void)fclose(f); | 169 | (void)fclose(f); |
96 | return 0; | 170 | return (0); |
97 | } | 171 | } |
98 | free(ypbuf); | 172 | free(ypbuf); |
99 | continue; | 173 | continue; |
100 | } | 174 | } |
101 | #endif | 175 | #endif |
102 | if (ether_line(buf, &try, hostname) == 0 && | 176 | if (ether_line(buf, &try, hostname) == 0 && |
103 | bcmp((char *)&try, (char *)e, sizeof try) == 0) { | 177 | memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { |
104 | (void)fclose(f); | 178 | (void)fclose(f); |
105 | return 0; | 179 | return (0); |
106 | } | 180 | } |
107 | } | 181 | } |
108 | (void)fclose(f); | 182 | (void)fclose(f); |
109 | errno = ENOENT; | 183 | errno = ENOENT; |
110 | return -1; | 184 | return (-1); |
111 | } | 185 | } |
112 | 186 | ||
187 | int | ||
113 | ether_hostton(hostname, e) | 188 | ether_hostton(hostname, e) |
114 | char *hostname; | 189 | char *hostname; |
115 | struct ether_addr *e; | 190 | struct ether_addr *e; |
116 | { | 191 | { |
117 | FILE *f; | 192 | FILE *f; |
118 | char buf[BUFSIZ]; | 193 | char buf[BUFSIZ+1], *p; |
119 | char try[MAXHOSTNAMELEN]; | 194 | char try[MAXHOSTNAMELEN]; |
195 | size_t len; | ||
120 | #ifdef YP | 196 | #ifdef YP |
121 | int hostlen = strlen(hostname); | 197 | int hostlen = strlen(hostname); |
122 | #endif | 198 | #endif |
123 | 199 | ||
124 | f = fopen(_PATH_ETHERS, "r"); | 200 | f = fopen(_PATH_ETHERS, "r"); |
125 | if (f==NULL) | 201 | if (f==NULL) |
126 | return -1; | 202 | return (-1); |
127 | 203 | ||
128 | while (fgets(buf, sizeof buf, f)) { | 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'; | ||
129 | #ifdef YP | 212 | #ifdef YP |
130 | /* A + in the file means try YP now. */ | 213 | /* A + in the file means try YP now. */ |
131 | if (!strncmp(buf, "+\n", sizeof buf)) { | 214 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
132 | char *ypbuf, *ypdom; | 215 | char *ypbuf, *ypdom; |
133 | int ypbuflen; | 216 | int ypbuflen; |
134 | 217 | ||
@@ -140,7 +223,7 @@ ether_hostton(hostname, e) | |||
140 | if (ether_line(ypbuf, e, try) == 0) { | 223 | if (ether_line(ypbuf, e, try) == 0) { |
141 | free(ypbuf); | 224 | free(ypbuf); |
142 | (void)fclose(f); | 225 | (void)fclose(f); |
143 | return 0; | 226 | return (0); |
144 | } | 227 | } |
145 | free(ypbuf); | 228 | free(ypbuf); |
146 | continue; | 229 | continue; |
@@ -148,31 +231,40 @@ ether_hostton(hostname, e) | |||
148 | #endif | 231 | #endif |
149 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | 232 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { |
150 | (void)fclose(f); | 233 | (void)fclose(f); |
151 | return 0; | 234 | return (0); |
152 | } | 235 | } |
153 | } | 236 | } |
154 | (void)fclose(f); | 237 | (void)fclose(f); |
155 | errno = ENOENT; | 238 | errno = ENOENT; |
156 | return -1; | 239 | return (-1); |
157 | } | 240 | } |
158 | 241 | ||
159 | ether_line(l, e, hostname) | 242 | int |
160 | char *l; | 243 | ether_line(line, e, hostname) |
244 | char *line; | ||
161 | struct ether_addr *e; | 245 | struct ether_addr *e; |
162 | char *hostname; | 246 | char *hostname; |
163 | { | 247 | { |
164 | u_int i[6]; | 248 | char *p; |
165 | 249 | size_t n; | |
166 | if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], | 250 | |
167 | &i[2], &i[3], &i[4], &i[5], hostname) == 7) { | 251 | /* Parse "xx:xx:xx:xx:xx:xx" */ |
168 | e->ether_addr_octet[0] = (u_char)i[0]; | 252 | if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) |
169 | e->ether_addr_octet[1] = (u_char)i[1]; | 253 | goto bad; |
170 | e->ether_addr_octet[2] = (u_char)i[2]; | 254 | |
171 | e->ether_addr_octet[3] = (u_char)i[3]; | 255 | /* Now get the hostname */ |
172 | e->ether_addr_octet[4] = (u_char)i[4]; | 256 | while (isspace(*p)) |
173 | e->ether_addr_octet[5] = (u_char)i[5]; | 257 | p++; |
174 | return 0; | 258 | if (*p == '\0') |
175 | } | 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: | ||
176 | errno = EINVAL; | 268 | errno = EINVAL; |
177 | return -1; | 269 | return (-1); |
178 | } | 270 | } |