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

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