Skip to content

Commit 569b05a

Browse files
committed
8327818: Implement Kerberos debug with sun.security.util.Debug
Reviewed-by: coffeys, ssahoo
1 parent dec68d7 commit 569b05a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+855
-816
lines changed

src/java.base/share/classes/sun/security/util/Debug.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -159,6 +159,33 @@ public static Debug getInstance(String option, String prefix)
159159
}
160160
}
161161

162+
/**
163+
* Get a Debug object corresponding to the given option on the given
164+
* property value.
165+
* <p>
166+
* Note: unlike other {@code getInstance} methods, this method does not
167+
* use the {@code java.security.debug} system property.
168+
* <p>
169+
* Usually, this method is used by other individual area-specific debug
170+
* settings. For example,
171+
* {@snippet lang=java:
172+
* Map<String, String> settings = loadLoginSettings();
173+
* String property = settings.get("login");
174+
* Debug debug = Debug.of("login", property);
175+
* }
176+
* @param option the debug option name
177+
* @param property debug setting for this option
178+
* @return a new Debug object if the property is true
179+
*/
180+
public static Debug of(String option, String property) {
181+
if ("true".equalsIgnoreCase(property)) {
182+
Debug d = new Debug();
183+
d.prefix = option;
184+
return d;
185+
}
186+
return null;
187+
}
188+
162189
/**
163190
* True if the system property "security.debug" contains the
164191
* string "option".

src/java.security.jgss/macosx/native/libosxkrb5/nativeccache.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -81,7 +81,7 @@ static jclass FindClass(JNIEnv *env, char *className)
8181
jclass cls = (*env)->FindClass(env, className);
8282

8383
if (cls == NULL) {
84-
printf("Couldn't find %s\n", className);
84+
fprintf(stderr, "Couldn't find %s\n", className);
8585
return NULL;
8686
}
8787

@@ -129,49 +129,49 @@ JNIEXPORT jint JNICALL DEF_JNI_OnLoad(JavaVM *jvm, void *reserved)
129129

130130
ticketConstructor = (*env)->GetMethodID(env, ticketClass, "<init>", "([B)V");
131131
if (ticketConstructor == 0) {
132-
printf("Couldn't find Ticket constructor\n");
132+
fprintf(stderr, "Couldn't find Ticket constructor\n");
133133
return JNI_ERR;
134134
}
135135

136136
principalNameConstructor = (*env)->GetMethodID(env, principalNameClass, "<init>", "(Ljava/lang/String;I)V");
137137
if (principalNameConstructor == 0) {
138-
printf("Couldn't find PrincipalName constructor\n");
138+
fprintf(stderr, "Couldn't find PrincipalName constructor\n");
139139
return JNI_ERR;
140140
}
141141

142142
encryptionKeyConstructor = (*env)->GetMethodID(env, encryptionKeyClass, "<init>", "(I[B)V");
143143
if (encryptionKeyConstructor == 0) {
144-
printf("Couldn't find EncryptionKey constructor\n");
144+
fprintf(stderr, "Couldn't find EncryptionKey constructor\n");
145145
return JNI_ERR;
146146
}
147147

148148
ticketFlagsConstructor = (*env)->GetMethodID(env, ticketFlagsClass, "<init>", "(I[B)V");
149149
if (ticketFlagsConstructor == 0) {
150-
printf("Couldn't find TicketFlags constructor\n");
150+
fprintf(stderr, "Couldn't find TicketFlags constructor\n");
151151
return JNI_ERR;
152152
}
153153

154154
kerberosTimeConstructor = (*env)->GetMethodID(env, kerberosTimeClass, "<init>", "(J)V");
155155
if (kerberosTimeConstructor == 0) {
156-
printf("Couldn't find KerberosTime constructor\n");
156+
fprintf(stderr, "Couldn't find KerberosTime constructor\n");
157157
return JNI_ERR;
158158
}
159159

160160
integerConstructor = (*env)->GetMethodID(env, javaLangIntegerClass, "<init>", "(I)V");
161161
if (integerConstructor == 0) {
162-
printf("Couldn't find Integer constructor\n");
162+
fprintf(stderr, "Couldn't find Integer constructor\n");
163163
return JNI_ERR;
164164
}
165165

166166
hostAddressConstructor = (*env)->GetMethodID(env, hostAddressClass, "<init>", "(I[B)V");
167167
if (hostAddressConstructor == 0) {
168-
printf("Couldn't find HostAddress constructor\n");
168+
fprintf(stderr, "Couldn't find HostAddress constructor\n");
169169
return JNI_ERR;
170170
}
171171

172172
hostAddressesConstructor = (*env)->GetMethodID(env, hostAddressesClass, "<init>", "([Lsun/security/krb5/internal/HostAddress;)V");
173173
if (hostAddressesConstructor == 0) {
174-
printf("Couldn't find HostAddresses constructor\n");
174+
fprintf(stderr, "Couldn't find HostAddresses constructor\n");
175175
return JNI_ERR;
176176
}
177177

@@ -376,7 +376,7 @@ JNIEXPORT jobject JNICALL Java_sun_security_krb5_Credentials_acquireDefaultNativ
376376
krbcredsConstructor = (*env)->GetMethodID(env, krbcredsClass, "<init>",
377377
"(Lsun/security/krb5/internal/Ticket;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/PrincipalName;Lsun/security/krb5/EncryptionKey;Lsun/security/krb5/internal/TicketFlags;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/KerberosTime;Lsun/security/krb5/internal/HostAddresses;)V");
378378
if (krbcredsConstructor == 0) {
379-
printf("Couldn't find sun.security.krb5.internal.Ticket constructor\n");
379+
fprintf(stderr, "Couldn't find sun.security.krb5.internal.Ticket constructor\n");
380380
break;
381381
}
382382
}

src/java.security.jgss/share/classes/javax/security/auth/kerberos/ServicePermission.java

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -447,43 +447,6 @@ private void readObject(java.io.ObjectInputStream s)
447447
s.defaultReadObject();
448448
init(getName(),getMask(actions));
449449
}
450-
451-
452-
/*
453-
public static void main(String[] args) throws Exception {
454-
ServicePermission this_ =
455-
new ServicePermission(args[0], "accept");
456-
ServicePermission that_ =
457-
new ServicePermission(args[1], "accept,initiate");
458-
System.out.println("-----\n");
459-
System.out.println("this.implies(that) = " + this_.implies(that_));
460-
System.out.println("-----\n");
461-
System.out.println("this = "+this_);
462-
System.out.println("-----\n");
463-
System.out.println("that = "+that_);
464-
System.out.println("-----\n");
465-
466-
KrbServicePermissionCollection nps =
467-
new KrbServicePermissionCollection();
468-
nps.add(this_);
469-
nps.add(new ServicePermission("nfs/example.com@EXAMPLE.COM",
470-
"accept"));
471-
nps.add(new ServicePermission("host/example.com@EXAMPLE.COM",
472-
"initiate"));
473-
System.out.println("nps.implies(that) = " + nps.implies(that_));
474-
System.out.println("-----\n");
475-
476-
Enumeration e = nps.elements();
477-
478-
while (e.hasMoreElements()) {
479-
ServicePermission x =
480-
(ServicePermission) e.nextElement();
481-
System.out.println("nps.e = " + x);
482-
}
483-
484-
}
485-
*/
486-
487450
}
488451

