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

1.31.2.1! jason       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.31.2.1! jason       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.31.2.1! jason      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.31.2.1! jason      26: #include <openssl/evp.h>
1.27      markus     27: #include <openssl/md5.h>
1.31.2.1! jason      28: #include <openssl/dsa.h>
        !            29: #include <openssl/rsa.h>
        !            30: #include "key.h"
        !            31: #include "authfd.h"
        !            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.31.2.1! jason      48:        Key *key;
1.21      markus     49:        char *comment;
1.1       deraadt    50: } Identity;
                     51:
1.31.2.1! jason      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.31.2.1! jason      72: idtab_init(void)
1.1       deraadt    73: {
1.31.2.1! jason      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.31.2.1! jason     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.31.2.1! jason     138: /* ssh1 only */
1.2       provos    139: void
1.31.2.1! jason     140: process_authentication_challenge1(SocketEntry *e)
1.1       deraadt   141: {
1.31.2.1! jason     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.31.2.1! jason     151:        key = key_new(KEY_RSA);
1.21      markus    152:        challenge = BN_new();
1.31.2.1! jason     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.31.2.1! jason     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.31.2.1! jason     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.31.2.1! jason     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);
        !           218:
        !           219:        key = dsa_key_from_blob(blob, blen);
        !           220:        if (key != NULL) {
        !           221:                private = lookup_private_key(key, NULL, 2);
        !           222:                if (private != NULL)
        !           223:                        ok = dsa_sign(private, &signature, &slen, data, dlen);
        !           224:        }
        !           225:        key_free(key);
        !           226:        buffer_init(&msg);
        !           227:        if (ok == 0) {
        !           228:                buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
        !           229:                buffer_put_string(&msg, signature, slen);
        !           230:        } else {
        !           231:                buffer_put_char(&msg, SSH_AGENT_FAILURE);
        !           232:        }
        !           233:        buffer_put_int(&e->output, buffer_len(&msg));
1.21      markus    234:        buffer_append(&e->output, buffer_ptr(&msg),
1.31.2.1! jason     235:            buffer_len(&msg));
1.21      markus    236:        buffer_free(&msg);
1.31.2.1! jason     237:        xfree(data);
        !           238:        xfree(blob);
        !           239:        if (signature != NULL)
        !           240:                xfree(signature);
1.1       deraadt   241: }
                    242:
1.31.2.1! jason     243: /* shared */
1.2       provos    244: void
1.31.2.1! jason     245: process_remove_identity(SocketEntry *e, int version)
1.1       deraadt   246: {
1.31.2.1! jason     247:        Key *key = NULL, *private;
        !           248:        unsigned char *blob;
        !           249:        unsigned int blen;
1.21      markus    250:        unsigned int bits;
1.31.2.1! jason     251:        int success = 0;
1.21      markus    252:
1.31.2.1! jason     253:        switch(version){
        !           254:        case 1:
        !           255:                key = key_new(KEY_RSA);
        !           256:                bits = buffer_get_int(&e->input);
        !           257:                buffer_get_bignum(&e->input, key->rsa->e);
        !           258:                buffer_get_bignum(&e->input, key->rsa->n);
        !           259:
        !           260:                if (bits != key_size(key))
        !           261:                        log("Warning: identity keysize mismatch: actual %d, announced %d",
        !           262:                              key_size(key), bits);
        !           263:                break;
        !           264:        case 2:
        !           265:                blob = buffer_get_string(&e->input, &blen);
        !           266:                key = dsa_key_from_blob(blob, blen);
        !           267:                xfree(blob);
        !           268:                break;
        !           269:        }
        !           270:        if (key != NULL) {
        !           271:                int idx;
        !           272:                private = lookup_private_key(key, &idx, version);
        !           273:                if (private != NULL) {
1.23      markus    274:                        /*
                    275:                         * We have this key.  Free the old key.  Since we
                    276:                         * don\'t want to leave empty slots in the middle of
                    277:                         * the array, we actually free the key there and copy
                    278:                         * data from the last entry.
                    279:                         */
1.31.2.1! jason     280:                        Idtab *tab = idtab_lookup(version);
        !           281:                        key_free(tab->identities[idx].key);
        !           282:                        xfree(tab->identities[idx].comment);
        !           283:                        if (idx != tab->nentries)
        !           284:                                tab->identities[idx] = tab->identities[tab->nentries];
        !           285:                        tab->nentries--;
        !           286:                        success = 1;
1.21      markus    287:                }
1.31.2.1! jason     288:                key_free(key);
        !           289:        }
1.1       deraadt   290:        buffer_put_int(&e->output, 1);
1.31.2.1! jason     291:        buffer_put_char(&e->output,
        !           292:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   293: }
                    294:
1.2       provos    295: void
1.31.2.1! jason     296: process_remove_all_identities(SocketEntry *e, int version)
1.1       deraadt   297: {
1.21      markus    298:        unsigned int i;
1.31.2.1! jason     299:        Idtab *tab = idtab_lookup(version);
1.21      markus    300:
                    301:        /* Loop over all identities and clear the keys. */
1.31.2.1! jason     302:        for (i = 0; i < tab->nentries; i++) {
        !           303:                key_free(tab->identities[i].key);
        !           304:                xfree(tab->identities[i].comment);
1.21      markus    305:        }
                    306:
                    307:        /* Mark that there are no identities. */
1.31.2.1! jason     308:        tab->nentries = 0;
1.21      markus    309:
                    310:        /* Send success. */
                    311:        buffer_put_int(&e->output, 1);
                    312:        buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
                    313:        return;
1.1       deraadt   314: }
                    315:
1.2       provos    316: void
1.31.2.1! jason     317: process_add_identity(SocketEntry *e, int version)
1.1       deraadt   318: {
1.31.2.1! jason     319:        Key *k = NULL;
        !           320:        RSA *rsa;
1.21      markus    321:        BIGNUM *aux;
                    322:        BN_CTX *ctx;
1.31.2.1! jason     323:        char *type;
        !           324:        char *comment;
        !           325:        int success = 0;
        !           326:        Idtab *tab = idtab_lookup(version);
1.21      markus    327:
1.31.2.1! jason     328:        switch (version) {
        !           329:        case 1:
        !           330:                k = key_new(KEY_RSA);
        !           331:                rsa = k->rsa;
        !           332:
        !           333:                /* allocate mem for private key */
        !           334:                /* XXX rsa->n and rsa->e are already allocated */
        !           335:                rsa->d = BN_new();
        !           336:                rsa->iqmp = BN_new();
        !           337:                rsa->q = BN_new();
        !           338:                rsa->p = BN_new();
        !           339:                rsa->dmq1 = BN_new();
        !           340:                rsa->dmp1 = BN_new();
        !           341:
        !           342:                buffer_get_int(&e->input);               /* ignored */
        !           343:
        !           344:                buffer_get_bignum(&e->input, rsa->n);
        !           345:                buffer_get_bignum(&e->input, rsa->e);
        !           346:                buffer_get_bignum(&e->input, rsa->d);
        !           347:                buffer_get_bignum(&e->input, rsa->iqmp);
        !           348:
        !           349:                /* SSH and SSL have p and q swapped */
        !           350:                buffer_get_bignum(&e->input, rsa->q);   /* p */
        !           351:                buffer_get_bignum(&e->input, rsa->p);   /* q */
        !           352:
        !           353:                /* Generate additional parameters */
        !           354:                aux = BN_new();
        !           355:                ctx = BN_CTX_new();
1.21      markus    356:
1.31.2.1! jason     357:                BN_sub(aux, rsa->q, BN_value_one());
        !           358:                BN_mod(rsa->dmq1, rsa->d, aux, ctx);
1.21      markus    359:
1.31.2.1! jason     360:                BN_sub(aux, rsa->p, BN_value_one());
        !           361:                BN_mod(rsa->dmp1, rsa->d, aux, ctx);
        !           362:
        !           363:                BN_clear_free(aux);
        !           364:                BN_CTX_free(ctx);
        !           365:
        !           366:                break;
        !           367:        case 2:
        !           368:                type = buffer_get_string(&e->input, NULL);
        !           369:                if (strcmp(type, KEX_DSS)) {
        !           370:                        buffer_clear(&e->input);
        !           371:                        xfree(type);
        !           372:                        goto send;
1.21      markus    373:                }
1.31.2.1! jason     374:                xfree(type);
        !           375:
        !           376:                k = key_new(KEY_DSA);
        !           377:
        !           378:                /* allocate mem for private key */
        !           379:                k->dsa->priv_key = BN_new();
1.1       deraadt   380:
1.31.2.1! jason     381:                buffer_get_bignum2(&e->input, k->dsa->p);
        !           382:                buffer_get_bignum2(&e->input, k->dsa->q);
        !           383:                buffer_get_bignum2(&e->input, k->dsa->g);
        !           384:                buffer_get_bignum2(&e->input, k->dsa->pub_key);
        !           385:                buffer_get_bignum2(&e->input, k->dsa->priv_key);
        !           386:
        !           387:                break;
        !           388:        }
        !           389:
        !           390:        comment = buffer_get_string(&e->input, NULL);
        !           391:        if (k == NULL) {
        !           392:                xfree(comment);
        !           393:                goto send;
        !           394:        }
        !           395:        success = 1;
        !           396:        if (lookup_private_key(k, NULL, version) == NULL) {
        !           397:                if (tab->nentries == 0)
        !           398:                        tab->identities = xmalloc(sizeof(Identity));
        !           399:                else
        !           400:                        tab->identities = xrealloc(tab->identities,
        !           401:                            (tab->nentries + 1) * sizeof(Identity));
        !           402:                tab->identities[tab->nentries].key = k;
        !           403:                tab->identities[tab->nentries].comment = comment;
        !           404:                /* Increment the number of identities. */
        !           405:                tab->nentries++;
        !           406:        } else {
        !           407:                key_free(k);
        !           408:                xfree(comment);
        !           409:        }
        !           410: send:
1.1       deraadt   411:        buffer_put_int(&e->output, 1);
1.31.2.1! jason     412:        buffer_put_char(&e->output,
        !           413:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   414: }
                    415:
1.31.2.1! jason     416: /* dispatch incoming messages */
        !           417:
1.2       provos    418: void
                    419: process_message(SocketEntry *e)
1.1       deraadt   420: {
1.21      markus    421:        unsigned int msg_len;
                    422:        unsigned int type;
                    423:        unsigned char *cp;
                    424:        if (buffer_len(&e->input) < 5)
                    425:                return;         /* Incomplete message. */
                    426:        cp = (unsigned char *) buffer_ptr(&e->input);
                    427:        msg_len = GET_32BIT(cp);
                    428:        if (msg_len > 256 * 1024) {
                    429:                shutdown(e->fd, SHUT_RDWR);
                    430:                close(e->fd);
                    431:                e->type = AUTH_UNUSED;
                    432:                return;
                    433:        }
                    434:        if (buffer_len(&e->input) < msg_len + 4)
                    435:                return;
                    436:        buffer_consume(&e->input, 4);
                    437:        type = buffer_get_char(&e->input);
                    438:
                    439:        switch (type) {
1.31.2.1! jason     440:        /* ssh1 */
1.21      markus    441:        case SSH_AGENTC_RSA_CHALLENGE:
1.31.2.1! jason     442:                process_authentication_challenge1(e);
        !           443:                break;
        !           444:        case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
        !           445:                process_request_identities(e, 1);
1.21      markus    446:                break;
                    447:        case SSH_AGENTC_ADD_RSA_IDENTITY:
1.31.2.1! jason     448:                process_add_identity(e, 1);
1.21      markus    449:                break;
                    450:        case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.31.2.1! jason     451:                process_remove_identity(e, 1);
1.21      markus    452:                break;
                    453:        case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.31.2.1! jason     454:                process_remove_all_identities(e, 1);
        !           455:                break;
        !           456:        /* ssh2 */
        !           457:        case SSH2_AGENTC_SIGN_REQUEST:
        !           458:                process_sign_request2(e);
        !           459:                break;
        !           460:        case SSH2_AGENTC_REQUEST_IDENTITIES:
        !           461:                process_request_identities(e, 2);
        !           462:                break;
        !           463:        case SSH2_AGENTC_ADD_IDENTITY:
        !           464:                process_add_identity(e, 2);
        !           465:                break;
        !           466:        case SSH2_AGENTC_REMOVE_IDENTITY:
        !           467:                process_remove_identity(e, 2);
        !           468:                break;
        !           469:        case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
        !           470:                process_remove_all_identities(e, 2);
1.21      markus    471:                break;
                    472:        default:
                    473:                /* Unknown message.  Respond with failure. */
                    474:                error("Unknown message %d", type);
                    475:                buffer_clear(&e->input);
                    476:                buffer_put_int(&e->output, 1);
                    477:                buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    478:                break;
                    479:        }
1.1       deraadt   480: }
                    481:
1.2       provos    482: void
                    483: new_socket(int type, int fd)
1.1       deraadt   484: {
1.21      markus    485:        unsigned int i, old_alloc;
                    486:        if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
                    487:                error("fcntl O_NONBLOCK: %s", strerror(errno));
                    488:
                    489:        if (fd > max_fd)
                    490:                max_fd = fd;
                    491:
                    492:        for (i = 0; i < sockets_alloc; i++)
                    493:                if (sockets[i].type == AUTH_UNUSED) {
                    494:                        sockets[i].fd = fd;
                    495:                        sockets[i].type = type;
                    496:                        buffer_init(&sockets[i].input);
                    497:                        buffer_init(&sockets[i].output);
                    498:                        return;
                    499:                }
                    500:        old_alloc = sockets_alloc;
                    501:        sockets_alloc += 10;
                    502:        if (sockets)
                    503:                sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
                    504:        else
                    505:                sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
                    506:        for (i = old_alloc; i < sockets_alloc; i++)
                    507:                sockets[i].type = AUTH_UNUSED;
                    508:        sockets[old_alloc].type = type;
                    509:        sockets[old_alloc].fd = fd;
                    510:        buffer_init(&sockets[old_alloc].input);
                    511:        buffer_init(&sockets[old_alloc].output);
1.1       deraadt   512: }
                    513:
1.2       provos    514: void
                    515: prepare_select(fd_set *readset, fd_set *writeset)
1.1       deraadt   516: {
1.21      markus    517:        unsigned int i;
                    518:        for (i = 0; i < sockets_alloc; i++)
                    519:                switch (sockets[i].type) {
                    520:                case AUTH_SOCKET:
                    521:                case AUTH_CONNECTION:
                    522:                        FD_SET(sockets[i].fd, readset);
                    523:                        if (buffer_len(&sockets[i].output) > 0)
                    524:                                FD_SET(sockets[i].fd, writeset);
                    525:                        break;
                    526:                case AUTH_UNUSED:
                    527:                        break;
                    528:                default:
                    529:                        fatal("Unknown socket type %d", sockets[i].type);
                    530:                        break;
                    531:                }
                    532: }
                    533:
1.28      markus    534: void
1.21      markus    535: after_select(fd_set *readset, fd_set *writeset)
                    536: {
                    537:        unsigned int i;
                    538:        int len, sock;
1.26      markus    539:        socklen_t slen;
1.21      markus    540:        char buf[1024];
                    541:        struct sockaddr_un sunaddr;
                    542:
                    543:        for (i = 0; i < sockets_alloc; i++)
                    544:                switch (sockets[i].type) {
                    545:                case AUTH_UNUSED:
                    546:                        break;
                    547:                case AUTH_SOCKET:
                    548:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.26      markus    549:                                slen = sizeof(sunaddr);
                    550:                                sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &slen);
1.21      markus    551:                                if (sock < 0) {
                    552:                                        perror("accept from AUTH_SOCKET");
                    553:                                        break;
                    554:                                }
                    555:                                new_socket(AUTH_CONNECTION, sock);
                    556:                        }
                    557:                        break;
                    558:                case AUTH_CONNECTION:
                    559:                        if (buffer_len(&sockets[i].output) > 0 &&
                    560:                            FD_ISSET(sockets[i].fd, writeset)) {
                    561:                                len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
                    562:                                         buffer_len(&sockets[i].output));
                    563:                                if (len <= 0) {
                    564:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    565:                                        close(sockets[i].fd);
                    566:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       567:                                        buffer_free(&sockets[i].input);
                    568:                                        buffer_free(&sockets[i].output);
1.21      markus    569:                                        break;
                    570:                                }
                    571:                                buffer_consume(&sockets[i].output, len);
                    572:                        }
                    573:                        if (FD_ISSET(sockets[i].fd, readset)) {
                    574:                                len = read(sockets[i].fd, buf, sizeof(buf));
                    575:                                if (len <= 0) {
                    576:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    577:                                        close(sockets[i].fd);
                    578:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       579:                                        buffer_free(&sockets[i].input);
                    580:                                        buffer_free(&sockets[i].output);
1.21      markus    581:                                        break;
                    582:                                }
                    583:                                buffer_append(&sockets[i].input, buf, len);
                    584:                                process_message(&sockets[i]);
                    585:                        }
                    586:                        break;
                    587:                default:
                    588:                        fatal("Unknown type %d", sockets[i].type);
                    589:                }
