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

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