From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA13571; Sat, 7 Feb 2004 14:17:33 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA12706 for ; Sat, 7 Feb 2004 14:17:32 +0100 (MET) Received: from kraid.nerim.net (smtp-106-saturday.nerim.net [62.4.16.106]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id i17DHVf09111 for ; Sat, 7 Feb 2004 14:17:31 +0100 (MET) Received: from hector.lesours (ours.starynkevitch.net [213.41.130.15]) by kraid.nerim.net (Postfix) with ESMTP id A5DCD4186F; Sat, 7 Feb 2004 14:17:30 +0100 (CET) Received: from basile by hector.lesours with local (Exim 4.30) id 1ApSKw-0005Oa-V1; Sat, 07 Feb 2004 14:17:30 +0100 Date: Sat, 7 Feb 2004 14:17:30 +0100 To: David MENTRE , caml-list@inria.fr Subject: Re: [Caml-list] How to read a password without displaying it? Message-ID: <20040207131730.GA20547@ours.starynkevitch.net> References: <87u123rqyn.fsf@linux-france.org> Mime-Version: 1.0 Content-Type: text/plain; charset=unknown-8bit Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87u123rqyn.fsf@linux-france.org> User-Agent: Mutt/1.5.5.1+cvs20040105i From: Basile STARYNKEVITCH X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 basile:01 basile:01 2004:99 textual:01 passwd:01 unix-like:01 readline:01 ncurses:01 textual:01 shameless:01 guis:01 python:01 gui:01 ioctl:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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: basilestarynkevitchnet aliases: basiletunesorg = bstarynknerimnet 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