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

1.138   ! djm         1: /* $OpenBSD: ssh-add.c,v 1.137 2019/01/20 22:03:29 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.135     markus     73:        _PATH_SSH_CLIENT_ID_XMSS,
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.135     markus     85: /* Maximum number of signatures (XMSS) */
                     86: static u_int maxsign = 0;
                     87: static u_int minleft = 0;
                     88:
1.124     tim        89: /* we keep a cache of one passphrase */
1.33      markus     90: static char *pass = NULL;
1.39      itojun     91: static void
1.33      markus     92: clear_pass(void)
                     93: {
                     94:        if (pass) {
1.109     djm        95:                explicit_bzero(pass, strlen(pass));
1.106     djm        96:                free(pass);
1.33      markus     97:                pass = NULL;
                     98:        }
                     99: }
                    100:
1.46      djm       101: static int
1.134     dlg       102: delete_file(int agent_fd, const char *filename, int key_only, int qflag)
1.1       deraadt   103: {
1.116     djm       104:        struct sshkey *public, *cert = NULL;
1.104     djm       105:        char *certpath = NULL, *comment = NULL;
1.116     djm       106:        int r, ret = -1;
1.1       deraadt   107:
1.116     djm       108:        if ((r = sshkey_load_public(filename, &public,  &comment)) != 0) {
                    109:                printf("Bad key file %s: %s\n", filename, ssh_err(r));
1.46      djm       110:                return -1;
1.12      markus    111:        }
1.116     djm       112:        if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
1.134     dlg       113:                if (!qflag) {
                    114:                        fprintf(stderr, "Identity removed: %s (%s)\n",
                    115:                            filename, comment);
                    116:                }
1.46      djm       117:                ret = 0;
                    118:        } else
1.116     djm       119:                fprintf(stderr, "Could not remove identity \"%s\": %s\n",
                    120:                    filename, ssh_err(r));
1.46      djm       121:
1.104     djm       122:        if (key_only)
                    123:                goto out;
                    124:
                    125:        /* Now try to delete the corresponding certificate too */
                    126:        free(comment);
1.105     markus    127:        comment = NULL;
1.104     djm       128:        xasprintf(&certpath, "%s-cert.pub", filename);
1.120     halex     129:        if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
                    130:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    131:                        error("Failed to load certificate \"%s\": %s",
                    132:                            certpath, ssh_err(r));
1.104     djm       133:                goto out;
1.120     halex     134:        }
                    135:
1.116     djm       136:        if (!sshkey_equal_public(cert, public))
1.104     djm       137:                fatal("Certificate %s does not match private key %s",
                    138:                    certpath, filename);
                    139:
1.120     halex     140:        if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
1.134     dlg       141:                if (!qflag) {
                    142:                        fprintf(stderr, "Identity removed: %s (%s)\n",
                    143:                            certpath, comment);
                    144:                }
1.104     djm       145:                ret = 0;
                    146:        } else
1.120     halex     147:                fprintf(stderr, "Could not remove identity \"%s\": %s\n",
                    148:                    certpath, ssh_err(r));
1.104     djm       149:
                    150:  out:
1.127     mmcc      151:        sshkey_free(cert);
                    152:        sshkey_free(public);
1.104     djm       153:        free(certpath);
                    154:        free(comment);
1.47      deraadt   155:
1.46      djm       156:        return ret;
1.1       deraadt   157: }
                    158:
1.19      markus    159: /* Send a request to remove all identities. */
1.46      djm       160: static int
1.136     djm       161: delete_all(int agent_fd, int qflag)
1.1       deraadt   162: {
1.46      djm       163:        int ret = -1;
1.19      markus    164:
1.130     djm       165:        /*
                    166:         * Since the agent might be forwarded, old or non-OpenSSH, when asked
                    167:         * to remove all keys, attempt to remove both protocol v.1 and v.2
                    168:         * keys.
                    169:         */
1.121     markus    170:        if (ssh_remove_all_identities(agent_fd, 2) == 0)
1.46      djm       171:                ret = 0;
1.121     markus    172:        /* ignore error-code for ssh1 */
                    173:        ssh_remove_all_identities(agent_fd, 1);
1.19      markus    174:
1.136     djm       175:        if (ret != 0)
                    176:                fprintf(stderr, "Failed to remove all identities.\n");
                    177:        else if (!qflag)
1.12      markus    178:                fprintf(stderr, "All identities removed.\n");
1.46      djm       179:
                    180:        return ret;
1.1       deraadt   181: }
                    182:
