ethers.c revision 1.5.4.1 1 1.5.4.1 jtc /* $NetBSD: ethers.c,v 1.5.4.1 1996/09/20 17:00:23 jtc 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.5.4.1 jtc #include "namespace.h"
11 1.1 deraadt #include <sys/types.h>
12 1.1 deraadt #include <sys/socket.h>
13 1.1 deraadt #include <net/if.h>
14 1.1 deraadt #include <netinet/in.h>
15 1.1 deraadt #include <netinet/if_ether.h>
16 1.1 deraadt #include <sys/param.h>
17 1.1 deraadt #include <paths.h>
18 1.1 deraadt #include <errno.h>
19 1.1 deraadt #include <stdio.h>
20 1.3 jtc #include <stdlib.h>
21 1.3 jtc #include <string.h>
22 1.1 deraadt
23 1.5.4.1 jtc #ifdef __weak_alias
24 1.5.4.1 jtc __weak_alias(ether_aton,_ether_aton);
25 1.5.4.1 jtc __weak_alias(ether_hostton,_ether_hostton);
26 1.5.4.1 jtc __weak_alias(ether_line,_ether_line);
27 1.5.4.1 jtc __weak_alias(ether_ntoa,_ether_ntoa);
28 1.5.4.1 jtc __weak_alias(ether_ntohost,_ether_ntohost);
29 1.5.4.1 jtc #endif
30 1.5.4.1 jtc
31 1.1 deraadt #ifndef _PATH_ETHERS
32 1.1 deraadt #define _PATH_ETHERS "/etc/ethers"
33 1.1 deraadt #endif
34 1.1 deraadt
35 1.1 deraadt char *
36 1.1 deraadt ether_ntoa(e)
37 1.1 deraadt struct ether_addr *e;
38 1.1 deraadt {
39 1.1 deraadt static char a[] = "xx:xx:xx:xx:xx:xx";
40 1.1 deraadt
41 1.1 deraadt sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x",
42 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
43 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
44 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
45 1.1 deraadt return a;
46 1.1 deraadt }
47 1.1 deraadt
48 1.1 deraadt struct ether_addr *
49 1.1 deraadt ether_aton(s)
50 1.1 deraadt char *s;
51 1.1 deraadt {
52 1.1 deraadt static struct ether_addr n;
53 1.1 deraadt u_int i[6];
54 1.1 deraadt
55 1.1 deraadt if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1],
56 1.1 deraadt &i[2], &i[3], &i[4], &i[5]) == 6) {
57 1.1 deraadt n.ether_addr_octet[0] = (u_char)i[0];
58 1.1 deraadt n.ether_addr_octet[1] = (u_char)i[1];
59 1.1 deraadt n.ether_addr_octet[2] = (u_char)i[2];
60 1.1 deraadt n.ether_addr_octet[3] = (u_char)i[3];
61 1.1 deraadt n.ether_addr_octet[4] = (u_char)i[4];
62 1.1 deraadt n.ether_addr_octet[5] = (u_char)i[5];
63 1.1 deraadt return &n;
64 1.1 deraadt }
65 1.1 deraadt return NULL;
66 1.1 deraadt }
67 1.1 deraadt
68 1.1 deraadt ether_ntohost(hostname, e)
69 1.1 deraadt char *hostname;
70 1.1 deraadt struct ether_addr *e;
71 1.1 deraadt {
72 1.1 deraadt FILE *f;
73 1.1 deraadt char buf[BUFSIZ];
74 1.1 deraadt struct ether_addr try;
75 1.1 deraadt
76 1.1 deraadt #ifdef YP
77 1.1 deraadt char trybuf[sizeof "xx:xx:xx:xx:xx:xx"];
78 1.1 deraadt int trylen;
79 1.1 deraadt
80 1.1 deraadt sprintf(trybuf, "%x:%x:%x:%x:%x:%x",
81 1.1 deraadt e->ether_addr_octet[0], e->ether_addr_octet[1],
82 1.1 deraadt e->ether_addr_octet[2], e->ether_addr_octet[3],
83 1.1 deraadt e->ether_addr_octet[4], e->ether_addr_octet[5]);
84 1.1 deraadt trylen = strlen(trybuf);
85 1.1 deraadt #endif
86 1.1 deraadt
87 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
88 1.1 deraadt if (f==NULL)
89 1.1 deraadt return -1;
90 1.1 deraadt while (fgets(buf, sizeof buf, f)) {
91 1.1 deraadt #ifdef YP
92 1.1 deraadt /* A + in the file means try YP now. */
93 1.1 deraadt if (!strncmp(buf, "+\n", sizeof buf)) {
94 1.1 deraadt char *ypbuf, *ypdom;
95 1.1 deraadt int ypbuflen;
96 1.1 deraadt
97 1.1 deraadt if (yp_get_default_domain(&ypdom))
98 1.1 deraadt continue;
99 1.1 deraadt if (yp_match(ypdom, "ethers.byaddr", trybuf,
100 1.1 deraadt trylen, &ypbuf, &ypbuflen))
101 1.1 deraadt continue;
102 1.1 deraadt if (ether_line(ypbuf, &try, hostname) == 0) {
103 1.1 deraadt free(ypbuf);
104 1.1 deraadt (void)fclose(f);
105 1.1 deraadt return 0;
106 1.1 deraadt }
107 1.1 deraadt free(ypbuf);
108 1.2 deraadt continue;
109 1.1 deraadt }
110 1.1 deraadt #endif
111 1.1 deraadt if (ether_line(buf, &try, hostname) == 0 &&
112 1.5.4.1 jtc memcmp((char *)&try, (char *)e, sizeof try) == 0) {
113 1.1 deraadt (void)fclose(f);
114 1.1 deraadt return 0;
115 1.1 deraadt }
116 1.1 deraadt }
117 1.1 deraadt (void)fclose(f);
118 1.1 deraadt errno = ENOENT;
119 1.1 deraadt return -1;
120 1.1 deraadt }
121 1.1 deraadt
122 1.1 deraadt ether_hostton(hostname, e)
123 1.1 deraadt char *hostname;
124 1.1 deraadt struct ether_addr *e;
125 1.1 deraadt {
126 1.1 deraadt FILE *f;
127 1.1 deraadt char buf[BUFSIZ];
128 1.1 deraadt char try[MAXHOSTNAMELEN];
129 1.1 deraadt #ifdef YP
130 1.1 deraadt int hostlen = strlen(hostname);
131 1.1 deraadt #endif
132 1.1 deraadt
133 1.1 deraadt f = fopen(_PATH_ETHERS, "r");
134 1.1 deraadt if (f==NULL)
135 1.1 deraadt return -1;
136 1.1 deraadt
137 1.1 deraadt while (fgets(buf, sizeof buf, f)) {
138 1.1 deraadt #ifdef YP
139 1.1 deraadt /* A + in the file means try YP now. */
140 1.1 deraadt if (!strncmp(buf, "+\n", sizeof buf)) {
141 1.1 deraadt char *ypbuf, *ypdom;
142 1.1 deraadt int ypbuflen;
143 1.1 deraadt
144 1.1 deraadt if (yp_get_default_domain(&ypdom))
145 1.1 deraadt continue;
146 1.1 deraadt if (yp_match(ypdom, "ethers.byname", hostname, hostlen,
147 1.1 deraadt &ypbuf, &ypbuflen))
148 1.1 deraadt continue;
149 1.1 deraadt if (ether_line(ypbuf, e, try) == 0) {
150 1.1 deraadt free(ypbuf);
151 1.1 deraadt (void)fclose(f);
152 1.1 deraadt return 0;
153 1.1 deraadt }
154 1.1 deraadt free(ypbuf);
155 1.2 deraadt continue;
156 1.1 deraadt }
157 1.1 deraadt #endif
158 1.1 deraadt if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) {
159 1.1 deraadt (void)fclose(f);
160 1.1 deraadt return 0;
161 1.1 deraadt }
162 1.1 deraadt }
163 1.1 deraadt (void)fclose(f);
164 1.1 deraadt errno = ENOENT;
165 1.1 deraadt return -1;
166 1.1 deraadt }
167 1.1 deraadt
168 1.1 deraadt ether_line(l, e, hostname)
169 1.1 deraadt char *l;
170 1.1 deraadt struct ether_addr *e;
171 1.1 deraadt char *hostname;
172 1.1 deraadt {
173 1.1 deraadt u_int i[6];
174 1.1 deraadt
175 1.1 deraadt if (sscanf(l, " %x:%x:%x:%x:%x:%x %s\n", &i[0], &i[1],
176 1.1 deraadt &i[2], &i[3], &i[4], &i[5], hostname) == 7) {
177 1.1 deraadt e->ether_addr_octet[0] = (u_char)i[0];
178 1.1 deraadt e->ether_addr_octet[1] = (u_char)i[1];
179 1.1 deraadt e->ether_addr_octet[2] = (u_char)i[2];
180 1.1 deraadt e->ether_addr_octet[3] = (u_char)i[3];
181 1.1 deraadt e->ether_addr_octet[4] = (u_char)i[4];
182 1.1 deraadt e->ether_addr_octet[5] = (u_char)i[5];
183 1.1 deraadt return 0;
184 1.1 deraadt }
185 1.1 deraadt errno = EINVAL;
186 1.1 deraadt return -1;
187 1.1 deraadt }
188