caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to read a password without displaying it?
@ 2004-02-07 12:53 David MENTRE
  2004-02-07 13:17 ` Basile STARYNKEVITCH
  2004-02-07 14:50 ` Alain.Frisch
  0 siblings, 2 replies; 9+ messages in thread
From: David MENTRE @ 2004-02-07 12:53 UTC (permalink / raw)
  To: caml-list

Hello,

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 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


Many thanks in advance for any help,
Yours,
d.
-- 
 David Mentré <dmentre@linux-france.org>

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 12:53 [Caml-list] How to read a password without displaying it? David MENTRE
@ 2004-02-07 13:17 ` Basile STARYNKEVITCH
  2004-02-07 13:42   ` Maxence Guesdon
  2004-02-07 14:45   ` David MENTRE
  2004-02-07 14:50 ` Alain.Frisch
  1 sibling, 2 replies; 9+ messages in thread
From: Basile STARYNKEVITCH @ 2004-02-07 13:17 UTC (permalink / raw)
  To: David MENTRE, caml-list

[-- 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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 13:17 ` Basile STARYNKEVITCH
@ 2004-02-07 13:42   ` Maxence Guesdon
  2004-02-07 14:45   ` David MENTRE
  1 sibling, 0 replies; 9+ messages in thread
From: Maxence Guesdon @ 2004-02-07 13:42 UTC (permalink / raw)
  To: caml-list

Hello,

In Cash (http://pauillac.inria.fr/cash/), you can turn the echo
of the tty on and off. It may be what you need.

Hope this helps,

-- 
Maxence Guesdon

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 13:17 ` Basile STARYNKEVITCH
  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
  1 sibling, 2 replies; 9+ messages in thread
From: David MENTRE @ 2004-02-07 14:45 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

Thank you Basile and Maxence for your replies,

In fact, I wanted to do that in caml only, to be portable on Windows and
I'm relunctant to bind to C code. However, from what you said, it seems
impossible.

I tried following trick, using '\b' on output. Strangely enough, the
output_char have no effect, even if I do a flush. The '*' are printed
*after* I do a <RETURN> on my terminal.  I don't understand this
behavior. The flush should send the caracters to the terminal.

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;
      output_char stdout '\b';
      output_char stdout '*';
      flush_all ();
     ) 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


Yours,
d.
-- 
 David Mentré <dmentre@linux-france.org>

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 12:53 [Caml-list] How to read a password without displaying it? David MENTRE
  2004-02-07 13:17 ` Basile STARYNKEVITCH
@ 2004-02-07 14:50 ` Alain.Frisch
  2004-02-07 15:12   ` David MENTRE
  1 sibling, 1 reply; 9+ messages in thread
From: Alain.Frisch @ 2004-02-07 14:50 UTC (permalink / raw)
  To: David MENTRE; +Cc: caml-list

On Sat, 7 Feb 2004, David MENTRE wrote:

> Any idea how I might read the password without displaying it?

The field c_echo in the record type Unix.terminal_io might be the
solution.

-- Alain

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 14:45   ` David MENTRE
@ 2004-02-07 14:54     ` David MENTRE
  2004-02-07 14:59     ` Remi Vanicat
  1 sibling, 0 replies; 9+ messages in thread
From: David MENTRE @ 2004-02-07 14:54 UTC (permalink / raw)
  To: Basile STARYNKEVITCH; +Cc: caml-list

David MENTRE <dmentre@linux-france.org> writes:

> I tried following trick, using '\b' on output. Strangely enough, the
> output_char have no effect, even if I do a flush. The '*' are printed
> *after* I do a <RETURN> on my terminal.  I don't understand this
> behavior. The flush should send the caracters to the terminal.

Oops, I remembered my old unix programming. The shell (or whatever in
front of the caml program) sends the whole line after <CR>. So my output
is done on the following line. (sight)

Yours,
d.
-- 
 david.mentre@wanadoo.fr

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 14:45   ` David MENTRE
  2004-02-07 14:54     ` David MENTRE
@ 2004-02-07 14:59     ` Remi Vanicat
  1 sibling, 0 replies; 9+ messages in thread
From: Remi Vanicat @ 2004-02-07 14:59 UTC (permalink / raw)
  To: caml-list

David MENTRE <dmentre@linux-france.org> writes:

> Thank you Basile and Maxence for your replies,
>
> In fact, I wanted to do that in caml only, to be portable on Windows
> and I'm relunctant to bind to C code. However, from what you said,
> it seems impossible.

Well, I would try to use the Terminal interface of the Unix library :

open Unix

let read_pass () =
  let attr = tcgetattr stdin in
  tcsetattr stdin TCSAFLUSH { attr with c_echo = false };
  let line = read_line () in
  tcsetattr stdin TCSAFLUSH attr;
  line

It work. Of course you could do something to print * when the user
type something, but this is a first solution. By the way i don't know
if this work on windows.

-- 
Rémi Vanicat

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 14:50 ` Alain.Frisch
@ 2004-02-07 15:12   ` David MENTRE
  2004-02-07 16:31     ` David Brown
  0 siblings, 1 reply; 9+ messages in thread
From: David MENTRE @ 2004-02-07 15:12 UTC (permalink / raw)
  To: Alain.Frisch; +Cc: caml-list

Hello Alain,

Alain.Frisch@ens.fr writes:

> The field c_echo in the record type Unix.terminal_io might be the
> solution.

Thanks a lot Alain, you found the right solution.

Here is the working code:

open Unix
[...]
let read_password () =
  let term_init = tcgetattr stdin in
  let term_no_echo = { term_init with c_echo = false; } in
  tcsetattr stdin TCSANOW term_no_echo;
  let password = read_line () in
  tcsetattr stdin TCSAFLUSH term_init;
  password


Many thanks,
d.
-- 
 David Mentré <dmentre@linux-france.org>

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] How to read a password without displaying it?
  2004-02-07 15:12   ` David MENTRE
@ 2004-02-07 16:31     ` David Brown
  0 siblings, 0 replies; 9+ messages in thread
From: David Brown @ 2004-02-07 16:31 UTC (permalink / raw)
  To: David MENTRE; +Cc: Alain.Frisch, caml-list

On Sat, Feb 07, 2004 at 04:12:01PM +0100, David MENTRE wrote:

> open Unix
> [...]
> let read_password () =
>   let term_init = tcgetattr stdin in
>   let term_no_echo = { term_init with c_echo = false; } in
>   tcsetattr stdin TCSANOW term_no_echo;
>   let password = read_line () in
>   tcsetattr stdin TCSAFLUSH term_init;
>   password

The only suggestion would be to put the read_line in a try clause, and
restore the terminal settings if an exception is raised.  Otherwise,
pressing ^C leaves the terminal with echo off.

Dave

-------------------
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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2004-02-07 16:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-02-07 12:53 [Caml-list] How to read a password without displaying it? David MENTRE
2004-02-07 13:17 ` Basile STARYNKEVITCH
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

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).