1. /*
  2. * @(#)TextCallbackHandler.java 1.8 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.security.auth.callback;
  8. /* JAAS imports */
  9. import javax.security.auth.callback.Callback;
  10. import javax.security.auth.callback.CallbackHandler;
  11. import javax.security.auth.callback.ConfirmationCallback;
  12. import javax.security.auth.callback.NameCallback;
  13. import javax.security.auth.callback.PasswordCallback;
  14. import javax.security.auth.callback.TextOutputCallback;
  15. import javax.security.auth.callback.UnsupportedCallbackException;
  16. /* Java imports */
  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.PushbackInputStream;
  22. import java.util.Arrays;
  23. import sun.security.util.Password;
  24. /**
  25. * <p>
  26. * Prompts and reads from the command line for answers to authentication
  27. * questions.
  28. * This can be used by a JAAS application to instantiate a
  29. * CallbackHandler
  30. * @see javax.security.auth.callback
  31. */
  32. public class TextCallbackHandler implements CallbackHandler {
  33. /**
  34. * <p>Creates a callback handler that prompts and reads from the
  35. * command line for answers to authentication questions.
  36. * This can be used by JAAS applications to instantiate a
  37. * CallbackHandler.
  38. */
  39. public TextCallbackHandler() { }
  40. /**
  41. * Handles the specified set of callbacks.
  42. *
  43. * @param callbacks the callbacks to handle
  44. * @throws IOException if an input or output error occurs.
  45. * @throws UnsupportedCallbackException if the callback is not an
  46. * instance of NameCallback or PasswordCallback
  47. */
  48. public void handle(Callback[] callbacks)
  49. throws IOException, UnsupportedCallbackException
  50. {
  51. ConfirmationCallback confirmation = null;
  52. for (int i = 0; i < callbacks.length; i++) {
  53. if (callbacks[i] instanceof TextOutputCallback) {
  54. TextOutputCallback tc = (TextOutputCallback) callbacks[i];
  55. String text;
  56. switch (tc.getMessageType()) {
  57. case TextOutputCallback.INFORMATION:
  58. text = "";
  59. break;
  60. case TextOutputCallback.WARNING:
  61. text = "Warning: ";
  62. break;
  63. case TextOutputCallback.ERROR:
  64. text = "Error: ";
  65. break;
  66. default:
  67. throw new UnsupportedCallbackException(
  68. callbacks[i], "Unrecognized message type");
  69. }
  70. String message = tc.getMessage();
  71. if (message != null) {
  72. text += message;
  73. }
  74. if (text != null) {
  75. System.err.println(text);
  76. }
  77. } else if (callbacks[i] instanceof NameCallback) {
  78. NameCallback nc = (NameCallback) callbacks[i];
  79. if (nc.getDefaultName() == null) {
  80. System.err.print(nc.getPrompt());
  81. } else {
  82. System.err.print(nc.getPrompt() +
  83. " [" + nc.getDefaultName() + "] ");
  84. }
  85. System.err.flush();
  86. String result = readLine();
  87. if (result.equals("")) {
  88. result = nc.getDefaultName();
  89. }
  90. nc.setName(result);
  91. } else if (callbacks[i] instanceof PasswordCallback) {
  92. PasswordCallback pc = (PasswordCallback) callbacks[i];
  93. System.err.print(pc.getPrompt());
  94. System.err.flush();
  95. pc.setPassword(Password.readPassword(System.in));
  96. } else if (callbacks[i] instanceof ConfirmationCallback) {
  97. confirmation = (ConfirmationCallback) callbacks[i];
  98. } else {
  99. throw new UnsupportedCallbackException(
  100. callbacks[i], "Unrecognized Callback");
  101. }
  102. }
  103. /* Do the confirmation callback last. */
  104. if (confirmation != null) {
  105. doConfirmation(confirmation);
  106. }
  107. }
  108. /* Reads a line of input */
  109. private String readLine() throws IOException {
  110. return new BufferedReader
  111. (new InputStreamReader(System.in)).readLine();
  112. }
  113. private void doConfirmation(ConfirmationCallback confirmation)
  114. throws IOException, UnsupportedCallbackException
  115. {
  116. String prefix;
  117. int messageType = confirmation.getMessageType();
  118. switch (messageType) {
  119. case ConfirmationCallback.WARNING:
  120. prefix = "Warning: ";
  121. break;
  122. case ConfirmationCallback.ERROR:
  123. prefix = "Error: ";
  124. break;
  125. case ConfirmationCallback.INFORMATION:
  126. prefix = "";
  127. break;
  128. default:
  129. throw new UnsupportedCallbackException(
  130. confirmation, "Unrecognized message type: " + messageType);
  131. }
  132. class OptionInfo {
  133. String name;
  134. int value;
  135. OptionInfo(String name, int value) {
  136. this.name = name;
  137. this.value = value;
  138. }
  139. }
  140. OptionInfo[] options;
  141. int optionType = confirmation.getOptionType();
  142. switch (optionType) {
  143. case ConfirmationCallback.YES_NO_OPTION:
  144. options = new OptionInfo[] {
  145. new OptionInfo("Yes", ConfirmationCallback.YES),
  146. new OptionInfo("No", ConfirmationCallback.NO)
  147. };
  148. break;
  149. case ConfirmationCallback.YES_NO_CANCEL_OPTION:
  150. options = new OptionInfo[] {
  151. new OptionInfo("Yes", ConfirmationCallback.YES),
  152. new OptionInfo("No", ConfirmationCallback.NO),
  153. new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
  154. };
  155. break;
  156. case ConfirmationCallback.OK_CANCEL_OPTION:
  157. options = new OptionInfo[] {
  158. new OptionInfo("OK", ConfirmationCallback.OK),
  159. new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
  160. };
  161. break;
  162. case ConfirmationCallback.UNSPECIFIED_OPTION:
  163. String[] optionStrings = confirmation.getOptions();
  164. options = new OptionInfo[optionStrings.length];
  165. for (int i = 0; i < options.length; i++) {
  166. options[i].value = i;
  167. }
  168. break;
  169. default:
  170. throw new UnsupportedCallbackException(
  171. confirmation, "Unrecognized option type: " + optionType);
  172. }
  173. int defaultOption = confirmation.getDefaultOption();
  174. String prompt = confirmation.getPrompt();
  175. if (prompt == null) {
  176. prompt = "";
  177. }
  178. prompt = prefix + prompt;
  179. if (!prompt.equals("")) {
  180. System.err.println(prompt);
  181. }
  182. for (int i = 0; i < options.length; i++) {
  183. if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) {
  184. // defaultOption is an index into the options array
  185. System.err.println(
  186. i + ". " + options[i].name +
  187. (i == defaultOption ? " [default]" : ""));
  188. } else {
  189. // defaultOption is an option value
  190. System.err.println(
  191. i + ". " + options[i].name +
  192. (options[i].value == defaultOption ? " [default]" : ""));
  193. }
  194. }
  195. System.err.print("Enter a number: ");
  196. System.err.flush();
  197. int result;
  198. try {
  199. result = Integer.parseInt(readLine());
  200. if (result < 0 || result > (options.length - 1)) {
  201. result = defaultOption;
  202. }
  203. result = options[result].value;
  204. } catch (NumberFormatException e) {
  205. result = defaultOption;
  206. }
  207. confirmation.setSelectedIndex(result);
  208. }
  209. }