1.1       deraadt   590: }
                    591:
1.6       deraadt   592: void
1.2       provos    593: check_parent_exists(int sig)
1.1       deraadt   594: {
1.29      deraadt   595:        if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1.21      markus    596:                /* printf("Parent has died - Authentication agent exiting.\n"); */
                    597:                exit(1);
                    598:        }
                    599:        signal(SIGALRM, check_parent_exists);
                    600:        alarm(10);
1.1       deraadt   601: }
                    602:
1.15      markus    603: void
                    604: cleanup_socket(void)
                    605: {
1.21      markus    606:        remove(socket_name);
                    607:        rmdir(socket_dir);
1.10      markus    608: }
                    609:
1.15      markus    610: void
                    611: cleanup_exit(int i)
                    612: {
1.21      markus    613:        cleanup_socket();
                    614:        exit(i);
1.15      markus    615: }
                    616:
                    617: void
                    618: usage()
                    619: {
1.21      markus    620:        fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
                    621:        fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
                    622:                __progname);
                    623:        exit(1);
1.15      markus    624: }
                    625:
1.2       provos    626: int
                    627: main(int ac, char **av)
1.1       deraadt   628: {
1.21      markus    629:        fd_set readset, writeset;
                    630:        int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
                    631:        struct sockaddr_un sunaddr;
                    632:        pid_t pid;
                    633:        char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
                    634:
                    635:        /* check if RSA support exists */
                    636:        if (rsa_alive() == 0) {
                    637:                fprintf(stderr,
                    638:                        "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",
                    639:                        __progname);
                    640:                exit(1);
                    641:        }
                    642:        while ((ch = getopt(ac, av, "cks")) != -1) {
                    643:                switch (ch) {
                    644:                case 'c':
                    645:                        if (s_flag)
                    646:                                usage();
                    647:                        c_flag++;
                    648:                        break;
                    649:                case 'k':
                    650:                        k_flag++;
                    651:                        break;
                    652:                case 's':
                    653:                        if (c_flag)
                    654:                                usage();
                    655:                        s_flag++;
                    656:                        break;
                    657:                default:
                    658:                        usage();
                    659:                }
                    660:        }
                    661:        ac -= optind;
                    662:        av += optind;
                    663:
                    664:        if (ac > 0 && (c_flag || k_flag || s_flag))
                    665:                usage();
                    666:
                    667:        if (ac == 0 && !c_flag && !k_flag && !s_flag) {
                    668:                shell = getenv("SHELL");
                    669:                if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
                    670:                        c_flag = 1;
                    671:        }
                    672:        if (k_flag) {
                    673:                pidstr = getenv(SSH_AGENTPID_ENV_NAME);
                    674:                if (pidstr == NULL) {
                    675:                        fprintf(stderr, "%s not set, cannot kill agent\n",
                    676:                                SSH_AGENTPID_ENV_NAME);
                    677:                        exit(1);
                    678:                }
                    679:                pid = atoi(pidstr);
                    680:                if (pid < 1) {  /* XXX PID_MAX check too */
1.29      deraadt   681:                /* Yes, PID_MAX check please */
1.21      markus    682:                        fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
                    683:                                SSH_AGENTPID_ENV_NAME, pidstr);
                    684:                        exit(1);
                    685:                }
                    686:                if (kill(pid, SIGTERM) == -1) {
                    687:                        perror("kill");
                    688:                        exit(1);
                    689:                }
                    690:                format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
                    691:                printf(format, SSH_AUTHSOCKET_ENV_NAME);
                    692:                printf(format, SSH_AGENTPID_ENV_NAME);
                    693:                printf("echo Agent pid %d killed;\n", pid);
                    694:                exit(0);
                    695:        }
                    696:        parent_pid = getpid();
                    697:
                    698:        /* Create private directory for agent socket */
                    699:        strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
                    700:        if (mkdtemp(socket_dir) == NULL) {
                    701:                perror("mkdtemp: private socket dir");
                    702:                exit(1);
                    703:        }
                    704:        snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
                    705:                 parent_pid);
                    706:
