J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.security.auth.callback; |
| 27 | |
| 28 | /* JAAS imports */ |
| 29 | import javax.security.auth.callback.Callback; |
| 30 | import javax.security.auth.callback.CallbackHandler; |
| 31 | import javax.security.auth.callback.ConfirmationCallback; |
| 32 | import javax.security.auth.callback.NameCallback; |
| 33 | import javax.security.auth.callback.PasswordCallback; |
| 34 | import javax.security.auth.callback.TextOutputCallback; |
| 35 | import javax.security.auth.callback.UnsupportedCallbackException; |
| 36 | |
| 37 | /* Java imports */ |
| 38 | import java.io.BufferedReader; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.io.InputStreamReader; |
| 42 | import java.io.PushbackInputStream; |
| 43 | import java.util.Arrays; |
| 44 | |
| 45 | import sun.security.util.Password; |
| 46 | |
| 47 | /** |
| 48 | * <p> |
| 49 | * Prompts and reads from the command line for answers to authentication |
| 50 | * questions. |
| 51 | * This can be used by a JAAS application to instantiate a |
| 52 | * CallbackHandler |
| 53 | * @see javax.security.auth.callback |
| 54 | */ |
| 55 | |
| 56 | public class TextCallbackHandler implements CallbackHandler { |
| 57 | |
| 58 | /** |
| 59 | * <p>Creates a callback handler that prompts and reads from the |
| 60 | * command line for answers to authentication questions. |
| 61 | * This can be used by JAAS applications to instantiate a |
| 62 | * CallbackHandler. |
| 63 | |
| 64 | */ |
| 65 | public TextCallbackHandler() { } |
| 66 | |
| 67 | /** |
| 68 | * Handles the specified set of callbacks. |
| 69 | * |
| 70 | * @param callbacks the callbacks to handle |
| 71 | * @throws IOException if an input or output error occurs. |
| 72 | * @throws UnsupportedCallbackException if the callback is not an |
| 73 | * instance of NameCallback or PasswordCallback |
| 74 | */ |
| 75 | public void handle(Callback[] callbacks) |
| 76 | throws IOException, UnsupportedCallbackException |
| 77 | { |
| 78 | ConfirmationCallback confirmation = null; |
| 79 | |
| 80 | for (int i = 0; i < callbacks.length; i++) { |
| 81 | if (callbacks[i] instanceof TextOutputCallback) { |
| 82 | TextOutputCallback tc = (TextOutputCallback) callbacks[i]; |
| 83 | |
| 84 | String text; |
| 85 | switch (tc.getMessageType()) { |
| 86 | case TextOutputCallback.INFORMATION: |
| 87 | text = ""; |
| 88 | break; |
| 89 | case TextOutputCallback.WARNING: |
| 90 | text = "Warning: "; |
| 91 | break; |
| 92 | case TextOutputCallback.ERROR: |
| 93 | text = "Error: "; |
| 94 | break; |
| 95 | default: |
| 96 | throw new UnsupportedCallbackException( |
| 97 | callbacks[i], "Unrecognized message type"); |
| 98 | } |
| 99 | |
| 100 | String message = tc.getMessage(); |
| 101 | if (message != null) { |
| 102 | text += message; |
| 103 | } |
| 104 | if (text != null) { |
| 105 | System.err.println(text); |
| 106 | } |
| 107 | |
| 108 | } else if (callbacks[i] instanceof NameCallback) { |
| 109 | NameCallback nc = (NameCallback) callbacks[i]; |
| 110 | |
| 111 | if (nc.getDefaultName() == null) { |
| 112 | System.err.print(nc.getPrompt()); |
| 113 | } else { |
| 114 | System.err.print(nc.getPrompt() + |
| 115 | " [" + nc.getDefaultName() + "] "); |
| 116 | } |
| 117 | System.err.flush(); |
| 118 | |
| 119 | String result = readLine(); |
| 120 | if (result.equals("")) { |
| 121 | result = nc.getDefaultName(); |
| 122 | } |
| 123 | |
| 124 | nc.setName(result); |
| 125 | |
| 126 | } else if (callbacks[i] instanceof PasswordCallback) { |
| 127 | PasswordCallback pc = (PasswordCallback) callbacks[i]; |
| 128 | |
| 129 | System.err.print(pc.getPrompt()); |
| 130 | System.err.flush(); |
| 131 | |
| 132 | pc.setPassword(Password.readPassword(System.in)); |
| 133 | |
| 134 | } else if (callbacks[i] instanceof ConfirmationCallback) { |
| 135 | confirmation = (ConfirmationCallback) callbacks[i]; |
| 136 | |
| 137 | } else { |
| 138 | throw new UnsupportedCallbackException( |
| 139 | callbacks[i], "Unrecognized Callback"); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Do the confirmation callback last. */ |
| 144 | if (confirmation != null) { |
| 145 | doConfirmation(confirmation); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* Reads a line of input */ |
| 150 | private String readLine() throws IOException { |
| 151 | return new BufferedReader |
| 152 | (new InputStreamReader(System.in)).readLine(); |
| 153 | } |
| 154 | |
| 155 | private void doConfirmation(ConfirmationCallback confirmation) |
| 156 | throws IOException, UnsupportedCallbackException |
| 157 | { |
| 158 | String prefix; |
| 159 | int messageType = confirmation.getMessageType(); |
| 160 | switch (messageType) { |
| 161 | case ConfirmationCallback.WARNING: |
| 162 | prefix = "Warning: "; |
| 163 | break; |
| 164 | case ConfirmationCallback.ERROR: |
| 165 | prefix = "Error: "; |
| 166 | break; |
| 167 | case ConfirmationCallback.INFORMATION: |
| 168 | prefix = ""; |
| 169 | break; |
| 170 | default: |
| 171 | throw new UnsupportedCallbackException( |
| 172 | confirmation, "Unrecognized message type: " + messageType); |
| 173 | } |
| 174 | |
| 175 | class OptionInfo { |
| 176 | String name; |
| 177 | int value; |
| 178 | OptionInfo(String name, int value) { |
| 179 | this.name = name; |
| 180 | this.value = value; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | OptionInfo[] options; |
| 185 | int optionType = confirmation.getOptionType(); |
| 186 | switch (optionType) { |
| 187 | case ConfirmationCallback.YES_NO_OPTION: |
| 188 | options = new OptionInfo[] { |
| 189 | new OptionInfo("Yes", ConfirmationCallback.YES), |
| 190 | new OptionInfo("No", ConfirmationCallback.NO) |
| 191 | }; |
| 192 | break; |
| 193 | case ConfirmationCallback.YES_NO_CANCEL_OPTION: |
| 194 | options = new OptionInfo[] { |
| 195 | new OptionInfo("Yes", ConfirmationCallback.YES), |
| 196 | new OptionInfo("No", ConfirmationCallback.NO), |
| 197 | new OptionInfo("Cancel", ConfirmationCallback.CANCEL) |
| 198 | }; |
| 199 | break; |
| 200 | case ConfirmationCallback.OK_CANCEL_OPTION: |
| 201 | options = new OptionInfo[] { |
| 202 | new OptionInfo("OK", ConfirmationCallback.OK), |
| 203 | new OptionInfo("Cancel", ConfirmationCallback.CANCEL) |
| 204 | }; |
| 205 | break; |
| 206 | case ConfirmationCallback.UNSPECIFIED_OPTION: |
| 207 | String[] optionStrings = confirmation.getOptions(); |
| 208 | options = new OptionInfo[optionStrings.length]; |
| 209 | for (int i = 0; i < options.length; i++) { |
| 210 | options[i] = new OptionInfo(optionStrings[i], i); |
| 211 | } |
| 212 | break; |
| 213 | default: |
| 214 | throw new UnsupportedCallbackException( |
| 215 | confirmation, "Unrecognized option type: " + optionType); |
| 216 | } |
| 217 | |
| 218 | int defaultOption = confirmation.getDefaultOption(); |
| 219 | |
| 220 | String prompt = confirmation.getPrompt(); |
| 221 | if (prompt == null) { |
| 222 | prompt = ""; |
| 223 | } |
| 224 | prompt = prefix + prompt; |
| 225 | if (!prompt.equals("")) { |
| 226 | System.err.println(prompt); |
| 227 | } |
| 228 | |
| 229 | for (int i = 0; i < options.length; i++) { |
| 230 | if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) { |
| 231 | // defaultOption is an index into the options array |
| 232 | System.err.println( |
| 233 | i + ". " + options[i].name + |
| 234 | (i == defaultOption ? " [default]" : "")); |
| 235 | } else { |
| 236 | // defaultOption is an option value |
| 237 | System.err.println( |
| 238 | i + ". " + options[i].name + |
| 239 | (options[i].value == defaultOption ? " [default]" : "")); |
| 240 | } |
| 241 | } |
| 242 | System.err.print("Enter a number: "); |
| 243 | System.err.flush(); |
| 244 | int result; |
| 245 | try { |
| 246 | result = Integer.parseInt(readLine()); |
| 247 | if (result < 0 || result > (options.length - 1)) { |
| 248 | result = defaultOption; |
| 249 | } |
| 250 | result = options[result].value; |
| 251 | } catch (NumberFormatException e) { |
| 252 | result = defaultOption; |
| 253 | } |
| 254 | |
| 255 | confirmation.setSelectedIndex(result); |
| 256 | } |
| 257 | } |