1.46      djm       183: static int
1.134     dlg       184: add_file(int agent_fd, const char *filename, int key_only, int qflag)
1.1       deraadt   185: {
1.116     djm       186:        struct sshkey *private, *cert;
1.36      markus    187:        char *comment = NULL;
1.102     djm       188:        char msg[1024], *certpath = NULL;
1.116     djm       189:        int r, fd, ret = -1;
1.135     markus    190:        size_t i;
                    191:        u_int32_t left;
1.116     djm       192:        struct sshbuf *keyblob;
1.135     markus    193:        struct ssh_identitylist *idlist;
1.12      markus    194:
1.101     djm       195:        if (strcmp(filename, "-") == 0) {
                    196:                fd = STDIN_FILENO;
                    197:                filename = "(stdin)";
                    198:        } else if ((fd = open(filename, O_RDONLY)) < 0) {
1.19      markus    199:                perror(filename);
1.46      djm       200:                return -1;
1.19      markus    201:        }
1.76      dtucker   202:
                    203:        /*
                    204:         * Since we'll try to load a keyfile multiple times, permission errors
                    205:         * will occur multiple times, so check perms first and bail if wrong.
                    206:         */
1.101     djm       207:        if (fd != STDIN_FILENO) {
1.116     djm       208:                if (sshkey_perm_ok(fd, filename) != 0) {
1.101     djm       209:                        close(fd);
                    210:                        return -1;
                    211:                }
                    212:        }
1.116     djm       213:        if ((keyblob = sshbuf_new()) == NULL)
                    214:                fatal("%s: sshbuf_new failed", __func__);
                    215:        if ((r = sshkey_load_file(fd, keyblob)) != 0) {
                    216:                fprintf(stderr, "Error loading key \"%s\": %s\n",
                    217:                    filename, ssh_err(r));
                    218:                sshbuf_free(keyblob);
1.101     djm       219:                close(fd);
                    220:                return -1;
                    221:        }
1.76      dtucker   222:        close(fd);
                    223:
1.12      markus    224:        /* At first, try empty passphrase */
1.125     tim       225:        if ((r = sshkey_parse_private_fileblob(keyblob, "", &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.33      markus    231:        /* try last */
1.110     djm       232:        if (private == NULL && pass != NULL) {
1.125     tim       233:                if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
                    234:                    &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.116     djm       235:                        fprintf(stderr, "Error loading key \"%s\": %s\n",
                    236:                            filename, ssh_err(r));
                    237:                        goto fail_load;
                    238:                }
1.110     djm       239:        }
1.31      markus    240:        if (private == NULL) {
1.33      markus    241:                /* clear passphrase since it did not work */
                    242:                clear_pass();
1.124     tim       243:                snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
                    244:                    filename, confirm ? " (will confirm each use)" : "");
1.12      markus    245:                for (;;) {
1.40      markus    246:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.116     djm       247:                        if (strcmp(pass, "") == 0)
                    248:                                goto fail_load;
                    249:                        if ((r = sshkey_parse_private_fileblob(keyblob, pass,
1.125     tim       250:                            &private, &comment)) == 0)
1.116     djm       251:                                break;
                    252:                        else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
                    253:                                fprintf(stderr,
                    254:                                    "Error loading key \"%s\": %s\n",
                    255:                                    filename, ssh_err(r));
                    256:  fail_load:
1.35      markus    257:                                clear_pass();
1.116     djm       258:                                sshbuf_free(keyblob);
1.46      djm       259:                                return -1;
1.12      markus    260:                        }
1.33      markus    261:                        clear_pass();
1.68      markus    262:                        snprintf(msg, sizeof msg,
1.124     tim       263:                            "Bad passphrase, try again for %s%s: ", filename,
1.119     halex     264:                            confirm ? " (will confirm each use)" : "");
1.12      markus    265:                }
                    266:        }
1.124     tim       267:        if (comment == NULL || *comment == '\0')
                    268:                comment = xstrdup(filename);
1.116     djm       269:        sshbuf_free(keyblob);
1.60      markus    270:
1.135     markus    271:        /* For XMSS */
                    272:        if ((r = sshkey_set_filename(private, filename)) != 0) {
                    273:                fprintf(stderr, "Could not add filename to private key: %s (%s)\n",
                    274:                    filename, comment);
                    275:                goto out;
                    276:        }
                    277:        if (maxsign && minleft &&
                    278:            (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) {
                    279:                for (i = 0; i < idlist->nkeys; i++) {
                    280:                        if (!sshkey_equal_public(idlist->keys[i], private))
                    281:                                continue;
                    282:                        left = sshkey_signatures_left(idlist->keys[i]);
                    283:                        if (left < minleft) {
                    284:                                fprintf(stderr,
                    285:                                    "Only %d signatures left.\n", left);
                    286:                                break;
                    287:                        }
                    288:                        fprintf(stderr, "Skipping update: ");
                    289:                        if (left == minleft) {
                    290:                                fprintf(stderr,
                    291:                                   "required signatures left (%d).\n", left);
                    292:                        } else {
                    293:                                fprintf(stderr,
                    294:                                   "more signatures left (%d) than"
                    295:                                    " required (%d).\n", left, minleft);
                    296:                        }
                    297:                        ssh_free_identitylist(idlist);
                    298:                        goto out;
                    299:                }
                    300:                ssh_free_identitylist(idlist);
                    301:        }
                    302:
1.116     djm       303:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
1.135     markus    304:            lifetime, confirm, maxsign)) == 0) {
1.60      markus    305:                ret = 0;
1.136     djm       306:                if (!qflag) {
                    307:                        fprintf(stderr, "Identity added: %s (%s)\n",
                    308:                            filename, comment);
                    309:                        if (lifetime != 0) {
                    310:                                fprintf(stderr,
                    311:                                    "Lifetime set to %d seconds\n", lifetime);
                    312:                        }
                    313:                        if (confirm != 0) {
                    314:                                fprintf(stderr, "The user must confirm "
                    315:                                    "each use of the key\n");
                    316:                        }
                    317:                }
1.60      markus    318:        } else {
1.116     djm       319:                fprintf(stderr, "Could not add identity \"%s\": %s\n",
                    320:                    filename, ssh_err(r));
1.56      markus    321:        }
                    322:
