[BACK]Return to ssh-agent.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/ssh-agent.c, Revision 1.54.2.2

1.1       deraadt     1: /*
1.22      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * The authentication agent program.
1.33      markus      6:  *
1.35      deraadt     7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
                     12:  *
1.54.2.1  jason      13:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.35      deraadt    14:  *
                     15:  * Redistribution and use in source and binary forms, with or without
                     16:  * modification, are permitted provided that the following conditions
                     17:  * are met:
                     18:  * 1. Redistributions of source code must retain the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer.
                     20:  * 2. Redistributions in binary form must reproduce the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer in the
                     22:  *    documentation and/or other materials provided with the distribution.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     25:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     26:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     27:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     28:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     29:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     30:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     31:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     32:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     33:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.22      deraadt    34:  */
1.1       deraadt    35:
                     36: #include "includes.h"
1.54.2.2! miod       37: #include <sys/queue.h>
        !            38: RCSID("$OpenBSD: ssh-agent.c,v 1.82 2002/03/04 17:27:39 stevesk Exp $");
1.47      markus     39:
                     40: #include <openssl/evp.h>
                     41: #include <openssl/md5.h>
1.1       deraadt    42:
                     43: #include "ssh.h"
                     44: #include "rsa.h"
                     45: #include "buffer.h"
                     46: #include "bufaux.h"
                     47: #include "xmalloc.h"
                     48: #include "getput.h"
1.32      markus     49: #include "key.h"
                     50: #include "authfd.h"
1.37      markus     51: #include "compat.h"
1.47      markus     52: #include "log.h"
1.7       deraadt    53:
1.54.2.1  jason      54: #ifdef SMARTCARD
                     55: #include <openssl/engine.h>
                     56: #include "scard.h"
                     57: #endif
                     58:
1.54.2.2! miod       59: typedef enum {
        !            60:        AUTH_UNUSED,
        !            61:        AUTH_SOCKET,
        !            62:        AUTH_CONNECTION
        !            63: } sock_type;
        !            64:
1.21      markus     65: typedef struct {
                     66:        int fd;
1.54.2.2! miod       67:        sock_type type;
1.21      markus     68:        Buffer input;
                     69:        Buffer output;
1.1       deraadt    70: } SocketEntry;
                     71:
1.45      markus     72: u_int sockets_alloc = 0;
1.1       deraadt    73: SocketEntry *sockets = NULL;
                     74:
1.54.2.2! miod       75: typedef struct identity {
        !            76:        TAILQ_ENTRY(identity) next;
1.33      markus     77:        Key *key;
1.21      markus     78:        char *comment;
1.1       deraadt    79: } Identity;
                     80:
1.33      markus     81: typedef struct {
                     82:        int nentries;
1.54.2.2! miod       83:        TAILQ_HEAD(idqueue, identity) idlist;
1.33      markus     84: } Idtab;
                     85:
                     86: /* private key table, one per protocol version */
                     87: Idtab idtable[3];
1.1       deraadt    88:
                     89: int max_fd = 0;
                     90:
1.11      markus     91: /* pid of shell == parent of agent */
1.29      deraadt    92: pid_t parent_pid = -1;
1.10      markus     93:
                     94: /* pathname and directory for AUTH_SOCKET */
                     95: char socket_name[1024];
                     96: char socket_dir[1024];
                     97:
1.20      markus     98: extern char *__progname;
                     99:
1.54.2.1  jason     100: static void
1.33      markus    101: idtab_init(void)
1.1       deraadt   102: {
1.33      markus    103:        int i;
1.54.2.2! miod      104:        for (i = 0; i <=2; i++) {
        !           105:                TAILQ_INIT(&idtable[i].idlist);
1.33      markus    106:                idtable[i].nentries = 0;
                    107:        }
                    108: }
                    109:
                    110: /* return private key table for requested protocol version */
1.54.2.1  jason     111: static Idtab *
1.33      markus    112: idtab_lookup(int version)
                    113: {
                    114:        if (version < 1 || version > 2)
                    115:                fatal("internal error, bad protocol version %d", version);
                    116:        return &idtable[version];
                    117: }
                    118:
                    119: /* return matching private key for given public key */
1.54.2.2! miod      120: static Identity *
        !           121: lookup_identity(Key *key, int version)
1.33      markus    122: {
1.54.2.2! miod      123:        Identity *id;
        !           124:
1.33      markus    125:        Idtab *tab = idtab_lookup(version);
1.54.2.2! miod      126:        TAILQ_FOREACH(id, &tab->idlist, next) {
        !           127:                if (key_equal(key, id->key))
        !           128:                        return (id);
1.33      markus    129:        }
1.54.2.2! miod      130:        return (NULL);
        !           131: }
        !           132:
        !           133: static void
        !           134: free_identity(Identity *id)
        !           135: {
        !           136:        key_free(id->key);
        !           137:        xfree(id->comment);
        !           138:        xfree(id);
1.33      markus    139: }
                    140:
                    141: /* send list of supported public keys to 'client' */
