2 * Copyright (c) 1996, 1998-2005, 2007-2018
3 * Todd C. Miller <Todd.Miller@sudo.ws>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Sponsored in part by the Defense Advanced Research Projects
18 * Agency (DARPA) and Air Force Research Laboratory, Air Force
19 * Materiel Command, USAF, under agreement number F39502-99-1-0512.
23 * This is an open source non-commercial project. Dear PVS-Studio, please check it.
24 * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
33 #include <sys/types.h>
39 #endif /* HAVE_STRING_H */
42 #endif /* HAVE_STRINGS_H */
50 #include "sudo_plugin.h"
52 enum tgetpass_errval {
55 TGP_ERRVAL_NOPASSWORD,
59 static volatile sig_atomic_t signo[NSIG];
61 static bool tty_present(void);
62 static void tgetpass_handler(int);
63 static char *getln(int, char *, size_t, int, enum tgetpass_errval *);
64 static char *sudo_askpass(const char *, const char *);
67 suspend(int signo, struct sudo_conv_callback *callback)
70 debug_decl(suspend, SUDO_DEBUG_CONV)
72 if (callback != NULL && SUDO_API_VERSION_GET_MAJOR(callback->version) != SUDO_CONV_CALLBACK_VERSION_MAJOR) {
73 sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
74 "callback major version mismatch, expected %u, got %u",
75 SUDO_CONV_CALLBACK_VERSION_MAJOR,
76 SUDO_API_VERSION_GET_MAJOR(callback->version));
80 if (callback != NULL && callback->on_suspend != NULL) {
81 if (callback->on_suspend(signo, callback->closure) == -1)
84 kill(getpid(), signo);
85 if (callback != NULL && callback->on_resume != NULL) {
86 if (callback->on_resume(signo, callback->closure) == -1)
89 debug_return_int(ret);
93 tgetpass_display_error(enum tgetpass_errval errval)
95 debug_decl(tgetpass_display_error, SUDO_DEBUG_CONV)
98 case TGP_ERRVAL_NOERROR:
100 case TGP_ERRVAL_TIMEOUT:
101 sudo_warnx(U_("timed out reading password"));
103 case TGP_ERRVAL_NOPASSWORD:
104 sudo_warnx(U_("no password was provided"));
106 case TGP_ERRVAL_READERROR:
107 sudo_warn(U_("unable to read password"));
114 * Like getpass(3) but with timeout and echo flags.
117 tgetpass(const char *prompt, int timeout, int flags,
118 struct sudo_conv_callback *callback)
120 struct sigaction sa, savealrm, saveint, savehup, savequit, saveterm;
121 struct sigaction savetstp, savettin, savettou;
123 static const char *askpass;
124 static char buf[SUDO_CONV_REPL_MAX + 1];
125 int i, input, output, save_errno, neednl = 0, need_restart;
126 enum tgetpass_errval errval;
127 debug_decl(tgetpass, SUDO_DEBUG_CONV)
129 (void) fflush(stdout);
131 if (askpass == NULL) {
132 askpass = getenv_unhooked("SUDO_ASKPASS");
133 if (askpass == NULL || *askpass == '\0')
134 askpass = sudo_conf_askpass_path();
137 /* If no tty present and we need to disable echo, try askpass. */
138 if (!ISSET(flags, TGP_STDIN|TGP_ECHO|TGP_ASKPASS|TGP_NOECHO_TRY) &&
140 if (askpass == NULL || getenv_unhooked("DISPLAY") == NULL) {
141 sudo_warnx(U_("no tty present and no askpass program specified"));
142 debug_return_str(NULL);
144 SET(flags, TGP_ASKPASS);
147 /* If using a helper program to get the password, run it instead. */
148 if (ISSET(flags, TGP_ASKPASS)) {
149 if (askpass == NULL || *askpass == '\0')
150 sudo_fatalx(U_("no askpass program specified, try setting SUDO_ASKPASS"));
151 debug_return_str_masked(sudo_askpass(askpass, prompt));
155 for (i = 0; i < NSIG; i++)
160 /* Open /dev/tty for reading/writing if possible else use stdin/stderr. */
161 if (ISSET(flags, TGP_STDIN) ||
162 (input = output = open(_PATH_TTY, O_RDWR)) == -1) {
163 input = STDIN_FILENO;
164 output = STDERR_FILENO;
168 * If we are using a tty but are not the foreground pgrp this will
169 * return EINTR. We send ourself SIGTTOU bracketed by callbacks.
171 if (!ISSET(flags, TGP_ECHO)) {
173 if (ISSET(flags, TGP_MASK))
174 neednl = sudo_term_cbreak(input);
176 neednl = sudo_term_noecho(input);
177 if (neednl || errno != EINTR)
179 /* Received SIGTTOU, suspend the process. */
180 if (suspend(SIGTTOU, callback) == -1) {
181 if (input != STDIN_FILENO)
183 debug_return_ptr(NULL);
189 * Catch signals that would otherwise cause the user to end
190 * up with echo turned off in the shell.
192 memset(&sa, 0, sizeof(sa));
193 sigemptyset(&sa.sa_mask);
194 sa.sa_flags = 0; /* don't restart system calls */
195 sa.sa_handler = tgetpass_handler;
196 (void) sigaction(SIGALRM, &sa, &savealrm);
197 (void) sigaction(SIGINT, &sa, &saveint);
198 (void) sigaction(SIGHUP, &sa, &savehup);
199 (void) sigaction(SIGQUIT, &sa, &savequit);
200 (void) sigaction(SIGTERM, &sa, &saveterm);
201 (void) sigaction(SIGTSTP, &sa, &savetstp);
202 (void) sigaction(SIGTTIN, &sa, &savettin);
203 (void) sigaction(SIGTTOU, &sa, &savettou);
206 if (write(output, prompt, strlen(prompt)) == -1)
212 pass = getln(input, buf, sizeof(buf), ISSET(flags, TGP_MASK), &errval);
216 if (neednl || pass == NULL) {
217 if (write(output, "\n", 1) == -1)
220 tgetpass_display_error(errval);
223 /* Restore old signal handlers. */
224 (void) sigaction(SIGALRM, &savealrm, NULL);
225 (void) sigaction(SIGINT, &saveint, NULL);
226 (void) sigaction(SIGHUP, &savehup, NULL);
227 (void) sigaction(SIGQUIT, &savequit, NULL);
228 (void) sigaction(SIGTERM, &saveterm, NULL);
229 (void) sigaction(SIGTSTP, &savetstp, NULL);
230 (void) sigaction(SIGTTIN, &savettin, NULL);
231 (void) sigaction(SIGTTOU, &savettou, NULL);
233 /* Restore old tty settings. */
234 if (!ISSET(flags, TGP_ECHO)) {
235 /* Restore old tty settings if possible. */
236 (void) sudo_term_restore(input, true);
238 if (input != STDIN_FILENO)
242 * If we were interrupted by a signal, resend it to ourselves
243 * now that we have restored the signal handlers.
245 for (i = 0; i < NSIG; i++) {
253 if (suspend(i, callback) == 0)
268 debug_return_str_masked(pass);
272 * Fork a child and exec sudo-askpass to get the password from the user.
275 sudo_askpass(const char *askpass, const char *prompt)
277 static char buf[SUDO_CONV_REPL_MAX + 1], *pass;
278 struct sigaction sa, savechld;
279 enum tgetpass_errval errval;
282 debug_decl(sudo_askpass, SUDO_DEBUG_CONV)
284 /* Set SIGCHLD handler to default since we call waitpid() below. */
285 memset(&sa, 0, sizeof(sa));
286 sigemptyset(&sa.sa_mask);
287 sa.sa_flags = SA_RESTART;
288 sa.sa_handler = SIG_DFL;
289 (void) sigaction(SIGCHLD, &sa, &savechld);
292 sudo_fatal(U_("unable to create pipe"));
294 child = sudo_debug_fork();
296 sudo_fatal(U_("unable to fork"));
299 /* child, point stdout to output side of the pipe and exec askpass */
300 if (dup2(pfd[1], STDOUT_FILENO) == -1) {
304 if (setuid(ROOT_UID) == -1)
305 sudo_warn("setuid(%d)", ROOT_UID);
306 if (setgid(user_details.gid)) {
307 sudo_warn(U_("unable to set gid to %u"), (unsigned int)user_details.gid);
310 if (setuid(user_details.uid)) {
311 sudo_warn(U_("unable to set uid to %u"), (unsigned int)user_details.uid);
314 closefrom(STDERR_FILENO + 1);
315 execl(askpass, askpass, prompt, (char *)NULL);
316 sudo_warn(U_("unable to run %s"), askpass);
320 /* Get response from child (askpass). */
321 (void) close(pfd[1]);
322 pass = getln(pfd[0], buf, sizeof(buf), 0, &errval);
323 (void) close(pfd[0]);
325 tgetpass_display_error(errval);
327 /* Wait for child to exit. */
329 pid_t rv = waitpid(child, &status, 0);
330 if (rv == -1 && errno != EINTR)
332 if (rv != -1 && !WIFSTOPPED(status))
337 errno = EINTR; /* make cancel button simulate ^C */
339 /* Restore saved SIGCHLD handler. */
340 (void) sigaction(SIGCHLD, &savechld, NULL);
342 debug_return_str_masked(pass);
345 extern int sudo_term_eof, sudo_term_erase, sudo_term_kill;
348 getln(int fd, char *buf, size_t bufsiz, int feedback,
349 enum tgetpass_errval *errval)
351 size_t left = bufsiz;
355 debug_decl(getln, SUDO_DEBUG_CONV)
357 *errval = TGP_ERRVAL_NOERROR;
360 *errval = TGP_ERRVAL_READERROR;
362 debug_return_str(NULL); /* sanity */
366 nr = read(fd, &c, 1);
367 if (nr != 1 || c == '\n' || c == '\r')
370 if (c == sudo_term_eof) {
373 } else if (c == sudo_term_kill) {
375 if (write(fd, "\b \b", 3) == -1)
381 } else if (c == sudo_term_erase) {
383 if (write(fd, "\b \b", 3) == -1)
390 ignore_result(write(fd, "*", 1));
398 if (write(fd, "\b \b", 3) == -1)
407 if (errno == EINTR) {
408 if (signo[SIGALRM] == 1)
409 *errval = TGP_ERRVAL_TIMEOUT;
411 *errval = TGP_ERRVAL_READERROR;
413 debug_return_str(NULL);
415 /* EOF is only an error if no bytes were read. */
416 if (left == bufsiz - 1) {
417 *errval = TGP_ERRVAL_NOPASSWORD;
418 debug_return_str(NULL);
422 debug_return_str_masked(buf);
427 tgetpass_handler(int s)
435 #if defined(HAVE_KINFO_PROC2_NETBSD) || defined(HAVE_KINFO_PROC_OPENBSD) || defined(HAVE_KINFO_PROC_FREEBSD) || defined(HAVE_KINFO_PROC_44BSD) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
436 debug_decl(tty_present, SUDO_DEBUG_UTIL)
437 debug_return_bool(user_details.tty != NULL);
440 debug_decl(tty_present, SUDO_DEBUG_UTIL)
442 if ((fd = open(_PATH_TTY, O_RDWR)) != -1)
444 debug_return_bool(fd != -1);