1.102     djm       323:        /* Skip trying to load the cert if requested */
                    324:        if (key_only)
                    325:                goto out;
1.93      djm       326:
                    327:        /* Now try to add the certificate flavour too */
                    328:        xasprintf(&certpath, "%s-cert.pub", filename);
1.116     djm       329:        if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
                    330:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    331:                        error("Failed to load certificate \"%s\": %s",
                    332:                            certpath, ssh_err(r));
1.96      djm       333:                goto out;
1.116     djm       334:        }
1.96      djm       335:
1.116     djm       336:        if (!sshkey_equal_public(cert, private)) {
1.96      djm       337:                error("Certificate %s does not match private key %s",
                    338:                    certpath, filename);
1.116     djm       339:                sshkey_free(cert);
1.96      djm       340:                goto out;
                    341:        }
1.93      djm       342:
1.96      djm       343:        /* Graft with private bits */
1.123     djm       344:        if ((r = sshkey_to_certified(private)) != 0) {
1.116     djm       345:                error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
                    346:                sshkey_free(cert);
                    347:                goto out;
                    348:        }
                    349:        if ((r = sshkey_cert_copy(cert, private)) != 0) {
1.132     markus    350:                error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
1.116     djm       351:                sshkey_free(cert);
1.96      djm       352:                goto out;
1.94      otto      353:        }
1.116     djm       354:        sshkey_free(cert);
1.93      djm       355:
1.116     djm       356:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
1.135     markus    357:            lifetime, confirm, maxsign)) != 0) {
1.116     djm       358:                error("Certificate %s (%s) add failed: %s", certpath,
                    359:                    private->cert->key_id, ssh_err(r));
                    360:                goto out;
1.96      djm       361:        }
1.136     djm       362:        /* success */
                    363:        if (!qflag) {
                    364:                fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
                    365:                    private->cert->key_id);
                    366:                if (lifetime != 0) {
                    367:                        fprintf(stderr, "Lifetime set to %d seconds\n",
                    368:                            lifetime);
                    369:                }
                    370:                if (confirm != 0) {
                    371:                        fprintf(stderr, "The user must confirm each use "
                    372:                            "of the key\n");
                    373:                }
                    374:        }
                    375:
1.96      djm       376:  out:
1.116     djm       377:        free(certpath);
1.106     djm       378:        free(comment);
1.116     djm       379:        sshkey_free(private);
1.47      deraadt   380:
1.46      djm       381:        return ret;
1.1       deraadt   382: }
                    383:
1.46      djm       384: static int
1.136     djm       385: update_card(int agent_fd, int add, const char *id, int qflag)
1.42      markus    386: {
1.108     djm       387:        char *pin = NULL;
1.116     djm       388:        int r, ret = -1;
1.53      rees      389:
1.108     djm       390:        if (add) {
                    391:                if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
                    392:                    RP_ALLOW_STDIN)) == NULL)
                    393:                        return -1;
                    394:        }
1.53      rees      395:
1.116     djm       396:        if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
                    397:            lifetime, confirm)) == 0) {
1.66      markus    398:                ret = 0;
1.136     djm       399:                if (!qflag) {
                    400:                        fprintf(stderr, "Card %s: %s\n",
                    401:                            add ? "added" : "removed", id);
                    402:                }
1.46      djm       403:        } else {
1.116     djm       404:                fprintf(stderr, "Could not %s card \"%s\": %s\n",
                    405:                    add ? "add" : "remove", id, ssh_err(r));
1.66      markus    406:                ret = -1;
1.46      djm       407:        }
1.106     djm       408:        free(pin);
1.66      markus    409:        return ret;
1.42      markus    410: }
                    411:
1.50      markus    412: static int
1.137     djm       413: test_key(int agent_fd, const char *filename)
                    414: {
                    415:        struct sshkey *key = NULL;
                    416:        u_char *sig = NULL;
                    417:        size_t slen = 0;
                    418:        int r, ret = -1;
                    419:        char data[1024];
                    420:
                    421:        if ((r = sshkey_load_public(filename, &key, NULL)) != 0) {
                    422:                error("Couldn't read public key %s: %s", filename, ssh_err(r));
                    423:                return -1;
                    424:        }
                    425:        arc4random_buf(data, sizeof(data));
                    426:        if ((r = ssh_agent_sign(agent_fd, key, &sig, &slen, data, sizeof(data),
                    427:            NULL, 0)) != 0) {
                    428:                error("Agent signature failed for %s: %s",
                    429:                    filename, ssh_err(r));
                    430:                goto done;
                    431:        }
                    432:        if ((r = sshkey_verify(key, sig, slen, data, sizeof(data),
                    433:            NULL, 0)) != 0) {
                    434:                error("Signature verification failed for %s: %s",
                    435:                    filename, ssh_err(r));
                    436:                goto done;
                    437:        }
                    438:        /* success */
                    439:        ret = 0;
                    440:  done:
                    441:        free(sig);
                    442:        sshkey_free(key);
                    443:        return ret;
                    444: }
                    445:
                    446: static int
