caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Basile STARYNKEVITCH <basile@starynkevitch.net>
To: David MENTRE <dmentre@linux-france.org>, caml-list@inria.fr
Subject: Re: [Caml-list] How to read a password without displaying it?
Date: Sat, 7 Feb 2004 14:17:30 +0100	[thread overview]
Message-ID: <20040207131730.GA20547@ours.starynkevitch.net> (raw)
In-Reply-To: <87u123rqyn.fsf@linux-france.org>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=unknown-8bit, Size: 2759 bytes --]

On Sat, Feb 07, 2004 at 01:53:20PM +0100, David MENTRÉ wrote:
> 
> In my textual OCaml program, I would like to read a password. Currently,
> I'm using read_line which is not very satisfying as the password is
> display on screen.
> 
>   let passwd = read_line () in
> 
> Any idea how I might read the password without displaying it? 

I have a few ideas, but I'm sure you already know them. First, I
assume you have (and target only) a Unix-like system. All tricks below
are probably non portable to Windows (an OS I never coded for, which I
don't know).

The first trick would be to call the C function getpass (which,
according to its man page, is obsolete).

You could also use the [in]famous GNU readline C library.

You could also use the ncurses library.

If you have an X11 server (but you apparently want a textual only
interface), you might use (shameless plug) my GUIS gtk "server"
program from http://starynkevitch.net/Basile/guisdoc.html You'll just
have to code a few lines of Python or Ruby code creating a simple GUI
interface. The ocaml application communicates with it using textual
protocols. Of course, you could also run some tiny command which ask
for your password.

You might also issue the ioctl (that you need to code in C and call
from Ocaml) which supress the echo and make the keyboard working in
raw (non cooked) mode. I forgot the ugly details. You have to reset
your tty to its previous state before exiting (or just after having
got the password)

> 
> I tried the following function, using input_char, but the password is
> still displayed on screen:
> 
> let read_password () =
>   let password_chars = ref [] in
>   let loop = ref true in
>   while !loop do
>     let c = input_char stdin in
>     if c <> '\n' then (
>       password_chars := c :: !password_chars
>      ) else (
>       loop := false
>      )
>   done;
>   let password = String.create (List.length !password_chars) in
>   let _, res = List.fold_right (fun c (i, s) -> s.[i] <- c; (i+1, s))
>       !password_chars (0, password) in
>   res

I belive this should work if it where boxed by appriate ioctl removing
the echo.

I believe that you cannot do anything without a little C code (because
ioctl or fcntl are not available to Ocaml, mostly because typing it is
a nightmare)

Regards.

-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2004-02-07 13:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-07 12:53 David MENTRE
2004-02-07 13:17 ` Basile STARYNKEVITCH [this message]
2004-02-07 13:42   ` Maxence Guesdon
2004-02-07 14:45   ` David MENTRE
2004-02-07 14:54     ` David MENTRE
2004-02-07 14:59     ` Remi Vanicat
2004-02-07 14:50 ` Alain.Frisch
2004-02-07 15:12   ` David MENTRE
2004-02-07 16:31     ` David Brown

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=20040207131730.GA20547@ours.starynkevitch.net \
    --to=basile@starynkevitch.net \
    --cc=caml-list@inria.fr \
    --cc=dmentre@linux-france.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).