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

1.136   ! djm         1: /* $OpenBSD: ssh-add.c,v 1.135 2018/02/23 15:58:37 markus Exp $ */
1.1       deraadt     2: /*
1.13      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Adds an identity to the authentication server, or removes an identity.
1.19      markus      7:  *
1.22      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
1.19      markus     14:  * SSH2 implementation,
1.41      markus     15:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.22      deraadt    16:  *
                     17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13      deraadt    36:  */
1.1       deraadt    37:
1.75      stevesk    38: #include <sys/types.h>
                     39: #include <sys/stat.h>
1.16      markus     40:
1.19      markus     41: #include <openssl/evp.h>
1.81      stevesk    42:
1.116     djm        43: #include <errno.h>
1.82      stevesk    44: #include <fcntl.h>
1.81      stevesk    45: #include <pwd.h>
1.88      stevesk    46: #include <stdio.h>
1.87      stevesk    47: #include <stdlib.h>
1.85      stevesk    48: #include <string.h>
1.84      stevesk    49: #include <unistd.h>
1.117     deraadt    50: #include <limits.h>
1.1       deraadt    51:
1.89      deraadt    52: #include "xmalloc.h"
1.27      markus     53: #include "ssh.h"
                     54: #include "log.h"
1.116     djm        55: #include "sshkey.h"
                     56: #include "sshbuf.h"
1.18      markus     57: #include "authfd.h"
1.16      markus     58: #include "authfile.h"
1.25      markus     59: #include "pathnames.h"
1.57      stevesk    60: #include "misc.h"
1.110     djm        61: #include "ssherr.h"
1.115     djm        62: #include "digest.h"
1.1       deraadt    63:
1.45      jakob      64: /* argv0 */
                     65: extern char *__progname;
                     66:
1.48      djm        67: /* Default files to add */
                     68: static char *default_files[] = {
                     69:        _PATH_SSH_CLIENT_ID_RSA,
                     70:        _PATH_SSH_CLIENT_ID_DSA,
1.99      djm        71:        _PATH_SSH_CLIENT_ID_ECDSA,
1.107     pascal     72:        _PATH_SSH_CLIENT_ID_ED25519,
1.135     markus     73:        _PATH_SSH_CLIENT_ID_XMSS,
1.48      djm        74:        NULL
                     75: };
                     76:
1.115     djm        77: static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
                     78:
1.56      markus     79: /* Default lifetime (0 == forever) */
1.57      stevesk    80: static int lifetime = 0;
1.48      djm        81:
1.65      markus     82: /* User has to confirm key use */
                     83: static int confirm = 0;
                     84:
1.135     markus     85: /* Maximum number of signatures (XMSS) */
                     86: static u_int maxsign = 0;
                     87: static u_int minleft = 0;
                     88:
1.124     tim        89: /* we keep a cache of one passphrase */
1.33      markus     90: static char *pass = NULL;
1.39      itojun     91: static void
1.33      markus     92: clear_pass(void)
                     93: {
                     94:        if (pass) {
1.109     djm        95:                explicit_bzero(pass, strlen(pass));
1.106     djm        96:                free(pass);
1.33      markus     97:                pass = NULL;
                     98:        }
                     99: }
                    100:
