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

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.72.2.1  jason      37: #include <sys/queue.h>
1.72.2.3! miod       38: RCSID("$OpenBSD: ssh-agent.c,v 1.72.2.2 2002/05/17 00:03:24 miod 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.72.2.1  jason      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.72.2.1  jason      66:        sock_type type;
1.21      markus     67:        Buffer input;
                     68:        Buffer output;
1.72.2.3! miod       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.72.2.1  jason      75: typedef struct identity {
                     76:        TAILQ_ENTRY(identity) next;
1.33      markus     77:        Key *key;
1.21      markus     78:        char *comment;
1.72.2.3! miod       79:        u_int death;
1.1       deraadt    80: } Identity;
                     81:
1.33      markus     82: typedef struct {
                     83:        int nentries;
1.72.2.1  jason      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.72.2.3! miod       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.72.2.1  jason     109:        for (i = 0; i <=2; i++) {
                    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.72.2.3! miod      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.72.2.1  jason     133: static Identity *
                    134: lookup_identity(Key *key, int version)
1.33      markus    135: {
1.72.2.1  jason     136:        Identity *id;
                    137:
1.33      markus    138:        Idtab *tab = idtab_lookup(version);
1.72.2.1  jason     139:        TAILQ_FOREACH(id, &tab->idlist, next) {
                    140:                if (key_equal(key, id->key))
                    141:                        return (id);
1.33      markus    142:        }
1.72.2.1  jason     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.72.2.1  jason     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.72.2.1  jason     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.72.2.1  jason     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.72.2.1  jason     192:        if ((challenge = BN_new()) == NULL)
                    193:                fatal("process_authentication_challenge1: BN_new failed");
1.33      markus    194:
1.72.2.3! miod      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.72.2.3! miod      201:        if (buffer_len(&e->request) == 0)
1.33      markus    202:                goto failure;
1.72.2.3! miod      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.72.2.1  jason     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.72.2.1  jason     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.72.2.3! miod      260:        blob = buffer_get_string(&e->request, &blen);
        !           261:        data = buffer_get_string(&e->request, &dlen);
1.37      markus    262:
1.72.2.3! miod      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.72.2.1  jason     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.72.2.1  jason     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.72.2.1  jason     301:        switch (version) {
1.33      markus    302:        case 1:
1.39      markus    303:                key = key_new(KEY_RSA1);
1.72.2.3! miod      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.72.2.3! miod      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.72.2.1  jason     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.72.2.1  jason     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.72.2.1  jason     349:        Identity *id;
1.21      markus    350:
                    351:        /* Loop over all identities and clear the keys. */
1.72.2.1  jason     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);
1.72.2.3! miod      364: }
        !           365:
        !           366: static void
        !           367: reaper(void)
        !           368: {
        !           369:        Idtab *tab;
        !           370:        Identity *id, *nxt;
        !           371:        int version;
        !           372:        u_int now = time(NULL);
        !           373:
        !           374:        for (version = 1; version < 3; version++) {
        !           375:                tab = idtab_lookup(version);
        !           376:                for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
        !           377:                        nxt = TAILQ_NEXT(id, next);
        !           378:                        if (id->death != 0 && now >= id->death) {
        !           379:                                TAILQ_REMOVE(&tab->idlist, id, next);
        !           380:                                free_identity(id);
        !           381:                                tab->nentries--;
        !           382:                        }
        !           383:                }
        !           384:        }
1.1       deraadt   385: }
                    386:
1.55      itojun    387: static void
1.33      markus    388: process_add_identity(SocketEntry *e, int version)
1.1       deraadt   389: {
1.33      markus    390:        Key *k = NULL;
1.39      markus    391:        char *type_name;
1.33      markus    392:        char *comment;
1.72.2.3! miod      393:        int type, success = 0, death = 0;
1.33      markus    394:        Idtab *tab = idtab_lookup(version);
                    395:
                    396:        switch (version) {
                    397:        case 1:
1.39      markus    398:                k = key_new_private(KEY_RSA1);
1.72.2.3! miod      399:                buffer_get_int(&e->request);                    /* ignored */
        !           400:                buffer_get_bignum(&e->request, k->rsa->n);
        !           401:                buffer_get_bignum(&e->request, k->rsa->e);
        !           402:                buffer_get_bignum(&e->request, k->rsa->d);
        !           403:                buffer_get_bignum(&e->request, k->rsa->iqmp);
1.33      markus    404:
                    405:                /* SSH and SSL have p and q swapped */
1.72.2.3! miod      406:                buffer_get_bignum(&e->request, k->rsa->q);      /* p */
        !           407:                buffer_get_bignum(&e->request, k->rsa->p);      /* q */
1.33      markus    408:
                    409:                /* Generate additional parameters */
1.60      markus    410:                rsa_generate_additional_parameters(k->rsa);
1.33      markus    411:                break;
                    412:        case 2:
1.72.2.3! miod      413:                type_name = buffer_get_string(&e->request, NULL);
1.46      markus    414:                type = key_type_from_name(type_name);
1.39      markus    415:                xfree(type_name);
1.72.2.1  jason     416:                switch (type) {
1.39      markus    417:                case KEY_DSA:
                    418:                        k = key_new_private(type);
1.72.2.3! miod      419:                        buffer_get_bignum2(&e->request, k->dsa->p);
        !           420:                        buffer_get_bignum2(&e->request, k->dsa->q);
        !           421:                        buffer_get_bignum2(&e->request, k->dsa->g);
        !           422:                        buffer_get_bignum2(&e->request, k->dsa->pub_key);
        !           423:                        buffer_get_bignum2(&e->request, k->dsa->priv_key);
1.39      markus    424:                        break;
                    425:                case KEY_RSA:
                    426:                        k = key_new_private(type);
1.72.2.3! miod      427:                        buffer_get_bignum2(&e->request, k->rsa->n);
        !           428:                        buffer_get_bignum2(&e->request, k->rsa->e);
        !           429:                        buffer_get_bignum2(&e->request, k->rsa->d);
        !           430:                        buffer_get_bignum2(&e->request, k->rsa->iqmp);
        !           431:                        buffer_get_bignum2(&e->request, k->rsa->p);
        !           432:                        buffer_get_bignum2(&e->request, k->rsa->q);
1.39      markus    433:
                    434:                        /* Generate additional parameters */
1.60      markus    435:                        rsa_generate_additional_parameters(k->rsa);
1.39      markus    436:                        break;
                    437:                default:
1.72.2.3! miod      438:                        buffer_clear(&e->request);
1.33      markus    439:                        goto send;
1.21      markus    440:                }
1.33      markus    441:                break;
                    442:        }
1.72.2.3! miod      443:        comment = buffer_get_string(&e->request, NULL);
1.33      markus    444:        if (k == NULL) {
                    445:                xfree(comment);
                    446:                goto send;
                    447:        }
                    448:        success = 1;
1.72.2.3! miod      449:        while (buffer_len(&e->request)) {
        !           450:                switch (buffer_get_char(&e->request)) {
        !           451:                case SSH_AGENT_CONSTRAIN_LIFETIME:
        !           452:                        death = time(NULL) + buffer_get_int(&e->request);
        !           453:                        break;
        !           454:                default:
        !           455:                        break;
        !           456:                }
        !           457:        }
1.72.2.1  jason     458:        if (lookup_identity(k, version) == NULL) {
                    459:                Identity *id = xmalloc(sizeof(Identity));
                    460:                id->key = k;
                    461:                id->comment = comment;
1.72.2.3! miod      462:                id->death = death;
1.72.2.1  jason     463:                TAILQ_INSERT_TAIL(&tab->idlist, id, next);
1.33      markus    464:                /* Increment the number of identities. */
                    465:                tab->nentries++;
                    466:        } else {
                    467:                key_free(k);
                    468:                xfree(comment);
                    469:        }
                    470: send:
1.1       deraadt   471:        buffer_put_int(&e->output, 1);
1.33      markus    472:        buffer_put_char(&e->output,
                    473:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
1.1       deraadt   474: }
                    475:
1.72.2.3! miod      476: /* XXX todo: encrypt sensitive data with passphrase */
        !           477: static void
        !           478: process_lock_agent(SocketEntry *e, int lock)
        !           479: {
        !           480:        char *passwd;
        !           481:        int success = 0;
        !           482:
        !           483:        passwd = buffer_get_string(&e->request, NULL);
        !           484:        if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
        !           485:                locked = 0;
        !           486:                memset(lock_passwd, 0, strlen(lock_passwd));
        !           487:                xfree(lock_passwd);
        !           488:                lock_passwd = NULL;
        !           489:                success = 1;
        !           490:        } else if (!locked && lock) {
        !           491:                locked = 1;
        !           492:                lock_passwd = xstrdup(passwd);
        !           493:                success = 1;
        !           494:        }
        !           495:        memset(passwd, 0, strlen(passwd));
        !           496:        xfree(passwd);
        !           497:
        !           498:        buffer_put_int(&e->output, 1);
        !           499:        buffer_put_char(&e->output,
        !           500:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
        !           501: }
        !           502:
        !           503: static void
        !           504: no_identities(SocketEntry *e, u_int type)
        !           505: {
        !           506:        Buffer msg;
        !           507:
        !           508:        buffer_init(&msg);
        !           509:        buffer_put_char(&msg,
        !           510:            (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
        !           511:            SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
        !           512:        buffer_put_int(&msg, 0);
        !           513:        buffer_put_int(&e->output, buffer_len(&msg));
        !           514:        buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
        !           515:        buffer_free(&msg);
        !           516: }
1.59      markus    517:
                    518: #ifdef SMARTCARD
                    519: static void
                    520: process_add_smartcard_key (SocketEntry *e)
                    521: {
1.72.2.2  miod      522:        Identity *id;
1.59      markus    523:        Idtab *tab;
1.72.2.2  miod      524:        Key **keys, *k;
                    525:        char *sc_reader_id = NULL, *pin;
                    526:        int i, version, success = 0;
1.72.2.1  jason     527:
1.72.2.3! miod      528:        sc_reader_id = buffer_get_string(&e->request, NULL);
        !           529:        pin = buffer_get_string(&e->request, NULL);
1.72.2.2  miod      530:        keys = sc_get_keys(sc_reader_id, pin);
1.69      markus    531:        xfree(sc_reader_id);
1.72.2.2  miod      532:        xfree(pin);
1.59      markus    533:
1.72.2.2  miod      534:        if (keys == NULL || keys[0] == NULL) {
                    535:                error("sc_get_keys failed");
1.59      markus    536:                goto send;
                    537:        }
1.72.2.2  miod      538:        for (i = 0; keys[i] != NULL; i++) {
                    539:                k = keys[i];
                    540:                version = k->type == KEY_RSA1 ? 1 : 2;
                    541:                tab = idtab_lookup(version);
                    542:                if (lookup_identity(k, version) == NULL) {
                    543:                        id = xmalloc(sizeof(Identity));
                    544:                        id->key = k;
                    545:                        id->comment = xstrdup("smartcard key");
1.72.2.3! miod      546:                        id->death = 0;
1.72.2.2  miod      547:                        TAILQ_INSERT_TAIL(&tab->idlist, id, next);
                    548:                        tab->nentries++;
                    549:                        success = 1;
                    550:                } else {
                    551:                        key_free(k);
                    552:                }
                    553:                keys[i] = NULL;
1.59      markus    554:        }
1.72.2.2  miod      555:        xfree(keys);
1.59      markus    556: send:
                    557:        buffer_put_int(&e->output, 1);
                    558:        buffer_put_char(&e->output,
                    559:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                    560: }
                    561:
                    562: static void
                    563: process_remove_smartcard_key(SocketEntry *e)
                    564: {
1.72.2.2  miod      565:        Identity *id;
                    566:        Idtab *tab;
                    567:        Key **keys, *k = NULL;
                    568:        char *sc_reader_id = NULL, *pin;
                    569:        int i, version, success = 0;
1.59      markus    570:
1.72.2.3! miod      571:        sc_reader_id = buffer_get_string(&e->request, NULL);
        !           572:        pin = buffer_get_string(&e->request, NULL);
1.72.2.2  miod      573:        keys = sc_get_keys(sc_reader_id, pin);
1.69      markus    574:        xfree(sc_reader_id);
1.72.2.2  miod      575:        xfree(pin);
1.59      markus    576:
1.72.2.2  miod      577:        if (keys == NULL || keys[0] == NULL) {
                    578:                error("sc_get_keys failed");
                    579:                goto send;
                    580:        }
                    581:        for (i = 0; keys[i] != NULL; i++) {
                    582:                k = keys[i];
                    583:                version = k->type == KEY_RSA1 ? 1 : 2;
                    584:                if ((id = lookup_identity(k, version)) != NULL) {
                    585:                        tab = idtab_lookup(version);
1.72.2.3! miod      586:                        TAILQ_REMOVE(&tab->idlist, id, next);
1.59      markus    587:                        tab->nentries--;
1.72.2.1  jason     588:                        free_identity(id);
1.59      markus    589:                        success = 1;
                    590:                }
                    591:                key_free(k);
1.72.2.2  miod      592:                keys[i] = NULL;
1.59      markus    593:        }
1.72.2.2  miod      594:        xfree(keys);
                    595: send:
1.59      markus    596:        buffer_put_int(&e->output, 1);
                    597:        buffer_put_char(&e->output,
                    598:            success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
                    599: }
1.70      jakob     600: #endif /* SMARTCARD */
1.59      markus    601:
1.33      markus    602: /* dispatch incoming messages */
                    603:
1.55      itojun    604: static void
1.2       provos    605: process_message(SocketEntry *e)
1.1       deraadt   606: {
1.45      markus    607:        u_int msg_len;
                    608:        u_int type;
                    609:        u_char *cp;
1.72.2.3! miod      610:
        !           611:        /* kill dead keys */
        !           612:        reaper();
        !           613:
1.21      markus    614:        if (buffer_len(&e->input) < 5)
                    615:                return;         /* Incomplete message. */
1.72.2.1  jason     616:        cp = buffer_ptr(&e->input);
1.21      markus    617:        msg_len = GET_32BIT(cp);
                    618:        if (msg_len > 256 * 1024) {
                    619:                shutdown(e->fd, SHUT_RDWR);
                    620:                close(e->fd);
                    621:                e->type = AUTH_UNUSED;
1.72.2.3! miod      622:                buffer_free(&e->input);
        !           623:                buffer_free(&e->output);
        !           624:                buffer_free(&e->request);
1.21      markus    625:                return;
                    626:        }
                    627:        if (buffer_len(&e->input) < msg_len + 4)
                    628:                return;
1.72.2.3! miod      629:
        !           630:        /* move the current input to e->request */
1.21      markus    631:        buffer_consume(&e->input, 4);
1.72.2.3! miod      632:        buffer_clear(&e->request);
        !           633:        buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
        !           634:        buffer_consume(&e->input, msg_len);
        !           635:        type = buffer_get_char(&e->request);
        !           636:
        !           637:        /* check wheter agent is locked */
        !           638:        if (locked && type != SSH_AGENTC_UNLOCK) {
        !           639:                buffer_clear(&e->request);
        !           640:                switch (type) {
        !           641:                case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
        !           642:                case SSH2_AGENTC_REQUEST_IDENTITIES:
        !           643:                        /* send empty lists */
        !           644:                        no_identities(e, type);
        !           645:                        break;
        !           646:                default:
        !           647:                        /* send a fail message for all other request types */
        !           648:                        buffer_put_int(&e->output, 1);
        !           649:                        buffer_put_char(&e->output, SSH_AGENT_FAILURE);
        !           650:                }
        !           651:                return;
        !           652:        }
1.21      markus    653:
1.59      markus    654:        debug("type %d", type);
1.21      markus    655:        switch (type) {
1.72.2.3! miod      656:        case SSH_AGENTC_LOCK:
        !           657:        case SSH_AGENTC_UNLOCK:
        !           658:                process_lock_agent(e, type == SSH_AGENTC_LOCK);
        !           659:                break;
1.33      markus    660:        /* ssh1 */
                    661:        case SSH_AGENTC_RSA_CHALLENGE:
                    662:                process_authentication_challenge1(e);
                    663:                break;
1.21      markus    664:        case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
1.33      markus    665:                process_request_identities(e, 1);
1.21      markus    666:                break;
                    667:        case SSH_AGENTC_ADD_RSA_IDENTITY:
1.72.2.3! miod      668:        case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
1.33      markus    669:                process_add_identity(e, 1);
1.21      markus    670:                break;
                    671:        case SSH_AGENTC_REMOVE_RSA_IDENTITY:
1.33      markus    672:                process_remove_identity(e, 1);
1.21      markus    673:                break;
                    674:        case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
1.33      markus    675:                process_remove_all_identities(e, 1);
                    676:                break;
                    677:        /* ssh2 */
                    678:        case SSH2_AGENTC_SIGN_REQUEST:
                    679:                process_sign_request2(e);
                    680:                break;
                    681:        case SSH2_AGENTC_REQUEST_IDENTITIES:
                    682:                process_request_identities(e, 2);
                    683:                break;
                    684:        case SSH2_AGENTC_ADD_IDENTITY:
1.72.2.3! miod      685:        case SSH2_AGENTC_ADD_ID_CONSTRAINED:
1.33      markus    686:                process_add_identity(e, 2);
                    687:                break;
                    688:        case SSH2_AGENTC_REMOVE_IDENTITY:
                    689:                process_remove_identity(e, 2);
                    690:                break;
                    691:        case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
                    692:                process_remove_all_identities(e, 2);
1.21      markus    693:                break;
1.59      markus    694: #ifdef SMARTCARD
                    695:        case SSH_AGENTC_ADD_SMARTCARD_KEY:
                    696:                process_add_smartcard_key(e);
1.72.2.1  jason     697:                break;
1.59      markus    698:        case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
                    699:                process_remove_smartcard_key(e);
1.72.2.1  jason     700:                break;
1.70      jakob     701: #endif /* SMARTCARD */
1.21      markus    702:        default:
                    703:                /* Unknown message.  Respond with failure. */
                    704:                error("Unknown message %d", type);
1.72.2.3! miod      705:                buffer_clear(&e->request);
1.21      markus    706:                buffer_put_int(&e->output, 1);
                    707:                buffer_put_char(&e->output, SSH_AGENT_FAILURE);
                    708:                break;
                    709:        }
1.1       deraadt   710: }
                    711:
1.55      itojun    712: static void
1.72.2.1  jason     713: new_socket(sock_type type, int fd)
1.1       deraadt   714: {
1.45      markus    715:        u_int i, old_alloc;
1.21      markus    716:        if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
                    717:                error("fcntl O_NONBLOCK: %s", strerror(errno));
                    718:
                    719:        if (fd > max_fd)
                    720:                max_fd = fd;
                    721:
                    722:        for (i = 0; i < sockets_alloc; i++)
                    723:                if (sockets[i].type == AUTH_UNUSED) {
                    724:                        sockets[i].fd = fd;
                    725:                        sockets[i].type = type;
                    726:                        buffer_init(&sockets[i].input);
                    727:                        buffer_init(&sockets[i].output);
1.72.2.3! miod      728:                        buffer_init(&sockets[i].request);
1.21      markus    729:                        return;
                    730:                }
                    731:        old_alloc = sockets_alloc;
                    732:        sockets_alloc += 10;
                    733:        if (sockets)
                    734:                sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
                    735:        else
                    736:                sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
                    737:        for (i = old_alloc; i < sockets_alloc; i++)
                    738:                sockets[i].type = AUTH_UNUSED;
                    739:        sockets[old_alloc].type = type;
                    740:        sockets[old_alloc].fd = fd;
                    741:        buffer_init(&sockets[old_alloc].input);
                    742:        buffer_init(&sockets[old_alloc].output);
1.72.2.3! miod      743:        buffer_init(&sockets[old_alloc].request);
1.1       deraadt   744: }
                    745:
1.55      itojun    746: static int
1.66      markus    747: prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
1.1       deraadt   748: {
1.46      markus    749:        u_int i, sz;
                    750:        int n = 0;
                    751:
                    752:        for (i = 0; i < sockets_alloc; i++) {
1.21      markus    753:                switch (sockets[i].type) {
                    754:                case AUTH_SOCKET:
                    755:                case AUTH_CONNECTION:
1.46      markus    756:                        n = MAX(n, sockets[i].fd);
1.21      markus    757:                        break;
                    758:                case AUTH_UNUSED:
                    759:                        break;
                    760:                default:
                    761:                        fatal("Unknown socket type %d", sockets[i].type);
                    762:                        break;
                    763:                }
1.46      markus    764:        }
                    765:
                    766:        sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1.66      markus    767:        if (*fdrp == NULL || sz > *nallocp) {
1.46      markus    768:                if (*fdrp)
1.54      stevesk   769:                        xfree(*fdrp);
1.46      markus    770:                if (*fdwp)
1.54      stevesk   771:                        xfree(*fdwp);
1.46      markus    772:                *fdrp = xmalloc(sz);
                    773:                *fdwp = xmalloc(sz);
1.66      markus    774:                *nallocp = sz;
1.46      markus    775:        }
1.66      markus    776:        if (n < *fdl)
                    777:                debug("XXX shrink: %d < %d", n, *fdl);
                    778:        *fdl = n;
1.46      markus    779:        memset(*fdrp, 0, sz);
                    780:        memset(*fdwp, 0, sz);
                    781:
                    782:        for (i = 0; i < sockets_alloc; i++) {
                    783:                switch (sockets[i].type) {
                    784:                case AUTH_SOCKET:
                    785:                case AUTH_CONNECTION:
                    786:                        FD_SET(sockets[i].fd, *fdrp);
                    787:                        if (buffer_len(&sockets[i].output) > 0)
                    788:                                FD_SET(sockets[i].fd, *fdwp);
                    789:                        break;
                    790:                default:
                    791:                        break;
                    792:                }
                    793:        }
                    794:        return (1);
1.21      markus    795: }
                    796:
1.55      itojun    797: static void
1.21      markus    798: after_select(fd_set *readset, fd_set *writeset)
                    799: {
1.45      markus    800:        u_int i;
1.21      markus    801:        int len, sock;
1.26      markus    802:        socklen_t slen;
1.21      markus    803:        char buf[1024];
                    804:        struct sockaddr_un sunaddr;
                    805:
                    806:        for (i = 0; i < sockets_alloc; i++)
                    807:                switch (sockets[i].type) {
                    808:                case AUTH_UNUSED:
                    809:                        break;
                    810:                case AUTH_SOCKET:
                    811:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.26      markus    812:                                slen = sizeof(sunaddr);
1.46      markus    813:                                sock = accept(sockets[i].fd,
                    814:                                    (struct sockaddr *) &sunaddr, &slen);
1.21      markus    815:                                if (sock < 0) {
1.72.2.1  jason     816:                                        error("accept from AUTH_SOCKET: %s",
                    817:                                            strerror(errno));
1.21      markus    818:                                        break;
                    819:                                }
                    820:                                new_socket(AUTH_CONNECTION, sock);
                    821:                        }
                    822:                        break;
                    823:                case AUTH_CONNECTION:
                    824:                        if (buffer_len(&sockets[i].output) > 0 &&
                    825:                            FD_ISSET(sockets[i].fd, writeset)) {
1.52      deraadt   826:                                do {
                    827:                                        len = write(sockets[i].fd,
                    828:                                            buffer_ptr(&sockets[i].output),
                    829:                                            buffer_len(&sockets[i].output));
                    830:                                        if (len == -1 && (errno == EAGAIN ||
                    831:                                            errno == EINTR))
                    832:                                                continue;
                    833:                                        break;
                    834:                                } while (1);
1.21      markus    835:                                if (len <= 0) {
                    836:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    837:                                        close(sockets[i].fd);
                    838:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       839:                                        buffer_free(&sockets[i].input);
                    840:                                        buffer_free(&sockets[i].output);
1.72.2.3! miod      841:                                        buffer_free(&sockets[i].request);
1.21      markus    842:                                        break;
                    843:                                }
                    844:                                buffer_consume(&sockets[i].output, len);
                    845:                        }
                    846:                        if (FD_ISSET(sockets[i].fd, readset)) {
1.52      deraadt   847:                                do {
                    848:                                        len = read(sockets[i].fd, buf, sizeof(buf));
                    849:                                        if (len == -1 && (errno == EAGAIN ||
                    850:                                            errno == EINTR))
                    851:                                                continue;
                    852:                                        break;
                    853:                                } while (1);
1.21      markus    854:                                if (len <= 0) {
                    855:                                        shutdown(sockets[i].fd, SHUT_RDWR);
                    856:                                        close(sockets[i].fd);
                    857:                                        sockets[i].type = AUTH_UNUSED;
1.30      djm       858:                                        buffer_free(&sockets[i].input);
                    859:                                        buffer_free(&sockets[i].output);
1.72.2.3! miod      860:                                        buffer_free(&sockets[i].request);
1.21      markus    861:                                        break;
                    862:                                }
                    863:                                buffer_append(&sockets[i].input, buf, len);
                    864:                                process_message(&sockets[i]);
                    865:                        }
                    866:                        break;
                    867:                default:
                    868:                        fatal("Unknown type %d", sockets[i].type);
                    869:                }
1.1       deraadt   870: }
                    871:
1.55      itojun    872: static void
1.72.2.1  jason     873: cleanup_socket(void *p)
1.15      markus    874: {
1.48      deraadt   875:        if (socket_name[0])
                    876:                unlink(socket_name);
                    877:        if (socket_dir[0])
                    878:                rmdir(socket_dir);
1.10      markus    879: }
                    880:
1.55      itojun    881: static void
1.15      markus    882: cleanup_exit(int i)
                    883: {
1.72.2.1  jason     884:        cleanup_socket(NULL);
1.21      markus    885:        exit(i);
1.15      markus    886: }
                    887:
1.55      itojun    888: static void
1.48      deraadt   889: cleanup_handler(int sig)
                    890: {
1.72.2.1  jason     891:        cleanup_socket(NULL);
1.48      deraadt   892:        _exit(2);
1.68      markus    893: }
                    894:
                    895: static void
                    896: check_parent_exists(int sig)
                    897: {
                    898:        int save_errno = errno;
                    899:
                    900:        if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
                    901:                /* printf("Parent has died - Authentication agent exiting.\n"); */
                    902:                cleanup_handler(sig); /* safe */
                    903:        }
                    904:        signal(SIGALRM, check_parent_exists);
                    905:        alarm(10);
                    906:        errno = save_errno;
1.48      deraadt   907: }
                    908:
1.55      itojun    909: static void
1.50      itojun    910: usage(void)
1.15      markus    911: {
1.72      jakob     912:        fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
1.46      markus    913:            __progname);
1.72      jakob     914:        fprintf(stderr, "Options:\n");
                    915:        fprintf(stderr, "  -c          Generate C-shell commands on stdout.\n");
                    916:        fprintf(stderr, "  -s          Generate Bourne shell commands on stdout.\n");
                    917:        fprintf(stderr, "  -k          Kill the current agent.\n");
                    918:        fprintf(stderr, "  -d          Debug mode.\n");
1.72.2.3! miod      919:        fprintf(stderr, "  -a socket   Bind agent socket to given name.\n");
1.21      markus    920:        exit(1);
1.15      markus    921: }
                    922:
1.2       provos    923: int
                    924: main(int ac, char **av)
1.1       deraadt   925: {
1.66      markus    926:        int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
1.21      markus    927:        struct sockaddr_un sunaddr;
1.41      markus    928:        struct rlimit rlim;
1.21      markus    929:        pid_t pid;
                    930:        char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
1.72.2.3! miod      931:        char *agentsocket = NULL;
1.43      markus    932:        extern int optind;
1.46      markus    933:        fd_set *readsetp = NULL, *writesetp = NULL;
1.53      markus    934:
                    935:        SSLeay_add_all_algorithms();
1.21      markus    936:
1.72.2.3! miod      937:        while ((ch = getopt(ac, av, "cdksa:")) != -1) {
1.21      markus    938:                switch (ch) {
                    939:                case 'c':
                    940:                        if (s_flag)
                    941:                                usage();
                    942:                        c_flag++;
                    943:                        break;
                    944:                case 'k':
                    945:                        k_flag++;
                    946:                        break;
                    947:                case 's':
                    948:                        if (c_flag)
                    949:                                usage();
                    950:                        s_flag++;
                    951:                        break;
1.57      markus    952:                case 'd':
                    953:                        if (d_flag)
                    954:                                usage();
                    955:                        d_flag++;
                    956:                        break;
1.72.2.3! miod      957:                case 'a':
        !           958:                        agentsocket = optarg;
        !           959:                        break;
1.21      markus    960:                default:
                    961:                        usage();
                    962:                }
                    963:        }
                    964:        ac -= optind;
                    965:        av += optind;
                    966:
1.57      markus    967:        if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
1.21      markus    968:                usage();
                    969:
1.72.2.2  miod      970:        if (ac == 0 && !c_flag && !s_flag) {
1.21      markus    971:                shell = getenv("SHELL");
                    972:                if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
                    973:                        c_flag = 1;
                    974:        }
                    975:        if (k_flag) {
                    976:                pidstr = getenv(SSH_AGENTPID_ENV_NAME);
                    977:                if (pidstr == NULL) {
                    978:                        fprintf(stderr, "%s not set, cannot kill agent\n",
1.46      markus    979:                            SSH_AGENTPID_ENV_NAME);
1.21      markus    980:                        exit(1);
                    981:                }
                    982:                pid = atoi(pidstr);
1.46      markus    983:                if (pid < 1) {
1.21      markus    984:                        fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
1.46      markus    985:                            SSH_AGENTPID_ENV_NAME, pidstr);
1.21      markus    986:                        exit(1);
                    987:                }
                    988:                if (kill(pid, SIGTERM) == -1) {
                    989:                        perror("kill");
                    990:                        exit(1);
                    991:                }
                    992:                format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
                    993:                printf(format, SSH_AUTHSOCKET_ENV_NAME);
                    994:                printf(format, SSH_AGENTPID_ENV_NAME);
1.72.2.3! miod      995:                printf("echo Agent pid %ld killed;\n", (long)pid);
1.21      markus    996:                exit(0);
                    997:        }
                    998:        parent_pid = getpid();
                    999:
1.72.2.3! miod     1000:        if (agentsocket == NULL) {
        !          1001:                /* Create private directory for agent socket */
        !          1002:                strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
        !          1003:                if (mkdtemp(socket_dir) == NULL) {
        !          1004:                        perror("mkdtemp: private socket dir");
        !          1005:                        exit(1);
        !          1006:                }
        !          1007:                snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
        !          1008:                    (long)parent_pid);
        !          1009:        } else {
        !          1010:                /* Try to use specified agent socket */
        !          1011:                socket_dir[0] = '\0';
        !          1012:                strlcpy(socket_name, agentsocket, sizeof socket_name);
1.21      markus   1013:        }
                   1014:
1.23      markus   1015:        /*
                   1016:         * Create socket early so it will exist before command gets run from
                   1017:         * the parent.
                   1018:         */
1.21      markus   1019:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                   1020:        if (sock < 0) {
                   1021:                perror("socket");
                   1022:                cleanup_exit(1);
                   1023:        }
                   1024:        memset(&sunaddr, 0, sizeof(sunaddr));
                   1025:        sunaddr.sun_family = AF_UNIX;
                   1026:        strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
                   1027:        if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
                   1028:                perror("bind");
                   1029:                cleanup_exit(1);
                   1030:        }
                   1031:        if (listen(sock, 5) < 0) {
                   1032:                perror("listen");
                   1033:                cleanup_exit(1);
                   1034:        }
1.46      markus   1035:
1.23      markus   1036:        /*
                   1037:         * Fork, and have the parent execute the command, if any, or present
                   1038:         * the socket data.  The child continues as the authentication agent.
                   1039:         */
1.57      markus   1040:        if (d_flag) {
                   1041:                log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
                   1042:                format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                   1043:                printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
                   1044:                    SSH_AUTHSOCKET_ENV_NAME);
1.72.2.3! miod     1045:                printf("echo Agent pid %ld;\n", (long)parent_pid);
1.57      markus   1046:                goto skip;
                   1047:        }
1.21      markus   1048:        pid = fork();
                   1049:        if (pid == -1) {
                   1050:                perror("fork");
1.72.2.1  jason    1051:                cleanup_exit(1);
1.21      markus   1052:        }
                   1053:        if (pid != 0) {         /* Parent - execute the given command. */
                   1054:                close(sock);
1.72.2.3! miod     1055:                snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
1.21      markus   1056:                if (ac == 0) {
                   1057:                        format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
                   1058:                        printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1.46      markus   1059:                            SSH_AUTHSOCKET_ENV_NAME);
1.21      markus   1060:                        printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
1.46      markus   1061:                            SSH_AGENTPID_ENV_NAME);
1.72.2.3! miod     1062:                        printf("echo Agent pid %ld;\n", (long)pid);
1.21      markus   1063:                        exit(0);
                   1064:                }
