[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.81

1.81    ! stevesk     1: /* $OpenBSD: ssh-add.c,v 1.80 2006/05/30 11:46:38 mk Exp $ */
1.1       deraadt     2: /*
1.13      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Adds an identity to the authentication server, or removes an identity.
1.19      markus      7:  *
1.22      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
1.19      markus     14:  * SSH2 implementation,
1.41      markus     15:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.22      deraadt    16:  *
                     17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13      deraadt    36:  */
1.1       deraadt    37:
                     38: #include "includes.h"
1.75      stevesk    39:
                     40: #include <sys/types.h>
                     41: #include <sys/stat.h>
1.16      markus     42:
1.19      markus     43: #include <openssl/evp.h>
1.81    ! stevesk    44:
        !            45: #include <pwd.h>
1.1       deraadt    46:
1.27      markus     47: #include "ssh.h"
1.1       deraadt    48: #include "rsa.h"
1.27      markus     49: #include "log.h"
1.1       deraadt    50: #include "xmalloc.h"
1.16      markus     51: #include "key.h"
1.18      markus     52: #include "authfd.h"
1.16      markus     53: #include "authfile.h"
1.25      markus     54: #include "pathnames.h"
1.57      stevesk    55: #include "misc.h"
1.1       deraadt    56:
1.45      jakob      57: /* argv0 */
                     58: extern char *__progname;
                     59:
1.48      djm        60: /* Default files to add */
                     61: static char *default_files[] = {
                     62:        _PATH_SSH_CLIENT_ID_RSA,
                     63:        _PATH_SSH_CLIENT_ID_DSA,
1.51      markus     64:        _PATH_SSH_CLIENT_IDENTITY,
1.48      djm        65:        NULL
                     66: };
                     67:
1.56      markus     68: /* Default lifetime (0 == forever) */
1.57      stevesk    69: static int lifetime = 0;
1.48      djm        70:
1.65      markus     71: /* User has to confirm key use */
                     72: static int confirm = 0;
                     73:
1.33      markus     74: /* we keep a cache of one passphrases */
                     75: static char *pass = NULL;
1.39      itojun     76: static void
1.33      markus     77: clear_pass(void)
                     78: {
                     79:        if (pass) {
                     80:                memset(pass, 0, strlen(pass));
                     81:                xfree(pass);
                     82:                pass = NULL;
                     83:        }
                     84: }
                     85:
1.46      djm        86: static int
1.7       markus     87: delete_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt    88: {
1.16      markus     89:        Key *public;
1.32      markus     90:        char *comment = NULL;
1.46      djm        91:        int ret = -1;
1.1       deraadt    92:
1.31      markus     93:        public = key_load_public(filename, &comment);
                     94:        if (public == NULL) {
                     95:                printf("Bad key file %s\n", filename);
1.46      djm        96:                return -1;
1.12      markus     97:        }
1.46      djm        98:        if (ssh_remove_identity(ac, public)) {
1.12      markus     99:                fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46      djm       100:                ret = 0;
                    101:        } else
1.12      markus    102:                fprintf(stderr, "Could not remove identity: %s\n", filename);
1.46      djm       103:
1.16      markus    104:        key_free(public);
1.12      markus    105:        xfree(comment);
1.47      deraadt   106:
1.46      djm       107:        return ret;
1.1       deraadt   108: }
                    109:
1.19      markus    110: /* Send a request to remove all identities. */
1.46      djm       111: static int
1.7       markus    112: delete_all(AuthenticationConnection *ac)
1.1       deraadt   113: {
1.46      djm       114:        int ret = -1;
1.19      markus    115:
1.46      djm       116:        if (ssh_remove_all_identities(ac, 1))
                    117:                ret = 0;
1.19      markus    118:        /* ignore error-code for ssh2 */
                    119:        ssh_remove_all_identities(ac, 2);
                    120:
1.46      djm       121:        if (ret == 0)
1.12      markus    122:                fprintf(stderr, "All identities removed.\n");
                    123:        else
1.24      markus    124:                fprintf(stderr, "Failed to remove all identities.\n");
1.46      djm       125:
                    126:        return ret;
1.1       deraadt   127: }
                    128:
1.46      djm       129: static int
1.7       markus    130: add_file(AuthenticationConnection *ac, const char *filename)
1.1       deraadt   131: {
1.16      markus    132:        Key *private;
1.36      markus    133:        char *comment = NULL;
                    134:        char msg[1024];
1.76      dtucker   135:        int fd, perms_ok, ret = -1;
1.12      markus    136:
1.76      dtucker   137:        if ((fd = open(filename, 0)) < 0) {
1.19      markus    138:                perror(filename);
1.46      djm       139:                return -1;
1.19      markus    140:        }
1.76      dtucker   141:
                    142:        /*
                    143:         * Since we'll try to load a keyfile multiple times, permission errors
                    144:         * will occur multiple times, so check perms first and bail if wrong.
                    145:         */
                    146:        perms_ok = key_perm_ok(fd, filename);
                    147:        close(fd);
                    148:        if (!perms_ok)
                    149:                return -1;
                    150:
1.12      markus    151:        /* At first, try empty passphrase */
1.31      markus    152:        private = key_load_private(filename, "", &comment);
                    153:        if (comment == NULL)
                    154:                comment = xstrdup(filename);
1.33      markus    155:        /* try last */
                    156:        if (private == NULL && pass != NULL)
                    157:                private = key_load_private(filename, pass, NULL);
1.31      markus    158:        if (private == NULL) {
1.33      markus    159:                /* clear passphrase since it did not work */
                    160:                clear_pass();
1.37      markus    161:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.72      djm       162:                    comment);
1.12      markus    163:                for (;;) {
1.40      markus    164:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.12      markus    165:                        if (strcmp(pass, "") == 0) {
1.35      markus    166:                                clear_pass();
1.31      markus    167:                                xfree(comment);
1.46      djm       168:                                return -1;
1.12      markus    169:                        }
1.31      markus    170:                        private = key_load_private(filename, pass, &comment);
                    171:                        if (private != NULL)
1.12      markus    172:                                break;
1.33      markus    173:                        clear_pass();
1.68      markus    174:                        snprintf(msg, sizeof msg,
                    175:                            "Bad passphrase, try again for %.200s: ", comment);
1.12      markus    176:                }
                    177:        }
1.60      markus    178:
1.69      djm       179:        if (ssh_add_identity_constrained(ac, private, comment, lifetime,
                    180:            confirm)) {
1.60      markus    181:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                    182:                ret = 0;
                    183:                if (lifetime != 0)
1.64      deraadt   184:                        fprintf(stderr,
1.60      markus    185:                            "Lifetime set to %d seconds\n", lifetime);
1.69      djm       186:                if (confirm != 0)
1.65      markus    187:                        fprintf(stderr,
                    188:                            "The user has to confirm each use of the key\n");
1.60      markus    189:        } else if (ssh_add_identity(ac, private, comment)) {
1.31      markus    190:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
1.46      djm       191:                ret = 0;
1.60      markus    192:        } else {
1.12      markus    193:                fprintf(stderr, "Could not add identity: %s\n", filename);
1.56      markus    194:        }
                    195:
1.31      markus    196:        xfree(comment);
1.16      markus    197:        key_free(private);
1.47      deraadt   198:
1.46      djm       199:        return ret;
1.1       deraadt   200: }
                    201:
