[BACK]Return to ssh-add.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/ssh-add.c, Revision 1.54

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.41      markus     14:  * Copyright (c) 2000, 2001 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.54    ! markus     38: RCSID("$OpenBSD: ssh-add.c,v 1.53 2002/03/21 22:44:05 rees 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.45      jakob      52: /* argv0 */
                     53: extern char *__progname;
                     54:
1.48      djm        55: /* Default files to add */
                     56: static char *default_files[] = {
                     57:        _PATH_SSH_CLIENT_ID_RSA,
                     58:        _PATH_SSH_CLIENT_ID_DSA,
1.51      markus     59:        _PATH_SSH_CLIENT_IDENTITY,
1.48      djm        60:        NULL
                     61: };
                     62:
                     63:
1.33      markus     64: /* we keep a cache of one passphrases */
                     65: static char *pass = NULL;
1.39      itojun     66: static void
1.33      markus     67: clear_pass(void)
                     68: {
                     69:        if (pass) {
                     70:                memset(pass, 0, strlen(pass));
                     71:                xfree(pass);
                     72:                pass = NULL;
                     73:        }
                     74: }
                     75:
1.46      djm        76: static int
1.7       markus     77: delete_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    78: {
1.16      markus     79:        Key *public;
1.32      markus     80:        char *comment = NULL;
1.46      djm        81:        int ret = -1;
1.1       deraadt    82:
1.31      markus     83:        public = key_load_public(filename, &comment);
                     84:        if (public == NULL) {
                     85:                printf("Bad key file %s\n", filename);
1.46      djm        86:                return -1;
1.12      markus     87:        }
1.46      djm        88:        if (ssh_remove_identity(ac, public)) {
1.12      markus     89:                fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46      djm        90:                ret = 0;
                     91:        } else
1.12      markus     92:                fprintf(stderr, "Could not remove identity: %s\n", filename);
1.46      djm        93:
1.16      markus     94:        key_free(public);
1.12      markus     95:        xfree(comment);
1.47      deraadt    96:
1.46      djm        97:        return ret;
1.1       deraadt    98: }
                     99:
1.19      markus    100: /* Send a request to remove all identities. */
1.46      djm       101: static int
1.7       markus    102: delete_all(AuthenticationConnection *ac)
1.1       deraadt   103: {
1.46      djm       104:        int ret = -1;
1.19      markus    105:
1.46      djm       106:        if (ssh_remove_all_identities(ac, 1))
                    107:                ret = 0;
1.19      markus    108:        /* ignore error-code for ssh2 */
                    109:        ssh_remove_all_identities(ac, 2);
                    110:
1.46      djm       111:        if (ret == 0)
1.12      markus    112:                fprintf(stderr, "All identities removed.\n");
                    113:        else
1.24      markus    114:                fprintf(stderr, "Failed to remove all identities.\n");
1.46      djm       115:
                    116:        return ret;
1.1       deraadt   117: }
                    118:
1.46      djm       119: static int
1.7       markus    120: add_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt   121: {
1.19      markus    122:        struct stat st;
1.16      markus    123:        Key *private;
1.36      markus    124:        char *comment = NULL;
                    125:        char msg[1024];
1.46      djm       126:        int ret = -1;
1.12      markus    127:
1.19      markus    128:        if (stat(filename, &st) < 0) {
                    129:                perror(filename);
1.46      djm       130:                return -1;
1.19      markus    131:        }
1.12      markus    132:        /* At first, try empty passphrase */
1.31      markus    133:        private = key_load_private(filename, "", &comment);
                    134:        if (comment == NULL)
                    135:                comment = xstrdup(filename);
1.33      markus    136:        /* try last */
                    137:        if (private == NULL && pass != NULL)
                    138:                private = key_load_private(filename, pass, NULL);
1.31      markus    139:        if (private == NULL) {
1.33      markus    140:                /* clear passphrase since it did not work */
                    141:                clear_pass();
1.37      markus    142:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.36      markus    143:                   comment);
1.12      markus    144:                for (;;) {
1.40      markus    145:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12      markus    146:                        if (strcmp(pass, "") == 0) {
1.35      markus    147:                                clear_pass();
1.31      markus    148:                                xfree(comment);
1.46      djm       149:                                return -1;
1.12      markus    150:                        }
1.31      markus    151:                        private = key_load_private(filename, pass, &comment);
                    152:                        if (private != NULL)
1.12      markus    153:                                break;
1.33      markus    154:                        clear_pass();
1.37      markus    155:                        strlcpy(msg, "Bad passphrase, try again: ", sizeof msg);
1.12      markus    156:                }
                    157:        }
1.46      djm       158:        if (ssh_add_identity(ac, private, comment)) {
1.31      markus    159:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.46      djm       160:                ret = 0;
                    161:        } else
1.12      markus    162:                fprintf(stderr, "Could not add identity: %s\n", filename);
1.46      djm       163:
1.31      markus    164:        xfree(comment);
1.16      markus    165:        key_free(private);
1.47      deraadt   166:
1.46      djm       167:        return ret;
1.1       deraadt   168: }
                    169:
