Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.16.2.1
1.1 deraadt 1: /*
1.13 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Created: Thu Apr 6 00:52:24 1995 ylo
6: * Adds an identity to the authentication server, or removes an identity.
1.16.2.1! jason 7: *
! 8: * SSH2 implementation,
! 9: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.13 deraadt 10: */
1.1 deraadt 11:
12: #include "includes.h"
1.16.2.1! jason 13: RCSID("$OpenBSD: ssh-add.c,v 1.20 2000/08/28 03:50:54 deraadt Exp $");
1.16 markus 14:
1.16.2.1! jason 15: #include <openssl/evp.h>
1.16 markus 16: #include <openssl/rsa.h>
17: #include <openssl/dsa.h>
1.1 deraadt 18:
19: #include "rsa.h"
20: #include "ssh.h"
21: #include "xmalloc.h"
1.16 markus 22: #include "key.h"
1.16.2.1! jason 23: #include "authfd.h"
1.16 markus 24: #include "authfile.h"
1.1 deraadt 25:
1.2 provos 26: void
1.7 markus 27: delete_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 28: {
1.16 markus 29: Key *public;
1.12 markus 30: char *comment;
1.1 deraadt 31:
1.16 markus 32: public = key_new(KEY_RSA);
33: if (!load_public_key(filename, public, &comment)) {
1.12 markus 34: printf("Bad key file %s: %s\n", filename, strerror(errno));
35: return;
36: }
1.16.2.1! jason 37: if (ssh_remove_identity(ac, public))
1.12 markus 38: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
39: else
40: fprintf(stderr, "Could not remove identity: %s\n", filename);
1.16 markus 41: key_free(public);
1.12 markus 42: xfree(comment);
1.1 deraadt 43: }
44:
1.16.2.1! jason 45: /* Send a request to remove all identities. */
1.2 provos 46: void
1.7 markus 47: delete_all(AuthenticationConnection *ac)
1.1 deraadt 48: {
1.16.2.1! jason 49: int success = 1;
! 50:
! 51: if (!ssh_remove_all_identities(ac, 1))
! 52: success = 0;
! 53: /* ignore error-code for ssh2 */
! 54: ssh_remove_all_identities(ac, 2);
! 55:
! 56: if (success)
1.12 markus 57: fprintf(stderr, "All identities removed.\n");
58: else
59: fprintf(stderr, "Failed to remove all identitities.\n");
1.1 deraadt 60: }
61:
1.14 markus 62: char *
63: ssh_askpass(char *askpass, char *msg)
64: {
65: pid_t pid;
66: size_t len;
67: char *nl, *pass;
68: int p[2], status;
69: char buf[1024];
70:
71: if (askpass == NULL)
72: fatal("internal error: askpass undefined");
73: if (pipe(p) < 0)
74: fatal("ssh_askpass: pipe: %s", strerror(errno));
75: if ((pid = fork()) < 0)
76: fatal("ssh_askpass: fork: %s", strerror(errno));
77: if (pid == 0) {
78: close(p[0]);
79: if (dup2(p[1], STDOUT_FILENO) < 0)
80: fatal("ssh_askpass: dup2: %s", strerror(errno));
81: execlp(askpass, askpass, msg, (char *) 0);
82: fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
83: }
84: close(p[1]);
85: len = read(p[0], buf, sizeof buf);
86: close(p[0]);
87: while (waitpid(pid, &status, 0) < 0)
88: if (errno != EINTR)
89: break;
90: if (len <= 1)
91: return xstrdup("");
92: nl = strchr(buf, '\n');
93: if (nl)
94: *nl = '\0';
95: pass = xstrdup(buf);
96: memset(buf, 0, sizeof(buf));
97: return pass;
98: }
99:
1.2 provos 100: void
1.7 markus 101: add_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 102: {
1.16.2.1! jason 103: struct stat st;
1.16 markus 104: Key *public;
105: Key *private;
1.14 markus 106: char *saved_comment, *comment, *askpass = NULL;
107: char buf[1024], msg[1024];
1.12 markus 108: int success;
1.14 markus 109: int interactive = isatty(STDIN_FILENO);
1.16.2.1! jason 110: int type = KEY_RSA;
1.12 markus 111:
1.16.2.1! jason 112: if (stat(filename, &st) < 0) {
! 113: perror(filename);
! 114: exit(1);
! 115: }
! 116: /*
! 117: * try to load the public key. right now this only works for RSA,
! 118: * since DSA keys are fully encrypted
! 119: */
1.16 markus 120: public = key_new(KEY_RSA);
121: if (!load_public_key(filename, public, &saved_comment)) {
1.16.2.1! jason 122: /* ok, so we will asume this is a DSA key */
! 123: type = KEY_DSA;
! 124: saved_comment = xstrdup(filename);
1.12 markus 125: }
1.16 markus 126: key_free(public);
1.12 markus 127:
1.15 markus 128: if (!interactive && getenv("DISPLAY")) {
129: if (getenv(SSH_ASKPASS_ENV))
130: askpass = getenv(SSH_ASKPASS_ENV);
131: else
132: askpass = SSH_ASKPASS_DEFAULT;
133: }
1.14 markus 134:
1.12 markus 135: /* At first, try empty passphrase */
1.16.2.1! jason 136: private = key_new(type);
1.16 markus 137: success = load_private_key(filename, "", private, &comment);
1.12 markus 138: if (!success) {
1.14 markus 139: printf("Need passphrase for %.200s\n", filename);
140: if (!interactive && askpass == NULL) {
1.12 markus 141: xfree(saved_comment);
142: return;
143: }
1.14 markus 144: snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
1.12 markus 145: for (;;) {
1.14 markus 146: char *pass;
147: if (interactive) {
148: snprintf(buf, sizeof buf, "%s: ", msg);
149: pass = read_passphrase(buf, 1);
150: } else {
151: pass = ssh_askpass(askpass, msg);
152: }
1.12 markus 153: if (strcmp(pass, "") == 0) {
154: xfree(pass);
155: xfree(saved_comment);
156: return;
157: }
1.16 markus 158: success = load_private_key(filename, pass, private, &comment);
1.12 markus 159: memset(pass, 0, strlen(pass));
160: xfree(pass);
161: if (success)
162: break;
1.14 markus 163: strlcpy(msg, "Bad passphrase, try again", sizeof msg);
1.12 markus 164: }
165: }
1.16.2.1! jason 166: xfree(comment);
! 167: if (ssh_add_identity(ac, private, saved_comment))
! 168: fprintf(stderr, "Identity added: %s (%s)\n", filename, saved_comment);
1.12 markus 169: else
170: fprintf(stderr, "Could not add identity: %s\n", filename);
1.16 markus 171: key_free(private);
1.16.2.1! jason 172: xfree(saved_comment);
1.1 deraadt 173: }
174:
1.2 provos 175: void
1.11 markus 176: list_identities(AuthenticationConnection *ac, int fp)
1.1 deraadt 177: {
1.16.2.1! jason 178: Key *key;
1.12 markus 179: char *comment;
1.16.2.1! jason 180: int had_identities = 0;
! 181: int version;
1.12 markus 182:
1.16.2.1! jason 183: for (version = 1; version <= 2; version++) {
! 184: for (key = ssh_get_first_identity(ac, &comment, version);
! 185: key != NULL;
! 186: key = ssh_get_next_identity(ac, &comment, version)) {
! 187: had_identities = 1;
! 188: if (fp) {
! 189: printf("%d %s %s\n",
! 190: key_size(key), key_fingerprint(key), comment);
1.12 markus 191: } else {
1.16.2.1! jason 192: if (!key_write(key, stdout))
! 193: fprintf(stderr, "key_write failed");
! 194: fprintf(stdout, " %s\n", comment);
1.12 markus 195: }
1.16.2.1! jason 196: key_free(key);
! 197: xfree(comment);
1.12 markus 198: }
199: }
200: if (!had_identities)
201: printf("The agent has no identities.\n");
1.1 deraadt 202: }
203:
1.2 provos 204: int
1.7 markus 205: main(int argc, char **argv)
1.1 deraadt 206: {
1.12 markus 207: AuthenticationConnection *ac = NULL;
208: struct passwd *pw;
209: char buf[1024];
210: int no_files = 1;
211: int i;
212: int deleting = 0;
213:
214: /* check if RSA support exists */
215: if (rsa_alive() == 0) {
216: extern char *__progname;
217:
218: fprintf(stderr,
219: "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
220: __progname);
221: exit(1);
222: }
1.16.2.1! jason 223: SSLeay_add_all_algorithms();
! 224:
1.12 markus 225: /* At first, get a connection to the authentication agent. */
226: ac = ssh_get_authentication_connection();
227: if (ac == NULL) {
228: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
229: exit(1);
230: }
231: for (i = 1; i < argc; i++) {
232: if ((strcmp(argv[i], "-l") == 0) ||
233: (strcmp(argv[i], "-L") == 0)) {
234: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
235: /* Don't default-add/delete if -l. */
236: no_files = 0;
237: continue;
238: }
239: if (strcmp(argv[i], "-d") == 0) {
240: deleting = 1;
241: continue;
242: }
243: if (strcmp(argv[i], "-D") == 0) {
244: delete_all(ac);
245: no_files = 0;
246: continue;
247: }
248: no_files = 0;
249: if (deleting)
250: delete_file(ac, argv[i]);
251: else
252: add_file(ac, argv[i]);
253: }
254: if (no_files) {
255: pw = getpwuid(getuid());
256: if (!pw) {
1.16.2.1! jason 257: fprintf(stderr, "No user found with uid %u\n",
! 258: (u_int)getuid());
1.12 markus 259: ssh_close_authentication_connection(ac);
260: exit(1);
261: }
262: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
263: if (deleting)
264: delete_file(ac, buf);
265: else
266: add_file(ac, buf);
267: }
268: ssh_close_authentication_connection(ac);
269: exit(0);
1.1 deraadt 270: }