All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robin Rosenberg <robin.rosenberg.lists@dewire.com>
To: "Shawn O. Pearce" <spearce@spearce.org>
Cc: Marek Zawirski <marek.zawirski@gmail.com>, git@vger.kernel.org
Subject: [JGIT RFC PATCH] Add a stdio prompt for SSH connection information.
Date: Sun, 22 Jun 2008 23:06:25 +0200	[thread overview]
Message-ID: <200806222306.25434.robin.rosenberg.lists@dewire.com> (raw)

With Java6 there is support for echo-less input of information
like passwords from the console.

Windows users must set the system headless promperty java.awt.headless
to true (-Djava.awt.headless=true) manually. Only unix system headless
operation is detected automatically via the DISPLAY environment variable.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---

Starting a command line utility like jgit and getting a graphical prompt is almost
an insult. The problem here is that Java 5, which we support, does not have a
portable way of disabling echoing of characters. Java 6 (and anythung newr)
does. There are several solutions involving non-portable tricks. Should we 
support an insecure practice of echoing passwords, or as I do here, only support
it if one is using Java 6. A downside of supporting it at all is that one needs a
JavaSE 6 compiler to build the thing.

btw, does anyone know if console() yields null when runnings as a Windows
service? I tentatively assume that it does without explicily setting the headless
property.

I'm also a little unsure about how to invoke the promptKeyboardInteractive method.

 .../jgit/transport/DefaultSshSessionFactory.java   |   86 +++++++++++++++++++-
 1 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
index 8a59904..c895988 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
@@ -39,6 +39,7 @@
 package org.spearce.jgit.transport;
 
 import java.awt.Container;
+import java.awt.GraphicsEnvironment;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.awt.Insets;
@@ -90,8 +91,18 @@ class DefaultSshSessionFactory extends SshSessionFactory {
 		final Session session = getUserJSch().getSession(user, host, port);
 		if (pass != null)
 			session.setPassword(pass);
-		else
-			session.setUserInfo(new AWT_UserInfo());
+		UserInfo userInfo = null;
+		try {
+			System.class.getMethod("console");
+			if (GraphicsEnvironment.isHeadless() || System.console() == null)
+				userInfo = new StdioUserInfo();
+		} catch (NoSuchMethodException e) {
+			throw new IllegalStateException(
+					"You will need JavaSE 6 for stdio based password prompts");
+		}
+		if (userInfo == null)
+			userInfo = new AWT_UserInfo();
+		session.setUserInfo(userInfo);
 		return session;
 	}
 
@@ -249,4 +260,75 @@ class DefaultSshSessionFactory extends SshSessionFactory {
 			return null; // cancel
 		}
 	}
+
+	static class StdioUserInfo implements UserInfo, UIKeyboardInteractive {
+
+		String password;
+
+		String passphrase;
+
+		public String getPassphrase() {
+			return passphrase;
+		}
+
+		public String getPassword() {
+			return password;
+		}
+
+		public boolean promptPassphrase(String msg) {
+			passphrase = null;
+			char[] readPassword = System.console().readPassword("Passphrase: ");
+			if (readPassword == null)
+				return false;
+			passphrase = new String(readPassword);
+			return true;
+		}
+
+		public boolean promptPassword(String msg) {
+			password = null;
+			char[] readPassword = System.console().readPassword("Password: ");
+			if (readPassword == null)
+				return false;
+			password = new String(readPassword);
+			return true;
+		}
+
+		public boolean promptYesNo(String msg) {
+			String readLine = System.console().readLine("%s [Y/n]: ", msg);
+			if (readLine == null)
+				return false;
+			if (readLine.indexOf("yY") > 0)
+				return true;
+			return false;
+		}
+
+		public void showMessage(String msg) {
+			System.console().format("%s\n", msg);
+		}
+
+		public String[] promptKeyboardInteractive(final String destination,
+				final String name, final String instruction,
+				final String[] prompt, final boolean[] echo) {
+			System.console().printf("%s\n", instruction);
+			System.console().printf("Information for %s@%s required.\n", name,
+					destination);
+			String[] ret = new String[prompt.length];
+			for (int i = 0; i < ret.length; ++i) {
+				String s;
+				if (echo[i])
+					s = System.console().readLine("%s :", prompt[i]);
+				else {
+					char[] cha = System.console().readPassword("%s :",
+							prompt[i]);
+					if (cha == null)
+						s = null;
+					else
+						s = new String(cha);
+				}
+				ret[i] = s;
+			}
+			return ret;
+		}
+
+	}
 }
-- 
1.5.5.1.178.g1f811

             reply	other threads:[~2008-06-22 21:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-22 21:06 Robin Rosenberg [this message]
2008-06-22 23:13 ` [JGIT RFC PATCH] Add a stdio prompt for SSH connection information Shawn O. Pearce
2008-06-23  6:23   ` Robin Rosenberg
2008-06-23  6:34     ` Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200806222306.25434.robin.rosenberg.lists@dewire.com \
    --to=robin.rosenberg.lists@dewire.com \
    --cc=git@vger.kernel.org \
    --cc=marek.zawirski@gmail.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.