1 | /*- |
---|
2 | * Copyright (c) 1994, Garrett Wollman |
---|
3 | * |
---|
4 | * Redistribution and use in source and binary forms, with or without |
---|
5 | * modification, are permitted provided that the following conditions |
---|
6 | * are met: |
---|
7 | * 1. Redistributions of source code must retain the above copyright |
---|
8 | * notice, this list of conditions and the following disclaimer. |
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer in the |
---|
11 | * documentation and/or other materials provided with the distribution. |
---|
12 | * |
---|
13 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND |
---|
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
16 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
23 | * SUCH DAMAGE. |
---|
24 | */ |
---|
25 | |
---|
26 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
27 | static char rcsid[] = "$Id$"; |
---|
28 | #endif /* LIBC_SCCS and not lint */ |
---|
29 | |
---|
30 | /* Since we compile with strict ANSI we need to undef it to get |
---|
31 | * prototypes for extensions |
---|
32 | */ |
---|
33 | #undef __STRICT_ANSI__ |
---|
34 | |
---|
35 | #include <sys/param.h> |
---|
36 | #include <sys/socket.h> |
---|
37 | #include <netinet/in.h> |
---|
38 | #include <arpa/inet.h> |
---|
39 | #include <netdb.h> |
---|
40 | #include <stdio.h> |
---|
41 | #include <ctype.h> |
---|
42 | #include <errno.h> |
---|
43 | #include <string.h> |
---|
44 | |
---|
45 | #ifndef _PATH_NETCONF |
---|
46 | #define _PATH_NETCONF "/etc/host.conf" |
---|
47 | #endif |
---|
48 | |
---|
49 | enum service_type { |
---|
50 | SERVICE_NONE = 0, |
---|
51 | SERVICE_BIND, |
---|
52 | SERVICE_TABLE, |
---|
53 | SERVICE_NIS }; |
---|
54 | #define SERVICE_MAX SERVICE_NIS |
---|
55 | |
---|
56 | static struct { |
---|
57 | const char *name; |
---|
58 | enum service_type type; |
---|
59 | } service_names[] = { |
---|
60 | { "hosts", SERVICE_TABLE }, |
---|
61 | { "/etc/hosts", SERVICE_TABLE }, |
---|
62 | { "hosttable", SERVICE_TABLE }, |
---|
63 | { "htable", SERVICE_TABLE }, |
---|
64 | { "bind", SERVICE_BIND }, |
---|
65 | { "dns", SERVICE_BIND }, |
---|
66 | { "domain", SERVICE_BIND }, |
---|
67 | { "yp", SERVICE_NIS }, |
---|
68 | { "yellowpages", SERVICE_NIS }, |
---|
69 | { "nis", SERVICE_NIS }, |
---|
70 | { 0, SERVICE_NONE } |
---|
71 | }; |
---|
72 | |
---|
73 | static enum service_type service_order[SERVICE_MAX + 1]; |
---|
74 | static int service_done = 0; |
---|
75 | |
---|
76 | static enum service_type |
---|
77 | get_service_name(const char *name) { |
---|
78 | int i; |
---|
79 | for(i = 0; service_names[i].type != SERVICE_NONE; i++) { |
---|
80 | if(!strcasecmp(name, service_names[i].name)) { |
---|
81 | return service_names[i].type; |
---|
82 | } |
---|
83 | } |
---|
84 | return SERVICE_NONE; |
---|
85 | } |
---|
86 | |
---|
87 | static void |
---|
88 | init_services() |
---|
89 | { |
---|
90 | char *cp, *p, buf[BUFSIZ]; |
---|
91 | register int cc = 0; |
---|
92 | FILE *fd; |
---|
93 | |
---|
94 | if ((fd = (FILE *)fopen(_PATH_NETCONF, "r")) == NULL) { |
---|
95 | /* make some assumptions */ |
---|
96 | service_order[0] = SERVICE_TABLE; |
---|
97 | service_order[1] = SERVICE_NONE; |
---|
98 | } else { |
---|
99 | while (fgets(buf, BUFSIZ, fd) != NULL && cc < SERVICE_MAX) { |
---|
100 | if(buf[0] == '#') |
---|
101 | continue; |
---|
102 | |
---|
103 | p = buf; |
---|
104 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') |
---|
105 | ; |
---|
106 | if (cp == NULL) |
---|
107 | continue; |
---|
108 | do { |
---|
109 | if (isalpha(cp[0])) { |
---|
110 | service_order[cc] = get_service_name(cp); |
---|
111 | if(service_order[cc] != SERVICE_NONE) |
---|
112 | cc++; |
---|
113 | } |
---|
114 | while ((cp = strsep(&p, "\n \t,:;")) != NULL && *cp == '\0') |
---|
115 | ; |
---|
116 | } while(cp != NULL && cc < SERVICE_MAX); |
---|
117 | } |
---|
118 | service_order[cc] = SERVICE_NONE; |
---|
119 | fclose(fd); |
---|
120 | } |
---|
121 | service_done = 1; |
---|
122 | } |
---|
123 | |
---|
124 | struct netent * |
---|
125 | getnetbyname(const char *name) |
---|
126 | { |
---|
127 | struct netent *hp = 0; |
---|
128 | int nserv = 0; |
---|
129 | |
---|
130 | if (!service_done) |
---|
131 | init_services(); |
---|
132 | |
---|
133 | while (!hp) { |
---|
134 | switch (service_order[nserv]) { |
---|
135 | case SERVICE_NONE: |
---|
136 | return NULL; |
---|
137 | case SERVICE_TABLE: |
---|
138 | hp = _getnetbyhtname(name); |
---|
139 | break; |
---|
140 | case SERVICE_BIND: |
---|
141 | hp = _getnetbydnsname(name); |
---|
142 | break; |
---|
143 | case SERVICE_NIS: |
---|
144 | hp = _getnetbynisname(name); |
---|
145 | break; |
---|
146 | } |
---|
147 | nserv++; |
---|
148 | } |
---|
149 | return hp; |
---|
150 | } |
---|
151 | |
---|
152 | struct netent * |
---|
153 | getnetbyaddr(addr, af) |
---|
154 | u_long addr; |
---|
155 | int af; |
---|
156 | { |
---|
157 | struct netent *hp = 0; |
---|
158 | int nserv = 0; |
---|
159 | |
---|
160 | if (!service_done) |
---|
161 | init_services(); |
---|
162 | |
---|
163 | while (!hp) { |
---|
164 | switch (service_order[nserv]) { |
---|
165 | case SERVICE_NONE: |
---|
166 | return 0; |
---|
167 | case SERVICE_TABLE: |
---|
168 | hp = _getnetbyhtaddr(addr, af); |
---|
169 | break; |
---|
170 | case SERVICE_BIND: |
---|
171 | hp = _getnetbydnsaddr(addr, af); |
---|
172 | break; |
---|
173 | case SERVICE_NIS: |
---|
174 | hp = _getnetbynisaddr(addr, af); |
---|
175 | break; |
---|
176 | } |
---|
177 | nserv++; |
---|
178 | } |
---|
179 | return hp; |
---|
180 | } |
---|
181 | |
---|
182 | void |
---|
183 | setnetent(stayopen) |
---|
184 | int stayopen; |
---|
185 | { |
---|
186 | _setnethtent(stayopen); |
---|
187 | _setnetdnsent(stayopen); |
---|
188 | } |
---|
189 | |
---|
190 | void |
---|
191 | endnetent() |
---|
192 | { |
---|
193 | _endnethtent(); |
---|
194 | _endnetdnsent(); |
---|
195 | } |
---|