489452

src/java.security.jgss/share/classes/sun/net/www/protocol/http/spnego/NegotiatorImpl.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,7 +35,6 @@
3535

3636
import sun.net.www.protocol.http.HttpCallerInfo;
3737
import sun.net.www.protocol.http.Negotiator;
38-
import sun.security.action.GetBooleanAction;
3938
import sun.security.action.GetPropertyAction;
4039
import sun.security.jgss.GSSManagerImpl;
4140
import sun.security.jgss.GSSContextImpl;
@@ -45,6 +44,8 @@
4544
import sun.security.util.ChannelBindingException;
4645
import sun.security.util.TlsChannelBinding;
4746

47+
import static sun.security.krb5.internal.Krb5.DEBUG;
48+
4849
/**
4950
* This class encapsulates all JAAS and JGSS API calls in a separate class
5051
* outside NegotiateAuthentication.java so that J2SE build can go smoothly
@@ -55,9 +56,6 @@
5556
*/
5657
public class NegotiatorImpl extends Negotiator {
5758

58-
private static final boolean DEBUG =
59-
GetBooleanAction.privilegedGetProperty("sun.security.krb5.debug");
60-
6159
private GSSContext context;
6260
private byte[] oneToken;
6361

@@ -105,8 +103,8 @@ private void init(HttpCallerInfo hci) throws GSSException, ChannelBindingExcepti
105103
((GSSContextImpl)context).requestDelegPolicy(true);
106104
}
107105
if (hci.serverCert != null) {
108-
if (DEBUG) {
109-
System.out.println("Negotiate: Setting CBT");
106+
if (DEBUG != null) {
107+
DEBUG.println("Negotiate: Setting CBT");
110108
}
111109
// set the channel binding token
112110
TlsChannelBinding b = TlsChannelBinding.create(hci.serverCert);
@@ -123,8 +121,8 @@ public NegotiatorImpl(HttpCallerInfo hci) throws IOException {
123121
try {
124122
init(hci);
125123
} catch (GSSException | ChannelBindingException e) {
126-
if (DEBUG) {
127-
System.out.println("Negotiate support not initiated, will " +
124+
if (DEBUG != null) {
125+
DEBUG.println("Negotiate support not initiated, will " +
128126
"fallback to other scheme if allowed. Reason:");
129127
e.printStackTrace();
130128
}
@@ -160,9 +158,9 @@ public byte[] nextToken(byte[] token) throws IOException {
160158
}
161159
return context.initSecContext(token, 0, token.length);
162160
} catch (GSSException e) {
163-
if (DEBUG) {
164-
System.out.println("Negotiate support cannot continue. Reason:");
165-
e.printStackTrace();
161+
if (DEBUG != null) {
162+
DEBUG.println("Negotiate support cannot continue. Reason:");
163+
e.printStackTrace(DEBUG.getPrintStream());
166164
}
167165
throw new IOException("Negotiate support cannot continue", e);
168166
}
@@ -181,9 +179,9 @@ public void disposeContext() throws IOException {
181179
context.dispose();
182180
}
183181
} catch (GSSException e) {
184-
if (DEBUG) {
185-
System.out.println("Cannot release resources. Reason:");
186-
e.printStackTrace();
182+
if (DEBUG != null) {
183+
DEBUG.println("Cannot release resources. Reason:");
184+
e.printStackTrace(DEBUG.getPrintStream());
187185
}
188186
throw new IOException("Cannot release resources", e);
189187
};

src/java.security.jgss/share/classes/sun/security/jgss/GSSCredentialImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -90,7 +90,7 @@ protected GSSCredentialImpl(GSSCredentialImpl src) {
9090
} catch (GSSException e) {
9191
if (defaultList) {
9292
// Try the next mechanism
93-
if (GSSUtil.DEBUG) {
93+
if (GSSUtil.DEBUG != null) {
9494
GSSUtil.debug("Ignore " + e + " while acquiring cred for "
9595
+ mechs[i]);
9696
// e.printStackTrace();

src/java.security.jgss/share/classes/sun/security/jgss/GSSHeader.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -317,25 +317,4 @@ private int putLength(int len, OutputStream out) throws IOException {
317317

318318
return retVal;
319319
}
320-
321-
// XXX Call these two in some central class
322-
private void debug(String str) {
323-
System.err.print(str);
324-
}
325-
326-
private String getHexBytes(byte[] bytes, int len)
327-
throws IOException {
328-
329-
StringBuilder sb = new StringBuilder();
330-
for (int i = 0; i < len; i++) {
331-
332-
int b1 = (bytes[i] >> 4) & 0x0f;
333-
int b2 = bytes[i] & 0x0f;
334-
335-
sb.append(Integer.toHexString(b1));
336-
sb.append(Integer.toHexString(b2));
337-
sb.append(' ');
338-
}
339-
return sb.toString();
340-
}
341320
}

src/java.security.jgss/share/classes/sun/security/jgss/GSSManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -88,7 +88,7 @@ public Oid[] getMechsForName(Oid nameType){
8888
}
8989
} catch (GSSException e) {
9090
// Squelch it and just skip over this mechanism
91-
if (GSSUtil.DEBUG) {
91+
if (GSSUtil.DEBUG != null) {
9292
GSSUtil.debug("Skip " + mech +
9393
": error retrieving supported name types");
9494
}

0 commit comments

Comments
 (0)