ethers.c revision 1.15 1 1.15 lukem /* $NetBSD: ethers.c,v 1.15 1999/09/16 11:45:11 lukem Exp $ */
2 1.5 cgd
3 1.1 deraadt /*
4 1.1 deraadt * ethers(3N) a la Sun.
5 1.1 deraadt *
6 1.1 deraadt * Written by Roland McGrath <roland (at) frob.com> 10/14/93.
7 1.1 deraadt * Public domain.
8 1.1 deraadt */
9 1.1 deraadt
10 1.10 jtc #include "namespace.h"
11 1.15 lukem #include <sys/param.h>
12 1.1 deraadt #include <sys/socket.h>
13 1.15 lukem
14 1.1 deraadt #include <net/if.h>
15 1.8 is #include <net/if_ether.h>
16 1.1 deraadt #include <netinet/in.h>
17 1.15 lukem
18 1.15 lukem #include <assert.h>
19 1.15 lukem #include <errno.h>
20 1.1 deraadt #include <paths.h>
21 1.1 deraadt #include <stdio.h>
22 1.3 jtc #include <stdlib.h>
23 1.3 jtc #include <string.h>
24 1.15 lukem
25 1.9 christos #ifdef YP
26 1.9 christos #include <rpcsvc/ypclnt.h>
27 1.10 jtc #endif
28 1.10 jtc
29 1.10 jtc #ifdef __weak_alias
30 1.10 jtc __weak_alias(ether_aton,_ether_aton);
31 1.10 jtc __weak_alias(ether_hostton,_ether_hostton);
32 1.10 jtc __weak_alias(ether_line,_ether_line);
33 1.10 jtc __weak_alias(ether_ntoa,_ether_ntoa);
34 1.10 jtc __weak_alias(ether_ntohost,_ether_ntohost);
35 1.9 christos #endif
36 1.1 deraadt
37 1.1 deraadt #ifndef _PATH_ETHERS
38 1.1 deraadt #define _PATH_ETHERS "/etc/ethers"
39 1.1 deraadt #endif
40 1.1 deraadt
41 1.1 deraadt char *
42 1.1 deraadt ether_ntoa(e)
43 1.1 deraadt struct ether_addr *e;
44 1.1 deraadt {
45 1.13 mycroft static char a[18];
46 1.1 deraadt
47 1.15 lukem _DIAGASSERT(e != NULL);
48 1.15 lukem #ifdef _DIAGNOSTIC
49 1.15 lukem if (e == NULL)
50 1.15 lukem return NULL;
51 1.15 lukem #endif
52 1.15 lukem
53 1.7 mrg snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x",
54 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
55 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
56 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
57 1.1 deraadt return a;
58 1.1 deraadt }
59 1.1 deraadt
60 1.1 deraadt struct ether_addr *
61 1.1 deraadt ether_aton(s)
62 1.11 lukem const char *s;
63 1.1 deraadt {
64 1.1 deraadt static struct ether_addr n;
65 1.1 deraadt u_int i[6];
66 1.1 deraadt
67 1.15 lukem _DIAGASSERT(s != NULL);
68 1.15 lukem #ifdef _DIAGNOSTIC
69 1.15 lukem if (s == NULL)
70 1.15 lukem return NULL;
71 1.15 lukem #endif
72 1.15 lukem
73 1.1 deraadt if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
74 1.1 deraadt &i[2], &i[3], &i[4], &i[5]) == 6) {
75 1.1 deraadt n.ether_addr_octet[0] = (u_char)i[0];
76 1.1 deraadt n.ether_addr_octet[1] = (u_char)i[1];
77 1.1 deraadt n.ether_addr_octet[2] = (u_char)i[2];
78 1.1 deraadt n.ether_addr_octet[3] = (u_char)i[3];
79 1.1 deraadt n.ether_addr_octet[4] = (u_char)i[4];
80 1.1 deraadt n.ether_addr_octet[5] = (u_char)i[5];
81 1.1 deraadt return &n;
82 1.1 deraadt }
83 1.1 deraadt return NULL;
84 1.1 deraadt }
85 1.1 deraadt
86 1.6 mikel int
87 1.1 deraadt ether_ntohost(hostname, e)
88 1.1 deraadt char *hostname;
89 1.1 deraadt struct ether_addr *e;
90 1.1 deraadt {
91 1.1 deraadt FILE *f;
92 1.11 lukem char *p;
93 1.12 mjacob size_t len;
94 1.1 deraadt struct ether_addr try;
95 1.1 deraadt #ifdef YP
96 1.1 deraadt char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
97 1.1 deraadt int trylen;
98 1.15 lukem #endif
99 1.1 deraadt
100 1.15 lukem _DIAGASSERT(hostname != NULL);
101 1.15 lukem _DIAGASSERT(e != NULL);
102 1.15 lukem #ifdef _DIAGNOSTIC
103 1.15 lukem if (hostname == NULL || e == NULL) {
104 1.15 lukem errno = EFAULT;
105 1.15 lukem return -1;
106 1.15 lukem }
107 1.15 lukem #endif
108 1.15 lukem
109 1.15 lukem #ifdef YP
110 1.11 lukem trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x",
111 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
112 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
113 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
114 1.1 deraadt #endif
115 1.1 deraadt
116 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
117 1.11 lukem if (f == NULL)
118 1.1 deraadt return -1;
119 1.11 lukem while ((p = fgetln(f, &len)) != NULL) {
120 1.11 lukem if (p[len - 1] != '\n')
121 1.11 lukem continue; /* skip lines w/o \n */
122 1.11 lukem p[--len] = '\0';
123 1.1 deraadt #ifdef YP
124 1.1 deraadt /* A + in the file means try YP now. */
125 1.11 lukem if (len == 1 && *p == '+') {
126 1.1 deraadt char *ypbuf, *ypdom;
127 1.1 deraadt int ypbuflen;
128 1.1 deraadt
129 1.1 deraadt if (yp_get_default_domain(&ypdom))
130 1.1 deraadt continue;
131 1.1 deraadt if (yp_match(ypdom, "ethers.byaddr", trybuf,
132 1.1 deraadt trylen, &ypbuf, &ypbuflen))
133 1.1 deraadt continue;
134 1.1 deraadt if (ether_line(ypbuf, &try, hostname) == 0) {
135 1.1 deraadt free(ypbuf);
136 1.1 deraadt (void)fclose(f);
137 1.1 deraadt return 0;
138 1.1 deraadt }
139 1.1 deraadt free(ypbuf);
140 1.2 deraadt continue;
141 1.1 deraadt }
142 1.1 deraadt #endif
143 1.11 lukem if (ether_line(p, &try, hostname) == 0 &&
144 1.14 christos memcmp(&try, e, sizeof try) == 0) {
145 1.1 deraadt (void)fclose(f);
146 1.1 deraadt return 0;
147 1.1 deraadt }
148 1.1 deraadt }
149 1.1 deraadt (void)fclose(f);
150 1.1 deraadt errno = ENOENT;
151 1.1 deraadt return -1;
152 1.1 deraadt }
153 1.1 deraadt
154 1.6 mikel int
155 1.1 deraadt ether_hostton(hostname, e)
156 1.11 lukem const char *hostname;
157 1.1 deraadt struct ether_addr *e;
158 1.1 deraadt {
159 1.1 deraadt FILE *f;
160 1.11 lukem char *p;
161 1.12 mjacob size_t len;
162 1.11 lukem char try[MAXHOSTNAMELEN + 1];
163 1.1 deraadt #ifdef YP
164 1.1 deraadt int hostlen = strlen(hostname);
165 1.1 deraadt #endif
166 1.1 deraadt
167 1.15 lukem _DIAGASSERT(hostname != NULL);
168 1.15 lukem _DIAGASSERT(e != NULL);
169 1.15 lukem #ifdef _DIAGNOSTIC
170 1.15 lukem if (hostname == NULL || e == NULL) {
171 1.15 lukem errno = EFAULT;
172 1.15 lukem return -1;
173 1.15 lukem }
174 1.15 lukem #endif
175 1.15 lukem
176 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
177 1.1 deraadt if (f==NULL)
178 1.1 deraadt return -1;
179 1.1 deraadt
180 1.11 lukem while ((p = fgetln(f, &len)) != NULL) {
181 1.11 lukem if (p[len - 1] != '\n')
182 1.11 lukem continue; /* skip lines w/o \n */
183 1.11 lukem p[--len] = '\0';
184 1.1 deraadt #ifdef YP
185 1.1 deraadt /* A + in the file means try YP now. */
186 1.11 lukem if (len == 1 && *p == '+') {
187 1.1 deraadt char *ypbuf, *ypdom;
188 1.1 deraadt int ypbuflen;
189 1.1 deraadt
190 1.1 deraadt if (yp_get_default_domain(&ypdom))
191 1.1 deraadt continue;
192 1.1 deraadt if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
193 1.1 deraadt &ypbuf, &ypbuflen))
194 1.1 deraadt continue;
195 1.1 deraadt if (ether_line(ypbuf, e, try) == 0) {
196 1.1 deraadt free(ypbuf);
197 1.1 deraadt (void)fclose(f);
198 1.1 deraadt return 0;
199 1.1 deraadt }
200 1.1 deraadt free(ypbuf);
201 1.2 deraadt continue;
202 1.1 deraadt }
203 1.1 deraadt #endif
204 1.11 lukem if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) {
205 1.1 deraadt (void)fclose(f);
206 1.1 deraadt return 0;
207 1.1 deraadt }
208 1.1 deraadt }
209 1.1 deraadt (void)fclose(f);
210 1.1 deraadt errno = ENOENT;
211 1.1 deraadt return -1;
212 1.1 deraadt }
213 1.1 deraadt
214 1.6 mikel int
215 1.1 deraadt ether_line(l, e, hostname)
216 1.11 lukem const char *l;
217 1.1 deraadt struct ether_addr *e;
218 1.1 deraadt char *hostname;
219 1.1 deraadt {
220 1.1 deraadt u_int i[6];
221 1.11 lukem static char buf[sizeof " %x:%x:%x:%x:%x:%x %s\\n" + 21];
222 1.11 lukem /* XXX: 21 == strlen (ASCII representation of 2^64) */
223 1.15 lukem
224 1.15 lukem _DIAGASSERT(l != NULL);
225 1.15 lukem _DIAGASSERT(e != NULL);
226 1.15 lukem _DIAGASSERT(hostname != NULL);
227 1.15 lukem #ifdef _DIAGNOSTIC
228 1.15 lukem if (l == NULL || e == NULL || hostname == NULL) {
229 1.15 lukem errno = EFAULT;
230 1.15 lukem return -1;
231 1.15 lukem }
232 1.15 lukem #endif
233 1.1 deraadt
234 1.11 lukem if (! buf[0])
235 1.11 lukem snprintf(buf, sizeof buf, " %%x:%%x:%%x:%%x:%%x:%%x %%%ds\\n",
236 1.11 lukem MAXHOSTNAMELEN);
237 1.11 lukem if (sscanf(l, buf,
238 1.11 lukem &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
239 1.1 deraadt e->ether_addr_octet[0] = (u_char)i[0];
240 1.1 deraadt e->ether_addr_octet[1] = (u_char)i[1];
241 1.1 deraadt e->ether_addr_octet[2] = (u_char)i[2];
242 1.1 deraadt e->ether_addr_octet[3] = (u_char)i[3];
243 1.1 deraadt e->ether_addr_octet[4] = (u_char)i[4];
244 1.1 deraadt e->ether_addr_octet[5] = (u_char)i[5];
245 1.1 deraadt return 0;
246 1.1 deraadt }
247 1.1 deraadt errno = EINVAL;
248 1.1 deraadt return -1;
249 1.1 deraadt }
250