blob: 1320cdaa162ee131a6f7ac140e24d45e64505ad8 [file] [log] [blame]
djm@openbsd.orgb108ce92017-01-04 02:21:43 +00001/* $OpenBSD: ssh-agent.c,v 1.216 2017/01/04 02:21:43 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Miller1f0311c2014-05-15 14:24:09 +100052#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100053#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100055#endif
Damien Millere3476ed2006-07-24 14:13:33 +100056
Darren Tucker39972492006-07-12 22:22:46 +100057#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100058#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000059#include <limits.h>
Damien Miller03e20032006-03-15 11:16:59 +110060#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110061# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110062#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110063#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100064#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100065#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100066#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100067#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100068#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100069#include <unistd.h>
Damien Millere97201f2015-05-21 17:55:15 +100070#ifdef HAVE_UTIL_H
71# include <util.h>
72#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110073
Damien Millerd7834352006-08-05 12:39:39 +100074#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "ssh.h"
76#include "rsa.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000077#include "sshbuf.h"
78#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100079#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110082#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110083#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000084#include "ssherr.h"
djm@openbsd.org786d5992016-11-30 03:07:37 +000085#include "match.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
djm@openbsd.org786d5992016-11-30 03:07:37 +000091#ifndef DEFAULT_PKCS11_WHITELIST
djm@openbsd.orgb108ce92017-01-04 02:21:43 +000092# define DEFAULT_PKCS11_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
djm@openbsd.org786d5992016-11-30 03:07:37 +000093#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
djm@openbsd.org786d5992016-11-30 03:07:37 +0000142/* PKCS#11 path whitelist */
143static char *pkcs11_whitelist;
144
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;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000150u_char lock_pwhash[LOCK_SIZE];
151u_char 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
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000374static char *
375agent_decode_alg(struct sshkey *key, u_int flags)
376{
377 if (key->type == KEY_RSA) {
378 if (flags & SSH_AGENT_RSA_SHA2_256)
379 return "rsa-sha2-256";
380 else if (flags & SSH_AGENT_RSA_SHA2_512)
381 return "rsa-sha2-512";
382 }
383 return NULL;
384}
385
Damien Millerad833b32000-08-23 10:46:23 +1000386/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000387static void
Damien Millerad833b32000-08-23 10:46:23 +1000388process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000389{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390 u_char *blob, *data, *signature = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000391 size_t blen, dlen, slen = 0;
392 u_int compat = 0, flags;
393 int r, ok = -1;
394 struct sshbuf *msg;
395 struct sshkey *key;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000396 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000397
djm@openbsd.org0088c572015-01-14 19:33:41 +0000398 if ((msg = sshbuf_new()) == NULL)
399 fatal("%s: sshbuf_new failed", __func__);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000400 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
401 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
402 (r = sshbuf_get_u32(e->request, &flags)) != 0)
403 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100404 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000405 compat = SSH_BUG_SIGBLOB;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000406 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000407 error("%s: cannot parse key blob: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000408 goto send;
Damien Millerad833b32000-08-23 10:46:23 +1000409 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000410 if ((id = lookup_identity(key, 2)) == NULL) {
411 verbose("%s: %s key not found", __func__, sshkey_type(key));
412 goto send;
413 }
414 if (id->confirm && confirm_key(id) != 0) {
415 verbose("%s: user refused key", __func__);
416 goto send;
417 }
418 if ((r = sshkey_sign(id->key, &signature, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000419 data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000420 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000421 goto send;
422 }
423 /* Success */
424 ok = 0;
425 send:
426 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000427 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000428 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
429 (r = sshbuf_put_string(msg, signature, slen)) != 0)
430 fatal("%s: buffer error: %s", __func__, ssh_err(r));
431 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
432 fatal("%s: buffer error: %s", __func__, ssh_err(r));
433
434 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
435 fatal("%s: buffer error: %s", __func__, ssh_err(r));
436
437 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000438 free(data);
439 free(blob);
440 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000441}
442
443/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000444static void
Damien Millerad833b32000-08-23 10:46:23 +1000445process_remove_identity(SocketEntry *e, int version)
446{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000447 size_t blen;
448 int r, success = 0;
449 struct sshkey *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000450 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000451#ifdef WITH_SSH1
452 u_int bits;
453#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100454
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000455 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000456#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000457 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000458 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
459 error("%s: sshkey_new failed", __func__);
460 return;
461 }
462 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
463 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
464 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
465 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466
markus@openbsd.org139ca812015-01-14 13:09:09 +0000467 if (bits != sshkey_size(key))
468 logit("Warning: identity keysize mismatch: "
469 "actual %u, announced %u",
470 sshkey_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000471 break;
Damien Miller86687062014-07-02 15:28:02 +1000472#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000473 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000474 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
475 fatal("%s: buffer error: %s", __func__, ssh_err(r));
476 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
477 error("%s: sshkey_from_blob failed: %s",
478 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000479 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000480 break;
481 }
482 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100483 Identity *id = lookup_identity(key, version);
484 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100485 /*
486 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100487 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000488 * the array, we actually free the key there and move
489 * all the entries between the empty slot and the end
490 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100491 */
Damien Millerad833b32000-08-23 10:46:23 +1000492 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 if (tab->nentries < 1)
494 fatal("process_remove_identity: "
495 "internal error: tab->nentries %d",
496 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100497 TAILQ_REMOVE(&tab->idlist, id, next);
498 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000499 tab->nentries--;
500 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100501 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000502 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000503 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000504 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
506
Ben Lindstrombba81212001-06-25 05:01:22 +0000507static void
Damien Millerad833b32000-08-23 10:46:23 +1000508process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509{
Damien Millerad833b32000-08-23 10:46:23 +1000510 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100511 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100514 for (id = TAILQ_FIRST(&tab->idlist); id;
515 id = TAILQ_FIRST(&tab->idlist)) {
516 TAILQ_REMOVE(&tab->idlist, id, next);
517 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100518 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Damien Miller95def091999-11-25 00:26:21 +1100520 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000521 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000522
523 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000524 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100525}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000526
Darren Tucker2812dc92007-03-21 20:45:06 +1100527/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000528static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000529reaper(void)
530{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000531 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532 Identity *id, *nxt;
533 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000534 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000535
536 for (version = 1; version < 3; version++) {
537 tab = idtab_lookup(version);
538 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
539 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100540 if (id->death == 0)
541 continue;
542 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100543 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000544 TAILQ_REMOVE(&tab->idlist, id, next);
545 free_identity(id);
546 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100547 } else
548 deadline = (deadline == 0) ? id->death :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000549 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000550 }
551 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100552 if (deadline == 0 || deadline <= now)
553 return 0;
554 else
555 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000556}
557
markus@openbsd.org139ca812015-01-14 13:09:09 +0000558/*
559 * XXX this and the corresponding serialisation function probably belongs
560 * in key.c
561 */
djm@openbsd.org111dfb22015-03-03 21:21:13 +0000562#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000563static int
564agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
565{
566 struct sshkey *k = NULL;
567 int r = SSH_ERR_INTERNAL_ERROR;
568
569 *kp = NULL;
570 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
571 return SSH_ERR_ALLOC_FAIL;
572
573 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
574 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
575 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
576 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
577 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
578 /* SSH1 and SSL have p and q swapped */
579 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
580 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
581 goto out;
582
583 /* Generate additional parameters */
584 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
585 goto out;
586 /* enable blinding */
587 if (RSA_blinding_on(k->rsa, NULL) != 1) {
588 r = SSH_ERR_LIBCRYPTO_ERROR;
589 goto out;
590 }
591
592 r = 0; /* success */
593 out:
594 if (r == 0)
595 *kp = k;
596 else
597 sshkey_free(k);
598 return r;
599}
Damien Miller6c203922015-03-03 13:48:48 -0800600#endif /* WITH_SSH1 */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000601
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000602static void
Damien Millerad833b32000-08-23 10:46:23 +1000603process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100604{
Damien Millerad833b32000-08-23 10:46:23 +1000605 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000606 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000607 int success = 0, confirm = 0;
608 u_int seconds;
609 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000610 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000611 struct sshkey *k = NULL;
612 u_char ctype;
613 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100614
Damien Millerad833b32000-08-23 10:46:23 +1000615 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000616#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000617 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000618 r = agent_decode_rsa1(e->request, &k);
Damien Millerc51d0732003-03-15 11:37:09 +1100619 break;
Damien Miller86687062014-07-02 15:28:02 +1000620#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100621 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000622 r = sshkey_private_deserialize(e->request, &k);
Damien Millerf0e90602013-12-07 10:40:26 +1100623 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100624 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000625 if (r != 0 || k == NULL ||
626 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
627 error("%s: decode private key: %s", __func__, ssh_err(r));
628 goto err;
629 }
Damien Miller86687062014-07-02 15:28:02 +1000630
markus@openbsd.org139ca812015-01-14 13:09:09 +0000631 while (sshbuf_len(e->request)) {
632 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
633 error("%s: buffer error: %s", __func__, ssh_err(r));
634 goto err;
635 }
636 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000637 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000638 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
639 error("%s: bad lifetime constraint: %s",
640 __func__, ssh_err(r));
641 goto err;
642 }
643 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000644 break;
Damien Miller6c711792003-01-24 11:36:23 +1100645 case SSH_AGENT_CONSTRAIN_CONFIRM:
646 confirm = 1;
647 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000648 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000649 error("%s: Unknown constraint %d", __func__, ctype);
650 err:
651 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000652 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000653 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000654 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000655 }
656 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000657
Damien Miller1cfadab2008-06-30 00:05:21 +1000658 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100659 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000660 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000661 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100662 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000663 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000664 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
665 /* Increment the number of identities. */
666 tab->nentries++;
667 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000668 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000669 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000670 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000671 id->comment = comment;
672 id->death = death;
673 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000674send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000675 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000676}
677
Ben Lindstrom2f717042002-06-06 21:52:03 +0000678/* XXX todo: encrypt sensitive data with passphrase */
679static void
680process_lock_agent(SocketEntry *e, int lock)
681{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000682 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000683 char *passwd;
684 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000685 static u_int fail_count = 0;
686 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000687
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000688 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000689 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000690 if (pwlen == 0) {
691 debug("empty password not supported");
692 } else if (locked && !lock) {
693 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
694 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
695 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000696 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000697 debug("agent unlocked");
698 locked = 0;
699 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000700 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000701 success = 1;
702 } else {
703 /* delay in 0.1s increments up to 10s */
704 if (fail_count < 100)
705 fail_count++;
706 delay = 100000 * fail_count;
707 debug("unlock failed, delaying %0.1lf seconds",
708 (double)delay/1000000);
709 usleep(delay);
710 }
711 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000712 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000713 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000714 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000715 arc4random_buf(lock_salt, sizeof(lock_salt));
716 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000717 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000718 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000719 success = 1;
720 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000721 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000722 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000723 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000724}
725
726static void
727no_identities(SocketEntry *e, u_int type)
728{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000729 struct sshbuf *msg;
730 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000731
markus@openbsd.org139ca812015-01-14 13:09:09 +0000732 if ((msg = sshbuf_new()) == NULL)
733 fatal("%s: sshbuf_new failed", __func__);
734 if ((r = sshbuf_put_u8(msg,
Ben Lindstrom2f717042002-06-06 21:52:03 +0000735 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
markus@openbsd.org139ca812015-01-14 13:09:09 +0000736 SSH_AGENT_RSA_IDENTITIES_ANSWER :
737 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
738 (r = sshbuf_put_u32(msg, 0)) != 0 ||
739 (r = sshbuf_put_stringb(e->output, msg)) != 0)
740 fatal("%s: buffer error: %s", __func__, ssh_err(r));
741 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000742}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000743
Damien Miller7ea845e2010-02-12 09:21:02 +1100744#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000745static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000746process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000747{
djm@openbsd.org786d5992016-11-30 03:07:37 +0000748 char *provider = NULL, *pin, canonical_provider[PATH_MAX];
markus@openbsd.org139ca812015-01-14 13:09:09 +0000749 int r, i, version, count = 0, success = 0, confirm = 0;
750 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000751 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000752 u_char type;
753 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000754 Identity *id;
755 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100756
markus@openbsd.org139ca812015-01-14 13:09:09 +0000757 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
758 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
759 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000760
markus@openbsd.org139ca812015-01-14 13:09:09 +0000761 while (sshbuf_len(e->request)) {
762 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
763 fatal("%s: buffer error: %s", __func__, ssh_err(r));
764 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000765 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000766 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
767 fatal("%s: buffer error: %s",
768 __func__, ssh_err(r));
769 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000770 break;
771 case SSH_AGENT_CONSTRAIN_CONFIRM:
772 confirm = 1;
773 break;
774 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000775 error("process_add_smartcard_key: "
776 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000777 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000778 }
779 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000780 if (realpath(provider, canonical_provider) == NULL) {
781 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
782 provider, strerror(errno));
783 goto send;
784 }
785 if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) {
786 verbose("refusing PKCS#11 add of \"%.100s\": "
787 "provider not whitelisted", canonical_provider);
788 goto send;
789 }
790 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000791 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000792 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000793
djm@openbsd.org786d5992016-11-30 03:07:37 +0000794 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100795 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000796 k = keys[i];
797 version = k->type == KEY_RSA1 ? 1 : 2;
798 tab = idtab_lookup(version);
799 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100800 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000801 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000802 id->provider = xstrdup(canonical_provider);
803 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000804 id->death = death;
805 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000806 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
807 tab->nentries++;
808 success = 1;
809 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000810 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000811 }
812 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000813 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000814send:
Darren Tuckera627d422013-06-02 07:31:17 +1000815 free(pin);
816 free(provider);
817 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000818 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000819}
820
821static void
822process_remove_smartcard_key(SocketEntry *e)
823{
Damien Miller7ea845e2010-02-12 09:21:02 +1100824 char *provider = NULL, *pin = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000825 int r, version, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100826 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000827 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000828
markus@openbsd.org139ca812015-01-14 13:09:09 +0000829 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
830 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
831 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000832 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000833
Damien Miller7ea845e2010-02-12 09:21:02 +1100834 for (version = 1; version < 3; version++) {
835 tab = idtab_lookup(version);
836 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
837 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100838 /* Skip file--based keys */
839 if (id->provider == NULL)
840 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100841 if (!strcmp(provider, id->provider)) {
842 TAILQ_REMOVE(&tab->idlist, id, next);
843 free_identity(id);
844 tab->nentries--;
845 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000846 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000847 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100848 if (pkcs11_del_provider(provider) == 0)
849 success = 1;
850 else
851 error("process_remove_smartcard_key:"
852 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000853 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000854 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000855}
Damien Miller7ea845e2010-02-12 09:21:02 +1100856#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000857
Damien Millerad833b32000-08-23 10:46:23 +1000858/* dispatch incoming messages */
859
Ben Lindstrombba81212001-06-25 05:01:22 +0000860static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000861process_message(SocketEntry *e)
862{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000863 u_int msg_len;
864 u_char type;
865 const u_char *cp;
866 int r;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000867
markus@openbsd.org139ca812015-01-14 13:09:09 +0000868 if (sshbuf_len(e->input) < 5)
Damien Miller95def091999-11-25 00:26:21 +1100869 return; /* Incomplete message. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000870 cp = sshbuf_ptr(e->input);
871 msg_len = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100872 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000873 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100874 return;
875 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000876 if (sshbuf_len(e->input) < msg_len + 4)
Damien Miller95def091999-11-25 00:26:21 +1100877 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000878
879 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000880 sshbuf_reset(e->request);
881 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
882 (r = sshbuf_get_u8(e->request, &type)) != 0)
883 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Ben Lindstrom2f717042002-06-06 21:52:03 +0000885 /* check wheter agent is locked */
886 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000887 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000888 switch (type) {
889 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
890 case SSH2_AGENTC_REQUEST_IDENTITIES:
891 /* send empty lists */
892 no_identities(e, type);
893 break;
894 default:
895 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000896 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000897 }
898 return;
899 }
900
Ben Lindstrom3f471632001-07-04 03:53:15 +0000901 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100902 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000903 case SSH_AGENTC_LOCK:
904 case SSH_AGENTC_UNLOCK:
905 process_lock_agent(e, type == SSH_AGENTC_LOCK);
906 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000907#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000908 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100909 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000910 process_authentication_challenge1(e);
911 break;
912 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
913 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100914 break;
915 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000916 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000917 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100918 break;
919 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000920 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100921 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000922#endif
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000923 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
924 process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
925 break;
Damien Millerad833b32000-08-23 10:46:23 +1000926 /* ssh2 */
927 case SSH2_AGENTC_SIGN_REQUEST:
928 process_sign_request2(e);
929 break;
930 case SSH2_AGENTC_REQUEST_IDENTITIES:
931 process_request_identities(e, 2);
932 break;
933 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000934 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000935 process_add_identity(e, 2);
936 break;
937 case SSH2_AGENTC_REMOVE_IDENTITY:
938 process_remove_identity(e, 2);
939 break;
940 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
941 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100942 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100943#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000944 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000945 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000946 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100947 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000948 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
949 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100950 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100951#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100952 default:
953 /* Unknown message. Respond with failure. */
954 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000955 sshbuf_reset(e->request);
956 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100957 break;
958 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959}
960
Ben Lindstrombba81212001-06-25 05:01:22 +0000961static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000962new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000963{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000964 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000965
Damien Miller232711f2004-06-15 10:35:30 +1000966 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967
Damien Miller95def091999-11-25 00:26:21 +1100968 if (fd > max_fd)
969 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000970
Damien Miller95def091999-11-25 00:26:21 +1100971 for (i = 0; i < sockets_alloc; i++)
972 if (sockets[i].type == AUTH_UNUSED) {
973 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000974 if ((sockets[i].input = sshbuf_new()) == NULL)
975 fatal("%s: sshbuf_new failed", __func__);
976 if ((sockets[i].output = sshbuf_new()) == NULL)
977 fatal("%s: sshbuf_new failed", __func__);
978 if ((sockets[i].request = sshbuf_new()) == NULL)
979 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000980 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100981 return;
982 }
983 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000984 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000985 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000986 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100987 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000988 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100989 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000990 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
991 fatal("%s: sshbuf_new failed", __func__);
992 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
993 fatal("%s: sshbuf_new failed", __func__);
994 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
995 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000996 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997}
998
Ben Lindstrombba81212001-06-25 05:01:22 +0000999static int
Darren Tucker2812dc92007-03-21 20:45:06 +11001000prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
1001 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002{
Darren Tucker55119252013-06-02 07:43:59 +10001003 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001004 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +11001005 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +10001006 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001007
1008 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +11001009 switch (sockets[i].type) {
1010 case AUTH_SOCKET:
1011 case AUTH_CONNECTION:
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001012 n = MAXIMUM(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +11001013 break;
1014 case AUTH_UNUSED:
1015 break;
1016 default:
1017 fatal("Unknown socket type %d", sockets[i].type);
1018 break;
1019 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001020 }
1021
1022 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001023 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +10001024 free(*fdrp);
1025 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001026 *fdrp = xmalloc(sz);
1027 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001028 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001029 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001030 if (n < *fdl)
1031 debug("XXX shrink: %d < %d", n, *fdl);
1032 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001033 memset(*fdrp, 0, sz);
1034 memset(*fdwp, 0, sz);
1035
1036 for (i = 0; i < sockets_alloc; i++) {
1037 switch (sockets[i].type) {
1038 case AUTH_SOCKET:
1039 case AUTH_CONNECTION:
1040 FD_SET(sockets[i].fd, *fdrp);
markus@openbsd.org139ca812015-01-14 13:09:09 +00001041 if (sshbuf_len(sockets[i].output) > 0)
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001042 FD_SET(sockets[i].fd, *fdwp);
1043 break;
1044 default:
1045 break;
1046 }
1047 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001048 deadline = reaper();
1049 if (parent_alive_interval != 0)
1050 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001051 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001052 if (deadline == 0) {
1053 *tvpp = NULL;
1054 } else {
1055 tv.tv_sec = deadline;
1056 tv.tv_usec = 0;
1057 *tvpp = &tv;
1058 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001059 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001060}
1061
Ben Lindstrombba81212001-06-25 05:01:22 +00001062static void
Damien Miller95def091999-11-25 00:26:21 +11001063after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064{
Ben Lindstrom822b6342002-06-23 21:38:49 +00001065 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +11001066 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +11001067 char buf[1024];
markus@openbsd.org139ca812015-01-14 13:09:09 +00001068 int len, sock, r;
Darren Tucker72473c62009-10-07 09:01:03 +11001069 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001070 uid_t euid;
1071 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072
Darren Tucker72473c62009-10-07 09:01:03 +11001073 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001074 switch (sockets[i].type) {
1075 case AUTH_UNUSED:
1076 break;
1077 case AUTH_SOCKET:
1078 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001079 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001080 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001081 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001082 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001083 error("accept from AUTH_SOCKET: %s",
1084 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001085 break;
1086 }
Damien Miller538f1812002-09-12 09:51:10 +10001087 if (getpeereid(sock, &euid, &egid) < 0) {
1088 error("getpeereid %d failed: %s",
1089 sock, strerror(errno));
1090 close(sock);
1091 break;
1092 }
Damien Milleraf9de382002-10-03 11:54:35 +10001093 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001094 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001095 "peer euid %u != uid %u",
1096 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001097 close(sock);
1098 break;
1099 }
Damien Miller95def091999-11-25 00:26:21 +11001100 new_socket(AUTH_CONNECTION, sock);
1101 }
1102 break;
1103 case AUTH_CONNECTION:
markus@openbsd.org139ca812015-01-14 13:09:09 +00001104 if (sshbuf_len(sockets[i].output) > 0 &&
Damien Miller95def091999-11-25 00:26:21 +11001105 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001106 len = write(sockets[i].fd,
markus@openbsd.org139ca812015-01-14 13:09:09 +00001107 sshbuf_ptr(sockets[i].output),
1108 sshbuf_len(sockets[i].output));
Darren Tucker72473c62009-10-07 09:01:03 +11001109 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_consume(sockets[i].output,
1118 len)) != 0)
1119 fatal("%s: buffer error: %s",
1120 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001121 }
1122 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001123 len = read(sockets[i].fd, buf, sizeof(buf));
1124 if (len == -1 && (errno == EAGAIN ||
1125 errno == EWOULDBLOCK ||
1126 errno == EINTR))
1127 continue;
Damien Miller95def091999-11-25 00:26:21 +11001128 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001129 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001130 break;
1131 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001132 if ((r = sshbuf_put(sockets[i].input,
1133 buf, len)) != 0)
1134 fatal("%s: buffer error: %s",
1135 __func__, ssh_err(r));
Damien Millerf4977942014-07-30 12:32:46 +10001136 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001137 process_message(&sockets[i]);
1138 }
1139 break;
1140 default:
1141 fatal("Unknown type %d", sockets[i].type);
1142 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143}
1144
Ben Lindstrombba81212001-06-25 05:01:22 +00001145static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001146cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001147{
Damien Millerb1e967c2014-07-03 21:22:40 +10001148 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1149 return;
1150 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001151 if (socket_name[0])
1152 unlink(socket_name);
1153 if (socket_dir[0])
1154 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155}
1156
Darren Tucker3e33cec2003-10-02 16:12:36 +10001157void
Damien Miller792c5111999-10-29 09:47:09 +10001158cleanup_exit(int i)
1159{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001160 cleanup_socket();
1161 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001162}
1163
Damien Miller1c13bd82006-03-26 14:28:14 +11001164/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001165static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001166cleanup_handler(int sig)
1167{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001168 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001169#ifdef ENABLE_PKCS11
1170 pkcs11_terminate();
1171#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001172 _exit(2);
1173}
1174
Ben Lindstrombba81212001-06-25 05:01:22 +00001175static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001176check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001177{
Darren Tucker3e78a512011-06-03 14:14:16 +10001178 /*
1179 * If our parent has exited then getppid() will return (pid_t)1,
1180 * so testing for that should be safe.
1181 */
1182 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001183 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001184 cleanup_socket();
1185 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001186 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001187}
1188
1189static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001190usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001191{
Damien Millerf0858de2014-04-20 13:01:30 +10001192 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001193 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
djm@openbsd.org786d5992016-11-30 03:07:37 +00001194 " [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001195 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001196 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001197}
1198
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199int
1200main(int ac, char **av)
1201{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001202 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001203 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001204 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001205 char *shell, *format, *pidstr, *agentsocket = NULL;
1206 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001207#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001208 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001209#endif
Damien Miller615f9392000-05-17 22:53:33 +10001210 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001211 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001212 pid_t pid;
1213 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001214 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001215 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001216 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001217
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001218 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +10001219 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1220 sanitise_stdfd();
1221
Damien Miller6cffb9a2002-09-04 16:20:26 +10001222 /* drop */
1223 setegid(getgid());
1224 setgid(getgid());
1225
Darren Tucker0fb7f592016-06-09 16:23:07 +10001226 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001227
Damien Miller1f0311c2014-05-15 14:24:09 +10001228#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001229 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001230#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001231
Damien Miller59d3d5b2003-08-22 09:34:41 +10001232 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001233 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001234
djm@openbsd.org786d5992016-11-30 03:07:37 +00001235 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001236 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001237 case 'E':
1238 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1239 if (fingerprint_hash == -1)
1240 fatal("Invalid hash algorithm \"%s\"", optarg);
1241 break;
Damien Miller95def091999-11-25 00:26:21 +11001242 case 'c':
1243 if (s_flag)
1244 usage();
1245 c_flag++;
1246 break;
1247 case 'k':
1248 k_flag++;
1249 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001250 case 'P':
1251 if (pkcs11_whitelist != NULL)
1252 fatal("-P option already specified");
1253 pkcs11_whitelist = xstrdup(optarg);
1254 break;
Damien Miller95def091999-11-25 00:26:21 +11001255 case 's':
1256 if (c_flag)
1257 usage();
1258 s_flag++;
1259 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001260 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001261 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001262 usage();
1263 d_flag++;
1264 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001265 case 'D':
1266 if (d_flag || D_flag)
1267 usage();
1268 D_flag++;
1269 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001270 case 'a':
1271 agentsocket = optarg;
1272 break;
Damien Miller53d81482003-01-22 11:47:19 +11001273 case 't':
1274 if ((lifetime = convtime(optarg)) == -1) {
1275 fprintf(stderr, "Invalid lifetime\n");
1276 usage();
1277 }
1278 break;
Damien Miller95def091999-11-25 00:26:21 +11001279 default:
1280 usage();
1281 }
Damien Miller792c5111999-10-29 09:47:09 +10001282 }
Damien Miller95def091999-11-25 00:26:21 +11001283 ac -= optind;
1284 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001285
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001286 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001287 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001288
djm@openbsd.org786d5992016-11-30 03:07:37 +00001289 if (pkcs11_whitelist == NULL)
1290 pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST);
1291
Ben Lindstromeecdf232002-04-02 21:03:51 +00001292 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001293 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001294 if (shell != NULL && (len = strlen(shell)) > 2 &&
1295 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001296 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001297 }
Damien Miller95def091999-11-25 00:26:21 +11001298 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001299 const char *errstr = NULL;
1300
Damien Miller95def091999-11-25 00:26:21 +11001301 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1302 if (pidstr == NULL) {
1303 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001304 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001305 exit(1);
1306 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001307 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1308 if (errstr) {
1309 fprintf(stderr,
1310 "%s=\"%s\", which is not a good PID: %s\n",
1311 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001312 exit(1);
1313 }
1314 if (kill(pid, SIGTERM) == -1) {
1315 perror("kill");
1316 exit(1);
1317 }
1318 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1319 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1320 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001321 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001322 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001323 }
Damien Miller95def091999-11-25 00:26:21 +11001324 parent_pid = getpid();
1325
Ben Lindstromb7788f32002-06-06 21:46:08 +00001326 if (agentsocket == NULL) {
1327 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001328 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001329 if (mkdtemp(socket_dir) == NULL) {
1330 perror("mkdtemp: private socket dir");
1331 exit(1);
1332 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001333 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1334 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001335 } else {
1336 /* Try to use specified agent socket */
1337 socket_dir[0] = '\0';
1338 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001339 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340
Damien Miller5428f641999-11-25 11:54:57 +11001341 /*
1342 * Create socket early so it will exist before command gets run from
1343 * the parent.
1344 */
Damien Millerab2ec582014-07-18 15:04:47 +10001345 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001346 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001347 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001348 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001349 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001350 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001351 }
Damien Millerab2ec582014-07-18 15:04:47 +10001352 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001353
Damien Miller5428f641999-11-25 11:54:57 +11001354 /*
1355 * Fork, and have the parent execute the command, if any, or present
1356 * the socket data. The child continues as the authentication agent.
1357 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001358 if (D_flag || d_flag) {
1359 log_init(__progname,
1360 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1361 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001362 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1363 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1364 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001365 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001366 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001367 goto skip;
1368 }
Damien Miller95def091999-11-25 00:26:21 +11001369 pid = fork();
1370 if (pid == -1) {
1371 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001372 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001373 }
1374 if (pid != 0) { /* Parent - execute the given command. */
1375 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001376 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001377 if (ac == 0) {
1378 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1379 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001380 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001381 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001382 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001383 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001384 exit(0);
1385 }
Damien Millere4340be2000-09-16 13:29:08 +11001386 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1387 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1388 perror("setenv");
1389 exit(1);
1390 }
Damien Miller95def091999-11-25 00:26:21 +11001391 execvp(av[0], av);
1392 perror(av[0]);
1393 exit(1);
1394 }
Damien Millerc653b892002-02-08 22:05:41 +11001395 /* child */
1396 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001397
1398 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001399 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001400 cleanup_exit(1);
1401 }
1402
1403 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001404 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1405 /* XXX might close listen socket */
1406 (void)dup2(fd, STDIN_FILENO);
1407 (void)dup2(fd, STDOUT_FILENO);
1408 (void)dup2(fd, STDERR_FILENO);
1409 if (fd > 2)
1410 close(fd);
1411 }
Damien Miller95def091999-11-25 00:26:21 +11001412
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001413#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001414 /* deny core dumps, since memory contains unencrypted private keys */
1415 rlim.rlim_cur = rlim.rlim_max = 0;
1416 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001417 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001418 cleanup_exit(1);
1419 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001420#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001421
1422skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001423
Damien Millerb1e967c2014-07-03 21:22:40 +10001424 cleanup_pid = getpid();
1425
Damien Miller7ea845e2010-02-12 09:21:02 +11001426#ifdef ENABLE_PKCS11
1427 pkcs11_init(0);
1428#endif
Damien Miller95def091999-11-25 00:26:21 +11001429 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001430 if (ac > 0)
1431 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001432 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001433 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001434 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001435 signal(SIGHUP, cleanup_handler);
1436 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001437 nalloc = 0;
1438
djm@openbsd.org786d5992016-11-30 03:07:37 +00001439 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001440 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001441 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001442
Damien Miller95def091999-11-25 00:26:21 +11001443 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001444 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1445 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001446 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001447 if (parent_alive_interval != 0)
1448 check_parent_exists();
1449 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001450 if (result < 0) {
1451 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001452 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001453 fatal("select: %s", strerror(saved_errno));
1454 } else if (result > 0)
1455 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001456 }
1457 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001458}