Showing the changes to java/pkg/sun/security/util/Debug.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,292 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1998, 2010, 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.util; | ||
| 27 | |||
| 28 | import java.math.BigInteger; | ||
| 29 | import java.util.regex.Pattern; | ||
| 30 | import java.util.regex.Matcher; | ||
| 31 | import java.util.Locale; | ||
| 32 | |||
| 33 | /** | ||
| 34 | * A utility class for debuging. | ||
| 35 | * | ||
| 36 | * @author Roland Schemers | ||
| 37 | */ | ||
| 38 | public class Debug { | ||
| 39 | |||
| 40 | private String prefix; | ||
| 41 | |||
| 42 | private static String args; | ||
| 43 | |||
| 44 | static { | ||
| 45 | args = java.security.AccessController.doPrivileged | ||
| 46 | (new sun.security.action.GetPropertyAction | ||
| 47 | ("java.security.debug")); | ||
| 48 | |||
| 49 | String args2 = java.security.AccessController.doPrivileged | ||
| 50 | (new sun.security.action.GetPropertyAction | ||
| 51 | ("java.security.auth.debug")); | ||
| 52 | |||
| 53 | if (args == null) { | ||
| 54 | args = args2; | ||
| 55 | } else { | ||
| 56 | if (args2 != null) | ||
| 57 | args = args + "," + args2; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (args != null) { | ||
| 61 | args = marshal(args); | ||
| 62 | if (args.equals("help")) { | ||
| 63 | Help(); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | public static void Help() | ||
| 69 | { | ||
| 70 | System.err.println(); | ||
| 71 | System.err.println("all turn on all debugging"); | ||
| 72 | System.err.println("access print all checkPermission results"); | ||
| 73 | System.err.println("combiner SubjectDomainCombiner debugging"); | ||
| 74 | System.err.println("gssloginconfig"); | ||
| 75 | System.err.println("configfile JAAS ConfigFile loading"); | ||
| 76 | System.err.println("configparser JAAS ConfigFile parsing"); | ||
| 77 | System.err.println(" GSS LoginConfigImpl debugging"); | ||
| 78 | System.err.println("jar jar verification"); | ||
| 79 | System.err.println("logincontext login context results"); | ||
| 80 | System.err.println("policy loading and granting"); | ||
| 81 | System.err.println("provider security provider debugging"); | ||
| 82 | System.err.println("scl permissions SecureClassLoader assigns"); | ||
| 83 | System.err.println(); | ||
| 84 | System.err.println("The following can be used with access:"); | ||
| 85 | System.err.println(); | ||
| 86 | System.err.println("stack include stack trace"); | ||
| 87 | System.err.println("domain dump all domains in context"); | ||
| 88 | System.err.println("failure before throwing exception, dump stack"); | ||
| 89 | System.err.println(" and domain that didn't have permission"); | ||
| 90 | System.err.println(); | ||
| 91 | System.err.println("The following can be used with stack and domain:"); | ||
| 92 | System.err.println(); | ||
| 93 | System.err.println("permission=<classname>"); | ||
| 94 | System.err.println(" only dump output if specified permission"); | ||
| 95 | System.err.println(" is being checked"); | ||
| 96 | System.err.println("codebase=<URL>"); | ||
| 97 | System.err.println(" only dump output if specified codebase"); | ||
| 98 | System.err.println(" is being checked"); | ||
| 99 | |||
| 100 | System.err.println(); | ||
| 101 | System.err.println("Note: Separate multiple options with a comma"); | ||
| 102 | System.exit(0); | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | /** | ||
| 107 | * Get a Debug object corresponding to whether or not the given | ||
| 108 | * option is set. Set the prefix to be the same as option. | ||
| 109 | */ | ||
| 110 | |||
| 111 | public static Debug getInstance(String option) | ||
| 112 | { | ||
| 113 | return getInstance(option, option); | ||
| 114 | } | ||
| 115 | |||
| 116 | /** | ||
| 117 | * Get a Debug object corresponding to whether or not the given | ||
| 118 | * option is set. Set the prefix to be prefix. | ||
| 119 | */ | ||
| 120 | public static Debug getInstance(String option, String prefix) | ||
| 121 | { | ||
| 122 | if (isOn(option)) { | ||
| 123 | Debug d = new Debug(); | ||
| 124 | d.prefix = prefix; | ||
| 125 | return d; | ||
| 126 | } else { | ||
| 127 | return null; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * True if the system property "security.debug" contains the | ||
| 133 | * string "option". | ||
| 134 | */ | ||
| 135 | public static boolean isOn(String option) | ||
| 136 | { | ||
| 137 | if (args == null) | ||
| 138 | return false; | ||
| 139 | else { | ||
| 140 | if (args.indexOf("all") != -1) | ||
| 141 | return true; | ||
| 142 | else | ||
| 143 | return (args.indexOf(option) != -1); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * print a message to stderr that is prefixed with the prefix | ||
| 149 | * created from the call to getInstance. | ||
| 150 | */ | ||
| 151 | |||
| 152 | public void println(String message) | ||
| 153 | { | ||
| 154 | System.err.println(prefix + ": "+message); | ||
| 155 | } | ||
| 156 | |||
| 157 | /** | ||
| 158 | * print a blank line to stderr that is prefixed with the prefix. | ||
| 159 | */ | ||
| 160 | |||
| 161 | public void println() | ||
| 162 | { | ||
| 163 | System.err.println(prefix + ":"); | ||
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * print a message to stderr that is prefixed with the prefix. | ||
| 168 | */ | ||
| 169 | |||
| 170 | public static void println(String prefix, String message) | ||
| 171 | { | ||
| 172 | System.err.println(prefix + ": "+message); | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 176 | * return a hexadecimal printed representation of the specified | ||
| 177 | * BigInteger object. the value is formatted to fit on lines of | ||
| 178 | * at least 75 characters, with embedded newlines. Words are | ||
| 179 | * separated for readability, with eight words (32 bytes) per line. | ||
| 180 | */ | ||
| 181 | public static String toHexString(BigInteger b) { | ||
| 182 | String hexValue = b.toString(16); | ||
| 183 | StringBuffer buf = new StringBuffer(hexValue.length()*2); | ||
| 184 | |||
| 185 | if (hexValue.startsWith("-")) { | ||
| 186 | buf.append(" -"); | ||
| 187 | hexValue = hexValue.substring(1); | ||
| 188 | } else { | ||
| 189 | buf.append(" "); // four spaces | ||
| 190 | } | ||
| 191 | if ((hexValue.length()%2) != 0) { | ||
| 192 | // add back the leading 0 | ||
| 193 | hexValue = "0" + hexValue; | ||
| 194 | } | ||
| 195 | int i=0; | ||
| 196 | while (i < hexValue.length()) { | ||
| 197 | // one byte at a time | ||
| 198 | buf.append(hexValue.substring(i, i+2)); | ||
| 199 | i+=2; | ||
| 200 | if (i!= hexValue.length()) { | ||
| 201 | if ((i%64) == 0) { | ||
| 202 | buf.append("\n "); // line after eight words | ||
| 203 | } else if (i%8 == 0) { | ||
| 204 | buf.append(" "); // space between words | ||
| 205 | } | ||
| 206 | } | ||
| 207 | } | ||
| 208 | return buf.toString(); | ||
| 209 | } | ||
| 210 | |||
| 211 | /** | ||
| 212 | * change a string into lower case except permission classes and URLs. | ||
| 213 | */ | ||
| 214 | private static String marshal(String args) { | ||
| 215 | if (args != null) { | ||
| 216 | StringBuffer target = new StringBuffer(); | ||
| 217 | StringBuffer source = new StringBuffer(args); | ||
| 218 | |||
| 219 | // obtain the "permission=<classname>" options | ||
| 220 | // the syntax of classname: IDENTIFIER.IDENTIFIER | ||
| 221 | // the regular express to match a class name: | ||
| 222 | // "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*" | ||
| 223 | String keyReg = "[Pp][Ee][Rr][Mm][Ii][Ss][Ss][Ii][Oo][Nn]="; | ||
| 224 | String keyStr = "permission="; | ||
| 225 | String reg = keyReg + | ||
| 226 | "[a-zA-Z_$][a-zA-Z0-9_$]*([.][a-zA-Z_$][a-zA-Z0-9_$]*)*"; | ||
| 227 | Pattern pattern = Pattern.compile(reg); | ||
| 228 | Matcher matcher = pattern.matcher(source); | ||
| 229 | StringBuffer left = new StringBuffer(); | ||
| 230 | while (matcher.find()) { | ||
| 231 | String matched = matcher.group(); | ||
| 232 | target.append(matched.replaceFirst(keyReg, keyStr)); | ||
| 233 | target.append(" "); | ||
| 234 | |||
| 235 | // delete the matched sequence | ||
| 236 | matcher.appendReplacement(left, ""); | ||
| 237 | } | ||
| 238 | matcher.appendTail(left); | ||
| 239 | source = left; | ||
| 240 | |||
| 241 | // obtain the "codebase=<URL>" options | ||
| 242 | // the syntax of URL is too flexible, and here assumes that the | ||
| 243 | // URL contains no space, comma(','), and semicolon(';'). That | ||
| 244 | // also means those characters also could be used as separator | ||
| 245 | // after codebase option. | ||
| 246 | // However, the assumption is incorrect in some special situation | ||
| 247 | // when the URL contains comma or semicolon | ||
| 248 | keyReg = "[Cc][Oo][Dd][Ee][Bb][Aa][Ss][Ee]="; | ||
| 249 | keyStr = "codebase="; | ||
| 250 | reg = keyReg + "[^, ;]*"; | ||
| 251 | pattern = Pattern.compile(reg); | ||
| 252 | matcher = pattern.matcher(source); | ||
| 253 | left = new StringBuffer(); | ||
| 254 | while (matcher.find()) { | ||
| 255 | String matched = matcher.group(); | ||
| 256 | target.append(matched.replaceFirst(keyReg, keyStr)); | ||
| 257 | target.append(" "); | ||
| 258 | |||
| 259 | // delete the matched sequence | ||
| 260 | matcher.appendReplacement(left, ""); | ||
| 261 | } | ||
| 262 | matcher.appendTail(left); | ||
| 263 | source = left; | ||
| 264 | |||
| 265 | // convert the rest to lower-case characters | ||
| 266 | target.append(source.toString().toLowerCase(Locale.ENGLISH)); | ||
| 267 | |||
| 268 | return target.toString(); | ||
| 269 | } | ||
| 270 | |||
| 271 | return null; | ||
| 272 | } | ||
| 273 | |||
| 274 | private final static char[] hexDigits = "0123456789abcdef".toCharArray(); | ||
| 275 | |||
| 276 | public static String toString(byte[] b) { | ||
| 277 | if (b == null) { | ||
| 278 | return "(null)"; | ||
| 279 | } | ||
| 280 | StringBuilder sb = new StringBuilder(b.length * 3); | ||
| 281 | for (int i = 0; i < b.length; i++) { | ||
| 282 | int k = b[i] & 0xff; | ||
| 283 | if (i != 0) { | ||
| 284 | sb.append(':'); | ||
| 285 | } | ||
| 286 | sb.append(hexDigits[k >>> 4]); | ||
| 287 | sb.append(hexDigits[k & 0xf]); | ||
| 288 | } | ||
| 289 | return sb.toString(); | ||
| 290 | } | ||
| 291 | |||
| 292 | } | ||