caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Turn echoing off on standard input to read e.g. passwords
@ 2019-03-28 20:03 Helmut Brandl
  2019-03-28 20:33 ` Matthew Ryan
  2019-04-01  9:05 ` Jeremie Dimino
  0 siblings, 2 replies; 7+ messages in thread
From: Helmut Brandl @ 2019-03-28 20:03 UTC (permalink / raw)
  To: caml users

Hello list,

Is there a portable way in ocaml to turn echoing off on standard input from the terminal to read e.g. passwords? By portable I mean that it works for Windows, Unix and Mac.

Thanks for any hint.

Regards
Helmut

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-28 20:03 [Caml-list] Turn echoing off on standard input to read e.g. passwords Helmut Brandl
@ 2019-03-28 20:33 ` Matthew Ryan
  2019-03-28 20:40   ` Helmut Brandl
                     ` (2 more replies)
  2019-04-01  9:05 ` Jeremie Dimino
  1 sibling, 3 replies; 7+ messages in thread
From: Matthew Ryan @ 2019-03-28 20:33 UTC (permalink / raw)
  To: Helmut Brandl; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 904 bytes --]

Hi Helmut,

The usual way to do this (for any language) is using ANSI escape sequences.
Code 8 sets the terminal to conceal characters and code 0 resets the
attributes, making them visible again.

For example, in a unix shell you can test this with echo and read:

echo -e '\x1b[8m'; read varname; echo -e '\x1b[0m'

To do the same from OCaml, you can output "\x1b[8m", read the password, and
then output "\x1b[0m" afterwards to switch printing back on.

I believe that this will work on Windows 10, but earlier versions may not
have the necessary ANSI support.

Hope this helps,
Matthew

On Thu, 28 Mar 2019, 20:04 Helmut Brandl, <helmut.brandl@gmx.net> wrote:

> Hello list,
>
> Is there a portable way in ocaml to turn echoing off on standard input
> from the terminal to read e.g. passwords? By portable I mean that it works
> for Windows, Unix and Mac.
>
> Thanks for any hint.
>
> Regards
> Helmut

[-- Attachment #2: Type: text/html, Size: 1543 bytes --]

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-28 20:33 ` Matthew Ryan
@ 2019-03-28 20:40   ` Helmut Brandl
  2019-03-28 21:12   ` Daniel Bünzli
  2019-03-29  3:05   ` Chet Murthy
  2 siblings, 0 replies; 7+ messages in thread
From: Helmut Brandl @ 2019-03-28 20:40 UTC (permalink / raw)
  To: Matthew Ryan; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 1077 bytes --]

Thanks.

> On Mar 28, 2019, at 14:33, Matthew Ryan <matthew@o1labs.org> wrote:
> 
> Hi Helmut,
> 
> The usual way to do this (for any language) is using ANSI escape sequences. Code 8 sets the terminal to conceal characters and code 0 resets the attributes, making them visible again.
> 
> For example, in a unix shell you can test this with echo and read:
> 
> echo -e '\x1b[8m'; read varname; echo -e '\x1b[0m'
> 
> To do the same from OCaml, you can output "\x1b[8m", read the password, and then output "\x1b[0m" afterwards to switch printing back on.
> 
> I believe that this will work on Windows 10, but earlier versions may not have the necessary ANSI support.
> 
> Hope this helps,
> Matthew
> 
> On Thu, 28 Mar 2019, 20:04 Helmut Brandl, <helmut.brandl@gmx.net <mailto:helmut.brandl@gmx.net>> wrote:
> Hello list,
> 
> Is there a portable way in ocaml to turn echoing off on standard input from the terminal to read e.g. passwords? By portable I mean that it works for Windows, Unix and Mac.
> 
> Thanks for any hint.
> 
> Regards
> Helmut


