blob: e081413b8154e5e05639673803e61cd98f1faca5 [file] [log] [blame]
markus@openbsd.org8fae3952020-03-06 18:28:27 +00001/* $OpenBSD: ssh-agent.c,v 1.257 2020/03/06 18:28:27 markus 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.orgb52ec0b2019-12-13 19:09:10 +000093#include "sk-api.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
Damien Millerad833b32000-08-23 10:46:23 +1000285/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000286static void
Damien Millerad833b32000-08-23 10:46:23 +1000287process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000288{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000289 const u_char *data;
290 u_char *signature = NULL;
291 size_t dlen, slen = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000292 u_int compat = 0, flags;
djm@openbsd.org05daa212019-11-16 22:36:48 +0000293 int r, ok = -1;
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +0000294 char *fp = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000295 struct sshbuf *msg;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000296 struct sshkey *key = NULL;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000297 struct identity *id;
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +0000298 struct notifier_ctx *notifier = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000299
djm@openbsd.org0088c572015-01-14 19:33:41 +0000300 if ((msg = sshbuf_new()) == NULL)
301 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000302 if ((r = sshkey_froms(e->request, &key)) != 0 ||
303 (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
djm@openbsd.org@openbsd.org93c68a82017-11-15 00:13:40 +0000304 (r = sshbuf_get_u32(e->request, &flags)) != 0) {
305 error("%s: couldn't parse request: %s", __func__, ssh_err(r));
306 goto send;
307 }
308
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000309 if ((id = lookup_identity(key)) == NULL) {
djm@openbsd.org0088c572015-01-14 19:33:41 +0000310 verbose("%s: %s key not found", __func__, sshkey_type(key));
311 goto send;
312 }
313 if (id->confirm && confirm_key(id) != 0) {
314 verbose("%s: user refused key", __func__);
315 goto send;
316 }
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +0000317 if (sshkey_is_sk(id->key) &&
318 (id->key->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
319 if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
320 SSH_FP_DEFAULT)) == NULL)
321 fatal("%s: fingerprint failed", __func__);
322 notifier = notify_start(0,
323 "Confirm user presence for key %s %s",
324 sshkey_type(id->key), fp);
325 }
326 if ((r = sshkey_sign(id->key, &signature, &slen,
327 data, dlen, agent_decode_alg(key, flags),
328 id->sk_provider, compat)) != 0) {
329 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
330 goto send;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000331 }
332 /* Success */
333 ok = 0;
334 send:
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +0000335 notify_complete(notifier);
djm@openbsd.org0088c572015-01-14 19:33:41 +0000336 sshkey_free(key);
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +0000337 free(fp);
Damien Millerad833b32000-08-23 10:46:23 +1000338 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000339 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
340 (r = sshbuf_put_string(msg, signature, slen)) != 0)
341 fatal("%s: buffer error: %s", __func__, ssh_err(r));
342 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
343 fatal("%s: buffer error: %s", __func__, ssh_err(r));
344
345 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
346 fatal("%s: buffer error: %s", __func__, ssh_err(r));
347
348 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000349 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000350}
351
352/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000353static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000354process_remove_identity(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000355{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000356 int r, success = 0;
357 struct sshkey *key = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000358 Identity *id;
Damien Miller7e8e8201999-11-16 13:37:16 +1100359
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000360 if ((r = sshkey_froms(e->request, &key)) != 0) {
361 error("%s: get key: %s", __func__, ssh_err(r));
362 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000363 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000364 if ((id = lookup_identity(key)) == NULL) {
365 debug("%s: key not found", __func__);
366 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000367 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000368 /* We have this key, free it. */
369 if (idtab->nentries < 1)
370 fatal("%s: internal error: nentries %d",
371 __func__, idtab->nentries);
372 TAILQ_REMOVE(&idtab->idlist, id, next);
373 free_identity(id);
374 idtab->nentries--;
375 sshkey_free(key);
376 success = 1;
377 done:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000378 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379}
380
Ben Lindstrombba81212001-06-25 05:01:22 +0000381static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000382process_remove_all_identities(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383{
Damien Miller1a534ae2002-01-22 23:26:13 +1100384 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 /* Loop over all identities and clear the keys. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000387 for (id = TAILQ_FIRST(&idtab->idlist); id;
388 id = TAILQ_FIRST(&idtab->idlist)) {
389 TAILQ_REMOVE(&idtab->idlist, id, next);
Damien Miller1a534ae2002-01-22 23:26:13 +1100390 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100391 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392
Damien Miller95def091999-11-25 00:26:21 +1100393 /* Mark that there are no identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000394 idtab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
396 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000397 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100398}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399
Darren Tucker2812dc92007-03-21 20:45:06 +1100400/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000401static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000402reaper(void)
403{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000404 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000405 Identity *id, *nxt;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000406
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000407 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
408 nxt = TAILQ_NEXT(id, next);
409 if (id->death == 0)
410 continue;
411 if (now >= id->death) {
412 debug("expiring key '%s'", id->comment);
413 TAILQ_REMOVE(&idtab->idlist, id, next);
414 free_identity(id);
415 idtab->nentries--;
416 } else
417 deadline = (deadline == 0) ? id->death :
418 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000419 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100420 if (deadline == 0 || deadline <= now)
421 return 0;
422 else
423 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000424}
425
426static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000427process_add_identity(SocketEntry *e)
Damien Miller95def091999-11-25 00:26:21 +1100428{
Damien Miller4c7728c2007-10-26 14:25:31 +1000429 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000430 int success = 0, confirm = 0;
markus@openbsd.org8fae3952020-03-06 18:28:27 +0000431 u_int seconds = 0, maxsign;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000432 char *fp, *comment = NULL, *ext_name = NULL, *sk_provider = NULL;
djm@openbsd.orge5a278a2020-01-25 00:06:48 +0000433 char canonical_provider[PATH_MAX];
Darren Tucker55119252013-06-02 07:43:59 +1000434 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000435 struct sshkey *k = NULL;
436 u_char ctype;
437 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100438
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000439 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
440 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000441 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
442 error("%s: decode private key: %s", __func__, ssh_err(r));
443 goto err;
444 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000445 while (sshbuf_len(e->request)) {
446 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
447 error("%s: buffer error: %s", __func__, ssh_err(r));
448 goto err;
449 }
450 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000451 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000452 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
453 error("%s: bad lifetime constraint: %s",
454 __func__, ssh_err(r));
455 goto err;
456 }
457 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000458 break;
Damien Miller6c711792003-01-24 11:36:23 +1100459 case SSH_AGENT_CONSTRAIN_CONFIRM:
460 confirm = 1;
461 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000462 case SSH_AGENT_CONSTRAIN_MAXSIGN:
463 if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
464 error("%s: bad maxsign constraint: %s",
465 __func__, ssh_err(r));
466 goto err;
467 }
468 if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
469 error("%s: cannot enable maxsign: %s",
470 __func__, ssh_err(r));
471 goto err;
472 }
473 break;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000474 case SSH_AGENT_CONSTRAIN_EXTENSION:
475 if ((r = sshbuf_get_cstring(e->request,
476 &ext_name, NULL)) != 0) {
477 error("%s: cannot parse extension: %s",
478 __func__, ssh_err(r));
479 goto err;
480 }
481 debug("%s: constraint ext %s", __func__, ext_name);
482 if (strcmp(ext_name, "sk-provider@openssh.com") == 0) {
483 if (sk_provider != NULL) {
484 error("%s already set", ext_name);
485 goto err;
486 }
487 if ((r = sshbuf_get_cstring(e->request,
488 &sk_provider, NULL)) != 0) {
489 error("%s: cannot parse %s: %s",
490 __func__, ext_name, ssh_err(r));
491 goto err;
492 }
493 } else {
494 error("%s: unsupported constraint \"%s\"",
495 __func__, ext_name);
496 goto err;
497 }
498 free(ext_name);
499 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000500 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000501 error("%s: Unknown constraint %d", __func__, ctype);
502 err:
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000503 free(sk_provider);
504 free(ext_name);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000505 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000506 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000507 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000508 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000509 }
510 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000511 if (sk_provider != NULL) {
markus@openbsd.org2c557442019-11-12 19:33:08 +0000512 if (!sshkey_is_sk(k)) {
naddy@openbsd.orga47f6a62020-02-06 22:30:54 +0000513 error("Cannot add provider: %s is not an "
514 "authenticator-hosted key", sshkey_type(k));
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000515 free(sk_provider);
516 goto send;
517 }
djm@openbsd.orge5a278a2020-01-25 00:06:48 +0000518 if (strcasecmp(sk_provider, "internal") == 0) {
djm@openbsd.orgab360062019-11-15 04:12:32 +0000519 debug("%s: internal provider", __func__);
djm@openbsd.orge5a278a2020-01-25 00:06:48 +0000520 } else {
521 if (realpath(sk_provider, canonical_provider) == NULL) {
522 verbose("failed provider \"%.100s\": "
523 "realpath: %s", sk_provider,
524 strerror(errno));
525 free(sk_provider);
526 goto send;
527 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000528 free(sk_provider);
djm@openbsd.orge5a278a2020-01-25 00:06:48 +0000529 sk_provider = xstrdup(canonical_provider);
530 if (match_pattern_list(sk_provider,
531 provider_whitelist, 0) != 1) {
532 error("Refusing add key: "
533 "provider %s not whitelisted", sk_provider);
534 free(sk_provider);
535 goto send;
536 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000537 }
538 }
markus@openbsd.orgbf219922019-11-13 07:53:10 +0000539 if ((r = sshkey_shield_private(k)) != 0) {
540 error("%s: shield private key: %s", __func__, ssh_err(r));
541 goto err;
542 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000543
Damien Miller1cfadab2008-06-30 00:05:21 +1000544 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100545 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000546 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000547 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100548 id = xcalloc(1, sizeof(Identity));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000549 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000550 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000551 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000552 } else {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000553 /* key state might have been updated */
554 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000555 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000556 free(id->sk_provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000557 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000558 id->key = k;
Damien Miller4c7728c2007-10-26 14:25:31 +1000559 id->comment = comment;
560 id->death = death;
561 id->confirm = confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000562 id->sk_provider = sk_provider;
563
564 if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
565 SSH_FP_DEFAULT)) == NULL)
566 fatal("%s: sshkey_fingerprint failed", __func__);
567 debug("%s: add %s %s \"%.100s\" (life: %u) (confirm: %u) "
568 "(provider: %s)", __func__, sshkey_ssh_name(k), fp, comment,
569 seconds, confirm, sk_provider == NULL ? "none" : sk_provider);
570 free(fp);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000571send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000572 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000573}
574
Ben Lindstrom2f717042002-06-06 21:52:03 +0000575/* XXX todo: encrypt sensitive data with passphrase */
576static void
577process_lock_agent(SocketEntry *e, int lock)
578{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000579 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000580 char *passwd;
581 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000582 static u_int fail_count = 0;
583 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000584
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000585 /*
586 * This is deliberately fatal: the user has requested that we lock,
587 * but we can't parse their request properly. The only safe thing to
588 * do is abort.
589 */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000590 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000591 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000592 if (pwlen == 0) {
593 debug("empty password not supported");
594 } else if (locked && !lock) {
595 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
596 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
597 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000598 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000599 debug("agent unlocked");
600 locked = 0;
601 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000602 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000603 success = 1;
604 } else {
605 /* delay in 0.1s increments up to 10s */
606 if (fail_count < 100)
607 fail_count++;
608 delay = 100000 * fail_count;
609 debug("unlock failed, delaying %0.1lf seconds",
610 (double)delay/1000000);
611 usleep(delay);
612 }
613 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000614 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000615 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000616 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000617 arc4random_buf(lock_salt, sizeof(lock_salt));
618 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000619 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000620 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000621 success = 1;
622 }
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +0000623 freezero(passwd, pwlen);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000624 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000625}
626
627static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000628no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000629{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000630 struct sshbuf *msg;
631 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000632
markus@openbsd.org139ca812015-01-14 13:09:09 +0000633 if ((msg = sshbuf_new()) == NULL)
634 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000635 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000636 (r = sshbuf_put_u32(msg, 0)) != 0 ||
637 (r = sshbuf_put_stringb(e->output, msg)) != 0)
638 fatal("%s: buffer error: %s", __func__, ssh_err(r));
639 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000640}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000641
Damien Miller7ea845e2010-02-12 09:21:02 +1100642#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000643static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000644process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000645{
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000646 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000647 char **comments = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000648 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000649 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000650 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000651 u_char type;
652 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000653 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100654
markus@openbsd.org139ca812015-01-14 13:09:09 +0000655 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000656 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
657 error("%s: buffer error: %s", __func__, ssh_err(r));
658 goto send;
659 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000660
markus@openbsd.org139ca812015-01-14 13:09:09 +0000661 while (sshbuf_len(e->request)) {
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000662 if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
663 error("%s: buffer error: %s", __func__, ssh_err(r));
664 goto send;
665 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000666 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000667 case SSH_AGENT_CONSTRAIN_LIFETIME:
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000668 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
669 error("%s: buffer error: %s",
markus@openbsd.org139ca812015-01-14 13:09:09 +0000670 __func__, ssh_err(r));
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000671 goto send;
672 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000673 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000674 break;
675 case SSH_AGENT_CONSTRAIN_CONFIRM:
676 confirm = 1;
677 break;
678 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000679 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000680 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000681 }
682 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000683 if (realpath(provider, canonical_provider) == NULL) {
684 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
685 provider, strerror(errno));
686 goto send;
687 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000688 if (match_pattern_list(canonical_provider, provider_whitelist, 0) != 1) {
djm@openbsd.org786d5992016-11-30 03:07:37 +0000689 verbose("refusing PKCS#11 add of \"%.100s\": "
690 "provider not whitelisted", canonical_provider);
691 goto send;
692 }
693 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000694 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000695 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000696
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000697 count = pkcs11_add_provider(canonical_provider, pin, &keys, &comments);
Damien Miller7ea845e2010-02-12 09:21:02 +1100698 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000699 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000700 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100701 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000702 id->key = k;
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000703 keys[i] = NULL; /* transferred */
djm@openbsd.org786d5992016-11-30 03:07:37 +0000704 id->provider = xstrdup(canonical_provider);
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000705 if (*comments[i] != '\0') {
706 id->comment = comments[i];
707 comments[i] = NULL; /* transferred */
708 } else {
709 id->comment = xstrdup(canonical_provider);
710 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000711 id->death = death;
712 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000713 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
714 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000715 success = 1;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000716 }
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000717 sshkey_free(keys[i]);
718 free(comments[i]);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000719 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000720send:
Darren Tuckera627d422013-06-02 07:31:17 +1000721 free(pin);
722 free(provider);
723 free(keys);
djm@openbsd.org89a8d452020-01-25 00:03:36 +0000724 free(comments);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000725 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000726}
727
728static void
729process_remove_smartcard_key(SocketEntry *e)
730{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000731 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000732 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100733 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000734
markus@openbsd.org139ca812015-01-14 13:09:09 +0000735 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000736 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
737 error("%s: buffer error: %s", __func__, ssh_err(r));
738 goto send;
739 }
Darren Tuckera627d422013-06-02 07:31:17 +1000740 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000741
djm@openbsd.org25f83762017-03-15 02:25:09 +0000742 if (realpath(provider, canonical_provider) == NULL) {
743 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
744 provider, strerror(errno));
745 goto send;
746 }
747
748 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000749 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
750 nxt = TAILQ_NEXT(id, next);
751 /* Skip file--based keys */
752 if (id->provider == NULL)
753 continue;
754 if (!strcmp(canonical_provider, id->provider)) {
755 TAILQ_REMOVE(&idtab->idlist, id, next);
756 free_identity(id);
757 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000758 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000759 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000760 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100761 success = 1;
762 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000763 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000764send:
Darren Tuckera627d422013-06-02 07:31:17 +1000765 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000766 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000767}
Damien Miller7ea845e2010-02-12 09:21:02 +1100768#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000769
Damien Millerad833b32000-08-23 10:46:23 +1000770/* dispatch incoming messages */
771
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000772static int
773process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000774{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000775 u_int msg_len;
776 u_char type;
777 const u_char *cp;
778 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000779 SocketEntry *e;
780
781 if (socknum >= sockets_alloc) {
782 fatal("%s: socket number %u >= allocated %u",
783 __func__, socknum, sockets_alloc);
784 }
785 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000786
markus@openbsd.org139ca812015-01-14 13:09:09 +0000787 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000788 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000789 cp = sshbuf_ptr(e->input);
790 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000791 if (msg_len > AGENT_MAX_LEN) {
792 debug("%s: socket %u (fd=%d) message too long %u > %u",
793 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
794 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100795 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000796 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000797 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000798
799 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000800 sshbuf_reset(e->request);
801 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000802 (r = sshbuf_get_u8(e->request, &type)) != 0) {
803 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
804 r == SSH_ERR_STRING_TOO_LARGE) {
805 debug("%s: buffer error: %s", __func__, ssh_err(r));
806 return -1;
807 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000808 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000809 }
810
811 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
djm@openbsd.org001aa552018-04-10 00:10:49 +0000813 /* check whether agent is locked */
Ben Lindstrom2f717042002-06-06 21:52:03 +0000814 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000815 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000816 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000817 case SSH2_AGENTC_REQUEST_IDENTITIES:
818 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000819 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000820 break;
821 default:
822 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000823 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000824 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000825 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000826 }
827
Damien Miller95def091999-11-25 00:26:21 +1100828 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000829 case SSH_AGENTC_LOCK:
830 case SSH_AGENTC_UNLOCK:
831 process_lock_agent(e, type == SSH_AGENTC_LOCK);
832 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000833 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000834 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000835 break;
Damien Millerad833b32000-08-23 10:46:23 +1000836 /* ssh2 */
837 case SSH2_AGENTC_SIGN_REQUEST:
838 process_sign_request2(e);
839 break;
840 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000841 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000842 break;
843 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000844 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000845 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000846 break;
847 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000848 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000849 break;
850 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000851 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100852 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100853#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000854 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000855 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000856 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100857 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000858 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
859 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100860 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100861#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100862 default:
863 /* Unknown message. Respond with failure. */
864 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000865 sshbuf_reset(e->request);
866 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100867 break;
868 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000869 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870}
871
Ben Lindstrombba81212001-06-25 05:01:22 +0000872static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000873new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000874{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000875 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000876
Damien Miller232711f2004-06-15 10:35:30 +1000877 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
Damien Miller95def091999-11-25 00:26:21 +1100879 if (fd > max_fd)
880 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
Damien Miller95def091999-11-25 00:26:21 +1100882 for (i = 0; i < sockets_alloc; i++)
883 if (sockets[i].type == AUTH_UNUSED) {
884 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000885 if ((sockets[i].input = sshbuf_new()) == NULL)
886 fatal("%s: sshbuf_new failed", __func__);
887 if ((sockets[i].output = sshbuf_new()) == NULL)
888 fatal("%s: sshbuf_new failed", __func__);
889 if ((sockets[i].request = sshbuf_new()) == NULL)
890 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000891 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100892 return;
893 }
894 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000895 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000896 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000897 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100898 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000899 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100900 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000901 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
902 fatal("%s: sshbuf_new failed", __func__);
903 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
904 fatal("%s: sshbuf_new failed", __func__);
905 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
906 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000907 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000908}
909
Ben Lindstrombba81212001-06-25 05:01:22 +0000910static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000911handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000913 struct sockaddr_un sunaddr;
914 socklen_t slen;
915 uid_t euid;
916 gid_t egid;
917 int fd;
918
919 slen = sizeof(sunaddr);
920 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000921 if (fd == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000922 error("accept from AUTH_SOCKET: %s", strerror(errno));
923 return -1;
924 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000925 if (getpeereid(fd, &euid, &egid) == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000926 error("getpeereid %d failed: %s", fd, strerror(errno));
927 close(fd);
928 return -1;
929 }
930 if ((euid != 0) && (getuid() != euid)) {
931 error("uid mismatch: peer euid %u != uid %u",
932 (u_int) euid, (u_int) getuid());
933 close(fd);
934 return -1;
935 }
936 new_socket(AUTH_CONNECTION, fd);
937 return 0;
938}
939
940static int
941handle_conn_read(u_int socknum)
942{
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000943 char buf[AGENT_RBUF_LEN];
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000944 ssize_t len;
945 int r;
946
947 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
948 if (len == -1) {
949 if (errno == EAGAIN || errno == EINTR)
950 return 0;
951 error("%s: read error on socket %u (fd %d): %s",
952 __func__, socknum, sockets[socknum].fd,
953 strerror(errno));
954 }
955 return -1;
956 }
957 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
958 fatal("%s: buffer error: %s", __func__, ssh_err(r));
959 explicit_bzero(buf, sizeof(buf));
960 process_message(socknum);
961 return 0;
962}
963
964static int
965handle_conn_write(u_int socknum)
966{
967 ssize_t len;
968 int r;
969
970 if (sshbuf_len(sockets[socknum].output) == 0)
971 return 0; /* shouldn't happen */
972 if ((len = write(sockets[socknum].fd,
973 sshbuf_ptr(sockets[socknum].output),
974 sshbuf_len(sockets[socknum].output))) <= 0) {
975 if (len == -1) {
976 if (errno == EAGAIN || errno == EINTR)
977 return 0;
978 error("%s: read error on socket %u (fd %d): %s",
979 __func__, socknum, sockets[socknum].fd,
980 strerror(errno));
981 }
982 return -1;
983 }
984 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
985 fatal("%s: buffer error: %s", __func__, ssh_err(r));
986 return 0;
987}
988
989static void
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000990after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000991{
992 size_t i;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000993 u_int socknum, activefds = npfd;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000994
995 for (i = 0; i < npfd; i++) {
996 if (pfd[i].revents == 0)
997 continue;
998 /* Find sockets entry */
999 for (socknum = 0; socknum < sockets_alloc; socknum++) {
1000 if (sockets[socknum].type != AUTH_SOCKET &&
1001 sockets[socknum].type != AUTH_CONNECTION)
1002 continue;
1003 if (pfd[i].fd == sockets[socknum].fd)
1004 break;
1005 }
1006 if (socknum >= sockets_alloc) {
1007 error("%s: no socket for fd %d", __func__, pfd[i].fd);
1008 continue;
1009 }
1010 /* Process events */
1011 switch (sockets[socknum].type) {
1012 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001013 if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
1014 break;
1015 if (npfd > maxfds) {
1016 debug3("out of fds (active %u >= limit %u); "
1017 "skipping accept", activefds, maxfds);
1018 break;
1019 }
1020 if (handle_socket_read(socknum) == 0)
1021 activefds++;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001022 break;
1023 case AUTH_CONNECTION:
1024 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
1025 handle_conn_read(socknum) != 0) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001026 goto close_sock;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001027 }
1028 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001029 handle_conn_write(socknum) != 0) {
1030 close_sock:
1031 if (activefds == 0)
1032 fatal("activefds == 0 at close_sock");
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001033 close_socket(&sockets[socknum]);
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001034 activefds--;
1035 break;
1036 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001037 break;
1038 default:
1039 break;
1040 }
1041 }
1042}
1043
1044static int
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001045prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001046{
1047 struct pollfd *pfd = *pfdp;
1048 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +10001049 time_t deadline;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001050 int r;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001051
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001052 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001053 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +11001054 switch (sockets[i].type) {
1055 case AUTH_SOCKET:
1056 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001057 npfd++;
Damien Miller95def091999-11-25 00:26:21 +11001058 break;
1059 case AUTH_UNUSED:
1060 break;
1061 default:
1062 fatal("Unknown socket type %d", sockets[i].type);
1063 break;
1064 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001065 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001066 if (npfd != *npfdp &&
1067 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
1068 fatal("%s: recallocarray failed", __func__);
1069 *pfdp = pfd;
1070 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001071
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001072 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001073 switch (sockets[i].type) {
1074 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001075 if (npfd > maxfds) {
1076 debug3("out of fds (active %zu >= limit %u); "
1077 "skipping arming listener", npfd, maxfds);
1078 break;
1079 }
1080 pfd[j].fd = sockets[i].fd;
1081 pfd[j].revents = 0;
1082 pfd[j].events = POLLIN;
1083 j++;
1084 break;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001085 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001086 pfd[j].fd = sockets[i].fd;
1087 pfd[j].revents = 0;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001088 /*
1089 * Only prepare to read if we can handle a full-size
1090 * input read buffer and enqueue a max size reply..
1091 */
1092 if ((r = sshbuf_check_reserve(sockets[i].input,
1093 AGENT_RBUF_LEN)) == 0 &&
1094 (r = sshbuf_check_reserve(sockets[i].output,
1095 AGENT_MAX_LEN)) == 0)
1096 pfd[j].events = POLLIN;
1097 else if (r != SSH_ERR_NO_BUFFER_SPACE) {
1098 fatal("%s: buffer error: %s",
1099 __func__, ssh_err(r));
1100 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001101 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001102 pfd[j].events |= POLLOUT;
1103 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001104 break;
1105 default:
1106 break;
1107 }
1108 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001109 deadline = reaper();
1110 if (parent_alive_interval != 0)
1111 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001112 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001113 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001114 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +11001115 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001116 if (deadline > INT_MAX / 1000)
1117 *timeoutp = INT_MAX / 1000;
1118 else
1119 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +11001120 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001121 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122}
1123
Ben Lindstrombba81212001-06-25 05:01:22 +00001124static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001125cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001126{
Damien Millerb1e967c2014-07-03 21:22:40 +10001127 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1128 return;
1129 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001130 if (socket_name[0])
1131 unlink(socket_name);
1132 if (socket_dir[0])
1133 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001134}
1135
Darren Tucker3e33cec2003-10-02 16:12:36 +10001136void
Damien Miller792c5111999-10-29 09:47:09 +10001137cleanup_exit(int i)
1138{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001139 cleanup_socket();
1140 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001141}
1142
Damien Miller1c13bd82006-03-26 14:28:14 +11001143/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001144static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001145cleanup_handler(int sig)
1146{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001147 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001148#ifdef ENABLE_PKCS11
1149 pkcs11_terminate();
1150#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001151 _exit(2);
1152}
1153
Ben Lindstrombba81212001-06-25 05:01:22 +00001154static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001155check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001156{
Darren Tucker3e78a512011-06-03 14:14:16 +10001157 /*
1158 * If our parent has exited then getppid() will return (pid_t)1,
1159 * so testing for that should be safe.
1160 */
1161 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001162 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001163 cleanup_socket();
1164 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001165 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001166}
1167
1168static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001169usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001170{
Damien Millerf0858de2014-04-20 13:01:30 +10001171 fprintf(stderr,
jmc@openbsd.org4402d6c2019-11-19 16:02:32 +00001172 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
1173 " [-P provider_whitelist] [-t life] [command [arg ...]]\n"
1174 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001175 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001176}
1177
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178int
1179main(int ac, char **av)
1180{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001181 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001182 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001183 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001184#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001185 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001186#endif
Damien Miller615f9392000-05-17 22:53:33 +10001187 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001188 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001189 pid_t pid;
1190 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001191 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001192 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001193 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001194 struct pollfd *pfd = NULL;
1195 size_t npfd = 0;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001196 u_int maxfds;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001197
Darren Tuckerce321d82005-10-03 18:11:24 +10001198 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1199 sanitise_stdfd();
1200
Damien Miller6cffb9a2002-09-04 16:20:26 +10001201 /* drop */
1202 setegid(getgid());
1203 setgid(getgid());
1204
Darren Tucker0fb7f592016-06-09 16:23:07 +10001205 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001206
Darren Tucker7694e9d2019-10-28 17:05:36 +11001207#ifdef RLIMIT_NOFILE
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001208 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1209 fatal("%s: getrlimit: %s", __progname, strerror(errno));
Darren Tucker7694e9d2019-10-28 17:05:36 +11001210#endif
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001211
Damien Miller59d3d5b2003-08-22 09:34:41 +10001212 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001213 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001214
djm@openbsd.org786d5992016-11-30 03:07:37 +00001215 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001216 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001217 case 'E':
1218 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1219 if (fingerprint_hash == -1)
1220 fatal("Invalid hash algorithm \"%s\"", optarg);
1221 break;
Damien Miller95def091999-11-25 00:26:21 +11001222 case 'c':
1223 if (s_flag)
1224 usage();
1225 c_flag++;
1226 break;
1227 case 'k':
1228 k_flag++;
1229 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001230 case 'P':
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001231 if (provider_whitelist != NULL)
djm@openbsd.org786d5992016-11-30 03:07:37 +00001232 fatal("-P option already specified");
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001233 provider_whitelist = xstrdup(optarg);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001234 break;
Damien Miller95def091999-11-25 00:26:21 +11001235 case 's':
1236 if (c_flag)
1237 usage();
1238 s_flag++;
1239 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001240 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001241 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001242 usage();
1243 d_flag++;
1244 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001245 case 'D':
1246 if (d_flag || D_flag)
1247 usage();
1248 D_flag++;
1249 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001250 case 'a':
1251 agentsocket = optarg;
1252 break;
Damien Miller53d81482003-01-22 11:47:19 +11001253 case 't':
1254 if ((lifetime = convtime(optarg)) == -1) {
1255 fprintf(stderr, "Invalid lifetime\n");
1256 usage();
1257 }
1258 break;
Damien Miller95def091999-11-25 00:26:21 +11001259 default:
1260 usage();
1261 }
Damien Miller792c5111999-10-29 09:47:09 +10001262 }
Damien Miller95def091999-11-25 00:26:21 +11001263 ac -= optind;
1264 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001265
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001266 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001267 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001268
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001269 if (provider_whitelist == NULL)
1270 provider_whitelist = xstrdup(DEFAULT_PROVIDER_WHITELIST);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001271
Ben Lindstromeecdf232002-04-02 21:03:51 +00001272 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001273 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001274 if (shell != NULL && (len = strlen(shell)) > 2 &&
1275 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001276 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001277 }
Damien Miller95def091999-11-25 00:26:21 +11001278 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001279 const char *errstr = NULL;
1280
Damien Miller95def091999-11-25 00:26:21 +11001281 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1282 if (pidstr == NULL) {
1283 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001284 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001285 exit(1);
1286 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001287 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1288 if (errstr) {
1289 fprintf(stderr,
1290 "%s=\"%s\", which is not a good PID: %s\n",
1291 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001292 exit(1);
1293 }
1294 if (kill(pid, SIGTERM) == -1) {
1295 perror("kill");
1296 exit(1);
1297 }
1298 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1299 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1300 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001301 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001302 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001303 }
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001304
1305 /*
1306 * Minimum file descriptors:
1307 * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1308 * a few spare for libc / stack protectors / sanitisers, etc.
1309 */
1310#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1311 if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
djm@openbsd.org960e7c62018-11-09 02:57:58 +00001312 fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001313 __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1314 maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1315
Damien Miller95def091999-11-25 00:26:21 +11001316 parent_pid = getpid();
1317
Ben Lindstromb7788f32002-06-06 21:46:08 +00001318 if (agentsocket == NULL) {
1319 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001320 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001321 if (mkdtemp(socket_dir) == NULL) {
1322 perror("mkdtemp: private socket dir");
1323 exit(1);
1324 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001325 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1326 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001327 } else {
1328 /* Try to use specified agent socket */
1329 socket_dir[0] = '\0';
1330 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001331 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332
Damien Miller5428f641999-11-25 11:54:57 +11001333 /*
1334 * Create socket early so it will exist before command gets run from
1335 * the parent.
1336 */
Damien Millerab2ec582014-07-18 15:04:47 +10001337 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001338 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001339 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001340 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001341 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001342 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001343 }
Damien Millerab2ec582014-07-18 15:04:47 +10001344 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001345
Damien Miller5428f641999-11-25 11:54:57 +11001346 /*
1347 * Fork, and have the parent execute the command, if any, or present
1348 * the socket data. The child continues as the authentication agent.
1349 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001350 if (D_flag || d_flag) {
1351 log_init(__progname,
1352 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1353 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001354 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1355 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1356 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001357 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001358 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001359 goto skip;
1360 }
Damien Miller95def091999-11-25 00:26:21 +11001361 pid = fork();
1362 if (pid == -1) {
1363 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001364 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001365 }
1366 if (pid != 0) { /* Parent - execute the given command. */
1367 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001368 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001369 if (ac == 0) {
1370 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1371 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001372 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001373 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001374 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001375 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001376 exit(0);
1377 }
Damien Millere4340be2000-09-16 13:29:08 +11001378 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1379 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1380 perror("setenv");
1381 exit(1);
1382 }
Damien Miller95def091999-11-25 00:26:21 +11001383 execvp(av[0], av);
1384 perror(av[0]);
1385 exit(1);
1386 }
Damien Millerc653b892002-02-08 22:05:41 +11001387 /* child */
1388 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001389
1390 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001391 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001392 cleanup_exit(1);
1393 }
1394
1395 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001396 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1397 /* XXX might close listen socket */
1398 (void)dup2(fd, STDIN_FILENO);
1399 (void)dup2(fd, STDOUT_FILENO);
1400 (void)dup2(fd, STDERR_FILENO);
1401 if (fd > 2)
1402 close(fd);
1403 }
Damien Miller95def091999-11-25 00:26:21 +11001404
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001405#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001406 /* deny core dumps, since memory contains unencrypted private keys */
1407 rlim.rlim_cur = rlim.rlim_max = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001408 if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001409 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001410 cleanup_exit(1);
1411 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001412#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001413
1414skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001415
Damien Millerb1e967c2014-07-03 21:22:40 +10001416 cleanup_pid = getpid();
1417
Damien Miller7ea845e2010-02-12 09:21:02 +11001418#ifdef ENABLE_PKCS11
1419 pkcs11_init(0);
1420#endif
Damien Miller95def091999-11-25 00:26:21 +11001421 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001422 if (ac > 0)
1423 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001424 idtab_init();
dtucker@openbsd.org3bf2a6a2020-01-23 07:10:22 +00001425 ssh_signal(SIGPIPE, SIG_IGN);
1426 ssh_signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
1427 ssh_signal(SIGHUP, cleanup_handler);
1428 ssh_signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001429
djm@openbsd.org786d5992016-11-30 03:07:37 +00001430 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001431 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001432 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001433
Damien Miller95def091999-11-25 00:26:21 +11001434 while (1) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001435 prepare_poll(&pfd, &npfd, &timeout, maxfds);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001436 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001437 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001438 if (parent_alive_interval != 0)
1439 check_parent_exists();
1440 (void) reaper(); /* remove expired keys */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001441 if (result == -1) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001442 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001443 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001444 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001445 } else if (result > 0)
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001446 after_poll(pfd, npfd, maxfds);
Damien Miller95def091999-11-25 00:26:21 +11001447 }
1448 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449}