Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] How to read a password without displaying it?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: [Caml-list] How to read a password without displaying it?
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