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

1.117   ! deraadt     1: /* $OpenBSD: ssh-add.c,v 1.116 2015/01/14 20:05:27 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.37      markus    227:                snprintf(msg, sizeof msg, "Enter passphrase for %.200s: ",
1.72      djm       228:                    comment);
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,
                    248:                            "Bad passphrase, try again for %.200s: ", comment);
1.12      markus    249:                }
                    250:        }
1.116     djm       251:        sshbuf_free(keyblob);
1.60      markus    252:
1.116     djm       253:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
                    254:            lifetime, confirm)) == 0) {
1.60      markus    255:                fprintf(stderr, "Identity added: %s (%s)\n", filename, comment);
                    256:                ret = 0;
                    257:                if (lifetime != 0)
1.64      deraadt   258:                        fprintf(stderr,
1.60      markus    259:                            "Lifetime set to %d seconds\n", lifetime);
1.69      djm       260:                if (confirm != 0)
1.65      markus    261:                        fprintf(stderr,
1.96      djm       262:                            "The user must confirm each use of the key\n");
1.60      markus    263:        } else {
1.116     djm       264:                fprintf(stderr, "Could not add identity \"%s\": %s\n",
                    265:                    filename, ssh_err(r));
1.56      markus    266:        }
                    267:
1.102     djm       268:        /* Skip trying to load the cert if requested */
                    269:        if (key_only)
                    270:                goto out;
1.93      djm       271:
                    272:        /* Now try to add the certificate flavour too */
                    273:        xasprintf(&certpath, "%s-cert.pub", filename);
1.116     djm       274:        if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
                    275:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    276:                        error("Failed to load certificate \"%s\": %s",
                    277:                            certpath, ssh_err(r));
1.96      djm       278:                goto out;
1.116     djm       279:        }
1.96      djm       280:
1.116     djm       281:        if (!sshkey_equal_public(cert, private)) {
1.96      djm       282:                error("Certificate %s does not match private key %s",
                    283:                    certpath, filename);
1.116     djm       284:                sshkey_free(cert);
1.96      djm       285:                goto out;
                    286:        }
1.93      djm       287:
1.96      djm       288:        /* Graft with private bits */
1.116     djm       289:        if ((r = sshkey_to_certified(private,
                    290:            sshkey_cert_is_legacy(cert))) != 0) {
                    291:                error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
                    292:                sshkey_free(cert);
                    293:                goto out;
                    294:        }
                    295:        if ((r = sshkey_cert_copy(cert, private)) != 0) {
                    296:                error("%s: key_cert_copy: %s", __func__, ssh_err(r));
                    297:                sshkey_free(cert);
1.96      djm       298:                goto out;
1.94      otto      299:        }
1.116     djm       300:        sshkey_free(cert);
1.93      djm       301:
1.116     djm       302:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
                    303:            lifetime, confirm)) != 0) {
                    304:                error("Certificate %s (%s) add failed: %s", certpath,
                    305:                    private->cert->key_id, ssh_err(r));
                    306:                goto out;
1.96      djm       307:        }
                    308:        fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
                    309:            private->cert->key_id);
                    310:        if (lifetime != 0)
                    311:                fprintf(stderr, "Lifetime set to %d seconds\n", lifetime);
                    312:        if (confirm != 0)
                    313:                fprintf(stderr, "The user must confirm each use of the key\n");
                    314:  out:
1.116     djm       315:        free(certpath);
1.106     djm       316:        free(comment);
1.116     djm       317:        sshkey_free(private);
1.47      deraadt   318:
1.46      djm       319:        return ret;
1.1       deraadt   320: }
                    321:
1.46      djm       322: static int
1.116     djm       323: update_card(int agent_fd, int add, const char *id)
1.42      markus    324: {
1.108     djm       325:        char *pin = NULL;
1.116     djm       326:        int r, ret = -1;
1.53      rees      327:
1.108     djm       328:        if (add) {
                    329:                if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
                    330:                    RP_ALLOW_STDIN)) == NULL)
                    331:                        return -1;
                    332:        }
