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

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