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

1.119   ! halex       1: /* $OpenBSD: ssh-add.c,v 1.118 2015/01/28 22:36:00 djm 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:
1.75      stevesk    38: #include <sys/types.h>
                     39: #include <sys/stat.h>
1.16      markus     40:
1.19      markus     41: #include <openssl/evp.h>
1.81      stevesk    42:
1.116     djm        43: #include <errno.h>
1.82      stevesk    44: #include <fcntl.h>
1.81      stevesk    45: #include <pwd.h>
1.88      stevesk    46: #include <stdio.h>
1.87      stevesk    47: #include <stdlib.h>
1.85      stevesk    48: #include <string.h>
1.84      stevesk    49: #include <unistd.h>
1.117     deraadt    50: #include <limits.h>
1.1       deraadt    51:
1.89      deraadt    52: #include "xmalloc.h"
1.27      markus     53: #include "ssh.h"
1.1       deraadt    54: #include "rsa.h"
1.27      markus     55: #include "log.h"
1.116     djm        56: #include "sshkey.h"
                     57: #include "sshbuf.h"
1.18      markus     58: #include "authfd.h"
1.16      markus     59: #include "authfile.h"
1.25      markus     60: #include "pathnames.h"
1.57      stevesk    61: #include "misc.h"
1.110     djm        62: #include "ssherr.h"
1.115     djm        63: #include "digest.h"
1.1       deraadt    64:
1.45      jakob      65: /* argv0 */
                     66: extern char *__progname;
                     67:
1.48      djm        68: /* Default files to add */
                     69: static char *default_files[] = {
                     70:        _PATH_SSH_CLIENT_ID_RSA,
                     71:        _PATH_SSH_CLIENT_ID_DSA,
1.99      djm        72:        _PATH_SSH_CLIENT_ID_ECDSA,
1.107     pascal     73:        _PATH_SSH_CLIENT_ID_ED25519,
1.51      markus     74:        _PATH_SSH_CLIENT_IDENTITY,
1.48      djm        75:        NULL
                     76: };
                     77:
1.115     djm        78: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
                     79:
1.56      markus     80: /* Default lifetime (0 == forever) */
1.57      stevesk    81: static int lifetime = 0;
1.48      djm        82:
1.65      markus     83: /* User has to confirm key use */
                     84: static int confirm = 0;
                     85:
1.33      markus     86: /* we keep a cache of one passphrases */
                     87: static char *pass = NULL;
1.39      itojun     88: static void
1.33      markus     89: clear_pass(void)
                     90: {
                     91:        if (pass) {
1.109     djm        92:                explicit_bzero(pass, strlen(pass));
1.106     djm        93:                free(pass);
1.33      markus     94:                pass = NULL;
                     95:        }
                     96: }
                     97:
1.46      djm        98: static int
1.116     djm        99: delete_file(int agent_fd, const char *filename, int key_only)
1.1       deraadt   100: {
1.116     djm       101:        struct sshkey *public, *cert = NULL;
1.104     djm       102:        char *certpath = NULL, *comment = NULL;
1.116     djm       103:        int r, ret = -1;
1.1       deraadt   104:
1.116     djm       105:        if ((r = sshkey_load_public(filename, &public,  &comment)) != 0) {
                    106:                printf("Bad key file %s: %s\n", filename, ssh_err(r));
1.46      djm       107:                return -1;
1.12      markus    108:        }
1.116     djm       109:        if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
1.12      markus    110:                fprintf(stderr, "Identity removed: %s (%s)\n", filename, comment);
1.46      djm       111:                ret = 0;
                    112:        } else
1.116     djm       113:                fprintf(stderr, "Could not remove identity \"%s\": %s\n",
                    114:                    filename, ssh_err(r));
1.46      djm       115:
1.104     djm       116:        if (key_only)
                    117:                goto out;
                    118:
                    119:        /* Now try to delete the corresponding certificate too */
                    120:        free(comment);
1.105     markus    121:        comment = NULL;
1.104     djm       122:        xasprintf(&certpath, "%s-cert.pub", filename);
1.116     djm       123:        if ((r = sshkey_load_public(certpath, &cert, &comment)) == 0)
1.104     djm       124:                goto out;
1.116     djm       125:        if (!sshkey_equal_public(cert, public))
1.104     djm       126:                fatal("Certificate %s does not match private key %s",
                    127:                    certpath, filename);
                    128:
1.116     djm       129:        if (ssh_remove_identity(agent_fd, cert)) {
1.104     djm       130:                fprintf(stderr, "Identity removed: %s (%s)\n", certpath,
                    131:                    comment);
                    132:                ret = 0;
                    133:        } else
                    134:                fprintf(stderr, "Could not remove identity: %s\n", certpath);
                    135:
                    136:  out:
                    137:        if (cert != NULL)
1.116     djm       138:                sshkey_free(cert);
1.104     djm       139:        if (public != NULL)
1.116     djm       140:                sshkey_free(public);
1.104     djm       141:        free(certpath);
                    142:        free(comment);
1.47      deraadt   143:
1.46      djm       144:        return ret;
1.1       deraadt   145: }
                    146:
1.19      markus    147: /* Send a request to remove all identities. */
1.46      djm       148: static int
1.116     djm       149: delete_all(int agent_fd)
1.1       deraadt   150: {
1.46      djm       151:        int ret = -1;
1.19      markus    152:
1.116     djm       153:        if (ssh_remove_all_identities(agent_fd, 1) == 0)
1.46      djm       154:                ret = 0;
1.19      markus    155:        /* ignore error-code for ssh2 */
1.116     djm       156:        /* XXX revisit */
                    157:        ssh_remove_all_identities(agent_fd, 2);
1.19      markus    158:
1.46      djm       159:        if (ret == 0)
1.12      markus    160:                fprintf(stderr, "All identities removed.\n");
                    161:        else
1.24      markus    162:                fprintf(stderr, "Failed to remove all identities.\n");
1.46      djm       163:
                    164:        return ret;
1.1       deraadt   165: }
                    166:
