/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This class discovers the location of Kerberos services by querying DNS,
* as defined in RFC 4120.
*
* @author Seema Malkani
* @since 1.7
*/
class KrbServiceLocator {
private KrbServiceLocator() {
}
/**
* Locates the KERBEROS service for a given domain.
* Queries DNS for a list of KERBEROS Service Text Records (TXT) for a
* given domain name.
* Information on the mapping of DNS hostnames and domain names
* to Kerberos realms is stored using DNS TXT records
*
* @param domainName A string domain name.
* @param environment The possibly null environment of the context.
* @return An ordered list of hostports for the Kerberos service or null if
* the service has not been located.
*/
// search realm in SRV TXT records
try {
// Create the DNS context using NamingManager rather than using
// the initial context constructor. This avoids having the initial
// context constructor call itself (when processing the URL
// argument in the getAttributes call).
if (!(ctx instanceof DirContext)) {
return null; // cannot create a DNS context
}
int numRecords = 0;
// gather the text records
int i = 0;
int j = 0;
while (i < numValues) {
try {
j++;
} catch (Exception e) {
// ignore bad value
}
i++;
}
numRecords = j;
// trim
if (numRecords < numValues) {
} else {
}
}
} catch (NamingException e) {
// ignore
}
return records;
}
/**
* Locates the KERBEROS service for a given domain.
* Queries DNS for a list of KERBEROS Service Location Records (SRV) for a
* given domain name.
*
* @param domainName A string domain name.
* @return An ordered list of hostports for the Kerberos service or null if
* the service has not been located.
*/
try {
// Create the DNS context using NamingManager rather than using
// the initial context constructor. This avoids having the initial
// context constructor call itself (when processing the URL
// argument in the getAttributes call).
if (!(ctx instanceof DirContext)) {
return null; // cannot create a DNS context
}
int numRecords = 0;
// create the service records
int i = 0;
int j = 0;
while (i < numValues) {
try {
j++;
} catch (Exception e) {
// ignore bad value
}
i++;
}
numRecords = j;
// trim
if (numRecords < numValues) {
}
// Sort the service records in ascending order of their
// priority value. For records with equal priority, move
// those with weight 0 to the top of the list.
if (numRecords > 1) {
}
// extract the host and port number from each service record
}
} catch (NamingException e) {
// e.printStackTrace();
// ignore
}
return hostports;
}
/**
* Extract hosts and port numbers from a list of SRV records.
* An array of hostports is returned or null if none were found.
*/
int head = 0;
int tail = 0;
int sublistLength = 0;
int k = 0;
}
// find the head and tail of the list of records having the same
// priority value.
head = i;
i++;
}
tail = i;
// select hostports from the sublist
for (int j = 0; j < sublistLength; j++) {
}
}
return hostports;
}
/*
* Randomly select a service record in the range [head, tail] and return
* its hostport value. Follows the algorithm in RFC 2782.
*/
int tail) {
}
// compute the running sum for records between head and tail
int sum = 0;
if (srvRecords[i] != null) {
}
}
// If all records have zero weight, select first available one;
// otherwise, randomly select a record according to its weight
break;
}
}
return hostport;
}
/**
* This class holds a DNS service (SRV) record.
*/
int priority;
int weight;
int sum;
/**
* Creates a service record object from a string record.
* DNS supplies the string record in the following format:
* <pre>
* <Priority> " " <Weight> " " <Port> " " <Host>
* </pre>
*/
} else {
throw new IllegalArgumentException();
}
}
/*
* Sort records in ascending order of priority value. For records with
* equal priority move those with weight 0 to the top of the list.
*/
return 1; // this > that
return -1; // this < that
return -1; // this < that
return 1; // this > that
} else {
return 0; // this == that
}
}
}
}