1.53      rees      333:
1.116     djm       334:        if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
                    335:            lifetime, confirm)) == 0) {
1.44      markus    336:                fprintf(stderr, "Card %s: %s\n",
1.47      deraadt   337:                    add ? "added" : "removed", id);
1.66      markus    338:                ret = 0;
1.46      djm       339:        } else {
1.116     djm       340:                fprintf(stderr, "Could not %s card \"%s\": %s\n",
                    341:                    add ? "add" : "remove", id, ssh_err(r));
1.66      markus    342:                ret = -1;
1.46      djm       343:        }
1.106     djm       344:        free(pin);
1.66      markus    345:        return ret;
1.42      markus    346: }
                    347:
1.50      markus    348: static int
1.116     djm       349: list_identities(int agent_fd, int do_fp)
1.1       deraadt   350: {
1.116     djm       351:        char *fp;
                    352:        int version, r, had_identities = 0;
                    353:        struct ssh_identitylist *idlist;
                    354:        size_t i;
1.12      markus    355:
1.19      markus    356:        for (version = 1; version <= 2; version++) {
1.116     djm       357:                if ((r = ssh_fetch_identitylist(agent_fd, version,
                    358:                    &idlist)) != 0) {
                    359:                        if (r != SSH_ERR_AGENT_NO_IDENTITIES)
                    360:                                fprintf(stderr, "error fetching identities for "
                    361:                                    "protocol %d: %s\n", version, ssh_err(r));
                    362:                        continue;
                    363:                }
                    364:                for (i = 0; i < idlist->nkeys; i++) {
1.19      markus    365:                        had_identities = 1;
1.30      markus    366:                        if (do_fp) {
1.116     djm       367:                                fp = sshkey_fingerprint(idlist->keys[i],
                    368:                                    fingerprint_hash, SSH_FP_DEFAULT);
1.23      markus    369:                                printf("%d %s %s (%s)\n",
1.116     djm       370:                                    sshkey_size(idlist->keys[i]), fp,
                    371:                                    idlist->comments[i],
                    372:                                    sshkey_type(idlist->keys[i]));
1.106     djm       373:                                free(fp);
1.12      markus    374:                        } else {
1.116     djm       375:                                if ((r = sshkey_write(idlist->keys[i],
                    376:                                    stdout)) != 0) {
                    377:                                        fprintf(stderr, "sshkey_write: %s\n",
                    378:                                            ssh_err(r));
                    379:                                        continue;
                    380:                                }
                    381:                                fprintf(stdout, " %s\n", idlist->comments[i]);
1.12      markus    382:                        }
                    383:                }
1.116     djm       384:                ssh_free_identitylist(idlist);
1.12      markus    385:        }
1.50      markus    386:        if (!had_identities) {
1.12      markus    387:                printf("The agent has no identities.\n");
1.50      markus    388:                return -1;
                    389:        }
                    390:        return 0;
1.1       deraadt   391: }
                    392:
1.48      djm       393: static int
1.116     djm       394: lock_agent(int agent_fd, int lock)
1.54      markus    395: {
                    396:        char prompt[100], *p1, *p2;
1.116     djm       397:        int r, passok = 1, ret = -1;
1.61      deraadt   398:
1.54      markus    399:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    400:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    401:        if (lock) {
                    402:                strlcpy(prompt, "Again: ", sizeof prompt);
                    403:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    404:                if (strcmp(p1, p2) != 0) {
                    405:                        fprintf(stderr, "Passwords do not match.\n");
                    406:                        passok = 0;
                    407:                }
1.109     djm       408:                explicit_bzero(p2, strlen(p2));
1.106     djm       409:                free(p2);
1.54      markus    410:        }
1.116     djm       411:        if (passok) {
                    412:                if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
                    413:                        fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    414:                        ret = 0;
                    415:                } else {
                    416:                        fprintf(stderr, "Failed to %slock agent: %s\n",
                    417:                            lock ? "" : "un", ssh_err(r));
                    418:                }
                    419:        }