1.116     djm       447: list_identities(int agent_fd, int do_fp)
1.1       deraadt   448: {
1.116     djm       449:        char *fp;
1.131     naddy     450:        int r;
1.116     djm       451:        struct ssh_identitylist *idlist;
1.135     markus    452:        u_int32_t left;
1.116     djm       453:        size_t i;
1.12      markus    454:
1.131     naddy     455:        if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
                    456:                if (r != SSH_ERR_AGENT_NO_IDENTITIES)
                    457:                        fprintf(stderr, "error fetching identities: %s\n",
                    458:                            ssh_err(r));
                    459:                else
                    460:                        printf("The agent has no identities.\n");
                    461:                return -1;
                    462:        }
                    463:        for (i = 0; i < idlist->nkeys; i++) {
                    464:                if (do_fp) {
                    465:                        fp = sshkey_fingerprint(idlist->keys[i],
                    466:                            fingerprint_hash, SSH_FP_DEFAULT);
                    467:                        printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
                    468:                            fp == NULL ? "(null)" : fp, idlist->comments[i],
                    469:                            sshkey_type(idlist->keys[i]));
                    470:                        free(fp);
                    471:                } else {
                    472:                        if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
                    473:                                fprintf(stderr, "sshkey_write: %s\n",
                    474:                                    ssh_err(r));
                    475:                                continue;
1.12      markus    476:                        }
1.135     markus    477:                        fprintf(stdout, " %s", idlist->comments[i]);
                    478:                        left = sshkey_signatures_left(idlist->keys[i]);
                    479:                        if (left > 0)
                    480:                                fprintf(stdout,
                    481:                                    " [signatures left %d]", left);
                    482:                        fprintf(stdout, "\n");
1.12      markus    483:                }
1.50      markus    484:        }
1.131     naddy     485:        ssh_free_identitylist(idlist);
1.50      markus    486:        return 0;
1.1       deraadt   487: }
                    488:
1.48      djm       489: static int
1.116     djm       490: lock_agent(int agent_fd, int lock)
1.54      markus    491: {
                    492:        char prompt[100], *p1, *p2;
1.116     djm       493:        int r, passok = 1, ret = -1;
1.61      deraadt   494:
1.54      markus    495:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    496:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    497:        if (lock) {
                    498:                strlcpy(prompt, "Again: ", sizeof prompt);
                    499:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    500:                if (strcmp(p1, p2) != 0) {
                    501:                        fprintf(stderr, "Passwords do not match.\n");
                    502:                        passok = 0;
                    503:                }
1.109     djm       504:                explicit_bzero(p2, strlen(p2));
1.106     djm       505:                free(p2);
1.54      markus    506:        }
1.116     djm       507:        if (passok) {
                    508:                if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
                    509:                        fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    510:                        ret = 0;
                    511:                } else {
                    512:                        fprintf(stderr, "Failed to %slock agent: %s\n",
                    513:                            lock ? "" : "un", ssh_err(r));
                    514:                }
                    515:        }
1.109     djm       516:        explicit_bzero(p1, strlen(p1));
1.106     djm       517:        free(p1);
1.62      markus    518:        return (ret);
1.54      markus    519: }
                    520:
                    521: static int
1.134     dlg       522: do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
1.48      djm       523: {
                    524:        if (deleting) {
1.134     dlg       525:                if (delete_file(agent_fd, file, key_only, qflag) == -1)
1.48      djm       526:                        return -1;
                    527:        } else {
1.134     dlg       528:                if (add_file(agent_fd, file, key_only, qflag) == -1)
1.48      djm       529:                        return -1;
                    530:        }
                    531:        return 0;
                    532: }
                    533:
1.42      markus    534: static void
                    535: usage(void)
                    536: {
1.90      sobrado   537:        fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45      jakob     538:        fprintf(stderr, "Options:\n");
                    539:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
1.115     djm       540:        fprintf(stderr, "  -E hash     Specify hash algorithm used for fingerprints.\n");
1.45      jakob     541:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
1.103     djm       542:        fprintf(stderr, "  -k          Load only keys and not certificates.\n");
                    543:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
1.135     markus    544:        fprintf(stderr, "  -m minleft  Maxsign is only changed if less than minleft are left (for XMSS)\n");
                    545:        fprintf(stderr, "  -M maxsign  Maximum number of signatures allowed (for XMSS)\n");
1.103     djm       546:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.45      jakob     547:        fprintf(stderr, "  -d          Delete identity.\n");
                    548:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    549:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    550:        fprintf(stderr, "  -X          Unlock agent.\n");
1.92      markus    551:        fprintf(stderr, "  -s pkcs11   Add keys from PKCS#11 provider.\n");
                    552:        fprintf(stderr, "  -e pkcs11   Remove keys provided by PKCS#11 provider.\n");
1.137     djm       553:        fprintf(stderr, "  -T pubkey   Test if ssh-agent can access matching private key.\n");
1.134     dlg       554:        fprintf(stderr, "  -q          Be quiet after a successful operation.\n");
1.138   ! djm       555:        fprintf(stderr, "  -v          Be more verbose.\n");
1.42      markus    556: }
                    557:
1.2       provos    558: int
1.7       markus    559: main(int argc, char **argv)
1.1       deraadt   560: {
1.43      markus    561:        extern char *optarg;
                    562:        extern int optind;
1.116     djm       563:        int agent_fd;
1.92      markus    564:        char *pkcs11provider = NULL;
1.116     djm       565:        int r, i, ch, deleting = 0, ret = 0, key_only = 0;
1.137     djm       566:        int xflag = 0, lflag = 0, Dflag = 0, qflag = 0, Tflag = 0;
1.138   ! djm       567:        SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
        !           568:        LogLevel log_level = SYSLOG_LEVEL_INFO;
1.73      djm       569:
1.128     dtucker   570:        ssh_malloc_init();      /* must be called before any mallocs */
1.73      djm       571:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    572:        sanitise_stdfd();
1.12      markus    573:
1.100     djm       574:        OpenSSL_add_all_algorithms();
1.112     djm       575:
1.138   ! djm       576:        log_init(__progname, log_level, log_facility, 1);
        !           577:
1.114     millert   578:        setvbuf(stdout, NULL, _IOLBF, 0);
1.19      markus    579:
1.116     djm       580:        /* First, get a connection to the authentication agent. */
                    581:        switch (r = ssh_get_authentication_socket(&agent_fd)) {
                    582:        case 0:
                    583:                break;
                    584:        case SSH_ERR_AGENT_NOT_PRESENT:
                    585:                fprintf(stderr, "Could not open a connection to your "
                    586:                    "authentication agent.\n");
                    587:                exit(2);
                    588:        default:
                    589:                fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50      markus    590:                exit(2);
1.12      markus    591:        }
1.116     djm       592:
1.138   ! djm       593:        while ((ch = getopt(argc, argv, "vklLcdDTxXE:e:M:m:qs:t:")) != -1) {
1.43      markus    594:                switch (ch) {
1.138   ! djm       595:                case 'v':
        !           596:                        if (log_level == SYSLOG_LEVEL_INFO)
        !           597:                                log_level = SYSLOG_LEVEL_DEBUG1;
        !           598:                        else if (log_level < SYSLOG_LEVEL_DEBUG3)
        !           599:                                log_level++;
        !           600:                        break;
1.115     djm       601:                case 'E':
                    602:                        fingerprint_hash = ssh_digest_alg_by_name(optarg);
                    603:                        if (fingerprint_hash == -1)
                    604:                                fatal("Invalid hash algorithm \"%s\"", optarg);
                    605:                        break;
1.102     djm       606:                case 'k':
                    607:                        key_only = 1;
                    608:                        break;
1.43      markus    609:                case 'l':
                    610:                case 'L':
1.115     djm       611:                        if (lflag != 0)
                    612:                                fatal("-%c flag already specified", lflag);
                    613:                        lflag = ch;
                    614:                        break;
1.54      markus    615:                case 'x':
                    616:                case 'X':
1.115     djm       617:                        if (xflag != 0)
                    618:                                fatal("-%c flag already specified", xflag);
                    619:                        xflag = ch;
                    620:                        break;
1.65      markus    621:                case 'c':
                    622:                        confirm = 1;
1.135     markus    623:                        break;
                    624:                case 'm':
                    625:                        minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL);
                    626:                        if (minleft == 0) {
                    627:                                usage();
                    628:                                ret = 1;
                    629:                                goto done;
                    630:                        }
                    631:                        break;
                    632:                case 'M':
                    633:                        maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL);
                    634:                        if (maxsign == 0) {
                    635:                                usage();
                    636:                                ret = 1;
                    637:                                goto done;
                    638:                        }
1.43      markus    639:                        break;
                    640:                case 'd':
1.12      markus    641:                        deleting = 1;
1.43      markus    642:                        break;
                    643:                case 'D':
1.115     djm       644:                        Dflag = 1;
                    645:                        break;
1.43      markus    646:                case 's':
1.92      markus    647:                        pkcs11provider = optarg;
1.43      markus    648:                        break;
                    649:                case 'e':
1.47      deraadt   650:                        deleting = 1;
1.92      markus    651:                        pkcs11provider = optarg;
1.56      markus    652:                        break;
                    653:                case 't':
1.57      stevesk   654:                        if ((lifetime = convtime(optarg)) == -1) {
                    655:                                fprintf(stderr, "Invalid lifetime\n");
                    656:                                ret = 1;
                    657:                                goto done;
                    658:                        }
1.43      markus    659:                        break;
1.134     dlg       660:                case 'q':
                    661:                        qflag = 1;
                    662:                        break;
1.137     djm       663:                case 'T':
                    664:                        Tflag = 1;
                    665:                        break;
1.43      markus    666:                default:
                    667:                        usage();
1.46      djm       668:                        ret = 1;
                    669:                        goto done;
1.42      markus    670:                }
                    671:        }
