blob: 8210a8e344982048d6e29d0c2b337c235fa5ce07 [file] [log] [blame]
Damien Miller5be9d9e2013-12-07 11:24:01 +11001/* $OpenBSD: ssh-agent.c,v 1.180 2013/12/06 13:39:49 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;
Darren Tucker55119252013-06-02 07:43:59 +1000109 time_t death;
Damien Miller6c711792003-01-24 11:36:23 +1100110 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111} Identity;
112
Damien Millerad833b32000-08-23 10:46:23 +1000113typedef struct {
114 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100115 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000116} Idtab;
117
118/* private key table, one per protocol version */
119Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121int max_fd = 0;
122
123/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000124pid_t parent_pid = -1;
Darren Tucker073f7952013-06-02 23:47:11 +1000125time_t parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
127/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100128char socket_name[MAXPATHLEN];
129char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130
Ben Lindstrom2f717042002-06-06 21:52:03 +0000131/* locking */
132int locked = 0;
133char *lock_passwd = NULL;
134
Damien Miller95def091999-11-25 00:26:21 +1100135extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100136
Darren Tucker55119252013-06-02 07:43:59 +1000137/* Default lifetime in seconds (0 == forever) */
138static long lifetime = 0;
Damien Miller53d81482003-01-22 11:47:19 +1100139
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static void
Damien Miller58f34862002-09-04 16:31:21 +1000141close_socket(SocketEntry *e)
142{
Damien Miller58f34862002-09-04 16:31:21 +1000143 close(e->fd);
144 e->fd = -1;
145 e->type = AUTH_UNUSED;
146 buffer_free(&e->input);
147 buffer_free(&e->output);
148 buffer_free(&e->request);
149}
150
151static void
Damien Millerad833b32000-08-23 10:46:23 +1000152idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Millerad833b32000-08-23 10:46:23 +1000154 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000155
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000156 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100157 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000158 idtable[i].nentries = 0;
159 }
160}
161
162/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000163static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000164idtab_lookup(int version)
165{
166 if (version < 1 || version > 2)
167 fatal("internal error, bad protocol version %d", version);
168 return &idtable[version];
169}
170
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000171static void
172free_identity(Identity *id)
173{
174 key_free(id->key);
Darren Tuckera627d422013-06-02 07:31:17 +1000175 free(id->provider);
176 free(id->comment);
177 free(id);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000178}
179
Damien Millerad833b32000-08-23 10:46:23 +1000180/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100181static Identity *
182lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000183{
Damien Miller1a534ae2002-01-22 23:26:13 +1100184 Identity *id;
185
Damien Millerad833b32000-08-23 10:46:23 +1000186 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100187 TAILQ_FOREACH(id, &tab->idlist, next) {
188 if (key_equal(key, id->key))
189 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000190 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100191 return (NULL);
192}
193
Damien Miller6c711792003-01-24 11:36:23 +1100194/* Check confirmation of keysign request */
195static int
196confirm_key(Identity *id)
197{
Darren Tuckerce327b62004-11-05 20:38:03 +1100198 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100199 int ret = -1;
200
201 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100202 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
203 id->comment, p))
204 ret = 0;
Darren Tuckera627d422013-06-02 07:31:17 +1000205 free(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100206
Damien Miller6c711792003-01-24 11:36:23 +1100207 return (ret);
208}
209
Damien Millerad833b32000-08-23 10:46:23 +1000210/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Damien Millerad833b32000-08-23 10:46:23 +1000212process_request_identities(SocketEntry *e, int version)
213{
214 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100215 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000216 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000219 buffer_put_char(&msg, (version == 1) ?
220 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
221 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100222 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100223 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000224 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
225 buffer_put_bignum(&msg, id->key->rsa->e);
226 buffer_put_bignum(&msg, id->key->rsa->n);
227 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000228 u_char *blob;
229 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100230 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000231 buffer_put_string(&msg, blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000232 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000233 }
234 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100235 }
236 buffer_put_int(&e->output, buffer_len(&msg));
237 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
238 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239}
240
Damien Millerad833b32000-08-23 10:46:23 +1000241/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000242static void
Damien Millerad833b32000-08-23 10:46:23 +1000243process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000245 u_char buf[32], mdbuf[16], session_id[16];
246 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000247 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000248 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000249 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100250 Buffer msg;
251 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000252 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100255 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100256 if ((challenge = BN_new()) == NULL)
257 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000258
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000259 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000260 buffer_get_bignum(&e->request, key->rsa->e);
261 buffer_get_bignum(&e->request, key->rsa->n);
262 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Damien Millerad833b32000-08-23 10:46:23 +1000264 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000265 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000266 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000267 buffer_get(&e->request, session_id, 16);
268 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000269 if (response_type != 1)
270 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller1a534ae2002-01-22 23:26:13 +1100272 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100273 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100274 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000275 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100276 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
277 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100278
Damien Millerad833b32000-08-23 10:46:23 +1000279 /* The response is MD5 of decrypted challenge plus session id. */
280 len = BN_num_bytes(challenge);
281 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000282 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000283 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100284 }
Damien Millerad833b32000-08-23 10:46:23 +1000285 memset(buf, 0, 32);
286 BN_bn2bin(challenge, buf + 32 - len);
287 MD5_Init(&md);
288 MD5_Update(&md, buf, 32);
289 MD5_Update(&md, session_id, 16);
290 MD5_Final(mdbuf, &md);
291
292 /* Send the response. */
293 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
294 for (i = 0; i < 16; i++)
295 buffer_put_char(&msg, mdbuf[i]);
296 goto send;
297 }
298
299failure:
300 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100301 buffer_put_char(&msg, SSH_AGENT_FAILURE);
302send:
303 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000304 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
305 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100306 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000307 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308}
309
Damien Millerad833b32000-08-23 10:46:23 +1000310/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000311static void
Damien Millerad833b32000-08-23 10:46:23 +1000312process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000314 u_char *blob, *data, *signature = NULL;
315 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000316 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000317 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000318 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000319 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000320 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000321
322 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000323
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000324 blob = buffer_get_string(&e->request, &blen);
325 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100326
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000327 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000328 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100329 if (flags & SSH_AGENT_OLD_SIGNATURE)
330 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000331
Damien Miller0bc1bd82000-11-13 22:57:25 +1100332 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000333 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100334 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100335 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100336 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000337 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000338 }
Damien Millerad833b32000-08-23 10:46:23 +1000339 buffer_init(&msg);
340 if (ok == 0) {
341 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
342 buffer_put_string(&msg, signature, slen);
343 } else {
344 buffer_put_char(&msg, SSH_AGENT_FAILURE);
345 }
346 buffer_put_int(&e->output, buffer_len(&msg));
347 buffer_append(&e->output, buffer_ptr(&msg),
348 buffer_len(&msg));
349 buffer_free(&msg);
Darren Tuckera627d422013-06-02 07:31:17 +1000350 free(data);
351 free(blob);
352 free(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000353 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000354}
355
356/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Damien Millerad833b32000-08-23 10:46:23 +1000358process_remove_identity(SocketEntry *e, int version)
359{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000360 u_int blen, bits;
361 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100362 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000363 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100364
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000365 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000366 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100367 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000368 bits = buffer_get_int(&e->request);
369 buffer_get_bignum(&e->request, key->rsa->e);
370 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000371
Damien Millerad833b32000-08-23 10:46:23 +1000372 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000373 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000374 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000375 break;
376 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000377 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100378 key = key_from_blob(blob, blen);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(blob);
Damien Millerad833b32000-08-23 10:46:23 +1000380 break;
381 }
382 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100383 Identity *id = lookup_identity(key, version);
384 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100385 /*
386 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100387 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000388 * the array, we actually free the key there and move
389 * all the entries between the empty slot and the end
390 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100391 */
Damien Millerad833b32000-08-23 10:46:23 +1000392 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 if (tab->nentries < 1)
394 fatal("process_remove_identity: "
395 "internal error: tab->nentries %d",
396 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100397 TAILQ_REMOVE(&tab->idlist, id, next);
398 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000399 tab->nentries--;
400 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
Damien Millerad833b32000-08-23 10:46:23 +1000402 key_free(key);
403 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000405 buffer_put_char(&e->output,
406 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407}
408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
Damien Millerad833b32000-08-23 10:46:23 +1000410process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411{
Damien Millerad833b32000-08-23 10:46:23 +1000412 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100413 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100416 for (id = TAILQ_FIRST(&tab->idlist); id;
417 id = TAILQ_FIRST(&tab->idlist)) {
418 TAILQ_REMOVE(&tab->idlist, id, next);
419 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100420 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Damien Miller95def091999-11-25 00:26:21 +1100422 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000423 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
425 /* Send success. */
426 buffer_put_int(&e->output, 1);
427 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100428}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000429
Darren Tucker2812dc92007-03-21 20:45:06 +1100430/* removes expired keys and returns number of seconds until the next expiry */
Darren Tucker55119252013-06-02 07:43:59 +1000431static time_t
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000432reaper(void)
433{
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000434 time_t deadline = 0, now = monotime();
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000435 Identity *id, *nxt;
436 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000437 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000438
439 for (version = 1; version < 3; version++) {
440 tab = idtab_lookup(version);
441 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
442 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100443 if (id->death == 0)
444 continue;
445 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100446 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000447 TAILQ_REMOVE(&tab->idlist, id, next);
448 free_identity(id);
449 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100450 } else
451 deadline = (deadline == 0) ? id->death :
452 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000453 }
454 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100455 if (deadline == 0 || deadline <= now)
456 return 0;
457 else
458 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000459}
460
461static void
Damien Millerad833b32000-08-23 10:46:23 +1000462process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100463{
Damien Millerad833b32000-08-23 10:46:23 +1000464 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000465 Identity *id;
Darren Tucker55119252013-06-02 07:43:59 +1000466 int type, success = 0, confirm = 0;
Damien Millerf0e90602013-12-07 10:40:26 +1100467 char *comment;
Darren Tucker55119252013-06-02 07:43:59 +1000468 time_t death = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000469 Key *k = NULL;
Damien 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 Miller0bc1bd82000-11-13 22:57:25 +1100486
Damien Miller5be9d9e2013-12-07 11:24:01 +1100487 /* enable blinding */
Damien Millerc51d0732003-03-15 11:37:09 +1100488 if (RSA_blinding_on(k->rsa, NULL) != 1) {
489 error("process_add_identity: RSA_blinding_on failed");
490 key_free(k);
491 goto send;
492 }
493 break;
Damien Millerf0e90602013-12-07 10:40:26 +1100494 case 2:
495 k = key_private_deserialize(&e->request);
496 if (k == NULL) {
497 buffer_clear(&e->request);
498 goto send;
499 }
500 break;
Damien Millerc51d0732003-03-15 11:37:09 +1100501 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000502 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000503 if (k == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000504 free(comment);
Damien Millerad833b32000-08-23 10:46:23 +1000505 goto send;
506 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000507 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000508 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000509 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000510 death = monotime() + buffer_get_int(&e->request);
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000511 break;
Damien Miller6c711792003-01-24 11:36:23 +1100512 case SSH_AGENT_CONSTRAIN_CONFIRM:
513 confirm = 1;
514 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000515 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000516 error("process_add_identity: "
517 "Unknown constraint type %d", type);
Darren Tuckera627d422013-06-02 07:31:17 +1000518 free(comment);
Damien Miller1cfadab2008-06-30 00:05:21 +1000519 key_free(k);
520 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000521 }
522 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000523 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100524 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000525 death = monotime() + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000526 if ((id = lookup_identity(k, version)) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100527 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000528 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000529 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
530 /* Increment the number of identities. */
531 tab->nentries++;
532 } else {
533 key_free(k);
Darren Tuckera627d422013-06-02 07:31:17 +1000534 free(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000535 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000536 id->comment = comment;
537 id->death = death;
538 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000539send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000540 buffer_put_int(&e->output, 1);
541 buffer_put_char(&e->output,
542 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
543}
544
Ben Lindstrom2f717042002-06-06 21:52:03 +0000545/* XXX todo: encrypt sensitive data with passphrase */
546static void
547process_lock_agent(SocketEntry *e, int lock)
548{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000549 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000550 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000551
552 passwd = buffer_get_string(&e->request, NULL);
553 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
554 locked = 0;
555 memset(lock_passwd, 0, strlen(lock_passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000556 free(lock_passwd);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000557 lock_passwd = NULL;
558 success = 1;
559 } else if (!locked && lock) {
560 locked = 1;
561 lock_passwd = xstrdup(passwd);
562 success = 1;
563 }
564 memset(passwd, 0, strlen(passwd));
Darren Tuckera627d422013-06-02 07:31:17 +1000565 free(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000566
Ben Lindstrom2f717042002-06-06 21:52:03 +0000567 buffer_put_int(&e->output, 1);
568 buffer_put_char(&e->output,
569 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000570}
571
572static void
573no_identities(SocketEntry *e, u_int type)
574{
575 Buffer msg;
576
577 buffer_init(&msg);
578 buffer_put_char(&msg,
579 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
580 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
581 buffer_put_int(&msg, 0);
582 buffer_put_int(&e->output, buffer_len(&msg));
583 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
584 buffer_free(&msg);
585}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000586
Damien Miller7ea845e2010-02-12 09:21:02 +1100587#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000588static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000589process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000590{
Damien Miller7ea845e2010-02-12 09:21:02 +1100591 char *provider = NULL, *pin;
Darren Tucker55119252013-06-02 07:43:59 +1000592 int i, type, version, count = 0, success = 0, confirm = 0;
593 time_t death = 0;
Damien Miller7ea845e2010-02-12 09:21:02 +1100594 Key **keys = NULL, *k;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000595 Identity *id;
596 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100597
Damien Miller7ea845e2010-02-12 09:21:02 +1100598 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000599 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000600
601 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000602 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000603 case SSH_AGENT_CONSTRAIN_LIFETIME:
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000604 death = monotime() + buffer_get_int(&e->request);
Damien Millerd94f20d2003-06-11 22:06:33 +1000605 break;
606 case SSH_AGENT_CONSTRAIN_CONFIRM:
607 confirm = 1;
608 break;
609 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000610 error("process_add_smartcard_key: "
611 "Unknown constraint type %d", type);
Damien Miller1cfadab2008-06-30 00:05:21 +1000612 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000613 }
614 }
615 if (lifetime && !death)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000616 death = monotime() + lifetime;
Damien Millerd94f20d2003-06-11 22:06:33 +1000617
Damien Miller7ea845e2010-02-12 09:21:02 +1100618 count = pkcs11_add_provider(provider, pin, &keys);
619 for (i = 0; i < count; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000620 k = keys[i];
621 version = k->type == KEY_RSA1 ? 1 : 2;
622 tab = idtab_lookup(version);
623 if (lookup_identity(k, version) == NULL) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100624 id = xcalloc(1, sizeof(Identity));
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000625 id->key = k;
Damien Miller7ea845e2010-02-12 09:21:02 +1100626 id->provider = xstrdup(provider);
627 id->comment = xstrdup(provider); /* XXX */
Damien Millerd94f20d2003-06-11 22:06:33 +1000628 id->death = death;
629 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000630 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
631 tab->nentries++;
632 success = 1;
633 } else {
634 key_free(k);
635 }
636 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000637 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000638send:
Darren Tuckera627d422013-06-02 07:31:17 +1000639 free(pin);
640 free(provider);
641 free(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000642 buffer_put_int(&e->output, 1);
643 buffer_put_char(&e->output,
644 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
645}
646
647static void
648process_remove_smartcard_key(SocketEntry *e)
649{
Damien Miller7ea845e2010-02-12 09:21:02 +1100650 char *provider = NULL, *pin = NULL;
651 int version, success = 0;
652 Identity *id, *nxt;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000653 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000654
Damien Miller7ea845e2010-02-12 09:21:02 +1100655 provider = buffer_get_string(&e->request, NULL);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000656 pin = buffer_get_string(&e->request, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000657 free(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658
Damien Miller7ea845e2010-02-12 09:21:02 +1100659 for (version = 1; version < 3; version++) {
660 tab = idtab_lookup(version);
661 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
662 nxt = TAILQ_NEXT(id, next);
663 if (!strcmp(provider, id->provider)) {
664 TAILQ_REMOVE(&tab->idlist, id, next);
665 free_identity(id);
666 tab->nentries--;
667 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000668 }
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669 }
Damien Miller7ea845e2010-02-12 09:21:02 +1100670 if (pkcs11_del_provider(provider) == 0)
671 success = 1;
672 else
673 error("process_remove_smartcard_key:"
674 " pkcs11_del_provider failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000675 free(provider);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000676 buffer_put_int(&e->output, 1);
677 buffer_put_char(&e->output,
678 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
679}
Damien Miller7ea845e2010-02-12 09:21:02 +1100680#endif /* ENABLE_PKCS11 */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000681
Damien Millerad833b32000-08-23 10:46:23 +1000682/* dispatch incoming messages */
683
Ben Lindstrombba81212001-06-25 05:01:22 +0000684static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685process_message(SocketEntry *e)
686{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000687 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000688 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000689
Damien Miller95def091999-11-25 00:26:21 +1100690 if (buffer_len(&e->input) < 5)
691 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100692 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100693 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100694 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000695 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100696 return;
697 }
698 if (buffer_len(&e->input) < msg_len + 4)
699 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000700
701 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100702 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000703 buffer_clear(&e->request);
704 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
705 buffer_consume(&e->input, msg_len);
706 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Ben Lindstrom2f717042002-06-06 21:52:03 +0000708 /* check wheter agent is locked */
709 if (locked && type != SSH_AGENTC_UNLOCK) {
710 buffer_clear(&e->request);
711 switch (type) {
712 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
713 case SSH2_AGENTC_REQUEST_IDENTITIES:
714 /* send empty lists */
715 no_identities(e, type);
716 break;
717 default:
718 /* send a fail message for all other request types */
719 buffer_put_int(&e->output, 1);
720 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
721 }
722 return;
723 }
724
Ben Lindstrom3f471632001-07-04 03:53:15 +0000725 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100726 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000727 case SSH_AGENTC_LOCK:
728 case SSH_AGENTC_UNLOCK:
729 process_lock_agent(e, type == SSH_AGENTC_LOCK);
730 break;
Damien Millerad833b32000-08-23 10:46:23 +1000731 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100732 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000733 process_authentication_challenge1(e);
734 break;
735 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
736 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100737 break;
738 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000739 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000740 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100741 break;
742 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000743 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100744 break;
745 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000746 process_remove_all_identities(e, 1);
747 break;
748 /* ssh2 */
749 case SSH2_AGENTC_SIGN_REQUEST:
750 process_sign_request2(e);
751 break;
752 case SSH2_AGENTC_REQUEST_IDENTITIES:
753 process_request_identities(e, 2);
754 break;
755 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000756 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000757 process_add_identity(e, 2);
758 break;
759 case SSH2_AGENTC_REMOVE_IDENTITY:
760 process_remove_identity(e, 2);
761 break;
762 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
763 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100764 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100765#ifdef ENABLE_PKCS11
Ben Lindstrom3f471632001-07-04 03:53:15 +0000766 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000767 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000768 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100769 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000770 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
771 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100772 break;
Damien Miller7ea845e2010-02-12 09:21:02 +1100773#endif /* ENABLE_PKCS11 */
Damien Miller95def091999-11-25 00:26:21 +1100774 default:
775 /* Unknown message. Respond with failure. */
776 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000777 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100778 buffer_put_int(&e->output, 1);
779 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
780 break;
781 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782}
783
Ben Lindstrombba81212001-06-25 05:01:22 +0000784static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000785new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000787 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000788
Damien Miller232711f2004-06-15 10:35:30 +1000789 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790
Damien Miller95def091999-11-25 00:26:21 +1100791 if (fd > max_fd)
792 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793
Damien Miller95def091999-11-25 00:26:21 +1100794 for (i = 0; i < sockets_alloc; i++)
795 if (sockets[i].type == AUTH_UNUSED) {
796 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100797 buffer_init(&sockets[i].input);
798 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000799 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000800 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100801 return;
802 }
803 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000804 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100805 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000806 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100807 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000808 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100809 sockets[old_alloc].fd = fd;
810 buffer_init(&sockets[old_alloc].input);
811 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000812 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814}
815
Ben Lindstrombba81212001-06-25 05:01:22 +0000816static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100817prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
818 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819{
Darren Tucker55119252013-06-02 07:43:59 +1000820 u_int i, sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000821 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100822 static struct timeval tv;
Darren Tucker55119252013-06-02 07:43:59 +1000823 time_t deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000824
825 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100826 switch (sockets[i].type) {
827 case AUTH_SOCKET:
828 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000829 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100830 break;
831 case AUTH_UNUSED:
832 break;
833 default:
834 fatal("Unknown socket type %d", sockets[i].type);
835 break;
836 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000837 }
838
839 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000840 if (*fdrp == NULL || sz > *nallocp) {
Darren Tuckera627d422013-06-02 07:31:17 +1000841 free(*fdrp);
842 free(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 *fdrp = xmalloc(sz);
844 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000845 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000847 if (n < *fdl)
848 debug("XXX shrink: %d < %d", n, *fdl);
849 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 memset(*fdrp, 0, sz);
851 memset(*fdwp, 0, sz);
852
853 for (i = 0; i < sockets_alloc; i++) {
854 switch (sockets[i].type) {
855 case AUTH_SOCKET:
856 case AUTH_CONNECTION:
857 FD_SET(sockets[i].fd, *fdrp);
858 if (buffer_len(&sockets[i].output) > 0)
859 FD_SET(sockets[i].fd, *fdwp);
860 break;
861 default:
862 break;
863 }
864 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100865 deadline = reaper();
866 if (parent_alive_interval != 0)
867 deadline = (deadline == 0) ? parent_alive_interval :
868 MIN(deadline, parent_alive_interval);
869 if (deadline == 0) {
870 *tvpp = NULL;
871 } else {
872 tv.tv_sec = deadline;
873 tv.tv_usec = 0;
874 *tvpp = &tv;
875 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000876 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877}
878
Ben Lindstrombba81212001-06-25 05:01:22 +0000879static void
Damien Miller95def091999-11-25 00:26:21 +1100880after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000882 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100883 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100884 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000885 int len, sock;
Darren Tucker72473c62009-10-07 09:01:03 +1100886 u_int i, orig_alloc;
Damien Miller538f1812002-09-12 09:51:10 +1000887 uid_t euid;
888 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889
Darren Tucker72473c62009-10-07 09:01:03 +1100890 for (i = 0, orig_alloc = sockets_alloc; i < orig_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100891 switch (sockets[i].type) {
892 case AUTH_UNUSED:
893 break;
894 case AUTH_SOCKET:
895 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100896 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000897 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100898 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100899 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100900 error("accept from AUTH_SOCKET: %s",
901 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100902 break;
903 }
Damien Miller538f1812002-09-12 09:51:10 +1000904 if (getpeereid(sock, &euid, &egid) < 0) {
905 error("getpeereid %d failed: %s",
906 sock, strerror(errno));
907 close(sock);
908 break;
909 }
Damien Milleraf9de382002-10-03 11:54:35 +1000910 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000911 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000912 "peer euid %u != uid %u",
913 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000914 close(sock);
915 break;
916 }
Damien Miller95def091999-11-25 00:26:21 +1100917 new_socket(AUTH_CONNECTION, sock);
918 }
919 break;
920 case AUTH_CONNECTION:
921 if (buffer_len(&sockets[i].output) > 0 &&
922 FD_ISSET(sockets[i].fd, writeset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100923 len = write(sockets[i].fd,
924 buffer_ptr(&sockets[i].output),
925 buffer_len(&sockets[i].output));
926 if (len == -1 && (errno == EAGAIN ||
927 errno == EWOULDBLOCK ||
928 errno == EINTR))
929 continue;
Damien Miller95def091999-11-25 00:26:21 +1100930 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000931 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100932 break;
933 }
934 buffer_consume(&sockets[i].output, len);
935 }
936 if (FD_ISSET(sockets[i].fd, readset)) {
Darren Tucker72473c62009-10-07 09:01:03 +1100937 len = read(sockets[i].fd, buf, sizeof(buf));
938 if (len == -1 && (errno == EAGAIN ||
939 errno == EWOULDBLOCK ||
940 errno == EINTR))
941 continue;
Damien Miller95def091999-11-25 00:26:21 +1100942 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000943 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100944 break;
945 }
946 buffer_append(&sockets[i].input, buf, len);
947 process_message(&sockets[i]);
948 }
949 break;
950 default:
951 fatal("Unknown type %d", sockets[i].type);
952 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000953}
954
Ben Lindstrombba81212001-06-25 05:01:22 +0000955static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000956cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000957{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000958 if (socket_name[0])
959 unlink(socket_name);
960 if (socket_dir[0])
961 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000962}
963
Darren Tucker3e33cec2003-10-02 16:12:36 +1000964void
Damien Miller792c5111999-10-29 09:47:09 +1000965cleanup_exit(int i)
966{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000967 cleanup_socket();
968 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000969}
970
Damien Miller1c13bd82006-03-26 14:28:14 +1100971/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000973cleanup_handler(int sig)
974{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000975 cleanup_socket();
Damien Miller7ea845e2010-02-12 09:21:02 +1100976#ifdef ENABLE_PKCS11
977 pkcs11_terminate();
978#endif
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000979 _exit(2);
980}
981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Darren Tucker2812dc92007-03-21 20:45:06 +1100983check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +0000984{
Darren Tucker3e78a512011-06-03 14:14:16 +1000985 /*
986 * If our parent has exited then getppid() will return (pid_t)1,
987 * so testing for that should be safe.
988 */
989 if (parent_pid != -1 && getppid() != parent_pid) {
Ben Lindstrom0250da02001-07-22 20:44:00 +0000990 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +1100991 cleanup_socket();
992 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000993 }
Ben Lindstrom0250da02001-07-22 20:44:00 +0000994}
995
996static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000997usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000998{
Damien Miller5cbe7ca2007-09-17 16:05:50 +1000999 fprintf(stderr, "usage: %s [options] [command [arg ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001000 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001001 fprintf(stderr, "Options:\n");
1002 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1003 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1004 fprintf(stderr, " -k Kill the current agent.\n");
1005 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001006 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001007 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001008 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001009}
1010
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001011int
1012main(int ac, char **av)
1013{
Damien Miller6c711792003-01-24 11:36:23 +11001014 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001015 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001016 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001017 char *shell, *format, *pidstr, *agentsocket = NULL;
1018 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001019 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001020#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001021 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001022#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001023 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001024 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001025 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001026 pid_t pid;
1027 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001028 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001029 size_t len;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001030
Darren Tuckerce321d82005-10-03 18:11:24 +10001031 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1032 sanitise_stdfd();
1033
Damien Miller6cffb9a2002-09-04 16:20:26 +10001034 /* drop */
1035 setegid(getgid());
1036 setgid(getgid());
1037
Damien Miller6c4914a2004-03-03 11:08:59 +11001038#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1039 /* Disable ptrace on Linux without sgid bit */
1040 prctl(PR_SET_DUMPABLE, 0);
1041#endif
1042
Damien Miller4314c2b2010-09-10 11:12:09 +10001043 OpenSSL_add_all_algorithms();
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001044
Damien Miller59d3d5b2003-08-22 09:34:41 +10001045 __progname = ssh_get_progname(av[0]);
Damien Miller60bc5172001-03-19 09:38:15 +11001046 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001047
Damien Miller53d81482003-01-22 11:47:19 +11001048 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001049 switch (ch) {
1050 case 'c':
1051 if (s_flag)
1052 usage();
1053 c_flag++;
1054 break;
1055 case 'k':
1056 k_flag++;
1057 break;
1058 case 's':
1059 if (c_flag)
1060 usage();
1061 s_flag++;
1062 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001063 case 'd':
1064 if (d_flag)
1065 usage();
1066 d_flag++;
1067 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001068 case 'a':
1069 agentsocket = optarg;
1070 break;
Damien Miller53d81482003-01-22 11:47:19 +11001071 case 't':
1072 if ((lifetime = convtime(optarg)) == -1) {
1073 fprintf(stderr, "Invalid lifetime\n");
1074 usage();
1075 }
1076 break;
Damien Miller95def091999-11-25 00:26:21 +11001077 default:
1078 usage();
1079 }
Damien Miller792c5111999-10-29 09:47:09 +10001080 }
Damien Miller95def091999-11-25 00:26:21 +11001081 ac -= optind;
1082 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001083
Ben Lindstromd94580c2001-07-04 03:48:02 +00001084 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001085 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001086
Ben Lindstromeecdf232002-04-02 21:03:51 +00001087 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001088 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001089 if (shell != NULL && (len = strlen(shell)) > 2 &&
1090 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001091 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001092 }
Damien Miller95def091999-11-25 00:26:21 +11001093 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001094 const char *errstr = NULL;
1095
Damien Miller95def091999-11-25 00:26:21 +11001096 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1097 if (pidstr == NULL) {
1098 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001099 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001100 exit(1);
1101 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001102 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1103 if (errstr) {
1104 fprintf(stderr,
1105 "%s=\"%s\", which is not a good PID: %s\n",
1106 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001107 exit(1);
1108 }
1109 if (kill(pid, SIGTERM) == -1) {
1110 perror("kill");
1111 exit(1);
1112 }
1113 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1114 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1115 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001116 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001117 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001118 }
Damien Miller95def091999-11-25 00:26:21 +11001119 parent_pid = getpid();
1120
Ben Lindstromb7788f32002-06-06 21:46:08 +00001121 if (agentsocket == NULL) {
1122 /* Create private directory for agent socket */
Damien Miller2cd62932010-12-01 11:50:35 +11001123 mktemp_proto(socket_dir, sizeof(socket_dir));
Ben Lindstromb7788f32002-06-06 21:46:08 +00001124 if (mkdtemp(socket_dir) == NULL) {
1125 perror("mkdtemp: private socket dir");
1126 exit(1);
1127 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001128 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1129 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001130 } else {
1131 /* Try to use specified agent socket */
1132 socket_dir[0] = '\0';
1133 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135
Damien Miller5428f641999-11-25 11:54:57 +11001136 /*
1137 * Create socket early so it will exist before command gets run from
1138 * the parent.
1139 */
Damien Miller95def091999-11-25 00:26:21 +11001140 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1141 if (sock < 0) {
1142 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001143 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001144 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001145 }
Damien Miller95def091999-11-25 00:26:21 +11001146 memset(&sunaddr, 0, sizeof(sunaddr));
1147 sunaddr.sun_family = AF_UNIX;
1148 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001149 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001150 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001151 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001152 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001153 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001154 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001156 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001157 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001158 perror("listen");
1159 cleanup_exit(1);
1160 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161
Damien Miller5428f641999-11-25 11:54:57 +11001162 /*
1163 * Fork, and have the parent execute the command, if any, or present
1164 * the socket data. The child continues as the authentication agent.
1165 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001166 if (d_flag) {
1167 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1168 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1169 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1170 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001171 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001172 goto skip;
1173 }
Damien Miller95def091999-11-25 00:26:21 +11001174 pid = fork();
1175 if (pid == -1) {
1176 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001177 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001178 }
1179 if (pid != 0) { /* Parent - execute the given command. */
1180 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001181 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001182 if (ac == 0) {
1183 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1184 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001185 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001186 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001187 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001188 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001189 exit(0);
1190 }
Damien Millere4340be2000-09-16 13:29:08 +11001191 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1192 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1193 perror("setenv");
1194 exit(1);
1195 }
Damien Miller95def091999-11-25 00:26:21 +11001196 execvp(av[0], av);
1197 perror(av[0]);
1198 exit(1);
1199 }
Damien Millerc653b892002-02-08 22:05:41 +11001200 /* child */
1201 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001202
1203 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001204 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001205 cleanup_exit(1);
1206 }
1207
1208 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001209 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1210 /* XXX might close listen socket */
1211 (void)dup2(fd, STDIN_FILENO);
1212 (void)dup2(fd, STDOUT_FILENO);
1213 (void)dup2(fd, STDERR_FILENO);
1214 if (fd > 2)
1215 close(fd);
1216 }
Damien Miller95def091999-11-25 00:26:21 +11001217
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001218#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001219 /* deny core dumps, since memory contains unencrypted private keys */
1220 rlim.rlim_cur = rlim.rlim_max = 0;
1221 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001222 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001223 cleanup_exit(1);
1224 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001225#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001226
1227skip:
Damien Miller7ea845e2010-02-12 09:21:02 +11001228
1229#ifdef ENABLE_PKCS11
1230 pkcs11_init(0);
1231#endif
Damien Miller95def091999-11-25 00:26:21 +11001232 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001233 if (ac > 0)
1234 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001235 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001236 signal(SIGPIPE, SIG_IGN);
Damien Miller1f0e86f2013-07-20 13:22:49 +10001237 signal(SIGINT, d_flag ? cleanup_handler : SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001238 signal(SIGHUP, cleanup_handler);
1239 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001240 nalloc = 0;
1241
Damien Miller95def091999-11-25 00:26:21 +11001242 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001243 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1244 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001245 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001246 if (parent_alive_interval != 0)
1247 check_parent_exists();
1248 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001249 if (result < 0) {
1250 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001251 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001252 fatal("select: %s", strerror(saved_errno));
1253 } else if (result > 0)
1254 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001255 }
1256 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257}