10Sstevel@tonic-gate /* 2*9600SNobutomo.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" 430Sstevel@tonic-gate RCSID("$OpenBSD: ssh-agent.c,v 1.105 2002/10/01 20:34:12 markus 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 "getput.h" 580Sstevel@tonic-gate #include "key.h" 590Sstevel@tonic-gate #include "authfd.h" 600Sstevel@tonic-gate #include "compat.h" 610Sstevel@tonic-gate #include "log.h" 620Sstevel@tonic-gate 630Sstevel@tonic-gate typedef enum { 640Sstevel@tonic-gate AUTH_UNUSED, 650Sstevel@tonic-gate AUTH_SOCKET, 660Sstevel@tonic-gate AUTH_CONNECTION 670Sstevel@tonic-gate } sock_type; 680Sstevel@tonic-gate 690Sstevel@tonic-gate typedef struct { 700Sstevel@tonic-gate int fd; 710Sstevel@tonic-gate sock_type type; 720Sstevel@tonic-gate Buffer input; 730Sstevel@tonic-gate Buffer output; 740Sstevel@tonic-gate Buffer request; 750Sstevel@tonic-gate } SocketEntry; 760Sstevel@tonic-gate 770Sstevel@tonic-gate u_int sockets_alloc = 0; 780Sstevel@tonic-gate SocketEntry *sockets = NULL; 790Sstevel@tonic-gate 800Sstevel@tonic-gate typedef struct identity { 810Sstevel@tonic-gate TAILQ_ENTRY(identity) next; 820Sstevel@tonic-gate Key *key; 830Sstevel@tonic-gate char *comment; 840Sstevel@tonic-gate u_int death; 850Sstevel@tonic-gate } Identity; 860Sstevel@tonic-gate 870Sstevel@tonic-gate typedef struct { 880Sstevel@tonic-gate int nentries; 890Sstevel@tonic-gate TAILQ_HEAD(idqueue, identity) idlist; 900Sstevel@tonic-gate } Idtab; 910Sstevel@tonic-gate 920Sstevel@tonic-gate /* private key table, one per protocol version */ 930Sstevel@tonic-gate Idtab idtable[3]; 940Sstevel@tonic-gate 950Sstevel@tonic-gate int max_fd = 0; 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* pid of shell == parent of agent */ 980Sstevel@tonic-gate pid_t parent_pid = -1; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate /* pathname and directory for AUTH_SOCKET */ 1010Sstevel@tonic-gate char socket_name[1024]; 1020Sstevel@tonic-gate char socket_dir[1024]; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate /* locking */ 1050Sstevel@tonic-gate int locked = 0; 1060Sstevel@tonic-gate char *lock_passwd = NULL; 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #ifdef HAVE___PROGNAME 1090Sstevel@tonic-gate extern char *__progname; 1100Sstevel@tonic-gate #else 1110Sstevel@tonic-gate char *__progname; 1120Sstevel@tonic-gate #endif 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate static void 1150Sstevel@tonic-gate close_socket(SocketEntry *e) 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate close(e->fd); 1180Sstevel@tonic-gate e->fd = -1; 1190Sstevel@tonic-gate e->type = AUTH_UNUSED; 1200Sstevel@tonic-gate buffer_free(&e->input); 1210Sstevel@tonic-gate buffer_free(&e->output); 1220Sstevel@tonic-gate buffer_free(&e->request); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate static void 1260Sstevel@tonic-gate idtab_init(void) 1270Sstevel@tonic-gate { 1280Sstevel@tonic-gate int i; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate for (i = 0; i <=2; i++) { 1310Sstevel@tonic-gate TAILQ_INIT(&idtable[i].idlist); 1320Sstevel@tonic-gate idtable[i].nentries = 0; 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* return private key table for requested protocol version */ 1370Sstevel@tonic-gate static Idtab * 1380Sstevel@tonic-gate idtab_lookup(int version) 1390Sstevel@tonic-gate { 1400Sstevel@tonic-gate if (version < 1 || version > 2) 1410Sstevel@tonic-gate fatal("internal error, bad protocol version %d", version); 1420Sstevel@tonic-gate return &idtable[version]; 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate static void 1460Sstevel@tonic-gate free_identity(Identity *id) 1470Sstevel@tonic-gate { 1480Sstevel@tonic-gate key_free(id->key); 1490Sstevel@tonic-gate xfree(id->comment); 1500Sstevel@tonic-gate xfree(id); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate /* return matching private key for given public key */ 1540Sstevel@tonic-gate static Identity * 1550Sstevel@tonic-gate lookup_identity(Key *key, int version) 1560Sstevel@tonic-gate { 1570Sstevel@tonic-gate Identity *id; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1600Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 1610Sstevel@tonic-gate if (key_equal(key, id->key)) 1620Sstevel@tonic-gate return (id); 1630Sstevel@tonic-gate } 1640Sstevel@tonic-gate return (NULL); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate /* send list of supported public keys to 'client' */ 1680Sstevel@tonic-gate static void 1690Sstevel@tonic-gate process_request_identities(SocketEntry *e, int version) 1700Sstevel@tonic-gate { 1710Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1720Sstevel@tonic-gate Identity *id; 1730Sstevel@tonic-gate Buffer msg; 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate buffer_init(&msg); 1760Sstevel@tonic-gate buffer_put_char(&msg, (version == 1) ? 1770Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 1780Sstevel@tonic-gate buffer_put_int(&msg, tab->nentries); 1790Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 1800Sstevel@tonic-gate if (id->key->type == KEY_RSA1) { 1810Sstevel@tonic-gate buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); 1820Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->e); 1830Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->n); 1840Sstevel@tonic-gate } else { 1850Sstevel@tonic-gate u_char *blob; 1860Sstevel@tonic-gate u_int blen; 1870Sstevel@tonic-gate key_to_blob(id->key, &blob, &blen); 1880Sstevel@tonic-gate buffer_put_string(&msg, blob, blen); 1890Sstevel@tonic-gate xfree(blob); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate buffer_put_cstring(&msg, id->comment); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 1940Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 1950Sstevel@tonic-gate buffer_free(&msg); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* ssh1 only */ 1990Sstevel@tonic-gate static void 2000Sstevel@tonic-gate process_authentication_challenge1(SocketEntry *e) 2010Sstevel@tonic-gate { 2020Sstevel@tonic-gate u_char buf[32], mdbuf[16], session_id[16]; 2030Sstevel@tonic-gate u_int response_type; 2040Sstevel@tonic-gate BIGNUM *challenge; 2050Sstevel@tonic-gate Identity *id; 2060Sstevel@tonic-gate int i, len; 2070Sstevel@tonic-gate Buffer msg; 2080Sstevel@tonic-gate MD5_CTX md; 2090Sstevel@tonic-gate Key *key; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate buffer_init(&msg); 2120Sstevel@tonic-gate key = key_new(KEY_RSA1); 2130Sstevel@tonic-gate if ((challenge = BN_new()) == NULL) 2140Sstevel@tonic-gate fatal("process_authentication_challenge1: BN_new failed"); 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 2170Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 2180Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 2190Sstevel@tonic-gate buffer_get_bignum(&e->request, challenge); 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate /* Only protocol 1.1 is supported */ 2220Sstevel@tonic-gate if (buffer_len(&e->request) == 0) 2230Sstevel@tonic-gate goto failure; 2240Sstevel@tonic-gate buffer_get(&e->request, session_id, 16); 2250Sstevel@tonic-gate response_type = buffer_get_int(&e->request); 2260Sstevel@tonic-gate if (response_type != 1) 2270Sstevel@tonic-gate goto failure; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate id = lookup_identity(key, 1); 2300Sstevel@tonic-gate if (id != NULL) { 2310Sstevel@tonic-gate Key *private = id->key; 2320Sstevel@tonic-gate /* Decrypt the challenge using the private key. */ 2330Sstevel@tonic-gate if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) 2340Sstevel@tonic-gate goto failure; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate /* The response is MD5 of decrypted challenge plus session id. */ 2370Sstevel@tonic-gate len = BN_num_bytes(challenge); 2380Sstevel@tonic-gate if (len <= 0 || len > 32) { 2390Sstevel@tonic-gate log("process_authentication_challenge: bad challenge length %d", len); 2400Sstevel@tonic-gate goto failure; 2410Sstevel@tonic-gate } 2420Sstevel@tonic-gate memset(buf, 0, 32); 2430Sstevel@tonic-gate BN_bn2bin(challenge, buf + 32 - len); 2440Sstevel@tonic-gate MD5_Init(&md); 2450Sstevel@tonic-gate MD5_Update(&md, buf, 32); 2460Sstevel@tonic-gate MD5_Update(&md, session_id, 16); 2470Sstevel@tonic-gate MD5_Final(mdbuf, &md); 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate /* Send the response. */ 2500Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); 2510Sstevel@tonic-gate for (i = 0; i < 16; i++) 2520Sstevel@tonic-gate buffer_put_char(&msg, mdbuf[i]); 2530Sstevel@tonic-gate goto send; 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate failure: 2570Sstevel@tonic-gate /* Unknown identity or protocol error. Send failure. */ 2580Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 2590Sstevel@tonic-gate send: 2600Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 2610Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 2620Sstevel@tonic-gate key_free(key); 2630Sstevel@tonic-gate BN_clear_free(challenge); 2640Sstevel@tonic-gate buffer_free(&msg); 2650Sstevel@tonic-gate } 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* ssh2 only */ 2680Sstevel@tonic-gate static void 2690Sstevel@tonic-gate process_sign_request2(SocketEntry *e) 2700Sstevel@tonic-gate { 2710Sstevel@tonic-gate u_char *blob, *data, *signature = NULL; 2720Sstevel@tonic-gate u_int blen, dlen, slen = 0; 2730Sstevel@tonic-gate int ok = -1, flags; 2740Sstevel@tonic-gate Buffer msg; 2750Sstevel@tonic-gate Key *key; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate datafellows = 0; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 2800Sstevel@tonic-gate data = buffer_get_string(&e->request, &dlen); 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate flags = buffer_get_int(&e->request); 2830Sstevel@tonic-gate if (flags & SSH_AGENT_OLD_SIGNATURE) 2840Sstevel@tonic-gate datafellows = SSH_BUG_SIGBLOB; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate key = key_from_blob(blob, blen); 2870Sstevel@tonic-gate if (key != NULL) { 2880Sstevel@tonic-gate Identity *id = lookup_identity(key, 2); 2890Sstevel@tonic-gate if (id != NULL) 2900Sstevel@tonic-gate ok = key_sign(id->key, &signature, &slen, data, dlen); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate key_free(key); 2930Sstevel@tonic-gate buffer_init(&msg); 2940Sstevel@tonic-gate if (ok == 0) { 2950Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); 2960Sstevel@tonic-gate buffer_put_string(&msg, signature, slen); 2970Sstevel@tonic-gate } else { 2980Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 3010Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), 3020Sstevel@tonic-gate buffer_len(&msg)); 3030Sstevel@tonic-gate buffer_free(&msg); 3040Sstevel@tonic-gate xfree(data); 3050Sstevel@tonic-gate xfree(blob); 3060Sstevel@tonic-gate if (signature != NULL) 3070Sstevel@tonic-gate xfree(signature); 3080Sstevel@tonic-gate } 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate /* shared */ 3110Sstevel@tonic-gate static void 3120Sstevel@tonic-gate process_remove_identity(SocketEntry *e, int version) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate u_int blen, bits; 3150Sstevel@tonic-gate int success = 0; 3160Sstevel@tonic-gate Key *key = NULL; 3170Sstevel@tonic-gate u_char *blob; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate switch (version) { 3200Sstevel@tonic-gate case 1: 3210Sstevel@tonic-gate key = key_new(KEY_RSA1); 3220Sstevel@tonic-gate bits = buffer_get_int(&e->request); 3230Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 3240Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate if (bits != key_size(key)) 3270Sstevel@tonic-gate log("Warning: identity keysize mismatch: actual %u, announced %u", 3280Sstevel@tonic-gate key_size(key), bits); 3290Sstevel@tonic-gate break; 3300Sstevel@tonic-gate case 2: 3310Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 3320Sstevel@tonic-gate key = key_from_blob(blob, blen); 3330Sstevel@tonic-gate xfree(blob); 3340Sstevel@tonic-gate break; 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate if (key != NULL) { 3370Sstevel@tonic-gate Identity *id = lookup_identity(key, version); 3380Sstevel@tonic-gate if (id != NULL) { 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * We have this key. Free the old key. Since we 3410Sstevel@tonic-gate * don\'t want to leave empty slots in the middle of 3420Sstevel@tonic-gate * the array, we actually free the key there and move 3430Sstevel@tonic-gate * all the entries between the empty slot and the end 3440Sstevel@tonic-gate * of the array. 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3470Sstevel@tonic-gate if (tab->nentries < 1) 3480Sstevel@tonic-gate fatal("process_remove_identity: " 3490Sstevel@tonic-gate "internal error: tab->nentries %d", 3500Sstevel@tonic-gate tab->nentries); 3510Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 3520Sstevel@tonic-gate free_identity(id); 3530Sstevel@tonic-gate tab->nentries--; 3540Sstevel@tonic-gate success = 1; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate key_free(key); 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate buffer_put_int(&e->output, 1); 3590Sstevel@tonic-gate buffer_put_char(&e->output, 3600Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate static void 3640Sstevel@tonic-gate process_remove_all_identities(SocketEntry *e, int version) 3650Sstevel@tonic-gate { 3660Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3670Sstevel@tonic-gate Identity *id; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate /* Loop over all identities and clear the keys. */ 3700Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; 3710Sstevel@tonic-gate id = TAILQ_FIRST(&tab->idlist)) { 3720Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 3730Sstevel@tonic-gate free_identity(id); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* Mark that there are no identities. */ 3770Sstevel@tonic-gate tab->nentries = 0; 3780Sstevel@tonic-gate 3790Sstevel@tonic-gate /* Send success. */ 3800Sstevel@tonic-gate buffer_put_int(&e->output, 1); 3810Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_SUCCESS); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate static void 3850Sstevel@tonic-gate reaper(void) 3860Sstevel@tonic-gate { 3870Sstevel@tonic-gate u_int now = time(NULL); 3880Sstevel@tonic-gate Identity *id, *nxt; 3890Sstevel@tonic-gate int version; 3900Sstevel@tonic-gate Idtab *tab; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate for (version = 1; version < 3; version++) { 3930Sstevel@tonic-gate tab = idtab_lookup(version); 3940Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { 3950Sstevel@tonic-gate nxt = TAILQ_NEXT(id, next); 3960Sstevel@tonic-gate if (id->death != 0 && now >= id->death) { 3970Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 3980Sstevel@tonic-gate free_identity(id); 3990Sstevel@tonic-gate tab->nentries--; 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate } 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate static void 4060Sstevel@tonic-gate process_add_identity(SocketEntry *e, int version) 4070Sstevel@tonic-gate { 4080Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 4090Sstevel@tonic-gate int type, success = 0, death = 0; 4100Sstevel@tonic-gate char *type_name, *comment; 4110Sstevel@tonic-gate Key *k = NULL; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate switch (version) { 4140Sstevel@tonic-gate case 1: 4150Sstevel@tonic-gate k = key_new_private(KEY_RSA1); 4160Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 4170Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->n); 4180Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->e); 4190Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->d); 4200Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->iqmp); 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate /* SSH and SSL have p and q swapped */ 4230Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->q); /* p */ 4240Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->p); /* q */ 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate /* Generate additional parameters */ 4270Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4280Sstevel@tonic-gate break; 4290Sstevel@tonic-gate case 2: 4300Sstevel@tonic-gate type_name = buffer_get_string(&e->request, NULL); 4310Sstevel@tonic-gate type = key_type_from_name(type_name); 4320Sstevel@tonic-gate xfree(type_name); 4330Sstevel@tonic-gate switch (type) { 4340Sstevel@tonic-gate case KEY_DSA: 4350Sstevel@tonic-gate k = key_new_private(type); 4360Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->p); 4370Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->q); 4380Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->g); 4390Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->pub_key); 4400Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->priv_key); 4410Sstevel@tonic-gate break; 4420Sstevel@tonic-gate case KEY_RSA: 4430Sstevel@tonic-gate k = key_new_private(type); 4440Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->n); 4450Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->e); 4460Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->d); 4470Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->iqmp); 4480Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->p); 4490Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->q); 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate /* Generate additional parameters */ 4520Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4530Sstevel@tonic-gate break; 4540Sstevel@tonic-gate default: 4550Sstevel@tonic-gate buffer_clear(&e->request); 4560Sstevel@tonic-gate goto send; 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate break; 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate comment = buffer_get_string(&e->request, NULL); 4610Sstevel@tonic-gate if (k == NULL) { 4620Sstevel@tonic-gate xfree(comment); 4630Sstevel@tonic-gate goto send; 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate success = 1; 4660Sstevel@tonic-gate while (buffer_len(&e->request)) { 4670Sstevel@tonic-gate switch (buffer_get_char(&e->request)) { 4680Sstevel@tonic-gate case SSH_AGENT_CONSTRAIN_LIFETIME: 4690Sstevel@tonic-gate death = time(NULL) + buffer_get_int(&e->request); 4700Sstevel@tonic-gate break; 4710Sstevel@tonic-gate default: 4720Sstevel@tonic-gate break; 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate } 4750Sstevel@tonic-gate if (lookup_identity(k, version) == NULL) { 4760Sstevel@tonic-gate Identity *id = xmalloc(sizeof(Identity)); 4770Sstevel@tonic-gate id->key = k; 4780Sstevel@tonic-gate id->comment = comment; 4790Sstevel@tonic-gate id->death = death; 4800Sstevel@tonic-gate TAILQ_INSERT_TAIL(&tab->idlist, id, next); 4810Sstevel@tonic-gate /* Increment the number of identities. */ 4820Sstevel@tonic-gate tab->nentries++; 4830Sstevel@tonic-gate } else { 4840Sstevel@tonic-gate key_free(k); 4850Sstevel@tonic-gate xfree(comment); 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate send: 4880Sstevel@tonic-gate buffer_put_int(&e->output, 1); 4890Sstevel@tonic-gate buffer_put_char(&e->output, 4900Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate /* XXX todo: encrypt sensitive data with passphrase */ 4940Sstevel@tonic-gate static void 4950Sstevel@tonic-gate process_lock_agent(SocketEntry *e, int lock) 4960Sstevel@tonic-gate { 4970Sstevel@tonic-gate int success = 0; 4980Sstevel@tonic-gate char *passwd; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate passwd = buffer_get_string(&e->request, NULL); 5010Sstevel@tonic-gate if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { 5020Sstevel@tonic-gate locked = 0; 5030Sstevel@tonic-gate memset(lock_passwd, 0, strlen(lock_passwd)); 5040Sstevel@tonic-gate xfree(lock_passwd); 5050Sstevel@tonic-gate lock_passwd = NULL; 5060Sstevel@tonic-gate success = 1; 5070Sstevel@tonic-gate } else if (!locked && lock) { 5080Sstevel@tonic-gate locked = 1; 5090Sstevel@tonic-gate lock_passwd = xstrdup(passwd); 5100Sstevel@tonic-gate success = 1; 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate memset(passwd, 0, strlen(passwd)); 5130Sstevel@tonic-gate xfree(passwd); 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5160Sstevel@tonic-gate buffer_put_char(&e->output, 5170Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate static void 5210Sstevel@tonic-gate no_identities(SocketEntry *e, u_int type) 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate Buffer msg; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate buffer_init(&msg); 5260Sstevel@tonic-gate buffer_put_char(&msg, 5270Sstevel@tonic-gate (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? 5280Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 5290Sstevel@tonic-gate buffer_put_int(&msg, 0); 5300Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 5310Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 5320Sstevel@tonic-gate buffer_free(&msg); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate 5350Sstevel@tonic-gate /* dispatch incoming messages */ 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate static void 5380Sstevel@tonic-gate process_message(SocketEntry *e) 5390Sstevel@tonic-gate { 5400Sstevel@tonic-gate u_int msg_len, type; 5410Sstevel@tonic-gate u_char *cp; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate /* kill dead keys */ 5440Sstevel@tonic-gate reaper(); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate if (buffer_len(&e->input) < 5) 5470Sstevel@tonic-gate return; /* Incomplete message. */ 5480Sstevel@tonic-gate cp = buffer_ptr(&e->input); 5490Sstevel@tonic-gate msg_len = GET_32BIT(cp); 5500Sstevel@tonic-gate if (msg_len > 256 * 1024) { 5510Sstevel@tonic-gate close_socket(e); 5520Sstevel@tonic-gate return; 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate if (buffer_len(&e->input) < msg_len + 4) 5550Sstevel@tonic-gate return; 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate /* move the current input to e->request */ 5580Sstevel@tonic-gate buffer_consume(&e->input, 4); 5590Sstevel@tonic-gate buffer_clear(&e->request); 5600Sstevel@tonic-gate buffer_append(&e->request, buffer_ptr(&e->input), msg_len); 5610Sstevel@tonic-gate buffer_consume(&e->input, msg_len); 5620Sstevel@tonic-gate type = buffer_get_char(&e->request); 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate /* check wheter agent is locked */ 5650Sstevel@tonic-gate if (locked && type != SSH_AGENTC_UNLOCK) { 5660Sstevel@tonic-gate buffer_clear(&e->request); 5670Sstevel@tonic-gate switch (type) { 5680Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 5690Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 5700Sstevel@tonic-gate /* send empty lists */ 5710Sstevel@tonic-gate no_identities(e, type); 5720Sstevel@tonic-gate break; 5730Sstevel@tonic-gate default: 5740Sstevel@tonic-gate /* send a fail message for all other request types */ 5750Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5760Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 5770Sstevel@tonic-gate } 5780Sstevel@tonic-gate return; 5790Sstevel@tonic-gate } 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate debug("type %d", type); 5820Sstevel@tonic-gate switch (type) { 5830Sstevel@tonic-gate case SSH_AGENTC_LOCK: 5840Sstevel@tonic-gate case SSH_AGENTC_UNLOCK: 5850Sstevel@tonic-gate process_lock_agent(e, type == SSH_AGENTC_LOCK); 5860Sstevel@tonic-gate break; 5870Sstevel@tonic-gate /* ssh1 */ 5880Sstevel@tonic-gate case SSH_AGENTC_RSA_CHALLENGE: 5890Sstevel@tonic-gate process_authentication_challenge1(e); 5900Sstevel@tonic-gate break; 5910Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 5920Sstevel@tonic-gate process_request_identities(e, 1); 5930Sstevel@tonic-gate break; 5940Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_IDENTITY: 5950Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: 5960Sstevel@tonic-gate process_add_identity(e, 1); 5970Sstevel@tonic-gate break; 5980Sstevel@tonic-gate case SSH_AGENTC_REMOVE_RSA_IDENTITY: 5990Sstevel@tonic-gate process_remove_identity(e, 1); 6000Sstevel@tonic-gate break; 6010Sstevel@tonic-gate case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: 6020Sstevel@tonic-gate process_remove_all_identities(e, 1); 6030Sstevel@tonic-gate break; 6040Sstevel@tonic-gate /* ssh2 */ 6050Sstevel@tonic-gate case SSH2_AGENTC_SIGN_REQUEST: 6060Sstevel@tonic-gate process_sign_request2(e); 6070Sstevel@tonic-gate break; 6080Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 6090Sstevel@tonic-gate process_request_identities(e, 2); 6100Sstevel@tonic-gate break; 6110Sstevel@tonic-gate case SSH2_AGENTC_ADD_IDENTITY: 6120Sstevel@tonic-gate case SSH2_AGENTC_ADD_ID_CONSTRAINED: 6130Sstevel@tonic-gate process_add_identity(e, 2); 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_IDENTITY: 6160Sstevel@tonic-gate process_remove_identity(e, 2); 6170Sstevel@tonic-gate break; 6180Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: 6190Sstevel@tonic-gate process_remove_all_identities(e, 2); 6200Sstevel@tonic-gate break; 6210Sstevel@tonic-gate default: 6220Sstevel@tonic-gate /* Unknown message. Respond with failure. */ 6230Sstevel@tonic-gate error("Unknown message %d", type); 6240Sstevel@tonic-gate buffer_clear(&e->request); 6250Sstevel@tonic-gate buffer_put_int(&e->output, 1); 6260Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate static void 6320Sstevel@tonic-gate new_socket(sock_type type, int fd) 6330Sstevel@tonic-gate { 6340Sstevel@tonic-gate u_int i, old_alloc; 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 6370Sstevel@tonic-gate error("fcntl O_NONBLOCK: %s", strerror(errno)); 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate if (fd > max_fd) 6400Sstevel@tonic-gate max_fd = fd; 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 6430Sstevel@tonic-gate if (sockets[i].type == AUTH_UNUSED) { 6440Sstevel@tonic-gate sockets[i].fd = fd; 6450Sstevel@tonic-gate sockets[i].type = type; 6460Sstevel@tonic-gate buffer_init(&sockets[i].input); 6470Sstevel@tonic-gate buffer_init(&sockets[i].output); 6480Sstevel@tonic-gate buffer_init(&sockets[i].request); 6490Sstevel@tonic-gate return; 6500Sstevel@tonic-gate } 6510Sstevel@tonic-gate old_alloc = sockets_alloc; 6520Sstevel@tonic-gate sockets_alloc += 10; 6530Sstevel@tonic-gate if (sockets) 6540Sstevel@tonic-gate sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); 6550Sstevel@tonic-gate else 6560Sstevel@tonic-gate sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); 6570Sstevel@tonic-gate for (i = old_alloc; i < sockets_alloc; i++) 6580Sstevel@tonic-gate sockets[i].type = AUTH_UNUSED; 6590Sstevel@tonic-gate sockets[old_alloc].type = type; 6600Sstevel@tonic-gate sockets[old_alloc].fd = fd; 6610Sstevel@tonic-gate buffer_init(&sockets[old_alloc].input); 6620Sstevel@tonic-gate buffer_init(&sockets[old_alloc].output); 6630Sstevel@tonic-gate buffer_init(&sockets[old_alloc].request); 6640Sstevel@tonic-gate } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate static int 6670Sstevel@tonic-gate prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) 6680Sstevel@tonic-gate { 6690Sstevel@tonic-gate u_int i, sz; 6700Sstevel@tonic-gate int n = 0; 6710Sstevel@tonic-gate 6720Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 6730Sstevel@tonic-gate switch (sockets[i].type) { 6740Sstevel@tonic-gate case AUTH_SOCKET: 6750Sstevel@tonic-gate case AUTH_CONNECTION: 6760Sstevel@tonic-gate n = MAX(n, sockets[i].fd); 6770Sstevel@tonic-gate break; 6780Sstevel@tonic-gate case AUTH_UNUSED: 6790Sstevel@tonic-gate break; 6800Sstevel@tonic-gate default: 6810Sstevel@tonic-gate fatal("Unknown socket type %d", sockets[i].type); 6820Sstevel@tonic-gate break; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); 6870Sstevel@tonic-gate if (*fdrp == NULL || sz > *nallocp) { 6880Sstevel@tonic-gate if (*fdrp) 6890Sstevel@tonic-gate xfree(*fdrp); 6900Sstevel@tonic-gate if (*fdwp) 6910Sstevel@tonic-gate xfree(*fdwp); 6920Sstevel@tonic-gate *fdrp = xmalloc(sz); 6930Sstevel@tonic-gate *fdwp = xmalloc(sz); 6940Sstevel@tonic-gate *nallocp = sz; 6950Sstevel@tonic-gate } 6960Sstevel@tonic-gate if (n < *fdl) 6970Sstevel@tonic-gate debug("XXX shrink: %d < %d", n, *fdl); 6980Sstevel@tonic-gate *fdl = n; 6990Sstevel@tonic-gate memset(*fdrp, 0, sz); 7000Sstevel@tonic-gate memset(*fdwp, 0, sz); 7010Sstevel@tonic-gate 7020Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 7030Sstevel@tonic-gate switch (sockets[i].type) { 7040Sstevel@tonic-gate case AUTH_SOCKET: 7050Sstevel@tonic-gate case AUTH_CONNECTION: 7060Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdrp); 7070Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0) 7080Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdwp); 7090Sstevel@tonic-gate break; 7100Sstevel@tonic-gate default: 7110Sstevel@tonic-gate break; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate return (1); 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate 7170Sstevel@tonic-gate static void 7180Sstevel@tonic-gate after_select(fd_set *readset, fd_set *writeset) 7190Sstevel@tonic-gate { 7200Sstevel@tonic-gate struct sockaddr_un sunaddr; 7210Sstevel@tonic-gate socklen_t slen; 7220Sstevel@tonic-gate char buf[1024]; 7230Sstevel@tonic-gate int len, sock; 7240Sstevel@tonic-gate u_int i; 7250Sstevel@tonic-gate uid_t euid; 7260Sstevel@tonic-gate gid_t egid; 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 7290Sstevel@tonic-gate switch (sockets[i].type) { 7300Sstevel@tonic-gate case AUTH_UNUSED: 7310Sstevel@tonic-gate break; 7320Sstevel@tonic-gate case AUTH_SOCKET: 7330Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 7340Sstevel@tonic-gate slen = sizeof(sunaddr); 7350Sstevel@tonic-gate sock = accept(sockets[i].fd, 7360Sstevel@tonic-gate (struct sockaddr *) &sunaddr, &slen); 7370Sstevel@tonic-gate if (sock < 0) { 7380Sstevel@tonic-gate error("accept from AUTH_SOCKET: %s", 7390Sstevel@tonic-gate strerror(errno)); 7400Sstevel@tonic-gate break; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate if (getpeereid(sock, &euid, &egid) < 0) { 7430Sstevel@tonic-gate error("getpeereid %d failed: %s", 7440Sstevel@tonic-gate sock, strerror(errno)); 7450Sstevel@tonic-gate close(sock); 7460Sstevel@tonic-gate break; 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate if ((euid != 0) && (getuid() != euid)) { 7490Sstevel@tonic-gate error("uid mismatch: " 7500Sstevel@tonic-gate "peer euid %u != uid %u", 7510Sstevel@tonic-gate (u_int) euid, (u_int) getuid()); 7520Sstevel@tonic-gate close(sock); 7530Sstevel@tonic-gate break; 7540Sstevel@tonic-gate } 7550Sstevel@tonic-gate new_socket(AUTH_CONNECTION, sock); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate break; 7580Sstevel@tonic-gate case AUTH_CONNECTION: 7590Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0 && 7600Sstevel@tonic-gate FD_ISSET(sockets[i].fd, writeset)) { 7610Sstevel@tonic-gate do { 7620Sstevel@tonic-gate len = write(sockets[i].fd, 7630Sstevel@tonic-gate buffer_ptr(&sockets[i].output), 7640Sstevel@tonic-gate buffer_len(&sockets[i].output)); 7650Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 7660Sstevel@tonic-gate errno == EINTR)) 7670Sstevel@tonic-gate continue; 7680Sstevel@tonic-gate break; 7690Sstevel@tonic-gate } while (1); 7700Sstevel@tonic-gate if (len <= 0) { 7710Sstevel@tonic-gate close_socket(&sockets[i]); 7720Sstevel@tonic-gate break; 7730Sstevel@tonic-gate } 7740Sstevel@tonic-gate buffer_consume(&sockets[i].output, len); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 7770Sstevel@tonic-gate do { 7780Sstevel@tonic-gate len = read(sockets[i].fd, buf, sizeof(buf)); 7790Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 7800Sstevel@tonic-gate errno == EINTR)) 7810Sstevel@tonic-gate continue; 7820Sstevel@tonic-gate break; 7830Sstevel@tonic-gate } while (1); 7840Sstevel@tonic-gate if (len <= 0) { 7850Sstevel@tonic-gate close_socket(&sockets[i]); 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate buffer_append(&sockets[i].input, buf, len); 7890Sstevel@tonic-gate process_message(&sockets[i]); 7900Sstevel@tonic-gate } 7910Sstevel@tonic-gate break; 7920Sstevel@tonic-gate default: 7930Sstevel@tonic-gate fatal("Unknown type %d", sockets[i].type); 7940Sstevel@tonic-gate } 7950Sstevel@tonic-gate } 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate static void 7980Sstevel@tonic-gate cleanup_socket(void *p) 7990Sstevel@tonic-gate { 8000Sstevel@tonic-gate if (socket_name[0]) 8010Sstevel@tonic-gate unlink(socket_name); 8020Sstevel@tonic-gate if (socket_dir[0]) 8030Sstevel@tonic-gate rmdir(socket_dir); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate 8060Sstevel@tonic-gate static void 8070Sstevel@tonic-gate cleanup_exit(int i) 8080Sstevel@tonic-gate { 8090Sstevel@tonic-gate cleanup_socket(NULL); 8100Sstevel@tonic-gate exit(i); 8110Sstevel@tonic-gate } 8120Sstevel@tonic-gate 8130Sstevel@tonic-gate static void 8140Sstevel@tonic-gate cleanup_handler(int sig) 8150Sstevel@tonic-gate { 8160Sstevel@tonic-gate cleanup_socket(NULL); 8170Sstevel@tonic-gate _exit(2); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate static void 8210Sstevel@tonic-gate check_parent_exists(int sig) 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate int save_errno = errno; 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate if (parent_pid != -1 && getppid() != parent_pid) { 8260Sstevel@tonic-gate /* printf("Parent has died - Authentication agent exiting.\n"); */ 8270Sstevel@tonic-gate cleanup_handler(sig); /* safe */ 8280Sstevel@tonic-gate } 8290Sstevel@tonic-gate signal(SIGALRM, check_parent_exists); 8300Sstevel@tonic-gate alarm(10); 8310Sstevel@tonic-gate errno = save_errno; 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate static void 8350Sstevel@tonic-gate usage(void) 8360Sstevel@tonic-gate { 8370Sstevel@tonic-gate fprintf(stderr, 8380Sstevel@tonic-gate gettext("Usage: %s [options] [command [args ...]]\n" 8390Sstevel@tonic-gate "Options:\n" 8400Sstevel@tonic-gate " -c Generate C-shell commands on stdout.\n" 8410Sstevel@tonic-gate " -s Generate Bourne shell commands on stdout.\n" 8420Sstevel@tonic-gate " -k Kill the current agent.\n" 8430Sstevel@tonic-gate " -d Debug mode.\n" 8440Sstevel@tonic-gate " -a socket Bind agent socket to given name.\n"), 8450Sstevel@tonic-gate __progname); 8460Sstevel@tonic-gate exit(1); 8470Sstevel@tonic-gate } 8480Sstevel@tonic-gate 8490Sstevel@tonic-gate int 8500Sstevel@tonic-gate main(int ac, char **av) 8510Sstevel@tonic-gate { 8520Sstevel@tonic-gate int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc; 8530Sstevel@tonic-gate char *shell, *pidstr, *agentsocket = NULL; 8540Sstevel@tonic-gate const char *format; 8550Sstevel@tonic-gate fd_set *readsetp = NULL, *writesetp = NULL; 8560Sstevel@tonic-gate struct sockaddr_un sunaddr; 8570Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 8580Sstevel@tonic-gate struct rlimit rlim; 8590Sstevel@tonic-gate #endif 8600Sstevel@tonic-gate #ifdef HAVE_CYGWIN 8610Sstevel@tonic-gate int prev_mask; 8620Sstevel@tonic-gate #endif 8630Sstevel@tonic-gate extern int optind; 8640Sstevel@tonic-gate extern char *optarg; 8650Sstevel@tonic-gate pid_t pid; 8660Sstevel@tonic-gate char pidstrbuf[1 + 3 * sizeof pid]; 8670Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 8680Sstevel@tonic-gate priv_set_t *myprivs; 8690Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 8700Sstevel@tonic-gate 8710Sstevel@tonic-gate /* drop */ 8720Sstevel@tonic-gate setegid(getgid()); 8730Sstevel@tonic-gate setgid(getgid()); 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate (void) g11n_setlocale(LC_ALL, ""); 8760Sstevel@tonic-gate 8770Sstevel@tonic-gate SSLeay_add_all_algorithms(); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate __progname = get_progname(av[0]); 8800Sstevel@tonic-gate init_rng(); 8810Sstevel@tonic-gate seed_rng(); 8820Sstevel@tonic-gate 8830Sstevel@tonic-gate while ((ch = getopt(ac, av, "cdksa:")) != -1) { 8840Sstevel@tonic-gate switch (ch) { 8850Sstevel@tonic-gate case 'c': 8860Sstevel@tonic-gate if (s_flag) 8870Sstevel@tonic-gate usage(); 8880Sstevel@tonic-gate c_flag++; 8890Sstevel@tonic-gate break; 8900Sstevel@tonic-gate case 'k': 8910Sstevel@tonic-gate k_flag++; 8920Sstevel@tonic-gate break; 8930Sstevel@tonic-gate case 's': 8940Sstevel@tonic-gate if (c_flag) 8950Sstevel@tonic-gate usage(); 8960Sstevel@tonic-gate s_flag++; 8970Sstevel@tonic-gate break; 8980Sstevel@tonic-gate case 'd': 8990Sstevel@tonic-gate if (d_flag) 9000Sstevel@tonic-gate usage(); 9010Sstevel@tonic-gate d_flag++; 9020Sstevel@tonic-gate break; 9030Sstevel@tonic-gate case 'a': 9040Sstevel@tonic-gate agentsocket = optarg; 9050Sstevel@tonic-gate break; 9060Sstevel@tonic-gate default: 9070Sstevel@tonic-gate usage(); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate ac -= optind; 9110Sstevel@tonic-gate av += optind; 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 9140Sstevel@tonic-gate usage(); 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate if (ac == 0 && !c_flag && !s_flag) { 9170Sstevel@tonic-gate shell = getenv("SHELL"); 9180Sstevel@tonic-gate if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) 9190Sstevel@tonic-gate c_flag = 1; 9200Sstevel@tonic-gate } 9210Sstevel@tonic-gate if (k_flag) { 9220Sstevel@tonic-gate pidstr = getenv(SSH_AGENTPID_ENV_NAME); 9230Sstevel@tonic-gate if (pidstr == NULL) { 9240Sstevel@tonic-gate fprintf(stderr, 9250Sstevel@tonic-gate gettext("%s not set, cannot kill agent\n"), 9260Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 9270Sstevel@tonic-gate exit(1); 9280Sstevel@tonic-gate } 9290Sstevel@tonic-gate pid = atoi(pidstr); 9300Sstevel@tonic-gate if (pid < 1) { 9310Sstevel@tonic-gate fprintf(stderr, 9320Sstevel@tonic-gate gettext("%s=\")%s\", which is not a good PID\n"), 9330Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME, pidstr); 9340Sstevel@tonic-gate exit(1); 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate if (kill(pid, SIGTERM) == -1) { 9370Sstevel@tonic-gate perror("kill"); 9380Sstevel@tonic-gate exit(1); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; 9410Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME); 9420Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME); 9430Sstevel@tonic-gate printf("echo "); 9440Sstevel@tonic-gate printf(gettext("Agent pid %ld killed;\n"), (long)pid); 9450Sstevel@tonic-gate exit(0); 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate parent_pid = getpid(); 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate if (agentsocket == NULL) { 9500Sstevel@tonic-gate /* Create private directory for agent socket */ 9510Sstevel@tonic-gate strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); 9520Sstevel@tonic-gate if (mkdtemp(socket_dir) == NULL) { 9530Sstevel@tonic-gate perror("mkdtemp: private socket dir"); 9540Sstevel@tonic-gate exit(1); 9550Sstevel@tonic-gate } 9560Sstevel@tonic-gate snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, 9570Sstevel@tonic-gate (long)parent_pid); 9580Sstevel@tonic-gate } else { 9590Sstevel@tonic-gate /* Try to use specified agent socket */ 9600Sstevel@tonic-gate socket_dir[0] = '\0'; 9610Sstevel@tonic-gate strlcpy(socket_name, agentsocket, sizeof socket_name); 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate /* 9650Sstevel@tonic-gate * Create socket early so it will exist before command gets run from 9660Sstevel@tonic-gate * the parent. 9670Sstevel@tonic-gate */ 9680Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0); 9690Sstevel@tonic-gate if (sock < 0) { 9700Sstevel@tonic-gate perror("socket"); 9710Sstevel@tonic-gate cleanup_exit(1); 9720Sstevel@tonic-gate } 9730Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr)); 9740Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX; 9750Sstevel@tonic-gate strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); 9760Sstevel@tonic-gate #ifdef HAVE_CYGWIN 9770Sstevel@tonic-gate prev_mask = umask(0177); 9780Sstevel@tonic-gate #endif 9790Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { 9800Sstevel@tonic-gate perror("bind"); 9810Sstevel@tonic-gate #ifdef HAVE_CYGWIN 9820Sstevel@tonic-gate umask(prev_mask); 9830Sstevel@tonic-gate #endif 9840Sstevel@tonic-gate cleanup_exit(1); 9850Sstevel@tonic-gate } 9860Sstevel@tonic-gate #ifdef HAVE_CYGWIN 9870Sstevel@tonic-gate umask(prev_mask); 9880Sstevel@tonic-gate #endif 9890Sstevel@tonic-gate if (listen(sock, 128) < 0) { 9900Sstevel@tonic-gate perror("listen"); 9910Sstevel@tonic-gate cleanup_exit(1); 9920Sstevel@tonic-gate } 9930Sstevel@tonic-gate 9940Sstevel@tonic-gate /* 9950Sstevel@tonic-gate * Fork, and have the parent execute the command, if any, or present 9960Sstevel@tonic-gate * the socket data. The child continues as the authentication agent. 9970Sstevel@tonic-gate */ 9980Sstevel@tonic-gate if (d_flag) { 9990Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); 10000Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10010Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10020Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 10030Sstevel@tonic-gate printf("echo "); 10040Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)parent_pid); 10050Sstevel@tonic-gate goto skip; 10060Sstevel@tonic-gate } 10070Sstevel@tonic-gate pid = fork(); 10080Sstevel@tonic-gate if (pid == -1) { 10090Sstevel@tonic-gate perror("fork"); 10100Sstevel@tonic-gate cleanup_exit(1); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate if (pid != 0) { /* Parent - execute the given command. */ 10130Sstevel@tonic-gate close(sock); 10140Sstevel@tonic-gate snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); 10150Sstevel@tonic-gate if (ac == 0) { 10160Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10170Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10180Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 10190Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, 10200Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 10210Sstevel@tonic-gate printf("echo "); 10220Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)pid); 10230Sstevel@tonic-gate exit(0); 10240Sstevel@tonic-gate } 10250Sstevel@tonic-gate if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || 10260Sstevel@tonic-gate setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { 10270Sstevel@tonic-gate perror("setenv"); 10280Sstevel@tonic-gate exit(1); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate execvp(av[0], av); 10310Sstevel@tonic-gate perror(av[0]); 10320Sstevel@tonic-gate exit(1); 10330Sstevel@tonic-gate } 10340Sstevel@tonic-gate /* child */ 10350Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 10380Sstevel@tonic-gate /* 10390Sstevel@tonic-gate * Drop unneeded privs, including basic ones like fork/exec. 10400Sstevel@tonic-gate * 10410Sstevel@tonic-gate * Idiom: remove from 'basic' privs we know we don't want, 10420Sstevel@tonic-gate * invert the result and remove the resulting set from P. 10430Sstevel@tonic-gate * 10440Sstevel@tonic-gate * None of the priv_delset() calls below, nor the setppriv call 10450Sstevel@tonic-gate * below can fail, so their return values are not checked. 10460Sstevel@tonic-gate */ 10470Sstevel@tonic-gate if ((myprivs = priv_str_to_set("basic", ",", NULL)) == NULL) 10480Sstevel@tonic-gate fatal("priv_str_to_set failed: %m"); 10490Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_EXEC); 10500Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_FORK); 10510Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_FILE_LINK_ANY); 10520Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_INFO); 10530Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_SESSION); 10540Sstevel@tonic-gate priv_inverse(myprivs); 10550Sstevel@tonic-gate (void) setppriv(PRIV_OFF, PRIV_PERMITTED, myprivs); 10560Sstevel@tonic-gate (void) priv_freeset(myprivs); 10570Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate if (setsid() == -1) { 10600Sstevel@tonic-gate error("setsid: %s", strerror(errno)); 10610Sstevel@tonic-gate cleanup_exit(1); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate (void)chdir("/"); 10650Sstevel@tonic-gate close(0); 10660Sstevel@tonic-gate close(1); 10670Sstevel@tonic-gate close(2); 10680Sstevel@tonic-gate 10690Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 10700Sstevel@tonic-gate /* deny core dumps, since memory contains unencrypted private keys */ 10710Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max = 0; 10720Sstevel@tonic-gate if (setrlimit(RLIMIT_CORE, &rlim) < 0) { 10730Sstevel@tonic-gate error("setrlimit RLIMIT_CORE: %s", strerror(errno)); 10740Sstevel@tonic-gate cleanup_exit(1); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate #endif 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate skip: 10790Sstevel@tonic-gate fatal_add_cleanup(cleanup_socket, NULL); 10800Sstevel@tonic-gate new_socket(AUTH_SOCKET, sock); 10810Sstevel@tonic-gate if (ac > 0) { 10820Sstevel@tonic-gate signal(SIGALRM, check_parent_exists); 10830Sstevel@tonic-gate alarm(10); 10840Sstevel@tonic-gate } 10850Sstevel@tonic-gate idtab_init(); 10860Sstevel@tonic-gate if (!d_flag) 10870Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 10880Sstevel@tonic-gate signal(SIGPIPE, SIG_IGN); 10890Sstevel@tonic-gate signal(SIGHUP, cleanup_handler); 10900Sstevel@tonic-gate signal(SIGTERM, cleanup_handler); 10910Sstevel@tonic-gate nalloc = 0; 10920Sstevel@tonic-gate 10930Sstevel@tonic-gate while (1) { 10940Sstevel@tonic-gate prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); 10950Sstevel@tonic-gate if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { 10960Sstevel@tonic-gate if (errno == EINTR) 10970Sstevel@tonic-gate continue; 10980Sstevel@tonic-gate fatal("select: %s", strerror(errno)); 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate after_select(readsetp, writesetp); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate /* NOTREACHED */ 11030Sstevel@tonic-gate return (0); /* keep lint happy */ 11040Sstevel@tonic-gate } 1105