summaryrefslogtreecommitdiff
path: root/ssh-add.c
diff options
context:
space:
mode:
Diffstat (limited to 'ssh-add.c')
-rw-r--r--ssh-add.c76
1 files changed, 73 insertions, 3 deletions
diff --git a/ssh-add.c b/ssh-add.c
index 2b4966d73..8effcdb07 100644
--- a/ssh-add.c
+++ b/ssh-add.c
@@ -14,7 +14,7 @@ Adds an identity to the authentication server, or removes an identity.
14*/ 14*/
15 15
16#include "includes.h" 16#include "includes.h"
17RCSID("$Id: ssh-add.c,v 1.2 1999/10/28 05:23:30 damien Exp $"); 17RCSID("$Id: ssh-add.c,v 1.3 1999/11/08 04:30:59 damien Exp $");
18 18
19#include "rsa.h" 19#include "rsa.h"
20#include "ssh.h" 20#include "ssh.h"
@@ -52,6 +52,7 @@ delete_all(AuthenticationConnection *ac)
52 fprintf(stderr, "Failed to remove all identitities.\n"); 52 fprintf(stderr, "Failed to remove all identitities.\n");
53} 53}
54 54
55#define BUFSIZE 1024
55void 56void
56add_file(AuthenticationConnection *ac, const char *filename) 57add_file(AuthenticationConnection *ac, const char *filename)
57{ 58{
@@ -59,6 +60,11 @@ add_file(AuthenticationConnection *ac, const char *filename)
59 RSA *public_key; 60 RSA *public_key;
60 char *saved_comment, *comment, *pass; 61 char *saved_comment, *comment, *pass;
61 int first; 62 int first;
63 int pipes[2];
64 char buf[BUFSIZE];
65 int tmp;
66 pid_t child;
67 FILE *pipef;
62 68
63 key = RSA_new(); 69 key = RSA_new();
64 public_key = RSA_new(); 70 public_key = RSA_new();
@@ -80,8 +86,72 @@ add_file(AuthenticationConnection *ac, const char *filename)
80 /* Ask for a passphrase. */ 86 /* Ask for a passphrase. */
81 if (getenv("DISPLAY") && !isatty(fileno(stdin))) 87 if (getenv("DISPLAY") && !isatty(fileno(stdin)))
82 { 88 {
83 xfree(saved_comment); 89 if (pipe(pipes) ==-1)
84 return; 90 {
91 fprintf(stderr, "Creating pipes failed: %s\n", strerror(errno));
92 exit(1);
93 }
94 if (fflush(NULL)==EOF)
95 {
96 fprintf(stderr, "Cannot flush buffers: %s\n", strerror(errno));
97 exit(1);
98 }
99 switch (child=fork())
100 {
101 case -1:
102 fprintf(stderr, "Cannot fork: %s\n", strerror(errno));
103 exit(1);
104 case 0:
105 close(pipes[0]);
106 if (dup2(pipes[1], 1) ==-1)
107 {
108 fprintf(stderr, "dup2 failed: %s\n", strerror(errno));
109 exit(1);
110 }
111 tmp=snprintf(buf, BUFSIZE, "Need passphrase for %s (%s)",
112 filename, saved_comment);
113 /* skip the prompt if it won't fit */
114 if (tmp < 0 || tmp >= BUFSIZE)
115 tmp=execlp("/usr/lib/ssh/ssh-askpass", "ssh-askpass", 0);
116 else
117 tmp=execlp("/usr/lib/ssh/ssh-askpass", "ssh-askpass", buf, 0);
118 if (tmp==-1)
119 {
120 fprintf(stderr, "Executing ssh-askpass failed: %s\n",
121 strerror(errno));
122 exit(1);
123 }
124 break;
125 default:
126 close(pipes[1]);
127 if ( (pipef=fdopen(pipes[0], "r")) ==NULL)
128 {
129 fprintf(stderr, "fdopen failed: %s\n", strerror(errno));
130 exit(1);
131 }
132 if(fgets(buf, sizeof(buf), pipef)==NULL)
133 {
134 xfree(saved_comment);
135 return;
136 }
137 fclose(pipef);
138 if (strchr(buf, '\n'))
139 *strchr(buf, '\n') = 0;
140 pass = xstrdup(buf);
141 memset(buf, 0, sizeof(buf));
142 if (waitpid(child, NULL, 0) ==-1)
143 {
144 fprintf(stderr, "Waiting for child failed: %s\n",
145 strerror(errno));
146 exit(1);
147 }
148 if (strcmp(pass, "") == 0)
149 {
150 xfree(saved_comment);
151 xfree(pass);
152 return;
153 }
154 }
85 } 155 }
86 else 156 else
87 { 157 {