2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * Created: Thu Apr 6 00:52:24 1995 ylo
6 * Adds an identity to the authentication server, or removes an identity.
9 * Copyright (c) 2000 Markus Friedl. All rights reserved.
13 RCSID("$OpenBSD: ssh-add.c,v 1.20 2000/08/28 03:50:54 deraadt Exp $");
15 #include <openssl/evp.h>
16 #include <openssl/rsa.h>
17 #include <openssl/dsa.h>
26 #ifdef HAVE___PROGNAME
27 extern char *__progname;
28 #else /* HAVE___PROGNAME */
29 static const char *__progname = "ssh-add";
30 #endif /* HAVE___PROGNAME */
33 delete_file(AuthenticationConnection *ac, const char *filename)
38 public = key_new(KEY_RSA);
39 if (!load_public_key(filename, public, &comment)) {
40 printf("Bad key file %s: %s\n", filename, strerror(errno));
43 if (ssh_remove_identity(ac, public))
44 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
46 fprintf(stderr, "Could not remove identity: %s\n", filename);
51 /* Send a request to remove all identities. */
53 delete_all(AuthenticationConnection *ac)
57 if (!ssh_remove_all_identities(ac, 1))
59 /* ignore error-code for ssh2 */
60 ssh_remove_all_identities(ac, 2);
63 fprintf(stderr, "All identities removed.\n");
65 fprintf(stderr, "Failed to remove all identitities.\n");
69 ssh_askpass(char *askpass, char *msg)
78 fatal("internal error: askpass undefined");
80 fatal("ssh_askpass: pipe: %s", strerror(errno));
81 if ((pid = fork()) < 0)
82 fatal("ssh_askpass: fork: %s", strerror(errno));
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));
91 len = read(p[0], buf, sizeof buf);
93 while (waitpid(pid, &status, 0) < 0)
98 nl = strchr(buf, '\n');
102 memset(buf, 0, sizeof(buf));
107 add_file(AuthenticationConnection *ac, const char *filename)
112 char *saved_comment, *comment, *askpass = NULL;
113 char buf[1024], msg[1024];
115 int interactive = isatty(STDIN_FILENO);
118 if (stat(filename, &st) < 0) {
123 * try to load the public key. right now this only works for RSA,
124 * since DSA keys are fully encrypted
126 public = key_new(KEY_RSA);
127 if (!load_public_key(filename, public, &saved_comment)) {
128 /* ok, so we will asume this is a DSA key */
130 saved_comment = xstrdup(filename);
134 if (!interactive && getenv("DISPLAY")) {
135 if (getenv(SSH_ASKPASS_ENV))
136 askpass = getenv(SSH_ASKPASS_ENV);
138 askpass = SSH_ASKPASS_DEFAULT;
141 /* At first, try empty passphrase */
142 private = key_new(type);
143 success = load_private_key(filename, "", private, &comment);
145 printf("Need passphrase for %.200s\n", filename);
146 if (!interactive && askpass == NULL) {
147 xfree(saved_comment);
150 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
154 snprintf(buf, sizeof buf, "%s: ", msg);
155 pass = read_passphrase(buf, 1);
157 pass = ssh_askpass(askpass, msg);
159 if (strcmp(pass, "") == 0) {
161 xfree(saved_comment);
164 success = load_private_key(filename, pass, private, &comment);
165 memset(pass, 0, strlen(pass));
169 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
173 if (ssh_add_identity(ac, private, saved_comment))
174 fprintf(stderr, "Identity added: %s (%s)\n", filename, saved_comment);
176 fprintf(stderr, "Could not add identity: %s\n", filename);
178 xfree(saved_comment);
182 list_identities(AuthenticationConnection *ac, int fp)
186 int had_identities = 0;
189 for (version = 1; version <= 2; version++) {
190 for (key = ssh_get_first_identity(ac, &comment, version);
192 key = ssh_get_next_identity(ac, &comment, version)) {
196 key_size(key), key_fingerprint(key), comment);
198 if (!key_write(key, stdout))
199 fprintf(stderr, "key_write failed");
200 fprintf(stdout, " %s\n", comment);
207 printf("The agent has no identities.\n");
211 main(int argc, char **argv)
213 AuthenticationConnection *ac = NULL;
222 /* check if RSA support exists */
223 if (rsa_alive() == 0) {
225 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
229 SSLeay_add_all_algorithms();
231 /* At first, get a connection to the authentication agent. */
232 ac = ssh_get_authentication_connection();
234 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
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. */
245 if (strcmp(argv[i], "-d") == 0) {
249 if (strcmp(argv[i], "-D") == 0) {
256 delete_file(ac, argv[i]);
258 add_file(ac, argv[i]);
261 pw = getpwuid(getuid());
263 fprintf(stderr, "No user found with uid %u\n",
265 ssh_close_authentication_connection(ac);
268 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
270 delete_file(ac, buf);
274 ssh_close_authentication_connection(ac);