10Sstevel@tonic-gate /* 29600SNobutomo.Nakano@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate /* 70Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 80Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 90Sstevel@tonic-gate * All rights reserved 100Sstevel@tonic-gate * The authentication agent program. 110Sstevel@tonic-gate * 120Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 130Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 140Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 150Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 160Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 170Sstevel@tonic-gate * 180Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 210Sstevel@tonic-gate * modification, are permitted provided that the following conditions 220Sstevel@tonic-gate * are met: 230Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 240Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 250Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 260Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 270Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 280Sstevel@tonic-gate * 290Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 300Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 310Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 320Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 330Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 340Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 350Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 360Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 370Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 380Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate 410Sstevel@tonic-gate #include "includes.h" 420Sstevel@tonic-gate #include "sys-queue.h" 4310296SHuie-Ying.Lee@Sun.COM RCSID("$OpenBSD: ssh-agent.c,v 1.159 2008/06/28 14:05:15 djm Exp $"); 440Sstevel@tonic-gate 450Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 460Sstevel@tonic-gate #include <priv.h> 470Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <openssl/evp.h> 500Sstevel@tonic-gate #include <openssl/md5.h> 510Sstevel@tonic-gate 520Sstevel@tonic-gate #include "ssh.h" 530Sstevel@tonic-gate #include "rsa.h" 540Sstevel@tonic-gate #include "buffer.h" 550Sstevel@tonic-gate #include "bufaux.h" 560Sstevel@tonic-gate #include "xmalloc.h" 570Sstevel@tonic-gate #include "key.h" 580Sstevel@tonic-gate #include "authfd.h" 590Sstevel@tonic-gate #include "compat.h" 600Sstevel@tonic-gate #include "log.h" 6110296SHuie-Ying.Lee@Sun.COM #include "readpass.h" 6210296SHuie-Ying.Lee@Sun.COM #include "misc.h" 630Sstevel@tonic-gate 640Sstevel@tonic-gate typedef enum { 650Sstevel@tonic-gate AUTH_UNUSED, 660Sstevel@tonic-gate AUTH_SOCKET, 670Sstevel@tonic-gate AUTH_CONNECTION 680Sstevel@tonic-gate } sock_type; 690Sstevel@tonic-gate 700Sstevel@tonic-gate typedef struct { 710Sstevel@tonic-gate int fd; 720Sstevel@tonic-gate sock_type type; 730Sstevel@tonic-gate Buffer input; 740Sstevel@tonic-gate Buffer output; 750Sstevel@tonic-gate Buffer request; 760Sstevel@tonic-gate } SocketEntry; 770Sstevel@tonic-gate 780Sstevel@tonic-gate u_int sockets_alloc = 0; 790Sstevel@tonic-gate SocketEntry *sockets = NULL; 800Sstevel@tonic-gate 810Sstevel@tonic-gate typedef struct identity { 820Sstevel@tonic-gate TAILQ_ENTRY(identity) next; 830Sstevel@tonic-gate Key *key; 840Sstevel@tonic-gate char *comment; 850Sstevel@tonic-gate u_int death; 8610296SHuie-Ying.Lee@Sun.COM u_int confirm; 870Sstevel@tonic-gate } Identity; 880Sstevel@tonic-gate 890Sstevel@tonic-gate typedef struct { 900Sstevel@tonic-gate int nentries; 910Sstevel@tonic-gate TAILQ_HEAD(idqueue, identity) idlist; 920Sstevel@tonic-gate } Idtab; 930Sstevel@tonic-gate 940Sstevel@tonic-gate /* private key table, one per protocol version */ 950Sstevel@tonic-gate Idtab idtable[3]; 960Sstevel@tonic-gate 970Sstevel@tonic-gate int max_fd = 0; 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* pid of shell == parent of agent */ 1000Sstevel@tonic-gate pid_t parent_pid = -1; 10110296SHuie-Ying.Lee@Sun.COM u_int parent_alive_interval = 0; 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate /* pathname and directory for AUTH_SOCKET */ 10410296SHuie-Ying.Lee@Sun.COM char socket_name[MAXPATHLEN]; 10510296SHuie-Ying.Lee@Sun.COM char socket_dir[MAXPATHLEN]; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* locking */ 1080Sstevel@tonic-gate int locked = 0; 1090Sstevel@tonic-gate char *lock_passwd = NULL; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate #ifdef HAVE___PROGNAME 1120Sstevel@tonic-gate extern char *__progname; 1130Sstevel@tonic-gate #else 1140Sstevel@tonic-gate char *__progname; 1150Sstevel@tonic-gate #endif 1160Sstevel@tonic-gate 11710296SHuie-Ying.Lee@Sun.COM /* Default lifetime (0 == forever) */ 11810296SHuie-Ying.Lee@Sun.COM static int lifetime = 0; 11910296SHuie-Ying.Lee@Sun.COM 1200Sstevel@tonic-gate static void 1210Sstevel@tonic-gate close_socket(SocketEntry *e) 1220Sstevel@tonic-gate { 1230Sstevel@tonic-gate close(e->fd); 1240Sstevel@tonic-gate e->fd = -1; 1250Sstevel@tonic-gate e->type = AUTH_UNUSED; 1260Sstevel@tonic-gate buffer_free(&e->input); 1270Sstevel@tonic-gate buffer_free(&e->output); 1280Sstevel@tonic-gate buffer_free(&e->request); 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate static void 1320Sstevel@tonic-gate idtab_init(void) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate int i; 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate for (i = 0; i <=2; i++) { 1370Sstevel@tonic-gate TAILQ_INIT(&idtable[i].idlist); 1380Sstevel@tonic-gate idtable[i].nentries = 0; 1390Sstevel@tonic-gate } 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* return private key table for requested protocol version */ 1430Sstevel@tonic-gate static Idtab * 1440Sstevel@tonic-gate idtab_lookup(int version) 1450Sstevel@tonic-gate { 1460Sstevel@tonic-gate if (version < 1 || version > 2) 1470Sstevel@tonic-gate fatal("internal error, bad protocol version %d", version); 1480Sstevel@tonic-gate return &idtable[version]; 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate static void 1520Sstevel@tonic-gate free_identity(Identity *id) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate key_free(id->key); 1550Sstevel@tonic-gate xfree(id->comment); 1560Sstevel@tonic-gate xfree(id); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* return matching private key for given public key */ 1600Sstevel@tonic-gate static Identity * 1610Sstevel@tonic-gate lookup_identity(Key *key, int version) 1620Sstevel@tonic-gate { 1630Sstevel@tonic-gate Identity *id; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1660Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 1670Sstevel@tonic-gate if (key_equal(key, id->key)) 1680Sstevel@tonic-gate return (id); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate return (NULL); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 17310296SHuie-Ying.Lee@Sun.COM /* Check confirmation of keysign request */ 17410296SHuie-Ying.Lee@Sun.COM static int 17510296SHuie-Ying.Lee@Sun.COM confirm_key(Identity *id) 17610296SHuie-Ying.Lee@Sun.COM { 17710296SHuie-Ying.Lee@Sun.COM char *p; 17810296SHuie-Ying.Lee@Sun.COM int ret = -1; 17910296SHuie-Ying.Lee@Sun.COM 18010296SHuie-Ying.Lee@Sun.COM p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); 18110296SHuie-Ying.Lee@Sun.COM if (ask_permission( 18210296SHuie-Ying.Lee@Sun.COM gettext("Allow use of key %s?\nKey fingerprint %s."), 18310296SHuie-Ying.Lee@Sun.COM id->comment, p)) 18410296SHuie-Ying.Lee@Sun.COM ret = 0; 18510296SHuie-Ying.Lee@Sun.COM xfree(p); 18610296SHuie-Ying.Lee@Sun.COM 18710296SHuie-Ying.Lee@Sun.COM return (ret); 18810296SHuie-Ying.Lee@Sun.COM } 18910296SHuie-Ying.Lee@Sun.COM 1900Sstevel@tonic-gate /* send list of supported public keys to 'client' */ 1910Sstevel@tonic-gate static void 1920Sstevel@tonic-gate process_request_identities(SocketEntry *e, int version) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1950Sstevel@tonic-gate Identity *id; 1960Sstevel@tonic-gate Buffer msg; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate buffer_init(&msg); 1990Sstevel@tonic-gate buffer_put_char(&msg, (version == 1) ? 2000Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 2010Sstevel@tonic-gate buffer_put_int(&msg, tab->nentries); 2020Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 2030Sstevel@tonic-gate if (id->key->type == KEY_RSA1) { 2040Sstevel@tonic-gate buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); 2050Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->e); 2060Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->n); 2070Sstevel@tonic-gate } else { 2080Sstevel@tonic-gate u_char *blob; 2090Sstevel@tonic-gate u_int blen; 2100Sstevel@tonic-gate key_to_blob(id->key, &blob, &blen); 2110Sstevel@tonic-gate buffer_put_string(&msg, blob, blen); 2120Sstevel@tonic-gate xfree(blob); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate buffer_put_cstring(&msg, id->comment); 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 2170Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 2180Sstevel@tonic-gate buffer_free(&msg); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* ssh1 only */ 2220Sstevel@tonic-gate static void 2230Sstevel@tonic-gate process_authentication_challenge1(SocketEntry *e) 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate u_char buf[32], mdbuf[16], session_id[16]; 2260Sstevel@tonic-gate u_int response_type; 2270Sstevel@tonic-gate BIGNUM *challenge; 2280Sstevel@tonic-gate Identity *id; 2290Sstevel@tonic-gate int i, len; 2300Sstevel@tonic-gate Buffer msg; 2310Sstevel@tonic-gate MD5_CTX md; 2320Sstevel@tonic-gate Key *key; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate buffer_init(&msg); 2350Sstevel@tonic-gate key = key_new(KEY_RSA1); 2360Sstevel@tonic-gate if ((challenge = BN_new()) == NULL) 2370Sstevel@tonic-gate fatal("process_authentication_challenge1: BN_new failed"); 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 2400Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 2410Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 2420Sstevel@tonic-gate buffer_get_bignum(&e->request, challenge); 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* Only protocol 1.1 is supported */ 2450Sstevel@tonic-gate if (buffer_len(&e->request) == 0) 2460Sstevel@tonic-gate goto failure; 2470Sstevel@tonic-gate buffer_get(&e->request, session_id, 16); 2480Sstevel@tonic-gate response_type = buffer_get_int(&e->request); 2490Sstevel@tonic-gate if (response_type != 1) 2500Sstevel@tonic-gate goto failure; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate id = lookup_identity(key, 1); 25310296SHuie-Ying.Lee@Sun.COM if (id != NULL && (!id->confirm || confirm_key(id) == 0)) { 2540Sstevel@tonic-gate Key *private = id->key; 2550Sstevel@tonic-gate /* Decrypt the challenge using the private key. */ 2560Sstevel@tonic-gate if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) 2570Sstevel@tonic-gate goto failure; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate /* The response is MD5 of decrypted challenge plus session id. */ 2600Sstevel@tonic-gate len = BN_num_bytes(challenge); 2610Sstevel@tonic-gate if (len <= 0 || len > 32) { 2620Sstevel@tonic-gate log("process_authentication_challenge: bad challenge length %d", len); 2630Sstevel@tonic-gate goto failure; 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate memset(buf, 0, 32); 2660Sstevel@tonic-gate BN_bn2bin(challenge, buf + 32 - len); 2670Sstevel@tonic-gate MD5_Init(&md); 2680Sstevel@tonic-gate MD5_Update(&md, buf, 32); 2690Sstevel@tonic-gate MD5_Update(&md, session_id, 16); 2700Sstevel@tonic-gate MD5_Final(mdbuf, &md); 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* Send the response. */ 2730Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); 2740Sstevel@tonic-gate for (i = 0; i < 16; i++) 2750Sstevel@tonic-gate buffer_put_char(&msg, mdbuf[i]); 2760Sstevel@tonic-gate goto send; 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate failure: 2800Sstevel@tonic-gate /* Unknown identity or protocol error. Send failure. */ 2810Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 2820Sstevel@tonic-gate send: 2830Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 2840Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 2850Sstevel@tonic-gate key_free(key); 2860Sstevel@tonic-gate BN_clear_free(challenge); 2870Sstevel@tonic-gate buffer_free(&msg); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* ssh2 only */ 2910Sstevel@tonic-gate static void 2920Sstevel@tonic-gate process_sign_request2(SocketEntry *e) 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate u_char *blob, *data, *signature = NULL; 2950Sstevel@tonic-gate u_int blen, dlen, slen = 0; 29610296SHuie-Ying.Lee@Sun.COM extern uint32_t datafellows; 29710296SHuie-Ying.Lee@Sun.COM int odatafellows; 2980Sstevel@tonic-gate int ok = -1, flags; 2990Sstevel@tonic-gate Buffer msg; 3000Sstevel@tonic-gate Key *key; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate datafellows = 0; 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 3050Sstevel@tonic-gate data = buffer_get_string(&e->request, &dlen); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate flags = buffer_get_int(&e->request); 30810296SHuie-Ying.Lee@Sun.COM odatafellows = datafellows; 3090Sstevel@tonic-gate if (flags & SSH_AGENT_OLD_SIGNATURE) 3100Sstevel@tonic-gate datafellows = SSH_BUG_SIGBLOB; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate key = key_from_blob(blob, blen); 3130Sstevel@tonic-gate if (key != NULL) { 3140Sstevel@tonic-gate Identity *id = lookup_identity(key, 2); 31510296SHuie-Ying.Lee@Sun.COM if (id != NULL && (!id->confirm || confirm_key(id) == 0)) 3160Sstevel@tonic-gate ok = key_sign(id->key, &signature, &slen, data, dlen); 31710296SHuie-Ying.Lee@Sun.COM key_free(key); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate buffer_init(&msg); 3200Sstevel@tonic-gate if (ok == 0) { 3210Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); 3220Sstevel@tonic-gate buffer_put_string(&msg, signature, slen); 3230Sstevel@tonic-gate } else { 3240Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 3270Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), 3280Sstevel@tonic-gate buffer_len(&msg)); 3290Sstevel@tonic-gate buffer_free(&msg); 3300Sstevel@tonic-gate xfree(data); 3310Sstevel@tonic-gate xfree(blob); 3320Sstevel@tonic-gate if (signature != NULL) 3330Sstevel@tonic-gate xfree(signature); 33410296SHuie-Ying.Lee@Sun.COM datafellows = odatafellows; 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate /* shared */ 3380Sstevel@tonic-gate static void 3390Sstevel@tonic-gate process_remove_identity(SocketEntry *e, int version) 3400Sstevel@tonic-gate { 3410Sstevel@tonic-gate u_int blen, bits; 3420Sstevel@tonic-gate int success = 0; 3430Sstevel@tonic-gate Key *key = NULL; 3440Sstevel@tonic-gate u_char *blob; 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate switch (version) { 3470Sstevel@tonic-gate case 1: 3480Sstevel@tonic-gate key = key_new(KEY_RSA1); 3490Sstevel@tonic-gate bits = buffer_get_int(&e->request); 3500Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 3510Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate if (bits != key_size(key)) 3540Sstevel@tonic-gate log("Warning: identity keysize mismatch: actual %u, announced %u", 3550Sstevel@tonic-gate key_size(key), bits); 3560Sstevel@tonic-gate break; 3570Sstevel@tonic-gate case 2: 3580Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 3590Sstevel@tonic-gate key = key_from_blob(blob, blen); 3600Sstevel@tonic-gate xfree(blob); 3610Sstevel@tonic-gate break; 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate if (key != NULL) { 3640Sstevel@tonic-gate Identity *id = lookup_identity(key, version); 3650Sstevel@tonic-gate if (id != NULL) { 3660Sstevel@tonic-gate /* 3670Sstevel@tonic-gate * We have this key. Free the old key. Since we 36810296SHuie-Ying.Lee@Sun.COM * don't want to leave empty slots in the middle of 3690Sstevel@tonic-gate * the array, we actually free the key there and move 3700Sstevel@tonic-gate * all the entries between the empty slot and the end 3710Sstevel@tonic-gate * of the array. 3720Sstevel@tonic-gate */ 3730Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3740Sstevel@tonic-gate if (tab->nentries < 1) 3750Sstevel@tonic-gate fatal("process_remove_identity: " 3760Sstevel@tonic-gate "internal error: tab->nentries %d", 3770Sstevel@tonic-gate tab->nentries); 3780Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 3790Sstevel@tonic-gate free_identity(id); 3800Sstevel@tonic-gate tab->nentries--; 3810Sstevel@tonic-gate success = 1; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate key_free(key); 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate buffer_put_int(&e->output, 1); 3860Sstevel@tonic-gate buffer_put_char(&e->output, 3870Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate static void 3910Sstevel@tonic-gate process_remove_all_identities(SocketEntry *e, int version) 3920Sstevel@tonic-gate { 3930Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3940Sstevel@tonic-gate Identity *id; 3950Sstevel@tonic-gate 3960Sstevel@tonic-gate /* Loop over all identities and clear the keys. */ 3970Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; 3980Sstevel@tonic-gate id = TAILQ_FIRST(&tab->idlist)) { 3990Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 4000Sstevel@tonic-gate free_identity(id); 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate /* Mark that there are no identities. */ 4040Sstevel@tonic-gate tab->nentries = 0; 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate /* Send success. */ 4070Sstevel@tonic-gate buffer_put_int(&e->output, 1); 4080Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_SUCCESS); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 41110296SHuie-Ying.Lee@Sun.COM /* removes expired keys and returns number of seconds until the next expiry */ 41210296SHuie-Ying.Lee@Sun.COM static u_int 4130Sstevel@tonic-gate reaper(void) 4140Sstevel@tonic-gate { 41510296SHuie-Ying.Lee@Sun.COM u_int deadline = 0, now = time(NULL); 4160Sstevel@tonic-gate Identity *id, *nxt; 4170Sstevel@tonic-gate int version; 4180Sstevel@tonic-gate Idtab *tab; 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate for (version = 1; version < 3; version++) { 4210Sstevel@tonic-gate tab = idtab_lookup(version); 4220Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { 4230Sstevel@tonic-gate nxt = TAILQ_NEXT(id, next); 42410296SHuie-Ying.Lee@Sun.COM if (id->death == 0) 42510296SHuie-Ying.Lee@Sun.COM continue; 42610296SHuie-Ying.Lee@Sun.COM if (now >= id->death) { 42710296SHuie-Ying.Lee@Sun.COM debug("expiring key '%s'", id->comment); 4280Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 4290Sstevel@tonic-gate free_identity(id); 4300Sstevel@tonic-gate tab->nentries--; 43110296SHuie-Ying.Lee@Sun.COM } else 43210296SHuie-Ying.Lee@Sun.COM deadline = (deadline == 0) ? id->death : 43310296SHuie-Ying.Lee@Sun.COM MIN(deadline, id->death); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate } 43610296SHuie-Ying.Lee@Sun.COM if (deadline == 0 || deadline <= now) 43710296SHuie-Ying.Lee@Sun.COM return 0; 43810296SHuie-Ying.Lee@Sun.COM else 43910296SHuie-Ying.Lee@Sun.COM return (deadline - now); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate static void 4430Sstevel@tonic-gate process_add_identity(SocketEntry *e, int version) 4440Sstevel@tonic-gate { 4450Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 44610296SHuie-Ying.Lee@Sun.COM Identity *id; 44710296SHuie-Ying.Lee@Sun.COM int type, success = 0, death = 0, confirm = 0; 4480Sstevel@tonic-gate char *type_name, *comment; 4490Sstevel@tonic-gate Key *k = NULL; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate switch (version) { 4520Sstevel@tonic-gate case 1: 4530Sstevel@tonic-gate k = key_new_private(KEY_RSA1); 4540Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 4550Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->n); 4560Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->e); 4570Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->d); 4580Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->iqmp); 4590Sstevel@tonic-gate 4600Sstevel@tonic-gate /* SSH and SSL have p and q swapped */ 4610Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->q); /* p */ 4620Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->p); /* q */ 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate /* Generate additional parameters */ 4650Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4660Sstevel@tonic-gate break; 4670Sstevel@tonic-gate case 2: 4680Sstevel@tonic-gate type_name = buffer_get_string(&e->request, NULL); 4690Sstevel@tonic-gate type = key_type_from_name(type_name); 4700Sstevel@tonic-gate xfree(type_name); 4710Sstevel@tonic-gate switch (type) { 4720Sstevel@tonic-gate case KEY_DSA: 4730Sstevel@tonic-gate k = key_new_private(type); 4740Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->p); 4750Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->q); 4760Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->g); 4770Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->pub_key); 4780Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->priv_key); 4790Sstevel@tonic-gate break; 4800Sstevel@tonic-gate case KEY_RSA: 4810Sstevel@tonic-gate k = key_new_private(type); 4820Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->n); 4830Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->e); 4840Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->d); 4850Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->iqmp); 4860Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->p); 4870Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->q); 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate /* Generate additional parameters */ 4900Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4910Sstevel@tonic-gate break; 4920Sstevel@tonic-gate default: 4930Sstevel@tonic-gate buffer_clear(&e->request); 4940Sstevel@tonic-gate goto send; 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate break; 4970Sstevel@tonic-gate } 49810296SHuie-Ying.Lee@Sun.COM /* enable blinding */ 49910296SHuie-Ying.Lee@Sun.COM switch (k->type) { 50010296SHuie-Ying.Lee@Sun.COM case KEY_RSA: 50110296SHuie-Ying.Lee@Sun.COM case KEY_RSA1: 50210296SHuie-Ying.Lee@Sun.COM if (RSA_blinding_on(k->rsa, NULL) != 1) { 50310296SHuie-Ying.Lee@Sun.COM error("process_add_identity: RSA_blinding_on failed"); 50410296SHuie-Ying.Lee@Sun.COM key_free(k); 50510296SHuie-Ying.Lee@Sun.COM goto send; 50610296SHuie-Ying.Lee@Sun.COM } 50710296SHuie-Ying.Lee@Sun.COM break; 50810296SHuie-Ying.Lee@Sun.COM } 5090Sstevel@tonic-gate comment = buffer_get_string(&e->request, NULL); 5100Sstevel@tonic-gate if (k == NULL) { 5110Sstevel@tonic-gate xfree(comment); 5120Sstevel@tonic-gate goto send; 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate while (buffer_len(&e->request)) { 51510296SHuie-Ying.Lee@Sun.COM switch ((type = buffer_get_char(&e->request))) { 5160Sstevel@tonic-gate case SSH_AGENT_CONSTRAIN_LIFETIME: 5170Sstevel@tonic-gate death = time(NULL) + buffer_get_int(&e->request); 5180Sstevel@tonic-gate break; 51910296SHuie-Ying.Lee@Sun.COM case SSH_AGENT_CONSTRAIN_CONFIRM: 52010296SHuie-Ying.Lee@Sun.COM confirm = 1; 52110296SHuie-Ying.Lee@Sun.COM break; 5220Sstevel@tonic-gate default: 52310296SHuie-Ying.Lee@Sun.COM error("process_add_identity: " 52410296SHuie-Ying.Lee@Sun.COM "Unknown constraint type %d", type); 52510296SHuie-Ying.Lee@Sun.COM xfree(comment); 52610296SHuie-Ying.Lee@Sun.COM key_free(k); 52710296SHuie-Ying.Lee@Sun.COM goto send; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate } 53010296SHuie-Ying.Lee@Sun.COM success = 1; 53110296SHuie-Ying.Lee@Sun.COM if (lifetime && !death) 53210296SHuie-Ying.Lee@Sun.COM death = time(NULL) + lifetime; 53310296SHuie-Ying.Lee@Sun.COM if ((id = lookup_identity(k, version)) == NULL) { 53410296SHuie-Ying.Lee@Sun.COM id = xmalloc(sizeof(Identity)); 5350Sstevel@tonic-gate id->key = k; 5360Sstevel@tonic-gate TAILQ_INSERT_TAIL(&tab->idlist, id, next); 5370Sstevel@tonic-gate /* Increment the number of identities. */ 5380Sstevel@tonic-gate tab->nentries++; 5390Sstevel@tonic-gate } else { 5400Sstevel@tonic-gate key_free(k); 54110296SHuie-Ying.Lee@Sun.COM xfree(id->comment); 5420Sstevel@tonic-gate } 54310296SHuie-Ying.Lee@Sun.COM id->comment = comment; 54410296SHuie-Ying.Lee@Sun.COM id->death = death; 54510296SHuie-Ying.Lee@Sun.COM id->confirm = confirm; 5460Sstevel@tonic-gate send: 5470Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5480Sstevel@tonic-gate buffer_put_char(&e->output, 5490Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 5500Sstevel@tonic-gate } 5510Sstevel@tonic-gate 5520Sstevel@tonic-gate /* XXX todo: encrypt sensitive data with passphrase */ 5530Sstevel@tonic-gate static void 5540Sstevel@tonic-gate process_lock_agent(SocketEntry *e, int lock) 5550Sstevel@tonic-gate { 5560Sstevel@tonic-gate int success = 0; 5570Sstevel@tonic-gate char *passwd; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate passwd = buffer_get_string(&e->request, NULL); 5600Sstevel@tonic-gate if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { 5610Sstevel@tonic-gate locked = 0; 5620Sstevel@tonic-gate memset(lock_passwd, 0, strlen(lock_passwd)); 5630Sstevel@tonic-gate xfree(lock_passwd); 5640Sstevel@tonic-gate lock_passwd = NULL; 5650Sstevel@tonic-gate success = 1; 5660Sstevel@tonic-gate } else if (!locked && lock) { 5670Sstevel@tonic-gate locked = 1; 5680Sstevel@tonic-gate lock_passwd = xstrdup(passwd); 5690Sstevel@tonic-gate success = 1; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate memset(passwd, 0, strlen(passwd)); 5720Sstevel@tonic-gate xfree(passwd); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5750Sstevel@tonic-gate buffer_put_char(&e->output, 5760Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate static void 5800Sstevel@tonic-gate no_identities(SocketEntry *e, u_int type) 5810Sstevel@tonic-gate { 5820Sstevel@tonic-gate Buffer msg; 5830Sstevel@tonic-gate 5840Sstevel@tonic-gate buffer_init(&msg); 5850Sstevel@tonic-gate buffer_put_char(&msg, 5860Sstevel@tonic-gate (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? 5870Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 5880Sstevel@tonic-gate buffer_put_int(&msg, 0); 5890Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 5900Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 5910Sstevel@tonic-gate buffer_free(&msg); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate /* dispatch incoming messages */ 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate static void 5970Sstevel@tonic-gate process_message(SocketEntry *e) 5980Sstevel@tonic-gate { 5990Sstevel@tonic-gate u_int msg_len, type; 6000Sstevel@tonic-gate u_char *cp; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate if (buffer_len(&e->input) < 5) 6030Sstevel@tonic-gate return; /* Incomplete message. */ 6040Sstevel@tonic-gate cp = buffer_ptr(&e->input); 60510296SHuie-Ying.Lee@Sun.COM msg_len = get_u32(cp); 6060Sstevel@tonic-gate if (msg_len > 256 * 1024) { 6070Sstevel@tonic-gate close_socket(e); 6080Sstevel@tonic-gate return; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate if (buffer_len(&e->input) < msg_len + 4) 6110Sstevel@tonic-gate return; 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate /* move the current input to e->request */ 6140Sstevel@tonic-gate buffer_consume(&e->input, 4); 6150Sstevel@tonic-gate buffer_clear(&e->request); 6160Sstevel@tonic-gate buffer_append(&e->request, buffer_ptr(&e->input), msg_len); 6170Sstevel@tonic-gate buffer_consume(&e->input, msg_len); 6180Sstevel@tonic-gate type = buffer_get_char(&e->request); 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate /* check wheter agent is locked */ 6210Sstevel@tonic-gate if (locked && type != SSH_AGENTC_UNLOCK) { 6220Sstevel@tonic-gate buffer_clear(&e->request); 6230Sstevel@tonic-gate switch (type) { 6240Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 6250Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 6260Sstevel@tonic-gate /* send empty lists */ 6270Sstevel@tonic-gate no_identities(e, type); 6280Sstevel@tonic-gate break; 6290Sstevel@tonic-gate default: 6300Sstevel@tonic-gate /* send a fail message for all other request types */ 6310Sstevel@tonic-gate buffer_put_int(&e->output, 1); 6320Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate return; 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate debug("type %d", type); 6380Sstevel@tonic-gate switch (type) { 6390Sstevel@tonic-gate case SSH_AGENTC_LOCK: 6400Sstevel@tonic-gate case SSH_AGENTC_UNLOCK: 6410Sstevel@tonic-gate process_lock_agent(e, type == SSH_AGENTC_LOCK); 6420Sstevel@tonic-gate break; 6430Sstevel@tonic-gate /* ssh1 */ 6440Sstevel@tonic-gate case SSH_AGENTC_RSA_CHALLENGE: 6450Sstevel@tonic-gate process_authentication_challenge1(e); 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 6480Sstevel@tonic-gate process_request_identities(e, 1); 6490Sstevel@tonic-gate break; 6500Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_IDENTITY: 6510Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: 6520Sstevel@tonic-gate process_add_identity(e, 1); 6530Sstevel@tonic-gate break; 6540Sstevel@tonic-gate case SSH_AGENTC_REMOVE_RSA_IDENTITY: 6550Sstevel@tonic-gate process_remove_identity(e, 1); 6560Sstevel@tonic-gate break; 6570Sstevel@tonic-gate case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: 6580Sstevel@tonic-gate process_remove_all_identities(e, 1); 6590Sstevel@tonic-gate break; 6600Sstevel@tonic-gate /* ssh2 */ 6610Sstevel@tonic-gate case SSH2_AGENTC_SIGN_REQUEST: 6620Sstevel@tonic-gate process_sign_request2(e); 6630Sstevel@tonic-gate break; 6640Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 6650Sstevel@tonic-gate process_request_identities(e, 2); 6660Sstevel@tonic-gate break; 6670Sstevel@tonic-gate case SSH2_AGENTC_ADD_IDENTITY: 6680Sstevel@tonic-gate case SSH2_AGENTC_ADD_ID_CONSTRAINED: 6690Sstevel@tonic-gate process_add_identity(e, 2); 6700Sstevel@tonic-gate break; 6710Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_IDENTITY: 6720Sstevel@tonic-gate process_remove_identity(e, 2); 6730Sstevel@tonic-gate break; 6740Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: 6750Sstevel@tonic-gate process_remove_all_identities(e, 2); 6760Sstevel@tonic-gate break; 6770Sstevel@tonic-gate default: 6780Sstevel@tonic-gate /* Unknown message. Respond with failure. */ 6790Sstevel@tonic-gate error("Unknown message %d", type); 6800Sstevel@tonic-gate buffer_clear(&e->request); 6810Sstevel@tonic-gate buffer_put_int(&e->output, 1); 6820Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 6830Sstevel@tonic-gate break; 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate static void 6880Sstevel@tonic-gate new_socket(sock_type type, int fd) 6890Sstevel@tonic-gate { 69010296SHuie-Ying.Lee@Sun.COM u_int i, old_alloc, new_alloc; 6910Sstevel@tonic-gate 69210296SHuie-Ying.Lee@Sun.COM set_nonblock(fd); 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate if (fd > max_fd) 6950Sstevel@tonic-gate max_fd = fd; 6960Sstevel@tonic-gate 6970Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 6980Sstevel@tonic-gate if (sockets[i].type == AUTH_UNUSED) { 6990Sstevel@tonic-gate sockets[i].fd = fd; 7000Sstevel@tonic-gate buffer_init(&sockets[i].input); 7010Sstevel@tonic-gate buffer_init(&sockets[i].output); 7020Sstevel@tonic-gate buffer_init(&sockets[i].request); 70310296SHuie-Ying.Lee@Sun.COM sockets[i].type = type; 7040Sstevel@tonic-gate return; 7050Sstevel@tonic-gate } 7060Sstevel@tonic-gate old_alloc = sockets_alloc; 70710296SHuie-Ying.Lee@Sun.COM new_alloc = sockets_alloc + 10; 70810296SHuie-Ying.Lee@Sun.COM sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0])); 70910296SHuie-Ying.Lee@Sun.COM for (i = old_alloc; i < new_alloc; i++) 7100Sstevel@tonic-gate sockets[i].type = AUTH_UNUSED; 71110296SHuie-Ying.Lee@Sun.COM sockets_alloc = new_alloc; 7120Sstevel@tonic-gate sockets[old_alloc].fd = fd; 7130Sstevel@tonic-gate buffer_init(&sockets[old_alloc].input); 7140Sstevel@tonic-gate buffer_init(&sockets[old_alloc].output); 7150Sstevel@tonic-gate buffer_init(&sockets[old_alloc].request); 71610296SHuie-Ying.Lee@Sun.COM sockets[old_alloc].type = type; 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate static int 72010296SHuie-Ying.Lee@Sun.COM prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp, 72110296SHuie-Ying.Lee@Sun.COM struct timeval **tvpp) 7220Sstevel@tonic-gate { 72310296SHuie-Ying.Lee@Sun.COM u_int i, sz, deadline; 7240Sstevel@tonic-gate int n = 0; 72510296SHuie-Ying.Lee@Sun.COM static struct timeval tv; 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 7280Sstevel@tonic-gate switch (sockets[i].type) { 7290Sstevel@tonic-gate case AUTH_SOCKET: 7300Sstevel@tonic-gate case AUTH_CONNECTION: 7310Sstevel@tonic-gate n = MAX(n, sockets[i].fd); 7320Sstevel@tonic-gate break; 7330Sstevel@tonic-gate case AUTH_UNUSED: 7340Sstevel@tonic-gate break; 7350Sstevel@tonic-gate default: 7360Sstevel@tonic-gate fatal("Unknown socket type %d", sockets[i].type); 7370Sstevel@tonic-gate break; 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate } 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); 7420Sstevel@tonic-gate if (*fdrp == NULL || sz > *nallocp) { 7430Sstevel@tonic-gate if (*fdrp) 7440Sstevel@tonic-gate xfree(*fdrp); 7450Sstevel@tonic-gate if (*fdwp) 7460Sstevel@tonic-gate xfree(*fdwp); 7470Sstevel@tonic-gate *fdrp = xmalloc(sz); 7480Sstevel@tonic-gate *fdwp = xmalloc(sz); 7490Sstevel@tonic-gate *nallocp = sz; 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate if (n < *fdl) 7520Sstevel@tonic-gate debug("XXX shrink: %d < %d", n, *fdl); 7530Sstevel@tonic-gate *fdl = n; 7540Sstevel@tonic-gate memset(*fdrp, 0, sz); 7550Sstevel@tonic-gate memset(*fdwp, 0, sz); 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 7580Sstevel@tonic-gate switch (sockets[i].type) { 7590Sstevel@tonic-gate case AUTH_SOCKET: 7600Sstevel@tonic-gate case AUTH_CONNECTION: 7610Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdrp); 7620Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0) 7630Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdwp); 7640Sstevel@tonic-gate break; 7650Sstevel@tonic-gate default: 7660Sstevel@tonic-gate break; 7670Sstevel@tonic-gate } 7680Sstevel@tonic-gate } 76910296SHuie-Ying.Lee@Sun.COM deadline = reaper(); 77010296SHuie-Ying.Lee@Sun.COM if (parent_alive_interval != 0) 77110296SHuie-Ying.Lee@Sun.COM deadline = (deadline == 0) ? parent_alive_interval : 77210296SHuie-Ying.Lee@Sun.COM MIN(deadline, parent_alive_interval); 77310296SHuie-Ying.Lee@Sun.COM if (deadline == 0) { 77410296SHuie-Ying.Lee@Sun.COM *tvpp = NULL; 77510296SHuie-Ying.Lee@Sun.COM } else { 77610296SHuie-Ying.Lee@Sun.COM tv.tv_sec = deadline; 77710296SHuie-Ying.Lee@Sun.COM tv.tv_usec = 0; 77810296SHuie-Ying.Lee@Sun.COM *tvpp = &tv; 77910296SHuie-Ying.Lee@Sun.COM } 7800Sstevel@tonic-gate return (1); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate static void 7840Sstevel@tonic-gate after_select(fd_set *readset, fd_set *writeset) 7850Sstevel@tonic-gate { 7860Sstevel@tonic-gate struct sockaddr_un sunaddr; 7870Sstevel@tonic-gate socklen_t slen; 7880Sstevel@tonic-gate char buf[1024]; 7890Sstevel@tonic-gate int len, sock; 7900Sstevel@tonic-gate u_int i; 7910Sstevel@tonic-gate uid_t euid; 7920Sstevel@tonic-gate gid_t egid; 7930Sstevel@tonic-gate 7940Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 7950Sstevel@tonic-gate switch (sockets[i].type) { 7960Sstevel@tonic-gate case AUTH_UNUSED: 7970Sstevel@tonic-gate break; 7980Sstevel@tonic-gate case AUTH_SOCKET: 7990Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 8000Sstevel@tonic-gate slen = sizeof(sunaddr); 8010Sstevel@tonic-gate sock = accept(sockets[i].fd, 80210296SHuie-Ying.Lee@Sun.COM (struct sockaddr *)&sunaddr, &slen); 8030Sstevel@tonic-gate if (sock < 0) { 8040Sstevel@tonic-gate error("accept from AUTH_SOCKET: %s", 8050Sstevel@tonic-gate strerror(errno)); 8060Sstevel@tonic-gate break; 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate if (getpeereid(sock, &euid, &egid) < 0) { 8090Sstevel@tonic-gate error("getpeereid %d failed: %s", 8100Sstevel@tonic-gate sock, strerror(errno)); 8110Sstevel@tonic-gate close(sock); 8120Sstevel@tonic-gate break; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate if ((euid != 0) && (getuid() != euid)) { 8150Sstevel@tonic-gate error("uid mismatch: " 8160Sstevel@tonic-gate "peer euid %u != uid %u", 8170Sstevel@tonic-gate (u_int) euid, (u_int) getuid()); 8180Sstevel@tonic-gate close(sock); 8190Sstevel@tonic-gate break; 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate new_socket(AUTH_CONNECTION, sock); 8220Sstevel@tonic-gate } 8230Sstevel@tonic-gate break; 8240Sstevel@tonic-gate case AUTH_CONNECTION: 8250Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0 && 8260Sstevel@tonic-gate FD_ISSET(sockets[i].fd, writeset)) { 8270Sstevel@tonic-gate do { 8280Sstevel@tonic-gate len = write(sockets[i].fd, 8290Sstevel@tonic-gate buffer_ptr(&sockets[i].output), 8300Sstevel@tonic-gate buffer_len(&sockets[i].output)); 8310Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 83210296SHuie-Ying.Lee@Sun.COM errno == EINTR || 83310296SHuie-Ying.Lee@Sun.COM errno == EWOULDBLOCK)) 8340Sstevel@tonic-gate continue; 8350Sstevel@tonic-gate break; 8360Sstevel@tonic-gate } while (1); 8370Sstevel@tonic-gate if (len <= 0) { 8380Sstevel@tonic-gate close_socket(&sockets[i]); 8390Sstevel@tonic-gate break; 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate buffer_consume(&sockets[i].output, len); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 8440Sstevel@tonic-gate do { 8450Sstevel@tonic-gate len = read(sockets[i].fd, buf, sizeof(buf)); 8460Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 84710296SHuie-Ying.Lee@Sun.COM errno == EINTR || 84810296SHuie-Ying.Lee@Sun.COM errno == EWOULDBLOCK)) 8490Sstevel@tonic-gate continue; 8500Sstevel@tonic-gate break; 8510Sstevel@tonic-gate } while (1); 8520Sstevel@tonic-gate if (len <= 0) { 8530Sstevel@tonic-gate close_socket(&sockets[i]); 8540Sstevel@tonic-gate break; 8550Sstevel@tonic-gate } 8560Sstevel@tonic-gate buffer_append(&sockets[i].input, buf, len); 8570Sstevel@tonic-gate process_message(&sockets[i]); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate break; 8600Sstevel@tonic-gate default: 8610Sstevel@tonic-gate fatal("Unknown type %d", sockets[i].type); 8620Sstevel@tonic-gate } 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate static void 86610296SHuie-Ying.Lee@Sun.COM cleanup_socket(void) 8670Sstevel@tonic-gate { 8680Sstevel@tonic-gate if (socket_name[0]) 8690Sstevel@tonic-gate unlink(socket_name); 8700Sstevel@tonic-gate if (socket_dir[0]) 8710Sstevel@tonic-gate rmdir(socket_dir); 8720Sstevel@tonic-gate } 8730Sstevel@tonic-gate 87410296SHuie-Ying.Lee@Sun.COM void 8750Sstevel@tonic-gate cleanup_exit(int i) 8760Sstevel@tonic-gate { 87710296SHuie-Ying.Lee@Sun.COM cleanup_socket(); 87810296SHuie-Ying.Lee@Sun.COM _exit(i); 8790Sstevel@tonic-gate } 8800Sstevel@tonic-gate 88110296SHuie-Ying.Lee@Sun.COM /*ARGSUSED*/ 8820Sstevel@tonic-gate static void 8830Sstevel@tonic-gate cleanup_handler(int sig) 8840Sstevel@tonic-gate { 88510296SHuie-Ying.Lee@Sun.COM cleanup_socket(); 8860Sstevel@tonic-gate _exit(2); 8870Sstevel@tonic-gate } 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate static void 89010296SHuie-Ying.Lee@Sun.COM check_parent_exists(void) 8910Sstevel@tonic-gate { 892*10528SHuie-Ying.Lee@Sun.COM #ifdef HAVE_SOLARIS_PRIVILEGE 893*10528SHuie-Ying.Lee@Sun.COM /* 894*10528SHuie-Ying.Lee@Sun.COM * We can not simply use "kill(ppid, 0) < 0" to detect if the parent 895*10528SHuie-Ying.Lee@Sun.COM * has exited when the child process no longer has the 896*10528SHuie-Ying.Lee@Sun.COM * PRIV_PROC_SESSION privilege. 897*10528SHuie-Ying.Lee@Sun.COM */ 898*10528SHuie-Ying.Lee@Sun.COM if (parent_pid != -1 && getppid() != parent_pid) { 899*10528SHuie-Ying.Lee@Sun.COM #else 90010296SHuie-Ying.Lee@Sun.COM if (parent_pid != -1 && kill(parent_pid, 0) < 0) { 901*10528SHuie-Ying.Lee@Sun.COM 902*10528SHuie-Ying.Lee@Sun.COM #endif 9030Sstevel@tonic-gate /* printf("Parent has died - Authentication agent exiting.\n"); */ 90410296SHuie-Ying.Lee@Sun.COM cleanup_socket(); 90510296SHuie-Ying.Lee@Sun.COM _exit(2); 9060Sstevel@tonic-gate } 9070Sstevel@tonic-gate } 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate static void 9100Sstevel@tonic-gate usage(void) 9110Sstevel@tonic-gate { 9120Sstevel@tonic-gate fprintf(stderr, 9130Sstevel@tonic-gate gettext("Usage: %s [options] [command [args ...]]\n" 9140Sstevel@tonic-gate "Options:\n" 9150Sstevel@tonic-gate " -c Generate C-shell commands on stdout.\n" 9160Sstevel@tonic-gate " -s Generate Bourne shell commands on stdout.\n" 9170Sstevel@tonic-gate " -k Kill the current agent.\n" 9180Sstevel@tonic-gate " -d Debug mode.\n" 91910296SHuie-Ying.Lee@Sun.COM " -a socket Bind agent socket to given name.\n" 92010296SHuie-Ying.Lee@Sun.COM " -t life Default identity lifetime (seconds).\n"), 9210Sstevel@tonic-gate __progname); 9220Sstevel@tonic-gate exit(1); 9230Sstevel@tonic-gate } 9240Sstevel@tonic-gate 9250Sstevel@tonic-gate int 9260Sstevel@tonic-gate main(int ac, char **av) 9270Sstevel@tonic-gate { 92810296SHuie-Ying.Lee@Sun.COM int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; 92910296SHuie-Ying.Lee@Sun.COM int sock, fd, ch, result, saved_errno; 93010296SHuie-Ying.Lee@Sun.COM u_int nalloc; 9310Sstevel@tonic-gate char *shell, *pidstr, *agentsocket = NULL; 9320Sstevel@tonic-gate const char *format; 9330Sstevel@tonic-gate fd_set *readsetp = NULL, *writesetp = NULL; 9340Sstevel@tonic-gate struct sockaddr_un sunaddr; 9350Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 9360Sstevel@tonic-gate struct rlimit rlim; 9370Sstevel@tonic-gate #endif 9380Sstevel@tonic-gate int prev_mask; 9390Sstevel@tonic-gate extern int optind; 9400Sstevel@tonic-gate extern char *optarg; 9410Sstevel@tonic-gate pid_t pid; 9420Sstevel@tonic-gate char pidstrbuf[1 + 3 * sizeof pid]; 94310296SHuie-Ying.Lee@Sun.COM struct timeval *tvp = NULL; 9440Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 9450Sstevel@tonic-gate priv_set_t *myprivs; 9460Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 9470Sstevel@tonic-gate 94810296SHuie-Ying.Lee@Sun.COM /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 94910296SHuie-Ying.Lee@Sun.COM sanitise_stdfd(); 95010296SHuie-Ying.Lee@Sun.COM 9510Sstevel@tonic-gate /* drop */ 9520Sstevel@tonic-gate setegid(getgid()); 9530Sstevel@tonic-gate setgid(getgid()); 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate SSLeay_add_all_algorithms(); 9560Sstevel@tonic-gate 9570Sstevel@tonic-gate __progname = get_progname(av[0]); 9580Sstevel@tonic-gate init_rng(); 9590Sstevel@tonic-gate seed_rng(); 9600Sstevel@tonic-gate 96110296SHuie-Ying.Lee@Sun.COM while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { 9620Sstevel@tonic-gate switch (ch) { 9630Sstevel@tonic-gate case 'c': 9640Sstevel@tonic-gate if (s_flag) 9650Sstevel@tonic-gate usage(); 9660Sstevel@tonic-gate c_flag++; 9670Sstevel@tonic-gate break; 9680Sstevel@tonic-gate case 'k': 9690Sstevel@tonic-gate k_flag++; 9700Sstevel@tonic-gate break; 9710Sstevel@tonic-gate case 's': 9720Sstevel@tonic-gate if (c_flag) 9730Sstevel@tonic-gate usage(); 9740Sstevel@tonic-gate s_flag++; 9750Sstevel@tonic-gate break; 9760Sstevel@tonic-gate case 'd': 9770Sstevel@tonic-gate if (d_flag) 9780Sstevel@tonic-gate usage(); 9790Sstevel@tonic-gate d_flag++; 9800Sstevel@tonic-gate break; 9810Sstevel@tonic-gate case 'a': 9820Sstevel@tonic-gate agentsocket = optarg; 9830Sstevel@tonic-gate break; 98410296SHuie-Ying.Lee@Sun.COM case 't': 98510296SHuie-Ying.Lee@Sun.COM if ((lifetime = convtime(optarg)) == -1) { 98610296SHuie-Ying.Lee@Sun.COM fprintf(stderr, gettext("Invalid lifetime\n")); 98710296SHuie-Ying.Lee@Sun.COM usage(); 98810296SHuie-Ying.Lee@Sun.COM } 98910296SHuie-Ying.Lee@Sun.COM break; 9900Sstevel@tonic-gate default: 9910Sstevel@tonic-gate usage(); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate ac -= optind; 9950Sstevel@tonic-gate av += optind; 9960Sstevel@tonic-gate 9970Sstevel@tonic-gate if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 9980Sstevel@tonic-gate usage(); 9990Sstevel@tonic-gate 10000Sstevel@tonic-gate if (ac == 0 && !c_flag && !s_flag) { 10010Sstevel@tonic-gate shell = getenv("SHELL"); 100210296SHuie-Ying.Lee@Sun.COM if (shell != NULL && 100310296SHuie-Ying.Lee@Sun.COM strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) 10040Sstevel@tonic-gate c_flag = 1; 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate if (k_flag) { 10070Sstevel@tonic-gate pidstr = getenv(SSH_AGENTPID_ENV_NAME); 10080Sstevel@tonic-gate if (pidstr == NULL) { 10090Sstevel@tonic-gate fprintf(stderr, 10100Sstevel@tonic-gate gettext("%s not set, cannot kill agent\n"), 10110Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 10120Sstevel@tonic-gate exit(1); 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate pid = atoi(pidstr); 10150Sstevel@tonic-gate if (pid < 1) { 10160Sstevel@tonic-gate fprintf(stderr, 101710296SHuie-Ying.Lee@Sun.COM gettext("%s not set, cannot kill agent\n"), 101810296SHuie-Ying.Lee@Sun.COM SSH_AGENTPID_ENV_NAME); 10190Sstevel@tonic-gate exit(1); 10200Sstevel@tonic-gate } 10210Sstevel@tonic-gate if (kill(pid, SIGTERM) == -1) { 10220Sstevel@tonic-gate perror("kill"); 10230Sstevel@tonic-gate exit(1); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; 10260Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME); 10270Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME); 10280Sstevel@tonic-gate printf("echo "); 10290Sstevel@tonic-gate printf(gettext("Agent pid %ld killed;\n"), (long)pid); 10300Sstevel@tonic-gate exit(0); 10310Sstevel@tonic-gate } 10320Sstevel@tonic-gate parent_pid = getpid(); 10330Sstevel@tonic-gate 10340Sstevel@tonic-gate if (agentsocket == NULL) { 10350Sstevel@tonic-gate /* Create private directory for agent socket */ 103610296SHuie-Ying.Lee@Sun.COM strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir); 10370Sstevel@tonic-gate if (mkdtemp(socket_dir) == NULL) { 10380Sstevel@tonic-gate perror("mkdtemp: private socket dir"); 10390Sstevel@tonic-gate exit(1); 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, 10420Sstevel@tonic-gate (long)parent_pid); 10430Sstevel@tonic-gate } else { 10440Sstevel@tonic-gate /* Try to use specified agent socket */ 10450Sstevel@tonic-gate socket_dir[0] = '\0'; 10460Sstevel@tonic-gate strlcpy(socket_name, agentsocket, sizeof socket_name); 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * Create socket early so it will exist before command gets run from 10510Sstevel@tonic-gate * the parent. 10520Sstevel@tonic-gate */ 10530Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0); 10540Sstevel@tonic-gate if (sock < 0) { 10550Sstevel@tonic-gate perror("socket"); 105610296SHuie-Ying.Lee@Sun.COM *socket_name = '\0'; /* Don't unlink any existing file */ 10570Sstevel@tonic-gate cleanup_exit(1); 10580Sstevel@tonic-gate } 10590Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr)); 10600Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX; 10610Sstevel@tonic-gate strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); 10620Sstevel@tonic-gate prev_mask = umask(0177); 106310296SHuie-Ying.Lee@Sun.COM if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) { 10640Sstevel@tonic-gate perror("bind"); 106510296SHuie-Ying.Lee@Sun.COM *socket_name = '\0'; /* Don't unlink any existing file */ 10660Sstevel@tonic-gate umask(prev_mask); 10670Sstevel@tonic-gate cleanup_exit(1); 10680Sstevel@tonic-gate } 10690Sstevel@tonic-gate umask(prev_mask); 107010296SHuie-Ying.Lee@Sun.COM if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { 10710Sstevel@tonic-gate perror("listen"); 10720Sstevel@tonic-gate cleanup_exit(1); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate 10750Sstevel@tonic-gate /* 10760Sstevel@tonic-gate * Fork, and have the parent execute the command, if any, or present 10770Sstevel@tonic-gate * the socket data. The child continues as the authentication agent. 10780Sstevel@tonic-gate */ 10790Sstevel@tonic-gate if (d_flag) { 10800Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); 10810Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10820Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10830Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 10840Sstevel@tonic-gate printf("echo "); 10850Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)parent_pid); 10860Sstevel@tonic-gate goto skip; 10870Sstevel@tonic-gate } 10880Sstevel@tonic-gate pid = fork(); 10890Sstevel@tonic-gate if (pid == -1) { 10900Sstevel@tonic-gate perror("fork"); 10910Sstevel@tonic-gate cleanup_exit(1); 10920Sstevel@tonic-gate } 10930Sstevel@tonic-gate if (pid != 0) { /* Parent - execute the given command. */ 10940Sstevel@tonic-gate close(sock); 10950Sstevel@tonic-gate snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); 10960Sstevel@tonic-gate if (ac == 0) { 10970Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10980Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10990Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 11000Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, 11010Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 11020Sstevel@tonic-gate printf("echo "); 110310296SHuie-Ying.Lee@Sun.COM printf(gettext("Agent pid %ld;\n"), (long)pid); 11040Sstevel@tonic-gate exit(0); 11050Sstevel@tonic-gate } 11060Sstevel@tonic-gate if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || 11070Sstevel@tonic-gate setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { 11080Sstevel@tonic-gate perror("setenv"); 11090Sstevel@tonic-gate exit(1); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate execvp(av[0], av); 11120Sstevel@tonic-gate perror(av[0]); 11130Sstevel@tonic-gate exit(1); 11140Sstevel@tonic-gate } 11150Sstevel@tonic-gate /* child */ 11160Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 11190Sstevel@tonic-gate /* 11200Sstevel@tonic-gate * Drop unneeded privs, including basic ones like fork/exec. 11210Sstevel@tonic-gate * 11220Sstevel@tonic-gate * Idiom: remove from 'basic' privs we know we don't want, 11230Sstevel@tonic-gate * invert the result and remove the resulting set from P. 11240Sstevel@tonic-gate * 11250Sstevel@tonic-gate * None of the priv_delset() calls below, nor the setppriv call 11260Sstevel@tonic-gate * below can fail, so their return values are not checked. 11270Sstevel@tonic-gate */ 11280Sstevel@tonic-gate if ((myprivs = priv_str_to_set("basic", ",", NULL)) == NULL) 11290Sstevel@tonic-gate fatal("priv_str_to_set failed: %m"); 11300Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_EXEC); 11310Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_FORK); 11320Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_FILE_LINK_ANY); 11330Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_INFO); 11340Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_SESSION); 11350Sstevel@tonic-gate priv_inverse(myprivs); 11360Sstevel@tonic-gate (void) setppriv(PRIV_OFF, PRIV_PERMITTED, myprivs); 11370Sstevel@tonic-gate (void) priv_freeset(myprivs); 11380Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate if (setsid() == -1) { 11410Sstevel@tonic-gate error("setsid: %s", strerror(errno)); 11420Sstevel@tonic-gate cleanup_exit(1); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate (void)chdir("/"); 114610296SHuie-Ying.Lee@Sun.COM if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 114710296SHuie-Ying.Lee@Sun.COM /* XXX might close listen socket */ 114810296SHuie-Ying.Lee@Sun.COM (void)dup2(fd, STDIN_FILENO); 114910296SHuie-Ying.Lee@Sun.COM (void)dup2(fd, STDOUT_FILENO); 115010296SHuie-Ying.Lee@Sun.COM (void)dup2(fd, STDERR_FILENO); 115110296SHuie-Ying.Lee@Sun.COM if (fd > 2) 115210296SHuie-Ying.Lee@Sun.COM close(fd); 115310296SHuie-Ying.Lee@Sun.COM } 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 11560Sstevel@tonic-gate /* deny core dumps, since memory contains unencrypted private keys */ 11570Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max = 0; 11580Sstevel@tonic-gate if (setrlimit(RLIMIT_CORE, &rlim) < 0) { 11590Sstevel@tonic-gate error("setrlimit RLIMIT_CORE: %s", strerror(errno)); 11600Sstevel@tonic-gate cleanup_exit(1); 11610Sstevel@tonic-gate } 11620Sstevel@tonic-gate #endif 11630Sstevel@tonic-gate 11640Sstevel@tonic-gate skip: 11650Sstevel@tonic-gate new_socket(AUTH_SOCKET, sock); 116610296SHuie-Ying.Lee@Sun.COM if (ac > 0) 116710296SHuie-Ying.Lee@Sun.COM parent_alive_interval = 10; 11680Sstevel@tonic-gate idtab_init(); 11690Sstevel@tonic-gate if (!d_flag) 11700Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 11710Sstevel@tonic-gate signal(SIGPIPE, SIG_IGN); 11720Sstevel@tonic-gate signal(SIGHUP, cleanup_handler); 11730Sstevel@tonic-gate signal(SIGTERM, cleanup_handler); 11740Sstevel@tonic-gate nalloc = 0; 11750Sstevel@tonic-gate 11760Sstevel@tonic-gate while (1) { 117710296SHuie-Ying.Lee@Sun.COM prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp); 117810296SHuie-Ying.Lee@Sun.COM result = select(max_fd + 1, readsetp, writesetp, NULL, tvp); 117910296SHuie-Ying.Lee@Sun.COM saved_errno = errno; 118010296SHuie-Ying.Lee@Sun.COM if (parent_alive_interval != 0) 118110296SHuie-Ying.Lee@Sun.COM check_parent_exists(); 118210296SHuie-Ying.Lee@Sun.COM (void) reaper(); /* remove expired keys */ 118310296SHuie-Ying.Lee@Sun.COM if (result < 0) { 118410296SHuie-Ying.Lee@Sun.COM if (saved_errno == EINTR) 11850Sstevel@tonic-gate continue; 118610296SHuie-Ying.Lee@Sun.COM fatal("select: %s", strerror(saved_errno)); 118710296SHuie-Ying.Lee@Sun.COM } else if (result > 0) 118810296SHuie-Ying.Lee@Sun.COM after_select(readsetp, writesetp); 11890Sstevel@tonic-gate } 11900Sstevel@tonic-gate /* NOTREACHED */ 11910Sstevel@tonic-gate return (0); /* keep lint happy */ 11920Sstevel@tonic-gate } 1193