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