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.
16 #include "fingerprint.h"
18 #ifdef HAVE___PROGNAME
19 extern char *__progname;
20 #else /* HAVE___PROGNAME */
21 const char *__progname = "ssh-add";
22 #endif /* HAVE___PROGNAME */
25 delete_file(AuthenticationConnection *ac, const char *filename)
31 if (!load_public_key(filename, key, &comment)) {
32 printf("Bad key file %s: %s\n", filename, strerror(errno));
35 if (ssh_remove_identity(ac, key))
36 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
38 fprintf(stderr, "Could not remove identity: %s\n", filename);
44 delete_all(AuthenticationConnection *ac)
46 /* Send a request to remove all identities. */
47 if (ssh_remove_all_identities(ac))
48 fprintf(stderr, "All identities removed.\n");
50 fprintf(stderr, "Failed to remove all identitities.\n");
54 ssh_askpass(char *askpass, char *msg)
63 fatal("internal error: askpass undefined");
65 fatal("ssh_askpass: pipe: %s", strerror(errno));
66 if ((pid = fork()) < 0)
67 fatal("ssh_askpass: fork: %s", strerror(errno));
70 if (dup2(p[1], STDOUT_FILENO) < 0)
71 fatal("ssh_askpass: dup2: %s", strerror(errno));
72 execlp(askpass, askpass, msg, (char *) 0);
73 fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
76 len = read(p[0], buf, sizeof buf);
78 while (waitpid(pid, &status, 0) < 0)
83 nl = strchr(buf, '\n');
87 memset(buf, 0, sizeof(buf));
92 add_file(AuthenticationConnection *ac, const char *filename)
96 char *saved_comment, *comment, *askpass = NULL;
97 char buf[1024], msg[1024];
99 int interactive = isatty(STDIN_FILENO);
102 public_key = RSA_new();
103 if (!load_public_key(filename, public_key, &saved_comment)) {
104 printf("Bad key file %s: %s\n", filename, strerror(errno));
107 RSA_free(public_key);
109 if (!interactive && getenv("DISPLAY")) {
110 if (getenv(SSH_ASKPASS_ENV))
111 askpass = getenv(SSH_ASKPASS_ENV);
113 askpass = SSH_ASKPASS_DEFAULT;
116 /* At first, try empty passphrase */
117 success = load_private_key(filename, "", key, &comment);
119 printf("Need passphrase for %.200s\n", filename);
120 if (!interactive && askpass == NULL) {
121 xfree(saved_comment);
124 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
128 snprintf(buf, sizeof buf, "%s: ", msg);
129 pass = read_passphrase(buf, 1);
131 pass = ssh_askpass(askpass, msg);
133 if (strcmp(pass, "") == 0) {
135 xfree(saved_comment);
138 success = load_private_key(filename, pass, key, &comment);
139 memset(pass, 0, strlen(pass));
143 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
146 xfree(saved_comment);
148 if (ssh_add_identity(ac, key, comment))
149 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
151 fprintf(stderr, "Could not add identity: %s\n", filename);
157 list_identities(AuthenticationConnection *ac, int fp)
167 for (status = ssh_get_first_identity(ac, e, n, &comment);
169 status = ssh_get_next_identity(ac, e, n, &comment)) {
170 unsigned int bits = BN_num_bits(n);
173 printf("%d %s %s\n", bits, fingerprint(e, n), comment);
178 error("list_identities: BN_bn2dec(e) failed.");
182 error("list_identities: BN_bn2dec(n) failed.");
184 printf("%d %s %s %s\n", bits, ebuf, nbuf, comment);
195 printf("The agent has no identities.\n");
199 main(int argc, char **argv)
201 AuthenticationConnection *ac = NULL;
208 /* check if RSA support exists */
209 if (rsa_alive() == 0) {
211 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
215 /* At first, get a connection to the authentication agent. */
216 ac = ssh_get_authentication_connection();
218 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
221 for (i = 1; i < argc; i++) {
222 if ((strcmp(argv[i], "-l") == 0) ||
223 (strcmp(argv[i], "-L") == 0)) {
224 list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
225 /* Don't default-add/delete if -l. */
229 if (strcmp(argv[i], "-d") == 0) {
233 if (strcmp(argv[i], "-D") == 0) {
240 delete_file(ac, argv[i]);
242 add_file(ac, argv[i]);
245 pw = getpwuid(getuid());
247 fprintf(stderr, "No user found with uid %d\n", (int) getuid());
248 ssh_close_authentication_connection(ac);
251 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
253 delete_file(ac, buf);
257 ssh_close_authentication_connection(ac);