author | aefimov |
Thu, 14 Nov 2019 23:16:40 +0000 | |
branch | aefimov-dns-client-branch |
changeset 59101 | 258033faefc9 |
parent 58870 | 35c438a6d45c |
permissions | -rw-r--r-- |
58870 | 1 |
/* |
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.dns.conf; |
|
27 |
||
28 |
import jdk.dns.client.internal.util.ReloadTracker; |
|
29 |
||
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.FileReader; |
|
32 |
import java.io.IOException; |
|
33 |
import java.nio.file.Paths; |
|
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
import java.security.PrivilegedExceptionAction; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.LinkedList; |
|
39 |
import java.util.List; |
|
40 |
import java.util.StringTokenizer; |
|
41 |
import java.util.concurrent.locks.ReadWriteLock; |
|
42 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
43 |
||
44 |
/* |
|
45 |
* An implementation of DnsResolverConfiguration for Solaris |
|
46 |
* and Linux. |
|
47 |
*/ |
|
48 |
||
49 |
public class DnsResolverConfiguration { |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
50 |
|
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
51 |
public static String getDefaultHostsFileLocation() { |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
52 |
return "/etc/hosts"; |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
53 |
} |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
54 |
|
58870 | 55 |
// Lock held whilst loading configuration or checking |
56 |
private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(); |
|
57 |
||
58 |
// |
|
59 |
private static String RESOLV_CONF_LOCATION = java.security.AccessController.doPrivileged( |
|
60 |
(PrivilegedAction<String>) () -> System.getProperty("jdk.dns.client.resolv.conf", "/etc/resolv.conf")); |
|
61 |
private static final ReloadTracker RESOLVE_CONF_TRACKER; |
|
62 |
||
63 |
||
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
64 |
// Cache timeout (300 seconds) in nano seconds - should be converted into property |
58870 | 65 |
// or configured as preference in the future. |
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
66 |
private static final long TIMEOUT = 300_000_000_000L; |
58870 | 67 |
|
68 |
// Parse /etc/resolv.conf to get the values for a particular |
|
69 |
// keyword. |
|
70 |
// |
|
71 |
private List<String> resolvconf(String keyword, |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
72 |
int maxperkeyword, |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
73 |
int maxkeywords) { |
58870 | 74 |
LinkedList<String> ll = new LinkedList<>(); |
75 |
||
76 |
try { |
|
77 |
BufferedReader in = |
|
78 |
new BufferedReader(new FileReader(RESOLV_CONF_LOCATION)); |
|
79 |
String line; |
|
80 |
while ((line = in.readLine()) != null) { |
|
81 |
int maxvalues = maxperkeyword; |
|
82 |
if (line.isEmpty()) |
|
83 |
continue; |
|
84 |
if (line.charAt(0) == '#' || line.charAt(0) == ';') |
|
85 |
continue; |
|
86 |
if (!line.startsWith(keyword)) |
|
87 |
continue; |
|
88 |
String value = line.substring(keyword.length()); |
|
89 |
if (value.isEmpty()) |
|
90 |
continue; |
|
91 |
if (value.charAt(0) != ' ' && value.charAt(0) != '\t') |
|
92 |
continue; |
|
93 |
StringTokenizer st = new StringTokenizer(value, " \t"); |
|
94 |
while (st.hasMoreTokens()) { |
|
95 |
String val = st.nextToken(); |
|
96 |
if (val.charAt(0) == '#' || val.charAt(0) == ';') { |
|
97 |
break; |
|
98 |
} |
|
99 |
if ("nameserver".equals(keyword)) { |
|
100 |
if (val.indexOf(':') >= 0 && |
|
101 |
val.indexOf('.') < 0 && // skip for IPv4 literals with port |
|
102 |
val.indexOf('[') < 0 && |
|
103 |
val.indexOf(']') < 0) { |
|
104 |
// IPv6 literal, in non-BSD-style. |
|
105 |
val = "[" + val + "]"; |
|
106 |
} |
|
107 |
} |
|
108 |
ll.add(val); |
|
109 |
if (--maxvalues == 0) { |
|
110 |
break; |
|
111 |
} |
|
112 |
} |
|
113 |
if (--maxkeywords == 0) { |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
in.close(); |
|
118 |
} catch (IOException ioe) { |
|
119 |
// problem reading value |
|
120 |
} |
|
121 |
||
122 |
return Collections.unmodifiableList(ll); |
|
123 |
} |
|
124 |
||
125 |
private volatile List<String> searchlist = Collections.emptyList(); |
|
126 |
private volatile List<String> nameservers = Collections.emptyList(); |
|
127 |
private volatile String domain = ""; |
|
128 |
||
129 |
||
130 |
// Load DNS configuration from OS |
|
131 |
private void loadConfig() { |
|
132 |
LOCK.readLock().lock(); |
|
133 |
try { |
|
134 |
var rs = RESOLVE_CONF_TRACKER.getReloadStatus(); |
|
135 |
if (!rs.isReloadNeeded() || !rs.isFileExists()) { |
|
136 |
return; |
|
137 |
} |
|
138 |
} finally { |
|
139 |
LOCK.readLock().unlock(); |
|
140 |
} |
|
141 |
||
142 |
LOCK.writeLock().lock(); |
|
143 |
try { |
|
144 |
var rs = RESOLVE_CONF_TRACKER.getReloadStatus(); |
|
145 |
// Check if reload is needed again |
|
146 |
if (rs.isReloadNeeded()) { |
|
147 |
if (rs.isFileExists()) { |
|
148 |
// get the name servers from /etc/resolv.conf |
|
149 |
nameservers = |
|
150 |
java.security.AccessController.doPrivileged( |
|
151 |
(PrivilegedAction<List<String>>) () -> { |
|
152 |
// typically MAXNS is 3 but we've picked 5 here |
|
153 |
// to allow for additional servers if required. |
|
154 |
return resolvconf("nameserver", 1, 5); |
|
155 |
}); |
|
156 |
||
157 |
// get the search list (or domain) |
|
158 |
searchlist = getSearchList(); |
|
159 |
||
160 |
// update the timestamp on the configuration |
|
161 |
RESOLVE_CONF_TRACKER.updateTimestamps(rs); |
|
162 |
} else { |
|
163 |
nameservers = Collections.emptyList(); |
|
164 |
searchlist = Collections.emptyList(); |
|
165 |
} |
|
166 |
} |
|
167 |
} finally { |
|
168 |
LOCK.writeLock().unlock(); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
// obtain search list or local domain |
|
173 |
private List<String> getSearchList() { |
|
174 |
||
175 |
List<String> sl; |
|
176 |
||
177 |
// first try the search keyword in /etc/resolv.conf |
|
178 |
||
179 |
/* run */ |
|
180 |
sl = java.security.AccessController.doPrivileged( |
|
181 |
(PrivilegedAction<List<String>>) () -> { |
|
182 |
List<String> ll; |
|
183 |
||
184 |
// first try search keyword (max 6 domains) |
|
185 |
ll = resolvconf("search", 6, 1); |
|
186 |
if (ll.size() > 0) { |
|
187 |
domain = ll.get(0); |
|
188 |
return ll; |
|
189 |
} |
|
190 |
||
191 |
return null; |
|
192 |
||
193 |
}); |
|
194 |
if (sl != null) { |
|
195 |
return sl; |
|
196 |
} |
|
197 |
||
198 |
// No search keyword so use local domain |
|
199 |
||
200 |
||
201 |
// LOCALDOMAIN has absolute priority on Solaris |
|
202 |
||
203 |
String localDomain = localDomain0(); |
|
204 |
if (localDomain != null && !localDomain.isEmpty()) { |
|
205 |
sl = new LinkedList<>(); |
|
206 |
sl.add(localDomain); |
|
207 |
return Collections.unmodifiableList(sl); |
|
208 |
} |
|
209 |
||
210 |
// try domain keyword in /etc/resolv.conf |
|
211 |
||
212 |
/* run */ |
|
213 |
sl = java.security.AccessController.doPrivileged( |
|
214 |
(PrivilegedAction<List<String>>) () -> { |
|
215 |
List<String> ll; |
|
216 |
||
217 |
ll = resolvconf("domain", 1, 1); |
|
218 |
if (ll.size() > 0) { |
|
219 |
domain = ll.get(0); |
|
220 |
return ll; |
|
221 |
} |
|
222 |
return null; |
|
223 |
||
224 |
}); |
|
225 |
if (sl != null) { |
|
226 |
// sl is already UnmodifiableList |
|
227 |
return sl; |
|
228 |
} |
|
229 |
||
230 |
// no local domain so try fallback (RPC) domain or |
|
231 |
// hostName |
|
232 |
||
233 |
sl = new LinkedList<>(); |
|
234 |
String domain = fallbackDomain0(); |
|
235 |
if (domain != null && !domain.isEmpty()) { |
|
236 |
sl.add(domain); |
|
237 |
} |
|
238 |
||
239 |
return Collections.unmodifiableList(sl); |
|
240 |
} |
|
241 |
||
242 |
||
243 |
// ---- |
|
244 |
||
245 |
public DnsResolverConfiguration() { |
|
246 |
} |
|
247 |
||
248 |
@SuppressWarnings("unchecked") |
|
249 |
public List<String> searchlist() { |
|
250 |
loadConfig(); |
|
251 |
// List is unmodifiable |
|
252 |
return searchlist; |
|
253 |
} |
|
254 |
||
255 |
@SuppressWarnings("unchecked") |
|
256 |
public List<String> nameservers() { |
|
257 |
loadConfig(); |
|
258 |
// List is unmodifiable |
|
259 |
return nameservers; |
|
260 |
} |
|
261 |
||
262 |
public String domain() { |
|
263 |
loadConfig(); |
|
264 |
return domain; |
|
265 |
} |
|
266 |
||
267 |
// --- Native methods -- |
|
268 |
||
269 |
static native String localDomain0(); |
|
270 |
||
271 |
static native String fallbackDomain0(); |
|
272 |
||
273 |
static { |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
274 |
var pa = (PrivilegedAction<Void>) () -> { |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
275 |
System.loadLibrary("resolver"); |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
276 |
return null; |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58870
diff
changeset
|
277 |
}; |
58870 | 278 |
if (System.getSecurityManager() == null) { |
279 |
pa.run(); |
|
280 |
} else { |
|
281 |
AccessController.doPrivileged(pa); |
|
282 |
} |
|
283 |
var pea = (PrivilegedExceptionAction<ReloadTracker>) () -> |
|
284 |
ReloadTracker.newInstance(Paths.get(RESOLV_CONF_LOCATION), TIMEOUT); |
|
285 |
// TODO: Revisit |
|
286 |
try { |
|
287 |
RESOLVE_CONF_TRACKER = System.getSecurityManager() == null ? pea.run() |
|
288 |
: AccessController.doPrivileged(pea); |
|
289 |
} catch (Exception e) { |
|
290 |
throw new RuntimeException("Can't instantiate resolver configuration file tracker", e); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
} |