blob: 25f10c54955277a648f7f4ef34c8d64269d377d6 [file] [log] [blame]
Damien Millerf4977942014-07-30 12:32:46 +10001/* $OpenBSD: ssh-agent.c,v 1.190 2014/07/25 21:22:03 dtucker 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
Damien Millerb1e967c2014-07-03 21:22:40 +1000129/* pid of process for which cleanup_socket is applicable */
130pid_t cleanup_pid = 0;
131
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100133char socket_name[MAXPATHLEN];
134char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Ben Lindstrom2f717042002-06-06 21:52:03 +0000136/* locking */
137int locked = 0;
138char *lock_passwd = NULL;
139
Damien Miller95def091999-11-25 00:26:21 +1100140extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100141
Darren Tucker55119252013-06-02 07:43:59 +1000142/* Default lifetime in seconds (0 == forever) */
143static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100144
Ben Lindstrombba81212001-06-25 05:01:22 +0000145static void
Damien Miller58f34862002-09-04 16:31:21 +1000146close_socket(SocketEntry *e)
147{
Damien Miller58f34862002-09-04 16:31:21 +1000148 close(e->fd);
149 e->fd = -1;
150 e->type = AUTH_UNUSED;
151 buffer_free(&e->input);
152 buffer_free(&e->output);
153 buffer_free(&e->request);
154}
155
156static void
Damien Millerad833b32000-08-23 10:46:23 +1000157idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Damien Millerad833b32000-08-23 10:46:23 +1000159 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000160
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000161 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100162 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000163 idtable[i].nentries = 0;
164 }
165}
166
167/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000169idtab_lookup(int version)
170{
171 if (version < 1 || version > 2)
172 fatal("internal error, bad protocol version %d", version);
173 return &idtable[version];
174}
175
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000176static void
177free_identity(Identity *id)
178{
179 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000180 free(id->provider);
181 free(id->comment);
182 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000183}
184
Damien Millerad833b32000-08-23 10:46:23 +1000185/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100186static Identity *
187lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000188{
Damien Miller1a534ae2002-01-22 23:26:13 +1100189 Identity *id;
190
Damien Millerad833b32000-08-23 10:46:23 +1000191 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100192 TAILQ_FOREACH(id, &tab->idlist, next) {
193 if (key_equal(key, id->key))
194 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000195 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100196 return (NULL);
197}
198
Damien Miller6c711792003-01-24 11:36:23 +1100199/* Check confirmation of keysign request */
200static int
201confirm_key(Identity *id)
202{
Darren Tuckerce327b62004-11-05 20:38:03 +1100203 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100204 int ret = -1;
205
206 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100207 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
208 id->comment, p))
209 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000210 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100211
Damien Miller6c711792003-01-24 11:36:23 +1100212 return (ret);
213}
214
Damien Millerad833b32000-08-23 10:46:23 +1000215/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000216static void
Damien Millerad833b32000-08-23 10:46:23 +1000217process_request_identities(SocketEntry *e, int version)
218{
219 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100220 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000221 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222
Damien Miller95def091999-11-25 00:26:21 +1100223 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000224 buffer_put_char(&msg, (version == 1) ?
225 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
226 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100227 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100228 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000229#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000230 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
231 buffer_put_bignum(&msg, id->key->rsa->e);
232 buffer_put_bignum(&msg, id->key->rsa->n);
Damien Miller1f0311c2014-05-15 14:24:09 +1000233#endif
Damien Millerad833b32000-08-23 10:46:23 +1000234 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000235 u_char *blob;
236 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100237 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000238 buffer_put_string(&msg, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000239 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000240 }
241 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100242 }
243 buffer_put_int(&e->output, buffer_len(&msg));
244 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
245 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}
247
Damien Miller1f0311c2014-05-15 14:24:09 +1000248#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000249/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000250static void
Damien Millerad833b32000-08-23 10:46:23 +1000251process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000253 u_char buf[32], mdbuf[16], session_id[16];
254 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000255 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000256 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000257 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100258 Buffer msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100259 struct ssh_digest_ctx *md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000260 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100263 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100264 if ((challenge = BN_new()) == NULL)
265 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000266
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000267 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000268 buffer_get_bignum(&e->request, key->rsa->e);
269 buffer_get_bignum(&e->request, key->rsa->n);
270 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Millerad833b32000-08-23 10:46:23 +1000272 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000273 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000274 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000275 buffer_get(&e->request, session_id, 16);
276 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000277 if (response_type != 1)
278 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279
Damien Miller1a534ae2002-01-22 23:26:13 +1100280 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100281 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100282 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000283 /* Decrypt the challenge using the private key. */
Damien Miller86687062014-07-02 15:28:02 +1000284 if (rsa_private_decrypt(challenge, challenge, private->rsa) != 0)
Damien Miller7650bc62001-01-30 09:27:26 +1100285 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100286
Damien Millerad833b32000-08-23 10:46:23 +1000287 /* The response is MD5 of decrypted challenge plus session id. */
288 len = BN_num_bytes(challenge);
289 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000290 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000291 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100292 }
Damien Millerad833b32000-08-23 10:46:23 +1000293 memset(buf, 0, 32);
294 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100295 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
296 ssh_digest_update(md, buf, 32) < 0 ||
297 ssh_digest_update(md, session_id, 16) < 0 ||
298 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
299 fatal("%s: md5 failed", __func__);
300 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000301
302 /* Send the response. */
303 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
304 for (i = 0; i < 16; i++)
305 buffer_put_char(&msg, mdbuf[i]);
306 goto send;
307 }
308
309failure:
310 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100311 buffer_put_char(&msg, SSH_AGENT_FAILURE);
312send:
313 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000314 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
315 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100316 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000317 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318}
Damien Miller1f0311c2014-05-15 14:24:09 +1000319#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Millerad833b32000-08-23 10:46:23 +1000321/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000322static void
Damien Millerad833b32000-08-23 10:46:23 +1000323process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000325 u_char *blob, *data, *signature = NULL;
326 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000327 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000328 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000329 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000330 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000331 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000332
333 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000334
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000335 blob = buffer_get_string(&e->request, &blen);
336 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100337
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000338 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000339 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100340 if (flags & SSH_AGENT_OLD_SIGNATURE)
341 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000342
Damien Miller0bc1bd82000-11-13 22:57:25 +1100343 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000344 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100345 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100346 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100347 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000348 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000349 }
Damien Millerad833b32000-08-23 10:46:23 +1000350 buffer_init(&msg);
351 if (ok == 0) {
352 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
353 buffer_put_string(&msg, signature, slen);
354 } else {
355 buffer_put_char(&msg, SSH_AGENT_FAILURE);
356 }
357 buffer_put_int(&e->output, buffer_len(&msg));
358 buffer_append(&e->output, buffer_ptr(&msg),
359 buffer_len(&msg));
360 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000361 free(data);
362 free(blob);
363 free(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000364 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000365}
366
367/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000368static void
Damien Millerad833b32000-08-23 10:46:23 +1000369process_remove_identity(SocketEntry *e, int version)
370{
Damien Miller86687062014-07-02 15:28:02 +1000371 u_int blen;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000372 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100373 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000374 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000375#ifdef WITH_SSH1
376 u_int bits;
377#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100378
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000379 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000380#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000381 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000383 bits = buffer_get_int(&e->request);
384 buffer_get_bignum(&e->request, key->rsa->e);
385 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386
Damien Millerad833b32000-08-23 10:46:23 +1000387 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000388 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000389 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000390 break;
Damien Miller86687062014-07-02 15:28:02 +1000391#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000392 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000393 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100394 key = key_from_blob(blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000395 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000396 break;
397 }
398 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100399 Identity *id = lookup_identity(key, version);
400 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100401 /*
402 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100403 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000404 * the array, we actually free the key there and move
405 * all the entries between the empty slot and the end
406 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100407 */
Damien Millerad833b32000-08-23 10:46:23 +1000408 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100409 if (tab->nentries < 1)
410 fatal("process_remove_identity: "
411 "internal error: tab->nentries %d",
412 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100413 TAILQ_REMOVE(&tab->idlist, id, next);
414 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000415 tab->nentries--;
416 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100417 }
Damien Millerad833b32000-08-23 10:46:23 +1000418 key_free(key);
419 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000421 buffer_put_char(&e->output,
422 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423}
424
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static void
Damien Millerad833b32000-08-23 10:46:23 +1000426process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000427{
Damien Millerad833b32000-08-23 10:46:23 +1000428 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100429 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Damien Miller95def091999-11-25 00:26:21 +1100431 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100432 for (id = TAILQ_FIRST(&tab->idlist); id;
433 id = TAILQ_FIRST(&tab->idlist)) {
434 TAILQ_REMOVE(&tab->idlist, id, next);
435 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100436 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000437
Damien Miller95def091999-11-25 00:26:21 +1100438 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000439 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
441 /* Send success. */
442 buffer_put_int(&e->output, 1);
443 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100444}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445
Darren Tucker2812dc92007-03-21 20:45:06 +1100446/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000447static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000448reaper(void)
449{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000450 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000451 Identity *id, *nxt;
452 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000453 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000454
455 for (version = 1; version < 3; version++) {
456 tab = idtab_lookup(version);
457 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
458 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100459 if (id->death == 0)
460 continue;
461 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100462 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000463 TAILQ_REMOVE(&tab->idlist, id, next);
464 free_identity(id);
465 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100466 } else
467 deadline = (deadline == 0) ? id->death :
468 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000469 }
470 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100471 if (deadline == 0 || deadline <= now)
472 return 0;
473 else
474 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000475}
476
477static void
Damien Millerad833b32000-08-23 10:46:23 +1000478process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100479{
Damien Millerad833b32000-08-23 10:46:23 +1000480 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000481 Identity *id;
Darren Tucker55119252013-06-02 07:43:59 +1000482 int type, success = 0, confirm = 0;
Damien Millerf0e90602013-12-07 10:40:26 +1100483 char *comment;
Darren Tucker55119252013-06-02 07:43:59 +1000484 time_t death = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000485 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100486
Damien Millerad833b32000-08-23 10:46:23 +1000487 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000488#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000489 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000491 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000492 buffer_get_bignum(&e->request, k->rsa->n);
493 buffer_get_bignum(&e->request, k->rsa->e);
494 buffer_get_bignum(&e->request, k->rsa->d);
495 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100496
Damien Millerad833b32000-08-23 10:46:23 +1000497 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000498 buffer_get_bignum(&e->request, k->rsa->q); /* p */
499 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100500
Damien Millerad833b32000-08-23 10:46:23 +1000501 /* Generate additional parameters */
Damien Miller86687062014-07-02 15:28:02 +1000502 if (rsa_generate_additional_parameters(k->rsa) != 0)
503 fatal("%s: rsa_generate_additional_parameters "
504 "error", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100505
Damien Miller5be9d9e2013-12-07 11:24:01 +1100506 /* enable blinding */
Damien Millerc51d0732003-03-15 11:37:09 +1100507 if (RSA_blinding_on(k->rsa, NULL) != 1) {
508 error("process_add_identity: RSA_blinding_on failed");
509 key_free(k);
510 goto send;
511 }
512 break;
Damien Miller86687062014-07-02 15:28:02 +1000513#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100514 case 2:
515 k = key_private_deserialize(&e->request);
516 if (k == NULL) {
517 buffer_clear(&e->request);
518 goto send;
519 }
520 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100521 }
Damien Miller86687062014-07-02 15:28:02 +1000522 if (k == NULL)
Damien Millerad833b32000-08-23 10:46:23 +1000523 goto send;
Damien Miller86687062014-07-02 15:28:02 +1000524 comment = buffer_get_string(&e->request, NULL);
525
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000526 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000527 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000528 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000529 death = monotime() + buffer_get_int(&e->request);
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000530 break;
Damien Miller6c711792003-01-24 11:36:23 +1100531 case SSH_AGENT_CONSTRAIN_CONFIRM:
532 confirm = 1;
533 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000534 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000535 error("process_add_identity: "
536 "Unknown constraint type %d", type);
Darren Tuckera627d422013-06-02 07:31:17 +1000537 free(comment);
Damien Miller1cfadab2008-06-30 00:05:21 +1000538 key_free(k);
539 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000540 }
541 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000542 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100543 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000544 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000545 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100546 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000547 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000548 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
549 /* Increment the number of identities. */
550 tab->nentries++;
551 } else {
552 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000554 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000555 id->comment = comment;
556 id->death = death;
557 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000558send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000559 buffer_put_int(&e->output, 1);
560 buffer_put_char(&e->output,
561 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
562}
563
Ben Lindstrom2f717042002-06-06 21:52:03 +0000564/* XXX todo: encrypt sensitive data with passphrase */
565static void
566process_lock_agent(SocketEntry *e, int lock)
567{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000568 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000569 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000570
571 passwd = buffer_get_string(&e->request, NULL);
572 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
573 locked = 0;
Damien Millera5103f42014-02-04 11:20:14 +1100574 explicit_bzero(lock_passwd, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000575 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000576 lock_passwd = NULL;
577 success = 1;
578 } else if (!locked && lock) {
579 locked = 1;
580 lock_passwd = xstrdup(passwd);
581 success = 1;
582 }
Damien Millera5103f42014-02-04 11:20:14 +1100583 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000584 free(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000585
Ben Lindstrom2f717042002-06-06 21:52:03 +0000586 buffer_put_int(&e->output, 1);
587 buffer_put_char(&e->output,
588 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000589}
590
591static void
592no_identities(SocketEntry *e, u_int type)
593{
594 Buffer msg;
595
596 buffer_init(&msg);
597 buffer_put_char(&msg,
598 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
599 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
600 buffer_put_int(&msg, 0);
601 buffer_put_int(&e->output, buffer_len(&msg));
602 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
603 buffer_free(&msg);
604}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000605
Damien Miller7ea845e2010-02-12 09:21:02 +1100606#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000608process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000609{
Damien Miller7ea845e2010-02-12 09:21:02 +1100610 char *provider = NULL, *pin;
Darren Tucker55119252013-06-02 07:43:59 +1000611 int i, type, version, count = 0, success = 0, confirm = 0;
612 time_t death = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100613 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000614 Identity *id;
615 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100616
Damien Miller7ea845e2010-02-12 09:21:02 +1100617 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000618 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000619
620 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000621 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000622 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000623 death = monotime() + buffer_get_int(&e->request);
Damien Millerd94f20d2003-06-11 22:06:33 +1000624 break;
625 case SSH_AGENT_CONSTRAIN_CONFIRM:
626 confirm = 1;
627 break;
628 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000629 error("process_add_smartcard_key: "
630 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000631 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000632 }
633 }
634 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000635 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000636
Damien Miller7ea845e2010-02-12 09:21:02 +1100637 count = pkcs11_add_provider(provider, pin, &keys);
638 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000639 k = keys[i];
640 version = k->type == KEY_RSA1 ? 1 : 2;
641 tab = idtab_lookup(version);
642 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100643 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100645 id->provider = xstrdup(provider);
646 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000647 id->death = death;
648 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000649 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
650 tab->nentries++;
651 success = 1;
652 } else {
653 key_free(k);
654 }
655 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000656 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000657send:
Darren Tuckera627d422013-06-02 07:31:17 +1000658 free(pin);
659 free(provider);
660 free(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661 buffer_put_int(&e->output, 1);
662 buffer_put_char(&e->output,
663 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
664}
665
666static void
667process_remove_smartcard_key(SocketEntry *e)
668{
Damien Miller7ea845e2010-02-12 09:21:02 +1100669 char *provider = NULL, *pin = NULL;
670 int version, success = 0;
671 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000672 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000673
Damien Miller7ea845e2010-02-12 09:21:02 +1100674 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000675 pin = buffer_get_string(&e->request, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000676 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677
Damien Miller7ea845e2010-02-12 09:21:02 +1100678 for (version = 1; version < 3; version++) {
679 tab = idtab_lookup(version);
680 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
681 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100682 /* Skip file--based keys */
683 if (id->provider == NULL)
684 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100685 if (!strcmp(provider, id->provider)) {
686 TAILQ_REMOVE(&tab->idlist, id, next);
687 free_identity(id);
688 tab->nentries--;
689 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000690 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000691 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100692 if (pkcs11_del_provider(provider) == 0)
693 success = 1;
694 else
695 error("process_remove_smartcard_key:"
696 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000697 free(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698 buffer_put_int(&e->output, 1);
699 buffer_put_char(&e->output,
700 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
701}
Damien Miller7ea845e2010-02-12 09:21:02 +1100702#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000703
Damien Millerad833b32000-08-23 10:46:23 +1000704/* dispatch incoming messages */
705
Ben Lindstrombba81212001-06-25 05:01:22 +0000706static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707process_message(SocketEntry *e)
708{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000709 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000710 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000711
Damien Miller95def091999-11-25 00:26:21 +1100712 if (buffer_len(&e->input) < 5)
713 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100714 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100715 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100716 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000717 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100718 return;
719 }
720 if (buffer_len(&e->input) < msg_len + 4)
721 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000722
723 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100724 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000725 buffer_clear(&e->request);
726 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
727 buffer_consume(&e->input, msg_len);
728 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Ben Lindstrom2f717042002-06-06 21:52:03 +0000730 /* check wheter agent is locked */
731 if (locked && type != SSH_AGENTC_UNLOCK) {
732 buffer_clear(&e->request);
733 switch (type) {
734 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
735 case SSH2_AGENTC_REQUEST_IDENTITIES:
736 /* send empty lists */
737 no_identities(e, type);
738 break;
739 default:
740 /* send a fail message for all other request types */
741 buffer_put_int(&e->output, 1);
742 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
743 }
744 return;
745 }
746
Ben Lindstrom3f471632001-07-04 03:53:15 +0000747 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100748 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000749 case SSH_AGENTC_LOCK:
750 case SSH_AGENTC_UNLOCK:
751 process_lock_agent(e, type == SSH_AGENTC_LOCK);
752 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000753#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000754 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100755 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000756 process_authentication_challenge1(e);
757 break;
758 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
759 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100760 break;
761 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000762 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000763 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100764 break;
765 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000766 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100767 break;
768 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000769 process_remove_all_identities(e, 1);
770 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000771#endif
Damien Millerad833b32000-08-23 10:46:23 +1000772 /* ssh2 */
773 case SSH2_AGENTC_SIGN_REQUEST:
774 process_sign_request2(e);
775 break;
776 case SSH2_AGENTC_REQUEST_IDENTITIES:
777 process_request_identities(e, 2);
778 break;
779 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000780 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000781 process_add_identity(e, 2);
782 break;
783 case SSH2_AGENTC_REMOVE_IDENTITY:
784 process_remove_identity(e, 2);
785 break;
786 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
787 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100788 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100789#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000790 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000791 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000792 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100793 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000794 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
795 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100796 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100797#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100798 default:
799 /* Unknown message. Respond with failure. */
800 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000801 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100802 buffer_put_int(&e->output, 1);
803 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
804 break;
805 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806}
807
Ben Lindstrombba81212001-06-25 05:01:22 +0000808static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000809new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000812
Damien Miller232711f2004-06-15 10:35:30 +1000813 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 if (fd > max_fd)
816 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817
Damien Miller95def091999-11-25 00:26:21 +1100818 for (i = 0; i < sockets_alloc; i++)
819 if (sockets[i].type == AUTH_UNUSED) {
820 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100821 buffer_init(&sockets[i].input);
822 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000823 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000824 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100825 return;
826 }
827 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000828 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100829 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000830 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100831 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000832 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100833 sockets[old_alloc].fd = fd;
834 buffer_init(&sockets[old_alloc].input);
835 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000836 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000837 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838}
839
Ben Lindstrombba81212001-06-25 05:01:22 +0000840static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100841prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
842 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843{
Darren Tucker55119252013-06-02 07:43:59 +1000844 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100846 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000847 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000848
849 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100850 switch (sockets[i].type) {
851 case AUTH_SOCKET:
852 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000853 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100854 break;
855 case AUTH_UNUSED:
856 break;
857 default:
858 fatal("Unknown socket type %d", sockets[i].type);
859 break;
860 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000861 }
862
863 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000864 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000865 free(*fdrp);
866 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000867 *fdrp = xmalloc(sz);
868 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000869 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000870 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000871 if (n < *fdl)
872 debug("XXX shrink: %d < %d", n, *fdl);
873 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000874 memset(*fdrp, 0, sz);
875 memset(*fdwp, 0, sz);
876
877 for (i = 0; i < sockets_alloc; i++) {
878 switch (sockets[i].type) {
879 case AUTH_SOCKET:
880 case AUTH_CONNECTION:
881 FD_SET(sockets[i].fd, *fdrp);
882 if (buffer_len(&sockets[i].output) > 0)
883 FD_SET(sockets[i].fd, *fdwp);
884 break;
885 default:
886 break;
887 }
888 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100889 deadline = reaper();
890 if (parent_alive_interval != 0)
891 deadline = (deadline == 0) ? parent_alive_interval :
892 MIN(deadline, parent_alive_interval);
893 if (deadline == 0) {
894 *tvpp = NULL;
895 } else {
896 tv.tv_sec = deadline;
897 tv.tv_usec = 0;
898 *tvpp = &tv;
899 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000900 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901}
902
Ben Lindstrombba81212001-06-25 05:01:22 +0000903static void
Damien Miller95def091999-11-25 00:26:21 +1100904after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000905{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000906 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100907 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100908 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000909 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100910 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000911 uid_t euid;
912 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913
Darren Tucker72473c62009-10-07 09:01:03 +1100914 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100915 switch (sockets[i].type) {
916 case AUTH_UNUSED:
917 break;
918 case AUTH_SOCKET:
919 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100920 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000921 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100922 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100923 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100924 error("accept from AUTH_SOCKET: %s",
925 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100926 break;
927 }
Damien Miller538f1812002-09-12 09:51:10 +1000928 if (getpeereid(sock, &euid, &egid) < 0) {
929 error("getpeereid %d failed: %s",
930 sock, strerror(errno));
931 close(sock);
932 break;
933 }
Damien Milleraf9de382002-10-03 11:54:35 +1000934 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000935 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000936 "peer euid %u != uid %u",
937 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000938 close(sock);
939 break;
940 }
Damien Miller95def091999-11-25 00:26:21 +1100941 new_socket(AUTH_CONNECTION, sock);
942 }
943 break;
944 case AUTH_CONNECTION:
945 if (buffer_len(&sockets[i].output) > 0 &&
946 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100947 len = write(sockets[i].fd,
948 buffer_ptr(&sockets[i].output),
949 buffer_len(&sockets[i].output));
950 if (len == -1 && (errno == EAGAIN ||
951 errno == EWOULDBLOCK ||
952 errno == EINTR))
953 continue;
Damien Miller95def091999-11-25 00:26:21 +1100954 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000955 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100956 break;
957 }
958 buffer_consume(&sockets[i].output, len);
959 }
960 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100961 len = read(sockets[i].fd, buf, sizeof(buf));
962 if (len == -1 && (errno == EAGAIN ||
963 errno == EWOULDBLOCK ||
964 errno == EINTR))
965 continue;
Damien Miller95def091999-11-25 00:26:21 +1100966 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000967 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100968 break;
969 }
970 buffer_append(&sockets[i].input, buf, len);
Damien Millerf4977942014-07-30 12:32:46 +1000971 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100972 process_message(&sockets[i]);
973 }
974 break;
975 default:
976 fatal("Unknown type %d", sockets[i].type);
977 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978}
979
Ben Lindstrombba81212001-06-25 05:01:22 +0000980static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000981cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000982{
Damien Millerb1e967c2014-07-03 21:22:40 +1000983 if (cleanup_pid != 0 && getpid() != cleanup_pid)
984 return;
985 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000986 if (socket_name[0])
987 unlink(socket_name);
988 if (socket_dir[0])
989 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990}
991
Darren Tucker3e33cec2003-10-02 16:12:36 +1000992void
Damien Miller792c5111999-10-29 09:47:09 +1000993cleanup_exit(int i)
994{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000995 cleanup_socket();
996 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000997}
998
Damien Miller1c13bd82006-03-26 14:28:14 +1100999/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001000static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001001cleanup_handler(int sig)
1002{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001003 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001004#ifdef ENABLE_PKCS11
1005 pkcs11_terminate();
1006#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001007 _exit(2);
1008}
1009
Ben Lindstrombba81212001-06-25 05:01:22 +00001010static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001011check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001012{
Darren Tucker3e78a512011-06-03 14:14:16 +10001013 /*
1014 * If our parent has exited then getppid() will return (pid_t)1,
1015 * so testing for that should be safe.
1016 */
1017 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001018 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001019 cleanup_socket();
1020 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001021 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001022}
1023
1024static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001025usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001026{
Damien Millerf0858de2014-04-20 13:01:30 +10001027 fprintf(stderr,
1028 "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-t life]\n"
1029 " [command [arg ...]]\n"
1030 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001031 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001032}
1033
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034int
1035main(int ac, char **av)
1036{
Damien Miller6c711792003-01-24 11:36:23 +11001037 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001038 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001039 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001040 char *shell, *format, *pidstr, *agentsocket = NULL;
1041 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001042#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001043 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001044#endif
Damien Miller615f9392000-05-17 22:53:33 +10001045 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001046 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001047 pid_t pid;
1048 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001049 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001050 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001051 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001052
Darren Tuckerce321d82005-10-03 18:11:24 +10001053 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1054 sanitise_stdfd();
1055
Damien Miller6cffb9a2002-09-04 16:20:26 +10001056 /* drop */
1057 setegid(getgid());
1058 setgid(getgid());
1059
Damien Miller6c4914a2004-03-03 11:08:59 +11001060#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1061 /* Disable ptrace on Linux without sgid bit */
1062 prctl(PR_SET_DUMPABLE, 0);
1063#endif
1064
Damien Miller1f0311c2014-05-15 14:24:09 +10001065#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001066 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001067#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001068
Damien Miller59d3d5b2003-08-22 09:34:41 +10001069 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001070 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001071
Damien Miller53d81482003-01-22 11:47:19 +11001072 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001073 switch (ch) {
1074 case 'c':
1075 if (s_flag)
1076 usage();
1077 c_flag++;
1078 break;
1079 case 'k':
1080 k_flag++;
1081 break;
1082 case 's':
1083 if (c_flag)
1084 usage();
1085 s_flag++;
1086 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001087 case 'd':
1088 if (d_flag)
1089 usage();
1090 d_flag++;
1091 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001092 case 'a':
1093 agentsocket = optarg;
1094 break;
Damien Miller53d81482003-01-22 11:47:19 +11001095 case 't':
1096 if ((lifetime = convtime(optarg)) == -1) {
1097 fprintf(stderr, "Invalid lifetime\n");
1098 usage();
1099 }
1100 break;
Damien Miller95def091999-11-25 00:26:21 +11001101 default:
1102 usage();
1103 }
Damien Miller792c5111999-10-29 09:47:09 +10001104 }
Damien Miller95def091999-11-25 00:26:21 +11001105 ac -= optind;
1106 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001107
Ben Lindstromd94580c2001-07-04 03:48:02 +00001108 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001109 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001110
Ben Lindstromeecdf232002-04-02 21:03:51 +00001111 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001112 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001113 if (shell != NULL && (len = strlen(shell)) > 2 &&
1114 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001115 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001116 }
Damien Miller95def091999-11-25 00:26:21 +11001117 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001118 const char *errstr = NULL;
1119
Damien Miller95def091999-11-25 00:26:21 +11001120 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1121 if (pidstr == NULL) {
1122 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001123 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001124 exit(1);
1125 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001126 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1127 if (errstr) {
1128 fprintf(stderr,
1129 "%s=\"%s\", which is not a good PID: %s\n",
1130 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001131 exit(1);
1132 }
1133 if (kill(pid, SIGTERM) == -1) {
1134 perror("kill");
1135 exit(1);
1136 }
1137 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1138 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1139 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001140 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001141 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001142 }
Damien Miller95def091999-11-25 00:26:21 +11001143 parent_pid = getpid();
1144
Ben Lindstromb7788f32002-06-06 21:46:08 +00001145 if (agentsocket == NULL) {
1146 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001147 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001148 if (mkdtemp(socket_dir) == NULL) {
1149 perror("mkdtemp: private socket dir");
1150 exit(1);
1151 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001152 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1153 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001154 } else {
1155 /* Try to use specified agent socket */
1156 socket_dir[0] = '\0';
1157 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001158 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001159
Damien Miller5428f641999-11-25 11:54:57 +11001160 /*
1161 * Create socket early so it will exist before command gets run from
1162 * the parent.
1163 */
Damien Millerab2ec582014-07-18 15:04:47 +10001164 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001165 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001166 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001167 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001168 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001169 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001170 }
Damien Millerab2ec582014-07-18 15:04:47 +10001171 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001172
Damien Miller5428f641999-11-25 11:54:57 +11001173 /*
1174 * Fork, and have the parent execute the command, if any, or present
1175 * the socket data. The child continues as the authentication agent.
1176 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001177 if (d_flag) {
1178 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1179 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1180 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1181 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001182 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001183 goto skip;
1184 }
Damien Miller95def091999-11-25 00:26:21 +11001185 pid = fork();
1186 if (pid == -1) {
1187 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001188 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001189 }
1190 if (pid != 0) { /* Parent - execute the given command. */
1191 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001192 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001193 if (ac == 0) {
1194 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1195 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001196 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001197 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001198 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001199 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001200 exit(0);
1201 }
Damien Millere4340be2000-09-16 13:29:08 +11001202 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1203 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1204 perror("setenv");
1205 exit(1);
1206 }
Damien Miller95def091999-11-25 00:26:21 +11001207 execvp(av[0], av);
1208 perror(av[0]);
1209 exit(1);
1210 }
Damien Millerc653b892002-02-08 22:05:41 +11001211 /* child */
1212 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001213
1214 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001215 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001216 cleanup_exit(1);
1217 }
1218
1219 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001220 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1221 /* XXX might close listen socket */
1222 (void)dup2(fd, STDIN_FILENO);
1223 (void)dup2(fd, STDOUT_FILENO);
1224 (void)dup2(fd, STDERR_FILENO);
1225 if (fd > 2)
1226 close(fd);
1227 }
Damien Miller95def091999-11-25 00:26:21 +11001228
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001229#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001230 /* deny core dumps, since memory contains unencrypted private keys */
1231 rlim.rlim_cur = rlim.rlim_max = 0;
1232 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001233 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001234 cleanup_exit(1);
1235 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001236#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001237
1238skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001239
Damien Millerb1e967c2014-07-03 21:22:40 +10001240 cleanup_pid = getpid();
1241
Damien Miller7ea845e2010-02-12 09:21:02 +11001242#ifdef ENABLE_PKCS11
1243 pkcs11_init(0);
1244#endif
Damien Miller95def091999-11-25 00:26:21 +11001245 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001246 if (ac > 0)
1247 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001248 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001249 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001250 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001251 signal(SIGHUP, cleanup_handler);
1252 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001253 nalloc = 0;
1254
Damien Miller95def091999-11-25 00:26:21 +11001255 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001256 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1257 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001258 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001259 if (parent_alive_interval != 0)
1260 check_parent_exists();
1261 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001262 if (result < 0) {
1263 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001264 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001265 fatal("select: %s", strerror(saved_errno));
1266 } else if (result > 0)
1267 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001268 }
1269 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270}