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