xref: /titanic_44/usr/src/cmd/ssh/ssh-agent/ssh-agent.c (revision 6f786ace10b9c0c7c5515e525fb660fbccfda6a3)
17c478bd9Sstevel@tonic-gate /*
2*6f786aceSNobutomo 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"
437c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: ssh-agent.c,v 1.105 2002/10/01 20:34:12 markus 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 "getput.h"
587c478bd9Sstevel@tonic-gate #include "key.h"
597c478bd9Sstevel@tonic-gate #include "authfd.h"
607c478bd9Sstevel@tonic-gate #include "compat.h"
617c478bd9Sstevel@tonic-gate #include "log.h"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate typedef enum {
647c478bd9Sstevel@tonic-gate 	AUTH_UNUSED,
657c478bd9Sstevel@tonic-gate 	AUTH_SOCKET,
667c478bd9Sstevel@tonic-gate 	AUTH_CONNECTION
677c478bd9Sstevel@tonic-gate } sock_type;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate typedef struct {
707c478bd9Sstevel@tonic-gate 	int fd;
717c478bd9Sstevel@tonic-gate 	sock_type type;
727c478bd9Sstevel@tonic-gate 	Buffer input;
737c478bd9Sstevel@tonic-gate 	Buffer output;
747c478bd9Sstevel@tonic-gate 	Buffer request;
757c478bd9Sstevel@tonic-gate } SocketEntry;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate u_int sockets_alloc = 0;
787c478bd9Sstevel@tonic-gate SocketEntry *sockets = NULL;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate typedef struct identity {
817c478bd9Sstevel@tonic-gate 	TAILQ_ENTRY(identity) next;
827c478bd9Sstevel@tonic-gate 	Key *key;
837c478bd9Sstevel@tonic-gate 	char *comment;
847c478bd9Sstevel@tonic-gate 	u_int death;
857c478bd9Sstevel@tonic-gate } Identity;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate typedef struct {
887c478bd9Sstevel@tonic-gate 	int nentries;
897c478bd9Sstevel@tonic-gate 	TAILQ_HEAD(idqueue, identity) idlist;
907c478bd9Sstevel@tonic-gate } Idtab;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* private key table, one per protocol version */
937c478bd9Sstevel@tonic-gate Idtab idtable[3];
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate int max_fd = 0;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /* pid of shell == parent of agent */
987c478bd9Sstevel@tonic-gate pid_t parent_pid = -1;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* pathname and directory for AUTH_SOCKET */
1017c478bd9Sstevel@tonic-gate char socket_name[1024];
1027c478bd9Sstevel@tonic-gate char socket_dir[1024];
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* locking */
1057c478bd9Sstevel@tonic-gate int locked = 0;
1067c478bd9Sstevel@tonic-gate char *lock_passwd = NULL;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
1097c478bd9Sstevel@tonic-gate extern char *__progname;
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate char *__progname;
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static void
1157c478bd9Sstevel@tonic-gate close_socket(SocketEntry *e)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate 	close(e->fd);
1187c478bd9Sstevel@tonic-gate 	e->fd = -1;
1197c478bd9Sstevel@tonic-gate 	e->type = AUTH_UNUSED;
1207c478bd9Sstevel@tonic-gate 	buffer_free(&e->input);
1217c478bd9Sstevel@tonic-gate 	buffer_free(&e->output);
1227c478bd9Sstevel@tonic-gate 	buffer_free(&e->request);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static void
1267c478bd9Sstevel@tonic-gate idtab_init(void)
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	int i;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	for (i = 0; i <=2; i++) {
1317c478bd9Sstevel@tonic-gate 		TAILQ_INIT(&idtable[i].idlist);
1327c478bd9Sstevel@tonic-gate 		idtable[i].nentries = 0;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* return private key table for requested protocol version */
1377c478bd9Sstevel@tonic-gate static Idtab *
1387c478bd9Sstevel@tonic-gate idtab_lookup(int version)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	if (version < 1 || version > 2)
1417c478bd9Sstevel@tonic-gate 		fatal("internal error, bad protocol version %d", version);
1427c478bd9Sstevel@tonic-gate 	return &idtable[version];
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static void
1467c478bd9Sstevel@tonic-gate free_identity(Identity *id)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	key_free(id->key);
1497c478bd9Sstevel@tonic-gate 	xfree(id->comment);
1507c478bd9Sstevel@tonic-gate 	xfree(id);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* return matching private key for given public key */
1547c478bd9Sstevel@tonic-gate static Identity *
1557c478bd9Sstevel@tonic-gate lookup_identity(Key *key, int version)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	Identity *id;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	Idtab *tab = idtab_lookup(version);
1607c478bd9Sstevel@tonic-gate 	TAILQ_FOREACH(id, &tab->idlist, next) {
1617c478bd9Sstevel@tonic-gate 		if (key_equal(key, id->key))
1627c478bd9Sstevel@tonic-gate 			return (id);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	return (NULL);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /* send list of supported public keys to 'client' */
1687c478bd9Sstevel@tonic-gate static void
1697c478bd9Sstevel@tonic-gate process_request_identities(SocketEntry *e, int version)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	Idtab *tab = idtab_lookup(version);
1727c478bd9Sstevel@tonic-gate 	Identity *id;
1737c478bd9Sstevel@tonic-gate 	Buffer msg;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	buffer_init(&msg);
1767c478bd9Sstevel@tonic-gate 	buffer_put_char(&msg, (version == 1) ?
1777c478bd9Sstevel@tonic-gate 	    SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
1787c478bd9Sstevel@tonic-gate 	buffer_put_int(&msg, tab->nentries);
1797c478bd9Sstevel@tonic-gate 	TAILQ_FOREACH(id, &tab->idlist, next) {
1807c478bd9Sstevel@tonic-gate 		if (id->key->type == KEY_RSA1) {
1817c478bd9Sstevel@tonic-gate 			buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
1827c478bd9Sstevel@tonic-gate 			buffer_put_bignum(&msg, id->key->rsa->e);
1837c478bd9Sstevel@tonic-gate 			buffer_put_bignum(&msg, id->key->rsa->n);
1847c478bd9Sstevel@tonic-gate 		} else {
1857c478bd9Sstevel@tonic-gate 			u_char *blob;
1867c478bd9Sstevel@tonic-gate 			u_int blen;
1877c478bd9Sstevel@tonic-gate 			key_to_blob(id->key, &blob, &blen);
1887c478bd9Sstevel@tonic-gate 			buffer_put_string(&msg, blob, blen);
1897c478bd9Sstevel@tonic-gate 			xfree(blob);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		buffer_put_cstring(&msg, id->comment);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, buffer_len(&msg));
1947c478bd9Sstevel@tonic-gate 	buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
1957c478bd9Sstevel@tonic-gate 	buffer_free(&msg);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /* ssh1 only */
1997c478bd9Sstevel@tonic-gate static void
2007c478bd9Sstevel@tonic-gate process_authentication_challenge1(SocketEntry *e)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	u_char buf[32], mdbuf[16], session_id[16];
2037c478bd9Sstevel@tonic-gate 	u_int response_type;
2047c478bd9Sstevel@tonic-gate 	BIGNUM *challenge;
2057c478bd9Sstevel@tonic-gate 	Identity *id;
2067c478bd9Sstevel@tonic-gate 	int i, len;
2077c478bd9Sstevel@tonic-gate 	Buffer msg;
2087c478bd9Sstevel@tonic-gate 	MD5_CTX md;
2097c478bd9Sstevel@tonic-gate 	Key *key;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	buffer_init(&msg);
2127c478bd9Sstevel@tonic-gate 	key = key_new(KEY_RSA1);
2137c478bd9Sstevel@tonic-gate 	if ((challenge = BN_new()) == NULL)
2147c478bd9Sstevel@tonic-gate 		fatal("process_authentication_challenge1: BN_new failed");
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	(void) buffer_get_int(&e->request);			/* ignored */
2177c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&e->request, key->rsa->e);
2187c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&e->request, key->rsa->n);
2197c478bd9Sstevel@tonic-gate 	buffer_get_bignum(&e->request, challenge);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	/* Only protocol 1.1 is supported */
2227c478bd9Sstevel@tonic-gate 	if (buffer_len(&e->request) == 0)
2237c478bd9Sstevel@tonic-gate 		goto failure;
2247c478bd9Sstevel@tonic-gate 	buffer_get(&e->request, session_id, 16);
2257c478bd9Sstevel@tonic-gate 	response_type = buffer_get_int(&e->request);
2267c478bd9Sstevel@tonic-gate 	if (response_type != 1)
2277c478bd9Sstevel@tonic-gate 		goto failure;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	id = lookup_identity(key, 1);
2307c478bd9Sstevel@tonic-gate 	if (id != NULL) {
2317c478bd9Sstevel@tonic-gate 		Key *private = id->key;
2327c478bd9Sstevel@tonic-gate 		/* Decrypt the challenge using the private key. */
2337c478bd9Sstevel@tonic-gate 		if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
2347c478bd9Sstevel@tonic-gate 			goto failure;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		/* The response is MD5 of decrypted challenge plus session id. */
2377c478bd9Sstevel@tonic-gate 		len = BN_num_bytes(challenge);
2387c478bd9Sstevel@tonic-gate 		if (len <= 0 || len > 32) {
2397c478bd9Sstevel@tonic-gate 			log("process_authentication_challenge: bad challenge length %d", len);
2407c478bd9Sstevel@tonic-gate 			goto failure;
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 		memset(buf, 0, 32);
2437c478bd9Sstevel@tonic-gate 		BN_bn2bin(challenge, buf + 32 - len);
2447c478bd9Sstevel@tonic-gate 		MD5_Init(&md);
2457c478bd9Sstevel@tonic-gate 		MD5_Update(&md, buf, 32);
2467c478bd9Sstevel@tonic-gate 		MD5_Update(&md, session_id, 16);
2477c478bd9Sstevel@tonic-gate 		MD5_Final(mdbuf, &md);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 		/* Send the response. */
2507c478bd9Sstevel@tonic-gate 		buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
2517c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++)
2527c478bd9Sstevel@tonic-gate 			buffer_put_char(&msg, mdbuf[i]);
2537c478bd9Sstevel@tonic-gate 		goto send;
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate failure:
2577c478bd9Sstevel@tonic-gate 	/* Unknown identity or protocol error.  Send failure. */
2587c478bd9Sstevel@tonic-gate 	buffer_put_char(&msg, SSH_AGENT_FAILURE);
2597c478bd9Sstevel@tonic-gate send:
2607c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, buffer_len(&msg));
2617c478bd9Sstevel@tonic-gate 	buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
2627c478bd9Sstevel@tonic-gate 	key_free(key);
2637c478bd9Sstevel@tonic-gate 	BN_clear_free(challenge);
2647c478bd9Sstevel@tonic-gate 	buffer_free(&msg);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /* ssh2 only */
2687c478bd9Sstevel@tonic-gate static void
2697c478bd9Sstevel@tonic-gate process_sign_request2(SocketEntry *e)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	u_char *blob, *data, *signature = NULL;
2727c478bd9Sstevel@tonic-gate 	u_int blen, dlen, slen = 0;
2737c478bd9Sstevel@tonic-gate 	int ok = -1, flags;
2747c478bd9Sstevel@tonic-gate 	Buffer msg;
2757c478bd9Sstevel@tonic-gate 	Key *key;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	datafellows = 0;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	blob = buffer_get_string(&e->request, &blen);
2807c478bd9Sstevel@tonic-gate 	data = buffer_get_string(&e->request, &dlen);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	flags = buffer_get_int(&e->request);
2837c478bd9Sstevel@tonic-gate 	if (flags & SSH_AGENT_OLD_SIGNATURE)
2847c478bd9Sstevel@tonic-gate 		datafellows = SSH_BUG_SIGBLOB;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	key = key_from_blob(blob, blen);
2877c478bd9Sstevel@tonic-gate 	if (key != NULL) {
2887c478bd9Sstevel@tonic-gate 		Identity *id = lookup_identity(key, 2);
2897c478bd9Sstevel@tonic-gate 		if (id != NULL)
2907c478bd9Sstevel@tonic-gate 			ok = key_sign(id->key, &signature, &slen, data, dlen);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	key_free(key);
2937c478bd9Sstevel@tonic-gate 	buffer_init(&msg);
2947c478bd9Sstevel@tonic-gate 	if (ok == 0) {
2957c478bd9Sstevel@tonic-gate 		buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
2967c478bd9Sstevel@tonic-gate 		buffer_put_string(&msg, signature, slen);
2977c478bd9Sstevel@tonic-gate 	} else {
2987c478bd9Sstevel@tonic-gate 		buffer_put_char(&msg, SSH_AGENT_FAILURE);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, buffer_len(&msg));
3017c478bd9Sstevel@tonic-gate 	buffer_append(&e->output, buffer_ptr(&msg),
3027c478bd9Sstevel@tonic-gate 	    buffer_len(&msg));
3037c478bd9Sstevel@tonic-gate 	buffer_free(&msg);
3047c478bd9Sstevel@tonic-gate 	xfree(data);
3057c478bd9Sstevel@tonic-gate 	xfree(blob);
3067c478bd9Sstevel@tonic-gate 	if (signature != NULL)
3077c478bd9Sstevel@tonic-gate 		xfree(signature);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /* shared */
3117c478bd9Sstevel@tonic-gate static void
3127c478bd9Sstevel@tonic-gate process_remove_identity(SocketEntry *e, int version)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	u_int blen, bits;
3157c478bd9Sstevel@tonic-gate 	int success = 0;
3167c478bd9Sstevel@tonic-gate 	Key *key = NULL;
3177c478bd9Sstevel@tonic-gate 	u_char *blob;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	switch (version) {
3207c478bd9Sstevel@tonic-gate 	case 1:
3217c478bd9Sstevel@tonic-gate 		key = key_new(KEY_RSA1);
3227c478bd9Sstevel@tonic-gate 		bits = buffer_get_int(&e->request);
3237c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, key->rsa->e);
3247c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, key->rsa->n);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (bits != key_size(key))
3277c478bd9Sstevel@tonic-gate 			log("Warning: identity keysize mismatch: actual %u, announced %u",
3287c478bd9Sstevel@tonic-gate 			    key_size(key), bits);
3297c478bd9Sstevel@tonic-gate 		break;
3307c478bd9Sstevel@tonic-gate 	case 2:
3317c478bd9Sstevel@tonic-gate 		blob = buffer_get_string(&e->request, &blen);
3327c478bd9Sstevel@tonic-gate 		key = key_from_blob(blob, blen);
3337c478bd9Sstevel@tonic-gate 		xfree(blob);
3347c478bd9Sstevel@tonic-gate 		break;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (key != NULL) {
3377c478bd9Sstevel@tonic-gate 		Identity *id = lookup_identity(key, version);
3387c478bd9Sstevel@tonic-gate 		if (id != NULL) {
3397c478bd9Sstevel@tonic-gate 			/*
3407c478bd9Sstevel@tonic-gate 			 * We have this key.  Free the old key.  Since we
3417c478bd9Sstevel@tonic-gate 			 * don\'t want to leave empty slots in the middle of
3427c478bd9Sstevel@tonic-gate 			 * the array, we actually free the key there and move
3437c478bd9Sstevel@tonic-gate 			 * all the entries between the empty slot and the end
3447c478bd9Sstevel@tonic-gate 			 * of the array.
3457c478bd9Sstevel@tonic-gate 			 */
3467c478bd9Sstevel@tonic-gate 			Idtab *tab = idtab_lookup(version);
3477c478bd9Sstevel@tonic-gate 			if (tab->nentries < 1)
3487c478bd9Sstevel@tonic-gate 				fatal("process_remove_identity: "
3497c478bd9Sstevel@tonic-gate 				    "internal error: tab->nentries %d",
3507c478bd9Sstevel@tonic-gate 				    tab->nentries);
3517c478bd9Sstevel@tonic-gate 			TAILQ_REMOVE(&tab->idlist, id, next);
3527c478bd9Sstevel@tonic-gate 			free_identity(id);
3537c478bd9Sstevel@tonic-gate 			tab->nentries--;
3547c478bd9Sstevel@tonic-gate 			success = 1;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		key_free(key);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, 1);
3597c478bd9Sstevel@tonic-gate 	buffer_put_char(&e->output,
3607c478bd9Sstevel@tonic-gate 	    success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static void
3647c478bd9Sstevel@tonic-gate process_remove_all_identities(SocketEntry *e, int version)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	Idtab *tab = idtab_lookup(version);
3677c478bd9Sstevel@tonic-gate 	Identity *id;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/* Loop over all identities and clear the keys. */
3707c478bd9Sstevel@tonic-gate 	for (id = TAILQ_FIRST(&tab->idlist); id;
3717c478bd9Sstevel@tonic-gate 	    id = TAILQ_FIRST(&tab->idlist)) {
3727c478bd9Sstevel@tonic-gate 		TAILQ_REMOVE(&tab->idlist, id, next);
3737c478bd9Sstevel@tonic-gate 		free_identity(id);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/* Mark that there are no identities. */
3777c478bd9Sstevel@tonic-gate 	tab->nentries = 0;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* Send success. */
3807c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, 1);
3817c478bd9Sstevel@tonic-gate 	buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static void
3857c478bd9Sstevel@tonic-gate reaper(void)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	u_int now = time(NULL);
3887c478bd9Sstevel@tonic-gate 	Identity *id, *nxt;
3897c478bd9Sstevel@tonic-gate 	int version;
3907c478bd9Sstevel@tonic-gate 	Idtab *tab;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	for (version = 1; version < 3; version++) {
3937c478bd9Sstevel@tonic-gate 		tab = idtab_lookup(version);
3947c478bd9Sstevel@tonic-gate 		for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
3957c478bd9Sstevel@tonic-gate 			nxt = TAILQ_NEXT(id, next);
3967c478bd9Sstevel@tonic-gate 			if (id->death != 0 && now >= id->death) {
3977c478bd9Sstevel@tonic-gate 				TAILQ_REMOVE(&tab->idlist, id, next);
3987c478bd9Sstevel@tonic-gate 				free_identity(id);
3997c478bd9Sstevel@tonic-gate 				tab->nentries--;
4007c478bd9Sstevel@tonic-gate 			}
4017c478bd9Sstevel@tonic-gate 		}
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate process_add_identity(SocketEntry *e, int version)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	Idtab *tab = idtab_lookup(version);
4097c478bd9Sstevel@tonic-gate 	int type, success = 0, death = 0;
4107c478bd9Sstevel@tonic-gate 	char *type_name, *comment;
4117c478bd9Sstevel@tonic-gate 	Key *k = NULL;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	switch (version) {
4147c478bd9Sstevel@tonic-gate 	case 1:
4157c478bd9Sstevel@tonic-gate 		k = key_new_private(KEY_RSA1);
4167c478bd9Sstevel@tonic-gate 		(void) buffer_get_int(&e->request);		/* ignored */
4177c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->n);
4187c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->e);
4197c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->d);
4207c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->iqmp);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		/* SSH and SSL have p and q swapped */
4237c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->q);	/* p */
4247c478bd9Sstevel@tonic-gate 		buffer_get_bignum(&e->request, k->rsa->p);	/* q */
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		/* Generate additional parameters */
4277c478bd9Sstevel@tonic-gate 		rsa_generate_additional_parameters(k->rsa);
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 	case 2:
4307c478bd9Sstevel@tonic-gate 		type_name = buffer_get_string(&e->request, NULL);
4317c478bd9Sstevel@tonic-gate 		type = key_type_from_name(type_name);
4327c478bd9Sstevel@tonic-gate 		xfree(type_name);
4337c478bd9Sstevel@tonic-gate 		switch (type) {
4347c478bd9Sstevel@tonic-gate 		case KEY_DSA:
4357c478bd9Sstevel@tonic-gate 			k = key_new_private(type);
4367c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->dsa->p);
4377c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->dsa->q);
4387c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->dsa->g);
4397c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->dsa->pub_key);
4407c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->dsa->priv_key);
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 		case KEY_RSA:
4437c478bd9Sstevel@tonic-gate 			k = key_new_private(type);
4447c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->n);
4457c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->e);
4467c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->d);
4477c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->iqmp);
4487c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->p);
4497c478bd9Sstevel@tonic-gate 			buffer_get_bignum2(&e->request, k->rsa->q);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 			/* Generate additional parameters */
4527c478bd9Sstevel@tonic-gate 			rsa_generate_additional_parameters(k->rsa);
4537c478bd9Sstevel@tonic-gate 			break;
4547c478bd9Sstevel@tonic-gate 		default:
4557c478bd9Sstevel@tonic-gate 			buffer_clear(&e->request);
4567c478bd9Sstevel@tonic-gate 			goto send;
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 		break;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	comment = buffer_get_string(&e->request, NULL);
4617c478bd9Sstevel@tonic-gate 	if (k == NULL) {
4627c478bd9Sstevel@tonic-gate 		xfree(comment);
4637c478bd9Sstevel@tonic-gate 		goto send;
4647c478bd9Sstevel@tonic-gate 	}
4657c478bd9Sstevel@tonic-gate 	success = 1;
4667c478bd9Sstevel@tonic-gate 	while (buffer_len(&e->request)) {
4677c478bd9Sstevel@tonic-gate 		switch (buffer_get_char(&e->request)) {
4687c478bd9Sstevel@tonic-gate 		case SSH_AGENT_CONSTRAIN_LIFETIME:
4697c478bd9Sstevel@tonic-gate 			death = time(NULL) + buffer_get_int(&e->request);
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		default:
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 		}
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	if (lookup_identity(k, version) == NULL) {
4767c478bd9Sstevel@tonic-gate 		Identity *id = xmalloc(sizeof(Identity));
4777c478bd9Sstevel@tonic-gate 		id->key = k;
4787c478bd9Sstevel@tonic-gate 		id->comment = comment;
4797c478bd9Sstevel@tonic-gate 		id->death = death;
4807c478bd9Sstevel@tonic-gate 		TAILQ_INSERT_TAIL(&tab->idlist, id, next);
4817c478bd9Sstevel@tonic-gate 		/* Increment the number of identities. */
4827c478bd9Sstevel@tonic-gate 		tab->nentries++;
4837c478bd9Sstevel@tonic-gate 	} else {
4847c478bd9Sstevel@tonic-gate 		key_free(k);
4857c478bd9Sstevel@tonic-gate 		xfree(comment);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate send:
4887c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, 1);
4897c478bd9Sstevel@tonic-gate 	buffer_put_char(&e->output,
4907c478bd9Sstevel@tonic-gate 	    success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /* XXX todo: encrypt sensitive data with passphrase */
4947c478bd9Sstevel@tonic-gate static void
4957c478bd9Sstevel@tonic-gate process_lock_agent(SocketEntry *e, int lock)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	int success = 0;
4987c478bd9Sstevel@tonic-gate 	char *passwd;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	passwd = buffer_get_string(&e->request, NULL);
5017c478bd9Sstevel@tonic-gate 	if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
5027c478bd9Sstevel@tonic-gate 		locked = 0;
5037c478bd9Sstevel@tonic-gate 		memset(lock_passwd, 0, strlen(lock_passwd));
5047c478bd9Sstevel@tonic-gate 		xfree(lock_passwd);
5057c478bd9Sstevel@tonic-gate 		lock_passwd = NULL;
5067c478bd9Sstevel@tonic-gate 		success = 1;
5077c478bd9Sstevel@tonic-gate 	} else if (!locked && lock) {
5087c478bd9Sstevel@tonic-gate 		locked = 1;
5097c478bd9Sstevel@tonic-gate 		lock_passwd = xstrdup(passwd);
5107c478bd9Sstevel@tonic-gate 		success = 1;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 	memset(passwd, 0, strlen(passwd));
5137c478bd9Sstevel@tonic-gate 	xfree(passwd);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, 1);
5167c478bd9Sstevel@tonic-gate 	buffer_put_char(&e->output,
5177c478bd9Sstevel@tonic-gate 	    success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static void
5217c478bd9Sstevel@tonic-gate no_identities(SocketEntry *e, u_int type)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 	Buffer msg;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	buffer_init(&msg);
5267c478bd9Sstevel@tonic-gate 	buffer_put_char(&msg,
5277c478bd9Sstevel@tonic-gate 	    (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
5287c478bd9Sstevel@tonic-gate 	    SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
5297c478bd9Sstevel@tonic-gate 	buffer_put_int(&msg, 0);
5307c478bd9Sstevel@tonic-gate 	buffer_put_int(&e->output, buffer_len(&msg));
5317c478bd9Sstevel@tonic-gate 	buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
5327c478bd9Sstevel@tonic-gate 	buffer_free(&msg);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /* dispatch incoming messages */
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate static void
5387c478bd9Sstevel@tonic-gate process_message(SocketEntry *e)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	u_int msg_len, type;
5417c478bd9Sstevel@tonic-gate 	u_char *cp;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/* kill dead keys */
5447c478bd9Sstevel@tonic-gate 	reaper();
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (buffer_len(&e->input) < 5)
5477c478bd9Sstevel@tonic-gate 		return;		/* Incomplete message. */
5487c478bd9Sstevel@tonic-gate 	cp = buffer_ptr(&e->input);
5497c478bd9Sstevel@tonic-gate 	msg_len = GET_32BIT(cp);
5507c478bd9Sstevel@tonic-gate 	if (msg_len > 256 * 1024) {
5517c478bd9Sstevel@tonic-gate 		close_socket(e);
5527c478bd9Sstevel@tonic-gate 		return;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 	if (buffer_len(&e->input) < msg_len + 4)
5557c478bd9Sstevel@tonic-gate 		return;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	/* move the current input to e->request */
5587c478bd9Sstevel@tonic-gate 	buffer_consume(&e->input, 4);
5597c478bd9Sstevel@tonic-gate 	buffer_clear(&e->request);
5607c478bd9Sstevel@tonic-gate 	buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
5617c478bd9Sstevel@tonic-gate 	buffer_consume(&e->input, msg_len);
5627c478bd9Sstevel@tonic-gate 	type = buffer_get_char(&e->request);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	/* check wheter agent is locked */
5657c478bd9Sstevel@tonic-gate 	if (locked && type != SSH_AGENTC_UNLOCK) {
5667c478bd9Sstevel@tonic-gate 		buffer_clear(&e->request);
5677c478bd9Sstevel@tonic-gate 		switch (type) {
5687c478bd9Sstevel@tonic-gate 		case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
5697c478bd9Sstevel@tonic-gate 		case SSH2_AGENTC_REQUEST_IDENTITIES:
5707c478bd9Sstevel@tonic-gate 			/* send empty lists */
5717c478bd9Sstevel@tonic-gate 			no_identities(e, type);
5727c478bd9Sstevel@tonic-gate 			break;
5737c478bd9Sstevel@tonic-gate 		default:
5747c478bd9Sstevel@tonic-gate 			/* send a fail message for all other request types */
5757c478bd9Sstevel@tonic-gate 			buffer_put_int(&e->output, 1);
5767c478bd9Sstevel@tonic-gate 			buffer_put_char(&e->output, SSH_AGENT_FAILURE);
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 		return;
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	debug("type %d", type);
5827c478bd9Sstevel@tonic-gate 	switch (type) {
5837c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_LOCK:
5847c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_UNLOCK:
5857c478bd9Sstevel@tonic-gate 		process_lock_agent(e, type == SSH_AGENTC_LOCK);
5867c478bd9Sstevel@tonic-gate 		break;
5877c478bd9Sstevel@tonic-gate 	/* ssh1 */
5887c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_RSA_CHALLENGE:
5897c478bd9Sstevel@tonic-gate 		process_authentication_challenge1(e);
5907c478bd9Sstevel@tonic-gate 		break;
5917c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
5927c478bd9Sstevel@tonic-gate 		process_request_identities(e, 1);
5937c478bd9Sstevel@tonic-gate 		break;
5947c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_ADD_RSA_IDENTITY:
5957c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
5967c478bd9Sstevel@tonic-gate 		process_add_identity(e, 1);
5977c478bd9Sstevel@tonic-gate 		break;
5987c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_REMOVE_RSA_IDENTITY:
5997c478bd9Sstevel@tonic-gate 		process_remove_identity(e, 1);
6007c478bd9Sstevel@tonic-gate 		break;
6017c478bd9Sstevel@tonic-gate 	case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
6027c478bd9Sstevel@tonic-gate 		process_remove_all_identities(e, 1);
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	/* ssh2 */
6057c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_SIGN_REQUEST:
6067c478bd9Sstevel@tonic-gate 		process_sign_request2(e);
6077c478bd9Sstevel@tonic-gate 		break;
6087c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_REQUEST_IDENTITIES:
6097c478bd9Sstevel@tonic-gate 		process_request_identities(e, 2);
6107c478bd9Sstevel@tonic-gate 		break;
6117c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_ADD_IDENTITY:
6127c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_ADD_ID_CONSTRAINED:
6137c478bd9Sstevel@tonic-gate 		process_add_identity(e, 2);
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_REMOVE_IDENTITY:
6167c478bd9Sstevel@tonic-gate 		process_remove_identity(e, 2);
6177c478bd9Sstevel@tonic-gate 		break;
6187c478bd9Sstevel@tonic-gate 	case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
6197c478bd9Sstevel@tonic-gate 		process_remove_all_identities(e, 2);
6207c478bd9Sstevel@tonic-gate 		break;
6217c478bd9Sstevel@tonic-gate 	default:
6227c478bd9Sstevel@tonic-gate 		/* Unknown message.  Respond with failure. */
6237c478bd9Sstevel@tonic-gate 		error("Unknown message %d", type);
6247c478bd9Sstevel@tonic-gate 		buffer_clear(&e->request);
6257c478bd9Sstevel@tonic-gate 		buffer_put_int(&e->output, 1);
6267c478bd9Sstevel@tonic-gate 		buffer_put_char(&e->output, SSH_AGENT_FAILURE);
6277c478bd9Sstevel@tonic-gate 		break;
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate static void
6327c478bd9Sstevel@tonic-gate new_socket(sock_type type, int fd)
6337c478bd9Sstevel@tonic-gate {
6347c478bd9Sstevel@tonic-gate 	u_int i, old_alloc;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
6377c478bd9Sstevel@tonic-gate 		error("fcntl O_NONBLOCK: %s", strerror(errno));
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if (fd > max_fd)
6407c478bd9Sstevel@tonic-gate 		max_fd = fd;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	for (i = 0; i < sockets_alloc; i++)
6437c478bd9Sstevel@tonic-gate 		if (sockets[i].type == AUTH_UNUSED) {
6447c478bd9Sstevel@tonic-gate 			sockets[i].fd = fd;
6457c478bd9Sstevel@tonic-gate 			sockets[i].type = type;
6467c478bd9Sstevel@tonic-gate 			buffer_init(&sockets[i].input);
6477c478bd9Sstevel@tonic-gate 			buffer_init(&sockets[i].output);
6487c478bd9Sstevel@tonic-gate 			buffer_init(&sockets[i].request);
6497c478bd9Sstevel@tonic-gate 			return;
6507c478bd9Sstevel@tonic-gate 		}
6517c478bd9Sstevel@tonic-gate 	old_alloc = sockets_alloc;
6527c478bd9Sstevel@tonic-gate 	sockets_alloc += 10;
6537c478bd9Sstevel@tonic-gate 	if (sockets)
6547c478bd9Sstevel@tonic-gate 		sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
6557c478bd9Sstevel@tonic-gate 	else
6567c478bd9Sstevel@tonic-gate 		sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
6577c478bd9Sstevel@tonic-gate 	for (i = old_alloc; i < sockets_alloc; i++)
6587c478bd9Sstevel@tonic-gate 		sockets[i].type = AUTH_UNUSED;
6597c478bd9Sstevel@tonic-gate 	sockets[old_alloc].type = type;
6607c478bd9Sstevel@tonic-gate 	sockets[old_alloc].fd = fd;
6617c478bd9Sstevel@tonic-gate 	buffer_init(&sockets[old_alloc].input);
6627c478bd9Sstevel@tonic-gate 	buffer_init(&sockets[old_alloc].output);
6637c478bd9Sstevel@tonic-gate 	buffer_init(&sockets[old_alloc].request);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static int
6677c478bd9Sstevel@tonic-gate prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	u_int i, sz;
6707c478bd9Sstevel@tonic-gate 	int n = 0;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	for (i = 0; i < sockets_alloc; i++) {
6737c478bd9Sstevel@tonic-gate 		switch (sockets[i].type) {
6747c478bd9Sstevel@tonic-gate 		case AUTH_SOCKET:
6757c478bd9Sstevel@tonic-gate 		case AUTH_CONNECTION:
6767c478bd9Sstevel@tonic-gate 			n = MAX(n, sockets[i].fd);
6777c478bd9Sstevel@tonic-gate 			break;
6787c478bd9Sstevel@tonic-gate 		case AUTH_UNUSED:
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		default:
6817c478bd9Sstevel@tonic-gate 			fatal("Unknown socket type %d", sockets[i].type);
6827c478bd9Sstevel@tonic-gate 			break;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 	}
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
6877c478bd9Sstevel@tonic-gate 	if (*fdrp == NULL || sz > *nallocp) {
6887c478bd9Sstevel@tonic-gate 		if (*fdrp)
6897c478bd9Sstevel@tonic-gate 			xfree(*fdrp);
6907c478bd9Sstevel@tonic-gate 		if (*fdwp)
6917c478bd9Sstevel@tonic-gate 			xfree(*fdwp);
6927c478bd9Sstevel@tonic-gate 		*fdrp = xmalloc(sz);
6937c478bd9Sstevel@tonic-gate 		*fdwp = xmalloc(sz);
6947c478bd9Sstevel@tonic-gate 		*nallocp = sz;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	if (n < *fdl)
6977c478bd9Sstevel@tonic-gate 		debug("XXX shrink: %d < %d", n, *fdl);
6987c478bd9Sstevel@tonic-gate 	*fdl = n;
6997c478bd9Sstevel@tonic-gate 	memset(*fdrp, 0, sz);
7007c478bd9Sstevel@tonic-gate 	memset(*fdwp, 0, sz);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	for (i = 0; i < sockets_alloc; i++) {
7037c478bd9Sstevel@tonic-gate 		switch (sockets[i].type) {
7047c478bd9Sstevel@tonic-gate 		case AUTH_SOCKET:
7057c478bd9Sstevel@tonic-gate 		case AUTH_CONNECTION:
7067c478bd9Sstevel@tonic-gate 			FD_SET(sockets[i].fd, *fdrp);
7077c478bd9Sstevel@tonic-gate 			if (buffer_len(&sockets[i].output) > 0)
7087c478bd9Sstevel@tonic-gate 				FD_SET(sockets[i].fd, *fdwp);
7097c478bd9Sstevel@tonic-gate 			break;
7107c478bd9Sstevel@tonic-gate 		default:
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	return (1);
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate static void
7187c478bd9Sstevel@tonic-gate after_select(fd_set *readset, fd_set *writeset)
7197c478bd9Sstevel@tonic-gate {
7207c478bd9Sstevel@tonic-gate 	struct sockaddr_un sunaddr;
7217c478bd9Sstevel@tonic-gate 	socklen_t slen;
7227c478bd9Sstevel@tonic-gate 	char buf[1024];
7237c478bd9Sstevel@tonic-gate 	int len, sock;
7247c478bd9Sstevel@tonic-gate 	u_int i;
7257c478bd9Sstevel@tonic-gate 	uid_t euid;
7267c478bd9Sstevel@tonic-gate 	gid_t egid;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	for (i = 0; i < sockets_alloc; i++)
7297c478bd9Sstevel@tonic-gate 		switch (sockets[i].type) {
7307c478bd9Sstevel@tonic-gate 		case AUTH_UNUSED:
7317c478bd9Sstevel@tonic-gate 			break;
7327c478bd9Sstevel@tonic-gate 		case AUTH_SOCKET:
7337c478bd9Sstevel@tonic-gate 			if (FD_ISSET(sockets[i].fd, readset)) {
7347c478bd9Sstevel@tonic-gate 				slen = sizeof(sunaddr);
7357c478bd9Sstevel@tonic-gate 				sock = accept(sockets[i].fd,
7367c478bd9Sstevel@tonic-gate 				    (struct sockaddr *) &sunaddr, &slen);
7377c478bd9Sstevel@tonic-gate 				if (sock < 0) {
7387c478bd9Sstevel@tonic-gate 					error("accept from AUTH_SOCKET: %s",
7397c478bd9Sstevel@tonic-gate 					    strerror(errno));
7407c478bd9Sstevel@tonic-gate 					break;
7417c478bd9Sstevel@tonic-gate 				}
7427c478bd9Sstevel@tonic-gate 				if (getpeereid(sock, &euid, &egid) < 0) {
7437c478bd9Sstevel@tonic-gate 					error("getpeereid %d failed: %s",
7447c478bd9Sstevel@tonic-gate 					    sock, strerror(errno));
7457c478bd9Sstevel@tonic-gate 					close(sock);
7467c478bd9Sstevel@tonic-gate 					break;
7477c478bd9Sstevel@tonic-gate 				}
7487c478bd9Sstevel@tonic-gate 				if ((euid != 0) && (getuid() != euid)) {
7497c478bd9Sstevel@tonic-gate 					error("uid mismatch: "
7507c478bd9Sstevel@tonic-gate 					    "peer euid %u != uid %u",
7517c478bd9Sstevel@tonic-gate 					    (u_int) euid, (u_int) getuid());
7527c478bd9Sstevel@tonic-gate 					close(sock);
7537c478bd9Sstevel@tonic-gate 					break;
7547c478bd9Sstevel@tonic-gate 				}
7557c478bd9Sstevel@tonic-gate 				new_socket(AUTH_CONNECTION, sock);
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 			break;
7587c478bd9Sstevel@tonic-gate 		case AUTH_CONNECTION:
7597c478bd9Sstevel@tonic-gate 			if (buffer_len(&sockets[i].output) > 0 &&
7607c478bd9Sstevel@tonic-gate 			    FD_ISSET(sockets[i].fd, writeset)) {
7617c478bd9Sstevel@tonic-gate 				do {
7627c478bd9Sstevel@tonic-gate 					len = write(sockets[i].fd,
7637c478bd9Sstevel@tonic-gate 					    buffer_ptr(&sockets[i].output),
7647c478bd9Sstevel@tonic-gate 					    buffer_len(&sockets[i].output));
7657c478bd9Sstevel@tonic-gate 					if (len == -1 && (errno == EAGAIN ||
7667c478bd9Sstevel@tonic-gate 					    errno == EINTR))
7677c478bd9Sstevel@tonic-gate 						continue;
7687c478bd9Sstevel@tonic-gate 					break;
7697c478bd9Sstevel@tonic-gate 				} while (1);
7707c478bd9Sstevel@tonic-gate 				if (len <= 0) {
7717c478bd9Sstevel@tonic-gate 					close_socket(&sockets[i]);
7727c478bd9Sstevel@tonic-gate 					break;
7737c478bd9Sstevel@tonic-gate 				}
7747c478bd9Sstevel@tonic-gate 				buffer_consume(&sockets[i].output, len);
7757c478bd9Sstevel@tonic-gate 			}
7767c478bd9Sstevel@tonic-gate 			if (FD_ISSET(sockets[i].fd, readset)) {
7777c478bd9Sstevel@tonic-gate 				do {
7787c478bd9Sstevel@tonic-gate 					len = read(sockets[i].fd, buf, sizeof(buf));
7797c478bd9Sstevel@tonic-gate 					if (len == -1 && (errno == EAGAIN ||
7807c478bd9Sstevel@tonic-gate 					    errno == EINTR))
7817c478bd9Sstevel@tonic-gate 						continue;
7827c478bd9Sstevel@tonic-gate 					break;
7837c478bd9Sstevel@tonic-gate 				} while (1);
7847c478bd9Sstevel@tonic-gate 				if (len <= 0) {
7857c478bd9Sstevel@tonic-gate 					close_socket(&sockets[i]);
7867c478bd9Sstevel@tonic-gate 					break;
7877c478bd9Sstevel@tonic-gate 				}
7887c478bd9Sstevel@tonic-gate 				buffer_append(&sockets[i].input, buf, len);
7897c478bd9Sstevel@tonic-gate 				process_message(&sockets[i]);
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 			break;
7927c478bd9Sstevel@tonic-gate 		default:
7937c478bd9Sstevel@tonic-gate 			fatal("Unknown type %d", sockets[i].type);
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate static void
7987c478bd9Sstevel@tonic-gate cleanup_socket(void *p)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	if (socket_name[0])
8017c478bd9Sstevel@tonic-gate 		unlink(socket_name);
8027c478bd9Sstevel@tonic-gate 	if (socket_dir[0])
8037c478bd9Sstevel@tonic-gate 		rmdir(socket_dir);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static void
8077c478bd9Sstevel@tonic-gate cleanup_exit(int i)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	cleanup_socket(NULL);
8107c478bd9Sstevel@tonic-gate 	exit(i);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate static void
8147c478bd9Sstevel@tonic-gate cleanup_handler(int sig)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	cleanup_socket(NULL);
8177c478bd9Sstevel@tonic-gate 	_exit(2);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate static void
8217c478bd9Sstevel@tonic-gate check_parent_exists(int sig)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	int save_errno = errno;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	if (parent_pid != -1 && getppid() != parent_pid) {
8267c478bd9Sstevel@tonic-gate 		/* printf("Parent has died - Authentication agent exiting.\n"); */
8277c478bd9Sstevel@tonic-gate 		cleanup_handler(sig); /* safe */
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	signal(SIGALRM, check_parent_exists);
8307c478bd9Sstevel@tonic-gate 	alarm(10);
8317c478bd9Sstevel@tonic-gate 	errno = save_errno;
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate static void
8357c478bd9Sstevel@tonic-gate usage(void)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	fprintf(stderr,
8387c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [options] [command [args ...]]\n"
8397c478bd9Sstevel@tonic-gate 		    "Options:\n"
8407c478bd9Sstevel@tonic-gate 		    "  -c          Generate C-shell commands on stdout.\n"
8417c478bd9Sstevel@tonic-gate 		    "  -s          Generate Bourne shell commands on stdout.\n"
8427c478bd9Sstevel@tonic-gate 		    "  -k          Kill the current agent.\n"
8437c478bd9Sstevel@tonic-gate 		    "  -d          Debug mode.\n"
8447c478bd9Sstevel@tonic-gate 		    "  -a socket   Bind agent socket to given name.\n"),
8457c478bd9Sstevel@tonic-gate 		__progname);
8467c478bd9Sstevel@tonic-gate 	exit(1);
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate int
8507c478bd9Sstevel@tonic-gate main(int ac, char **av)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	int sock, c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0, ch, nalloc;
8537c478bd9Sstevel@tonic-gate 	char *shell, *pidstr, *agentsocket = NULL;
8547c478bd9Sstevel@tonic-gate 	const char *format;
8557c478bd9Sstevel@tonic-gate 	fd_set *readsetp = NULL, *writesetp = NULL;
8567c478bd9Sstevel@tonic-gate 	struct sockaddr_un sunaddr;
8577c478bd9Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT
8587c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
8597c478bd9Sstevel@tonic-gate #endif
8607c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
8617c478bd9Sstevel@tonic-gate 	int prev_mask;
8627c478bd9Sstevel@tonic-gate #endif
8637c478bd9Sstevel@tonic-gate 	extern int optind;
8647c478bd9Sstevel@tonic-gate 	extern char *optarg;
8657c478bd9Sstevel@tonic-gate 	pid_t pid;
8667c478bd9Sstevel@tonic-gate 	char pidstrbuf[1 + 3 * sizeof pid];
8677c478bd9Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE
8687c478bd9Sstevel@tonic-gate 	priv_set_t *myprivs;
8697c478bd9Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	/* drop */
8727c478bd9Sstevel@tonic-gate 	setegid(getgid());
8737c478bd9Sstevel@tonic-gate 	setgid(getgid());
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	SSLeay_add_all_algorithms();
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	__progname = get_progname(av[0]);
8807c478bd9Sstevel@tonic-gate 	init_rng();
8817c478bd9Sstevel@tonic-gate 	seed_rng();
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	while ((ch = getopt(ac, av, "cdksa:")) != -1) {
8847c478bd9Sstevel@tonic-gate 		switch (ch) {
8857c478bd9Sstevel@tonic-gate 		case 'c':
8867c478bd9Sstevel@tonic-gate 			if (s_flag)
8877c478bd9Sstevel@tonic-gate 				usage();
8887c478bd9Sstevel@tonic-gate 			c_flag++;
8897c478bd9Sstevel@tonic-gate 			break;
8907c478bd9Sstevel@tonic-gate 		case 'k':
8917c478bd9Sstevel@tonic-gate 			k_flag++;
8927c478bd9Sstevel@tonic-gate 			break;
8937c478bd9Sstevel@tonic-gate 		case 's':
8947c478bd9Sstevel@tonic-gate 			if (c_flag)
8957c478bd9Sstevel@tonic-gate 				usage();
8967c478bd9Sstevel@tonic-gate 			s_flag++;
8977c478bd9Sstevel@tonic-gate 			break;
8987c478bd9Sstevel@tonic-gate 		case 'd':
8997c478bd9Sstevel@tonic-gate 			if (d_flag)
9007c478bd9Sstevel@tonic-gate 				usage();
9017c478bd9Sstevel@tonic-gate 			d_flag++;
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		case 'a':
9047c478bd9Sstevel@tonic-gate 			agentsocket = optarg;
9057c478bd9Sstevel@tonic-gate 			break;
9067c478bd9Sstevel@tonic-gate 		default:
9077c478bd9Sstevel@tonic-gate 			usage();
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 	ac -= optind;
9117c478bd9Sstevel@tonic-gate 	av += optind;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
9147c478bd9Sstevel@tonic-gate 		usage();
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	if (ac == 0 && !c_flag && !s_flag) {
9177c478bd9Sstevel@tonic-gate 		shell = getenv("SHELL");
9187c478bd9Sstevel@tonic-gate 		if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
9197c478bd9Sstevel@tonic-gate 			c_flag = 1;
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	if (k_flag) {
9227c478bd9Sstevel@tonic-gate 		pidstr = getenv(SSH_AGENTPID_ENV_NAME);
9237c478bd9Sstevel@tonic-gate 		if (pidstr == NULL) {
9247c478bd9Sstevel@tonic-gate 			fprintf(stderr,
9257c478bd9Sstevel@tonic-gate 				gettext("%s not set, cannot kill agent\n"),
9267c478bd9Sstevel@tonic-gate 				SSH_AGENTPID_ENV_NAME);
9277c478bd9Sstevel@tonic-gate 			exit(1);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 		pid = atoi(pidstr);
9307c478bd9Sstevel@tonic-gate 		if (pid < 1) {
9317c478bd9Sstevel@tonic-gate 			fprintf(stderr,
9327c478bd9Sstevel@tonic-gate 			    gettext("%s=\")%s\", which is not a good PID\n"),
9337c478bd9Sstevel@tonic-gate 			    SSH_AGENTPID_ENV_NAME, pidstr);
9347c478bd9Sstevel@tonic-gate 			exit(1);
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 		if (kill(pid, SIGTERM) == -1) {
9377c478bd9Sstevel@tonic-gate 			perror("kill");
9387c478bd9Sstevel@tonic-gate 			exit(1);
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
9417c478bd9Sstevel@tonic-gate 		printf(format, SSH_AUTHSOCKET_ENV_NAME);
9427c478bd9Sstevel@tonic-gate 		printf(format, SSH_AGENTPID_ENV_NAME);
9437c478bd9Sstevel@tonic-gate 		printf("echo ");
9447c478bd9Sstevel@tonic-gate 		printf(gettext("Agent pid %ld killed;\n"), (long)pid);
9457c478bd9Sstevel@tonic-gate 		exit(0);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 	parent_pid = getpid();
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	if (agentsocket == NULL) {
9507c478bd9Sstevel@tonic-gate 		/* Create private directory for agent socket */
9517c478bd9Sstevel@tonic-gate 		strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
9527c478bd9Sstevel@tonic-gate 		if (mkdtemp(socket_dir) == NULL) {
9537c478bd9Sstevel@tonic-gate 			perror("mkdtemp: private socket dir");
9547c478bd9Sstevel@tonic-gate 			exit(1);
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 		snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
9577c478bd9Sstevel@tonic-gate 		    (long)parent_pid);
9587c478bd9Sstevel@tonic-gate 	} else {
9597c478bd9Sstevel@tonic-gate 		/* Try to use specified agent socket */
9607c478bd9Sstevel@tonic-gate 		socket_dir[0] = '\0';
9617c478bd9Sstevel@tonic-gate 		strlcpy(socket_name, agentsocket, sizeof socket_name);
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * Create socket early so it will exist before command gets run from
9667c478bd9Sstevel@tonic-gate 	 * the parent.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	sock = socket(AF_UNIX, SOCK_STREAM, 0);
9697c478bd9Sstevel@tonic-gate 	if (sock < 0) {
9707c478bd9Sstevel@tonic-gate 		perror("socket");
9717c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 	memset(&sunaddr, 0, sizeof(sunaddr));
9747c478bd9Sstevel@tonic-gate 	sunaddr.sun_family = AF_UNIX;
9757c478bd9Sstevel@tonic-gate 	strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
9767c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
9777c478bd9Sstevel@tonic-gate 	prev_mask = umask(0177);
9787c478bd9Sstevel@tonic-gate #endif
9797c478bd9Sstevel@tonic-gate 	if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
9807c478bd9Sstevel@tonic-gate 		perror("bind");
9817c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
9827c478bd9Sstevel@tonic-gate 		umask(prev_mask);
9837c478bd9Sstevel@tonic-gate #endif
9847c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate #ifdef HAVE_CYGWIN
9877c478bd9Sstevel@tonic-gate 	umask(prev_mask);
9887c478bd9Sstevel@tonic-gate #endif
9897c478bd9Sstevel@tonic-gate 	if (listen(sock, 128) < 0) {
9907c478bd9Sstevel@tonic-gate 		perror("listen");
9917c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/*
9957c478bd9Sstevel@tonic-gate 	 * Fork, and have the parent execute the command, if any, or present
9967c478bd9Sstevel@tonic-gate 	 * the socket data.  The child continues as the authentication agent.
9977c478bd9Sstevel@tonic-gate 	 */
9987c478bd9Sstevel@tonic-gate 	if (d_flag) {
9997c478bd9Sstevel@tonic-gate 		log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
10007c478bd9Sstevel@tonic-gate 		format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
10017c478bd9Sstevel@tonic-gate 		printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
10027c478bd9Sstevel@tonic-gate 		    SSH_AUTHSOCKET_ENV_NAME);
10037c478bd9Sstevel@tonic-gate 		printf("echo ");
10047c478bd9Sstevel@tonic-gate 		printf(gettext("Agent pid %ld;\n"), (long)parent_pid);
10057c478bd9Sstevel@tonic-gate 		goto skip;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	pid = fork();
10087c478bd9Sstevel@tonic-gate 	if (pid == -1) {
10097c478bd9Sstevel@tonic-gate 		perror("fork");
10107c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	if (pid != 0) {		/* Parent - execute the given command. */
10137c478bd9Sstevel@tonic-gate 		close(sock);
10147c478bd9Sstevel@tonic-gate 		snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
10157c478bd9Sstevel@tonic-gate 		if (ac == 0) {
10167c478bd9Sstevel@tonic-gate 			format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
10177c478bd9Sstevel@tonic-gate 			printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
10187c478bd9Sstevel@tonic-gate 			    SSH_AUTHSOCKET_ENV_NAME);
10197c478bd9Sstevel@tonic-gate 			printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
10207c478bd9Sstevel@tonic-gate 			    SSH_AGENTPID_ENV_NAME);
10217c478bd9Sstevel@tonic-gate 			printf("echo ");
10227c478bd9Sstevel@tonic-gate 			printf(gettext("Agent pid %ld;\n"), (long)pid);
10237c478bd9Sstevel@tonic-gate 			exit(0);
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 		if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
10267c478bd9Sstevel@tonic-gate 		    setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
10277c478bd9Sstevel@tonic-gate 			perror("setenv");
10287c478bd9Sstevel@tonic-gate 			exit(1);
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 		execvp(av[0], av);
10317c478bd9Sstevel@tonic-gate 		perror(av[0]);
10327c478bd9Sstevel@tonic-gate 		exit(1);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 	/* child */
10357c478bd9Sstevel@tonic-gate 	log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate #ifdef HAVE_SOLARIS_PRIVILEGE
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * Drop unneeded privs, including basic ones like fork/exec.
10407c478bd9Sstevel@tonic-gate 	 *
10417c478bd9Sstevel@tonic-gate 	 * Idiom: remove from 'basic' privs we know we don't want,
10427c478bd9Sstevel@tonic-gate 	 * invert the result and remove the resulting set from P.
10437c478bd9Sstevel@tonic-gate 	 *
10447c478bd9Sstevel@tonic-gate 	 * None of the priv_delset() calls below, nor the setppriv call
10457c478bd9Sstevel@tonic-gate 	 * below can fail, so their return values are not checked.
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	if ((myprivs = priv_str_to_set("basic", ",", NULL)) == NULL)
10487c478bd9Sstevel@tonic-gate 		fatal("priv_str_to_set failed: %m");
10497c478bd9Sstevel@tonic-gate 	(void) priv_delset(myprivs, PRIV_PROC_EXEC);
10507c478bd9Sstevel@tonic-gate 	(void) priv_delset(myprivs, PRIV_PROC_FORK);
10517c478bd9Sstevel@tonic-gate 	(void) priv_delset(myprivs, PRIV_FILE_LINK_ANY);
10527c478bd9Sstevel@tonic-gate 	(void) priv_delset(myprivs, PRIV_PROC_INFO);
10537c478bd9Sstevel@tonic-gate 	(void) priv_delset(myprivs, PRIV_PROC_SESSION);
10547c478bd9Sstevel@tonic-gate 	priv_inverse(myprivs);
10557c478bd9Sstevel@tonic-gate 	(void) setppriv(PRIV_OFF, PRIV_PERMITTED, myprivs);
10567c478bd9Sstevel@tonic-gate 	(void) priv_freeset(myprivs);
10577c478bd9Sstevel@tonic-gate #endif /* HAVE_SOLARIS_PRIVILEGE */
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (setsid() == -1) {
10607c478bd9Sstevel@tonic-gate 		error("setsid: %s", strerror(errno));
10617c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
10627c478bd9Sstevel@tonic-gate 	}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	(void)chdir("/");
10657c478bd9Sstevel@tonic-gate 	close(0);
10667c478bd9Sstevel@tonic-gate 	close(1);
10677c478bd9Sstevel@tonic-gate 	close(2);
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate #ifdef HAVE_SETRLIMIT
10707c478bd9Sstevel@tonic-gate 	/* deny core dumps, since memory contains unencrypted private keys */
10717c478bd9Sstevel@tonic-gate 	rlim.rlim_cur = rlim.rlim_max = 0;
10727c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
10737c478bd9Sstevel@tonic-gate 		error("setrlimit RLIMIT_CORE: %s", strerror(errno));
10747c478bd9Sstevel@tonic-gate 		cleanup_exit(1);
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate #endif
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate skip:
10797c478bd9Sstevel@tonic-gate 	fatal_add_cleanup(cleanup_socket, NULL);
10807c478bd9Sstevel@tonic-gate 	new_socket(AUTH_SOCKET, sock);
10817c478bd9Sstevel@tonic-gate 	if (ac > 0) {
10827c478bd9Sstevel@tonic-gate 		signal(SIGALRM, check_parent_exists);
10837c478bd9Sstevel@tonic-gate 		alarm(10);
10847c478bd9Sstevel@tonic-gate 	}
10857c478bd9Sstevel@tonic-gate 	idtab_init();
10867c478bd9Sstevel@tonic-gate 	if (!d_flag)
10877c478bd9Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
10887c478bd9Sstevel@tonic-gate 	signal(SIGPIPE, SIG_IGN);
10897c478bd9Sstevel@tonic-gate 	signal(SIGHUP, cleanup_handler);
10907c478bd9Sstevel@tonic-gate 	signal(SIGTERM, cleanup_handler);
10917c478bd9Sstevel@tonic-gate 	nalloc = 0;
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	while (1) {
10947c478bd9Sstevel@tonic-gate 		prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
10957c478bd9Sstevel@tonic-gate 		if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
10967c478bd9Sstevel@tonic-gate 			if (errno == EINTR)
10977c478bd9Sstevel@tonic-gate 				continue;
10987c478bd9Sstevel@tonic-gate 			fatal("select: %s", strerror(errno));
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 		after_select(readsetp, writesetp);
11017c478bd9Sstevel@tonic-gate 	}
11027c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
11037c478bd9Sstevel@tonic-gate 	return (0);	/* keep lint happy */
11047c478bd9Sstevel@tonic-gate }
1105