getpass.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* Copyright (C) 1992-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <stdio_ext.h>
  16. #include <string.h> /* For string function builtin redirect. */
  17. #include <termios.h>
  18. #include <unistd.h>
  19. #include <wchar.h>
  20. #define flockfile(s) _IO_flockfile (s)
  21. #define funlockfile(s) _IO_funlockfile (s)
  22. #include <libc-lock.h>
  23. /* It is desirable to use this bit on systems that have it.
  24. The only bit of terminal state we want to twiddle is echoing, which is
  25. done in software; there is no need to change the state of the terminal
  26. hardware. */
  27. #ifndef TCSASOFT
  28. #define TCSASOFT 0
  29. #endif
  30. static void
  31. call_fclose (void *arg)
  32. {
  33. if (arg != NULL)
  34. fclose (arg);
  35. }
  36. char *
  37. getpass (const char *prompt)
  38. {
  39. FILE *in, *out;
  40. struct termios s, t;
  41. int tty_changed;
  42. static char *buf;
  43. static size_t bufsize;
  44. ssize_t nread;
  45. /* Try to write to and read from the terminal if we can.
  46. If we can't open the terminal, use stderr and stdin. */
  47. in = fopen ("/dev/tty", "w+ce");
  48. if (in == NULL)
  49. {
  50. in = stdin;
  51. out = stderr;
  52. }
  53. else
  54. {
  55. /* We do the locking ourselves. */
  56. __fsetlocking (in, FSETLOCKING_BYCALLER);
  57. out = in;
  58. }
  59. /* Make sure the stream we opened is closed even if the thread is
  60. canceled. */
  61. __libc_cleanup_push (call_fclose, in == out ? in : NULL);
  62. flockfile (out);
  63. /* Turn echoing off if it is on now. */
  64. if (__tcgetattr (fileno (in), &t) == 0)
  65. {
  66. /* Save the old one. */
  67. s = t;
  68. /* Tricky, tricky. */
  69. t.c_lflag &= ~(ECHO|ISIG);
  70. tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
  71. }
  72. else
  73. tty_changed = 0;
  74. /* Write the prompt. */
  75. __fxprintf (out, "%s", prompt);
  76. __fflush_unlocked (out);
  77. /* Read the password. */
  78. nread = __getline (&buf, &bufsize, in);
  79. if (buf != NULL)
  80. {
  81. if (nread < 0)
  82. buf[0] = '\0';
  83. else if (buf[nread - 1] == '\n')
  84. {
  85. /* Remove the newline. */
  86. buf[nread - 1] = '\0';
  87. if (tty_changed)
  88. /* Write the newline that was not echoed. */
  89. __fxprintf (out, "\n");
  90. }
  91. }
  92. /* Restore the original setting. */
  93. if (tty_changed)
  94. (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
  95. funlockfile (out);
  96. __libc_cleanup_pop (0);
  97. if (in != stdin)
  98. /* We opened the terminal; now close it. */
  99. fclose (in);
  100. return buf;
  101. }