diff options
Diffstat (limited to 'src/lib/libc/net/ethers.c')
-rw-r--r-- | src/lib/libc/net/ethers.c | 179 |
1 files changed, 124 insertions, 55 deletions
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c index 0f32b9b71b..2c1e562245 100644 --- a/src/lib/libc/net/ethers.c +++ b/src/lib/libc/net/ethers.c | |||
@@ -1,12 +1,31 @@ | |||
1 | /* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ | 1 | /* $OpenBSD: ethers.c,v 1.17 2004/02/16 19:41:12 otto Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * ethers(3N) a la Sun. | 4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
5 | * | 9 | * |
6 | * Written by Roland McGrath <roland@frob.com> 10/14/93. | 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
7 | * Public domain. | 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
8 | */ | 17 | */ |
9 | 18 | ||
19 | /* | ||
20 | * ethers(3) a la Sun. | ||
21 | * Originally Written by Roland McGrath <roland@frob.com> 10/14/93. | ||
22 | * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com> | ||
23 | */ | ||
24 | |||
25 | #if defined(LIBC_SCCS) && !defined(lint) | ||
26 | static char rcsid[] = "$OpenBSD: ethers.c,v 1.17 2004/02/16 19:41:12 otto Exp $"; | ||
27 | #endif /* LIBC_SCCS and not lint */ | ||
28 | |||
10 | #include <sys/types.h> | 29 | #include <sys/types.h> |
11 | #include <sys/socket.h> | 30 | #include <sys/socket.h> |
12 | #include <net/if.h> | 31 | #include <net/if.h> |
@@ -18,22 +37,56 @@ | |||
18 | #include <stdio.h> | 37 | #include <stdio.h> |
19 | #include <stdlib.h> | 38 | #include <stdlib.h> |
20 | #include <string.h> | 39 | #include <string.h> |
40 | #include <ctype.h> | ||
41 | #ifdef YP | ||
42 | #include <rpcsvc/ypclnt.h> | ||
43 | #endif | ||
21 | 44 | ||
22 | #ifndef _PATH_ETHERS | 45 | #ifndef _PATH_ETHERS |
23 | #define _PATH_ETHERS "/etc/ethers" | 46 | #define _PATH_ETHERS "/etc/ethers" |
24 | #endif | 47 | #endif |
25 | 48 | ||
49 | static char * _ether_aton(char *, struct ether_addr *); | ||
50 | |||
26 | char * | 51 | char * |
27 | ether_ntoa(e) | 52 | ether_ntoa(e) |
28 | struct ether_addr *e; | 53 | struct ether_addr *e; |
29 | { | 54 | { |
30 | static char a[] = "xx:xx:xx:xx:xx:xx"; | 55 | static char a[] = "xx:xx:xx:xx:xx:xx"; |
31 | 56 | ||
32 | sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | 57 | (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", |
33 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 58 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
34 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 59 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
35 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | 60 | e->ether_addr_octet[4], e->ether_addr_octet[5]); |
36 | return a; | 61 | |
62 | return (a); | ||
63 | } | ||
64 | |||
65 | static char * | ||
66 | _ether_aton(s, e) | ||
67 | char *s; | ||
68 | struct ether_addr *e; | ||
69 | { | ||
70 | int i; | ||
71 | long l; | ||
72 | char *pp; | ||
73 | |||
74 | while (isspace(*s)) | ||
75 | s++; | ||
76 | |||
77 | /* expect 6 hex octets separated by ':' or space/NUL if last octet */ | ||
78 | for (i = 0; i < 6; i++) { | ||
79 | l = strtol(s, &pp, 16); | ||
80 | if (pp == s || l > 0xFF || l < 0) | ||
81 | return (NULL); | ||
82 | if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) | ||
83 | return (NULL); | ||
84 | e->ether_addr_octet[i] = (u_char)l; | ||
85 | s = pp + 1; | ||
86 | } | ||
87 | |||
88 | /* return character after the octets ala strtol(3) */ | ||
89 | return (pp); | ||
37 | } | 90 | } |
38 | 91 | ||
39 | struct ether_addr * | 92 | struct ether_addr * |
@@ -41,34 +94,26 @@ ether_aton(s) | |||
41 | char *s; | 94 | char *s; |
42 | { | 95 | { |
43 | static struct ether_addr n; | 96 | static struct ether_addr n; |
44 | u_int i[6]; | 97 | |
45 | 98 | 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 | } | 99 | } |
58 | 100 | ||
101 | int | ||
59 | ether_ntohost(hostname, e) | 102 | ether_ntohost(hostname, e) |
60 | char *hostname; | 103 | char *hostname; |
61 | struct ether_addr *e; | 104 | struct ether_addr *e; |
62 | { | 105 | { |
63 | FILE *f; | 106 | FILE *f; |
64 | char buf[BUFSIZ]; | 107 | char buf[BUFSIZ+1], *p; |
108 | size_t len; | ||
65 | struct ether_addr try; | 109 | struct ether_addr try; |
66 | |||
67 | #ifdef YP | 110 | #ifdef YP |
68 | char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; | 111 | char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; |
69 | int trylen; | 112 | int trylen; |
113 | #endif | ||
70 | 114 | ||
71 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", | 115 | #ifdef YP |
116 | snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", | ||
72 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 117 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
73 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 118 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
74 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | 119 | e->ether_addr_octet[4], e->ether_addr_octet[5]); |
@@ -76,12 +121,19 @@ ether_ntohost(hostname, e) | |||
76 | #endif | 121 | #endif |
77 | 122 | ||
78 | f = fopen(_PATH_ETHERS, "r"); | 123 | f = fopen(_PATH_ETHERS, "r"); |
79 | if (f==NULL) | 124 | if (f == NULL) |
80 | return -1; | 125 | return (-1); |
81 | while (fgets(buf, sizeof buf, f)) { | 126 | while ((p = fgetln(f, &len)) != NULL) { |
127 | if (p[len-1] == '\n') | ||
128 | len--; | ||
129 | if (len > sizeof(buf) - 2) | ||
130 | continue; | ||
131 | (void)memcpy(buf, p, len); | ||
132 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
133 | buf[len+1] = '\0'; | ||
82 | #ifdef YP | 134 | #ifdef YP |
83 | /* A + in the file means try YP now. */ | 135 | /* A + in the file means try YP now. */ |
84 | if (!strncmp(buf, "+\n", sizeof buf)) { | 136 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
85 | char *ypbuf, *ypdom; | 137 | char *ypbuf, *ypdom; |
86 | int ypbuflen; | 138 | int ypbuflen; |
87 | 139 | ||
@@ -93,42 +145,51 @@ ether_ntohost(hostname, e) | |||
93 | if (ether_line(ypbuf, &try, hostname) == 0) { | 145 | if (ether_line(ypbuf, &try, hostname) == 0) { |
94 | free(ypbuf); | 146 | free(ypbuf); |
95 | (void)fclose(f); | 147 | (void)fclose(f); |
96 | return 0; | 148 | return (0); |
97 | } | 149 | } |
98 | free(ypbuf); | 150 | free(ypbuf); |
99 | continue; | 151 | continue; |
100 | } | 152 | } |
101 | #endif | 153 | #endif |
102 | if (ether_line(buf, &try, hostname) == 0 && | 154 | if (ether_line(buf, &try, hostname) == 0 && |
103 | bcmp((char *)&try, (char *)e, sizeof try) == 0) { | 155 | memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { |
104 | (void)fclose(f); | 156 | (void)fclose(f); |
105 | return 0; | 157 | return (0); |
106 | } | 158 | } |
107 | } | 159 | } |
108 | (void)fclose(f); | 160 | (void)fclose(f); |
109 | errno = ENOENT; | 161 | errno = ENOENT; |
110 | return -1; | 162 | return (-1); |
111 | } | 163 | } |
112 | 164 | ||
165 | int | ||
113 | ether_hostton(hostname, e) | 166 | ether_hostton(hostname, e) |
114 | char *hostname; | 167 | char *hostname; |
115 | struct ether_addr *e; | 168 | struct ether_addr *e; |
116 | { | 169 | { |
117 | FILE *f; | 170 | FILE *f; |
118 | char buf[BUFSIZ]; | 171 | char buf[BUFSIZ+1], *p; |
119 | char try[MAXHOSTNAMELEN]; | 172 | char try[MAXHOSTNAMELEN]; |
173 | size_t len; | ||
120 | #ifdef YP | 174 | #ifdef YP |
121 | int hostlen = strlen(hostname); | 175 | int hostlen = strlen(hostname); |
122 | #endif | 176 | #endif |
123 | 177 | ||
124 | f = fopen(_PATH_ETHERS, "r"); | 178 | f = fopen(_PATH_ETHERS, "r"); |
125 | if (f==NULL) | 179 | if (f==NULL) |
126 | return -1; | 180 | return (-1); |
127 | 181 | ||
128 | while (fgets(buf, sizeof buf, f)) { | 182 | while ((p = fgetln(f, &len)) != NULL) { |
183 | if (p[len-1] == '\n') | ||
184 | len--; | ||
185 | if (len > sizeof(buf) - 2) | ||
186 | continue; | ||
187 | memcpy(buf, p, len); | ||
188 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
189 | buf[len+1] = '\0'; | ||
129 | #ifdef YP | 190 | #ifdef YP |
130 | /* A + in the file means try YP now. */ | 191 | /* A + in the file means try YP now. */ |
131 | if (!strncmp(buf, "+\n", sizeof buf)) { | 192 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
132 | char *ypbuf, *ypdom; | 193 | char *ypbuf, *ypdom; |
133 | int ypbuflen; | 194 | int ypbuflen; |
134 | 195 | ||
@@ -140,7 +201,7 @@ ether_hostton(hostname, e) | |||
140 | if (ether_line(ypbuf, e, try) == 0) { | 201 | if (ether_line(ypbuf, e, try) == 0) { |
141 | free(ypbuf); | 202 | free(ypbuf); |
142 | (void)fclose(f); | 203 | (void)fclose(f); |
143 | return 0; | 204 | return (0); |
144 | } | 205 | } |
145 | free(ypbuf); | 206 | free(ypbuf); |
146 | continue; | 207 | continue; |
@@ -148,31 +209,39 @@ ether_hostton(hostname, e) | |||
148 | #endif | 209 | #endif |
149 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | 210 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { |
150 | (void)fclose(f); | 211 | (void)fclose(f); |
151 | return 0; | 212 | return (0); |
152 | } | 213 | } |
153 | } | 214 | } |
154 | (void)fclose(f); | 215 | (void)fclose(f); |
155 | errno = ENOENT; | 216 | errno = ENOENT; |
156 | return -1; | 217 | return (-1); |
157 | } | 218 | } |
158 | 219 | ||
159 | ether_line(l, e, hostname) | 220 | int |
160 | char *l; | 221 | ether_line(line, e, hostname) |
222 | char *line; | ||
161 | struct ether_addr *e; | 223 | struct ether_addr *e; |
162 | char *hostname; | 224 | char *hostname; |
163 | { | 225 | { |
164 | u_int i[6]; | 226 | char *p; |
165 | 227 | size_t n; | |
166 | if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], | 228 | |
167 | &i[2], &i[3], &i[4], &i[5], hostname) == 7) { | 229 | /* Parse "xx:xx:xx:xx:xx:xx" */ |
168 | e->ether_addr_octet[0] = (u_char)i[0]; | 230 | if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) |
169 | e->ether_addr_octet[1] = (u_char)i[1]; | 231 | goto bad; |
170 | e->ether_addr_octet[2] = (u_char)i[2]; | 232 | |
171 | e->ether_addr_octet[3] = (u_char)i[3]; | 233 | /* Now get the hostname */ |
172 | e->ether_addr_octet[4] = (u_char)i[4]; | 234 | while (isspace(*p)) |
173 | e->ether_addr_octet[5] = (u_char)i[5]; | 235 | p++; |
174 | return 0; | 236 | if (*p == '\0') |
175 | } | 237 | goto bad; |
238 | n = strcspn(p, " \t\n"); | ||
239 | if (n >= MAXHOSTNAMELEN) | ||
240 | goto bad; | ||
241 | strlcpy(hostname, p, n + 1); | ||
242 | return (0); | ||
243 | |||
244 | bad: | ||
176 | errno = EINVAL; | 245 | errno = EINVAL; |
177 | return -1; | 246 | return (-1); |
178 | } | 247 | } |