1.54.2.1  jason     142: static void
1.33      markus    143: process_request_identities(SocketEntry *e, int version)
                    144: {
                    145:        Idtab *tab = idtab_lookup(version);
1.21      markus    146:        Buffer msg;
1.54.2.2! miod      147:        Identity *id;
1.1       deraadt   148:
1.21      markus    149:        buffer_init(&msg);
1.33      markus    150:        buffer_put_char(&msg, (version == 1) ?
                    151:            SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
                    152:        buffer_put_int(&msg, tab->nentries);
1.54.2.2! miod      153:        TAILQ_FOREACH(id, &tab->idlist, next) {
1.39      markus    154:                if (id->key->type == KEY_RSA1) {
1.33      markus    155:                        buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
                    156:                        buffer_put_bignum(&msg, id->key->rsa->e);
                    157:                        buffer_put_bignum(&msg, id->key->rsa->n);
                    158:                } else {
1.45      markus    159:                        u_char *blob;
                    160:                        u_int blen;
1.39      markus    161:                        key_to_blob(id->key, &blob, &blen);
1.33      markus    162:                        buffer_put_string(&msg, blob, blen);
                    163:                        xfree(blob);
                    164:                }
                    165:                buffer_put_cstring(&msg, id->comment);
1.21      markus    166:        }
                    167:        buffer_put_int(&e->output, buffer_len(&msg));
                    168:        buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
                    169:        buffer_free(&msg);
1.1       deraadt   170: }
                    171:
1.33      markus    172: /* ssh1 only */
1.54.2.1  jason     173: static void
1.33      markus    174: process_authentication_challenge1(SocketEntry *e)
1.1       deraadt   175: {
1.54.2.2! miod      176:        Identity *id;
        !           177:        Key *key;
1.33      markus    178:        BIGNUM *challenge;
                    179:        int i, len;
1.21      markus    180:        Buffer msg;
                    181:        MD5_CTX md;
1.45      markus    182:        u_char buf[32], mdbuf[16], session_id[16];
                    183:        u_int response_type;
1.21      markus    184:
                    185:        buffer_init(&msg);
1.39      markus    186:        key = key_new(KEY_RSA1);
1.54.2.2! miod      187:        if ((challenge = BN_new()) == NULL)
        !           188:                fatal("process_authentication_challenge1: BN_new failed");
1.33      markus    189:
                    190:        buffer_get_int(&e->input);                              /* ignored */
                    191:        buffer_get_bignum(&e->input, key->rsa->e);
                    192:        buffer_get_bignum(&e->input, key->rsa->n);
1.21      markus    193:        buffer_get_bignum(&e->input, challenge);
                    194:
1.33      markus    195:        /* Only protocol 1.1 is supported */
                    196:        if (buffer_len(&e->input) == 0)
                    197:                goto failure;
1.54.2.2! miod      198:        buffer_get(&e->input, session_id, 16);
1.33      markus    199:        response_type = buffer_get_int(&e->input);
                    200:        if (response_type != 1)
                    201:                goto failure;
                    202:
1.54.2.2! miod      203:        id = lookup_identity(key, 1);
        !           204:        if (id != NULL) {
        !           205:                Key *private = id->key;
1.33      markus    206:                /* Decrypt the challenge using the private key. */
1.49      markus    207:                if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
                    208:                        goto failure;
1.33      markus    209:
                    210:                /* The response is MD5 of decrypted challenge plus session id. */
                    211:                len = BN_num_bytes(challenge);
                    212:                if (len <= 0 || len > 32) {
                    213:                        log("process_authentication_challenge: bad challenge length %d", len);
                    214:                        goto failure;
                    215:                }
                    216:                memset(buf, 0, 32);
                    217:                BN_bn2bin(challenge, buf + 32 - len);
                    218:                MD5_Init(&md);
                    219:                MD5_Update(&md, buf, 32);
                    220:                MD5_Update(&md, session_id, 16);
                    221:                MD5_Final(mdbuf, &md);
                    222:
                    223:                /* Send the response. */
                    224:                buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
                    225:                for (i = 0; i < 16; i++)
                    226:                        buffer_put_char(&msg, mdbuf[i]);
                    227:                goto send;
                    228:        }
1.21      markus    229:
1.33      markus    230: failure:
                    231:        /* Unknown identity or protocol error.  Send failure. */
1.21      markus    232:        buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    233: send:
                    234:        buffer_put_int(&e->output, buffer_len(&msg));
1.33      markus    235:        buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
                    236:        key_free(key);
                    237:        BN_clear_free(challenge);
                    238:        buffer_free(&msg);
                    239: }
                    240:
                    241: /* ssh2 only */
1.54.2.1  jason     242: static void
1.33      markus    243: process_sign_request2(SocketEntry *e)
                    244: {
                    245:        extern int datafellows;
1.54.2.2! miod      246:        Key *key;
1.45      markus    247:        u_char *blob, *data, *signature = NULL;
                    248:        u_int blen, dlen, slen = 0;
1.37      markus    249:        int flags;
1.33      markus    250:        Buffer msg;
                    251:        int ok = -1;
                    252:
                    253:        datafellows = 0;
1.43      markus    254:
1.33      markus    255:        blob = buffer_get_string(&e->input, &blen);
                    256:        data = buffer_get_string(&e->input, &dlen);
1.37      markus    257:
                    258:        flags = buffer_get_int(&e->input);
                    259:        if (flags & SSH_AGENT_OLD_SIGNATURE)
                    260:                datafellows = SSH_BUG_SIGBLOB;
1.33      markus    261:
1.39      markus    262:        key = key_from_blob(blob, blen);
1.33      markus    263:        if (key != NULL) {
1.54.2.2! miod      264:                Identity *id = lookup_identity(key, 2);
        !           265:                if (id != NULL)
        !           266:                        ok = key_sign(id->key, &signature, &slen, data, dlen);
1.33      markus    267:        }
                    268:        key_free(key);
                    269:        buffer_init(&msg);
                    270:        if (ok == 0) {
                    271:                buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
                    272:                buffer_put_string(&msg, signature, slen);
                    273:        } else {
                    274:                buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    275:        }
                    276:        buffer_put_int(&e->output, buffer_len(&msg));
1.21      markus    277:        buffer_append(&e->output, buffer_ptr(&msg),
1.33      markus    278:            buffer_len(&msg));
1.21      markus    279:        buffer_free(&msg);
1.33      markus    280:        xfree(data);
                    281:        xfree(blob);
                    282:        if (signature != NULL)
                    283:                xfree(signature);
1.1       deraadt   284: }
                    285:
1.33      markus    286: /* shared */
1.54.2.1  jason     287: static void
1.33      markus    288: process_remove_identity(SocketEntry *e, int version)
1.1       deraadt   289: {
1.54.2.2! miod      290:        Key *key = NULL;
1.45      markus    291:        u_char *blob;
                    292:        u_int blen;
                    293:        u_int bits;
1.33      markus    294:        int success = 0;
1.21      markus    295:
1.54.2.2! miod      296:        switch (version) {
1.33      markus    297:        case 1:
1.39      markus    298:                key = key_new(KEY_RSA1);
1.33      markus    299:                bits = buffer_get_int(&e->input);
                    300:                buffer_get_bignum(&e->input, key->rsa->e);
                    301:                buffer_get_bignum(&e->input, key->rsa->n);
                    302:
                    303:                if (bits != key_size(key))
                    304:                        log("Warning: identity keysize mismatch: actual %d, announced %d",
1.46      markus    305:                            key_size(key), bits);
1.33      markus    306:                break;
                    307:        case 2:
                    308:                blob = buffer_get_string(&e->input, &blen);
1.39      markus    309:                key = key_from_blob(blob, blen);
1.33      markus    310:                xfree(blob);
                    311:                break;
                    312:        }
                    313:        if (key != NULL) {
1.54.2.2! miod      314:                Identity *id = lookup_identity(key, version);
        !           315:                if (id != NULL) {
1.23      markus    316:                        /*
                    317:                         * We have this key.  Free the old key.  Since we
                    318:                         * don\'t want to leave empty slots in the middle of
1.40      markus    319:                         * the array, we actually free the key there and move
                    320:                         * all the entries between the empty slot and the end
                    321:                         * of the array.
1.23      markus    322:                         */
1.33      markus    323:                        Idtab *tab = idtab_lookup(version);
1.38      markus    324:                        if (tab->nentries < 1)
                    325:                                fatal("process_remove_identity: "
                    326:                                    "internal error: tab->nentries %d",
                    327:                                    tab->nentries);
1.54.2.2! miod      328:                        TAILQ_REMOVE(&tab->idlist, id, next);
        !           329:                        free_identity(id);
1.33      markus    330:                        tab->nentries--;
                    331:                        success = 1;
1.21      markus    332:                }
1.33      markus    333:                key_free(key);
                    334:        }
1.1       deraadt   335:        buffer_put_int(&e->output, 1);
1.33      markus    336:        buffer_put_char(&e->output,
                    337:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   338: }
                    339:
1.54.2.1  jason     340: static void
1.33      markus    341: process_remove_all_identities(SocketEntry *e, int version)
1.1       deraadt   342: {
1.33      markus    343:        Idtab *tab = idtab_lookup(version);
1.54.2.2! miod      344:        Identity *id;
1.21      markus    345:
                    346:        /* Loop over all identities and clear the keys. */
1.54.2.2! miod      347:        for (id = TAILQ_FIRST(&tab->idlist); id;
        !           348:            id = TAILQ_FIRST(&tab->idlist)) {
        !           349:                TAILQ_REMOVE(&tab->idlist, id, next);
        !           350:                free_identity(id);
1.21      markus    351:        }
                    352:
                    353:        /* Mark that there are no identities. */
1.33      markus    354:        tab->nentries = 0;
1.21      markus    355:
                    356:        /* Send success. */
                    357:        buffer_put_int(&e->output, 1);
                    358:        buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    359:        return;
1.1       deraadt   360: }
                    361:
1.54.2.1  jason     362: static void
1.33      markus    363: process_add_identity(SocketEntry *e, int version)
1.1       deraadt   364: {
1.33      markus    365:        Key *k = NULL;
1.39      markus    366:        char *type_name;
1.33      markus    367:        char *comment;
1.39      markus    368:        int type, success = 0;
1.33      markus    369:        Idtab *tab = idtab_lookup(version);
                    370:
                    371:        switch (version) {
                    372:        case 1:
1.39      markus    373:                k = key_new_private(KEY_RSA1);
1.46      markus    374:                buffer_get_int(&e->input);                      /* ignored */
1.39      markus    375:                buffer_get_bignum(&e->input, k->rsa->n);
                    376:                buffer_get_bignum(&e->input, k->rsa->e);
                    377:                buffer_get_bignum(&e->input, k->rsa->d);
                    378:                buffer_get_bignum(&e->input, k->rsa->iqmp);
1.33      markus    379:
                    380:                /* SSH and SSL have p and q swapped */
1.39      markus    381:                buffer_get_bignum(&e->input, k->rsa->q);        /* p */
                    382:                buffer_get_bignum(&e->input, k->rsa->p);        /* q */
1.33      markus    383:
                    384:                /* Generate additional parameters */
1.54.2.1  jason     385:                rsa_generate_additional_parameters(k->rsa);
1.33      markus    386:                break;
                    387:        case 2:
1.39      markus    388:                type_name = buffer_get_string(&e->input, NULL);
1.46      markus    389:                type = key_type_from_name(type_name);
1.39      markus    390:                xfree(type_name);
1.54.2.2! miod      391:                switch (type) {
1.39      markus    392:                case KEY_DSA:
                    393:                        k = key_new_private(type);
                    394:                        buffer_get_bignum2(&e->input, k->dsa->p);
                    395:                        buffer_get_bignum2(&e->input, k->dsa->q);
                    396:                        buffer_get_bignum2(&e->input, k->dsa->g);
                    397:                        buffer_get_bignum2(&e->input, k->dsa->pub_key);
                    398:                        buffer_get_bignum2(&e->input, k->dsa->priv_key);
                    399:                        break;
                    400:                case KEY_RSA:
                    401:                        k = key_new_private(type);
                    402:                        buffer_get_bignum2(&e->input, k->rsa->n);
                    403:                        buffer_get_bignum2(&e->input, k->rsa->e);
                    404:                        buffer_get_bignum2(&e->input, k->rsa->d);
                    405:                        buffer_get_bignum2(&e->input, k->rsa->iqmp);
                    406:                        buffer_get_bignum2(&e->input, k->rsa->p);
                    407:                        buffer_get_bignum2(&e->input, k->rsa->q);
                    408:
                    409:                        /* Generate additional parameters */
1.54.2.1  jason     410:                        rsa_generate_additional_parameters(k->rsa);
1.39      markus    411:                        break;
                    412:                default:
1.33      markus    413:                        buffer_clear(&e->input);
                    414:                        goto send;
1.21      markus    415:                }
1.33      markus    416:                break;
                    417:        }
                    418:        comment = buffer_get_string(&e->input, NULL);
                    419:        if (k == NULL) {
                    420:                xfree(comment);
                    421:                goto send;
                    422:        }
                    423:        success = 1;
1.54.2.2! miod      424:        if (lookup_identity(k, version) == NULL) {
        !           425:                Identity *id = xmalloc(sizeof(Identity));
        !           426:                id->key = k;
        !           427:                id->comment = comment;
        !           428:                TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.33      markus    429:                /* Increment the number of identities. */
                    430:                tab->nentries++;
                    431:        } else {
                    432:                key_free(k);
                    433:                xfree(comment);
                    434:        }
                    435: send:
1.1       deraadt   436:        buffer_put_int(&e->output, 1);
1.33      markus    437:        buffer_put_char(&e->output,
                    438:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   439: }
                    440:
1.54.2.1  jason     441:
                    442: #ifdef SMARTCARD
                    443: static void
                    444: process_add_smartcard_key (SocketEntry *e)
                    445: {
                    446:        Idtab *tab;
                    447:        Key *n = NULL, *k = NULL;
                    448:        char *sc_reader_id = NULL;
                    449:        int success = 0;
1.54.2.2! miod      450:
1.54.2.1  jason     451:        sc_reader_id = buffer_get_string(&e->input, NULL);
                    452:        k = sc_get_key(sc_reader_id);
                    453:        xfree(sc_reader_id);
                    454:
                    455:        if (k == NULL) {
                    456:                error("sc_get_pubkey failed");
                    457:                goto send;
                    458:        }
                    459:        success = 1;
                    460:
                    461:        tab = idtab_lookup(1);
                    462:        k->type = KEY_RSA1;
1.54.2.2! miod      463:        if (lookup_identity(k, 1) == NULL) {
        !           464:                Identity *id = xmalloc(sizeof(Identity));
1.54.2.1  jason     465:                n = key_new(KEY_RSA1);
                    466:                BN_copy(n->rsa->n, k->rsa->n);
                    467:                BN_copy(n->rsa->e, k->rsa->e);
                    468:                RSA_set_method(n->rsa, sc_get_engine());
1.54.2.2! miod      469:                id->key = n;
        !           470:                id->comment = xstrdup("rsa1 smartcard");
        !           471:                TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.54.2.1  jason     472:                tab->nentries++;
                    473:        }
                    474:        k->type = KEY_RSA;
                    475:        tab = idtab_lookup(2);
1.54.2.2! miod      476:        if (lookup_identity(k, 2) == NULL) {
        !           477:                Identity *id = xmalloc(sizeof(Identity));
1.54.2.1  jason     478:                n = key_new(KEY_RSA);
                    479:                BN_copy(n->rsa->n, k->rsa->n);
                    480:                BN_copy(n->rsa->e, k->rsa->e);
                    481:                RSA_set_method(n->rsa, sc_get_engine());
1.54.2.2! miod      482:                id->key = n;
        !           483:                id->comment = xstrdup("rsa smartcard");
        !           484:                TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.54.2.1  jason     485:                tab->nentries++;
                    486:        }
                    487:        key_free(k);
                    488: send:
                    489:        buffer_put_int(&e->output, 1);
                    490:        buffer_put_char(&e->output,
                    491:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                    492: }
                    493:
                    494: static void
                    495: process_remove_smartcard_key(SocketEntry *e)
                    496: {
1.54.2.2! miod      497:        Key *k = NULL;
1.54.2.1  jason     498:        int success = 0;
                    499:        char *sc_reader_id = NULL;
                    500:
                    501:        sc_reader_id = buffer_get_string(&e->input, NULL);
                    502:        k = sc_get_key(sc_reader_id);
                    503:        xfree(sc_reader_id);
                    504:
                    505:        if (k == NULL) {
                    506:                error("sc_get_pubkey failed");
                    507:        } else {
1.54.2.2! miod      508:                Identity *id;
1.54.2.1  jason     509:                k->type = KEY_RSA1;
1.54.2.2! miod      510:                id = lookup_identity(k, 1);
        !           511:                if (id != NULL) {
1.54.2.1  jason     512:                        Idtab *tab = idtab_lookup(1);
1.54.2.2! miod      513:                        TAILQ_REMOVE(&tab->idlist, id, next);
        !           514:                        free_identity(id);
1.54.2.1  jason     515:                        tab->nentries--;
                    516:                        success = 1;
                    517:                }
                    518:                k->type = KEY_RSA;
1.54.2.2! miod      519:                id = lookup_identity(k, 2);
        !           520:                if (id != NULL) {
1.54.2.1  jason     521:                        Idtab *tab = idtab_lookup(2);
1.54.2.2! miod      522:                        TAILQ_REMOVE(&tab->idlist, id, next);
        !           523:                        free_identity(id);
1.54.2.1  jason     524:                        tab->nentries--;
                    525:                        success = 1;
                    526:                }
                    527:                key_free(k);
                    528:        }
                    529:
                    530:        buffer_put_int(&e->output, 1);
                    531:        buffer_put_char(&e->output,
                    532:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                    533: }
                    534: #endif /* SMARTCARD */
                    535:
1.33      markus    536: /* dispatch incoming messages */
                    537:
1.54.2.1  jason     538: static void
1.2       provos    539: process_message(SocketEntry *e)
1.1       deraadt   540: {
1.45      markus    541:        u_int msg_len;
                    542:        u_int type;
                    543:        u_char *cp;
1.21      markus    544:        if (buffer_len(&e->input) < 5)
                    545:                return;         /* Incomplete message. */
1.54.2.2! miod      546:        cp = buffer_ptr(&e->input);
1.21      markus    547:        msg_len = GET_32BIT(cp);
                    548:        if (msg_len > 256 * 1024) {
                    549:                shutdown(e->fd, SHUT_RDWR);
                    550:                close(e->fd);
                    551:                e->type = AUTH_UNUSED;
                    552:                return;
                    553:        }
                    554:        if (buffer_len(&e->input) < msg_len + 4)
                    555:                return;
                    556:        buffer_consume(&e->input, 4);
                    557:        type = buffer_get_char(&e->input);
                    558:
1.54.2.1  jason     559:        debug("type %d", type);
1.21      markus    560:        switch (type) {
1.33      markus    561:        /* ssh1 */
                    562:        case SSH_AGENTC_RSA_CHALLENGE:
                    563:                process_authentication_challenge1(e);
                    564:                break;
1.21      markus    565:        case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33      markus    566:                process_request_identities(e, 1);
1.21      markus    567:                break;
                    568:        case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33      markus    569:                process_add_identity(e, 1);
1.21      markus    570:                break;
                    571:        case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33      markus    572:                process_remove_identity(e, 1);
1.21      markus    573:                break;
                    574:        case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33      markus    575:                process_remove_all_identities(e, 1);
                    576:                break;
                    577:        /* ssh2 */
                    578:        case SSH2_AGENTC_SIGN_REQUEST:
                    579:                process_sign_request2(e);
                    580:                break;
                    581:        case SSH2_AGENTC_REQUEST_IDENTITIES:
                    582:                process_request_identities(e, 2);
                    583:                break;
                    584:        case SSH2_AGENTC_ADD_IDENTITY:
                    585:                process_add_identity(e, 2);
                    586:                break;
                    587:        case SSH2_AGENTC_REMOVE_IDENTITY:
                    588:                process_remove_identity(e, 2);
                    589:                break;
                    590:        case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
                    591:                process_remove_all_identities(e, 2);
1.21      markus    592:                break;
1.54.2.1  jason     593: #ifdef SMARTCARD
                    594:        case SSH_AGENTC_ADD_SMARTCARD_KEY:
                    595:                process_add_smartcard_key(e);
1.54.2.2! miod      596:                break;
1.54.2.1  jason     597:        case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
                    598:                process_remove_smartcard_key(e);
1.54.2.2! miod      599:                break;
1.54.2.1  jason     600: #endif /* SMARTCARD */
1.21      markus    601:        default:
                    602:                /* Unknown message.  Respond with failure. */
                    603:                error("Unknown message %d", type);
                    604:                buffer_clear(&e->input);
                    605:                buffer_put_int(&e->output, 1);
                    606:                buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    607:                break;
                    608:        }
1.1       deraadt   609: }
                    610:
1.54.2.1  jason     611: static void
1.54.2.2! miod      612: new_socket(sock_type type, int fd)
1.1       deraadt   613: {
1.45      markus    614:        u_int i, old_alloc;
1.21      markus    615:        if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
                    616:                error("fcntl O_NONBLOCK: %s", strerror(errno));
                    617:
                    618:        if (fd > max_fd)
                    619:                max_fd = fd;
                    620:
                    621:        for (i = 0; i < sockets_alloc; i++)
                    622:                if (sockets[i].type == AUTH_UNUSED) {
                    623:                        sockets[i].fd = fd;
                    624:                        sockets[i].type = type;
                    625:                        buffer_init(&sockets[i].input);
                    626:                        buffer_init(&sockets[i].output);
                    627:                        return;
                    628:                }
                    629:        old_alloc = sockets_alloc;
                    630:        sockets_alloc += 10;
                    631:        if (sockets)
                    632:                sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
                    633:        else
                    634:                sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
                    635:        for (i = old_alloc; i < sockets_alloc; i++)
                    636:                sockets[i].type = AUTH_UNUSED;
                    637:        sockets[old_alloc].type = type;
                    638:        sockets[old_alloc].fd = fd;
                    639:        buffer_init(&sockets[old_alloc].input);
                    640:        buffer_init(&sockets[old_alloc].output);
1.1       deraadt   641: }
                    642:
1.54.2.1  jason     643: static int
                    644: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
1.1       deraadt   645: {
1.46      markus    646:        u_int i, sz;
                    647:        int n = 0;
                    648:
                    649:        for (i = 0; i < sockets_alloc; i++) {
1.21      markus    650:                switch (sockets[i].type) {
                    651:                case AUTH_SOCKET:
                    652:                case AUTH_CONNECTION:
1.46      markus    653:                        n = MAX(n, sockets[i].fd);
1.21      markus    654:                        break;
                    655:                case AUTH_UNUSED:
                    656:                        break;
                    657:                default:
                    658:                        fatal("Unknown socket type %d", sockets[i].type);
                    659:                        break;
                    660:                }
1.46      markus    661:        }
                    662:
                    663:        sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.54.2.1  jason     664:        if (*fdrp == NULL || sz > *nallocp) {
1.46      markus    665:                if (*fdrp)
1.54      stevesk   666:                        xfree(*fdrp);
1.46      markus    667:                if (*fdwp)
1.54      stevesk   668:                        xfree(*fdwp);
1.46      markus    669:                *fdrp = xmalloc(sz);
                    670:                *fdwp = xmalloc(sz);
1.54.2.1  jason     671:                *nallocp = sz;
1.46      markus    672:        }
1.54.2.1  jason     673:        if (n < *fdl)
                    674:                debug("XXX shrink: %d < %d", n, *fdl);
                    675:        *fdl = n;
1.46      markus    676:        memset(*fdrp, 0, sz);
                    677:        memset(*fdwp, 0, sz);
                    678:
                    679:        for (i = 0; i < sockets_alloc; i++) {
                    680:                switch (sockets[i].type) {
                    681:                case AUTH_SOCKET:
                    682:                case AUTH_CONNECTION:
                    683:                        FD_SET(sockets[i].fd, *fdrp);
                    684:                        if (buffer_len(&sockets[i].output) > 0)
                    685:                                FD_SET(sockets[i].fd, *fdwp);
                    686:                        break;
                    687:                default:
                    688:                        break;
                    689:                }
                    690:        }
                    691:        return (1);
1.21      markus    692: }
                    693:
1.54.2.1  jason     694: static void
1.21      markus    695: after_select(fd_set *readset, fd_set *writeset)
                    696: {
1.45      markus    697:        u_int i;
1.21      markus    698:        int len, sock;
1.26      markus    699:        socklen_t slen;
1.21      markus    700:        char buf[1024];
                    701:        struct sockaddr_un sunaddr;
                    702:
                    703:        for (i = 0; i < sockets_alloc; i++)
                    704:                switch (sockets[i].type) {
                    705:                case AUTH_UNUSED:
                    706:                        break;
                    707:                case AUTH_SOCKET:
                    708:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.26      markus    709:                                slen = sizeof(sunaddr);
1.46      markus    710:                                sock = accept(sockets[i].fd,
                    711:                                    (struct sockaddr *) &sunaddr, &slen);
1.21      markus    712:                                if (sock < 0) {
1.54.2.2! miod      713:                                        error("accept from AUTH_SOCKET: %s",
        !           714:                                            strerror(errno));
1.21      markus    715:                                        break;
                    716:                                }
                    717:                                new_socket(AUTH_CONNECTION, sock);
                    718:                        }
                    719:                        break;
                    720:                case AUTH_CONNECTION:
                    721:                        if (buffer_len(&sockets[i].output) > 0 &&
                    722:                            FD_ISSET(sockets[i].fd, writeset)) {
1.52      deraadt   723:                                do {
                    724:                                        len = write(sockets[i].fd,
                    725:                                            buffer_ptr(&sockets[i].output),
                    726:                                            buffer_len(&sockets[i].output));
                    727:                                        if (len == -1 && (errno == EAGAIN ||
                    728:                                            errno == EINTR))
                    729:                                                continue;
                    730:                                        break;
                    731:                                } while (1);
1.21      markus    732:                                if (len <= 0) {
                    733:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    734:                                        close(sockets[i].fd);
                    735:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       736:                                        buffer_free(&sockets[i].input);
                    737:                                        buffer_free(&sockets[i].output);
1.21      markus    738:                                        break;
                    739:                                }
                    740:                                buffer_consume(&sockets[i].output, len);
                    741:                        }
                    742:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.52      deraadt   743:                                do {
                    744:                                        len = read(sockets[i].fd, buf, sizeof(buf));
                    745:                                        if (len == -1 && (errno == EAGAIN ||
                    746:                                            errno == EINTR))
                    747:                                                continue;
                    748:                                        break;
                    749:                                } while (1);
1.21      markus    750:                                if (len <= 0) {
                    751:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    752:                                        close(sockets[i].fd);
                    753:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       754:                                        buffer_free(&sockets[i].input);
                    755:                                        buffer_free(&sockets[i].output);
1.21      markus    756:                                        break;
                    757:                                }
                    758:                                buffer_append(&sockets[i].input, buf, len);
                    759:                                process_message(&sockets[i]);
                    760:                        }
                    761:                        break;
                    762:                default:
                    763:                        fatal("Unknown type %d", sockets[i].type);
                    764:                }
1.1       deraadt   765: }
                    766:
1.54.2.1  jason     767: static void
1.54.2.2! miod      768: cleanup_socket(void *p)
1.15      markus    769: {
1.48      deraadt   770:        if (socket_name[0])
                    771:                unlink(socket_name);
                    772:        if (socket_dir[0])
                    773:                rmdir(socket_dir);
1.10      markus    774: }
                    775:
1.54.2.1  jason     776: static void
1.15      markus    777: cleanup_exit(int i)
                    778: {
1.54.2.2! miod      779:        cleanup_socket(NULL);
1.21      markus    780:        exit(i);
1.15      markus    781: }
                    782:
1.54.2.1  jason     783: static void
1.48      deraadt   784: cleanup_handler(int sig)
                    785: {
1.54.2.2! miod      786:        cleanup_socket(NULL);
1.48      deraadt   787:        _exit(2);
                    788: }
                    789:
1.54.2.1  jason     790: static void
                    791: check_parent_exists(int sig)
                    792: {
                    793:        int save_errno = errno;
                    794:
                    795:        if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
                    796:                /* printf("Parent has died - Authentication agent exiting.\n"); */
                    797:                cleanup_handler(sig); /* safe */
                    798:        }
                    799:        signal(SIGALRM, check_parent_exists);
                    800:        alarm(10);
                    801:        errno = save_errno;
                    802: }
                    803:
                    804: static void
1.50      itojun    805: usage(void)
1.15      markus    806: {
1.54.2.1  jason     807:        fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
1.46      markus    808:            __progname);
1.54.2.1  jason     809:        fprintf(stderr, "Options:\n");
                    810:        fprintf(stderr, "  -c          Generate C-shell commands on stdout.\n");
                    811:        fprintf(stderr, "  -s          Generate Bourne shell commands on stdout.\n");
                    812:        fprintf(stderr, "  -k          Kill the current agent.\n");
                    813:        fprintf(stderr, "  -d          Debug mode.\n");
1.21      markus    814:        exit(1);
1.15      markus    815: }
                    816:
1.2       provos    817: int
                    818: main(int ac, char **av)
1.1       deraadt   819: {
1.54.2.1  jason     820:        int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
1.21      markus    821:        struct sockaddr_un sunaddr;
1.41      markus    822:        struct rlimit rlim;
1.21      markus    823:        pid_t pid;
                    824:        char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.43      markus    825:        extern int optind;
1.46      markus    826:        fd_set *readsetp = NULL, *writesetp = NULL;
1.53      markus    827:
                    828:        SSLeay_add_all_algorithms();
1.21      markus    829:
1.54.2.1  jason     830:        while ((ch = getopt(ac, av, "cdks")) != -1) {
1.21      markus    831:                switch (ch) {
                    832:                case 'c':
                    833:                        if (s_flag)
                    834:                                usage();
                    835:                        c_flag++;
                    836:                        break;
                    837:                case 'k':
                    838:                        k_flag++;
                    839:                        break;
                    840:                case 's':
                    841:                        if (c_flag)
                    842:                                usage();
                    843:                        s_flag++;
                    844:                        break;
1.54.2.1  jason     845:                case 'd':
                    846:                        if (d_flag)
                    847:                                usage();
                    848:                        d_flag++;
                    849:                        break;
1.21      markus    850:                default:
                    851:                        usage();
                    852:                }
                    853:        }
                    854:        ac -= optind;
                    855:        av += optind;
                    856:
1.54.2.1  jason     857:        if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21      markus    858:                usage();
                    859:
1.54.2.1  jason     860:        if (ac == 0 && !c_flag && !k_flag && !s_flag && !d_flag) {
1.21      markus    861:                shell = getenv("SHELL");
                    862:                if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
                    863:                        c_flag = 1;
                    864:        }
                    865:        if (k_flag) {
                    866:                pidstr = getenv(SSH_AGENTPID_ENV_NAME);
                    867:                if (pidstr == NULL) {
                    868:                        fprintf(stderr, "%s not set, cannot kill agent\n",
1.46      markus    869:                            SSH_AGENTPID_ENV_NAME);
1.21      markus    870:                        exit(1);
                    871:                }
                    872:                pid = atoi(pidstr);
1.46      markus    873:                if (pid < 1) {
1.21      markus    874:                        fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46      markus    875:                            SSH_AGENTPID_ENV_NAME, pidstr);
1.21      markus    876:                        exit(1);
                    877:                }
                    878:                if (kill(pid, SIGTERM) == -1) {
                    879:                        perror("kill");
                    880:                        exit(1);
                    881:                }
                    882:                format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
                    883:                printf(format, SSH_AUTHSOCKET_ENV_NAME);
                    884:                printf(format, SSH_AGENTPID_ENV_NAME);
                    885:                printf("echo Agent pid %d killed;\n", pid);
                    886:                exit(0);
                    887:        }
                    888:        parent_pid = getpid();
                    889:
                    890:        /* Create private directory for agent socket */
                    891:        strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
                    892:        if (mkdtemp(socket_dir) == NULL) {
                    893:                perror("mkdtemp: private socket dir");
                    894:                exit(1);
                    895:        }
                    896:        snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
1.46      markus    897:            parent_pid);
1.21      markus    898:
1.23      markus    899:        /*
                    900:         * Create socket early so it will exist before command gets run from
                    901:         * the parent.
                    902:         */
