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

1.34    ! markus      1: /*     $OpenBSD: ssh-agent.c,v 1.33 2000/08/19 21:34:43 markus 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:  * Created: Wed Mar 29 03:46:59 1995 ylo
                      8:  * The authentication agent program.
1.33      markus      9:  *
                     10:  * SSH2 implementation,
                     11:  * Copyright (c) 2000 Markus Friedl. All rights reserved.
1.22      deraadt    12:  */
1.1       deraadt    13:
                     14: #include "includes.h"
1.34    ! markus     15: RCSID("$OpenBSD: ssh-agent.c,v 1.33 2000/08/19 21:34:43 markus Exp $");
1.1       deraadt    16:
                     17: #include "ssh.h"
                     18: #include "rsa.h"
                     19: #include "buffer.h"
                     20: #include "bufaux.h"
                     21: #include "xmalloc.h"
                     22: #include "packet.h"
                     23: #include "getput.h"
                     24: #include "mpaux.h"
                     25:
1.33      markus     26: #include <openssl/evp.h>
1.27      markus     27: #include <openssl/md5.h>
1.32      markus     28: #include <openssl/dsa.h>
                     29: #include <openssl/rsa.h>
                     30: #include "key.h"
                     31: #include "authfd.h"
1.33      markus     32: #include "dsa.h"
                     33: #include "kex.h"
1.7       deraadt    34:
1.21      markus     35: typedef struct {
                     36:        int fd;
                     37:        enum {
                     38:                AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
                     39:        } type;
                     40:        Buffer input;
                     41:        Buffer output;
1.1       deraadt    42: } SocketEntry;
                     43:
                     44: unsigned int sockets_alloc = 0;
                     45: SocketEntry *sockets = NULL;
                     46:
1.21      markus     47: typedef struct {
1.33      markus     48:        Key *key;
1.21      markus     49:        char *comment;
1.1       deraadt    50: } Identity;
                     51:
1.33      markus     52: typedef struct {
                     53:        int nentries;
                     54:        Identity *identities;
                     55: } Idtab;
                     56:
                     57: /* private key table, one per protocol version */
                     58: Idtab idtable[3];
1.1       deraadt    59:
                     60: int max_fd = 0;
                     61:
1.11      markus     62: /* pid of shell == parent of agent */
1.29      deraadt    63: pid_t parent_pid = -1;
1.10      markus     64:
                     65: /* pathname and directory for AUTH_SOCKET */
                     66: char socket_name[1024];
                     67: char socket_dir[1024];
                     68:
1.20      markus     69: extern char *__progname;
                     70:
1.2       provos     71: void
1.33      markus     72: idtab_init(void)
1.1       deraadt    73: {
1.33      markus     74:        int i;
                     75:        for (i = 0; i <=2; i++){
                     76:                idtable[i].identities = NULL;
                     77:                idtable[i].nentries = 0;
                     78:        }
                     79: }
                     80:
                     81: /* return private key table for requested protocol version */
                     82: Idtab *
                     83: idtab_lookup(int version)
                     84: {
                     85:        if (version < 1 || version > 2)
                     86:                fatal("internal error, bad protocol version %d", version);
                     87:        return &idtable[version];
                     88: }
                     89:
                     90: /* return matching private key for given public key */
                     91: Key *
                     92: lookup_private_key(Key *key, int *idx, int version)
                     93: {
                     94:        int i;
                     95:        Idtab *tab = idtab_lookup(version);
                     96:        for (i = 0; i < tab->nentries; i++) {
                     97:                if (key_equal(key, tab->identities[i].key)) {
                     98:                        if (idx != NULL)
                     99:                                *idx = i;
                    100:                        return tab->identities[i].key;
                    101:                }
                    102:        }
                    103:        return NULL;
                    104: }
                    105:
                    106: /* send list of supported public keys to 'client' */
                    107: void
                    108: process_request_identities(SocketEntry *e, int version)
                    109: {
                    110:        Idtab *tab = idtab_lookup(version);
1.21      markus    111:        Buffer msg;
                    112:        int i;
1.1       deraadt   113:
1.21      markus    114:        buffer_init(&msg);
1.33      markus    115:        buffer_put_char(&msg, (version == 1) ?
                    116:            SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
                    117:        buffer_put_int(&msg, tab->nentries);
                    118:        for (i = 0; i < tab->nentries; i++) {
                    119:                Identity *id = &tab->identities[i];
                    120:                if (id->key->type == KEY_RSA) {
                    121:                        buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
                    122:                        buffer_put_bignum(&msg, id->key->rsa->e);
                    123:                        buffer_put_bignum(&msg, id->key->rsa->n);
                    124:                } else {
                    125:                        unsigned char *blob;
                    126:                        unsigned int blen;
                    127:                        dsa_make_key_blob(id->key, &blob, &blen);
                    128:                        buffer_put_string(&msg, blob, blen);
                    129:                        xfree(blob);
                    130:                }
                    131:                buffer_put_cstring(&msg, id->comment);
1.21      markus    132:        }
                    133:        buffer_put_int(&e->output, buffer_len(&msg));
                    134:        buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
                    135:        buffer_free(&msg);
1.1       deraadt   136: }
                    137:
1.33      markus    138: /* ssh1 only */
1.2       provos    139: void
1.33      markus    140: process_authentication_challenge1(SocketEntry *e)
1.1       deraadt   141: {
1.33      markus    142:        Key *key, *private;
                    143:        BIGNUM *challenge;
                    144:        int i, len;
1.21      markus    145:        Buffer msg;
                    146:        MD5_CTX md;
                    147:        unsigned char buf[32], mdbuf[16], session_id[16];
                    148:        unsigned int response_type;
                    149:
                    150:        buffer_init(&msg);
1.33      markus    151:        key = key_new(KEY_RSA);
1.21      markus    152:        challenge = BN_new();
1.33      markus    153:
                    154:        buffer_get_int(&e->input);                              /* ignored */
                    155:        buffer_get_bignum(&e->input, key->rsa->e);
                    156:        buffer_get_bignum(&e->input, key->rsa->n);
1.21      markus    157:        buffer_get_bignum(&e->input, challenge);
                    158:
1.33      markus    159:        /* Only protocol 1.1 is supported */
                    160:        if (buffer_len(&e->input) == 0)
                    161:                goto failure;
                    162:        buffer_get(&e->input, (char *) session_id, 16);
                    163:        response_type = buffer_get_int(&e->input);
                    164:        if (response_type != 1)
                    165:                goto failure;
                    166:
                    167:        private = lookup_private_key(key, NULL, 1);
                    168:        if (private != NULL) {
                    169:                /* Decrypt the challenge using the private key. */
                    170:                rsa_private_decrypt(challenge, challenge, private->rsa);
                    171:
                    172:                /* The response is MD5 of decrypted challenge plus session id. */
                    173:                len = BN_num_bytes(challenge);
                    174:                if (len <= 0 || len > 32) {
                    175:                        log("process_authentication_challenge: bad challenge length %d", len);
                    176:                        goto failure;
                    177:                }
                    178:                memset(buf, 0, 32);
                    179:                BN_bn2bin(challenge, buf + 32 - len);
                    180:                MD5_Init(&md);
                    181:                MD5_Update(&md, buf, 32);
                    182:                MD5_Update(&md, session_id, 16);
                    183:                MD5_Final(mdbuf, &md);
                    184:
                    185:                /* Send the response. */
                    186:                buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
                    187:                for (i = 0; i < 16; i++)
                    188:                        buffer_put_char(&msg, mdbuf[i]);
                    189:                goto send;
                    190:        }
1.21      markus    191:
1.33      markus    192: failure:
                    193:        /* Unknown identity or protocol error.  Send failure. */
1.21      markus    194:        buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    195: send:
                    196:        buffer_put_int(&e->output, buffer_len(&msg));
1.33      markus    197:        buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
                    198:        key_free(key);
                    199:        BN_clear_free(challenge);
                    200:        buffer_free(&msg);
                    201: }
                    202:
                    203: /* ssh2 only */
                    204: void
                    205: process_sign_request2(SocketEntry *e)
                    206: {
                    207:        extern int datafellows;
                    208:        Key *key, *private;
                    209:        unsigned char *blob, *data, *signature = NULL;
                    210:        unsigned int blen, dlen, slen = 0;
                    211:        Buffer msg;
                    212:        int ok = -1;
                    213:
                    214:        datafellows = 0;
                    215:
                    216:        blob = buffer_get_string(&e->input, &blen);
                    217:        data = buffer_get_string(&e->input, &dlen);
1.34    ! markus    218:        buffer_get_int(&e->input);                      /* flags, unused */
1.33      markus    219:
                    220:        key = dsa_key_from_blob(blob, blen);
                    221:        if (key != NULL) {
                    222:                private = lookup_private_key(key, NULL, 2);
                    223:                if (private != NULL)
                    224:                        ok = dsa_sign(private, &signature, &slen, data, dlen);
                    225:        }
                    226:        key_free(key);
                    227:        buffer_init(&msg);
                    228:        if (ok == 0) {
                    229:                buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
                    230:                buffer_put_string(&msg, signature, slen);
                    231:        } else {
                    232:                buffer_put_char(&msg, SSH_AGENT_FAILURE);
                    233:        }
                    234:        buffer_put_int(&e->output, buffer_len(&msg));
1.21      markus    235:        buffer_append(&e->output, buffer_ptr(&msg),
1.33      markus    236:            buffer_len(&msg));
1.21      markus    237:        buffer_free(&msg);
1.33      markus    238:        xfree(data);
                    239:        xfree(blob);
                    240:        if (signature != NULL)
                    241:                xfree(signature);
1.1       deraadt   242: }
                    243:
1.33      markus    244: /* shared */
1.2       provos    245: void
1.33      markus    246: process_remove_identity(SocketEntry *e, int version)
1.1       deraadt   247: {
1.33      markus    248:        Key *key = NULL, *private;
                    249:        unsigned char *blob;
                    250:        unsigned int blen;
1.21      markus    251:        unsigned int bits;
1.33      markus    252:        int success = 0;
1.21      markus    253:
1.33      markus    254:        switch(version){
                    255:        case 1:
                    256:                key = key_new(KEY_RSA);
                    257:                bits = buffer_get_int(&e->input);
                    258:                buffer_get_bignum(&e->input, key->rsa->e);
                    259:                buffer_get_bignum(&e->input, key->rsa->n);
                    260:
                    261:                if (bits != key_size(key))
                    262:                        log("Warning: identity keysize mismatch: actual %d, announced %d",
                    263:                              key_size(key), bits);
                    264:                break;
                    265:        case 2:
                    266:                blob = buffer_get_string(&e->input, &blen);
                    267:                key = dsa_key_from_blob(blob, blen);
                    268:                xfree(blob);
                    269:                break;
                    270:        }
                    271:        if (key != NULL) {
                    272:                int idx;
                    273:                private = lookup_private_key(key, &idx, version);
                    274:                if (private != NULL) {
1.23      markus    275:                        /*
                    276:                         * We have this key.  Free the old key.  Since we
                    277:                         * don\'t want to leave empty slots in the middle of
                    278:                         * the array, we actually free the key there and copy
                    279:                         * data from the last entry.
                    280:                         */
1.33      markus    281:                        Idtab *tab = idtab_lookup(version);
                    282:                        key_free(tab->identities[idx].key);
                    283:                        xfree(tab->identities[idx].comment);
                    284:                        if (idx != tab->nentries)
                    285:                                tab->identities[idx] = tab->identities[tab->nentries];
                    286:                        tab->nentries--;
                    287:                        success = 1;
1.21      markus    288:                }
1.33      markus    289:                key_free(key);
                    290:        }
1.1       deraadt   291:        buffer_put_int(&e->output, 1);
1.33      markus    292:        buffer_put_char(&e->output,
                    293:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   294: }
                    295:
1.2       provos    296: void
1.33      markus    297: process_remove_all_identities(SocketEntry *e, int version)
1.1       deraadt   298: {
1.21      markus    299:        unsigned int i;
1.33      markus    300:        Idtab *tab = idtab_lookup(version);
1.21      markus    301:
                    302:        /* Loop over all identities and clear the keys. */
1.33      markus    303:        for (i = 0; i < tab->nentries; i++) {
                    304:                key_free(tab->identities[i].key);
                    305:                xfree(tab->identities[i].comment);
1.21      markus    306:        }
                    307:
                    308:        /* Mark that there are no identities. */
1.33      markus    309:        tab->nentries = 0;
1.21      markus    310:
                    311:        /* Send success. */
                    312:        buffer_put_int(&e->output, 1);
                    313:        buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    314:        return;
1.1       deraadt   315: }
                    316:
1.2       provos    317: void
1.33      markus    318: process_add_identity(SocketEntry *e, int version)
1.1       deraadt   319: {
1.33      markus    320:        Key *k = NULL;
                    321:        RSA *rsa;
1.21      markus    322:        BIGNUM *aux;
                    323:        BN_CTX *ctx;
1.33      markus    324:        char *type;
                    325:        char *comment;
                    326:        int success = 0;
                    327:        Idtab *tab = idtab_lookup(version);
                    328:
                    329:        switch (version) {
                    330:        case 1:
                    331:                k = key_new(KEY_RSA);
                    332:                rsa = k->rsa;
                    333:
                    334:                /* allocate mem for private key */
                    335:                /* XXX rsa->n and rsa->e are already allocated */
                    336:                rsa->d = BN_new();
                    337:                rsa->iqmp = BN_new();
                    338:                rsa->q = BN_new();
                    339:                rsa->p = BN_new();
                    340:                rsa->dmq1 = BN_new();
                    341:                rsa->dmp1 = BN_new();
                    342:
                    343:                buffer_get_int(&e->input);               /* ignored */
                    344:
                    345:                buffer_get_bignum(&e->input, rsa->n);
                    346:                buffer_get_bignum(&e->input, rsa->e);
                    347:                buffer_get_bignum(&e->input, rsa->d);
                    348:                buffer_get_bignum(&e->input, rsa->iqmp);
                    349:
                    350:                /* SSH and SSL have p and q swapped */
                    351:                buffer_get_bignum(&e->input, rsa->q);   /* p */
                    352:                buffer_get_bignum(&e->input, rsa->p);   /* q */
                    353:
                    354:                /* Generate additional parameters */
                    355:                aux = BN_new();
                    356:                ctx = BN_CTX_new();
1.21      markus    357:
1.33      markus    358:                BN_sub(aux, rsa->q, BN_value_one());
                    359:                BN_mod(rsa->dmq1, rsa->d, aux, ctx);
                    360:
                    361:                BN_sub(aux, rsa->p, BN_value_one());
                    362:                BN_mod(rsa->dmp1, rsa->d, aux, ctx);
1.21      markus    363:
1.33      markus    364:                BN_clear_free(aux);
                    365:                BN_CTX_free(ctx);
1.21      markus    366:
1.33      markus    367:                break;
                    368:        case 2:
                    369:                type = buffer_get_string(&e->input, NULL);
                    370:                if (strcmp(type, KEX_DSS)) {
                    371:                        buffer_clear(&e->input);
                    372:                        xfree(type);
                    373:                        goto send;
1.21      markus    374:                }
1.33      markus    375:                xfree(type);
1.1       deraadt   376:
1.33      markus    377:                k = key_new(KEY_DSA);
                    378:
                    379:                /* allocate mem for private key */
                    380:                k->dsa->priv_key = BN_new();
                    381:
                    382:                buffer_get_bignum2(&e->input, k->dsa->p);
                    383:                buffer_get_bignum2(&e->input, k->dsa->q);
                    384:                buffer_get_bignum2(&e->input, k->dsa->g);
                    385:                buffer_get_bignum2(&e->input, k->dsa->pub_key);
                    386:                buffer_get_bignum2(&e->input, k->dsa->priv_key);
                    387:
                    388:                break;
                    389:        }
                    390:
                    391:        comment = buffer_get_string(&e->input, NULL);
                    392:        if (k == NULL) {
                    393:                xfree(comment);
                    394:                goto send;
                    395:        }
                    396:        success = 1;
                    397:        if (lookup_private_key(k, NULL, version) == NULL) {
                    398:                if (tab->nentries == 0)
                    399:                        tab->identities = xmalloc(sizeof(Identity));
                    400:                else
                    401:                        tab->identities = xrealloc(tab->identities,
                    402:                            (tab->nentries + 1) * sizeof(Identity));
                    403:                tab->identities[tab->nentries].key = k;
                    404:                tab->identities[tab->nentries].comment = comment;
                    405:                /* Increment the number of identities. */
                    406:                tab->nentries++;
                    407:        } else {
                    408:                key_free(k);
                    409:                xfree(comment);
                    410:        }
                    411: send:
1.1       deraadt   412:        buffer_put_int(&e->output, 1);
1.33      markus    413:        buffer_put_char(&e->output,
                    414:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   415: }
                    416:
1.33      markus    417: /* dispatch incoming messages */
                    418:
1.2       provos    419: void
                    420: process_message(SocketEntry *e)
1.1       deraadt   421: {
1.21      markus    422:        unsigned int msg_len;
                    423:        unsigned int type;
                    424:        unsigned char *cp;
                    425:        if (buffer_len(&e->input) < 5)
                    426:                return;         /* Incomplete message. */
                    427:        cp = (unsigned char *) buffer_ptr(&e->input);
                    428:        msg_len = GET_32BIT(cp);
                    429:        if (msg_len > 256 * 1024) {
                    430:                shutdown(e->fd, SHUT_RDWR);
                    431:                close(e->fd);
                    432:                e->type = AUTH_UNUSED;
                    433:                return;
                    434:        }
                    435:        if (buffer_len(&e->input) < msg_len + 4)
                    436:                return;
                    437:        buffer_consume(&e->input, 4);
                    438:        type = buffer_get_char(&e->input);
                    439:
                    440:        switch (type) {
1.33      markus    441:        /* ssh1 */
                    442:        case SSH_AGENTC_RSA_CHALLENGE:
                    443:                process_authentication_challenge1(e);
                    444:                break;
1.21      markus    445:        case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33      markus    446:                process_request_identities(e, 1);
1.21      markus    447:                break;
                    448:        case SSH_AGENTC_ADD_RSA_IDENTITY:
1.33      markus    449:                process_add_identity(e, 1);
1.21      markus    450:                break;
                    451:        case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33      markus    452:                process_remove_identity(e, 1);
1.21      markus    453:                break;
                    454:        case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33      markus    455:                process_remove_all_identities(e, 1);
                    456:                break;
                    457:        /* ssh2 */
                    458:        case SSH2_AGENTC_SIGN_REQUEST:
                    459:                process_sign_request2(e);
                    460:                break;
                    461:        case SSH2_AGENTC_REQUEST_IDENTITIES:
                    462:                process_request_identities(e, 2);
                    463:                break;
                    464:        case SSH2_AGENTC_ADD_IDENTITY:
                    465:                process_add_identity(e, 2);
                    466:                break;
                    467:        case SSH2_AGENTC_REMOVE_IDENTITY:
                    468:                process_remove_identity(e, 2);
                    469:                break;
                    470:        case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
                    471:                process_remove_all_identities(e, 2);
1.21      markus    472:                break;
                    473:        default:
                    474:                /* Unknown message.  Respond with failure. */
                    475:                error("Unknown message %d", type);
                    476:                buffer_clear(&e->input);
                    477:                buffer_put_int(&e->output, 1);
                    478:                buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    479:                break;
                    480:        }
1.1       deraadt   481: }
                    482:
1.2       provos    483: void
                    484: new_socket(int type, int fd)
1.1       deraadt   485: {
1.21      markus    486:        unsigned int i, old_alloc;
                    487:        if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
                    488:                error("fcntl O_NONBLOCK: %s", strerror(errno));
                    489:
                    490:        if (fd > max_fd)
                    491:                max_fd = fd;
                    492:
                    493:        for (i = 0; i < sockets_alloc; i++)
                    494:                if (sockets[i].type == AUTH_UNUSED) {
                    495:                        sockets[i].fd = fd;
                    496:                        sockets[i].type = type;
                    497:                        buffer_init(&sockets[i].input);
                    498:                        buffer_init(&sockets[i].output);
                    499:                        return;
                    500:                }
                    501:        old_alloc = sockets_alloc;
                    502:        sockets_alloc += 10;
                    503:        if (sockets)
                    504:                sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
                    505:        else
                    506:                sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
                    507:        for (i = old_alloc; i < sockets_alloc; i++)
                    508:                sockets[i].type = AUTH_UNUSED;
                    509:        sockets[old_alloc].type = type;
                    510:        sockets[old_alloc].fd = fd;
                    511:        buffer_init(&sockets[old_alloc].input);
                    512:        buffer_init(&sockets[old_alloc].output);
1.1       deraadt   513: }
                    514:
1.2       provos    515: void
                    516: prepare_select(fd_set *readset, fd_set *writeset)
1.1       deraadt   517: {
1.21      markus    518:        unsigned int i;
                    519:        for (i = 0; i < sockets_alloc; i++)
                    520:                switch (sockets[i].type) {
                    521:                case AUTH_SOCKET:
                    522:                case AUTH_CONNECTION:
                    523:                        FD_SET(sockets[i].fd, readset);
                    524:                        if (buffer_len(&sockets[i].output) > 0)
                    525:                                FD_SET(sockets[i].fd, writeset);
                    526:                        break;
                    527:                case AUTH_UNUSED:
                    528:                        break;
                    529:                default:
                    530:                        fatal("Unknown socket type %d", sockets[i].type);
                    531:                        break;
                    532:                }
                    533: }
                    534:
1.28      markus    535: void
1.21      markus    536: after_select(fd_set *readset, fd_set *writeset)
                    537: {
                    538:        unsigned int i;
                    539:        int len, sock;
1.26      markus    540:        socklen_t slen;
1.21      markus    541:        char buf[1024];
                    542:        struct sockaddr_un sunaddr;
                    543:
                    544:        for (i = 0; i < sockets_alloc; i++)
                    545:                switch (sockets[i].type) {
                    546:                case AUTH_UNUSED:
                    547:                        break;
                    548:                case AUTH_SOCKET:
                    549:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.26      markus    550:                                slen = sizeof(sunaddr);
                    551:                                sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
1.21      markus    552:                                if (sock < 0) {
                    553:                                        perror("accept from AUTH_SOCKET");
                    554:                                        break;
                    555:                                }
                    556:                                new_socket(AUTH_CONNECTION, sock);
                    557:                        }
                    558:                        break;
                    559:                case AUTH_CONNECTION:
                    560:                        if (buffer_len(&sockets[i].output) > 0 &&
                    561:                            FD_ISSET(sockets[i].fd, writeset)) {
                    562:                                len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
                    563:                                         buffer_len(&sockets[i].output));
                    564:                                if (len <= 0) {
                    565:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    566:                                        close(sockets[i].fd);
                    567:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       568:                                        buffer_free(&sockets[i].input);
                    569:                                        buffer_free(&sockets[i].output);
1.21      markus    570:                                        break;
                    571:                                }
                    572:                                buffer_consume(&sockets[i].output, len);
                    573:                        }
                    574:                        if (FD_ISSET(sockets[i].fd, readset)) {
                    575:                                len = read(sockets[i].fd, buf, sizeof(buf));
                    576:                                if (len <= 0) {
                    577:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    578:                                        close(sockets[i].fd);
                    579:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       580:                                        buffer_free(&sockets[i].input);
                    581:                                        buffer_free(&sockets[i].output);
1.21      markus    582:                                        break;
                    583:                                }
                    584:                                buffer_append(&sockets[i].input, buf, len);
                    585:                                process_message(&sockets[i]);
                    586:                        }
                    587:                        break;
                    588:                default:
                    589:                        fatal("Unknown type %d", sockets[i].type);
                    590:                }
1.1       deraadt   591: }
                    592:
1.6       deraadt   593: void
1.2       provos    594: check_parent_exists(int sig)
1.1       deraadt   595: {
1.29      deraadt   596:        if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21      markus    597:                /* printf("Parent has died - Authentication agent exiting.\n"); */
                    598:                exit(1);
                    599:        }
                    600:        signal(SIGALRM, check_parent_exists);
                    601:        alarm(10);
1.1       deraadt   602: }
                    603:
1.15      markus    604: void
                    605: cleanup_socket(void)
                    606: {
1.21      markus    607:        remove(socket_name);
                    608:        rmdir(socket_dir);
1.10      markus    609: }
                    610:
1.15      markus    611: void
                    612: cleanup_exit(int i)
                    613: {
1.21      markus    614:        cleanup_socket();
                    615:        exit(i);
1.15      markus    616: }
                    617:
                    618: void
                    619: usage()
                    620: {
1.21      markus    621:        fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
                    622:        fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
                    623:                __progname);
                    624:        exit(1);
1.15      markus    625: }
                    626:
1.2       provos    627: int
                    628: main(int ac, char **av)
1.1       deraadt   629: {
1.21      markus    630:        fd_set readset, writeset;
                    631:        int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
                    632:        struct sockaddr_un sunaddr;
                    633:        pid_t pid;
                    634:        char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
                    635:
                    636:        /* check if RSA support exists */
                    637:        if (rsa_alive() == 0) {
                    638:                fprintf(stderr,
                    639:                        "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",
                    640:                        __progname);
                    641:                exit(1);
                    642:        }
                    643:        while ((ch = getopt(ac, av, "cks")) != -1) {
                    644:                switch (ch) {
                    645:                case 'c':
                    646:                        if (s_flag)
                    647:                                usage();
                    648:                        c_flag++;
                    649:                        break;
                    650:                case 'k':
                    651:                        k_flag++;
                    652:                        break;
                    653:                case 's':
                    654:                        if (c_flag)
                    655:                                usage();
                    656:                        s_flag++;
                    657:                        break;
                    658:                default:
                    659:                        usage();
                    660:                }
                    661:        }
                    662:        ac -= optind;
                    663:        av += optind;
                    664:
                    665:        if (ac > 0 && (c_flag || k_flag || s_flag))
                    666:                usage();
                    667:
                    668:        if (ac == 0 && !c_flag && !k_flag && !s_flag) {
                    669:                shell = getenv("SHELL");
                    670:                if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
                    671:                        c_flag = 1;
                    672:        }
                    673:        if (k_flag) {
                    674:                pidstr = getenv(SSH_AGENTPID_ENV_NAME);
                    675:                if (pidstr == NULL) {
                    676:                        fprintf(stderr, "%s not set, cannot kill agent\n",
                    677:                                SSH_AGENTPID_ENV_NAME);
                    678:                        exit(1);
                    679:                }
                    680:                pid = atoi(pidstr);
                    681:                if (pid < 1) {  /* XXX PID_MAX check too */
1.29      deraadt   682:                /* Yes, PID_MAX check please */
1.21      markus    683:                        fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
                    684:                                SSH_AGENTPID_ENV_NAME, pidstr);
                    685:                        exit(1);
                    686:                }
                    687:                if (kill(pid, SIGTERM) == -1) {
                    688:                        perror("kill");
                    689:                        exit(1);
                    690:                }
                    691:                format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
                    692:                printf(format, SSH_AUTHSOCKET_ENV_NAME);
                    693:                printf(format, SSH_AGENTPID_ENV_NAME);
                    694:                printf("echo Agent pid %d killed;\n", pid);
                    695:                exit(0);
                    696:        }
                    697:        parent_pid = getpid();
                    698:
                    699:        /* Create private directory for agent socket */
                    700:        strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
                    701:        if (mkdtemp(socket_dir) == NULL) {
                    702:                perror("mkdtemp: private socket dir");
                    703:                exit(1);
                    704:        }
                    705:        snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
                    706:                 parent_pid);
                    707:
1.23      markus    708:        /*
                    709:         * Create socket early so it will exist before command gets run from
                    710:         * the parent.
                    711:         */
1.21      markus    712:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                    713:        if (sock < 0) {
                    714:                perror("socket");
                    715:                cleanup_exit(1);
                    716:        }
                    717:        memset(&sunaddr, 0, sizeof(sunaddr));
                    718:        sunaddr.sun_family = AF_UNIX;
                    719:        strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
                    720:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
                    721:                perror("bind");
                    722:                cleanup_exit(1);
                    723:        }
                    724:        if (listen(sock, 5) < 0) {
                    725:                perror("listen");
                    726:                cleanup_exit(1);
                    727:        }
1.23      markus    728:        /*
                    729:         * Fork, and have the parent execute the command, if any, or present
                    730:         * the socket data.  The child continues as the authentication agent.
                    731:         */
1.21      markus    732:        pid = fork();
                    733:        if (pid == -1) {
                    734:                perror("fork");
                    735:                exit(1);
                    736:        }
                    737:        if (pid != 0) {         /* Parent - execute the given command. */
                    738:                close(sock);
                    739:                snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
                    740:                if (ac == 0) {
                    741:                        format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                    742:                        printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
                    743:                               SSH_AUTHSOCKET_ENV_NAME);
                    744:                        printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
                    745:                               SSH_AGENTPID_ENV_NAME);
                    746:                        printf("echo Agent pid %d;\n", pid);
                    747:                        exit(0);
                    748:                }
                    749:                setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
                    750:                setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
                    751:                execvp(av[0], av);
                    752:                perror(av[0]);
                    753:                exit(1);
                    754:        }
                    755:        close(0);
                    756:        close(1);
                    757:        close(2);
                    758:
                    759:        if (setsid() == -1) {
                    760:                perror("setsid");
                    761:                cleanup_exit(1);
                    762:        }
                    763:        if (atexit(cleanup_socket) < 0) {
                    764:                perror("atexit");
                    765:                cleanup_exit(1);
                    766:        }
                    767:        new_socket(AUTH_SOCKET, sock);
                    768:        if (ac > 0) {
                    769:                signal(SIGALRM, check_parent_exists);
                    770:                alarm(10);
                    771:        }
1.33      markus    772:        idtab_init();
1.21      markus    773:        signal(SIGINT, SIG_IGN);
                    774:        signal(SIGPIPE, SIG_IGN);
1.28      markus    775:        signal(SIGHUP, cleanup_exit);
                    776:        signal(SIGTERM, cleanup_exit);
1.21      markus    777:        while (1) {
                    778:                FD_ZERO(&readset);
                    779:                FD_ZERO(&writeset);
                    780:                prepare_select(&readset, &writeset);
                    781:                if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
                    782:                        if (errno == EINTR)
                    783:                                continue;
                    784:                        exit(1);
                    785:                }
                    786:                after_select(&readset, &writeset);
1.15      markus    787:        }
1.21      markus    788:        /* NOTREACHED */
1.1       deraadt   789: }