readpass.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* $OpenBSD: readpass.c,v 1.63 2020/08/11 09:45:54 djm Exp $ */
  2. /*
  3. * Copyright (c) 2001 Markus Friedl. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #ifdef HAVE_PATHS_H
  31. # include <paths.h>
  32. #endif
  33. #include <signal.h>
  34. #include <stdarg.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <unistd.h>
  39. #include "xmalloc.h"
  40. #include "misc.h"
  41. #include "pathnames.h"
  42. #include "log.h"
  43. #include "ssh.h"
  44. #include "uidswap.h"
  45. static char *
  46. ssh_askpass(char *askpass, const char *msg, const char *env_hint)
  47. {
  48. pid_t pid, ret;
  49. size_t len;
  50. char *pass;
  51. int p[2], status;
  52. char buf[1024];
  53. void (*osigchld)(int);
  54. if (fflush(stdout) != 0)
  55. error("%s: fflush: %s", __func__, strerror(errno));
  56. if (askpass == NULL)
  57. fatal("internal error: askpass undefined");
  58. if (pipe(p) == -1) {
  59. error("%s: pipe: %s", __func__, strerror(errno));
  60. return NULL;
  61. }
  62. osigchld = ssh_signal(SIGCHLD, SIG_DFL);
  63. if ((pid = fork()) == -1) {
  64. error("%s: fork: %s", __func__, strerror(errno));
  65. ssh_signal(SIGCHLD, osigchld);
  66. return NULL;
  67. }
  68. if (pid == 0) {
  69. close(p[0]);
  70. if (dup2(p[1], STDOUT_FILENO) == -1)
  71. fatal("%s: dup2: %s", __func__, strerror(errno));
  72. if (env_hint != NULL)
  73. setenv("SSH_ASKPASS_PROMPT", env_hint, 1);
  74. execlp(askpass, askpass, msg, (char *)NULL);
  75. fatal("%s: exec(%s): %s", __func__, askpass, strerror(errno));
  76. }
  77. close(p[1]);
  78. len = 0;
  79. do {
  80. ssize_t r = read(p[0], buf + len, sizeof(buf) - 1 - len);
  81. if (r == -1 && errno == EINTR)
  82. continue;
  83. if (r <= 0)
  84. break;
  85. len += r;
  86. } while (sizeof(buf) - 1 - len > 0);
  87. buf[len] = '\0';
  88. close(p[0]);
  89. while ((ret = waitpid(pid, &status, 0)) == -1)
  90. if (errno != EINTR)
  91. break;
  92. ssh_signal(SIGCHLD, osigchld);
  93. if (ret == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  94. explicit_bzero(buf, sizeof(buf));
  95. return NULL;
  96. }
  97. buf[strcspn(buf, "\r\n")] = '\0';
  98. pass = xstrdup(buf);
  99. explicit_bzero(buf, sizeof(buf));
  100. return pass;
  101. }
  102. /* private/internal read_passphrase flags */
  103. #define RP_ASK_PERMISSION 0x8000 /* pass hint to askpass for confirm UI */
  104. /*
  105. * Reads a passphrase from /dev/tty with echo turned off/on. Returns the
  106. * passphrase (allocated with xmalloc). Exits if EOF is encountered. If
  107. * RP_ALLOW_STDIN is set, the passphrase will be read from stdin if no
  108. * tty is available
  109. */
  110. char *
  111. read_passphrase(const char *prompt, int flags)
  112. {
  113. char cr = '\r', *askpass = NULL, *ret, buf[1024];
  114. int rppflags, ttyfd, use_askpass = 0, allow_askpass = 0;
  115. const char *askpass_hint = NULL;
  116. const char *s;
  117. if ((s = getenv("DISPLAY")) != NULL)
  118. allow_askpass = *s != '\0';
  119. if ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) != NULL) {
  120. if (strcasecmp(s, "force") == 0) {
  121. use_askpass = 1;
  122. allow_askpass = 1;
  123. } else if (strcasecmp(s, "prefer") == 0)
  124. use_askpass = allow_askpass;
  125. else if (strcasecmp(s, "never") == 0)
  126. allow_askpass = 0;
  127. }
  128. rppflags = (flags & RP_ECHO) ? RPP_ECHO_ON : RPP_ECHO_OFF;
  129. if (use_askpass)
  130. debug("%s: requested to askpass", __func__);
  131. else if (flags & RP_USE_ASKPASS)
  132. use_askpass = 1;
  133. else if (flags & RP_ALLOW_STDIN) {
  134. if (!isatty(STDIN_FILENO)) {
  135. debug("read_passphrase: stdin is not a tty");
  136. use_askpass = 1;
  137. }
  138. } else {
  139. rppflags |= RPP_REQUIRE_TTY;
  140. ttyfd = open(_PATH_TTY, O_RDWR);
  141. if (ttyfd >= 0) {
  142. /*
  143. * If we're on a tty, ensure that show the prompt at
  144. * the beginning of the line. This will hopefully
  145. * clobber any password characters the user has
  146. * optimistically typed before echo is disabled.
  147. */
  148. (void)write(ttyfd, &cr, 1);
  149. close(ttyfd);
  150. } else {
  151. debug("read_passphrase: can't open %s: %s", _PATH_TTY,
  152. strerror(errno));
  153. use_askpass = 1;
  154. }
  155. }
  156. if ((flags & RP_USE_ASKPASS) && !allow_askpass)
  157. return (flags & RP_ALLOW_EOF) ? NULL : xstrdup("");
  158. if (use_askpass && allow_askpass) {
  159. if (getenv(SSH_ASKPASS_ENV))
  160. askpass = getenv(SSH_ASKPASS_ENV);
  161. else
  162. askpass = _PATH_SSH_ASKPASS_DEFAULT;
  163. if ((flags & RP_ASK_PERMISSION) != 0)
  164. askpass_hint = "confirm";
  165. if ((ret = ssh_askpass(askpass, prompt, askpass_hint)) == NULL)
  166. if (!(flags & RP_ALLOW_EOF))
  167. return xstrdup("");
  168. return ret;
  169. }
  170. if (readpassphrase(prompt, buf, sizeof buf, rppflags) == NULL) {
  171. if (flags & RP_ALLOW_EOF)
  172. return NULL;
  173. return xstrdup("");
  174. }
  175. ret = xstrdup(buf);
  176. explicit_bzero(buf, sizeof(buf));
  177. return ret;
  178. }
  179. int
  180. ask_permission(const char *fmt, ...)
  181. {
  182. va_list args;
  183. char *p, prompt[1024];
  184. int allowed = 0;
  185. va_start(args, fmt);
  186. vsnprintf(prompt, sizeof(prompt), fmt, args);
  187. va_end(args);
  188. p = read_passphrase(prompt,
  189. RP_USE_ASKPASS|RP_ALLOW_EOF|RP_ASK_PERMISSION);
  190. if (p != NULL) {
  191. /*
  192. * Accept empty responses and responses consisting
  193. * of the word "yes" as affirmative.
  194. */
  195. if (*p == '\0' || *p == '\n' ||
  196. strcasecmp(p, "yes") == 0)
  197. allowed = 1;
  198. free(p);
  199. }
  200. return (allowed);
  201. }
  202. struct notifier_ctx {
  203. pid_t pid;
  204. void (*osigchld)(int);
  205. };
  206. struct notifier_ctx *
  207. notify_start(int force_askpass, const char *fmt, ...)
  208. {
  209. va_list args;
  210. char *prompt = NULL;
  211. int devnull;
  212. pid_t pid;
  213. void (*osigchld)(int);
  214. const char *askpass, *s;
  215. struct notifier_ctx *ret = NULL;
  216. va_start(args, fmt);
  217. xvasprintf(&prompt, fmt, args);
  218. va_end(args);
  219. if (fflush(NULL) != 0)
  220. error("%s: fflush: %s", __func__, strerror(errno));
  221. if (!force_askpass && isatty(STDERR_FILENO)) {
  222. (void)write(STDERR_FILENO, "\r", 1);
  223. (void)write(STDERR_FILENO, prompt, strlen(prompt));
  224. (void)write(STDERR_FILENO, "\r\n", 2);
  225. goto out;
  226. }
  227. if ((askpass = getenv("SSH_ASKPASS")) == NULL)
  228. askpass = _PATH_SSH_ASKPASS_DEFAULT;
  229. if (*askpass == '\0') {
  230. debug3("%s: cannot notify: no askpass", __func__);
  231. goto out;
  232. }
  233. if (getenv("DISPLAY") == NULL &&
  234. ((s = getenv(SSH_ASKPASS_REQUIRE_ENV)) == NULL ||
  235. strcmp(s, "force") != 0)) {
  236. debug3("%s: cannot notify: no display", __func__);
  237. goto out;
  238. }
  239. osigchld = ssh_signal(SIGCHLD, SIG_DFL);
  240. if ((pid = fork()) == -1) {
  241. error("%s: fork: %s", __func__, strerror(errno));
  242. ssh_signal(SIGCHLD, osigchld);
  243. free(prompt);
  244. return NULL;
  245. }
  246. if (pid == 0) {
  247. if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1)
  248. fatal("%s: open %s", __func__, strerror(errno));
  249. if (dup2(devnull, STDIN_FILENO) == -1 ||
  250. dup2(devnull, STDOUT_FILENO) == -1)
  251. fatal("%s: dup2: %s", __func__, strerror(errno));
  252. closefrom(STDERR_FILENO + 1);
  253. setenv("SSH_ASKPASS_PROMPT", "none", 1); /* hint to UI */
  254. execlp(askpass, askpass, prompt, (char *)NULL);
  255. error("%s: exec(%s): %s", __func__, askpass, strerror(errno));
  256. _exit(1);
  257. /* NOTREACHED */
  258. }
  259. if ((ret = calloc(1, sizeof(*ret))) == NULL) {
  260. kill(pid, SIGTERM);
  261. fatal("%s: calloc failed", __func__);
  262. }
  263. ret->pid = pid;
  264. ret->osigchld = osigchld;
  265. out:
  266. free(prompt);
  267. return ret;
  268. }
  269. //void
  270. //notify_complete(struct notifier_ctx *ctx)
  271. //{
  272. // int ret;
  273. // if (ctx == NULL || ctx->pid <= 0) {
  274. // free(ctx);
  275. // return;
  276. // }
  277. // kill(ctx->pid, SIGTERM);
  278. // while ((ret = waitpid(ctx->pid, NULL, 0)) == -1) {
  279. // if (errno != EINTR)
  280. // break;
  281. // }
  282. // if (ret == -1)
  283. // fatal("%s: waitpid: %s", __func__, strerror(errno));
  284. // ssh_signal(SIGCHLD, ctx->osigchld);
  285. // free(ctx);
  286. //}