bb_askpass.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Ask for a password
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* do nothing signal handler */
  11. static void askpass_timeout(int UNUSED_PARAM ignore)
  12. {
  13. }
  14. char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt)
  15. {
  16. #define MAX_LINE 0xfff
  17. char *ret;
  18. int i;
  19. struct sigaction sa, oldsa;
  20. struct termios tio, oldtio;
  21. tcflush(fd, TCIFLUSH);
  22. /* Was buggy: was printing prompt *before* flushing input,
  23. * which was upsetting "expect" based scripts of some users.
  24. */
  25. fputs_stdout(prompt);
  26. fflush_all();
  27. tcgetattr(fd, &oldtio);
  28. tio = oldtio;
  29. /* Switch off echo. ECHOxyz meaning:
  30. * ECHO echo input chars
  31. * ECHOE echo BS-SP-BS on erase character
  32. * ECHOK echo kill char specially, not as ^c (ECHOKE controls how exactly)
  33. * ECHOKE erase all input via BS-SP-BS on kill char (else go to next line)
  34. * ECHOCTL Echo ctrl chars as ^c (else echo verbatim:
  35. * e.g. up arrow emits "ESC-something" and thus moves cursor up!)
  36. * ECHONL Echo NL even if ECHO is not set
  37. * ECHOPRT On erase, echo erased chars
  38. * [qwe<BS><BS><BS> input looks like "qwe\ewq/" on screen]
  39. */
  40. tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
  41. tcsetattr(fd, TCSANOW, &tio);
  42. memset(&sa, 0, sizeof(sa));
  43. /* sa.sa_flags = 0; - no SA_RESTART! */
  44. /* SIGINT and SIGALRM will interrupt reads below */
  45. sa.sa_handler = askpass_timeout;
  46. sigaction(SIGINT, &sa, &oldsa);
  47. if (timeout) {
  48. sigaction_set(SIGALRM, &sa);
  49. alarm(timeout);
  50. }
  51. ret = NULL;
  52. i = 0;
  53. while (1) {
  54. int r;
  55. /* User input is uber-slow, no need to optimize reallocs.
  56. * Grow it on every char.
  57. */
  58. ret = xrealloc(ret, i + 2);
  59. r = read(fd, &ret[i], 1);
  60. if ((i == 0 && r == 0) /* EOF (^D) with no password */
  61. || r < 0 /* read is interrupted by timeout or ^C */
  62. ) {
  63. ret[i] = '\0'; /* paranoia */
  64. nuke_str(ret); /* paranoia */
  65. free(ret);
  66. ret = NULL;
  67. break;
  68. }
  69. if (r == 0 /* EOF */
  70. || ret[i] == '\r' || ret[i] == '\n' /* EOL */
  71. || ++i == MAX_LINE /* line limit */
  72. ) {
  73. ret[i] = '\0';
  74. break;
  75. }
  76. }
  77. if (timeout) {
  78. alarm(0);
  79. }
  80. sigaction_set(SIGINT, &oldsa);
  81. tcsetattr(fd, TCSANOW, &oldtio);
  82. bb_putchar('\n');
  83. fflush_all();
  84. return ret;
  85. }
  86. char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
  87. {
  88. return bb_ask_noecho(STDIN_FILENO, 0, prompt);
  89. }