]> andersk Git - openssh.git/blame - ssh-add.c
- (djm) Fix server not exiting with jobs in background.
[openssh.git] / ssh-add.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5 * Created: Thu Apr 6 00:52:24 1995 ylo
6 * Adds an identity to the authentication server, or removes an identity.
2e73a022 7 *
8 * SSH2 implementation,
9 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5260325f 10 */
8efc0c15 11
12#include "includes.h"
14a9a859 13RCSID("$OpenBSD: ssh-add.c,v 1.20 2000/08/28 03:50:54 deraadt Exp $");
8efc0c15 14
2e73a022 15#include <openssl/evp.h>
a306f2dd 16#include <openssl/rsa.h>
17#include <openssl/dsa.h>
18
8efc0c15 19#include "rsa.h"
20#include "ssh.h"
21#include "xmalloc.h"
a306f2dd 22#include "key.h"
4c8722d9 23#include "authfd.h"
a306f2dd 24#include "authfile.h"
8efc0c15 25
f601d847 26#ifdef HAVE___PROGNAME
27extern char *__progname;
28#else /* HAVE___PROGNAME */
3fd95d9a 29static const char *__progname = "ssh-add";
f601d847 30#endif /* HAVE___PROGNAME */
31
8efc0c15 32void
5068f573 33delete_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 34{
a306f2dd 35 Key *public;
5260325f 36 char *comment;
37
a306f2dd 38 public = key_new(KEY_RSA);
39 if (!load_public_key(filename, public, &comment)) {
5260325f 40 printf("Bad key file %s: %s\n", filename, strerror(errno));
41 return;
42 }
2e73a022 43 if (ssh_remove_identity(ac, public))
5260325f 44 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
45 else
46 fprintf(stderr, "Could not remove identity: %s\n", filename);
a306f2dd 47 key_free(public);
5260325f 48 xfree(comment);
8efc0c15 49}
50
2e73a022 51/* Send a request to remove all identities. */
8efc0c15 52void
5068f573 53delete_all(AuthenticationConnection *ac)
8efc0c15 54{
2e73a022 55 int success = 1;
56
57 if (!ssh_remove_all_identities(ac, 1))
58 success = 0;
59 /* ignore error-code for ssh2 */
60 ssh_remove_all_identities(ac, 2);
61
62 if (success)
5260325f 63 fprintf(stderr, "All identities removed.\n");
64 else
65 fprintf(stderr, "Failed to remove all identitities.\n");
8efc0c15 66}
67
aa3378df 68char *
69ssh_askpass(char *askpass, char *msg)
70{
71 pid_t pid;
72 size_t len;
73 char *nl, *pass;
74 int p[2], status;
75 char buf[1024];
76
77 if (askpass == NULL)
78 fatal("internal error: askpass undefined");
79 if (pipe(p) < 0)
80 fatal("ssh_askpass: pipe: %s", strerror(errno));
81 if ((pid = fork()) < 0)
82 fatal("ssh_askpass: fork: %s", strerror(errno));
83 if (pid == 0) {
84 close(p[0]);
85 if (dup2(p[1], STDOUT_FILENO) < 0)
86 fatal("ssh_askpass: dup2: %s", strerror(errno));
87 execlp(askpass, askpass, msg, (char *) 0);
88 fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
89 }
90 close(p[1]);
91 len = read(p[0], buf, sizeof buf);
92 close(p[0]);
93 while (waitpid(pid, &status, 0) < 0)
94 if (errno != EINTR)
95 break;
96 if (len <= 1)
97 return xstrdup("");
98 nl = strchr(buf, '\n');
99 if (nl)
100 *nl = '\0';
101 pass = xstrdup(buf);
102 memset(buf, 0, sizeof(buf));
103 return pass;
104}
105
8efc0c15 106void
5068f573 107add_file(AuthenticationConnection *ac, const char *filename)
8efc0c15 108{
2e73a022 109 struct stat st;
a306f2dd 110 Key *public;
111 Key *private;
aa3378df 112 char *saved_comment, *comment, *askpass = NULL;
113 char buf[1024], msg[1024];
5260325f 114 int success;
aa3378df 115 int interactive = isatty(STDIN_FILENO);
4c8722d9 116 int type = KEY_RSA;
5260325f 117
2e73a022 118 if (stat(filename, &st) < 0) {
119 perror(filename);
120 exit(1);
121 }
4c8722d9 122 /*
123 * try to load the public key. right now this only works for RSA,
124 * since DSA keys are fully encrypted
125 */
a306f2dd 126 public = key_new(KEY_RSA);
127 if (!load_public_key(filename, public, &saved_comment)) {
4c8722d9 128 /* ok, so we will asume this is a DSA key */
129 type = KEY_DSA;
130 saved_comment = xstrdup(filename);
5260325f 131 }
a306f2dd 132 key_free(public);
dad9a31e 133
57112b5a 134 if (!interactive && getenv("DISPLAY")) {
135 if (getenv(SSH_ASKPASS_ENV))
136 askpass = getenv(SSH_ASKPASS_ENV);
137 else
138 askpass = SSH_ASKPASS_DEFAULT;
139 }
aa3378df 140
5260325f 141 /* At first, try empty passphrase */
4c8722d9 142 private = key_new(type);
a306f2dd 143 success = load_private_key(filename, "", private, &comment);
5260325f 144 if (!success) {
aa3378df 145 printf("Need passphrase for %.200s\n", filename);
146 if (!interactive && askpass == NULL) {
147 xfree(saved_comment);
148 return;
5260325f 149 }
aa3378df 150 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
151 for (;;) {
152 char *pass;
153 if (interactive) {
154 snprintf(buf, sizeof buf, "%s: ", msg);
155 pass = read_passphrase(buf, 1);
156 } else {
157 pass = ssh_askpass(askpass, msg);
158 }
5260325f 159 if (strcmp(pass, "") == 0) {
160 xfree(pass);
161 xfree(saved_comment);
162 return;
163 }
a306f2dd 164 success = load_private_key(filename, pass, private, &comment);
5260325f 165 memset(pass, 0, strlen(pass));
166 xfree(pass);
167 if (success)
168 break;
aa3378df 169 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
5260325f 170 }
171 }
2e73a022 172 xfree(comment);
173 if (ssh_add_identity(ac, private, saved_comment))
174 fprintf(stderr, "Identity added: %s (%s)\n", filename, saved_comment);
5260325f 175 else
176 fprintf(stderr, "Could not add identity: %s\n", filename);
a306f2dd 177 key_free(private);
2e73a022 178 xfree(saved_comment);
8efc0c15 179}
180
181void
f095fcc7 182list_identities(AuthenticationConnection *ac, int fp)
8efc0c15 183{
2e73a022 184 Key *key;
5260325f 185 char *comment;
2e73a022 186 int had_identities = 0;
187 int version;
188
189 for (version = 1; version <= 2; version++) {
190 for (key = ssh_get_first_identity(ac, &comment, version);
191 key != NULL;
192 key = ssh_get_next_identity(ac, &comment, version)) {
193 had_identities = 1;
194 if (fp) {
195 printf("%d %s %s\n",
196 key_size(key), key_fingerprint(key), comment);
5260325f 197 } else {
2e73a022 198 if (!key_write(key, stdout))
199 fprintf(stderr, "key_write failed");
200 fprintf(stdout, " %s\n", comment);
5260325f 201 }
2e73a022 202 key_free(key);
203 xfree(comment);
5260325f 204 }
f095fcc7 205 }
5260325f 206 if (!had_identities)
207 printf("The agent has no identities.\n");
8efc0c15 208}
209
210int
5068f573 211main(int argc, char **argv)
8efc0c15 212{
5260325f 213 AuthenticationConnection *ac = NULL;
214 struct passwd *pw;
215 char buf[1024];
216 int no_files = 1;
217 int i;
218 int deleting = 0;
219
264dce47 220 init_rng();
221
5260325f 222 /* check if RSA support exists */
223 if (rsa_alive() == 0) {
5260325f 224 fprintf(stderr,
225 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
226 __progname);
227 exit(1);
8efc0c15 228 }
2e73a022 229 SSLeay_add_all_algorithms();
230
5260325f 231 /* At first, get a connection to the authentication agent. */
232 ac = ssh_get_authentication_connection();
233 if (ac == NULL) {
234 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
235 exit(1);
8efc0c15 236 }
5260325f 237 for (i = 1; i < argc; i++) {
238 if ((strcmp(argv[i], "-l") == 0) ||
239 (strcmp(argv[i], "-L") == 0)) {
240 list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
241 /* Don't default-add/delete if -l. */
242 no_files = 0;
243 continue;
244 }
245 if (strcmp(argv[i], "-d") == 0) {
246 deleting = 1;
247 continue;
248 }
249 if (strcmp(argv[i], "-D") == 0) {
250 delete_all(ac);
251 no_files = 0;
252 continue;
253 }
254 no_files = 0;
255 if (deleting)
256 delete_file(ac, argv[i]);
257 else
258 add_file(ac, argv[i]);
8efc0c15 259 }
5260325f 260 if (no_files) {
261 pw = getpwuid(getuid());
262 if (!pw) {
14a9a859 263 fprintf(stderr, "No user found with uid %u\n",
264 (u_int)getuid());
5260325f 265 ssh_close_authentication_connection(ac);
266 exit(1);
267 }
268 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
269 if (deleting)
270 delete_file(ac, buf);
271 else
272 add_file(ac, buf);
8efc0c15 273 }
5260325f 274 ssh_close_authentication_connection(ac);
275 exit(0);
8efc0c15 276}
This page took 0.115554 seconds and 5 git commands to generate.