1.109     djm       420:        explicit_bzero(p1, strlen(p1));
1.106     djm       421:        free(p1);
1.62      markus    422:        return (ret);
1.54      markus    423: }
                    424:
                    425: static int
1.116     djm       426: do_file(int agent_fd, int deleting, int key_only, char *file)
1.48      djm       427: {
                    428:        if (deleting) {
1.116     djm       429:                if (delete_file(agent_fd, file, key_only) == -1)
1.48      djm       430:                        return -1;
                    431:        } else {
1.116     djm       432:                if (add_file(agent_fd, file, key_only) == -1)
1.48      djm       433:                        return -1;
                    434:        }
                    435:        return 0;
                    436: }
                    437:
1.42      markus    438: static void
                    439: usage(void)
                    440: {
1.90      sobrado   441:        fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45      jakob     442:        fprintf(stderr, "Options:\n");
                    443:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
1.115     djm       444:        fprintf(stderr, "  -E hash     Specify hash algorithm used for fingerprints.\n");
1.45      jakob     445:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
1.103     djm       446:        fprintf(stderr, "  -k          Load only keys and not certificates.\n");
                    447:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
                    448:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.45      jakob     449:        fprintf(stderr, "  -d          Delete identity.\n");
                    450:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    451:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    452:        fprintf(stderr, "  -X          Unlock agent.\n");
1.92      markus    453:        fprintf(stderr, "  -s pkcs11   Add keys from PKCS#11 provider.\n");
                    454:        fprintf(stderr, "  -e pkcs11   Remove keys provided by PKCS#11 provider.\n");
1.42      markus    455: }
                    456:
