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

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