1.138   ! djm       672:        log_init(__progname, log_level, log_facility, 1);
1.115     djm       673:
                    674:        if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
                    675:                fatal("Invalid combination of actions");
                    676:        else if (xflag) {
1.116     djm       677:                if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115     djm       678:                        ret = 1;
                    679:                goto done;
                    680:        } else if (lflag) {
1.116     djm       681:                if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115     djm       682:                        ret = 1;
                    683:                goto done;
                    684:        } else if (Dflag) {
1.136     djm       685:                if (delete_all(agent_fd, qflag) == -1)
1.115     djm       686:                        ret = 1;
                    687:                goto done;
                    688:        }
                    689:
1.43      markus    690:        argc -= optind;
                    691:        argv += optind;
1.137     djm       692:        if (Tflag) {
                    693:                if (argc <= 0)
                    694:                        fatal("no keys to test");
                    695:                for (r = i = 0; i < argc; i++)
                    696:                        r |= test_key(agent_fd, argv[i]);
                    697:                ret = r == 0 ? 0 : 1;
                    698:                goto done;
                    699:        }
1.92      markus    700:        if (pkcs11provider != NULL) {
1.136     djm       701:                if (update_card(agent_fd, !deleting, pkcs11provider,
                    702:                    qflag) == -1)
1.46      djm       703:                        ret = 1;
1.43      markus    704:                goto done;
1.12      markus    705:        }
1.43      markus    706:        if (argc == 0) {
1.117     deraadt   707:                char buf[PATH_MAX];
1.48      djm       708:                struct passwd *pw;
1.52      markus    709:                struct stat st;
                    710:                int count = 0;
1.48      djm       711:
                    712:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   713:                        fprintf(stderr, "No user found with uid %u\n",
                    714:                            (u_int)getuid());
1.46      djm       715:                        ret = 1;
                    716:                        goto done;
1.12      markus    717:                }
1.48      djm       718:
1.71      deraadt   719:                for (i = 0; default_files[i]; i++) {
1.51      markus    720:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       721:                            default_files[i]);
1.52      markus    722:                        if (stat(buf, &st) < 0)
                    723:                                continue;
1.134     dlg       724:                        if (do_file(agent_fd, deleting, key_only, buf,
                    725:                            qflag) == -1)
1.46      djm       726:                                ret = 1;
1.52      markus    727:                        else
                    728:                                count++;
1.46      djm       729:                }
1.52      markus    730:                if (count == 0)
                    731:                        ret = 1;
1.43      markus    732:        } else {
1.71      deraadt   733:                for (i = 0; i < argc; i++) {
1.116     djm       734:                        if (do_file(agent_fd, deleting, key_only,
1.134     dlg       735:                            argv[i], qflag) == -1)
1.48      djm       736:                                ret = 1;
1.43      markus    737:                }
1.12      markus    738:        }
1.33      markus    739:        clear_pass();
1.43      markus    740:
                    741: done:
1.116     djm       742:        ssh_close_authentication_socket(agent_fd);
1.46      djm       743:        return ret;
1.1       deraadt   744: }