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

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