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

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