| 1 | (* HCoop Domtool (http://hcoop.sourceforge.net/) |
| 2 | * Copyright (c) 2006, Adam Chlipala |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | *) |
| 18 | |
| 19 | (* Code for receiving and executing configuration files *) |
| 20 | |
| 21 | structure Client :> CLIENT = struct |
| 22 | |
| 23 | datatype passwd_result = |
| 24 | Passwd of string |
| 25 | | Aborted |
| 26 | | Error |
| 27 | |
| 28 | fun hastty () = |
| 29 | Posix.ProcEnv.isatty Posix.FileSys.stdin |
| 30 | |
| 31 | fun getpass () = |
| 32 | let |
| 33 | val tty = Posix.FileSys.stdin |
| 34 | val termios = SOME (Posix.TTY.TC.getattr tty) |
| 35 | handle OS.SysErr (reason, SOME syserr) => |
| 36 | if syserr = Posix.Error.notty orelse syserr = Posix.Error.inval then |
| 37 | (print "Warning: no terminal found, not hiding password\n"; |
| 38 | TextIO.flushOut TextIO.stdOut; |
| 39 | NONE) |
| 40 | else raise OS.SysErr (reason, SOME syserr) |
| 41 | val fields = case termios of SOME termios => SOME (Posix.TTY.fieldsOf termios) |
| 42 | | NONE => NONE |
| 43 | |
| 44 | val termios' = case fields of SOME fields => |
| 45 | SOME (Posix.TTY.termios {iflag = #iflag fields, |
| 46 | oflag = #oflag fields, |
| 47 | cflag = #cflag fields, |
| 48 | lflag = Posix.TTY.L.flags [Posix.TTY.L.clear (Posix.TTY.L.echo, #lflag fields), |
| 49 | Posix.TTY.L.echonl, |
| 50 | Posix.TTY.L.icanon], |
| 51 | cc = #cc fields, |
| 52 | ispeed = #ispeed fields, |
| 53 | ospeed = #ospeed fields}) |
| 54 | | NONE => NONE |
| 55 | |
| 56 | fun reset () = case termios of SOME termios => Posix.TTY.TC.setattr (tty, Posix.TTY.TC.sanow, termios) |
| 57 | | NONE => () |
| 58 | in |
| 59 | print " Password: "; |
| 60 | TextIO.flushOut TextIO.stdOut; |
| 61 | case termios' of SOME termios' => Posix.TTY.TC.setattr (tty, Posix.TTY.TC.sanow, termios') |
| 62 | | NONE => (); |
| 63 | case TextIO.inputLine TextIO.stdIn of |
| 64 | NONE => (reset (); |
| 65 | Aborted) |
| 66 | | SOME pass => |
| 67 | (print "Confirm password: "; |
| 68 | TextIO.flushOut TextIO.stdOut; |
| 69 | case TextIO.inputLine TextIO.stdIn of |
| 70 | NONE => (reset (); |
| 71 | Aborted) |
| 72 | | SOME pass' => |
| 73 | (reset (); |
| 74 | if pass = pass' then |
| 75 | Passwd (String.substring (pass, 0, size pass - 1)) |
| 76 | else |
| 77 | (print "Passwords don't match!\n"; |
| 78 | Error))) |
| 79 | end |
| 80 | |
| 81 | end |