1.46      djm       101: static int
1.134     dlg       102: delete_file(int agent_fd, const char *filename, int key_only, int qflag)
1.1       deraadt   103: {
1.116     djm       104:        struct sshkey *public, *cert = NULL;
1.104     djm       105:        char *certpath = NULL, *comment = NULL;
1.116     djm       106:        int r, ret = -1;
1.1       deraadt   107:
1.116     djm       108:        if ((r = sshkey_load_public(filename, &public,  &comment)) != 0) {
                    109:                printf("Bad key file %s: %s\n", filename, ssh_err(r));
1.46      djm       110:                return -1;
1.12      markus    111:        }
1.116     djm       112:        if ((r = ssh_remove_identity(agent_fd, public)) == 0) {
1.134     dlg       113:                if (!qflag) {
                    114:                        fprintf(stderr, "Identity removed: %s (%s)\n",
                    115:                            filename, comment);
                    116:                }
1.46      djm       117:                ret = 0;
                    118:        } else
1.116     djm       119:                fprintf(stderr, "Could not remove identity \"%s\": %s\n",
                    120:                    filename, ssh_err(r));
1.46      djm       121:
1.104     djm       122:        if (key_only)
                    123:                goto out;
                    124:
                    125:        /* Now try to delete the corresponding certificate too */
                    126:        free(comment);
1.105     markus    127:        comment = NULL;
1.104     djm       128:        xasprintf(&certpath, "%s-cert.pub", filename);
1.120     halex     129:        if ((r = sshkey_load_public(certpath, &cert, &comment)) != 0) {
                    130:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    131:                        error("Failed to load certificate \"%s\": %s",
                    132:                            certpath, ssh_err(r));
1.104     djm       133:                goto out;
1.120     halex     134:        }
                    135:
1.116     djm       136:        if (!sshkey_equal_public(cert, public))
1.104     djm       137:                fatal("Certificate %s does not match private key %s",
                    138:                    certpath, filename);
                    139:
1.120     halex     140:        if ((r = ssh_remove_identity(agent_fd, cert)) == 0) {
1.134     dlg       141:                if (!qflag) {
                    142:                        fprintf(stderr, "Identity removed: %s (%s)\n",
                    143:                            certpath, comment);
                    144:                }
1.104     djm       145:                ret = 0;
                    146:        } else
1.120     halex     147:                fprintf(stderr, "Could not remove identity \"%s\": %s\n",
                    148:                    certpath, ssh_err(r));
1.104     djm       149:
                    150:  out:
1.127     mmcc      151:        sshkey_free(cert);
                    152:        sshkey_free(public);
1.104     djm       153:        free(certpath);
                    154:        free(comment);
1.47      deraadt   155:
1.46      djm       156:        return ret;
1.1       deraadt   157: }
                    158:
1.19      markus    159: /* Send a request to remove all identities. */
1.46      djm       160: static int
1.136   ! djm       161: delete_all(int agent_fd, int qflag)
1.1       deraadt   162: {
1.46      djm       163:        int ret = -1;
1.19      markus    164:
1.130     djm       165:        /*
                    166:         * Since the agent might be forwarded, old or non-OpenSSH, when asked
                    167:         * to remove all keys, attempt to remove both protocol v.1 and v.2
                    168:         * keys.
                    169:         */
1.121     markus    170:        if (ssh_remove_all_identities(agent_fd, 2) == 0)
1.46      djm       171:                ret = 0;
1.121     markus    172:        /* ignore error-code for ssh1 */
                    173:        ssh_remove_all_identities(agent_fd, 1);
1.19      markus    174:
1.136   ! djm       175:        if (ret != 0)
        !           176:                fprintf(stderr, "Failed to remove all identities.\n");
        !           177:        else if (!qflag)
1.12      markus    178:                fprintf(stderr, "All identities removed.\n");
1.46      djm       179:
                    180:        return ret;
1.1       deraadt   181: }
                    182:
1.46      djm       183: static int
1.134     dlg       184: add_file(int agent_fd, const char *filename, int key_only, int qflag)
1.1       deraadt   185: {
1.116     djm       186:        struct sshkey *private, *cert;
1.36      markus    187:        char *comment = NULL;
1.102     djm       188:        char msg[1024], *certpath = NULL;
1.116     djm       189:        int r, fd, ret = -1;
1.135     markus    190:        size_t i;
                    191:        u_int32_t left;
1.116     djm       192:        struct sshbuf *keyblob;
1.135     markus    193:        struct ssh_identitylist *idlist;
1.12      markus    194:
1.101     djm       195:        if (strcmp(filename, "-") == 0) {
                    196:                fd = STDIN_FILENO;
                    197:                filename = "(stdin)";
                    198:        } else if ((fd = open(filename, O_RDONLY)) < 0) {
1.19      markus    199:                perror(filename);
1.46      djm       200:                return -1;
1.19      markus    201:        }
1.76      dtucker   202:
                    203:        /*
                    204:         * Since we'll try to load a keyfile multiple times, permission errors
                    205:         * will occur multiple times, so check perms first and bail if wrong.
                    206:         */
1.101     djm       207:        if (fd != STDIN_FILENO) {
1.116     djm       208:                if (sshkey_perm_ok(fd, filename) != 0) {
1.101     djm       209:                        close(fd);
                    210:                        return -1;
                    211:                }
                    212:        }
1.116     djm       213:        if ((keyblob = sshbuf_new()) == NULL)
                    214:                fatal("%s: sshbuf_new failed", __func__);
                    215:        if ((r = sshkey_load_file(fd, keyblob)) != 0) {
                    216:                fprintf(stderr, "Error loading key \"%s\": %s\n",
                    217:                    filename, ssh_err(r));
                    218:                sshbuf_free(keyblob);
1.101     djm       219:                close(fd);
                    220:                return -1;
                    221:        }
1.76      dtucker   222:        close(fd);
                    223:
1.12      markus    224:        /* At first, try empty passphrase */
1.125     tim       225:        if ((r = sshkey_parse_private_fileblob(keyblob, "", &private,
                    226:            &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.116     djm       227:                fprintf(stderr, "Error loading key \"%s\": %s\n",
                    228:                    filename, ssh_err(r));
                    229:                goto fail_load;
                    230:        }
1.33      markus    231:        /* try last */
1.110     djm       232:        if (private == NULL && pass != NULL) {
1.125     tim       233:                if ((r = sshkey_parse_private_fileblob(keyblob, pass, &private,
                    234:                    &comment)) != 0 && r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
1.116     djm       235:                        fprintf(stderr, "Error loading key \"%s\": %s\n",
                    236:                            filename, ssh_err(r));
                    237:                        goto fail_load;
                    238:                }
1.110     djm       239:        }
1.31      markus    240:        if (private == NULL) {
1.33      markus    241:                /* clear passphrase since it did not work */
                    242:                clear_pass();
1.124     tim       243:                snprintf(msg, sizeof msg, "Enter passphrase for %s%s: ",
                    244:                    filename, confirm ? " (will confirm each use)" : "");
1.12      markus    245:                for (;;) {
1.40      markus    246:                        pass = read_passphrase(msg, RP_ALLOW_STDIN);
1.116     djm       247:                        if (strcmp(pass, "") == 0)
                    248:                                goto fail_load;
                    249:                        if ((r = sshkey_parse_private_fileblob(keyblob, pass,
1.125     tim       250:                            &private, &comment)) == 0)
1.116     djm       251:                                break;
                    252:                        else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE) {
                    253:                                fprintf(stderr,
                    254:                                    "Error loading key \"%s\": %s\n",
                    255:                                    filename, ssh_err(r));
                    256:  fail_load:
1.35      markus    257:                                clear_pass();
1.116     djm       258:                                sshbuf_free(keyblob);
1.46      djm       259:                                return -1;
1.12      markus    260:                        }
1.33      markus    261:                        clear_pass();
1.68      markus    262:                        snprintf(msg, sizeof msg,
1.124     tim       263:                            "Bad passphrase, try again for %s%s: ", filename,
1.119     halex     264:                            confirm ? " (will confirm each use)" : "");
1.12      markus    265:                }
                    266:        }
1.124     tim       267:        if (comment == NULL || *comment == '\0')
                    268:                comment = xstrdup(filename);
1.116     djm       269:        sshbuf_free(keyblob);
1.60      markus    270:
1.135     markus    271:        /* For XMSS */
                    272:        if ((r = sshkey_set_filename(private, filename)) != 0) {
                    273:                fprintf(stderr, "Could not add filename to private key: %s (%s)\n",
                    274:                    filename, comment);
                    275:                goto out;
                    276:        }
                    277:        if (maxsign && minleft &&
                    278:            (r = ssh_fetch_identitylist(agent_fd, &idlist)) == 0) {
                    279:                for (i = 0; i < idlist->nkeys; i++) {
                    280:                        if (!sshkey_equal_public(idlist->keys[i], private))
                    281:                                continue;
                    282:                        left = sshkey_signatures_left(idlist->keys[i]);
                    283:                        if (left < minleft) {
                    284:                                fprintf(stderr,
                    285:                                    "Only %d signatures left.\n", left);
                    286:                                break;
                    287:                        }
                    288:                        fprintf(stderr, "Skipping update: ");
                    289:                        if (left == minleft) {
                    290:                                fprintf(stderr,
                    291:                                   "required signatures left (%d).\n", left);
                    292:                        } else {
                    293:                                fprintf(stderr,
                    294:                                   "more signatures left (%d) than"
                    295:                                    " required (%d).\n", left, minleft);
                    296:                        }
                    297:                        ssh_free_identitylist(idlist);
                    298:                        goto out;
                    299:                }
                    300:                ssh_free_identitylist(idlist);
                    301:        }
                    302:
1.116     djm       303:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
1.135     markus    304:            lifetime, confirm, maxsign)) == 0) {
1.60      markus    305:                ret = 0;
1.136   ! djm       306:                if (!qflag) {
        !           307:                        fprintf(stderr, "Identity added: %s (%s)\n",
        !           308:                            filename, comment);
        !           309:                        if (lifetime != 0) {
        !           310:                                fprintf(stderr,
        !           311:                                    "Lifetime set to %d seconds\n", lifetime);
        !           312:                        }
        !           313:                        if (confirm != 0) {
        !           314:                                fprintf(stderr, "The user must confirm "
        !           315:                                    "each use of the key\n");
        !           316:                        }
        !           317:                }
1.60      markus    318:        } else {
1.116     djm       319:                fprintf(stderr, "Could not add identity \"%s\": %s\n",
                    320:                    filename, ssh_err(r));
1.56      markus    321:        }
                    322:
1.102     djm       323:        /* Skip trying to load the cert if requested */
                    324:        if (key_only)
                    325:                goto out;
1.93      djm       326:
                    327:        /* Now try to add the certificate flavour too */
                    328:        xasprintf(&certpath, "%s-cert.pub", filename);
1.116     djm       329:        if ((r = sshkey_load_public(certpath, &cert, NULL)) != 0) {
                    330:                if (r != SSH_ERR_SYSTEM_ERROR || errno != ENOENT)
                    331:                        error("Failed to load certificate \"%s\": %s",
                    332:                            certpath, ssh_err(r));
1.96      djm       333:                goto out;
1.116     djm       334:        }
1.96      djm       335:
1.116     djm       336:        if (!sshkey_equal_public(cert, private)) {
1.96      djm       337:                error("Certificate %s does not match private key %s",
                    338:                    certpath, filename);
1.116     djm       339:                sshkey_free(cert);
1.96      djm       340:                goto out;
                    341:        }
1.93      djm       342:
1.96      djm       343:        /* Graft with private bits */
1.123     djm       344:        if ((r = sshkey_to_certified(private)) != 0) {
1.116     djm       345:                error("%s: sshkey_to_certified: %s", __func__, ssh_err(r));
                    346:                sshkey_free(cert);
                    347:                goto out;
                    348:        }
                    349:        if ((r = sshkey_cert_copy(cert, private)) != 0) {
1.132     markus    350:                error("%s: sshkey_cert_copy: %s", __func__, ssh_err(r));
1.116     djm       351:                sshkey_free(cert);
1.96      djm       352:                goto out;
1.94      otto      353:        }
1.116     djm       354:        sshkey_free(cert);
1.93      djm       355:
1.116     djm       356:        if ((r = ssh_add_identity_constrained(agent_fd, private, comment,
1.135     markus    357:            lifetime, confirm, maxsign)) != 0) {
1.116     djm       358:                error("Certificate %s (%s) add failed: %s", certpath,
                    359:                    private->cert->key_id, ssh_err(r));
                    360:                goto out;
1.96      djm       361:        }
1.136   ! djm       362:        /* success */
        !           363:        if (!qflag) {
        !           364:                fprintf(stderr, "Certificate added: %s (%s)\n", certpath,
        !           365:                    private->cert->key_id);
        !           366:                if (lifetime != 0) {
        !           367:                        fprintf(stderr, "Lifetime set to %d seconds\n",
        !           368:                            lifetime);
        !           369:                }
        !           370:                if (confirm != 0) {
        !           371:                        fprintf(stderr, "The user must confirm each use "
        !           372:                            "of the key\n");
        !           373:                }
        !           374:        }
        !           375:
1.96      djm       376:  out:
1.116     djm       377:        free(certpath);
1.106     djm       378:        free(comment);
1.116     djm       379:        sshkey_free(private);
1.47      deraadt   380:
1.46      djm       381:        return ret;
1.1       deraadt   382: }
                    383:
1.46      djm       384: static int
1.136   ! djm       385: update_card(int agent_fd, int add, const char *id, int qflag)
1.42      markus    386: {
1.108     djm       387:        char *pin = NULL;
1.116     djm       388:        int r, ret = -1;
1.53      rees      389:
1.108     djm       390:        if (add) {
                    391:                if ((pin = read_passphrase("Enter passphrase for PKCS#11: ",
                    392:                    RP_ALLOW_STDIN)) == NULL)
                    393:                        return -1;
                    394:        }
1.53      rees      395:
1.116     djm       396:        if ((r = ssh_update_card(agent_fd, add, id, pin == NULL ? "" : pin,
                    397:            lifetime, confirm)) == 0) {
1.66      markus    398:                ret = 0;
1.136   ! djm       399:                if (!qflag) {
        !           400:                        fprintf(stderr, "Card %s: %s\n",
        !           401:                            add ? "added" : "removed", id);
        !           402:                }
1.46      djm       403:        } else {
1.116     djm       404:                fprintf(stderr, "Could not %s card \"%s\": %s\n",
                    405:                    add ? "add" : "remove", id, ssh_err(r));
1.66      markus    406:                ret = -1;
1.46      djm       407:        }
1.106     djm       408:        free(pin);
1.66      markus    409:        return ret;
1.42      markus    410: }
                    411:
1.50      markus    412: static int
1.116     djm       413: list_identities(int agent_fd, int do_fp)
1.1       deraadt   414: {
1.116     djm       415:        char *fp;
1.131     naddy     416:        int r;
1.116     djm       417:        struct ssh_identitylist *idlist;
1.135     markus    418:        u_int32_t left;
1.116     djm       419:        size_t i;
1.12      markus    420:
1.131     naddy     421:        if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
                    422:                if (r != SSH_ERR_AGENT_NO_IDENTITIES)
                    423:                        fprintf(stderr, "error fetching identities: %s\n",
                    424:                            ssh_err(r));
                    425:                else
                    426:                        printf("The agent has no identities.\n");
                    427:                return -1;
                    428:        }
                    429:        for (i = 0; i < idlist->nkeys; i++) {
                    430:                if (do_fp) {
                    431:                        fp = sshkey_fingerprint(idlist->keys[i],
                    432:                            fingerprint_hash, SSH_FP_DEFAULT);
                    433:                        printf("%u %s %s (%s)\n", sshkey_size(idlist->keys[i]),
                    434:                            fp == NULL ? "(null)" : fp, idlist->comments[i],
                    435:                            sshkey_type(idlist->keys[i]));
                    436:                        free(fp);
                    437:                } else {
                    438:                        if ((r = sshkey_write(idlist->keys[i], stdout)) != 0) {
                    439:                                fprintf(stderr, "sshkey_write: %s\n",
                    440:                                    ssh_err(r));
                    441:                                continue;
1.12      markus    442:                        }
1.135     markus    443:                        fprintf(stdout, " %s", idlist->comments[i]);
                    444:                        left = sshkey_signatures_left(idlist->keys[i]);
                    445:                        if (left > 0)
                    446:                                fprintf(stdout,
                    447:                                    " [signatures left %d]", left);
                    448:                        fprintf(stdout, "\n");
1.12      markus    449:                }
1.50      markus    450:        }
1.131     naddy     451:        ssh_free_identitylist(idlist);
1.50      markus    452:        return 0;
1.1       deraadt   453: }
                    454:
1.48      djm       455: static int
1.116     djm       456: lock_agent(int agent_fd, int lock)
1.54      markus    457: {
                    458:        char prompt[100], *p1, *p2;
1.116     djm       459:        int r, passok = 1, ret = -1;
1.61      deraadt   460:
1.54      markus    461:        strlcpy(prompt, "Enter lock password: ", sizeof(prompt));
                    462:        p1 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    463:        if (lock) {
                    464:                strlcpy(prompt, "Again: ", sizeof prompt);
                    465:                p2 = read_passphrase(prompt, RP_ALLOW_STDIN);
                    466:                if (strcmp(p1, p2) != 0) {
                    467:                        fprintf(stderr, "Passwords do not match.\n");
                    468:                        passok = 0;
                    469:                }
1.109     djm       470:                explicit_bzero(p2, strlen(p2));
1.106     djm       471:                free(p2);
1.54      markus    472:        }
1.116     djm       473:        if (passok) {
                    474:                if ((r = ssh_lock_agent(agent_fd, lock, p1)) == 0) {
                    475:                        fprintf(stderr, "Agent %slocked.\n", lock ? "" : "un");
                    476:                        ret = 0;
                    477:                } else {
                    478:                        fprintf(stderr, "Failed to %slock agent: %s\n",
                    479:                            lock ? "" : "un", ssh_err(r));
                    480:                }
                    481:        }
1.109     djm       482:        explicit_bzero(p1, strlen(p1));
1.106     djm       483:        free(p1);
1.62      markus    484:        return (ret);
1.54      markus    485: }
                    486:
                    487: static int
1.134     dlg       488: do_file(int agent_fd, int deleting, int key_only, char *file, int qflag)
1.48      djm       489: {
                    490:        if (deleting) {
1.134     dlg       491:                if (delete_file(agent_fd, file, key_only, qflag) == -1)
1.48      djm       492:                        return -1;
                    493:        } else {
1.134     dlg       494:                if (add_file(agent_fd, file, key_only, qflag) == -1)
1.48      djm       495:                        return -1;
                    496:        }
                    497:        return 0;
                    498: }
                    499:
1.42      markus    500: static void
                    501: usage(void)
                    502: {
1.90      sobrado   503:        fprintf(stderr, "usage: %s [options] [file ...]\n", __progname);
1.45      jakob     504:        fprintf(stderr, "Options:\n");
                    505:        fprintf(stderr, "  -l          List fingerprints of all identities.\n");
1.115     djm       506:        fprintf(stderr, "  -E hash     Specify hash algorithm used for fingerprints.\n");
1.45      jakob     507:        fprintf(stderr, "  -L          List public key parameters of all identities.\n");
1.103     djm       508:        fprintf(stderr, "  -k          Load only keys and not certificates.\n");
                    509:        fprintf(stderr, "  -c          Require confirmation to sign using identities\n");
1.135     markus    510:        fprintf(stderr, "  -m minleft  Maxsign is only changed if less than minleft are left (for XMSS)\n");
                    511:        fprintf(stderr, "  -M maxsign  Maximum number of signatures allowed (for XMSS)\n");
1.103     djm       512:        fprintf(stderr, "  -t life     Set lifetime (in seconds) when adding identities.\n");
1.45      jakob     513:        fprintf(stderr, "  -d          Delete identity.\n");
                    514:        fprintf(stderr, "  -D          Delete all identities.\n");
1.55      markus    515:        fprintf(stderr, "  -x          Lock agent.\n");
1.63      markus    516:        fprintf(stderr, "  -X          Unlock agent.\n");
1.92      markus    517:        fprintf(stderr, "  -s pkcs11   Add keys from PKCS#11 provider.\n");
                    518:        fprintf(stderr, "  -e pkcs11   Remove keys provided by PKCS#11 provider.\n");
1.134     dlg       519:        fprintf(stderr, "  -q          Be quiet after a successful operation.\n");
1.42      markus    520: }
                    521:
1.2       provos    522: int
1.7       markus    523: main(int argc, char **argv)
1.1       deraadt   524: {
1.43      markus    525:        extern char *optarg;
                    526:        extern int optind;
1.116     djm       527:        int agent_fd;
1.92      markus    528:        char *pkcs11provider = NULL;
1.116     djm       529:        int r, i, ch, deleting = 0, ret = 0, key_only = 0;
1.134     dlg       530:        int xflag = 0, lflag = 0, Dflag = 0, qflag = 0;
1.73      djm       531:
1.128     dtucker   532:        ssh_malloc_init();      /* must be called before any mallocs */
1.73      djm       533:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    534:        sanitise_stdfd();
1.12      markus    535:
1.100     djm       536:        OpenSSL_add_all_algorithms();
1.112     djm       537:
1.114     millert   538:        setvbuf(stdout, NULL, _IOLBF, 0);
1.19      markus    539:
1.116     djm       540:        /* First, get a connection to the authentication agent. */
                    541:        switch (r = ssh_get_authentication_socket(&agent_fd)) {
                    542:        case 0:
                    543:                break;
                    544:        case SSH_ERR_AGENT_NOT_PRESENT:
                    545:                fprintf(stderr, "Could not open a connection to your "
                    546:                    "authentication agent.\n");
                    547:                exit(2);
                    548:        default:
                    549:                fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
1.50      markus    550:                exit(2);
1.12      markus    551:        }
1.116     djm       552:
1.135     markus    553:        while ((ch = getopt(argc, argv, "klLcdDxXE:e:M:m:qs:t:")) != -1) {
1.43      markus    554:                switch (ch) {
1.115     djm       555:                case 'E':
                    556:                        fingerprint_hash = ssh_digest_alg_by_name(optarg);
                    557:                        if (fingerprint_hash == -1)
                    558:                                fatal("Invalid hash algorithm \"%s\"", optarg);
                    559:                        break;
1.102     djm       560:                case 'k':
                    561:                        key_only = 1;
                    562:                        break;
1.43      markus    563:                case 'l':
                    564:                case 'L':
1.115     djm       565:                        if (lflag != 0)
                    566:                                fatal("-%c flag already specified", lflag);
                    567:                        lflag = ch;
                    568:                        break;
1.54      markus    569:                case 'x':
                    570:                case 'X':
1.115     djm       571:                        if (xflag != 0)
                    572:                                fatal("-%c flag already specified", xflag);
                    573:                        xflag = ch;
                    574:                        break;
1.65      markus    575:                case 'c':
                    576:                        confirm = 1;
1.135     markus    577:                        break;
                    578:                case 'm':
                    579:                        minleft = (int)strtonum(optarg, 1, UINT_MAX, NULL);
                    580:                        if (minleft == 0) {
                    581:                                usage();
                    582:                                ret = 1;
                    583:                                goto done;
                    584:                        }
                    585:                        break;
                    586:                case 'M':
                    587:                        maxsign = (int)strtonum(optarg, 1, UINT_MAX, NULL);
                    588:                        if (maxsign == 0) {
                    589:                                usage();
                    590:                                ret = 1;
                    591:                                goto done;
                    592:                        }
1.43      markus    593:                        break;
                    594:                case 'd':
1.12      markus    595:                        deleting = 1;
1.43      markus    596:                        break;
                    597:                case 'D':
1.115     djm       598:                        Dflag = 1;
                    599:                        break;
1.43      markus    600:                case 's':
1.92      markus    601:                        pkcs11provider = optarg;
1.43      markus    602:                        break;
                    603:                case 'e':
1.47      deraadt   604:                        deleting = 1;
1.92      markus    605:                        pkcs11provider = optarg;
1.56      markus    606:                        break;
                    607:                case 't':
1.57      stevesk   608:                        if ((lifetime = convtime(optarg)) == -1) {
                    609:                                fprintf(stderr, "Invalid lifetime\n");
                    610:                                ret = 1;
                    611:                                goto done;
                    612:                        }
1.43      markus    613:                        break;
1.134     dlg       614:                case 'q':
                    615:                        qflag = 1;
                    616:                        break;
1.43      markus    617:                default:
                    618:                        usage();
1.46      djm       619:                        ret = 1;
                    620:                        goto done;
1.42      markus    621:                }
                    622:        }
1.115     djm       623:
                    624:        if ((xflag != 0) + (lflag != 0) + (Dflag != 0) > 1)
                    625:                fatal("Invalid combination of actions");
                    626:        else if (xflag) {
1.116     djm       627:                if (lock_agent(agent_fd, xflag == 'x' ? 1 : 0) == -1)
1.115     djm       628:                        ret = 1;
                    629:                goto done;
                    630:        } else if (lflag) {
1.116     djm       631:                if (list_identities(agent_fd, lflag == 'l' ? 1 : 0) == -1)
1.115     djm       632:                        ret = 1;
                    633:                goto done;
                    634:        } else if (Dflag) {
1.136   ! djm       635:                if (delete_all(agent_fd, qflag) == -1)
1.115     djm       636:                        ret = 1;
                    637:                goto done;
                    638:        }
                    639:
1.43      markus    640:        argc -= optind;
                    641:        argv += optind;
1.92      markus    642:        if (pkcs11provider != NULL) {
1.136   ! djm       643:                if (update_card(agent_fd, !deleting, pkcs11provider,
        !           644:                    qflag) == -1)
1.46      djm       645:                        ret = 1;
1.43      markus    646:                goto done;
1.12      markus    647:        }
1.43      markus    648:        if (argc == 0) {
1.117     deraadt   649:                char buf[PATH_MAX];
1.48      djm       650:                struct passwd *pw;
1.52      markus    651:                struct stat st;
                    652:                int count = 0;
1.48      djm       653:
                    654:                if ((pw = getpwuid(getuid())) == NULL) {
1.20      deraadt   655:                        fprintf(stderr, "No user found with uid %u\n",
                    656:                            (u_int)getuid());
1.46      djm       657:                        ret = 1;
                    658:                        goto done;
1.12      markus    659:                }
1.48      djm       660:
1.71      deraadt   661:                for (i = 0; default_files[i]; i++) {
1.51      markus    662:                        snprintf(buf, sizeof(buf), "%s/%s", pw->pw_dir,
1.48      djm       663:                            default_files[i]);
1.52      markus    664:                        if (stat(buf, &st) < 0)
                    665:                                continue;
1.134     dlg       666:                        if (do_file(agent_fd, deleting, key_only, buf,
                    667:                            qflag) == -1)
1.46      djm       668:                                ret = 1;
1.52      markus    669:                        else
                    670:                                count++;
1.46      djm       671:                }
1.52      markus    672:                if (count == 0)
                    673:                        ret = 1;
1.43      markus    674:        } else {
1.71      deraadt   675:                for (i = 0; i < argc; i++) {
1.116     djm       676:                        if (do_file(agent_fd, deleting, key_only,
1.134     dlg       677:                            argv[i], qflag) == -1)
1.48      djm       678:                                ret = 1;
1.43      markus    679:                }
1.12      markus    680:        }
1.33      markus    681:        clear_pass();
1.43      markus    682:
                    683: done:
1.116     djm       684:        ssh_close_authentication_socket(agent_fd);
1.46      djm       685:        return ret;
1.1       deraadt   686: }