Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.23
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: * Adds an identity to the authentication server, or removes an identity.
1.19 markus 6: *
1.22 deraadt 7: * As far as I am concerned, the code I have written for this software
8: * can be used freely for any purpose. Any derived versions of this
9: * software must be clearly marked as such, and if the derived work is
10: * incompatible with the protocol description in the RFC file, it must be
11: * called by a name other than "ssh" or "Secure Shell".
12: *
1.19 markus 13: * SSH2 implementation,
14: * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.22 deraadt 15: *
16: * Redistribution and use in source and binary forms, with or without
17: * modification, are permitted provided that the following conditions
18: * are met:
19: * 1. Redistributions of source code must retain the above copyright
20: * notice, this list of conditions and the following disclaimer.
21: * 2. Redistributions in binary form must reproduce the above copyright
22: * notice, this list of conditions and the following disclaimer in the
23: * documentation and/or other materials provided with the distribution.
24: *
25: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13 deraadt 35: */
1.1 deraadt 36:
37: #include "includes.h"
1.23 ! markus 38: RCSID("$OpenBSD: ssh-add.c,v 1.22 2000/09/07 20:27:54 deraadt Exp $");
1.16 markus 39:
1.19 markus 40: #include <openssl/evp.h>
1.16 markus 41: #include <openssl/rsa.h>
42: #include <openssl/dsa.h>
1.1 deraadt 43:
44: #include "rsa.h"
45: #include "ssh.h"
46: #include "xmalloc.h"
1.16 markus 47: #include "key.h"
1.18 markus 48: #include "authfd.h"
1.16 markus 49: #include "authfile.h"
1.1 deraadt 50:
1.2 provos 51: void
1.7 markus 52: delete_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 53: {
1.16 markus 54: Key *public;
1.12 markus 55: char *comment;
1.1 deraadt 56:
1.23 ! markus 57: public = key_new(KEY_RSA1);
1.16 markus 58: if (!load_public_key(filename, public, &comment)) {
1.21 markus 59: key_free(public);
1.23 ! markus 60: public = key_new(KEY_UNSPEC);
1.21 markus 61: if (!try_load_public_key(filename, public, &comment)) {
62: printf("Bad key file %s\n", filename);
63: return;
64: }
1.12 markus 65: }
1.19 markus 66: if (ssh_remove_identity(ac, public))
1.12 markus 67: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
68: else
69: fprintf(stderr, "Could not remove identity: %s\n", filename);
1.16 markus 70: key_free(public);
1.12 markus 71: xfree(comment);
1.1 deraadt 72: }
73:
1.19 markus 74: /* Send a request to remove all identities. */
1.2 provos 75: void
1.7 markus 76: delete_all(AuthenticationConnection *ac)
1.1 deraadt 77: {
1.19 markus 78: int success = 1;
79:
80: if (!ssh_remove_all_identities(ac, 1))
81: success = 0;
82: /* ignore error-code for ssh2 */
83: ssh_remove_all_identities(ac, 2);
84:
85: if (success)
1.12 markus 86: fprintf(stderr, "All identities removed.\n");
87: else
88: fprintf(stderr, "Failed to remove all identitities.\n");
1.1 deraadt 89: }
90:
1.14 markus 91: char *
92: ssh_askpass(char *askpass, char *msg)
93: {
94: pid_t pid;
95: size_t len;
96: char *nl, *pass;
97: int p[2], status;
98: char buf[1024];
99:
100: if (askpass == NULL)
101: fatal("internal error: askpass undefined");
102: if (pipe(p) < 0)
103: fatal("ssh_askpass: pipe: %s", strerror(errno));
104: if ((pid = fork()) < 0)
105: fatal("ssh_askpass: fork: %s", strerror(errno));
106: if (pid == 0) {
107: close(p[0]);
108: if (dup2(p[1], STDOUT_FILENO) < 0)
109: fatal("ssh_askpass: dup2: %s", strerror(errno));
110: execlp(askpass, askpass, msg, (char *) 0);
111: fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
112: }
113: close(p[1]);
114: len = read(p[0], buf, sizeof buf);
115: close(p[0]);
116: while (waitpid(pid, &status, 0) < 0)
117: if (errno != EINTR)
118: break;
119: if (len <= 1)
120: return xstrdup("");
121: nl = strchr(buf, '\n');
122: if (nl)
123: *nl = '\0';
124: pass = xstrdup(buf);
125: memset(buf, 0, sizeof(buf));
126: return pass;
127: }
128:
1.2 provos 129: void
1.7 markus 130: add_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 131: {
1.19 markus 132: struct stat st;
1.16 markus 133: Key *public;
134: Key *private;
1.14 markus 135: char *saved_comment, *comment, *askpass = NULL;
136: char buf[1024], msg[1024];
1.12 markus 137: int success;
1.14 markus 138: int interactive = isatty(STDIN_FILENO);
1.23 ! markus 139: int type = KEY_RSA1;
1.12 markus 140:
1.19 markus 141: if (stat(filename, &st) < 0) {
142: perror(filename);
143: exit(1);
144: }
1.18 markus 145: /*
146: * try to load the public key. right now this only works for RSA,
147: * since DSA keys are fully encrypted
148: */
1.23 ! markus 149: public = key_new(KEY_RSA1);
1.16 markus 150: if (!load_public_key(filename, public, &saved_comment)) {
1.23 ! markus 151: /* ok, so we will assume this is 'some' key */
! 152: type = KEY_UNSPEC;
1.18 markus 153: saved_comment = xstrdup(filename);
1.12 markus 154: }
1.16 markus 155: key_free(public);
1.12 markus 156:
1.15 markus 157: if (!interactive && getenv("DISPLAY")) {
158: if (getenv(SSH_ASKPASS_ENV))
159: askpass = getenv(SSH_ASKPASS_ENV);
160: else
161: askpass = SSH_ASKPASS_DEFAULT;
162: }
1.14 markus 163:
1.12 markus 164: /* At first, try empty passphrase */
1.18 markus 165: private = key_new(type);
1.16 markus 166: success = load_private_key(filename, "", private, &comment);
1.12 markus 167: if (!success) {
1.14 markus 168: printf("Need passphrase for %.200s\n", filename);
169: if (!interactive && askpass == NULL) {
1.12 markus 170: xfree(saved_comment);
171: return;
172: }
1.14 markus 173: snprintf(msg, sizeof msg, "Enter passphrase for %.200s", saved_comment);
1.12 markus 174: for (;;) {
1.14 markus 175: char *pass;
176: if (interactive) {
177: snprintf(buf, sizeof buf, "%s: ", msg);
178: pass = read_passphrase(buf, 1);
179: } else {
180: pass = ssh_askpass(askpass, msg);
181: }
1.12 markus 182: if (strcmp(pass, "") == 0) {
183: xfree(pass);
184: xfree(saved_comment);
185: return;
186: }
1.16 markus 187: success = load_private_key(filename, pass, private, &comment);
1.12 markus 188: memset(pass, 0, strlen(pass));
189: xfree(pass);
190: if (success)
191: break;
1.14 markus 192: strlcpy(msg, "Bad passphrase, try again", sizeof msg);
1.12 markus 193: }
194: }
1.19 markus 195: xfree(comment);
196: if (ssh_add_identity(ac, private, saved_comment))
197: fprintf(stderr, "Identity added: %s (%s)\n", filename, saved_comment);
1.12 markus 198: else
199: fprintf(stderr, "Could not add identity: %s\n", filename);
1.16 markus 200: key_free(private);
1.19 markus 201: xfree(saved_comment);
1.1 deraadt 202: }
203:
1.2 provos 204: void
1.11 markus 205: list_identities(AuthenticationConnection *ac, int fp)
1.1 deraadt 206: {
1.19 markus 207: Key *key;
1.12 markus 208: char *comment;
1.19 markus 209: int had_identities = 0;
210: int version;
1.12 markus 211:
1.19 markus 212: for (version = 1; version <= 2; version++) {
213: for (key = ssh_get_first_identity(ac, &comment, version);
214: key != NULL;
215: key = ssh_get_next_identity(ac, &comment, version)) {
216: had_identities = 1;
217: if (fp) {
1.23 ! markus 218: printf("%d %s %s (%s)\n",
! 219: key_size(key), key_fingerprint(key),
! 220: comment, key_type(key));
1.12 markus 221: } else {
1.19 markus 222: if (!key_write(key, stdout))
223: fprintf(stderr, "key_write failed");
224: fprintf(stdout, " %s\n", comment);
1.12 markus 225: }
1.19 markus 226: key_free(key);
227: xfree(comment);
1.12 markus 228: }
229: }
230: if (!had_identities)
231: printf("The agent has no identities.\n");
1.1 deraadt 232: }
233:
1.2 provos 234: int
1.7 markus 235: main(int argc, char **argv)
1.1 deraadt 236: {
1.12 markus 237: AuthenticationConnection *ac = NULL;
238: struct passwd *pw;
239: char buf[1024];
240: int no_files = 1;
241: int i;
242: int deleting = 0;
243:
1.19 markus 244: SSLeay_add_all_algorithms();
245:
1.12 markus 246: /* At first, get a connection to the authentication agent. */
247: ac = ssh_get_authentication_connection();
248: if (ac == NULL) {
249: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
250: exit(1);
251: }
252: for (i = 1; i < argc; i++) {
253: if ((strcmp(argv[i], "-l") == 0) ||
254: (strcmp(argv[i], "-L") == 0)) {
255: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
256: /* Don't default-add/delete if -l. */
257: no_files = 0;
258: continue;
259: }
260: if (strcmp(argv[i], "-d") == 0) {
261: deleting = 1;
262: continue;
263: }
264: if (strcmp(argv[i], "-D") == 0) {
265: delete_all(ac);
266: no_files = 0;
267: continue;
268: }
269: no_files = 0;
270: if (deleting)
271: delete_file(ac, argv[i]);
272: else
273: add_file(ac, argv[i]);
274: }
275: if (no_files) {
276: pw = getpwuid(getuid());
277: if (!pw) {
1.20 deraadt 278: fprintf(stderr, "No user found with uid %u\n",
279: (u_int)getuid());
1.12 markus 280: ssh_close_authentication_connection(ac);
281: exit(1);
282: }
283: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, SSH_CLIENT_IDENTITY);
284: if (deleting)
285: delete_file(ac, buf);
286: else
287: add_file(ac, buf);
288: }
289: ssh_close_authentication_connection(ac);
290: exit(0);
1.1 deraadt 291: }