blob: bc96ad705e09849864072322cf2c81bc83f2753c [file] [log] [blame]
Damien Miller1f0311c2014-05-15 14:24:09 +10001/* $OpenBSD: ssh-agent.c,v 1.185 2014/04/29 18:01:49 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Miller1f0311c2014-05-15 14:24:09 +100052#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100053#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100055#endif
Damien Millere3476ed2006-07-24 14:13:33 +100056
Darren Tucker39972492006-07-12 22:22:46 +100057#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100058#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110059#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110060# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110061#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100063#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100066#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100067#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110069
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
72#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100074#include "key.h"
75#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110076#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110078#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110079#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller7ea845e2010-02-12 09:21:02 +110081#ifdef ENABLE_PKCS11
82#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000083#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000084
Damien Miller6c4914a2004-03-03 11:08:59 +110085#if defined(HAVE_SYS_PRCTL_H)
86#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
87#endif
88
Ben Lindstrom65366a82001-12-06 16:32:47 +000089typedef enum {
90 AUTH_UNUSED,
91 AUTH_SOCKET,
92 AUTH_CONNECTION
93} sock_type;
94
Damien Miller95def091999-11-25 00:26:21 +110095typedef struct {
96 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000097 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110098 Buffer input;
99 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000100 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101} SocketEntry;
102
Ben Lindstrom46c16222000-12-22 01:43:59 +0000103u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104SocketEntry *sockets = NULL;
105
Damien Miller1a534ae2002-01-22 23:26:13 +1100106typedef struct identity {
107 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000108 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100109 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100110 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000111 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100112 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113} Identity;
114
Damien Millerad833b32000-08-23 10:46:23 +1000115typedef struct {
116 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100117 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000118} Idtab;
119
120/* private key table, one per protocol version */
121Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123int max_fd = 0;
124
125/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000126pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000127time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100130char socket_name[MAXPATHLEN];
131char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Ben Lindstrom2f717042002-06-06 21:52:03 +0000133/* locking */
134int locked = 0;
135char *lock_passwd = NULL;
136
Damien Miller95def091999-11-25 00:26:21 +1100137extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100138
Darren Tucker55119252013-06-02 07:43:59 +1000139/* Default lifetime in seconds (0 == forever) */
140static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100141
Ben Lindstrombba81212001-06-25 05:01:22 +0000142static void
Damien Miller58f34862002-09-04 16:31:21 +1000143close_socket(SocketEntry *e)
144{
Damien Miller58f34862002-09-04 16:31:21 +1000145 close(e->fd);
146 e->fd = -1;
147 e->type = AUTH_UNUSED;
148 buffer_free(&e->input);
149 buffer_free(&e->output);
150 buffer_free(&e->request);
151}
152
153static void
Damien Millerad833b32000-08-23 10:46:23 +1000154idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155{
Damien Millerad833b32000-08-23 10:46:23 +1000156 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000157
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000158 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100159 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000160 idtable[i].nentries = 0;
161 }
162}
163
164/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000166idtab_lookup(int version)
167{
168 if (version < 1 || version > 2)
169 fatal("internal error, bad protocol version %d", version);
170 return &idtable[version];
171}
172
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000173static void
174free_identity(Identity *id)
175{
176 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000177 free(id->provider);
178 free(id->comment);
179 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000180}
181
Damien Millerad833b32000-08-23 10:46:23 +1000182/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100183static Identity *
184lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000185{
Damien Miller1a534ae2002-01-22 23:26:13 +1100186 Identity *id;
187
Damien Millerad833b32000-08-23 10:46:23 +1000188 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100189 TAILQ_FOREACH(id, &tab->idlist, next) {
190 if (key_equal(key, id->key))
191 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000192 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100193 return (NULL);
194}
195
Damien Miller6c711792003-01-24 11:36:23 +1100196/* Check confirmation of keysign request */
197static int
198confirm_key(Identity *id)
199{
Darren Tuckerce327b62004-11-05 20:38:03 +1100200 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100201 int ret = -1;
202
203 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100204 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
205 id->comment, p))
206 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000207 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100208
Damien Miller6c711792003-01-24 11:36:23 +1100209 return (ret);
210}
211
Damien Millerad833b32000-08-23 10:46:23 +1000212/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000213static void
Damien Millerad833b32000-08-23 10:46:23 +1000214process_request_identities(SocketEntry *e, int version)
215{
216 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100217 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000218 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller95def091999-11-25 00:26:21 +1100220 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000221 buffer_put_char(&msg, (version == 1) ?
222 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
223 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100224 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100225 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000226#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000227 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
228 buffer_put_bignum(&msg, id->key->rsa->e);
229 buffer_put_bignum(&msg, id->key->rsa->n);
Damien Miller1f0311c2014-05-15 14:24:09 +1000230#endif
Damien Millerad833b32000-08-23 10:46:23 +1000231 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000232 u_char *blob;
233 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100234 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000235 buffer_put_string(&msg, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000236 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000237 }
238 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100239 }
240 buffer_put_int(&e->output, buffer_len(&msg));
241 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
242 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}
244
Damien Miller1f0311c2014-05-15 14:24:09 +1000245#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000246/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000247static void
Damien Millerad833b32000-08-23 10:46:23 +1000248process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000250 u_char buf[32], mdbuf[16], session_id[16];
251 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000252 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000253 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000254 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100255 Buffer msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100256 struct ssh_digest_ctx *md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000257 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100260 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100261 if ((challenge = BN_new()) == NULL)
262 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000263
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000264 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000265 buffer_get_bignum(&e->request, key->rsa->e);
266 buffer_get_bignum(&e->request, key->rsa->n);
267 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Millerad833b32000-08-23 10:46:23 +1000269 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000270 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000271 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000272 buffer_get(&e->request, session_id, 16);
273 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000274 if (response_type != 1)
275 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller1a534ae2002-01-22 23:26:13 +1100277 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100278 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100279 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000280 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100281 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
282 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100283
Damien Millerad833b32000-08-23 10:46:23 +1000284 /* The response is MD5 of decrypted challenge plus session id. */
285 len = BN_num_bytes(challenge);
286 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000287 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000288 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100289 }
Damien Millerad833b32000-08-23 10:46:23 +1000290 memset(buf, 0, 32);
291 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100292 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
293 ssh_digest_update(md, buf, 32) < 0 ||
294 ssh_digest_update(md, session_id, 16) < 0 ||
295 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
296 fatal("%s: md5 failed", __func__);
297 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000298
299 /* Send the response. */
300 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
301 for (i = 0; i < 16; i++)
302 buffer_put_char(&msg, mdbuf[i]);
303 goto send;
304 }
305
306failure:
307 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100308 buffer_put_char(&msg, SSH_AGENT_FAILURE);
309send:
310 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000311 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
312 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100313 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000314 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315}
Damien Miller1f0311c2014-05-15 14:24:09 +1000316#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Millerad833b32000-08-23 10:46:23 +1000318/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000319static void
Damien Millerad833b32000-08-23 10:46:23 +1000320process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000322 u_char *blob, *data, *signature = NULL;
323 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000324 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000325 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000326 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000327 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000328 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000329
330 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000331
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000332 blob = buffer_get_string(&e->request, &blen);
333 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100334
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000335 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000336 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100337 if (flags & SSH_AGENT_OLD_SIGNATURE)
338 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000339
Damien Miller0bc1bd82000-11-13 22:57:25 +1100340 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000341 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100342 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100343 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100344 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000345 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000346 }
Damien Millerad833b32000-08-23 10:46:23 +1000347 buffer_init(&msg);
348 if (ok == 0) {
349 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
350 buffer_put_string(&msg, signature, slen);
351 } else {
352 buffer_put_char(&msg, SSH_AGENT_FAILURE);
353 }
354 buffer_put_int(&e->output, buffer_len(&msg));
355 buffer_append(&e->output, buffer_ptr(&msg),
356 buffer_len(&msg));
357 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000358 free(data);
359 free(blob);
360 free(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000361 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000362}
363
364/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Millerad833b32000-08-23 10:46:23 +1000366process_remove_identity(SocketEntry *e, int version)
367{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000368 u_int blen, bits;
369 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100370 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100372
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000373 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000374 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100375 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000376 bits = buffer_get_int(&e->request);
377 buffer_get_bignum(&e->request, key->rsa->e);
378 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379
Damien Millerad833b32000-08-23 10:46:23 +1000380 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000381 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000382 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000383 break;
384 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000385 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 key = key_from_blob(blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000387 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000388 break;
389 }
390 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100391 Identity *id = lookup_identity(key, version);
392 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100393 /*
394 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100395 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000396 * the array, we actually free the key there and move
397 * all the entries between the empty slot and the end
398 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100399 */
Damien Millerad833b32000-08-23 10:46:23 +1000400 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 if (tab->nentries < 1)
402 fatal("process_remove_identity: "
403 "internal error: tab->nentries %d",
404 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100405 TAILQ_REMOVE(&tab->idlist, id, next);
406 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000407 tab->nentries--;
408 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100409 }
Damien Millerad833b32000-08-23 10:46:23 +1000410 key_free(key);
411 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000413 buffer_put_char(&e->output,
414 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415}
416
Ben Lindstrombba81212001-06-25 05:01:22 +0000417static void
Damien Millerad833b32000-08-23 10:46:23 +1000418process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419{
Damien Millerad833b32000-08-23 10:46:23 +1000420 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100421 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100424 for (id = TAILQ_FIRST(&tab->idlist); id;
425 id = TAILQ_FIRST(&tab->idlist)) {
426 TAILQ_REMOVE(&tab->idlist, id, next);
427 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100428 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
Damien Miller95def091999-11-25 00:26:21 +1100430 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000431 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
433 /* Send success. */
434 buffer_put_int(&e->output, 1);
435 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100436}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Darren Tucker2812dc92007-03-21 20:45:06 +1100438/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000439static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000440reaper(void)
441{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000442 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000443 Identity *id, *nxt;
444 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000445 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000446
447 for (version = 1; version < 3; version++) {
448 tab = idtab_lookup(version);
449 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
450 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100451 if (id->death == 0)
452 continue;
453 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100454 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000455 TAILQ_REMOVE(&tab->idlist, id, next);
456 free_identity(id);
457 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100458 } else
459 deadline = (deadline == 0) ? id->death :
460 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000461 }
462 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100463 if (deadline == 0 || deadline <= now)
464 return 0;
465 else
466 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000467}
468
469static void
Damien Millerad833b32000-08-23 10:46:23 +1000470process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100471{
Damien Millerad833b32000-08-23 10:46:23 +1000472 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000473 Identity *id;
Darren Tucker55119252013-06-02 07:43:59 +1000474 int type, success = 0, confirm = 0;
Damien Millerf0e90602013-12-07 10:40:26 +1100475 char *comment;
Darren Tucker55119252013-06-02 07:43:59 +1000476 time_t death = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000477 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100478
Damien Millerad833b32000-08-23 10:46:23 +1000479 switch (version) {
480 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000482 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000483 buffer_get_bignum(&e->request, k->rsa->n);
484 buffer_get_bignum(&e->request, k->rsa->e);
485 buffer_get_bignum(&e->request, k->rsa->d);
486 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100487
Damien Millerad833b32000-08-23 10:46:23 +1000488 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000489 buffer_get_bignum(&e->request, k->rsa->q); /* p */
490 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100491
Damien Millerad833b32000-08-23 10:46:23 +1000492 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000493 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494
Damien Miller5be9d9e2013-12-07 11:24:01 +1100495 /* enable blinding */
Damien Millerc51d0732003-03-15 11:37:09 +1100496 if (RSA_blinding_on(k->rsa, NULL) != 1) {
497 error("process_add_identity: RSA_blinding_on failed");
498 key_free(k);
499 goto send;
500 }
501 break;
Damien Millerf0e90602013-12-07 10:40:26 +1100502 case 2:
503 k = key_private_deserialize(&e->request);
504 if (k == NULL) {
505 buffer_clear(&e->request);
506 goto send;
507 }
508 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100509 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000510 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000511 if (k == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000512 free(comment);
Damien Millerad833b32000-08-23 10:46:23 +1000513 goto send;
514 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000515 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000516 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000517 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000518 death = monotime() + buffer_get_int(&e->request);
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000519 break;
Damien Miller6c711792003-01-24 11:36:23 +1100520 case SSH_AGENT_CONSTRAIN_CONFIRM:
521 confirm = 1;
522 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000523 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000524 error("process_add_identity: "
525 "Unknown constraint type %d", type);
Darren Tuckera627d422013-06-02 07:31:17 +1000526 free(comment);
Damien Miller1cfadab2008-06-30 00:05:21 +1000527 key_free(k);
528 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000529 }
530 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000531 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100532 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000533 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000534 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100535 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000536 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000537 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
538 /* Increment the number of identities. */
539 tab->nentries++;
540 } else {
541 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000542 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000543 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000544 id->comment = comment;
545 id->death = death;
546 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000547send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000548 buffer_put_int(&e->output, 1);
549 buffer_put_char(&e->output,
550 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
551}
552
Ben Lindstrom2f717042002-06-06 21:52:03 +0000553/* XXX todo: encrypt sensitive data with passphrase */
554static void
555process_lock_agent(SocketEntry *e, int lock)
556{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000557 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000558 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000559
560 passwd = buffer_get_string(&e->request, NULL);
561 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
562 locked = 0;
Damien Millera5103f42014-02-04 11:20:14 +1100563 explicit_bzero(lock_passwd, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000564 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000565 lock_passwd = NULL;
566 success = 1;
567 } else if (!locked && lock) {
568 locked = 1;
569 lock_passwd = xstrdup(passwd);
570 success = 1;
571 }
Damien Millera5103f42014-02-04 11:20:14 +1100572 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000573 free(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000574
Ben Lindstrom2f717042002-06-06 21:52:03 +0000575 buffer_put_int(&e->output, 1);
576 buffer_put_char(&e->output,
577 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000578}
579
580static void
581no_identities(SocketEntry *e, u_int type)
582{
583 Buffer msg;
584
585 buffer_init(&msg);
586 buffer_put_char(&msg,
587 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
588 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
589 buffer_put_int(&msg, 0);
590 buffer_put_int(&e->output, buffer_len(&msg));
591 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
592 buffer_free(&msg);
593}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000594
Damien Miller7ea845e2010-02-12 09:21:02 +1100595#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000596static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000597process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000598{
Damien Miller7ea845e2010-02-12 09:21:02 +1100599 char *provider = NULL, *pin;
Darren Tucker55119252013-06-02 07:43:59 +1000600 int i, type, version, count = 0, success = 0, confirm = 0;
601 time_t death = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100602 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000603 Identity *id;
604 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100605
Damien Miller7ea845e2010-02-12 09:21:02 +1100606 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000607 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000608
609 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000610 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000611 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000612 death = monotime() + buffer_get_int(&e->request);
Damien Millerd94f20d2003-06-11 22:06:33 +1000613 break;
614 case SSH_AGENT_CONSTRAIN_CONFIRM:
615 confirm = 1;
616 break;
617 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000618 error("process_add_smartcard_key: "
619 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000620 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000621 }
622 }
623 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000624 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000625
Damien Miller7ea845e2010-02-12 09:21:02 +1100626 count = pkcs11_add_provider(provider, pin, &keys);
627 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000628 k = keys[i];
629 version = k->type == KEY_RSA1 ? 1 : 2;
630 tab = idtab_lookup(version);
631 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100632 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000633 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100634 id->provider = xstrdup(provider);
635 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000636 id->death = death;
637 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000638 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
639 tab->nentries++;
640 success = 1;
641 } else {
642 key_free(k);
643 }
644 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000645 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000646send:
Darren Tuckera627d422013-06-02 07:31:17 +1000647 free(pin);
648 free(provider);
649 free(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000650 buffer_put_int(&e->output, 1);
651 buffer_put_char(&e->output,
652 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
653}
654
655static void
656process_remove_smartcard_key(SocketEntry *e)
657{
Damien Miller7ea845e2010-02-12 09:21:02 +1100658 char *provider = NULL, *pin = NULL;
659 int version, success = 0;
660 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000661 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000662
Damien Miller7ea845e2010-02-12 09:21:02 +1100663 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000664 pin = buffer_get_string(&e->request, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000665 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000666
Damien Miller7ea845e2010-02-12 09:21:02 +1100667 for (version = 1; version < 3; version++) {
668 tab = idtab_lookup(version);
669 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
670 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100671 /* Skip file--based keys */
672 if (id->provider == NULL)
673 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100674 if (!strcmp(provider, id->provider)) {
675 TAILQ_REMOVE(&tab->idlist, id, next);
676 free_identity(id);
677 tab->nentries--;
678 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000679 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000680 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100681 if (pkcs11_del_provider(provider) == 0)
682 success = 1;
683 else
684 error("process_remove_smartcard_key:"
685 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000686 free(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000687 buffer_put_int(&e->output, 1);
688 buffer_put_char(&e->output,
689 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
690}
Damien Miller7ea845e2010-02-12 09:21:02 +1100691#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000692
Damien Millerad833b32000-08-23 10:46:23 +1000693/* dispatch incoming messages */
694
Ben Lindstrombba81212001-06-25 05:01:22 +0000695static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696process_message(SocketEntry *e)
697{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000698 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000699 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000700
Damien Miller95def091999-11-25 00:26:21 +1100701 if (buffer_len(&e->input) < 5)
702 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100703 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100704 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100705 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000706 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100707 return;
708 }
709 if (buffer_len(&e->input) < msg_len + 4)
710 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000711
712 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100713 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000714 buffer_clear(&e->request);
715 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
716 buffer_consume(&e->input, msg_len);
717 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Ben Lindstrom2f717042002-06-06 21:52:03 +0000719 /* check wheter agent is locked */
720 if (locked && type != SSH_AGENTC_UNLOCK) {
721 buffer_clear(&e->request);
722 switch (type) {
723 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
724 case SSH2_AGENTC_REQUEST_IDENTITIES:
725 /* send empty lists */
726 no_identities(e, type);
727 break;
728 default:
729 /* send a fail message for all other request types */
730 buffer_put_int(&e->output, 1);
731 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
732 }
733 return;
734 }
735
Ben Lindstrom3f471632001-07-04 03:53:15 +0000736 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100737 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000738 case SSH_AGENTC_LOCK:
739 case SSH_AGENTC_UNLOCK:
740 process_lock_agent(e, type == SSH_AGENTC_LOCK);
741 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000742#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000743 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100744 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000745 process_authentication_challenge1(e);
746 break;
747 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
748 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100749 break;
750 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000751 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000752 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
754 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000755 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100756 break;
757 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000758 process_remove_all_identities(e, 1);
759 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000760#endif
Damien Millerad833b32000-08-23 10:46:23 +1000761 /* ssh2 */
762 case SSH2_AGENTC_SIGN_REQUEST:
763 process_sign_request2(e);
764 break;
765 case SSH2_AGENTC_REQUEST_IDENTITIES:
766 process_request_identities(e, 2);
767 break;
768 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000769 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000770 process_add_identity(e, 2);
771 break;
772 case SSH2_AGENTC_REMOVE_IDENTITY:
773 process_remove_identity(e, 2);
774 break;
775 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
776 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100777 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100778#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000779 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000780 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000781 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100782 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000783 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
784 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100785 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100786#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100787 default:
788 /* Unknown message. Respond with failure. */
789 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000790 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100791 buffer_put_int(&e->output, 1);
792 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
793 break;
794 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
Ben Lindstrombba81212001-06-25 05:01:22 +0000797static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000798new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000800 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000801
Damien Miller232711f2004-06-15 10:35:30 +1000802 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803
Damien Miller95def091999-11-25 00:26:21 +1100804 if (fd > max_fd)
805 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 for (i = 0; i < sockets_alloc; i++)
808 if (sockets[i].type == AUTH_UNUSED) {
809 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100810 buffer_init(&sockets[i].input);
811 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000812 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100814 return;
815 }
816 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000817 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100818 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000819 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100820 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000821 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100822 sockets[old_alloc].fd = fd;
823 buffer_init(&sockets[old_alloc].input);
824 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000825 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000826 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827}
828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100830prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
831 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832{
Darren Tucker55119252013-06-02 07:43:59 +1000833 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000834 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100835 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000836 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000837
838 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100839 switch (sockets[i].type) {
840 case AUTH_SOCKET:
841 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100843 break;
844 case AUTH_UNUSED:
845 break;
846 default:
847 fatal("Unknown socket type %d", sockets[i].type);
848 break;
849 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 }
851
852 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000853 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000854 free(*fdrp);
855 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000856 *fdrp = xmalloc(sz);
857 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000858 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000859 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000860 if (n < *fdl)
861 debug("XXX shrink: %d < %d", n, *fdl);
862 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000863 memset(*fdrp, 0, sz);
864 memset(*fdwp, 0, sz);
865
866 for (i = 0; i < sockets_alloc; i++) {
867 switch (sockets[i].type) {
868 case AUTH_SOCKET:
869 case AUTH_CONNECTION:
870 FD_SET(sockets[i].fd, *fdrp);
871 if (buffer_len(&sockets[i].output) > 0)
872 FD_SET(sockets[i].fd, *fdwp);
873 break;
874 default:
875 break;
876 }
877 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100878 deadline = reaper();
879 if (parent_alive_interval != 0)
880 deadline = (deadline == 0) ? parent_alive_interval :
881 MIN(deadline, parent_alive_interval);
882 if (deadline == 0) {
883 *tvpp = NULL;
884 } else {
885 tv.tv_sec = deadline;
886 tv.tv_usec = 0;
887 *tvpp = &tv;
888 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000889 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890}
891
Ben Lindstrombba81212001-06-25 05:01:22 +0000892static void
Damien Miller95def091999-11-25 00:26:21 +1100893after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000895 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100896 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100897 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000898 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100899 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000900 uid_t euid;
901 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Darren Tucker72473c62009-10-07 09:01:03 +1100903 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100904 switch (sockets[i].type) {
905 case AUTH_UNUSED:
906 break;
907 case AUTH_SOCKET:
908 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100909 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000910 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100911 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100912 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100913 error("accept from AUTH_SOCKET: %s",
914 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100915 break;
916 }
Damien Miller538f1812002-09-12 09:51:10 +1000917 if (getpeereid(sock, &euid, &egid) < 0) {
918 error("getpeereid %d failed: %s",
919 sock, strerror(errno));
920 close(sock);
921 break;
922 }
Damien Milleraf9de382002-10-03 11:54:35 +1000923 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000924 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000925 "peer euid %u != uid %u",
926 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000927 close(sock);
928 break;
929 }
Damien Miller95def091999-11-25 00:26:21 +1100930 new_socket(AUTH_CONNECTION, sock);
931 }
932 break;
933 case AUTH_CONNECTION:
934 if (buffer_len(&sockets[i].output) > 0 &&
935 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100936 len = write(sockets[i].fd,
937 buffer_ptr(&sockets[i].output),
938 buffer_len(&sockets[i].output));
939 if (len == -1 && (errno == EAGAIN ||
940 errno == EWOULDBLOCK ||
941 errno == EINTR))
942 continue;
Damien Miller95def091999-11-25 00:26:21 +1100943 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000944 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100945 break;
946 }
947 buffer_consume(&sockets[i].output, len);
948 }
949 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100950 len = read(sockets[i].fd, buf, sizeof(buf));
951 if (len == -1 && (errno == EAGAIN ||
952 errno == EWOULDBLOCK ||
953 errno == EINTR))
954 continue;
Damien Miller95def091999-11-25 00:26:21 +1100955 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000956 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100957 break;
958 }
959 buffer_append(&sockets[i].input, buf, len);
960 process_message(&sockets[i]);
961 }
962 break;
963 default:
964 fatal("Unknown type %d", sockets[i].type);
965 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966}
967
Ben Lindstrombba81212001-06-25 05:01:22 +0000968static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000969cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000970{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000971 if (socket_name[0])
972 unlink(socket_name);
973 if (socket_dir[0])
974 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000975}
976
Darren Tucker3e33cec2003-10-02 16:12:36 +1000977void
Damien Miller792c5111999-10-29 09:47:09 +1000978cleanup_exit(int i)
979{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000980 cleanup_socket();
981 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000982}
983
Damien Miller1c13bd82006-03-26 14:28:14 +1100984/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000985static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000986cleanup_handler(int sig)
987{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000988 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +1100989#ifdef ENABLE_PKCS11
990 pkcs11_terminate();
991#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000992 _exit(2);
993}
994
Ben Lindstrombba81212001-06-25 05:01:22 +0000995static void
Darren Tucker2812dc92007-03-21 20:45:06 +1100996check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +0000997{
Darren Tucker3e78a512011-06-03 14:14:16 +1000998 /*
999 * If our parent has exited then getppid() will return (pid_t)1,
1000 * so testing for that should be safe.
1001 */
1002 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001003 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001004 cleanup_socket();
1005 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001006 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001007}
1008
1009static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001010usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001011{
Damien Millerf0858de2014-04-20 13:01:30 +10001012 fprintf(stderr,
1013 "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-t life]\n"
1014 " [command [arg ...]]\n"
1015 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001016 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001017}
1018
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019int
1020main(int ac, char **av)
1021{
Damien Miller6c711792003-01-24 11:36:23 +11001022 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001023 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001024 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001025 char *shell, *format, *pidstr, *agentsocket = NULL;
1026 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001027 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001028#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001029 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001030#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001031 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001032 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001033 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001034 pid_t pid;
1035 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001036 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001037 size_t len;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001038
Darren Tuckerce321d82005-10-03 18:11:24 +10001039 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1040 sanitise_stdfd();
1041
Damien Miller6cffb9a2002-09-04 16:20:26 +10001042 /* drop */
1043 setegid(getgid());
1044 setgid(getgid());
1045
Damien Miller6c4914a2004-03-03 11:08:59 +11001046#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1047 /* Disable ptrace on Linux without sgid bit */
1048 prctl(PR_SET_DUMPABLE, 0);
1049#endif
1050
Damien Miller1f0311c2014-05-15 14:24:09 +10001051#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001052 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001053#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001054
Damien Miller59d3d5b2003-08-22 09:34:41 +10001055 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001056 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001057
Damien Miller53d81482003-01-22 11:47:19 +11001058 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001059 switch (ch) {
1060 case 'c':
1061 if (s_flag)
1062 usage();
1063 c_flag++;
1064 break;
1065 case 'k':
1066 k_flag++;
1067 break;
1068 case 's':
1069 if (c_flag)
1070 usage();
1071 s_flag++;
1072 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001073 case 'd':
1074 if (d_flag)
1075 usage();
1076 d_flag++;
1077 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001078 case 'a':
1079 agentsocket = optarg;
1080 break;
Damien Miller53d81482003-01-22 11:47:19 +11001081 case 't':
1082 if ((lifetime = convtime(optarg)) == -1) {
1083 fprintf(stderr, "Invalid lifetime\n");
1084 usage();
1085 }
1086 break;
Damien Miller95def091999-11-25 00:26:21 +11001087 default:
1088 usage();
1089 }
Damien Miller792c5111999-10-29 09:47:09 +10001090 }
Damien Miller95def091999-11-25 00:26:21 +11001091 ac -= optind;
1092 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001093
Ben Lindstromd94580c2001-07-04 03:48:02 +00001094 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001095 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001096
Ben Lindstromeecdf232002-04-02 21:03:51 +00001097 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001098 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001099 if (shell != NULL && (len = strlen(shell)) > 2 &&
1100 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001101 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001102 }
Damien Miller95def091999-11-25 00:26:21 +11001103 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001104 const char *errstr = NULL;
1105
Damien Miller95def091999-11-25 00:26:21 +11001106 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1107 if (pidstr == NULL) {
1108 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001109 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001110 exit(1);
1111 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001112 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1113 if (errstr) {
1114 fprintf(stderr,
1115 "%s=\"%s\", which is not a good PID: %s\n",
1116 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001117 exit(1);
1118 }
1119 if (kill(pid, SIGTERM) == -1) {
1120 perror("kill");
1121 exit(1);
1122 }
1123 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1124 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1125 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001126 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001127 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001128 }
Damien Miller95def091999-11-25 00:26:21 +11001129 parent_pid = getpid();
1130
Ben Lindstromb7788f32002-06-06 21:46:08 +00001131 if (agentsocket == NULL) {
1132 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001133 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001134 if (mkdtemp(socket_dir) == NULL) {
1135 perror("mkdtemp: private socket dir");
1136 exit(1);
1137 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001138 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1139 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001140 } else {
1141 /* Try to use specified agent socket */
1142 socket_dir[0] = '\0';
1143 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001144 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145
Damien Miller5428f641999-11-25 11:54:57 +11001146 /*
1147 * Create socket early so it will exist before command gets run from
1148 * the parent.
1149 */
Damien Miller95def091999-11-25 00:26:21 +11001150 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1151 if (sock < 0) {
1152 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001153 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001154 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001155 }
Damien Miller95def091999-11-25 00:26:21 +11001156 memset(&sunaddr, 0, sizeof(sunaddr));
1157 sunaddr.sun_family = AF_UNIX;
1158 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001159 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001160 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001161 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001162 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001163 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001164 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001166 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001167 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001168 perror("listen");
1169 cleanup_exit(1);
1170 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001171
Damien Miller5428f641999-11-25 11:54:57 +11001172 /*
1173 * Fork, and have the parent execute the command, if any, or present
1174 * the socket data. The child continues as the authentication agent.
1175 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001176 if (d_flag) {
1177 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1178 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1179 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1180 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001181 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001182 goto skip;
1183 }
Damien Miller95def091999-11-25 00:26:21 +11001184 pid = fork();
1185 if (pid == -1) {
1186 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001187 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001188 }
1189 if (pid != 0) { /* Parent - execute the given command. */
1190 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001191 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001192 if (ac == 0) {
1193 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1194 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001195 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001196 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001197 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001198 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001199 exit(0);
1200 }
Damien Millere4340be2000-09-16 13:29:08 +11001201 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1202 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1203 perror("setenv");
1204 exit(1);
1205 }
Damien Miller95def091999-11-25 00:26:21 +11001206 execvp(av[0], av);
1207 perror(av[0]);
1208 exit(1);
1209 }
Damien Millerc653b892002-02-08 22:05:41 +11001210 /* child */
1211 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001212
1213 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001214 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001215 cleanup_exit(1);
1216 }
1217
1218 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001219 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1220 /* XXX might close listen socket */
1221 (void)dup2(fd, STDIN_FILENO);
1222 (void)dup2(fd, STDOUT_FILENO);
1223 (void)dup2(fd, STDERR_FILENO);
1224 if (fd > 2)
1225 close(fd);
1226 }
Damien Miller95def091999-11-25 00:26:21 +11001227
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001228#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001229 /* deny core dumps, since memory contains unencrypted private keys */
1230 rlim.rlim_cur = rlim.rlim_max = 0;
1231 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001232 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001233 cleanup_exit(1);
1234 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001235#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001236
1237skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001238
1239#ifdef ENABLE_PKCS11
1240 pkcs11_init(0);
1241#endif
Damien Miller95def091999-11-25 00:26:21 +11001242 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001243 if (ac > 0)
1244 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001245 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001246 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001247 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001248 signal(SIGHUP, cleanup_handler);
1249 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001250 nalloc = 0;
1251
Damien Miller95def091999-11-25 00:26:21 +11001252 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001253 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1254 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001255 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001256 if (parent_alive_interval != 0)
1257 check_parent_exists();
1258 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001259 if (result < 0) {
1260 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001261 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001262 fatal("select: %s", strerror(saved_errno));
1263 } else if (result > 0)
1264 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001265 }
1266 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267}