1.21      markus    903:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                    904:        if (sock < 0) {
                    905:                perror("socket");
                    906:                cleanup_exit(1);
                    907:        }
                    908:        memset(&sunaddr, 0, sizeof(sunaddr));
                    909:        sunaddr.sun_family = AF_UNIX;
                    910:        strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
                    911:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
                    912:                perror("bind");
                    913:                cleanup_exit(1);
                    914:        }
                    915:        if (listen(sock, 5) < 0) {
                    916:                perror("listen");
                    917:                cleanup_exit(1);
                    918:        }
1.46      markus    919:
1.23      markus    920:        /*
                    921:         * Fork, and have the parent execute the command, if any, or present
                    922:         * the socket data.  The child continues as the authentication agent.
                    923:         */
1.54.2.1  jason     924:        if (d_flag) {
                    925:                log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
                    926:                format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                    927:                printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
                    928:                    SSH_AUTHSOCKET_ENV_NAME);
                    929:                printf("echo Agent pid %d;\n", parent_pid);
                    930:                goto skip;
                    931:        }
1.21      markus    932:        pid = fork();
                    933:        if (pid == -1) {
                    934:                perror("fork");
1.54.2.2! miod      935:                cleanup_exit(1);
1.21      markus    936:        }
                    937:        if (pid != 0) {         /* Parent - execute the given command. */
                    938:                close(sock);
                    939:                snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
                    940:                if (ac == 0) {
                    941:                        format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                    942:                        printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46      markus    943:                            SSH_AUTHSOCKET_ENV_NAME);
1.21      markus    944:                        printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46      markus    945:                            SSH_AGENTPID_ENV_NAME);
1.21      markus    946:                        printf("echo Agent pid %d;\n", pid);
                    947:                        exit(0);
                    948:                }
