1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* 7*0Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 8*0Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 9*0Sstevel@tonic-gate * All rights reserved 10*0Sstevel@tonic-gate * The authentication agent program. 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 13*0Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 14*0Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 15*0Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 16*0Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 17*0Sstevel@tonic-gate * 18*0Sstevel@tonic-gate * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 21*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 22*0Sstevel@tonic-gate * are met: 23*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 24*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 25*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 26*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 27*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 28*0Sstevel@tonic-gate * 29*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 30*0Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 31*0Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 32*0Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 33*0Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 34*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 35*0Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 36*0Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 37*0Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 38*0Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 39*0Sstevel@tonic-gate */ 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #include "includes.h" 42*0Sstevel@tonic-gate #include "sys-queue.h" 43*0Sstevel@tonic-gate RCSID("$OpenBSD: ssh-agent.c,v 1.105 2002/10/01 20:34:12 markus Exp $"); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 48*0Sstevel@tonic-gate #include <priv.h> 49*0Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate #include <openssl/evp.h> 52*0Sstevel@tonic-gate #include <openssl/md5.h> 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate #include "ssh.h" 55*0Sstevel@tonic-gate #include "rsa.h" 56*0Sstevel@tonic-gate #include "buffer.h" 57*0Sstevel@tonic-gate #include "bufaux.h" 58*0Sstevel@tonic-gate #include "xmalloc.h" 59*0Sstevel@tonic-gate #include "getput.h" 60*0Sstevel@tonic-gate #include "key.h" 61*0Sstevel@tonic-gate #include "authfd.h" 62*0Sstevel@tonic-gate #include "compat.h" 63*0Sstevel@tonic-gate #include "log.h" 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate #ifdef SMARTCARD 66*0Sstevel@tonic-gate #include "scard.h" 67*0Sstevel@tonic-gate #endif 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate typedef enum { 70*0Sstevel@tonic-gate AUTH_UNUSED, 71*0Sstevel@tonic-gate AUTH_SOCKET, 72*0Sstevel@tonic-gate AUTH_CONNECTION 73*0Sstevel@tonic-gate } sock_type; 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate typedef struct { 76*0Sstevel@tonic-gate int fd; 77*0Sstevel@tonic-gate sock_type type; 78*0Sstevel@tonic-gate Buffer input; 79*0Sstevel@tonic-gate Buffer output; 80*0Sstevel@tonic-gate Buffer request; 81*0Sstevel@tonic-gate } SocketEntry; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate u_int sockets_alloc = 0; 84*0Sstevel@tonic-gate SocketEntry *sockets = NULL; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate typedef struct identity { 87*0Sstevel@tonic-gate TAILQ_ENTRY(identity) next; 88*0Sstevel@tonic-gate Key *key; 89*0Sstevel@tonic-gate char *comment; 90*0Sstevel@tonic-gate u_int death; 91*0Sstevel@tonic-gate } Identity; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate typedef struct { 94*0Sstevel@tonic-gate int nentries; 95*0Sstevel@tonic-gate TAILQ_HEAD(idqueue, identity) idlist; 96*0Sstevel@tonic-gate } Idtab; 97*0Sstevel@tonic-gate 98*0Sstevel@tonic-gate /* private key table, one per protocol version */ 99*0Sstevel@tonic-gate Idtab idtable[3]; 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate int max_fd = 0; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate /* pid of shell == parent of agent */ 104*0Sstevel@tonic-gate pid_t parent_pid = -1; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /* pathname and directory for AUTH_SOCKET */ 107*0Sstevel@tonic-gate char socket_name[1024]; 108*0Sstevel@tonic-gate char socket_dir[1024]; 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate /* locking */ 111*0Sstevel@tonic-gate int locked = 0; 112*0Sstevel@tonic-gate char *lock_passwd = NULL; 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate #ifdef HAVE___PROGNAME 115*0Sstevel@tonic-gate extern char *__progname; 116*0Sstevel@tonic-gate #else 117*0Sstevel@tonic-gate char *__progname; 118*0Sstevel@tonic-gate #endif 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate static void 121*0Sstevel@tonic-gate close_socket(SocketEntry *e) 122*0Sstevel@tonic-gate { 123*0Sstevel@tonic-gate close(e->fd); 124*0Sstevel@tonic-gate e->fd = -1; 125*0Sstevel@tonic-gate e->type = AUTH_UNUSED; 126*0Sstevel@tonic-gate buffer_free(&e->input); 127*0Sstevel@tonic-gate buffer_free(&e->output); 128*0Sstevel@tonic-gate buffer_free(&e->request); 129*0Sstevel@tonic-gate } 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate static void 132*0Sstevel@tonic-gate idtab_init(void) 133*0Sstevel@tonic-gate { 134*0Sstevel@tonic-gate int i; 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gate for (i = 0; i <=2; i++) { 137*0Sstevel@tonic-gate TAILQ_INIT(&idtable[i].idlist); 138*0Sstevel@tonic-gate idtable[i].nentries = 0; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate /* return private key table for requested protocol version */ 143*0Sstevel@tonic-gate static Idtab * 144*0Sstevel@tonic-gate idtab_lookup(int version) 145*0Sstevel@tonic-gate { 146*0Sstevel@tonic-gate if (version < 1 || version > 2) 147*0Sstevel@tonic-gate fatal("internal error, bad protocol version %d", version); 148*0Sstevel@tonic-gate return &idtable[version]; 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate static void 152*0Sstevel@tonic-gate free_identity(Identity *id) 153*0Sstevel@tonic-gate { 154*0Sstevel@tonic-gate key_free(id->key); 155*0Sstevel@tonic-gate xfree(id->comment); 156*0Sstevel@tonic-gate xfree(id); 157*0Sstevel@tonic-gate } 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate /* return matching private key for given public key */ 160*0Sstevel@tonic-gate static Identity * 161*0Sstevel@tonic-gate lookup_identity(Key *key, int version) 162*0Sstevel@tonic-gate { 163*0Sstevel@tonic-gate Identity *id; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 166*0Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 167*0Sstevel@tonic-gate if (key_equal(key, id->key)) 168*0Sstevel@tonic-gate return (id); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate return (NULL); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* send list of supported public keys to 'client' */ 174*0Sstevel@tonic-gate static void 175*0Sstevel@tonic-gate process_request_identities(SocketEntry *e, int version) 176*0Sstevel@tonic-gate { 177*0Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 178*0Sstevel@tonic-gate Identity *id; 179*0Sstevel@tonic-gate Buffer msg; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate buffer_init(&msg); 182*0Sstevel@tonic-gate buffer_put_char(&msg, (version == 1) ? 183*0Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 184*0Sstevel@tonic-gate buffer_put_int(&msg, tab->nentries); 185*0Sstevel@tonic-gate TAILQ_FOREACH(id, &tab->idlist, next) { 186*0Sstevel@tonic-gate if (id->key->type == KEY_RSA1) { 187*0Sstevel@tonic-gate buffer_put_int(&msg, BN_num_bits(id->key->rsa->n)); 188*0Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->e); 189*0Sstevel@tonic-gate buffer_put_bignum(&msg, id->key->rsa->n); 190*0Sstevel@tonic-gate } else { 191*0Sstevel@tonic-gate u_char *blob; 192*0Sstevel@tonic-gate u_int blen; 193*0Sstevel@tonic-gate key_to_blob(id->key, &blob, &blen); 194*0Sstevel@tonic-gate buffer_put_string(&msg, blob, blen); 195*0Sstevel@tonic-gate xfree(blob); 196*0Sstevel@tonic-gate } 197*0Sstevel@tonic-gate buffer_put_cstring(&msg, id->comment); 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 200*0Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 201*0Sstevel@tonic-gate buffer_free(&msg); 202*0Sstevel@tonic-gate } 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate /* ssh1 only */ 205*0Sstevel@tonic-gate static void 206*0Sstevel@tonic-gate process_authentication_challenge1(SocketEntry *e) 207*0Sstevel@tonic-gate { 208*0Sstevel@tonic-gate u_char buf[32], mdbuf[16], session_id[16]; 209*0Sstevel@tonic-gate u_int response_type; 210*0Sstevel@tonic-gate BIGNUM *challenge; 211*0Sstevel@tonic-gate Identity *id; 212*0Sstevel@tonic-gate int i, len; 213*0Sstevel@tonic-gate Buffer msg; 214*0Sstevel@tonic-gate MD5_CTX md; 215*0Sstevel@tonic-gate Key *key; 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate buffer_init(&msg); 218*0Sstevel@tonic-gate key = key_new(KEY_RSA1); 219*0Sstevel@tonic-gate if ((challenge = BN_new()) == NULL) 220*0Sstevel@tonic-gate fatal("process_authentication_challenge1: BN_new failed"); 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 223*0Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 224*0Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 225*0Sstevel@tonic-gate buffer_get_bignum(&e->request, challenge); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate /* Only protocol 1.1 is supported */ 228*0Sstevel@tonic-gate if (buffer_len(&e->request) == 0) 229*0Sstevel@tonic-gate goto failure; 230*0Sstevel@tonic-gate buffer_get(&e->request, session_id, 16); 231*0Sstevel@tonic-gate response_type = buffer_get_int(&e->request); 232*0Sstevel@tonic-gate if (response_type != 1) 233*0Sstevel@tonic-gate goto failure; 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate id = lookup_identity(key, 1); 236*0Sstevel@tonic-gate if (id != NULL) { 237*0Sstevel@tonic-gate Key *private = id->key; 238*0Sstevel@tonic-gate /* Decrypt the challenge using the private key. */ 239*0Sstevel@tonic-gate if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0) 240*0Sstevel@tonic-gate goto failure; 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate /* The response is MD5 of decrypted challenge plus session id. */ 243*0Sstevel@tonic-gate len = BN_num_bytes(challenge); 244*0Sstevel@tonic-gate if (len <= 0 || len > 32) { 245*0Sstevel@tonic-gate log("process_authentication_challenge: bad challenge length %d", len); 246*0Sstevel@tonic-gate goto failure; 247*0Sstevel@tonic-gate } 248*0Sstevel@tonic-gate memset(buf, 0, 32); 249*0Sstevel@tonic-gate BN_bn2bin(challenge, buf + 32 - len); 250*0Sstevel@tonic-gate MD5_Init(&md); 251*0Sstevel@tonic-gate MD5_Update(&md, buf, 32); 252*0Sstevel@tonic-gate MD5_Update(&md, session_id, 16); 253*0Sstevel@tonic-gate MD5_Final(mdbuf, &md); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate /* Send the response. */ 256*0Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE); 257*0Sstevel@tonic-gate for (i = 0; i < 16; i++) 258*0Sstevel@tonic-gate buffer_put_char(&msg, mdbuf[i]); 259*0Sstevel@tonic-gate goto send; 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate failure: 263*0Sstevel@tonic-gate /* Unknown identity or protocol error. Send failure. */ 264*0Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 265*0Sstevel@tonic-gate send: 266*0Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 267*0Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 268*0Sstevel@tonic-gate key_free(key); 269*0Sstevel@tonic-gate BN_clear_free(challenge); 270*0Sstevel@tonic-gate buffer_free(&msg); 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate /* ssh2 only */ 274*0Sstevel@tonic-gate static void 275*0Sstevel@tonic-gate process_sign_request2(SocketEntry *e) 276*0Sstevel@tonic-gate { 277*0Sstevel@tonic-gate u_char *blob, *data, *signature = NULL; 278*0Sstevel@tonic-gate u_int blen, dlen, slen = 0; 279*0Sstevel@tonic-gate extern int datafellows; 280*0Sstevel@tonic-gate int ok = -1, flags; 281*0Sstevel@tonic-gate Buffer msg; 282*0Sstevel@tonic-gate Key *key; 283*0Sstevel@tonic-gate 284*0Sstevel@tonic-gate datafellows = 0; 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 287*0Sstevel@tonic-gate data = buffer_get_string(&e->request, &dlen); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate flags = buffer_get_int(&e->request); 290*0Sstevel@tonic-gate if (flags & SSH_AGENT_OLD_SIGNATURE) 291*0Sstevel@tonic-gate datafellows = SSH_BUG_SIGBLOB; 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate key = key_from_blob(blob, blen); 294*0Sstevel@tonic-gate if (key != NULL) { 295*0Sstevel@tonic-gate Identity *id = lookup_identity(key, 2); 296*0Sstevel@tonic-gate if (id != NULL) 297*0Sstevel@tonic-gate ok = key_sign(id->key, &signature, &slen, data, dlen); 298*0Sstevel@tonic-gate } 299*0Sstevel@tonic-gate key_free(key); 300*0Sstevel@tonic-gate buffer_init(&msg); 301*0Sstevel@tonic-gate if (ok == 0) { 302*0Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE); 303*0Sstevel@tonic-gate buffer_put_string(&msg, signature, slen); 304*0Sstevel@tonic-gate } else { 305*0Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_FAILURE); 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 308*0Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), 309*0Sstevel@tonic-gate buffer_len(&msg)); 310*0Sstevel@tonic-gate buffer_free(&msg); 311*0Sstevel@tonic-gate xfree(data); 312*0Sstevel@tonic-gate xfree(blob); 313*0Sstevel@tonic-gate if (signature != NULL) 314*0Sstevel@tonic-gate xfree(signature); 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate /* shared */ 318*0Sstevel@tonic-gate static void 319*0Sstevel@tonic-gate process_remove_identity(SocketEntry *e, int version) 320*0Sstevel@tonic-gate { 321*0Sstevel@tonic-gate u_int blen, bits; 322*0Sstevel@tonic-gate int success = 0; 323*0Sstevel@tonic-gate Key *key = NULL; 324*0Sstevel@tonic-gate u_char *blob; 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate switch (version) { 327*0Sstevel@tonic-gate case 1: 328*0Sstevel@tonic-gate key = key_new(KEY_RSA1); 329*0Sstevel@tonic-gate bits = buffer_get_int(&e->request); 330*0Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->e); 331*0Sstevel@tonic-gate buffer_get_bignum(&e->request, key->rsa->n); 332*0Sstevel@tonic-gate 333*0Sstevel@tonic-gate if (bits != key_size(key)) 334*0Sstevel@tonic-gate log("Warning: identity keysize mismatch: actual %u, announced %u", 335*0Sstevel@tonic-gate key_size(key), bits); 336*0Sstevel@tonic-gate break; 337*0Sstevel@tonic-gate case 2: 338*0Sstevel@tonic-gate blob = buffer_get_string(&e->request, &blen); 339*0Sstevel@tonic-gate key = key_from_blob(blob, blen); 340*0Sstevel@tonic-gate xfree(blob); 341*0Sstevel@tonic-gate break; 342*0Sstevel@tonic-gate } 343*0Sstevel@tonic-gate if (key != NULL) { 344*0Sstevel@tonic-gate Identity *id = lookup_identity(key, version); 345*0Sstevel@tonic-gate if (id != NULL) { 346*0Sstevel@tonic-gate /* 347*0Sstevel@tonic-gate * We have this key. Free the old key. Since we 348*0Sstevel@tonic-gate * don\'t want to leave empty slots in the middle of 349*0Sstevel@tonic-gate * the array, we actually free the key there and move 350*0Sstevel@tonic-gate * all the entries between the empty slot and the end 351*0Sstevel@tonic-gate * of the array. 352*0Sstevel@tonic-gate */ 353*0Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 354*0Sstevel@tonic-gate if (tab->nentries < 1) 355*0Sstevel@tonic-gate fatal("process_remove_identity: " 356*0Sstevel@tonic-gate "internal error: tab->nentries %d", 357*0Sstevel@tonic-gate tab->nentries); 358*0Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 359*0Sstevel@tonic-gate free_identity(id); 360*0Sstevel@tonic-gate tab->nentries--; 361*0Sstevel@tonic-gate success = 1; 362*0Sstevel@tonic-gate } 363*0Sstevel@tonic-gate key_free(key); 364*0Sstevel@tonic-gate } 365*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 366*0Sstevel@tonic-gate buffer_put_char(&e->output, 367*0Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate static void 371*0Sstevel@tonic-gate process_remove_all_identities(SocketEntry *e, int version) 372*0Sstevel@tonic-gate { 373*0Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 374*0Sstevel@tonic-gate Identity *id; 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gate /* Loop over all identities and clear the keys. */ 377*0Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; 378*0Sstevel@tonic-gate id = TAILQ_FIRST(&tab->idlist)) { 379*0Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 380*0Sstevel@tonic-gate free_identity(id); 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate 383*0Sstevel@tonic-gate /* Mark that there are no identities. */ 384*0Sstevel@tonic-gate tab->nentries = 0; 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate /* Send success. */ 387*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 388*0Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_SUCCESS); 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate static void 392*0Sstevel@tonic-gate reaper(void) 393*0Sstevel@tonic-gate { 394*0Sstevel@tonic-gate u_int now = time(NULL); 395*0Sstevel@tonic-gate Identity *id, *nxt; 396*0Sstevel@tonic-gate int version; 397*0Sstevel@tonic-gate Idtab *tab; 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate for (version = 1; version < 3; version++) { 400*0Sstevel@tonic-gate tab = idtab_lookup(version); 401*0Sstevel@tonic-gate for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) { 402*0Sstevel@tonic-gate nxt = TAILQ_NEXT(id, next); 403*0Sstevel@tonic-gate if (id->death != 0 && now >= id->death) { 404*0Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 405*0Sstevel@tonic-gate free_identity(id); 406*0Sstevel@tonic-gate tab->nentries--; 407*0Sstevel@tonic-gate } 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate static void 413*0Sstevel@tonic-gate process_add_identity(SocketEntry *e, int version) 414*0Sstevel@tonic-gate { 415*0Sstevel@tonic-gate Idtab *tab = idtab_lookup(version); 416*0Sstevel@tonic-gate int type, success = 0, death = 0; 417*0Sstevel@tonic-gate char *type_name, *comment; 418*0Sstevel@tonic-gate Key *k = NULL; 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate switch (version) { 421*0Sstevel@tonic-gate case 1: 422*0Sstevel@tonic-gate k = key_new_private(KEY_RSA1); 423*0Sstevel@tonic-gate (void) buffer_get_int(&e->request); /* ignored */ 424*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->n); 425*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->e); 426*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->d); 427*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->iqmp); 428*0Sstevel@tonic-gate 429*0Sstevel@tonic-gate /* SSH and SSL have p and q swapped */ 430*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->q); /* p */ 431*0Sstevel@tonic-gate buffer_get_bignum(&e->request, k->rsa->p); /* q */ 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate /* Generate additional parameters */ 434*0Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 435*0Sstevel@tonic-gate break; 436*0Sstevel@tonic-gate case 2: 437*0Sstevel@tonic-gate type_name = buffer_get_string(&e->request, NULL); 438*0Sstevel@tonic-gate type = key_type_from_name(type_name); 439*0Sstevel@tonic-gate xfree(type_name); 440*0Sstevel@tonic-gate switch (type) { 441*0Sstevel@tonic-gate case KEY_DSA: 442*0Sstevel@tonic-gate k = key_new_private(type); 443*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->p); 444*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->q); 445*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->g); 446*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->pub_key); 447*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->dsa->priv_key); 448*0Sstevel@tonic-gate break; 449*0Sstevel@tonic-gate case KEY_RSA: 450*0Sstevel@tonic-gate k = key_new_private(type); 451*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->n); 452*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->e); 453*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->d); 454*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->iqmp); 455*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->p); 456*0Sstevel@tonic-gate buffer_get_bignum2(&e->request, k->rsa->q); 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate /* Generate additional parameters */ 459*0Sstevel@tonic-gate rsa_generate_additional_parameters(k->rsa); 460*0Sstevel@tonic-gate break; 461*0Sstevel@tonic-gate default: 462*0Sstevel@tonic-gate buffer_clear(&e->request); 463*0Sstevel@tonic-gate goto send; 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate break; 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate comment = buffer_get_string(&e->request, NULL); 468*0Sstevel@tonic-gate if (k == NULL) { 469*0Sstevel@tonic-gate xfree(comment); 470*0Sstevel@tonic-gate goto send; 471*0Sstevel@tonic-gate } 472*0Sstevel@tonic-gate success = 1; 473*0Sstevel@tonic-gate while (buffer_len(&e->request)) { 474*0Sstevel@tonic-gate switch (buffer_get_char(&e->request)) { 475*0Sstevel@tonic-gate case SSH_AGENT_CONSTRAIN_LIFETIME: 476*0Sstevel@tonic-gate death = time(NULL) + buffer_get_int(&e->request); 477*0Sstevel@tonic-gate break; 478*0Sstevel@tonic-gate default: 479*0Sstevel@tonic-gate break; 480*0Sstevel@tonic-gate } 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate if (lookup_identity(k, version) == NULL) { 483*0Sstevel@tonic-gate Identity *id = xmalloc(sizeof(Identity)); 484*0Sstevel@tonic-gate id->key = k; 485*0Sstevel@tonic-gate id->comment = comment; 486*0Sstevel@tonic-gate id->death = death; 487*0Sstevel@tonic-gate TAILQ_INSERT_TAIL(&tab->idlist, id, next); 488*0Sstevel@tonic-gate /* Increment the number of identities. */ 489*0Sstevel@tonic-gate tab->nentries++; 490*0Sstevel@tonic-gate } else { 491*0Sstevel@tonic-gate key_free(k); 492*0Sstevel@tonic-gate xfree(comment); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate send: 495*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 496*0Sstevel@tonic-gate buffer_put_char(&e->output, 497*0Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 498*0Sstevel@tonic-gate } 499*0Sstevel@tonic-gate 500*0Sstevel@tonic-gate /* XXX todo: encrypt sensitive data with passphrase */ 501*0Sstevel@tonic-gate static void 502*0Sstevel@tonic-gate process_lock_agent(SocketEntry *e, int lock) 503*0Sstevel@tonic-gate { 504*0Sstevel@tonic-gate int success = 0; 505*0Sstevel@tonic-gate char *passwd; 506*0Sstevel@tonic-gate 507*0Sstevel@tonic-gate passwd = buffer_get_string(&e->request, NULL); 508*0Sstevel@tonic-gate if (locked && !lock && strcmp(passwd, lock_passwd) == 0) { 509*0Sstevel@tonic-gate locked = 0; 510*0Sstevel@tonic-gate memset(lock_passwd, 0, strlen(lock_passwd)); 511*0Sstevel@tonic-gate xfree(lock_passwd); 512*0Sstevel@tonic-gate lock_passwd = NULL; 513*0Sstevel@tonic-gate success = 1; 514*0Sstevel@tonic-gate } else if (!locked && lock) { 515*0Sstevel@tonic-gate locked = 1; 516*0Sstevel@tonic-gate lock_passwd = xstrdup(passwd); 517*0Sstevel@tonic-gate success = 1; 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate memset(passwd, 0, strlen(passwd)); 520*0Sstevel@tonic-gate xfree(passwd); 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 523*0Sstevel@tonic-gate buffer_put_char(&e->output, 524*0Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 525*0Sstevel@tonic-gate } 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate static void 528*0Sstevel@tonic-gate no_identities(SocketEntry *e, u_int type) 529*0Sstevel@tonic-gate { 530*0Sstevel@tonic-gate Buffer msg; 531*0Sstevel@tonic-gate 532*0Sstevel@tonic-gate buffer_init(&msg); 533*0Sstevel@tonic-gate buffer_put_char(&msg, 534*0Sstevel@tonic-gate (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ? 535*0Sstevel@tonic-gate SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER); 536*0Sstevel@tonic-gate buffer_put_int(&msg, 0); 537*0Sstevel@tonic-gate buffer_put_int(&e->output, buffer_len(&msg)); 538*0Sstevel@tonic-gate buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg)); 539*0Sstevel@tonic-gate buffer_free(&msg); 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate 542*0Sstevel@tonic-gate #ifdef SMARTCARD 543*0Sstevel@tonic-gate static void 544*0Sstevel@tonic-gate process_add_smartcard_key (SocketEntry *e) 545*0Sstevel@tonic-gate { 546*0Sstevel@tonic-gate char *sc_reader_id = NULL, *pin; 547*0Sstevel@tonic-gate int i, version, success = 0; 548*0Sstevel@tonic-gate Key **keys, *k; 549*0Sstevel@tonic-gate Identity *id; 550*0Sstevel@tonic-gate Idtab *tab; 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gate sc_reader_id = buffer_get_string(&e->request, NULL); 553*0Sstevel@tonic-gate pin = buffer_get_string(&e->request, NULL); 554*0Sstevel@tonic-gate keys = sc_get_keys(sc_reader_id, pin); 555*0Sstevel@tonic-gate xfree(sc_reader_id); 556*0Sstevel@tonic-gate xfree(pin); 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gate if (keys == NULL || keys[0] == NULL) { 559*0Sstevel@tonic-gate error("sc_get_keys failed"); 560*0Sstevel@tonic-gate goto send; 561*0Sstevel@tonic-gate } 562*0Sstevel@tonic-gate for (i = 0; keys[i] != NULL; i++) { 563*0Sstevel@tonic-gate k = keys[i]; 564*0Sstevel@tonic-gate version = k->type == KEY_RSA1 ? 1 : 2; 565*0Sstevel@tonic-gate tab = idtab_lookup(version); 566*0Sstevel@tonic-gate if (lookup_identity(k, version) == NULL) { 567*0Sstevel@tonic-gate id = xmalloc(sizeof(Identity)); 568*0Sstevel@tonic-gate id->key = k; 569*0Sstevel@tonic-gate id->comment = xstrdup("smartcard key"); 570*0Sstevel@tonic-gate id->death = 0; 571*0Sstevel@tonic-gate TAILQ_INSERT_TAIL(&tab->idlist, id, next); 572*0Sstevel@tonic-gate tab->nentries++; 573*0Sstevel@tonic-gate success = 1; 574*0Sstevel@tonic-gate } else { 575*0Sstevel@tonic-gate key_free(k); 576*0Sstevel@tonic-gate } 577*0Sstevel@tonic-gate keys[i] = NULL; 578*0Sstevel@tonic-gate } 579*0Sstevel@tonic-gate xfree(keys); 580*0Sstevel@tonic-gate send: 581*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 582*0Sstevel@tonic-gate buffer_put_char(&e->output, 583*0Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate static void 587*0Sstevel@tonic-gate process_remove_smartcard_key(SocketEntry *e) 588*0Sstevel@tonic-gate { 589*0Sstevel@tonic-gate char *sc_reader_id = NULL, *pin; 590*0Sstevel@tonic-gate int i, version, success = 0; 591*0Sstevel@tonic-gate Key **keys, *k = NULL; 592*0Sstevel@tonic-gate Identity *id; 593*0Sstevel@tonic-gate Idtab *tab; 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gate sc_reader_id = buffer_get_string(&e->request, NULL); 596*0Sstevel@tonic-gate pin = buffer_get_string(&e->request, NULL); 597*0Sstevel@tonic-gate keys = sc_get_keys(sc_reader_id, pin); 598*0Sstevel@tonic-gate xfree(sc_reader_id); 599*0Sstevel@tonic-gate xfree(pin); 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate if (keys == NULL || keys[0] == NULL) { 602*0Sstevel@tonic-gate error("sc_get_keys failed"); 603*0Sstevel@tonic-gate goto send; 604*0Sstevel@tonic-gate } 605*0Sstevel@tonic-gate for (i = 0; keys[i] != NULL; i++) { 606*0Sstevel@tonic-gate k = keys[i]; 607*0Sstevel@tonic-gate version = k->type == KEY_RSA1 ? 1 : 2; 608*0Sstevel@tonic-gate if ((id = lookup_identity(k, version)) != NULL) { 609*0Sstevel@tonic-gate tab = idtab_lookup(version); 610*0Sstevel@tonic-gate TAILQ_REMOVE(&tab->idlist, id, next); 611*0Sstevel@tonic-gate tab->nentries--; 612*0Sstevel@tonic-gate free_identity(id); 613*0Sstevel@tonic-gate success = 1; 614*0Sstevel@tonic-gate } 615*0Sstevel@tonic-gate key_free(k); 616*0Sstevel@tonic-gate keys[i] = NULL; 617*0Sstevel@tonic-gate } 618*0Sstevel@tonic-gate xfree(keys); 619*0Sstevel@tonic-gate send: 620*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 621*0Sstevel@tonic-gate buffer_put_char(&e->output, 622*0Sstevel@tonic-gate success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE); 623*0Sstevel@tonic-gate } 624*0Sstevel@tonic-gate #endif /* SMARTCARD */ 625*0Sstevel@tonic-gate 626*0Sstevel@tonic-gate /* dispatch incoming messages */ 627*0Sstevel@tonic-gate 628*0Sstevel@tonic-gate static void 629*0Sstevel@tonic-gate process_message(SocketEntry *e) 630*0Sstevel@tonic-gate { 631*0Sstevel@tonic-gate u_int msg_len, type; 632*0Sstevel@tonic-gate u_char *cp; 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate /* kill dead keys */ 635*0Sstevel@tonic-gate reaper(); 636*0Sstevel@tonic-gate 637*0Sstevel@tonic-gate if (buffer_len(&e->input) < 5) 638*0Sstevel@tonic-gate return; /* Incomplete message. */ 639*0Sstevel@tonic-gate cp = buffer_ptr(&e->input); 640*0Sstevel@tonic-gate msg_len = GET_32BIT(cp); 641*0Sstevel@tonic-gate if (msg_len > 256 * 1024) { 642*0Sstevel@tonic-gate close_socket(e); 643*0Sstevel@tonic-gate return; 644*0Sstevel@tonic-gate } 645*0Sstevel@tonic-gate if (buffer_len(&e->input) < msg_len + 4) 646*0Sstevel@tonic-gate return; 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate /* move the current input to e->request */ 649*0Sstevel@tonic-gate buffer_consume(&e->input, 4); 650*0Sstevel@tonic-gate buffer_clear(&e->request); 651*0Sstevel@tonic-gate buffer_append(&e->request, buffer_ptr(&e->input), msg_len); 652*0Sstevel@tonic-gate buffer_consume(&e->input, msg_len); 653*0Sstevel@tonic-gate type = buffer_get_char(&e->request); 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gate /* check wheter agent is locked */ 656*0Sstevel@tonic-gate if (locked && type != SSH_AGENTC_UNLOCK) { 657*0Sstevel@tonic-gate buffer_clear(&e->request); 658*0Sstevel@tonic-gate switch (type) { 659*0Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 660*0Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 661*0Sstevel@tonic-gate /* send empty lists */ 662*0Sstevel@tonic-gate no_identities(e, type); 663*0Sstevel@tonic-gate break; 664*0Sstevel@tonic-gate default: 665*0Sstevel@tonic-gate /* send a fail message for all other request types */ 666*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 667*0Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 668*0Sstevel@tonic-gate } 669*0Sstevel@tonic-gate return; 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate debug("type %d", type); 673*0Sstevel@tonic-gate switch (type) { 674*0Sstevel@tonic-gate case SSH_AGENTC_LOCK: 675*0Sstevel@tonic-gate case SSH_AGENTC_UNLOCK: 676*0Sstevel@tonic-gate process_lock_agent(e, type == SSH_AGENTC_LOCK); 677*0Sstevel@tonic-gate break; 678*0Sstevel@tonic-gate /* ssh1 */ 679*0Sstevel@tonic-gate case SSH_AGENTC_RSA_CHALLENGE: 680*0Sstevel@tonic-gate process_authentication_challenge1(e); 681*0Sstevel@tonic-gate break; 682*0Sstevel@tonic-gate case SSH_AGENTC_REQUEST_RSA_IDENTITIES: 683*0Sstevel@tonic-gate process_request_identities(e, 1); 684*0Sstevel@tonic-gate break; 685*0Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_IDENTITY: 686*0Sstevel@tonic-gate case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED: 687*0Sstevel@tonic-gate process_add_identity(e, 1); 688*0Sstevel@tonic-gate break; 689*0Sstevel@tonic-gate case SSH_AGENTC_REMOVE_RSA_IDENTITY: 690*0Sstevel@tonic-gate process_remove_identity(e, 1); 691*0Sstevel@tonic-gate break; 692*0Sstevel@tonic-gate case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES: 693*0Sstevel@tonic-gate process_remove_all_identities(e, 1); 694*0Sstevel@tonic-gate break; 695*0Sstevel@tonic-gate /* ssh2 */ 696*0Sstevel@tonic-gate case SSH2_AGENTC_SIGN_REQUEST: 697*0Sstevel@tonic-gate process_sign_request2(e); 698*0Sstevel@tonic-gate break; 699*0Sstevel@tonic-gate case SSH2_AGENTC_REQUEST_IDENTITIES: 700*0Sstevel@tonic-gate process_request_identities(e, 2); 701*0Sstevel@tonic-gate break; 702*0Sstevel@tonic-gate case SSH2_AGENTC_ADD_IDENTITY: 703*0Sstevel@tonic-gate case SSH2_AGENTC_ADD_ID_CONSTRAINED: 704*0Sstevel@tonic-gate process_add_identity(e, 2); 705*0Sstevel@tonic-gate break; 706*0Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_IDENTITY: 707*0Sstevel@tonic-gate process_remove_identity(e, 2); 708*0Sstevel@tonic-gate break; 709*0Sstevel@tonic-gate case SSH2_AGENTC_REMOVE_ALL_IDENTITIES: 710*0Sstevel@tonic-gate process_remove_all_identities(e, 2); 711*0Sstevel@tonic-gate break; 712*0Sstevel@tonic-gate #ifdef SMARTCARD 713*0Sstevel@tonic-gate case SSH_AGENTC_ADD_SMARTCARD_KEY: 714*0Sstevel@tonic-gate process_add_smartcard_key(e); 715*0Sstevel@tonic-gate break; 716*0Sstevel@tonic-gate case SSH_AGENTC_REMOVE_SMARTCARD_KEY: 717*0Sstevel@tonic-gate process_remove_smartcard_key(e); 718*0Sstevel@tonic-gate break; 719*0Sstevel@tonic-gate #endif /* SMARTCARD */ 720*0Sstevel@tonic-gate default: 721*0Sstevel@tonic-gate /* Unknown message. Respond with failure. */ 722*0Sstevel@tonic-gate error("Unknown message %d", type); 723*0Sstevel@tonic-gate buffer_clear(&e->request); 724*0Sstevel@tonic-gate buffer_put_int(&e->output, 1); 725*0Sstevel@tonic-gate buffer_put_char(&e->output, SSH_AGENT_FAILURE); 726*0Sstevel@tonic-gate break; 727*0Sstevel@tonic-gate } 728*0Sstevel@tonic-gate } 729*0Sstevel@tonic-gate 730*0Sstevel@tonic-gate static void 731*0Sstevel@tonic-gate new_socket(sock_type type, int fd) 732*0Sstevel@tonic-gate { 733*0Sstevel@tonic-gate u_int i, old_alloc; 734*0Sstevel@tonic-gate 735*0Sstevel@tonic-gate if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) 736*0Sstevel@tonic-gate error("fcntl O_NONBLOCK: %s", strerror(errno)); 737*0Sstevel@tonic-gate 738*0Sstevel@tonic-gate if (fd > max_fd) 739*0Sstevel@tonic-gate max_fd = fd; 740*0Sstevel@tonic-gate 741*0Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 742*0Sstevel@tonic-gate if (sockets[i].type == AUTH_UNUSED) { 743*0Sstevel@tonic-gate sockets[i].fd = fd; 744*0Sstevel@tonic-gate sockets[i].type = type; 745*0Sstevel@tonic-gate buffer_init(&sockets[i].input); 746*0Sstevel@tonic-gate buffer_init(&sockets[i].output); 747*0Sstevel@tonic-gate buffer_init(&sockets[i].request); 748*0Sstevel@tonic-gate return; 749*0Sstevel@tonic-gate } 750*0Sstevel@tonic-gate old_alloc = sockets_alloc; 751*0Sstevel@tonic-gate sockets_alloc += 10; 752*0Sstevel@tonic-gate if (sockets) 753*0Sstevel@tonic-gate sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0])); 754*0Sstevel@tonic-gate else 755*0Sstevel@tonic-gate sockets = xmalloc(sockets_alloc * sizeof(sockets[0])); 756*0Sstevel@tonic-gate for (i = old_alloc; i < sockets_alloc; i++) 757*0Sstevel@tonic-gate sockets[i].type = AUTH_UNUSED; 758*0Sstevel@tonic-gate sockets[old_alloc].type = type; 759*0Sstevel@tonic-gate sockets[old_alloc].fd = fd; 760*0Sstevel@tonic-gate buffer_init(&sockets[old_alloc].input); 761*0Sstevel@tonic-gate buffer_init(&sockets[old_alloc].output); 762*0Sstevel@tonic-gate buffer_init(&sockets[old_alloc].request); 763*0Sstevel@tonic-gate } 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gate static int 766*0Sstevel@tonic-gate prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp) 767*0Sstevel@tonic-gate { 768*0Sstevel@tonic-gate u_int i, sz; 769*0Sstevel@tonic-gate int n = 0; 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 772*0Sstevel@tonic-gate switch (sockets[i].type) { 773*0Sstevel@tonic-gate case AUTH_SOCKET: 774*0Sstevel@tonic-gate case AUTH_CONNECTION: 775*0Sstevel@tonic-gate n = MAX(n, sockets[i].fd); 776*0Sstevel@tonic-gate break; 777*0Sstevel@tonic-gate case AUTH_UNUSED: 778*0Sstevel@tonic-gate break; 779*0Sstevel@tonic-gate default: 780*0Sstevel@tonic-gate fatal("Unknown socket type %d", sockets[i].type); 781*0Sstevel@tonic-gate break; 782*0Sstevel@tonic-gate } 783*0Sstevel@tonic-gate } 784*0Sstevel@tonic-gate 785*0Sstevel@tonic-gate sz = howmany(n+1, NFDBITS) * sizeof(fd_mask); 786*0Sstevel@tonic-gate if (*fdrp == NULL || sz > *nallocp) { 787*0Sstevel@tonic-gate if (*fdrp) 788*0Sstevel@tonic-gate xfree(*fdrp); 789*0Sstevel@tonic-gate if (*fdwp) 790*0Sstevel@tonic-gate xfree(*fdwp); 791*0Sstevel@tonic-gate *fdrp = xmalloc(sz); 792*0Sstevel@tonic-gate *fdwp = xmalloc(sz); 793*0Sstevel@tonic-gate *nallocp = sz; 794*0Sstevel@tonic-gate } 795*0Sstevel@tonic-gate if (n < *fdl) 796*0Sstevel@tonic-gate debug("XXX shrink: %d < %d", n, *fdl); 797*0Sstevel@tonic-gate *fdl = n; 798*0Sstevel@tonic-gate memset(*fdrp, 0, sz); 799*0Sstevel@tonic-gate memset(*fdwp, 0, sz); 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) { 802*0Sstevel@tonic-gate switch (sockets[i].type) { 803*0Sstevel@tonic-gate case AUTH_SOCKET: 804*0Sstevel@tonic-gate case AUTH_CONNECTION: 805*0Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdrp); 806*0Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0) 807*0Sstevel@tonic-gate FD_SET(sockets[i].fd, *fdwp); 808*0Sstevel@tonic-gate break; 809*0Sstevel@tonic-gate default: 810*0Sstevel@tonic-gate break; 811*0Sstevel@tonic-gate } 812*0Sstevel@tonic-gate } 813*0Sstevel@tonic-gate return (1); 814*0Sstevel@tonic-gate } 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate static void 817*0Sstevel@tonic-gate after_select(fd_set *readset, fd_set *writeset) 818*0Sstevel@tonic-gate { 819*0Sstevel@tonic-gate struct sockaddr_un sunaddr; 820*0Sstevel@tonic-gate socklen_t slen; 821*0Sstevel@tonic-gate char buf[1024]; 822*0Sstevel@tonic-gate int len, sock; 823*0Sstevel@tonic-gate u_int i; 824*0Sstevel@tonic-gate uid_t euid; 825*0Sstevel@tonic-gate gid_t egid; 826*0Sstevel@tonic-gate 827*0Sstevel@tonic-gate for (i = 0; i < sockets_alloc; i++) 828*0Sstevel@tonic-gate switch (sockets[i].type) { 829*0Sstevel@tonic-gate case AUTH_UNUSED: 830*0Sstevel@tonic-gate break; 831*0Sstevel@tonic-gate case AUTH_SOCKET: 832*0Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 833*0Sstevel@tonic-gate slen = sizeof(sunaddr); 834*0Sstevel@tonic-gate sock = accept(sockets[i].fd, 835*0Sstevel@tonic-gate (struct sockaddr *) &sunaddr, &slen); 836*0Sstevel@tonic-gate if (sock < 0) { 837*0Sstevel@tonic-gate error("accept from AUTH_SOCKET: %s", 838*0Sstevel@tonic-gate strerror(errno)); 839*0Sstevel@tonic-gate break; 840*0Sstevel@tonic-gate } 841*0Sstevel@tonic-gate if (getpeereid(sock, &euid, &egid) < 0) { 842*0Sstevel@tonic-gate error("getpeereid %d failed: %s", 843*0Sstevel@tonic-gate sock, strerror(errno)); 844*0Sstevel@tonic-gate close(sock); 845*0Sstevel@tonic-gate break; 846*0Sstevel@tonic-gate } 847*0Sstevel@tonic-gate if ((euid != 0) && (getuid() != euid)) { 848*0Sstevel@tonic-gate error("uid mismatch: " 849*0Sstevel@tonic-gate "peer euid %u != uid %u", 850*0Sstevel@tonic-gate (u_int) euid, (u_int) getuid()); 851*0Sstevel@tonic-gate close(sock); 852*0Sstevel@tonic-gate break; 853*0Sstevel@tonic-gate } 854*0Sstevel@tonic-gate new_socket(AUTH_CONNECTION, sock); 855*0Sstevel@tonic-gate } 856*0Sstevel@tonic-gate break; 857*0Sstevel@tonic-gate case AUTH_CONNECTION: 858*0Sstevel@tonic-gate if (buffer_len(&sockets[i].output) > 0 && 859*0Sstevel@tonic-gate FD_ISSET(sockets[i].fd, writeset)) { 860*0Sstevel@tonic-gate do { 861*0Sstevel@tonic-gate len = write(sockets[i].fd, 862*0Sstevel@tonic-gate buffer_ptr(&sockets[i].output), 863*0Sstevel@tonic-gate buffer_len(&sockets[i].output)); 864*0Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 865*0Sstevel@tonic-gate errno == EINTR)) 866*0Sstevel@tonic-gate continue; 867*0Sstevel@tonic-gate break; 868*0Sstevel@tonic-gate } while (1); 869*0Sstevel@tonic-gate if (len <= 0) { 870*0Sstevel@tonic-gate close_socket(&sockets[i]); 871*0Sstevel@tonic-gate break; 872*0Sstevel@tonic-gate } 873*0Sstevel@tonic-gate buffer_consume(&sockets[i].output, len); 874*0Sstevel@tonic-gate } 875*0Sstevel@tonic-gate if (FD_ISSET(sockets[i].fd, readset)) { 876*0Sstevel@tonic-gate do { 877*0Sstevel@tonic-gate len = read(sockets[i].fd, buf, sizeof(buf)); 878*0Sstevel@tonic-gate if (len == -1 && (errno == EAGAIN || 879*0Sstevel@tonic-gate errno == EINTR)) 880*0Sstevel@tonic-gate continue; 881*0Sstevel@tonic-gate break; 882*0Sstevel@tonic-gate } while (1); 883*0Sstevel@tonic-gate if (len <= 0) { 884*0Sstevel@tonic-gate close_socket(&sockets[i]); 885*0Sstevel@tonic-gate break; 886*0Sstevel@tonic-gate } 887*0Sstevel@tonic-gate buffer_append(&sockets[i].input, buf, len); 888*0Sstevel@tonic-gate process_message(&sockets[i]); 889*0Sstevel@tonic-gate } 890*0Sstevel@tonic-gate break; 891*0Sstevel@tonic-gate default: 892*0Sstevel@tonic-gate fatal("Unknown type %d", sockets[i].type); 893*0Sstevel@tonic-gate } 894*0Sstevel@tonic-gate } 895*0Sstevel@tonic-gate 896*0Sstevel@tonic-gate static void 897*0Sstevel@tonic-gate cleanup_socket(void *p) 898*0Sstevel@tonic-gate { 899*0Sstevel@tonic-gate if (socket_name[0]) 900*0Sstevel@tonic-gate unlink(socket_name); 901*0Sstevel@tonic-gate if (socket_dir[0]) 902*0Sstevel@tonic-gate rmdir(socket_dir); 903*0Sstevel@tonic-gate } 904*0Sstevel@tonic-gate 905*0Sstevel@tonic-gate static void 906*0Sstevel@tonic-gate cleanup_exit(int i) 907*0Sstevel@tonic-gate { 908*0Sstevel@tonic-gate cleanup_socket(NULL); 909*0Sstevel@tonic-gate exit(i); 910*0Sstevel@tonic-gate } 911*0Sstevel@tonic-gate 912*0Sstevel@tonic-gate static void 913*0Sstevel@tonic-gate cleanup_handler(int sig) 914*0Sstevel@tonic-gate { 915*0Sstevel@tonic-gate cleanup_socket(NULL); 916*0Sstevel@tonic-gate _exit(2); 917*0Sstevel@tonic-gate } 918*0Sstevel@tonic-gate 919*0Sstevel@tonic-gate static void 920*0Sstevel@tonic-gate check_parent_exists(int sig) 921*0Sstevel@tonic-gate { 922*0Sstevel@tonic-gate int save_errno = errno; 923*0Sstevel@tonic-gate 924*0Sstevel@tonic-gate if (parent_pid != -1 && getppid() != parent_pid) { 925*0Sstevel@tonic-gate /* printf("Parent has died - Authentication agent exiting.\n"); */ 926*0Sstevel@tonic-gate cleanup_handler(sig); /* safe */ 927*0Sstevel@tonic-gate } 928*0Sstevel@tonic-gate signal(SIGALRM, check_parent_exists); 929*0Sstevel@tonic-gate alarm(10); 930*0Sstevel@tonic-gate errno = save_errno; 931*0Sstevel@tonic-gate } 932*0Sstevel@tonic-gate 933*0Sstevel@tonic-gate static void 934*0Sstevel@tonic-gate usage(void) 935*0Sstevel@tonic-gate { 936*0Sstevel@tonic-gate fprintf(stderr, 937*0Sstevel@tonic-gate gettext("Usage: %s [options] [command [args ...]]\n" 938*0Sstevel@tonic-gate "Options:\n" 939*0Sstevel@tonic-gate " -c Generate C-shell commands on stdout.\n" 940*0Sstevel@tonic-gate " -s Generate Bourne shell commands on stdout.\n" 941*0Sstevel@tonic-gate " -k Kill the current agent.\n" 942*0Sstevel@tonic-gate " -d Debug mode.\n" 943*0Sstevel@tonic-gate " -a socket Bind agent socket to given name.\n"), 944*0Sstevel@tonic-gate __progname); 945*0Sstevel@tonic-gate exit(1); 946*0Sstevel@tonic-gate } 947*0Sstevel@tonic-gate 948*0Sstevel@tonic-gate int 949*0Sstevel@tonic-gate main(int ac, char **av) 950*0Sstevel@tonic-gate { 951*0Sstevel@tonic-gate int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc; 952*0Sstevel@tonic-gate char *shell, *pidstr, *agentsocket = NULL; 953*0Sstevel@tonic-gate const char *format; 954*0Sstevel@tonic-gate fd_set *readsetp = NULL, *writesetp = NULL; 955*0Sstevel@tonic-gate struct sockaddr_un sunaddr; 956*0Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 957*0Sstevel@tonic-gate struct rlimit rlim; 958*0Sstevel@tonic-gate #endif 959*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN 960*0Sstevel@tonic-gate int prev_mask; 961*0Sstevel@tonic-gate #endif 962*0Sstevel@tonic-gate extern int optind; 963*0Sstevel@tonic-gate extern char *optarg; 964*0Sstevel@tonic-gate pid_t pid; 965*0Sstevel@tonic-gate char pidstrbuf[1 + 3 * sizeof pid]; 966*0Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 967*0Sstevel@tonic-gate priv_set_t *myprivs; 968*0Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 969*0Sstevel@tonic-gate 970*0Sstevel@tonic-gate /* drop */ 971*0Sstevel@tonic-gate setegid(getgid()); 972*0Sstevel@tonic-gate setgid(getgid()); 973*0Sstevel@tonic-gate 974*0Sstevel@tonic-gate (void) g11n_setlocale(LC_ALL, ""); 975*0Sstevel@tonic-gate 976*0Sstevel@tonic-gate SSLeay_add_all_algorithms(); 977*0Sstevel@tonic-gate 978*0Sstevel@tonic-gate __progname = get_progname(av[0]); 979*0Sstevel@tonic-gate init_rng(); 980*0Sstevel@tonic-gate seed_rng(); 981*0Sstevel@tonic-gate 982*0Sstevel@tonic-gate while ((ch = getopt(ac, av, "cdksa:")) != -1) { 983*0Sstevel@tonic-gate switch (ch) { 984*0Sstevel@tonic-gate case 'c': 985*0Sstevel@tonic-gate if (s_flag) 986*0Sstevel@tonic-gate usage(); 987*0Sstevel@tonic-gate c_flag++; 988*0Sstevel@tonic-gate break; 989*0Sstevel@tonic-gate case 'k': 990*0Sstevel@tonic-gate k_flag++; 991*0Sstevel@tonic-gate break; 992*0Sstevel@tonic-gate case 's': 993*0Sstevel@tonic-gate if (c_flag) 994*0Sstevel@tonic-gate usage(); 995*0Sstevel@tonic-gate s_flag++; 996*0Sstevel@tonic-gate break; 997*0Sstevel@tonic-gate case 'd': 998*0Sstevel@tonic-gate if (d_flag) 999*0Sstevel@tonic-gate usage(); 1000*0Sstevel@tonic-gate d_flag++; 1001*0Sstevel@tonic-gate break; 1002*0Sstevel@tonic-gate case 'a': 1003*0Sstevel@tonic-gate agentsocket = optarg; 1004*0Sstevel@tonic-gate break; 1005*0Sstevel@tonic-gate default: 1006*0Sstevel@tonic-gate usage(); 1007*0Sstevel@tonic-gate } 1008*0Sstevel@tonic-gate } 1009*0Sstevel@tonic-gate ac -= optind; 1010*0Sstevel@tonic-gate av += optind; 1011*0Sstevel@tonic-gate 1012*0Sstevel@tonic-gate if (ac > 0 && (c_flag || k_flag || s_flag || d_flag)) 1013*0Sstevel@tonic-gate usage(); 1014*0Sstevel@tonic-gate 1015*0Sstevel@tonic-gate if (ac == 0 && !c_flag && !s_flag) { 1016*0Sstevel@tonic-gate shell = getenv("SHELL"); 1017*0Sstevel@tonic-gate if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0) 1018*0Sstevel@tonic-gate c_flag = 1; 1019*0Sstevel@tonic-gate } 1020*0Sstevel@tonic-gate if (k_flag) { 1021*0Sstevel@tonic-gate pidstr = getenv(SSH_AGENTPID_ENV_NAME); 1022*0Sstevel@tonic-gate if (pidstr == NULL) { 1023*0Sstevel@tonic-gate fprintf(stderr, 1024*0Sstevel@tonic-gate gettext("%s not set, cannot kill agent\n"), 1025*0Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 1026*0Sstevel@tonic-gate exit(1); 1027*0Sstevel@tonic-gate } 1028*0Sstevel@tonic-gate pid = atoi(pidstr); 1029*0Sstevel@tonic-gate if (pid < 1) { 1030*0Sstevel@tonic-gate fprintf(stderr, 1031*0Sstevel@tonic-gate gettext("%s=\")%s\", which is not a good PID\n"), 1032*0Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME, pidstr); 1033*0Sstevel@tonic-gate exit(1); 1034*0Sstevel@tonic-gate } 1035*0Sstevel@tonic-gate if (kill(pid, SIGTERM) == -1) { 1036*0Sstevel@tonic-gate perror("kill"); 1037*0Sstevel@tonic-gate exit(1); 1038*0Sstevel@tonic-gate } 1039*0Sstevel@tonic-gate format = c_flag ? "unsetenv %s;\n" : "unset %s;\n"; 1040*0Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME); 1041*0Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME); 1042*0Sstevel@tonic-gate printf("echo "); 1043*0Sstevel@tonic-gate printf(gettext("Agent pid %ld killed;\n"), (long)pid); 1044*0Sstevel@tonic-gate exit(0); 1045*0Sstevel@tonic-gate } 1046*0Sstevel@tonic-gate parent_pid = getpid(); 1047*0Sstevel@tonic-gate 1048*0Sstevel@tonic-gate if (agentsocket == NULL) { 1049*0Sstevel@tonic-gate /* Create private directory for agent socket */ 1050*0Sstevel@tonic-gate strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir); 1051*0Sstevel@tonic-gate if (mkdtemp(socket_dir) == NULL) { 1052*0Sstevel@tonic-gate perror("mkdtemp: private socket dir"); 1053*0Sstevel@tonic-gate exit(1); 1054*0Sstevel@tonic-gate } 1055*0Sstevel@tonic-gate snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir, 1056*0Sstevel@tonic-gate (long)parent_pid); 1057*0Sstevel@tonic-gate } else { 1058*0Sstevel@tonic-gate /* Try to use specified agent socket */ 1059*0Sstevel@tonic-gate socket_dir[0] = '\0'; 1060*0Sstevel@tonic-gate strlcpy(socket_name, agentsocket, sizeof socket_name); 1061*0Sstevel@tonic-gate } 1062*0Sstevel@tonic-gate 1063*0Sstevel@tonic-gate /* 1064*0Sstevel@tonic-gate * Create socket early so it will exist before command gets run from 1065*0Sstevel@tonic-gate * the parent. 1066*0Sstevel@tonic-gate */ 1067*0Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0); 1068*0Sstevel@tonic-gate if (sock < 0) { 1069*0Sstevel@tonic-gate perror("socket"); 1070*0Sstevel@tonic-gate cleanup_exit(1); 1071*0Sstevel@tonic-gate } 1072*0Sstevel@tonic-gate memset(&sunaddr, 0, sizeof(sunaddr)); 1073*0Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX; 1074*0Sstevel@tonic-gate strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path)); 1075*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN 1076*0Sstevel@tonic-gate prev_mask = umask(0177); 1077*0Sstevel@tonic-gate #endif 1078*0Sstevel@tonic-gate if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { 1079*0Sstevel@tonic-gate perror("bind"); 1080*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN 1081*0Sstevel@tonic-gate umask(prev_mask); 1082*0Sstevel@tonic-gate #endif 1083*0Sstevel@tonic-gate cleanup_exit(1); 1084*0Sstevel@tonic-gate } 1085*0Sstevel@tonic-gate #ifdef HAVE_CYGWIN 1086*0Sstevel@tonic-gate umask(prev_mask); 1087*0Sstevel@tonic-gate #endif 1088*0Sstevel@tonic-gate if (listen(sock, 128) < 0) { 1089*0Sstevel@tonic-gate perror("listen"); 1090*0Sstevel@tonic-gate cleanup_exit(1); 1091*0Sstevel@tonic-gate } 1092*0Sstevel@tonic-gate 1093*0Sstevel@tonic-gate /* 1094*0Sstevel@tonic-gate * Fork, and have the parent execute the command, if any, or present 1095*0Sstevel@tonic-gate * the socket data. The child continues as the authentication agent. 1096*0Sstevel@tonic-gate */ 1097*0Sstevel@tonic-gate if (d_flag) { 1098*0Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1); 1099*0Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 1100*0Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 1101*0Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 1102*0Sstevel@tonic-gate printf("echo "); 1103*0Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)parent_pid); 1104*0Sstevel@tonic-gate goto skip; 1105*0Sstevel@tonic-gate } 1106*0Sstevel@tonic-gate pid = fork(); 1107*0Sstevel@tonic-gate if (pid == -1) { 1108*0Sstevel@tonic-gate perror("fork"); 1109*0Sstevel@tonic-gate cleanup_exit(1); 1110*0Sstevel@tonic-gate } 1111*0Sstevel@tonic-gate if (pid != 0) { /* Parent - execute the given command. */ 1112*0Sstevel@tonic-gate close(sock); 1113*0Sstevel@tonic-gate snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid); 1114*0Sstevel@tonic-gate if (ac == 0) { 1115*0Sstevel@tonic-gate format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n"; 1116*0Sstevel@tonic-gate printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name, 1117*0Sstevel@tonic-gate SSH_AUTHSOCKET_ENV_NAME); 1118*0Sstevel@tonic-gate printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf, 1119*0Sstevel@tonic-gate SSH_AGENTPID_ENV_NAME); 1120*0Sstevel@tonic-gate printf("echo "); 1121*0Sstevel@tonic-gate printf(gettext("Agent pid %ld;\n"), (long)pid); 1122*0Sstevel@tonic-gate exit(0); 1123*0Sstevel@tonic-gate } 1124*0Sstevel@tonic-gate if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 || 1125*0Sstevel@tonic-gate setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) { 1126*0Sstevel@tonic-gate perror("setenv"); 1127*0Sstevel@tonic-gate exit(1); 1128*0Sstevel@tonic-gate } 1129*0Sstevel@tonic-gate execvp(av[0], av); 1130*0Sstevel@tonic-gate perror(av[0]); 1131*0Sstevel@tonic-gate exit(1); 1132*0Sstevel@tonic-gate } 1133*0Sstevel@tonic-gate /* child */ 1134*0Sstevel@tonic-gate log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0); 1135*0Sstevel@tonic-gate 1136*0Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE 1137*0Sstevel@tonic-gate /* 1138*0Sstevel@tonic-gate * Drop unneeded privs, including basic ones like fork/exec. 1139*0Sstevel@tonic-gate * 1140*0Sstevel@tonic-gate * Idiom: remove from 'basic' privs we know we don't want, 1141*0Sstevel@tonic-gate * invert the result and remove the resulting set from P. 1142*0Sstevel@tonic-gate * 1143*0Sstevel@tonic-gate * None of the priv_delset() calls below, nor the setppriv call 1144*0Sstevel@tonic-gate * below can fail, so their return values are not checked. 1145*0Sstevel@tonic-gate */ 1146*0Sstevel@tonic-gate if ((myprivs = priv_str_to_set("basic", ",", NULL)) == NULL) 1147*0Sstevel@tonic-gate fatal("priv_str_to_set failed: %m"); 1148*0Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_EXEC); 1149*0Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_FORK); 1150*0Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_FILE_LINK_ANY); 1151*0Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_INFO); 1152*0Sstevel@tonic-gate (void) priv_delset(myprivs, PRIV_PROC_SESSION); 1153*0Sstevel@tonic-gate priv_inverse(myprivs); 1154*0Sstevel@tonic-gate (void) setppriv(PRIV_OFF, PRIV_PERMITTED, myprivs); 1155*0Sstevel@tonic-gate (void) priv_freeset(myprivs); 1156*0Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */ 1157*0Sstevel@tonic-gate 1158*0Sstevel@tonic-gate if (setsid() == -1) { 1159*0Sstevel@tonic-gate error("setsid: %s", strerror(errno)); 1160*0Sstevel@tonic-gate cleanup_exit(1); 1161*0Sstevel@tonic-gate } 1162*0Sstevel@tonic-gate 1163*0Sstevel@tonic-gate (void)chdir("/"); 1164*0Sstevel@tonic-gate close(0); 1165*0Sstevel@tonic-gate close(1); 1166*0Sstevel@tonic-gate close(2); 1167*0Sstevel@tonic-gate 1168*0Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT 1169*0Sstevel@tonic-gate /* deny core dumps, since memory contains unencrypted private keys */ 1170*0Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max = 0; 1171*0Sstevel@tonic-gate if (setrlimit(RLIMIT_CORE, &rlim) < 0) { 1172*0Sstevel@tonic-gate error("setrlimit RLIMIT_CORE: %s", strerror(errno)); 1173*0Sstevel@tonic-gate cleanup_exit(1); 1174*0Sstevel@tonic-gate } 1175*0Sstevel@tonic-gate #endif 1176*0Sstevel@tonic-gate 1177*0Sstevel@tonic-gate skip: 1178*0Sstevel@tonic-gate fatal_add_cleanup(cleanup_socket, NULL); 1179*0Sstevel@tonic-gate new_socket(AUTH_SOCKET, sock); 1180*0Sstevel@tonic-gate if (ac > 0) { 1181*0Sstevel@tonic-gate signal(SIGALRM, check_parent_exists); 1182*0Sstevel@tonic-gate alarm(10); 1183*0Sstevel@tonic-gate } 1184*0Sstevel@tonic-gate idtab_init(); 1185*0Sstevel@tonic-gate if (!d_flag) 1186*0Sstevel@tonic-gate signal(SIGINT, SIG_IGN); 1187*0Sstevel@tonic-gate signal(SIGPIPE, SIG_IGN); 1188*0Sstevel@tonic-gate signal(SIGHUP, cleanup_handler); 1189*0Sstevel@tonic-gate signal(SIGTERM, cleanup_handler); 1190*0Sstevel@tonic-gate nalloc = 0; 1191*0Sstevel@tonic-gate 1192*0Sstevel@tonic-gate while (1) { 1193*0Sstevel@tonic-gate prepare_select(&readsetp, &writesetp, &max_fd, &nalloc); 1194*0Sstevel@tonic-gate if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) { 1195*0Sstevel@tonic-gate if (errno == EINTR) 1196*0Sstevel@tonic-gate continue; 1197*0Sstevel@tonic-gate fatal("select: %s", strerror(errno)); 1198*0Sstevel@tonic-gate } 1199*0Sstevel@tonic-gate after_select(readsetp, writesetp); 1200*0Sstevel@tonic-gate } 1201*0Sstevel@tonic-gate /* NOTREACHED */ 1202*0Sstevel@tonic-gate return (0); /* keep lint happy */ 1203*0Sstevel@tonic-gate } 1204