blob: ae2bacca54e7e8f19607061935ef60886d545b8f [file] [log] [blame]
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001/* $OpenBSD: ssh-agent.c,v 1.193 2014/12/21 23:35:14 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
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
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
72#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100074#include "key.h"
75#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110076#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000077#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110078#include "misc.h"
Damien Miller4a1c7aa2014-02-04 11:03:36 +110079#include "digest.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller7ea845e2010-02-12 09:21:02 +110081#ifdef ENABLE_PKCS11
82#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000083#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000084
Damien Miller6c4914a2004-03-03 11:08:59 +110085#if defined(HAVE_SYS_PRCTL_H)
86#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
87#endif
88
Ben Lindstrom65366a82001-12-06 16:32:47 +000089typedef enum {
90 AUTH_UNUSED,
91 AUTH_SOCKET,
92 AUTH_CONNECTION
93} sock_type;
94
Damien Miller95def091999-11-25 00:26:21 +110095typedef struct {
96 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000097 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110098 Buffer input;
99 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000100 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101} SocketEntry;
102
Ben Lindstrom46c16222000-12-22 01:43:59 +0000103u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104SocketEntry *sockets = NULL;
105
Damien Miller1a534ae2002-01-22 23:26:13 +1100106typedef struct identity {
107 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000108 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100109 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100110 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000111 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100112 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113} Identity;
114
Damien Millerad833b32000-08-23 10:46:23 +1000115typedef struct {
116 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100117 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000118} Idtab;
119
120/* private key table, one per protocol version */
121Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123int max_fd = 0;
124
125/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000126pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000127time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Damien Millerb1e967c2014-07-03 21:22:40 +1000129/* pid of process for which cleanup_socket is applicable */
130pid_t cleanup_pid = 0;
131
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100133char socket_name[MAXPATHLEN];
134char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Ben Lindstrom2f717042002-06-06 21:52:03 +0000136/* locking */
137int locked = 0;
138char *lock_passwd = NULL;
139
Damien Miller95def091999-11-25 00:26:21 +1100140extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100141
Darren Tucker55119252013-06-02 07:43:59 +1000142/* Default lifetime in seconds (0 == forever) */
143static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100144
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000145static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
146
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static void
Damien Miller58f34862002-09-04 16:31:21 +1000148close_socket(SocketEntry *e)
149{
Damien Miller58f34862002-09-04 16:31:21 +1000150 close(e->fd);
151 e->fd = -1;
152 e->type = AUTH_UNUSED;
153 buffer_free(&e->input);
154 buffer_free(&e->output);
155 buffer_free(&e->request);
156}
157
158static void
Damien Millerad833b32000-08-23 10:46:23 +1000159idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160{
Damien Millerad833b32000-08-23 10:46:23 +1000161 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000162
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000163 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100164 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000165 idtable[i].nentries = 0;
166 }
167}
168
169/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000170static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000171idtab_lookup(int version)
172{
173 if (version < 1 || version > 2)
174 fatal("internal error, bad protocol version %d", version);
175 return &idtable[version];
176}
177
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000178static void
179free_identity(Identity *id)
180{
181 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000182 free(id->provider);
183 free(id->comment);
184 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000185}
186
Damien Millerad833b32000-08-23 10:46:23 +1000187/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100188static Identity *
189lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000190{
Damien Miller1a534ae2002-01-22 23:26:13 +1100191 Identity *id;
192
Damien Millerad833b32000-08-23 10:46:23 +1000193 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100194 TAILQ_FOREACH(id, &tab->idlist, next) {
195 if (key_equal(key, id->key))
196 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000197 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100198 return (NULL);
199}
200
Damien Miller6c711792003-01-24 11:36:23 +1100201/* Check confirmation of keysign request */
202static int
203confirm_key(Identity *id)
204{
Darren Tuckerce327b62004-11-05 20:38:03 +1100205 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100206 int ret = -1;
207
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000208 p = key_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
Darren Tuckerce327b62004-11-05 20:38:03 +1100209 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
210 id->comment, p))
211 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000212 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100213
Damien Miller6c711792003-01-24 11:36:23 +1100214 return (ret);
215}
216
Damien Millerad833b32000-08-23 10:46:23 +1000217/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static void
Damien Millerad833b32000-08-23 10:46:23 +1000219process_request_identities(SocketEntry *e, int version)
220{
221 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100222 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000223 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224
Damien Miller95def091999-11-25 00:26:21 +1100225 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000226 buffer_put_char(&msg, (version == 1) ?
227 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
228 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100229 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 if (id->key->type == KEY_RSA1) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000231#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000232 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
233 buffer_put_bignum(&msg, id->key->rsa->e);
234 buffer_put_bignum(&msg, id->key->rsa->n);
Damien Miller1f0311c2014-05-15 14:24:09 +1000235#endif
Damien Millerad833b32000-08-23 10:46:23 +1000236 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000237 u_char *blob;
238 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100239 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000240 buffer_put_string(&msg, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000241 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000242 }
243 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100244 }
245 buffer_put_int(&e->output, buffer_len(&msg));
246 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
247 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248}
249
Damien Miller1f0311c2014-05-15 14:24:09 +1000250#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000251/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Millerad833b32000-08-23 10:46:23 +1000253process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000255 u_char buf[32], mdbuf[16], session_id[16];
256 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000257 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000258 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000259 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100260 Buffer msg;
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100261 struct ssh_digest_ctx *md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000262 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Miller95def091999-11-25 00:26:21 +1100264 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100265 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100266 if ((challenge = BN_new()) == NULL)
267 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000268
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000269 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000270 buffer_get_bignum(&e->request, key->rsa->e);
271 buffer_get_bignum(&e->request, key->rsa->n);
272 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Millerad833b32000-08-23 10:46:23 +1000274 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000275 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000276 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000277 buffer_get(&e->request, session_id, 16);
278 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000279 if (response_type != 1)
280 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281
Damien Miller1a534ae2002-01-22 23:26:13 +1100282 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100283 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100284 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000285 /* Decrypt the challenge using the private key. */
Damien Miller86687062014-07-02 15:28:02 +1000286 if (rsa_private_decrypt(challenge, challenge, private->rsa) != 0)
Damien Miller7650bc62001-01-30 09:27:26 +1100287 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100288
Damien Millerad833b32000-08-23 10:46:23 +1000289 /* The response is MD5 of decrypted challenge plus session id. */
290 len = BN_num_bytes(challenge);
291 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000292 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000293 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100294 }
Damien Millerad833b32000-08-23 10:46:23 +1000295 memset(buf, 0, 32);
296 BN_bn2bin(challenge, buf + 32 - len);
Damien Miller4a1c7aa2014-02-04 11:03:36 +1100297 if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
298 ssh_digest_update(md, buf, 32) < 0 ||
299 ssh_digest_update(md, session_id, 16) < 0 ||
300 ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
301 fatal("%s: md5 failed", __func__);
302 ssh_digest_free(md);
Damien Millerad833b32000-08-23 10:46:23 +1000303
304 /* Send the response. */
305 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
306 for (i = 0; i < 16; i++)
307 buffer_put_char(&msg, mdbuf[i]);
308 goto send;
309 }
310
311failure:
312 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100313 buffer_put_char(&msg, SSH_AGENT_FAILURE);
314send:
315 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000316 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
317 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100318 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000319 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320}
Damien Miller1f0311c2014-05-15 14:24:09 +1000321#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322
Damien Millerad833b32000-08-23 10:46:23 +1000323/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000324static void
Damien Millerad833b32000-08-23 10:46:23 +1000325process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000327 u_char *blob, *data, *signature = NULL;
328 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000329 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000330 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000331 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000332 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000333 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000334
335 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000336
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000337 blob = buffer_get_string(&e->request, &blen);
338 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100339
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000340 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000341 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100342 if (flags & SSH_AGENT_OLD_SIGNATURE)
343 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000344
Damien Miller0bc1bd82000-11-13 22:57:25 +1100345 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000346 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100347 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100348 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100349 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000350 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000351 }
Damien Millerad833b32000-08-23 10:46:23 +1000352 buffer_init(&msg);
353 if (ok == 0) {
354 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
355 buffer_put_string(&msg, signature, slen);
356 } else {
357 buffer_put_char(&msg, SSH_AGENT_FAILURE);
358 }
359 buffer_put_int(&e->output, buffer_len(&msg));
360 buffer_append(&e->output, buffer_ptr(&msg),
361 buffer_len(&msg));
362 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000363 free(data);
364 free(blob);
365 free(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000366 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000367}
368
369/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000370static void
Damien Millerad833b32000-08-23 10:46:23 +1000371process_remove_identity(SocketEntry *e, int version)
372{
Damien Miller86687062014-07-02 15:28:02 +1000373 u_int blen;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000374 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100375 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000376 u_char *blob;
Damien Miller86687062014-07-02 15:28:02 +1000377#ifdef WITH_SSH1
378 u_int bits;
379#endif /* WITH_SSH1 */
Damien Miller7e8e8201999-11-16 13:37:16 +1100380
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000381 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000382#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000383 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000385 bits = buffer_get_int(&e->request);
386 buffer_get_bignum(&e->request, key->rsa->e);
387 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388
Damien Millerad833b32000-08-23 10:46:23 +1000389 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000390 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000391 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000392 break;
Damien Miller86687062014-07-02 15:28:02 +1000393#endif /* WITH_SSH1 */
Damien Millerad833b32000-08-23 10:46:23 +1000394 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000395 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 key = key_from_blob(blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000397 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000398 break;
399 }
400 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100401 Identity *id = lookup_identity(key, version);
402 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100403 /*
404 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100405 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000406 * the array, we actually free the key there and move
407 * all the entries between the empty slot and the end
408 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100409 */
Damien Millerad833b32000-08-23 10:46:23 +1000410 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100411 if (tab->nentries < 1)
412 fatal("process_remove_identity: "
413 "internal error: tab->nentries %d",
414 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100415 TAILQ_REMOVE(&tab->idlist, id, next);
416 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000417 tab->nentries--;
418 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100419 }
Damien Millerad833b32000-08-23 10:46:23 +1000420 key_free(key);
421 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000423 buffer_put_char(&e->output,
424 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425}
426
Ben Lindstrombba81212001-06-25 05:01:22 +0000427static void
Damien Millerad833b32000-08-23 10:46:23 +1000428process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429{
Damien Millerad833b32000-08-23 10:46:23 +1000430 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100431 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000432
Damien Miller95def091999-11-25 00:26:21 +1100433 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100434 for (id = TAILQ_FIRST(&tab->idlist); id;
435 id = TAILQ_FIRST(&tab->idlist)) {
436 TAILQ_REMOVE(&tab->idlist, id, next);
437 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100438 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000439
Damien Miller95def091999-11-25 00:26:21 +1100440 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000441 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000442
443 /* Send success. */
444 buffer_put_int(&e->output, 1);
445 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100446}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Darren Tucker2812dc92007-03-21 20:45:06 +1100448/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000449static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000450reaper(void)
451{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000452 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000453 Identity *id, *nxt;
454 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000455 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000456
457 for (version = 1; version < 3; version++) {
458 tab = idtab_lookup(version);
459 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
460 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100461 if (id->death == 0)
462 continue;
463 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100464 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000465 TAILQ_REMOVE(&tab->idlist, id, next);
466 free_identity(id);
467 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100468 } else
469 deadline = (deadline == 0) ? id->death :
470 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000471 }
472 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100473 if (deadline == 0 || deadline <= now)
474 return 0;
475 else
476 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000477}
478
479static void
Damien Millerad833b32000-08-23 10:46:23 +1000480process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100481{
Damien Millerad833b32000-08-23 10:46:23 +1000482 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000483 Identity *id;
Darren Tucker55119252013-06-02 07:43:59 +1000484 int type, success = 0, confirm = 0;
Damien Millerf0e90602013-12-07 10:40:26 +1100485 char *comment;
Darren Tucker55119252013-06-02 07:43:59 +1000486 time_t death = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000487 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100488
Damien Millerad833b32000-08-23 10:46:23 +1000489 switch (version) {
Damien Miller86687062014-07-02 15:28:02 +1000490#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000491 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000493 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000494 buffer_get_bignum(&e->request, k->rsa->n);
495 buffer_get_bignum(&e->request, k->rsa->e);
496 buffer_get_bignum(&e->request, k->rsa->d);
497 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100498
Damien Millerad833b32000-08-23 10:46:23 +1000499 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000500 buffer_get_bignum(&e->request, k->rsa->q); /* p */
501 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100502
Damien Millerad833b32000-08-23 10:46:23 +1000503 /* Generate additional parameters */
Damien Miller86687062014-07-02 15:28:02 +1000504 if (rsa_generate_additional_parameters(k->rsa) != 0)
505 fatal("%s: rsa_generate_additional_parameters "
506 "error", __func__);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100507
Damien Miller5be9d9e2013-12-07 11:24:01 +1100508 /* enable blinding */
Damien Millerc51d0732003-03-15 11:37:09 +1100509 if (RSA_blinding_on(k->rsa, NULL) != 1) {
510 error("process_add_identity: RSA_blinding_on failed");
511 key_free(k);
512 goto send;
513 }
514 break;
Damien Miller86687062014-07-02 15:28:02 +1000515#endif /* WITH_SSH1 */
Damien Millerf0e90602013-12-07 10:40:26 +1100516 case 2:
517 k = key_private_deserialize(&e->request);
518 if (k == NULL) {
519 buffer_clear(&e->request);
520 goto send;
521 }
522 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100523 }
Damien Miller86687062014-07-02 15:28:02 +1000524 if (k == NULL)
Damien Millerad833b32000-08-23 10:46:23 +1000525 goto send;
Damien Miller86687062014-07-02 15:28:02 +1000526 comment = buffer_get_string(&e->request, NULL);
527
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000528 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000529 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000530 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000531 death = monotime() + buffer_get_int(&e->request);
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000532 break;
Damien Miller6c711792003-01-24 11:36:23 +1100533 case SSH_AGENT_CONSTRAIN_CONFIRM:
534 confirm = 1;
535 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000536 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000537 error("process_add_identity: "
538 "Unknown constraint type %d", type);
Darren Tuckera627d422013-06-02 07:31:17 +1000539 free(comment);
Damien Miller1cfadab2008-06-30 00:05:21 +1000540 key_free(k);
541 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000542 }
543 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000544 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100545 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000546 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000547 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100548 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000549 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000550 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
551 /* Increment the number of identities. */
552 tab->nentries++;
553 } else {
554 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000555 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000556 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000557 id->comment = comment;
558 id->death = death;
559 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000560send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000561 buffer_put_int(&e->output, 1);
562 buffer_put_char(&e->output,
563 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
564}
565
Ben Lindstrom2f717042002-06-06 21:52:03 +0000566/* XXX todo: encrypt sensitive data with passphrase */
567static void
568process_lock_agent(SocketEntry *e, int lock)
569{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000570 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000571 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000572
573 passwd = buffer_get_string(&e->request, NULL);
574 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
575 locked = 0;
Damien Millera5103f42014-02-04 11:20:14 +1100576 explicit_bzero(lock_passwd, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000577 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000578 lock_passwd = NULL;
579 success = 1;
580 } else if (!locked && lock) {
581 locked = 1;
582 lock_passwd = xstrdup(passwd);
583 success = 1;
584 }
Damien Millera5103f42014-02-04 11:20:14 +1100585 explicit_bzero(passwd, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000586 free(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000587
Ben Lindstrom2f717042002-06-06 21:52:03 +0000588 buffer_put_int(&e->output, 1);
589 buffer_put_char(&e->output,
590 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000591}
592
593static void
594no_identities(SocketEntry *e, u_int type)
595{
596 Buffer msg;
597
598 buffer_init(&msg);
599 buffer_put_char(&msg,
600 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
601 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
602 buffer_put_int(&msg, 0);
603 buffer_put_int(&e->output, buffer_len(&msg));
604 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
605 buffer_free(&msg);
606}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000607
Damien Miller7ea845e2010-02-12 09:21:02 +1100608#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000609static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000610process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000611{
Damien Miller7ea845e2010-02-12 09:21:02 +1100612 char *provider = NULL, *pin;
Darren Tucker55119252013-06-02 07:43:59 +1000613 int i, type, version, count = 0, success = 0, confirm = 0;
614 time_t death = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100615 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000616 Identity *id;
617 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100618
Damien Miller7ea845e2010-02-12 09:21:02 +1100619 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000620 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000621
622 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000623 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000624 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000625 death = monotime() + buffer_get_int(&e->request);
Damien Millerd94f20d2003-06-11 22:06:33 +1000626 break;
627 case SSH_AGENT_CONSTRAIN_CONFIRM:
628 confirm = 1;
629 break;
630 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000631 error("process_add_smartcard_key: "
632 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000633 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000634 }
635 }
636 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000637 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000638
Damien Miller7ea845e2010-02-12 09:21:02 +1100639 count = pkcs11_add_provider(provider, pin, &keys);
640 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000641 k = keys[i];
642 version = k->type == KEY_RSA1 ? 1 : 2;
643 tab = idtab_lookup(version);
644 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100645 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000646 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100647 id->provider = xstrdup(provider);
648 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000649 id->death = death;
650 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000651 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
652 tab->nentries++;
653 success = 1;
654 } else {
655 key_free(k);
656 }
657 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000659send:
Darren Tuckera627d422013-06-02 07:31:17 +1000660 free(pin);
661 free(provider);
662 free(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000663 buffer_put_int(&e->output, 1);
664 buffer_put_char(&e->output,
665 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
666}
667
668static void
669process_remove_smartcard_key(SocketEntry *e)
670{
Damien Miller7ea845e2010-02-12 09:21:02 +1100671 char *provider = NULL, *pin = NULL;
672 int version, success = 0;
673 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000674 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000675
Damien Miller7ea845e2010-02-12 09:21:02 +1100676 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000677 pin = buffer_get_string(&e->request, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000678 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000679
Damien Miller7ea845e2010-02-12 09:21:02 +1100680 for (version = 1; version < 3; version++) {
681 tab = idtab_lookup(version);
682 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
683 nxt = TAILQ_NEXT(id, next);
Damien Miller0b36c832013-12-29 17:45:51 +1100684 /* Skip file--based keys */
685 if (id->provider == NULL)
686 continue;
Damien Miller7ea845e2010-02-12 09:21:02 +1100687 if (!strcmp(provider, id->provider)) {
688 TAILQ_REMOVE(&tab->idlist, id, next);
689 free_identity(id);
690 tab->nentries--;
691 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000692 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000693 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100694 if (pkcs11_del_provider(provider) == 0)
695 success = 1;
696 else
697 error("process_remove_smartcard_key:"
698 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000699 free(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700 buffer_put_int(&e->output, 1);
701 buffer_put_char(&e->output,
702 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
703}
Damien Miller7ea845e2010-02-12 09:21:02 +1100704#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000705
Damien Millerad833b32000-08-23 10:46:23 +1000706/* dispatch incoming messages */
707
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709process_message(SocketEntry *e)
710{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000711 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000712 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000713
Damien Miller95def091999-11-25 00:26:21 +1100714 if (buffer_len(&e->input) < 5)
715 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100716 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100717 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100718 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000719 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100720 return;
721 }
722 if (buffer_len(&e->input) < msg_len + 4)
723 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000724
725 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100726 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000727 buffer_clear(&e->request);
728 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
729 buffer_consume(&e->input, msg_len);
730 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Ben Lindstrom2f717042002-06-06 21:52:03 +0000732 /* check wheter agent is locked */
733 if (locked && type != SSH_AGENTC_UNLOCK) {
734 buffer_clear(&e->request);
735 switch (type) {
736 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
737 case SSH2_AGENTC_REQUEST_IDENTITIES:
738 /* send empty lists */
739 no_identities(e, type);
740 break;
741 default:
742 /* send a fail message for all other request types */
743 buffer_put_int(&e->output, 1);
744 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
745 }
746 return;
747 }
748
Ben Lindstrom3f471632001-07-04 03:53:15 +0000749 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100750 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000751 case SSH_AGENTC_LOCK:
752 case SSH_AGENTC_UNLOCK:
753 process_lock_agent(e, type == SSH_AGENTC_LOCK);
754 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000755#ifdef WITH_SSH1
Damien Millerad833b32000-08-23 10:46:23 +1000756 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100757 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000758 process_authentication_challenge1(e);
759 break;
760 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
761 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100762 break;
763 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000764 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000765 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100766 break;
767 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000768 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100769 break;
770 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000771 process_remove_all_identities(e, 1);
772 break;
Damien Miller1f0311c2014-05-15 14:24:09 +1000773#endif
Damien Millerad833b32000-08-23 10:46:23 +1000774 /* ssh2 */
775 case SSH2_AGENTC_SIGN_REQUEST:
776 process_sign_request2(e);
777 break;
778 case SSH2_AGENTC_REQUEST_IDENTITIES:
779 process_request_identities(e, 2);
780 break;
781 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000782 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000783 process_add_identity(e, 2);
784 break;
785 case SSH2_AGENTC_REMOVE_IDENTITY:
786 process_remove_identity(e, 2);
787 break;
788 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
789 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100790 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100791#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000792 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000793 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000794 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100795 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000796 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
797 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100798 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100799#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100800 default:
801 /* Unknown message. Respond with failure. */
802 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000803 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100804 buffer_put_int(&e->output, 1);
805 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
806 break;
807 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808}
809
Ben Lindstrombba81212001-06-25 05:01:22 +0000810static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000811new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000814
Damien Miller232711f2004-06-15 10:35:30 +1000815 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816
Damien Miller95def091999-11-25 00:26:21 +1100817 if (fd > max_fd)
818 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819
Damien Miller95def091999-11-25 00:26:21 +1100820 for (i = 0; i < sockets_alloc; i++)
821 if (sockets[i].type == AUTH_UNUSED) {
822 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100823 buffer_init(&sockets[i].input);
824 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000825 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000826 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100827 return;
828 }
829 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000830 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100831 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000832 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100833 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000834 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100835 sockets[old_alloc].fd = fd;
836 buffer_init(&sockets[old_alloc].input);
837 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000838 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000839 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840}
841
Ben Lindstrombba81212001-06-25 05:01:22 +0000842static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100843prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
844 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000845{
Darren Tucker55119252013-06-02 07:43:59 +1000846 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100848 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000849 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850
851 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100852 switch (sockets[i].type) {
853 case AUTH_SOCKET:
854 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000855 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100856 break;
857 case AUTH_UNUSED:
858 break;
859 default:
860 fatal("Unknown socket type %d", sockets[i].type);
861 break;
862 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000863 }
864
865 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000866 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000867 free(*fdrp);
868 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000869 *fdrp = xmalloc(sz);
870 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000871 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000872 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000873 if (n < *fdl)
874 debug("XXX shrink: %d < %d", n, *fdl);
875 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000876 memset(*fdrp, 0, sz);
877 memset(*fdwp, 0, sz);
878
879 for (i = 0; i < sockets_alloc; i++) {
880 switch (sockets[i].type) {
881 case AUTH_SOCKET:
882 case AUTH_CONNECTION:
883 FD_SET(sockets[i].fd, *fdrp);
884 if (buffer_len(&sockets[i].output) > 0)
885 FD_SET(sockets[i].fd, *fdwp);
886 break;
887 default:
888 break;
889 }
890 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100891 deadline = reaper();
892 if (parent_alive_interval != 0)
893 deadline = (deadline == 0) ? parent_alive_interval :
894 MIN(deadline, parent_alive_interval);
895 if (deadline == 0) {
896 *tvpp = NULL;
897 } else {
898 tv.tv_sec = deadline;
899 tv.tv_usec = 0;
900 *tvpp = &tv;
901 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000902 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000903}
904
Ben Lindstrombba81212001-06-25 05:01:22 +0000905static void
Damien Miller95def091999-11-25 00:26:21 +1100906after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000908 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100909 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100910 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000911 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100912 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000913 uid_t euid;
914 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000915
Darren Tucker72473c62009-10-07 09:01:03 +1100916 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100917 switch (sockets[i].type) {
918 case AUTH_UNUSED:
919 break;
920 case AUTH_SOCKET:
921 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100922 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000923 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100924 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100925 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100926 error("accept from AUTH_SOCKET: %s",
927 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100928 break;
929 }
Damien Miller538f1812002-09-12 09:51:10 +1000930 if (getpeereid(sock, &euid, &egid) < 0) {
931 error("getpeereid %d failed: %s",
932 sock, strerror(errno));
933 close(sock);
934 break;
935 }
Damien Milleraf9de382002-10-03 11:54:35 +1000936 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000937 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000938 "peer euid %u != uid %u",
939 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000940 close(sock);
941 break;
942 }
Damien Miller95def091999-11-25 00:26:21 +1100943 new_socket(AUTH_CONNECTION, sock);
944 }
945 break;
946 case AUTH_CONNECTION:
947 if (buffer_len(&sockets[i].output) > 0 &&
948 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100949 len = write(sockets[i].fd,
950 buffer_ptr(&sockets[i].output),
951 buffer_len(&sockets[i].output));
952 if (len == -1 && (errno == EAGAIN ||
953 errno == EWOULDBLOCK ||
954 errno == EINTR))
955 continue;
Damien Miller95def091999-11-25 00:26:21 +1100956 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000957 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100958 break;
959 }
960 buffer_consume(&sockets[i].output, len);
961 }
962 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100963 len = read(sockets[i].fd, buf, sizeof(buf));
964 if (len == -1 && (errno == EAGAIN ||
965 errno == EWOULDBLOCK ||
966 errno == EINTR))
967 continue;
Damien Miller95def091999-11-25 00:26:21 +1100968 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000969 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100970 break;
971 }
972 buffer_append(&sockets[i].input, buf, len);
Damien Millerf4977942014-07-30 12:32:46 +1000973 explicit_bzero(buf, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +1100974 process_message(&sockets[i]);
975 }
976 break;
977 default:
978 fatal("Unknown type %d", sockets[i].type);
979 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980}
981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000983cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000984{
Damien Millerb1e967c2014-07-03 21:22:40 +1000985 if (cleanup_pid != 0 && getpid() != cleanup_pid)
986 return;
987 debug("%s: cleanup", __func__);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000988 if (socket_name[0])
989 unlink(socket_name);
990 if (socket_dir[0])
991 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
Darren Tucker3e33cec2003-10-02 16:12:36 +1000994void
Damien Miller792c5111999-10-29 09:47:09 +1000995cleanup_exit(int i)
996{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000997 cleanup_socket();
998 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000999}
1000
Damien Miller1c13bd82006-03-26 14:28:14 +11001001/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001002static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001003cleanup_handler(int sig)
1004{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001005 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001006#ifdef ENABLE_PKCS11
1007 pkcs11_terminate();
1008#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001009 _exit(2);
1010}
1011
Ben Lindstrombba81212001-06-25 05:01:22 +00001012static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001013check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001014{
Darren Tucker3e78a512011-06-03 14:14:16 +10001015 /*
1016 * If our parent has exited then getppid() will return (pid_t)1,
1017 * so testing for that should be safe.
1018 */
1019 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001020 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001021 cleanup_socket();
1022 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001023 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001024}
1025
1026static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001027usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001028{
Damien Millerf0858de2014-04-20 13:01:30 +10001029 fprintf(stderr,
jmc@openbsd.orga5375cc2014-12-21 23:35:14 +00001030 "usage: ssh-agent [-c | -s] [-d] [-a bind_address] [-E fingerprint_hash]\n"
1031 " [-t life] [command [arg ...]]\n"
Damien Millerf0858de2014-04-20 13:01:30 +10001032 " ssh-agent [-c | -s] -k\n");
Damien Miller95def091999-11-25 00:26:21 +11001033 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001034}
1035
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036int
1037main(int ac, char **av)
1038{
Damien Miller6c711792003-01-24 11:36:23 +11001039 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001040 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001041 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001042 char *shell, *format, *pidstr, *agentsocket = NULL;
1043 fd_set *readsetp = NULL, *writesetp = NULL;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001044#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001045 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001046#endif
Damien Miller615f9392000-05-17 22:53:33 +10001047 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001048 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001049 pid_t pid;
1050 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001051 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001052 size_t len;
Damien Millerab2ec582014-07-18 15:04:47 +10001053 mode_t prev_mask;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001054
Darren Tuckerce321d82005-10-03 18:11:24 +10001055 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1056 sanitise_stdfd();
1057
Damien Miller6cffb9a2002-09-04 16:20:26 +10001058 /* drop */
1059 setegid(getgid());
1060 setgid(getgid());
1061
Damien Miller6c4914a2004-03-03 11:08:59 +11001062#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1063 /* Disable ptrace on Linux without sgid bit */
1064 prctl(PR_SET_DUMPABLE, 0);
1065#endif
1066
Damien Miller1f0311c2014-05-15 14:24:09 +10001067#ifdef WITH_OPENSSL
Damien Miller4314c2b2010-09-10 11:12:09 +10001068 OpenSSL_add_all_algorithms();
Damien Miller1f0311c2014-05-15 14:24:09 +10001069#endif
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001070
Damien Miller59d3d5b2003-08-22 09:34:41 +10001071 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001072 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001073
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001074 while ((ch = getopt(ac, av, "cdksE:a:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001075 switch (ch) {
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001076 case 'E':
1077 fingerprint_hash = ssh_digest_alg_by_name(optarg);
1078 if (fingerprint_hash == -1)
1079 fatal("Invalid hash algorithm \"%s\"", optarg);
1080 break;
Damien Miller95def091999-11-25 00:26:21 +11001081 case 'c':
1082 if (s_flag)
1083 usage();
1084 c_flag++;
1085 break;
1086 case 'k':
1087 k_flag++;
1088 break;
1089 case 's':
1090 if (c_flag)
1091 usage();
1092 s_flag++;
1093 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001094 case 'd':
1095 if (d_flag)
1096 usage();
1097 d_flag++;
1098 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001099 case 'a':
1100 agentsocket = optarg;
1101 break;
Damien Miller53d81482003-01-22 11:47:19 +11001102 case 't':
1103 if ((lifetime = convtime(optarg)) == -1) {
1104 fprintf(stderr, "Invalid lifetime\n");
1105 usage();
1106 }
1107 break;
Damien Miller95def091999-11-25 00:26:21 +11001108 default:
1109 usage();
1110 }
Damien Miller792c5111999-10-29 09:47:09 +10001111 }
Damien Miller95def091999-11-25 00:26:21 +11001112 ac -= optind;
1113 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001114
Ben Lindstromd94580c2001-07-04 03:48:02 +00001115 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001116 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001117
Ben Lindstromeecdf232002-04-02 21:03:51 +00001118 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001119 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001120 if (shell != NULL && (len = strlen(shell)) > 2 &&
1121 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001122 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001123 }
Damien Miller95def091999-11-25 00:26:21 +11001124 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001125 const char *errstr = NULL;
1126
Damien Miller95def091999-11-25 00:26:21 +11001127 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1128 if (pidstr == NULL) {
1129 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001130 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001131 exit(1);
1132 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001133 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1134 if (errstr) {
1135 fprintf(stderr,
1136 "%s=\"%s\", which is not a good PID: %s\n",
1137 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001138 exit(1);
1139 }
1140 if (kill(pid, SIGTERM) == -1) {
1141 perror("kill");
1142 exit(1);
1143 }
1144 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1145 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1146 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001147 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001148 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001149 }
Damien Miller95def091999-11-25 00:26:21 +11001150 parent_pid = getpid();
1151
Ben Lindstromb7788f32002-06-06 21:46:08 +00001152 if (agentsocket == NULL) {
1153 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001154 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001155 if (mkdtemp(socket_dir) == NULL) {
1156 perror("mkdtemp: private socket dir");
1157 exit(1);
1158 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001159 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1160 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001161 } else {
1162 /* Try to use specified agent socket */
1163 socket_dir[0] = '\0';
1164 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001165 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001166
Damien Miller5428f641999-11-25 11:54:57 +11001167 /*
1168 * Create socket early so it will exist before command gets run from
1169 * the parent.
1170 */
Damien Millerab2ec582014-07-18 15:04:47 +10001171 prev_mask = umask(0177);
Damien Miller7acefbb2014-07-18 14:11:24 +10001172 sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
Damien Miller95def091999-11-25 00:26:21 +11001173 if (sock < 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10001174 /* XXX - unix_listener() calls error() not perror() */
Darren Tucker1dee8682004-11-05 20:26:49 +11001175 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001176 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001177 }
Damien Millerab2ec582014-07-18 15:04:47 +10001178 umask(prev_mask);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001179
Damien Miller5428f641999-11-25 11:54:57 +11001180 /*
1181 * Fork, and have the parent execute the command, if any, or present
1182 * the socket data. The child continues as the authentication agent.
1183 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001184 if (d_flag) {
1185 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1186 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1187 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1188 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001189 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001190 goto skip;
1191 }
Damien Miller95def091999-11-25 00:26:21 +11001192 pid = fork();
1193 if (pid == -1) {
1194 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001195 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001196 }
1197 if (pid != 0) { /* Parent - execute the given command. */
1198 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001199 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001200 if (ac == 0) {
1201 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1202 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001203 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001204 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001205 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001206 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001207 exit(0);
1208 }
Damien Millere4340be2000-09-16 13:29:08 +11001209 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1210 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1211 perror("setenv");
1212 exit(1);
1213 }
Damien Miller95def091999-11-25 00:26:21 +11001214 execvp(av[0], av);
1215 perror(av[0]);
1216 exit(1);
1217 }
Damien Millerc653b892002-02-08 22:05:41 +11001218 /* child */
1219 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001220
1221 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001222 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001223 cleanup_exit(1);
1224 }
1225
1226 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001227 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1228 /* XXX might close listen socket */
1229 (void)dup2(fd, STDIN_FILENO);
1230 (void)dup2(fd, STDOUT_FILENO);
1231 (void)dup2(fd, STDERR_FILENO);
1232 if (fd > 2)
1233 close(fd);
1234 }
Damien Miller95def091999-11-25 00:26:21 +11001235
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001236#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001237 /* deny core dumps, since memory contains unencrypted private keys */
1238 rlim.rlim_cur = rlim.rlim_max = 0;
1239 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001240 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001241 cleanup_exit(1);
1242 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001243#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001244
1245skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001246
Damien Millerb1e967c2014-07-03 21:22:40 +10001247 cleanup_pid = getpid();
1248
Damien Miller7ea845e2010-02-12 09:21:02 +11001249#ifdef ENABLE_PKCS11
1250 pkcs11_init(0);
1251#endif
Damien Miller95def091999-11-25 00:26:21 +11001252 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001253 if (ac > 0)
1254 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001255 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001256 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001257 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001258 signal(SIGHUP, cleanup_handler);
1259 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001260 nalloc = 0;
1261
Damien Miller95def091999-11-25 00:26:21 +11001262 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001263 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1264 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001265 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001266 if (parent_alive_interval != 0)
1267 check_parent_exists();
1268 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001269 if (result < 0) {
1270 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001271 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001272 fatal("select: %s", strerror(saved_errno));
1273 } else if (result > 0)
1274 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001275 }
1276 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001277}