Skip to content

Commit c14de57

Browse files
committed
Canonical hostnames in orb, realm adapter. isLocal extended
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
1 parent 49282b8 commit c14de57

8 files changed

Lines changed: 57 additions & 65 deletions

File tree

appserver/orb/orb-iiop/src/main/java/org/glassfish/enterprise/iiop/impl/OrbCreator.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.sun.corba.ee.spi.misc.ORBConstants;
2525
import com.sun.corba.ee.spi.orb.ORB;
2626
import com.sun.enterprise.module.HK2Module;
27-
import com.sun.enterprise.util.net.NetUtils;
2827

2928
import java.lang.System.Logger;
3029
import java.util.ArrayList;
@@ -35,6 +34,7 @@
3534

3635
import org.glassfish.api.admin.ProcessEnvironment.ProcessType;
3736
import org.glassfish.external.amx.AMXGlassfish;
37+
import org.glassfish.internal.api.ORBLocator;
3838
import org.glassfish.orb.admin.config.IiopListener;
3939
import org.glassfish.orb.admin.config.Orb;
4040
import org.jvnet.hk2.config.types.Property;
@@ -82,8 +82,6 @@ final class OrbCreator {
8282
private static final String SUN_GIOP_DEFAULT_FRAGMENT_SIZE = "8192";
8383
private static final String SUN_GIOP_DEFAULT_BUFFER_SIZE = "8192";
8484

85-
private static final String DEFAULT_ORB_INIT_HOST = NetUtils.getHostName();
86-
8785
// This will only apply for stand-alone java clients, since
8886
// in the server the orb port comes from domain.xml, and in an appclient
8987
// the port is set from the sun-acc.xml. It's set to the same
@@ -362,16 +360,7 @@ static String evaluateInitialHost(Properties props, List<IiopListener> listeners
362360
initialHost = listener.getAddress();
363361
}
364362
}
365-
return initialHost == null ? DEFAULT_ORB_INIT_HOST : replaceAnyWithLocalHost(initialHost);
366-
}
367-
368-
private static String replaceAnyWithLocalHost(String orbInitialHost) {
369-
if (orbInitialHost.equals("0.0.0.0")
370-
|| orbInitialHost.equals("::")
371-
|| orbInitialHost.equals("::ffff:0.0.0.0")) {
372-
return DEFAULT_ORB_INIT_HOST;
373-
}
374-
return orbInitialHost;
363+
return ORBLocator.toConcreteHost(initialHost);
375364
}
376365

377366

appserver/orb/orb-iiop/src/main/java/org/glassfish/enterprise/iiop/impl/PEORBConfigurator.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,8 @@
3030
import com.sun.corba.ee.spi.transport.Acceptor;
3131
import com.sun.corba.ee.spi.transport.TransportDefault;
3232
import com.sun.corba.ee.spi.transport.TransportManager;
33-
import com.sun.enterprise.util.net.NetUtils;
3433

3534
import java.lang.System.Logger;
36-
import java.util.Arrays;
37-
import java.util.HashSet;
38-
import java.util.Set;
3935
import java.util.stream.Collectors;
4036
import java.util.stream.Stream;
4137

@@ -49,6 +45,7 @@
4945
import org.glassfish.external.probe.provider.StatsProviderManager;
5046
import org.glassfish.grizzly.config.dom.Ssl;
5147
import org.glassfish.internal.api.Globals;
48+
import org.glassfish.internal.api.ORBLocator;
5249
import org.glassfish.orb.admin.config.IiopListener;
5350
import org.glassfish.pfl.dynamic.copyobject.spi.ObjectCopierFactory ;
5451

