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("$Id: ssh-add.c,v 1.15 1999/12/02 20:05:40 markus Exp $");
16 #include "fingerprint.h"
19 delete_file(AuthenticationConnection *ac, const char *filename)
25 if (!load_public_key(filename, key, &comment)) {
26 printf("Bad key file %s: %s\n", filename, strerror(errno));
29 if (ssh_remove_identity(ac, key))
30 fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
32 fprintf(stderr, "Could not remove identity: %s\n", filename);
38 delete_all(AuthenticationConnection *ac)
40 /* Send a request to remove all identities. */
41 if (ssh_remove_all_identities(ac))
42 fprintf(stderr, "All identities removed.\n");
44 fprintf(stderr, "Failed to remove all identitities.\n");
48 ssh_askpass(char *askpass, char *msg)
57 fatal("internal error: askpass undefined");
59 fatal("ssh_askpass: pipe: %s", strerror(errno));
60 if ((pid = fork()) < 0)
61 fatal("ssh_askpass: fork: %s", strerror(errno));
64 if (dup2(p[1], STDOUT_FILENO) < 0)
65 fatal("ssh_askpass: dup2: %s", strerror(errno));
66 execlp(askpass, askpass, msg, (char *) 0);
67 fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
70 len = read(p[0], buf, sizeof buf);
72 while (waitpid(pid, &status, 0) < 0)
77 nl = strchr(buf, '\n');
81 memset(buf, 0, sizeof(buf));
86 add_file(AuthenticationConnection *ac, const char *filename)
90 char *saved_comment, *comment, *askpass = NULL;
91 char buf[1024], msg[1024];
93 int interactive = isatty(STDIN_FILENO);
96 public_key = RSA_new();
97 if (!load_public_key(filename, public_key, &saved_comment)) {
98 printf("Bad key file %s: %s\n", filename, strerror(errno));
101 RSA_free(public_key);
103 if (!interactive && getenv("DISPLAY")) {
104 if (getenv(SSH_ASKPASS_ENV))
105 askpass = getenv(SSH_ASKPASS_ENV);
107 askpass = SSH_ASKPASS_DEFAULT;
110 /* At first, try empty passphrase */
111 success = load_private_key(filename, "", key, &comment);
113 printf("Need passphrase for %.200s\n", filename);
114 if (!interactive && askpass == NULL) {
115 xfree(saved_comment);
118 snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
122 snprintf(buf, sizeof buf, "%s: ", msg);
123 pass = read_passphrase(buf, 1);
125 pass = ssh_askpass(askpass, msg);
127 if (strcmp(pass, "") == 0) {
129 xfree(saved_comment);
132 success = load_private_key(filename, pass, key, &comment);
133 memset(pass, 0, strlen(pass));
137 strlcpy(msg, "Bad passphrase, try again", sizeof msg);
140 xfree(saved_comment);
142 if (ssh_add_identity(ac, key, comment))
143 fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
145 fprintf(stderr, "Could not add identity: %s\n", filename);
151 list_identities(AuthenticationConnection *ac, int fp)
161 for (status = ssh_get_first_identity(ac, e, n, &comment);
163 status = ssh_get_next_identity(ac, e, n, &comment)) {
164 unsigned int bits = BN_num_bits(n);
167 printf("%d %s %s\n", bits, fingerprint(e, n), comment);
172 error("list_identities: BN_bn2dec(e) failed.");
176 error("list_identities: BN_bn2dec(n) failed.");
178 printf("%d %s %s %s\n", bits, ebuf, nbuf, comment);
189 printf("The agent has no identities.\n");
193 main(int argc, char **argv)
195 AuthenticationConnection *ac = NULL;
202 /* check if RSA support exists */
203 if (rsa_alive() == 0) {
204 extern char *__progname;
207 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
211 /* At first, get a connection to the authentication agent. */
212 ac = ssh_get_authentication_connection();
214 fprintf(stderr, "Could not open a connection to your authentication agent.\n");
217 for (i = 1; i < argc; i++) {
218 if ((strcmp(argv[i], "-l") == 0) ||
219 (strcmp(argv[i], "-L") == 0)) {
220 list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
221 /* Don't default-add/delete if -l. */
225 if (strcmp(argv[i], "-d") == 0) {
229 if (strcmp(argv[i], "-D") == 0) {
236 delete_file(ac, argv[i]);
238 add_file(ac, argv[i]);
241 pw = getpwuid(getuid());
243 fprintf(stderr, "No user found with uid %d\n", (int) getuid());
244 ssh_close_authentication_connection(ac);
247 snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
249 delete_file(ac, buf);
253 ssh_close_authentication_connection(ac);