[-- Attachment #2: Type: text/html, Size: 2304 bytes --]

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-28 20:33 ` Matthew Ryan
  2019-03-28 20:40   ` Helmut Brandl
@ 2019-03-28 21:12   ` Daniel Bünzli
  2019-03-29  3:05   ` Chet Murthy
  2 siblings, 0 replies; 7+ messages in thread
From: Daniel Bünzli @ 2019-03-28 21:12 UTC (permalink / raw)
  To: Helmut Brandl, Matthew Ryan; +Cc: caml users

Just for refence another way (that will in no way work on Windows) is to temporarily tweak the terminal attributes. The ocamlunix book shows how to do this [here][1]. 

In contrast to ANSI escapes which I believe is just a rendering trick that snippet will not allow to cut and paste the input password to recover it, whether that's a property you care about or not is up to your use case.

Best,

Daniel


[1]: https://ocaml.github.io/ocamlunix/files.html#sec49

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-28 20:33 ` Matthew Ryan
  2019-03-28 20:40   ` Helmut Brandl
  2019-03-28 21:12   ` Daniel Bünzli
@ 2019-03-29  3:05   ` Chet Murthy
  2019-03-29 23:13     ` Chet Murthy
  2 siblings, 1 reply; 7+ messages in thread
From: Chet Murthy @ 2019-03-29  3:05 UTC (permalink / raw)
  To: Matthew Ryan; +Cc: Helmut Brandl, caml users

[-- Attachment #1: Type: text/plain, Size: 2661 bytes --]

[Argh, resending, b/c got the recipient-list wrong]

Perhaps this has changed in the N years since last I worked with
sgttyb/termios.  But back in the day, the answer was "there is no portable
way; the usual way is to manipulate terminal (actually, serial-port)
attributes".

This is less difficult than it seems: there should be support in ncurses
for it, and there should be a portable API between glibc (on Unixes) and
cygwin (on Winders).

I *do* think it would be worth looking at (for instance) the code of sudo,
to see what libraries it calls.

OK, I'm done, lemme adjust this onion on my belt,
--chet--

P.S. i re-reading the above (for resend) I'm being unclear.  So I'll
restate: the standard way that I've seen this done (many times over the
years) is to manipulate the state of the serial-port and the
"line-discipline" (that is, the equivalent of using the "stty" command).
This is not connected with issuing terminal escape-sequences, because the
former changes what is actually sent on the wire, whereas the latter
changes only what is displayed in the virtual or real terminal.  E.g. "stty
-echo" disables the echoing of characters in the line-discipline --
whatever you type into a TTY, the kernel-level terminal-driver will not
echo those chars back.  Whereas telling the terminal emulator (or real
terminal) to not display chars, doesn't change that the chars are actually
sent.

It's possible that this is a difference without relevance at this point in
the evolution of UNIX software.  But for sure, it was manipulation of
line-discipline characteristics, that was the means of disabling echo,
"back in the day".

On Thu, Mar 28, 2019 at 1:33 PM Matthew Ryan <matthew@o1labs.org> wrote:

> Hi Helmut,
>
> The usual way to do this (for any language) is using ANSI escape
> sequences. Code 8 sets the terminal to conceal characters and code 0 resets
> the attributes, making them visible again.
>
> For example, in a unix shell you can test this with echo and read:
>
> echo -e '\x1b[8m'; read varname; echo -e '\x1b[0m'
>
> To do the same from OCaml, you can output "\x1b[8m", read the password,
> and then output "\x1b[0m" afterwards to switch printing back on.
>
> I believe that this will work on Windows 10, but earlier versions may not
> have the necessary ANSI support.
>
> Hope this helps,
> Matthew
>
> On Thu, 28 Mar 2019, 20:04 Helmut Brandl, <helmut.brandl@gmx.net> wrote:
>
>> Hello list,
>>
>> Is there a portable way in ocaml to turn echoing off on standard input
>> from the terminal to read e.g. passwords? By portable I mean that it works
>> for Windows, Unix and Mac.
>>
>> Thanks for any hint.
>>
>> Regards
>> Helmut
>
>

[-- Attachment #2: Type: text/html, Size: 3832 bytes --]

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-29  3:05   ` Chet Murthy
@ 2019-03-29 23:13     ` Chet Murthy
  0 siblings, 0 replies; 7+ messages in thread
From: Chet Murthy @ 2019-03-29 23:13 UTC (permalink / raw)
  To: Matthew Ryan; +Cc: Helmut Brandl, caml users

[-- Attachment #1: Type: text/plain, Size: 1754 bytes --]

A little Googling turns up that the author of the Unix library (Xavier,
IIRC) provided support for termios(3).  So you can already do what you want
in Ocaml with no extra C ugly bits.

Here's a little ocaml program to demonstrate, and after it, some strace
output showing the way it calls ioctl(2) to manipulate the line discipline
(relevant bits bolded in HTML format mail).

let main () =
  let open Unix in
  let tios = tcgetattr stdin in
  Printf.printf "c_echo: %b\n" tios.c_echo ;
  Printf.printf "c_echoe: %b\n" tios.c_echoe ;
  Printf.printf "c_echok: %b\n" tios.c_echok ;
  Printf.printf "c_echonl: %b\n" tios.c_echonl ;
  flush Pervasives.stdout ;
  tios.c_echo <- false ;
  tcsetattr stdin TCSANOW tios ;
  let tios = tcgetattr stdin in
  Printf.printf "AFTER c_echo: %b\nSleeping 10 sec ....\n" tios.c_echo ;
  flush Pervasives.stdout ;
  Unix.sleep 10;
  tios.c_echo <- true ;
  tcsetattr stdin TCSANOW tios ;
  ()
;;

main() ;;

=====================
$ strace -eioctl ./noecho
ioctl(0, TCGETS, {B38400 opost isig icanon echo ...}) = 0
c_echo: true
c_echoe: true
c_echok: true
c_echonl: false
ioctl(0, TCGETS, {B38400 opost isig icanon *echo* ...}) = 0
ioctl(0, TCGETS, {B38400 opost isig icanon *echo* ...}) = 0
ioctl(0, SNDCTL_TMR_START or TCSETS, {B38400 opost isig icanon *-echo *...})
= 0
ioctl(0, TCGETS, {B38400 opost isig icanon *-echo *...}) = 0
ioctl(0, TCGETS, {B38400 opost isig icanon *-echo* ...}) = 0
AFTER c_echo: false
Sleeping 10 sec ....
ioctl(0, TCGETS, {B38400 opost isig icanon *-echo* ...}) = 0
ioctl(0, TCGETS, {B38400 opost isig icanon *-echo* ...}) = 0
ioctl(0, SNDCTL_TMR_START or TCSETS, {B38400 opost isig icanon *echo* ...})
= 0
ioctl(0, TCGETS, {B38400 opost isig icanon *echo* ...}) = 0
+++ exited with 0 +++

[-- Attachment #2: Type: text/html, Size: 2436 bytes --]

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

* Re: [Caml-list] Turn echoing off on standard input to read e.g. passwords
  2019-03-28 20:03 [Caml-list] Turn echoing off on standard input to read e.g. passwords Helmut Brandl
  2019-03-28 20:33 ` Matthew Ryan
@ 2019-04-01  9:05 ` Jeremie Dimino
  1 sibling, 0 replies; 7+ messages in thread
From: Jeremie Dimino @ 2019-04-01  9:05 UTC (permalink / raw)
  To: Helmut Brandl; +Cc: caml users

Hi Helmut,

If you don't mind the extra dependency, there is a complete example in
the lambda-term library:

https://github.com/ocaml-community/lambda-term/blob/master/examples/read_password.ml

The main differences with the other solutions mentioned in this thread
are that this version works on Windows, supports line edition and
supports customisation such as displaying nothing or displaying stars.

Best,

Jeremie

On Thu, Mar 28, 2019 at 8:04 PM Helmut Brandl <helmut.brandl@gmx.net> wrote:
>
> Hello list,
>
> Is there a portable way in ocaml to turn echoing off on standard input from the terminal to read e.g. passwords? By portable I mean that it works for Windows, Unix and Mac.
>
> Thanks for any hint.
>
> Regards
> Helmut



-- 
Jeremie

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

end of thread, other threads:[~2019-04-01  9:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-28 20:03 [Caml-list] Turn echoing off on standard input to read e.g. passwords Helmut Brandl
2019-03-28 20:33 ` Matthew Ryan
2019-03-28 20:40   ` Helmut Brandl
2019-03-28 21:12   ` Daniel Bünzli
2019-03-29  3:05   ` Chet Murthy
2019-03-29 23:13     ` Chet Murthy
2019-04-01  9:05 ` Jeremie Dimino

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