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.
12 #include <openssl/rsa.h>
13 #include <openssl/dsa.h>
19 #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);
106 public = key_new(KEY_RSA);
107 if (!load_public_key(filename, public, &saved_comment)) {
108 printf("Bad key file %s: %s\n", filename, strerror(errno));
113 if (!interactive && getenv("DISPLAY")) {
114 if (getenv(SSH_ASKPASS_ENV))
115 askpass = getenv(SSH_ASKPASS_ENV);
117 askpass = SSH_ASKPASS_DEFAULT;
120 /* At first, try empty passphrase */
121 private = key_new(KEY_RSA);
122 success = load_private_key(filename, "", private, &comment);
124 printf("Need passphrase for %.200s\n", filename);
125 if (!interactive && askpass == NULL) {
126 xfree(saved_comment);
129 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
133 snprintf(buf, sizeof buf, "%s: ", msg);
134 pass = read_passphrase(buf, 1);
136 pass = ssh_askpass(askpass, msg);
138 if (strcmp(pass, "") == 0) {
140 xfree(saved_comment);
143 success = load_private_key(filename, pass, private, &comment);
144 memset(pass, 0, strlen(pass));
148 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
151 xfree(saved_comment);
153 if (ssh_add_identity(ac, private->rsa, comment))
154 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
156 fprintf(stderr, "Could not add identity: %s\n", filename);
162 list_identities(AuthenticationConnection *ac, int fp)
172 for (status = ssh_get_first_identity(ac, e, n, &comment);
174 status = ssh_get_next_identity(ac, e, n, &comment)) {
175 unsigned int bits = BN_num_bits(n);
178 printf("%d %s %s\n", bits, fingerprint(e, n), comment);
183 error("list_identities: BN_bn2dec(e) failed.");
187 error("list_identities: BN_bn2dec(n) failed.");
189 printf("%d %s %s %s\n", bits, ebuf, nbuf, comment);
200 printf("The agent has no identities.\n");
204 main(int argc, char **argv)
206 AuthenticationConnection *ac = NULL;
213 /* check if RSA support exists */
214 if (rsa_alive() == 0) {
216 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
220 /* At first, get a connection to the authentication agent. */
221 ac = ssh_get_authentication_connection();
223 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
226 for (i = 1; i < argc; i++) {
227 if ((strcmp(argv[i], "-l") == 0) ||
228 (strcmp(argv[i], "-L") == 0)) {
229 list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
230 /* Don't default-add/delete if -l. */
234 if (strcmp(argv[i], "-d") == 0) {
238 if (strcmp(argv[i], "-D") == 0) {
245 delete_file(ac, argv[i]);
247 add_file(ac, argv[i]);
250 pw = getpwuid(getuid());
252 fprintf(stderr, "No user found with uid %d\n", (int) getuid());
253 ssh_close_authentication_connection(ac);
256 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
258 delete_file(ac, buf);
262 ssh_close_authentication_connection(ac);