1f29af3b2SXin LI.\" $OpenBSD: readpassphrase.3,v 1.17 2007/05/31 19:19:28 jmc Exp $ 213d98e8cSBrian Feldman.\" 3f29af3b2SXin LI.\" Copyright (c) 2000, 2002 Todd C. Miller <Todd.Miller@courtesan.com> 413d98e8cSBrian Feldman.\" 5f29af3b2SXin LI.\" Permission to use, copy, modify, and distribute this software for any 6f29af3b2SXin LI.\" purpose with or without fee is hereby granted, provided that the above 7f29af3b2SXin LI.\" copyright notice and this permission notice appear in all copies. 813d98e8cSBrian Feldman.\" 9f29af3b2SXin LI.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10f29af3b2SXin LI.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11f29af3b2SXin LI.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12f29af3b2SXin LI.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13f29af3b2SXin LI.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14f29af3b2SXin LI.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15f29af3b2SXin LI.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16f29af3b2SXin LI.\" 17f29af3b2SXin LI.\" Sponsored in part by the Defense Advanced Research Projects 18f29af3b2SXin LI.\" Agency (DARPA) and Air Force Research Laboratory, Air Force 19f29af3b2SXin LI.\" Materiel Command, USAF, under agreement number F39502-99-1-0512. 2013d98e8cSBrian Feldman.\" 21f29af3b2SXin LI.Dd May 31, 2007 2213d98e8cSBrian Feldman.Dt READPASSPHRASE 3 2313d98e8cSBrian Feldman.Os 2413d98e8cSBrian Feldman.Sh NAME 2513d98e8cSBrian Feldman.Nm readpassphrase 2613d98e8cSBrian Feldman.Nd get a passphrase from the user 2713d98e8cSBrian Feldman.Sh SYNOPSIS 28f3a90e87SRuslan Ermilov.In readpassphrase.h 29f3a90e87SRuslan Ermilov.Ft "char *" 3013d98e8cSBrian Feldman.Fn readpassphrase "const char *prompt" "char *buf" "size_t bufsiz" "int flags" 3113d98e8cSBrian Feldman.Sh DESCRIPTION 3213d98e8cSBrian FeldmanThe 3313d98e8cSBrian Feldman.Fn readpassphrase 3413d98e8cSBrian Feldmanfunction displays a prompt to, and reads in a passphrase from, 3513d98e8cSBrian Feldman.Pa /dev/tty . 3613d98e8cSBrian FeldmanIf this file is inaccessible 3713d98e8cSBrian Feldmanand the 3813d98e8cSBrian Feldman.Dv RPP_REQUIRE_TTY 3913d98e8cSBrian Feldmanflag is not set, 4013d98e8cSBrian Feldman.Fn readpassphrase 4113d98e8cSBrian Feldmandisplays the prompt on the standard error output and reads from the standard 4213d98e8cSBrian Feldmaninput. 4313d98e8cSBrian FeldmanIn this case it is generally not possible to turn off echo. 4413d98e8cSBrian Feldman.Pp 4513d98e8cSBrian FeldmanUp to 4613d98e8cSBrian Feldman.Fa bufsiz 47f3a90e87SRuslan Ermilov\- 1 characters (one is for the 48f3a90e87SRuslan Ermilov.Dv NUL ) 49f3a90e87SRuslan Ermilovare read into the provided buffer 5013d98e8cSBrian Feldman.Fa buf . 5113d98e8cSBrian FeldmanAny additional 5213d98e8cSBrian Feldmancharacters and the terminating newline (or return) character are discarded. 5313d98e8cSBrian Feldman.Pp 5495f4226bSRuslan ErmilovThe 5513d98e8cSBrian Feldman.Fn readpassphrase 5695f4226bSRuslan Ermilovfunction 5713d98e8cSBrian Feldmantakes the following optional 5813d98e8cSBrian Feldman.Fa flags : 5913d98e8cSBrian Feldman.Pp 60f3a90e87SRuslan Ermilov.Bl -tag -width ".Dv RPP_REQUIRE_TTY" -compact 61f3a90e87SRuslan Ermilov.It Dv RPP_ECHO_OFF 62f3a90e87SRuslan Ermilovturn off echo (default behavior) 63f3a90e87SRuslan Ermilov.It Dv RPP_ECHO_ON 64f3a90e87SRuslan Ermilovleave echo on 65f3a90e87SRuslan Ermilov.It Dv RPP_REQUIRE_TTY 66f3a90e87SRuslan Ermilovfail if there is no tty 67f3a90e87SRuslan Ermilov.It Dv RPP_FORCELOWER 68f3a90e87SRuslan Ermilovforce input to lower case 69f3a90e87SRuslan Ermilov.It Dv RPP_FORCEUPPER 70f3a90e87SRuslan Ermilovforce input to upper case 71f3a90e87SRuslan Ermilov.It Dv RPP_SEVENBIT 72f3a90e87SRuslan Ermilovstrip the high bit from input 73f29af3b2SXin LI.It Dv RPP_STDIN 74f29af3b2SXin LIforce read of passphrase from stdin 75f3a90e87SRuslan Ermilov.El 7613d98e8cSBrian Feldman.Pp 7713d98e8cSBrian FeldmanThe calling process should zero the passphrase as soon as possible to 7813d98e8cSBrian Feldmanavoid leaving the cleartext passphrase visible in the process's address 7913d98e8cSBrian Feldmanspace. 8013d98e8cSBrian Feldman.Sh RETURN VALUES 81a7a8a766SBrian FeldmanUpon successful completion, 8213d98e8cSBrian Feldman.Fn readpassphrase 83f29af3b2SXin LIreturns a pointer to the NUL-terminated passphrase. 84a7a8a766SBrian FeldmanIf an error is encountered, the terminal state is restored and 85f3a90e87SRuslan Ermilova 86f3a90e87SRuslan Ermilov.Dv NULL 87f3a90e87SRuslan Ermilovpointer is returned. 8824a0682cSRuslan Ermilov.Sh FILES 8924a0682cSRuslan Ermilov.Bl -tag -width ".Pa /dev/tty" -compact 9024a0682cSRuslan Ermilov.It Pa /dev/tty 91a7a8a766SBrian Feldman.El 9213d98e8cSBrian Feldman.Sh EXAMPLES 9313d98e8cSBrian FeldmanThe following code fragment will read a passphrase from 9413d98e8cSBrian Feldman.Pa /dev/tty 9513d98e8cSBrian Feldmaninto the buffer 9613d98e8cSBrian Feldman.Fa passbuf . 9713d98e8cSBrian Feldman.Bd -literal -offset indent 9813d98e8cSBrian Feldmanchar passbuf[1024]; 9913d98e8cSBrian Feldman 10013d98e8cSBrian Feldman\&... 10113d98e8cSBrian Feldman 10213d98e8cSBrian Feldmanif (readpassphrase("Response: ", passbuf, sizeof(passbuf), 10313d98e8cSBrian Feldman RPP_REQUIRE_TTY) == NULL) 10413d98e8cSBrian Feldman errx(1, "unable to read passphrase"); 10513d98e8cSBrian Feldman 10613d98e8cSBrian Feldmanif (compare(transform(passbuf), epass) != 0) 10713d98e8cSBrian Feldman errx(1, "bad passphrase"); 10813d98e8cSBrian Feldman 10913d98e8cSBrian Feldman\&... 11013d98e8cSBrian Feldman 11113d98e8cSBrian Feldmanmemset(passbuf, 0, sizeof(passbuf)); 11213d98e8cSBrian Feldman.Ed 11324a0682cSRuslan Ermilov.Sh ERRORS 11424a0682cSRuslan Ermilov.Bl -tag -width Er 11524a0682cSRuslan Ermilov.It Bq Er EINTR 11624a0682cSRuslan ErmilovThe 11724a0682cSRuslan Ermilov.Fn readpassphrase 11824a0682cSRuslan Ermilovfunction was interrupted by a signal. 11924a0682cSRuslan Ermilov.It Bq Er EINVAL 12024a0682cSRuslan ErmilovThe 121f29af3b2SXin LI.Ar bufsiz 12224a0682cSRuslan Ermilovargument was zero. 12324a0682cSRuslan Ermilov.It Bq Er EIO 12424a0682cSRuslan ErmilovThe process is a member of a background process attempting to read 12524a0682cSRuslan Ermilovfrom its controlling terminal, the process is ignoring or blocking 12624a0682cSRuslan Ermilovthe 12724a0682cSRuslan Ermilov.Dv SIGTTIN 128f29af3b2SXin LIsignal, or the process group is orphaned. 12924a0682cSRuslan Ermilov.It Bq Er EMFILE 13024a0682cSRuslan ErmilovThe process has already reached its limit for open file descriptors. 13124a0682cSRuslan Ermilov.It Bq Er ENFILE 13224a0682cSRuslan ErmilovThe system file table is full. 13324a0682cSRuslan Ermilov.It Bq Er ENOTTY 13424a0682cSRuslan ErmilovThere is no controlling terminal and the 13524a0682cSRuslan Ermilov.Dv RPP_REQUIRE_TTY 13624a0682cSRuslan Ermilovflag was specified. 13713d98e8cSBrian Feldman.El 138f29af3b2SXin LI.Sh SIGNALS 139f29af3b2SXin LIThe 140f29af3b2SXin LI.Fn readpassphrase 141f29af3b2SXin LIfunction 142f29af3b2SXin LIwill catch the following signals: 143f29af3b2SXin LI.Bd -literal -offset indent 144f29af3b2SXin LISIGALRM SIGHUP SIGINT 145f29af3b2SXin LISIGPIPE SIGQUIT SIGTERM 146f29af3b2SXin LISIGTSTP SIGTTIN SIGTTOU 147f29af3b2SXin LI.Ed 148f29af3b2SXin LI.Pp 149f29af3b2SXin LIWhen one of the above signals is intercepted, terminal echo will 150f29af3b2SXin LIbe restored if it had previously been turned off. 151f29af3b2SXin LIIf a signal handler was installed for the signal when 152f29af3b2SXin LI.Fn readpassphrase 153f29af3b2SXin LIwas called, that handler is then executed. 154f29af3b2SXin LIIf no handler was previously installed for the signal then the 155f29af3b2SXin LIdefault action is taken as per 156f29af3b2SXin LI.Xr sigaction 2 . 157f29af3b2SXin LI.Pp 158f29af3b2SXin LIThe 159f29af3b2SXin LI.Dv SIGTSTP , SIGTTIN 160f29af3b2SXin LIand 161f29af3b2SXin LI.Dv SIGTTOU 162f29af3b2SXin LIsignals (stop signals generated from keyboard or due to terminal I/O 163f29af3b2SXin LIfrom a background process) are treated specially. 164f29af3b2SXin LIWhen the process is resumed after it has been stopped, 165f29af3b2SXin LI.Fn readpassphrase 166f29af3b2SXin LIwill reprint the prompt and the user may then enter a passphrase. 16713d98e8cSBrian Feldman.Sh SEE ALSO 168a7a8a766SBrian Feldman.Xr sigaction 2 , 16913d98e8cSBrian Feldman.Xr getpass 3 170a7a8a766SBrian Feldman.Sh STANDARDS 171a7a8a766SBrian FeldmanThe 172a7a8a766SBrian Feldman.Fn readpassphrase 173a7a8a766SBrian Feldmanfunction is an 174a7a8a766SBrian Feldmanextension and should not be used if portability is desired. 17513d98e8cSBrian Feldman.Sh HISTORY 17613d98e8cSBrian FeldmanThe 17713d98e8cSBrian Feldman.Fn readpassphrase 17813d98e8cSBrian Feldmanfunction first appeared in 179*60cc4a3eSDavid E. O'Brien.Fx 4.6 180*60cc4a3eSDavid E. O'Brienand 18113d98e8cSBrian Feldman.Ox 2.9 . 182