17c478bd9Sstevel@tonic-gate /* 26f786aceSNobutomo Nakano * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* 77c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 87c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 97c478bd9Sstevel@tonic-gate * All rights reserved 107c478bd9Sstevel@tonic-gate * The authentication agent program. 117c478bd9Sstevel@tonic-gate * 127c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 137c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 147c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 157c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 167c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 177c478bd9Sstevel@tonic-gate * 187c478bd9Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 217c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions 227c478bd9Sstevel@tonic-gate * are met: 237c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 247c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 257c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 267c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 277c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 287c478bd9Sstevel@tonic-gate * 297c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 307c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 317c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 327c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 337c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 347c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 357c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 367c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 377c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 387c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include "includes.h" 427c478bd9Sstevel@tonic-gate #include "sys-queue.h" 43*ef4d27fbSHuie-Ying Lee RCSID("$OpenBSD: ssh-agent.c,v 1.159 2008/06/28 14:05:15 djm Exp $"); 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 467c478bd9Sstevel@tonic-gate #include <priv.h> 477c478bd9Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #include <openssl/evp.h> 507c478bd9Sstevel@tonic-gate #include <openssl/md5.h> 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #include "ssh.h" 537c478bd9Sstevel@tonic-gate #include "rsa.h" 547c478bd9Sstevel@tonic-gate #include "buffer.h" 557c478bd9Sstevel@tonic-gate #include "bufaux.h" 567c478bd9Sstevel@tonic-gate #include "xmalloc.h" 577c478bd9Sstevel@tonic-gate #include "key.h" 587c478bd9Sstevel@tonic-gate #include "authfd.h" 597c478bd9Sstevel@tonic-gate #include "compat.h" 607c478bd9Sstevel@tonic-gate #include "log.h" 61*ef4d27fbSHuie-Ying Lee #include "readpass.h" 62*ef4d27fbSHuie-Ying Lee #include "misc.h" 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate typedef enum { 657c478bd9Sstevel@tonic-gate AUTH_UNUSED, 667c478bd9Sstevel@tonic-gate AUTH_SOCKET, 677c478bd9Sstevel@tonic-gate AUTH_CONNECTION 687c478bd9Sstevel@tonic-gate } sock_type; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate typedef struct { 717c478bd9Sstevel@tonic-gate int fd; 727c478bd9Sstevel@tonic-gate sock_type type; 737c478bd9Sstevel@tonic-gate Buffer input; 747c478bd9Sstevel@tonic-gate Buffer output; 757c478bd9Sstevel@tonic-gate Buffer request; 767c478bd9Sstevel@tonic-gate } SocketEntry; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate u_int sockets_alloc = 0; 797c478bd9Sstevel@tonic-gate SocketEntry *sockets = NULL; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate typedef struct identity { 827c478bd9Sstevel@tonic-gate TAILQ_ENTRY(identity) next; 837c478bd9Sstevel@tonic-gate Key *key; 847c478bd9Sstevel@tonic-gate char *comment; 857c478bd9Sstevel@tonic-gate u_int death; 86*ef4d27fbSHuie-Ying Lee u_int confirm; 877c478bd9Sstevel@tonic-gate } Identity; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate typedef struct { 907c478bd9Sstevel@tonic-gate int nentries; 917c478bd9Sstevel@tonic-gate TAILQ_HEAD(idqueue, identity) idlist; 927c478bd9Sstevel@tonic-gate } Idtab; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* private key table, one per protocol version */ 957c478bd9Sstevel@tonic-gate Idtab idtable[3]; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate int max_fd = 0; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* pid of shell == parent of agent */ 1007c478bd9Sstevel@tonic-gate pid_t parent_pid = -1; 101*ef4d27fbSHuie-Ying Lee u_int parent_alive_interval = 0; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* pathname and directory for AUTH_SOCKET */ 104*ef4d27fbSHuie-Ying Lee char socket_name[MAXPATHLEN]; 105*ef4d27fbSHuie-Ying Lee char socket_dir[MAXPATHLEN]; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* locking */ 1087c478bd9Sstevel@tonic-gate int locked = 0; 1097c478bd9Sstevel@tonic-gate char *lock_passwd = NULL; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME 1127c478bd9Sstevel@tonic-gate extern char *__progname; 1137c478bd9Sstevel@tonic-gate #else 1147c478bd9Sstevel@tonic-gate char *__progname; 1157c478bd9Sstevel@tonic-gate #endif 1167c478bd9Sstevel@tonic-gate 117*ef4d27fbSHuie-Ying Lee /* Default lifetime (0 == forever) */ 118*ef4d27fbSHuie-Ying Lee static int lifetime = 0; 119*ef4d27fbSHuie-Ying Lee 1207c478bd9Sstevel@tonic-gate static void 1217c478bd9Sstevel@tonic-gate close_socket(SocketEntry *e) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate close(e->fd); 1247c478bd9Sstevel@tonic-gate e->fd = -1; 1257c478bd9Sstevel@tonic-gate e->type = AUTH_UNUSED; 1267c478bd9Sstevel@tonic-gate buffer_free(&e->input); 1277c478bd9Sstevel@tonic-gate buffer_free(&e->output); 1287c478bd9Sstevel@tonic-gate buffer_free(&e->request); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate static void 1327c478bd9Sstevel@tonic-gate idtab_init(void) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate int i; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate for (i = 0; i <=2; i++) { 1377c478bd9Sstevel@tonic-gate TAILQ_INIT(&idtable[i].idlist); 1387c478bd9Sstevel@tonic-gate idtable[i].nentries = 0; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* return private key table for requested protocol version */ 1437c478bd9Sstevel@tonic-gate static Idtab * 1447c478bd9Sstevel@tonic-gate idtab_lookup(int version) 1457c478bd9Sstevel@tonic-gate { 1467c478bd9Sstevel@tonic-gate if (version < 1 || version > 2) 1477c478bd9Sstevel@tonic-gate fatal("internal error, bad protocol version %d", version); 1487c478bd9Sstevel@tonic-gate return &idtable[version]; 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate static void 1527c478bd9Sstevel@tonic-gate free_identity(Identity *id) 1537c478bd9Sstevel@tonic-gate { 1547c478bd9Sstevel@tonic-gate key_free(id->key); 1557c478bd9Sstevel@tonic-gate xfree(id->comment); 1567c478bd9Sstevel@tonic-gate xfree(id); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* return matching private key for given public key */ 1607c478bd9Sstevel@tonic-gate static Identity * 1617c478bd9Sstevel@tonic-gate lookup_identity(Key *key, int version) 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate Identity *id; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1667c478bd9Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 1677c478bd9Sstevel@tonic-gate if (key_equal(key, id->key)) 1687c478bd9Sstevel@tonic-gate return (id); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate return (NULL); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 173*ef4d27fbSHuie-Ying Lee /* Check confirmation of keysign request */ 174*ef4d27fbSHuie-Ying Lee static int 175*ef4d27fbSHuie-Ying Lee confirm_key(Identity *id) 176*ef4d27fbSHuie-Ying Lee { 177*ef4d27fbSHuie-Ying Lee char *p; 178*ef4d27fbSHuie-Ying Lee int ret = -1; 179*ef4d27fbSHuie-Ying Lee 180*ef4d27fbSHuie-Ying Lee p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX); 181*ef4d27fbSHuie-Ying Lee if (ask_permission( 182*ef4d27fbSHuie-Ying Lee gettext("Allow use of key %s?\nKey fingerprint %s."), 183*ef4d27fbSHuie-Ying Lee id->comment, p)) 184*ef4d27fbSHuie-Ying Lee ret = 0; 185*ef4d27fbSHuie-Ying Lee xfree(p); 186*ef4d27fbSHuie-Ying Lee 187*ef4d27fbSHuie-Ying Lee return (ret); 188*ef4d27fbSHuie-Ying Lee } 189*ef4d27fbSHuie-Ying Lee 1907c478bd9Sstevel@tonic-gate /* send list of supported public keys to 'client' */ 1917c478bd9Sstevel@tonic-gate static void 1927c478bd9Sstevel@tonic-gate process_request_identities(SocketEntry *e, int version) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 1957c478bd9Sstevel@tonic-gate Identity *id; 1967c478bd9Sstevel@tonic-gate Buffer msg; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate buffer_init(&msg); 1997c478bd9Sstevel@tonic-gate buffer_put_char(&msg, (version == 1) ? 2007c478bd9Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 2017c478bd9Sstevel@tonic-gate buffer_put_int(&msg, tab->nentries); 2027c478bd9Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 2037c478bd9Sstevel@tonic-gate if (id->key->type == KEY_RSA1) { 2047c478bd9Sstevel@tonic-gate buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); 2057c478bd9Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->e); 2067c478bd9Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->n); 2077c478bd9Sstevel@tonic-gate } else { 2087c478bd9Sstevel@tonic-gate u_char *blob; 2097c478bd9Sstevel@tonic-gate u_int blen; 2107c478bd9Sstevel@tonic-gate key_to_blob(id->key, &blob, &blen); 2117c478bd9Sstevel@tonic-gate buffer_put_string(&msg, blob, blen); 2127c478bd9Sstevel@tonic-gate xfree(blob); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate buffer_put_cstring(&msg, id->comment); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 2177c478bd9Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 2187c478bd9Sstevel@tonic-gate buffer_free(&msg); 2197c478bd9Sstevel@tonic-gate } 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* ssh1 only */ 2227c478bd9Sstevel@tonic-gate static void 2237c478bd9Sstevel@tonic-gate process_authentication_challenge1(SocketEntry *e) 2247c478bd9Sstevel@tonic-gate { 2257c478bd9Sstevel@tonic-gate u_char buf[32], mdbuf[16], session_id[16]; 2267c478bd9Sstevel@tonic-gate u_int response_type; 2277c478bd9Sstevel@tonic-gate BIGNUM *challenge; 2287c478bd9Sstevel@tonic-gate Identity *id; 2297c478bd9Sstevel@tonic-gate int i, len; 2307c478bd9Sstevel@tonic-gate Buffer msg; 2317c478bd9Sstevel@tonic-gate MD5_CTX md; 2327c478bd9Sstevel@tonic-gate Key *key; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate buffer_init(&msg); 2357c478bd9Sstevel@tonic-gate key = key_new(KEY_RSA1); 2367c478bd9Sstevel@tonic-gate if ((challenge = BN_new()) == NULL) 2377c478bd9Sstevel@tonic-gate fatal("process_authentication_challenge1: BN_new failed"); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 2407c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 2417c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 2427c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, challenge); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* Only protocol 1.1 is supported */ 2457c478bd9Sstevel@tonic-gate if (buffer_len(&e->request) == 0) 2467c478bd9Sstevel@tonic-gate goto failure; 2477c478bd9Sstevel@tonic-gate buffer_get(&e->request, session_id, 16); 2487c478bd9Sstevel@tonic-gate response_type = buffer_get_int(&e->request); 2497c478bd9Sstevel@tonic-gate if (response_type != 1) 2507c478bd9Sstevel@tonic-gate goto failure; 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate id = lookup_identity(key, 1); 253*ef4d27fbSHuie-Ying Lee if (id != NULL && (!id->confirm || confirm_key(id) == 0)) { 2547c478bd9Sstevel@tonic-gate Key *private = id->key; 2557c478bd9Sstevel@tonic-gate /* Decrypt the challenge using the private key. */ 2567c478bd9Sstevel@tonic-gate if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) 2577c478bd9Sstevel@tonic-gate goto failure; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate /* The response is MD5 of decrypted challenge plus session id. */ 2607c478bd9Sstevel@tonic-gate len = BN_num_bytes(challenge); 2617c478bd9Sstevel@tonic-gate if (len <= 0 || len > 32) { 2627c478bd9Sstevel@tonic-gate log("process_authentication_challenge: bad challenge length %d", len); 2637c478bd9Sstevel@tonic-gate goto failure; 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate memset(buf, 0, 32); 2667c478bd9Sstevel@tonic-gate BN_bn2bin(challenge, buf + 32 - len); 2677c478bd9Sstevel@tonic-gate MD5_Init(&md); 2687c478bd9Sstevel@tonic-gate MD5_Update(&md, buf, 32); 2697c478bd9Sstevel@tonic-gate MD5_Update(&md, session_id, 16); 2707c478bd9Sstevel@tonic-gate MD5_Final(mdbuf, &md); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate /* Send the response. */ 2737c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); 2747c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++) 2757c478bd9Sstevel@tonic-gate buffer_put_char(&msg, mdbuf[i]); 2767c478bd9Sstevel@tonic-gate goto send; 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate failure: 2807c478bd9Sstevel@tonic-gate /* Unknown identity or protocol error. Send failure. */ 2817c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 2827c478bd9Sstevel@tonic-gate send: 2837c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 2847c478bd9Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 2857c478bd9Sstevel@tonic-gate key_free(key); 2867c478bd9Sstevel@tonic-gate BN_clear_free(challenge); 2877c478bd9Sstevel@tonic-gate buffer_free(&msg); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate /* ssh2 only */ 2917c478bd9Sstevel@tonic-gate static void 2927c478bd9Sstevel@tonic-gate process_sign_request2(SocketEntry *e) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate u_char *blob, *data, *signature = NULL; 2957c478bd9Sstevel@tonic-gate u_int blen, dlen, slen = 0; 296*ef4d27fbSHuie-Ying Lee extern uint32_t datafellows; 297*ef4d27fbSHuie-Ying Lee int odatafellows; 2987c478bd9Sstevel@tonic-gate int ok = -1, flags; 2997c478bd9Sstevel@tonic-gate Buffer msg; 3007c478bd9Sstevel@tonic-gate Key *key; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate datafellows = 0; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 3057c478bd9Sstevel@tonic-gate data = buffer_get_string(&e->request, &dlen); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate flags = buffer_get_int(&e->request); 308*ef4d27fbSHuie-Ying Lee odatafellows = datafellows; 3097c478bd9Sstevel@tonic-gate if (flags & SSH_AGENT_OLD_SIGNATURE) 3107c478bd9Sstevel@tonic-gate datafellows = SSH_BUG_SIGBLOB; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate key = key_from_blob(blob, blen); 3137c478bd9Sstevel@tonic-gate if (key != NULL) { 3147c478bd9Sstevel@tonic-gate Identity *id = lookup_identity(key, 2); 315*ef4d27fbSHuie-Ying Lee if (id != NULL && (!id->confirm || confirm_key(id) == 0)) 3167c478bd9Sstevel@tonic-gate ok = key_sign(id->key, &signature, &slen, data, dlen); 3177c478bd9Sstevel@tonic-gate key_free(key); 318*ef4d27fbSHuie-Ying Lee } 3197c478bd9Sstevel@tonic-gate buffer_init(&msg); 3207c478bd9Sstevel@tonic-gate if (ok == 0) { 3217c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); 3227c478bd9Sstevel@tonic-gate buffer_put_string(&msg, signature, slen); 3237c478bd9Sstevel@tonic-gate } else { 3247c478bd9Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 3277c478bd9Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), 3287c478bd9Sstevel@tonic-gate buffer_len(&msg)); 3297c478bd9Sstevel@tonic-gate buffer_free(&msg); 3307c478bd9Sstevel@tonic-gate xfree(data); 3317c478bd9Sstevel@tonic-gate xfree(blob); 3327c478bd9Sstevel@tonic-gate if (signature != NULL) 3337c478bd9Sstevel@tonic-gate xfree(signature); 334*ef4d27fbSHuie-Ying Lee datafellows = odatafellows; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate /* shared */ 3387c478bd9Sstevel@tonic-gate static void 3397c478bd9Sstevel@tonic-gate process_remove_identity(SocketEntry *e, int version) 3407c478bd9Sstevel@tonic-gate { 3417c478bd9Sstevel@tonic-gate u_int blen, bits; 3427c478bd9Sstevel@tonic-gate int success = 0; 3437c478bd9Sstevel@tonic-gate Key *key = NULL; 3447c478bd9Sstevel@tonic-gate u_char *blob; 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate switch (version) { 3477c478bd9Sstevel@tonic-gate case 1: 3487c478bd9Sstevel@tonic-gate key = key_new(KEY_RSA1); 3497c478bd9Sstevel@tonic-gate bits = buffer_get_int(&e->request); 3507c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 3517c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate if (bits != key_size(key)) 3547c478bd9Sstevel@tonic-gate log("Warning: identity keysize mismatch: actual %u, announced %u", 3557c478bd9Sstevel@tonic-gate key_size(key), bits); 3567c478bd9Sstevel@tonic-gate break; 3577c478bd9Sstevel@tonic-gate case 2: 3587c478bd9Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 3597c478bd9Sstevel@tonic-gate key = key_from_blob(blob, blen); 3607c478bd9Sstevel@tonic-gate xfree(blob); 3617c478bd9Sstevel@tonic-gate break; 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate if (key != NULL) { 3647c478bd9Sstevel@tonic-gate Identity *id = lookup_identity(key, version); 3657c478bd9Sstevel@tonic-gate if (id != NULL) { 3667c478bd9Sstevel@tonic-gate /* 3677c478bd9Sstevel@tonic-gate * We have this key. Free the old key. Since we 368*ef4d27fbSHuie-Ying Lee * don't want to leave empty slots in the middle of 3697c478bd9Sstevel@tonic-gate * the array, we actually free the key there and move 3707c478bd9Sstevel@tonic-gate * all the entries between the empty slot and the end 3717c478bd9Sstevel@tonic-gate * of the array. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3747c478bd9Sstevel@tonic-gate if (tab->nentries < 1) 3757c478bd9Sstevel@tonic-gate fatal("process_remove_identity: " 3767c478bd9Sstevel@tonic-gate "internal error: tab->nentries %d", 3777c478bd9Sstevel@tonic-gate tab->nentries); 3787c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 3797c478bd9Sstevel@tonic-gate free_identity(id); 3807c478bd9Sstevel@tonic-gate tab->nentries--; 3817c478bd9Sstevel@tonic-gate success = 1; 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate key_free(key); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 3867c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, 3877c478bd9Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate static void 3917c478bd9Sstevel@tonic-gate process_remove_all_identities(SocketEntry *e, int version) 3927c478bd9Sstevel@tonic-gate { 3937c478bd9Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 3947c478bd9Sstevel@tonic-gate Identity *id; 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate /* Loop over all identities and clear the keys. */ 3977c478bd9Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; 3987c478bd9Sstevel@tonic-gate id = TAILQ_FIRST(&tab->idlist)) { 3997c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 4007c478bd9Sstevel@tonic-gate free_identity(id); 4017c478bd9Sstevel@tonic-gate } 4027c478bd9Sstevel@tonic-gate 4037c478bd9Sstevel@tonic-gate /* Mark that there are no identities. */ 4047c478bd9Sstevel@tonic-gate tab->nentries = 0; 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate /* Send success. */ 4077c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 4087c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_SUCCESS); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 411*ef4d27fbSHuie-Ying Lee /* removes expired keys and returns number of seconds until the next expiry */ 412*ef4d27fbSHuie-Ying Lee static u_int 4137c478bd9Sstevel@tonic-gate reaper(void) 4147c478bd9Sstevel@tonic-gate { 415*ef4d27fbSHuie-Ying Lee u_int deadline = 0, now = time(NULL); 4167c478bd9Sstevel@tonic-gate Identity *id, *nxt; 4177c478bd9Sstevel@tonic-gate int version; 4187c478bd9Sstevel@tonic-gate Idtab *tab; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate for (version = 1; version < 3; version++) { 4217c478bd9Sstevel@tonic-gate tab = idtab_lookup(version); 4227c478bd9Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { 4237c478bd9Sstevel@tonic-gate nxt = TAILQ_NEXT(id, next); 424*ef4d27fbSHuie-Ying Lee if (id->death == 0) 425*ef4d27fbSHuie-Ying Lee continue; 426*ef4d27fbSHuie-Ying Lee if (now >= id->death) { 427*ef4d27fbSHuie-Ying Lee debug("expiring key '%s'", id->comment); 4287c478bd9Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 4297c478bd9Sstevel@tonic-gate free_identity(id); 4307c478bd9Sstevel@tonic-gate tab->nentries--; 431*ef4d27fbSHuie-Ying Lee } else 432*ef4d27fbSHuie-Ying Lee deadline = (deadline == 0) ? id->death : 433*ef4d27fbSHuie-Ying Lee MIN(deadline, id->death); 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate } 436*ef4d27fbSHuie-Ying Lee if (deadline == 0 || deadline <= now) 437*ef4d27fbSHuie-Ying Lee return 0; 438*ef4d27fbSHuie-Ying Lee else 439*ef4d27fbSHuie-Ying Lee return (deadline - now); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate static void 4437c478bd9Sstevel@tonic-gate process_add_identity(SocketEntry *e, int version) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 446*ef4d27fbSHuie-Ying Lee Identity *id; 447*ef4d27fbSHuie-Ying Lee int type, success = 0, death = 0, confirm = 0; 4487c478bd9Sstevel@tonic-gate char *type_name, *comment; 4497c478bd9Sstevel@tonic-gate Key *k = NULL; 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate switch (version) { 4527c478bd9Sstevel@tonic-gate case 1: 4537c478bd9Sstevel@tonic-gate k = key_new_private(KEY_RSA1); 4547c478bd9Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 4557c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->n); 4567c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->e); 4577c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->d); 4587c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->iqmp); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate /* SSH and SSL have p and q swapped */ 4617c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->q); /* p */ 4627c478bd9Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->p); /* q */ 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate /* Generate additional parameters */ 4657c478bd9Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4667c478bd9Sstevel@tonic-gate break; 4677c478bd9Sstevel@tonic-gate case 2: 4687c478bd9Sstevel@tonic-gate type_name = buffer_get_string(&e->request, NULL); 4697c478bd9Sstevel@tonic-gate type = key_type_from_name(type_name); 4707c478bd9Sstevel@tonic-gate xfree(type_name); 4717c478bd9Sstevel@tonic-gate switch (type) { 4727c478bd9Sstevel@tonic-gate case KEY_DSA: 4737c478bd9Sstevel@tonic-gate k = key_new_private(type); 4747c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->p); 4757c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->q); 4767c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->g); 4777c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->pub_key); 4787c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->priv_key); 4797c478bd9Sstevel@tonic-gate break; 4807c478bd9Sstevel@tonic-gate case KEY_RSA: 4817c478bd9Sstevel@tonic-gate k = key_new_private(type); 4827c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->n); 4837c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->e); 4847c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->d); 4857c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->iqmp); 4867c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->p); 4877c478bd9Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->q); 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate /* Generate additional parameters */ 4907c478bd9Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 4917c478bd9Sstevel@tonic-gate break; 4927c478bd9Sstevel@tonic-gate default: 4937c478bd9Sstevel@tonic-gate buffer_clear(&e->request); 4947c478bd9Sstevel@tonic-gate goto send; 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate break; 4977c478bd9Sstevel@tonic-gate } 498*ef4d27fbSHuie-Ying Lee /* enable blinding */ 499*ef4d27fbSHuie-Ying Lee switch (k->type) { 500*ef4d27fbSHuie-Ying Lee case KEY_RSA: 501*ef4d27fbSHuie-Ying Lee case KEY_RSA1: 502*ef4d27fbSHuie-Ying Lee if (RSA_blinding_on(k->rsa, NULL) != 1) { 503*ef4d27fbSHuie-Ying Lee error("process_add_identity: RSA_blinding_on failed"); 504*ef4d27fbSHuie-Ying Lee key_free(k); 505*ef4d27fbSHuie-Ying Lee goto send; 506*ef4d27fbSHuie-Ying Lee } 507*ef4d27fbSHuie-Ying Lee break; 508*ef4d27fbSHuie-Ying Lee } 5097c478bd9Sstevel@tonic-gate comment = buffer_get_string(&e->request, NULL); 5107c478bd9Sstevel@tonic-gate if (k == NULL) { 5117c478bd9Sstevel@tonic-gate xfree(comment); 5127c478bd9Sstevel@tonic-gate goto send; 5137c478bd9Sstevel@tonic-gate } 5147c478bd9Sstevel@tonic-gate while (buffer_len(&e->request)) { 515*ef4d27fbSHuie-Ying Lee switch ((type = buffer_get_char(&e->request))) { 5167c478bd9Sstevel@tonic-gate case SSH_AGENT_CONSTRAIN_LIFETIME: 5177c478bd9Sstevel@tonic-gate death = time(NULL) + buffer_get_int(&e->request); 5187c478bd9Sstevel@tonic-gate break; 519*ef4d27fbSHuie-Ying Lee case SSH_AGENT_CONSTRAIN_CONFIRM: 520*ef4d27fbSHuie-Ying Lee confirm = 1; 5217c478bd9Sstevel@tonic-gate break; 522*ef4d27fbSHuie-Ying Lee default: 523*ef4d27fbSHuie-Ying Lee error("process_add_identity: " 524*ef4d27fbSHuie-Ying Lee "Unknown constraint type %d", type); 525*ef4d27fbSHuie-Ying Lee xfree(comment); 526*ef4d27fbSHuie-Ying Lee key_free(k); 527*ef4d27fbSHuie-Ying Lee goto send; 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate } 530*ef4d27fbSHuie-Ying Lee success = 1; 531*ef4d27fbSHuie-Ying Lee if (lifetime && !death) 532*ef4d27fbSHuie-Ying Lee death = time(NULL) + lifetime; 533*ef4d27fbSHuie-Ying Lee if ((id = lookup_identity(k, version)) == NULL) { 534*ef4d27fbSHuie-Ying Lee id = xmalloc(sizeof(Identity)); 5357c478bd9Sstevel@tonic-gate id->key = k; 5367c478bd9Sstevel@tonic-gate TAILQ_INSERT_TAIL(&tab->idlist, id, next); 5377c478bd9Sstevel@tonic-gate /* Increment the number of identities. */ 5387c478bd9Sstevel@tonic-gate tab->nentries++; 5397c478bd9Sstevel@tonic-gate } else { 5407c478bd9Sstevel@tonic-gate key_free(k); 541*ef4d27fbSHuie-Ying Lee xfree(id->comment); 5427c478bd9Sstevel@tonic-gate } 543*ef4d27fbSHuie-Ying Lee id->comment = comment; 544*ef4d27fbSHuie-Ying Lee id->death = death; 545*ef4d27fbSHuie-Ying Lee id->confirm = confirm; 5467c478bd9Sstevel@tonic-gate send: 5477c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5487c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, 5497c478bd9Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate /* XXX todo: encrypt sensitive data with passphrase */ 5537c478bd9Sstevel@tonic-gate static void 5547c478bd9Sstevel@tonic-gate process_lock_agent(SocketEntry *e, int lock) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate int success = 0; 5577c478bd9Sstevel@tonic-gate char *passwd; 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate passwd = buffer_get_string(&e->request, NULL); 5607c478bd9Sstevel@tonic-gate if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { 5617c478bd9Sstevel@tonic-gate locked = 0; 5627c478bd9Sstevel@tonic-gate memset(lock_passwd, 0, strlen(lock_passwd)); 5637c478bd9Sstevel@tonic-gate xfree(lock_passwd); 5647c478bd9Sstevel@tonic-gate lock_passwd = NULL; 5657c478bd9Sstevel@tonic-gate success = 1; 5667c478bd9Sstevel@tonic-gate } else if (!locked && lock) { 5677c478bd9Sstevel@tonic-gate locked = 1; 5687c478bd9Sstevel@tonic-gate lock_passwd = xstrdup(passwd); 5697c478bd9Sstevel@tonic-gate success = 1; 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate memset(passwd, 0, strlen(passwd)); 5727c478bd9Sstevel@tonic-gate xfree(passwd); 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 5757c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, 5767c478bd9Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate static void 5807c478bd9Sstevel@tonic-gate no_identities(SocketEntry *e, u_int type) 5817c478bd9Sstevel@tonic-gate { 5827c478bd9Sstevel@tonic-gate Buffer msg; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate buffer_init(&msg); 5857c478bd9Sstevel@tonic-gate buffer_put_char(&msg, 5867c478bd9Sstevel@tonic-gate (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? 5877c478bd9Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 5887c478bd9Sstevel@tonic-gate buffer_put_int(&msg, 0); 5897c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 5907c478bd9Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 5917c478bd9Sstevel@tonic-gate buffer_free(&msg); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate /* dispatch incoming messages */ 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate static void 5977c478bd9Sstevel@tonic-gate process_message(SocketEntry *e) 5987c478bd9Sstevel@tonic-gate { 5997c478bd9Sstevel@tonic-gate u_int msg_len, type; 6007c478bd9Sstevel@tonic-gate u_char *cp; 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if (buffer_len(&e->input) < 5) 6037c478bd9Sstevel@tonic-gate return; /* Incomplete message. */ 6047c478bd9Sstevel@tonic-gate cp = buffer_ptr(&e->input); 605*ef4d27fbSHuie-Ying Lee msg_len = get_u32(cp); 6067c478bd9Sstevel@tonic-gate if (msg_len > 256 * 1024) { 6077c478bd9Sstevel@tonic-gate close_socket(e); 6087c478bd9Sstevel@tonic-gate return; 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate if (buffer_len(&e->input) < msg_len + 4) 6117c478bd9Sstevel@tonic-gate return; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* move the current input to e->request */ 6147c478bd9Sstevel@tonic-gate buffer_consume(&e->input, 4); 6157c478bd9Sstevel@tonic-gate buffer_clear(&e->request); 6167c478bd9Sstevel@tonic-gate buffer_append(&e->request, buffer_ptr(&e->input), msg_len); 6177c478bd9Sstevel@tonic-gate buffer_consume(&e->input, msg_len); 6187c478bd9Sstevel@tonic-gate type = buffer_get_char(&e->request); 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate /* check wheter agent is locked */ 6217c478bd9Sstevel@tonic-gate if (locked && type != SSH_AGENTC_UNLOCK) { 6227c478bd9Sstevel@tonic-gate buffer_clear(&e->request); 6237c478bd9Sstevel@tonic-gate switch (type) { 6247c478bd9Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 6257c478bd9Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 6267c478bd9Sstevel@tonic-gate /* send empty lists */ 6277c478bd9Sstevel@tonic-gate no_identities(e, type); 6287c478bd9Sstevel@tonic-gate break; 6297c478bd9Sstevel@tonic-gate default: 6307c478bd9Sstevel@tonic-gate /* send a fail message for all other request types */ 6317c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 6327c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 6337c478bd9Sstevel@tonic-gate } 6347c478bd9Sstevel@tonic-gate return; 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate debug("type %d", type); 6387c478bd9Sstevel@tonic-gate switch (type) { 6397c478bd9Sstevel@tonic-gate case SSH_AGENTC_LOCK: 6407c478bd9Sstevel@tonic-gate case SSH_AGENTC_UNLOCK: 6417c478bd9Sstevel@tonic-gate process_lock_agent(e, type == SSH_AGENTC_LOCK); 6427c478bd9Sstevel@tonic-gate break; 6437c478bd9Sstevel@tonic-gate /* ssh1 */ 6447c478bd9Sstevel@tonic-gate case SSH_AGENTC_RSA_CHALLENGE: 6457c478bd9Sstevel@tonic-gate process_authentication_challenge1(e); 6467c478bd9Sstevel@tonic-gate break; 6477c478bd9Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 6487c478bd9Sstevel@tonic-gate process_request_identities(e, 1); 6497c478bd9Sstevel@tonic-gate break; 6507c478bd9Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_IDENTITY: 6517c478bd9Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: 6527c478bd9Sstevel@tonic-gate process_add_identity(e, 1); 6537c478bd9Sstevel@tonic-gate break; 6547c478bd9Sstevel@tonic-gate case SSH_AGENTC_REMOVE_RSA_IDENTITY: 6557c478bd9Sstevel@tonic-gate process_remove_identity(e, 1); 6567c478bd9Sstevel@tonic-gate break; 6577c478bd9Sstevel@tonic-gate case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: 6587c478bd9Sstevel@tonic-gate process_remove_all_identities(e, 1); 6597c478bd9Sstevel@tonic-gate break; 6607c478bd9Sstevel@tonic-gate /* ssh2 */ 6617c478bd9Sstevel@tonic-gate case SSH2_AGENTC_SIGN_REQUEST: 6627c478bd9Sstevel@tonic-gate process_sign_request2(e); 6637c478bd9Sstevel@tonic-gate break; 6647c478bd9Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 6657c478bd9Sstevel@tonic-gate process_request_identities(e, 2); 6667c478bd9Sstevel@tonic-gate break; 6677c478bd9Sstevel@tonic-gate case SSH2_AGENTC_ADD_IDENTITY: 6687c478bd9Sstevel@tonic-gate case SSH2_AGENTC_ADD_ID_CONSTRAINED: 6697c478bd9Sstevel@tonic-gate process_add_identity(e, 2); 6707c478bd9Sstevel@tonic-gate break; 6717c478bd9Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_IDENTITY: 6727c478bd9Sstevel@tonic-gate process_remove_identity(e, 2); 6737c478bd9Sstevel@tonic-gate break; 6747c478bd9Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: 6757c478bd9Sstevel@tonic-gate process_remove_all_identities(e, 2); 6767c478bd9Sstevel@tonic-gate break; 6777c478bd9Sstevel@tonic-gate default: 6787c478bd9Sstevel@tonic-gate /* Unknown message. Respond with failure. */ 6797c478bd9Sstevel@tonic-gate error("Unknown message %d", type); 6807c478bd9Sstevel@tonic-gate buffer_clear(&e->request); 6817c478bd9Sstevel@tonic-gate buffer_put_int(&e->output, 1); 6827c478bd9Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 6837c478bd9Sstevel@tonic-gate break; 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate static void 6887c478bd9Sstevel@tonic-gate new_socket(sock_type type, int fd) 6897c478bd9Sstevel@tonic-gate { 690*ef4d27fbSHuie-Ying Lee u_int i, old_alloc, new_alloc; 6917c478bd9Sstevel@tonic-gate 692*ef4d27fbSHuie-Ying Lee set_nonblock(fd); 6937c478bd9Sstevel@tonic-gate 6947c478bd9Sstevel@tonic-gate if (fd > max_fd) 6957c478bd9Sstevel@tonic-gate max_fd = fd; 6967c478bd9Sstevel@tonic-gate 6977c478bd9Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 6987c478bd9Sstevel@tonic-gate if (sockets[i].type == AUTH_UNUSED) { 6997c478bd9Sstevel@tonic-gate sockets[i].fd = fd; 7007c478bd9Sstevel@tonic-gate buffer_init(&sockets[i].input); 7017c478bd9Sstevel@tonic-gate buffer_init(&sockets[i].output); 7027c478bd9Sstevel@tonic-gate buffer_init(&sockets[i].request); 703*ef4d27fbSHuie-Ying Lee sockets[i].type = type; 7047c478bd9Sstevel@tonic-gate return; 7057c478bd9Sstevel@tonic-gate } 7067c478bd9Sstevel@tonic-gate old_alloc = sockets_alloc; 707*ef4d27fbSHuie-Ying Lee new_alloc = sockets_alloc + 10; 708*ef4d27fbSHuie-Ying Lee sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0])); 709*ef4d27fbSHuie-Ying Lee for (i = old_alloc; i < new_alloc; i++) 7107c478bd9Sstevel@tonic-gate sockets[i].type = AUTH_UNUSED; 711*ef4d27fbSHuie-Ying Lee sockets_alloc = new_alloc; 7127c478bd9Sstevel@tonic-gate sockets[old_alloc].fd = fd; 7137c478bd9Sstevel@tonic-gate buffer_init(&sockets[old_alloc].input); 7147c478bd9Sstevel@tonic-gate buffer_init(&sockets[old_alloc].output); 7157c478bd9Sstevel@tonic-gate buffer_init(&sockets[old_alloc].request); 716*ef4d27fbSHuie-Ying Lee sockets[old_alloc].type = type; 7177c478bd9Sstevel@tonic-gate } 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate static int 720*ef4d27fbSHuie-Ying Lee prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp, 721*ef4d27fbSHuie-Ying Lee struct timeval **tvpp) 7227c478bd9Sstevel@tonic-gate { 723*ef4d27fbSHuie-Ying Lee u_int i, sz, deadline; 7247c478bd9Sstevel@tonic-gate int n = 0; 725*ef4d27fbSHuie-Ying Lee static struct timeval tv; 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 7287c478bd9Sstevel@tonic-gate switch (sockets[i].type) { 7297c478bd9Sstevel@tonic-gate case AUTH_SOCKET: 7307c478bd9Sstevel@tonic-gate case AUTH_CONNECTION: 7317c478bd9Sstevel@tonic-gate n = MAX(n, sockets[i].fd); 7327c478bd9Sstevel@tonic-gate break; 7337c478bd9Sstevel@tonic-gate case AUTH_UNUSED: 7347c478bd9Sstevel@tonic-gate break; 7357c478bd9Sstevel@tonic-gate default: 7367c478bd9Sstevel@tonic-gate fatal("Unknown socket type %d", sockets[i].type); 7377c478bd9Sstevel@tonic-gate break; 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); 7427c478bd9Sstevel@tonic-gate if (*fdrp == NULL || sz > *nallocp) { 7437c478bd9Sstevel@tonic-gate if (*fdrp) 7447c478bd9Sstevel@tonic-gate xfree(*fdrp); 7457c478bd9Sstevel@tonic-gate if (*fdwp) 7467c478bd9Sstevel@tonic-gate xfree(*fdwp); 7477c478bd9Sstevel@tonic-gate *fdrp = xmalloc(sz); 7487c478bd9Sstevel@tonic-gate *fdwp = xmalloc(sz); 7497c478bd9Sstevel@tonic-gate *nallocp = sz; 7507c478bd9Sstevel@tonic-gate } 7517c478bd9Sstevel@tonic-gate if (n < *fdl) 7527c478bd9Sstevel@tonic-gate debug("XXX shrink: %d < %d", n, *fdl); 7537c478bd9Sstevel@tonic-gate *fdl = n; 7547c478bd9Sstevel@tonic-gate memset(*fdrp, 0, sz); 7557c478bd9Sstevel@tonic-gate memset(*fdwp, 0, sz); 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 7587c478bd9Sstevel@tonic-gate switch (sockets[i].type) { 7597c478bd9Sstevel@tonic-gate case AUTH_SOCKET: 7607c478bd9Sstevel@tonic-gate case AUTH_CONNECTION: 7617c478bd9Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdrp); 7627c478bd9Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0) 7637c478bd9Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdwp); 7647c478bd9Sstevel@tonic-gate break; 7657c478bd9Sstevel@tonic-gate default: 7667c478bd9Sstevel@tonic-gate break; 7677c478bd9Sstevel@tonic-gate } 7687c478bd9Sstevel@tonic-gate } 769*ef4d27fbSHuie-Ying Lee deadline = reaper(); 770*ef4d27fbSHuie-Ying Lee if (parent_alive_interval != 0) 771*ef4d27fbSHuie-Ying Lee deadline = (deadline == 0) ? parent_alive_interval : 772*ef4d27fbSHuie-Ying Lee MIN(deadline, parent_alive_interval); 773*ef4d27fbSHuie-Ying Lee if (deadline == 0) { 774*ef4d27fbSHuie-Ying Lee *tvpp = NULL; 775*ef4d27fbSHuie-Ying Lee } else { 776*ef4d27fbSHuie-Ying Lee tv.tv_sec = deadline; 777*ef4d27fbSHuie-Ying Lee tv.tv_usec = 0; 778*ef4d27fbSHuie-Ying Lee *tvpp = &tv; 779*ef4d27fbSHuie-Ying Lee } 7807c478bd9Sstevel@tonic-gate return (1); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate static void 7847c478bd9Sstevel@tonic-gate after_select(fd_set *readset, fd_set *writeset) 7857c478bd9Sstevel@tonic-gate { 7867c478bd9Sstevel@tonic-gate struct sockaddr_un sunaddr; 7877c478bd9Sstevel@tonic-gate socklen_t slen; 7887c478bd9Sstevel@tonic-gate char buf[1024]; 7897c478bd9Sstevel@tonic-gate int len, sock; 7907c478bd9Sstevel@tonic-gate u_int i; 7917c478bd9Sstevel@tonic-gate uid_t euid; 7927c478bd9Sstevel@tonic-gate gid_t egid; 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 7957c478bd9Sstevel@tonic-gate switch (sockets[i].type) { 7967c478bd9Sstevel@tonic-gate case AUTH_UNUSED: 7977c478bd9Sstevel@tonic-gate break; 7987c478bd9Sstevel@tonic-gate case AUTH_SOCKET: 7997c478bd9Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 8007c478bd9Sstevel@tonic-gate slen = sizeof(sunaddr); 8017c478bd9Sstevel@tonic-gate sock = accept(sockets[i].fd, 8027c478bd9Sstevel@tonic-gate (struct sockaddr *)&sunaddr, &slen); 8037c478bd9Sstevel@tonic-gate if (sock < 0) { 8047c478bd9Sstevel@tonic-gate error("accept from AUTH_SOCKET: %s", 8057c478bd9Sstevel@tonic-gate strerror(errno)); 8067c478bd9Sstevel@tonic-gate break; 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate if (getpeereid(sock, &euid, &egid) < 0) { 8097c478bd9Sstevel@tonic-gate error("getpeereid %d failed: %s", 8107c478bd9Sstevel@tonic-gate sock, strerror(errno)); 8117c478bd9Sstevel@tonic-gate close(sock); 8127c478bd9Sstevel@tonic-gate break; 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate if ((euid != 0) && (getuid() != euid)) { 8157c478bd9Sstevel@tonic-gate error("uid mismatch: " 8167c478bd9Sstevel@tonic-gate "peer euid %u != uid %u", 8177c478bd9Sstevel@tonic-gate (u_int) euid, (u_int) getuid()); 8187c478bd9Sstevel@tonic-gate close(sock); 8197c478bd9Sstevel@tonic-gate break; 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate new_socket(AUTH_CONNECTION, sock); 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate break; 8247c478bd9Sstevel@tonic-gate case AUTH_CONNECTION: 8257c478bd9Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0 && 8267c478bd9Sstevel@tonic-gate FD_ISSET(sockets[i].fd, writeset)) { 8277c478bd9Sstevel@tonic-gate do { 8287c478bd9Sstevel@tonic-gate len = write(sockets[i].fd, 8297c478bd9Sstevel@tonic-gate buffer_ptr(&sockets[i].output), 8307c478bd9Sstevel@tonic-gate buffer_len(&sockets[i].output)); 8317c478bd9Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 832*ef4d27fbSHuie-Ying Lee errno == EINTR || 833*ef4d27fbSHuie-Ying Lee errno == EWOULDBLOCK)) 8347c478bd9Sstevel@tonic-gate continue; 8357c478bd9Sstevel@tonic-gate break; 8367c478bd9Sstevel@tonic-gate } while (1); 8377c478bd9Sstevel@tonic-gate if (len <= 0) { 8387c478bd9Sstevel@tonic-gate close_socket(&sockets[i]); 8397c478bd9Sstevel@tonic-gate break; 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate buffer_consume(&sockets[i].output, len); 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 8447c478bd9Sstevel@tonic-gate do { 8457c478bd9Sstevel@tonic-gate len = read(sockets[i].fd, buf, sizeof(buf)); 8467c478bd9Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 847*ef4d27fbSHuie-Ying Lee errno == EINTR || 848*ef4d27fbSHuie-Ying Lee errno == EWOULDBLOCK)) 8497c478bd9Sstevel@tonic-gate continue; 8507c478bd9Sstevel@tonic-gate break; 8517c478bd9Sstevel@tonic-gate } while (1); 8527c478bd9Sstevel@tonic-gate if (len <= 0) { 8537c478bd9Sstevel@tonic-gate close_socket(&sockets[i]); 8547c478bd9Sstevel@tonic-gate break; 8557c478bd9Sstevel@tonic-gate } 8567c478bd9Sstevel@tonic-gate buffer_append(&sockets[i].input, buf, len); 8577c478bd9Sstevel@tonic-gate process_message(&sockets[i]); 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate break; 8607c478bd9Sstevel@tonic-gate default: 8617c478bd9Sstevel@tonic-gate fatal("Unknown type %d", sockets[i].type); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate } 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate static void 866*ef4d27fbSHuie-Ying Lee cleanup_socket(void) 8677c478bd9Sstevel@tonic-gate { 8687c478bd9Sstevel@tonic-gate if (socket_name[0]) 8697c478bd9Sstevel@tonic-gate unlink(socket_name); 8707c478bd9Sstevel@tonic-gate if (socket_dir[0]) 8717c478bd9Sstevel@tonic-gate rmdir(socket_dir); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate 874*ef4d27fbSHuie-Ying Lee void 8757c478bd9Sstevel@tonic-gate cleanup_exit(int i) 8767c478bd9Sstevel@tonic-gate { 877*ef4d27fbSHuie-Ying Lee cleanup_socket(); 878*ef4d27fbSHuie-Ying Lee _exit(i); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate 881*ef4d27fbSHuie-Ying Lee /*ARGSUSED*/ 8827c478bd9Sstevel@tonic-gate static void 8837c478bd9Sstevel@tonic-gate cleanup_handler(int sig) 8847c478bd9Sstevel@tonic-gate { 885*ef4d27fbSHuie-Ying Lee cleanup_socket(); 8867c478bd9Sstevel@tonic-gate _exit(2); 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate static void 890*ef4d27fbSHuie-Ying Lee check_parent_exists(void) 8917c478bd9Sstevel@tonic-gate { 892*ef4d27fbSHuie-Ying Lee if (parent_pid != -1 && kill(parent_pid, 0) < 0) { 8937c478bd9Sstevel@tonic-gate /* printf("Parent has died - Authentication agent exiting.\n"); */ 894*ef4d27fbSHuie-Ying Lee cleanup_socket(); 895*ef4d27fbSHuie-Ying Lee _exit(2); 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate } 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate static void 9007c478bd9Sstevel@tonic-gate usage(void) 9017c478bd9Sstevel@tonic-gate { 9027c478bd9Sstevel@tonic-gate fprintf(stderr, 9037c478bd9Sstevel@tonic-gate gettext("Usage: %s [options] [command [args ...]]\n" 9047c478bd9Sstevel@tonic-gate "Options:\n" 9057c478bd9Sstevel@tonic-gate " -c Generate C-shell commands on stdout.\n" 9067c478bd9Sstevel@tonic-gate " -s Generate Bourne shell commands on stdout.\n" 9077c478bd9Sstevel@tonic-gate " -k Kill the current agent.\n" 9087c478bd9Sstevel@tonic-gate " -d Debug mode.\n" 909*ef4d27fbSHuie-Ying Lee " -a socket Bind agent socket to given name.\n" 910*ef4d27fbSHuie-Ying Lee " -t life Default identity lifetime (seconds).\n"), 9117c478bd9Sstevel@tonic-gate __progname); 9127c478bd9Sstevel@tonic-gate exit(1); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate int 9167c478bd9Sstevel@tonic-gate main(int ac, char **av) 9177c478bd9Sstevel@tonic-gate { 918*ef4d27fbSHuie-Ying Lee int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0; 919*ef4d27fbSHuie-Ying Lee int sock, fd, ch, result, saved_errno; 920*ef4d27fbSHuie-Ying Lee u_int nalloc; 9217c478bd9Sstevel@tonic-gate char *shell, *pidstr, *agentsocket = NULL; 9227c478bd9Sstevel@tonic-gate const char *format; 9237c478bd9Sstevel@tonic-gate fd_set *readsetp = NULL, *writesetp = NULL; 9247c478bd9Sstevel@tonic-gate struct sockaddr_un sunaddr; 9257c478bd9Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 9267c478bd9Sstevel@tonic-gate struct rlimit rlim; 9277c478bd9Sstevel@tonic-gate #endif 9287c478bd9Sstevel@tonic-gate int prev_mask; 9297c478bd9Sstevel@tonic-gate extern int optind; 9307c478bd9Sstevel@tonic-gate extern char *optarg; 9317c478bd9Sstevel@tonic-gate pid_t pid; 9327c478bd9Sstevel@tonic-gate char pidstrbuf[1 + 3 * sizeof pid]; 933*ef4d27fbSHuie-Ying Lee struct timeval *tvp = NULL; 9347c478bd9Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 9357c478bd9Sstevel@tonic-gate priv_set_t *myprivs; 9367c478bd9Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 9377c478bd9Sstevel@tonic-gate 938*ef4d27fbSHuie-Ying Lee /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ 939*ef4d27fbSHuie-Ying Lee sanitise_stdfd(); 940*ef4d27fbSHuie-Ying Lee 9417c478bd9Sstevel@tonic-gate /* drop */ 9427c478bd9Sstevel@tonic-gate setegid(getgid()); 9437c478bd9Sstevel@tonic-gate setgid(getgid()); 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate SSLeay_add_all_algorithms(); 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate __progname = get_progname(av[0]); 9487c478bd9Sstevel@tonic-gate init_rng(); 9497c478bd9Sstevel@tonic-gate seed_rng(); 9507c478bd9Sstevel@tonic-gate 951*ef4d27fbSHuie-Ying Lee while ((ch = getopt(ac, av, "cdksa:t:")) != -1) { 9527c478bd9Sstevel@tonic-gate switch (ch) { 9537c478bd9Sstevel@tonic-gate case 'c': 9547c478bd9Sstevel@tonic-gate if (s_flag) 9557c478bd9Sstevel@tonic-gate usage(); 9567c478bd9Sstevel@tonic-gate c_flag++; 9577c478bd9Sstevel@tonic-gate break; 9587c478bd9Sstevel@tonic-gate case 'k': 9597c478bd9Sstevel@tonic-gate k_flag++; 9607c478bd9Sstevel@tonic-gate break; 9617c478bd9Sstevel@tonic-gate case 's': 9627c478bd9Sstevel@tonic-gate if (c_flag) 9637c478bd9Sstevel@tonic-gate usage(); 9647c478bd9Sstevel@tonic-gate s_flag++; 9657c478bd9Sstevel@tonic-gate break; 9667c478bd9Sstevel@tonic-gate case 'd': 9677c478bd9Sstevel@tonic-gate if (d_flag) 9687c478bd9Sstevel@tonic-gate usage(); 9697c478bd9Sstevel@tonic-gate d_flag++; 9707c478bd9Sstevel@tonic-gate break; 9717c478bd9Sstevel@tonic-gate case 'a': 9727c478bd9Sstevel@tonic-gate agentsocket = optarg; 9737c478bd9Sstevel@tonic-gate break; 974*ef4d27fbSHuie-Ying Lee case 't': 975*ef4d27fbSHuie-Ying Lee if ((lifetime = convtime(optarg)) == -1) { 976*ef4d27fbSHuie-Ying Lee fprintf(stderr, gettext("Invalid lifetime\n")); 977*ef4d27fbSHuie-Ying Lee usage(); 978*ef4d27fbSHuie-Ying Lee } 979*ef4d27fbSHuie-Ying Lee break; 9807c478bd9Sstevel@tonic-gate default: 9817c478bd9Sstevel@tonic-gate usage(); 9827c478bd9Sstevel@tonic-gate } 9837c478bd9Sstevel@tonic-gate } 9847c478bd9Sstevel@tonic-gate ac -= optind; 9857c478bd9Sstevel@tonic-gate av += optind; 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 9887c478bd9Sstevel@tonic-gate usage(); 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate if (ac == 0 && !c_flag && !s_flag) { 9917c478bd9Sstevel@tonic-gate shell = getenv("SHELL"); 992*ef4d27fbSHuie-Ying Lee if (shell != NULL && 993*ef4d27fbSHuie-Ying Lee strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) 9947c478bd9Sstevel@tonic-gate c_flag = 1; 9957c478bd9Sstevel@tonic-gate } 9967c478bd9Sstevel@tonic-gate if (k_flag) { 9977c478bd9Sstevel@tonic-gate pidstr = getenv(SSH_AGENTPID_ENV_NAME); 9987c478bd9Sstevel@tonic-gate if (pidstr == NULL) { 9997c478bd9Sstevel@tonic-gate fprintf(stderr, 10007c478bd9Sstevel@tonic-gate gettext("%s not set, cannot kill agent\n"), 10017c478bd9Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 10027c478bd9Sstevel@tonic-gate exit(1); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate pid = atoi(pidstr); 10057c478bd9Sstevel@tonic-gate if (pid < 1) { 10067c478bd9Sstevel@tonic-gate fprintf(stderr, 1007*ef4d27fbSHuie-Ying Lee gettext("%s not set, cannot kill agent\n"), 1008*ef4d27fbSHuie-Ying Lee SSH_AGENTPID_ENV_NAME); 10097c478bd9Sstevel@tonic-gate exit(1); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate if (kill(pid, SIGTERM) == -1) { 10127c478bd9Sstevel@tonic-gate perror("kill"); 10137c478bd9Sstevel@tonic-gate exit(1); 10147c478bd9Sstevel@tonic-gate } 10157c478bd9Sstevel@tonic-gate format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; 10167c478bd9Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME); 10177c478bd9Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME); 10187c478bd9Sstevel@tonic-gate printf("echo "); 10197c478bd9Sstevel@tonic-gate printf(gettext("Agent pid %ld killed;\n"), (long)pid); 10207c478bd9Sstevel@tonic-gate exit(0); 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate parent_pid = getpid(); 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate if (agentsocket == NULL) { 10257c478bd9Sstevel@tonic-gate /* Create private directory for agent socket */ 1026*ef4d27fbSHuie-Ying Lee strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir); 10277c478bd9Sstevel@tonic-gate if (mkdtemp(socket_dir) == NULL) { 10287c478bd9Sstevel@tonic-gate perror("mkdtemp: private socket dir"); 10297c478bd9Sstevel@tonic-gate exit(1); 10307c478bd9Sstevel@tonic-gate } 10317c478bd9Sstevel@tonic-gate snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, 10327c478bd9Sstevel@tonic-gate (long)parent_pid); 10337c478bd9Sstevel@tonic-gate } else { 10347c478bd9Sstevel@tonic-gate /* Try to use specified agent socket */ 10357c478bd9Sstevel@tonic-gate socket_dir[0] = '\0'; 10367c478bd9Sstevel@tonic-gate strlcpy(socket_name, agentsocket, sizeof socket_name); 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate /* 10407c478bd9Sstevel@tonic-gate * Create socket early so it will exist before command gets run from 10417c478bd9Sstevel@tonic-gate * the parent. 10427c478bd9Sstevel@tonic-gate */ 10437c478bd9Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0); 10447c478bd9Sstevel@tonic-gate if (sock < 0) { 10457c478bd9Sstevel@tonic-gate perror("socket"); 1046*ef4d27fbSHuie-Ying Lee *socket_name = '\0'; /* Don't unlink any existing file */ 10477c478bd9Sstevel@tonic-gate cleanup_exit(1); 10487c478bd9Sstevel@tonic-gate } 10497c478bd9Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr)); 10507c478bd9Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX; 10517c478bd9Sstevel@tonic-gate strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); 10527c478bd9Sstevel@tonic-gate prev_mask = umask(0177); 10537c478bd9Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) { 10547c478bd9Sstevel@tonic-gate perror("bind"); 1055*ef4d27fbSHuie-Ying Lee *socket_name = '\0'; /* Don't unlink any existing file */ 10567c478bd9Sstevel@tonic-gate umask(prev_mask); 10577c478bd9Sstevel@tonic-gate cleanup_exit(1); 10587c478bd9Sstevel@tonic-gate } 10597c478bd9Sstevel@tonic-gate umask(prev_mask); 1060*ef4d27fbSHuie-Ying Lee if (listen(sock, SSH_LISTEN_BACKLOG) < 0) { 10617c478bd9Sstevel@tonic-gate perror("listen"); 10627c478bd9Sstevel@tonic-gate cleanup_exit(1); 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate /* 10667c478bd9Sstevel@tonic-gate * Fork, and have the parent execute the command, if any, or present 10677c478bd9Sstevel@tonic-gate * the socket data. The child continues as the authentication agent. 10687c478bd9Sstevel@tonic-gate */ 10697c478bd9Sstevel@tonic-gate if (d_flag) { 10707c478bd9Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); 10717c478bd9Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10727c478bd9Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10737c478bd9Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 10747c478bd9Sstevel@tonic-gate printf("echo "); 10757c478bd9Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)parent_pid); 10767c478bd9Sstevel@tonic-gate goto skip; 10777c478bd9Sstevel@tonic-gate } 10787c478bd9Sstevel@tonic-gate pid = fork(); 10797c478bd9Sstevel@tonic-gate if (pid == -1) { 10807c478bd9Sstevel@tonic-gate perror("fork"); 10817c478bd9Sstevel@tonic-gate cleanup_exit(1); 10827c478bd9Sstevel@tonic-gate } 10837c478bd9Sstevel@tonic-gate if (pid != 0) { /* Parent - execute the given command. */ 10847c478bd9Sstevel@tonic-gate close(sock); 10857c478bd9Sstevel@tonic-gate snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); 10867c478bd9Sstevel@tonic-gate if (ac == 0) { 10877c478bd9Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 10887c478bd9Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 10897c478bd9Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 10907c478bd9Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, 10917c478bd9Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 10927c478bd9Sstevel@tonic-gate printf("echo "); 10937c478bd9Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)pid); 10947c478bd9Sstevel@tonic-gate exit(0); 10957c478bd9Sstevel@tonic-gate } 10967c478bd9Sstevel@tonic-gate if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || 10977c478bd9Sstevel@tonic-gate setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { 10987c478bd9Sstevel@tonic-gate perror("setenv"); 10997c478bd9Sstevel@tonic-gate exit(1); 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate execvp(av[0], av); 11027c478bd9Sstevel@tonic-gate perror(av[0]); 11037c478bd9Sstevel@tonic-gate exit(1); 11047c478bd9Sstevel@tonic-gate } 11057c478bd9Sstevel@tonic-gate /* child */ 11067c478bd9Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); 11077c478bd9Sstevel@tonic-gate 11087c478bd9Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 11097c478bd9Sstevel@tonic-gate /* 11107c478bd9Sstevel@tonic-gate * Drop unneeded privs, including basic ones like fork/exec. 11117c478bd9Sstevel@tonic-gate * 11127c478bd9Sstevel@tonic-gate * Idiom: remove from 'basic' privs we know we don't want, 11137c478bd9Sstevel@tonic-gate * invert the result and remove the resulting set from P. 11147c478bd9Sstevel@tonic-gate * 11157c478bd9Sstevel@tonic-gate * None of the priv_delset() calls below, nor the setppriv call 11167c478bd9Sstevel@tonic-gate * below can fail, so their return values are not checked. 11177c478bd9Sstevel@tonic-gate */ 11187c478bd9Sstevel@tonic-gate if ((myprivs = priv_str_to_set("basic", ",", NULL)) == NULL) 11197c478bd9Sstevel@tonic-gate fatal("priv_str_to_set failed: %m"); 11207c478bd9Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_EXEC); 11217c478bd9Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_FORK); 11227c478bd9Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_FILE_LINK_ANY); 11237c478bd9Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_INFO); 11247c478bd9Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_SESSION); 11257c478bd9Sstevel@tonic-gate priv_inverse(myprivs); 11267c478bd9Sstevel@tonic-gate (void) setppriv(PRIV_OFF, PRIV_PERMITTED, myprivs); 11277c478bd9Sstevel@tonic-gate (void) priv_freeset(myprivs); 11287c478bd9Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 11297c478bd9Sstevel@tonic-gate 11307c478bd9Sstevel@tonic-gate if (setsid() == -1) { 11317c478bd9Sstevel@tonic-gate error("setsid: %s", strerror(errno)); 11327c478bd9Sstevel@tonic-gate cleanup_exit(1); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate (void)chdir("/"); 1136*ef4d27fbSHuie-Ying Lee if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 1137*ef4d27fbSHuie-Ying Lee /* XXX might close listen socket */ 1138*ef4d27fbSHuie-Ying Lee (void)dup2(fd, STDIN_FILENO); 1139*ef4d27fbSHuie-Ying Lee (void)dup2(fd, STDOUT_FILENO); 1140*ef4d27fbSHuie-Ying Lee (void)dup2(fd, STDERR_FILENO); 1141*ef4d27fbSHuie-Ying Lee if (fd > 2) 1142*ef4d27fbSHuie-Ying Lee close(fd); 1143*ef4d27fbSHuie-Ying Lee } 11447c478bd9Sstevel@tonic-gate 11457c478bd9Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 11467c478bd9Sstevel@tonic-gate /* deny core dumps, since memory contains unencrypted private keys */ 11477c478bd9Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max = 0; 11487c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_CORE, &rlim) < 0) { 11497c478bd9Sstevel@tonic-gate error("setrlimit RLIMIT_CORE: %s", strerror(errno)); 11507c478bd9Sstevel@tonic-gate cleanup_exit(1); 11517c478bd9Sstevel@tonic-gate } 11527c478bd9Sstevel@tonic-gate #endif 11537c478bd9Sstevel@tonic-gate 11547c478bd9Sstevel@tonic-gate skip: 11557c478bd9Sstevel@tonic-gate new_socket(AUTH_SOCKET, sock); 1156*ef4d27fbSHuie-Ying Lee if (ac > 0) 1157*ef4d27fbSHuie-Ying Lee parent_alive_interval = 10; 11587c478bd9Sstevel@tonic-gate idtab_init(); 11597c478bd9Sstevel@tonic-gate if (!d_flag) 11607c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 11617c478bd9Sstevel@tonic-gate signal(SIGPIPE, SIG_IGN); 11627c478bd9Sstevel@tonic-gate signal(SIGHUP, cleanup_handler); 11637c478bd9Sstevel@tonic-gate signal(SIGTERM, cleanup_handler); 11647c478bd9Sstevel@tonic-gate nalloc = 0; 11657c478bd9Sstevel@tonic-gate 11667c478bd9Sstevel@tonic-gate while (1) { 1167*ef4d27fbSHuie-Ying Lee prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp); 1168*ef4d27fbSHuie-Ying Lee result = select(max_fd + 1, readsetp, writesetp, NULL, tvp); 1169*ef4d27fbSHuie-Ying Lee saved_errno = errno; 1170*ef4d27fbSHuie-Ying Lee if (parent_alive_interval != 0) 1171*ef4d27fbSHuie-Ying Lee check_parent_exists(); 1172*ef4d27fbSHuie-Ying Lee (void) reaper(); /* remove expired keys */ 1173*ef4d27fbSHuie-Ying Lee if (result < 0) { 1174*ef4d27fbSHuie-Ying Lee if (saved_errno == EINTR) 11757c478bd9Sstevel@tonic-gate continue; 1176*ef4d27fbSHuie-Ying Lee fatal("select: %s", strerror(saved_errno)); 1177*ef4d27fbSHuie-Ying Lee } else if (result > 0) 11787c478bd9Sstevel@tonic-gate after_select(readsetp, writesetp); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate /* NOTREACHED */ 11817c478bd9Sstevel@tonic-gate return (0); /* keep lint happy */ 11827c478bd9Sstevel@tonic-gate } 1183