blob: 6de62d7fbda5cb2ce0468120c2f4db9016c0a863 [file] [log] [blame]
djm@openbsd.org05daa212019-11-16 22:36:48 +00001/* $OpenBSD: ssh-agent.c,v 1.247 2019/11/16 22:36:48 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>
djm@openbsd.org07da39f2019-10-31 21:22:01 +000044#include <sys/wait.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
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000064#ifdef HAVE_POLL_H
65# include <poll.h>
66#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110067#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100068#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100069#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100070#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100071#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100072#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100073#include <unistd.h>
Damien Millere97201f2015-05-21 17:55:15 +100074#ifdef HAVE_UTIL_H
75# include <util.h>
76#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110077
Damien Millerd7834352006-08-05 12:39:39 +100078#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "ssh.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000080#include "sshbuf.h"
81#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110083#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110085#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110086#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000087#include "ssherr.h"
djm@openbsd.org786d5992016-11-30 03:07:37 +000088#include "match.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000089#include "msg.h"
djm@openbsd.org45ffa362019-11-15 02:38:07 +000090#include "ssherr.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000091#include "pathnames.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110092#include "ssh-pkcs11.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000093#include "ssh-sk.h"
Ben Lindstrom3f471632001-07-04 03:53:15 +000094
djm@openbsd.org07da39f2019-10-31 21:22:01 +000095#ifndef DEFAULT_PROVIDER_WHITELIST
96# define DEFAULT_PROVIDER_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
djm@openbsd.org786d5992016-11-30 03:07:37 +000097#endif
98
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000099/* Maximum accepted message length */
100#define AGENT_MAX_LEN (256*1024)
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000101/* Maximum bytes to read from client socket */
102#define AGENT_RBUF_LEN (4096)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000103
Ben Lindstrom65366a82001-12-06 16:32:47 +0000104typedef enum {
105 AUTH_UNUSED,
106 AUTH_SOCKET,
107 AUTH_CONNECTION
108} sock_type;
109
Damien Miller95def091999-11-25 00:26:21 +1100110typedef struct {
111 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000112 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000113 struct sshbuf *input;
114 struct sshbuf *output;
115 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116} SocketEntry;
117
Ben Lindstrom46c16222000-12-22 01:43:59 +0000118u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119SocketEntry *sockets = NULL;
120
Damien Miller1a534ae2002-01-22 23:26:13 +1100121typedef struct identity {
122 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000123 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100124 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100125 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000126 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100127 u_int confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000128 char *sk_provider;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129} Identity;
130
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000131struct idtable {
Damien Millerad833b32000-08-23 10:46:23 +1000132 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100133 TAILQ_HEAD(idqueue, identity) idlist;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000134};
Damien Millerad833b32000-08-23 10:46:23 +1000135
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000136/* private key table */
137struct idtable *idtab;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
139int max_fd = 0;
140
141/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000142pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000143time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Millerb1e967c2014-07-03 21:22:40 +1000145/* pid of process for which cleanup_socket is applicable */
146pid_t cleanup_pid = 0;
147
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000149char socket_name[PATH_MAX];
150char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000152/* PKCS#11/Security key path whitelist */
153static char *provider_whitelist;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000154
Ben Lindstrom2f717042002-06-06 21:52:03 +0000155/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000156#define LOCK_SIZE 32
157#define LOCK_SALT_SIZE 16
158#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000159int locked = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000160u_char lock_pwhash[LOCK_SIZE];
161u_char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000162
Damien Miller95def091999-11-25 00:26:21 +1100163extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100164
Darren Tucker55119252013-06-02 07:43:59 +1000165/* Default lifetime in seconds (0 == forever) */
166static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100167
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000168static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
169
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static void
Damien Miller58f34862002-09-04 16:31:21 +1000171close_socket(SocketEntry *e)
172{
Damien Miller58f34862002-09-04 16:31:21 +1000173 close(e->fd);
174 e->fd = -1;
175 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000176 sshbuf_free(e->input);
177 sshbuf_free(e->output);
178 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000179}
180
181static void
Damien Millerad833b32000-08-23 10:46:23 +1000182idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000184 idtab = xcalloc(1, sizeof(*idtab));
185 TAILQ_INIT(&idtab->idlist);
186 idtab->nentries = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000187}
188
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000189static void
190free_identity(Identity *id)
191{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000192 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000193 free(id->provider);
194 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000195 free(id->sk_provider);
Darren Tuckera627d422013-06-02 07:31:17 +1000196 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000197}
198
Damien Millerad833b32000-08-23 10:46:23 +1000199/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100200static Identity *
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000201lookup_identity(struct sshkey *key)
Damien Millerad833b32000-08-23 10:46:23 +1000202{
Damien Miller1a534ae2002-01-22 23:26:13 +1100203 Identity *id;
204
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000205 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000206 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100207 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000208 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100209 return (NULL);
210}
211
Damien Miller6c711792003-01-24 11:36:23 +1100212/* Check confirmation of keysign request */
213static int
214confirm_key(Identity *id)
215{
Darren Tuckerce327b62004-11-05 20:38:03 +1100216 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100217 int ret = -1;
218
markus@openbsd.org139ca812015-01-14 13:09:09 +0000219 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000220 if (p != NULL &&
221 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100222 id->comment, p))
223 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000224 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100225
Damien Miller6c711792003-01-24 11:36:23 +1100226 return (ret);
227}
228
markus@openbsd.org139ca812015-01-14 13:09:09 +0000229static void
230send_status(SocketEntry *e, int success)
231{
232 int r;
233
234 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
235 (r = sshbuf_put_u8(e->output, success ?
236 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
237 fatal("%s: buffer error: %s", __func__, ssh_err(r));
238}
239
Damien Millerad833b32000-08-23 10:46:23 +1000240/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000241static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000242process_request_identities(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000243{
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__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000250 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
251 (r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000252 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000253 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000254 if ((r = sshkey_puts_opts(id->key, msg, SSHKEY_SERIALIZE_INFO))
255 != 0 ||
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000256 (r = sshbuf_put_cstring(msg, id->comment)) != 0) {
257 error("%s: put key/comment: %s", __func__,
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000258 ssh_err(r));
259 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000260 }
Damien Miller95def091999-11-25 00:26:21 +1100261 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000262 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
263 fatal("%s: buffer error: %s", __func__, ssh_err(r));
264 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265}
266
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000268static char *
269agent_decode_alg(struct sshkey *key, u_int flags)
270{
271 if (key->type == KEY_RSA) {
272 if (flags & SSH_AGENT_RSA_SHA2_256)
273 return "rsa-sha2-256";
274 else if (flags & SSH_AGENT_RSA_SHA2_512)
275 return "rsa-sha2-512";
djm@openbsd.org2317ce42019-06-14 03:51:47 +0000276 } else if (key->type == KEY_RSA_CERT) {
277 if (flags & SSH_AGENT_RSA_SHA2_256)
278 return "rsa-sha2-256-cert-v01@openssh.com";
279 else if (flags & SSH_AGENT_RSA_SHA2_512)
280 return "rsa-sha2-512-cert-v01@openssh.com";
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000281 }
282 return NULL;
283}
284
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000285static int
286provider_sign(const char *provider, struct sshkey *key,
287 u_char **sigp, size_t *lenp,
288 const u_char *data, size_t datalen,
289 const char *alg, u_int compat)
290{
291 int status, pair[2], r = SSH_ERR_INTERNAL_ERROR;
292 pid_t pid;
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000293 char *helper, *verbosity = NULL, *fp = NULL;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000294 struct sshbuf *kbuf, *req, *resp;
295 u_char version;
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000296 struct notifier_ctx *notifier = NULL;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000297
298 debug3("%s: start for provider %s", __func__, provider);
299
300 *sigp = NULL;
301 *lenp = 0;
302
303 helper = getenv("SSH_SK_HELPER");
304 if (helper == NULL || strlen(helper) == 0)
305 helper = _PATH_SSH_SK_HELPER;
306 if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
307 verbosity = "-vvv";
308
309 /* Start helper */
310 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
311 error("socketpair: %s", strerror(errno));
312 return SSH_ERR_SYSTEM_ERROR;
313 }
314 if ((pid = fork()) == -1) {
315 error("fork: %s", strerror(errno));
316 close(pair[0]);
317 close(pair[1]);
318 return SSH_ERR_SYSTEM_ERROR;
319 }
320 if (pid == 0) {
321 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
322 (dup2(pair[1], STDOUT_FILENO) == -1))
323 fatal("%s: dup2: %s", __func__, ssh_err(r));
324 close(pair[0]);
325 close(pair[1]);
326 closefrom(STDERR_FILENO + 1);
327 debug("%s: starting %s %s", __func__, helper,
328 verbosity == NULL ? "" : verbosity);
329 execlp(helper, helper, verbosity, (char *)NULL);
330 fatal("%s: execlp: %s", __func__, strerror(errno));
331 }
332 close(pair[1]);
333
334 if ((kbuf = sshbuf_new()) == NULL ||
335 (req = sshbuf_new()) == NULL ||
336 (resp = sshbuf_new()) == NULL)
337 fatal("%s: sshbuf_new failed", __func__);
338
339 if ((r = sshkey_private_serialize(key, kbuf)) != 0 ||
340 (r = sshbuf_put_stringb(req, kbuf)) != 0 ||
341 (r = sshbuf_put_cstring(req, provider)) != 0 ||
342 (r = sshbuf_put_string(req, data, datalen)) != 0 ||
343 (r = sshbuf_put_u32(req, compat)) != 0)
344 fatal("%s: compose: %s", __func__, ssh_err(r));
djm@openbsd.org05daa212019-11-16 22:36:48 +0000345
346 if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
347 SSH_FP_DEFAULT)) == NULL)
348 fatal("%s: sshkey_fingerprint failed", __func__);
349 notifier = notify_start(0,
350 "Confirm user presence for key %s %s", sshkey_type(key), fp);
351
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000352 if ((r = ssh_msg_send(pair[0], SSH_SK_HELPER_VERSION, req)) != 0) {
353 error("%s: send: %s", __func__, ssh_err(r));
354 goto out;
355 }
356 if ((r = ssh_msg_recv(pair[0], resp)) != 0) {
357 error("%s: receive: %s", __func__, ssh_err(r));
358 goto out;
359 }
360 if ((r = sshbuf_get_u8(resp, &version)) != 0) {
361 error("%s: parse version: %s", __func__, ssh_err(r));
362 goto out;
363 }
364 if (version != SSH_SK_HELPER_VERSION) {
365 error("%s: unsupported version: got %u, expected %u",
366 __func__, version, SSH_SK_HELPER_VERSION);
367 r = SSH_ERR_INVALID_FORMAT;
368 goto out;
369 }
370 if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) {
371 error("%s: parse signature: %s", __func__, ssh_err(r));
372 r = SSH_ERR_INVALID_FORMAT;
373 goto out;
374 }
375 if (sshbuf_len(resp) != 0) {
376 error("%s: trailing data in response", __func__);
377 r = SSH_ERR_INVALID_FORMAT;
378 goto out;
379 }
380 /* success */
381 r = 0;
382 out:
383 while (waitpid(pid, &status, 0) == -1) {
384 if (errno != EINTR)
385 fatal("%s: waitpid: %s", __func__, ssh_err(r));
386 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000387 notify_complete(notifier);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000388 if (!WIFEXITED(status)) {
389 error("%s: helper %s exited abnormally", __func__, helper);
390 if (r == 0)
391 r = SSH_ERR_SYSTEM_ERROR;
392 } else if (WEXITSTATUS(status) != 0) {
393 error("%s: helper %s exited with non-zero exit status",
394 __func__, helper);
395 if (r == 0)
396 r = SSH_ERR_SYSTEM_ERROR;
397 }
398 if (r != 0) {
399 freezero(*sigp, *lenp);
400 *sigp = NULL;
401 *lenp = 0;
402 }
403 sshbuf_free(kbuf);
404 sshbuf_free(req);
405 sshbuf_free(resp);
406 return r;
407}
408
Damien Millerad833b32000-08-23 10:46:23 +1000409/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Damien Millerad833b32000-08-23 10:46:23 +1000411process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000413 const u_char *data;
414 u_char *signature = NULL;
415 size_t dlen, slen = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000416 u_int compat = 0, flags;
djm@openbsd.org05daa212019-11-16 22:36:48 +0000417 int r, ok = -1;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000418 struct sshbuf *msg;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000419 struct sshkey *key = NULL;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000420 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000421
djm@openbsd.org0088c572015-01-14 19:33:41 +0000422 if ((msg = sshbuf_new()) == NULL)
423 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000424 if ((r = sshkey_froms(e->request, &key)) != 0 ||
425 (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
djm@openbsd.org@openbsd.org93c68a82017-11-15 00:13:40 +0000426 (r = sshbuf_get_u32(e->request, &flags)) != 0) {
427 error("%s: couldn't parse request: %s", __func__, ssh_err(r));
428 goto send;
429 }
430
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000431 if ((id = lookup_identity(key)) == NULL) {
djm@openbsd.org0088c572015-01-14 19:33:41 +0000432 verbose("%s: %s key not found", __func__, sshkey_type(key));
433 goto send;
434 }
435 if (id->confirm && confirm_key(id) != 0) {
436 verbose("%s: user refused key", __func__);
437 goto send;
438 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000439 if (id->sk_provider != NULL) {
djm@openbsd.org05daa212019-11-16 22:36:48 +0000440 if ((r = provider_sign(id->sk_provider, id->key, &signature,
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000441 &slen, data, dlen, agent_decode_alg(key, flags),
djm@openbsd.org05daa212019-11-16 22:36:48 +0000442 compat)) != 0) {
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000443 error("%s: sign: %s", __func__, ssh_err(r));
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000444 goto send;
445 }
446 } else {
447 if ((r = sshkey_sign(id->key, &signature, &slen,
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000448 data, dlen, agent_decode_alg(key, flags),
449 NULL, compat)) != 0) {
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000450 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
451 goto send;
452 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000453 }
454 /* Success */
455 ok = 0;
456 send:
457 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000458 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000459 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
460 (r = sshbuf_put_string(msg, signature, slen)) != 0)
461 fatal("%s: buffer error: %s", __func__, ssh_err(r));
462 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
463 fatal("%s: buffer error: %s", __func__, ssh_err(r));
464
465 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
466 fatal("%s: buffer error: %s", __func__, ssh_err(r));
467
468 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000469 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000470}
471
472/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000473static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000474process_remove_identity(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000475{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000476 int r, success = 0;
477 struct sshkey *key = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000478 Identity *id;
Damien Miller7e8e8201999-11-16 13:37:16 +1100479
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000480 if ((r = sshkey_froms(e->request, &key)) != 0) {
481 error("%s: get key: %s", __func__, ssh_err(r));
482 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000483 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000484 if ((id = lookup_identity(key)) == NULL) {
485 debug("%s: key not found", __func__);
486 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000487 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000488 /* We have this key, free it. */
489 if (idtab->nentries < 1)
490 fatal("%s: internal error: nentries %d",
491 __func__, idtab->nentries);
492 TAILQ_REMOVE(&idtab->idlist, id, next);
493 free_identity(id);
494 idtab->nentries--;
495 sshkey_free(key);
496 success = 1;
497 done:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000498 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000499}
500
Ben Lindstrombba81212001-06-25 05:01:22 +0000501static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000502process_remove_all_identities(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503{
Damien Miller1a534ae2002-01-22 23:26:13 +1100504 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505
Damien Miller95def091999-11-25 00:26:21 +1100506 /* Loop over all identities and clear the keys. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000507 for (id = TAILQ_FIRST(&idtab->idlist); id;
508 id = TAILQ_FIRST(&idtab->idlist)) {
509 TAILQ_REMOVE(&idtab->idlist, id, next);
Damien Miller1a534ae2002-01-22 23:26:13 +1100510 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100511 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000512
Damien Miller95def091999-11-25 00:26:21 +1100513 /* Mark that there are no identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000514 idtab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
516 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000517 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100518}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519
Darren Tucker2812dc92007-03-21 20:45:06 +1100520/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000521static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000522reaper(void)
523{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000524 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000525 Identity *id, *nxt;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000526
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000527 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
528 nxt = TAILQ_NEXT(id, next);
529 if (id->death == 0)
530 continue;
531 if (now >= id->death) {
532 debug("expiring key '%s'", id->comment);
533 TAILQ_REMOVE(&idtab->idlist, id, next);
534 free_identity(id);
535 idtab->nentries--;
536 } else
537 deadline = (deadline == 0) ? id->death :
538 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000539 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100540 if (deadline == 0 || deadline <= now)
541 return 0;
542 else
543 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000544}
545
546static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000547process_add_identity(SocketEntry *e)
Damien Miller95def091999-11-25 00:26:21 +1100548{
Damien Miller4c7728c2007-10-26 14:25:31 +1000549 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000550 int success = 0, confirm = 0;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000551 u_int seconds, maxsign;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000552 char *fp, *comment = NULL, *ext_name = NULL, *sk_provider = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000553 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000554 struct sshkey *k = NULL;
555 u_char ctype;
556 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100557
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000558 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
559 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000560 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
561 error("%s: decode private key: %s", __func__, ssh_err(r));
562 goto err;
563 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000564 while (sshbuf_len(e->request)) {
565 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
566 error("%s: buffer error: %s", __func__, ssh_err(r));
567 goto err;
568 }
569 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000570 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000571 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
572 error("%s: bad lifetime constraint: %s",
573 __func__, ssh_err(r));
574 goto err;
575 }
576 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000577 break;
Damien Miller6c711792003-01-24 11:36:23 +1100578 case SSH_AGENT_CONSTRAIN_CONFIRM:
579 confirm = 1;
580 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000581 case SSH_AGENT_CONSTRAIN_MAXSIGN:
582 if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
583 error("%s: bad maxsign constraint: %s",
584 __func__, ssh_err(r));
585 goto err;
586 }
587 if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
588 error("%s: cannot enable maxsign: %s",
589 __func__, ssh_err(r));
590 goto err;
591 }
592 break;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000593 case SSH_AGENT_CONSTRAIN_EXTENSION:
594 if ((r = sshbuf_get_cstring(e->request,
595 &ext_name, NULL)) != 0) {
596 error("%s: cannot parse extension: %s",
597 __func__, ssh_err(r));
598 goto err;
599 }
600 debug("%s: constraint ext %s", __func__, ext_name);
601 if (strcmp(ext_name, "sk-provider@openssh.com") == 0) {
602 if (sk_provider != NULL) {
603 error("%s already set", ext_name);
604 goto err;
605 }
606 if ((r = sshbuf_get_cstring(e->request,
607 &sk_provider, NULL)) != 0) {
608 error("%s: cannot parse %s: %s",
609 __func__, ext_name, ssh_err(r));
610 goto err;
611 }
612 } else {
613 error("%s: unsupported constraint \"%s\"",
614 __func__, ext_name);
615 goto err;
616 }
617 free(ext_name);
618 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000619 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000620 error("%s: Unknown constraint %d", __func__, ctype);
621 err:
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000622 free(sk_provider);
623 free(ext_name);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000624 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000625 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000626 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000627 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000628 }
629 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000630 if (sk_provider != NULL) {
markus@openbsd.org2c557442019-11-12 19:33:08 +0000631 if (!sshkey_is_sk(k)) {
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000632 error("Cannot add provider: %s is not a security key",
633 sshkey_type(k));
634 free(sk_provider);
635 goto send;
636 }
djm@openbsd.orgab360062019-11-15 04:12:32 +0000637 if (strcasecmp(sk_provider, "internal") == 0)
638 debug("%s: internal provider", __func__);
639 else if (match_pattern_list(sk_provider,
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000640 provider_whitelist, 0) != 1) {
641 error("Refusing add key: provider %s not whitelisted",
642 sk_provider);
643 free(sk_provider);
644 goto send;
645 }
646 }
markus@openbsd.orgbf219922019-11-13 07:53:10 +0000647 if ((r = sshkey_shield_private(k)) != 0) {
648 error("%s: shield private key: %s", __func__, ssh_err(r));
649 goto err;
650 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000651
Damien Miller1cfadab2008-06-30 00:05:21 +1000652 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100653 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000654 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000655 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100656 id = xcalloc(1, sizeof(Identity));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000657 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000658 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000659 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000660 } else {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000661 /* key state might have been updated */
662 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000663 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000664 free(id->sk_provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000665 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000666 id->key = k;
Damien Miller4c7728c2007-10-26 14:25:31 +1000667 id->comment = comment;
668 id->death = death;
669 id->confirm = confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000670 id->sk_provider = sk_provider;
671
672 if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
673 SSH_FP_DEFAULT)) == NULL)
674 fatal("%s: sshkey_fingerprint failed", __func__);
675 debug("%s: add %s %s \"%.100s\" (life: %u) (confirm: %u) "
676 "(provider: %s)", __func__, sshkey_ssh_name(k), fp, comment,
677 seconds, confirm, sk_provider == NULL ? "none" : sk_provider);
678 free(fp);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000679send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000680 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000681}
682
Ben Lindstrom2f717042002-06-06 21:52:03 +0000683/* XXX todo: encrypt sensitive data with passphrase */
684static void
685process_lock_agent(SocketEntry *e, int lock)
686{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000687 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000688 char *passwd;
689 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000690 static u_int fail_count = 0;
691 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000692
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000693 /*
694 * This is deliberately fatal: the user has requested that we lock,
695 * but we can't parse their request properly. The only safe thing to
696 * do is abort.
697 */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000698 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000699 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000700 if (pwlen == 0) {
701 debug("empty password not supported");
702 } else if (locked && !lock) {
703 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
704 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
705 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000706 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000707 debug("agent unlocked");
708 locked = 0;
709 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000710 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000711 success = 1;
712 } else {
713 /* delay in 0.1s increments up to 10s */
714 if (fail_count < 100)
715 fail_count++;
716 delay = 100000 * fail_count;
717 debug("unlock failed, delaying %0.1lf seconds",
718 (double)delay/1000000);
719 usleep(delay);
720 }
721 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000722 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000723 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000724 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000725 arc4random_buf(lock_salt, sizeof(lock_salt));
726 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000727 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000728 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000729 success = 1;
730 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000731 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000732 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000733 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000734}
735
736static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000737no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000738{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000739 struct sshbuf *msg;
740 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000741
markus@openbsd.org139ca812015-01-14 13:09:09 +0000742 if ((msg = sshbuf_new()) == NULL)
743 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000744 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000745 (r = sshbuf_put_u32(msg, 0)) != 0 ||
746 (r = sshbuf_put_stringb(e->output, msg)) != 0)
747 fatal("%s: buffer error: %s", __func__, ssh_err(r));
748 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000749}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000750
Damien Miller7ea845e2010-02-12 09:21:02 +1100751#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000752static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000753process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000754{
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000755 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000756 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000757 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000758 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000759 u_char type;
760 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000761 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100762
markus@openbsd.org139ca812015-01-14 13:09:09 +0000763 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000764 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
765 error("%s: buffer error: %s", __func__, ssh_err(r));
766 goto send;
767 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000768
markus@openbsd.org139ca812015-01-14 13:09:09 +0000769 while (sshbuf_len(e->request)) {
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000770 if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
771 error("%s: buffer error: %s", __func__, ssh_err(r));
772 goto send;
773 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000774 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000775 case SSH_AGENT_CONSTRAIN_LIFETIME:
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000776 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
777 error("%s: buffer error: %s",
markus@openbsd.org139ca812015-01-14 13:09:09 +0000778 __func__, ssh_err(r));
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000779 goto send;
780 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000781 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000782 break;
783 case SSH_AGENT_CONSTRAIN_CONFIRM:
784 confirm = 1;
785 break;
786 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000787 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000788 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000789 }
790 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000791 if (realpath(provider, canonical_provider) == NULL) {
792 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
793 provider, strerror(errno));
794 goto send;
795 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000796 if (match_pattern_list(canonical_provider, provider_whitelist, 0) != 1) {
djm@openbsd.org786d5992016-11-30 03:07:37 +0000797 verbose("refusing PKCS#11 add of \"%.100s\": "
798 "provider not whitelisted", canonical_provider);
799 goto send;
800 }
801 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000802 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000803 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000804
djm@openbsd.org786d5992016-11-30 03:07:37 +0000805 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100806 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000807 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000808 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100809 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000810 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000811 id->provider = xstrdup(canonical_provider);
812 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000813 id->death = death;
814 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000815 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
816 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000817 success = 1;
818 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000819 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000820 }
821 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000822 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000823send:
Darren Tuckera627d422013-06-02 07:31:17 +1000824 free(pin);
825 free(provider);
826 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000827 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000828}
829
830static void
831process_remove_smartcard_key(SocketEntry *e)
832{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000833 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000834 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100835 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000836
markus@openbsd.org139ca812015-01-14 13:09:09 +0000837 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000838 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
839 error("%s: buffer error: %s", __func__, ssh_err(r));
840 goto send;
841 }
Darren Tuckera627d422013-06-02 07:31:17 +1000842 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000843
djm@openbsd.org25f83762017-03-15 02:25:09 +0000844 if (realpath(provider, canonical_provider) == NULL) {
845 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
846 provider, strerror(errno));
847 goto send;
848 }
849
850 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000851 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
852 nxt = TAILQ_NEXT(id, next);
853 /* Skip file--based keys */
854 if (id->provider == NULL)
855 continue;
856 if (!strcmp(canonical_provider, id->provider)) {
857 TAILQ_REMOVE(&idtab->idlist, id, next);
858 free_identity(id);
859 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000860 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000861 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000862 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100863 success = 1;
864 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000865 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000866send:
Darren Tuckera627d422013-06-02 07:31:17 +1000867 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000868 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000869}
Damien Miller7ea845e2010-02-12 09:21:02 +1100870#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000871
Damien Millerad833b32000-08-23 10:46:23 +1000872/* dispatch incoming messages */
873
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000874static int
875process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000877 u_int msg_len;
878 u_char type;
879 const u_char *cp;
880 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000881 SocketEntry *e;
882
883 if (socknum >= sockets_alloc) {
884 fatal("%s: socket number %u >= allocated %u",
885 __func__, socknum, sockets_alloc);
886 }
887 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000888
markus@openbsd.org139ca812015-01-14 13:09:09 +0000889 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000890 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000891 cp = sshbuf_ptr(e->input);
892 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000893 if (msg_len > AGENT_MAX_LEN) {
894 debug("%s: socket %u (fd=%d) message too long %u > %u",
895 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
896 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100897 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000898 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000899 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000900
901 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000902 sshbuf_reset(e->request);
903 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000904 (r = sshbuf_get_u8(e->request, &type)) != 0) {
905 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
906 r == SSH_ERR_STRING_TOO_LARGE) {
907 debug("%s: buffer error: %s", __func__, ssh_err(r));
908 return -1;
909 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000910 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000911 }
912
913 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
djm@openbsd.org001aa552018-04-10 00:10:49 +0000915 /* check whether agent is locked */
Ben Lindstrom2f717042002-06-06 21:52:03 +0000916 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000917 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000918 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000919 case SSH2_AGENTC_REQUEST_IDENTITIES:
920 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000921 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000922 break;
923 default:
924 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000925 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000926 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000927 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000928 }
929
Damien Miller95def091999-11-25 00:26:21 +1100930 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000931 case SSH_AGENTC_LOCK:
932 case SSH_AGENTC_UNLOCK:
933 process_lock_agent(e, type == SSH_AGENTC_LOCK);
934 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000935 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000936 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000937 break;
Damien Millerad833b32000-08-23 10:46:23 +1000938 /* ssh2 */
939 case SSH2_AGENTC_SIGN_REQUEST:
940 process_sign_request2(e);
941 break;
942 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000943 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000944 break;
945 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000946 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000947 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000948 break;
949 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000950 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000951 break;
952 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000953 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100954 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100955#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000956 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000957 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000958 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100959 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000960 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
961 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100962 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100963#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100964 default:
965 /* Unknown message. Respond with failure. */
966 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000967 sshbuf_reset(e->request);
968 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100969 break;
970 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000971 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972}
973
Ben Lindstrombba81212001-06-25 05:01:22 +0000974static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000975new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000977 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000978
Damien Miller232711f2004-06-15 10:35:30 +1000979 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980
Damien Miller95def091999-11-25 00:26:21 +1100981 if (fd > max_fd)
982 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983
Damien Miller95def091999-11-25 00:26:21 +1100984 for (i = 0; i < sockets_alloc; i++)
985 if (sockets[i].type == AUTH_UNUSED) {
986 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000987 if ((sockets[i].input = sshbuf_new()) == NULL)
988 fatal("%s: sshbuf_new failed", __func__);
989 if ((sockets[i].output = sshbuf_new()) == NULL)
990 fatal("%s: sshbuf_new failed", __func__);
991 if ((sockets[i].request = sshbuf_new()) == NULL)
992 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000993 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100994 return;
995 }
996 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000997 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000998 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000999 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001000 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +10001001 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +11001002 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +00001003 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
1004 fatal("%s: sshbuf_new failed", __func__);
1005 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
1006 fatal("%s: sshbuf_new failed", __func__);
1007 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
1008 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +10001009 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010}
1011
Ben Lindstrombba81212001-06-25 05:01:22 +00001012static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001013handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001015 struct sockaddr_un sunaddr;
1016 socklen_t slen;
1017 uid_t euid;
1018 gid_t egid;
1019 int fd;
1020
1021 slen = sizeof(sunaddr);
1022 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001023 if (fd == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001024 error("accept from AUTH_SOCKET: %s", strerror(errno));
1025 return -1;
1026 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001027 if (getpeereid(fd, &euid, &egid) == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001028 error("getpeereid %d failed: %s", fd, strerror(errno));
1029 close(fd);
1030 return -1;
1031 }
1032 if ((euid != 0) && (getuid() != euid)) {
1033 error("uid mismatch: peer euid %u != uid %u",
1034 (u_int) euid, (u_int) getuid());
1035 close(fd);
1036 return -1;
1037 }
1038 new_socket(AUTH_CONNECTION, fd);
1039 return 0;
1040}
1041
1042static int
1043handle_conn_read(u_int socknum)
1044{
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001045 char buf[AGENT_RBUF_LEN];
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001046 ssize_t len;
1047 int r;
1048
1049 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
1050 if (len == -1) {
1051 if (errno == EAGAIN || errno == EINTR)
1052 return 0;
1053 error("%s: read error on socket %u (fd %d): %s",
1054 __func__, socknum, sockets[socknum].fd,
1055 strerror(errno));
1056 }
1057 return -1;
1058 }
1059 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
1060 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1061 explicit_bzero(buf, sizeof(buf));
1062 process_message(socknum);
1063 return 0;
1064}
1065
1066static int
1067handle_conn_write(u_int socknum)
1068{
1069 ssize_t len;
1070 int r;
1071
1072 if (sshbuf_len(sockets[socknum].output) == 0)
1073 return 0; /* shouldn't happen */
1074 if ((len = write(sockets[socknum].fd,
1075 sshbuf_ptr(sockets[socknum].output),
1076 sshbuf_len(sockets[socknum].output))) <= 0) {
1077 if (len == -1) {
1078 if (errno == EAGAIN || errno == EINTR)
1079 return 0;
1080 error("%s: read error on socket %u (fd %d): %s",
1081 __func__, socknum, sockets[socknum].fd,
1082 strerror(errno));
1083 }
1084 return -1;
1085 }
1086 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
1087 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1088 return 0;
1089}
1090
1091static void
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001092after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001093{
1094 size_t i;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001095 u_int socknum, activefds = npfd;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001096
1097 for (i = 0; i < npfd; i++) {
1098 if (pfd[i].revents == 0)
1099 continue;
1100 /* Find sockets entry */
1101 for (socknum = 0; socknum < sockets_alloc; socknum++) {
1102 if (sockets[socknum].type != AUTH_SOCKET &&
1103 sockets[socknum].type != AUTH_CONNECTION)
1104 continue;
1105 if (pfd[i].fd == sockets[socknum].fd)
1106 break;
1107 }
1108 if (socknum >= sockets_alloc) {
1109 error("%s: no socket for fd %d", __func__, pfd[i].fd);
1110 continue;
1111 }
1112 /* Process events */
1113 switch (sockets[socknum].type) {
1114 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001115 if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
1116 break;
1117 if (npfd > maxfds) {
1118 debug3("out of fds (active %u >= limit %u); "
1119 "skipping accept", activefds, maxfds);
1120 break;
1121 }
1122 if (handle_socket_read(socknum) == 0)
1123 activefds++;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001124 break;
1125 case AUTH_CONNECTION:
1126 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
1127 handle_conn_read(socknum) != 0) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001128 goto close_sock;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001129 }
1130 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001131 handle_conn_write(socknum) != 0) {
1132 close_sock:
1133 if (activefds == 0)
1134 fatal("activefds == 0 at close_sock");
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001135 close_socket(&sockets[socknum]);
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001136 activefds--;
1137 break;
1138 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001139 break;
1140 default:
1141 break;
1142 }
1143 }
1144}
1145
1146static int
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001147prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001148{
1149 struct pollfd *pfd = *pfdp;
1150 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +10001151 time_t deadline;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001152 int r;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001153
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001154 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001155 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +11001156 switch (sockets[i].type) {
1157 case AUTH_SOCKET:
1158 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001159 npfd++;
Damien Miller95def091999-11-25 00:26:21 +11001160 break;
1161 case AUTH_UNUSED:
1162 break;
1163 default:
1164 fatal("Unknown socket type %d", sockets[i].type);
1165 break;
1166 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001167 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001168 if (npfd != *npfdp &&
1169 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
1170 fatal("%s: recallocarray failed", __func__);
1171 *pfdp = pfd;
1172 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001173
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001174 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 switch (sockets[i].type) {
1176 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001177 if (npfd > maxfds) {
1178 debug3("out of fds (active %zu >= limit %u); "
1179 "skipping arming listener", npfd, maxfds);
1180 break;
1181 }
1182 pfd[j].fd = sockets[i].fd;
1183 pfd[j].revents = 0;
1184 pfd[j].events = POLLIN;
1185 j++;
1186 break;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001187 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001188 pfd[j].fd = sockets[i].fd;
1189 pfd[j].revents = 0;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001190 /*
1191 * Only prepare to read if we can handle a full-size
1192 * input read buffer and enqueue a max size reply..
1193 */
1194 if ((r = sshbuf_check_reserve(sockets[i].input,
1195 AGENT_RBUF_LEN)) == 0 &&
1196 (r = sshbuf_check_reserve(sockets[i].output,
1197 AGENT_MAX_LEN)) == 0)
1198 pfd[j].events = POLLIN;
1199 else if (r != SSH_ERR_NO_BUFFER_SPACE) {
1200 fatal("%s: buffer error: %s",
1201 __func__, ssh_err(r));
1202 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001203 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001204 pfd[j].events |= POLLOUT;
1205 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001206 break;
1207 default:
1208 break;
1209 }
1210 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001211 deadline = reaper();
1212 if (parent_alive_interval != 0)
1213 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001214 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001215 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001216 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +11001217 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001218 if (deadline > INT_MAX / 1000)
1219 *timeoutp = INT_MAX / 1000;
1220 else
1221 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +11001222 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001223 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224}
1225
Ben Lindstrombba81212001-06-25 05:01:22 +00001226static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001227cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001228{
Damien Millerb1e967c2014-07-03 21:22:40 +10001229 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1230 return;
1231 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001232 if (socket_name[0])
1233 unlink(socket_name);
1234 if (socket_dir[0])
1235 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001236}
1237
Darren Tucker3e33cec2003-10-02 16:12:36 +10001238void
Damien Miller792c5111999-10-29 09:47:09 +10001239cleanup_exit(int i)
1240{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001241 cleanup_socket();
1242 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001243}
1244
Damien Miller1c13bd82006-03-26 14:28:14 +11001245/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001246static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001247cleanup_handler(int sig)
1248{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001249 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001250#ifdef ENABLE_PKCS11
1251 pkcs11_terminate();
1252#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001253 _exit(2);
1254}
1255
Ben Lindstrombba81212001-06-25 05:01:22 +00001256static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001257check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001258{
Darren Tucker3e78a512011-06-03 14:14:16 +10001259 /*
1260 * If our parent has exited then getppid() will return (pid_t)1,
1261 * so testing for that should be safe.
1262 */
1263 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001264 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001265 cleanup_socket();
1266 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001267 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001268}
1269
1270static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001271usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001272{
Damien Millerf0858de2014-04-20 13:01:30 +10001273 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001274 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001275 " [-P provider_whitelist] [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001276 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001277 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001278}
1279
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280int
1281main(int ac, char **av)
1282{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001283 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001284 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001285 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001286#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001287 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001288#endif
Damien Miller615f9392000-05-17 22:53:33 +10001289 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001290 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001291 pid_t pid;
1292 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001293 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001294 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001295 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001296 struct pollfd *pfd = NULL;
1297 size_t npfd = 0;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001298 u_int maxfds;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001299
Darren Tuckerce321d82005-10-03 18:11:24 +10001300 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1301 sanitise_stdfd();
1302
Damien Miller6cffb9a2002-09-04 16:20:26 +10001303 /* drop */
1304 setegid(getgid());
1305 setgid(getgid());
1306
Darren Tucker0fb7f592016-06-09 16:23:07 +10001307 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001308
Darren Tucker7694e9d2019-10-28 17:05:36 +11001309#ifdef RLIMIT_NOFILE
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001310 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1311 fatal("%s: getrlimit: %s", __progname, strerror(errno));
Darren Tucker7694e9d2019-10-28 17:05:36 +11001312#endif
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001313
Damien Miller59d3d5b2003-08-22 09:34:41 +10001314 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001315 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001316
djm@openbsd.org786d5992016-11-30 03:07:37 +00001317 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001318 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001319 case 'E':
1320 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1321 if (fingerprint_hash == -1)
1322 fatal("Invalid hash algorithm \"%s\"", optarg);
1323 break;
Damien Miller95def091999-11-25 00:26:21 +11001324 case 'c':
1325 if (s_flag)
1326 usage();
1327 c_flag++;
1328 break;
1329 case 'k':
1330 k_flag++;
1331 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001332 case 'P':
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001333 if (provider_whitelist != NULL)
djm@openbsd.org786d5992016-11-30 03:07:37 +00001334 fatal("-P option already specified");
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001335 provider_whitelist = xstrdup(optarg);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001336 break;
Damien Miller95def091999-11-25 00:26:21 +11001337 case 's':
1338 if (c_flag)
1339 usage();
1340 s_flag++;
1341 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001342 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001343 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001344 usage();
1345 d_flag++;
1346 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001347 case 'D':
1348 if (d_flag || D_flag)
1349 usage();
1350 D_flag++;
1351 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001352 case 'a':
1353 agentsocket = optarg;
1354 break;
Damien Miller53d81482003-01-22 11:47:19 +11001355 case 't':
1356 if ((lifetime = convtime(optarg)) == -1) {
1357 fprintf(stderr, "Invalid lifetime\n");
1358 usage();
1359 }
1360 break;
Damien Miller95def091999-11-25 00:26:21 +11001361 default:
1362 usage();
1363 }
Damien Miller792c5111999-10-29 09:47:09 +10001364 }
Damien Miller95def091999-11-25 00:26:21 +11001365 ac -= optind;
1366 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001367
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001368 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001369 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001370
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001371 if (provider_whitelist == NULL)
1372 provider_whitelist = xstrdup(DEFAULT_PROVIDER_WHITELIST);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001373
Ben Lindstromeecdf232002-04-02 21:03:51 +00001374 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001375 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001376 if (shell != NULL && (len = strlen(shell)) > 2 &&
1377 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001378 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001379 }
Damien Miller95def091999-11-25 00:26:21 +11001380 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001381 const char *errstr = NULL;
1382
Damien Miller95def091999-11-25 00:26:21 +11001383 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1384 if (pidstr == NULL) {
1385 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001386 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001387 exit(1);
1388 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001389 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1390 if (errstr) {
1391 fprintf(stderr,
1392 "%s=\"%s\", which is not a good PID: %s\n",
1393 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001394 exit(1);
1395 }
1396 if (kill(pid, SIGTERM) == -1) {
1397 perror("kill");
1398 exit(1);
1399 }
1400 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1401 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1402 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001403 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001404 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001405 }
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001406
1407 /*
1408 * Minimum file descriptors:
1409 * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1410 * a few spare for libc / stack protectors / sanitisers, etc.
1411 */
1412#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1413 if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
djm@openbsd.org960e7c62018-11-09 02:57:58 +00001414 fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001415 __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1416 maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1417
Damien Miller95def091999-11-25 00:26:21 +11001418 parent_pid = getpid();
1419
Ben Lindstromb7788f32002-06-06 21:46:08 +00001420 if (agentsocket == NULL) {
1421 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001422 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001423 if (mkdtemp(socket_dir) == NULL) {
1424 perror("mkdtemp: private socket dir");
1425 exit(1);
1426 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001427 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1428 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001429 } else {
1430 /* Try to use specified agent socket */
1431 socket_dir[0] = '\0';
1432 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001433 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434
Damien Miller5428f641999-11-25 11:54:57 +11001435 /*
1436 * Create socket early so it will exist before command gets run from
1437 * the parent.
1438 */
Damien Millerab2ec582014-07-18 15:04:47 +10001439 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001440 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001441 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001442 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001443 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001444 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001445 }
Damien Millerab2ec582014-07-18 15:04:47 +10001446 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001447
Damien Miller5428f641999-11-25 11:54:57 +11001448 /*
1449 * Fork, and have the parent execute the command, if any, or present
1450 * the socket data. The child continues as the authentication agent.
1451 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001452 if (D_flag || d_flag) {
1453 log_init(__progname,
1454 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1455 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001456 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1457 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1458 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001459 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001460 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001461 goto skip;
1462 }
Damien Miller95def091999-11-25 00:26:21 +11001463 pid = fork();
1464 if (pid == -1) {
1465 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001466 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001467 }
1468 if (pid != 0) { /* Parent - execute the given command. */
1469 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001470 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001471 if (ac == 0) {
1472 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1473 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001474 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001475 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001476 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001477 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001478 exit(0);
1479 }
Damien Millere4340be2000-09-16 13:29:08 +11001480 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1481 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1482 perror("setenv");
1483 exit(1);
1484 }
Damien Miller95def091999-11-25 00:26:21 +11001485 execvp(av[0], av);
1486 perror(av[0]);
1487 exit(1);
1488 }
Damien Millerc653b892002-02-08 22:05:41 +11001489 /* child */
1490 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001491
1492 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001493 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001494 cleanup_exit(1);
1495 }
1496
1497 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001498 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1499 /* XXX might close listen socket */
1500 (void)dup2(fd, STDIN_FILENO);
1501 (void)dup2(fd, STDOUT_FILENO);
1502 (void)dup2(fd, STDERR_FILENO);
1503 if (fd > 2)
1504 close(fd);
1505 }
Damien Miller95def091999-11-25 00:26:21 +11001506
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001507#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001508 /* deny core dumps, since memory contains unencrypted private keys */
1509 rlim.rlim_cur = rlim.rlim_max = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001510 if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001511 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001512 cleanup_exit(1);
1513 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001514#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001515
1516skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001517
Damien Millerb1e967c2014-07-03 21:22:40 +10001518 cleanup_pid = getpid();
1519
Damien Miller7ea845e2010-02-12 09:21:02 +11001520#ifdef ENABLE_PKCS11
1521 pkcs11_init(0);
1522#endif
Damien Miller95def091999-11-25 00:26:21 +11001523 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001524 if (ac > 0)
1525 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001526 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001527 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001528 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001529 signal(SIGHUP, cleanup_handler);
1530 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001531
djm@openbsd.org786d5992016-11-30 03:07:37 +00001532 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001533 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001534 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001535
Damien Miller95def091999-11-25 00:26:21 +11001536 while (1) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001537 prepare_poll(&pfd, &npfd, &timeout, maxfds);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001538 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001539 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001540 if (parent_alive_interval != 0)
1541 check_parent_exists();
1542 (void) reaper(); /* remove expired keys */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001543 if (result == -1) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001544 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001545 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001546 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001547 } else if (result > 0)
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001548 after_poll(pfd, npfd, maxfds);
Damien Miller95def091999-11-25 00:26:21 +11001549 }
1550 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551}