diff options
Diffstat (limited to 'src/lib/libc/net/ethers.c')
-rw-r--r-- | src/lib/libc/net/ethers.c | 188 |
1 files changed, 121 insertions, 67 deletions
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c index 0f32b9b71b..d4243ff1da 100644 --- a/src/lib/libc/net/ethers.c +++ b/src/lib/libc/net/ethers.c | |||
@@ -1,10 +1,25 @@ | |||
1 | /* $NetBSD: ethers.c,v 1.5 1995/02/25 06:20:28 cgd Exp $ */ | 1 | /* $OpenBSD: ethers.c,v 1.20 2005/08/06 20:30:03 espie 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. | ||
17 | */ | ||
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> | ||
8 | */ | 23 | */ |
9 | 24 | ||
10 | #include <sys/types.h> | 25 | #include <sys/types.h> |
@@ -18,57 +33,77 @@ | |||
18 | #include <stdio.h> | 33 | #include <stdio.h> |
19 | #include <stdlib.h> | 34 | #include <stdlib.h> |
20 | #include <string.h> | 35 | #include <string.h> |
36 | #include <ctype.h> | ||
37 | #ifdef YP | ||
38 | #include <rpcsvc/ypclnt.h> | ||
39 | #endif | ||
21 | 40 | ||
22 | #ifndef _PATH_ETHERS | 41 | #ifndef _PATH_ETHERS |
23 | #define _PATH_ETHERS "/etc/ethers" | 42 | #define _PATH_ETHERS "/etc/ethers" |
24 | #endif | 43 | #endif |
25 | 44 | ||
45 | static char * _ether_aton(const char *, struct ether_addr *); | ||
46 | |||
26 | char * | 47 | char * |
27 | ether_ntoa(e) | 48 | ether_ntoa(struct ether_addr *e) |
28 | struct ether_addr *e; | ||
29 | { | 49 | { |
30 | static char a[] = "xx:xx:xx:xx:xx:xx"; | 50 | static char a[] = "xx:xx:xx:xx:xx:xx"; |
31 | 51 | ||
32 | sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", | 52 | (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", |
33 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 53 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
34 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 54 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
35 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | 55 | e->ether_addr_octet[4], e->ether_addr_octet[5]); |
36 | return a; | 56 | |
57 | return (a); | ||
58 | } | ||
59 | |||
60 | static char * | ||
61 | _ether_aton(const char *s, struct ether_addr *e) | ||
62 | { | ||
63 | int i; | ||
64 | long l; | ||
65 | char *pp; | ||
66 | |||
67 | while (isspace(*s)) | ||
68 | s++; | ||
69 | |||
70 | /* expect 6 hex octets separated by ':' or space/NUL if last octet */ | ||
71 | for (i = 0; i < 6; i++) { | ||
72 | l = strtol(s, &pp, 16); | ||
73 | if (pp == s || l > 0xFF || l < 0) | ||
74 | return (NULL); | ||
75 | if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) | ||
76 | return (NULL); | ||
77 | e->ether_addr_octet[i] = (u_char)l; | ||
78 | s = pp + 1; | ||
79 | } | ||
80 | |||
81 | /* return character after the octets ala strtol(3) */ | ||
82 | return (pp); | ||
37 | } | 83 | } |
38 | 84 | ||
39 | struct ether_addr * | 85 | struct ether_addr * |
40 | ether_aton(s) | 86 | ether_aton(const char *s) |
41 | char *s; | ||
42 | { | 87 | { |
43 | static struct ether_addr n; | 88 | static struct ether_addr n; |
44 | u_int i[6]; | 89 | |
45 | 90 | 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 | } | 91 | } |
58 | 92 | ||
59 | ether_ntohost(hostname, e) | 93 | int |
60 | char *hostname; | 94 | ether_ntohost(char *hostname, struct ether_addr *e) |
61 | struct ether_addr *e; | ||
62 | { | 95 | { |
63 | FILE *f; | 96 | FILE *f; |
64 | char buf[BUFSIZ]; | 97 | char buf[BUFSIZ+1], *p; |
98 | size_t len; | ||
65 | struct ether_addr try; | 99 | struct ether_addr try; |
66 | |||
67 | #ifdef YP | 100 | #ifdef YP |
68 | char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; | 101 | char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; |
69 | int trylen; | 102 | int trylen; |
103 | #endif | ||
70 | 104 | ||
71 | sprintf(trybuf, "%x:%x:%x:%x:%x:%x", | 105 | #ifdef YP |
106 | snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", | ||
72 | e->ether_addr_octet[0], e->ether_addr_octet[1], | 107 | e->ether_addr_octet[0], e->ether_addr_octet[1], |
73 | e->ether_addr_octet[2], e->ether_addr_octet[3], | 108 | e->ether_addr_octet[2], e->ether_addr_octet[3], |
74 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | 109 | e->ether_addr_octet[4], e->ether_addr_octet[5]); |
@@ -76,12 +111,19 @@ ether_ntohost(hostname, e) | |||
76 | #endif | 111 | #endif |
77 | 112 | ||
78 | f = fopen(_PATH_ETHERS, "r"); | 113 | f = fopen(_PATH_ETHERS, "r"); |
79 | if (f==NULL) | 114 | if (f == NULL) |
80 | return -1; | 115 | return (-1); |
81 | while (fgets(buf, sizeof buf, f)) { | 116 | while ((p = fgetln(f, &len)) != NULL) { |
117 | if (p[len-1] == '\n') | ||
118 | len--; | ||
119 | if (len > sizeof(buf) - 2) | ||
120 | continue; | ||
121 | (void)memcpy(buf, p, len); | ||
122 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
123 | buf[len+1] = '\0'; | ||
82 | #ifdef YP | 124 | #ifdef YP |
83 | /* A + in the file means try YP now. */ | 125 | /* A + in the file means try YP now. */ |
84 | if (!strncmp(buf, "+\n", sizeof buf)) { | 126 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
85 | char *ypbuf, *ypdom; | 127 | char *ypbuf, *ypdom; |
86 | int ypbuflen; | 128 | int ypbuflen; |
87 | 129 | ||
@@ -93,42 +135,49 @@ ether_ntohost(hostname, e) | |||
93 | if (ether_line(ypbuf, &try, hostname) == 0) { | 135 | if (ether_line(ypbuf, &try, hostname) == 0) { |
94 | free(ypbuf); | 136 | free(ypbuf); |
95 | (void)fclose(f); | 137 | (void)fclose(f); |
96 | return 0; | 138 | return (0); |
97 | } | 139 | } |
98 | free(ypbuf); | 140 | free(ypbuf); |
99 | continue; | 141 | continue; |
100 | } | 142 | } |
101 | #endif | 143 | #endif |
102 | if (ether_line(buf, &try, hostname) == 0 && | 144 | if (ether_line(buf, &try, hostname) == 0 && |
103 | bcmp((char *)&try, (char *)e, sizeof try) == 0) { | 145 | memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { |
104 | (void)fclose(f); | 146 | (void)fclose(f); |
105 | return 0; | 147 | return (0); |
106 | } | 148 | } |
107 | } | 149 | } |
108 | (void)fclose(f); | 150 | (void)fclose(f); |
109 | errno = ENOENT; | 151 | errno = ENOENT; |
110 | return -1; | 152 | return (-1); |
111 | } | 153 | } |
112 | 154 | ||
113 | ether_hostton(hostname, e) | 155 | int |
114 | char *hostname; | 156 | ether_hostton(const char *hostname, struct ether_addr *e) |
115 | struct ether_addr *e; | ||
116 | { | 157 | { |
117 | FILE *f; | 158 | FILE *f; |
118 | char buf[BUFSIZ]; | 159 | char buf[BUFSIZ+1], *p; |
119 | char try[MAXHOSTNAMELEN]; | 160 | char try[MAXHOSTNAMELEN]; |
161 | size_t len; | ||
120 | #ifdef YP | 162 | #ifdef YP |
121 | int hostlen = strlen(hostname); | 163 | int hostlen = strlen(hostname); |
122 | #endif | 164 | #endif |
123 | 165 | ||
124 | f = fopen(_PATH_ETHERS, "r"); | 166 | f = fopen(_PATH_ETHERS, "r"); |
125 | if (f==NULL) | 167 | if (f==NULL) |
126 | return -1; | 168 | return (-1); |
127 | 169 | ||
128 | while (fgets(buf, sizeof buf, f)) { | 170 | while ((p = fgetln(f, &len)) != NULL) { |
171 | if (p[len-1] == '\n') | ||
172 | len--; | ||
173 | if (len > sizeof(buf) - 2) | ||
174 | continue; | ||
175 | memcpy(buf, p, len); | ||
176 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
177 | buf[len+1] = '\0'; | ||
129 | #ifdef YP | 178 | #ifdef YP |
130 | /* A + in the file means try YP now. */ | 179 | /* A + in the file means try YP now. */ |
131 | if (!strncmp(buf, "+\n", sizeof buf)) { | 180 | if (!strncmp(buf, "+\n", sizeof(buf))) { |
132 | char *ypbuf, *ypdom; | 181 | char *ypbuf, *ypdom; |
133 | int ypbuflen; | 182 | int ypbuflen; |
134 | 183 | ||
@@ -140,7 +189,7 @@ ether_hostton(hostname, e) | |||
140 | if (ether_line(ypbuf, e, try) == 0) { | 189 | if (ether_line(ypbuf, e, try) == 0) { |
141 | free(ypbuf); | 190 | free(ypbuf); |
142 | (void)fclose(f); | 191 | (void)fclose(f); |
143 | return 0; | 192 | return (0); |
144 | } | 193 | } |
145 | free(ypbuf); | 194 | free(ypbuf); |
146 | continue; | 195 | continue; |
@@ -148,31 +197,36 @@ ether_hostton(hostname, e) | |||
148 | #endif | 197 | #endif |
149 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | 198 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { |
150 | (void)fclose(f); | 199 | (void)fclose(f); |
151 | return 0; | 200 | return (0); |
152 | } | 201 | } |
153 | } | 202 | } |
154 | (void)fclose(f); | 203 | (void)fclose(f); |
155 | errno = ENOENT; | 204 | errno = ENOENT; |
156 | return -1; | 205 | return (-1); |
157 | } | 206 | } |
158 | 207 | ||
159 | ether_line(l, e, hostname) | 208 | int |
160 | char *l; | 209 | ether_line(const char *line, struct ether_addr *e, char *hostname) |
161 | struct ether_addr *e; | ||
162 | char *hostname; | ||
163 | { | 210 | { |
164 | u_int i[6]; | 211 | char *p; |
165 | 212 | size_t n; | |
166 | if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1], | 213 | |
167 | &i[2], &i[3], &i[4], &i[5], hostname) == 7) { | 214 | /* Parse "xx:xx:xx:xx:xx:xx" */ |
168 | e->ether_addr_octet[0] = (u_char)i[0]; | 215 | if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) |
169 | e->ether_addr_octet[1] = (u_char)i[1]; | 216 | goto bad; |
170 | e->ether_addr_octet[2] = (u_char)i[2]; | 217 | |
171 | e->ether_addr_octet[3] = (u_char)i[3]; | 218 | /* Now get the hostname */ |
172 | e->ether_addr_octet[4] = (u_char)i[4]; | 219 | while (isspace(*p)) |
173 | e->ether_addr_octet[5] = (u_char)i[5]; | 220 | p++; |
174 | return 0; | 221 | if (*p == '\0') |
175 | } | 222 | goto bad; |
223 | n = strcspn(p, " \t\n"); | ||
224 | if (n >= MAXHOSTNAMELEN) | ||
225 | goto bad; | ||
226 | strlcpy(hostname, p, n + 1); | ||
227 | return (0); | ||
228 | |||
229 | bad: | ||
176 | errno = EINVAL; | 230 | errno = EINVAL; |
177 | return -1; | 231 | return (-1); |
178 | } | 232 | } |