blob: c3b11729c04a10959a1406141479a43fbc304370 [file] [log] [blame]
Damien Miller1f0e86f2013-07-20 13:22:49 +10001/* $OpenBSD: ssh-agent.c,v 1.177 2013/07/20 01:50:20 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <openssl/evp.h>
53#include <openssl/md5.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Millere3476ed2006-07-24 14:13:33 +100055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100057#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110058#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110059# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110060#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110061#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100062#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100063#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100064#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100065#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100067#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110068
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "ssh.h"
71#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "key.h"
74#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110075#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110077#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller7ea845e2010-02-12 09:21:02 +110079#ifdef ENABLE_PKCS11
80#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000081#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000082
Damien Miller6c4914a2004-03-03 11:08:59 +110083#if defined(HAVE_SYS_PRCTL_H)
84#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
85#endif
86
Ben Lindstrom65366a82001-12-06 16:32:47 +000087typedef enum {
88 AUTH_UNUSED,
89 AUTH_SOCKET,
90 AUTH_CONNECTION
91} sock_type;
92
Damien Miller95def091999-11-25 00:26:21 +110093typedef struct {
94 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000095 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110096 Buffer input;
97 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000098 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099} SocketEntry;
100
Ben Lindstrom46c16222000-12-22 01:43:59 +0000101u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102SocketEntry *sockets = NULL;
103
Damien Miller1a534ae2002-01-22 23:26:13 +1100104typedef struct identity {
105 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000106 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100107 char *comment;
Damien Miller7ea845e2010-02-12 09:21:02 +1100108 char *provider;
Darren Tucker55119252013-06-02 07:43:59 +1000109 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100110 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111} Identity;
112
Damien Millerad833b32000-08-23 10:46:23 +1000113typedef struct {
114 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100115 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000116} Idtab;
117
118/* private key table, one per protocol version */
119Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121int max_fd = 0;
122
123/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000124pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000125time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
127/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100128char socket_name[MAXPATHLEN];
129char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Ben Lindstrom2f717042002-06-06 21:52:03 +0000131/* locking */
132int locked = 0;
133char *lock_passwd = NULL;
134
Damien Miller95def091999-11-25 00:26:21 +1100135extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100136
Darren Tucker55119252013-06-02 07:43:59 +1000137/* Default lifetime in seconds (0 == forever) */
138static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100139
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static void
Damien Miller58f34862002-09-04 16:31:21 +1000141close_socket(SocketEntry *e)
142{
Damien Miller58f34862002-09-04 16:31:21 +1000143 close(e->fd);
144 e->fd = -1;
145 e->type = AUTH_UNUSED;
146 buffer_free(&e->input);
147 buffer_free(&e->output);
148 buffer_free(&e->request);
149}
150
151static void
Damien Millerad833b32000-08-23 10:46:23 +1000152idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Millerad833b32000-08-23 10:46:23 +1000154 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000155
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000156 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100157 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000158 idtable[i].nentries = 0;
159 }
160}
161
162/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000164idtab_lookup(int version)
165{
166 if (version < 1 || version > 2)
167 fatal("internal error, bad protocol version %d", version);
168 return &idtable[version];
169}
170
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000171static void
172free_identity(Identity *id)
173{
174 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000175 free(id->provider);
176 free(id->comment);
177 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000178}
179
Damien Millerad833b32000-08-23 10:46:23 +1000180/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100181static Identity *
182lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000183{
Damien Miller1a534ae2002-01-22 23:26:13 +1100184 Identity *id;
185
Damien Millerad833b32000-08-23 10:46:23 +1000186 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100187 TAILQ_FOREACH(id, &tab->idlist, next) {
188 if (key_equal(key, id->key))
189 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000190 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100191 return (NULL);
192}
193
Damien Miller6c711792003-01-24 11:36:23 +1100194/* Check confirmation of keysign request */
195static int
196confirm_key(Identity *id)
197{
Darren Tuckerce327b62004-11-05 20:38:03 +1100198 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100199 int ret = -1;
200
201 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100202 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
203 id->comment, p))
204 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000205 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100206
Damien Miller6c711792003-01-24 11:36:23 +1100207 return (ret);
208}
209
Damien Millerad833b32000-08-23 10:46:23 +1000210/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Damien Millerad833b32000-08-23 10:46:23 +1000212process_request_identities(SocketEntry *e, int version)
213{
214 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100215 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000216 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000219 buffer_put_char(&msg, (version == 1) ?
220 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
221 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100222 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100223 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000224 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
225 buffer_put_bignum(&msg, id->key->rsa->e);
226 buffer_put_bignum(&msg, id->key->rsa->n);
227 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000228 u_char *blob;
229 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000231 buffer_put_string(&msg, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000232 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000233 }
234 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100235 }
236 buffer_put_int(&e->output, buffer_len(&msg));
237 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
238 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Millerad833b32000-08-23 10:46:23 +1000241/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000242static void
Damien Millerad833b32000-08-23 10:46:23 +1000243process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000245 u_char buf[32], mdbuf[16], session_id[16];
246 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000247 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000248 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000249 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100250 Buffer msg;
251 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000252 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100255 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100256 if ((challenge = BN_new()) == NULL)
257 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000258
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000259 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000260 buffer_get_bignum(&e->request, key->rsa->e);
261 buffer_get_bignum(&e->request, key->rsa->n);
262 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Millerad833b32000-08-23 10:46:23 +1000264 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000265 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000266 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000267 buffer_get(&e->request, session_id, 16);
268 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000269 if (response_type != 1)
270 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller1a534ae2002-01-22 23:26:13 +1100272 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100273 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100274 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000275 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100276 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
277 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100278
Damien Millerad833b32000-08-23 10:46:23 +1000279 /* The response is MD5 of decrypted challenge plus session id. */
280 len = BN_num_bytes(challenge);
281 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000282 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000283 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100284 }
Damien Millerad833b32000-08-23 10:46:23 +1000285 memset(buf, 0, 32);
286 BN_bn2bin(challenge, buf + 32 - len);
287 MD5_Init(&md);
288 MD5_Update(&md, buf, 32);
289 MD5_Update(&md, session_id, 16);
290 MD5_Final(mdbuf, &md);
291
292 /* Send the response. */
293 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
294 for (i = 0; i < 16; i++)
295 buffer_put_char(&msg, mdbuf[i]);
296 goto send;
297 }
298
299failure:
300 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100301 buffer_put_char(&msg, SSH_AGENT_FAILURE);
302send:
303 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000304 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
305 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100306 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000307 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308}
309
Damien Millerad833b32000-08-23 10:46:23 +1000310/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000311static void
Damien Millerad833b32000-08-23 10:46:23 +1000312process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000314 u_char *blob, *data, *signature = NULL;
315 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000316 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000317 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000318 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000319 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000320 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000321
322 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000323
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000324 blob = buffer_get_string(&e->request, &blen);
325 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100326
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000327 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000328 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100329 if (flags & SSH_AGENT_OLD_SIGNATURE)
330 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000331
Damien Miller0bc1bd82000-11-13 22:57:25 +1100332 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000333 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100334 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100335 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000337 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000338 }
Damien Millerad833b32000-08-23 10:46:23 +1000339 buffer_init(&msg);
340 if (ok == 0) {
341 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
342 buffer_put_string(&msg, signature, slen);
343 } else {
344 buffer_put_char(&msg, SSH_AGENT_FAILURE);
345 }
346 buffer_put_int(&e->output, buffer_len(&msg));
347 buffer_append(&e->output, buffer_ptr(&msg),
348 buffer_len(&msg));
349 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(data);
351 free(blob);
352 free(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000353 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000354}
355
356/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Damien Millerad833b32000-08-23 10:46:23 +1000358process_remove_identity(SocketEntry *e, int version)
359{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000360 u_int blen, bits;
361 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100362 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100364
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000365 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000366 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100367 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000368 bits = buffer_get_int(&e->request);
369 buffer_get_bignum(&e->request, key->rsa->e);
370 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Damien Millerad833b32000-08-23 10:46:23 +1000372 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000373 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000374 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000375 break;
376 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000377 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 key = key_from_blob(blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000380 break;
381 }
382 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100383 Identity *id = lookup_identity(key, version);
384 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100385 /*
386 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100387 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000388 * the array, we actually free the key there and move
389 * all the entries between the empty slot and the end
390 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100391 */
Damien Millerad833b32000-08-23 10:46:23 +1000392 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 if (tab->nentries < 1)
394 fatal("process_remove_identity: "
395 "internal error: tab->nentries %d",
396 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100397 TAILQ_REMOVE(&tab->idlist, id, next);
398 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000399 tab->nentries--;
400 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
Damien Millerad833b32000-08-23 10:46:23 +1000402 key_free(key);
403 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000405 buffer_put_char(&e->output,
406 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407}
408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
Damien Millerad833b32000-08-23 10:46:23 +1000410process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Damien Millerad833b32000-08-23 10:46:23 +1000412 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100413 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100416 for (id = TAILQ_FIRST(&tab->idlist); id;
417 id = TAILQ_FIRST(&tab->idlist)) {
418 TAILQ_REMOVE(&tab->idlist, id, next);
419 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100420 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Miller95def091999-11-25 00:26:21 +1100422 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000423 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
425 /* Send success. */
426 buffer_put_int(&e->output, 1);
427 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100428}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
Darren Tucker2812dc92007-03-21 20:45:06 +1100430/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000431static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000432reaper(void)
433{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000434 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000435 Identity *id, *nxt;
436 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000437 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000438
439 for (version = 1; version < 3; version++) {
440 tab = idtab_lookup(version);
441 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
442 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100443 if (id->death == 0)
444 continue;
445 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100446 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000447 TAILQ_REMOVE(&tab->idlist, id, next);
448 free_identity(id);
449 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100450 } else
451 deadline = (deadline == 0) ? id->death :
452 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000453 }
454 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100455 if (deadline == 0 || deadline <= now)
456 return 0;
457 else
458 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000459}
460
461static void
Damien Millerad833b32000-08-23 10:46:23 +1000462process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100463{
Damien Millerad833b32000-08-23 10:46:23 +1000464 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000465 Identity *id;
Darren Tucker55119252013-06-02 07:43:59 +1000466 int type, success = 0, confirm = 0;
Darren Tucker8ccb7392010-09-10 12:28:24 +1000467 char *type_name, *comment;
Darren Tucker55119252013-06-02 07:43:59 +1000468 time_t death = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000469 Key *k = NULL;
Damien Miller6af914a2010-09-10 11:39:26 +1000470#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000471 BIGNUM *exponent;
472 EC_POINT *q;
Damien Miller195dbaf2010-10-07 22:05:11 +1100473 char *curve;
Damien Miller6af914a2010-09-10 11:39:26 +1000474#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100475 u_char *cert;
476 u_int len;
Damien Miller95def091999-11-25 00:26:21 +1100477
Damien Millerad833b32000-08-23 10:46:23 +1000478 switch (version) {
479 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000481 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000482 buffer_get_bignum(&e->request, k->rsa->n);
483 buffer_get_bignum(&e->request, k->rsa->e);
484 buffer_get_bignum(&e->request, k->rsa->d);
485 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100486
Damien Millerad833b32000-08-23 10:46:23 +1000487 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000488 buffer_get_bignum(&e->request, k->rsa->q); /* p */
489 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100490
Damien Millerad833b32000-08-23 10:46:23 +1000491 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000492 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000493 break;
494 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000495 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000496 type = key_type_from_name(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000497 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498 case KEY_DSA:
499 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000500 buffer_get_bignum2(&e->request, k->dsa->p);
501 buffer_get_bignum2(&e->request, k->dsa->q);
502 buffer_get_bignum2(&e->request, k->dsa->g);
503 buffer_get_bignum2(&e->request, k->dsa->pub_key);
504 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100505 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000506 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100507 case KEY_DSA_CERT:
508 cert = buffer_get_string(&e->request, &len);
509 if ((k = key_from_blob(cert, len)) == NULL)
510 fatal("Certificate parse failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000511 free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100512 key_add_private(k);
513 buffer_get_bignum2(&e->request, k->dsa->priv_key);
514 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000515#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000516 case KEY_ECDSA:
517 k = key_new_private(type);
518 k->ecdsa_nid = key_ecdsa_nid_from_name(type_name);
519 curve = buffer_get_string(&e->request, NULL);
520 if (k->ecdsa_nid != key_curve_name_to_nid(curve))
521 fatal("%s: curve names mismatch", __func__);
Darren Tuckera627d422013-06-02 07:31:17 +1000522 free(curve);
Damien Millereb8b60e2010-08-31 22:41:14 +1000523 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
524 if (k->ecdsa == NULL)
525 fatal("%s: EC_KEY_new_by_curve_name failed",
526 __func__);
527 q = EC_POINT_new(EC_KEY_get0_group(k->ecdsa));
528 if (q == NULL)
529 fatal("%s: BN_new failed", __func__);
530 if ((exponent = BN_new()) == NULL)
531 fatal("%s: BN_new failed", __func__);
532 buffer_get_ecpoint(&e->request,
533 EC_KEY_get0_group(k->ecdsa), q);
534 buffer_get_bignum2(&e->request, exponent);
535 if (EC_KEY_set_public_key(k->ecdsa, q) != 1)
536 fatal("%s: EC_KEY_set_public_key failed",
537 __func__);
538 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
539 fatal("%s: EC_KEY_set_private_key failed",
540 __func__);
541 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
542 EC_KEY_get0_public_key(k->ecdsa)) != 0)
543 fatal("%s: bad ECDSA public key", __func__);
544 if (key_ec_validate_private(k->ecdsa) != 0)
545 fatal("%s: bad ECDSA private key", __func__);
546 BN_clear_free(exponent);
547 EC_POINT_free(q);
548 break;
549 case KEY_ECDSA_CERT:
550 cert = buffer_get_string(&e->request, &len);
551 if ((k = key_from_blob(cert, len)) == NULL)
552 fatal("Certificate parse failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(cert);
Damien Millereb8b60e2010-08-31 22:41:14 +1000554 key_add_private(k);
555 if ((exponent = BN_new()) == NULL)
556 fatal("%s: BN_new failed", __func__);
557 buffer_get_bignum2(&e->request, exponent);
558 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
559 fatal("%s: EC_KEY_set_private_key failed",
560 __func__);
561 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
562 EC_KEY_get0_public_key(k->ecdsa)) != 0 ||
563 key_ec_validate_private(k->ecdsa) != 0)
564 fatal("%s: bad ECDSA key", __func__);
565 BN_clear_free(exponent);
566 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000567#endif /* OPENSSL_HAS_ECC */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100568 case KEY_RSA:
569 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000570 buffer_get_bignum2(&e->request, k->rsa->n);
571 buffer_get_bignum2(&e->request, k->rsa->e);
572 buffer_get_bignum2(&e->request, k->rsa->d);
573 buffer_get_bignum2(&e->request, k->rsa->iqmp);
574 buffer_get_bignum2(&e->request, k->rsa->p);
575 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100576
577 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000578 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100579 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000580 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100581 case KEY_RSA_CERT:
582 cert = buffer_get_string(&e->request, &len);
583 if ((k = key_from_blob(cert, len)) == NULL)
584 fatal("Certificate parse failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000585 free(cert);
Damien Miller0a80ca12010-02-27 07:55:05 +1100586 key_add_private(k);
587 buffer_get_bignum2(&e->request, k->rsa->d);
588 buffer_get_bignum2(&e->request, k->rsa->iqmp);
589 buffer_get_bignum2(&e->request, k->rsa->p);
590 buffer_get_bignum2(&e->request, k->rsa->q);
591 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100592 default:
Darren Tuckera627d422013-06-02 07:31:17 +1000593 free(type_name);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000594 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000595 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100596 }
Darren Tuckera627d422013-06-02 07:31:17 +1000597 free(type_name);
Damien Millerad833b32000-08-23 10:46:23 +1000598 break;
599 }
Damien Millerc51d0732003-03-15 11:37:09 +1100600 /* enable blinding */
601 switch (k->type) {
602 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000603 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100604 case KEY_RSA_CERT:
Damien Millerc51d0732003-03-15 11:37:09 +1100605 case KEY_RSA1:
606 if (RSA_blinding_on(k->rsa, NULL) != 1) {
607 error("process_add_identity: RSA_blinding_on failed");
608 key_free(k);
609 goto send;
610 }
611 break;
612 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000613 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000614 if (k == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000615 free(comment);
Damien Millerad833b32000-08-23 10:46:23 +1000616 goto send;
617 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000618 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000619 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000620 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000621 death = monotime() + buffer_get_int(&e->request);
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000622 break;
Damien Miller6c711792003-01-24 11:36:23 +1100623 case SSH_AGENT_CONSTRAIN_CONFIRM:
624 confirm = 1;
625 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000626 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000627 error("process_add_identity: "
628 "Unknown constraint type %d", type);
Darren Tuckera627d422013-06-02 07:31:17 +1000629 free(comment);
Damien Miller1cfadab2008-06-30 00:05:21 +1000630 key_free(k);
631 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000632 }
633 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000634 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100635 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000636 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000637 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100638 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000639 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000640 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
641 /* Increment the number of identities. */
642 tab->nentries++;
643 } else {
644 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000645 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000646 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000647 id->comment = comment;
648 id->death = death;
649 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000650send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000651 buffer_put_int(&e->output, 1);
652 buffer_put_char(&e->output,
653 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
654}
655
Ben Lindstrom2f717042002-06-06 21:52:03 +0000656/* XXX todo: encrypt sensitive data with passphrase */
657static void
658process_lock_agent(SocketEntry *e, int lock)
659{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000660 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000661 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000662
663 passwd = buffer_get_string(&e->request, NULL);
664 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
665 locked = 0;
666 memset(lock_passwd, 0, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000667 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000668 lock_passwd = NULL;
669 success = 1;
670 } else if (!locked && lock) {
671 locked = 1;
672 lock_passwd = xstrdup(passwd);
673 success = 1;
674 }
675 memset(passwd, 0, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000676 free(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000677
Ben Lindstrom2f717042002-06-06 21:52:03 +0000678 buffer_put_int(&e->output, 1);
679 buffer_put_char(&e->output,
680 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000681}
682
683static void
684no_identities(SocketEntry *e, u_int type)
685{
686 Buffer msg;
687
688 buffer_init(&msg);
689 buffer_put_char(&msg,
690 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
691 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
692 buffer_put_int(&msg, 0);
693 buffer_put_int(&e->output, buffer_len(&msg));
694 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
695 buffer_free(&msg);
696}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000697
Damien Miller7ea845e2010-02-12 09:21:02 +1100698#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000699static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000700process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000701{
Damien Miller7ea845e2010-02-12 09:21:02 +1100702 char *provider = NULL, *pin;
Darren Tucker55119252013-06-02 07:43:59 +1000703 int i, type, version, count = 0, success = 0, confirm = 0;
704 time_t death = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100705 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000706 Identity *id;
707 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100708
Damien Miller7ea845e2010-02-12 09:21:02 +1100709 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000710 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000711
712 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000713 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000714 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000715 death = monotime() + buffer_get_int(&e->request);
Damien Millerd94f20d2003-06-11 22:06:33 +1000716 break;
717 case SSH_AGENT_CONSTRAIN_CONFIRM:
718 confirm = 1;
719 break;
720 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000721 error("process_add_smartcard_key: "
722 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000723 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000724 }
725 }
726 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000727 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000728
Damien Miller7ea845e2010-02-12 09:21:02 +1100729 count = pkcs11_add_provider(provider, pin, &keys);
730 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000731 k = keys[i];
732 version = k->type == KEY_RSA1 ? 1 : 2;
733 tab = idtab_lookup(version);
734 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100735 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000736 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100737 id->provider = xstrdup(provider);
738 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000739 id->death = death;
740 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000741 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
742 tab->nentries++;
743 success = 1;
744 } else {
745 key_free(k);
746 }
747 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000748 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000749send:
Darren Tuckera627d422013-06-02 07:31:17 +1000750 free(pin);
751 free(provider);
752 free(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000753 buffer_put_int(&e->output, 1);
754 buffer_put_char(&e->output,
755 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
756}
757
758static void
759process_remove_smartcard_key(SocketEntry *e)
760{
Damien Miller7ea845e2010-02-12 09:21:02 +1100761 char *provider = NULL, *pin = NULL;
762 int version, success = 0;
763 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000764 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000765
Damien Miller7ea845e2010-02-12 09:21:02 +1100766 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000767 pin = buffer_get_string(&e->request, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000768 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000769
Damien Miller7ea845e2010-02-12 09:21:02 +1100770 for (version = 1; version < 3; version++) {
771 tab = idtab_lookup(version);
772 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
773 nxt = TAILQ_NEXT(id, next);
774 if (!strcmp(provider, id->provider)) {
775 TAILQ_REMOVE(&tab->idlist, id, next);
776 free_identity(id);
777 tab->nentries--;
778 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000779 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000780 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100781 if (pkcs11_del_provider(provider) == 0)
782 success = 1;
783 else
784 error("process_remove_smartcard_key:"
785 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000786 free(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000787 buffer_put_int(&e->output, 1);
788 buffer_put_char(&e->output,
789 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
790}
Damien Miller7ea845e2010-02-12 09:21:02 +1100791#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000792
Damien Millerad833b32000-08-23 10:46:23 +1000793/* dispatch incoming messages */
794
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796process_message(SocketEntry *e)
797{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000798 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000799 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000800
Damien Miller95def091999-11-25 00:26:21 +1100801 if (buffer_len(&e->input) < 5)
802 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100803 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100804 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100805 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000806 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100807 return;
808 }
809 if (buffer_len(&e->input) < msg_len + 4)
810 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000811
812 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100813 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000814 buffer_clear(&e->request);
815 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
816 buffer_consume(&e->input, msg_len);
817 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818
Ben Lindstrom2f717042002-06-06 21:52:03 +0000819 /* check wheter agent is locked */
820 if (locked && type != SSH_AGENTC_UNLOCK) {
821 buffer_clear(&e->request);
822 switch (type) {
823 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
824 case SSH2_AGENTC_REQUEST_IDENTITIES:
825 /* send empty lists */
826 no_identities(e, type);
827 break;
828 default:
829 /* send a fail message for all other request types */
830 buffer_put_int(&e->output, 1);
831 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
832 }
833 return;
834 }
835
Ben Lindstrom3f471632001-07-04 03:53:15 +0000836 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100837 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000838 case SSH_AGENTC_LOCK:
839 case SSH_AGENTC_UNLOCK:
840 process_lock_agent(e, type == SSH_AGENTC_LOCK);
841 break;
Damien Millerad833b32000-08-23 10:46:23 +1000842 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100843 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000844 process_authentication_challenge1(e);
845 break;
846 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
847 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100848 break;
849 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000850 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000851 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100852 break;
853 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000854 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100855 break;
856 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000857 process_remove_all_identities(e, 1);
858 break;
859 /* ssh2 */
860 case SSH2_AGENTC_SIGN_REQUEST:
861 process_sign_request2(e);
862 break;
863 case SSH2_AGENTC_REQUEST_IDENTITIES:
864 process_request_identities(e, 2);
865 break;
866 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000867 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000868 process_add_identity(e, 2);
869 break;
870 case SSH2_AGENTC_REMOVE_IDENTITY:
871 process_remove_identity(e, 2);
872 break;
873 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
874 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100875 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100876#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000877 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000878 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000879 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100880 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000881 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
882 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100883 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100884#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100885 default:
886 /* Unknown message. Respond with failure. */
887 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000888 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100889 buffer_put_int(&e->output, 1);
890 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
891 break;
892 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000893}
894
Ben Lindstrombba81212001-06-25 05:01:22 +0000895static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000896new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000898 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000899
Damien Miller232711f2004-06-15 10:35:30 +1000900 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901
Damien Miller95def091999-11-25 00:26:21 +1100902 if (fd > max_fd)
903 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904
Damien Miller95def091999-11-25 00:26:21 +1100905 for (i = 0; i < sockets_alloc; i++)
906 if (sockets[i].type == AUTH_UNUSED) {
907 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100908 buffer_init(&sockets[i].input);
909 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000910 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000911 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100912 return;
913 }
914 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000915 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100916 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000917 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100918 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000919 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100920 sockets[old_alloc].fd = fd;
921 buffer_init(&sockets[old_alloc].input);
922 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000923 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000924 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925}
926
Ben Lindstrombba81212001-06-25 05:01:22 +0000927static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100928prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
929 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000930{
Darren Tucker55119252013-06-02 07:43:59 +1000931 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000932 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100933 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000934 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000935
936 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100937 switch (sockets[i].type) {
938 case AUTH_SOCKET:
939 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000940 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100941 break;
942 case AUTH_UNUSED:
943 break;
944 default:
945 fatal("Unknown socket type %d", sockets[i].type);
946 break;
947 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000948 }
949
950 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000951 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000952 free(*fdrp);
953 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954 *fdrp = xmalloc(sz);
955 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000956 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000957 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000958 if (n < *fdl)
959 debug("XXX shrink: %d < %d", n, *fdl);
960 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000961 memset(*fdrp, 0, sz);
962 memset(*fdwp, 0, sz);
963
964 for (i = 0; i < sockets_alloc; i++) {
965 switch (sockets[i].type) {
966 case AUTH_SOCKET:
967 case AUTH_CONNECTION:
968 FD_SET(sockets[i].fd, *fdrp);
969 if (buffer_len(&sockets[i].output) > 0)
970 FD_SET(sockets[i].fd, *fdwp);
971 break;
972 default:
973 break;
974 }
975 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100976 deadline = reaper();
977 if (parent_alive_interval != 0)
978 deadline = (deadline == 0) ? parent_alive_interval :
979 MIN(deadline, parent_alive_interval);
980 if (deadline == 0) {
981 *tvpp = NULL;
982 } else {
983 tv.tv_sec = deadline;
984 tv.tv_usec = 0;
985 *tvpp = &tv;
986 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000987 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988}
989
Ben Lindstrombba81212001-06-25 05:01:22 +0000990static void
Damien Miller95def091999-11-25 00:26:21 +1100991after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000993 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100994 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100995 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000996 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100997 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000998 uid_t euid;
999 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001000
Darren Tucker72473c62009-10-07 09:01:03 +11001001 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001002 switch (sockets[i].type) {
1003 case AUTH_UNUSED:
1004 break;
1005 case AUTH_SOCKET:
1006 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001007 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001008 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001009 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001010 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001011 error("accept from AUTH_SOCKET: %s",
1012 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001013 break;
1014 }
Damien Miller538f1812002-09-12 09:51:10 +10001015 if (getpeereid(sock, &euid, &egid) < 0) {
1016 error("getpeereid %d failed: %s",
1017 sock, strerror(errno));
1018 close(sock);
1019 break;
1020 }
Damien Milleraf9de382002-10-03 11:54:35 +10001021 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001022 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001023 "peer euid %u != uid %u",
1024 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001025 close(sock);
1026 break;
1027 }
Damien Miller95def091999-11-25 00:26:21 +11001028 new_socket(AUTH_CONNECTION, sock);
1029 }
1030 break;
1031 case AUTH_CONNECTION:
1032 if (buffer_len(&sockets[i].output) > 0 &&
1033 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001034 len = write(sockets[i].fd,
1035 buffer_ptr(&sockets[i].output),
1036 buffer_len(&sockets[i].output));
1037 if (len == -1 && (errno == EAGAIN ||
1038 errno == EWOULDBLOCK ||
1039 errno == EINTR))
1040 continue;
Damien Miller95def091999-11-25 00:26:21 +11001041 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001042 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001043 break;
1044 }
1045 buffer_consume(&sockets[i].output, len);
1046 }
1047 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001048 len = read(sockets[i].fd, buf, sizeof(buf));
1049 if (len == -1 && (errno == EAGAIN ||
1050 errno == EWOULDBLOCK ||
1051 errno == EINTR))
1052 continue;
Damien Miller95def091999-11-25 00:26:21 +11001053 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001054 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001055 break;
1056 }
1057 buffer_append(&sockets[i].input, buf, len);
1058 process_message(&sockets[i]);
1059 }
1060 break;
1061 default:
1062 fatal("Unknown type %d", sockets[i].type);
1063 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001064}
1065
Ben Lindstrombba81212001-06-25 05:01:22 +00001066static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001067cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001068{
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001069 if (socket_name[0])
1070 unlink(socket_name);
1071 if (socket_dir[0])
1072 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073}
1074
Darren Tucker3e33cec2003-10-02 16:12:36 +10001075void
Damien Miller792c5111999-10-29 09:47:09 +10001076cleanup_exit(int i)
1077{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001078 cleanup_socket();
1079 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001080}
1081
Damien Miller1c13bd82006-03-26 14:28:14 +11001082/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001083static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001084cleanup_handler(int sig)
1085{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001086 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001087#ifdef ENABLE_PKCS11
1088 pkcs11_terminate();
1089#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001090 _exit(2);
1091}
1092
Ben Lindstrombba81212001-06-25 05:01:22 +00001093static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001094check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001095{
Darren Tucker3e78a512011-06-03 14:14:16 +10001096 /*
1097 * If our parent has exited then getppid() will return (pid_t)1,
1098 * so testing for that should be safe.
1099 */
1100 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +00001101 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001102 cleanup_socket();
1103 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001104 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001105}
1106
1107static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001108usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001109{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001110 fprintf(stderr, "usage: %s [options] [command [arg ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001111 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001112 fprintf(stderr, "Options:\n");
1113 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1114 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1115 fprintf(stderr, " -k Kill the current agent.\n");
1116 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001117 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001118 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001119 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001120}
1121
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001122int
1123main(int ac, char **av)
1124{
Damien Miller6c711792003-01-24 11:36:23 +11001125 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001126 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001127 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001128 char *shell, *format, *pidstr, *agentsocket = NULL;
1129 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001130 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001131#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001132 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001133#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001134 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001135 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001136 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001137 pid_t pid;
1138 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001139 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001140 size_t len;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001141
Darren Tuckerce321d82005-10-03 18:11:24 +10001142 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1143 sanitise_stdfd();
1144
Damien Miller6cffb9a2002-09-04 16:20:26 +10001145 /* drop */
1146 setegid(getgid());
1147 setgid(getgid());
1148
Damien Miller6c4914a2004-03-03 11:08:59 +11001149#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1150 /* Disable ptrace on Linux without sgid bit */
1151 prctl(PR_SET_DUMPABLE, 0);
1152#endif
1153
Damien Miller4314c2b2010-09-10 11:12:09 +10001154 OpenSSL_add_all_algorithms();
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001155
Damien Miller59d3d5b2003-08-22 09:34:41 +10001156 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001157 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001158
Damien Miller53d81482003-01-22 11:47:19 +11001159 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001160 switch (ch) {
1161 case 'c':
1162 if (s_flag)
1163 usage();
1164 c_flag++;
1165 break;
1166 case 'k':
1167 k_flag++;
1168 break;
1169 case 's':
1170 if (c_flag)
1171 usage();
1172 s_flag++;
1173 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001174 case 'd':
1175 if (d_flag)
1176 usage();
1177 d_flag++;
1178 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001179 case 'a':
1180 agentsocket = optarg;
1181 break;
Damien Miller53d81482003-01-22 11:47:19 +11001182 case 't':
1183 if ((lifetime = convtime(optarg)) == -1) {
1184 fprintf(stderr, "Invalid lifetime\n");
1185 usage();
1186 }
1187 break;
Damien Miller95def091999-11-25 00:26:21 +11001188 default:
1189 usage();
1190 }
Damien Miller792c5111999-10-29 09:47:09 +10001191 }
Damien Miller95def091999-11-25 00:26:21 +11001192 ac -= optind;
1193 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001194
Ben Lindstromd94580c2001-07-04 03:48:02 +00001195 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001196 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001197
Ben Lindstromeecdf232002-04-02 21:03:51 +00001198 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001199 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001200 if (shell != NULL && (len = strlen(shell)) > 2 &&
1201 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001202 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001203 }
Damien Miller95def091999-11-25 00:26:21 +11001204 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001205 const char *errstr = NULL;
1206
Damien Miller95def091999-11-25 00:26:21 +11001207 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1208 if (pidstr == NULL) {
1209 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001210 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001211 exit(1);
1212 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001213 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1214 if (errstr) {
1215 fprintf(stderr,
1216 "%s=\"%s\", which is not a good PID: %s\n",
1217 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001218 exit(1);
1219 }
1220 if (kill(pid, SIGTERM) == -1) {
1221 perror("kill");
1222 exit(1);
1223 }
1224 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1225 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1226 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001227 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001228 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001229 }
Damien Miller95def091999-11-25 00:26:21 +11001230 parent_pid = getpid();
1231
Ben Lindstromb7788f32002-06-06 21:46:08 +00001232 if (agentsocket == NULL) {
1233 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001234 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001235 if (mkdtemp(socket_dir) == NULL) {
1236 perror("mkdtemp: private socket dir");
1237 exit(1);
1238 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001239 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1240 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001241 } else {
1242 /* Try to use specified agent socket */
1243 socket_dir[0] = '\0';
1244 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001245 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246
Damien Miller5428f641999-11-25 11:54:57 +11001247 /*
1248 * Create socket early so it will exist before command gets run from
1249 * the parent.
1250 */
Damien Miller95def091999-11-25 00:26:21 +11001251 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1252 if (sock < 0) {
1253 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001254 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001255 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001256 }
Damien Miller95def091999-11-25 00:26:21 +11001257 memset(&sunaddr, 0, sizeof(sunaddr));
1258 sunaddr.sun_family = AF_UNIX;
1259 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001260 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001261 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001262 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001263 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001264 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001265 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001267 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001268 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001269 perror("listen");
1270 cleanup_exit(1);
1271 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001272
Damien Miller5428f641999-11-25 11:54:57 +11001273 /*
1274 * Fork, and have the parent execute the command, if any, or present
1275 * the socket data. The child continues as the authentication agent.
1276 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001277 if (d_flag) {
1278 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1279 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1280 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1281 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001282 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001283 goto skip;
1284 }
Damien Miller95def091999-11-25 00:26:21 +11001285 pid = fork();
1286 if (pid == -1) {
1287 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001288 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001289 }
1290 if (pid != 0) { /* Parent - execute the given command. */
1291 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001292 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001293 if (ac == 0) {
1294 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1295 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001296 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001297 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001298 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001299 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001300 exit(0);
1301 }
Damien Millere4340be2000-09-16 13:29:08 +11001302 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1303 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1304 perror("setenv");
1305 exit(1);
1306 }
Damien Miller95def091999-11-25 00:26:21 +11001307 execvp(av[0], av);
1308 perror(av[0]);
1309 exit(1);
1310 }
Damien Millerc653b892002-02-08 22:05:41 +11001311 /* child */
1312 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001313
1314 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001315 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001316 cleanup_exit(1);
1317 }
1318
1319 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001320 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1321 /* XXX might close listen socket */
1322 (void)dup2(fd, STDIN_FILENO);
1323 (void)dup2(fd, STDOUT_FILENO);
1324 (void)dup2(fd, STDERR_FILENO);
1325 if (fd > 2)
1326 close(fd);
1327 }
Damien Miller95def091999-11-25 00:26:21 +11001328
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001329#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001330 /* deny core dumps, since memory contains unencrypted private keys */
1331 rlim.rlim_cur = rlim.rlim_max = 0;
1332 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001333 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001334 cleanup_exit(1);
1335 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001336#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001337
1338skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001339
1340#ifdef ENABLE_PKCS11
1341 pkcs11_init(0);
1342#endif
Damien Miller95def091999-11-25 00:26:21 +11001343 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001344 if (ac > 0)
1345 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001346 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001347 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001348 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001349 signal(SIGHUP, cleanup_handler);
1350 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001351 nalloc = 0;
1352
Damien Miller95def091999-11-25 00:26:21 +11001353 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001354 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1355 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001356 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001357 if (parent_alive_interval != 0)
1358 check_parent_exists();
1359 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001360 if (result < 0) {
1361 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001362 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001363 fatal("select: %s", strerror(saved_errno));
1364 } else if (result > 0)
1365 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001366 }
1367 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001368}