Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/KrbServiceLocator.java
41159 views
1
/*
2
* Copyright (c) 2006, 2021, 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 sun.security.krb5;
27
28
import sun.security.krb5.internal.Krb5;
29
30
import java.security.AccessController;
31
import java.security.PrivilegedActionException;
32
import java.security.PrivilegedExceptionAction;
33
import java.util.Arrays;
34
import java.util.Hashtable;
35
import java.util.Random;
36
import java.util.StringTokenizer;
37
38
import javax.naming.*;
39
import javax.naming.directory.*;
40
import javax.naming.spi.NamingManager;
41
42
/**
43
* This class discovers the location of Kerberos services by querying DNS,
44
* as defined in RFC 4120.
45
*
46
* @author Seema Malkani
47
* @since 1.7
48
*/
49
50
class KrbServiceLocator {
51
52
private static final String SRV_RR = "SRV";
53
private static final String[] SRV_RR_ATTR = new String[] {SRV_RR};
54
55
private static final String SRV_TXT = "TXT";
56
private static final String[] SRV_TXT_ATTR = new String[] {SRV_TXT};
57
58
private static final Random random = new Random();
59
60
private KrbServiceLocator() {
61
}
62
63
/**
64
* Locates the KERBEROS service for a given domain.
65
* Queries DNS for a list of KERBEROS Service Text Records (TXT) for a
66
* given domain name.
67
* Information on the mapping of DNS hostnames and domain names
68
* to Kerberos realms is stored using DNS TXT records
69
*
70
* @param realmName A string realm name.
71
* @return An ordered list of hostports for the Kerberos service or null if
72
* the service has not been located.
73
*/
74
@SuppressWarnings("removal")
75
static String[] getKerberosService(String realmName) {
76
77
// search realm in SRV TXT records
78
String dnsUrl = "dns:///_kerberos." + realmName;
79
String[] records = null;
80
try {
81
// Create the DNS context using NamingManager rather than using
82
// the initial context constructor. This avoids having the initial
83
// context constructor call itself (when processing the URL
84
// argument in the getAttributes call).
85
Context ctx = NamingManager.getURLContext("dns", new Hashtable<>(0));
86
if (!(ctx instanceof DirContext)) {
87
return null; // cannot create a DNS context
88
}
89
Attributes attrs = null;
90
try {
91
// both connect and accept are needed since DNS is thru UDP
92
attrs = AccessController.doPrivileged(
93
(PrivilegedExceptionAction<Attributes>)
94
() -> ((DirContext)ctx).getAttributes(
95
dnsUrl, SRV_TXT_ATTR),
96
null,
97
new java.net.SocketPermission("*", "connect,accept"));
98
} catch (PrivilegedActionException e) {
99
throw (NamingException)e.getCause();
100
}
101
Attribute attr;
102
103
if (attrs != null && ((attr = attrs.get(SRV_TXT)) != null)) {
104
int numValues = attr.size();
105
int numRecords = 0;
106
String[] txtRecords = new String[numValues];
107
108
// gather the text records
109
int i = 0;
110
int j = 0;
111
while (i < numValues) {
112
try {
113
txtRecords[j] = (String)attr.get(i);
114
j++;
115
} catch (Exception e) {
116
// ignore bad value
117
}
118
i++;
119
}
120
numRecords = j;
121
122
// trim
123
if (numRecords < numValues) {
124
String[] trimmed = new String[numRecords];
125
System.arraycopy(txtRecords, 0, trimmed, 0, numRecords);
126
records = trimmed;
127
} else {
128
records = txtRecords;
129
}
130
}
131
} catch (NamingException e) {
132
// ignore
133
}
134
return records;
135
}
136
137
/**
138
* Locates the KERBEROS service for a given domain.
139
* Queries DNS for a list of KERBEROS Service Location Records (SRV) for a
140
* given domain name.
141
*
142
* @param realmName A string realm name.
143
* @param protocol the protocol string, can be "_udp" or "_tcp"
144
* @return An ordered list of hostports for the Kerberos service or null if
145
* the service has not been located.
146
*/
147
@SuppressWarnings("removal")
148
static String[] getKerberosService(String realmName, String protocol) {
149
150
String dnsUrl = "dns:///_kerberos." + protocol + "." + realmName;
151
String[] hostports = null;
152
153
try {
154
// Create the DNS context using NamingManager rather than using
155
// the initial context constructor. This avoids having the initial
156
// context constructor call itself (when processing the URL
157
// argument in the getAttributes call).
158
Context ctx = NamingManager.getURLContext("dns", new Hashtable<>(0));
159
if (!(ctx instanceof DirContext)) {
160
return null; // cannot create a DNS context
161
}
162
163
Attributes attrs = null;
164
try {
165
// both connect and accept are needed since DNS is thru UDP
166
attrs = AccessController.doPrivileged(
167
(PrivilegedExceptionAction<Attributes>)
168
() -> ((DirContext)ctx).getAttributes(
169
dnsUrl, SRV_RR_ATTR),
170
null,
171
new java.net.SocketPermission("*", "connect,accept"));
172
} catch (PrivilegedActionException e) {
173
throw (NamingException)e.getCause();
174
}
175
176
Attribute attr;
177
178
if (attrs != null && ((attr = attrs.get(SRV_RR)) != null)) {
179
int numValues = attr.size();
180
int numRecords = 0;
181
SrvRecord[] srvRecords = new SrvRecord[numValues];
182
183
// create the service records
184
int i = 0;
185
int j = 0;
186
while (i < numValues) {
187
try {
188
srvRecords[j] = new SrvRecord((String) attr.get(i));
189
j++;
190
} catch (Exception e) {
191
// ignore bad value
192
}
193
i++;
194
}
195
numRecords = j;
196
197
// trim
198
if (numRecords < numValues) {
199
SrvRecord[] trimmed = new SrvRecord[numRecords];
200
System.arraycopy(srvRecords, 0, trimmed, 0, numRecords);
201
srvRecords = trimmed;
202
}
203
204
// Sort the service records in ascending order of their
205
// priority value. For records with equal priority, move
206
// those with weight 0 to the top of the list.
207
if (numRecords > 1) {
208
Arrays.sort(srvRecords);
209
}
210
211
// extract the host and port number from each service record
212
hostports = extractHostports(srvRecords);
213
}
214
} catch (NamingException e) {
215
// e.printStackTrace();
216
// ignore
217
}
218
return hostports;
219
}
220
221
/**
222
* Extract hosts and port numbers from a list of SRV records.
223
* An array of hostports is returned or null if none were found.
224
*/
225
private static String[] extractHostports(SrvRecord[] srvRecords) {
226
String[] hostports = null;
227
228
int head = 0;
229
int tail = 0;
230
int sublistLength = 0;
231
int k = 0;
232
for (int i = 0; i < srvRecords.length; i++) {
233
if (hostports == null) {
234
hostports = new String[srvRecords.length];
235
}
236
// find the head and tail of the list of records having the same
237
// priority value.
238
head = i;
239
while (i < srvRecords.length - 1 &&
240
srvRecords[i].priority == srvRecords[i + 1].priority) {
241
i++;
242
}
243
tail = i;
244
245
// select hostports from the sublist
246
sublistLength = (tail - head) + 1;
247
for (int j = 0; j < sublistLength; j++) {
248
hostports[k++] = selectHostport(srvRecords, head, tail);
249
}
250
}
251
return hostports;
252
}
253
254
/*
255
* Randomly select a service record in the range [head, tail] and return
256
* its hostport value. Follows the algorithm in RFC 2782.
257
*/
258
private static String selectHostport(SrvRecord[] srvRecords, int head,
259
int tail) {
260
if (head == tail) {
261
return srvRecords[head].hostport;
262
}
263
264
// compute the running sum for records between head and tail
265
int sum = 0;
266
for (int i = head; i <= tail; i++) {
267
if (srvRecords[i] != null) {
268
sum += srvRecords[i].weight;
269
srvRecords[i].sum = sum;
270
}
271
}
272
String hostport = null;
273
274
// If all records have zero weight, select first available one;
275
// otherwise, randomly select a record according to its weight
276
int target = (sum == 0 ? 0 : random.nextInt(sum + 1));
277
for (int i = head; i <= tail; i++) {
278
if (srvRecords[i] != null && srvRecords[i].sum >= target) {
279
hostport = srvRecords[i].hostport;
280
srvRecords[i] = null; // make this record unavailable
281
break;
282
}
283
}
284
return hostport;
285
}
286
287
/**
288
* This class holds a DNS service (SRV) record.
289
* See http://www.ietf.org/rfc/rfc2782.txt
290
*/
291
292
static class SrvRecord implements Comparable<SrvRecord> {
293
294
int priority;
295
int weight;
296
int sum;
297
String hostport;
298
299
/**
300
* Creates a service record object from a string record.
301
* DNS supplies the string record in the following format:
302
* <pre>
303
* <Priority> " " <Weight> " " <Port> " " <Host>
304
* </pre>
305
*/
306
SrvRecord(String srvRecord) throws Exception {
307
StringTokenizer tokenizer = new StringTokenizer(srvRecord, " ");
308
String port;
309
310
if (tokenizer.countTokens() == 4) {
311
priority = Integer.parseInt(tokenizer.nextToken());
312
weight = Integer.parseInt(tokenizer.nextToken());
313
port = tokenizer.nextToken();
314
hostport = tokenizer.nextToken() + ":" + port;
315
} else {
316
throw new IllegalArgumentException();
317
}
318
}
319
320
/*
321
* Sort records in ascending order of priority value. For records with
322
* equal priority move those with weight 0 to the top of the list.
323
*/
324
public int compareTo(SrvRecord that) {
325
if (priority > that.priority) {
326
return 1; // this > that
327
} else if (priority < that.priority) {
328
return -1; // this < that
329
} else if (weight == 0 && that.weight != 0) {
330
return -1; // this < that
331
} else if (weight != 0 && that.weight == 0) {
332
return 1; // this > that
333
} else {
334
return 0; // this == that
335
}
336
}
337
}
338
}
339
340