blob: 09d12dc3f86541f8a72fb9abc948a7b86f4e9b79 [file] [log] [blame]
djm@openbsd.orgb52ec0b2019-12-13 19:09:10 +00001/* $OpenBSD: ssh-agent.c,v 1.251 2019/12/13 19:09:10 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
djm@openbsd.org07da39f2019-10-31 21:22:01 +000044#include <sys/wait.h>
Damien Miller9aec9192006-08-05 10:57:45 +100045#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
Damien Miller574c41f2006-03-15 11:40:10 +110048#ifdef HAVE_SYS_UN_H
49# include <sys/un.h>
50#endif
Damien Miller9b481512002-09-12 10:43:29 +100051#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100052
Damien Miller1f0311c2014-05-15 14:24:09 +100053#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110055#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100056#endif
Damien Millere3476ed2006-07-24 14:13:33 +100057
Darren Tucker39972492006-07-12 22:22:46 +100058#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100059#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000060#include <limits.h>
Damien Miller03e20032006-03-15 11:16:59 +110061#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110062# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110063#endif
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000064#ifdef HAVE_POLL_H
65# include <poll.h>
66#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110067#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100068#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100069#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100070#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100071#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100072#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100073#include <unistd.h>
Damien Millere97201f2015-05-21 17:55:15 +100074#ifdef HAVE_UTIL_H
75# include <util.h>
76#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110077
Damien Millerd7834352006-08-05 12:39:39 +100078#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "ssh.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000080#include "sshbuf.h"
81#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110083#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000084#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110085#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110086#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000087#include "ssherr.h"
djm@openbsd.org786d5992016-11-30 03:07:37 +000088#include "match.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000089#include "msg.h"
djm@openbsd.org45ffa362019-11-15 02:38:07 +000090#include "ssherr.h"
djm@openbsd.org07da39f2019-10-31 21:22:01 +000091#include "pathnames.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110092#include "ssh-pkcs11.h"
djm@openbsd.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.org1b11ea72018-02-23 15:58:37 +0000431 u_int seconds, maxsign;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000432 char *fp, *comment = NULL, *ext_name = NULL, *sk_provider = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000433 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000434 struct sshkey *k = NULL;
435 u_char ctype;
436 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100437
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000438 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
439 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000440 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
441 error("%s: decode private key: %s", __func__, ssh_err(r));
442 goto err;
443 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000444 while (sshbuf_len(e->request)) {
445 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
446 error("%s: buffer error: %s", __func__, ssh_err(r));
447 goto err;
448 }
449 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000450 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000451 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
452 error("%s: bad lifetime constraint: %s",
453 __func__, ssh_err(r));
454 goto err;
455 }
456 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000457 break;
Damien Miller6c711792003-01-24 11:36:23 +1100458 case SSH_AGENT_CONSTRAIN_CONFIRM:
459 confirm = 1;
460 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000461 case SSH_AGENT_CONSTRAIN_MAXSIGN:
462 if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
463 error("%s: bad maxsign constraint: %s",
464 __func__, ssh_err(r));
465 goto err;
466 }
467 if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
468 error("%s: cannot enable maxsign: %s",
469 __func__, ssh_err(r));
470 goto err;
471 }
472 break;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000473 case SSH_AGENT_CONSTRAIN_EXTENSION:
474 if ((r = sshbuf_get_cstring(e->request,
475 &ext_name, NULL)) != 0) {
476 error("%s: cannot parse extension: %s",
477 __func__, ssh_err(r));
478 goto err;
479 }
480 debug("%s: constraint ext %s", __func__, ext_name);
481 if (strcmp(ext_name, "sk-provider@openssh.com") == 0) {
482 if (sk_provider != NULL) {
483 error("%s already set", ext_name);
484 goto err;
485 }
486 if ((r = sshbuf_get_cstring(e->request,
487 &sk_provider, NULL)) != 0) {
488 error("%s: cannot parse %s: %s",
489 __func__, ext_name, ssh_err(r));
490 goto err;
491 }
492 } else {
493 error("%s: unsupported constraint \"%s\"",
494 __func__, ext_name);
495 goto err;
496 }
497 free(ext_name);
498 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000499 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000500 error("%s: Unknown constraint %d", __func__, ctype);
501 err:
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000502 free(sk_provider);
503 free(ext_name);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000504 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000505 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000506 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000507 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000508 }
509 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000510 if (sk_provider != NULL) {
markus@openbsd.org2c557442019-11-12 19:33:08 +0000511 if (!sshkey_is_sk(k)) {
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000512 error("Cannot add provider: %s is not a security key",
513 sshkey_type(k));
514 free(sk_provider);
515 goto send;
516 }
djm@openbsd.orgab360062019-11-15 04:12:32 +0000517 if (strcasecmp(sk_provider, "internal") == 0)
518 debug("%s: internal provider", __func__);
519 else if (match_pattern_list(sk_provider,
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000520 provider_whitelist, 0) != 1) {
521 error("Refusing add key: provider %s not whitelisted",
522 sk_provider);
523 free(sk_provider);
524 goto send;
525 }
526 }
markus@openbsd.orgbf219922019-11-13 07:53:10 +0000527 if ((r = sshkey_shield_private(k)) != 0) {
528 error("%s: shield private key: %s", __func__, ssh_err(r));
529 goto err;
530 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000531
Damien Miller1cfadab2008-06-30 00:05:21 +1000532 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100533 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000534 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000535 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100536 id = xcalloc(1, sizeof(Identity));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000537 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000538 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000539 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000540 } else {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000541 /* key state might have been updated */
542 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000543 free(id->comment);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000544 free(id->sk_provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000545 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000546 id->key = k;
Damien Miller4c7728c2007-10-26 14:25:31 +1000547 id->comment = comment;
548 id->death = death;
549 id->confirm = confirm;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000550 id->sk_provider = sk_provider;
551
552 if ((fp = sshkey_fingerprint(k, SSH_FP_HASH_DEFAULT,
553 SSH_FP_DEFAULT)) == NULL)
554 fatal("%s: sshkey_fingerprint failed", __func__);
555 debug("%s: add %s %s \"%.100s\" (life: %u) (confirm: %u) "
556 "(provider: %s)", __func__, sshkey_ssh_name(k), fp, comment,
557 seconds, confirm, sk_provider == NULL ? "none" : sk_provider);
558 free(fp);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000559send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000560 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000561}
562
Ben Lindstrom2f717042002-06-06 21:52:03 +0000563/* XXX todo: encrypt sensitive data with passphrase */
564static void
565process_lock_agent(SocketEntry *e, int lock)
566{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000567 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000568 char *passwd;
569 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000570 static u_int fail_count = 0;
571 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000572
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000573 /*
574 * This is deliberately fatal: the user has requested that we lock,
575 * but we can't parse their request properly. The only safe thing to
576 * do is abort.
577 */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000578 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000579 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000580 if (pwlen == 0) {
581 debug("empty password not supported");
582 } else if (locked && !lock) {
583 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
584 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
585 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000586 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000587 debug("agent unlocked");
588 locked = 0;
589 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000590 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000591 success = 1;
592 } else {
593 /* delay in 0.1s increments up to 10s */
594 if (fail_count < 100)
595 fail_count++;
596 delay = 100000 * fail_count;
597 debug("unlock failed, delaying %0.1lf seconds",
598 (double)delay/1000000);
599 usleep(delay);
600 }
601 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000602 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000603 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000604 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000605 arc4random_buf(lock_salt, sizeof(lock_salt));
606 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000607 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000608 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000609 success = 1;
610 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000611 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000612 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000613 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000614}
615
616static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000617no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000618{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000619 struct sshbuf *msg;
620 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000621
markus@openbsd.org139ca812015-01-14 13:09:09 +0000622 if ((msg = sshbuf_new()) == NULL)
623 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000624 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000625 (r = sshbuf_put_u32(msg, 0)) != 0 ||
626 (r = sshbuf_put_stringb(e->output, msg)) != 0)
627 fatal("%s: buffer error: %s", __func__, ssh_err(r));
628 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000629}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000630
Damien Miller7ea845e2010-02-12 09:21:02 +1100631#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000632static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000633process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000634{
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000635 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000636 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000637 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000638 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000639 u_char type;
640 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000641 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100642
markus@openbsd.org139ca812015-01-14 13:09:09 +0000643 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000644 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
645 error("%s: buffer error: %s", __func__, ssh_err(r));
646 goto send;
647 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000648
markus@openbsd.org139ca812015-01-14 13:09:09 +0000649 while (sshbuf_len(e->request)) {
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000650 if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
651 error("%s: buffer error: %s", __func__, ssh_err(r));
652 goto send;
653 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000654 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000655 case SSH_AGENT_CONSTRAIN_LIFETIME:
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000656 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
657 error("%s: buffer error: %s",
markus@openbsd.org139ca812015-01-14 13:09:09 +0000658 __func__, ssh_err(r));
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000659 goto send;
660 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000661 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000662 break;
663 case SSH_AGENT_CONSTRAIN_CONFIRM:
664 confirm = 1;
665 break;
666 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000667 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000668 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000669 }
670 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000671 if (realpath(provider, canonical_provider) == NULL) {
672 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
673 provider, strerror(errno));
674 goto send;
675 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000676 if (match_pattern_list(canonical_provider, provider_whitelist, 0) != 1) {
djm@openbsd.org786d5992016-11-30 03:07:37 +0000677 verbose("refusing PKCS#11 add of \"%.100s\": "
678 "provider not whitelisted", canonical_provider);
679 goto send;
680 }
681 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000682 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000683 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000684
djm@openbsd.org786d5992016-11-30 03:07:37 +0000685 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100686 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000687 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000688 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100689 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000690 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000691 id->provider = xstrdup(canonical_provider);
692 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000693 id->death = death;
694 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000695 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
696 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000697 success = 1;
698 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000699 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000700 }
701 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000702 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000703send:
Darren Tuckera627d422013-06-02 07:31:17 +1000704 free(pin);
705 free(provider);
706 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000707 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000708}
709
710static void
711process_remove_smartcard_key(SocketEntry *e)
712{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000713 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000714 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100715 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000716
markus@openbsd.org139ca812015-01-14 13:09:09 +0000717 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000718 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
719 error("%s: buffer error: %s", __func__, ssh_err(r));
720 goto send;
721 }
Darren Tuckera627d422013-06-02 07:31:17 +1000722 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000723
djm@openbsd.org25f83762017-03-15 02:25:09 +0000724 if (realpath(provider, canonical_provider) == NULL) {
725 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
726 provider, strerror(errno));
727 goto send;
728 }
729
730 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000731 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
732 nxt = TAILQ_NEXT(id, next);
733 /* Skip file--based keys */
734 if (id->provider == NULL)
735 continue;
736 if (!strcmp(canonical_provider, id->provider)) {
737 TAILQ_REMOVE(&idtab->idlist, id, next);
738 free_identity(id);
739 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000740 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000741 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000742 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100743 success = 1;
744 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000745 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000746send:
Darren Tuckera627d422013-06-02 07:31:17 +1000747 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000748 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000749}
Damien Miller7ea845e2010-02-12 09:21:02 +1100750#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000751
Damien Millerad833b32000-08-23 10:46:23 +1000752/* dispatch incoming messages */
753
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000754static int
755process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000756{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000757 u_int msg_len;
758 u_char type;
759 const u_char *cp;
760 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000761 SocketEntry *e;
762
763 if (socknum >= sockets_alloc) {
764 fatal("%s: socket number %u >= allocated %u",
765 __func__, socknum, sockets_alloc);
766 }
767 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000768
markus@openbsd.org139ca812015-01-14 13:09:09 +0000769 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000770 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000771 cp = sshbuf_ptr(e->input);
772 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000773 if (msg_len > AGENT_MAX_LEN) {
774 debug("%s: socket %u (fd=%d) message too long %u > %u",
775 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
776 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100777 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000778 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000779 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000780
781 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000782 sshbuf_reset(e->request);
783 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000784 (r = sshbuf_get_u8(e->request, &type)) != 0) {
785 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
786 r == SSH_ERR_STRING_TOO_LARGE) {
787 debug("%s: buffer error: %s", __func__, ssh_err(r));
788 return -1;
789 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000790 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000791 }
792
793 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794
djm@openbsd.org001aa552018-04-10 00:10:49 +0000795 /* check whether agent is locked */
Ben Lindstrom2f717042002-06-06 21:52:03 +0000796 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000797 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000798 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000799 case SSH2_AGENTC_REQUEST_IDENTITIES:
800 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000801 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000802 break;
803 default:
804 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000805 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000806 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000807 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000808 }
809
Damien Miller95def091999-11-25 00:26:21 +1100810 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000811 case SSH_AGENTC_LOCK:
812 case SSH_AGENTC_UNLOCK:
813 process_lock_agent(e, type == SSH_AGENTC_LOCK);
814 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000815 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000816 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000817 break;
Damien Millerad833b32000-08-23 10:46:23 +1000818 /* ssh2 */
819 case SSH2_AGENTC_SIGN_REQUEST:
820 process_sign_request2(e);
821 break;
822 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000823 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000824 break;
825 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000826 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000827 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000828 break;
829 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000830 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000831 break;
832 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000833 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100834 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100835#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000836 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000837 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000838 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100839 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000840 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
841 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100842 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100843#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100844 default:
845 /* Unknown message. Respond with failure. */
846 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000847 sshbuf_reset(e->request);
848 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100849 break;
850 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000851 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852}
853
Ben Lindstrombba81212001-06-25 05:01:22 +0000854static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000855new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000856{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000857 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000858
Damien Miller232711f2004-06-15 10:35:30 +1000859 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000860
Damien Miller95def091999-11-25 00:26:21 +1100861 if (fd > max_fd)
862 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863
Damien Miller95def091999-11-25 00:26:21 +1100864 for (i = 0; i < sockets_alloc; i++)
865 if (sockets[i].type == AUTH_UNUSED) {
866 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000867 if ((sockets[i].input = sshbuf_new()) == NULL)
868 fatal("%s: sshbuf_new failed", __func__);
869 if ((sockets[i].output = sshbuf_new()) == NULL)
870 fatal("%s: sshbuf_new failed", __func__);
871 if ((sockets[i].request = sshbuf_new()) == NULL)
872 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000873 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100874 return;
875 }
876 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000877 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000878 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000879 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100880 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000881 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100882 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000883 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
884 fatal("%s: sshbuf_new failed", __func__);
885 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
886 fatal("%s: sshbuf_new failed", __func__);
887 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
888 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000889 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890}
891
Ben Lindstrombba81212001-06-25 05:01:22 +0000892static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000893handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000894{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000895 struct sockaddr_un sunaddr;
896 socklen_t slen;
897 uid_t euid;
898 gid_t egid;
899 int fd;
900
901 slen = sizeof(sunaddr);
902 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000903 if (fd == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000904 error("accept from AUTH_SOCKET: %s", strerror(errno));
905 return -1;
906 }
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000907 if (getpeereid(fd, &euid, &egid) == -1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000908 error("getpeereid %d failed: %s", fd, strerror(errno));
909 close(fd);
910 return -1;
911 }
912 if ((euid != 0) && (getuid() != euid)) {
913 error("uid mismatch: peer euid %u != uid %u",
914 (u_int) euid, (u_int) getuid());
915 close(fd);
916 return -1;
917 }
918 new_socket(AUTH_CONNECTION, fd);
919 return 0;
920}
921
922static int
923handle_conn_read(u_int socknum)
924{
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000925 char buf[AGENT_RBUF_LEN];
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000926 ssize_t len;
927 int r;
928
929 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
930 if (len == -1) {
931 if (errno == EAGAIN || errno == EINTR)
932 return 0;
933 error("%s: read error on socket %u (fd %d): %s",
934 __func__, socknum, sockets[socknum].fd,
935 strerror(errno));
936 }
937 return -1;
938 }
939 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
940 fatal("%s: buffer error: %s", __func__, ssh_err(r));
941 explicit_bzero(buf, sizeof(buf));
942 process_message(socknum);
943 return 0;
944}
945
946static int
947handle_conn_write(u_int socknum)
948{
949 ssize_t len;
950 int r;
951
952 if (sshbuf_len(sockets[socknum].output) == 0)
953 return 0; /* shouldn't happen */
954 if ((len = write(sockets[socknum].fd,
955 sshbuf_ptr(sockets[socknum].output),
956 sshbuf_len(sockets[socknum].output))) <= 0) {
957 if (len == -1) {
958 if (errno == EAGAIN || errno == EINTR)
959 return 0;
960 error("%s: read error on socket %u (fd %d): %s",
961 __func__, socknum, sockets[socknum].fd,
962 strerror(errno));
963 }
964 return -1;
965 }
966 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
967 fatal("%s: buffer error: %s", __func__, ssh_err(r));
968 return 0;
969}
970
971static void
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000972after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000973{
974 size_t i;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000975 u_int socknum, activefds = npfd;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000976
977 for (i = 0; i < npfd; i++) {
978 if (pfd[i].revents == 0)
979 continue;
980 /* Find sockets entry */
981 for (socknum = 0; socknum < sockets_alloc; socknum++) {
982 if (sockets[socknum].type != AUTH_SOCKET &&
983 sockets[socknum].type != AUTH_CONNECTION)
984 continue;
985 if (pfd[i].fd == sockets[socknum].fd)
986 break;
987 }
988 if (socknum >= sockets_alloc) {
989 error("%s: no socket for fd %d", __func__, pfd[i].fd);
990 continue;
991 }
992 /* Process events */
993 switch (sockets[socknum].type) {
994 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000995 if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
996 break;
997 if (npfd > maxfds) {
998 debug3("out of fds (active %u >= limit %u); "
999 "skipping accept", activefds, maxfds);
1000 break;
1001 }
1002 if (handle_socket_read(socknum) == 0)
1003 activefds++;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001004 break;
1005 case AUTH_CONNECTION:
1006 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
1007 handle_conn_read(socknum) != 0) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001008 goto close_sock;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001009 }
1010 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001011 handle_conn_write(socknum) != 0) {
1012 close_sock:
1013 if (activefds == 0)
1014 fatal("activefds == 0 at close_sock");
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001015 close_socket(&sockets[socknum]);
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001016 activefds--;
1017 break;
1018 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001019 break;
1020 default:
1021 break;
1022 }
1023 }
1024}
1025
1026static int
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001027prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001028{
1029 struct pollfd *pfd = *pfdp;
1030 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +10001031 time_t deadline;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001032 int r;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001033
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001034 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001035 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +11001036 switch (sockets[i].type) {
1037 case AUTH_SOCKET:
1038 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001039 npfd++;
Damien Miller95def091999-11-25 00:26:21 +11001040 break;
1041 case AUTH_UNUSED:
1042 break;
1043 default:
1044 fatal("Unknown socket type %d", sockets[i].type);
1045 break;
1046 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001047 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001048 if (npfd != *npfdp &&
1049 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
1050 fatal("%s: recallocarray failed", __func__);
1051 *pfdp = pfd;
1052 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001053
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001054 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001055 switch (sockets[i].type) {
1056 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001057 if (npfd > maxfds) {
1058 debug3("out of fds (active %zu >= limit %u); "
1059 "skipping arming listener", npfd, maxfds);
1060 break;
1061 }
1062 pfd[j].fd = sockets[i].fd;
1063 pfd[j].revents = 0;
1064 pfd[j].events = POLLIN;
1065 j++;
1066 break;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001067 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001068 pfd[j].fd = sockets[i].fd;
1069 pfd[j].revents = 0;
djm@openbsd.orgd6915882019-01-22 22:58:50 +00001070 /*
1071 * Only prepare to read if we can handle a full-size
1072 * input read buffer and enqueue a max size reply..
1073 */
1074 if ((r = sshbuf_check_reserve(sockets[i].input,
1075 AGENT_RBUF_LEN)) == 0 &&
1076 (r = sshbuf_check_reserve(sockets[i].output,
1077 AGENT_MAX_LEN)) == 0)
1078 pfd[j].events = POLLIN;
1079 else if (r != SSH_ERR_NO_BUFFER_SPACE) {
1080 fatal("%s: buffer error: %s",
1081 __func__, ssh_err(r));
1082 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001083 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001084 pfd[j].events |= POLLOUT;
1085 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001086 break;
1087 default:
1088 break;
1089 }
1090 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001091 deadline = reaper();
1092 if (parent_alive_interval != 0)
1093 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001094 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001095 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001096 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +11001097 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001098 if (deadline > INT_MAX / 1000)
1099 *timeoutp = INT_MAX / 1000;
1100 else
1101 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +11001102 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001103 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001104}
1105
Ben Lindstrombba81212001-06-25 05:01:22 +00001106static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001107cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001108{
Damien Millerb1e967c2014-07-03 21:22:40 +10001109 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1110 return;
1111 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001112 if (socket_name[0])
1113 unlink(socket_name);
1114 if (socket_dir[0])
1115 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116}
1117
Darren Tucker3e33cec2003-10-02 16:12:36 +10001118void
Damien Miller792c5111999-10-29 09:47:09 +10001119cleanup_exit(int i)
1120{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001121 cleanup_socket();
1122 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001123}
1124
Damien Miller1c13bd82006-03-26 14:28:14 +11001125/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001126static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001127cleanup_handler(int sig)
1128{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001129 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001130#ifdef ENABLE_PKCS11
1131 pkcs11_terminate();
1132#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001133 _exit(2);
1134}
1135
Ben Lindstrombba81212001-06-25 05:01:22 +00001136static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001137check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001138{
Darren Tucker3e78a512011-06-03 14:14:16 +10001139 /*
1140 * If our parent has exited then getppid() will return (pid_t)1,
1141 * so testing for that should be safe.
1142 */
1143 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001144 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001145 cleanup_socket();
1146 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001147 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001148}
1149
1150static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001151usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001152{
Damien Millerf0858de2014-04-20 13:01:30 +10001153 fprintf(stderr,
jmc@openbsd.org4402d6c2019-11-19 16:02:32 +00001154 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
1155 " [-P provider_whitelist] [-t life] [command [arg ...]]\n"
1156 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001157 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001158}
1159
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001160int
1161main(int ac, char **av)
1162{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001163 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001164 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001165 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001166#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001167 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001168#endif
Damien Miller615f9392000-05-17 22:53:33 +10001169 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001170 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001171 pid_t pid;
1172 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001173 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001174 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001175 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001176 struct pollfd *pfd = NULL;
1177 size_t npfd = 0;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001178 u_int maxfds;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001179
Darren Tuckerce321d82005-10-03 18:11:24 +10001180 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1181 sanitise_stdfd();
1182
Damien Miller6cffb9a2002-09-04 16:20:26 +10001183 /* drop */
1184 setegid(getgid());
1185 setgid(getgid());
1186
Darren Tucker0fb7f592016-06-09 16:23:07 +10001187 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001188
Darren Tucker7694e9d2019-10-28 17:05:36 +11001189#ifdef RLIMIT_NOFILE
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001190 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1191 fatal("%s: getrlimit: %s", __progname, strerror(errno));
Darren Tucker7694e9d2019-10-28 17:05:36 +11001192#endif
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001193
Damien Miller59d3d5b2003-08-22 09:34:41 +10001194 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001195 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001196
djm@openbsd.org786d5992016-11-30 03:07:37 +00001197 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001198 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001199 case 'E':
1200 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1201 if (fingerprint_hash == -1)
1202 fatal("Invalid hash algorithm \"%s\"", optarg);
1203 break;
Damien Miller95def091999-11-25 00:26:21 +11001204 case 'c':
1205 if (s_flag)
1206 usage();
1207 c_flag++;
1208 break;
1209 case 'k':
1210 k_flag++;
1211 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001212 case 'P':
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001213 if (provider_whitelist != NULL)
djm@openbsd.org786d5992016-11-30 03:07:37 +00001214 fatal("-P option already specified");
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001215 provider_whitelist = xstrdup(optarg);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001216 break;
Damien Miller95def091999-11-25 00:26:21 +11001217 case 's':
1218 if (c_flag)
1219 usage();
1220 s_flag++;
1221 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001222 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001223 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001224 usage();
1225 d_flag++;
1226 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001227 case 'D':
1228 if (d_flag || D_flag)
1229 usage();
1230 D_flag++;
1231 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001232 case 'a':
1233 agentsocket = optarg;
1234 break;
Damien Miller53d81482003-01-22 11:47:19 +11001235 case 't':
1236 if ((lifetime = convtime(optarg)) == -1) {
1237 fprintf(stderr, "Invalid lifetime\n");
1238 usage();
1239 }
1240 break;
Damien Miller95def091999-11-25 00:26:21 +11001241 default:
1242 usage();
1243 }
Damien Miller792c5111999-10-29 09:47:09 +10001244 }
Damien Miller95def091999-11-25 00:26:21 +11001245 ac -= optind;
1246 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001247
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001248 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001249 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001250
djm@openbsd.org07da39f2019-10-31 21:22:01 +00001251 if (provider_whitelist == NULL)
1252 provider_whitelist = xstrdup(DEFAULT_PROVIDER_WHITELIST);
djm@openbsd.org786d5992016-11-30 03:07:37 +00001253
Ben Lindstromeecdf232002-04-02 21:03:51 +00001254 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001255 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001256 if (shell != NULL && (len = strlen(shell)) > 2 &&
1257 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001258 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001259 }
Damien Miller95def091999-11-25 00:26:21 +11001260 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001261 const char *errstr = NULL;
1262
Damien Miller95def091999-11-25 00:26:21 +11001263 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1264 if (pidstr == NULL) {
1265 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001266 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001267 exit(1);
1268 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001269 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1270 if (errstr) {
1271 fprintf(stderr,
1272 "%s=\"%s\", which is not a good PID: %s\n",
1273 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001274 exit(1);
1275 }
1276 if (kill(pid, SIGTERM) == -1) {
1277 perror("kill");
1278 exit(1);
1279 }
1280 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1281 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1282 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001283 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001284 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001285 }
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001286
1287 /*
1288 * Minimum file descriptors:
1289 * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1290 * a few spare for libc / stack protectors / sanitisers, etc.
1291 */
1292#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1293 if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
djm@openbsd.org960e7c62018-11-09 02:57:58 +00001294 fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001295 __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1296 maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1297
Damien Miller95def091999-11-25 00:26:21 +11001298 parent_pid = getpid();
1299
Ben Lindstromb7788f32002-06-06 21:46:08 +00001300 if (agentsocket == NULL) {
1301 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001302 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001303 if (mkdtemp(socket_dir) == NULL) {
1304 perror("mkdtemp: private socket dir");
1305 exit(1);
1306 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001307 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1308 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001309 } else {
1310 /* Try to use specified agent socket */
1311 socket_dir[0] = '\0';
1312 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001313 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001314
Damien Miller5428f641999-11-25 11:54:57 +11001315 /*
1316 * Create socket early so it will exist before command gets run from
1317 * the parent.
1318 */
Damien Millerab2ec582014-07-18 15:04:47 +10001319 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001320 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001321 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001322 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001323 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001324 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001325 }
Damien Millerab2ec582014-07-18 15:04:47 +10001326 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001327
Damien Miller5428f641999-11-25 11:54:57 +11001328 /*
1329 * Fork, and have the parent execute the command, if any, or present
1330 * the socket data. The child continues as the authentication agent.
1331 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001332 if (D_flag || d_flag) {
1333 log_init(__progname,
1334 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1335 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001336 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1337 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1338 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001339 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001340 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001341 goto skip;
1342 }
Damien Miller95def091999-11-25 00:26:21 +11001343 pid = fork();
1344 if (pid == -1) {
1345 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001346 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001347 }
1348 if (pid != 0) { /* Parent - execute the given command. */
1349 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001350 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001351 if (ac == 0) {
1352 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1353 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001354 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001355 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001356 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001357 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001358 exit(0);
1359 }
Damien Millere4340be2000-09-16 13:29:08 +11001360 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1361 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1362 perror("setenv");
1363 exit(1);
1364 }
Damien Miller95def091999-11-25 00:26:21 +11001365 execvp(av[0], av);
1366 perror(av[0]);
1367 exit(1);
1368 }
Damien Millerc653b892002-02-08 22:05:41 +11001369 /* child */
1370 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001371
1372 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001373 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001374 cleanup_exit(1);
1375 }
1376
1377 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001378 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1379 /* XXX might close listen socket */
1380 (void)dup2(fd, STDIN_FILENO);
1381 (void)dup2(fd, STDOUT_FILENO);
1382 (void)dup2(fd, STDERR_FILENO);
1383 if (fd > 2)
1384 close(fd);
1385 }
Damien Miller95def091999-11-25 00:26:21 +11001386
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001387#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001388 /* deny core dumps, since memory contains unencrypted private keys */
1389 rlim.rlim_cur = rlim.rlim_max = 0;
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001390 if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001391 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001392 cleanup_exit(1);
1393 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001394#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001395
1396skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001397
Damien Millerb1e967c2014-07-03 21:22:40 +10001398 cleanup_pid = getpid();
1399
Damien Miller7ea845e2010-02-12 09:21:02 +11001400#ifdef ENABLE_PKCS11
1401 pkcs11_init(0);
1402#endif
Damien Miller95def091999-11-25 00:26:21 +11001403 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001404 if (ac > 0)
1405 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001406 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001407 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001408 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001409 signal(SIGHUP, cleanup_handler);
1410 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001411
djm@openbsd.org786d5992016-11-30 03:07:37 +00001412 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001413 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001414 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001415
Damien Miller95def091999-11-25 00:26:21 +11001416 while (1) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001417 prepare_poll(&pfd, &npfd, &timeout, maxfds);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001418 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001419 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001420 if (parent_alive_interval != 0)
1421 check_parent_exists();
1422 (void) reaper(); /* remove expired keys */
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +00001423 if (result == -1) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001424 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001425 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001426 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001427 } else if (result > 0)
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001428 after_poll(pfd, npfd, maxfds);
Damien Miller95def091999-11-25 00:26:21 +11001429 }
1430 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431}