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