blob: c75575f66443316939a4504248ff417f0ee6c146 [file] [log] [blame]
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001/* $OpenBSD: ssh-agent.c,v 1.202 2015/04/24 06:26:49 jmc 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
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000039#include <sys/param.h> /* MIN MAX */
Damien Miller574c41f2006-03-15 11:40:10 +110040#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100041#include <sys/param.h>
42#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110043#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100044#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100045#ifdef HAVE_SYS_TIME_H
46# include <sys/time.h>
47#endif
Damien Miller574c41f2006-03-15 11:40:10 +110048#ifdef HAVE_SYS_UN_H
49# include <sys/un.h>
50#endif
Damien Miller9b481512002-09-12 10:43:29 +100051#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100052
Damien Miller1f0311c2014-05-15 14:24:09 +100053#ifdef WITH_OPENSSL
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <openssl/evp.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110055#include "openbsd-compat/openssl-compat.h"
Damien Miller1f0311c2014-05-15 14:24:09 +100056#endif
Damien Millere3476ed2006-07-24 14:13:33 +100057
Darren Tucker39972492006-07-12 22:22:46 +100058#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100059#include <fcntl.h>
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000060#include <limits.h>
Damien Miller03e20032006-03-15 11:16:59 +110061#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110062# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110063#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110064#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100065#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100066#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100067#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100068#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100069#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100070#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110071
markus@openbsd.org139ca812015-01-14 13:09:09 +000072#include "key.h" /* XXX for typedef */
73#include "buffer.h" /* XXX for typedef */
74
Damien Millerd7834352006-08-05 12:39:39 +100075#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076#include "ssh.h"
77#include "rsa.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000078#include "sshbuf.h"
79#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100080#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110081#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000082#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110083#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110084#include "digest.h"
markus@openbsd.org139ca812015-01-14 13:09:09 +000085#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller7ea845e2010-02-12 09:21:02 +110087#ifdef ENABLE_PKCS11
88#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000089#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000090
Damien Miller6c4914a2004-03-03 11:08:59 +110091#if defined(HAVE_SYS_PRCTL_H)
92#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
93#endif
94
Ben Lindstrom65366a82001-12-06 16:32:47 +000095typedef enum {
96 AUTH_UNUSED,
97 AUTH_SOCKET,
98 AUTH_CONNECTION
99} sock_type;
100
Damien Miller95def091999-11-25 00:26:21 +1100101typedef struct {
102 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000103 sock_type type;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000104 struct sshbuf *input;
105 struct sshbuf *output;
106 struct sshbuf *request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} SocketEntry;
108
Ben Lindstrom46c16222000-12-22 01:43:59 +0000109u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110SocketEntry *sockets = NULL;
111
Damien Miller1a534ae2002-01-22 23:26:13 +1100112typedef struct identity {
113 TAILQ_ENTRY(identity) next;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000114 struct sshkey *key;
Damien Miller95def091999-11-25 00:26:21 +1100115 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100116 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000117 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100118 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119} Identity;
120
Damien Millerad833b32000-08-23 10:46:23 +1000121typedef struct {
122 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100123 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000124} Idtab;
125
126/* private key table, one per protocol version */
127Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
129int max_fd = 0;
130
131/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000132pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000133time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Millerb1e967c2014-07-03 21:22:40 +1000135/* pid of process for which cleanup_socket is applicable */
136pid_t cleanup_pid = 0;
137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* pathname and directory for AUTH_SOCKET */
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000139char socket_name[PATH_MAX];
140char socket_dir[PATH_MAX];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Ben Lindstrom2f717042002-06-06 21:52:03 +0000142/* locking */
143int locked = 0;
144char *lock_passwd = NULL;
145
Damien Miller95def091999-11-25 00:26:21 +1100146extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100147
Darren Tucker55119252013-06-02 07:43:59 +1000148/* Default lifetime in seconds (0 == forever) */
149static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100150
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000151static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Damien Miller58f34862002-09-04 16:31:21 +1000154close_socket(SocketEntry *e)
155{
Damien Miller58f34862002-09-04 16:31:21 +1000156 close(e->fd);
157 e->fd = -1;
158 e->type = AUTH_UNUSED;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000159 sshbuf_free(e->input);
160 sshbuf_free(e->output);
161 sshbuf_free(e->request);
Damien Miller58f34862002-09-04 16:31:21 +1000162}
163
164static void
Damien Millerad833b32000-08-23 10:46:23 +1000165idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166{
Damien Millerad833b32000-08-23 10:46:23 +1000167 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000168
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000169 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100170 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000171 idtable[i].nentries = 0;
172 }
173}
174
175/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000176static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000177idtab_lookup(int version)
178{
179 if (version < 1 || version > 2)
180 fatal("internal error, bad protocol version %d", version);
181 return &idtable[version];
182}
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 *
markus@openbsd.org139ca812015-01-14 13:09:09 +0000195lookup_identity(struct sshkey *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000196{
Damien Miller1a534ae2002-01-22 23:26:13 +1100197 Identity *id;
198
Damien Millerad833b32000-08-23 10:46:23 +1000199 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100200 TAILQ_FOREACH(id, &tab->idlist, next) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000201 if (sshkey_equal(key, id->key))
Damien Miller1a534ae2002-01-22 23:26:13 +1100202 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000203 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100204 return (NULL);
205}
206
Damien Miller6c711792003-01-24 11:36:23 +1100207/* Check confirmation of keysign request */
208static int
209confirm_key(Identity *id)
210{
Darren Tuckerce327b62004-11-05 20:38:03 +1100211 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100212 int ret = -1;
213
markus@openbsd.org139ca812015-01-14 13:09:09 +0000214 p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000215 if (p != NULL &&
216 ask_permission("Allow use of key %s?\nKey fingerprint %s.",
Darren Tuckerce327b62004-11-05 20:38:03 +1100217 id->comment, p))
218 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000219 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100220
Damien Miller6c711792003-01-24 11:36:23 +1100221 return (ret);
222}
223
markus@openbsd.org139ca812015-01-14 13:09:09 +0000224static void
225send_status(SocketEntry *e, int success)
226{
227 int r;
228
229 if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
230 (r = sshbuf_put_u8(e->output, success ?
231 SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
232 fatal("%s: buffer error: %s", __func__, ssh_err(r));
233}
234
Damien Millerad833b32000-08-23 10:46:23 +1000235/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Millerad833b32000-08-23 10:46:23 +1000237process_request_identities(SocketEntry *e, int version)
238{
239 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100240 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000241 struct sshbuf *msg;
242 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243
markus@openbsd.org139ca812015-01-14 13:09:09 +0000244 if ((msg = sshbuf_new()) == NULL)
245 fatal("%s: sshbuf_new failed", __func__);
246 if ((r = sshbuf_put_u8(msg, (version == 1) ?
247 SSH_AGENT_RSA_IDENTITIES_ANSWER :
248 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
249 (r = sshbuf_put_u32(msg, tab->nentries)) != 0)
250 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller1a534ae2002-01-22 23:26:13 +1100251 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100252 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000253#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000254 if ((r = sshbuf_put_u32(msg,
255 BN_num_bits(id->key->rsa->n))) != 0 ||
256 (r = sshbuf_put_bignum1(msg,
257 id->key->rsa->e)) != 0 ||
258 (r = sshbuf_put_bignum1(msg,
259 id->key->rsa->n)) != 0)
260 fatal("%s: buffer error: %s",
261 __func__, ssh_err(r));
Damien Miller1f0311c2014-05-15 14:24:09 +1000262#endif
Damien Millerad833b32000-08-23 10:46:23 +1000263 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000264 u_char *blob;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000265 size_t blen;
266
267 if ((r = sshkey_to_blob(id->key, &blob, &blen)) != 0) {
268 error("%s: sshkey_to_blob: %s", __func__,
269 ssh_err(r));
270 continue;
271 }
272 if ((r = sshbuf_put_string(msg, blob, blen)) != 0)
273 fatal("%s: buffer error: %s",
274 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000275 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000276 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000277 if ((r = sshbuf_put_cstring(msg, id->comment)) != 0)
278 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +1100279 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000280 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
281 fatal("%s: buffer error: %s", __func__, ssh_err(r));
282 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283}
284
Damien Miller1f0311c2014-05-15 14:24:09 +1000285#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000286/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000287static void
Damien Millerad833b32000-08-23 10:46:23 +1000288process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000290 u_char buf[32], mdbuf[16], session_id[16];
291 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000292 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000293 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000294 int r, len;
295 struct sshbuf *msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100296 struct ssh_digest_ctx *md;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000297 struct sshkey *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
markus@openbsd.org139ca812015-01-14 13:09:09 +0000299 if ((msg = sshbuf_new()) == NULL)
300 fatal("%s: sshbuf_new failed", __func__);
301 if ((key = sshkey_new(KEY_RSA1)) == NULL)
302 fatal("%s: sshkey_new failed", __func__);
Damien Millerda755162002-01-22 23:09:22 +1100303 if ((challenge = BN_new()) == NULL)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000304 fatal("%s: BN_new failed", __func__);
Damien Millerad833b32000-08-23 10:46:23 +1000305
markus@openbsd.org139ca812015-01-14 13:09:09 +0000306 if ((r = sshbuf_get_u32(e->request, NULL)) != 0 || /* ignored */
307 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
308 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0 ||
309 (r = sshbuf_get_bignum1(e->request, challenge)))
310 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Millerad833b32000-08-23 10:46:23 +1000312 /* Only protocol 1.1 is supported */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000313 if (sshbuf_len(e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000314 goto failure;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000315 if ((r = sshbuf_get(e->request, session_id, sizeof(session_id))) != 0 ||
316 (r = sshbuf_get_u32(e->request, &response_type)) != 0)
317 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000318 if (response_type != 1)
319 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320
Damien Miller1a534ae2002-01-22 23:26:13 +1100321 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100322 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000323 struct sshkey *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000324 /* Decrypt the challenge using the private key. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000325 if ((r = rsa_private_decrypt(challenge, challenge,
326 private->rsa) != 0)) {
327 fatal("%s: rsa_public_encrypt: %s", __func__,
328 ssh_err(r));
329 goto failure; /* XXX ? */
330 }
Damien Millerfd7c9111999-11-08 16:15:55 +1100331
markus@openbsd.org139ca812015-01-14 13:09:09 +0000332 /* The response is MD5 of decrypted challenge plus session id */
Damien Millerad833b32000-08-23 10:46:23 +1000333 len = BN_num_bytes(challenge);
334 if (len <= 0 || len > 32) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000335 logit("%s: bad challenge length %d", __func__, len);
Damien Millerad833b32000-08-23 10:46:23 +1000336 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100337 }
Damien Millerad833b32000-08-23 10:46:23 +1000338 memset(buf, 0, 32);
339 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100340 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
341 ssh_digest_update(md, buf, 32) < 0 ||
342 ssh_digest_update(md, session_id, 16) < 0 ||
343 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
344 fatal("%s: md5 failed", __func__);
345 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000346
347 /* Send the response. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000348 if ((r = sshbuf_put_u8(msg, SSH_AGENT_RSA_RESPONSE)) != 0 ||
349 (r = sshbuf_put(msg, mdbuf, sizeof(mdbuf))) != 0)
350 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerad833b32000-08-23 10:46:23 +1000351 goto send;
352 }
353
markus@openbsd.org139ca812015-01-14 13:09:09 +0000354 failure:
Damien Millerad833b32000-08-23 10:46:23 +1000355 /* Unknown identity or protocol error. Send failure. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000356 if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
357 fatal("%s: buffer error: %s", __func__, ssh_err(r));
358 send:
359 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
360 fatal("%s: buffer error: %s", __func__, ssh_err(r));
361 sshkey_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100362 BN_clear_free(challenge);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000363 sshbuf_free(msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364}
Damien Miller1f0311c2014-05-15 14:24:09 +1000365#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Millerad833b32000-08-23 10:46:23 +1000367/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000368static void
Damien Millerad833b32000-08-23 10:46:23 +1000369process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000371 u_char *blob, *data, *signature = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000372 size_t blen, dlen, slen = 0;
373 u_int compat = 0, flags;
374 int r, ok = -1;
375 struct sshbuf *msg;
376 struct sshkey *key;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000377 struct identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000378
djm@openbsd.org0088c572015-01-14 19:33:41 +0000379 if ((msg = sshbuf_new()) == NULL)
380 fatal("%s: sshbuf_new failed", __func__);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000381 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0 ||
382 (r = sshbuf_get_string(e->request, &data, &dlen)) != 0 ||
383 (r = sshbuf_get_u32(e->request, &flags)) != 0)
384 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller62cee002000-09-23 17:15:56 +1100385 if (flags & SSH_AGENT_OLD_SIGNATURE)
markus@openbsd.org139ca812015-01-14 13:09:09 +0000386 compat = SSH_BUG_SIGBLOB;
djm@openbsd.org0088c572015-01-14 19:33:41 +0000387 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000388 error("%s: cannot parse key blob: %s", __func__, ssh_err(ok));
djm@openbsd.org0088c572015-01-14 19:33:41 +0000389 goto send;
Damien Millerad833b32000-08-23 10:46:23 +1000390 }
djm@openbsd.org0088c572015-01-14 19:33:41 +0000391 if ((id = lookup_identity(key, 2)) == NULL) {
392 verbose("%s: %s key not found", __func__, sshkey_type(key));
393 goto send;
394 }
395 if (id->confirm && confirm_key(id) != 0) {
396 verbose("%s: user refused key", __func__);
397 goto send;
398 }
399 if ((r = sshkey_sign(id->key, &signature, &slen,
400 data, dlen, compat)) != 0) {
401 error("%s: sshkey_sign: %s", __func__, ssh_err(ok));
402 goto send;
403 }
404 /* Success */
405 ok = 0;
406 send:
407 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000408 if (ok == 0) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000409 if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
410 (r = sshbuf_put_string(msg, signature, slen)) != 0)
411 fatal("%s: buffer error: %s", __func__, ssh_err(r));
412 } else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
413 fatal("%s: buffer error: %s", __func__, ssh_err(r));
414
415 if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
416 fatal("%s: buffer error: %s", __func__, ssh_err(r));
417
418 sshbuf_free(msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000419 free(data);
420 free(blob);
421 free(signature);
Damien Millerad833b32000-08-23 10:46:23 +1000422}
423
424/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000425static void
Damien Millerad833b32000-08-23 10:46:23 +1000426process_remove_identity(SocketEntry *e, int version)
427{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000428 size_t blen;
429 int r, success = 0;
430 struct sshkey *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000431 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000432#ifdef WITH_SSH1
433 u_int bits;
434#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100435
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000436 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000437#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000438 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000439 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
440 error("%s: sshkey_new failed", __func__);
441 return;
442 }
443 if ((r = sshbuf_get_u32(e->request, &bits)) != 0 ||
444 (r = sshbuf_get_bignum1(e->request, key->rsa->e)) != 0 ||
445 (r = sshbuf_get_bignum1(e->request, key->rsa->n)) != 0)
446 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
markus@openbsd.org139ca812015-01-14 13:09:09 +0000448 if (bits != sshkey_size(key))
449 logit("Warning: identity keysize mismatch: "
450 "actual %u, announced %u",
451 sshkey_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000452 break;
Damien Miller86687062014-07-02 15:28:02 +1000453#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000454 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000455 if ((r = sshbuf_get_string(e->request, &blob, &blen)) != 0)
456 fatal("%s: buffer error: %s", __func__, ssh_err(r));
457 if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
458 error("%s: sshkey_from_blob failed: %s",
459 __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000460 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000461 break;
462 }
463 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100464 Identity *id = lookup_identity(key, version);
465 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100466 /*
467 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100468 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000469 * the array, we actually free the key there and move
470 * all the entries between the empty slot and the end
471 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100472 */
Damien Millerad833b32000-08-23 10:46:23 +1000473 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 if (tab->nentries < 1)
475 fatal("process_remove_identity: "
476 "internal error: tab->nentries %d",
477 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100478 TAILQ_REMOVE(&tab->idlist, id, next);
479 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000480 tab->nentries--;
481 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100482 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000483 sshkey_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000484 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000485 send_status(e, success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486}
487
Ben Lindstrombba81212001-06-25 05:01:22 +0000488static void
Damien Millerad833b32000-08-23 10:46:23 +1000489process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000490{
Damien Millerad833b32000-08-23 10:46:23 +1000491 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100492 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000493
Damien Miller95def091999-11-25 00:26:21 +1100494 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100495 for (id = TAILQ_FIRST(&tab->idlist); id;
496 id = TAILQ_FIRST(&tab->idlist)) {
497 TAILQ_REMOVE(&tab->idlist, id, next);
498 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100499 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000500
Damien Miller95def091999-11-25 00:26:21 +1100501 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000502 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000503
504 /* Send success. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000505 send_status(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100506}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507
Darren Tucker2812dc92007-03-21 20:45:06 +1100508/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000509static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000510reaper(void)
511{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000512 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000513 Identity *id, *nxt;
514 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000515 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000516
517 for (version = 1; version < 3; version++) {
518 tab = idtab_lookup(version);
519 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
520 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100521 if (id->death == 0)
522 continue;
523 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100524 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000525 TAILQ_REMOVE(&tab->idlist, id, next);
526 free_identity(id);
527 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100528 } else
529 deadline = (deadline == 0) ? id->death :
530 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000531 }
532 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100533 if (deadline == 0 || deadline <= now)
534 return 0;
535 else
536 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000537}
538
markus@openbsd.org139ca812015-01-14 13:09:09 +0000539/*
540 * XXX this and the corresponding serialisation function probably belongs
541 * in key.c
542 */
djm@openbsd.org111dfb22015-03-03 21:21:13 +0000543#ifdef WITH_SSH1
markus@openbsd.org139ca812015-01-14 13:09:09 +0000544static int
545agent_decode_rsa1(struct sshbuf *m, struct sshkey **kp)
546{
547 struct sshkey *k = NULL;
548 int r = SSH_ERR_INTERNAL_ERROR;
549
550 *kp = NULL;
551 if ((k = sshkey_new_private(KEY_RSA1)) == NULL)
552 return SSH_ERR_ALLOC_FAIL;
553
554 if ((r = sshbuf_get_u32(m, NULL)) != 0 || /* ignored */
555 (r = sshbuf_get_bignum1(m, k->rsa->n)) != 0 ||
556 (r = sshbuf_get_bignum1(m, k->rsa->e)) != 0 ||
557 (r = sshbuf_get_bignum1(m, k->rsa->d)) != 0 ||
558 (r = sshbuf_get_bignum1(m, k->rsa->iqmp)) != 0 ||
559 /* SSH1 and SSL have p and q swapped */
560 (r = sshbuf_get_bignum1(m, k->rsa->q)) != 0 || /* p */
561 (r = sshbuf_get_bignum1(m, k->rsa->p)) != 0) /* q */
562 goto out;
563
564 /* Generate additional parameters */
565 if ((r = rsa_generate_additional_parameters(k->rsa)) != 0)
566 goto out;
567 /* enable blinding */
568 if (RSA_blinding_on(k->rsa, NULL) != 1) {
569 r = SSH_ERR_LIBCRYPTO_ERROR;
570 goto out;
571 }
572
573 r = 0; /* success */
574 out:
575 if (r == 0)
576 *kp = k;
577 else
578 sshkey_free(k);
579 return r;
580}
Damien Miller6c203922015-03-03 13:48:48 -0800581#endif /* WITH_SSH1 */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000582
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000583static void
Damien Millerad833b32000-08-23 10:46:23 +1000584process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100585{
Damien Millerad833b32000-08-23 10:46:23 +1000586 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000587 Identity *id;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000588 int success = 0, confirm = 0;
589 u_int seconds;
590 char *comment = NULL;
Darren Tucker55119252013-06-02 07:43:59 +1000591 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000592 struct sshkey *k = NULL;
593 u_char ctype;
594 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100595
Damien Millerad833b32000-08-23 10:46:23 +1000596 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000597#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000598 case 1:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000599 r = agent_decode_rsa1(e->request, &k);
Damien Millerc51d0732003-03-15 11:37:09 +1100600 break;
Damien Miller86687062014-07-02 15:28:02 +1000601#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100602 case 2:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000603 r = sshkey_private_deserialize(e->request, &k);
Damien Millerf0e90602013-12-07 10:40:26 +1100604 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100605 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000606 if (r != 0 || k == NULL ||
607 (r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
608 error("%s: decode private key: %s", __func__, ssh_err(r));
609 goto err;
610 }
Damien Miller86687062014-07-02 15:28:02 +1000611
markus@openbsd.org139ca812015-01-14 13:09:09 +0000612 while (sshbuf_len(e->request)) {
613 if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
614 error("%s: buffer error: %s", __func__, ssh_err(r));
615 goto err;
616 }
617 switch (ctype) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000618 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000619 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
620 error("%s: bad lifetime constraint: %s",
621 __func__, ssh_err(r));
622 goto err;
623 }
624 death = monotime() + seconds;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000625 break;
Damien Miller6c711792003-01-24 11:36:23 +1100626 case SSH_AGENT_CONSTRAIN_CONFIRM:
627 confirm = 1;
628 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000629 default:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000630 error("%s: Unknown constraint %d", __func__, ctype);
631 err:
632 sshbuf_reset(e->request);
Darren Tuckera627d422013-06-02 07:31:17 +1000633 free(comment);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000634 sshkey_free(k);
Damien Miller1cfadab2008-06-30 00:05:21 +1000635 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000636 }
637 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000638
Damien Miller1cfadab2008-06-30 00:05:21 +1000639 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100640 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000641 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000642 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100643 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000644 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000645 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
646 /* Increment the number of identities. */
647 tab->nentries++;
648 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000649 sshkey_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000650 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000651 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000652 id->comment = comment;
653 id->death = death;
654 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000655send:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000656 send_status(e, success);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000657}
658
Ben Lindstrom2f717042002-06-06 21:52:03 +0000659/* XXX todo: encrypt sensitive data with passphrase */
660static void
661process_lock_agent(SocketEntry *e, int lock)
662{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000663 int r, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000664 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000665
markus@openbsd.org139ca812015-01-14 13:09:09 +0000666 if ((r = sshbuf_get_cstring(e->request, &passwd, NULL)) != 0)
667 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Ben Lindstrom2f717042002-06-06 21:52:03 +0000668 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
669 locked = 0;
Damien Millera5103f42014-02-04 11:20:14 +1100670 explicit_bzero(lock_passwd, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000671 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000672 lock_passwd = NULL;
673 success = 1;
674 } else if (!locked && lock) {
675 locked = 1;
676 lock_passwd = xstrdup(passwd);
677 success = 1;
678 }
Damien Millera5103f42014-02-04 11:20:14 +1100679 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000680 free(passwd);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000681 send_status(e, success);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000682}
683
684static void
685no_identities(SocketEntry *e, u_int type)
686{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000687 struct sshbuf *msg;
688 int r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000689
markus@openbsd.org139ca812015-01-14 13:09:09 +0000690 if ((msg = sshbuf_new()) == NULL)
691 fatal("%s: sshbuf_new failed", __func__);
692 if ((r = sshbuf_put_u8(msg,
Ben Lindstrom2f717042002-06-06 21:52:03 +0000693 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
markus@openbsd.org139ca812015-01-14 13:09:09 +0000694 SSH_AGENT_RSA_IDENTITIES_ANSWER :
695 SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
696 (r = sshbuf_put_u32(msg, 0)) != 0 ||
697 (r = sshbuf_put_stringb(e->output, msg)) != 0)
698 fatal("%s: buffer error: %s", __func__, ssh_err(r));
699 sshbuf_free(msg);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000700}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000701
Damien Miller7ea845e2010-02-12 09:21:02 +1100702#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000703static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000704process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000705{
Damien Miller7ea845e2010-02-12 09:21:02 +1100706 char *provider = NULL, *pin;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000707 int r, i, version, count = 0, success = 0, confirm = 0;
708 u_int seconds;
Darren Tucker55119252013-06-02 07:43:59 +1000709 time_t death = 0;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000710 u_char type;
711 struct sshkey **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000712 Identity *id;
713 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100714
markus@openbsd.org139ca812015-01-14 13:09:09 +0000715 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
716 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
717 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd94f20d2003-06-11 22:06:33 +1000718
markus@openbsd.org139ca812015-01-14 13:09:09 +0000719 while (sshbuf_len(e->request)) {
720 if ((r = sshbuf_get_u8(e->request, &type)) != 0)
721 fatal("%s: buffer error: %s", __func__, ssh_err(r));
722 switch (type) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000723 case SSH_AGENT_CONSTRAIN_LIFETIME:
markus@openbsd.org139ca812015-01-14 13:09:09 +0000724 if ((r = sshbuf_get_u32(e->request, &seconds)) != 0)
725 fatal("%s: buffer error: %s",
726 __func__, ssh_err(r));
727 death = monotime() + seconds;
Damien Millerd94f20d2003-06-11 22:06:33 +1000728 break;
729 case SSH_AGENT_CONSTRAIN_CONFIRM:
730 confirm = 1;
731 break;
732 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000733 error("process_add_smartcard_key: "
734 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000735 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000736 }
737 }
738 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000739 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000740
Damien Miller7ea845e2010-02-12 09:21:02 +1100741 count = pkcs11_add_provider(provider, pin, &keys);
742 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000743 k = keys[i];
744 version = k->type == KEY_RSA1 ? 1 : 2;
745 tab = idtab_lookup(version);
746 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100747 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000748 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100749 id->provider = xstrdup(provider);
750 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000751 id->death = death;
752 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000753 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
754 tab->nentries++;
755 success = 1;
756 } else {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000757 sshkey_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000758 }
759 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000760 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000761send:
Darren Tuckera627d422013-06-02 07:31:17 +1000762 free(pin);
763 free(provider);
764 free(keys);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000765 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000766}
767
768static void
769process_remove_smartcard_key(SocketEntry *e)
770{
Damien Miller7ea845e2010-02-12 09:21:02 +1100771 char *provider = NULL, *pin = NULL;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000772 int r, version, success = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100773 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000774 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000775
markus@openbsd.org139ca812015-01-14 13:09:09 +0000776 if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
777 (r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0)
778 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Darren Tuckera627d422013-06-02 07:31:17 +1000779 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000780
Damien Miller7ea845e2010-02-12 09:21:02 +1100781 for (version = 1; version < 3; version++) {
782 tab = idtab_lookup(version);
783 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
784 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100785 /* Skip file--based keys */
786 if (id->provider == NULL)
787 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100788 if (!strcmp(provider, id->provider)) {
789 TAILQ_REMOVE(&tab->idlist, id, next);
790 free_identity(id);
791 tab->nentries--;
792 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000793 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000794 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100795 if (pkcs11_del_provider(provider) == 0)
796 success = 1;
797 else
798 error("process_remove_smartcard_key:"
799 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000800 free(provider);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000801 send_status(e, success);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000802}
Damien Miller7ea845e2010-02-12 09:21:02 +1100803#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000804
Damien Millerad833b32000-08-23 10:46:23 +1000805/* dispatch incoming messages */
806
Ben Lindstrombba81212001-06-25 05:01:22 +0000807static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808process_message(SocketEntry *e)
809{
markus@openbsd.org139ca812015-01-14 13:09:09 +0000810 u_int msg_len;
811 u_char type;
812 const u_char *cp;
813 int r;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000814
markus@openbsd.org139ca812015-01-14 13:09:09 +0000815 if (sshbuf_len(e->input) < 5)
Damien Miller95def091999-11-25 00:26:21 +1100816 return; /* Incomplete message. */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000817 cp = sshbuf_ptr(e->input);
818 msg_len = PEEK_U32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100819 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000820 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100821 return;
822 }
markus@openbsd.org139ca812015-01-14 13:09:09 +0000823 if (sshbuf_len(e->input) < msg_len + 4)
Damien Miller95def091999-11-25 00:26:21 +1100824 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000825
826 /* move the current input to e->request */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000827 sshbuf_reset(e->request);
828 if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
829 (r = sshbuf_get_u8(e->request, &type)) != 0)
830 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831
Ben Lindstrom2f717042002-06-06 21:52:03 +0000832 /* check wheter agent is locked */
833 if (locked && type != SSH_AGENTC_UNLOCK) {
markus@openbsd.org139ca812015-01-14 13:09:09 +0000834 sshbuf_reset(e->request);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000835 switch (type) {
836 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
837 case SSH2_AGENTC_REQUEST_IDENTITIES:
838 /* send empty lists */
839 no_identities(e, type);
840 break;
841 default:
842 /* send a fail message for all other request types */
markus@openbsd.org139ca812015-01-14 13:09:09 +0000843 send_status(e, 0);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000844 }
845 return;
846 }
847
Ben Lindstrom3f471632001-07-04 03:53:15 +0000848 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100849 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000850 case SSH_AGENTC_LOCK:
851 case SSH_AGENTC_UNLOCK:
852 process_lock_agent(e, type == SSH_AGENTC_LOCK);
853 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000854#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000855 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100856 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000857 process_authentication_challenge1(e);
858 break;
859 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
860 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100861 break;
862 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000863 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000864 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100865 break;
866 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000867 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100868 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000869#endif
djm@openbsd.org2f04af92015-03-04 21:12:59 +0000870 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
871 process_remove_all_identities(e, 1); /* safe for !WITH_SSH1 */
872 break;
Damien Millerad833b32000-08-23 10:46:23 +1000873 /* ssh2 */
874 case SSH2_AGENTC_SIGN_REQUEST:
875 process_sign_request2(e);
876 break;
877 case SSH2_AGENTC_REQUEST_IDENTITIES:
878 process_request_identities(e, 2);
879 break;
880 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000881 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000882 process_add_identity(e, 2);
883 break;
884 case SSH2_AGENTC_REMOVE_IDENTITY:
885 process_remove_identity(e, 2);
886 break;
887 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
888 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100889 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100890#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000891 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000892 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000893 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100894 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000895 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
896 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100897 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100898#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100899 default:
900 /* Unknown message. Respond with failure. */
901 error("Unknown message %d", type);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000902 sshbuf_reset(e->request);
903 send_status(e, 0);
Damien Miller95def091999-11-25 00:26:21 +1100904 break;
905 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906}
907
Ben Lindstrombba81212001-06-25 05:01:22 +0000908static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000909new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000910{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000911 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000912
Damien Miller232711f2004-06-15 10:35:30 +1000913 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 if (fd > max_fd)
916 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917
Damien Miller95def091999-11-25 00:26:21 +1100918 for (i = 0; i < sockets_alloc; i++)
919 if (sockets[i].type == AUTH_UNUSED) {
920 sockets[i].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000921 if ((sockets[i].input = sshbuf_new()) == NULL)
922 fatal("%s: sshbuf_new failed", __func__);
923 if ((sockets[i].output = sshbuf_new()) == NULL)
924 fatal("%s: sshbuf_new failed", __func__);
925 if ((sockets[i].request = sshbuf_new()) == NULL)
926 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000927 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100928 return;
929 }
930 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000931 new_alloc = sockets_alloc + 10;
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000932 sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000933 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100934 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000935 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100936 sockets[old_alloc].fd = fd;
markus@openbsd.org139ca812015-01-14 13:09:09 +0000937 if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
938 fatal("%s: sshbuf_new failed", __func__);
939 if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
940 fatal("%s: sshbuf_new failed", __func__);
941 if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
942 fatal("%s: sshbuf_new failed", __func__);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000943 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944}
945
Ben Lindstrombba81212001-06-25 05:01:22 +0000946static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100947prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
948 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949{
Darren Tucker55119252013-06-02 07:43:59 +1000950 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000951 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100952 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000953 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954
955 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100956 switch (sockets[i].type) {
957 case AUTH_SOCKET:
958 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000959 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100960 break;
961 case AUTH_UNUSED:
962 break;
963 default:
964 fatal("Unknown socket type %d", sockets[i].type);
965 break;
966 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000967 }
968
969 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000970 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000971 free(*fdrp);
972 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000973 *fdrp = xmalloc(sz);
974 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000975 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000976 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000977 if (n < *fdl)
978 debug("XXX shrink: %d < %d", n, *fdl);
979 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000980 memset(*fdrp, 0, sz);
981 memset(*fdwp, 0, sz);
982
983 for (i = 0; i < sockets_alloc; i++) {
984 switch (sockets[i].type) {
985 case AUTH_SOCKET:
986 case AUTH_CONNECTION:
987 FD_SET(sockets[i].fd, *fdrp);
markus@openbsd.org139ca812015-01-14 13:09:09 +0000988 if (sshbuf_len(sockets[i].output) > 0)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000989 FD_SET(sockets[i].fd, *fdwp);
990 break;
991 default:
992 break;
993 }
994 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100995 deadline = reaper();
996 if (parent_alive_interval != 0)
997 deadline = (deadline == 0) ? parent_alive_interval :
998 MIN(deadline, parent_alive_interval);
999 if (deadline == 0) {
1000 *tvpp = NULL;
1001 } else {
1002 tv.tv_sec = deadline;
1003 tv.tv_usec = 0;
1004 *tvpp = &tv;
1005 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001006 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007}
1008
Ben Lindstrombba81212001-06-25 05:01:22 +00001009static void
Damien Miller95def091999-11-25 00:26:21 +11001010after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011{
Ben Lindstrom822b6342002-06-23 21:38:49 +00001012 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +11001013 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +11001014 char buf[1024];
markus@openbsd.org139ca812015-01-14 13:09:09 +00001015 int len, sock, r;
Darren Tucker72473c62009-10-07 09:01:03 +11001016 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001017 uid_t euid;
1018 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001019
Darren Tucker72473c62009-10-07 09:01:03 +11001020 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001021 switch (sockets[i].type) {
1022 case AUTH_UNUSED:
1023 break;
1024 case AUTH_SOCKET:
1025 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001026 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001027 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001028 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001029 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001030 error("accept from AUTH_SOCKET: %s",
1031 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001032 break;
1033 }
Damien Miller538f1812002-09-12 09:51:10 +10001034 if (getpeereid(sock, &euid, &egid) < 0) {
1035 error("getpeereid %d failed: %s",
1036 sock, strerror(errno));
1037 close(sock);
1038 break;
1039 }
Damien Milleraf9de382002-10-03 11:54:35 +10001040 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001041 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001042 "peer euid %u != uid %u",
1043 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001044 close(sock);
1045 break;
1046 }
Damien Miller95def091999-11-25 00:26:21 +11001047 new_socket(AUTH_CONNECTION, sock);
1048 }
1049 break;
1050 case AUTH_CONNECTION:
markus@openbsd.org139ca812015-01-14 13:09:09 +00001051 if (sshbuf_len(sockets[i].output) > 0 &&
Damien Miller95def091999-11-25 00:26:21 +11001052 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001053 len = write(sockets[i].fd,
markus@openbsd.org139ca812015-01-14 13:09:09 +00001054 sshbuf_ptr(sockets[i].output),
1055 sshbuf_len(sockets[i].output));
Darren Tucker72473c62009-10-07 09:01:03 +11001056 if (len == -1 && (errno == EAGAIN ||
1057 errno == EWOULDBLOCK ||
1058 errno == EINTR))
1059 continue;
Damien Miller95def091999-11-25 00:26:21 +11001060 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001061 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001062 break;
1063 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001064 if ((r = sshbuf_consume(sockets[i].output,
1065 len)) != 0)
1066 fatal("%s: buffer error: %s",
1067 __func__, ssh_err(r));
Damien Miller95def091999-11-25 00:26:21 +11001068 }
1069 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001070 len = read(sockets[i].fd, buf, sizeof(buf));
1071 if (len == -1 && (errno == EAGAIN ||
1072 errno == EWOULDBLOCK ||
1073 errno == EINTR))
1074 continue;
Damien Miller95def091999-11-25 00:26:21 +11001075 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001076 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001077 break;
1078 }
markus@openbsd.org139ca812015-01-14 13:09:09 +00001079 if ((r = sshbuf_put(sockets[i].input,
1080 buf, len)) != 0)
1081 fatal("%s: buffer error: %s",
1082 __func__, ssh_err(r));
Damien Millerf4977942014-07-30 12:32:46 +10001083 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11001084 process_message(&sockets[i]);
1085 }
1086 break;
1087 default:
1088 fatal("Unknown type %d", sockets[i].type);
1089 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001090}
1091
Ben Lindstrombba81212001-06-25 05:01:22 +00001092static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001093cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001094{
Damien Millerb1e967c2014-07-03 21:22:40 +10001095 if (cleanup_pid != 0 && getpid() != cleanup_pid)
1096 return;
1097 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001098 if (socket_name[0])
1099 unlink(socket_name);
1100 if (socket_dir[0])
1101 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001102}
1103
Darren Tucker3e33cec2003-10-02 16:12:36 +10001104void
Damien Miller792c5111999-10-29 09:47:09 +10001105cleanup_exit(int i)
1106{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001107 cleanup_socket();
1108 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001109}
1110
Damien Miller1c13bd82006-03-26 14:28:14 +11001111/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001113cleanup_handler(int sig)
1114{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001115 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001116#ifdef ENABLE_PKCS11
1117 pkcs11_terminate();
1118#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001119 _exit(2);
1120}
1121
Ben Lindstrombba81212001-06-25 05:01:22 +00001122static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001123check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001124{
Darren Tucker3e78a512011-06-03 14:14:16 +10001125 /*
1126 * If our parent has exited then getppid() will return (pid_t)1,
1127 * so testing for that should be safe.
1128 */
1129 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001130 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001131 cleanup_socket();
1132 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001133 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001134}
1135
1136static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001137usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001138{
Damien Millerf0858de2014-04-20 13:01:30 +10001139 fprintf(stderr,
jmc@openbsd.orgb7ca2762015-04-24 06:26:49 +00001140 "usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001141 " [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001142 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001143 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001144}
1145
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001146int
1147main(int ac, char **av)
1148{
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001149 int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001150 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001151 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001152 char *shell, *format, *pidstr, *agentsocket = NULL;
1153 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001154#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001155 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001156#endif
Damien Miller615f9392000-05-17 22:53:33 +10001157 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001158 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001159 pid_t pid;
1160 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001161 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001162 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001163 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001164
Darren Tuckerce321d82005-10-03 18:11:24 +10001165 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1166 sanitise_stdfd();
1167
Damien Miller6cffb9a2002-09-04 16:20:26 +10001168 /* drop */
1169 setegid(getgid());
1170 setgid(getgid());
1171
Damien Miller6c4914a2004-03-03 11:08:59 +11001172#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1173 /* Disable ptrace on Linux without sgid bit */
1174 prctl(PR_SET_DUMPABLE, 0);
1175#endif
1176
Damien Miller1f0311c2014-05-15 14:24:09 +10001177#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001178 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001179#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001180
Damien Miller59d3d5b2003-08-22 09:34:41 +10001181 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001182 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001183
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001184 while ((ch = getopt(ac, av, "cDdksE:a:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001185 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001186 case 'E':
1187 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1188 if (fingerprint_hash == -1)
1189 fatal("Invalid hash algorithm \"%s\"", optarg);
1190 break;
Damien Miller95def091999-11-25 00:26:21 +11001191 case 'c':
1192 if (s_flag)
1193 usage();
1194 c_flag++;
1195 break;
1196 case 'k':
1197 k_flag++;
1198 break;
1199 case 's':
1200 if (c_flag)
1201 usage();
1202 s_flag++;
1203 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001204 case 'd':
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001205 if (d_flag || D_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001206 usage();
1207 d_flag++;
1208 break;
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001209 case 'D':
1210 if (d_flag || D_flag)
1211 usage();
1212 D_flag++;
1213 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001214 case 'a':
1215 agentsocket = optarg;
1216 break;
Damien Miller53d81482003-01-22 11:47:19 +11001217 case 't':
1218 if ((lifetime = convtime(optarg)) == -1) {
1219 fprintf(stderr, "Invalid lifetime\n");
1220 usage();
1221 }
1222 break;
Damien Miller95def091999-11-25 00:26:21 +11001223 default:
1224 usage();
1225 }
Damien Miller792c5111999-10-29 09:47:09 +10001226 }
Damien Miller95def091999-11-25 00:26:21 +11001227 ac -= optind;
1228 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001229
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001230 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
Damien Miller95def091999-11-25 00:26:21 +11001231 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001232
Ben Lindstromeecdf232002-04-02 21:03:51 +00001233 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001234 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001235 if (shell != NULL && (len = strlen(shell)) > 2 &&
1236 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001237 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001238 }
Damien Miller95def091999-11-25 00:26:21 +11001239 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001240 const char *errstr = NULL;
1241
Damien Miller95def091999-11-25 00:26:21 +11001242 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1243 if (pidstr == NULL) {
1244 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001245 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001246 exit(1);
1247 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001248 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1249 if (errstr) {
1250 fprintf(stderr,
1251 "%s=\"%s\", which is not a good PID: %s\n",
1252 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001253 exit(1);
1254 }
1255 if (kill(pid, SIGTERM) == -1) {
1256 perror("kill");
1257 exit(1);
1258 }
1259 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1260 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1261 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001262 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001263 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001264 }
Damien Miller95def091999-11-25 00:26:21 +11001265 parent_pid = getpid();
1266
Ben Lindstromb7788f32002-06-06 21:46:08 +00001267 if (agentsocket == NULL) {
1268 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001269 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001270 if (mkdtemp(socket_dir) == NULL) {
1271 perror("mkdtemp: private socket dir");
1272 exit(1);
1273 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001274 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1275 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001276 } else {
1277 /* Try to use specified agent socket */
1278 socket_dir[0] = '\0';
1279 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001280 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281
Damien Miller5428f641999-11-25 11:54:57 +11001282 /*
1283 * Create socket early so it will exist before command gets run from
1284 * the parent.
1285 */
Damien Millerab2ec582014-07-18 15:04:47 +10001286 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001287 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001288 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001289 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001290 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001291 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001292 }
Damien Millerab2ec582014-07-18 15:04:47 +10001293 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001294
Damien Miller5428f641999-11-25 11:54:57 +11001295 /*
1296 * Fork, and have the parent execute the command, if any, or present
1297 * the socket data. The child continues as the authentication agent.
1298 */
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001299 if (D_flag || d_flag) {
1300 log_init(__progname,
1301 d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
1302 SYSLOG_FACILITY_AUTH, 1);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001303 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1304 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1305 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001306 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001307 goto skip;
1308 }
Damien Miller95def091999-11-25 00:26:21 +11001309 pid = fork();
1310 if (pid == -1) {
1311 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001312 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001313 }
1314 if (pid != 0) { /* Parent - execute the given command. */
1315 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001316 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001317 if (ac == 0) {
1318 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1319 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001320 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001321 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001322 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001323 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001324 exit(0);
1325 }
Damien Millere4340be2000-09-16 13:29:08 +11001326 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1327 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1328 perror("setenv");
1329 exit(1);
1330 }
Damien Miller95def091999-11-25 00:26:21 +11001331 execvp(av[0], av);
1332 perror(av[0]);
1333 exit(1);
1334 }
Damien Millerc653b892002-02-08 22:05:41 +11001335 /* child */
1336 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001337
1338 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001339 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001340 cleanup_exit(1);
1341 }
1342
1343 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001344 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1345 /* XXX might close listen socket */
1346 (void)dup2(fd, STDIN_FILENO);
1347 (void)dup2(fd, STDOUT_FILENO);
1348 (void)dup2(fd, STDERR_FILENO);
1349 if (fd > 2)
1350 close(fd);
1351 }
Damien Miller95def091999-11-25 00:26:21 +11001352
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001353#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001354 /* deny core dumps, since memory contains unencrypted private keys */
1355 rlim.rlim_cur = rlim.rlim_max = 0;
1356 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001357 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001358 cleanup_exit(1);
1359 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001360#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001361
1362skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001363
Damien Millerb1e967c2014-07-03 21:22:40 +10001364 cleanup_pid = getpid();
1365
Damien Miller7ea845e2010-02-12 09:21:02 +11001366#ifdef ENABLE_PKCS11
1367 pkcs11_init(0);
1368#endif
Damien Miller95def091999-11-25 00:26:21 +11001369 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001370 if (ac > 0)
1371 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001372 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001373 signal(SIGPIPE, SIG_IGN);
djm@openbsd.org2ea97462015-04-24 05:26:44 +00001374 signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001375 signal(SIGHUP, cleanup_handler);
1376 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001377 nalloc = 0;
1378
Damien Miller95def091999-11-25 00:26:21 +11001379 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001380 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1381 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001382 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001383 if (parent_alive_interval != 0)
1384 check_parent_exists();
1385 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001386 if (result < 0) {
1387 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001388 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001389 fatal("select: %s", strerror(saved_errno));
1390 } else if (result > 0)
1391 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001392 }
1393 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001394}