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