10Sstevel@tonic-gate /*
2*7947SHuie-Ying.Lee@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /*
60Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
70Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
80Sstevel@tonic-gate * All rights reserved
90Sstevel@tonic-gate * Adds an identity to the authentication server, or removes an identity.
100Sstevel@tonic-gate *
110Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
120Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
130Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
140Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
150Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * SSH2 implementation,
180Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
210Sstevel@tonic-gate * modification, are permitted provided that the following conditions
220Sstevel@tonic-gate * are met:
230Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
240Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
250Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
260Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
270Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
300Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
310Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
320Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
330Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
340Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
350Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
360Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
370Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
380Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
390Sstevel@tonic-gate */
400Sstevel@tonic-gate
410Sstevel@tonic-gate #include "includes.h"
420Sstevel@tonic-gate RCSID("$OpenBSD: ssh-add.c,v 1.63 2002/09/19 15:51:23 markus Exp $");
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <openssl/evp.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include "ssh.h"
470Sstevel@tonic-gate #include "rsa.h"
480Sstevel@tonic-gate #include "log.h"
490Sstevel@tonic-gate #include "xmalloc.h"
500Sstevel@tonic-gate #include "key.h"
510Sstevel@tonic-gate #include "authfd.h"
520Sstevel@tonic-gate #include "authfile.h"
530Sstevel@tonic-gate #include "pathnames.h"
540Sstevel@tonic-gate #include "readpass.h"
550Sstevel@tonic-gate #include "misc.h"
560Sstevel@tonic-gate
570Sstevel@tonic-gate #ifdef HAVE___PROGNAME
580Sstevel@tonic-gate extern char *__progname;
590Sstevel@tonic-gate #else
600Sstevel@tonic-gate char *__progname;
610Sstevel@tonic-gate #endif
620Sstevel@tonic-gate
630Sstevel@tonic-gate /* argv0 */
640Sstevel@tonic-gate extern char *__progname;
650Sstevel@tonic-gate
660Sstevel@tonic-gate /* Default files to add */
670Sstevel@tonic-gate static char *default_files[] = {
680Sstevel@tonic-gate _PATH_SSH_CLIENT_ID_RSA,
690Sstevel@tonic-gate _PATH_SSH_CLIENT_ID_DSA,
700Sstevel@tonic-gate _PATH_SSH_CLIENT_IDENTITY,
710Sstevel@tonic-gate NULL
720Sstevel@tonic-gate };
730Sstevel@tonic-gate
740Sstevel@tonic-gate /* Default lifetime (0 == forever) */
750Sstevel@tonic-gate static int lifetime = 0;
760Sstevel@tonic-gate
770Sstevel@tonic-gate /* we keep a cache of one passphrases */
780Sstevel@tonic-gate static char *pass = NULL;
790Sstevel@tonic-gate static void
clear_pass(void)800Sstevel@tonic-gate clear_pass(void)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate if (pass) {
830Sstevel@tonic-gate memset(pass, 0, strlen(pass));
840Sstevel@tonic-gate xfree(pass);
850Sstevel@tonic-gate pass = NULL;
860Sstevel@tonic-gate }
870Sstevel@tonic-gate }
880Sstevel@tonic-gate
890Sstevel@tonic-gate static int
delete_file(AuthenticationConnection * ac,const char * filename)900Sstevel@tonic-gate delete_file(AuthenticationConnection *ac, const char *filename)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate Key *public;
930Sstevel@tonic-gate char *comment = NULL;
940Sstevel@tonic-gate int ret = -1;
950Sstevel@tonic-gate
960Sstevel@tonic-gate public = key_load_public(filename, &comment);
970Sstevel@tonic-gate if (public == NULL) {
980Sstevel@tonic-gate printf(gettext("Bad key file %s\n"), filename);
990Sstevel@tonic-gate return -1;
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate if (ssh_remove_identity(ac, public)) {
1020Sstevel@tonic-gate fprintf(stderr, gettext("Identity removed: %s (%s)\n"),
1030Sstevel@tonic-gate filename, comment);
1040Sstevel@tonic-gate ret = 0;
1050Sstevel@tonic-gate } else
1060Sstevel@tonic-gate fprintf(stderr, gettext("Could not remove identity: %s\n"),
1070Sstevel@tonic-gate filename);
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate key_free(public);
1100Sstevel@tonic-gate xfree(comment);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate return ret;
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /* Send a request to remove all identities. */
1160Sstevel@tonic-gate static int
delete_all(AuthenticationConnection * ac)1170Sstevel@tonic-gate delete_all(AuthenticationConnection *ac)
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate int ret = -1;
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (ssh_remove_all_identities(ac, 1))
1220Sstevel@tonic-gate ret = 0;
1230Sstevel@tonic-gate /* ignore error-code for ssh2 */
1240Sstevel@tonic-gate ssh_remove_all_identities(ac, 2);
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (ret == 0)
1270Sstevel@tonic-gate fprintf(stderr, gettext("All identities removed.\n"));
1280Sstevel@tonic-gate else
1290Sstevel@tonic-gate fprintf(stderr, gettext("Failed to remove all identities.\n"));
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate return ret;
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate static int
add_file(AuthenticationConnection * ac,const char * filename)1350Sstevel@tonic-gate add_file(AuthenticationConnection *ac, const char *filename)
1360Sstevel@tonic-gate {
1370Sstevel@tonic-gate struct stat st;
1380Sstevel@tonic-gate Key *private;
1390Sstevel@tonic-gate char *comment = NULL;
1400Sstevel@tonic-gate char msg[1024];
1410Sstevel@tonic-gate int ret = -1;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if (stat(filename, &st) < 0) {
1440Sstevel@tonic-gate perror(filename);
1450Sstevel@tonic-gate return -1;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate /* At first, try empty passphrase */
1480Sstevel@tonic-gate private = key_load_private(filename, "", &comment);
1490Sstevel@tonic-gate if (comment == NULL)
1500Sstevel@tonic-gate comment = xstrdup(filename);
1510Sstevel@tonic-gate /* try last */
1520Sstevel@tonic-gate if (private == NULL && pass != NULL)
1530Sstevel@tonic-gate private = key_load_private(filename, pass, NULL);
1540Sstevel@tonic-gate if (private == NULL) {
1550Sstevel@tonic-gate /* clear passphrase since it did not work */
1560Sstevel@tonic-gate clear_pass();
1570Sstevel@tonic-gate snprintf(msg, sizeof msg,
1580Sstevel@tonic-gate gettext("Enter passphrase for %.200s: "), comment);
1590Sstevel@tonic-gate for (;;) {
1600Sstevel@tonic-gate pass = read_passphrase(msg, RP_ALLOW_STDIN);
1610Sstevel@tonic-gate if (strcmp(pass, "") == 0) {
1620Sstevel@tonic-gate clear_pass();
1630Sstevel@tonic-gate xfree(comment);
1640Sstevel@tonic-gate return -1;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate private = key_load_private(filename, pass, &comment);
1670Sstevel@tonic-gate if (private != NULL)
1680Sstevel@tonic-gate break;
1690Sstevel@tonic-gate clear_pass();
1700Sstevel@tonic-gate strlcpy(msg, gettext("Bad passphrase, try again: "),
1710Sstevel@tonic-gate sizeof msg);
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate if (ssh_add_identity_constrained(ac, private, comment, lifetime)) {
1760Sstevel@tonic-gate fprintf(stderr, gettext("Identity added: %s (%s)\n"),
1770Sstevel@tonic-gate filename, comment);
1780Sstevel@tonic-gate ret = 0;
1790Sstevel@tonic-gate if (lifetime != 0)
1800Sstevel@tonic-gate fprintf(stderr,
1810Sstevel@tonic-gate gettext("Lifetime set to %d seconds\n"), lifetime);
1820Sstevel@tonic-gate } else if (ssh_add_identity(ac, private, comment)) {
1830Sstevel@tonic-gate fprintf(stderr, gettext("Identity added: %s (%s)\n"),
1840Sstevel@tonic-gate filename, comment);
1850Sstevel@tonic-gate ret = 0;
1860Sstevel@tonic-gate } else {
1870Sstevel@tonic-gate fprintf(stderr, gettext("Could not add identity: %s\n"),
1880Sstevel@tonic-gate filename);
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate xfree(comment);
1920Sstevel@tonic-gate key_free(private);
1930Sstevel@tonic-gate
1940Sstevel@tonic-gate return ret;
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate static int
list_identities(AuthenticationConnection * ac,int do_fp)1980Sstevel@tonic-gate list_identities(AuthenticationConnection *ac, int do_fp)
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate Key *key;
2010Sstevel@tonic-gate char *comment, *fp;
2020Sstevel@tonic-gate int had_identities = 0;
2030Sstevel@tonic-gate int version;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate for (version = 1; version <= 2; version++) {
2060Sstevel@tonic-gate for (key = ssh_get_first_identity(ac, &comment, version);
2070Sstevel@tonic-gate key != NULL;
2080Sstevel@tonic-gate key = ssh_get_next_identity(ac, &comment, version)) {
2090Sstevel@tonic-gate had_identities = 1;
2100Sstevel@tonic-gate if (do_fp) {
2110Sstevel@tonic-gate fp = key_fingerprint(key, SSH_FP_MD5,
2120Sstevel@tonic-gate SSH_FP_HEX);
2130Sstevel@tonic-gate printf("%d %s %s (%s)\n",
2140Sstevel@tonic-gate key_size(key), fp, comment, key_type(key));
2150Sstevel@tonic-gate xfree(fp);
2160Sstevel@tonic-gate } else {
2170Sstevel@tonic-gate if (!key_write(key, stdout))
2180Sstevel@tonic-gate fprintf(stderr,
2190Sstevel@tonic-gate gettext("key_write failed"));
2200Sstevel@tonic-gate fprintf(stdout, " %s\n", comment);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate key_free(key);
2230Sstevel@tonic-gate xfree(comment);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate if (!had_identities) {
2270Sstevel@tonic-gate printf(gettext("The agent has no identities.\n"));
2280Sstevel@tonic-gate return -1;
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate return 0;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate static int
lock_agent(AuthenticationConnection * ac,int lock)2340Sstevel@tonic-gate lock_agent(AuthenticationConnection *ac, int lock)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate char prompt[100], *p1, *p2;
2370Sstevel@tonic-gate int passok = 1, ret = -1;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
2400Sstevel@tonic-gate p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
2410Sstevel@tonic-gate if (lock) {
2420Sstevel@tonic-gate strlcpy(prompt, "Again: ", sizeof prompt);
2430Sstevel@tonic-gate p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
2440Sstevel@tonic-gate if (strcmp(p1, p2) != 0) {
2450Sstevel@tonic-gate fprintf(stderr, gettext("Passwords do not match.\n"));
2460Sstevel@tonic-gate passok = 0;
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate memset(p2, 0, strlen(p2));
2490Sstevel@tonic-gate xfree(p2);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate if (passok && ssh_lock_agent(ac, lock, p1)) {
2520Sstevel@tonic-gate if (lock)
2530Sstevel@tonic-gate fprintf(stderr, gettext("Agent locked.\n"));
2540Sstevel@tonic-gate else
2550Sstevel@tonic-gate fprintf(stderr, gettext("Agent unlocked.\n"));
2560Sstevel@tonic-gate ret = 0;
2570Sstevel@tonic-gate } else {
2580Sstevel@tonic-gate if (lock)
2590Sstevel@tonic-gate fprintf(stderr, gettext("Failed to lock agent.\n"));
2600Sstevel@tonic-gate else
2610Sstevel@tonic-gate fprintf(stderr, gettext("Failed to unlock agent.\n"));
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate memset(p1, 0, strlen(p1));
2640Sstevel@tonic-gate xfree(p1);
2650Sstevel@tonic-gate return (ret);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate static int
do_file(AuthenticationConnection * ac,int deleting,char * file)2690Sstevel@tonic-gate do_file(AuthenticationConnection *ac, int deleting, char *file)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate if (deleting) {
2720Sstevel@tonic-gate if (delete_file(ac, file) == -1)
2730Sstevel@tonic-gate return -1;
2740Sstevel@tonic-gate } else {
2750Sstevel@tonic-gate if (add_file(ac, file) == -1)
2760Sstevel@tonic-gate return -1;
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate return 0;
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate
2810Sstevel@tonic-gate static void
usage(void)2820Sstevel@tonic-gate usage(void)
2830Sstevel@tonic-gate {
2840Sstevel@tonic-gate fprintf(stderr,
2850Sstevel@tonic-gate gettext( "Usage: %s [options]\n"
2860Sstevel@tonic-gate "Options:\n"
2870Sstevel@tonic-gate " -l List fingerprints of all identities.\n"
2880Sstevel@tonic-gate " -L List public key parameters of all identities.\n"
2890Sstevel@tonic-gate " -d Delete identity.\n"
2900Sstevel@tonic-gate " -D Delete all identities.\n"
2910Sstevel@tonic-gate " -x Lock agent.\n"
2920Sstevel@tonic-gate " -X Unlock agent.\n"
2930Sstevel@tonic-gate " -t life Set lifetime (seconds) when adding identities.\n"
2940Sstevel@tonic-gate ), __progname);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate int
main(int argc,char ** argv)2980Sstevel@tonic-gate main(int argc, char **argv)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate extern char *optarg;
3010Sstevel@tonic-gate extern int optind;
3020Sstevel@tonic-gate AuthenticationConnection *ac = NULL;
3030Sstevel@tonic-gate int i, ch, deleting = 0, ret = 0;
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate __progname = get_progname(argv[0]);
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate (void) g11n_setlocale(LC_ALL, "");
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate init_rng();
3100Sstevel@tonic-gate seed_rng();
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate SSLeay_add_all_algorithms();
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /* At first, get a connection to the authentication agent. */
3150Sstevel@tonic-gate ac = ssh_get_authentication_connection();
3160Sstevel@tonic-gate if (ac == NULL) {
3170Sstevel@tonic-gate fprintf(stderr, gettext("Could not open a connection "
3180Sstevel@tonic-gate "to your authentication agent.\n"));
3190Sstevel@tonic-gate exit(2);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate while ((ch = getopt(argc, argv, "lLdDxXe:s:t:")) != -1) {
3220Sstevel@tonic-gate switch (ch) {
3230Sstevel@tonic-gate case 'l':
3240Sstevel@tonic-gate case 'L':
3250Sstevel@tonic-gate if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
3260Sstevel@tonic-gate ret = 1;
3270Sstevel@tonic-gate goto done;
3280Sstevel@tonic-gate break;
3290Sstevel@tonic-gate case 'x':
3300Sstevel@tonic-gate case 'X':
3310Sstevel@tonic-gate if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
3320Sstevel@tonic-gate ret = 1;
3330Sstevel@tonic-gate goto done;
3340Sstevel@tonic-gate break;
3350Sstevel@tonic-gate case 'd':
3360Sstevel@tonic-gate deleting = 1;
3370Sstevel@tonic-gate break;
3380Sstevel@tonic-gate case 'D':
3390Sstevel@tonic-gate if (delete_all(ac) == -1)
3400Sstevel@tonic-gate ret = 1;
3410Sstevel@tonic-gate goto done;
3420Sstevel@tonic-gate break;
3430Sstevel@tonic-gate case 't':
3440Sstevel@tonic-gate if ((lifetime = convtime(optarg)) == -1) {
3450Sstevel@tonic-gate fprintf(stderr, gettext("Invalid lifetime\n"));
3460Sstevel@tonic-gate ret = 1;
3470Sstevel@tonic-gate goto done;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate break;
3500Sstevel@tonic-gate default:
3510Sstevel@tonic-gate usage();
3520Sstevel@tonic-gate ret = 1;
3530Sstevel@tonic-gate goto done;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate argc -= optind;
3570Sstevel@tonic-gate argv += optind;
3580Sstevel@tonic-gate if (argc == 0) {
3590Sstevel@tonic-gate char buf[MAXPATHLEN];
3600Sstevel@tonic-gate struct passwd *pw;
3610Sstevel@tonic-gate struct stat st;
3620Sstevel@tonic-gate int count = 0;
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL) {
3650Sstevel@tonic-gate fprintf(stderr, gettext("No user found with uid %u\n"),
3660Sstevel@tonic-gate (u_int)getuid());
3670Sstevel@tonic-gate ret = 1;
3680Sstevel@tonic-gate goto done;
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate
3710Sstevel@tonic-gate for(i = 0; default_files[i]; i++) {
3720Sstevel@tonic-gate snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
3730Sstevel@tonic-gate default_files[i]);
3740Sstevel@tonic-gate if (stat(buf, &st) < 0)
3750Sstevel@tonic-gate continue;
3760Sstevel@tonic-gate if (do_file(ac, deleting, buf) == -1)
3770Sstevel@tonic-gate ret = 1;
3780Sstevel@tonic-gate else
3790Sstevel@tonic-gate count++;
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate if (count == 0)
3820Sstevel@tonic-gate ret = 1;
3830Sstevel@tonic-gate } else {
3840Sstevel@tonic-gate for(i = 0; i < argc; i++) {
3850Sstevel@tonic-gate if (do_file(ac, deleting, argv[i]) == -1)
3860Sstevel@tonic-gate ret = 1;
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate clear_pass();
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate done:
3920Sstevel@tonic-gate ssh_close_authentication_connection(ac);
3930Sstevel@tonic-gate return ret;
3940Sstevel@tonic-gate }
395