Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.22.2.4
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.22.2.3 jason 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.22.2.4! jason 38: RCSID("$OpenBSD: ssh-add.c,v 1.36 2001/04/18 21:57:42 markus Exp $");
1.16 markus 39:
1.19 markus 40: #include <openssl/evp.h>
1.1 deraadt 41:
42: #include "ssh.h"
1.22.2.1 jason 43: #include "rsa.h"
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.22.2.1 jason 49: #include "pathnames.h"
50: #include "readpass.h"
1.1 deraadt 51:
1.22.2.4! jason 52: /* we keep a cache of one passphrases */
! 53: static char *pass = NULL;
! 54: void
! 55: clear_pass(void)
! 56: {
! 57: if (pass) {
! 58: memset(pass, 0, strlen(pass));
! 59: xfree(pass);
! 60: pass = NULL;
! 61: }
! 62: }
! 63:
1.2 provos 64: void
1.7 markus 65: delete_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 66: {
1.16 markus 67: Key *public;
1.22.2.4! jason 68: char *comment = NULL;
1.1 deraadt 69:
1.22.2.4! jason 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.2 provos 84: 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.22.2.1 jason 97: fprintf(stderr, "Failed to remove all identities.\n");
1.1 deraadt 98: }
99:
1.2 provos 100: 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.22.2.4! jason 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.22.2.4! jason 113: private = key_load_private(filename, "", &comment);
! 114: if (comment == NULL)
! 115: comment = xstrdup(filename);
! 116: /* try last */
! 117: if (private == NULL && pass != NULL)
! 118: private = key_load_private(filename, pass, NULL);
! 119: if (private == NULL) {
! 120: /* clear passphrase since it did not work */
! 121: clear_pass();
1.14 markus 122: printf("Need passphrase for %.200s\n", filename);
1.22.2.4! jason 123: snprintf(msg, sizeof msg, "Enter passphrase for %.200s ",
! 124: comment);
1.12 markus 125: for (;;) {
1.22.2.4! jason 126: pass = read_passphrase(msg, 1);
1.12 markus 127: if (strcmp(pass, "") == 0) {
1.22.2.4! jason 128: clear_pass();
! 129: xfree(comment);
1.12 markus 130: return;
131: }
1.22.2.4! jason 132: private = key_load_private(filename, pass, &comment);
! 133: if (private != NULL)
1.12 markus 134: break;
1.22.2.4! jason 135: clear_pass();
! 136: strlcpy(msg, "Bad passphrase, try again ", sizeof msg);
1.12 markus 137: }
138: }
1.22.2.4! jason 139: if (ssh_add_identity(ac, private, comment))
! 140: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.12 markus 141: else
142: fprintf(stderr, "Could not add identity: %s\n", filename);
1.22.2.4! jason 143: xfree(comment);
1.16 markus 144: key_free(private);
1.1 deraadt 145: }
146:
1.2 provos 147: void
1.22.2.3 jason 148: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 149: {
1.19 markus 150: Key *key;
1.22.2.3 jason 151: char *comment, *fp;
1.19 markus 152: int had_identities = 0;
153: int version;
1.12 markus 154:
1.19 markus 155: for (version = 1; version <= 2; version++) {
156: for (key = ssh_get_first_identity(ac, &comment, version);
157: key != NULL;
158: key = ssh_get_next_identity(ac, &comment, version)) {
159: had_identities = 1;
1.22.2.3 jason 160: if (do_fp) {
161: fp = key_fingerprint(key, SSH_FP_MD5,
162: SSH_FP_HEX);
1.22.2.1 jason 163: printf("%d %s %s (%s)\n",
1.22.2.3 jason 164: key_size(key), fp, comment, key_type(key));
165: xfree(fp);
1.12 markus 166: } else {
1.19 markus 167: if (!key_write(key, stdout))
168: fprintf(stderr, "key_write failed");
169: fprintf(stdout, " %s\n", comment);
1.12 markus 170: }
1.19 markus 171: key_free(key);
172: xfree(comment);
1.12 markus 173: }
174: }
175: if (!had_identities)
176: printf("The agent has no identities.\n");
1.1 deraadt 177: }
178:
1.2 provos 179: int
1.7 markus 180: main(int argc, char **argv)
1.1 deraadt 181: {
1.12 markus 182: AuthenticationConnection *ac = NULL;
183: struct passwd *pw;
184: char buf[1024];
185: int no_files = 1;
186: int i;
187: int deleting = 0;
188:
1.22.2.1 jason 189: SSLeay_add_all_algorithms();
1.19 markus 190:
1.12 markus 191: /* At first, get a connection to the authentication agent. */
192: ac = ssh_get_authentication_connection();
193: if (ac == NULL) {
194: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
195: exit(1);
196: }
197: for (i = 1; i < argc; i++) {
198: if ((strcmp(argv[i], "-l") == 0) ||
199: (strcmp(argv[i], "-L") == 0)) {
200: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
201: /* Don't default-add/delete if -l. */
202: no_files = 0;
203: continue;
204: }
205: if (strcmp(argv[i], "-d") == 0) {
206: deleting = 1;
207: continue;
208: }
209: if (strcmp(argv[i], "-D") == 0) {
210: delete_all(ac);
211: no_files = 0;
212: continue;
213: }
214: no_files = 0;
215: if (deleting)
216: delete_file(ac, argv[i]);
217: else
218: add_file(ac, argv[i]);
219: }
220: if (no_files) {
221: pw = getpwuid(getuid());
222: if (!pw) {
1.20 deraadt 223: fprintf(stderr, "No user found with uid %u\n",
224: (u_int)getuid());
1.12 markus 225: ssh_close_authentication_connection(ac);
226: exit(1);
227: }
1.22.2.1 jason 228: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12 markus 229: if (deleting)
230: delete_file(ac, buf);
231: else
232: add_file(ac, buf);
233: }
1.22.2.4! jason 234: clear_pass();
1.12 markus 235: ssh_close_authentication_connection(ac);
236: exit(0);
1.1 deraadt 237: }