Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.33
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.29 deraadt 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.33 ! markus 38: RCSID("$OpenBSD: ssh-add.c,v 1.32 2001/04/08 13:03:00 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;
! 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.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.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.24 markus 97: fprintf(stderr, "Failed to remove all identities.\n");
1.1 deraadt 98: }
99:
1.14 markus 100: char *
101: ssh_askpass(char *askpass, char *msg)
102: {
103: pid_t pid;
104: size_t len;
105: char *nl, *pass;
106: int p[2], status;
107: char buf[1024];
108:
1.26 markus 109: if (fflush(stdout) != 0)
110: error("ssh_askpass: fflush: %s", strerror(errno));
1.14 markus 111: if (askpass == NULL)
112: fatal("internal error: askpass undefined");
113: if (pipe(p) < 0)
114: fatal("ssh_askpass: pipe: %s", strerror(errno));
115: if ((pid = fork()) < 0)
116: fatal("ssh_askpass: fork: %s", strerror(errno));
117: if (pid == 0) {
118: close(p[0]);
119: if (dup2(p[1], STDOUT_FILENO) < 0)
120: fatal("ssh_askpass: dup2: %s", strerror(errno));
121: execlp(askpass, askpass, msg, (char *) 0);
122: fatal("ssh_askpass: exec(%s): %s", askpass, strerror(errno));
123: }
124: close(p[1]);
125: len = read(p[0], buf, sizeof buf);
126: close(p[0]);
127: while (waitpid(pid, &status, 0) < 0)
128: if (errno != EINTR)
129: break;
130: if (len <= 1)
131: return xstrdup("");
132: nl = strchr(buf, '\n');
133: if (nl)
134: *nl = '\0';
135: pass = xstrdup(buf);
136: memset(buf, 0, sizeof(buf));
137: return pass;
138: }
139:
1.2 provos 140: void
1.7 markus 141: add_file(AuthenticationConnection *ac, const char *filename)
1.1 deraadt 142: {
1.19 markus 143: struct stat st;
1.16 markus 144: Key *private;
1.33 ! markus 145: char *comment = NULL, *askpass = NULL;
1.14 markus 146: char buf[1024], msg[1024];
147: int interactive = isatty(STDIN_FILENO);
1.12 markus 148:
1.19 markus 149: if (stat(filename, &st) < 0) {
150: perror(filename);
151: exit(1);
152: }
1.15 markus 153: if (!interactive && getenv("DISPLAY")) {
154: if (getenv(SSH_ASKPASS_ENV))
155: askpass = getenv(SSH_ASKPASS_ENV);
156: else
1.25 markus 157: askpass = _PATH_SSH_ASKPASS_DEFAULT;
1.15 markus 158: }
1.14 markus 159:
1.12 markus 160: /* At first, try empty passphrase */
1.31 markus 161: private = key_load_private(filename, "", &comment);
162: if (comment == NULL)
163: comment = xstrdup(filename);
1.33 ! markus 164: /* try last */
! 165: if (private == NULL && pass != NULL)
! 166: private = key_load_private(filename, pass, NULL);
1.31 markus 167: if (private == NULL) {
1.33 ! markus 168: /* clear passphrase since it did not work */
! 169: clear_pass();
1.14 markus 170: printf("Need passphrase for %.200s\n", filename);
171: if (!interactive && askpass == NULL) {
1.31 markus 172: xfree(comment);
1.12 markus 173: return;
174: }
1.31 markus 175: snprintf(msg, sizeof msg, "Enter passphrase for %.200s", comment);
1.12 markus 176: for (;;) {
1.14 markus 177: if (interactive) {
178: snprintf(buf, sizeof buf, "%s: ", msg);
179: pass = read_passphrase(buf, 1);
180: } else {
181: pass = ssh_askpass(askpass, msg);
182: }
1.12 markus 183: if (strcmp(pass, "") == 0) {
184: xfree(pass);
1.31 markus 185: xfree(comment);
1.12 markus 186: return;
187: }
1.31 markus 188: private = key_load_private(filename, pass, &comment);
189: if (private != NULL)
1.12 markus 190: break;
1.33 ! markus 191: clear_pass();
1.14 markus 192: strlcpy(msg, "Bad passphrase, try again", sizeof msg);
1.12 markus 193: }
194: }
1.31 markus 195: if (ssh_add_identity(ac, private, comment))
196: fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.12 markus 197: else
198: fprintf(stderr, "Could not add identity: %s\n", filename);
1.31 markus 199: xfree(comment);
1.16 markus 200: key_free(private);
1.1 deraadt 201: }
202:
1.2 provos 203: void
1.30 markus 204: list_identities(AuthenticationConnection *ac, int do_fp)
1.1 deraadt 205: {
1.19 markus 206: Key *key;
1.30 markus 207: char *comment, *fp;
1.19 markus 208: int had_identities = 0;
209: int version;
1.12 markus 210:
1.19 markus 211: for (version = 1; version <= 2; version++) {
212: for (key = ssh_get_first_identity(ac, &comment, version);
213: key != NULL;
214: key = ssh_get_next_identity(ac, &comment, version)) {
215: had_identities = 1;
1.30 markus 216: if (do_fp) {
217: fp = key_fingerprint(key, SSH_FP_MD5,
218: SSH_FP_HEX);
1.23 markus 219: printf("%d %s %s (%s)\n",
1.30 markus 220: key_size(key), fp, comment, key_type(key));
221: xfree(fp);
1.12 markus 222: } else {
1.19 markus 223: if (!key_write(key, stdout))
224: fprintf(stderr, "key_write failed");
225: fprintf(stdout, " %s\n", comment);
1.12 markus 226: }
1.19 markus 227: key_free(key);
228: xfree(comment);
1.12 markus 229: }
230: }
231: if (!had_identities)
232: printf("The agent has no identities.\n");
1.1 deraadt 233: }
234:
1.2 provos 235: int
1.7 markus 236: main(int argc, char **argv)
1.1 deraadt 237: {
1.12 markus 238: AuthenticationConnection *ac = NULL;
239: struct passwd *pw;
240: char buf[1024];
241: int no_files = 1;
242: int i;
243: int deleting = 0;
244:
1.28 stevesk 245: SSLeay_add_all_algorithms();
1.19 markus 246:
1.12 markus 247: /* At first, get a connection to the authentication agent. */
248: ac = ssh_get_authentication_connection();
249: if (ac == NULL) {
250: fprintf(stderr, "Could not open a connection to your authentication agent.\n");
251: exit(1);
252: }
253: for (i = 1; i < argc; i++) {
254: if ((strcmp(argv[i], "-l") == 0) ||
255: (strcmp(argv[i], "-L") == 0)) {
256: list_identities(ac, argv[i][1] == 'l' ? 1 : 0);
257: /* Don't default-add/delete if -l. */
258: no_files = 0;
259: continue;
260: }
261: if (strcmp(argv[i], "-d") == 0) {
262: deleting = 1;
263: continue;
264: }
265: if (strcmp(argv[i], "-D") == 0) {
266: delete_all(ac);
267: no_files = 0;
268: continue;
269: }
270: no_files = 0;
271: if (deleting)
272: delete_file(ac, argv[i]);
273: else
274: add_file(ac, argv[i]);
275: }
276: if (no_files) {
277: pw = getpwuid(getuid());
278: if (!pw) {
1.20 deraadt 279: fprintf(stderr, "No user found with uid %u\n",
280: (u_int)getuid());
1.12 markus 281: ssh_close_authentication_connection(ac);
282: exit(1);
283: }
1.25 markus 284: snprintf(buf, sizeof buf, "%s/%s", pw->pw_dir, _PATH_SSH_CLIENT_IDENTITY);
1.12 markus 285: if (deleting)
286: delete_file(ac, buf);
287: else
288: add_file(ac, buf);
289: }
1.33 ! markus 290: clear_pass();
1.12 markus 291: ssh_close_authentication_connection(ac);
292: exit(0);
1.1 deraadt 293: }