blob: f745c2513575ea463019627f2c3372d7adbb38c2 [file] [log] [blame]
Damien Miller7ea845e2010-02-12 09:21:02 +11001/* $OpenBSD: ssh-agent.c,v 1.163 2010/02/08 10:50:20 markus 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 Miller7ea845e2010-02-12 09:21:02 +1100175 xfree(id->provider);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000176 xfree(id->comment);
177 xfree(id);
178}
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;
Damien Miller6c711792003-01-24 11:36:23 +1100205 xfree(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);
232 xfree(blob);
233 }
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);
350 xfree(data);
351 xfree(blob);
352 if (signature != NULL)
353 xfree(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000354 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000355}
356
357/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000358static void
Damien Millerad833b32000-08-23 10:46:23 +1000359process_remove_identity(SocketEntry *e, int version)
360{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000361 u_int blen, bits;
362 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100363 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000364 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100365
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000366 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000367 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100368 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000369 bits = buffer_get_int(&e->request);
370 buffer_get_bignum(&e->request, key->rsa->e);
371 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000372
Damien Millerad833b32000-08-23 10:46:23 +1000373 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000374 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000375 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000376 break;
377 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000378 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000380 xfree(blob);
381 break;
382 }
383 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100384 Identity *id = lookup_identity(key, version);
385 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100386 /*
387 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100388 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000389 * the array, we actually free the key there and move
390 * all the entries between the empty slot and the end
391 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100392 */
Damien Millerad833b32000-08-23 10:46:23 +1000393 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100394 if (tab->nentries < 1)
395 fatal("process_remove_identity: "
396 "internal error: tab->nentries %d",
397 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100398 TAILQ_REMOVE(&tab->idlist, id, next);
399 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000400 tab->nentries--;
401 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100402 }
Damien Millerad833b32000-08-23 10:46:23 +1000403 key_free(key);
404 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000406 buffer_put_char(&e->output,
407 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408}
409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Damien Millerad833b32000-08-23 10:46:23 +1000411process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412{
Damien Millerad833b32000-08-23 10:46:23 +1000413 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100414 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100417 for (id = TAILQ_FIRST(&tab->idlist); id;
418 id = TAILQ_FIRST(&tab->idlist)) {
419 TAILQ_REMOVE(&tab->idlist, id, next);
420 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100421 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Damien Miller95def091999-11-25 00:26:21 +1100423 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000424 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425
426 /* Send success. */
427 buffer_put_int(&e->output, 1);
428 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100429}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000430
Darren Tucker2812dc92007-03-21 20:45:06 +1100431/* removes expired keys and returns number of seconds until the next expiry */
432static u_int
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000433reaper(void)
434{
Darren Tucker2812dc92007-03-21 20:45:06 +1100435 u_int deadline = 0, now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000436 Identity *id, *nxt;
437 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000438 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000439
440 for (version = 1; version < 3; version++) {
441 tab = idtab_lookup(version);
442 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
443 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100444 if (id->death == 0)
445 continue;
446 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100447 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000448 TAILQ_REMOVE(&tab->idlist, id, next);
449 free_identity(id);
450 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100451 } else
452 deadline = (deadline == 0) ? id->death :
453 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000454 }
455 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100456 if (deadline == 0 || deadline <= now)
457 return 0;
458 else
459 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000460}
461
462static void
Damien Millerad833b32000-08-23 10:46:23 +1000463process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100464{
Damien Millerad833b32000-08-23 10:46:23 +1000465 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000466 Identity *id;
Damien Miller6c711792003-01-24 11:36:23 +1100467 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000468 char *type_name, *comment;
469 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100470
Damien Millerad833b32000-08-23 10:46:23 +1000471 switch (version) {
472 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000474 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000475 buffer_get_bignum(&e->request, k->rsa->n);
476 buffer_get_bignum(&e->request, k->rsa->e);
477 buffer_get_bignum(&e->request, k->rsa->d);
478 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100479
Damien Millerad833b32000-08-23 10:46:23 +1000480 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000481 buffer_get_bignum(&e->request, k->rsa->q); /* p */
482 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100483
Damien Millerad833b32000-08-23 10:46:23 +1000484 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000485 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000486 break;
487 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000488 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000489 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000491 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 case KEY_DSA:
493 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000494 buffer_get_bignum2(&e->request, k->dsa->p);
495 buffer_get_bignum2(&e->request, k->dsa->q);
496 buffer_get_bignum2(&e->request, k->dsa->g);
497 buffer_get_bignum2(&e->request, k->dsa->pub_key);
498 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100499 break;
500 case KEY_RSA:
501 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000502 buffer_get_bignum2(&e->request, k->rsa->n);
503 buffer_get_bignum2(&e->request, k->rsa->e);
504 buffer_get_bignum2(&e->request, k->rsa->d);
505 buffer_get_bignum2(&e->request, k->rsa->iqmp);
506 buffer_get_bignum2(&e->request, k->rsa->p);
507 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100508
509 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000510 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511 break;
512 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000513 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000514 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100515 }
Damien Millerad833b32000-08-23 10:46:23 +1000516 break;
517 }
Damien Millerc51d0732003-03-15 11:37:09 +1100518 /* enable blinding */
519 switch (k->type) {
520 case KEY_RSA:
521 case KEY_RSA1:
522 if (RSA_blinding_on(k->rsa, NULL) != 1) {
523 error("process_add_identity: RSA_blinding_on failed");
524 key_free(k);
525 goto send;
526 }
527 break;
528 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000529 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000530 if (k == NULL) {
531 xfree(comment);
532 goto send;
533 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000534 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000535 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000536 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000537 death = time(NULL) + buffer_get_int(&e->request);
538 break;
Damien Miller6c711792003-01-24 11:36:23 +1100539 case SSH_AGENT_CONSTRAIN_CONFIRM:
540 confirm = 1;
541 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000542 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000543 error("process_add_identity: "
544 "Unknown constraint type %d", type);
545 xfree(comment);
546 key_free(k);
547 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000548 }
549 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000550 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100551 if (lifetime && !death)
552 death = time(NULL) + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000553 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100554 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000555 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000556 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
557 /* Increment the number of identities. */
558 tab->nentries++;
559 } else {
560 key_free(k);
Damien Miller4c7728c2007-10-26 14:25:31 +1000561 xfree(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000562 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000563 id->comment = comment;
564 id->death = death;
565 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000566send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000567 buffer_put_int(&e->output, 1);
568 buffer_put_char(&e->output,
569 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
570}
571
Ben Lindstrom2f717042002-06-06 21:52:03 +0000572/* XXX todo: encrypt sensitive data with passphrase */
573static void
574process_lock_agent(SocketEntry *e, int lock)
575{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000576 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000577 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000578
579 passwd = buffer_get_string(&e->request, NULL);
580 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
581 locked = 0;
582 memset(lock_passwd, 0, strlen(lock_passwd));
583 xfree(lock_passwd);
584 lock_passwd = NULL;
585 success = 1;
586 } else if (!locked && lock) {
587 locked = 1;
588 lock_passwd = xstrdup(passwd);
589 success = 1;
590 }
591 memset(passwd, 0, strlen(passwd));
592 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000593
Ben Lindstrom2f717042002-06-06 21:52:03 +0000594 buffer_put_int(&e->output, 1);
595 buffer_put_char(&e->output,
596 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000597}
598
599static void
600no_identities(SocketEntry *e, u_int type)
601{
602 Buffer msg;
603
604 buffer_init(&msg);
605 buffer_put_char(&msg,
606 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
607 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
608 buffer_put_int(&msg, 0);
609 buffer_put_int(&e->output, buffer_len(&msg));
610 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
611 buffer_free(&msg);
612}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000613
Damien Miller7ea845e2010-02-12 09:21:02 +1100614#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000616process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000617{
Damien Miller7ea845e2010-02-12 09:21:02 +1100618 char *provider = NULL, *pin;
619 int i, type, version, count = 0, success = 0, death = 0, confirm = 0;
620 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000621 Identity *id;
622 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100623
Damien Miller7ea845e2010-02-12 09:21:02 +1100624 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000625 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000626
627 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000628 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000629 case SSH_AGENT_CONSTRAIN_LIFETIME:
630 death = time(NULL) + buffer_get_int(&e->request);
631 break;
632 case SSH_AGENT_CONSTRAIN_CONFIRM:
633 confirm = 1;
634 break;
635 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000636 error("process_add_smartcard_key: "
637 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000638 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000639 }
640 }
641 if (lifetime && !death)
642 death = time(NULL) + lifetime;
643
Damien Miller7ea845e2010-02-12 09:21:02 +1100644 count = pkcs11_add_provider(provider, pin, &keys);
645 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000646 k = keys[i];
647 version = k->type == KEY_RSA1 ? 1 : 2;
648 tab = idtab_lookup(version);
649 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100650 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000651 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100652 id->provider = xstrdup(provider);
653 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000654 id->death = death;
655 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000656 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
657 tab->nentries++;
658 success = 1;
659 } else {
660 key_free(k);
661 }
662 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000663 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000664send:
Damien Miller7ea845e2010-02-12 09:21:02 +1100665 if (pin)
666 xfree(pin);
667 if (provider)
668 xfree(provider);
669 if (keys)
670 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000671 buffer_put_int(&e->output, 1);
672 buffer_put_char(&e->output,
673 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
674}
675
676static void
677process_remove_smartcard_key(SocketEntry *e)
678{
Damien Miller7ea845e2010-02-12 09:21:02 +1100679 char *provider = NULL, *pin = NULL;
680 int version, success = 0;
681 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000682 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000683
Damien Miller7ea845e2010-02-12 09:21:02 +1100684 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000685 pin = buffer_get_string(&e->request, NULL);
Ben Lindstromba72d302002-03-22 03:51:06 +0000686 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000687
Damien Miller7ea845e2010-02-12 09:21:02 +1100688 for (version = 1; version < 3; version++) {
689 tab = idtab_lookup(version);
690 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
691 nxt = TAILQ_NEXT(id, next);
692 if (!strcmp(provider, id->provider)) {
693 TAILQ_REMOVE(&tab->idlist, id, next);
694 free_identity(id);
695 tab->nentries--;
696 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000697 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100699 if (pkcs11_del_provider(provider) == 0)
700 success = 1;
701 else
702 error("process_remove_smartcard_key:"
703 " pkcs11_del_provider failed");
704 xfree(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000705 buffer_put_int(&e->output, 1);
706 buffer_put_char(&e->output,
707 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
708}
Damien Miller7ea845e2010-02-12 09:21:02 +1100709#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000710
Damien Millerad833b32000-08-23 10:46:23 +1000711/* dispatch incoming messages */
712
Ben Lindstrombba81212001-06-25 05:01:22 +0000713static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714process_message(SocketEntry *e)
715{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000716 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000717 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000718
Damien Miller95def091999-11-25 00:26:21 +1100719 if (buffer_len(&e->input) < 5)
720 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100721 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100722 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100723 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000724 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100725 return;
726 }
727 if (buffer_len(&e->input) < msg_len + 4)
728 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000729
730 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100731 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000732 buffer_clear(&e->request);
733 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
734 buffer_consume(&e->input, msg_len);
735 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000736
Ben Lindstrom2f717042002-06-06 21:52:03 +0000737 /* check wheter agent is locked */
738 if (locked && type != SSH_AGENTC_UNLOCK) {
739 buffer_clear(&e->request);
740 switch (type) {
741 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
742 case SSH2_AGENTC_REQUEST_IDENTITIES:
743 /* send empty lists */
744 no_identities(e, type);
745 break;
746 default:
747 /* send a fail message for all other request types */
748 buffer_put_int(&e->output, 1);
749 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
750 }
751 return;
752 }
753
Ben Lindstrom3f471632001-07-04 03:53:15 +0000754 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100755 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000756 case SSH_AGENTC_LOCK:
757 case SSH_AGENTC_UNLOCK:
758 process_lock_agent(e, type == SSH_AGENTC_LOCK);
759 break;
Damien Millerad833b32000-08-23 10:46:23 +1000760 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100761 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000762 process_authentication_challenge1(e);
763 break;
764 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
765 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100766 break;
767 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000768 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000769 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100770 break;
771 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000772 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100773 break;
774 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000775 process_remove_all_identities(e, 1);
776 break;
777 /* ssh2 */
778 case SSH2_AGENTC_SIGN_REQUEST:
779 process_sign_request2(e);
780 break;
781 case SSH2_AGENTC_REQUEST_IDENTITIES:
782 process_request_identities(e, 2);
783 break;
784 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000785 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000786 process_add_identity(e, 2);
787 break;
788 case SSH2_AGENTC_REMOVE_IDENTITY:
789 process_remove_identity(e, 2);
790 break;
791 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
792 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100793 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100794#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000795 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000796 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000797 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100798 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000799 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
800 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100801 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100802#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100803 default:
804 /* Unknown message. Respond with failure. */
805 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000806 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100807 buffer_put_int(&e->output, 1);
808 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
809 break;
810 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000811}
812
Ben Lindstrombba81212001-06-25 05:01:22 +0000813static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000814new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000816 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000817
Damien Miller232711f2004-06-15 10:35:30 +1000818 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819
Damien Miller95def091999-11-25 00:26:21 +1100820 if (fd > max_fd)
821 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822
Damien Miller95def091999-11-25 00:26:21 +1100823 for (i = 0; i < sockets_alloc; i++)
824 if (sockets[i].type == AUTH_UNUSED) {
825 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100826 buffer_init(&sockets[i].input);
827 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000828 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000829 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100830 return;
831 }
832 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000833 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100834 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000835 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100836 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000837 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100838 sockets[old_alloc].fd = fd;
839 buffer_init(&sockets[old_alloc].input);
840 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000841 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000842 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843}
844
Ben Lindstrombba81212001-06-25 05:01:22 +0000845static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100846prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
847 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000848{
Darren Tucker2812dc92007-03-21 20:45:06 +1100849 u_int i, sz, deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100851 static struct timeval tv;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852
853 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100854 switch (sockets[i].type) {
855 case AUTH_SOCKET:
856 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000857 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100858 break;
859 case AUTH_UNUSED:
860 break;
861 default:
862 fatal("Unknown socket type %d", sockets[i].type);
863 break;
864 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000865 }
866
867 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000868 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000869 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000870 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000871 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000872 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000873 *fdrp = xmalloc(sz);
874 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000875 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000876 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000877 if (n < *fdl)
878 debug("XXX shrink: %d < %d", n, *fdl);
879 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000880 memset(*fdrp, 0, sz);
881 memset(*fdwp, 0, sz);
882
883 for (i = 0; i < sockets_alloc; i++) {
884 switch (sockets[i].type) {
885 case AUTH_SOCKET:
886 case AUTH_CONNECTION:
887 FD_SET(sockets[i].fd, *fdrp);
888 if (buffer_len(&sockets[i].output) > 0)
889 FD_SET(sockets[i].fd, *fdwp);
890 break;
891 default:
892 break;
893 }
894 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100895 deadline = reaper();
896 if (parent_alive_interval != 0)
897 deadline = (deadline == 0) ? parent_alive_interval :
898 MIN(deadline, parent_alive_interval);
899 if (deadline == 0) {
900 *tvpp = NULL;
901 } else {
902 tv.tv_sec = deadline;
903 tv.tv_usec = 0;
904 *tvpp = &tv;
905 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000906 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000907}
908
Ben Lindstrombba81212001-06-25 05:01:22 +0000909static void
Damien Miller95def091999-11-25 00:26:21 +1100910after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000911{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000912 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100913 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100914 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000915 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100916 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000917 uid_t euid;
918 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000919
Darren Tucker72473c62009-10-07 09:01:03 +1100920 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100921 switch (sockets[i].type) {
922 case AUTH_UNUSED:
923 break;
924 case AUTH_SOCKET:
925 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100926 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000927 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100928 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100929 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100930 error("accept from AUTH_SOCKET: %s",
931 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100932 break;
933 }
Damien Miller538f1812002-09-12 09:51:10 +1000934 if (getpeereid(sock, &euid, &egid) < 0) {
935 error("getpeereid %d failed: %s",
936 sock, strerror(errno));
937 close(sock);
938 break;
939 }
Damien Milleraf9de382002-10-03 11:54:35 +1000940 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000941 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000942 "peer euid %u != uid %u",
943 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000944 close(sock);
945 break;
946 }
Damien Miller95def091999-11-25 00:26:21 +1100947 new_socket(AUTH_CONNECTION, sock);
948 }
949 break;
950 case AUTH_CONNECTION:
951 if (buffer_len(&sockets[i].output) > 0 &&
952 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100953 len = write(sockets[i].fd,
954 buffer_ptr(&sockets[i].output),
955 buffer_len(&sockets[i].output));
956 if (len == -1 && (errno == EAGAIN ||
957 errno == EWOULDBLOCK ||
958 errno == EINTR))
959 continue;
Damien Miller95def091999-11-25 00:26:21 +1100960 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000961 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100962 break;
963 }
964 buffer_consume(&sockets[i].output, len);
965 }
966 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100967 len = read(sockets[i].fd, buf, sizeof(buf));
968 if (len == -1 && (errno == EAGAIN ||
969 errno == EWOULDBLOCK ||
970 errno == EINTR))
971 continue;
Damien Miller95def091999-11-25 00:26:21 +1100972 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000973 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100974 break;
975 }
976 buffer_append(&sockets[i].input, buf, len);
977 process_message(&sockets[i]);
978 }
979 break;
980 default:
981 fatal("Unknown type %d", sockets[i].type);
982 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983}
984
Ben Lindstrombba81212001-06-25 05:01:22 +0000985static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000986cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000987{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000988 if (socket_name[0])
989 unlink(socket_name);
990 if (socket_dir[0])
991 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000992}
993
Darren Tucker3e33cec2003-10-02 16:12:36 +1000994void
Damien Miller792c5111999-10-29 09:47:09 +1000995cleanup_exit(int i)
996{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000997 cleanup_socket();
998 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000999}
1000
Damien Miller1c13bd82006-03-26 14:28:14 +11001001/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001002static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001003cleanup_handler(int sig)
1004{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001005 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +11001006#ifdef ENABLE_PKCS11
1007 pkcs11_terminate();
1008#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001009 _exit(2);
1010}
1011
Ben Lindstrombba81212001-06-25 05:01:22 +00001012static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001013check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001014{
Ben Lindstrom0250da02001-07-22 20:44:00 +00001015 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1016 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001017 cleanup_socket();
1018 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001019 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001020}
1021
1022static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001023usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001024{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001025 fprintf(stderr, "usage: %s [options] [command [arg ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001026 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001027 fprintf(stderr, "Options:\n");
1028 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1029 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1030 fprintf(stderr, " -k Kill the current agent.\n");
1031 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001032 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001033 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001034 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001035}
1036
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037int
1038main(int ac, char **av)
1039{
Damien Miller6c711792003-01-24 11:36:23 +11001040 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001041 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001042 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001043 char *shell, *format, *pidstr, *agentsocket = NULL;
1044 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001045 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001046#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001047 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001048#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001049 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001050 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001051 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001052 pid_t pid;
1053 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001054 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001055 size_t len;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001056
Darren Tuckerce321d82005-10-03 18:11:24 +10001057 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1058 sanitise_stdfd();
1059
Damien Miller6cffb9a2002-09-04 16:20:26 +10001060 /* drop */
1061 setegid(getgid());
1062 setgid(getgid());
1063
Damien Miller6c4914a2004-03-03 11:08:59 +11001064#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1065 /* Disable ptrace on Linux without sgid bit */
1066 prctl(PR_SET_DUMPABLE, 0);
1067#endif
1068
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001069 SSLeay_add_all_algorithms();
1070
Damien Miller59d3d5b2003-08-22 09:34:41 +10001071 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001072 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001073 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001074
Damien Miller53d81482003-01-22 11:47:19 +11001075 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001076 switch (ch) {
1077 case 'c':
1078 if (s_flag)
1079 usage();
1080 c_flag++;
1081 break;
1082 case 'k':
1083 k_flag++;
1084 break;
1085 case 's':
1086 if (c_flag)
1087 usage();
1088 s_flag++;
1089 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001090 case 'd':
1091 if (d_flag)
1092 usage();
1093 d_flag++;
1094 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001095 case 'a':
1096 agentsocket = optarg;
1097 break;
Damien Miller53d81482003-01-22 11:47:19 +11001098 case 't':
1099 if ((lifetime = convtime(optarg)) == -1) {
1100 fprintf(stderr, "Invalid lifetime\n");
1101 usage();
1102 }
1103 break;
Damien Miller95def091999-11-25 00:26:21 +11001104 default:
1105 usage();
1106 }
Damien Miller792c5111999-10-29 09:47:09 +10001107 }
Damien Miller95def091999-11-25 00:26:21 +11001108 ac -= optind;
1109 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001110
Ben Lindstromd94580c2001-07-04 03:48:02 +00001111 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001112 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001113
Ben Lindstromeecdf232002-04-02 21:03:51 +00001114 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001115 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001116 if (shell != NULL && (len = strlen(shell)) > 2 &&
1117 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001118 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001119 }
Damien Miller95def091999-11-25 00:26:21 +11001120 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001121 const char *errstr = NULL;
1122
Damien Miller95def091999-11-25 00:26:21 +11001123 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1124 if (pidstr == NULL) {
1125 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001126 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001127 exit(1);
1128 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001129 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1130 if (errstr) {
1131 fprintf(stderr,
1132 "%s=\"%s\", which is not a good PID: %s\n",
1133 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001134 exit(1);
1135 }
1136 if (kill(pid, SIGTERM) == -1) {
1137 perror("kill");
1138 exit(1);
1139 }
1140 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1141 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1142 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001143 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001144 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001145 }
Damien Miller95def091999-11-25 00:26:21 +11001146 parent_pid = getpid();
1147
Ben Lindstromb7788f32002-06-06 21:46:08 +00001148 if (agentsocket == NULL) {
1149 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001150 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001151 if (mkdtemp(socket_dir) == NULL) {
1152 perror("mkdtemp: private socket dir");
1153 exit(1);
1154 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001155 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1156 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001157 } else {
1158 /* Try to use specified agent socket */
1159 socket_dir[0] = '\0';
1160 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001161 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001162
Damien Miller5428f641999-11-25 11:54:57 +11001163 /*
1164 * Create socket early so it will exist before command gets run from
1165 * the parent.
1166 */
Damien Miller95def091999-11-25 00:26:21 +11001167 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1168 if (sock < 0) {
1169 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001170 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001171 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001172 }
Damien Miller95def091999-11-25 00:26:21 +11001173 memset(&sunaddr, 0, sizeof(sunaddr));
1174 sunaddr.sun_family = AF_UNIX;
1175 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001176 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001177 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001178 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001179 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001180 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001181 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001182 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001183 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001184 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001185 perror("listen");
1186 cleanup_exit(1);
1187 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001188
Damien Miller5428f641999-11-25 11:54:57 +11001189 /*
1190 * Fork, and have the parent execute the command, if any, or present
1191 * the socket data. The child continues as the authentication agent.
1192 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001193 if (d_flag) {
1194 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1195 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1196 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1197 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001198 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001199 goto skip;
1200 }
Damien Miller95def091999-11-25 00:26:21 +11001201 pid = fork();
1202 if (pid == -1) {
1203 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001204 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001205 }
1206 if (pid != 0) { /* Parent - execute the given command. */
1207 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001208 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001209 if (ac == 0) {
1210 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1211 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001212 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001213 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001214 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001215 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001216 exit(0);
1217 }
Damien Millere4340be2000-09-16 13:29:08 +11001218 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1219 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1220 perror("setenv");
1221 exit(1);
1222 }
Damien Miller95def091999-11-25 00:26:21 +11001223 execvp(av[0], av);
1224 perror(av[0]);
1225 exit(1);
1226 }
Damien Millerc653b892002-02-08 22:05:41 +11001227 /* child */
1228 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001229
1230 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001231 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001232 cleanup_exit(1);
1233 }
1234
1235 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001236 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1237 /* XXX might close listen socket */
1238 (void)dup2(fd, STDIN_FILENO);
1239 (void)dup2(fd, STDOUT_FILENO);
1240 (void)dup2(fd, STDERR_FILENO);
1241 if (fd > 2)
1242 close(fd);
1243 }
Damien Miller95def091999-11-25 00:26:21 +11001244
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001245#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001246 /* deny core dumps, since memory contains unencrypted private keys */
1247 rlim.rlim_cur = rlim.rlim_max = 0;
1248 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001249 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001250 cleanup_exit(1);
1251 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001252#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001253
1254skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001255
1256#ifdef ENABLE_PKCS11
1257 pkcs11_init(0);
1258#endif
Damien Miller95def091999-11-25 00:26:21 +11001259 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001260 if (ac > 0)
1261 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001262 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001263 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001264 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001265 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001266 signal(SIGHUP, cleanup_handler);
1267 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001268 nalloc = 0;
1269
Damien Miller95def091999-11-25 00:26:21 +11001270 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001271 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1272 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001273 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001274 if (parent_alive_interval != 0)
1275 check_parent_exists();
1276 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001277 if (result < 0) {
1278 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001279 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001280 fatal("select: %s", strerror(saved_errno));
1281 } else if (result > 0)
1282 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001283 }
1284 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285}