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.
10 RCSID("$OpenBSD: ssh-add.c,v 1.18 2000/07/16 08:27:21 markus Exp $");
12 #include <openssl/rsa.h>
13 #include <openssl/dsa.h>
18 #include "fingerprint.h"
23 #ifdef HAVE___PROGNAME
24 extern char *__progname;
25 #else /* HAVE___PROGNAME */
26 static const char *__progname = "ssh-add";
27 #endif /* HAVE___PROGNAME */
30 delete_file(AuthenticationConnection *ac, const char *filename)
35 public = key_new(KEY_RSA);
36 if (!load_public_key(filename, public, &comment)) {
37 printf("Bad key file %s: %s\n", filename, strerror(errno));
40 if (ssh_remove_identity(ac, public->rsa))
41 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
43 fprintf(stderr, "Could not remove identity: %s\n", filename);
49 delete_all(AuthenticationConnection *ac)
51 /* Send a request to remove all identities. */
52 if (ssh_remove_all_identities(ac))
53 fprintf(stderr, "All identities removed.\n");
55 fprintf(stderr, "Failed to remove all identitities.\n");
59 ssh_askpass(char *askpass, char *msg)
68 fatal("internal error: askpass undefined");
70 fatal("ssh_askpass: pipe: %s", strerror(errno));
71 if ((pid = fork()) < 0)
72 fatal("ssh_askpass: fork: %s", strerror(errno));
75 if (dup2(p[1], STDOUT_FILENO) < 0)
76 fatal("ssh_askpass: dup2: %s", strerror(errno));
77 execlp(askpass, askpass, msg, (char *) 0);
78 fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
81 len = read(p[0], buf, sizeof buf);
83 while (waitpid(pid, &status, 0) < 0)
88 nl = strchr(buf, '\n');
92 memset(buf, 0, sizeof(buf));
97 add_file(AuthenticationConnection *ac, const char *filename)
101 char *saved_comment, *comment, *askpass = NULL;
102 char buf[1024], msg[1024];
104 int interactive = isatty(STDIN_FILENO);
108 * try to load the public key. right now this only works for RSA,
109 * since DSA keys are fully encrypted
111 public = key_new(KEY_RSA);
112 if (!load_public_key(filename, public, &saved_comment)) {
113 /* ok, so we will asume this is a DSA key */
115 saved_comment = xstrdup(filename);
119 if (!interactive && getenv("DISPLAY")) {
120 if (getenv(SSH_ASKPASS_ENV))
121 askpass = getenv(SSH_ASKPASS_ENV);
123 askpass = SSH_ASKPASS_DEFAULT;
126 /* At first, try empty passphrase */
127 private = key_new(type);
128 success = load_private_key(filename, "", private, &comment);
130 printf("Need passphrase for %.200s\n", filename);
131 if (!interactive && askpass == NULL) {
132 xfree(saved_comment);
135 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
139 snprintf(buf, sizeof buf, "%s: ", msg);
140 pass = read_passphrase(buf, 1);
142 pass = ssh_askpass(askpass, msg);
144 if (strcmp(pass, "") == 0) {
146 xfree(saved_comment);
149 success = load_private_key(filename, pass, private, &comment);
150 memset(pass, 0, strlen(pass));
154 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
157 xfree(saved_comment);
159 if (ssh_add_identity(ac, private, comment))
160 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
162 fprintf(stderr, "Could not add identity: %s\n", filename);
168 list_identities(AuthenticationConnection *ac, int fp)
178 for (status = ssh_get_first_identity(ac, e, n, &comment);
180 status = ssh_get_next_identity(ac, e, n, &comment)) {
181 unsigned int bits = BN_num_bits(n);
184 printf("%d %s %s\n", bits, fingerprint(e, n), comment);
189 error("list_identities: BN_bn2dec(e) failed.");
193 error("list_identities: BN_bn2dec(n) failed.");
195 printf("%d %s %s %s\n", bits, ebuf, nbuf, comment);
206 printf("The agent has no identities.\n");
210 main(int argc, char **argv)
212 AuthenticationConnection *ac = NULL;
221 /* check if RSA support exists */
222 if (rsa_alive() == 0) {
224 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
228 /* At first, get a connection to the authentication agent. */
229 ac = ssh_get_authentication_connection();
231 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
234 for (i = 1; i < argc; i++) {
235 if ((strcmp(argv[i], "-l") == 0) ||
236 (strcmp(argv[i], "-L") == 0)) {
237 list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
238 /* Don't default-add/delete if -l. */
242 if (strcmp(argv[i], "-d") == 0) {
246 if (strcmp(argv[i], "-D") == 0) {
253 delete_file(ac, argv[i]);
255 add_file(ac, argv[i]);
258 pw = getpwuid(getuid());
260 fprintf(stderr, "No user found with uid %d\n", (int) getuid());
261 ssh_close_authentication_connection(ac);
264 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
266 delete_file(ac, buf);
270 ssh_close_authentication_connection(ac);