1.36      deraadt   949:                if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
                    950:                    setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
                    951:                        perror("setenv");
                    952:                        exit(1);
                    953:                }
1.21      markus    954:                execvp(av[0], av);
                    955:                perror(av[0]);
                    956:                exit(1);
                    957:        }
1.54.2.2! miod      958:        /* child */
        !           959:        log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.54.2.1  jason     960:
                    961:        if (setsid() == -1) {
1.54.2.2! miod      962:                error("setsid: %s", strerror(errno));
1.54.2.1  jason     963:                cleanup_exit(1);
                    964:        }
                    965:
                    966:        (void)chdir("/");
1.21      markus    967:        close(0);
                    968:        close(1);
                    969:        close(2);
                    970:
1.41      markus    971:        /* deny core dumps, since memory contains unencrypted private keys */
                    972:        rlim.rlim_cur = rlim.rlim_max = 0;
                    973:        if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.54.2.2! miod      974:                error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.41      markus    975:                cleanup_exit(1);
                    976:        }
1.54.2.1  jason     977:
                    978: skip:
1.54.2.2! miod      979:        fatal_add_cleanup(cleanup_socket, NULL);
1.21      markus    980:        new_socket(AUTH_SOCKET, sock);
                    981:        if (ac > 0) {
                    982:                signal(SIGALRM, check_parent_exists);
                    983:                alarm(10);
                    984:        }
1.33      markus    985:        idtab_init();
1.54.2.1  jason     986:        if (!d_flag)
                    987:                signal(SIGINT, SIG_IGN);
1.21      markus    988:        signal(SIGPIPE, SIG_IGN);
1.48      deraadt   989:        signal(SIGHUP, cleanup_handler);
                    990:        signal(SIGTERM, cleanup_handler);
1.54.2.1  jason     991:        nalloc = 0;
                    992:
1.21      markus    993:        while (1) {
1.54.2.1  jason     994:                prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1.46      markus    995:                if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21      markus    996:                        if (errno == EINTR)
                    997:                                continue;
1.54.2.2! miod      998:                        fatal("select: %s", strerror(errno));
1.21      markus    999:                }
1.46      markus   1000:                after_select(readsetp, writesetp);
1.15      markus   1001:        }
1.21      markus   1002:        /* NOTREACHED */
1.1       deraadt  1003: }