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

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