blob: afba413d7827366fc15f2f83e3afe1bfb90bacd2 [file] [log] [blame]
Damien Miller2cd62932010-12-01 11:50:35 +11001/* $OpenBSD: ssh-agent.c,v 1.171 2010/11/21 01:01:13 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;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000109 u_int 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 Tucker2812dc92007-03-21 20:45:06 +1100125u_int 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
Damien Miller53d81482003-01-22 11:47:19 +1100137/* Default lifetime (0 == forever) */
138static int lifetime = 0;
139
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);
Damien Millera183c6e2010-02-12 09:22:31 +1100175 if (id->provider != NULL)
176 xfree(id->provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000177 xfree(id->comment);
178 xfree(id);
179}
180
Damien Millerad833b32000-08-23 10:46:23 +1000181/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100182static Identity *
183lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000184{
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 Identity *id;
186
Damien Millerad833b32000-08-23 10:46:23 +1000187 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100188 TAILQ_FOREACH(id, &tab->idlist, next) {
189 if (key_equal(key, id->key))
190 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000191 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100192 return (NULL);
193}
194
Damien Miller6c711792003-01-24 11:36:23 +1100195/* Check confirmation of keysign request */
196static int
197confirm_key(Identity *id)
198{
Darren Tuckerce327b62004-11-05 20:38:03 +1100199 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100200 int ret = -1;
201
202 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100203 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
204 id->comment, p))
205 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100206 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100207
Damien Miller6c711792003-01-24 11:36:23 +1100208 return (ret);
209}
210
Damien Millerad833b32000-08-23 10:46:23 +1000211/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000212static void
Damien Millerad833b32000-08-23 10:46:23 +1000213process_request_identities(SocketEntry *e, int version)
214{
215 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100216 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000217 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000220 buffer_put_char(&msg, (version == 1) ?
221 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
222 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100223 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100224 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000225 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
226 buffer_put_bignum(&msg, id->key->rsa->e);
227 buffer_put_bignum(&msg, id->key->rsa->n);
228 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000229 u_char *blob;
230 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100231 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000232 buffer_put_string(&msg, blob, blen);
233 xfree(blob);
234 }
235 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100236 }
237 buffer_put_int(&e->output, buffer_len(&msg));
238 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
239 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Millerad833b32000-08-23 10:46:23 +1000242/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000243static void
Damien Millerad833b32000-08-23 10:46:23 +1000244process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000246 u_char buf[32], mdbuf[16], session_id[16];
247 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000248 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000249 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000250 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100251 Buffer msg;
252 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000253 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller95def091999-11-25 00:26:21 +1100255 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100256 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100257 if ((challenge = BN_new()) == NULL)
258 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000259
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000260 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000261 buffer_get_bignum(&e->request, key->rsa->e);
262 buffer_get_bignum(&e->request, key->rsa->n);
263 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Millerad833b32000-08-23 10:46:23 +1000265 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000266 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000267 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000268 buffer_get(&e->request, session_id, 16);
269 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000270 if (response_type != 1)
271 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller1a534ae2002-01-22 23:26:13 +1100273 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100274 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100275 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000276 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100277 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
278 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100279
Damien Millerad833b32000-08-23 10:46:23 +1000280 /* The response is MD5 of decrypted challenge plus session id. */
281 len = BN_num_bytes(challenge);
282 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000283 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000284 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100285 }
Damien Millerad833b32000-08-23 10:46:23 +1000286 memset(buf, 0, 32);
287 BN_bn2bin(challenge, buf + 32 - len);
288 MD5_Init(&md);
289 MD5_Update(&md, buf, 32);
290 MD5_Update(&md, session_id, 16);
291 MD5_Final(mdbuf, &md);
292
293 /* Send the response. */
294 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
295 for (i = 0; i < 16; i++)
296 buffer_put_char(&msg, mdbuf[i]);
297 goto send;
298 }
299
300failure:
301 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100302 buffer_put_char(&msg, SSH_AGENT_FAILURE);
303send:
304 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000305 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
306 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100307 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000308 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309}
310
Damien Millerad833b32000-08-23 10:46:23 +1000311/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000312static void
Damien Millerad833b32000-08-23 10:46:23 +1000313process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000315 u_char *blob, *data, *signature = NULL;
316 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000317 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000318 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000319 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000320 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000321 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000322
323 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000324
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000325 blob = buffer_get_string(&e->request, &blen);
326 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100327
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000328 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000329 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100330 if (flags & SSH_AGENT_OLD_SIGNATURE)
331 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000332
Damien Miller0bc1bd82000-11-13 22:57:25 +1100333 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000334 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100335 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100336 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100337 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000338 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000339 }
Damien Millerad833b32000-08-23 10:46:23 +1000340 buffer_init(&msg);
341 if (ok == 0) {
342 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
343 buffer_put_string(&msg, signature, slen);
344 } else {
345 buffer_put_char(&msg, SSH_AGENT_FAILURE);
346 }
347 buffer_put_int(&e->output, buffer_len(&msg));
348 buffer_append(&e->output, buffer_ptr(&msg),
349 buffer_len(&msg));
350 buffer_free(&msg);
351 xfree(data);
352 xfree(blob);
353 if (signature != NULL)
354 xfree(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000355 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000356}
357
358/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000359static void
Damien Millerad833b32000-08-23 10:46:23 +1000360process_remove_identity(SocketEntry *e, int version)
361{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000362 u_int blen, bits;
363 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100364 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000365 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100366
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000367 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000368 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000370 bits = buffer_get_int(&e->request);
371 buffer_get_bignum(&e->request, key->rsa->e);
372 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373
Damien Millerad833b32000-08-23 10:46:23 +1000374 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000375 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000376 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000377 break;
378 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000379 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000381 xfree(blob);
382 break;
383 }
384 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100385 Identity *id = lookup_identity(key, version);
386 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100387 /*
388 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100389 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000390 * the array, we actually free the key there and move
391 * all the entries between the empty slot and the end
392 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100393 */
Damien Millerad833b32000-08-23 10:46:23 +1000394 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 if (tab->nentries < 1)
396 fatal("process_remove_identity: "
397 "internal error: tab->nentries %d",
398 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100399 TAILQ_REMOVE(&tab->idlist, id, next);
400 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000401 tab->nentries--;
402 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100403 }
Damien Millerad833b32000-08-23 10:46:23 +1000404 key_free(key);
405 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000407 buffer_put_char(&e->output,
408 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409}
410
Ben Lindstrombba81212001-06-25 05:01:22 +0000411static void
Damien Millerad833b32000-08-23 10:46:23 +1000412process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413{
Damien Millerad833b32000-08-23 10:46:23 +1000414 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100415 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller95def091999-11-25 00:26:21 +1100417 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100418 for (id = TAILQ_FIRST(&tab->idlist); id;
419 id = TAILQ_FIRST(&tab->idlist)) {
420 TAILQ_REMOVE(&tab->idlist, id, next);
421 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100422 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Miller95def091999-11-25 00:26:21 +1100424 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000425 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000426
427 /* Send success. */
428 buffer_put_int(&e->output, 1);
429 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100430}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000431
Darren Tucker2812dc92007-03-21 20:45:06 +1100432/* removes expired keys and returns number of seconds until the next expiry */
433static u_int
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000434reaper(void)
435{
Darren Tucker2812dc92007-03-21 20:45:06 +1100436 u_int deadline = 0, now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000437 Identity *id, *nxt;
438 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000439 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000440
441 for (version = 1; version < 3; version++) {
442 tab = idtab_lookup(version);
443 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
444 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100445 if (id->death == 0)
446 continue;
447 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100448 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000449 TAILQ_REMOVE(&tab->idlist, id, next);
450 free_identity(id);
451 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100452 } else
453 deadline = (deadline == 0) ? id->death :
454 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000455 }
456 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100457 if (deadline == 0 || deadline <= now)
458 return 0;
459 else
460 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000461}
462
463static void
Damien Millerad833b32000-08-23 10:46:23 +1000464process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100465{
Damien Millerad833b32000-08-23 10:46:23 +1000466 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000467 Identity *id;
Damien Miller6c711792003-01-24 11:36:23 +1100468 int type, success = 0, death = 0, confirm = 0;
Darren Tucker8ccb7392010-09-10 12:28:24 +1000469 char *type_name, *comment;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000470 Key *k = NULL;
Damien Miller6af914a2010-09-10 11:39:26 +1000471#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000472 BIGNUM *exponent;
473 EC_POINT *q;
Damien Miller195dbaf2010-10-07 22:05:11 +1100474 char *curve;
Damien Miller6af914a2010-09-10 11:39:26 +1000475#endif
Damien Miller0a80ca12010-02-27 07:55:05 +1100476 u_char *cert;
477 u_int len;
Damien Miller95def091999-11-25 00:26:21 +1100478
Damien Millerad833b32000-08-23 10:46:23 +1000479 switch (version) {
480 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000482 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000483 buffer_get_bignum(&e->request, k->rsa->n);
484 buffer_get_bignum(&e->request, k->rsa->e);
485 buffer_get_bignum(&e->request, k->rsa->d);
486 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100487
Damien Millerad833b32000-08-23 10:46:23 +1000488 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000489 buffer_get_bignum(&e->request, k->rsa->q); /* p */
490 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100491
Damien Millerad833b32000-08-23 10:46:23 +1000492 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000493 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000494 break;
495 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000496 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000497 type = key_type_from_name(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000498 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 case KEY_DSA:
500 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000501 buffer_get_bignum2(&e->request, k->dsa->p);
502 buffer_get_bignum2(&e->request, k->dsa->q);
503 buffer_get_bignum2(&e->request, k->dsa->g);
504 buffer_get_bignum2(&e->request, k->dsa->pub_key);
505 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100506 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000507 case KEY_DSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100508 case KEY_DSA_CERT:
509 cert = buffer_get_string(&e->request, &len);
510 if ((k = key_from_blob(cert, len)) == NULL)
511 fatal("Certificate parse failed");
512 xfree(cert);
513 key_add_private(k);
514 buffer_get_bignum2(&e->request, k->dsa->priv_key);
515 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000516#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000517 case KEY_ECDSA:
518 k = key_new_private(type);
519 k->ecdsa_nid = key_ecdsa_nid_from_name(type_name);
520 curve = buffer_get_string(&e->request, NULL);
521 if (k->ecdsa_nid != key_curve_name_to_nid(curve))
522 fatal("%s: curve names mismatch", __func__);
523 xfree(curve);
524 k->ecdsa = EC_KEY_new_by_curve_name(k->ecdsa_nid);
525 if (k->ecdsa == NULL)
526 fatal("%s: EC_KEY_new_by_curve_name failed",
527 __func__);
528 q = EC_POINT_new(EC_KEY_get0_group(k->ecdsa));
529 if (q == NULL)
530 fatal("%s: BN_new failed", __func__);
531 if ((exponent = BN_new()) == NULL)
532 fatal("%s: BN_new failed", __func__);
533 buffer_get_ecpoint(&e->request,
534 EC_KEY_get0_group(k->ecdsa), q);
535 buffer_get_bignum2(&e->request, exponent);
536 if (EC_KEY_set_public_key(k->ecdsa, q) != 1)
537 fatal("%s: EC_KEY_set_public_key failed",
538 __func__);
539 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
540 fatal("%s: EC_KEY_set_private_key failed",
541 __func__);
542 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
543 EC_KEY_get0_public_key(k->ecdsa)) != 0)
544 fatal("%s: bad ECDSA public key", __func__);
545 if (key_ec_validate_private(k->ecdsa) != 0)
546 fatal("%s: bad ECDSA private key", __func__);
547 BN_clear_free(exponent);
548 EC_POINT_free(q);
549 break;
550 case KEY_ECDSA_CERT:
551 cert = buffer_get_string(&e->request, &len);
552 if ((k = key_from_blob(cert, len)) == NULL)
553 fatal("Certificate parse failed");
554 xfree(cert);
555 key_add_private(k);
556 if ((exponent = BN_new()) == NULL)
557 fatal("%s: BN_new failed", __func__);
558 buffer_get_bignum2(&e->request, exponent);
559 if (EC_KEY_set_private_key(k->ecdsa, exponent) != 1)
560 fatal("%s: EC_KEY_set_private_key failed",
561 __func__);
562 if (key_ec_validate_public(EC_KEY_get0_group(k->ecdsa),
563 EC_KEY_get0_public_key(k->ecdsa)) != 0 ||
564 key_ec_validate_private(k->ecdsa) != 0)
565 fatal("%s: bad ECDSA key", __func__);
566 BN_clear_free(exponent);
567 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000568#endif /* OPENSSL_HAS_ECC */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569 case KEY_RSA:
570 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000571 buffer_get_bignum2(&e->request, k->rsa->n);
572 buffer_get_bignum2(&e->request, k->rsa->e);
573 buffer_get_bignum2(&e->request, k->rsa->d);
574 buffer_get_bignum2(&e->request, k->rsa->iqmp);
575 buffer_get_bignum2(&e->request, k->rsa->p);
576 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100577
578 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000579 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100580 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000581 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100582 case KEY_RSA_CERT:
583 cert = buffer_get_string(&e->request, &len);
584 if ((k = key_from_blob(cert, len)) == NULL)
585 fatal("Certificate parse failed");
586 xfree(cert);
587 key_add_private(k);
588 buffer_get_bignum2(&e->request, k->rsa->d);
589 buffer_get_bignum2(&e->request, k->rsa->iqmp);
590 buffer_get_bignum2(&e->request, k->rsa->p);
591 buffer_get_bignum2(&e->request, k->rsa->q);
592 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100593 default:
Damien Millereb8b60e2010-08-31 22:41:14 +1000594 xfree(type_name);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000595 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000596 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100597 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000598 xfree(type_name);
Damien Millerad833b32000-08-23 10:46:23 +1000599 break;
600 }
Damien Millerc51d0732003-03-15 11:37:09 +1100601 /* enable blinding */
602 switch (k->type) {
603 case KEY_RSA:
Damien Miller4e270b02010-04-16 15:56:21 +1000604 case KEY_RSA_CERT_V00:
Damien Miller0a80ca12010-02-27 07:55:05 +1100605 case KEY_RSA_CERT:
Damien Millerc51d0732003-03-15 11:37:09 +1100606 case KEY_RSA1:
607 if (RSA_blinding_on(k->rsa, NULL) != 1) {
608 error("process_add_identity: RSA_blinding_on failed");
609 key_free(k);
610 goto send;
611 }
612 break;
613 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000614 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000615 if (k == NULL) {
616 xfree(comment);
617 goto send;
618 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000619 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000620 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000621 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000622 death = time(NULL) + buffer_get_int(&e->request);
623 break;
Damien Miller6c711792003-01-24 11:36:23 +1100624 case SSH_AGENT_CONSTRAIN_CONFIRM:
625 confirm = 1;
626 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000627 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000628 error("process_add_identity: "
629 "Unknown constraint type %d", type);
630 xfree(comment);
631 key_free(k);
632 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000633 }
634 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000635 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100636 if (lifetime && !death)
637 death = time(NULL) + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000638 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100639 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000640 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000641 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
642 /* Increment the number of identities. */
643 tab->nentries++;
644 } else {
645 key_free(k);
Damien Miller4c7728c2007-10-26 14:25:31 +1000646 xfree(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000647 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000648 id->comment = comment;
649 id->death = death;
650 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000651send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000652 buffer_put_int(&e->output, 1);
653 buffer_put_char(&e->output,
654 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
655}
656
Ben Lindstrom2f717042002-06-06 21:52:03 +0000657/* XXX todo: encrypt sensitive data with passphrase */
658static void
659process_lock_agent(SocketEntry *e, int lock)
660{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000661 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000662 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000663
664 passwd = buffer_get_string(&e->request, NULL);
665 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
666 locked = 0;
667 memset(lock_passwd, 0, strlen(lock_passwd));
668 xfree(lock_passwd);
669 lock_passwd = NULL;
670 success = 1;
671 } else if (!locked && lock) {
672 locked = 1;
673 lock_passwd = xstrdup(passwd);
674 success = 1;
675 }
676 memset(passwd, 0, strlen(passwd));
677 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000678
Ben Lindstrom2f717042002-06-06 21:52:03 +0000679 buffer_put_int(&e->output, 1);
680 buffer_put_char(&e->output,
681 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000682}
683
684static void
685no_identities(SocketEntry *e, u_int type)
686{
687 Buffer msg;
688
689 buffer_init(&msg);
690 buffer_put_char(&msg,
691 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
692 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
693 buffer_put_int(&msg, 0);
694 buffer_put_int(&e->output, buffer_len(&msg));
695 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
696 buffer_free(&msg);
697}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698
Damien Miller7ea845e2010-02-12 09:21:02 +1100699#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000701process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000702{
Damien Miller7ea845e2010-02-12 09:21:02 +1100703 char *provider = NULL, *pin;
704 int i, type, version, count = 0, success = 0, death = 0, confirm = 0;
705 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:
715 death = time(NULL) + buffer_get_int(&e->request);
716 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)
727 death = time(NULL) + lifetime;
728
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:
Damien Miller7ea845e2010-02-12 09:21:02 +1100750 if (pin)
751 xfree(pin);
752 if (provider)
753 xfree(provider);
754 if (keys)
755 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000756 buffer_put_int(&e->output, 1);
757 buffer_put_char(&e->output,
758 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
759}
760
761static void
762process_remove_smartcard_key(SocketEntry *e)
763{
Damien Miller7ea845e2010-02-12 09:21:02 +1100764 char *provider = NULL, *pin = NULL;
765 int version, success = 0;
766 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000767 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000768
Damien Miller7ea845e2010-02-12 09:21:02 +1100769 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000770 pin = buffer_get_string(&e->request, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000771 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000772
Damien Miller7ea845e2010-02-12 09:21:02 +1100773 for (version = 1; version < 3; version++) {
774 tab = idtab_lookup(version);
775 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
776 nxt = TAILQ_NEXT(id, next);
777 if (!strcmp(provider, id->provider)) {
778 TAILQ_REMOVE(&tab->idlist, id, next);
779 free_identity(id);
780 tab->nentries--;
781 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000782 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000783 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100784 if (pkcs11_del_provider(provider) == 0)
785 success = 1;
786 else
787 error("process_remove_smartcard_key:"
788 " pkcs11_del_provider failed");
789 xfree(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000790 buffer_put_int(&e->output, 1);
791 buffer_put_char(&e->output,
792 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
793}
Damien Miller7ea845e2010-02-12 09:21:02 +1100794#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000795
Damien Millerad833b32000-08-23 10:46:23 +1000796/* dispatch incoming messages */
797
Ben Lindstrombba81212001-06-25 05:01:22 +0000798static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799process_message(SocketEntry *e)
800{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000801 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000802 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000803
Damien Miller95def091999-11-25 00:26:21 +1100804 if (buffer_len(&e->input) < 5)
805 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100806 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100807 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100808 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000809 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100810 return;
811 }
812 if (buffer_len(&e->input) < msg_len + 4)
813 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000814
815 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100816 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000817 buffer_clear(&e->request);
818 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
819 buffer_consume(&e->input, msg_len);
820 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821
Ben Lindstrom2f717042002-06-06 21:52:03 +0000822 /* check wheter agent is locked */
823 if (locked && type != SSH_AGENTC_UNLOCK) {
824 buffer_clear(&e->request);
825 switch (type) {
826 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
827 case SSH2_AGENTC_REQUEST_IDENTITIES:
828 /* send empty lists */
829 no_identities(e, type);
830 break;
831 default:
832 /* send a fail message for all other request types */
833 buffer_put_int(&e->output, 1);
834 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
835 }
836 return;
837 }
838
Ben Lindstrom3f471632001-07-04 03:53:15 +0000839 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100840 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000841 case SSH_AGENTC_LOCK:
842 case SSH_AGENTC_UNLOCK:
843 process_lock_agent(e, type == SSH_AGENTC_LOCK);
844 break;
Damien Millerad833b32000-08-23 10:46:23 +1000845 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100846 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000847 process_authentication_challenge1(e);
848 break;
849 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
850 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100851 break;
852 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000853 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000854 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100855 break;
856 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000857 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100858 break;
859 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000860 process_remove_all_identities(e, 1);
861 break;
862 /* ssh2 */
863 case SSH2_AGENTC_SIGN_REQUEST:
864 process_sign_request2(e);
865 break;
866 case SSH2_AGENTC_REQUEST_IDENTITIES:
867 process_request_identities(e, 2);
868 break;
869 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000870 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000871 process_add_identity(e, 2);
872 break;
873 case SSH2_AGENTC_REMOVE_IDENTITY:
874 process_remove_identity(e, 2);
875 break;
876 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
877 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100878 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100879#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000880 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000881 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000882 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100883 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000884 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
885 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100886 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100887#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100888 default:
889 /* Unknown message. Respond with failure. */
890 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000891 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100892 buffer_put_int(&e->output, 1);
893 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
894 break;
895 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896}
897
Ben Lindstrombba81212001-06-25 05:01:22 +0000898static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000899new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000901 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000902
Damien Miller232711f2004-06-15 10:35:30 +1000903 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904
Damien Miller95def091999-11-25 00:26:21 +1100905 if (fd > max_fd)
906 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907
Damien Miller95def091999-11-25 00:26:21 +1100908 for (i = 0; i < sockets_alloc; i++)
909 if (sockets[i].type == AUTH_UNUSED) {
910 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100911 buffer_init(&sockets[i].input);
912 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000913 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000914 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100915 return;
916 }
917 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000918 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100919 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000920 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100921 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000922 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100923 sockets[old_alloc].fd = fd;
924 buffer_init(&sockets[old_alloc].input);
925 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000926 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000927 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000928}
929
Ben Lindstrombba81212001-06-25 05:01:22 +0000930static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100931prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
932 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000933{
Darren Tucker2812dc92007-03-21 20:45:06 +1100934 u_int i, sz, deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000935 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100936 static struct timeval tv;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000937
938 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100939 switch (sockets[i].type) {
940 case AUTH_SOCKET:
941 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000942 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100943 break;
944 case AUTH_UNUSED:
945 break;
946 default:
947 fatal("Unknown socket type %d", sockets[i].type);
948 break;
949 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000950 }
951
952 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000953 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000954 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000955 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000956 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000957 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000958 *fdrp = xmalloc(sz);
959 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000960 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000961 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000962 if (n < *fdl)
963 debug("XXX shrink: %d < %d", n, *fdl);
964 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000965 memset(*fdrp, 0, sz);
966 memset(*fdwp, 0, sz);
967
968 for (i = 0; i < sockets_alloc; i++) {
969 switch (sockets[i].type) {
970 case AUTH_SOCKET:
971 case AUTH_CONNECTION:
972 FD_SET(sockets[i].fd, *fdrp);
973 if (buffer_len(&sockets[i].output) > 0)
974 FD_SET(sockets[i].fd, *fdwp);
975 break;
976 default:
977 break;
978 }
979 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100980 deadline = reaper();
981 if (parent_alive_interval != 0)
982 deadline = (deadline == 0) ? parent_alive_interval :
983 MIN(deadline, parent_alive_interval);
984 if (deadline == 0) {
985 *tvpp = NULL;
986 } else {
987 tv.tv_sec = deadline;
988 tv.tv_usec = 0;
989 *tvpp = &tv;
990 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000991 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
Ben Lindstrombba81212001-06-25 05:01:22 +0000994static void
Damien Miller95def091999-11-25 00:26:21 +1100995after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000997 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100998 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100999 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +00001000 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +11001001 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +10001002 uid_t euid;
1003 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004
Darren Tucker72473c62009-10-07 09:01:03 +11001005 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001006 switch (sockets[i].type) {
1007 case AUTH_UNUSED:
1008 break;
1009 case AUTH_SOCKET:
1010 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +11001011 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001012 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +11001013 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +11001014 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001015 error("accept from AUTH_SOCKET: %s",
1016 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001017 break;
1018 }
Damien Miller538f1812002-09-12 09:51:10 +10001019 if (getpeereid(sock, &euid, &egid) < 0) {
1020 error("getpeereid %d failed: %s",
1021 sock, strerror(errno));
1022 close(sock);
1023 break;
1024 }
Damien Milleraf9de382002-10-03 11:54:35 +10001025 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +10001026 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +10001027 "peer euid %u != uid %u",
1028 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +10001029 close(sock);
1030 break;
1031 }
Damien Miller95def091999-11-25 00:26:21 +11001032 new_socket(AUTH_CONNECTION, sock);
1033 }
1034 break;
1035 case AUTH_CONNECTION:
1036 if (buffer_len(&sockets[i].output) > 0 &&
1037 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001038 len = write(sockets[i].fd,
1039 buffer_ptr(&sockets[i].output),
1040 buffer_len(&sockets[i].output));
1041 if (len == -1 && (errno == EAGAIN ||
1042 errno == EWOULDBLOCK ||
1043 errno == EINTR))
1044 continue;
Damien Miller95def091999-11-25 00:26:21 +11001045 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001046 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001047 break;
1048 }
1049 buffer_consume(&sockets[i].output, len);
1050 }
1051 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +11001052 len = read(sockets[i].fd, buf, sizeof(buf));
1053 if (len == -1 && (errno == EAGAIN ||
1054 errno == EWOULDBLOCK ||
1055 errno == EINTR))
1056 continue;
Damien Miller95def091999-11-25 00:26:21 +11001057 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +10001058 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +11001059 break;
1060 }
1061 buffer_append(&sockets[i].input, buf, len);
1062 process_message(&sockets[i]);
1063 }
1064 break;
1065 default:
1066 fatal("Unknown type %d", sockets[i].type);
1067 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068}
1069
Ben Lindstrombba81212001-06-25 05:01:22 +00001070static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001071cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +10001072{
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001073 if (socket_name[0])
1074 unlink(socket_name);
1075 if (socket_dir[0])
1076 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001077}
1078
Darren Tucker3e33cec2003-10-02 16:12:36 +10001079void
Damien Miller792c5111999-10-29 09:47:09 +10001080cleanup_exit(int i)
1081{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001082 cleanup_socket();
1083 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001084}
1085
Damien Miller1c13bd82006-03-26 14:28:14 +11001086/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001087static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001088cleanup_handler(int sig)
1089{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001090 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001091#ifdef ENABLE_PKCS11
1092 pkcs11_terminate();
1093#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001094 _exit(2);
1095}
1096
Ben Lindstrombba81212001-06-25 05:01:22 +00001097static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001098check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001099{
Ben Lindstrom0250da02001-07-22 20:44:00 +00001100 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1101 /* 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 Millerf9b625c2000-07-09 22:42:32 +10001157 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001158 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001159
Damien Miller53d81482003-01-22 11:47:19 +11001160 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001161 switch (ch) {
1162 case 'c':
1163 if (s_flag)
1164 usage();
1165 c_flag++;
1166 break;
1167 case 'k':
1168 k_flag++;
1169 break;
1170 case 's':
1171 if (c_flag)
1172 usage();
1173 s_flag++;
1174 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001175 case 'd':
1176 if (d_flag)
1177 usage();
1178 d_flag++;
1179 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001180 case 'a':
1181 agentsocket = optarg;
1182 break;
Damien Miller53d81482003-01-22 11:47:19 +11001183 case 't':
1184 if ((lifetime = convtime(optarg)) == -1) {
1185 fprintf(stderr, "Invalid lifetime\n");
1186 usage();
1187 }
1188 break;
Damien Miller95def091999-11-25 00:26:21 +11001189 default:
1190 usage();
1191 }
Damien Miller792c5111999-10-29 09:47:09 +10001192 }
Damien Miller95def091999-11-25 00:26:21 +11001193 ac -= optind;
1194 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001195
Ben Lindstromd94580c2001-07-04 03:48:02 +00001196 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001197 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001198
Ben Lindstromeecdf232002-04-02 21:03:51 +00001199 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001200 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001201 if (shell != NULL && (len = strlen(shell)) > 2 &&
1202 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001203 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001204 }
Damien Miller95def091999-11-25 00:26:21 +11001205 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001206 const char *errstr = NULL;
1207
Damien Miller95def091999-11-25 00:26:21 +11001208 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1209 if (pidstr == NULL) {
1210 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001211 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001212 exit(1);
1213 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001214 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1215 if (errstr) {
1216 fprintf(stderr,
1217 "%s=\"%s\", which is not a good PID: %s\n",
1218 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001219 exit(1);
1220 }
1221 if (kill(pid, SIGTERM) == -1) {
1222 perror("kill");
1223 exit(1);
1224 }
1225 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1226 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1227 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001228 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001229 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001230 }
Damien Miller95def091999-11-25 00:26:21 +11001231 parent_pid = getpid();
1232
Ben Lindstromb7788f32002-06-06 21:46:08 +00001233 if (agentsocket == NULL) {
1234 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001235 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001236 if (mkdtemp(socket_dir) == NULL) {
1237 perror("mkdtemp: private socket dir");
1238 exit(1);
1239 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001240 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1241 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001242 } else {
1243 /* Try to use specified agent socket */
1244 socket_dir[0] = '\0';
1245 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001246 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247
Damien Miller5428f641999-11-25 11:54:57 +11001248 /*
1249 * Create socket early so it will exist before command gets run from
1250 * the parent.
1251 */
Damien Miller95def091999-11-25 00:26:21 +11001252 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1253 if (sock < 0) {
1254 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001255 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001256 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001257 }
Damien Miller95def091999-11-25 00:26:21 +11001258 memset(&sunaddr, 0, sizeof(sunaddr));
1259 sunaddr.sun_family = AF_UNIX;
1260 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001261 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001262 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001263 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001264 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001265 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001266 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001268 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001269 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001270 perror("listen");
1271 cleanup_exit(1);
1272 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001273
Damien Miller5428f641999-11-25 11:54:57 +11001274 /*
1275 * Fork, and have the parent execute the command, if any, or present
1276 * the socket data. The child continues as the authentication agent.
1277 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001278 if (d_flag) {
1279 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1280 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1281 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1282 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001283 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001284 goto skip;
1285 }
Damien Miller95def091999-11-25 00:26:21 +11001286 pid = fork();
1287 if (pid == -1) {
1288 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001289 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001290 }
1291 if (pid != 0) { /* Parent - execute the given command. */
1292 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001293 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001294 if (ac == 0) {
1295 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1296 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001297 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001298 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001299 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001300 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001301 exit(0);
1302 }
Damien Millere4340be2000-09-16 13:29:08 +11001303 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1304 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1305 perror("setenv");
1306 exit(1);
1307 }
Damien Miller95def091999-11-25 00:26:21 +11001308 execvp(av[0], av);
1309 perror(av[0]);
1310 exit(1);
1311 }
Damien Millerc653b892002-02-08 22:05:41 +11001312 /* child */
1313 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001314
1315 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001316 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001317 cleanup_exit(1);
1318 }
1319
1320 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001321 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1322 /* XXX might close listen socket */
1323 (void)dup2(fd, STDIN_FILENO);
1324 (void)dup2(fd, STDOUT_FILENO);
1325 (void)dup2(fd, STDERR_FILENO);
1326 if (fd > 2)
1327 close(fd);
1328 }
Damien Miller95def091999-11-25 00:26:21 +11001329
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001330#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001331 /* deny core dumps, since memory contains unencrypted private keys */
1332 rlim.rlim_cur = rlim.rlim_max = 0;
1333 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001334 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001335 cleanup_exit(1);
1336 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001337#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001338
1339skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001340
1341#ifdef ENABLE_PKCS11
1342 pkcs11_init(0);
1343#endif
Damien Miller95def091999-11-25 00:26:21 +11001344 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001345 if (ac > 0)
1346 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001347 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001348 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001349 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001350 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001351 signal(SIGHUP, cleanup_handler);
1352 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001353 nalloc = 0;
1354
Damien Miller95def091999-11-25 00:26:21 +11001355 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001356 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1357 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001358 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001359 if (parent_alive_interval != 0)
1360 check_parent_exists();
1361 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001362 if (result < 0) {
1363 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001364 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001365 fatal("select: %s", strerror(saved_errno));
1366 } else if (result > 0)
1367 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001368 }
1369 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370}