blob: c38906d94278e2388cabb621472b01d7ca88a00e [file] [log] [blame]
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001/* $OpenBSD: ssh-agent.c,v 1.212 2016/02/15 09:47:49 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
Damien Millerd7834352006-08-05 12:39:39 +100075#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "ssh.h"
77#include "rsa.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000078#include "sshbuf.h"
79#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100080#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110081#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110083#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110084#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000085#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller7ea845e2010-02-12 09:21:02 +110087#ifdef ENABLE_PKCS11
88#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000089#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000090
Damien Miller6c4914a2004-03-03 11:08:59 +110091#if defined(HAVE_SYS_PRCTL_H)
92#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
93#endif
94
Ben Lindstrom65366a82001-12-06 16:32:47 +000095typedef enum {
96 AUTH_UNUSED,
97 AUTH_SOCKET,
98 AUTH_CONNECTION
99} sock_type;
100
Damien Miller95def091999-11-25 00:26:21 +1100101typedef struct {
102 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000103 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000104 struct sshbuf *input;
105 struct sshbuf *output;
106 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} SocketEntry;
108
Ben Lindstrom46c16222000-12-22 01:43:59 +0000109u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110SocketEntry *sockets = NULL;
111
Damien Miller1a534ae2002-01-22 23:26:13 +1100112typedef struct identity {
113 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000114 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100115 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100116 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000117 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100118 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119} Identity;
120
Damien Millerad833b32000-08-23 10:46:23 +1000121typedef struct {
122 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100123 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000124} Idtab;
125
126/* private key table, one per protocol version */
127Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129int max_fd = 0;
130
131/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000132pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000133time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Millerb1e967c2014-07-03 21:22:40 +1000135/* pid of process for which cleanup_socket is applicable */
136pid_t cleanup_pid = 0;
137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000139char socket_name[PATH_MAX];
140char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Ben Lindstrom2f717042002-06-06 21:52:03 +0000142/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000143#define LOCK_SIZE 32
144#define LOCK_SALT_SIZE 16
145#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000146int locked = 0;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000147char lock_passwd[LOCK_SIZE];
148char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000149
Damien Miller95def091999-11-25 00:26:21 +1100150extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100151
Darren Tucker55119252013-06-02 07:43:59 +1000152/* Default lifetime in seconds (0 == forever) */
153static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100154
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000155static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
156
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static void
Damien Miller58f34862002-09-04 16:31:21 +1000158close_socket(SocketEntry *e)
159{
Damien Miller58f34862002-09-04 16:31:21 +1000160 close(e->fd);
161 e->fd = -1;
162 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000163 sshbuf_free(e->input);
164 sshbuf_free(e->output);
165 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000166}
167
168static void
Damien Millerad833b32000-08-23 10:46:23 +1000169idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Millerad833b32000-08-23 10:46:23 +1000171 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000172
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000173 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100174 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000175 idtable[i].nentries = 0;
176 }
177}
178
179/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000180static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000181idtab_lookup(int version)
182{
183 if (version < 1 || version > 2)
184 fatal("internal error, bad protocol version %d", version);
185 return &idtable[version];
186}
187
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000188static void
189free_identity(Identity *id)
190{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000191 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000192 free(id->provider);
193 free(id->comment);
194 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000195}
196
Damien Millerad833b32000-08-23 10:46:23 +1000197/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100198static Identity *
markus@openbsd.org139ca812015-01-14 13:09:09 +0000199lookup_identity(struct sshkey *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000200{
Damien Miller1a534ae2002-01-22 23:26:13 +1100201 Identity *id;
202
Damien Millerad833b32000-08-23 10:46:23 +1000203 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100204 TAILQ_FOREACH(id, &tab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000205 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100206 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000207 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100208 return (NULL);
209}
210
Damien Miller6c711792003-01-24 11:36:23 +1100211/* Check confirmation of keysign request */
212static int
213confirm_key(Identity *id)
214{
Darren Tuckerce327b62004-11-05 20:38:03 +1100215 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100216 int ret = -1;
217
markus@openbsd.org139ca812015-01-14 13:09:09 +0000218 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000219 if (p != NULL &&
220 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100221 id->comment, p))
222 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000223 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100224
Damien Miller6c711792003-01-24 11:36:23 +1100225 return (ret);
226}
227
markus@openbsd.org139ca812015-01-14 13:09:09 +0000228static void
229send_status(SocketEntry *e, int success)
230{
231 int r;
232
233 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
234 (r = sshbuf_put_u8(e->output, success ?
235 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
236 fatal("%s: buffer error: %s", __func__, ssh_err(r));
237}
238
Damien Millerad833b32000-08-23 10:46:23 +1000239/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
Damien Millerad833b32000-08-23 10:46:23 +1000241process_request_identities(SocketEntry *e, int version)
242{
243 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100244 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000245 struct sshbuf *msg;
246 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
markus@openbsd.org139ca812015-01-14 13:09:09 +0000248 if ((msg = sshbuf_new()) == NULL)
249 fatal("%s: sshbuf_new failed", __func__);
250 if ((r = sshbuf_put_u8(msg, (version == 1) ?
251 SSH_AGENT_RSA_IDENTITIES_ANSWER :
252 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
253 (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
254 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1a534ae2002-01-22 23:26:13 +1100255 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100256 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000257#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000258 if ((r = sshbuf_put_u32(msg,
259 BN_num_bits(id->key->rsa->n))) != 0 ||
260 (r = sshbuf_put_bignum1(msg,
261 id->key->rsa->e)) != 0 ||
262 (r = sshbuf_put_bignum1(msg,
263 id->key->rsa->n)) != 0)
264 fatal("%s: buffer error: %s",
265 __func__, ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000266#endif
Damien Millerad833b32000-08-23 10:46:23 +1000267 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000268 u_char *blob;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000269 size_t blen;
270
271 if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
272 error("%s: sshkey_to_blob: %s", __func__,
273 ssh_err(r));
274 continue;
275 }
276 if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
277 fatal("%s: buffer error: %s",
278 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000279 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000280 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000281 if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
282 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100283 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000284 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
285 fatal("%s: buffer error: %s", __func__, ssh_err(r));
286 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000287}
288
Damien Miller1f0311c2014-05-15 14:24:09 +1000289#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000290/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static void
Damien Millerad833b32000-08-23 10:46:23 +1000292process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000294 u_char buf[32], mdbuf[16], session_id[16];
295 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000296 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000297 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000298 int r, len;
299 struct sshbuf *msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100300 struct ssh_digest_ctx *md;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000301 struct sshkey *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
markus@openbsd.org139ca812015-01-14 13:09:09 +0000303 if ((msg = sshbuf_new()) == NULL)
304 fatal("%s: sshbuf_new failed", __func__);
305 if ((key = sshkey_new(KEY_RSA1)) == NULL)
306 fatal("%s: sshkey_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100307 if ((challenge = BN_new()) == NULL)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000308 fatal("%s: BN_new failed", __func__);
Damien Millerad833b32000-08-23 10:46:23 +1000309
markus@openbsd.org139ca812015-01-14 13:09:09 +0000310 if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
311 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
312 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
313 (r = sshbuf_get_bignum1(e->request, challenge)))
314 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
Damien Millerad833b32000-08-23 10:46:23 +1000316 /* Only protocol 1.1 is supported */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000317 if (sshbuf_len(e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000318 goto failure;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000319 if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
320 (r = sshbuf_get_u32(e->request, &response_type)) != 0)
321 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000322 if (response_type != 1)
323 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller1a534ae2002-01-22 23:26:13 +1100325 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100326 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000327 struct sshkey *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000328 /* Decrypt the challenge using the private key. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000329 if ((r = rsa_private_decrypt(challenge, challenge,
330 private->rsa) != 0)) {
331 fatal("%s: rsa_public_encrypt: %s", __func__,
332 ssh_err(r));
333 goto failure; /* XXX ? */
334 }
Damien Millerfd7c9111999-11-08 16:15:55 +1100335
markus@openbsd.org139ca812015-01-14 13:09:09 +0000336 /* The response is MD5 of decrypted challenge plus session id */
Damien Millerad833b32000-08-23 10:46:23 +1000337 len = BN_num_bytes(challenge);
338 if (len <= 0 || len > 32) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000339 logit("%s: bad challenge length %d", __func__, len);
Damien Millerad833b32000-08-23 10:46:23 +1000340 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100341 }
Damien Millerad833b32000-08-23 10:46:23 +1000342 memset(buf, 0, 32);
343 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100344 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
345 ssh_digest_update(md, buf, 32) < 0 ||
346 ssh_digest_update(md, session_id, 16) < 0 ||
347 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
348 fatal("%s: md5 failed", __func__);
349 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000350
351 /* Send the response. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000352 if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
353 (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
354 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000355 goto send;
356 }
357
markus@openbsd.org139ca812015-01-14 13:09:09 +0000358 failure:
Damien Millerad833b32000-08-23 10:46:23 +1000359 /* Unknown identity or protocol error. Send failure. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000360 if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
361 fatal("%s: buffer error: %s", __func__, ssh_err(r));
362 send:
363 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
364 fatal("%s: buffer error: %s", __func__, ssh_err(r));
365 sshkey_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100366 BN_clear_free(challenge);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000367 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368}
Damien Miller1f0311c2014-05-15 14:24:09 +1000369#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000371static char *
372agent_decode_alg(struct sshkey *key, u_int flags)
373{
374 if (key->type == KEY_RSA) {
375 if (flags & SSH_AGENT_RSA_SHA2_256)
376 return "rsa-sha2-256";
377 else if (flags & SSH_AGENT_RSA_SHA2_512)
378 return "rsa-sha2-512";
379 }
380 return NULL;
381}
382
Damien Millerad833b32000-08-23 10:46:23 +1000383/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000384static void
Damien Millerad833b32000-08-23 10:46:23 +1000385process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000387 u_char *blob, *data, *signature = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000388 size_t blen, dlen, slen = 0;
389 u_int compat = 0, flags;
390 int r, ok = -1;
391 struct sshbuf *msg;
392 struct sshkey *key;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000393 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000394
djm@openbsd.org0088c572015-01-14 19:33:41 +0000395 if ((msg = sshbuf_new()) == NULL)
396 fatal("%s: sshbuf_new failed", __func__);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000397 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
398 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
399 (r = sshbuf_get_u32(e->request, &flags)) != 0)
400 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100401 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000402 compat = SSH_BUG_SIGBLOB;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000403 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000404 error("%s: cannot parse key blob: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000405 goto send;
Damien Millerad833b32000-08-23 10:46:23 +1000406 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000407 if ((id = lookup_identity(key, 2)) == NULL) {
408 verbose("%s: %s key not found", __func__, sshkey_type(key));
409 goto send;
410 }
411 if (id->confirm && confirm_key(id) != 0) {
412 verbose("%s: user refused key", __func__);
413 goto send;
414 }
415 if ((r = sshkey_sign(id->key, &signature, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000416 data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000417 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000418 goto send;
419 }
420 /* Success */
421 ok = 0;
422 send:
423 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000424 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000425 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
426 (r = sshbuf_put_string(msg, signature, slen)) != 0)
427 fatal("%s: buffer error: %s", __func__, ssh_err(r));
428 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
429 fatal("%s: buffer error: %s", __func__, ssh_err(r));
430
431 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
432 fatal("%s: buffer error: %s", __func__, ssh_err(r));
433
434 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000435 free(data);
436 free(blob);
437 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000438}
439
440/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000441static void
Damien Millerad833b32000-08-23 10:46:23 +1000442process_remove_identity(SocketEntry *e, int version)
443{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000444 size_t blen;
445 int r, success = 0;
446 struct sshkey *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000448#ifdef WITH_SSH1
449 u_int bits;
450#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100451
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000452 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000453#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000454 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000455 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
456 error("%s: sshkey_new failed", __func__);
457 return;
458 }
459 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
460 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
461 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
462 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000463
markus@openbsd.org139ca812015-01-14 13:09:09 +0000464 if (bits != sshkey_size(key))
465 logit("Warning: identity keysize mismatch: "
466 "actual %u, announced %u",
467 sshkey_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000468 break;
Damien Miller86687062014-07-02 15:28:02 +1000469#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000470 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000471 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
472 fatal("%s: buffer error: %s", __func__, ssh_err(r));
473 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
474 error("%s: sshkey_from_blob failed: %s",
475 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000476 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000477 break;
478 }
479 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100480 Identity *id = lookup_identity(key, version);
481 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100482 /*
483 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100484 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000485 * the array, we actually free the key there and move
486 * all the entries between the empty slot and the end
487 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100488 */
Damien Millerad833b32000-08-23 10:46:23 +1000489 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 if (tab->nentries < 1)
491 fatal("process_remove_identity: "
492 "internal error: tab->nentries %d",
493 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100494 TAILQ_REMOVE(&tab->idlist, id, next);
495 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000496 tab->nentries--;
497 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100498 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000499 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000500 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000501 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502}
503
Ben Lindstrombba81212001-06-25 05:01:22 +0000504static void
Damien Millerad833b32000-08-23 10:46:23 +1000505process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506{
Damien Millerad833b32000-08-23 10:46:23 +1000507 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100508 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509
Damien Miller95def091999-11-25 00:26:21 +1100510 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100511 for (id = TAILQ_FIRST(&tab->idlist); id;
512 id = TAILQ_FIRST(&tab->idlist)) {
513 TAILQ_REMOVE(&tab->idlist, id, next);
514 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100515 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Damien Miller95def091999-11-25 00:26:21 +1100517 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000518 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
520 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000521 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100522}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Darren Tucker2812dc92007-03-21 20:45:06 +1100524/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000525static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000526reaper(void)
527{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000528 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000529 Identity *id, *nxt;
530 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000531 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532
533 for (version = 1; version < 3; version++) {
534 tab = idtab_lookup(version);
535 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
536 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100537 if (id->death == 0)
538 continue;
539 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100540 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000541 TAILQ_REMOVE(&tab->idlist, id, next);
542 free_identity(id);
543 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100544 } else
545 deadline = (deadline == 0) ? id->death :
546 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000547 }
548 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100549 if (deadline == 0 || deadline <= now)
550 return 0;
551 else
552 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000553}
554
markus@openbsd.org139ca812015-01-14 13:09:09 +0000555/*
556 * XXX this and the corresponding serialisation function probably belongs
557 * in key.c
558 */
djm@openbsd.org111dfb22015-03-03 21:21:13 +0000559#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000560static int
561agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
562{
563 struct sshkey *k = NULL;
564 int r = SSH_ERR_INTERNAL_ERROR;
565
566 *kp = NULL;
567 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
568 return SSH_ERR_ALLOC_FAIL;
569
570 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
571 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
572 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
573 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
574 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
575 /* SSH1 and SSL have p and q swapped */
576 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
577 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
578 goto out;
579
580 /* Generate additional parameters */
581 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
582 goto out;
583 /* enable blinding */
584 if (RSA_blinding_on(k->rsa, NULL) != 1) {
585 r = SSH_ERR_LIBCRYPTO_ERROR;
586 goto out;
587 }
588
589 r = 0; /* success */
590 out:
591 if (r == 0)
592 *kp = k;
593 else
594 sshkey_free(k);
595 return r;
596}
Damien Miller6c203922015-03-03 13:48:48 -0800597#endif /* WITH_SSH1 */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000598
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000599static void
Damien Millerad833b32000-08-23 10:46:23 +1000600process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100601{
Damien Millerad833b32000-08-23 10:46:23 +1000602 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000603 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000604 int success = 0, confirm = 0;
605 u_int seconds;
606 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000607 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000608 struct sshkey *k = NULL;
609 u_char ctype;
610 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100611
Damien Millerad833b32000-08-23 10:46:23 +1000612 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000613#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000614 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000615 r = agent_decode_rsa1(e->request, &k);
Damien Millerc51d0732003-03-15 11:37:09 +1100616 break;
Damien Miller86687062014-07-02 15:28:02 +1000617#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100618 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000619 r = sshkey_private_deserialize(e->request, &k);
Damien Millerf0e90602013-12-07 10:40:26 +1100620 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100621 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000622 if (r != 0 || k == NULL ||
623 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
624 error("%s: decode private key: %s", __func__, ssh_err(r));
625 goto err;
626 }
Damien Miller86687062014-07-02 15:28:02 +1000627
markus@openbsd.org139ca812015-01-14 13:09:09 +0000628 while (sshbuf_len(e->request)) {
629 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
630 error("%s: buffer error: %s", __func__, ssh_err(r));
631 goto err;
632 }
633 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000634 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000635 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
636 error("%s: bad lifetime constraint: %s",
637 __func__, ssh_err(r));
638 goto err;
639 }
640 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000641 break;
Damien Miller6c711792003-01-24 11:36:23 +1100642 case SSH_AGENT_CONSTRAIN_CONFIRM:
643 confirm = 1;
644 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000645 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000646 error("%s: Unknown constraint %d", __func__, ctype);
647 err:
648 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000649 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000650 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000651 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000652 }
653 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000654
Damien Miller1cfadab2008-06-30 00:05:21 +1000655 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100656 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000657 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000658 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100659 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000660 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000661 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
662 /* Increment the number of identities. */
663 tab->nentries++;
664 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000665 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000666 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000667 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000668 id->comment = comment;
669 id->death = death;
670 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000671send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000672 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000673}
674
Ben Lindstrom2f717042002-06-06 21:52:03 +0000675/* XXX todo: encrypt sensitive data with passphrase */
676static void
677process_lock_agent(SocketEntry *e, int lock)
678{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000679 int r, success = 0, delay;
680 char *passwd, passwdhash[LOCK_SIZE];
681 static u_int fail_count = 0;
682 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000683
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000684 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000685 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000686 if (pwlen == 0) {
687 debug("empty password not supported");
688 } else if (locked && !lock) {
689 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
690 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
691 fatal("bcrypt_pbkdf");
692 if (timingsafe_bcmp(passwdhash, lock_passwd, LOCK_SIZE) == 0) {
693 debug("agent unlocked");
694 locked = 0;
695 fail_count = 0;
696 explicit_bzero(lock_passwd, sizeof(lock_passwd));
697 success = 1;
698 } else {
699 /* delay in 0.1s increments up to 10s */
700 if (fail_count < 100)
701 fail_count++;
702 delay = 100000 * fail_count;
703 debug("unlock failed, delaying %0.1lf seconds",
704 (double)delay/1000000);
705 usleep(delay);
706 }
707 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000708 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000709 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000710 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000711 arc4random_buf(lock_salt, sizeof(lock_salt));
712 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
713 lock_passwd, sizeof(lock_passwd), LOCK_ROUNDS) < 0)
714 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000715 success = 1;
716 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000717 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000718 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000719 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000720}
721
722static void
723no_identities(SocketEntry *e, u_int type)
724{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000725 struct sshbuf *msg;
726 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000727
markus@openbsd.org139ca812015-01-14 13:09:09 +0000728 if ((msg = sshbuf_new()) == NULL)
729 fatal("%s: sshbuf_new failed", __func__);
730 if ((r = sshbuf_put_u8(msg,
Ben Lindstrom2f717042002-06-06 21:52:03 +0000731 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
markus@openbsd.org139ca812015-01-14 13:09:09 +0000732 SSH_AGENT_RSA_IDENTITIES_ANSWER :
733 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
734 (r = sshbuf_put_u32(msg, 0)) != 0 ||
735 (r = sshbuf_put_stringb(e->output, msg)) != 0)
736 fatal("%s: buffer error: %s", __func__, ssh_err(r));
737 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000738}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000739
Damien Miller7ea845e2010-02-12 09:21:02 +1100740#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000741static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000742process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000743{
Damien Miller7ea845e2010-02-12 09:21:02 +1100744 char *provider = NULL, *pin;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000745 int r, i, version, count = 0, success = 0, confirm = 0;
746 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000747 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000748 u_char type;
749 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000750 Identity *id;
751 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100752
markus@openbsd.org139ca812015-01-14 13:09:09 +0000753 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
754 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
755 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000756
markus@openbsd.org139ca812015-01-14 13:09:09 +0000757 while (sshbuf_len(e->request)) {
758 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
760 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000761 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000762 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
763 fatal("%s: buffer error: %s",
764 __func__, ssh_err(r));
765 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000766 break;
767 case SSH_AGENT_CONSTRAIN_CONFIRM:
768 confirm = 1;
769 break;
770 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000771 error("process_add_smartcard_key: "
772 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000773 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000774 }
775 }
776 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000777 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000778
Damien Miller7ea845e2010-02-12 09:21:02 +1100779 count = pkcs11_add_provider(provider, pin, &keys);
780 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000781 k = keys[i];
782 version = k->type == KEY_RSA1 ? 1 : 2;
783 tab = idtab_lookup(version);
784 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100785 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000786 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100787 id->provider = xstrdup(provider);
788 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000789 id->death = death;
790 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000791 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
792 tab->nentries++;
793 success = 1;
794 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000795 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000796 }
797 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000798 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000799send:
Darren Tuckera627d422013-06-02 07:31:17 +1000800 free(pin);
801 free(provider);
802 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000803 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000804}
805
806static void
807process_remove_smartcard_key(SocketEntry *e)
808{
Damien Miller7ea845e2010-02-12 09:21:02 +1100809 char *provider = NULL, *pin = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000810 int r, version, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100811 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000812 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000813
markus@openbsd.org139ca812015-01-14 13:09:09 +0000814 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
815 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
816 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000817 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000818
Damien Miller7ea845e2010-02-12 09:21:02 +1100819 for (version = 1; version < 3; version++) {
820 tab = idtab_lookup(version);
821 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
822 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100823 /* Skip file--based keys */
824 if (id->provider == NULL)
825 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100826 if (!strcmp(provider, id->provider)) {
827 TAILQ_REMOVE(&tab->idlist, id, next);
828 free_identity(id);
829 tab->nentries--;
830 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000831 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000832 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100833 if (pkcs11_del_provider(provider) == 0)
834 success = 1;
835 else
836 error("process_remove_smartcard_key:"
837 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000838 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000839 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000840}
Damien Miller7ea845e2010-02-12 09:21:02 +1100841#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000842
Damien Millerad833b32000-08-23 10:46:23 +1000843/* dispatch incoming messages */
844
Ben Lindstrombba81212001-06-25 05:01:22 +0000845static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000846process_message(SocketEntry *e)
847{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000848 u_int msg_len;
849 u_char type;
850 const u_char *cp;
851 int r;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000852
markus@openbsd.org139ca812015-01-14 13:09:09 +0000853 if (sshbuf_len(e->input) < 5)
Damien Miller95def091999-11-25 00:26:21 +1100854 return; /* Incomplete message. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000855 cp = sshbuf_ptr(e->input);
856 msg_len = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100857 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000858 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100859 return;
860 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000861 if (sshbuf_len(e->input) < msg_len + 4)
Damien Miller95def091999-11-25 00:26:21 +1100862 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000863
864 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000865 sshbuf_reset(e->request);
866 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
867 (r = sshbuf_get_u8(e->request, &type)) != 0)
868 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869
Ben Lindstrom2f717042002-06-06 21:52:03 +0000870 /* check wheter agent is locked */
871 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000872 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000873 switch (type) {
874 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
875 case SSH2_AGENTC_REQUEST_IDENTITIES:
876 /* send empty lists */
877 no_identities(e, type);
878 break;
879 default:
880 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000881 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000882 }
883 return;
884 }
885
Ben Lindstrom3f471632001-07-04 03:53:15 +0000886 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100887 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000888 case SSH_AGENTC_LOCK:
889 case SSH_AGENTC_UNLOCK:
890 process_lock_agent(e, type == SSH_AGENTC_LOCK);
891 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000892#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000893 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100894 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000895 process_authentication_challenge1(e);
896 break;
897 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
898 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100899 break;
900 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000901 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000902 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100903 break;
904 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000905 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100906 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000907#endif
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000908 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
909 process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
910 break;
Damien Millerad833b32000-08-23 10:46:23 +1000911 /* ssh2 */
912 case SSH2_AGENTC_SIGN_REQUEST:
913 process_sign_request2(e);
914 break;
915 case SSH2_AGENTC_REQUEST_IDENTITIES:
916 process_request_identities(e, 2);
917 break;
918 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000919 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000920 process_add_identity(e, 2);
921 break;
922 case SSH2_AGENTC_REMOVE_IDENTITY:
923 process_remove_identity(e, 2);
924 break;
925 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
926 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100927 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100928#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000929 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000930 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000931 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100932 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000933 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
934 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100935 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100936#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100937 default:
938 /* Unknown message. Respond with failure. */
939 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000940 sshbuf_reset(e->request);
941 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100942 break;
943 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944}
945
Ben Lindstrombba81212001-06-25 05:01:22 +0000946static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000947new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000949 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000950
Damien Miller232711f2004-06-15 10:35:30 +1000951 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952
Damien Miller95def091999-11-25 00:26:21 +1100953 if (fd > max_fd)
954 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955
Damien Miller95def091999-11-25 00:26:21 +1100956 for (i = 0; i < sockets_alloc; i++)
957 if (sockets[i].type == AUTH_UNUSED) {
958 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000959 if ((sockets[i].input = sshbuf_new()) == NULL)
960 fatal("%s: sshbuf_new failed", __func__);
961 if ((sockets[i].output = sshbuf_new()) == NULL)
962 fatal("%s: sshbuf_new failed", __func__);
963 if ((sockets[i].request = sshbuf_new()) == NULL)
964 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000965 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100966 return;
967 }
968 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000969 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000970 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000971 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100972 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000973 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100974 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000975 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
976 fatal("%s: sshbuf_new failed", __func__);
977 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
978 fatal("%s: sshbuf_new failed", __func__);
979 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
980 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000981 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982}
983
Ben Lindstrombba81212001-06-25 05:01:22 +0000984static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100985prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
986 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987{
Darren Tucker55119252013-06-02 07:43:59 +1000988 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000989 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100990 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000991 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000992
993 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100994 switch (sockets[i].type) {
995 case AUTH_SOCKET:
996 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000997 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100998 break;
999 case AUTH_UNUSED:
1000 break;
1001 default:
1002 fatal("Unknown socket type %d", sockets[i].type);
1003 break;
1004 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001005 }
1006
1007 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001008 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +10001009 free(*fdrp);
1010 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001011 *fdrp = xmalloc(sz);
1012 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001013 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001014 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001015 if (n < *fdl)
1016 debug("XXX shrink: %d < %d", n, *fdl);
1017 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001018 memset(*fdrp, 0, sz);
1019 memset(*fdwp, 0, sz);
1020
1021 for (i = 0; i < sockets_alloc; i++) {
1022 switch (sockets[i].type) {
1023 case AUTH_SOCKET:
1024 case AUTH_CONNECTION:
1025 FD_SET(sockets[i].fd, *fdrp);
markus@openbsd.org139ca812015-01-14 13:09:09 +00001026 if (sshbuf_len(sockets[i].output) > 0)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001027 FD_SET(sockets[i].fd, *fdwp);
1028 break;
1029 default:
1030 break;
1031 }
1032 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001033 deadline = reaper();
1034 if (parent_alive_interval != 0)
1035 deadline = (deadline == 0) ? parent_alive_interval :
1036 MIN(deadline, parent_alive_interval);
1037 if (deadline == 0) {
1038 *tvpp = NULL;
1039 } else {
1040 tv.tv_sec = deadline;
1041 tv.tv_usec = 0;
1042 *tvpp = &tv;
1043 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001044 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001045}
1046
Ben Lindstrombba81212001-06-25 05:01:22 +00001047static void
Damien Miller95def091999-11-25 00:26:21 +11001048after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001049{
Ben Lindstrom822b6342002-06-23 21:38:49 +00001050 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +11001051 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +11001052 char buf[1024];
markus@openbsd.org139ca812015-01-14 13:09:09 +00001053 int len, sock, r;
Darren Tucker72473c62009-10-07 09:01:03 +11001054 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001055 uid_t euid;
1056 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001057
Darren Tucker72473c62009-10-07 09:01:03 +11001058 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001059 switch (sockets[i].type) {
1060 case AUTH_UNUSED:
1061 break;
1062 case AUTH_SOCKET:
1063 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001064 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001065 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001066 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001067 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001068 error("accept from AUTH_SOCKET: %s",
1069 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001070 break;
1071 }
Damien Miller538f1812002-09-12 09:51:10 +10001072 if (getpeereid(sock, &euid, &egid) < 0) {
1073 error("getpeereid %d failed: %s",
1074 sock, strerror(errno));
1075 close(sock);
1076 break;
1077 }
Damien Milleraf9de382002-10-03 11:54:35 +10001078 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001079 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001080 "peer euid %u != uid %u",
1081 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001082 close(sock);
1083 break;
1084 }
Damien Miller95def091999-11-25 00:26:21 +11001085 new_socket(AUTH_CONNECTION, sock);
1086 }
1087 break;
1088 case AUTH_CONNECTION:
markus@openbsd.org139ca812015-01-14 13:09:09 +00001089 if (sshbuf_len(sockets[i].output) > 0 &&
Damien Miller95def091999-11-25 00:26:21 +11001090 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001091 len = write(sockets[i].fd,
markus@openbsd.org139ca812015-01-14 13:09:09 +00001092 sshbuf_ptr(sockets[i].output),
1093 sshbuf_len(sockets[i].output));
Darren Tucker72473c62009-10-07 09:01:03 +11001094 if (len == -1 && (errno == EAGAIN ||
1095 errno == EWOULDBLOCK ||
1096 errno == EINTR))
1097 continue;
Damien Miller95def091999-11-25 00:26:21 +11001098 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001099 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001100 break;
1101 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001102 if ((r = sshbuf_consume(sockets[i].output,
1103 len)) != 0)
1104 fatal("%s: buffer error: %s",
1105 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001106 }
1107 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001108 len = read(sockets[i].fd, buf, sizeof(buf));
1109 if (len == -1 && (errno == EAGAIN ||
1110 errno == EWOULDBLOCK ||
1111 errno == EINTR))
1112 continue;
Damien Miller95def091999-11-25 00:26:21 +11001113 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001114 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001115 break;
1116 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001117 if ((r = sshbuf_put(sockets[i].input,
1118 buf, len)) != 0)
1119 fatal("%s: buffer error: %s",
1120 __func__, ssh_err(r));
Damien Millerf4977942014-07-30 12:32:46 +10001121 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001122 process_message(&sockets[i]);
1123 }
1124 break;
1125 default:
1126 fatal("Unknown type %d", sockets[i].type);
1127 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128}
1129
Ben Lindstrombba81212001-06-25 05:01:22 +00001130static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001131cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001132{
Damien Millerb1e967c2014-07-03 21:22:40 +10001133 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1134 return;
1135 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001136 if (socket_name[0])
1137 unlink(socket_name);
1138 if (socket_dir[0])
1139 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140}
1141
Darren Tucker3e33cec2003-10-02 16:12:36 +10001142void
Damien Miller792c5111999-10-29 09:47:09 +10001143cleanup_exit(int i)
1144{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001145 cleanup_socket();
1146 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001147}
1148
Damien Miller1c13bd82006-03-26 14:28:14 +11001149/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001150static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001151cleanup_handler(int sig)
1152{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001153 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001154#ifdef ENABLE_PKCS11
1155 pkcs11_terminate();
1156#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001157 _exit(2);
1158}
1159
Ben Lindstrombba81212001-06-25 05:01:22 +00001160static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001161check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001162{
Darren Tucker3e78a512011-06-03 14:14:16 +10001163 /*
1164 * If our parent has exited then getppid() will return (pid_t)1,
1165 * so testing for that should be safe.
1166 */
1167 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001168 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001169 cleanup_socket();
1170 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001171 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001172}
1173
1174static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001175usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001176{
Damien Millerf0858de2014-04-20 13:01:30 +10001177 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001178 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001179 " [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001180 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001181 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001182}
1183
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001184int
1185main(int ac, char **av)
1186{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001187 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001188 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001189 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001190 char *shell, *format, *pidstr, *agentsocket = NULL;
1191 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001192#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001193 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001194#endif
Damien Miller615f9392000-05-17 22:53:33 +10001195 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001196 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001197 pid_t pid;
1198 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001199 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001200 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001201 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001202
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001203 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +10001204 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1205 sanitise_stdfd();
1206
Damien Miller6cffb9a2002-09-04 16:20:26 +10001207 /* drop */
1208 setegid(getgid());
1209 setgid(getgid());
1210
Damien Miller6c4914a2004-03-03 11:08:59 +11001211#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1212 /* Disable ptrace on Linux without sgid bit */
1213 prctl(PR_SET_DUMPABLE, 0);
1214#endif
1215
Damien Miller1f0311c2014-05-15 14:24:09 +10001216#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001217 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001218#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001219
Damien Miller59d3d5b2003-08-22 09:34:41 +10001220 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001221 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001222
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001223 while ((ch = getopt(ac, av, "cDdksE:a:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001224 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001225 case 'E':
1226 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1227 if (fingerprint_hash == -1)
1228 fatal("Invalid hash algorithm \"%s\"", optarg);
1229 break;
Damien Miller95def091999-11-25 00:26:21 +11001230 case 'c':
1231 if (s_flag)
1232 usage();
1233 c_flag++;
1234 break;
1235 case 'k':
1236 k_flag++;
1237 break;
1238 case 's':
1239 if (c_flag)
1240 usage();
1241 s_flag++;
1242 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001243 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001244 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001245 usage();
1246 d_flag++;
1247 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001248 case 'D':
1249 if (d_flag || D_flag)
1250 usage();
1251 D_flag++;
1252 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001253 case 'a':
1254 agentsocket = optarg;
1255 break;
Damien Miller53d81482003-01-22 11:47:19 +11001256 case 't':
1257 if ((lifetime = convtime(optarg)) == -1) {
1258 fprintf(stderr, "Invalid lifetime\n");
1259 usage();
1260 }
1261 break;
Damien Miller95def091999-11-25 00:26:21 +11001262 default:
1263 usage();
1264 }
Damien Miller792c5111999-10-29 09:47:09 +10001265 }
Damien Miller95def091999-11-25 00:26:21 +11001266 ac -= optind;
1267 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001268
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001269 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001270 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001271
Ben Lindstromeecdf232002-04-02 21:03:51 +00001272 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001273 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001274 if (shell != NULL && (len = strlen(shell)) > 2 &&
1275 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001276 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001277 }
Damien Miller95def091999-11-25 00:26:21 +11001278 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001279 const char *errstr = NULL;
1280
Damien Miller95def091999-11-25 00:26:21 +11001281 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1282 if (pidstr == NULL) {
1283 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001284 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001285 exit(1);
1286 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001287 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1288 if (errstr) {
1289 fprintf(stderr,
1290 "%s=\"%s\", which is not a good PID: %s\n",
1291 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001292 exit(1);
1293 }
1294 if (kill(pid, SIGTERM) == -1) {
1295 perror("kill");
1296 exit(1);
1297 }
1298 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1299 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1300 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001301 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001302 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001303 }
Damien Miller95def091999-11-25 00:26:21 +11001304 parent_pid = getpid();
1305
Ben Lindstromb7788f32002-06-06 21:46:08 +00001306 if (agentsocket == NULL) {
1307 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001308 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001309 if (mkdtemp(socket_dir) == NULL) {
1310 perror("mkdtemp: private socket dir");
1311 exit(1);
1312 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001313 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1314 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001315 } else {
1316 /* Try to use specified agent socket */
1317 socket_dir[0] = '\0';
1318 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001319 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001320
Damien Miller5428f641999-11-25 11:54:57 +11001321 /*
1322 * Create socket early so it will exist before command gets run from
1323 * the parent.
1324 */
Damien Millerab2ec582014-07-18 15:04:47 +10001325 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001326 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001327 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001328 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001329 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001330 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001331 }
Damien Millerab2ec582014-07-18 15:04:47 +10001332 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001333
Damien Miller5428f641999-11-25 11:54:57 +11001334 /*
1335 * Fork, and have the parent execute the command, if any, or present
1336 * the socket data. The child continues as the authentication agent.
1337 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001338 if (D_flag || d_flag) {
1339 log_init(__progname,
1340 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1341 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001342 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1343 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1344 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001345 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001346 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001347 goto skip;
1348 }
Damien Miller95def091999-11-25 00:26:21 +11001349 pid = fork();
1350 if (pid == -1) {
1351 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001352 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001353 }
1354 if (pid != 0) { /* Parent - execute the given command. */
1355 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001356 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001357 if (ac == 0) {
1358 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1359 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001360 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001361 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001362 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001363 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001364 exit(0);
1365 }
Damien Millere4340be2000-09-16 13:29:08 +11001366 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1367 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1368 perror("setenv");
1369 exit(1);
1370 }
Damien Miller95def091999-11-25 00:26:21 +11001371 execvp(av[0], av);
1372 perror(av[0]);
1373 exit(1);
1374 }
Damien Millerc653b892002-02-08 22:05:41 +11001375 /* child */
1376 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001377
1378 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001379 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001380 cleanup_exit(1);
1381 }
1382
1383 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001384 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1385 /* XXX might close listen socket */
1386 (void)dup2(fd, STDIN_FILENO);
1387 (void)dup2(fd, STDOUT_FILENO);
1388 (void)dup2(fd, STDERR_FILENO);
1389 if (fd > 2)
1390 close(fd);
1391 }
Damien Miller95def091999-11-25 00:26:21 +11001392
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001393#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001394 /* deny core dumps, since memory contains unencrypted private keys */
1395 rlim.rlim_cur = rlim.rlim_max = 0;
1396 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001397 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001398 cleanup_exit(1);
1399 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001400#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001401
1402skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001403
Damien Millerb1e967c2014-07-03 21:22:40 +10001404 cleanup_pid = getpid();
1405
Damien Miller7ea845e2010-02-12 09:21:02 +11001406#ifdef ENABLE_PKCS11
1407 pkcs11_init(0);
1408#endif
Damien Miller95def091999-11-25 00:26:21 +11001409 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001410 if (ac > 0)
1411 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001412 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001413 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001414 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001415 signal(SIGHUP, cleanup_handler);
1416 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001417 nalloc = 0;
1418
doug@openbsd.org43849a42015-12-11 17:41:37 +00001419 if (pledge("stdio cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001420 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001421 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001422
Damien Miller95def091999-11-25 00:26:21 +11001423 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001424 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1425 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001426 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001427 if (parent_alive_interval != 0)
1428 check_parent_exists();
1429 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001430 if (result < 0) {
1431 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001432 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001433 fatal("select: %s", strerror(saved_errno));
1434 } else if (result > 0)
1435 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001436 }
1437 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438}