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