1.2       provos    457: int
1.7       markus    458: main(int argc, char **argv)
1.1       deraadt   459: {
1.43      markus    460:        extern char *optarg;
                    461:        extern int optind;
1.116     djm       462:        int agent_fd;
1.92      markus    463:        char *pkcs11provider = NULL;
1.116     djm       464:        int r, i, ch, deleting = 0, ret = 0, key_only = 0;
1.115     djm       465:        int xflag = 0, lflag = 0, Dflag = 0;
1.73      djm       466:
                    467:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    468:        sanitise_stdfd();
1.12      markus    469:
1.100     djm       470:        OpenSSL_add_all_algorithms();
1.112     djm       471:
1.114     millert   472:        setvbuf(stdout, NULL, _IOLBF, 0);
1.19      markus    473:
1.116     djm       474:        /* First, get a connection to the authentication agent. */
                    475:        switch (r = ssh_get_authentication_socket(&agent_fd)) {
                    476:        case 0:
                    477:                break;
                    478:        case SSH_ERR_AGENT_NOT_PRESENT:
                    479:                fprintf(stderr, "Could not open a connection to your "
                    480:                    "authentication agent.\n");
                    481:                exit(2);
                    482:        default:
                    483:                fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50      markus    484:                exit(2);
1.12      markus    485:        }
1.116     djm       486:
1.115     djm       487:        while ((ch = getopt(argc, argv, "klLcdDxXE:e:s:t:")) != -1) {
1.43      markus    488:                switch (ch) {
1.115     djm       489:                case 'E':
                    490:                        fingerprint_hash = ssh_digest_alg_by_name(optarg);
                    491:                        if (fingerprint_hash == -1)
                    492:                                fatal("Invalid hash algorithm \"%s\"", optarg);
                    493:                        break;
1.102     djm       494:                case 'k':
                    495:                        key_only = 1;
                    496:                        break;
1.43      markus    497:                case 'l':
                    498:                case 'L':
1.115     djm       499:                        if (lflag != 0)
                    500:                                fatal("-%c flag already specified", lflag);
                    501:                        lflag = ch;
                    502:                        break;
1.54      markus    503:                case 'x':
                    504:                case 'X':
1.115     djm       505:                        if (xflag != 0)
                    506:                                fatal("-%c flag already specified", xflag);
                    507:                        xflag = ch;
                    508:                        break;
1.65      markus    509:                case 'c':
                    510:                        confirm = 1;
1.43      markus    511:                        break;
                    512:                case 'd':
1.12      markus    513:                        deleting = 1;
1.43      markus    514:                        break;
                    515:                case 'D':
1.115     djm       516:                        Dflag = 1;
                    517:                        break;
1.43      markus    518:                case 's':
1.92      markus    519:                        pkcs11provider = optarg;
1.43      markus    520:                        break;
                    521:                case 'e':
1.47      deraadt   522:                        deleting = 1;
1.92      markus    523:                        pkcs11provider = optarg;
1.56      markus    524:                        break;
                    525:                case 't':
1.57      stevesk   526:                        if ((lifetime = convtime(optarg)) == -1) {
                    527:                                fprintf(stderr, "Invalid lifetime\n");
                    528:                                ret = 1;
                    529:                                goto done;
                    530:                        }
1.43      markus    531:                        break;
                    532:                default:
                    533:                        usage();
1.46      djm       534:                        ret = 1;
                    535:                        goto done;
1.42      markus    536:                }
                    537:        }
1.115     djm       538:
                    539:        if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
                    540:                fatal("Invalid combination of actions");
                    541:        else if (xflag) {
1.116     djm       542:                if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115     djm       543:                        ret = 1;
                    544:                goto done;
                    545:        } else if (lflag) {
1.116     djm       546:                if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115     djm       547:                        ret = 1;
                    548:                goto done;
                    549:        } else if (Dflag) {
1.116     djm       550:                if (delete_all(agent_fd) == -1)
1.115     djm       551:                        ret = 1;
                    552:                goto done;
                    553:        }
                    554:
1.43      markus    555:        argc -= optind;
                    556:        argv += optind;
1.92      markus    557:        if (pkcs11provider != NULL) {
1.116     djm       558:                if (update_card(agent_fd, !deleting, pkcs11provider) == -1)
1.46      djm       559:                        ret = 1;
1.43      markus    560:                goto done;
1.12      markus    561:        }
1.43      markus    562:        if (argc == 0) {
1.117   ! deraadt   563:                char buf[PATH_MAX];
1.48      djm       564:                struct passwd *pw;
1.52      markus    565:                struct stat st;
                    566:                int count = 0;
1.48      djm       567:
                    568:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   569:                        fprintf(stderr, "No user found with uid %u\n",
                    570:                            (u_int)getuid());
1.46      djm       571:                        ret = 1;
                    572:                        goto done;
1.12      markus    573:                }
1.48      djm       574:
1.71      deraadt   575:                for (i = 0; default_files[i]; i++) {
1.51      markus    576:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       577:                            default_files[i]);
1.52      markus    578:                        if (stat(buf, &st) < 0)
                    579:                                continue;
1.116     djm       580:                        if (do_file(agent_fd, deleting, key_only, buf) == -1)
1.46      djm       581:                                ret = 1;
1.52      markus    582:                        else
                    583:                                count++;
1.46      djm       584:                }
1.52      markus    585:                if (count == 0)
                    586:                        ret = 1;
1.43      markus    587:        } else {
1.71      deraadt   588:                for (i = 0; i < argc; i++) {
1.116     djm       589:                        if (do_file(agent_fd, deleting, key_only,
                    590:                            argv[i]) == -1)
1.48      djm       591:                                ret = 1;
1.43      markus    592:                }
1.12      markus    593:        }
1.33      markus    594:        clear_pass();
1.43      markus    595:
                    596: done:
1.116     djm       597:        ssh_close_authentication_socket(agent_fd);
1.46      djm       598:        return ret;
1.1       deraadt   599: }