1.23      markus    707:        /*
                    708:         * Create socket early so it will exist before command gets run from
                    709:         * the parent.
                    710:         */
1.21      markus    711:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                    712:        if (sock < 0) {
                    713:                perror("socket");
                    714:                cleanup_exit(1);
                    715:        }
                    716:        memset(&sunaddr, 0, sizeof(sunaddr));
                    717:        sunaddr.sun_family = AF_UNIX;
                    718:        strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
                    719:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
                    720:                perror("bind");
                    721:                cleanup_exit(1);
                    722:        }
                    723:        if (listen(sock, 5) < 0) {
                    724:                perror("listen");
                    725:                cleanup_exit(1);
                    726:        }
1.23      markus    727:        /*
                    728:         * Fork, and have the parent execute the command, if any, or present
                    729:         * the socket data.  The child continues as the authentication agent.
                    730:         */
1.21      markus    731:        pid = fork();
                    732:        if (pid == -1) {
                    733:                perror("fork");
                    734:                exit(1);
                    735:        }
                    736:        if (pid != 0) {         /* Parent - execute the given command. */
                    737:                close(sock);
                    738:                snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
                    739:                if (ac == 0) {
                    740:                        format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                    741:                        printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
                    742:                               SSH_AUTHSOCKET_ENV_NAME);
                    743:                        printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
                    744:                               SSH_AGENTPID_ENV_NAME);
                    745:                        printf("echo Agent pid %d;\n", pid);
                    746:                        exit(0);
                    747:                }
                    748:                setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
                    749:                setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
                    750:                execvp(av[0], av);
                    751:                perror(av[0]);
                    752:                exit(1);
                    753:        }
                    754:        close(0);
                    755:        close(1);
                    756:        close(2);
                    757:
                    758:        if (setsid() == -1) {
                    759:                perror("setsid");
                    760:                cleanup_exit(1);
                    761:        }
                    762:        if (atexit(cleanup_socket) < 0) {
                    763:                perror("atexit");
                    764:                cleanup_exit(1);
                    765:        }
                    766:        new_socket(AUTH_SOCKET, sock);
                    767:        if (ac > 0) {
                    768:                signal(SIGALRM, check_parent_exists);
                    769:                alarm(10);
                    770:        }
1.31.2.1! jason     771:        idtab_init();
1.21      markus    772:        signal(SIGINT, SIG_IGN);
                    773:        signal(SIGPIPE, SIG_IGN);
1.28      markus    774:        signal(SIGHUP, cleanup_exit);
                    775:        signal(SIGTERM, cleanup_exit);
1.21      markus    776:        while (1) {
                    777:                FD_ZERO(&readset);
                    778:                FD_ZERO(&writeset);
                    779:                prepare_select(&readset, &writeset);
                    780:                if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
                    781:                        if (errno == EINTR)
                    782:                                continue;
                    783:                        exit(1);
                    784:                }
                    785:                after_select(&readset, &writeset);
1.15      markus    786:        }
1.21      markus    787:        /* NOTREACHED */
1.1       deraadt   788: }