Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.41
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,
1.41 ! markus 14: * Copyright (c) 2000, 2001 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.41 ! markus 38: RCSID("$OpenBSD: ssh-add.c,v 1.40 2001/06/24 05:35:33 markus Exp $");
1.16 markus 39:
1.19 markus 40: #include <openssl/evp.h>
1.1 deraadt 41:
1.27 markus 42: #include "ssh.h"
1.1 deraadt 43: #include "rsa.h"
1.27 markus 44: #include "log.h"
1.1 deraadt 45: #include "xmalloc.h"
1.16 markus 46: #include "key.h"
1.18 markus 47: #include "authfd.h"
1.16 markus 48: #include "authfile.h"
1.25 markus 49: #include "pathnames.h"
1.27 markus 50: #include "readpass.h"
1.1 deraadt 51:
1.33 markus 52: /* we keep a cache of one passphrases */
53: static char *pass = NULL;
1.39 itojun 54: static void
1.33 markus 55: clear_pass(void)
56: {
57: if (pass) {
58: memset(pass, 0, strlen(pass));
59: xfree(pass);
60: pass = NULL;
61: }
62: }
63:
1.39 itojun 64: static void
1.7 markus 65: delete_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 66: {
1.16 markus 67: Key *public;
1.32 markus 68: char *comment = NULL;
1.1 deraadt 69:
1.31 markus 70: public = key_load_public(filename, &comment);
71: if (public == NULL) {
72: printf("Bad key file %s\n", filename);
73: return;
1.12 markus 74: }
1.19 markus 75: if (ssh_remove_identity(ac, public))
1.12 markus 76: fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
77: else
78: fprintf(stderr, "Could not remove identity: %s\n", filename);
1.16 markus 79: key_free(public);
1.12 markus 80: xfree(comment);
1.1 deraadt 81: }
82:
1.19 markus 83: /* Send a request to remove all identities. */
1.39 itojun 84: static void
1.7 markus 85: delete_all(AuthenticationConnection *ac)
1.1 deraadt 86: {
1.19 markus 87: int success = 1;
88:
89: if (!ssh_remove_all_identities(ac, 1))
90: success = 0;
91: /* ignore error-code for ssh2 */
92: ssh_remove_all_identities(ac, 2);
93:
94: if (success)
1.12 markus 95: fprintf(stderr, "All identities removed.\n");
96: else
1.24 markus 97: fprintf(stderr, "Failed to remove all identities.\n");
1.1 deraadt 98: }
99:
1.39 itojun 100: static void
1.7 markus 101: add_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 102: {
1.19 markus 103: struct stat st;
1.16 markus 104: Key *private;
1.36 markus 105: char *comment = NULL;
106: char msg[1024];
1.12 markus 107:
1.19 markus 108: if (stat(filename, &st) < 0) {
109: perror(filename);
110: exit(1);
111: }
1.12 markus 112: /* At first, try empty passphrase */
1.31 markus 113: private = key_load_private(filename, "", &comment);
114: if (comment == NULL)
115: comment = xstrdup(filename);
1.33 markus 116: /* try last */
117: if (private == NULL && pass != NULL)
118: private = key_load_private(filename, pass, NULL);
1.31 markus 119: if (private == NULL) {
1.33 markus 120: /* clear passphrase since it did not work */
121: clear_pass();
1.37 markus 122: snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.36 markus 123: comment);
1.12 markus 124: for (;;) {
1.40 markus 125: pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12 markus 126: if (strcmp(pass, "") == 0) {
1.35 markus 127: clear_pass();
1.31 markus 128: xfree(comment);
1.12 markus 129: return;
130: }
1.31 markus 131: private = key_load_private(filename, pass, &comment);
132: if (private != NULL)
1.12 markus 133: break;
1.33 markus 134: clear_pass();
1.37 markus 135: strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
1.12 markus 136: }
137: }
1.31 markus 138: if (ssh_add_identity(ac, private, comment))
139: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.12 markus 140: else
141: fprintf(stderr, "Could not add identity: %s\n", filename);
1.31 markus 142: xfree(comment);
1.16 markus 143: key_free(private);
1.1 deraadt 144: }
145:
1.39 itojun 146: static void
1.30 markus 147: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 148: {
1.19 markus 149: Key *key;
1.30 markus 150: char *comment, *fp;
1.19 markus 151: int had_identities = 0;
152: int version;
1.12 markus 153:
1.19 markus 154: for (version = 1; version <= 2; version++) {
155: for (key = ssh_get_first_identity(ac, &comment, version);
156: key != NULL;
157: key = ssh_get_next_identity(ac, &comment, version)) {
158: had_identities = 1;
1.30 markus 159: if (do_fp) {
160: fp = key_fingerprint(key, SSH_FP_MD5,
161: SSH_FP_HEX);
1.23 markus 162: printf("%d %s %s (%s)\n",
1.30 markus 163: key_size(key), fp, comment, key_type(key));
164: xfree(fp);
1.12 markus 165: } else {
1.19 markus 166: if (!key_write(key, stdout))
167: fprintf(stderr, "key_write failed");
168: fprintf(stdout, " %s\n", comment);
1.12 markus 169: }
1.19 markus 170: key_free(key);
171: xfree(comment);
1.12 markus 172: }
173: }
174: if (!had_identities)
175: printf("The agent has no identities.\n");
1.1 deraadt 176: }
177:
1.2 provos 178: int
1.7 markus 179: main(int argc, char **argv)
1.1 deraadt 180: {
1.12 markus 181: AuthenticationConnection *ac = NULL;
182: struct passwd *pw;
183: char buf[1024];
184: int no_files = 1;
185: int i;
186: int deleting = 0;
187:
1.28 stevesk 188: SSLeay_add_all_algorithms();
1.19 markus 189:
1.12 markus 190: /* At first, get a connection to the authentication agent. */
191: ac = ssh_get_authentication_connection();
192: if (ac == NULL) {
193: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
194: exit(1);
195: }
196: for (i = 1; i < argc; i++) {
197: if ((strcmp(argv[i], "-l") == 0) ||
198: (strcmp(argv[i], "-L") == 0)) {
199: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
200: /* Don't default-add/delete if -l. */
201: no_files = 0;
202: continue;
203: }
204: if (strcmp(argv[i], "-d") == 0) {
205: deleting = 1;
206: continue;
207: }
208: if (strcmp(argv[i], "-D") == 0) {
209: delete_all(ac);
210: no_files = 0;
211: continue;
212: }
213: no_files = 0;
214: if (deleting)
215: delete_file(ac, argv[i]);
216: else
217: add_file(ac, argv[i]);
218: }
219: if (no_files) {
220: pw = getpwuid(getuid());
221: if (!pw) {
1.20 deraadt 222: fprintf(stderr, "No user found with uid %u\n",
223: (u_int)getuid());
1.12 markus 224: ssh_close_authentication_connection(ac);
225: exit(1);
226: }
1.25 markus 227: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12 markus 228: if (deleting)
229: delete_file(ac, buf);
230: else
231: add_file(ac, buf);
232: }
1.33 markus 233: clear_pass();
1.12 markus 234: ssh_close_authentication_connection(ac);
235: exit(0);
1.1 deraadt 236: }