1.46      djm       170: static int
1.44      markus    171: update_card(AuthenticationConnection *ac, int add, const char *id)
1.42      markus    172: {
1.53      rees      173:        char *pin;
                    174:
                    175:        pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
                    176:        if (pin == NULL)
                    177:                return -1;
                    178:
                    179:        if (ssh_update_card(ac, add, id, pin)) {
1.44      markus    180:                fprintf(stderr, "Card %s: %s\n",
1.47      deraadt   181:                    add ? "added" : "removed", id);
1.46      djm       182:                return 0;
                    183:        } else {
1.44      markus    184:                fprintf(stderr, "Could not %s card: %s\n",
1.47      deraadt   185:                    add ? "add" : "remove", id);
1.46      djm       186:                return -1;
                    187:        }
1.42      markus    188: }
                    189:
1.50      markus    190: static int
1.30      markus    191: list_identities(AuthenticationConnection *ac, int do_fp)
1.1       deraadt   192: {
1.19      markus    193:        Key *key;
1.30      markus    194:        char *comment, *fp;
1.19      markus    195:        int had_identities = 0;
                    196:        int version;
1.12      markus    197:
1.19      markus    198:        for (version = 1; version <= 2; version++) {
                    199:                for (key = ssh_get_first_identity(ac, &comment, version);
1.47      deraadt   200:                    key != NULL;
                    201:                    key = ssh_get_next_identity(ac, &comment, version)) {
1.19      markus    202:                        had_identities = 1;
1.30      markus    203:                        if (do_fp) {
                    204:                                fp = key_fingerprint(key, SSH_FP_MD5,
                    205:                                    SSH_FP_HEX);
1.23      markus    206:                                printf("%d %s %s (%s)\n",
1.30      markus    207:                                    key_size(key), fp, comment, key_type(key));
                    208:                                xfree(fp);
1.12      markus    209:                        } else {
1.19      markus    210:                                if (!key_write(key, stdout))
                    211:                                        fprintf(stderr, "key_write failed");
                    212:                                fprintf(stdout, " %s\n", comment);
1.12      markus    213:                        }
1.19      markus    214:                        key_free(key);
                    215:                        xfree(comment);
1.12      markus    216:                }
                    217:        }
1.50      markus    218:        if (!had_identities) {
1.12      markus    219:                printf("The agent has no identities.\n");
1.50      markus    220:                return -1;
                    221:        }
                    222:        return 0;
1.1       deraadt   223: }
                    224:
1.48      djm       225: static int
1.54    ! markus    226: lock_agent(AuthenticationConnection *ac, int lock)
        !           227: {
        !           228:        char prompt[100], *p1, *p2;
        !           229:        int passok = 1, ret = -1;
        !           230:
        !           231:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
        !           232:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
        !           233:        if (lock) {
        !           234:                strlcpy(prompt, "Again: ", sizeof prompt);
        !           235:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
        !           236:                if (strcmp(p1, p2) != 0) {
        !           237:                        fprintf(stderr, "Passwords do not match.\n");
        !           238:                        passok = 0;
        !           239:                }
        !           240:                memset(p2, 0, strlen(p2));
        !           241:                xfree(p2);
        !           242:        }
        !           243:        if (passok && ssh_lock_agent(ac, lock, p1)) {
        !           244:                fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
        !           245:                ret = 0;
        !           246:        } else
        !           247:                fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
        !           248:        memset(p1, 0, strlen(p1));
        !           249:        xfree(p1);
        !           250:        return -1;
        !           251: }
        !           252:
        !           253: static int
1.48      djm       254: do_file(AuthenticationConnection *ac, int deleting, char *file)
                    255: {
                    256:        if (deleting) {
                    257:                if (delete_file(ac, file) == -1)
                    258:                        return -1;
                    259:        } else {
                    260:                if (add_file(ac, file) == -1)
                    261:                        return -1;
                    262:        }
                    263:        return 0;
                    264: }
                    265:
1.42      markus    266: static void
                    267: usage(void)
                    268: {
1.45      jakob     269:        fprintf(stderr, "Usage: %s [options]\n", __progname);
                    270:        fprintf(stderr, "Options:\n");
                    271:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
                    272:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
                    273:        fprintf(stderr, "  -d          Delete identity.\n");
                    274:        fprintf(stderr, "  -D          Delete all identities.\n");
                    275: #ifdef SMARTCARD
                    276:        fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
                    277:        fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
                    278: #endif
1.42      markus    279: }
                    280:
1.2       provos    281: int
1.7       markus    282: main(int argc, char **argv)
1.1       deraadt   283: {
1.43      markus    284:        extern char *optarg;
                    285:        extern int optind;
1.12      markus    286:        AuthenticationConnection *ac = NULL;
1.44      markus    287:        char *sc_reader_id = NULL;
1.46      djm       288:        int i, ch, deleting = 0, ret = 0;
1.12      markus    289:
1.28      stevesk   290:        SSLeay_add_all_algorithms();
1.19      markus    291:
1.12      markus    292:        /* At first, get a connection to the authentication agent. */
                    293:        ac = ssh_get_authentication_connection();
                    294:        if (ac == NULL) {
                    295:                fprintf(stderr, "Could not open a connection to your authentication agent.\n");
1.50      markus    296:                exit(2);
1.12      markus    297:        }
1.54    ! markus    298:        while ((ch = getopt(argc, argv, "lLdDxXe:s:")) != -1) {
1.43      markus    299:                switch (ch) {
                    300:                case 'l':
                    301:                case 'L':
1.50      markus    302:                        if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
1.54    ! markus    303:                                ret = 1;
        !           304:                        goto done;
        !           305:                        break;
        !           306:                case 'x':
        !           307:                case 'X':
        !           308:                        if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
1.50      markus    309:                                ret = 1;
1.43      markus    310:                        goto done;
                    311:                        break;
                    312:                case 'd':
1.12      markus    313:                        deleting = 1;
1.43      markus    314:                        break;
                    315:                case 'D':
1.46      djm       316:                        if (delete_all(ac) == -1)
                    317:                                ret = 1;
1.43      markus    318:                        goto done;
                    319:                        break;
                    320:                case 's':
1.44      markus    321:                        sc_reader_id = optarg;
1.43      markus    322:                        break;
                    323:                case 'e':
1.47      deraadt   324:                        deleting = 1;
1.44      markus    325:                        sc_reader_id = optarg;
1.43      markus    326:                        break;
                    327:                default:
                    328:                        usage();
1.46      djm       329:                        ret = 1;
                    330:                        goto done;
1.42      markus    331:                }
                    332:        }
1.43      markus    333:        argc -= optind;
                    334:        argv += optind;
1.44      markus    335:        if (sc_reader_id != NULL) {
1.46      djm       336:                if (update_card(ac, !deleting, sc_reader_id) == -1)
                    337:                        ret = 1;
1.43      markus    338:                goto done;
1.12      markus    339:        }
1.43      markus    340:        if (argc == 0) {
1.48      djm       341:                char buf[MAXPATHLEN];
                    342:                struct passwd *pw;
1.52      markus    343:                struct stat st;
                    344:                int count = 0;
1.48      djm       345:
                    346:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   347:                        fprintf(stderr, "No user found with uid %u\n",
                    348:                            (u_int)getuid());
1.46      djm       349:                        ret = 1;
                    350:                        goto done;
1.12      markus    351:                }
1.48      djm       352:
                    353:                for(i = 0; default_files[i]; i++) {
1.51      markus    354:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       355:                            default_files[i]);
1.52      markus    356:                        if (stat(buf, &st) < 0)
                    357:                                continue;
1.48      djm       358:                        if (do_file(ac, deleting, buf) == -1)
1.46      djm       359:                                ret = 1;
1.52      markus    360:                        else
                    361:                                count++;
1.46      djm       362:                }
1.52      markus    363:                if (count == 0)
                    364:                        ret = 1;
1.43      markus    365:        } else {
1.48      djm       366:                for(i = 0; i < argc; i++) {
1.49      deraadt   367:                        if (do_file(ac, deleting, argv[i]) == -1)
1.48      djm       368:                                ret = 1;
1.43      markus    369:                }
1.12      markus    370:        }
1.33      markus    371:        clear_pass();
1.43      markus    372:
                    373: done:
1.12      markus    374:        ssh_close_authentication_connection(ac);
1.46      djm       375:        return ret;
1.1       deraadt   376: }