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

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