blob: 24500d9d64c3d485589458eb916e5a3d9a4ada14 [file] [log] [blame]
djm@openbsd.org0088c572015-01-14 19:33:41 +00001/* $OpenBSD: ssh-agent.c,v 1.195 2015/01/14 19:33:41 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>
Damien Miller03e20032006-03-15 11:16:59 +110059#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110060# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110061#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110062#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100063#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100064#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100065#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100066#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100067#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100068#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110069
markus@openbsd.org139ca812015-01-14 13:09:09 +000070#include "key.h" /* XXX for typedef */
71#include "buffer.h" /* XXX for typedef */
72
Damien Millerd7834352006-08-05 12:39:39 +100073#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074#include "ssh.h"
75#include "rsa.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000076#include "sshbuf.h"
77#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100078#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110079#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000080#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110081#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110082#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000083#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller7ea845e2010-02-12 09:21:02 +110085#ifdef ENABLE_PKCS11
86#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000087#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000088
Damien Miller6c4914a2004-03-03 11:08:59 +110089#if defined(HAVE_SYS_PRCTL_H)
90#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
91#endif
92
Ben Lindstrom65366a82001-12-06 16:32:47 +000093typedef enum {
94 AUTH_UNUSED,
95 AUTH_SOCKET,
96 AUTH_CONNECTION
97} sock_type;
98
Damien Miller95def091999-11-25 00:26:21 +110099typedef struct {
100 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000101 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000102 struct sshbuf *input;
103 struct sshbuf *output;
104 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105} SocketEntry;
106
Ben Lindstrom46c16222000-12-22 01:43:59 +0000107u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108SocketEntry *sockets = NULL;
109
Damien Miller1a534ae2002-01-22 23:26:13 +1100110typedef struct identity {
111 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000112 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100113 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100114 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000115 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100116 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} Identity;
118
Damien Millerad833b32000-08-23 10:46:23 +1000119typedef struct {
120 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100121 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000122} Idtab;
123
124/* private key table, one per protocol version */
125Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
127int max_fd = 0;
128
129/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000130pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000131time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Millerb1e967c2014-07-03 21:22:40 +1000133/* pid of process for which cleanup_socket is applicable */
134pid_t cleanup_pid = 0;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100137char socket_name[MAXPATHLEN];
138char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Ben Lindstrom2f717042002-06-06 21:52:03 +0000140/* locking */
141int locked = 0;
142char *lock_passwd = NULL;
143
Damien Miller95def091999-11-25 00:26:21 +1100144extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100145
Darren Tucker55119252013-06-02 07:43:59 +1000146/* Default lifetime in seconds (0 == forever) */
147static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100148
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000149static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Miller58f34862002-09-04 16:31:21 +1000152close_socket(SocketEntry *e)
153{
Damien Miller58f34862002-09-04 16:31:21 +1000154 close(e->fd);
155 e->fd = -1;
156 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000157 sshbuf_free(e->input);
158 sshbuf_free(e->output);
159 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000160}
161
162static void
Damien Millerad833b32000-08-23 10:46:23 +1000163idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Millerad833b32000-08-23 10:46:23 +1000165 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000166
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000167 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100168 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000169 idtable[i].nentries = 0;
170 }
171}
172
173/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000174static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000175idtab_lookup(int version)
176{
177 if (version < 1 || version > 2)
178 fatal("internal error, bad protocol version %d", version);
179 return &idtable[version];
180}
181
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000182static void
183free_identity(Identity *id)
184{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000185 sshkey_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000186 free(id->provider);
187 free(id->comment);
188 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000189}
190
Damien Millerad833b32000-08-23 10:46:23 +1000191/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100192static Identity *
markus@openbsd.org139ca812015-01-14 13:09:09 +0000193lookup_identity(struct sshkey *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000194{
Damien Miller1a534ae2002-01-22 23:26:13 +1100195 Identity *id;
196
Damien Millerad833b32000-08-23 10:46:23 +1000197 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100198 TAILQ_FOREACH(id, &tab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000199 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100200 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000201 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100202 return (NULL);
203}
204
Damien Miller6c711792003-01-24 11:36:23 +1100205/* Check confirmation of keysign request */
206static int
207confirm_key(Identity *id)
208{
Darren Tuckerce327b62004-11-05 20:38:03 +1100209 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100210 int ret = -1;
211
markus@openbsd.org139ca812015-01-14 13:09:09 +0000212 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
Darren Tuckerce327b62004-11-05 20:38:03 +1100213 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
214 id->comment, p))
215 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000216 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100217
Damien Miller6c711792003-01-24 11:36:23 +1100218 return (ret);
219}
220
markus@openbsd.org139ca812015-01-14 13:09:09 +0000221static void
222send_status(SocketEntry *e, int success)
223{
224 int r;
225
226 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
227 (r = sshbuf_put_u8(e->output, success ?
228 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
229 fatal("%s: buffer error: %s", __func__, ssh_err(r));
230}
231
Damien Millerad833b32000-08-23 10:46:23 +1000232/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static void
Damien Millerad833b32000-08-23 10:46:23 +1000234process_request_identities(SocketEntry *e, int version)
235{
236 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100237 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000238 struct sshbuf *msg;
239 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240
markus@openbsd.org139ca812015-01-14 13:09:09 +0000241 if ((msg = sshbuf_new()) == NULL)
242 fatal("%s: sshbuf_new failed", __func__);
243 if ((r = sshbuf_put_u8(msg, (version == 1) ?
244 SSH_AGENT_RSA_IDENTITIES_ANSWER :
245 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
246 (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
247 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1a534ae2002-01-22 23:26:13 +1100248 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100249 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000250#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000251 if ((r = sshbuf_put_u32(msg,
252 BN_num_bits(id->key->rsa->n))) != 0 ||
253 (r = sshbuf_put_bignum1(msg,
254 id->key->rsa->e)) != 0 ||
255 (r = sshbuf_put_bignum1(msg,
256 id->key->rsa->n)) != 0)
257 fatal("%s: buffer error: %s",
258 __func__, ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000259#endif
Damien Millerad833b32000-08-23 10:46:23 +1000260 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000261 u_char *blob;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000262 size_t blen;
263
264 if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
265 error("%s: sshkey_to_blob: %s", __func__,
266 ssh_err(r));
267 continue;
268 }
269 if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
270 fatal("%s: buffer error: %s",
271 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000272 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000273 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000274 if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
275 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100276 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000277 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
279 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280}
281
Damien Miller1f0311c2014-05-15 14:24:09 +1000282#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000283/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Millerad833b32000-08-23 10:46:23 +1000285process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000287 u_char buf[32], mdbuf[16], session_id[16];
288 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000289 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000290 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000291 int r, len;
292 struct sshbuf *msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100293 struct ssh_digest_ctx *md;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000294 struct sshkey *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295
markus@openbsd.org139ca812015-01-14 13:09:09 +0000296 if ((msg = sshbuf_new()) == NULL)
297 fatal("%s: sshbuf_new failed", __func__);
298 if ((key = sshkey_new(KEY_RSA1)) == NULL)
299 fatal("%s: sshkey_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100300 if ((challenge = BN_new()) == NULL)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000301 fatal("%s: BN_new failed", __func__);
Damien Millerad833b32000-08-23 10:46:23 +1000302
markus@openbsd.org139ca812015-01-14 13:09:09 +0000303 if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
304 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
305 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
306 (r = sshbuf_get_bignum1(e->request, challenge)))
307 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308
Damien Millerad833b32000-08-23 10:46:23 +1000309 /* Only protocol 1.1 is supported */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000310 if (sshbuf_len(e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000311 goto failure;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000312 if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
313 (r = sshbuf_get_u32(e->request, &response_type)) != 0)
314 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000315 if (response_type != 1)
316 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller1a534ae2002-01-22 23:26:13 +1100318 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100319 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000320 struct sshkey *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000321 /* Decrypt the challenge using the private key. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000322 if ((r = rsa_private_decrypt(challenge, challenge,
323 private->rsa) != 0)) {
324 fatal("%s: rsa_public_encrypt: %s", __func__,
325 ssh_err(r));
326 goto failure; /* XXX ? */
327 }
Damien Millerfd7c9111999-11-08 16:15:55 +1100328
markus@openbsd.org139ca812015-01-14 13:09:09 +0000329 /* The response is MD5 of decrypted challenge plus session id */
Damien Millerad833b32000-08-23 10:46:23 +1000330 len = BN_num_bytes(challenge);
331 if (len <= 0 || len > 32) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000332 logit("%s: bad challenge length %d", __func__, len);
Damien Millerad833b32000-08-23 10:46:23 +1000333 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100334 }
Damien Millerad833b32000-08-23 10:46:23 +1000335 memset(buf, 0, 32);
336 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100337 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
338 ssh_digest_update(md, buf, 32) < 0 ||
339 ssh_digest_update(md, session_id, 16) < 0 ||
340 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
341 fatal("%s: md5 failed", __func__);
342 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000343
344 /* Send the response. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000345 if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
346 (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
347 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000348 goto send;
349 }
350
markus@openbsd.org139ca812015-01-14 13:09:09 +0000351 failure:
Damien Millerad833b32000-08-23 10:46:23 +1000352 /* Unknown identity or protocol error. Send failure. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000353 if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
354 fatal("%s: buffer error: %s", __func__, ssh_err(r));
355 send:
356 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
357 fatal("%s: buffer error: %s", __func__, ssh_err(r));
358 sshkey_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100359 BN_clear_free(challenge);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000360 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000361}
Damien Miller1f0311c2014-05-15 14:24:09 +1000362#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Millerad833b32000-08-23 10:46:23 +1000364/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000365static void
Damien Millerad833b32000-08-23 10:46:23 +1000366process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000368 u_char *blob, *data, *signature = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000369 size_t blen, dlen, slen = 0;
370 u_int compat = 0, flags;
371 int r, ok = -1;
372 struct sshbuf *msg;
373 struct sshkey *key;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000374 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000375
djm@openbsd.org0088c572015-01-14 19:33:41 +0000376 if ((msg = sshbuf_new()) == NULL)
377 fatal("%s: sshbuf_new failed", __func__);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000378 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
379 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
380 (r = sshbuf_get_u32(e->request, &flags)) != 0)
381 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100382 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000383 compat = SSH_BUG_SIGBLOB;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000384 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000385 error("%s: cannot parse key blob: %s", __func__, ssh_err(ok));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000386 goto send;
Damien Millerad833b32000-08-23 10:46:23 +1000387 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000388 if ((id = lookup_identity(key, 2)) == NULL) {
389 verbose("%s: %s key not found", __func__, sshkey_type(key));
390 goto send;
391 }
392 if (id->confirm && confirm_key(id) != 0) {
393 verbose("%s: user refused key", __func__);
394 goto send;
395 }
396 if ((r = sshkey_sign(id->key, &signature, &slen,
397 data, dlen, compat)) != 0) {
398 error("%s: sshkey_sign: %s", __func__, ssh_err(ok));
399 goto send;
400 }
401 /* Success */
402 ok = 0;
403 send:
404 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000405 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000406 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
407 (r = sshbuf_put_string(msg, signature, slen)) != 0)
408 fatal("%s: buffer error: %s", __func__, ssh_err(r));
409 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
410 fatal("%s: buffer error: %s", __func__, ssh_err(r));
411
412 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
413 fatal("%s: buffer error: %s", __func__, ssh_err(r));
414
415 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000416 free(data);
417 free(blob);
418 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000419}
420
421/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static void
Damien Millerad833b32000-08-23 10:46:23 +1000423process_remove_identity(SocketEntry *e, int version)
424{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000425 size_t blen;
426 int r, success = 0;
427 struct sshkey *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000428 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000429#ifdef WITH_SSH1
430 u_int bits;
431#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100432
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000433 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000434#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000435 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000436 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
437 error("%s: sshkey_new failed", __func__);
438 return;
439 }
440 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
441 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
442 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
443 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000444
markus@openbsd.org139ca812015-01-14 13:09:09 +0000445 if (bits != sshkey_size(key))
446 logit("Warning: identity keysize mismatch: "
447 "actual %u, announced %u",
448 sshkey_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000449 break;
Damien Miller86687062014-07-02 15:28:02 +1000450#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000451 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000452 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
453 fatal("%s: buffer error: %s", __func__, ssh_err(r));
454 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
455 error("%s: sshkey_from_blob failed: %s",
456 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000457 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000458 break;
459 }
460 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100461 Identity *id = lookup_identity(key, version);
462 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100463 /*
464 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100465 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000466 * the array, we actually free the key there and move
467 * all the entries between the empty slot and the end
468 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100469 */
Damien Millerad833b32000-08-23 10:46:23 +1000470 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 if (tab->nentries < 1)
472 fatal("process_remove_identity: "
473 "internal error: tab->nentries %d",
474 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100475 TAILQ_REMOVE(&tab->idlist, id, next);
476 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000477 tab->nentries--;
478 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100479 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000480 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000481 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000482 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}
484
Ben Lindstrombba81212001-06-25 05:01:22 +0000485static void
Damien Millerad833b32000-08-23 10:46:23 +1000486process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000487{
Damien Millerad833b32000-08-23 10:46:23 +1000488 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100489 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490
Damien Miller95def091999-11-25 00:26:21 +1100491 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100492 for (id = TAILQ_FIRST(&tab->idlist); id;
493 id = TAILQ_FIRST(&tab->idlist)) {
494 TAILQ_REMOVE(&tab->idlist, id, next);
495 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100496 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000497
Damien Miller95def091999-11-25 00:26:21 +1100498 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000499 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
501 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000502 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100503}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000504
Darren Tucker2812dc92007-03-21 20:45:06 +1100505/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000506static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000507reaper(void)
508{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000509 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000510 Identity *id, *nxt;
511 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000512 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000513
514 for (version = 1; version < 3; version++) {
515 tab = idtab_lookup(version);
516 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
517 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100518 if (id->death == 0)
519 continue;
520 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100521 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000522 TAILQ_REMOVE(&tab->idlist, id, next);
523 free_identity(id);
524 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100525 } else
526 deadline = (deadline == 0) ? id->death :
527 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000528 }
529 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100530 if (deadline == 0 || deadline <= now)
531 return 0;
532 else
533 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000534}
535
Damien Millerb03ebe22015-01-15 03:08:58 +1100536#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000537/*
538 * XXX this and the corresponding serialisation function probably belongs
539 * in key.c
540 */
541static int
542agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
543{
544 struct sshkey *k = NULL;
545 int r = SSH_ERR_INTERNAL_ERROR;
546
547 *kp = NULL;
548 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
549 return SSH_ERR_ALLOC_FAIL;
550
551 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
552 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
553 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
554 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
555 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
556 /* SSH1 and SSL have p and q swapped */
557 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
558 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
559 goto out;
560
561 /* Generate additional parameters */
562 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
563 goto out;
564 /* enable blinding */
565 if (RSA_blinding_on(k->rsa, NULL) != 1) {
566 r = SSH_ERR_LIBCRYPTO_ERROR;
567 goto out;
568 }
569
570 r = 0; /* success */
571 out:
572 if (r == 0)
573 *kp = k;
574 else
575 sshkey_free(k);
576 return r;
577}
Damien Millerb03ebe22015-01-15 03:08:58 +1100578#endif
markus@openbsd.org139ca812015-01-14 13:09:09 +0000579
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000580static void
Damien Millerad833b32000-08-23 10:46:23 +1000581process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100582{
Damien Millerad833b32000-08-23 10:46:23 +1000583 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000584 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000585 int success = 0, confirm = 0;
586 u_int seconds;
587 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000588 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000589 struct sshkey *k = NULL;
590 u_char ctype;
591 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100592
Damien Millerad833b32000-08-23 10:46:23 +1000593 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000594#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000595 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000596 r = agent_decode_rsa1(e->request, &k);
Damien Millerc51d0732003-03-15 11:37:09 +1100597 break;
Damien Miller86687062014-07-02 15:28:02 +1000598#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100599 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000600 r = sshkey_private_deserialize(e->request, &k);
Damien Millerf0e90602013-12-07 10:40:26 +1100601 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100602 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000603 if (r != 0 || k == NULL ||
604 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
605 error("%s: decode private key: %s", __func__, ssh_err(r));
606 goto err;
607 }
Damien Miller86687062014-07-02 15:28:02 +1000608
markus@openbsd.org139ca812015-01-14 13:09:09 +0000609 while (sshbuf_len(e->request)) {
610 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
611 error("%s: buffer error: %s", __func__, ssh_err(r));
612 goto err;
613 }
614 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000615 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000616 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
617 error("%s: bad lifetime constraint: %s",
618 __func__, ssh_err(r));
619 goto err;
620 }
621 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000622 break;
Damien Miller6c711792003-01-24 11:36:23 +1100623 case SSH_AGENT_CONSTRAIN_CONFIRM:
624 confirm = 1;
625 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000626 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000627 error("%s: Unknown constraint %d", __func__, ctype);
628 err:
629 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000630 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000631 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000632 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000633 }
634 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000635
Damien Miller1cfadab2008-06-30 00:05:21 +1000636 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100637 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000638 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000639 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100640 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000641 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000642 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
643 /* Increment the number of identities. */
644 tab->nentries++;
645 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000646 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000647 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000648 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000649 id->comment = comment;
650 id->death = death;
651 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000652send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000653 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000654}
655
Ben Lindstrom2f717042002-06-06 21:52:03 +0000656/* XXX todo: encrypt sensitive data with passphrase */
657static void
658process_lock_agent(SocketEntry *e, int lock)
659{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000660 int r, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000661 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000662
markus@openbsd.org139ca812015-01-14 13:09:09 +0000663 if ((r = sshbuf_get_cstring(e->request, &passwd, NULL)) != 0)
664 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000665 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
666 locked = 0;
Damien Millera5103f42014-02-04 11:20:14 +1100667 explicit_bzero(lock_passwd, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000668 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000669 lock_passwd = NULL;
670 success = 1;
671 } else if (!locked && lock) {
672 locked = 1;
673 lock_passwd = xstrdup(passwd);
674 success = 1;
675 }
Damien Millera5103f42014-02-04 11:20:14 +1100676 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000677 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000678 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000679}
680
681static void
682no_identities(SocketEntry *e, u_int type)
683{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000684 struct sshbuf *msg;
685 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000686
markus@openbsd.org139ca812015-01-14 13:09:09 +0000687 if ((msg = sshbuf_new()) == NULL)
688 fatal("%s: sshbuf_new failed", __func__);
689 if ((r = sshbuf_put_u8(msg,
Ben Lindstrom2f717042002-06-06 21:52:03 +0000690 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
markus@openbsd.org139ca812015-01-14 13:09:09 +0000691 SSH_AGENT_RSA_IDENTITIES_ANSWER :
692 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
693 (r = sshbuf_put_u32(msg, 0)) != 0 ||
694 (r = sshbuf_put_stringb(e->output, msg)) != 0)
695 fatal("%s: buffer error: %s", __func__, ssh_err(r));
696 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000697}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698
Damien Miller7ea845e2010-02-12 09:21:02 +1100699#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000701process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000702{
Damien Miller7ea845e2010-02-12 09:21:02 +1100703 char *provider = NULL, *pin;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000704 int r, i, version, count = 0, success = 0, confirm = 0;
705 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000706 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000707 u_char type;
708 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000709 Identity *id;
710 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100711
markus@openbsd.org139ca812015-01-14 13:09:09 +0000712 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
713 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
714 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000715
markus@openbsd.org139ca812015-01-14 13:09:09 +0000716 while (sshbuf_len(e->request)) {
717 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
718 fatal("%s: buffer error: %s", __func__, ssh_err(r));
719 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000720 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000721 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
722 fatal("%s: buffer error: %s",
723 __func__, ssh_err(r));
724 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000725 break;
726 case SSH_AGENT_CONSTRAIN_CONFIRM:
727 confirm = 1;
728 break;
729 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000730 error("process_add_smartcard_key: "
731 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000732 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000733 }
734 }
735 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000736 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000737
Damien Miller7ea845e2010-02-12 09:21:02 +1100738 count = pkcs11_add_provider(provider, pin, &keys);
739 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000740 k = keys[i];
741 version = k->type == KEY_RSA1 ? 1 : 2;
742 tab = idtab_lookup(version);
743 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100744 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000745 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100746 id->provider = xstrdup(provider);
747 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000748 id->death = death;
749 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000750 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
751 tab->nentries++;
752 success = 1;
753 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000754 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000755 }
756 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000757 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000758send:
Darren Tuckera627d422013-06-02 07:31:17 +1000759 free(pin);
760 free(provider);
761 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000762 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000763}
764
765static void
766process_remove_smartcard_key(SocketEntry *e)
767{
Damien Miller7ea845e2010-02-12 09:21:02 +1100768 char *provider = NULL, *pin = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000769 int r, version, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100770 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000771 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000772
markus@openbsd.org139ca812015-01-14 13:09:09 +0000773 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
774 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
775 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000776 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000777
Damien Miller7ea845e2010-02-12 09:21:02 +1100778 for (version = 1; version < 3; version++) {
779 tab = idtab_lookup(version);
780 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
781 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100782 /* Skip file--based keys */
783 if (id->provider == NULL)
784 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100785 if (!strcmp(provider, id->provider)) {
786 TAILQ_REMOVE(&tab->idlist, id, next);
787 free_identity(id);
788 tab->nentries--;
789 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000790 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000791 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100792 if (pkcs11_del_provider(provider) == 0)
793 success = 1;
794 else
795 error("process_remove_smartcard_key:"
796 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000797 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000798 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000799}
Damien Miller7ea845e2010-02-12 09:21:02 +1100800#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000801
Damien Millerad833b32000-08-23 10:46:23 +1000802/* dispatch incoming messages */
803
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805process_message(SocketEntry *e)
806{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000807 u_int msg_len;
808 u_char type;
809 const u_char *cp;
810 int r;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000811
markus@openbsd.org139ca812015-01-14 13:09:09 +0000812 if (sshbuf_len(e->input) < 5)
Damien Miller95def091999-11-25 00:26:21 +1100813 return; /* Incomplete message. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000814 cp = sshbuf_ptr(e->input);
815 msg_len = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100816 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000817 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100818 return;
819 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000820 if (sshbuf_len(e->input) < msg_len + 4)
Damien Miller95def091999-11-25 00:26:21 +1100821 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000822
823 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000824 sshbuf_reset(e->request);
825 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
826 (r = sshbuf_get_u8(e->request, &type)) != 0)
827 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828
Ben Lindstrom2f717042002-06-06 21:52:03 +0000829 /* check wheter agent is locked */
830 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000831 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000832 switch (type) {
833 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
834 case SSH2_AGENTC_REQUEST_IDENTITIES:
835 /* send empty lists */
836 no_identities(e, type);
837 break;
838 default:
839 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000840 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000841 }
842 return;
843 }
844
Ben Lindstrom3f471632001-07-04 03:53:15 +0000845 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100846 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000847 case SSH_AGENTC_LOCK:
848 case SSH_AGENTC_UNLOCK:
849 process_lock_agent(e, type == SSH_AGENTC_LOCK);
850 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000851#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000852 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100853 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000854 process_authentication_challenge1(e);
855 break;
856 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
857 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100858 break;
859 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000860 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000861 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100862 break;
863 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000864 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100865 break;
866 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000867 process_remove_all_identities(e, 1);
868 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000869#endif
Damien Millerad833b32000-08-23 10:46:23 +1000870 /* ssh2 */
871 case SSH2_AGENTC_SIGN_REQUEST:
872 process_sign_request2(e);
873 break;
874 case SSH2_AGENTC_REQUEST_IDENTITIES:
875 process_request_identities(e, 2);
876 break;
877 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000878 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000879 process_add_identity(e, 2);
880 break;
881 case SSH2_AGENTC_REMOVE_IDENTITY:
882 process_remove_identity(e, 2);
883 break;
884 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
885 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100886 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100887#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000888 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000889 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000890 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100891 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000892 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
893 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100894 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100895#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100896 default:
897 /* Unknown message. Respond with failure. */
898 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000899 sshbuf_reset(e->request);
900 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100901 break;
902 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903}
904
Ben Lindstrombba81212001-06-25 05:01:22 +0000905static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000906new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000908 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000909
Damien Miller232711f2004-06-15 10:35:30 +1000910 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911
Damien Miller95def091999-11-25 00:26:21 +1100912 if (fd > max_fd)
913 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 for (i = 0; i < sockets_alloc; i++)
916 if (sockets[i].type == AUTH_UNUSED) {
917 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000918 if ((sockets[i].input = sshbuf_new()) == NULL)
919 fatal("%s: sshbuf_new failed", __func__);
920 if ((sockets[i].output = sshbuf_new()) == NULL)
921 fatal("%s: sshbuf_new failed", __func__);
922 if ((sockets[i].request = sshbuf_new()) == NULL)
923 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000924 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100925 return;
926 }
927 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000928 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100929 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000930 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100931 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000932 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100933 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000934 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
935 fatal("%s: sshbuf_new failed", __func__);
936 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
937 fatal("%s: sshbuf_new failed", __func__);
938 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
939 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000940 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000941}
942
Ben Lindstrombba81212001-06-25 05:01:22 +0000943static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100944prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
945 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946{
Darren Tucker55119252013-06-02 07:43:59 +1000947 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000948 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100949 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000950 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000951
952 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100953 switch (sockets[i].type) {
954 case AUTH_SOCKET:
955 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000956 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100957 break;
958 case AUTH_UNUSED:
959 break;
960 default:
961 fatal("Unknown socket type %d", sockets[i].type);
962 break;
963 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000964 }
965
966 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000967 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000968 free(*fdrp);
969 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000970 *fdrp = xmalloc(sz);
971 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000972 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000973 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000974 if (n < *fdl)
975 debug("XXX shrink: %d < %d", n, *fdl);
976 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000977 memset(*fdrp, 0, sz);
978 memset(*fdwp, 0, sz);
979
980 for (i = 0; i < sockets_alloc; i++) {
981 switch (sockets[i].type) {
982 case AUTH_SOCKET:
983 case AUTH_CONNECTION:
984 FD_SET(sockets[i].fd, *fdrp);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000985 if (sshbuf_len(sockets[i].output) > 0)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000986 FD_SET(sockets[i].fd, *fdwp);
987 break;
988 default:
989 break;
990 }
991 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100992 deadline = reaper();
993 if (parent_alive_interval != 0)
994 deadline = (deadline == 0) ? parent_alive_interval :
995 MIN(deadline, parent_alive_interval);
996 if (deadline == 0) {
997 *tvpp = NULL;
998 } else {
999 tv.tv_sec = deadline;
1000 tv.tv_usec = 0;
1001 *tvpp = &tv;
1002 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001003 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004}
1005
Ben Lindstrombba81212001-06-25 05:01:22 +00001006static void
Damien Miller95def091999-11-25 00:26:21 +11001007after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008{
Ben Lindstrom822b6342002-06-23 21:38:49 +00001009 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +11001010 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +11001011 char buf[1024];
markus@openbsd.org139ca812015-01-14 13:09:09 +00001012 int len, sock, r;
Darren Tucker72473c62009-10-07 09:01:03 +11001013 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001014 uid_t euid;
1015 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001016
Darren Tucker72473c62009-10-07 09:01:03 +11001017 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001018 switch (sockets[i].type) {
1019 case AUTH_UNUSED:
1020 break;
1021 case AUTH_SOCKET:
1022 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001023 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001024 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001025 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001026 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001027 error("accept from AUTH_SOCKET: %s",
1028 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001029 break;
1030 }
Damien Miller538f1812002-09-12 09:51:10 +10001031 if (getpeereid(sock, &euid, &egid) < 0) {
1032 error("getpeereid %d failed: %s",
1033 sock, strerror(errno));
1034 close(sock);
1035 break;
1036 }
Damien Milleraf9de382002-10-03 11:54:35 +10001037 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001038 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001039 "peer euid %u != uid %u",
1040 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001041 close(sock);
1042 break;
1043 }
Damien Miller95def091999-11-25 00:26:21 +11001044 new_socket(AUTH_CONNECTION, sock);
1045 }
1046 break;
1047 case AUTH_CONNECTION:
markus@openbsd.org139ca812015-01-14 13:09:09 +00001048 if (sshbuf_len(sockets[i].output) > 0 &&
Damien Miller95def091999-11-25 00:26:21 +11001049 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001050 len = write(sockets[i].fd,
markus@openbsd.org139ca812015-01-14 13:09:09 +00001051 sshbuf_ptr(sockets[i].output),
1052 sshbuf_len(sockets[i].output));
Darren Tucker72473c62009-10-07 09:01:03 +11001053 if (len == -1 && (errno == EAGAIN ||
1054 errno == EWOULDBLOCK ||
1055 errno == EINTR))
1056 continue;
Damien Miller95def091999-11-25 00:26:21 +11001057 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001058 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001059 break;
1060 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001061 if ((r = sshbuf_consume(sockets[i].output,
1062 len)) != 0)
1063 fatal("%s: buffer error: %s",
1064 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001065 }
1066 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001067 len = read(sockets[i].fd, buf, sizeof(buf));
1068 if (len == -1 && (errno == EAGAIN ||
1069 errno == EWOULDBLOCK ||
1070 errno == EINTR))
1071 continue;
Damien Miller95def091999-11-25 00:26:21 +11001072 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001073 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001074 break;
1075 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001076 if ((r = sshbuf_put(sockets[i].input,
1077 buf, len)) != 0)
1078 fatal("%s: buffer error: %s",
1079 __func__, ssh_err(r));
Damien Millerf4977942014-07-30 12:32:46 +10001080 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001081 process_message(&sockets[i]);
1082 }
1083 break;
1084 default:
1085 fatal("Unknown type %d", sockets[i].type);
1086 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001087}
1088
Ben Lindstrombba81212001-06-25 05:01:22 +00001089static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001090cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001091{
Damien Millerb1e967c2014-07-03 21:22:40 +10001092 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1093 return;
1094 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001095 if (socket_name[0])
1096 unlink(socket_name);
1097 if (socket_dir[0])
1098 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001099}
1100
Darren Tucker3e33cec2003-10-02 16:12:36 +10001101void
Damien Miller792c5111999-10-29 09:47:09 +10001102cleanup_exit(int i)
1103{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001104 cleanup_socket();
1105 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001106}
1107
Damien Miller1c13bd82006-03-26 14:28:14 +11001108/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001109static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001110cleanup_handler(int sig)
1111{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001112 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001113#ifdef ENABLE_PKCS11
1114 pkcs11_terminate();
1115#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001116 _exit(2);
1117}
1118
Ben Lindstrombba81212001-06-25 05:01:22 +00001119static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001120check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001121{
Darren Tucker3e78a512011-06-03 14:14:16 +10001122 /*
1123 * If our parent has exited then getppid() will return (pid_t)1,
1124 * so testing for that should be safe.
1125 */
1126 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001127 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001128 cleanup_socket();
1129 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001130 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001131}
1132
1133static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001134usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001135{
Damien Millerf0858de2014-04-20 13:01:30 +10001136 fprintf(stderr,
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001137 "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-E fingerprint_hash]\n"
1138 " [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001139 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001140 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001141}
1142
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001143int
1144main(int ac, char **av)
1145{
Damien Miller6c711792003-01-24 11:36:23 +11001146 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001147 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001148 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001149 char *shell, *format, *pidstr, *agentsocket = NULL;
1150 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001151#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001152 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001153#endif
Damien Miller615f9392000-05-17 22:53:33 +10001154 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001155 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001156 pid_t pid;
1157 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001158 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001159 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001160 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001161
Darren Tuckerce321d82005-10-03 18:11:24 +10001162 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1163 sanitise_stdfd();
1164
Damien Miller6cffb9a2002-09-04 16:20:26 +10001165 /* drop */
1166 setegid(getgid());
1167 setgid(getgid());
1168
Damien Miller6c4914a2004-03-03 11:08:59 +11001169#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1170 /* Disable ptrace on Linux without sgid bit */
1171 prctl(PR_SET_DUMPABLE, 0);
1172#endif
1173
Damien Miller1f0311c2014-05-15 14:24:09 +10001174#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001175 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001176#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001177
Damien Miller59d3d5b2003-08-22 09:34:41 +10001178 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001179 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001180
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001181 while ((ch = getopt(ac, av, "cdksE:a:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001182 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001183 case 'E':
1184 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1185 if (fingerprint_hash == -1)
1186 fatal("Invalid hash algorithm \"%s\"", optarg);
1187 break;
Damien Miller95def091999-11-25 00:26:21 +11001188 case 'c':
1189 if (s_flag)
1190 usage();
1191 c_flag++;
1192 break;
1193 case 'k':
1194 k_flag++;
1195 break;
1196 case 's':
1197 if (c_flag)
1198 usage();
1199 s_flag++;
1200 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001201 case 'd':
1202 if (d_flag)
1203 usage();
1204 d_flag++;
1205 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001206 case 'a':
1207 agentsocket = optarg;
1208 break;
Damien Miller53d81482003-01-22 11:47:19 +11001209 case 't':
1210 if ((lifetime = convtime(optarg)) == -1) {
1211 fprintf(stderr, "Invalid lifetime\n");
1212 usage();
1213 }
1214 break;
Damien Miller95def091999-11-25 00:26:21 +11001215 default:
1216 usage();
1217 }
Damien Miller792c5111999-10-29 09:47:09 +10001218 }
Damien Miller95def091999-11-25 00:26:21 +11001219 ac -= optind;
1220 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001221
Ben Lindstromd94580c2001-07-04 03:48:02 +00001222 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001223 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001224
Ben Lindstromeecdf232002-04-02 21:03:51 +00001225 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001226 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001227 if (shell != NULL && (len = strlen(shell)) > 2 &&
1228 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001229 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001230 }
Damien Miller95def091999-11-25 00:26:21 +11001231 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001232 const char *errstr = NULL;
1233
Damien Miller95def091999-11-25 00:26:21 +11001234 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1235 if (pidstr == NULL) {
1236 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001237 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001238 exit(1);
1239 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001240 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1241 if (errstr) {
1242 fprintf(stderr,
1243 "%s=\"%s\", which is not a good PID: %s\n",
1244 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001245 exit(1);
1246 }
1247 if (kill(pid, SIGTERM) == -1) {
1248 perror("kill");
1249 exit(1);
1250 }
1251 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1252 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1253 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001254 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001255 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001256 }
Damien Miller95def091999-11-25 00:26:21 +11001257 parent_pid = getpid();
1258
Ben Lindstromb7788f32002-06-06 21:46:08 +00001259 if (agentsocket == NULL) {
1260 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001261 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001262 if (mkdtemp(socket_dir) == NULL) {
1263 perror("mkdtemp: private socket dir");
1264 exit(1);
1265 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001266 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1267 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001268 } else {
1269 /* Try to use specified agent socket */
1270 socket_dir[0] = '\0';
1271 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001272 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001273
Damien Miller5428f641999-11-25 11:54:57 +11001274 /*
1275 * Create socket early so it will exist before command gets run from
1276 * the parent.
1277 */
Damien Millerab2ec582014-07-18 15:04:47 +10001278 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001279 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001280 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001281 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001282 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001283 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001284 }
Damien Millerab2ec582014-07-18 15:04:47 +10001285 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001286
Damien Miller5428f641999-11-25 11:54:57 +11001287 /*
1288 * Fork, and have the parent execute the command, if any, or present
1289 * the socket data. The child continues as the authentication agent.
1290 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001291 if (d_flag) {
1292 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1293 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1294 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1295 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001296 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001297 goto skip;
1298 }
Damien Miller95def091999-11-25 00:26:21 +11001299 pid = fork();
1300 if (pid == -1) {
1301 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001302 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001303 }
1304 if (pid != 0) { /* Parent - execute the given command. */
1305 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001306 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001307 if (ac == 0) {
1308 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1309 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001310 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001311 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001312 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001313 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001314 exit(0);
1315 }
Damien Millere4340be2000-09-16 13:29:08 +11001316 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1317 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1318 perror("setenv");
1319 exit(1);
1320 }
Damien Miller95def091999-11-25 00:26:21 +11001321 execvp(av[0], av);
1322 perror(av[0]);
1323 exit(1);
1324 }
Damien Millerc653b892002-02-08 22:05:41 +11001325 /* child */
1326 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001327
1328 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001329 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001330 cleanup_exit(1);
1331 }
1332
1333 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001334 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1335 /* XXX might close listen socket */
1336 (void)dup2(fd, STDIN_FILENO);
1337 (void)dup2(fd, STDOUT_FILENO);
1338 (void)dup2(fd, STDERR_FILENO);
1339 if (fd > 2)
1340 close(fd);
1341 }
Damien Miller95def091999-11-25 00:26:21 +11001342
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001343#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001344 /* deny core dumps, since memory contains unencrypted private keys */
1345 rlim.rlim_cur = rlim.rlim_max = 0;
1346 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001347 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001348 cleanup_exit(1);
1349 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001350#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001351
1352skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001353
Damien Millerb1e967c2014-07-03 21:22:40 +10001354 cleanup_pid = getpid();
1355
Damien Miller7ea845e2010-02-12 09:21:02 +11001356#ifdef ENABLE_PKCS11
1357 pkcs11_init(0);
1358#endif
Damien Miller95def091999-11-25 00:26:21 +11001359 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001360 if (ac > 0)
1361 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001362 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001363 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001364 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001365 signal(SIGHUP, cleanup_handler);
1366 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001367 nalloc = 0;
1368
Damien Miller95def091999-11-25 00:26:21 +11001369 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001370 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1371 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001372 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001373 if (parent_alive_interval != 0)
1374 check_parent_exists();
1375 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001376 if (result < 0) {
1377 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001378 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001379 fatal("select: %s", strerror(saved_errno));
1380 } else if (result > 0)
1381 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001382 }
1383 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384}