blob: 034f31387564af002c51c9cd9073a624adba0419 [file] [log] [blame]
otto@openbsd.org0323d9b2019-06-06 05:13:13 +00001/* $OpenBSD: ssh-agent.c,v 1.234 2019/06/06 05:13:13 otto Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Miller1f0311c2014-05-15 14:24:09 +100052#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100053#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100055#endif
Damien Millere3476ed2006-07-24 14:13:33 +100056
Darren Tucker39972492006-07-12 22:22:46 +100057#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100058#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000059#include <limits.h>
Damien Miller03e20032006-03-15 11:16:59 +110060#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110061# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110062#endif
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000063#ifdef HAVE_POLL_H
64# include <poll.h>
65#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110066#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100067#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100068#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100069#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100070#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100071#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100072#include <unistd.h>
Damien Millere97201f2015-05-21 17:55:15 +100073#ifdef HAVE_UTIL_H
74# include <util.h>
75#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110076
Damien Millerd7834352006-08-05 12:39:39 +100077#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078#include "ssh.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000079#include "sshbuf.h"
80#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100081#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110082#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000083#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110084#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110085#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000086#include "ssherr.h"
djm@openbsd.org786d5992016-11-30 03:07:37 +000087#include "match.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller7ea845e2010-02-12 09:21:02 +110089#ifdef ENABLE_PKCS11
90#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000091#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000092
djm@openbsd.org786d5992016-11-30 03:07:37 +000093#ifndef DEFAULT_PKCS11_WHITELIST
djm@openbsd.orgb108ce92017-01-04 02:21:43 +000094# define DEFAULT_PKCS11_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
djm@openbsd.org786d5992016-11-30 03:07:37 +000095#endif
96
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +000097/* Maximum accepted message length */
98#define AGENT_MAX_LEN (256*1024)
djm@openbsd.orgd6915882019-01-22 22:58:50 +000099/* Maximum bytes to read from client socket */
100#define AGENT_RBUF_LEN (4096)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000101
Ben Lindstrom65366a82001-12-06 16:32:47 +0000102typedef enum {
103 AUTH_UNUSED,
104 AUTH_SOCKET,
105 AUTH_CONNECTION
106} sock_type;
107
Damien Miller95def091999-11-25 00:26:21 +1100108typedef struct {
109 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000110 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000111 struct sshbuf *input;
112 struct sshbuf *output;
113 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114} SocketEntry;
115
Ben Lindstrom46c16222000-12-22 01:43:59 +0000116u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117SocketEntry *sockets = NULL;
118
Damien Miller1a534ae2002-01-22 23:26:13 +1100119typedef struct identity {
120 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000121 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100122 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100123 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000124 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100125 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126} Identity;
127
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000128struct idtable {
Damien Millerad833b32000-08-23 10:46:23 +1000129 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100130 TAILQ_HEAD(idqueue, identity) idlist;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000131};
Damien Millerad833b32000-08-23 10:46:23 +1000132
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000133/* private key table */
134struct idtable *idtab;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
136int max_fd = 0;
137
138/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000139pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000140time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Millerb1e967c2014-07-03 21:22:40 +1000142/* pid of process for which cleanup_socket is applicable */
143pid_t cleanup_pid = 0;
144
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000146char socket_name[PATH_MAX];
147char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
djm@openbsd.org786d5992016-11-30 03:07:37 +0000149/* PKCS#11 path whitelist */
150static char *pkcs11_whitelist;
151
Ben Lindstrom2f717042002-06-06 21:52:03 +0000152/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000153#define LOCK_SIZE 32
154#define LOCK_SALT_SIZE 16
155#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000156int locked = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000157u_char lock_pwhash[LOCK_SIZE];
158u_char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000159
Damien Miller95def091999-11-25 00:26:21 +1100160extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100161
Darren Tucker55119252013-06-02 07:43:59 +1000162/* Default lifetime in seconds (0 == forever) */
163static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100164
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000165static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
166
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static void
Damien Miller58f34862002-09-04 16:31:21 +1000168close_socket(SocketEntry *e)
169{
Damien Miller58f34862002-09-04 16:31:21 +1000170 close(e->fd);
171 e->fd = -1;
172 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000173 sshbuf_free(e->input);
174 sshbuf_free(e->output);
175 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000176}
177
178static void
Damien Millerad833b32000-08-23 10:46:23 +1000179idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000181 idtab = xcalloc(1, sizeof(*idtab));
182 TAILQ_INIT(&idtab->idlist);
183 idtab->nentries = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000184}
185
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000186static void
187free_identity(Identity *id)
188{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000189 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000190 free(id->provider);
191 free(id->comment);
192 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000193}
194
Damien Millerad833b32000-08-23 10:46:23 +1000195/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100196static Identity *
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000197lookup_identity(struct sshkey *key)
Damien Millerad833b32000-08-23 10:46:23 +1000198{
Damien Miller1a534ae2002-01-22 23:26:13 +1100199 Identity *id;
200
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000201 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000202 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100203 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000204 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100205 return (NULL);
206}
207
Damien Miller6c711792003-01-24 11:36:23 +1100208/* Check confirmation of keysign request */
209static int
210confirm_key(Identity *id)
211{
Darren Tuckerce327b62004-11-05 20:38:03 +1100212 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100213 int ret = -1;
214
markus@openbsd.org139ca812015-01-14 13:09:09 +0000215 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000216 if (p != NULL &&
217 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100218 id->comment, p))
219 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000220 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100221
Damien Miller6c711792003-01-24 11:36:23 +1100222 return (ret);
223}
224
markus@openbsd.org139ca812015-01-14 13:09:09 +0000225static void
226send_status(SocketEntry *e, int success)
227{
228 int r;
229
230 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
231 (r = sshbuf_put_u8(e->output, success ?
232 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
233 fatal("%s: buffer error: %s", __func__, ssh_err(r));
234}
235
Damien Millerad833b32000-08-23 10:46:23 +1000236/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000238process_request_identities(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000239{
Damien Miller1a534ae2002-01-22 23:26:13 +1100240 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000241 struct sshbuf *msg;
242 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
markus@openbsd.org139ca812015-01-14 13:09:09 +0000244 if ((msg = sshbuf_new()) == NULL)
245 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000246 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
247 (r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000248 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000249 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000250 if ((r = sshkey_puts_opts(id->key, msg, SSHKEY_SERIALIZE_INFO))
251 != 0 ||
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000252 (r = sshbuf_put_cstring(msg, id->comment)) != 0) {
253 error("%s: put key/comment: %s", __func__,
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000254 ssh_err(r));
255 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000256 }
Damien Miller95def091999-11-25 00:26:21 +1100257 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000258 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
259 fatal("%s: buffer error: %s", __func__, ssh_err(r));
260 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261}
262
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000264static char *
265agent_decode_alg(struct sshkey *key, u_int flags)
266{
267 if (key->type == KEY_RSA) {
268 if (flags & SSH_AGENT_RSA_SHA2_256)
269 return "rsa-sha2-256";
270 else if (flags & SSH_AGENT_RSA_SHA2_512)
271 return "rsa-sha2-512";
272 }
273 return NULL;
274}
275
Damien Millerad833b32000-08-23 10:46:23 +1000276/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000277static void
Damien Millerad833b32000-08-23 10:46:23 +1000278process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000279{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000280 const u_char *data;
281 u_char *signature = NULL;
282 size_t dlen, slen = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000283 u_int compat = 0, flags;
284 int r, ok = -1;
285 struct sshbuf *msg;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000286 struct sshkey *key = NULL;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000287 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000288
djm@openbsd.org0088c572015-01-14 19:33:41 +0000289 if ((msg = sshbuf_new()) == NULL)
290 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000291 if ((r = sshkey_froms(e->request, &key)) != 0 ||
292 (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
djm@openbsd.org@openbsd.org93c68a82017-11-15 00:13:40 +0000293 (r = sshbuf_get_u32(e->request, &flags)) != 0) {
294 error("%s: couldn't parse request: %s", __func__, ssh_err(r));
295 goto send;
296 }
297
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000298 if ((id = lookup_identity(key)) == NULL) {
djm@openbsd.org0088c572015-01-14 19:33:41 +0000299 verbose("%s: %s key not found", __func__, sshkey_type(key));
300 goto send;
301 }
302 if (id->confirm && confirm_key(id) != 0) {
303 verbose("%s: user refused key", __func__);
304 goto send;
305 }
306 if ((r = sshkey_sign(id->key, &signature, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000307 data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000308 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000309 goto send;
310 }
311 /* Success */
312 ok = 0;
313 send:
314 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000315 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000316 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
317 (r = sshbuf_put_string(msg, signature, slen)) != 0)
318 fatal("%s: buffer error: %s", __func__, ssh_err(r));
319 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
320 fatal("%s: buffer error: %s", __func__, ssh_err(r));
321
322 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
323 fatal("%s: buffer error: %s", __func__, ssh_err(r));
324
325 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000326 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000327}
328
329/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000331process_remove_identity(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000332{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000333 int r, success = 0;
334 struct sshkey *key = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000335 Identity *id;
Damien Miller7e8e8201999-11-16 13:37:16 +1100336
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000337 if ((r = sshkey_froms(e->request, &key)) != 0) {
338 error("%s: get key: %s", __func__, ssh_err(r));
339 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000340 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000341 if ((id = lookup_identity(key)) == NULL) {
342 debug("%s: key not found", __func__);
343 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000344 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000345 /* We have this key, free it. */
346 if (idtab->nentries < 1)
347 fatal("%s: internal error: nentries %d",
348 __func__, idtab->nentries);
349 TAILQ_REMOVE(&idtab->idlist, id, next);
350 free_identity(id);
351 idtab->nentries--;
352 sshkey_free(key);
353 success = 1;
354 done:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000355 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356}
357
Ben Lindstrombba81212001-06-25 05:01:22 +0000358static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000359process_remove_all_identities(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360{
Damien Miller1a534ae2002-01-22 23:26:13 +1100361 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller95def091999-11-25 00:26:21 +1100363 /* Loop over all identities and clear the keys. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000364 for (id = TAILQ_FIRST(&idtab->idlist); id;
365 id = TAILQ_FIRST(&idtab->idlist)) {
366 TAILQ_REMOVE(&idtab->idlist, id, next);
Damien Miller1a534ae2002-01-22 23:26:13 +1100367 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100368 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000369
Damien Miller95def091999-11-25 00:26:21 +1100370 /* Mark that there are no identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000371 idtab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
373 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000374 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100375}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376
Darren Tucker2812dc92007-03-21 20:45:06 +1100377/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000378static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000379reaper(void)
380{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000381 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000382 Identity *id, *nxt;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000383
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000384 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
385 nxt = TAILQ_NEXT(id, next);
386 if (id->death == 0)
387 continue;
388 if (now >= id->death) {
389 debug("expiring key '%s'", id->comment);
390 TAILQ_REMOVE(&idtab->idlist, id, next);
391 free_identity(id);
392 idtab->nentries--;
393 } else
394 deadline = (deadline == 0) ? id->death :
395 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000396 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100397 if (deadline == 0 || deadline <= now)
398 return 0;
399 else
400 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000401}
402
403static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000404process_add_identity(SocketEntry *e)
Damien Miller95def091999-11-25 00:26:21 +1100405{
Damien Miller4c7728c2007-10-26 14:25:31 +1000406 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000407 int success = 0, confirm = 0;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000408 u_int seconds, maxsign;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000409 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000410 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000411 struct sshkey *k = NULL;
412 u_char ctype;
413 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100414
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000415 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
416 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000417 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
418 error("%s: decode private key: %s", __func__, ssh_err(r));
419 goto err;
420 }
Damien Miller86687062014-07-02 15:28:02 +1000421
markus@openbsd.org139ca812015-01-14 13:09:09 +0000422 while (sshbuf_len(e->request)) {
423 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
424 error("%s: buffer error: %s", __func__, ssh_err(r));
425 goto err;
426 }
427 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000428 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000429 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
430 error("%s: bad lifetime constraint: %s",
431 __func__, ssh_err(r));
432 goto err;
433 }
434 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000435 break;
Damien Miller6c711792003-01-24 11:36:23 +1100436 case SSH_AGENT_CONSTRAIN_CONFIRM:
437 confirm = 1;
438 break;
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000439 case SSH_AGENT_CONSTRAIN_MAXSIGN:
440 if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
441 error("%s: bad maxsign constraint: %s",
442 __func__, ssh_err(r));
443 goto err;
444 }
445 if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
446 error("%s: cannot enable maxsign: %s",
447 __func__, ssh_err(r));
448 goto err;
449 }
450 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000451 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000452 error("%s: Unknown constraint %d", __func__, ctype);
453 err:
454 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000455 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000456 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000457 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000458 }
459 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000460
Damien Miller1cfadab2008-06-30 00:05:21 +1000461 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100462 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000463 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000464 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100465 id = xcalloc(1, sizeof(Identity));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000466 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000467 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000468 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000469 } else {
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000470 /* key state might have been updated */
471 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000472 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000473 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000474 id->key = k;
Damien Miller4c7728c2007-10-26 14:25:31 +1000475 id->comment = comment;
476 id->death = death;
477 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000478send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000479 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000480}
481
Ben Lindstrom2f717042002-06-06 21:52:03 +0000482/* XXX todo: encrypt sensitive data with passphrase */
483static void
484process_lock_agent(SocketEntry *e, int lock)
485{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000486 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000487 char *passwd;
488 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000489 static u_int fail_count = 0;
490 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000491
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000492 /*
493 * This is deliberately fatal: the user has requested that we lock,
494 * but we can't parse their request properly. The only safe thing to
495 * do is abort.
496 */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000497 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000498 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000499 if (pwlen == 0) {
500 debug("empty password not supported");
501 } else if (locked && !lock) {
502 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
503 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
504 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000505 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000506 debug("agent unlocked");
507 locked = 0;
508 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000509 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000510 success = 1;
511 } else {
512 /* delay in 0.1s increments up to 10s */
513 if (fail_count < 100)
514 fail_count++;
515 delay = 100000 * fail_count;
516 debug("unlock failed, delaying %0.1lf seconds",
517 (double)delay/1000000);
518 usleep(delay);
519 }
520 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000521 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000522 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000523 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000524 arc4random_buf(lock_salt, sizeof(lock_salt));
525 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000526 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000527 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000528 success = 1;
529 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000530 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000531 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000532 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000533}
534
535static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000536no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000537{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000538 struct sshbuf *msg;
539 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000540
markus@openbsd.org139ca812015-01-14 13:09:09 +0000541 if ((msg = sshbuf_new()) == NULL)
542 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000543 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000544 (r = sshbuf_put_u32(msg, 0)) != 0 ||
545 (r = sshbuf_put_stringb(e->output, msg)) != 0)
546 fatal("%s: buffer error: %s", __func__, ssh_err(r));
547 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000548}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000549
Damien Miller7ea845e2010-02-12 09:21:02 +1100550#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000551static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000552process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000553{
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000554 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000555 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000556 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000557 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000558 u_char type;
559 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000560 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100561
markus@openbsd.org139ca812015-01-14 13:09:09 +0000562 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000563 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
564 error("%s: buffer error: %s", __func__, ssh_err(r));
565 goto send;
566 }
Damien Millerd94f20d2003-06-11 22:06:33 +1000567
markus@openbsd.org139ca812015-01-14 13:09:09 +0000568 while (sshbuf_len(e->request)) {
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000569 if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
570 error("%s: buffer error: %s", __func__, ssh_err(r));
571 goto send;
572 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000573 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000574 case SSH_AGENT_CONSTRAIN_LIFETIME:
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000575 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
576 error("%s: buffer error: %s",
markus@openbsd.org139ca812015-01-14 13:09:09 +0000577 __func__, ssh_err(r));
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000578 goto send;
579 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000580 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000581 break;
582 case SSH_AGENT_CONSTRAIN_CONFIRM:
583 confirm = 1;
584 break;
585 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000586 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000587 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000588 }
589 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000590 if (realpath(provider, canonical_provider) == NULL) {
591 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
592 provider, strerror(errno));
593 goto send;
594 }
595 if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) {
596 verbose("refusing PKCS#11 add of \"%.100s\": "
597 "provider not whitelisted", canonical_provider);
598 goto send;
599 }
600 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000601 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000602 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000603
djm@openbsd.org786d5992016-11-30 03:07:37 +0000604 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100605 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000606 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000607 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100608 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000609 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000610 id->provider = xstrdup(canonical_provider);
611 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000612 id->death = death;
613 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000614 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
615 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000616 success = 1;
617 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000618 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000619 }
620 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000621 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000622send:
Darren Tuckera627d422013-06-02 07:31:17 +1000623 free(pin);
624 free(provider);
625 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000626 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000627}
628
629static void
630process_remove_smartcard_key(SocketEntry *e)
631{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000632 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000633 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100634 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000635
markus@openbsd.org139ca812015-01-14 13:09:09 +0000636 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
djm@openbsd.org@openbsd.org83a1e5d2017-11-15 02:10:16 +0000637 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
638 error("%s: buffer error: %s", __func__, ssh_err(r));
639 goto send;
640 }
Darren Tuckera627d422013-06-02 07:31:17 +1000641 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000642
djm@openbsd.org25f83762017-03-15 02:25:09 +0000643 if (realpath(provider, canonical_provider) == NULL) {
644 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
645 provider, strerror(errno));
646 goto send;
647 }
648
649 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000650 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
651 nxt = TAILQ_NEXT(id, next);
652 /* Skip file--based keys */
653 if (id->provider == NULL)
654 continue;
655 if (!strcmp(canonical_provider, id->provider)) {
656 TAILQ_REMOVE(&idtab->idlist, id, next);
657 free_identity(id);
658 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000659 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000661 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100662 success = 1;
663 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000664 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000665send:
Darren Tuckera627d422013-06-02 07:31:17 +1000666 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000667 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000668}
Damien Miller7ea845e2010-02-12 09:21:02 +1100669#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000670
Damien Millerad833b32000-08-23 10:46:23 +1000671/* dispatch incoming messages */
672
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000673static int
674process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000675{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000676 u_int msg_len;
677 u_char type;
678 const u_char *cp;
679 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000680 SocketEntry *e;
681
682 if (socknum >= sockets_alloc) {
683 fatal("%s: socket number %u >= allocated %u",
684 __func__, socknum, sockets_alloc);
685 }
686 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000687
markus@openbsd.org139ca812015-01-14 13:09:09 +0000688 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000689 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000690 cp = sshbuf_ptr(e->input);
691 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000692 if (msg_len > AGENT_MAX_LEN) {
693 debug("%s: socket %u (fd=%d) message too long %u > %u",
694 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
695 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100696 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000697 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000698 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000699
700 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000701 sshbuf_reset(e->request);
702 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000703 (r = sshbuf_get_u8(e->request, &type)) != 0) {
704 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
705 r == SSH_ERR_STRING_TOO_LARGE) {
706 debug("%s: buffer error: %s", __func__, ssh_err(r));
707 return -1;
708 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000709 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000710 }
711
712 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713
djm@openbsd.org001aa552018-04-10 00:10:49 +0000714 /* check whether agent is locked */
Ben Lindstrom2f717042002-06-06 21:52:03 +0000715 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000716 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000717 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000718 case SSH2_AGENTC_REQUEST_IDENTITIES:
719 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000720 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000721 break;
722 default:
723 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000724 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000725 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000726 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000727 }
728
Damien Miller95def091999-11-25 00:26:21 +1100729 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000730 case SSH_AGENTC_LOCK:
731 case SSH_AGENTC_UNLOCK:
732 process_lock_agent(e, type == SSH_AGENTC_LOCK);
733 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000734 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000735 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000736 break;
Damien Millerad833b32000-08-23 10:46:23 +1000737 /* ssh2 */
738 case SSH2_AGENTC_SIGN_REQUEST:
739 process_sign_request2(e);
740 break;
741 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000742 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000743 break;
744 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000745 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000746 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000747 break;
748 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000749 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000750 break;
751 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000752 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100754#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000755 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000756 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000757 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100758 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000759 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
760 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100761 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100762#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100763 default:
764 /* Unknown message. Respond with failure. */
765 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000766 sshbuf_reset(e->request);
767 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100768 break;
769 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000770 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000771}
772
Ben Lindstrombba81212001-06-25 05:01:22 +0000773static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000774new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000775{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000776 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000777
Damien Miller232711f2004-06-15 10:35:30 +1000778 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779
Damien Miller95def091999-11-25 00:26:21 +1100780 if (fd > max_fd)
781 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782
Damien Miller95def091999-11-25 00:26:21 +1100783 for (i = 0; i < sockets_alloc; i++)
784 if (sockets[i].type == AUTH_UNUSED) {
785 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000786 if ((sockets[i].input = sshbuf_new()) == NULL)
787 fatal("%s: sshbuf_new failed", __func__);
788 if ((sockets[i].output = sshbuf_new()) == NULL)
789 fatal("%s: sshbuf_new failed", __func__);
790 if ((sockets[i].request = sshbuf_new()) == NULL)
791 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000792 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100793 return;
794 }
795 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000796 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000797 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000798 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100799 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000800 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100801 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000802 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
803 fatal("%s: sshbuf_new failed", __func__);
804 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
805 fatal("%s: sshbuf_new failed", __func__);
806 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
807 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000808 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000809}
810
Ben Lindstrombba81212001-06-25 05:01:22 +0000811static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000812handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000813{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000814 struct sockaddr_un sunaddr;
815 socklen_t slen;
816 uid_t euid;
817 gid_t egid;
818 int fd;
819
820 slen = sizeof(sunaddr);
821 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
822 if (fd < 0) {
823 error("accept from AUTH_SOCKET: %s", strerror(errno));
824 return -1;
825 }
826 if (getpeereid(fd, &euid, &egid) < 0) {
827 error("getpeereid %d failed: %s", fd, strerror(errno));
828 close(fd);
829 return -1;
830 }
831 if ((euid != 0) && (getuid() != euid)) {
832 error("uid mismatch: peer euid %u != uid %u",
833 (u_int) euid, (u_int) getuid());
834 close(fd);
835 return -1;
836 }
837 new_socket(AUTH_CONNECTION, fd);
838 return 0;
839}
840
841static int
842handle_conn_read(u_int socknum)
843{
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000844 char buf[AGENT_RBUF_LEN];
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000845 ssize_t len;
846 int r;
847
848 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
849 if (len == -1) {
850 if (errno == EAGAIN || errno == EINTR)
851 return 0;
852 error("%s: read error on socket %u (fd %d): %s",
853 __func__, socknum, sockets[socknum].fd,
854 strerror(errno));
855 }
856 return -1;
857 }
858 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
859 fatal("%s: buffer error: %s", __func__, ssh_err(r));
860 explicit_bzero(buf, sizeof(buf));
861 process_message(socknum);
862 return 0;
863}
864
865static int
866handle_conn_write(u_int socknum)
867{
868 ssize_t len;
869 int r;
870
871 if (sshbuf_len(sockets[socknum].output) == 0)
872 return 0; /* shouldn't happen */
873 if ((len = write(sockets[socknum].fd,
874 sshbuf_ptr(sockets[socknum].output),
875 sshbuf_len(sockets[socknum].output))) <= 0) {
876 if (len == -1) {
877 if (errno == EAGAIN || errno == EINTR)
878 return 0;
879 error("%s: read error on socket %u (fd %d): %s",
880 __func__, socknum, sockets[socknum].fd,
881 strerror(errno));
882 }
883 return -1;
884 }
885 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
886 fatal("%s: buffer error: %s", __func__, ssh_err(r));
887 return 0;
888}
889
890static void
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000891after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000892{
893 size_t i;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000894 u_int socknum, activefds = npfd;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000895
896 for (i = 0; i < npfd; i++) {
897 if (pfd[i].revents == 0)
898 continue;
899 /* Find sockets entry */
900 for (socknum = 0; socknum < sockets_alloc; socknum++) {
901 if (sockets[socknum].type != AUTH_SOCKET &&
902 sockets[socknum].type != AUTH_CONNECTION)
903 continue;
904 if (pfd[i].fd == sockets[socknum].fd)
905 break;
906 }
907 if (socknum >= sockets_alloc) {
908 error("%s: no socket for fd %d", __func__, pfd[i].fd);
909 continue;
910 }
911 /* Process events */
912 switch (sockets[socknum].type) {
913 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000914 if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
915 break;
916 if (npfd > maxfds) {
917 debug3("out of fds (active %u >= limit %u); "
918 "skipping accept", activefds, maxfds);
919 break;
920 }
921 if (handle_socket_read(socknum) == 0)
922 activefds++;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000923 break;
924 case AUTH_CONNECTION:
925 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
926 handle_conn_read(socknum) != 0) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000927 goto close_sock;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000928 }
929 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000930 handle_conn_write(socknum) != 0) {
931 close_sock:
932 if (activefds == 0)
933 fatal("activefds == 0 at close_sock");
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000934 close_socket(&sockets[socknum]);
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000935 activefds--;
936 break;
937 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000938 break;
939 default:
940 break;
941 }
942 }
943}
944
945static int
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000946prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000947{
948 struct pollfd *pfd = *pfdp;
949 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +1000950 time_t deadline;
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000951 int r;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000952
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000953 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100955 switch (sockets[i].type) {
956 case AUTH_SOCKET:
957 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000958 npfd++;
Damien Miller95def091999-11-25 00:26:21 +1100959 break;
960 case AUTH_UNUSED:
961 break;
962 default:
963 fatal("Unknown socket type %d", sockets[i].type);
964 break;
965 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000966 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000967 if (npfd != *npfdp &&
968 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
969 fatal("%s: recallocarray failed", __func__);
970 *pfdp = pfd;
971 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000972
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000973 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000974 switch (sockets[i].type) {
975 case AUTH_SOCKET:
djm@openbsd.orgb2140a72018-05-11 03:38:51 +0000976 if (npfd > maxfds) {
977 debug3("out of fds (active %zu >= limit %u); "
978 "skipping arming listener", npfd, maxfds);
979 break;
980 }
981 pfd[j].fd = sockets[i].fd;
982 pfd[j].revents = 0;
983 pfd[j].events = POLLIN;
984 j++;
985 break;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000986 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000987 pfd[j].fd = sockets[i].fd;
988 pfd[j].revents = 0;
djm@openbsd.orgd6915882019-01-22 22:58:50 +0000989 /*
990 * Only prepare to read if we can handle a full-size
991 * input read buffer and enqueue a max size reply..
992 */
993 if ((r = sshbuf_check_reserve(sockets[i].input,
994 AGENT_RBUF_LEN)) == 0 &&
995 (r = sshbuf_check_reserve(sockets[i].output,
996 AGENT_MAX_LEN)) == 0)
997 pfd[j].events = POLLIN;
998 else if (r != SSH_ERR_NO_BUFFER_SPACE) {
999 fatal("%s: buffer error: %s",
1000 __func__, ssh_err(r));
1001 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001002 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001003 pfd[j].events |= POLLOUT;
1004 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001005 break;
1006 default:
1007 break;
1008 }
1009 }
Darren Tucker2812dc92007-03-21 20:45:06 +11001010 deadline = reaper();
1011 if (parent_alive_interval != 0)
1012 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001013 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +11001014 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001015 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +11001016 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001017 if (deadline > INT_MAX / 1000)
1018 *timeoutp = INT_MAX / 1000;
1019 else
1020 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +11001021 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001022 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023}
1024
Ben Lindstrombba81212001-06-25 05:01:22 +00001025static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001026cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001027{
Damien Millerb1e967c2014-07-03 21:22:40 +10001028 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1029 return;
1030 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001031 if (socket_name[0])
1032 unlink(socket_name);
1033 if (socket_dir[0])
1034 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035}
1036
Darren Tucker3e33cec2003-10-02 16:12:36 +10001037void
Damien Miller792c5111999-10-29 09:47:09 +10001038cleanup_exit(int i)
1039{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001040 cleanup_socket();
1041 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001042}
1043
Damien Miller1c13bd82006-03-26 14:28:14 +11001044/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001045static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001046cleanup_handler(int sig)
1047{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001048 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001049#ifdef ENABLE_PKCS11
1050 pkcs11_terminate();
1051#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001052 _exit(2);
1053}
1054
Ben Lindstrombba81212001-06-25 05:01:22 +00001055static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001056check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001057{
Darren Tucker3e78a512011-06-03 14:14:16 +10001058 /*
1059 * If our parent has exited then getppid() will return (pid_t)1,
1060 * so testing for that should be safe.
1061 */
1062 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001063 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001064 cleanup_socket();
1065 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001066 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001067}
1068
1069static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001070usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001071{
Damien Millerf0858de2014-04-20 13:01:30 +10001072 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001073 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
djm@openbsd.org786d5992016-11-30 03:07:37 +00001074 " [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001075 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001076 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001077}
1078
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001079int
1080main(int ac, char **av)
1081{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001082 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001083 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001084 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001085#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001086 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001087#endif
Damien Miller615f9392000-05-17 22:53:33 +10001088 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001089 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001090 pid_t pid;
1091 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001092 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001093 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001094 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001095 struct pollfd *pfd = NULL;
1096 size_t npfd = 0;
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001097 u_int maxfds;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001098
Darren Tuckerce321d82005-10-03 18:11:24 +10001099 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1100 sanitise_stdfd();
1101
Damien Miller6cffb9a2002-09-04 16:20:26 +10001102 /* drop */
1103 setegid(getgid());
1104 setgid(getgid());
1105
Darren Tucker0fb7f592016-06-09 16:23:07 +10001106 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001107
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001108 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
1109 fatal("%s: getrlimit: %s", __progname, strerror(errno));
1110
Damien Miller59d3d5b2003-08-22 09:34:41 +10001111 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001112 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001113
djm@openbsd.org786d5992016-11-30 03:07:37 +00001114 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001115 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001116 case 'E':
1117 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1118 if (fingerprint_hash == -1)
1119 fatal("Invalid hash algorithm \"%s\"", optarg);
1120 break;
Damien Miller95def091999-11-25 00:26:21 +11001121 case 'c':
1122 if (s_flag)
1123 usage();
1124 c_flag++;
1125 break;
1126 case 'k':
1127 k_flag++;
1128 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001129 case 'P':
1130 if (pkcs11_whitelist != NULL)
1131 fatal("-P option already specified");
1132 pkcs11_whitelist = xstrdup(optarg);
1133 break;
Damien Miller95def091999-11-25 00:26:21 +11001134 case 's':
1135 if (c_flag)
1136 usage();
1137 s_flag++;
1138 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001139 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001140 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001141 usage();
1142 d_flag++;
1143 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001144 case 'D':
1145 if (d_flag || D_flag)
1146 usage();
1147 D_flag++;
1148 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001149 case 'a':
1150 agentsocket = optarg;
1151 break;
Damien Miller53d81482003-01-22 11:47:19 +11001152 case 't':
1153 if ((lifetime = convtime(optarg)) == -1) {
1154 fprintf(stderr, "Invalid lifetime\n");
1155 usage();
1156 }
1157 break;
Damien Miller95def091999-11-25 00:26:21 +11001158 default:
1159 usage();
1160 }
Damien Miller792c5111999-10-29 09:47:09 +10001161 }
Damien Miller95def091999-11-25 00:26:21 +11001162 ac -= optind;
1163 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001164
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001165 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001166 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001167
djm@openbsd.org786d5992016-11-30 03:07:37 +00001168 if (pkcs11_whitelist == NULL)
1169 pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST);
1170
Ben Lindstromeecdf232002-04-02 21:03:51 +00001171 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001172 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001173 if (shell != NULL && (len = strlen(shell)) > 2 &&
1174 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001175 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001176 }
Damien Miller95def091999-11-25 00:26:21 +11001177 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001178 const char *errstr = NULL;
1179
Damien Miller95def091999-11-25 00:26:21 +11001180 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1181 if (pidstr == NULL) {
1182 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001183 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001184 exit(1);
1185 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001186 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1187 if (errstr) {
1188 fprintf(stderr,
1189 "%s=\"%s\", which is not a good PID: %s\n",
1190 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001191 exit(1);
1192 }
1193 if (kill(pid, SIGTERM) == -1) {
1194 perror("kill");
1195 exit(1);
1196 }
1197 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1198 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1199 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001200 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001201 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001202 }
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001203
1204 /*
1205 * Minimum file descriptors:
1206 * stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
1207 * a few spare for libc / stack protectors / sanitisers, etc.
1208 */
1209#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
1210 if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
djm@openbsd.org960e7c62018-11-09 02:57:58 +00001211 fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001212 __progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
1213 maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
1214
Damien Miller95def091999-11-25 00:26:21 +11001215 parent_pid = getpid();
1216
Ben Lindstromb7788f32002-06-06 21:46:08 +00001217 if (agentsocket == NULL) {
1218 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001219 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001220 if (mkdtemp(socket_dir) == NULL) {
1221 perror("mkdtemp: private socket dir");
1222 exit(1);
1223 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001224 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1225 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001226 } else {
1227 /* Try to use specified agent socket */
1228 socket_dir[0] = '\0';
1229 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001230 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001231
Damien Miller5428f641999-11-25 11:54:57 +11001232 /*
1233 * Create socket early so it will exist before command gets run from
1234 * the parent.
1235 */
Damien Millerab2ec582014-07-18 15:04:47 +10001236 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001237 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001238 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001239 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001240 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001241 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001242 }
Damien Millerab2ec582014-07-18 15:04:47 +10001243 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001244
Damien Miller5428f641999-11-25 11:54:57 +11001245 /*
1246 * Fork, and have the parent execute the command, if any, or present
1247 * the socket data. The child continues as the authentication agent.
1248 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001249 if (D_flag || d_flag) {
1250 log_init(__progname,
1251 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1252 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001253 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1254 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1255 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001256 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001257 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001258 goto skip;
1259 }
Damien Miller95def091999-11-25 00:26:21 +11001260 pid = fork();
1261 if (pid == -1) {
1262 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001263 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001264 }
1265 if (pid != 0) { /* Parent - execute the given command. */
1266 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001267 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001268 if (ac == 0) {
1269 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1270 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001271 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001272 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001273 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001274 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001275 exit(0);
1276 }
Damien Millere4340be2000-09-16 13:29:08 +11001277 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1278 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1279 perror("setenv");
1280 exit(1);
1281 }
Damien Miller95def091999-11-25 00:26:21 +11001282 execvp(av[0], av);
1283 perror(av[0]);
1284 exit(1);
1285 }
Damien Millerc653b892002-02-08 22:05:41 +11001286 /* child */
1287 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001288
1289 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001290 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001291 cleanup_exit(1);
1292 }
1293
1294 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001295 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1296 /* XXX might close listen socket */
1297 (void)dup2(fd, STDIN_FILENO);
1298 (void)dup2(fd, STDOUT_FILENO);
1299 (void)dup2(fd, STDERR_FILENO);
1300 if (fd > 2)
1301 close(fd);
1302 }
Damien Miller95def091999-11-25 00:26:21 +11001303
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001304#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001305 /* deny core dumps, since memory contains unencrypted private keys */
1306 rlim.rlim_cur = rlim.rlim_max = 0;
1307 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001308 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001309 cleanup_exit(1);
1310 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001311#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001312
1313skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001314
Damien Millerb1e967c2014-07-03 21:22:40 +10001315 cleanup_pid = getpid();
1316
Damien Miller7ea845e2010-02-12 09:21:02 +11001317#ifdef ENABLE_PKCS11
1318 pkcs11_init(0);
1319#endif
Damien Miller95def091999-11-25 00:26:21 +11001320 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001321 if (ac > 0)
1322 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001323 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001324 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001325 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001326 signal(SIGHUP, cleanup_handler);
1327 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001328
djm@openbsd.org786d5992016-11-30 03:07:37 +00001329 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001330 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001331 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001332
Damien Miller95def091999-11-25 00:26:21 +11001333 while (1) {
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001334 prepare_poll(&pfd, &npfd, &timeout, maxfds);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001335 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001336 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001337 if (parent_alive_interval != 0)
1338 check_parent_exists();
1339 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001340 if (result < 0) {
1341 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001342 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001343 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001344 } else if (result > 0)
djm@openbsd.orgb2140a72018-05-11 03:38:51 +00001345 after_poll(pfd, npfd, maxfds);
Damien Miller95def091999-11-25 00:26:21 +11001346 }
1347 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348}