25

I could use getpass() to get a password. However, the man page says:

This function is obsolete. Do not use it.

What is the current way to get a password from the user's terminal without echoing it, in a POSIX-compliant way? [Originally I said "portably", but my intention was to avoid using an obsolete function.]

Jerry Penner
  • 1,339
  • 1
  • 12
  • 13
  • 4
    There is no portable way - this depends heavily on your platform. –  Jul 28 '09 at 20:16
  • 1
    @Jerry, it isn't worth it... even though it is LEGACY, it is the most portable way to go about it. – Michael Aaron Safyan Apr 14 '10 at 01:03
  • @MichaelAaronSafyan I agree. It's a strange decision to make `getpass()` obsolete and even removed from POSIX, without providing an alternative (like NetBSD's `getpass_r()`). Now everyone makes his/her own version, ridden with security/usability/portability bugs. – maxelost Jun 11 '22 at 03:02

6 Answers6

18

this should work on linux/macosx, a windows version should use Get/Set ConsoleMode

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

int
main(int argc, char **argv)
{
    struct termios oflags, nflags;
    char password[64];

    /* disabling echo */
    tcgetattr(fileno(stdin), &oflags);
    nflags = oflags;
    nflags.c_lflag &= ~ECHO;
    nflags.c_lflag |= ECHONL;

    if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
        perror("tcsetattr");
        return EXIT_FAILURE;
    }

    printf("password: ");
    fgets(password, sizeof(password), stdin);
    password[strlen(password) - 1] = 0;
    printf("you typed '%s'\n", password);

    /* restore terminal */
    if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
        perror("tcsetattr");
        return EXIT_FAILURE;
    }

    return 0;
}
dfa
  • 114,442
  • 31
  • 189
  • 228
  • This seems the most straightforward way to me. – Jerry Penner Jul 29 '09 at 19:23
  • 4
    You should use /dev/tty first before trying stdin, as if you're using a pipe, stdin would be the piped content, not the terminal input. – xryl669 Mar 12 '13 at 13:48
  • 4
    I wouldn't mind seeing char password[64] = {0}; or a memset before it is used if it is reentrant – Grady Player Apr 29 '14 at 18:17
  • 4
    `password[strlen(password) - 1] = 0;` - this is incredibly wrong. – ecatmur Sep 04 '15 at 11:39
  • @ecatmur It is to remove the newline. fgets already ensures the string fits and is nul-terminated. – stark Aug 01 '17 at 21:25
  • 2
    `password[strlen(password) - 1] = 0;` only removes the newline if it's there, else it wrongly removes the last character of the password. `password[ strcspn( password, "\n" ) ] = '\0'` [works all the time](https://port70.net/~nsz/c/c11/n1570.html#7.24.5.3). – Andrew Henle Aug 08 '19 at 23:08
5

You could use the ncurses library to read from standard input without echoing the results to the screen. (Call noecho() before getting any input). The library has been around for ages and works on a wide variety of platforms (the windows version can be found here)

indy
  • 759
  • 4
  • 4
  • I made a simple curses app (it's been a couple decaded since I used curses), and found that it cleared the screen when I did initscr(). There is probably some way around that, but it wasn't obvious from a quick perusal of the man pages. – Jerry Penner Jul 29 '09 at 19:25
5

Even though this is a very old question that has already been answered, here's what I've been using (which is very similar to the accepted answer):

#include <termios.h>
#include <cstdio>

//
// The following is a slightly modifed version taken from:
// http://www.gnu.org/software/libc/manual/html_node/getpass.html
//
ssize_t my_getpass (char *prompt, char **lineptr, size_t *n, FILE *stream)
{
    struct termios _old, _new;
    int nread;

    /* Turn echoing off and fail if we can’t. */
    if (tcgetattr (fileno (stream), &_old) != 0)
        return -1;
    _new = _old;
    _new.c_lflag &= ~ECHO;
    if (tcsetattr (fileno (stream), TCSAFLUSH, &_new) != 0)
        return -1;

    /* Display the prompt */
    if (prompt)
        printf("%s", prompt);

    /* Read the password. */
    nread = getline (lineptr, n, stream);

    /* Remove the carriage return */
    if (nread >= 1 && (*lineptr)[nread - 1] == '\n')
    {
        (*lineptr)[nread-1] = 0;
        nread--;
    }
    printf("\n");

    /* Restore terminal. */
    (void) tcsetattr (fileno (stream), TCSAFLUSH, &_old);

    return nread;
}

//
// Test harness - demonstrate calling my_getpass().
//
int main(int argc, char *argv[])
{
    size_t maxlen = 255;
    char pwd[maxlen];
    char *pPwd = pwd; // <-- haven't figured out how to avoid this.

    int count = my_getpass((char*)"Enter Password: ", &pPwd, &maxlen, stdin);

    printf("Size of password: %d\nPassword in plaintext: %s\n", count, pwd);

    return 0;
}
Vanessa Deagan
  • 417
  • 6
  • 15
  • Me gusta. This will even let you pipe in your password from the command line; however, if you're gonna do that then you may not want to print out a password prompt. – JesseTG Feb 28 '16 at 00:50
1

On windows, you can probably use the SetConsoleMode api, described here.

Brian
  • 25,523
  • 18
  • 82
  • 173
1

According to the University of Milwaukee's documentation it is obsolete because:

The getpass() function is not threadsafe because it manipulates global signal state.

The getpass() function is scheduled to be withdrawn from a future version of the X/Open CAE Specification.

Kredns
  • 36,461
  • 52
  • 152
  • 203
0

Another simple solution for Windows. Include "conio.h"

  for (;;) {
  int c = _getch();
  switch (c)
  {
  case '\r':
  case '\n':
  case EOF:
    _putch('\n');
    break;

  default:
    _putch('*'); //mask
    thePassword += char(c);
    continue;
  }
  break;
}
Marius Matioc
  • 537
  • 6
  • 7