1.46      djm       202: static int
1.44      markus    203: update_card(AuthenticationConnection *ac, int add, const char *id)
1.42      markus    204: {
1.53      rees      205:        char *pin;
1.66      markus    206:        int ret = -1;
1.53      rees      207:
                    208:        pin = read_passphrase("Enter passphrase for smartcard: ", RP_ALLOW_STDIN);
                    209:        if (pin == NULL)
                    210:                return -1;
                    211:
1.67      djm       212:        if (ssh_update_card(ac, add, id, pin, lifetime, confirm)) {
1.44      markus    213:                fprintf(stderr, "Card %s: %s\n",
1.47      deraadt   214:                    add ? "added" : "removed", id);
1.66      markus    215:                ret = 0;
1.46      djm       216:        } else {
1.44      markus    217:                fprintf(stderr, "Could not %s card: %s\n",
1.47      deraadt   218:                    add ? "add" : "remove", id);
1.66      markus    219:                ret = -1;
1.46      djm       220:        }
1.66      markus    221:        xfree(pin);
                    222:        return ret;
1.42      markus    223: }
                    224:
1.50      markus    225: static int
1.30      markus    226: list_identities(AuthenticationConnection *ac, int do_fp)
1.1       deraadt   227: {
1.19      markus    228:        Key *key;
1.30      markus    229:        char *comment, *fp;
1.19      markus    230:        int had_identities = 0;
                    231:        int version;
1.12      markus    232:
1.19      markus    233:        for (version = 1; version <= 2; version++) {
                    234:                for (key = ssh_get_first_identity(ac, &comment, version);
1.47      deraadt   235:                    key != NULL;
                    236:                    key = ssh_get_next_identity(ac, &comment, version)) {
1.19      markus    237:                        had_identities = 1;
1.30      markus    238:                        if (do_fp) {
                    239:                                fp = key_fingerprint(key, SSH_FP_MD5,
                    240:                                    SSH_FP_HEX);
1.23      markus    241:                                printf("%d %s %s (%s)\n",
1.30      markus    242:                                    key_size(key), fp, comment, key_type(key));
                    243:                                xfree(fp);
1.12      markus    244:                        } else {
1.19      markus    245:                                if (!key_write(key, stdout))
                    246:                                        fprintf(stderr, "key_write failed");
                    247:                                fprintf(stdout, " %s\n", comment);
1.12      markus    248:                        }
1.19      markus    249:                        key_free(key);
                    250:                        xfree(comment);
1.12      markus    251:                }
                    252:        }
1.50      markus    253:        if (!had_identities) {
1.12      markus    254:                printf("The agent has no identities.\n");
1.50      markus    255:                return -1;
                    256:        }
                    257:        return 0;
1.1       deraadt   258: }
                    259:
