blob: 4b6c44e90adac22ae0e6a0ef4a8b9e683e7979fc [file] [log] [blame]
djm@openbsd.org6bff9522019-11-14 21:27:29 +00001/* $OpenBSD: ssh-agent.c,v 1.243 2019/11/14 21:27:30 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"
90#include "pathnames.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110091#include "ssh-pkcs11.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000092#include "ssh-sk.h"
Ben Lindstrom3f471632001-07-04 03:53:15 +000093
djm@openbsd.org07da39f2019-10-31 21:22:01 +000094#ifndef DEFAULT_PROVIDER_WHITELIST
95# define DEFAULT_PROVIDER_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
djm@openbsd.org786d5992016-11-30 03:07:37 +000096#endif
97
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000098/* Maximum accepted message length */
99#define AGENT_MAX_LEN (256*1024)
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000100/* Maximum bytes to read from client socket */
101#define AGENT_RBUF_LEN (4096)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000102
Ben Lindstrom65366a82001-12-06 16:32:47 +0000103typedef enum {
104 AUTH_UNUSED,
105 AUTH_SOCKET,
106 AUTH_CONNECTION
107} sock_type;
108
Damien Miller95def091999-11-25 00:26:21 +1100109typedef struct {
110 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000111 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000112 struct sshbuf *input;
113 struct sshbuf *output;
114 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115} SocketEntry;
116
Ben Lindstrom46c16222000-12-22 01:43:59 +0000117u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118SocketEntry *sockets = NULL;
119
Damien Miller1a534ae2002-01-22 23:26:13 +1100120typedef struct identity {
121 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000122 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100123 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100124 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000125 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100126 u_int confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000127 char *sk_provider;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128} Identity;
129
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000130struct idtable {
Damien Millerad833b32000-08-23 10:46:23 +1000131 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100132 TAILQ_HEAD(idqueue, identity) idlist;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000133};
Damien Millerad833b32000-08-23 10:46:23 +1000134
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000135/* private key table */
136struct idtable *idtab;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
138int max_fd = 0;
139
140/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000141pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000142time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Millerb1e967c2014-07-03 21:22:40 +1000144/* pid of process for which cleanup_socket is applicable */
145pid_t cleanup_pid = 0;
146
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000148char socket_name[PATH_MAX];
149char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000151/* PKCS#11/Security key path whitelist */
152static char *provider_whitelist;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000153
Ben Lindstrom2f717042002-06-06 21:52:03 +0000154/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000155#define LOCK_SIZE 32
156#define LOCK_SALT_SIZE 16
157#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000158int locked = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000159u_char lock_pwhash[LOCK_SIZE];
160u_char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000161
Damien Miller95def091999-11-25 00:26:21 +1100162extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100163
Darren Tucker55119252013-06-02 07:43:59 +1000164/* Default lifetime in seconds (0 == forever) */
165static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100166
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000167static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
168
Ben Lindstrombba81212001-06-25 05:01:22 +0000169static void
Damien Miller58f34862002-09-04 16:31:21 +1000170close_socket(SocketEntry *e)
171{
Damien Miller58f34862002-09-04 16:31:21 +1000172 close(e->fd);
173 e->fd = -1;
174 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000175 sshbuf_free(e->input);
176 sshbuf_free(e->output);
177 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000178}
179
180static void
Damien Millerad833b32000-08-23 10:46:23 +1000181idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000183 idtab = xcalloc(1, sizeof(*idtab));
184 TAILQ_INIT(&idtab->idlist);
185 idtab->nentries = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000186}
187
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000188static void
189free_identity(Identity *id)
190{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000191 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000192 free(id->provider);
193 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000194 free(id->sk_provider);
Darren Tuckera627d422013-06-02 07:31:17 +1000195 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000196}
197
Damien Millerad833b32000-08-23 10:46:23 +1000198/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100199static Identity *
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000200lookup_identity(struct sshkey *key)
Damien Millerad833b32000-08-23 10:46:23 +1000201{
Damien Miller1a534ae2002-01-22 23:26:13 +1100202 Identity *id;
203
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000204 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000205 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100206 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000207 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100208 return (NULL);
209}
210
Damien Miller6c711792003-01-24 11:36:23 +1100211/* Check confirmation of keysign request */
212static int
213confirm_key(Identity *id)
214{
Darren Tuckerce327b62004-11-05 20:38:03 +1100215 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100216 int ret = -1;
217
markus@openbsd.org139ca812015-01-14 13:09:09 +0000218 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000219 if (p != NULL &&
220 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100221 id->comment, p))
222 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000223 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100224
Damien Miller6c711792003-01-24 11:36:23 +1100225 return (ret);
226}
227
markus@openbsd.org139ca812015-01-14 13:09:09 +0000228static void
229send_status(SocketEntry *e, int success)
230{
231 int r;
232
233 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
234 (r = sshbuf_put_u8(e->output, success ?
235 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
236 fatal("%s: buffer error: %s", __func__, ssh_err(r));
237}
238
Damien Millerad833b32000-08-23 10:46:23 +1000239/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000241process_request_identities(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000242{
Damien Miller1a534ae2002-01-22 23:26:13 +1100243 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000244 struct sshbuf *msg;
245 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
markus@openbsd.org139ca812015-01-14 13:09:09 +0000247 if ((msg = sshbuf_new()) == NULL)
248 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000249 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
250 (r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000251 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000252 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000253 if ((r = sshkey_puts_opts(id->key, msg, SSHKEY_SERIALIZE_INFO))
254 != 0 ||
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000255 (r = sshbuf_put_cstring(msg, id->comment)) != 0) {
256 error("%s: put key/comment: %s", __func__,
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000257 ssh_err(r));
258 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000259 }
Damien Miller95def091999-11-25 00:26:21 +1100260 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000261 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
262 fatal("%s: buffer error: %s", __func__, ssh_err(r));
263 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264}
265
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000267static char *
268agent_decode_alg(struct sshkey *key, u_int flags)
269{
270 if (key->type == KEY_RSA) {
271 if (flags & SSH_AGENT_RSA_SHA2_256)
272 return "rsa-sha2-256";
273 else if (flags & SSH_AGENT_RSA_SHA2_512)
274 return "rsa-sha2-512";
djm@openbsd.org2317ce42019-06-14 03:51:47 +0000275 } else if (key->type == KEY_RSA_CERT) {
276 if (flags & SSH_AGENT_RSA_SHA2_256)
277 return "rsa-sha2-256-cert-v01@openssh.com";
278 else if (flags & SSH_AGENT_RSA_SHA2_512)
279 return "rsa-sha2-512-cert-v01@openssh.com";
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000280 }
281 return NULL;
282}
283
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000284static int
285provider_sign(const char *provider, struct sshkey *key,
286 u_char **sigp, size_t *lenp,
287 const u_char *data, size_t datalen,
288 const char *alg, u_int compat)
289{
290 int status, pair[2], r = SSH_ERR_INTERNAL_ERROR;
291 pid_t pid;
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000292 char *helper, *verbosity = NULL, *fp = NULL;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000293 struct sshbuf *kbuf, *req, *resp;
294 u_char version;
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000295 struct notifier_ctx *notifier = NULL;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000296
297 debug3("%s: start for provider %s", __func__, provider);
298
299 *sigp = NULL;
300 *lenp = 0;
301
djm@openbsd.org6bff9522019-11-14 21:27:29 +0000302#ifdef ENABLE_SK_INTERNAL
303 if (strcasecmp(provider, "internal") == 0) {
304 return sshsk_sign(provider, key, sigp, lenp,
305 data, datalen, compat);
306 }
307#endif
308
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000309 helper = getenv("SSH_SK_HELPER");
310 if (helper == NULL || strlen(helper) == 0)
311 helper = _PATH_SSH_SK_HELPER;
312 if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
313 verbosity = "-vvv";
314
315 /* Start helper */
316 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
317 error("socketpair: %s", strerror(errno));
318 return SSH_ERR_SYSTEM_ERROR;
319 }
320 if ((pid = fork()) == -1) {
321 error("fork: %s", strerror(errno));
322 close(pair[0]);
323 close(pair[1]);
324 return SSH_ERR_SYSTEM_ERROR;
325 }
326 if (pid == 0) {
327 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
328 (dup2(pair[1], STDOUT_FILENO) == -1))
329 fatal("%s: dup2: %s", __func__, ssh_err(r));
330 close(pair[0]);
331 close(pair[1]);
332 closefrom(STDERR_FILENO + 1);
333 debug("%s: starting %s %s", __func__, helper,
334 verbosity == NULL ? "" : verbosity);
335 execlp(helper, helper, verbosity, (char *)NULL);
336 fatal("%s: execlp: %s", __func__, strerror(errno));
337 }
338 close(pair[1]);
339
340 if ((kbuf = sshbuf_new()) == NULL ||
341 (req = sshbuf_new()) == NULL ||
342 (resp = sshbuf_new()) == NULL)
343 fatal("%s: sshbuf_new failed", __func__);
344
345 if ((r = sshkey_private_serialize(key, kbuf)) != 0 ||
346 (r = sshbuf_put_stringb(req, kbuf)) != 0 ||
347 (r = sshbuf_put_cstring(req, provider)) != 0 ||
348 (r = sshbuf_put_string(req, data, datalen)) != 0 ||
349 (r = sshbuf_put_u32(req, compat)) != 0)
350 fatal("%s: compose: %s", __func__, ssh_err(r));
351 if ((r = ssh_msg_send(pair[0], SSH_SK_HELPER_VERSION, req)) != 0) {
352 error("%s: send: %s", __func__, ssh_err(r));
353 goto out;
354 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000355 if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
356 SSH_FP_DEFAULT)) == NULL)
357 fatal("%s: sshkey_fingerprint failed", __func__);
358 notifier = notify_start(0,
359 "Confirm user presence for key %s %s", sshkey_type(key), fp);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000360 if ((r = ssh_msg_recv(pair[0], resp)) != 0) {
361 error("%s: receive: %s", __func__, ssh_err(r));
362 goto out;
363 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000364 notify_complete(notifier);
365 notifier = NULL;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000366 if ((r = sshbuf_get_u8(resp, &version)) != 0) {
367 error("%s: parse version: %s", __func__, ssh_err(r));
368 goto out;
369 }
370 if (version != SSH_SK_HELPER_VERSION) {
371 error("%s: unsupported version: got %u, expected %u",
372 __func__, version, SSH_SK_HELPER_VERSION);
373 r = SSH_ERR_INVALID_FORMAT;
374 goto out;
375 }
376 if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) {
377 error("%s: parse signature: %s", __func__, ssh_err(r));
378 r = SSH_ERR_INVALID_FORMAT;
379 goto out;
380 }
381 if (sshbuf_len(resp) != 0) {
382 error("%s: trailing data in response", __func__);
383 r = SSH_ERR_INVALID_FORMAT;
384 goto out;
385 }
386 /* success */
387 r = 0;
388 out:
389 while (waitpid(pid, &status, 0) == -1) {
390 if (errno != EINTR)
391 fatal("%s: waitpid: %s", __func__, ssh_err(r));
392 }
djm@openbsd.orge44bb612019-11-12 22:36:44 +0000393 notify_complete(notifier);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000394 if (!WIFEXITED(status)) {
395 error("%s: helper %s exited abnormally", __func__, helper);
396 if (r == 0)
397 r = SSH_ERR_SYSTEM_ERROR;
398 } else if (WEXITSTATUS(status) != 0) {
399 error("%s: helper %s exited with non-zero exit status",
400 __func__, helper);
401 if (r == 0)
402 r = SSH_ERR_SYSTEM_ERROR;
403 }
404 if (r != 0) {
405 freezero(*sigp, *lenp);
406 *sigp = NULL;
407 *lenp = 0;
408 }
409 sshbuf_free(kbuf);
410 sshbuf_free(req);
411 sshbuf_free(resp);
412 return r;
413}
414
Damien Millerad833b32000-08-23 10:46:23 +1000415/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Damien Millerad833b32000-08-23 10:46:23 +1000417process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000419 const u_char *data;
420 u_char *signature = NULL;
421 size_t dlen, slen = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000422 u_int compat = 0, flags;
423 int r, ok = -1;
424 struct sshbuf *msg;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000425 struct sshkey *key = NULL;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000426 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000427
djm@openbsd.org0088c572015-01-14 19:33:41 +0000428 if ((msg = sshbuf_new()) == NULL)
429 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000430 if ((r = sshkey_froms(e->request, &key)) != 0 ||
431 (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
djm@openbsd.org@openbsd.org93c68a82017-11-15 00:13:40 +0000432 (r = sshbuf_get_u32(e->request, &flags)) != 0) {
433 error("%s: couldn't parse request: %s", __func__, ssh_err(r));
434 goto send;
435 }
436
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000437 if ((id = lookup_identity(key)) == NULL) {
djm@openbsd.org0088c572015-01-14 19:33:41 +0000438 verbose("%s: %s key not found", __func__, sshkey_type(key));
439 goto send;
440 }
441 if (id->confirm && confirm_key(id) != 0) {
442 verbose("%s: user refused key", __func__);
443 goto send;
444 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000445 if (id->sk_provider != NULL) {
446 if ((r = provider_sign(id->sk_provider, id->key, &signature,
447 &slen, data, dlen, agent_decode_alg(key, flags),
448 compat)) != 0) {
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000449 error("%s: sign: %s", __func__, ssh_err(r));
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000450 goto send;
451 }
452 } else {
453 if ((r = sshkey_sign(id->key, &signature, &slen,
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000454 data, dlen, agent_decode_alg(key, flags),
455 NULL, compat)) != 0) {
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000456 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
457 goto send;
458 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000459 }
460 /* Success */
461 ok = 0;
462 send:
463 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000464 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000465 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
466 (r = sshbuf_put_string(msg, signature, slen)) != 0)
467 fatal("%s: buffer error: %s", __func__, ssh_err(r));
468 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
469 fatal("%s: buffer error: %s", __func__, ssh_err(r));
470
471 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
472 fatal("%s: buffer error: %s", __func__, ssh_err(r));
473
474 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000475 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000476}
477
478/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000479static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000480process_remove_identity(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000481{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000482 int r, success = 0;
483 struct sshkey *key = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000484 Identity *id;
Damien Miller7e8e8201999-11-16 13:37:16 +1100485
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000486 if ((r = sshkey_froms(e->request, &key)) != 0) {
487 error("%s: get key: %s", __func__, ssh_err(r));
488 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000489 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000490 if ((id = lookup_identity(key)) == NULL) {
491 debug("%s: key not found", __func__);
492 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000493 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000494 /* We have this key, free it. */
495 if (idtab->nentries < 1)
496 fatal("%s: internal error: nentries %d",
497 __func__, idtab->nentries);
498 TAILQ_REMOVE(&idtab->idlist, id, next);
499 free_identity(id);
500 idtab->nentries--;
501 sshkey_free(key);
502 success = 1;
503 done:
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
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000508process_remove_all_identities(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509{
Damien Miller1a534ae2002-01-22 23:26:13 +1100510 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000511
Damien Miller95def091999-11-25 00:26:21 +1100512 /* Loop over all identities and clear the keys. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000513 for (id = TAILQ_FIRST(&idtab->idlist); id;
514 id = TAILQ_FIRST(&idtab->idlist)) {
515 TAILQ_REMOVE(&idtab->idlist, id, next);
Damien Miller1a534ae2002-01-22 23:26:13 +1100516 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100517 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 /* Mark that there are no identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000520 idtab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000521
522 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000523 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100524}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000525
Darren Tucker2812dc92007-03-21 20:45:06 +1100526/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000527static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000528reaper(void)
529{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000530 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000531 Identity *id, *nxt;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000533 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
534 nxt = TAILQ_NEXT(id, next);
535 if (id->death == 0)
536 continue;
537 if (now >= id->death) {
538 debug("expiring key '%s'", id->comment);
539 TAILQ_REMOVE(&idtab->idlist, id, next);
540 free_identity(id);
541 idtab->nentries--;
542 } else
543 deadline = (deadline == 0) ? id->death :
544 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000545 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100546 if (deadline == 0 || deadline <= now)
547 return 0;
548 else
549 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000550}
551
552static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000553process_add_identity(SocketEntry *e)
Damien Miller95def091999-11-25 00:26:21 +1100554{
Damien Miller4c7728c2007-10-26 14:25:31 +1000555 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000556 int success = 0, confirm = 0;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000557 u_int seconds, maxsign;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000558 char *fp, *comment = NULL, *ext_name = NULL, *sk_provider = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000559 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000560 struct sshkey *k = NULL;
561 u_char ctype;
562 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100563
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000564 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
565 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000566 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
567 error("%s: decode private key: %s", __func__, ssh_err(r));
568 goto err;
569 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000570 while (sshbuf_len(e->request)) {
571 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
572 error("%s: buffer error: %s", __func__, ssh_err(r));
573 goto err;
574 }
575 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000576 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000577 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
578 error("%s: bad lifetime constraint: %s",
579 __func__, ssh_err(r));
580 goto err;
581 }
582 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000583 break;
Damien Miller6c711792003-01-24 11:36:23 +1100584 case SSH_AGENT_CONSTRAIN_CONFIRM:
585 confirm = 1;
586 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000587 case SSH_AGENT_CONSTRAIN_MAXSIGN:
588 if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
589 error("%s: bad maxsign constraint: %s",
590 __func__, ssh_err(r));
591 goto err;
592 }
593 if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
594 error("%s: cannot enable maxsign: %s",
595 __func__, ssh_err(r));
596 goto err;
597 }
598 break;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000599 case SSH_AGENT_CONSTRAIN_EXTENSION:
600 if ((r = sshbuf_get_cstring(e->request,
601 &ext_name, NULL)) != 0) {
602 error("%s: cannot parse extension: %s",
603 __func__, ssh_err(r));
604 goto err;
605 }
606 debug("%s: constraint ext %s", __func__, ext_name);
607 if (strcmp(ext_name, "sk-provider@openssh.com") == 0) {
608 if (sk_provider != NULL) {
609 error("%s already set", ext_name);
610 goto err;
611 }
612 if ((r = sshbuf_get_cstring(e->request,
613 &sk_provider, NULL)) != 0) {
614 error("%s: cannot parse %s: %s",
615 __func__, ext_name, ssh_err(r));
616 goto err;
617 }
618 } else {
619 error("%s: unsupported constraint \"%s\"",
620 __func__, ext_name);
621 goto err;
622 }
623 free(ext_name);
624 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000625 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000626 error("%s: Unknown constraint %d", __func__, ctype);
627 err:
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000628 free(sk_provider);
629 free(ext_name);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000630 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000631 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000632 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000633 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000634 }
635 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000636 if (sk_provider != NULL) {
markus@openbsd.org2c557442019-11-12 19:33:08 +0000637 if (!sshkey_is_sk(k)) {
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000638 error("Cannot add provider: %s is not a security key",
639 sshkey_type(k));
640 free(sk_provider);
641 goto send;
642 }
643 if (match_pattern_list(sk_provider,
644 provider_whitelist, 0) != 1) {
645 error("Refusing add key: provider %s not whitelisted",
646 sk_provider);
647 free(sk_provider);
648 goto send;
649 }
650 }
markus@openbsd.orgbf219922019-11-13 07:53:10 +0000651 if ((r = sshkey_shield_private(k)) != 0) {
652 error("%s: shield private key: %s", __func__, ssh_err(r));
653 goto err;
654 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000655
Damien Miller1cfadab2008-06-30 00:05:21 +1000656 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100657 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000658 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000659 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100660 id = xcalloc(1, sizeof(Identity));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000661 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000662 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000663 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000664 } else {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000665 /* key state might have been updated */
666 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000667 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000668 free(id->sk_provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000669 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000670 id->key = k;
Damien Miller4c7728c2007-10-26 14:25:31 +1000671 id->comment = comment;
672 id->death = death;
673 id->confirm = confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000674 id->sk_provider = sk_provider;
675
676 if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
677 SSH_FP_DEFAULT)) == NULL)
678 fatal("%s: sshkey_fingerprint failed", __func__);
679 debug("%s: add %s %s \"%.100s\" (life: %u) (confirm: %u) "
680 "(provider: %s)", __func__, sshkey_ssh_name(k), fp, comment,
681 seconds, confirm, sk_provider == NULL ? "none" : sk_provider);
682 free(fp);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000683send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000684 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000685}
686
Ben Lindstrom2f717042002-06-06 21:52:03 +0000687/* XXX todo: encrypt sensitive data with passphrase */
688static void
689process_lock_agent(SocketEntry *e, int lock)
690{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000691 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000692 char *passwd;
693 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000694 static u_int fail_count = 0;
695 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000696
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000697 /*
698 * This is deliberately fatal: the user has requested that we lock,
699 * but we can't parse their request properly. The only safe thing to
700 * do is abort.
701 */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000702 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000703 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000704 if (pwlen == 0) {
705 debug("empty password not supported");
706 } else if (locked && !lock) {
707 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
708 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
709 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000710 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000711 debug("agent unlocked");
712 locked = 0;
713 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000714 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000715 success = 1;
716 } else {
717 /* delay in 0.1s increments up to 10s */
718 if (fail_count < 100)
719 fail_count++;
720 delay = 100000 * fail_count;
721 debug("unlock failed, delaying %0.1lf seconds",
722 (double)delay/1000000);
723 usleep(delay);
724 }
725 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000726 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000727 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000728 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000729 arc4random_buf(lock_salt, sizeof(lock_salt));
730 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000731 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000732 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000733 success = 1;
734 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000735 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000736 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000737 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000738}
739
740static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000741no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000742{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000743 struct sshbuf *msg;
744 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000745
markus@openbsd.org139ca812015-01-14 13:09:09 +0000746 if ((msg = sshbuf_new()) == NULL)
747 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000748 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000749 (r = sshbuf_put_u32(msg, 0)) != 0 ||
750 (r = sshbuf_put_stringb(e->output, msg)) != 0)
751 fatal("%s: buffer error: %s", __func__, ssh_err(r));
752 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000753}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000754
Damien Miller7ea845e2010-02-12 09:21:02 +1100755#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000756static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000757process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000758{
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000759 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000760 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000761 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000762 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000763 u_char type;
764 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000765 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100766
markus@openbsd.org139ca812015-01-14 13:09:09 +0000767 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000768 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
769 error("%s: buffer error: %s", __func__, ssh_err(r));
770 goto send;
771 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000772
markus@openbsd.org139ca812015-01-14 13:09:09 +0000773 while (sshbuf_len(e->request)) {
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000774 if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
775 error("%s: buffer error: %s", __func__, ssh_err(r));
776 goto send;
777 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000778 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000779 case SSH_AGENT_CONSTRAIN_LIFETIME:
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000780 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
781 error("%s: buffer error: %s",
markus@openbsd.org139ca812015-01-14 13:09:09 +0000782 __func__, ssh_err(r));
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000783 goto send;
784 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000785 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000786 break;
787 case SSH_AGENT_CONSTRAIN_CONFIRM:
788 confirm = 1;
789 break;
790 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000791 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000792 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000793 }
794 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000795 if (realpath(provider, canonical_provider) == NULL) {
796 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
797 provider, strerror(errno));
798 goto send;
799 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000800 if (match_pattern_list(canonical_provider, provider_whitelist, 0) != 1) {
djm@openbsd.org786d5992016-11-30 03:07:37 +0000801 verbose("refusing PKCS#11 add of \"%.100s\": "
802 "provider not whitelisted", canonical_provider);
803 goto send;
804 }
805 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000806 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000807 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000808
djm@openbsd.org786d5992016-11-30 03:07:37 +0000809 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100810 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000811 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000812 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100813 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000814 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000815 id->provider = xstrdup(canonical_provider);
816 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000817 id->death = death;
818 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000819 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
820 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000821 success = 1;
822 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000823 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000824 }
825 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000826 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000827send:
Darren Tuckera627d422013-06-02 07:31:17 +1000828 free(pin);
829 free(provider);
830 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000831 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000832}
833
834static void
835process_remove_smartcard_key(SocketEntry *e)
836{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000837 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000838 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100839 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000840
markus@openbsd.org139ca812015-01-14 13:09:09 +0000841 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000842 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
843 error("%s: buffer error: %s", __func__, ssh_err(r));
844 goto send;
845 }
Darren Tuckera627d422013-06-02 07:31:17 +1000846 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000847
djm@openbsd.org25f83762017-03-15 02:25:09 +0000848 if (realpath(provider, canonical_provider) == NULL) {
849 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
850 provider, strerror(errno));
851 goto send;
852 }
853
854 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000855 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
856 nxt = TAILQ_NEXT(id, next);
857 /* Skip file--based keys */
858 if (id->provider == NULL)
859 continue;
860 if (!strcmp(canonical_provider, id->provider)) {
861 TAILQ_REMOVE(&idtab->idlist, id, next);
862 free_identity(id);
863 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000864 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000865 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000866 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100867 success = 1;
868 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000869 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000870send:
Darren Tuckera627d422013-06-02 07:31:17 +1000871 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000872 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000873}
Damien Miller7ea845e2010-02-12 09:21:02 +1100874#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000875
Damien Millerad833b32000-08-23 10:46:23 +1000876/* dispatch incoming messages */
877
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000878static int
879process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000881 u_int msg_len;
882 u_char type;
883 const u_char *cp;
884 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000885 SocketEntry *e;
886
887 if (socknum >= sockets_alloc) {
888 fatal("%s: socket number %u >= allocated %u",
889 __func__, socknum, sockets_alloc);
890 }
891 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000892
markus@openbsd.org139ca812015-01-14 13:09:09 +0000893 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000894 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000895 cp = sshbuf_ptr(e->input);
896 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000897 if (msg_len > AGENT_MAX_LEN) {
898 debug("%s: socket %u (fd=%d) message too long %u > %u",
899 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
900 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100901 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000902 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000903 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000904
905 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000906 sshbuf_reset(e->request);
907 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000908 (r = sshbuf_get_u8(e->request, &type)) != 0) {
909 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
910 r == SSH_ERR_STRING_TOO_LARGE) {
911 debug("%s: buffer error: %s", __func__, ssh_err(r));
912 return -1;
913 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000914 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000915 }
916
917 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000918
djm@openbsd.org001aa552018-04-10 00:10:49 +0000919 /* check whether agent is locked */
Ben Lindstrom2f717042002-06-06 21:52:03 +0000920 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000921 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000922 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000923 case SSH2_AGENTC_REQUEST_IDENTITIES:
924 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000925 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000926 break;
927 default:
928 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000929 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000930 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000931 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000932 }
933
Damien Miller95def091999-11-25 00:26:21 +1100934 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000935 case SSH_AGENTC_LOCK:
936 case SSH_AGENTC_UNLOCK:
937 process_lock_agent(e, type == SSH_AGENTC_LOCK);
938 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000939 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000940 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000941 break;
Damien Millerad833b32000-08-23 10:46:23 +1000942 /* ssh2 */
943 case SSH2_AGENTC_SIGN_REQUEST:
944 process_sign_request2(e);
945 break;
946 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000947 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000948 break;
949 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000950 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000951 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000952 break;
953 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000954 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000955 break;
956 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000957 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100958 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100959#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000960 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000961 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000962 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100963 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000964 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
965 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100966 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100967#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100968 default:
969 /* Unknown message. Respond with failure. */
970 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000971 sshbuf_reset(e->request);
972 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100973 break;
974 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000975 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976}
977
Ben Lindstrombba81212001-06-25 05:01:22 +0000978static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000979new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000981 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000982
Damien Miller232711f2004-06-15 10:35:30 +1000983 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Damien Miller95def091999-11-25 00:26:21 +1100985 if (fd > max_fd)
986 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987
Damien Miller95def091999-11-25 00:26:21 +1100988 for (i = 0; i < sockets_alloc; i++)
989 if (sockets[i].type == AUTH_UNUSED) {
990 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000991 if ((sockets[i].input = sshbuf_new()) == NULL)
992 fatal("%s: sshbuf_new failed", __func__);
993 if ((sockets[i].output = sshbuf_new()) == NULL)
994 fatal("%s: sshbuf_new failed", __func__);
995 if ((sockets[i].request = sshbuf_new()) == NULL)
996 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000997 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100998 return;
999 }
1000 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +10001001 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001002 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +10001003 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001004 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +10001005 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +11001006 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +00001007 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
1008 fatal("%s: sshbuf_new failed", __func__);
1009 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
1010 fatal("%s: sshbuf_new failed", __func__);
1011 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
1012 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +10001013 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014}
1015
Ben Lindstrombba81212001-06-25 05:01:22 +00001016static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001017handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001019 struct sockaddr_un sunaddr;
1020 socklen_t slen;
1021 uid_t euid;
1022 gid_t egid;
1023 int fd;
1024
1025 slen = sizeof(sunaddr);
1026 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001027 if (fd == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001028 error("accept from AUTH_SOCKET: %s", strerror(errno));
1029 return -1;
1030 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001031 if (getpeereid(fd, &euid, &egid) == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001032 error("getpeereid %d failed: %s", fd, strerror(errno));
1033 close(fd);
1034 return -1;
1035 }
1036 if ((euid != 0) && (getuid() != euid)) {
1037 error("uid mismatch: peer euid %u != uid %u",
1038 (u_int) euid, (u_int) getuid());
1039 close(fd);
1040 return -1;
1041 }
1042 new_socket(AUTH_CONNECTION, fd);
1043 return 0;
1044}
1045
1046static int
1047handle_conn_read(u_int socknum)
1048{
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001049 char buf[AGENT_RBUF_LEN];
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001050 ssize_t len;
1051 int r;
1052
1053 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
1054 if (len == -1) {
1055 if (errno == EAGAIN || errno == EINTR)
1056 return 0;
1057 error("%s: read error on socket %u (fd %d): %s",
1058 __func__, socknum, sockets[socknum].fd,
1059 strerror(errno));
1060 }
1061 return -1;
1062 }
1063 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
1064 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1065 explicit_bzero(buf, sizeof(buf));
1066 process_message(socknum);
1067 return 0;
1068}
1069
1070static int
1071handle_conn_write(u_int socknum)
1072{
1073 ssize_t len;
1074 int r;
1075
1076 if (sshbuf_len(sockets[socknum].output) == 0)
1077 return 0; /* shouldn't happen */
1078 if ((len = write(sockets[socknum].fd,
1079 sshbuf_ptr(sockets[socknum].output),
1080 sshbuf_len(sockets[socknum].output))) <= 0) {
1081 if (len == -1) {
1082 if (errno == EAGAIN || errno == EINTR)
1083 return 0;
1084 error("%s: read error on socket %u (fd %d): %s",
1085 __func__, socknum, sockets[socknum].fd,
1086 strerror(errno));
1087 }
1088 return -1;
1089 }
1090 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
1091 fatal("%s: buffer error: %s", __func__, ssh_err(r));
1092 return 0;
1093}
1094
1095static void
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001096after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001097{
1098 size_t i;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001099 u_int socknum, activefds = npfd;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001100
1101 for (i = 0; i < npfd; i++) {
1102 if (pfd[i].revents == 0)
1103 continue;
1104 /* Find sockets entry */
1105 for (socknum = 0; socknum < sockets_alloc; socknum++) {
1106 if (sockets[socknum].type != AUTH_SOCKET &&
1107 sockets[socknum].type != AUTH_CONNECTION)
1108 continue;
1109 if (pfd[i].fd == sockets[socknum].fd)
1110 break;
1111 }
1112 if (socknum >= sockets_alloc) {
1113 error("%s: no socket for fd %d", __func__, pfd[i].fd);
1114 continue;
1115 }
1116 /* Process events */
1117 switch (sockets[socknum].type) {
1118 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001119 if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
1120 break;
1121 if (npfd > maxfds) {
1122 debug3("out of fds (active %u >= limit %u); "
1123 "skipping accept", activefds, maxfds);
1124 break;
1125 }
1126 if (handle_socket_read(socknum) == 0)
1127 activefds++;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001128 break;
1129 case AUTH_CONNECTION:
1130 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
1131 handle_conn_read(socknum) != 0) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001132 goto close_sock;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001133 }
1134 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001135 handle_conn_write(socknum) != 0) {
1136 close_sock:
1137 if (activefds == 0)
1138 fatal("activefds == 0 at close_sock");
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001139 close_socket(&sockets[socknum]);
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001140 activefds--;
1141 break;
1142 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001143 break;
1144 default:
1145 break;
1146 }
1147 }
1148}
1149
1150static int
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001151prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001152{
1153 struct pollfd *pfd = *pfdp;
1154 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +10001155 time_t deadline;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001156 int r;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001157
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001158 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001159 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +11001160 switch (sockets[i].type) {
1161 case AUTH_SOCKET:
1162 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001163 npfd++;
Damien Miller95def091999-11-25 00:26:21 +11001164 break;
1165 case AUTH_UNUSED:
1166 break;
1167 default:
1168 fatal("Unknown socket type %d", sockets[i].type);
1169 break;
1170 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001171 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001172 if (npfd != *npfdp &&
1173 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
1174 fatal("%s: recallocarray failed", __func__);
1175 *pfdp = pfd;
1176 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001177
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001178 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001179 switch (sockets[i].type) {
1180 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001181 if (npfd > maxfds) {
1182 debug3("out of fds (active %zu >= limit %u); "
1183 "skipping arming listener", npfd, maxfds);
1184 break;
1185 }
1186 pfd[j].fd = sockets[i].fd;
1187 pfd[j].revents = 0;
1188 pfd[j].events = POLLIN;
1189 j++;
1190 break;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001191 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001192 pfd[j].fd = sockets[i].fd;
1193 pfd[j].revents = 0;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001194 /*
1195 * Only prepare to read if we can handle a full-size
1196 * input read buffer and enqueue a max size reply..
1197 */
1198 if ((r = sshbuf_check_reserve(sockets[i].input,
1199 AGENT_RBUF_LEN)) == 0 &&
1200 (r = sshbuf_check_reserve(sockets[i].output,
1201 AGENT_MAX_LEN)) == 0)
1202 pfd[j].events = POLLIN;
1203 else if (r != SSH_ERR_NO_BUFFER_SPACE) {
1204 fatal("%s: buffer error: %s",
1205 __func__, ssh_err(r));
1206 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001207 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001208 pfd[j].events |= POLLOUT;
1209 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001210 break;
1211 default:
1212 break;
1213 }
1214 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001215 deadline = reaper();
1216 if (parent_alive_interval != 0)
1217 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001218 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001219 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001220 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +11001221 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001222 if (deadline > INT_MAX / 1000)
1223 *timeoutp = INT_MAX / 1000;
1224 else
1225 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +11001226 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001227 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228}
1229
Ben Lindstrombba81212001-06-25 05:01:22 +00001230static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001231cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001232{
Damien Millerb1e967c2014-07-03 21:22:40 +10001233 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1234 return;
1235 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001236 if (socket_name[0])
1237 unlink(socket_name);
1238 if (socket_dir[0])
1239 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001240}
1241
Darren Tucker3e33cec2003-10-02 16:12:36 +10001242void
Damien Miller792c5111999-10-29 09:47:09 +10001243cleanup_exit(int i)
1244{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001245 cleanup_socket();
1246 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001247}
1248
Damien Miller1c13bd82006-03-26 14:28:14 +11001249/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001250static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001251cleanup_handler(int sig)
1252{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001253 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001254#ifdef ENABLE_PKCS11
1255 pkcs11_terminate();
1256#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001257 _exit(2);
1258}
1259
Ben Lindstrombba81212001-06-25 05:01:22 +00001260static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001261check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001262{
Darren Tucker3e78a512011-06-03 14:14:16 +10001263 /*
1264 * If our parent has exited then getppid() will return (pid_t)1,
1265 * so testing for that should be safe.
1266 */
1267 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001268 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001269 cleanup_socket();
1270 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001271 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001272}
1273
1274static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001275usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001276{
Damien Millerf0858de2014-04-20 13:01:30 +10001277 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001278 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001279 " [-P provider_whitelist] [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001280 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001281 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001282}
1283
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284int
1285main(int ac, char **av)
1286{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001287 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001288 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001289 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001290#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001291 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001292#endif
Damien Miller615f9392000-05-17 22:53:33 +10001293 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001294 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001295 pid_t pid;
1296 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001297 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001298 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001299 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001300 struct pollfd *pfd = NULL;
1301 size_t npfd = 0;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001302 u_int maxfds;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001303
Darren Tuckerce321d82005-10-03 18:11:24 +10001304 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1305 sanitise_stdfd();
1306
Damien Miller6cffb9a2002-09-04 16:20:26 +10001307 /* drop */
1308 setegid(getgid());
1309 setgid(getgid());
1310
Darren Tucker0fb7f592016-06-09 16:23:07 +10001311 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001312
Darren Tucker7694e9d2019-10-28 17:05:36 +11001313#ifdef RLIMIT_NOFILE
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001314 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1315 fatal("%s: getrlimit: %s", __progname, strerror(errno));
Darren Tucker7694e9d2019-10-28 17:05:36 +11001316#endif
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001317
Damien Miller59d3d5b2003-08-22 09:34:41 +10001318 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001319 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001320
djm@openbsd.org786d5992016-11-30 03:07:37 +00001321 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001322 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001323 case 'E':
1324 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1325 if (fingerprint_hash == -1)
1326 fatal("Invalid hash algorithm \"%s\"", optarg);
1327 break;
Damien Miller95def091999-11-25 00:26:21 +11001328 case 'c':
1329 if (s_flag)
1330 usage();
1331 c_flag++;
1332 break;
1333 case 'k':
1334 k_flag++;
1335 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001336 case 'P':
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001337 if (provider_whitelist != NULL)
djm@openbsd.org786d5992016-11-30 03:07:37 +00001338 fatal("-P option already specified");
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001339 provider_whitelist = xstrdup(optarg);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001340 break;
Damien Miller95def091999-11-25 00:26:21 +11001341 case 's':
1342 if (c_flag)
1343 usage();
1344 s_flag++;
1345 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001346 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001347 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001348 usage();
1349 d_flag++;
1350 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001351 case 'D':
1352 if (d_flag || D_flag)
1353 usage();
1354 D_flag++;
1355 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001356 case 'a':
1357 agentsocket = optarg;
1358 break;
Damien Miller53d81482003-01-22 11:47:19 +11001359 case 't':
1360 if ((lifetime = convtime(optarg)) == -1) {
1361 fprintf(stderr, "Invalid lifetime\n");
1362 usage();
1363 }
1364 break;
Damien Miller95def091999-11-25 00:26:21 +11001365 default:
1366 usage();
1367 }
Damien Miller792c5111999-10-29 09:47:09 +10001368 }
Damien Miller95def091999-11-25 00:26:21 +11001369 ac -= optind;
1370 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001371
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001372 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001373 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001374
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001375 if (provider_whitelist == NULL)
1376 provider_whitelist = xstrdup(DEFAULT_PROVIDER_WHITELIST);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001377
Ben Lindstromeecdf232002-04-02 21:03:51 +00001378 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001379 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001380 if (shell != NULL && (len = strlen(shell)) > 2 &&
1381 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001382 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001383 }
Damien Miller95def091999-11-25 00:26:21 +11001384 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001385 const char *errstr = NULL;
1386
Damien Miller95def091999-11-25 00:26:21 +11001387 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1388 if (pidstr == NULL) {
1389 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001390 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001391 exit(1);
1392 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001393 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1394 if (errstr) {
1395 fprintf(stderr,
1396 "%s=\"%s\", which is not a good PID: %s\n",
1397 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001398 exit(1);
1399 }
1400 if (kill(pid, SIGTERM) == -1) {
1401 perror("kill");
1402 exit(1);
1403 }
1404 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1405 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1406 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001407 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001408 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001409 }
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001410
1411 /*
1412 * Minimum file descriptors:
1413 * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1414 * a few spare for libc / stack protectors / sanitisers, etc.
1415 */
1416#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1417 if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
djm@openbsd.org960e7c62018-11-09 02:57:58 +00001418 fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001419 __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1420 maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1421
Damien Miller95def091999-11-25 00:26:21 +11001422 parent_pid = getpid();
1423
Ben Lindstromb7788f32002-06-06 21:46:08 +00001424 if (agentsocket == NULL) {
1425 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001426 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001427 if (mkdtemp(socket_dir) == NULL) {
1428 perror("mkdtemp: private socket dir");
1429 exit(1);
1430 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001431 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1432 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001433 } else {
1434 /* Try to use specified agent socket */
1435 socket_dir[0] = '\0';
1436 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001437 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001438
Damien Miller5428f641999-11-25 11:54:57 +11001439 /*
1440 * Create socket early so it will exist before command gets run from
1441 * the parent.
1442 */
Damien Millerab2ec582014-07-18 15:04:47 +10001443 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001444 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001445 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001446 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001447 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001448 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001449 }
Damien Millerab2ec582014-07-18 15:04:47 +10001450 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001451
Damien Miller5428f641999-11-25 11:54:57 +11001452 /*
1453 * Fork, and have the parent execute the command, if any, or present
1454 * the socket data. The child continues as the authentication agent.
1455 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001456 if (D_flag || d_flag) {
1457 log_init(__progname,
1458 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1459 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001460 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1461 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1462 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001463 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001464 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001465 goto skip;
1466 }
Damien Miller95def091999-11-25 00:26:21 +11001467 pid = fork();
1468 if (pid == -1) {
1469 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001470 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001471 }
1472 if (pid != 0) { /* Parent - execute the given command. */
1473 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001474 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001475 if (ac == 0) {
1476 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1477 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001478 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001479 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001480 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001481 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001482 exit(0);
1483 }
Damien Millere4340be2000-09-16 13:29:08 +11001484 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1485 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1486 perror("setenv");
1487 exit(1);
1488 }
Damien Miller95def091999-11-25 00:26:21 +11001489 execvp(av[0], av);
1490 perror(av[0]);
1491 exit(1);
1492 }
Damien Millerc653b892002-02-08 22:05:41 +11001493 /* child */
1494 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001495
1496 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001497 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001498 cleanup_exit(1);
1499 }
1500
1501 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001502 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1503 /* XXX might close listen socket */
1504 (void)dup2(fd, STDIN_FILENO);
1505 (void)dup2(fd, STDOUT_FILENO);
1506 (void)dup2(fd, STDERR_FILENO);
1507 if (fd > 2)
1508 close(fd);
1509 }
Damien Miller95def091999-11-25 00:26:21 +11001510
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001511#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001512 /* deny core dumps, since memory contains unencrypted private keys */
1513 rlim.rlim_cur = rlim.rlim_max = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001514 if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001515 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001516 cleanup_exit(1);
1517 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001518#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001519
1520skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001521
Damien Millerb1e967c2014-07-03 21:22:40 +10001522 cleanup_pid = getpid();
1523
Damien Miller7ea845e2010-02-12 09:21:02 +11001524#ifdef ENABLE_PKCS11
1525 pkcs11_init(0);
1526#endif
Damien Miller95def091999-11-25 00:26:21 +11001527 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001528 if (ac > 0)
1529 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001530 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001531 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001532 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001533 signal(SIGHUP, cleanup_handler);
1534 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001535
djm@openbsd.org786d5992016-11-30 03:07:37 +00001536 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001537 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001538 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001539
Damien Miller95def091999-11-25 00:26:21 +11001540 while (1) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001541 prepare_poll(&pfd, &npfd, &timeout, maxfds);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001542 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001543 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001544 if (parent_alive_interval != 0)
1545 check_parent_exists();
1546 (void) reaper(); /* remove expired keys */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001547 if (result == -1) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001548 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001549 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001550 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001551 } else if (result > 0)
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001552 after_poll(pfd, npfd, maxfds);
Damien Miller95def091999-11-25 00:26:21 +11001553 }
1554 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001555}