Imported Upstream version 1.7.0
[debian/sudo] / tgetpass.c
index 2c94cdb11a26083cbaae183f56de0d823c4cfbaf..62faead2345f6c3d38cbbde484c0f6b42fde99f5 100644 (file)
@@ -1,5 +1,6 @@
 /*
- * Copyright (c) 1996, 1998-2005 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1996, 1998-2005, 2007-2008
+ *     Todd C. Miller <Todd.Miller@courtesan.com>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -70,7 +71,7 @@
 #include "sudo.h"
 
 #ifndef lint
-__unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.111.2.7 2008/06/21 00:27:01 millert Exp $";
+__unused static const char rcsid[] = "$Sudo: tgetpass.c,v 1.126 2008/12/09 20:55:49 millert Exp $";
 #endif /* lint */
 
 #ifndef TCSASOFT
@@ -114,6 +115,7 @@ static volatile sig_atomic_t signo;
 
 static void handler __P((int));
 static char *getln __P((int, char *, size_t));
+static char *sudo_askpass __P((const char *));
 
 /*
  * Like getpass(3) but with timeout and echo flags.
@@ -132,6 +134,11 @@ tgetpass(prompt, timeout, flags)
     int input, output, save_errno;
 
     (void) fflush(stdout);
+
+    /* If using a helper program to get the password, run it instead. */
+    if (ISSET(flags, TGP_ASKPASS) && user_askpass)
+       return(sudo_askpass(prompt));
+
 restart:
     signo = 0;
     pass = NULL;
@@ -147,6 +154,7 @@ restart:
      * Catch signals that would otherwise cause the user to end
      * up with echo turned off in the shell.
      */
+    zero_bytes(&sa, sizeof(sa));
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = SA_INTERRUPT;        /* don't restart system calls */
     sa.sa_handler = handler;
@@ -169,8 +177,8 @@ restart:
 #endif
        (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term);
     } else {
-       memset(&term, 0, sizeof(term));
-       memset(&oterm, 0, sizeof(oterm));
+       zero_bytes(&term, sizeof(term));
+       zero_bytes(&oterm, sizeof(oterm));
     }
 
     /* No output if we are already backgrounded. */
@@ -224,26 +232,74 @@ restart:
     return(pass);
 }
 
+/*
+ * Fork a child and exec sudo-askpass to get the password from the user.
+ */
+static char *
+sudo_askpass(prompt)
+    const char *prompt;
+{
+    static char buf[SUDO_PASS_MAX + 1], *pass;
+    sigaction_t sa, saved_sa_pipe;
+    int pfd[2];
+    pid_t pid;
+
+    if (pipe(pfd) == -1)
+       error(1, "unable to create pipe");
+
+    if ((pid = fork()) == -1)
+       error(1, "unable to fork");
+
+    if (pid == 0) {
+       /* child, point stdout to output side of the pipe and exec askpass */
+       (void) dup2(pfd[1], STDOUT_FILENO);
+       set_perms(PERM_FULL_USER);
+       closefrom(STDERR_FILENO + 1);
+       execl(user_askpass, user_askpass, prompt, (char *)NULL);
+       warning("unable to run %s", user_askpass);
+       _exit(255);
+    }
+
+    /* Ignore SIGPIPE in case child exits prematurely */
+    zero_bytes(&sa, sizeof(sa));
+    sigemptyset(&sa.sa_mask);
+    sa.sa_flags = 0;
+    sa.sa_handler = SIG_IGN;
+    (void) sigaction(SIGPIPE, &sa, &saved_sa_pipe);
+
+    /* Get response from child (askpass) and restore SIGPIPE handler */
+    (void) close(pfd[1]);
+    pass = getln(pfd[0], buf, sizeof(buf));
+    (void) close(pfd[0]);
+    (void) sigaction(SIGPIPE, &saved_sa_pipe, NULL);
+
+    return(pass);
+}
+
 static char *
 getln(fd, buf, bufsiz)
     int fd;
     char *buf;
     size_t bufsiz;
 {
-    char c, *cp;
-    ssize_t nr;
+    ssize_t nr = -1;
+    char *cp = buf;
+    char c = '\0';
 
     if (bufsiz == 0) {
        errno = EINVAL;
        return(NULL);                   /* sanity */
     }
 
-    cp = buf;
-    nr = -1;
-    while (--bufsiz && (nr = read(fd, &c, 1)) == 1 && c != '\n' && c != '\r')
+    while (--bufsiz) {
+       nr = read(fd, &c, 1);
+       if (nr != 1 || c == '\n' || c == '\r')
+           break;
        *cp++ = c;
+    }
     *cp = '\0';
-    return(nr == -1 ? NULL : buf);
+
+    return(nr == 1 ? buf : NULL);
 }
 
 static void
@@ -253,3 +309,13 @@ handler(s)
     if (s != SIGALRM)
        signo = s;
 }
+
+int
+tty_present()
+{
+    int fd;
+
+    if ((fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) != -1)
+       close(fd);
+    return(fd != -1);
+}