READPASSPHRASE(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’… READPASSPHRASE(3) β€” like, the library functions manual bestie

okay so real talk: this is readpassphrase from section 3 of the manual (that's the library-functions girlies ✨). She's the one you call when you need a passphrase from a human and you don't want it just floating around on screen for everyone to see. Very main character, very discreet. πŸŽ€


🌸 NAME 🌸

readpassphrase β€” get a passphrase from the user, bestie. That's it, that's the vibe.


✨ SYNOPSIS ✨

#include <readpassphrase.h>

char *
readpassphrase(const char *prompt, char *buf, size_t bufsiz, int flags);

πŸ’– DESCRIPTION πŸ’–

so the readpassphrase() function pops a prompt on screen and reads a passphrase from /dev/tty. Like, from the actual terminal, not your messy redirected stdin. iconic behaviour honestly.

but !! here's the tea πŸ«– β€” if that file is inaccessible and the RPP_REQUIRE_TTY flag is not set, then readpassphrase() displays the prompt on the standard error output and reads from standard input instead. and in that case? yeah, it's generally not possible to turn off echo. 😭 your characters are gonna be visible, ngl.

up to bufsiz - 1 characters get read into the buffer buf (the minus one is for the NUL, she needs her space 🫢). any extra characters and the terminating newline (or return) character get discarded. bye.

the readpassphrase() function takes these optional flags:

  • RPP_ECHO_OFF β€” turn off echo. this is the default behavior, no cap.
  • RPP_ECHO_ON β€” leave echo on. bold choice, but ok.
  • RPP_REQUIRE_TTY β€” fail if there's no tty. she's not doing charity work.
  • RPP_FORCELOWER β€” force input to lower case.
  • RPP_FORCEUPPER β€” force input to upper case.
  • RPP_SEVENBIT β€” strip the high bit from input.
  • RPP_STDIN β€” force read of passphrase from stdin.
ok so this part is important, listen up πŸ‘‘ β€” the calling process should zero the passphrase as soon as possible, to avoid leaving the cleartext passphrase visible in the process's address space. like, don't just leave it lying around. that's giving security hazard energy. πŸ’€

🌈 RETURN VALUES 🌈

upon successful completion, readpassphrase() returns a pointer to the NUL-terminated passphrase. πŸŽ‰

if an error is encountered, the terminal state is restored and a NULL pointer is returned. so she cleans up after herself, that's classy.


πŸ¦‹ FILES πŸ¦‹

  • /dev/tty

πŸ“ EXAMPLES πŸ“

the following code fragment will read a passphrase from /dev/tty into the buffer passbuf. look at her go ✨

char passbuf[1024];

...

if (readpassphrase("Response: ", passbuf, sizeof(passbuf),
    RPP_REQUIRE_TTY) == NULL)
        errx(1, "unable to read passphrase");

if (compare(transform(passbuf), epass) != 0)
        errx(1, "bad passphrase");

...

memset(passbuf, 0, sizeof(passbuf));

😭 ERRORS 😭

  • [EINTR] β€” the readpassphrase() function was interrupted by a signal. rude.
  • [EINVAL] β€” the bufsiz argument was zero. girl. zero? zero?! 😭
  • [EIO] β€” the process is a member of a background process attempting to read from its controlling terminal, the process is ignoring or blocking the SIGTTIN signal, or the process group is orphaned.
  • [EMFILE] β€” the process has already reached its limit for open file descriptors.
  • [ENFILE] β€” the system file table is full. full full.
  • [ENOTTY] β€” there is no controlling terminal and the RPP_REQUIRE_TTY flag was specified.

πŸ”₯ SIGNALS πŸ”₯

the readpassphrase() function will catch the following signals:

SIGALRM         SIGHUP          SIGINT
SIGPIPE         SIGQUIT         SIGTERM
SIGTSTP         SIGTTIN         SIGTTOU

when one of the above signals is intercepted, terminal echo will be restored if it had previously been turned off. mhm, she puts it back πŸ’…

if a signal handler was installed for the signal when readpassphrase() was called, that handler is then executed. if no handler was previously installed for the signal then the default action is taken as per sigaction(2).

and here's the special-guest part ⭐ β€” the SIGTSTP, SIGTTIN and SIGTTOU signals (stop signals generated from keyboard or due to terminal I/O from a background process) are treated specially. when the process is resumed after it has been stopped, readpassphrase() will reprint the prompt and the user may then enter a passphrase. like she literally picks up right where she left off, obsessed.


🧚 SEE ALSO 🧚


πŸ’œ STANDARDS πŸ’œ

the readpassphrase() function is an extension and should not be used if portability is desired. so like, she's not everywhere, bestie. check if your platform's invited to the party first. πŸ™ˆ


πŸ‘‘ HISTORY πŸ‘‘

the readpassphrase() function first appeared in FreeBSD 4.6 and OpenBSD 2.9.


and that's the whole page, no notes πŸ’– this cutie comes to you from the Debian crew, dated May 31, 2007 β€” yeah she's vintage, she's aged like fine wine, she's still serving. mwah 😘🫢

πŸ“¦ where this page comes from