blob: 0c6c3659217ff190b99eb7f3d7dbfdd903378dab [file] [log] [blame]
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001/* $OpenBSD: ssh-agent.c,v 1.224 2017/07/24 04:34:28 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>
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)
99
Ben Lindstrom65366a82001-12-06 16:32:47 +0000100typedef enum {
101 AUTH_UNUSED,
102 AUTH_SOCKET,
103 AUTH_CONNECTION
104} sock_type;
105
Damien Miller95def091999-11-25 00:26:21 +1100106typedef struct {
107 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000108 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000109 struct sshbuf *input;
110 struct sshbuf *output;
111 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112} SocketEntry;
113
Ben Lindstrom46c16222000-12-22 01:43:59 +0000114u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115SocketEntry *sockets = NULL;
116
Damien Miller1a534ae2002-01-22 23:26:13 +1100117typedef struct identity {
118 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000119 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100120 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100121 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000122 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100123 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124} Identity;
125
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000126struct idtable {
Damien Millerad833b32000-08-23 10:46:23 +1000127 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100128 TAILQ_HEAD(idqueue, identity) idlist;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000129};
Damien Millerad833b32000-08-23 10:46:23 +1000130
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000131/* private key table */
132struct idtable *idtab;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133
134int max_fd = 0;
135
136/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000137pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000138time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Millerb1e967c2014-07-03 21:22:40 +1000140/* pid of process for which cleanup_socket is applicable */
141pid_t cleanup_pid = 0;
142
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000144char socket_name[PATH_MAX];
145char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
djm@openbsd.org786d5992016-11-30 03:07:37 +0000147/* PKCS#11 path whitelist */
148static char *pkcs11_whitelist;
149
Ben Lindstrom2f717042002-06-06 21:52:03 +0000150/* locking */
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000151#define LOCK_SIZE 32
152#define LOCK_SALT_SIZE 16
153#define LOCK_ROUNDS 1
Ben Lindstrom2f717042002-06-06 21:52:03 +0000154int locked = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000155u_char lock_pwhash[LOCK_SIZE];
156u_char lock_salt[LOCK_SALT_SIZE];
Ben Lindstrom2f717042002-06-06 21:52:03 +0000157
Damien Miller95def091999-11-25 00:26:21 +1100158extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100159
Darren Tucker55119252013-06-02 07:43:59 +1000160/* Default lifetime in seconds (0 == forever) */
161static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100162
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000163static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void
Damien Miller58f34862002-09-04 16:31:21 +1000166close_socket(SocketEntry *e)
167{
Damien Miller58f34862002-09-04 16:31:21 +1000168 close(e->fd);
169 e->fd = -1;
170 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000171 sshbuf_free(e->input);
172 sshbuf_free(e->output);
173 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000174}
175
176static void
Damien Millerad833b32000-08-23 10:46:23 +1000177idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000179 idtab = xcalloc(1, sizeof(*idtab));
180 TAILQ_INIT(&idtab->idlist);
181 idtab->nentries = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000182}
183
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000184static void
185free_identity(Identity *id)
186{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000187 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000188 free(id->provider);
189 free(id->comment);
190 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000191}
192
Damien Millerad833b32000-08-23 10:46:23 +1000193/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100194static Identity *
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000195lookup_identity(struct sshkey *key)
Damien Millerad833b32000-08-23 10:46:23 +1000196{
Damien Miller1a534ae2002-01-22 23:26:13 +1100197 Identity *id;
198
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000199 TAILQ_FOREACH(id, &idtab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000200 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100201 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000202 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100203 return (NULL);
204}
205
Damien Miller6c711792003-01-24 11:36:23 +1100206/* Check confirmation of keysign request */
207static int
208confirm_key(Identity *id)
209{
Darren Tuckerce327b62004-11-05 20:38:03 +1100210 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100211 int ret = -1;
212
markus@openbsd.org139ca812015-01-14 13:09:09 +0000213 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000214 if (p != NULL &&
215 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100216 id->comment, p))
217 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000218 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100219
Damien Miller6c711792003-01-24 11:36:23 +1100220 return (ret);
221}
222
markus@openbsd.org139ca812015-01-14 13:09:09 +0000223static void
224send_status(SocketEntry *e, int success)
225{
226 int r;
227
228 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
229 (r = sshbuf_put_u8(e->output, success ?
230 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
231 fatal("%s: buffer error: %s", __func__, ssh_err(r));
232}
233
Damien Millerad833b32000-08-23 10:46:23 +1000234/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000236process_request_identities(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000237{
Damien Miller1a534ae2002-01-22 23:26:13 +1100238 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000239 struct sshbuf *msg;
240 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
markus@openbsd.org139ca812015-01-14 13:09:09 +0000242 if ((msg = sshbuf_new()) == NULL)
243 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000244 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
245 (r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000246 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000247 TAILQ_FOREACH(id, &idtab->idlist, next) {
248 if ((r = sshkey_puts(id->key, msg)) != 0 ||
249 (r = sshbuf_put_cstring(msg, id->comment)) != 0) {
250 error("%s: put key/comment: %s", __func__,
djm@openbsd.org873d3e72017-04-30 23:18:44 +0000251 ssh_err(r));
252 continue;
Damien Millerad833b32000-08-23 10:46:23 +1000253 }
Damien Miller95def091999-11-25 00:26:21 +1100254 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000255 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
256 fatal("%s: buffer error: %s", __func__, ssh_err(r));
257 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258}
259
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000261static char *
262agent_decode_alg(struct sshkey *key, u_int flags)
263{
264 if (key->type == KEY_RSA) {
265 if (flags & SSH_AGENT_RSA_SHA2_256)
266 return "rsa-sha2-256";
267 else if (flags & SSH_AGENT_RSA_SHA2_512)
268 return "rsa-sha2-512";
269 }
270 return NULL;
271}
272
Damien Millerad833b32000-08-23 10:46:23 +1000273/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000274static void
Damien Millerad833b32000-08-23 10:46:23 +1000275process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276{
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000277 const u_char *data;
278 u_char *signature = NULL;
279 size_t dlen, slen = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000280 u_int compat = 0, flags;
281 int r, ok = -1;
282 struct sshbuf *msg;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000283 struct sshkey *key = NULL;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000284 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000285
djm@openbsd.org0088c572015-01-14 19:33:41 +0000286 if ((msg = sshbuf_new()) == NULL)
287 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000288 if ((r = sshkey_froms(e->request, &key)) != 0 ||
289 (r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000290 (r = sshbuf_get_u32(e->request, &flags)) != 0)
291 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100292 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000293 compat = SSH_BUG_SIGBLOB;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000294 if ((id = lookup_identity(key)) == NULL) {
djm@openbsd.org0088c572015-01-14 19:33:41 +0000295 verbose("%s: %s key not found", __func__, sshkey_type(key));
296 goto send;
297 }
298 if (id->confirm && confirm_key(id) != 0) {
299 verbose("%s: user refused key", __func__);
300 goto send;
301 }
302 if ((r = sshkey_sign(id->key, &signature, &slen,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000303 data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
djm@openbsd.org39736be2015-12-11 02:20:28 +0000304 error("%s: sshkey_sign: %s", __func__, ssh_err(r));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000305 goto send;
306 }
307 /* Success */
308 ok = 0;
309 send:
310 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000311 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000312 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
313 (r = sshbuf_put_string(msg, signature, slen)) != 0)
314 fatal("%s: buffer error: %s", __func__, ssh_err(r));
315 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
316 fatal("%s: buffer error: %s", __func__, ssh_err(r));
317
318 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
319 fatal("%s: buffer error: %s", __func__, ssh_err(r));
320
321 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000322 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000323}
324
325/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000326static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000327process_remove_identity(SocketEntry *e)
Damien Millerad833b32000-08-23 10:46:23 +1000328{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000329 int r, success = 0;
330 struct sshkey *key = NULL;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000331 Identity *id;
Damien Miller7e8e8201999-11-16 13:37:16 +1100332
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000333 if ((r = sshkey_froms(e->request, &key)) != 0) {
334 error("%s: get key: %s", __func__, ssh_err(r));
335 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000336 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000337 if ((id = lookup_identity(key)) == NULL) {
338 debug("%s: key not found", __func__);
339 goto done;
Damien Millerad833b32000-08-23 10:46:23 +1000340 }
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000341 /* We have this key, free it. */
342 if (idtab->nentries < 1)
343 fatal("%s: internal error: nentries %d",
344 __func__, idtab->nentries);
345 TAILQ_REMOVE(&idtab->idlist, id, next);
346 free_identity(id);
347 idtab->nentries--;
348 sshkey_free(key);
349 success = 1;
350 done:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000351 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352}
353
Ben Lindstrombba81212001-06-25 05:01:22 +0000354static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000355process_remove_all_identities(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356{
Damien Miller1a534ae2002-01-22 23:26:13 +1100357 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
Damien Miller95def091999-11-25 00:26:21 +1100359 /* Loop over all identities and clear the keys. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000360 for (id = TAILQ_FIRST(&idtab->idlist); id;
361 id = TAILQ_FIRST(&idtab->idlist)) {
362 TAILQ_REMOVE(&idtab->idlist, id, next);
Damien Miller1a534ae2002-01-22 23:26:13 +1100363 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100364 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Damien Miller95def091999-11-25 00:26:21 +1100366 /* Mark that there are no identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000367 idtab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000368
369 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000370 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100371}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Darren Tucker2812dc92007-03-21 20:45:06 +1100373/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000374static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000375reaper(void)
376{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000377 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000378 Identity *id, *nxt;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000379
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000380 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
381 nxt = TAILQ_NEXT(id, next);
382 if (id->death == 0)
383 continue;
384 if (now >= id->death) {
385 debug("expiring key '%s'", id->comment);
386 TAILQ_REMOVE(&idtab->idlist, id, next);
387 free_identity(id);
388 idtab->nentries--;
389 } else
390 deadline = (deadline == 0) ? id->death :
391 MINIMUM(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000392 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100393 if (deadline == 0 || deadline <= now)
394 return 0;
395 else
396 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000397}
398
399static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000400process_add_identity(SocketEntry *e)
Damien Miller95def091999-11-25 00:26:21 +1100401{
Damien Miller4c7728c2007-10-26 14:25:31 +1000402 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000403 int success = 0, confirm = 0;
404 u_int seconds;
405 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000406 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000407 struct sshkey *k = NULL;
408 u_char ctype;
409 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100410
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000411 if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
412 k == NULL ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000413 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
414 error("%s: decode private key: %s", __func__, ssh_err(r));
415 goto err;
416 }
Damien Miller86687062014-07-02 15:28:02 +1000417
markus@openbsd.org139ca812015-01-14 13:09:09 +0000418 while (sshbuf_len(e->request)) {
419 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
420 error("%s: buffer error: %s", __func__, ssh_err(r));
421 goto err;
422 }
423 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000424 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000425 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
426 error("%s: bad lifetime constraint: %s",
427 __func__, ssh_err(r));
428 goto err;
429 }
430 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000431 break;
Damien Miller6c711792003-01-24 11:36:23 +1100432 case SSH_AGENT_CONSTRAIN_CONFIRM:
433 confirm = 1;
434 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000435 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000436 error("%s: Unknown constraint %d", __func__, ctype);
437 err:
438 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000439 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000440 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000441 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000442 }
443 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000444
Damien Miller1cfadab2008-06-30 00:05:21 +1000445 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100446 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000447 death = monotime() + lifetime;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000448 if ((id = lookup_identity(k)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100449 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000450 id->key = k;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000451 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000452 /* Increment the number of identities. */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000453 idtab->nentries++;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000454 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000455 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000456 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000457 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000458 id->comment = comment;
459 id->death = death;
460 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000461send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000462 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000463}
464
Ben Lindstrom2f717042002-06-06 21:52:03 +0000465/* XXX todo: encrypt sensitive data with passphrase */
466static void
467process_lock_agent(SocketEntry *e, int lock)
468{
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000469 int r, success = 0, delay;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000470 char *passwd;
471 u_char passwdhash[LOCK_SIZE];
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000472 static u_int fail_count = 0;
473 size_t pwlen;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000474
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000475 if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000476 fatal("%s: buffer error: %s", __func__, ssh_err(r));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000477 if (pwlen == 0) {
478 debug("empty password not supported");
479 } else if (locked && !lock) {
480 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
481 passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
482 fatal("bcrypt_pbkdf");
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000483 if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000484 debug("agent unlocked");
485 locked = 0;
486 fail_count = 0;
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000487 explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000488 success = 1;
489 } else {
490 /* delay in 0.1s increments up to 10s */
491 if (fail_count < 100)
492 fail_count++;
493 delay = 100000 * fail_count;
494 debug("unlock failed, delaying %0.1lf seconds",
495 (double)delay/1000000);
496 usleep(delay);
497 }
498 explicit_bzero(passwdhash, sizeof(passwdhash));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000499 } else if (!locked && lock) {
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000500 debug("agent locked");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000501 locked = 1;
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000502 arc4random_buf(lock_salt, sizeof(lock_salt));
503 if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
djm@openbsd.org1a31d022016-05-02 08:49:03 +0000504 lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000505 fatal("bcrypt_pbkdf");
Ben Lindstrom2f717042002-06-06 21:52:03 +0000506 success = 1;
507 }
dtucker@openbsd.org9173d0f2015-05-15 05:44:21 +0000508 explicit_bzero(passwd, pwlen);
Darren Tuckera627d422013-06-02 07:31:17 +1000509 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000510 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000511}
512
513static void
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000514no_identities(SocketEntry *e)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000515{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000516 struct sshbuf *msg;
517 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000518
markus@openbsd.org139ca812015-01-14 13:09:09 +0000519 if ((msg = sshbuf_new()) == NULL)
520 fatal("%s: sshbuf_new failed", __func__);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000521 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
markus@openbsd.org139ca812015-01-14 13:09:09 +0000522 (r = sshbuf_put_u32(msg, 0)) != 0 ||
523 (r = sshbuf_put_stringb(e->output, msg)) != 0)
524 fatal("%s: buffer error: %s", __func__, ssh_err(r));
525 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000526}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000527
Damien Miller7ea845e2010-02-12 09:21:02 +1100528#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000529static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000530process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000531{
djm@openbsd.org786d5992016-11-30 03:07:37 +0000532 char *provider = NULL, *pin, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000533 int r, i, count = 0, success = 0, confirm = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000534 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000535 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000536 u_char type;
537 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000538 Identity *id;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100539
markus@openbsd.org139ca812015-01-14 13:09:09 +0000540 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
541 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
542 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000543
markus@openbsd.org139ca812015-01-14 13:09:09 +0000544 while (sshbuf_len(e->request)) {
545 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
546 fatal("%s: buffer error: %s", __func__, ssh_err(r));
547 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000548 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000549 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
550 fatal("%s: buffer error: %s",
551 __func__, ssh_err(r));
552 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000553 break;
554 case SSH_AGENT_CONSTRAIN_CONFIRM:
555 confirm = 1;
556 break;
557 default:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000558 error("%s: Unknown constraint type %d", __func__, type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000559 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000560 }
561 }
djm@openbsd.org786d5992016-11-30 03:07:37 +0000562 if (realpath(provider, canonical_provider) == NULL) {
563 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
564 provider, strerror(errno));
565 goto send;
566 }
567 if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) {
568 verbose("refusing PKCS#11 add of \"%.100s\": "
569 "provider not whitelisted", canonical_provider);
570 goto send;
571 }
572 debug("%s: add %.100s", __func__, canonical_provider);
Damien Millerd94f20d2003-06-11 22:06:33 +1000573 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000574 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000575
djm@openbsd.org786d5992016-11-30 03:07:37 +0000576 count = pkcs11_add_provider(canonical_provider, pin, &keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100577 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000578 k = keys[i];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000579 if (lookup_identity(k) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100580 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000581 id->key = k;
djm@openbsd.org786d5992016-11-30 03:07:37 +0000582 id->provider = xstrdup(canonical_provider);
583 id->comment = xstrdup(canonical_provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000584 id->death = death;
585 id->confirm = confirm;
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000586 TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
587 idtab->nentries++;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000588 success = 1;
589 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000590 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000591 }
592 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000593 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000594send:
Darren Tuckera627d422013-06-02 07:31:17 +1000595 free(pin);
596 free(provider);
597 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000598 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000599}
600
601static void
602process_remove_smartcard_key(SocketEntry *e)
603{
djm@openbsd.org25f83762017-03-15 02:25:09 +0000604 char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000605 int r, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100606 Identity *id, *nxt;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607
markus@openbsd.org139ca812015-01-14 13:09:09 +0000608 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
609 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
610 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000611 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000612
djm@openbsd.org25f83762017-03-15 02:25:09 +0000613 if (realpath(provider, canonical_provider) == NULL) {
614 verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
615 provider, strerror(errno));
616 goto send;
617 }
618
619 debug("%s: remove %.100s", __func__, canonical_provider);
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000620 for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
621 nxt = TAILQ_NEXT(id, next);
622 /* Skip file--based keys */
623 if (id->provider == NULL)
624 continue;
625 if (!strcmp(canonical_provider, id->provider)) {
626 TAILQ_REMOVE(&idtab->idlist, id, next);
627 free_identity(id);
628 idtab->nentries--;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000629 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000630 }
djm@openbsd.org25f83762017-03-15 02:25:09 +0000631 if (pkcs11_del_provider(canonical_provider) == 0)
Damien Miller7ea845e2010-02-12 09:21:02 +1100632 success = 1;
633 else
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000634 error("%s: pkcs11_del_provider failed", __func__);
deraadt@openbsd.org1a321bf2017-03-15 03:52:30 +0000635send:
Darren Tuckera627d422013-06-02 07:31:17 +1000636 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000637 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000638}
Damien Miller7ea845e2010-02-12 09:21:02 +1100639#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000640
Damien Millerad833b32000-08-23 10:46:23 +1000641/* dispatch incoming messages */
642
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000643static int
644process_message(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000645{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000646 u_int msg_len;
647 u_char type;
648 const u_char *cp;
649 int r;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000650 SocketEntry *e;
651
652 if (socknum >= sockets_alloc) {
653 fatal("%s: socket number %u >= allocated %u",
654 __func__, socknum, sockets_alloc);
655 }
656 e = &sockets[socknum];
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000657
markus@openbsd.org139ca812015-01-14 13:09:09 +0000658 if (sshbuf_len(e->input) < 5)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000659 return 0; /* Incomplete message header. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000660 cp = sshbuf_ptr(e->input);
661 msg_len = PEEK_U32(cp);
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000662 if (msg_len > AGENT_MAX_LEN) {
663 debug("%s: socket %u (fd=%d) message too long %u > %u",
664 __func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
665 return -1;
Damien Miller95def091999-11-25 00:26:21 +1100666 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000667 if (sshbuf_len(e->input) < msg_len + 4)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000668 return 0; /* Incomplete message body. */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000669
670 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000671 sshbuf_reset(e->request);
672 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000673 (r = sshbuf_get_u8(e->request, &type)) != 0) {
674 if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
675 r == SSH_ERR_STRING_TOO_LARGE) {
676 debug("%s: buffer error: %s", __func__, ssh_err(r));
677 return -1;
678 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000679 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000680 }
681
682 debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683
Ben Lindstrom2f717042002-06-06 21:52:03 +0000684 /* check wheter agent is locked */
685 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000686 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000687 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000688 case SSH2_AGENTC_REQUEST_IDENTITIES:
689 /* send empty lists */
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000690 no_identities(e);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000691 break;
692 default:
693 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000694 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000695 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000696 return 0;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000697 }
698
Damien Miller95def091999-11-25 00:26:21 +1100699 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000700 case SSH_AGENTC_LOCK:
701 case SSH_AGENTC_UNLOCK:
702 process_lock_agent(e, type == SSH_AGENTC_LOCK);
703 break;
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000704 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000705 process_remove_all_identities(e); /* safe for !WITH_SSH1 */
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000706 break;
Damien Millerad833b32000-08-23 10:46:23 +1000707 /* ssh2 */
708 case SSH2_AGENTC_SIGN_REQUEST:
709 process_sign_request2(e);
710 break;
711 case SSH2_AGENTC_REQUEST_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000712 process_request_identities(e);
Damien Millerad833b32000-08-23 10:46:23 +1000713 break;
714 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000715 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000716 process_add_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000717 break;
718 case SSH2_AGENTC_REMOVE_IDENTITY:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000719 process_remove_identity(e);
Damien Millerad833b32000-08-23 10:46:23 +1000720 break;
721 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
djm@openbsd.orgf4a6a882017-04-30 23:29:10 +0000722 process_remove_all_identities(e);
Damien Miller95def091999-11-25 00:26:21 +1100723 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100724#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000725 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000726 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000727 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100728 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000729 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
730 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100731 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100732#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100733 default:
734 /* Unknown message. Respond with failure. */
735 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000736 sshbuf_reset(e->request);
737 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100738 break;
739 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000740 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000741}
742
Ben Lindstrombba81212001-06-25 05:01:22 +0000743static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000744new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000745{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000746 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000747
Damien Miller232711f2004-06-15 10:35:30 +1000748 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000749
Damien Miller95def091999-11-25 00:26:21 +1100750 if (fd > max_fd)
751 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000752
Damien Miller95def091999-11-25 00:26:21 +1100753 for (i = 0; i < sockets_alloc; i++)
754 if (sockets[i].type == AUTH_UNUSED) {
755 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000756 if ((sockets[i].input = sshbuf_new()) == NULL)
757 fatal("%s: sshbuf_new failed", __func__);
758 if ((sockets[i].output = sshbuf_new()) == NULL)
759 fatal("%s: sshbuf_new failed", __func__);
760 if ((sockets[i].request = sshbuf_new()) == NULL)
761 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000762 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100763 return;
764 }
765 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000766 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000767 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000768 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100769 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000770 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100771 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000772 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
773 fatal("%s: sshbuf_new failed", __func__);
774 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
775 fatal("%s: sshbuf_new failed", __func__);
776 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
777 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000778 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000779}
780
Ben Lindstrombba81212001-06-25 05:01:22 +0000781static int
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000782handle_socket_read(u_int socknum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783{
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000784 struct sockaddr_un sunaddr;
785 socklen_t slen;
786 uid_t euid;
787 gid_t egid;
788 int fd;
789
790 slen = sizeof(sunaddr);
791 fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
792 if (fd < 0) {
793 error("accept from AUTH_SOCKET: %s", strerror(errno));
794 return -1;
795 }
796 if (getpeereid(fd, &euid, &egid) < 0) {
797 error("getpeereid %d failed: %s", fd, strerror(errno));
798 close(fd);
799 return -1;
800 }
801 if ((euid != 0) && (getuid() != euid)) {
802 error("uid mismatch: peer euid %u != uid %u",
803 (u_int) euid, (u_int) getuid());
804 close(fd);
805 return -1;
806 }
807 new_socket(AUTH_CONNECTION, fd);
808 return 0;
809}
810
811static int
812handle_conn_read(u_int socknum)
813{
814 char buf[1024];
815 ssize_t len;
816 int r;
817
818 if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
819 if (len == -1) {
820 if (errno == EAGAIN || errno == EINTR)
821 return 0;
822 error("%s: read error on socket %u (fd %d): %s",
823 __func__, socknum, sockets[socknum].fd,
824 strerror(errno));
825 }
826 return -1;
827 }
828 if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
829 fatal("%s: buffer error: %s", __func__, ssh_err(r));
830 explicit_bzero(buf, sizeof(buf));
831 process_message(socknum);
832 return 0;
833}
834
835static int
836handle_conn_write(u_int socknum)
837{
838 ssize_t len;
839 int r;
840
841 if (sshbuf_len(sockets[socknum].output) == 0)
842 return 0; /* shouldn't happen */
843 if ((len = write(sockets[socknum].fd,
844 sshbuf_ptr(sockets[socknum].output),
845 sshbuf_len(sockets[socknum].output))) <= 0) {
846 if (len == -1) {
847 if (errno == EAGAIN || errno == EINTR)
848 return 0;
849 error("%s: read error on socket %u (fd %d): %s",
850 __func__, socknum, sockets[socknum].fd,
851 strerror(errno));
852 }
853 return -1;
854 }
855 if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
856 fatal("%s: buffer error: %s", __func__, ssh_err(r));
857 return 0;
858}
859
860static void
861after_poll(struct pollfd *pfd, size_t npfd)
862{
863 size_t i;
864 u_int socknum;
865
866 for (i = 0; i < npfd; i++) {
867 if (pfd[i].revents == 0)
868 continue;
869 /* Find sockets entry */
870 for (socknum = 0; socknum < sockets_alloc; socknum++) {
871 if (sockets[socknum].type != AUTH_SOCKET &&
872 sockets[socknum].type != AUTH_CONNECTION)
873 continue;
874 if (pfd[i].fd == sockets[socknum].fd)
875 break;
876 }
877 if (socknum >= sockets_alloc) {
878 error("%s: no socket for fd %d", __func__, pfd[i].fd);
879 continue;
880 }
881 /* Process events */
882 switch (sockets[socknum].type) {
883 case AUTH_SOCKET:
884 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
885 handle_socket_read(socknum) != 0)
886 close_socket(&sockets[socknum]);
887 break;
888 case AUTH_CONNECTION:
889 if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
890 handle_conn_read(socknum) != 0) {
891 close_socket(&sockets[socknum]);
892 break;
893 }
894 if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
895 handle_conn_write(socknum) != 0)
896 close_socket(&sockets[socknum]);
897 break;
898 default:
899 break;
900 }
901 }
902}
903
904static int
905prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp)
906{
907 struct pollfd *pfd = *pfdp;
908 size_t i, j, npfd = 0;
Darren Tucker55119252013-06-02 07:43:59 +1000909 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000910
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000911 /* Count active sockets */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000912 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100913 switch (sockets[i].type) {
914 case AUTH_SOCKET:
915 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000916 npfd++;
Damien Miller95def091999-11-25 00:26:21 +1100917 break;
918 case AUTH_UNUSED:
919 break;
920 default:
921 fatal("Unknown socket type %d", sockets[i].type);
922 break;
923 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000924 }
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000925 if (npfd != *npfdp &&
926 (pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
927 fatal("%s: recallocarray failed", __func__);
928 *pfdp = pfd;
929 *npfdp = npfd;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000930
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000931 for (i = j = 0; i < sockets_alloc; i++) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000932 switch (sockets[i].type) {
933 case AUTH_SOCKET:
934 case AUTH_CONNECTION:
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000935 pfd[j].fd = sockets[i].fd;
936 pfd[j].revents = 0;
937 /* XXX backoff when input buffer full */
938 pfd[j].events = POLLIN;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000939 if (sshbuf_len(sockets[i].output) > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000940 pfd[j].events |= POLLOUT;
941 j++;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000942 break;
943 default:
944 break;
945 }
946 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100947 deadline = reaper();
948 if (parent_alive_interval != 0)
949 deadline = (deadline == 0) ? parent_alive_interval :
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000950 MINIMUM(deadline, parent_alive_interval);
Darren Tucker2812dc92007-03-21 20:45:06 +1100951 if (deadline == 0) {
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +0000952 *timeoutp = -1; /* INFTIM */
Darren Tucker2812dc92007-03-21 20:45:06 +1100953 } else {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +0000954 if (deadline > INT_MAX / 1000)
955 *timeoutp = INT_MAX / 1000;
956 else
957 *timeoutp = deadline * 1000;
Darren Tucker2812dc92007-03-21 20:45:06 +1100958 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000959 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
Ben Lindstrombba81212001-06-25 05:01:22 +0000962static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000963cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000964{
Damien Millerb1e967c2014-07-03 21:22:40 +1000965 if (cleanup_pid != 0 && getpid() != cleanup_pid)
966 return;
967 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000968 if (socket_name[0])
969 unlink(socket_name);
970 if (socket_dir[0])
971 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000972}
973
Darren Tucker3e33cec2003-10-02 16:12:36 +1000974void
Damien Miller792c5111999-10-29 09:47:09 +1000975cleanup_exit(int i)
976{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000977 cleanup_socket();
978 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000979}
980
Damien Miller1c13bd82006-03-26 14:28:14 +1100981/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000983cleanup_handler(int sig)
984{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000985 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +1100986#ifdef ENABLE_PKCS11
987 pkcs11_terminate();
988#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000989 _exit(2);
990}
991
Ben Lindstrombba81212001-06-25 05:01:22 +0000992static void
Darren Tucker2812dc92007-03-21 20:45:06 +1100993check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +0000994{
Darren Tucker3e78a512011-06-03 14:14:16 +1000995 /*
996 * If our parent has exited then getppid() will return (pid_t)1,
997 * so testing for that should be safe.
998 */
999 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001000 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001001 cleanup_socket();
1002 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001003 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001004}
1005
1006static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001007usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001008{
Damien Millerf0858de2014-04-20 13:01:30 +10001009 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001010 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
djm@openbsd.org786d5992016-11-30 03:07:37 +00001011 " [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001012 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001013 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001014}
1015
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016int
1017main(int ac, char **av)
1018{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001019 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001020 int sock, fd, ch, result, saved_errno;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001021 char *shell, *format, *pidstr, *agentsocket = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001022#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001023 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001024#endif
Damien Miller615f9392000-05-17 22:53:33 +10001025 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001026 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001027 pid_t pid;
1028 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker90133232009-06-21 17:50:15 +10001029 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001030 mode_t prev_mask;
djm@openbsd.org9f0e44e2017-07-24 04:34:28 +00001031 int timeout = -1; /* INFTIM */
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001032 struct pollfd *pfd = NULL;
1033 size_t npfd = 0;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001034
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001035 ssh_malloc_init(); /* must be called before any mallocs */
Darren Tuckerce321d82005-10-03 18:11:24 +10001036 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1037 sanitise_stdfd();
1038
Damien Miller6cffb9a2002-09-04 16:20:26 +10001039 /* drop */
1040 setegid(getgid());
1041 setgid(getgid());
1042
Darren Tucker0fb7f592016-06-09 16:23:07 +10001043 platform_disable_tracing(0); /* strict=no */
Damien Miller6c4914a2004-03-03 11:08:59 +11001044
Damien Miller1f0311c2014-05-15 14:24:09 +10001045#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001046 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001047#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001048
Damien Miller59d3d5b2003-08-22 09:34:41 +10001049 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001050 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001051
djm@openbsd.org786d5992016-11-30 03:07:37 +00001052 while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001053 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001054 case 'E':
1055 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1056 if (fingerprint_hash == -1)
1057 fatal("Invalid hash algorithm \"%s\"", optarg);
1058 break;
Damien Miller95def091999-11-25 00:26:21 +11001059 case 'c':
1060 if (s_flag)
1061 usage();
1062 c_flag++;
1063 break;
1064 case 'k':
1065 k_flag++;
1066 break;
djm@openbsd.org786d5992016-11-30 03:07:37 +00001067 case 'P':
1068 if (pkcs11_whitelist != NULL)
1069 fatal("-P option already specified");
1070 pkcs11_whitelist = xstrdup(optarg);
1071 break;
Damien Miller95def091999-11-25 00:26:21 +11001072 case 's':
1073 if (c_flag)
1074 usage();
1075 s_flag++;
1076 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001077 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001078 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001079 usage();
1080 d_flag++;
1081 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001082 case 'D':
1083 if (d_flag || D_flag)
1084 usage();
1085 D_flag++;
1086 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001087 case 'a':
1088 agentsocket = optarg;
1089 break;
Damien Miller53d81482003-01-22 11:47:19 +11001090 case 't':
1091 if ((lifetime = convtime(optarg)) == -1) {
1092 fprintf(stderr, "Invalid lifetime\n");
1093 usage();
1094 }
1095 break;
Damien Miller95def091999-11-25 00:26:21 +11001096 default:
1097 usage();
1098 }
Damien Miller792c5111999-10-29 09:47:09 +10001099 }
Damien Miller95def091999-11-25 00:26:21 +11001100 ac -= optind;
1101 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001102
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001103 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001104 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001105
djm@openbsd.org786d5992016-11-30 03:07:37 +00001106 if (pkcs11_whitelist == NULL)
1107 pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST);
1108
Ben Lindstromeecdf232002-04-02 21:03:51 +00001109 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001110 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001111 if (shell != NULL && (len = strlen(shell)) > 2 &&
1112 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001113 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001114 }
Damien Miller95def091999-11-25 00:26:21 +11001115 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001116 const char *errstr = NULL;
1117
Damien Miller95def091999-11-25 00:26:21 +11001118 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1119 if (pidstr == NULL) {
1120 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001121 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001122 exit(1);
1123 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001124 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1125 if (errstr) {
1126 fprintf(stderr,
1127 "%s=\"%s\", which is not a good PID: %s\n",
1128 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001129 exit(1);
1130 }
1131 if (kill(pid, SIGTERM) == -1) {
1132 perror("kill");
1133 exit(1);
1134 }
1135 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1136 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1137 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001138 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001139 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001140 }
Damien Miller95def091999-11-25 00:26:21 +11001141 parent_pid = getpid();
1142
Ben Lindstromb7788f32002-06-06 21:46:08 +00001143 if (agentsocket == NULL) {
1144 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001145 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001146 if (mkdtemp(socket_dir) == NULL) {
1147 perror("mkdtemp: private socket dir");
1148 exit(1);
1149 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001150 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1151 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001152 } else {
1153 /* Try to use specified agent socket */
1154 socket_dir[0] = '\0';
1155 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001156 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157
Damien Miller5428f641999-11-25 11:54:57 +11001158 /*
1159 * Create socket early so it will exist before command gets run from
1160 * the parent.
1161 */
Damien Millerab2ec582014-07-18 15:04:47 +10001162 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001163 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001164 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001165 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001166 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001167 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001168 }
Damien Millerab2ec582014-07-18 15:04:47 +10001169 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001170
Damien Miller5428f641999-11-25 11:54:57 +11001171 /*
1172 * Fork, and have the parent execute the command, if any, or present
1173 * the socket data. The child continues as the authentication agent.
1174 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001175 if (D_flag || d_flag) {
1176 log_init(__progname,
1177 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1178 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001179 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1180 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1181 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001182 printf("echo Agent pid %ld;\n", (long)parent_pid);
dtucker@openbsd.org79394ed2015-12-11 02:29:03 +00001183 fflush(stdout);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001184 goto skip;
1185 }
Damien Miller95def091999-11-25 00:26:21 +11001186 pid = fork();
1187 if (pid == -1) {
1188 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001189 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001190 }
1191 if (pid != 0) { /* Parent - execute the given command. */
1192 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001193 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001194 if (ac == 0) {
1195 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1196 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001197 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001198 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001199 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001200 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001201 exit(0);
1202 }
Damien Millere4340be2000-09-16 13:29:08 +11001203 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1204 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1205 perror("setenv");
1206 exit(1);
1207 }
Damien Miller95def091999-11-25 00:26:21 +11001208 execvp(av[0], av);
1209 perror(av[0]);
1210 exit(1);
1211 }
Damien Millerc653b892002-02-08 22:05:41 +11001212 /* child */
1213 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001214
1215 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001216 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001217 cleanup_exit(1);
1218 }
1219
1220 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001221 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1222 /* XXX might close listen socket */
1223 (void)dup2(fd, STDIN_FILENO);
1224 (void)dup2(fd, STDOUT_FILENO);
1225 (void)dup2(fd, STDERR_FILENO);
1226 if (fd > 2)
1227 close(fd);
1228 }
Damien Miller95def091999-11-25 00:26:21 +11001229
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001230#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001231 /* deny core dumps, since memory contains unencrypted private keys */
1232 rlim.rlim_cur = rlim.rlim_max = 0;
1233 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001234 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001235 cleanup_exit(1);
1236 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001237#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001238
1239skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001240
Damien Millerb1e967c2014-07-03 21:22:40 +10001241 cleanup_pid = getpid();
1242
Damien Miller7ea845e2010-02-12 09:21:02 +11001243#ifdef ENABLE_PKCS11
1244 pkcs11_init(0);
1245#endif
Damien Miller95def091999-11-25 00:26:21 +11001246 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001247 if (ac > 0)
1248 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001249 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001250 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001251 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001252 signal(SIGHUP, cleanup_handler);
1253 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001254
djm@openbsd.org786d5992016-11-30 03:07:37 +00001255 if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001256 fatal("%s: pledge: %s", __progname, strerror(errno));
Damien Miller4626cba2016-01-08 14:24:56 +11001257 platform_pledge_agent();
djm@openbsd.orgd9521622015-12-01 23:29:24 +00001258
Damien Miller95def091999-11-25 00:26:21 +11001259 while (1) {
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001260 prepare_poll(&pfd, &npfd, &timeout);
1261 result = poll(pfd, npfd, timeout);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001262 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001263 if (parent_alive_interval != 0)
1264 check_parent_exists();
1265 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001266 if (result < 0) {
1267 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001268 continue;
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001269 fatal("poll: %s", strerror(saved_errno));
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001270 } else if (result > 0)
djm@openbsd.orgfd0e8fa2017-07-19 01:15:02 +00001271 after_poll(pfd, npfd);
Damien Miller95def091999-11-25 00:26:21 +11001272 }
1273 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274}