1.46      djm       167: static int
1.116     djm       168: add_file(int agent_fd, const char *filename, int key_only)
1.1       deraadt   169: {
1.116     djm       170:        struct sshkey *private, *cert;
1.36      markus    171:        char *comment = NULL;
1.102     djm       172:        char msg[1024], *certpath = NULL;
1.116     djm       173:        int r, fd, ret = -1;
                    174:        struct sshbuf *keyblob;
1.12      markus    175:
1.101     djm       176:        if (strcmp(filename, "-") == 0) {
                    177:                fd = STDIN_FILENO;
                    178:                filename = "(stdin)";
                    179:        } else if ((fd = open(filename, O_RDONLY)) < 0) {
1.19      markus    180:                perror(filename);
1.46      djm       181:                return -1;
1.19      markus    182:        }
1.76      dtucker   183:
                    184:        /*
                    185:         * Since we'll try to load a keyfile multiple times, permission errors
                    186:         * will occur multiple times, so check perms first and bail if wrong.
                    187:         */
1.101     djm       188:        if (fd != STDIN_FILENO) {
1.116     djm       189:                if (sshkey_perm_ok(fd, filename) != 0) {
1.101     djm       190:                        close(fd);
                    191:                        return -1;
                    192:                }
                    193:        }
1.116     djm       194:        if ((keyblob = sshbuf_new()) == NULL)
                    195:                fatal("%s: sshbuf_new failed", __func__);
                    196:        if ((r = sshkey_load_file(fd, keyblob)) != 0) {
                    197:                fprintf(stderr, "Error loading key \"%s\": %s\n",
                    198:                    filename, ssh_err(r));
                    199:                sshbuf_free(keyblob);
1.101     djm       200:                close(fd);
                    201:                return -1;
                    202:        }
1.76      dtucker   203:        close(fd);
                    204:
1.12      markus    205:        /* At first, try empty passphrase */
1.116     djm       206:        if ((r = sshkey_parse_private_fileblob(keyblob, "", filename,
                    207:            &private, &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
                    208:                fprintf(stderr, "Error loading key \"%s\": %s\n",
                    209:                    filename, ssh_err(r));
                    210:                goto fail_load;
                    211:        }
1.33      markus    212:        /* try last */
1.110     djm       213:        if (private == NULL && pass != NULL) {
1.116     djm       214:                if ((r = sshkey_parse_private_fileblob(keyblob, pass, filename,
1.110     djm       215:                    &private, &comment)) != 0 &&
1.116     djm       216:                    r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
                    217:                        fprintf(stderr, "Error loading key \"%s\": %s\n",
                    218:                            filename, ssh_err(r));
                    219:                        goto fail_load;
                    220:                }
1.110     djm       221:        }
1.113     benno     222:        if (comment == NULL)
                    223:                comment = xstrdup(filename);
1.31      markus    224:        if (private == NULL) {
1.33      markus    225:                /* clear passphrase since it did not work */
                    226:                clear_pass();
1.119   ! halex     227:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s%s: ",
        !           228:                    comment, confirm ? " (will confirm each use)" : "");
1.12      markus    229:                for (;;) {
1.40      markus    230:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.116     djm       231:                        if (strcmp(pass, "") == 0)
                    232:                                goto fail_load;
                    233:                        if ((r = sshkey_parse_private_fileblob(keyblob, pass,
                    234:                            filename, &private, NULL)) == 0)
                    235:                                break;
                    236:                        else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
                    237:                                fprintf(stderr,
                    238:                                    "Error loading key \"%s\": %s\n",
                    239:                                    filename, ssh_err(r));
                    240:  fail_load:
1.35      markus    241:                                clear_pass();
1.106     djm       242:                                free(comment);
1.116     djm       243:                                sshbuf_free(keyblob);
1.46      djm       244:                                return -1;
1.12      markus    245:                        }
1.33      markus    246:                        clear_pass();
1.68      markus    247:                        snprintf(msg, sizeof msg,
1.119   ! halex     248:                            "Bad passphrase, try again for %.200s%s: ", comment,
        !           249:                            confirm ? " (will confirm each use)" : "");
1.12      markus    250:                }
                    251:        }
1.116     djm       252:        sshbuf_free(keyblob);
1.60      markus    253:
1.116     djm       254:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
                    255:            lifetime, confirm)) == 0) {
1.60      markus    256:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                    257:                ret = 0;
                    258:                if (lifetime != 0)
1.64      deraadt   259:                        fprintf(stderr,
1.60      markus    260:                            "Lifetime set to %d seconds\n", lifetime);
1.69      djm       261:                if (confirm != 0)
1.65      markus    262:                        fprintf(stderr,
1.96      djm       263:                            "The user must confirm each use of the key\n");
1.60      markus    264:        } else {
1.116     djm       265:                fprintf(stderr, "Could not add identity \"%s\": %s\n",
                    266:                    filename, ssh_err(r));
1.56      markus    267:        }
                    268:
1.102     djm       269:        /* Skip trying to load the cert if requested */
                    270:        if (key_only)
                    271:                goto out;
1.93      djm       272:
                    273:        /* Now try to add the certificate flavour too */
                    274:        xasprintf(&certpath, "%s-cert.pub", filename);
1.116     djm       275:        if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
                    276:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    277:                        error("Failed to load certificate \"%s\": %s",
                    278:                            certpath, ssh_err(r));
1.96      djm       279:                goto out;
1.116     djm       280:        }
1.96      djm       281:
1.116     djm       282:        if (!sshkey_equal_public(cert, private)) {
1.96      djm       283:                error("Certificate %s does not match private key %s",
                    284:                    certpath, filename);
1.116     djm       285:                sshkey_free(cert);
1.96      djm       286:                goto out;
                    287:        }
1.93      djm       288:
1.96      djm       289:        /* Graft with private bits */
1.116     djm       290:        if ((r = sshkey_to_certified(private,
                    291:            sshkey_cert_is_legacy(cert))) != 0) {
                    292:                error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
                    293:                sshkey_free(cert);
                    294:                goto out;
                    295:        }
                    296:        if ((r = sshkey_cert_copy(cert, private)) != 0) {
                    297:                error("%s: key_cert_copy: %s", __func__, ssh_err(r));
                    298:                sshkey_free(cert);
1.96      djm       299:                goto out;
1.94      otto      300:        }
1.116     djm       301:        sshkey_free(cert);
1.93      djm       302:
1.116     djm       303:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
                    304:            lifetime, confirm)) != 0) {
                    305:                error("Certificate %s (%s) add failed: %s", certpath,
                    306:                    private->cert->key_id, ssh_err(r));
                    307:                goto out;
1.96      djm       308:        }
                    309:        fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
                    310:            private->cert->key_id);
                    311:        if (lifetime != 0)
                    312:                fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
                    313:        if (confirm != 0)
                    314:                fprintf(stderr, "The user must confirm each use of the key\n");
                    315:  out:
1.116     djm       316:        free(certpath);
1.106     djm       317:        free(comment);
1.116     djm       318:        sshkey_free(private);
1.47      deraadt   319:
1.46      djm       320:        return ret;
1.1       deraadt   321: }
                    322:
1.46      djm       323: static int
1.116     djm       324: update_card(int agent_fd, int add, const char *id)
1.42      markus    325: {
1.108     djm       326:        char *pin = NULL;
1.116     djm       327:        int r, ret = -1;
1.53      rees      328:
1.108     djm       329:        if (add) {
                    330:                if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
                    331:                    RP_ALLOW_STDIN)) == NULL)
                    332:                        return -1;
                    333:        }
1.53      rees      334:
1.116     djm       335:        if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
                    336:            lifetime, confirm)) == 0) {
1.44      markus    337:                fprintf(stderr, "Card %s: %s\n",
1.47      deraadt   338:                    add ? "added" : "removed", id);
1.66      markus    339:                ret = 0;
1.46      djm       340:        } else {
1.116     djm       341:                fprintf(stderr, "Could not %s card \"%s\": %s\n",
                    342:                    add ? "add" : "remove", id, ssh_err(r));
1.66      markus    343:                ret = -1;
1.46      djm       344:        }
1.106     djm       345:        free(pin);
1.66      markus    346:        return ret;
1.42      markus    347: }
                    348:
1.50      markus    349: static int
1.116     djm       350: list_identities(int agent_fd, int do_fp)
1.1       deraadt   351: {
1.116     djm       352:        char *fp;
                    353:        int version, r, had_identities = 0;
                    354:        struct ssh_identitylist *idlist;
                    355:        size_t i;
1.12      markus    356:
1.19      markus    357:        for (version = 1; version <= 2; version++) {
1.116     djm       358:                if ((r = ssh_fetch_identitylist(agent_fd, version,
                    359:                    &idlist)) != 0) {
                    360:                        if (r != SSH_ERR_AGENT_NO_IDENTITIES)
                    361:                                fprintf(stderr, "error fetching identities for "
                    362:                                    "protocol %d: %s\n", version, ssh_err(r));
                    363:                        continue;
                    364:                }
                    365:                for (i = 0; i < idlist->nkeys; i++) {
1.19      markus    366:                        had_identities = 1;
1.30      markus    367:                        if (do_fp) {
1.116     djm       368:                                fp = sshkey_fingerprint(idlist->keys[i],
                    369:                                    fingerprint_hash, SSH_FP_DEFAULT);
1.23      markus    370:                                printf("%d %s %s (%s)\n",
1.118     djm       371:                                    sshkey_size(idlist->keys[i]),
                    372:                                    fp == NULL ? "(null)" : fp,
1.116     djm       373:                                    idlist->comments[i],
                    374:                                    sshkey_type(idlist->keys[i]));
1.106     djm       375:                                free(fp);
1.12      markus    376:                        } else {
1.116     djm       377:                                if ((r = sshkey_write(idlist->keys[i],
                    378:                                    stdout)) != 0) {
                    379:                                        fprintf(stderr, "sshkey_write: %s\n",
                    380:                                            ssh_err(r));
                    381:                                        continue;
                    382:                                }
                    383:                                fprintf(stdout, " %s\n", idlist->comments[i]);
1.12      markus    384:                        }
                    385:                }
1.116     djm       386:                ssh_free_identitylist(idlist);
1.12      markus    387:        }
1.50      markus    388:        if (!had_identities) {
1.12      markus    389:                printf("The agent has no identities.\n");
1.50      markus    390:                return -1;
                    391:        }
                    392:        return 0;
1.1       deraadt   393: }
                    394:
1.48      djm       395: static int
1.116     djm       396: lock_agent(int agent_fd, int lock)
1.54      markus    397: {
                    398:        char prompt[100], *p1, *p2;
1.116     djm       399:        int r, passok = 1, ret = -1;
1.61      deraadt   400:
1.54      markus    401:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    402:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    403:        if (lock) {
                    404:                strlcpy(prompt, "Again: ", sizeof prompt);
                    405:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    406:                if (strcmp(p1, p2) != 0) {
                    407:                        fprintf(stderr, "Passwords do not match.\n");
                    408:                        passok = 0;
                    409:                }
1.109     djm       410:                explicit_bzero(p2, strlen(p2));
1.106     djm       411:                free(p2);
1.54      markus    412:        }
1.116     djm       413:        if (passok) {
                    414:                if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
                    415:                        fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    416:                        ret = 0;
                    417:                } else {
                    418:                        fprintf(stderr, "Failed to %slock agent: %s\n",
                    419:                            lock ? "" : "un", ssh_err(r));
                    420:                }
                    421:        }
1.109     djm       422:        explicit_bzero(p1, strlen(p1));
1.106     djm       423:        free(p1);
1.62      markus    424:        return (ret);
1.54      markus    425: }
                    426:
                    427: static int
1.116     djm       428: do_file(int agent_fd, int deleting, int key_only, char *file)
1.48      djm       429: {
                    430:        if (deleting) {
1.116     djm       431:                if (delete_file(agent_fd, file, key_only) == -1)
1.48      djm       432:                        return -1;
                    433:        } else {
1.116     djm       434:                if (add_file(agent_fd, file, key_only) == -1)
1.48      djm       435:                        return -1;
                    436:        }
                    437:        return 0;
                    438: }
                    439:
1.42      markus    440: static void
                    441: usage(void)
                    442: {
1.90      sobrado   443:        fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45      jakob     444:        fprintf(stderr, "Options:\n");
                    445:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
1.115     djm       446:        fprintf(stderr, "  -E hash     Specify hash algorithm used for fingerprints.\n");
1.45      jakob     447:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
1.103     djm       448:        fprintf(stderr, "  -k          Load only keys and not certificates.\n");
                    449:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
                    450:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.45      jakob     451:        fprintf(stderr, "  -d          Delete identity.\n");
                    452:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    453:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    454:        fprintf(stderr, "  -X          Unlock agent.\n");
1.92      markus    455:        fprintf(stderr, "  -s pkcs11   Add keys from PKCS#11 provider.\n");
                    456:        fprintf(stderr, "  -e pkcs11   Remove keys provided by PKCS#11 provider.\n");
1.42      markus    457: }
                    458:
1.2       provos    459: int
1.7       markus    460: main(int argc, char **argv)
1.1       deraadt   461: {
1.43      markus    462:        extern char *optarg;
                    463:        extern int optind;
1.116     djm       464:        int agent_fd;
1.92      markus    465:        char *pkcs11provider = NULL;
1.116     djm       466:        int r, i, ch, deleting = 0, ret = 0, key_only = 0;
1.115     djm       467:        int xflag = 0, lflag = 0, Dflag = 0;
1.73      djm       468:
                    469:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    470:        sanitise_stdfd();
1.12      markus    471:
1.100     djm       472:        OpenSSL_add_all_algorithms();
1.112     djm       473:
1.114     millert   474:        setvbuf(stdout, NULL, _IOLBF, 0);
1.19      markus    475:
1.116     djm       476:        /* First, get a connection to the authentication agent. */
                    477:        switch (r = ssh_get_authentication_socket(&agent_fd)) {
                    478:        case 0:
                    479:                break;
                    480:        case SSH_ERR_AGENT_NOT_PRESENT:
                    481:                fprintf(stderr, "Could not open a connection to your "
                    482:                    "authentication agent.\n");
                    483:                exit(2);
                    484:        default:
                    485:                fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50      markus    486:                exit(2);
1.12      markus    487:        }
1.116     djm       488:
1.115     djm       489:        while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
1.43      markus    490:                switch (ch) {
1.115     djm       491:                case 'E':
                    492:                        fingerprint_hash = ssh_digest_alg_by_name(optarg);
                    493:                        if (fingerprint_hash == -1)
                    494:                                fatal("Invalid hash algorithm \"%s\"", optarg);
                    495:                        break;
1.102     djm       496:                case 'k':
                    497:                        key_only = 1;
                    498:                        break;
1.43      markus    499:                case 'l':
                    500:                case 'L':
1.115     djm       501:                        if (lflag != 0)
                    502:                                fatal("-%c flag already specified", lflag);
                    503:                        lflag = ch;
                    504:                        break;
1.54      markus    505:                case 'x':
                    506:                case 'X':
1.115     djm       507:                        if (xflag != 0)
                    508:                                fatal("-%c flag already specified", xflag);
                    509:                        xflag = ch;
                    510:                        break;
1.65      markus    511:                case 'c':
                    512:                        confirm = 1;
1.43      markus    513:                        break;
                    514:                case 'd':
1.12      markus    515:                        deleting = 1;
1.43      markus    516:                        break;
                    517:                case 'D':
1.115     djm       518:                        Dflag = 1;
                    519:                        break;
1.43      markus    520:                case 's':
1.92      markus    521:                        pkcs11provider = optarg;
1.43      markus    522:                        break;
                    523:                case 'e':
1.47      deraadt   524:                        deleting = 1;
1.92      markus    525:                        pkcs11provider = optarg;
1.56      markus    526:                        break;
                    527:                case 't':
1.57      stevesk   528:                        if ((lifetime = convtime(optarg)) == -1) {
                    529:                                fprintf(stderr, "Invalid lifetime\n");
                    530:                                ret = 1;
                    531:                                goto done;
                    532:                        }
1.43      markus    533:                        break;
                    534:                default:
                    535:                        usage();
1.46      djm       536:                        ret = 1;
                    537:                        goto done;
1.42      markus    538:                }
                    539:        }
1.115     djm       540:
                    541:        if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
                    542:                fatal("Invalid combination of actions");
                    543:        else if (xflag) {
1.116     djm       544:                if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115     djm       545:                        ret = 1;
                    546:                goto done;
                    547:        } else if (lflag) {
1.116     djm       548:                if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115     djm       549:                        ret = 1;
                    550:                goto done;
                    551:        } else if (Dflag) {
1.116     djm       552:                if (delete_all(agent_fd) == -1)
1.115     djm       553:                        ret = 1;
                    554:                goto done;
                    555:        }
                    556:
1.43      markus    557:        argc -= optind;
                    558:        argv += optind;
1.92      markus    559:        if (pkcs11provider != NULL) {
1.116     djm       560:                if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
1.46      djm       561:                        ret = 1;
1.43      markus    562:                goto done;
1.12      markus    563:        }
1.43      markus    564:        if (argc == 0) {
1.117     deraadt   565:                char buf[PATH_MAX];
1.48      djm       566:                struct passwd *pw;
1.52      markus    567:                struct stat st;
                    568:                int count = 0;
1.48      djm       569:
                    570:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   571:                        fprintf(stderr, "No user found with uid %u\n",
                    572:                            (u_int)getuid());
1.46      djm       573:                        ret = 1;
                    574:                        goto done;
1.12      markus    575:                }
1.48      djm       576:
1.71      deraadt   577:                for (i = 0; default_files[i]; i++) {
1.51      markus    578:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       579:                            default_files[i]);
1.52      markus    580:                        if (stat(buf, &st) < 0)
                    581:                                continue;
1.116     djm       582:                        if (do_file(agent_fd, deleting, key_only, buf) == -1)
1.46      djm       583:                                ret = 1;
1.52      markus    584:                        else
                    585:                                count++;
1.46      djm       586:                }
1.52      markus    587:                if (count == 0)
                    588:                        ret = 1;
1.43      markus    589:        } else {
1.71      deraadt   590:                for (i = 0; i < argc; i++) {
1.116     djm       591:                        if (do_file(agent_fd, deleting, key_only,
                    592:                            argv[i]) == -1)
1.48      djm       593:                                ret = 1;
1.43      markus    594:                }
1.12      markus    595:        }
1.33      markus    596:        clear_pass();
1.43      markus    597:
                    598: done:
1.116     djm       599:        ssh_close_authentication_socket(agent_fd);
1.46      djm       600:        return ret;
1.1       deraadt   601: }