@@ -62,8 +59,6 @@ public class PEORBConfigurator implements ORBConfigurator {
6259
private static final String SSL = "SSL";
6360
private static final String SSL_MUTUALAUTH = "SSL_MUTUALAUTH";
6461
private static final String IIOP_CLEAR_TEXT_CONNECTION = "IIOP_CLEAR_TEXT";
65-
private static final String DEFAULT_ORB_INIT_HOST = NetUtils.getHostName();
66-
private static final Set<String> ANY_ADDRS = new HashSet<>(Arrays.asList("0.0.0.0", "::", "::ffff:0.0.0.0"));
6762

6863
@Override
6964
public synchronized void configure(DataCollector dc, ORB orb) {
@@ -131,7 +126,7 @@ private void createORBListeners(IIOPUtils iiopUtils, IiopListener[] iiopListener
131126
}
132127

133128
boolean isLazy = Boolean.parseBoolean(iiopListener.getLazyInit());
134-
String host = handleAddrAny(iiopListener.getAddress());
129+
String host = ORBLocator.toConcreteHost(iiopListener.getAddress());
135130
int port = Integer.parseInt(iiopListener.getPort());
136131
boolean isSslListener = Boolean.parseBoolean(iiopListener.getSecurityEnabled()) && iiopListener.getSsl() != null;
137132
if (isSslListener) {
@@ -148,13 +143,6 @@ private void createORBListeners(IIOPUtils iiopUtils, IiopListener[] iiopListener
148143
}
149144
}
150145

151-
private String handleAddrAny(String hostAddr) {
152-
if (!ANY_ADDRS.contains(hostAddr)) {
153-
return hostAddr;
154-
}
155-
return DEFAULT_ORB_INIT_HOST;
156-
}
157-
158146
private Acceptor addAcceptor(org.omg.CORBA.ORB orb, boolean isLazy, String host, String type, int port) {
159147
LOG.log(DEBUG, "addAcceptor(orb, isLazy={0}, host={1}, type={2}, port={3})", isLazy, host, type, port);
160148
ORB theOrb = (ORB) orb;

appserver/security/webintegration/src/main/java/com/sun/web/security/RealmAdapter.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,18 @@ private List<String> getHostAndPort(HttpRequest request) throws IOException {
10791079
InetAddress[] localHostAdresses;
10801080
if (nwAddress == null || nwAddress.equals("0.0.0.0")) {
10811081
nwAddress = NetUtils.getCanonicalHostName();
1082-
if (!nwAddress.equals(hostPort[0])) {
1082+
if (nwAddress.equals(hostPort[0])) {
1083+
// Host names are the same, compare the ports
1084+
String nwPort = nwListener.getPort();
1085+
// If the listener port is different from the port
1086+
// in the Host header, then request is received by WS frontend
1087+
if (nwPort.equals(hostPort[1])) {
1088+
isWebServerRequest = false;
1089+
breakFromLoop = true;
1090+
} else {
1091+
isWebServerRequest = true;
1092+
}
1093+
} else {
10831094
// compare the InetAddress objects
10841095
// only if the hostname in the header
10851096
// does not match with the hostname in the
@@ -1104,20 +1115,6 @@ private List<String> getHostAndPort(HttpRequest request) throws IOException {
11041115
isWebServerRequest = true;
11051116
}
11061117
}
1107-
} else {
1108-
// Host names are the same, compare the ports
1109-
String nwPort = nwListener.getPort();
1110-
// If the listener port is different from the port
1111-
// in the Host header, then request is received by WS frontend
1112-
if (!nwPort.equals(hostPort[1])) {
1113-
isWebServerRequest = true;
1114-
1115-
} else {
1116-
isWebServerRequest = false;
1117-
breakFromLoop = true;
1118-
1119-
}
1120-
11211118
}
11221119
}
11231120
if (breakFromLoop && !isWebServerRequest) {

appserver/transaction/jts/src/main/java/com/sun/jts/CosTransactions/DefaultTransactionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static void setServerName(Properties properties) {
219219
if (serverId != null) {
220220
LOG.log(INFO,"Recoverable JTS instance, serverId = [{0}]",serverId);
221221
}
222-
String serverName = "UnknownHost"/*#Frozen*/;
222+
final String serverName;
223223
if (properties.getProperty(JTS_XA_SERVER_NAME) == null) {
224224
serverName = NetUtils.getHostName();
225225
} else {

nucleus/cluster/admin/src/main/java/com/sun/enterprise/v3/admin/cluster/ValidateNodeCommand.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,15 @@ private void validatePathSimple(String propname, String value, String configValu
281281

282282
private void validateHostname(String propname, String value, String configValue) throws CommandValidationException {
283283
try {
284-
// First do a simple case insensitve string comparison. If that
284+
// First do a simple case insensitive string comparison. If that
285285
// matches then it's good enough for us.
286286
validateString(propname, value, configValue, true);
287287
return;
288288
} catch (CommandValidationException e) {
289289
// Strings don't match, but we could have a case of
290-
// "sidewinder" and "sidewinder.us.oracle.com". NetUtils
291-
// isEqual() handles this check.
290+
// "sidewinder" and "sidewinder.us.oracle.com".
292291
if (!NetUtils.isSameHost(value, configValue)) {
293-
// If they both refer to the localhost then consider them them same.
292+
// If they both refer to the local host then consider them them same.
294293
if (!NetUtils.isLocal(value) || !NetUtils.isLocal(configValue)) {
295294
throw new CommandValidationException(
296295
Strings.get("attribute.mismatch", name, propname, value, configValue));

nucleus/cluster/cli/src/main/java/com/sun/enterprise/admin/cli/cluster/CreateLocalInstanceFilesystemCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ private void checkDASCoordinates() throws CommandException {
185185
// Check if hostName is valid by looking up its address
186186
InetAddress.getByName(DASHost);
187187
} catch (UnknownHostException e) {
188-
String thisHost = NetUtils.getHostName();
188+
String thisHost = NetUtils.getCanonicalHostName();
189189
String msg = Strings.get("Instance.DasHostUnknown", DASHost, thisHost);
190190
throw new CommandException(msg, e);
191191
}
192192

193193
// See if DAS is reachable
194194
if (!NetUtils.isRunning(DASHost, DASPort)) {
195195
// DAS provided host and port
196-
String thisHost = NetUtils.getHostName();
196+
String thisHost = NetUtils.getCanonicalHostName();
197197
String msg = Strings.get("Instance.DasHostUnreachable", DASHost, Integer.toString(DASPort), thisHost);
198198
throw new CommandException(msg);
199199
}

nucleus/common/common-util/src/main/java/com/sun/enterprise/util/net/NetUtils.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,13 @@ public static boolean isLocal(final String address) {
265265
if (LOCALHOST_IP.equals(address)) {
266266
return true;
267267
}
268-
269-
final String[] myIPs = getHostIPs();
270-
for (String myIP : myIPs) {
271-
if (address.equals(myIP)) {
268+
try {
269+
InetAddress inetAddress = InetAddress.getByName(address);
270+
if (getHostNames().contains(inetAddress.getCanonicalHostName())) {
271+
// if the address is same as the host name, it is considered local too
272272
return true;
273273
}
274-
}
275-
276-
if (getHostName().equals(address)) {
277-
// if the address is same as the host name, it is considered local too
278-
return true;
279-
}
280-
try {
281-
return InetAddress.getByName(address).isLoopbackAddress();
274+
return inetAddress.isLoopbackAddress();
282275
} catch (UnknownHostException e) {
283276
LOG.log(TRACE, "Unable to resolve the address.", e);
284277
return false;
@@ -368,8 +361,13 @@ public static InetAddress[] getHostAddresses() {
368361

369362

370363
/**
371-
* @return resolved host name of the local host (see <code>hostname</code> command on linux) or
372-
* loopback hostname.
364+
* Returns the host name of the local host (see <code>hostname</code> command on linux).
365+
* If the host name cannot be resolved, loopback host name is returned.
366+
* <p>
367+
* The result should be resolvable from the same machine, but might be unknown
368+
* to other hosts.
369+
*
370+
* @return host name of the local host or loopback hostname.
373371
*/
374372
public static String getHostName() {
375373
try {
@@ -402,7 +400,6 @@ public static String getCanonicalHostName() {
402400
* @return list of autodetected host names except loopbacks and unresolvable host names. Never null.
403401
*/
404402
public static List<String> getHostNames() {
405-
ThrowingPredicate<NetworkInterface> isLoopback = NetworkInterface::isLoopback;
406403
ThrowingPredicate<InetAddress> isHostName = address -> {
407404
if (address.getCanonicalHostName().equals(address.getHostAddress())) {
408405
return false;

nucleus/common/internal-api/src/main/java/org/glassfish/internal/api/ORBLocator.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.sun.enterprise.util.net.NetUtils;
2121

22+
import java.util.Set;
23+
2224
import org.jvnet.hk2.annotations.Contract;
2325
import org.omg.CORBA.ORB;
2426

@@ -39,7 +41,13 @@ public interface ORBLocator {
3941
// but we can't reference ORBConstants from the naming bundle!
4042
String FOLB_CLIENT_GROUP_INFO_SERVICE = "FolbClientGroupInfoService";
4143

42-
String DEFAULT_ORB_INIT_HOST = NetUtils.getHostName();
44+
/**
45+
* Zero ip addresses, ANY host, wildcard addresses,
46+
* and the IPv6 equivalent of the wildcard address.
47+
*/
48+
Set<String> ANY_ADDRS = Set.of("0.0.0.0", "::", "::ffff:0.0.0.0");
49+
50+
String DEFAULT_ORB_INIT_HOST = NetUtils.getCanonicalHostName();
4351
String DEFAULT_ORB_INIT_PORT = "3700";
4452

4553
// This property is true if SSL is required to be used by
@@ -61,4 +69,18 @@ public interface ORBLocator {
6169
* @return an initialized ORB instance
6270
*/
6371
ORB getORB();
72+
73+
/**
74+
* If null or from {@link #ANY_ADDRS} set, returns {@link #DEFAULT_ORB_INIT_HOST}.
75+
* Returns the parameter otherwise.
76+
*
77+
* @param hostOrAny
78+
* @return host name to use for ORB initialization
79+
*/
80+
public static String toConcreteHost(String hostOrAny) {
81+
if (hostOrAny == null || ANY_ADDRS.contains(hostOrAny)) {
82+
return DEFAULT_ORB_INIT_HOST;
83+
}
84+
return hostOrAny;
85+
}
6486
}

0 commit comments

Comments
 (0)