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