1.36      deraadt  1065:                if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
                   1066:                    setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
                   1067:                        perror("setenv");
                   1068:                        exit(1);
                   1069:                }
1.21      markus   1070:                execvp(av[0], av);
                   1071:                perror(av[0]);
                   1072:                exit(1);
                   1073:        }
1.72.2.1  jason    1074:        /* child */
                   1075:        log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
1.67      stevesk  1076:
                   1077:        if (setsid() == -1) {
1.72.2.1  jason    1078:                error("setsid: %s", strerror(errno));
1.67      stevesk  1079:                cleanup_exit(1);
                   1080:        }
                   1081:
                   1082:        (void)chdir("/");
1.21      markus   1083:        close(0);
                   1084:        close(1);
                   1085:        close(2);
                   1086:
1.41      markus   1087:        /* deny core dumps, since memory contains unencrypted private keys */
                   1088:        rlim.rlim_cur = rlim.rlim_max = 0;
                   1089:        if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
1.72.2.1  jason    1090:                error("setrlimit RLIMIT_CORE: %s", strerror(errno));
1.21      markus   1091:                cleanup_exit(1);
                   1092:        }
1.57      markus   1093:
                   1094: skip:
1.72.2.1  jason    1095:        fatal_add_cleanup(cleanup_socket, NULL);
1.21      markus   1096:        new_socket(AUTH_SOCKET, sock);
                   1097:        if (ac > 0) {
                   1098:                signal(SIGALRM, check_parent_exists);
                   1099:                alarm(10);
                   1100:        }
1.33      markus   1101:        idtab_init();
1.61      markus   1102:        if (!d_flag)
1.57      markus   1103:                signal(SIGINT, SIG_IGN);
1.61      markus   1104:        signal(SIGPIPE, SIG_IGN);
1.48      deraadt  1105:        signal(SIGHUP, cleanup_handler);
                   1106:        signal(SIGTERM, cleanup_handler);
1.66      markus   1107:        nalloc = 0;
                   1108:
1.21      markus   1109:        while (1) {
1.66      markus   1110:                prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
1.46      markus   1111:                if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
1.21      markus   1112:                        if (errno == EINTR)
                   1113:                                continue;
1.72.2.1  jason    1114:                        fatal("select: %s", strerror(errno));
1.21      markus   1115:                }
1.46      markus   1116:                after_select(readsetp, writesetp);
1.15      markus   1117:        }
1.21      markus   1118:        /* NOTREACHED */
1.1       deraadt  1119: }