1.\" $OpenBSD: readpassphrase.3,v 1.11 2003/05/30 17:21:06 jmc Exp $ 2.\" 3.\" Copyright (c) 2000 Todd C. Miller <Todd.Miller@courtesan.com> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. The name of the author may not be used to endorse or promote products 15.\" derived from this software without specific prior written permission. 16.\" 17.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 19.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 20.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21.\" EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27.\" 28.Dd December 7, 2001 29.Dt READPASSPHRASE 3 30.Os 31.Sh NAME 32.Nm readpassphrase 33.Nd get a passphrase from the user 34.Sh SYNOPSIS 35.Fd #include <readpassphrase.h> 36.Ft char * 37.Fn readpassphrase "const char *prompt" "char *buf" "size_t bufsiz" "int flags" 38.Sh DESCRIPTION 39The 40.Fn readpassphrase 41function displays a prompt to, and reads in a passphrase from, 42.Pa /dev/tty . 43If this file is inaccessible 44and the 45.Dv RPP_REQUIRE_TTY 46flag is not set, 47.Fn readpassphrase 48displays the prompt on the standard error output and reads from the standard 49input. 50In this case it is generally not possible to turn off echo. 51.Pp 52Up to 53.Fa bufsiz 54- 1 characters (one is for the NUL) are read into the provided buffer 55.Fa buf . 56Any additional 57characters and the terminating newline (or return) character are discarded. 58.Pp 59.Fn readpassphrase 60takes the following optional 61.Fa flags : 62.Pp 63.Bd -literal -offset indent -compact 64RPP_ECHO_OFF turn off echo (default behavior) 65RPP_ECHO_ON leave echo on 66RPP_REQUIRE_TTY fail if there is no tty 67RPP_FORCELOWER force input to lower case 68RPP_FORCEUPPER force input to upper case 69RPP_SEVENBIT strip the high bit from input 70RPP_STDIN force read of passphrase from stdin 71.Ed 72.Pp 73The calling process should zero the passphrase as soon as possible to 74avoid leaving the cleartext passphrase visible in the process's address 75space. 76.Sh RETURN VALUES 77Upon successful completion, 78.Fn readpassphrase 79returns a pointer to the null-terminated passphrase. 80If an error is encountered, the terminal state is restored and 81a null pointer is returned. 82.Sh FILES 83.Bl -tag -width /dev/tty -compact 84.It Pa /dev/tty 85.El 86.Sh EXAMPLES 87The following code fragment will read a passphrase from 88.Pa /dev/tty 89into the buffer 90.Fa passbuf . 91.Bd -literal -offset indent 92char passbuf[1024]; 93 94\&... 95 96if (readpassphrase("Response: ", passbuf, sizeof(passbuf), 97 RPP_REQUIRE_TTY) == NULL) 98 errx(1, "unable to read passphrase"); 99 100if (compare(transform(passbuf), epass) != 0) 101 errx(1, "bad passphrase"); 102 103\&... 104 105memset(passbuf, 0, sizeof(passbuf)); 106.Ed 107.Sh ERRORS 108.Bl -tag -width Er 109.It Bq Er EINTR 110The 111.Fn readpassphrase 112function was interrupted by a signal. 113.It Bq Er EINVAL 114The 115.Ar bufsiz 116argument was zero. 117.It Bq Er EIO 118The process is a member of a background process attempting to read 119from its controlling terminal, the process is ignoring or blocking 120the SIGTTIN signal or the process group is orphaned. 121.It Bq Er EMFILE 122The process has already reached its limit for open file descriptors. 123.It Bq Er ENFILE 124The system file table is full. 125.It Bq Er ENOTTY 126There is no controlling terminal and the 127.Dv RPP_REQUIRE_TTY 128flag was specified. 129.El 130.Sh SIGNALS 131.Fn readpassphrase 132will catch the following signals: 133.Pp 134.Bd -literal -offset indent -compact 135SIGALRM 136SIGHUP 137SIGINT 138SIGPIPE 139SIGQUIT 140SIGTERM 141SIGTSTP 142SIGTTIN 143SIGTTOU 144.Ed 145.Pp 146When one of the above signals is intercepted, terminal echo will 147be restored if it had previously been turned off. 148If a signal handler was installed for the signal when 149.Fn readpassphrase 150was called that handler is then executed. 151If no handler was previously installed for the signal then the 152default action is taken as per 153.Xr sigaction 2 . 154.Pp 155The 156.Dv SIGTSTP , 157.Dv SIGTTIN 158and 159.Dv SIGTTOU 160signals (stop signals generated from keyboard or due to terminal I/O 161from a background process) are treated specially. 162When the process is resumed after it has been stopped, 163.Fn readpassphrase 164will reprint the prompt and the user may then enter a passphrase. 165.Sh SEE ALSO 166.Xr sigaction 2 , 167.Xr getpass 3 168.Sh STANDARDS 169The 170.Fn readpassphrase 171function is an 172.Ox 173extension and should not be used if portability is desired. 174.Sh HISTORY 175The 176.Fn readpassphrase 177function first appeared in 178.Ox 2.9 . 179