1.48      djm       260: static int
1.54      markus    261: lock_agent(AuthenticationConnection *ac, int lock)
                    262: {
                    263:        char prompt[100], *p1, *p2;
                    264:        int passok = 1, ret = -1;
1.61      deraadt   265:
1.54      markus    266:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    267:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    268:        if (lock) {
                    269:                strlcpy(prompt, "Again: ", sizeof prompt);
                    270:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    271:                if (strcmp(p1, p2) != 0) {
                    272:                        fprintf(stderr, "Passwords do not match.\n");
                    273:                        passok = 0;
                    274:                }
                    275:                memset(p2, 0, strlen(p2));
                    276:                xfree(p2);
                    277:        }
                    278:        if (passok && ssh_lock_agent(ac, lock, p1)) {
                    279:                fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    280:                ret = 0;
                    281:        } else
                    282:                fprintf(stderr, "Failed to %slock agent.\n", lock ? "" : "un");
                    283:        memset(p1, 0, strlen(p1));
                    284:        xfree(p1);
1.62      markus    285:        return (ret);
1.54      markus    286: }
                    287:
                    288: static int
1.48      djm       289: do_file(AuthenticationConnection *ac, int deleting, char *file)
                    290: {
                    291:        if (deleting) {
                    292:                if (delete_file(ac, file) == -1)
                    293:                        return -1;
                    294:        } else {
                    295:                if (add_file(ac, file) == -1)
                    296:                        return -1;
                    297:        }
                    298:        return 0;
                    299: }
                    300:
1.42      markus    301: static void
                    302: usage(void)
                    303: {
1.80      mk        304:        fprintf(stderr, "Usage: %s [options] [file ...]\n", __progname);
1.45      jakob     305:        fprintf(stderr, "Options:\n");
                    306:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
                    307:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
                    308:        fprintf(stderr, "  -d          Delete identity.\n");
                    309:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    310:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    311:        fprintf(stderr, "  -X          Unlock agent.\n");
1.56      markus    312:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.65      markus    313:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
1.45      jakob     314: #ifdef SMARTCARD
                    315:        fprintf(stderr, "  -s reader   Add key in smartcard reader.\n");
                    316:        fprintf(stderr, "  -e reader   Remove key in smartcard reader.\n");
                    317: #endif
1.42      markus    318: }
                    319:
1.2       provos    320: int
1.7       markus    321: main(int argc, char **argv)
1.1       deraadt   322: {
1.43      markus    323:        extern char *optarg;
                    324:        extern int optind;
1.12      markus    325:        AuthenticationConnection *ac = NULL;
1.44      markus    326:        char *sc_reader_id = NULL;
1.46      djm       327:        int i, ch, deleting = 0, ret = 0;
1.73      djm       328:
                    329:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    330:        sanitise_stdfd();
1.12      markus    331:
1.28      stevesk   332:        SSLeay_add_all_algorithms();
1.19      markus    333:
1.12      markus    334:        /* At first, get a connection to the authentication agent. */
                    335:        ac = ssh_get_authentication_connection();
                    336:        if (ac == NULL) {
1.74      deraadt   337:                fprintf(stderr,
                    338:                    "Could not open a connection to your authentication agent.\n");
1.50      markus    339:                exit(2);
1.12      markus    340:        }
1.65      markus    341:        while ((ch = getopt(argc, argv, "lLcdDxXe:s:t:")) != -1) {
1.43      markus    342:                switch (ch) {
                    343:                case 'l':
                    344:                case 'L':
1.50      markus    345:                        if (list_identities(ac, ch == 'l' ? 1 : 0) == -1)
1.54      markus    346:                                ret = 1;
                    347:                        goto done;
                    348:                case 'x':
                    349:                case 'X':
                    350:                        if (lock_agent(ac, ch == 'x' ? 1 : 0) == -1)
1.50      markus    351:                                ret = 1;
1.43      markus    352:                        goto done;
1.65      markus    353:                case 'c':
                    354:                        confirm = 1;
1.43      markus    355:                        break;
                    356:                case 'd':
1.12      markus    357:                        deleting = 1;
1.43      markus    358:                        break;
                    359:                case 'D':
1.46      djm       360:                        if (delete_all(ac) == -1)
                    361:                                ret = 1;
1.43      markus    362:                        goto done;
                    363:                case 's':
1.44      markus    364:                        sc_reader_id = optarg;
1.43      markus    365:                        break;
                    366:                case 'e':
1.47      deraadt   367:                        deleting = 1;
1.44      markus    368:                        sc_reader_id = optarg;
1.56      markus    369:                        break;
                    370:                case 't':
1.57      stevesk   371:                        if ((lifetime = convtime(optarg)) == -1) {
                    372:                                fprintf(stderr, "Invalid lifetime\n");
                    373:                                ret = 1;
                    374:                                goto done;
                    375:                        }
1.43      markus    376:                        break;
                    377:                default:
                    378:                        usage();
1.46      djm       379:                        ret = 1;
                    380:                        goto done;
1.42      markus    381:                }
                    382:        }
1.43      markus    383:        argc -= optind;
                    384:        argv += optind;
1.44      markus    385:        if (sc_reader_id != NULL) {
1.46      djm       386:                if (update_card(ac, !deleting, sc_reader_id) == -1)
                    387:                        ret = 1;
1.43      markus    388:                goto done;
1.12      markus    389:        }
1.43      markus    390:        if (argc == 0) {
1.48      djm       391:                char buf[MAXPATHLEN];
                    392:                struct passwd *pw;
1.52      markus    393:                struct stat st;
                    394:                int count = 0;
1.48      djm       395:
                    396:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   397:                        fprintf(stderr, "No user found with uid %u\n",
                    398:                            (u_int)getuid());
1.46      djm       399:                        ret = 1;
                    400:                        goto done;
1.12      markus    401:                }
1.48      djm       402:
1.71      deraadt   403:                for (i = 0; default_files[i]; i++) {
1.51      markus    404:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       405:                            default_files[i]);
1.52      markus    406:                        if (stat(buf, &st) < 0)
                    407:                                continue;
1.48      djm       408:                        if (do_file(ac, deleting, buf) == -1)
1.46      djm       409:                                ret = 1;
1.52      markus    410:                        else
                    411:                                count++;
1.46      djm       412:                }
1.52      markus    413:                if (count == 0)
                    414:                        ret = 1;
1.43      markus    415:        } else {
1.71      deraadt   416:                for (i = 0; i < argc; i++) {
1.49      deraadt   417:                        if (do_file(ac, deleting, argv[i]) == -1)
1.48      djm       418:                                ret = 1;
1.43      markus    419:                }
1.12      markus    420:        }
1.33      markus    421:        clear_pass();
1.43      markus    422:
                    423: done:
1.12      markus    424:        ssh_close_authentication_connection(ac);
1.46      djm       425:        return ret;
1.1       deraadt   426: }