summaryrefslogtreecommitdiff
path: root/readpass.c
diff options
context:
space:
mode:
authorBen Lindstrom <mouring@eviladmin.org>2001-04-19 20:33:07 +0000
committerBen Lindstrom <mouring@eviladmin.org>2001-04-19 20:33:07 +0000
commit5eb97b6f3d878e6b40cfa73a18bacc293ea6b1ad (patch)
tree44f6ee7e020d9ac349256c6d4538734b4162bc5a /readpass.c
parentf73e05eca801c3a3488caa3b6bf1585ab7e2c13a (diff)
- markus@cvs.openbsd.org 2001/04/18 21:57:42
[readpass.c ssh-add.c] call askpass from ssh, too, based on work by roth@feep.net, ok deraadt
Diffstat (limited to 'readpass.c')
-rw-r--r--readpass.c71
1 files changed, 70 insertions, 1 deletions
diff --git a/readpass.c b/readpass.c
index 3d73af747..b93eaba43 100644
--- a/readpass.c
+++ b/readpass.c
@@ -32,11 +32,58 @@
32 */ 32 */
33 33
34#include "includes.h" 34#include "includes.h"
35RCSID("$OpenBSD: readpass.c,v 1.14 2001/02/08 19:30:52 itojun Exp $"); 35RCSID("$OpenBSD: readpass.c,v 1.15 2001/04/18 21:57:41 markus Exp $");
36 36
37#include "xmalloc.h" 37#include "xmalloc.h"
38#include "cli.h" 38#include "cli.h"
39#include "readpass.h" 39#include "readpass.h"
40#include "pathnames.h"
41#include "log.h"
42#include "atomicio.h"
43#include "ssh.h"
44
45char *
46ssh_askpass(char *askpass, char *msg)
47{
48 pid_t pid;
49 size_t len;
50 char *nl, *pass;
51 int p[2], status;
52 char buf[1024];
53
54 if (fflush(stdout) != 0)
55 error("ssh_askpass: fflush: %s", strerror(errno));
56 if (askpass == NULL)
57 fatal("internal error: askpass undefined");
58 if (pipe(p) < 0)
59 fatal("ssh_askpass: pipe: %s", strerror(errno));
60 if ((pid = fork()) < 0)
61 fatal("ssh_askpass: fork: %s", strerror(errno));
62 if (pid == 0) {
63 seteuid(getuid());
64 setuid(getuid());
65 close(p[0]);
66 if (dup2(p[1], STDOUT_FILENO) < 0)
67 fatal("ssh_askpass: dup2: %s", strerror(errno));
68 execlp(askpass, askpass, msg, (char *) 0);
69 fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
70 }
71 close(p[1]);
72 len = read(p[0], buf, sizeof buf);
73 close(p[0]);
74 while (waitpid(pid, &status, 0) < 0)
75 if (errno != EINTR)
76 break;
77 if (len <= 1)
78 return xstrdup("");
79 nl = strchr(buf, '\n');
80 if (nl)
81 *nl = '\0';
82 pass = xstrdup(buf);
83 memset(buf, 0, sizeof(buf));
84 return pass;
85}
86
40 87
41/* 88/*
42 * Reads a passphrase from /dev/tty with echo turned off. Returns the 89 * Reads a passphrase from /dev/tty with echo turned off. Returns the
@@ -51,5 +98,27 @@ RCSID("$OpenBSD: readpass.c,v 1.14 2001/02/08 19:30:52 itojun Exp $");
51char * 98char *
52read_passphrase(const char *prompt, int from_stdin) 99read_passphrase(const char *prompt, int from_stdin)
53{ 100{
101 char *askpass = NULL;
102 int use_askpass = 0, ttyfd;
103
104 if (from_stdin) {
105 if (!isatty(STDIN_FILENO))
106 use_askpass = 1;
107 } else {
108 ttyfd = open("/dev/tty", O_RDWR);
109 if (ttyfd >= 0)
110 close(ttyfd);
111 else
112 use_askpass = 1;
113 }
114
115 if (use_askpass && getenv("DISPLAY")) {
116 if (getenv(SSH_ASKPASS_ENV))
117 askpass = getenv(SSH_ASKPASS_ENV);
118 else
119 askpass = _PATH_SSH_ASKPASS_DEFAULT;
120 return ssh_askpass(askpass, prompt);
121 }
122
54 return cli_read_passphrase(prompt, from_stdin, 0); 123 return cli_read_passphrase(prompt, from_stdin, 0);
55} 124}