0N/A/*
2362N/A * Copyright (c) 2000, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.security.auth.callback;
0N/A
0N/A/* JAAS imports */
0N/Aimport javax.security.auth.callback.Callback;
0N/Aimport javax.security.auth.callback.CallbackHandler;
0N/Aimport javax.security.auth.callback.ConfirmationCallback;
0N/Aimport javax.security.auth.callback.NameCallback;
0N/Aimport javax.security.auth.callback.PasswordCallback;
0N/Aimport javax.security.auth.callback.TextOutputCallback;
0N/Aimport javax.security.auth.callback.UnsupportedCallbackException;
0N/A
0N/A/* Java imports */
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.io.PushbackInputStream;
0N/Aimport java.util.Arrays;
0N/A
0N/Aimport sun.security.util.Password;
0N/A
0N/A/**
0N/A * <p>
0N/A * Prompts and reads from the command line for answers to authentication
0N/A * questions.
0N/A * This can be used by a JAAS application to instantiate a
0N/A * CallbackHandler
0N/A * @see javax.security.auth.callback
0N/A */
0N/A
0N/Apublic class TextCallbackHandler implements CallbackHandler {
0N/A
0N/A /**
0N/A * <p>Creates a callback handler that prompts and reads from the
0N/A * command line for answers to authentication questions.
0N/A * This can be used by JAAS applications to instantiate a
0N/A * CallbackHandler.
0N/A
0N/A */
0N/A public TextCallbackHandler() { }
0N/A
0N/A /**
0N/A * Handles the specified set of callbacks.
0N/A *
0N/A * @param callbacks the callbacks to handle
0N/A * @throws IOException if an input or output error occurs.
0N/A * @throws UnsupportedCallbackException if the callback is not an
0N/A * instance of NameCallback or PasswordCallback
0N/A */
0N/A public void handle(Callback[] callbacks)
0N/A throws IOException, UnsupportedCallbackException
0N/A {
0N/A ConfirmationCallback confirmation = null;
0N/A
0N/A for (int i = 0; i < callbacks.length; i++) {
0N/A if (callbacks[i] instanceof TextOutputCallback) {
0N/A TextOutputCallback tc = (TextOutputCallback) callbacks[i];
0N/A
0N/A String text;
0N/A switch (tc.getMessageType()) {
0N/A case TextOutputCallback.INFORMATION:
0N/A text = "";
0N/A break;
0N/A case TextOutputCallback.WARNING:
0N/A text = "Warning: ";
0N/A break;
0N/A case TextOutputCallback.ERROR:
0N/A text = "Error: ";
0N/A break;
0N/A default:
0N/A throw new UnsupportedCallbackException(
0N/A callbacks[i], "Unrecognized message type");
0N/A }
0N/A
0N/A String message = tc.getMessage();
0N/A if (message != null) {
0N/A text += message;
0N/A }
0N/A if (text != null) {
0N/A System.err.println(text);
0N/A }
0N/A
0N/A } else if (callbacks[i] instanceof NameCallback) {
0N/A NameCallback nc = (NameCallback) callbacks[i];
0N/A
0N/A if (nc.getDefaultName() == null) {
0N/A System.err.print(nc.getPrompt());
0N/A } else {
0N/A System.err.print(nc.getPrompt() +
0N/A " [" + nc.getDefaultName() + "] ");
0N/A }
0N/A System.err.flush();
0N/A
0N/A String result = readLine();
0N/A if (result.equals("")) {
0N/A result = nc.getDefaultName();
0N/A }
0N/A
0N/A nc.setName(result);
0N/A
0N/A } else if (callbacks[i] instanceof PasswordCallback) {
0N/A PasswordCallback pc = (PasswordCallback) callbacks[i];
0N/A
0N/A System.err.print(pc.getPrompt());
0N/A System.err.flush();
0N/A
1572N/A pc.setPassword(Password.readPassword(System.in, pc.isEchoOn()));
0N/A
0N/A } else if (callbacks[i] instanceof ConfirmationCallback) {
0N/A confirmation = (ConfirmationCallback) callbacks[i];
0N/A
0N/A } else {
0N/A throw new UnsupportedCallbackException(
0N/A callbacks[i], "Unrecognized Callback");
0N/A }
0N/A }
0N/A
0N/A /* Do the confirmation callback last. */
0N/A if (confirmation != null) {
0N/A doConfirmation(confirmation);
0N/A }
0N/A }
0N/A
0N/A /* Reads a line of input */
0N/A private String readLine() throws IOException {
1249N/A String result = new BufferedReader
0N/A (new InputStreamReader(System.in)).readLine();
1249N/A if (result == null) {
1249N/A throw new IOException("Cannot read from System.in");
1249N/A }
1249N/A return result;
0N/A }
0N/A
0N/A private void doConfirmation(ConfirmationCallback confirmation)
0N/A throws IOException, UnsupportedCallbackException
0N/A {
0N/A String prefix;
0N/A int messageType = confirmation.getMessageType();
0N/A switch (messageType) {
0N/A case ConfirmationCallback.WARNING:
0N/A prefix = "Warning: ";
0N/A break;
0N/A case ConfirmationCallback.ERROR:
0N/A prefix = "Error: ";
0N/A break;
0N/A case ConfirmationCallback.INFORMATION:
0N/A prefix = "";
0N/A break;
0N/A default:
0N/A throw new UnsupportedCallbackException(
0N/A confirmation, "Unrecognized message type: " + messageType);
0N/A }
0N/A
0N/A class OptionInfo {
0N/A String name;
0N/A int value;
0N/A OptionInfo(String name, int value) {
0N/A this.name = name;
0N/A this.value = value;
0N/A }
0N/A }
0N/A
0N/A OptionInfo[] options;
0N/A int optionType = confirmation.getOptionType();
0N/A switch (optionType) {
0N/A case ConfirmationCallback.YES_NO_OPTION:
0N/A options = new OptionInfo[] {
0N/A new OptionInfo("Yes", ConfirmationCallback.YES),
0N/A new OptionInfo("No", ConfirmationCallback.NO)
0N/A };
0N/A break;
0N/A case ConfirmationCallback.YES_NO_CANCEL_OPTION:
0N/A options = new OptionInfo[] {
0N/A new OptionInfo("Yes", ConfirmationCallback.YES),
0N/A new OptionInfo("No", ConfirmationCallback.NO),
0N/A new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
0N/A };
0N/A break;
0N/A case ConfirmationCallback.OK_CANCEL_OPTION:
0N/A options = new OptionInfo[] {
0N/A new OptionInfo("OK", ConfirmationCallback.OK),
0N/A new OptionInfo("Cancel", ConfirmationCallback.CANCEL)
0N/A };
0N/A break;
0N/A case ConfirmationCallback.UNSPECIFIED_OPTION:
0N/A String[] optionStrings = confirmation.getOptions();
0N/A options = new OptionInfo[optionStrings.length];
0N/A for (int i = 0; i < options.length; i++) {
0N/A options[i] = new OptionInfo(optionStrings[i], i);
0N/A }
0N/A break;
0N/A default:
0N/A throw new UnsupportedCallbackException(
0N/A confirmation, "Unrecognized option type: " + optionType);
0N/A }
0N/A
0N/A int defaultOption = confirmation.getDefaultOption();
0N/A
0N/A String prompt = confirmation.getPrompt();
0N/A if (prompt == null) {
0N/A prompt = "";
0N/A }
0N/A prompt = prefix + prompt;
0N/A if (!prompt.equals("")) {
0N/A System.err.println(prompt);
0N/A }
0N/A
0N/A for (int i = 0; i < options.length; i++) {
0N/A if (optionType == ConfirmationCallback.UNSPECIFIED_OPTION) {
0N/A // defaultOption is an index into the options array
0N/A System.err.println(
0N/A i + ". " + options[i].name +
0N/A (i == defaultOption ? " [default]" : ""));
0N/A } else {
0N/A // defaultOption is an option value
0N/A System.err.println(
0N/A i + ". " + options[i].name +
0N/A (options[i].value == defaultOption ? " [default]" : ""));
0N/A }
0N/A }
0N/A System.err.print("Enter a number: ");
0N/A System.err.flush();
0N/A int result;
0N/A try {
0N/A result = Integer.parseInt(readLine());
0N/A if (result < 0 || result > (options.length - 1)) {
0N/A result = defaultOption;
0N/A }
0N/A result = options[result].value;
0N/A } catch (NumberFormatException e) {
0N/A result = defaultOption;
0N/A }
0N/A
0N/A confirmation.setSelectedIndex(result);
0N/A }
0N/A}