blob: 34b19b754ec10a970dd48e0f6da6a90bfe5a0b65 [file] [log] [blame]
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +00001/* $OpenBSD: ssh-agent.c,v 1.203 2015/05/15 05:44:21 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
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000039#include <sys/param.h> /* MIN MAX */
Damien Miller574c41f2006-03-15 11:40:10 +110040#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100041#include <sys/param.h>
42#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110043#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100044#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100045#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
Damien Miller574c41f2006-03-15 11:40:10 +110048#ifdef HAVE_SYS_UN_H
49# include <sys/un.h>
50#endif
Damien Miller9b481512002-09-12 10:43:29 +100051#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100052
Damien Miller1f0311c2014-05-15 14:24:09 +100053#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110055#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100056#endif
Damien Millere3476ed2006-07-24 14:13:33 +100057
Darren Tucker39972492006-07-12 22:22:46 +100058#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100059#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000060#include <limits.h>
Damien Miller03e20032006-03-15 11:16:59 +110061#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110062# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110063#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110064#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100065#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100066#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100067#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100068#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100069#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100070#include <unistd.h>
Damien Millere97201f2015-05-21 17:55:15 +100071#ifdef HAVE_UTIL_H
72# include <util.h>
73#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110074
markus@openbsd.org139ca812015-01-14 13:09:09 +000075#include "key.h" /* XXX for typedef */
76#include "buffer.h" /* XXX for typedef */
77
Damien Millerd7834352006-08-05 12:39:39 +100078#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "ssh.h"
80#include "rsa.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000081#include "sshbuf.h"
82#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100083#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110084#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000085#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110086#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110087#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000088#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller7ea845e2010-02-12 09:21:02 +110090#ifdef ENABLE_PKCS11
91#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000092#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000093
Damien Miller6c4914a2004-03-03 11:08:59 +110094#if defined(HAVE_SYS_PRCTL_H)
95#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
96#endif
97
Ben Lindstrom65366a82001-12-06 16:32:47 +000098typedef enum {
99 AUTH_UNUSED,
100 AUTH_SOCKET,
101 AUTH_CONNECTION
102} sock_type;
103
Damien Miller95def091999-11-25 00:26:21 +1100104typedef struct {
105 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000106 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000107 struct sshbuf *input;
108 struct sshbuf *output;
109 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110} SocketEntry;
111
Ben Lindstrom46c16222000-12-22 01:43:59 +0000112u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113SocketEntry *sockets = NULL;
114
Damien Miller1a534ae2002-01-22 23:26:13 +1100115typedef struct identity {
116 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000117 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100118 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100119 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000120 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100121 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122} Identity;
123
Damien Millerad833b32000-08-23 10:46:23 +1000124typedef struct {
125 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100126 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000127} Idtab;
128
129/* private key table, one per protocol version */
130Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
132int max_fd = 0;
133
134/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000135pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000136time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Millerb1e967c2014-07-03 21:22:40 +1000138/* pid of process for which cleanup_socket is applicable */
139pid_t cleanup_pid = 0;
140
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000142char socket_name[PATH_MAX];
143char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Ben Lindstrom2f717042002-06-06 21:52:03 +0000145/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000146#define LOCK_SIZE 32
147#define LOCK_SALT_SIZE 16
148#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000149int locked = 0;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000150char lock_passwd[LOCK_SIZE];
151char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000152
Damien Miller95def091999-11-25 00:26:21 +1100153extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100154
Darren Tucker55119252013-06-02 07:43:59 +1000155/* Default lifetime in seconds (0 == forever) */
156static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100157
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000158static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void
Damien Miller58f34862002-09-04 16:31:21 +1000161close_socket(SocketEntry *e)
162{
Damien Miller58f34862002-09-04 16:31:21 +1000163 close(e->fd);
164 e->fd = -1;
165 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000166 sshbuf_free(e->input);
167 sshbuf_free(e->output);
168 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000169}
170
171static void
Damien Millerad833b32000-08-23 10:46:23 +1000172idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Millerad833b32000-08-23 10:46:23 +1000174 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000175
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000176 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100177 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000178 idtable[i].nentries = 0;
179 }
180}
181
182/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000184idtab_lookup(int version)
185{
186 if (version < 1 || version > 2)
187 fatal("internal error, bad protocol version %d", version);
188 return &idtable[version];
189}
190
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000191static void
192free_identity(Identity *id)
193{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000194 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000195 free(id->provider);
196 free(id->comment);
197 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000198}
199
Damien Millerad833b32000-08-23 10:46:23 +1000200/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100201static Identity *
markus@openbsd.org139ca812015-01-14 13:09:09 +0000202lookup_identity(struct sshkey *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000203{
Damien Miller1a534ae2002-01-22 23:26:13 +1100204 Identity *id;
205
Damien Millerad833b32000-08-23 10:46:23 +1000206 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100207 TAILQ_FOREACH(id, &tab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000208 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100209 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000210 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100211 return (NULL);
212}
213
Damien Miller6c711792003-01-24 11:36:23 +1100214/* Check confirmation of keysign request */
215static int
216confirm_key(Identity *id)
217{
Darren Tuckerce327b62004-11-05 20:38:03 +1100218 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100219 int ret = -1;
220
markus@openbsd.org139ca812015-01-14 13:09:09 +0000221 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000222 if (p != NULL &&
223 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100224 id->comment, p))
225 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000226 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100227
Damien Miller6c711792003-01-24 11:36:23 +1100228 return (ret);
229}
230
markus@openbsd.org139ca812015-01-14 13:09:09 +0000231static void
232send_status(SocketEntry *e, int success)
233{
234 int r;
235
236 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
237 (r = sshbuf_put_u8(e->output, success ?
238 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
239 fatal("%s: buffer error: %s", __func__, ssh_err(r));
240}
241
Damien Millerad833b32000-08-23 10:46:23 +1000242/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Millerad833b32000-08-23 10:46:23 +1000244process_request_identities(SocketEntry *e, int version)
245{
246 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100247 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000248 struct sshbuf *msg;
249 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
markus@openbsd.org139ca812015-01-14 13:09:09 +0000251 if ((msg = sshbuf_new()) == NULL)
252 fatal("%s: sshbuf_new failed", __func__);
253 if ((r = sshbuf_put_u8(msg, (version == 1) ?
254 SSH_AGENT_RSA_IDENTITIES_ANSWER :
255 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
256 (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
257 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1a534ae2002-01-22 23:26:13 +1100258 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100259 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000260#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000261 if ((r = sshbuf_put_u32(msg,
262 BN_num_bits(id->key->rsa->n))) != 0 ||
263 (r = sshbuf_put_bignum1(msg,
264 id->key->rsa->e)) != 0 ||
265 (r = sshbuf_put_bignum1(msg,
266 id->key->rsa->n)) != 0)
267 fatal("%s: buffer error: %s",
268 __func__, ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000269#endif
Damien Millerad833b32000-08-23 10:46:23 +1000270 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000271 u_char *blob;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000272 size_t blen;
273
274 if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
275 error("%s: sshkey_to_blob: %s", __func__,
276 ssh_err(r));
277 continue;
278 }
279 if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
280 fatal("%s: buffer error: %s",
281 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000282 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000283 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000284 if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100286 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000287 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
288 fatal("%s: buffer error: %s", __func__, ssh_err(r));
289 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290}
291
Damien Miller1f0311c2014-05-15 14:24:09 +1000292#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000293/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static void
Damien Millerad833b32000-08-23 10:46:23 +1000295process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000297 u_char buf[32], mdbuf[16], session_id[16];
298 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000299 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000300 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000301 int r, len;
302 struct sshbuf *msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100303 struct ssh_digest_ctx *md;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000304 struct sshkey *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305
markus@openbsd.org139ca812015-01-14 13:09:09 +0000306 if ((msg = sshbuf_new()) == NULL)
307 fatal("%s: sshbuf_new failed", __func__);
308 if ((key = sshkey_new(KEY_RSA1)) == NULL)
309 fatal("%s: sshkey_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100310 if ((challenge = BN_new()) == NULL)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000311 fatal("%s: BN_new failed", __func__);
Damien Millerad833b32000-08-23 10:46:23 +1000312
markus@openbsd.org139ca812015-01-14 13:09:09 +0000313 if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
314 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
315 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
316 (r = sshbuf_get_bignum1(e->request, challenge)))
317 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Millerad833b32000-08-23 10:46:23 +1000319 /* Only protocol 1.1 is supported */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000320 if (sshbuf_len(e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000321 goto failure;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000322 if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
323 (r = sshbuf_get_u32(e->request, &response_type)) != 0)
324 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000325 if (response_type != 1)
326 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000327
Damien Miller1a534ae2002-01-22 23:26:13 +1100328 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100329 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000330 struct sshkey *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000331 /* Decrypt the challenge using the private key. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000332 if ((r = rsa_private_decrypt(challenge, challenge,
333 private->rsa) != 0)) {
334 fatal("%s: rsa_public_encrypt: %s", __func__,
335 ssh_err(r));
336 goto failure; /* XXX ? */
337 }
Damien Millerfd7c9111999-11-08 16:15:55 +1100338
markus@openbsd.org139ca812015-01-14 13:09:09 +0000339 /* The response is MD5 of decrypted challenge plus session id */
Damien Millerad833b32000-08-23 10:46:23 +1000340 len = BN_num_bytes(challenge);
341 if (len <= 0 || len > 32) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000342 logit("%s: bad challenge length %d", __func__, len);
Damien Millerad833b32000-08-23 10:46:23 +1000343 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100344 }
Damien Millerad833b32000-08-23 10:46:23 +1000345 memset(buf, 0, 32);
346 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100347 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
348 ssh_digest_update(md, buf, 32) < 0 ||
349 ssh_digest_update(md, session_id, 16) < 0 ||
350 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
351 fatal("%s: md5 failed", __func__);
352 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000353
354 /* Send the response. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000355 if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
356 (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
357 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000358 goto send;
359 }
360
markus@openbsd.org139ca812015-01-14 13:09:09 +0000361 failure:
Damien Millerad833b32000-08-23 10:46:23 +1000362 /* Unknown identity or protocol error. Send failure. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000363 if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
364 fatal("%s: buffer error: %s", __func__, ssh_err(r));
365 send:
366 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
367 fatal("%s: buffer error: %s", __func__, ssh_err(r));
368 sshkey_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100369 BN_clear_free(challenge);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000370 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371}
Damien Miller1f0311c2014-05-15 14:24:09 +1000372#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Damien Millerad833b32000-08-23 10:46:23 +1000374/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000375static void
Damien Millerad833b32000-08-23 10:46:23 +1000376process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000377{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000378 u_char *blob, *data, *signature = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000379 size_t blen, dlen, slen = 0;
380 u_int compat = 0, flags;
381 int r, ok = -1;
382 struct sshbuf *msg;
383 struct sshkey *key;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000384 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000385
djm@openbsd.org0088c572015-01-14 19:33:41 +0000386 if ((msg = sshbuf_new()) == NULL)
387 fatal("%s: sshbuf_new failed", __func__);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000388 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
389 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
390 (r = sshbuf_get_u32(e->request, &flags)) != 0)
391 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100392 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000393 compat = SSH_BUG_SIGBLOB;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000394 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000395 error("%s: cannot parse key blob: %s", __func__, ssh_err(ok));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000396 goto send;
Damien Millerad833b32000-08-23 10:46:23 +1000397 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000398 if ((id = lookup_identity(key, 2)) == NULL) {
399 verbose("%s: %s key not found", __func__, sshkey_type(key));
400 goto send;
401 }
402 if (id->confirm && confirm_key(id) != 0) {
403 verbose("%s: user refused key", __func__);
404 goto send;
405 }
406 if ((r = sshkey_sign(id->key, &signature, &slen,
407 data, dlen, compat)) != 0) {
408 error("%s: sshkey_sign: %s", __func__, ssh_err(ok));
409 goto send;
410 }
411 /* Success */
412 ok = 0;
413 send:
414 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000415 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000416 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
417 (r = sshbuf_put_string(msg, signature, slen)) != 0)
418 fatal("%s: buffer error: %s", __func__, ssh_err(r));
419 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
420 fatal("%s: buffer error: %s", __func__, ssh_err(r));
421
422 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
423 fatal("%s: buffer error: %s", __func__, ssh_err(r));
424
425 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000426 free(data);
427 free(blob);
428 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000429}
430
431/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000432static void
Damien Millerad833b32000-08-23 10:46:23 +1000433process_remove_identity(SocketEntry *e, int version)
434{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000435 size_t blen;
436 int r, success = 0;
437 struct sshkey *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000438 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000439#ifdef WITH_SSH1
440 u_int bits;
441#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100442
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000443 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000444#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000445 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000446 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
447 error("%s: sshkey_new failed", __func__);
448 return;
449 }
450 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
451 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
452 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
453 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454
markus@openbsd.org139ca812015-01-14 13:09:09 +0000455 if (bits != sshkey_size(key))
456 logit("Warning: identity keysize mismatch: "
457 "actual %u, announced %u",
458 sshkey_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000459 break;
Damien Miller86687062014-07-02 15:28:02 +1000460#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000461 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000462 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
463 fatal("%s: buffer error: %s", __func__, ssh_err(r));
464 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
465 error("%s: sshkey_from_blob failed: %s",
466 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000467 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000468 break;
469 }
470 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100471 Identity *id = lookup_identity(key, version);
472 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100473 /*
474 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100475 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000476 * the array, we actually free the key there and move
477 * all the entries between the empty slot and the end
478 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100479 */
Damien Millerad833b32000-08-23 10:46:23 +1000480 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 if (tab->nentries < 1)
482 fatal("process_remove_identity: "
483 "internal error: tab->nentries %d",
484 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100485 TAILQ_REMOVE(&tab->idlist, id, next);
486 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000487 tab->nentries--;
488 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100489 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000490 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000491 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000492 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493}
494
Ben Lindstrombba81212001-06-25 05:01:22 +0000495static void
Damien Millerad833b32000-08-23 10:46:23 +1000496process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497{
Damien Millerad833b32000-08-23 10:46:23 +1000498 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100499 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100502 for (id = TAILQ_FIRST(&tab->idlist); id;
503 id = TAILQ_FIRST(&tab->idlist)) {
504 TAILQ_REMOVE(&tab->idlist, id, next);
505 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100506 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Damien Miller95def091999-11-25 00:26:21 +1100508 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000509 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510
511 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000512 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100513}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Darren Tucker2812dc92007-03-21 20:45:06 +1100515/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000516static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000517reaper(void)
518{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000519 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000520 Identity *id, *nxt;
521 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000522 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000523
524 for (version = 1; version < 3; version++) {
525 tab = idtab_lookup(version);
526 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
527 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100528 if (id->death == 0)
529 continue;
530 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100531 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532 TAILQ_REMOVE(&tab->idlist, id, next);
533 free_identity(id);
534 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100535 } else
536 deadline = (deadline == 0) ? id->death :
537 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000538 }
539 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100540 if (deadline == 0 || deadline <= now)
541 return 0;
542 else
543 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000544}
545
markus@openbsd.org139ca812015-01-14 13:09:09 +0000546/*
547 * XXX this and the corresponding serialisation function probably belongs
548 * in key.c
549 */
djm@openbsd.org111dfb22015-03-03 21:21:13 +0000550#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000551static int
552agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
553{
554 struct sshkey *k = NULL;
555 int r = SSH_ERR_INTERNAL_ERROR;
556
557 *kp = NULL;
558 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
559 return SSH_ERR_ALLOC_FAIL;
560
561 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
562 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
563 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
564 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
565 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
566 /* SSH1 and SSL have p and q swapped */
567 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
568 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
569 goto out;
570
571 /* Generate additional parameters */
572 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
573 goto out;
574 /* enable blinding */
575 if (RSA_blinding_on(k->rsa, NULL) != 1) {
576 r = SSH_ERR_LIBCRYPTO_ERROR;
577 goto out;
578 }
579
580 r = 0; /* success */
581 out:
582 if (r == 0)
583 *kp = k;
584 else
585 sshkey_free(k);
586 return r;
587}
Damien Miller6c203922015-03-03 13:48:48 -0800588#endif /* WITH_SSH1 */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000589
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000590static void
Damien Millerad833b32000-08-23 10:46:23 +1000591process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100592{
Damien Millerad833b32000-08-23 10:46:23 +1000593 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000594 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000595 int success = 0, confirm = 0;
596 u_int seconds;
597 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000598 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000599 struct sshkey *k = NULL;
600 u_char ctype;
601 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100602
Damien Millerad833b32000-08-23 10:46:23 +1000603 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000604#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000605 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000606 r = agent_decode_rsa1(e->request, &k);
Damien Millerc51d0732003-03-15 11:37:09 +1100607 break;
Damien Miller86687062014-07-02 15:28:02 +1000608#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100609 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000610 r = sshkey_private_deserialize(e->request, &k);
Damien Millerf0e90602013-12-07 10:40:26 +1100611 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100612 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000613 if (r != 0 || k == NULL ||
614 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
615 error("%s: decode private key: %s", __func__, ssh_err(r));
616 goto err;
617 }
Damien Miller86687062014-07-02 15:28:02 +1000618
markus@openbsd.org139ca812015-01-14 13:09:09 +0000619 while (sshbuf_len(e->request)) {
620 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
621 error("%s: buffer error: %s", __func__, ssh_err(r));
622 goto err;
623 }
624 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000625 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000626 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
627 error("%s: bad lifetime constraint: %s",
628 __func__, ssh_err(r));
629 goto err;
630 }
631 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000632 break;
Damien Miller6c711792003-01-24 11:36:23 +1100633 case SSH_AGENT_CONSTRAIN_CONFIRM:
634 confirm = 1;
635 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000636 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000637 error("%s: Unknown constraint %d", __func__, ctype);
638 err:
639 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000640 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000641 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000642 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000643 }
644 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000645
Damien Miller1cfadab2008-06-30 00:05:21 +1000646 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100647 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000648 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000649 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100650 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000651 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000652 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
653 /* Increment the number of identities. */
654 tab->nentries++;
655 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000656 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000657 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000658 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000659 id->comment = comment;
660 id->death = death;
661 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000662send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000663 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000664}
665
Ben Lindstrom2f717042002-06-06 21:52:03 +0000666/* XXX todo: encrypt sensitive data with passphrase */
667static void
668process_lock_agent(SocketEntry *e, int lock)
669{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000670 int r, success = 0, delay;
671 char *passwd, passwdhash[LOCK_SIZE];
672 static u_int fail_count = 0;
673 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000674
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000675 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000676 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000677 if (pwlen == 0) {
678 debug("empty password not supported");
679 } else if (locked && !lock) {
680 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
681 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
682 fatal("bcrypt_pbkdf");
683 if (timingsafe_bcmp(passwdhash, lock_passwd, LOCK_SIZE) == 0) {
684 debug("agent unlocked");
685 locked = 0;
686 fail_count = 0;
687 explicit_bzero(lock_passwd, sizeof(lock_passwd));
688 success = 1;
689 } else {
690 /* delay in 0.1s increments up to 10s */
691 if (fail_count < 100)
692 fail_count++;
693 delay = 100000 * fail_count;
694 debug("unlock failed, delaying %0.1lf seconds",
695 (double)delay/1000000);
696 usleep(delay);
697 }
698 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000699 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000700 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000701 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000702 arc4random_buf(lock_salt, sizeof(lock_salt));
703 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
704 lock_passwd, sizeof(lock_passwd), LOCK_ROUNDS) < 0)
705 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000706 success = 1;
707 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000708 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000709 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000710 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000711}
712
713static void
714no_identities(SocketEntry *e, u_int type)
715{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000716 struct sshbuf *msg;
717 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000718
markus@openbsd.org139ca812015-01-14 13:09:09 +0000719 if ((msg = sshbuf_new()) == NULL)
720 fatal("%s: sshbuf_new failed", __func__);
721 if ((r = sshbuf_put_u8(msg,
Ben Lindstrom2f717042002-06-06 21:52:03 +0000722 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
markus@openbsd.org139ca812015-01-14 13:09:09 +0000723 SSH_AGENT_RSA_IDENTITIES_ANSWER :
724 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
725 (r = sshbuf_put_u32(msg, 0)) != 0 ||
726 (r = sshbuf_put_stringb(e->output, msg)) != 0)
727 fatal("%s: buffer error: %s", __func__, ssh_err(r));
728 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000729}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000730
Damien Miller7ea845e2010-02-12 09:21:02 +1100731#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000732static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000733process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000734{
Damien Miller7ea845e2010-02-12 09:21:02 +1100735 char *provider = NULL, *pin;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000736 int r, i, version, count = 0, success = 0, confirm = 0;
737 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000738 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000739 u_char type;
740 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000741 Identity *id;
742 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100743
markus@openbsd.org139ca812015-01-14 13:09:09 +0000744 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
745 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
746 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000747
markus@openbsd.org139ca812015-01-14 13:09:09 +0000748 while (sshbuf_len(e->request)) {
749 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
750 fatal("%s: buffer error: %s", __func__, ssh_err(r));
751 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000752 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000753 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
754 fatal("%s: buffer error: %s",
755 __func__, ssh_err(r));
756 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000757 break;
758 case SSH_AGENT_CONSTRAIN_CONFIRM:
759 confirm = 1;
760 break;
761 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000762 error("process_add_smartcard_key: "
763 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000764 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000765 }
766 }
767 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000768 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000769
Damien Miller7ea845e2010-02-12 09:21:02 +1100770 count = pkcs11_add_provider(provider, pin, &keys);
771 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000772 k = keys[i];
773 version = k->type == KEY_RSA1 ? 1 : 2;
774 tab = idtab_lookup(version);
775 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100776 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000777 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100778 id->provider = xstrdup(provider);
779 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000780 id->death = death;
781 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000782 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
783 tab->nentries++;
784 success = 1;
785 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000786 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000787 }
788 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000789 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000790send:
Darren Tuckera627d422013-06-02 07:31:17 +1000791 free(pin);
792 free(provider);
793 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000794 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000795}
796
797static void
798process_remove_smartcard_key(SocketEntry *e)
799{
Damien Miller7ea845e2010-02-12 09:21:02 +1100800 char *provider = NULL, *pin = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000801 int r, version, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100802 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000803 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000804
markus@openbsd.org139ca812015-01-14 13:09:09 +0000805 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
806 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
807 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000808 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000809
Damien Miller7ea845e2010-02-12 09:21:02 +1100810 for (version = 1; version < 3; version++) {
811 tab = idtab_lookup(version);
812 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
813 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100814 /* Skip file--based keys */
815 if (id->provider == NULL)
816 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100817 if (!strcmp(provider, id->provider)) {
818 TAILQ_REMOVE(&tab->idlist, id, next);
819 free_identity(id);
820 tab->nentries--;
821 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000822 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000823 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100824 if (pkcs11_del_provider(provider) == 0)
825 success = 1;
826 else
827 error("process_remove_smartcard_key:"
828 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000829 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000830 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000831}
Damien Miller7ea845e2010-02-12 09:21:02 +1100832#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000833
Damien Millerad833b32000-08-23 10:46:23 +1000834/* dispatch incoming messages */
835
Ben Lindstrombba81212001-06-25 05:01:22 +0000836static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000837process_message(SocketEntry *e)
838{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000839 u_int msg_len;
840 u_char type;
841 const u_char *cp;
842 int r;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000843
markus@openbsd.org139ca812015-01-14 13:09:09 +0000844 if (sshbuf_len(e->input) < 5)
Damien Miller95def091999-11-25 00:26:21 +1100845 return; /* Incomplete message. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000846 cp = sshbuf_ptr(e->input);
847 msg_len = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100848 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000849 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100850 return;
851 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000852 if (sshbuf_len(e->input) < msg_len + 4)
Damien Miller95def091999-11-25 00:26:21 +1100853 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000854
855 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000856 sshbuf_reset(e->request);
857 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
858 (r = sshbuf_get_u8(e->request, &type)) != 0)
859 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Ben Lindstrom2f717042002-06-06 21:52:03 +0000861 /* check wheter agent is locked */
862 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000863 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000864 switch (type) {
865 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
866 case SSH2_AGENTC_REQUEST_IDENTITIES:
867 /* send empty lists */
868 no_identities(e, type);
869 break;
870 default:
871 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000872 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000873 }
874 return;
875 }
876
Ben Lindstrom3f471632001-07-04 03:53:15 +0000877 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100878 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000879 case SSH_AGENTC_LOCK:
880 case SSH_AGENTC_UNLOCK:
881 process_lock_agent(e, type == SSH_AGENTC_LOCK);
882 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000883#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000884 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100885 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000886 process_authentication_challenge1(e);
887 break;
888 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
889 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100890 break;
891 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000892 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000893 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100894 break;
895 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000896 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100897 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000898#endif
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000899 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
900 process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
901 break;
Damien Millerad833b32000-08-23 10:46:23 +1000902 /* ssh2 */
903 case SSH2_AGENTC_SIGN_REQUEST:
904 process_sign_request2(e);
905 break;
906 case SSH2_AGENTC_REQUEST_IDENTITIES:
907 process_request_identities(e, 2);
908 break;
909 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000910 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000911 process_add_identity(e, 2);
912 break;
913 case SSH2_AGENTC_REMOVE_IDENTITY:
914 process_remove_identity(e, 2);
915 break;
916 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
917 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100918 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100919#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000920 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000921 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000922 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100923 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000924 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
925 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100926 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100927#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100928 default:
929 /* Unknown message. Respond with failure. */
930 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000931 sshbuf_reset(e->request);
932 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100933 break;
934 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
Ben Lindstrombba81212001-06-25 05:01:22 +0000937static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000938new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000940 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000941
Damien Miller232711f2004-06-15 10:35:30 +1000942 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000943
Damien Miller95def091999-11-25 00:26:21 +1100944 if (fd > max_fd)
945 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946
Damien Miller95def091999-11-25 00:26:21 +1100947 for (i = 0; i < sockets_alloc; i++)
948 if (sockets[i].type == AUTH_UNUSED) {
949 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000950 if ((sockets[i].input = sshbuf_new()) == NULL)
951 fatal("%s: sshbuf_new failed", __func__);
952 if ((sockets[i].output = sshbuf_new()) == NULL)
953 fatal("%s: sshbuf_new failed", __func__);
954 if ((sockets[i].request = sshbuf_new()) == NULL)
955 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000956 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100957 return;
958 }
959 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000960 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000961 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000962 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100963 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000964 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100965 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000966 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
967 fatal("%s: sshbuf_new failed", __func__);
968 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
969 fatal("%s: sshbuf_new failed", __func__);
970 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
971 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000972 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973}
974
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100976prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
977 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000978{
Darren Tucker55119252013-06-02 07:43:59 +1000979 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000980 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100981 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000982 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000983
984 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100985 switch (sockets[i].type) {
986 case AUTH_SOCKET:
987 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000988 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100989 break;
990 case AUTH_UNUSED:
991 break;
992 default:
993 fatal("Unknown socket type %d", sockets[i].type);
994 break;
995 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000996 }
997
998 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000999 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +10001000 free(*fdrp);
1001 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001002 *fdrp = xmalloc(sz);
1003 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001004 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001005 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001006 if (n < *fdl)
1007 debug("XXX shrink: %d < %d", n, *fdl);
1008 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001009 memset(*fdrp, 0, sz);
1010 memset(*fdwp, 0, sz);
1011
1012 for (i = 0; i < sockets_alloc; i++) {
1013 switch (sockets[i].type) {
1014 case AUTH_SOCKET:
1015 case AUTH_CONNECTION:
1016 FD_SET(sockets[i].fd, *fdrp);
markus@openbsd.org139ca812015-01-14 13:09:09 +00001017 if (sshbuf_len(sockets[i].output) > 0)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001018 FD_SET(sockets[i].fd, *fdwp);
1019 break;
1020 default:
1021 break;
1022 }
1023 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001024 deadline = reaper();
1025 if (parent_alive_interval != 0)
1026 deadline = (deadline == 0) ? parent_alive_interval :
1027 MIN(deadline, parent_alive_interval);
1028 if (deadline == 0) {
1029 *tvpp = NULL;
1030 } else {
1031 tv.tv_sec = deadline;
1032 tv.tv_usec = 0;
1033 *tvpp = &tv;
1034 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001035 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036}
1037
Ben Lindstrombba81212001-06-25 05:01:22 +00001038static void
Damien Miller95def091999-11-25 00:26:21 +11001039after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040{
Ben Lindstrom822b6342002-06-23 21:38:49 +00001041 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +11001042 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +11001043 char buf[1024];
markus@openbsd.org139ca812015-01-14 13:09:09 +00001044 int len, sock, r;
Darren Tucker72473c62009-10-07 09:01:03 +11001045 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001046 uid_t euid;
1047 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048
Darren Tucker72473c62009-10-07 09:01:03 +11001049 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001050 switch (sockets[i].type) {
1051 case AUTH_UNUSED:
1052 break;
1053 case AUTH_SOCKET:
1054 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001055 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001056 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001057 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001058 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001059 error("accept from AUTH_SOCKET: %s",
1060 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001061 break;
1062 }
Damien Miller538f1812002-09-12 09:51:10 +10001063 if (getpeereid(sock, &euid, &egid) < 0) {
1064 error("getpeereid %d failed: %s",
1065 sock, strerror(errno));
1066 close(sock);
1067 break;
1068 }
Damien Milleraf9de382002-10-03 11:54:35 +10001069 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001070 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001071 "peer euid %u != uid %u",
1072 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001073 close(sock);
1074 break;
1075 }
Damien Miller95def091999-11-25 00:26:21 +11001076 new_socket(AUTH_CONNECTION, sock);
1077 }
1078 break;
1079 case AUTH_CONNECTION:
markus@openbsd.org139ca812015-01-14 13:09:09 +00001080 if (sshbuf_len(sockets[i].output) > 0 &&
Damien Miller95def091999-11-25 00:26:21 +11001081 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001082 len = write(sockets[i].fd,
markus@openbsd.org139ca812015-01-14 13:09:09 +00001083 sshbuf_ptr(sockets[i].output),
1084 sshbuf_len(sockets[i].output));
Darren Tucker72473c62009-10-07 09:01:03 +11001085 if (len == -1 && (errno == EAGAIN ||
1086 errno == EWOULDBLOCK ||
1087 errno == EINTR))
1088 continue;
Damien Miller95def091999-11-25 00:26:21 +11001089 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001090 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001091 break;
1092 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001093 if ((r = sshbuf_consume(sockets[i].output,
1094 len)) != 0)
1095 fatal("%s: buffer error: %s",
1096 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001097 }
1098 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001099 len = read(sockets[i].fd, buf, sizeof(buf));
1100 if (len == -1 && (errno == EAGAIN ||
1101 errno == EWOULDBLOCK ||
1102 errno == EINTR))
1103 continue;
Damien Miller95def091999-11-25 00:26:21 +11001104 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001105 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001106 break;
1107 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001108 if ((r = sshbuf_put(sockets[i].input,
1109 buf, len)) != 0)
1110 fatal("%s: buffer error: %s",
1111 __func__, ssh_err(r));
Damien Millerf4977942014-07-30 12:32:46 +10001112 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001113 process_message(&sockets[i]);
1114 }
1115 break;
1116 default:
1117 fatal("Unknown type %d", sockets[i].type);
1118 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119}
1120
Ben Lindstrombba81212001-06-25 05:01:22 +00001121static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001122cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001123{
Damien Millerb1e967c2014-07-03 21:22:40 +10001124 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1125 return;
1126 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001127 if (socket_name[0])
1128 unlink(socket_name);
1129 if (socket_dir[0])
1130 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131}
1132
Darren Tucker3e33cec2003-10-02 16:12:36 +10001133void
Damien Miller792c5111999-10-29 09:47:09 +10001134cleanup_exit(int i)
1135{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001136 cleanup_socket();
1137 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001138}
1139
Damien Miller1c13bd82006-03-26 14:28:14 +11001140/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001141static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001142cleanup_handler(int sig)
1143{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001144 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001145#ifdef ENABLE_PKCS11
1146 pkcs11_terminate();
1147#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001148 _exit(2);
1149}
1150
Ben Lindstrombba81212001-06-25 05:01:22 +00001151static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001152check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001153{
Darren Tucker3e78a512011-06-03 14:14:16 +10001154 /*
1155 * If our parent has exited then getppid() will return (pid_t)1,
1156 * so testing for that should be safe.
1157 */
1158 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001159 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001160 cleanup_socket();
1161 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001162 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001163}
1164
1165static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001166usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001167{
Damien Millerf0858de2014-04-20 13:01:30 +10001168 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001169 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001170 " [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001171 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001172 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001173}
1174
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175int
1176main(int ac, char **av)
1177{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001178 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001179 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001180 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001181 char *shell, *format, *pidstr, *agentsocket = NULL;
1182 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001183#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001184 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001185#endif
Damien Miller615f9392000-05-17 22:53:33 +10001186 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001187 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001188 pid_t pid;
1189 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001190 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001191 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001192 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001193
Darren Tuckerce321d82005-10-03 18:11:24 +10001194 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1195 sanitise_stdfd();
1196
Damien Miller6cffb9a2002-09-04 16:20:26 +10001197 /* drop */
1198 setegid(getgid());
1199 setgid(getgid());
1200
Damien Miller6c4914a2004-03-03 11:08:59 +11001201#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1202 /* Disable ptrace on Linux without sgid bit */
1203 prctl(PR_SET_DUMPABLE, 0);
1204#endif
1205
Damien Miller1f0311c2014-05-15 14:24:09 +10001206#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001207 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001208#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001209
Damien Miller59d3d5b2003-08-22 09:34:41 +10001210 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001211 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001212
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001213 while ((ch = getopt(ac, av, "cDdksE:a:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001214 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001215 case 'E':
1216 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1217 if (fingerprint_hash == -1)
1218 fatal("Invalid hash algorithm \"%s\"", optarg);
1219 break;
Damien Miller95def091999-11-25 00:26:21 +11001220 case 'c':
1221 if (s_flag)
1222 usage();
1223 c_flag++;
1224 break;
1225 case 'k':
1226 k_flag++;
1227 break;
1228 case 's':
1229 if (c_flag)
1230 usage();
1231 s_flag++;
1232 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001233 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001234 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001235 usage();
1236 d_flag++;
1237 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001238 case 'D':
1239 if (d_flag || D_flag)
1240 usage();
1241 D_flag++;
1242 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001243 case 'a':
1244 agentsocket = optarg;
1245 break;
Damien Miller53d81482003-01-22 11:47:19 +11001246 case 't':
1247 if ((lifetime = convtime(optarg)) == -1) {
1248 fprintf(stderr, "Invalid lifetime\n");
1249 usage();
1250 }
1251 break;
Damien Miller95def091999-11-25 00:26:21 +11001252 default:
1253 usage();
1254 }
Damien Miller792c5111999-10-29 09:47:09 +10001255 }
Damien Miller95def091999-11-25 00:26:21 +11001256 ac -= optind;
1257 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001258
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001259 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001260 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001261
Ben Lindstromeecdf232002-04-02 21:03:51 +00001262 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001263 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001264 if (shell != NULL && (len = strlen(shell)) > 2 &&
1265 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001266 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001267 }
Damien Miller95def091999-11-25 00:26:21 +11001268 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001269 const char *errstr = NULL;
1270
Damien Miller95def091999-11-25 00:26:21 +11001271 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1272 if (pidstr == NULL) {
1273 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001274 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001275 exit(1);
1276 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001277 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1278 if (errstr) {
1279 fprintf(stderr,
1280 "%s=\"%s\", which is not a good PID: %s\n",
1281 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001282 exit(1);
1283 }
1284 if (kill(pid, SIGTERM) == -1) {
1285 perror("kill");
1286 exit(1);
1287 }
1288 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1289 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1290 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001291 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001292 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001293 }
Damien Miller95def091999-11-25 00:26:21 +11001294 parent_pid = getpid();
1295
Ben Lindstromb7788f32002-06-06 21:46:08 +00001296 if (agentsocket == NULL) {
1297 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001298 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001299 if (mkdtemp(socket_dir) == NULL) {
1300 perror("mkdtemp: private socket dir");
1301 exit(1);
1302 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001303 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1304 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001305 } else {
1306 /* Try to use specified agent socket */
1307 socket_dir[0] = '\0';
1308 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001309 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310
Damien Miller5428f641999-11-25 11:54:57 +11001311 /*
1312 * Create socket early so it will exist before command gets run from
1313 * the parent.
1314 */
Damien Millerab2ec582014-07-18 15:04:47 +10001315 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001316 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001317 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001318 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001319 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001320 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001321 }
Damien Millerab2ec582014-07-18 15:04:47 +10001322 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001323
Damien Miller5428f641999-11-25 11:54:57 +11001324 /*
1325 * Fork, and have the parent execute the command, if any, or present
1326 * the socket data. The child continues as the authentication agent.
1327 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001328 if (D_flag || d_flag) {
1329 log_init(__progname,
1330 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1331 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001332 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1333 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1334 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001335 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001336 goto skip;
1337 }
Damien Miller95def091999-11-25 00:26:21 +11001338 pid = fork();
1339 if (pid == -1) {
1340 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001341 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001342 }
1343 if (pid != 0) { /* Parent - execute the given command. */
1344 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001345 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001346 if (ac == 0) {
1347 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1348 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001349 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001350 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001351 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001352 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001353 exit(0);
1354 }
Damien Millere4340be2000-09-16 13:29:08 +11001355 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1356 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1357 perror("setenv");
1358 exit(1);
1359 }
Damien Miller95def091999-11-25 00:26:21 +11001360 execvp(av[0], av);
1361 perror(av[0]);
1362 exit(1);
1363 }
Damien Millerc653b892002-02-08 22:05:41 +11001364 /* child */
1365 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001366
1367 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001368 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001369 cleanup_exit(1);
1370 }
1371
1372 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001373 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1374 /* XXX might close listen socket */
1375 (void)dup2(fd, STDIN_FILENO);
1376 (void)dup2(fd, STDOUT_FILENO);
1377 (void)dup2(fd, STDERR_FILENO);
1378 if (fd > 2)
1379 close(fd);
1380 }
Damien Miller95def091999-11-25 00:26:21 +11001381
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001382#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001383 /* deny core dumps, since memory contains unencrypted private keys */
1384 rlim.rlim_cur = rlim.rlim_max = 0;
1385 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001386 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001387 cleanup_exit(1);
1388 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001389#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001390
1391skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001392
Damien Millerb1e967c2014-07-03 21:22:40 +10001393 cleanup_pid = getpid();
1394
Damien Miller7ea845e2010-02-12 09:21:02 +11001395#ifdef ENABLE_PKCS11
1396 pkcs11_init(0);
1397#endif
Damien Miller95def091999-11-25 00:26:21 +11001398 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001399 if (ac > 0)
1400 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001401 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001402 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001403 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001404 signal(SIGHUP, cleanup_handler);
1405 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001406 nalloc = 0;
1407
Damien Miller95def091999-11-25 00:26:21 +11001408 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001409 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1410 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001411 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001412 if (parent_alive_interval != 0)
1413 check_parent_exists();
1414 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001415 if (result < 0) {
1416 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001417 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001418 fatal("select: %s", strerror(saved_errno));
1419 } else if (result > 0)
1420 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001421 }
1422 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423}