blob: a0713b238d4f4711a9659d0d4ce47bf7d3e1d83b [file] [log] [blame]
Damien Miller5598b4f2006-07-24 14:09:40 +10001/* $OpenBSD: ssh-agent.c,v 1.145 2006/07/22 19:08:54 stevesk 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 Miller42fb0682006-03-15 14:03:06 +110040#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100041#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110042#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#endif
Damien Miller9b481512002-09-12 10:43:29 +100045#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110046#include <sys/resource.h>
Damien Millere3b60b52006-07-10 21:08:03 +100047
Darren Tucker39972492006-07-12 22:22:46 +100048#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100049#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110050#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110051# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110052#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110053#include <signal.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100054#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +100055#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110056
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include <openssl/evp.h>
58#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
60#include "ssh.h"
61#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "buffer.h"
63#include "bufaux.h"
64#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100065#include "key.h"
66#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110067#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000068#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110069#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Ben Lindstrom3f471632001-07-04 03:53:15 +000071#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000072#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000073#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000074
Damien Miller6c4914a2004-03-03 11:08:59 +110075#if defined(HAVE_SYS_PRCTL_H)
76#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
77#endif
78
Ben Lindstrom65366a82001-12-06 16:32:47 +000079typedef enum {
80 AUTH_UNUSED,
81 AUTH_SOCKET,
82 AUTH_CONNECTION
83} sock_type;
84
Damien Miller95def091999-11-25 00:26:21 +110085typedef struct {
86 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000087 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110088 Buffer input;
89 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000090 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091} SocketEntry;
92
Ben Lindstrom46c16222000-12-22 01:43:59 +000093u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094SocketEntry *sockets = NULL;
95
Damien Miller1a534ae2002-01-22 23:26:13 +110096typedef struct identity {
97 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100098 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110099 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000100 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100101 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102} Identity;
103
Damien Millerad833b32000-08-23 10:46:23 +1000104typedef struct {
105 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100106 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000107} Idtab;
108
109/* private key table, one per protocol version */
110Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
112int max_fd = 0;
113
114/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000115pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
117/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100118char socket_name[MAXPATHLEN];
119char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Ben Lindstrom2f717042002-06-06 21:52:03 +0000121/* locking */
122int locked = 0;
123char *lock_passwd = NULL;
124
Damien Miller95def091999-11-25 00:26:21 +1100125extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100126
Damien Miller53d81482003-01-22 11:47:19 +1100127/* Default lifetime (0 == forever) */
128static int lifetime = 0;
129
Ben Lindstrombba81212001-06-25 05:01:22 +0000130static void
Damien Miller58f34862002-09-04 16:31:21 +1000131close_socket(SocketEntry *e)
132{
Damien Miller58f34862002-09-04 16:31:21 +1000133 close(e->fd);
134 e->fd = -1;
135 e->type = AUTH_UNUSED;
136 buffer_free(&e->input);
137 buffer_free(&e->output);
138 buffer_free(&e->request);
139}
140
141static void
Damien Millerad833b32000-08-23 10:46:23 +1000142idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Millerad833b32000-08-23 10:46:23 +1000144 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000145
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000146 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100147 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000148 idtable[i].nentries = 0;
149 }
150}
151
152/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000154idtab_lookup(int version)
155{
156 if (version < 1 || version > 2)
157 fatal("internal error, bad protocol version %d", version);
158 return &idtable[version];
159}
160
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000161static void
162free_identity(Identity *id)
163{
164 key_free(id->key);
165 xfree(id->comment);
166 xfree(id);
167}
168
Damien Millerad833b32000-08-23 10:46:23 +1000169/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100170static Identity *
171lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000172{
Damien Miller1a534ae2002-01-22 23:26:13 +1100173 Identity *id;
174
Damien Millerad833b32000-08-23 10:46:23 +1000175 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100176 TAILQ_FOREACH(id, &tab->idlist, next) {
177 if (key_equal(key, id->key))
178 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000179 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100180 return (NULL);
181}
182
Damien Miller6c711792003-01-24 11:36:23 +1100183/* Check confirmation of keysign request */
184static int
185confirm_key(Identity *id)
186{
Darren Tuckerce327b62004-11-05 20:38:03 +1100187 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100188 int ret = -1;
189
190 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100191 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
192 id->comment, p))
193 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100194 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100195
Damien Miller6c711792003-01-24 11:36:23 +1100196 return (ret);
197}
198
Damien Millerad833b32000-08-23 10:46:23 +1000199/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Damien Millerad833b32000-08-23 10:46:23 +1000201process_request_identities(SocketEntry *e, int version)
202{
203 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100204 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000205 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206
Damien Miller95def091999-11-25 00:26:21 +1100207 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000208 buffer_put_char(&msg, (version == 1) ?
209 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
210 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100211 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100212 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000213 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
214 buffer_put_bignum(&msg, id->key->rsa->e);
215 buffer_put_bignum(&msg, id->key->rsa->n);
216 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000217 u_char *blob;
218 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100219 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000220 buffer_put_string(&msg, blob, blen);
221 xfree(blob);
222 }
223 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100224 }
225 buffer_put_int(&e->output, buffer_len(&msg));
226 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
227 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000228}
229
Damien Millerad833b32000-08-23 10:46:23 +1000230/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000231static void
Damien Millerad833b32000-08-23 10:46:23 +1000232process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000234 u_char buf[32], mdbuf[16], session_id[16];
235 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000236 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000237 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000238 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100239 Buffer msg;
240 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000241 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Miller95def091999-11-25 00:26:21 +1100243 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100244 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100245 if ((challenge = BN_new()) == NULL)
246 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000247
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000248 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000249 buffer_get_bignum(&e->request, key->rsa->e);
250 buffer_get_bignum(&e->request, key->rsa->n);
251 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Millerad833b32000-08-23 10:46:23 +1000253 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000254 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000255 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000256 buffer_get(&e->request, session_id, 16);
257 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000258 if (response_type != 1)
259 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Miller1a534ae2002-01-22 23:26:13 +1100261 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100262 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100263 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000264 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100265 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
266 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100267
Damien Millerad833b32000-08-23 10:46:23 +1000268 /* The response is MD5 of decrypted challenge plus session id. */
269 len = BN_num_bytes(challenge);
270 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000271 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000272 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100273 }
Damien Millerad833b32000-08-23 10:46:23 +1000274 memset(buf, 0, 32);
275 BN_bn2bin(challenge, buf + 32 - len);
276 MD5_Init(&md);
277 MD5_Update(&md, buf, 32);
278 MD5_Update(&md, session_id, 16);
279 MD5_Final(mdbuf, &md);
280
281 /* Send the response. */
282 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
283 for (i = 0; i < 16; i++)
284 buffer_put_char(&msg, mdbuf[i]);
285 goto send;
286 }
287
288failure:
289 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100290 buffer_put_char(&msg, SSH_AGENT_FAILURE);
291send:
292 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000293 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
294 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100295 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000296 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297}
298
Damien Millerad833b32000-08-23 10:46:23 +1000299/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000300static void
Damien Millerad833b32000-08-23 10:46:23 +1000301process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000303 u_char *blob, *data, *signature = NULL;
304 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000305 extern int datafellows;
306 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000307 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000308 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000309
310 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000311
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000312 blob = buffer_get_string(&e->request, &blen);
313 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100314
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000315 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100316 if (flags & SSH_AGENT_OLD_SIGNATURE)
317 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000318
Damien Miller0bc1bd82000-11-13 22:57:25 +1100319 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000320 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100321 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100322 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100323 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000324 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000325 }
Damien Millerad833b32000-08-23 10:46:23 +1000326 buffer_init(&msg);
327 if (ok == 0) {
328 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
329 buffer_put_string(&msg, signature, slen);
330 } else {
331 buffer_put_char(&msg, SSH_AGENT_FAILURE);
332 }
333 buffer_put_int(&e->output, buffer_len(&msg));
334 buffer_append(&e->output, buffer_ptr(&msg),
335 buffer_len(&msg));
336 buffer_free(&msg);
337 xfree(data);
338 xfree(blob);
339 if (signature != NULL)
340 xfree(signature);
341}
342
343/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000344static void
Damien Millerad833b32000-08-23 10:46:23 +1000345process_remove_identity(SocketEntry *e, int version)
346{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000347 u_int blen, bits;
348 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100349 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000350 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100351
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000352 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000353 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100354 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000355 bits = buffer_get_int(&e->request);
356 buffer_get_bignum(&e->request, key->rsa->e);
357 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358
Damien Millerad833b32000-08-23 10:46:23 +1000359 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000360 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000361 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000362 break;
363 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000364 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100365 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000366 xfree(blob);
367 break;
368 }
369 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100370 Identity *id = lookup_identity(key, version);
371 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100372 /*
373 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100374 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000375 * the array, we actually free the key there and move
376 * all the entries between the empty slot and the end
377 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100378 */
Damien Millerad833b32000-08-23 10:46:23 +1000379 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380 if (tab->nentries < 1)
381 fatal("process_remove_identity: "
382 "internal error: tab->nentries %d",
383 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100384 TAILQ_REMOVE(&tab->idlist, id, next);
385 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000386 tab->nentries--;
387 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100388 }
Damien Millerad833b32000-08-23 10:46:23 +1000389 key_free(key);
390 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000392 buffer_put_char(&e->output,
393 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394}
395
Ben Lindstrombba81212001-06-25 05:01:22 +0000396static void
Damien Millerad833b32000-08-23 10:46:23 +1000397process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398{
Damien Millerad833b32000-08-23 10:46:23 +1000399 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100400 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Damien Miller95def091999-11-25 00:26:21 +1100402 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100403 for (id = TAILQ_FIRST(&tab->idlist); id;
404 id = TAILQ_FIRST(&tab->idlist)) {
405 TAILQ_REMOVE(&tab->idlist, id, next);
406 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100407 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller95def091999-11-25 00:26:21 +1100409 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000410 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000411
412 /* Send success. */
413 buffer_put_int(&e->output, 1);
414 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100415}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Ben Lindstrombba81212001-06-25 05:01:22 +0000417static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000418reaper(void)
419{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000420 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000421 Identity *id, *nxt;
422 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000423 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000424
425 for (version = 1; version < 3; version++) {
426 tab = idtab_lookup(version);
427 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
428 nxt = TAILQ_NEXT(id, next);
429 if (id->death != 0 && now >= id->death) {
430 TAILQ_REMOVE(&tab->idlist, id, next);
431 free_identity(id);
432 tab->nentries--;
433 }
434 }
435 }
436}
437
438static void
Damien Millerad833b32000-08-23 10:46:23 +1000439process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100440{
Damien Millerad833b32000-08-23 10:46:23 +1000441 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100442 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000443 char *type_name, *comment;
444 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100445
Damien Millerad833b32000-08-23 10:46:23 +1000446 switch (version) {
447 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100448 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000449 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000450 buffer_get_bignum(&e->request, k->rsa->n);
451 buffer_get_bignum(&e->request, k->rsa->e);
452 buffer_get_bignum(&e->request, k->rsa->d);
453 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100454
Damien Millerad833b32000-08-23 10:46:23 +1000455 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 buffer_get_bignum(&e->request, k->rsa->q); /* p */
457 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100458
Damien Millerad833b32000-08-23 10:46:23 +1000459 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000460 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000461 break;
462 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000463 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000464 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100465 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000466 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467 case KEY_DSA:
468 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000469 buffer_get_bignum2(&e->request, k->dsa->p);
470 buffer_get_bignum2(&e->request, k->dsa->q);
471 buffer_get_bignum2(&e->request, k->dsa->g);
472 buffer_get_bignum2(&e->request, k->dsa->pub_key);
473 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 break;
475 case KEY_RSA:
476 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000477 buffer_get_bignum2(&e->request, k->rsa->n);
478 buffer_get_bignum2(&e->request, k->rsa->e);
479 buffer_get_bignum2(&e->request, k->rsa->d);
480 buffer_get_bignum2(&e->request, k->rsa->iqmp);
481 buffer_get_bignum2(&e->request, k->rsa->p);
482 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483
484 /* 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 break;
487 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000488 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000489 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100490 }
Damien Millerad833b32000-08-23 10:46:23 +1000491 break;
492 }
Damien Millerc51d0732003-03-15 11:37:09 +1100493 /* enable blinding */
494 switch (k->type) {
495 case KEY_RSA:
496 case KEY_RSA1:
497 if (RSA_blinding_on(k->rsa, NULL) != 1) {
498 error("process_add_identity: RSA_blinding_on failed");
499 key_free(k);
500 goto send;
501 }
502 break;
503 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000504 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000505 if (k == NULL) {
506 xfree(comment);
507 goto send;
508 }
509 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000510 while (buffer_len(&e->request)) {
511 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000512 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000513 death = time(NULL) + buffer_get_int(&e->request);
514 break;
Damien Miller6c711792003-01-24 11:36:23 +1100515 case SSH_AGENT_CONSTRAIN_CONFIRM:
516 confirm = 1;
517 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000518 default:
519 break;
520 }
521 }
Damien Miller53d81482003-01-22 11:47:19 +1100522 if (lifetime && !death)
523 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000524 if (lookup_identity(k, version) == NULL) {
525 Identity *id = xmalloc(sizeof(Identity));
526 id->key = k;
527 id->comment = comment;
528 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100529 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000530 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
531 /* Increment the number of identities. */
532 tab->nentries++;
533 } else {
534 key_free(k);
535 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000536 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000537send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000538 buffer_put_int(&e->output, 1);
539 buffer_put_char(&e->output,
540 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
541}
542
Ben Lindstrom2f717042002-06-06 21:52:03 +0000543/* XXX todo: encrypt sensitive data with passphrase */
544static void
545process_lock_agent(SocketEntry *e, int lock)
546{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000547 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000548 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000549
550 passwd = buffer_get_string(&e->request, NULL);
551 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
552 locked = 0;
553 memset(lock_passwd, 0, strlen(lock_passwd));
554 xfree(lock_passwd);
555 lock_passwd = NULL;
556 success = 1;
557 } else if (!locked && lock) {
558 locked = 1;
559 lock_passwd = xstrdup(passwd);
560 success = 1;
561 }
562 memset(passwd, 0, strlen(passwd));
563 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000564
Ben Lindstrom2f717042002-06-06 21:52:03 +0000565 buffer_put_int(&e->output, 1);
566 buffer_put_char(&e->output,
567 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000568}
569
570static void
571no_identities(SocketEntry *e, u_int type)
572{
573 Buffer msg;
574
575 buffer_init(&msg);
576 buffer_put_char(&msg,
577 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
578 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
579 buffer_put_int(&msg, 0);
580 buffer_put_int(&e->output, buffer_len(&msg));
581 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
582 buffer_free(&msg);
583}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000584
585#ifdef SMARTCARD
586static void
587process_add_smartcard_key (SocketEntry *e)
588{
Ben Lindstromba72d302002-03-22 03:51:06 +0000589 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000590 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000591 Key **keys, *k;
592 Identity *id;
593 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100594
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000595 sc_reader_id = buffer_get_string(&e->request, NULL);
596 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000597
598 while (buffer_len(&e->request)) {
599 switch (buffer_get_char(&e->request)) {
600 case SSH_AGENT_CONSTRAIN_LIFETIME:
601 death = time(NULL) + buffer_get_int(&e->request);
602 break;
603 case SSH_AGENT_CONSTRAIN_CONFIRM:
604 confirm = 1;
605 break;
606 default:
607 break;
608 }
609 }
610 if (lifetime && !death)
611 death = time(NULL) + lifetime;
612
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000613 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000614 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000615 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000616
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000617 if (keys == NULL || keys[0] == NULL) {
618 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000619 goto send;
620 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000621 for (i = 0; keys[i] != NULL; i++) {
622 k = keys[i];
623 version = k->type == KEY_RSA1 ? 1 : 2;
624 tab = idtab_lookup(version);
625 if (lookup_identity(k, version) == NULL) {
626 id = xmalloc(sizeof(Identity));
627 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000628 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000629 id->death = death;
630 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000631 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
632 tab->nentries++;
633 success = 1;
634 } else {
635 key_free(k);
636 }
637 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000638 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000639 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000640send:
641 buffer_put_int(&e->output, 1);
642 buffer_put_char(&e->output,
643 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
644}
645
646static void
647process_remove_smartcard_key(SocketEntry *e)
648{
Ben Lindstromba72d302002-03-22 03:51:06 +0000649 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000650 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000651 Key **keys, *k = NULL;
652 Identity *id;
653 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000654
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000655 sc_reader_id = buffer_get_string(&e->request, NULL);
656 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000657 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000658 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000659 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000661 if (keys == NULL || keys[0] == NULL) {
662 error("sc_get_keys failed");
663 goto send;
664 }
665 for (i = 0; keys[i] != NULL; i++) {
666 k = keys[i];
667 version = k->type == KEY_RSA1 ? 1 : 2;
668 if ((id = lookup_identity(k, version)) != NULL) {
669 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000670 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000671 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100672 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000673 success = 1;
674 }
675 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000676 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000678 xfree(keys);
679send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000680 buffer_put_int(&e->output, 1);
681 buffer_put_char(&e->output,
682 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
683}
Ben Lindstromffce1472001-08-06 21:57:31 +0000684#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000685
Damien Millerad833b32000-08-23 10:46:23 +1000686/* dispatch incoming messages */
687
Ben Lindstrombba81212001-06-25 05:01:22 +0000688static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000689process_message(SocketEntry *e)
690{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000691 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000692 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000693
694 /* kill dead keys */
695 reaper();
696
Damien Miller95def091999-11-25 00:26:21 +1100697 if (buffer_len(&e->input) < 5)
698 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100699 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100700 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100701 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000702 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100703 return;
704 }
705 if (buffer_len(&e->input) < msg_len + 4)
706 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000707
708 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100709 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000710 buffer_clear(&e->request);
711 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
712 buffer_consume(&e->input, msg_len);
713 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000714
Ben Lindstrom2f717042002-06-06 21:52:03 +0000715 /* check wheter agent is locked */
716 if (locked && type != SSH_AGENTC_UNLOCK) {
717 buffer_clear(&e->request);
718 switch (type) {
719 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
720 case SSH2_AGENTC_REQUEST_IDENTITIES:
721 /* send empty lists */
722 no_identities(e, type);
723 break;
724 default:
725 /* send a fail message for all other request types */
726 buffer_put_int(&e->output, 1);
727 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
728 }
729 return;
730 }
731
Ben Lindstrom3f471632001-07-04 03:53:15 +0000732 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100733 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000734 case SSH_AGENTC_LOCK:
735 case SSH_AGENTC_UNLOCK:
736 process_lock_agent(e, type == SSH_AGENTC_LOCK);
737 break;
Damien Millerad833b32000-08-23 10:46:23 +1000738 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100739 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000740 process_authentication_challenge1(e);
741 break;
742 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
743 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100744 break;
745 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000746 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000747 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100748 break;
749 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000750 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100751 break;
752 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000753 process_remove_all_identities(e, 1);
754 break;
755 /* ssh2 */
756 case SSH2_AGENTC_SIGN_REQUEST:
757 process_sign_request2(e);
758 break;
759 case SSH2_AGENTC_REQUEST_IDENTITIES:
760 process_request_identities(e, 2);
761 break;
762 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000763 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000764 process_add_identity(e, 2);
765 break;
766 case SSH2_AGENTC_REMOVE_IDENTITY:
767 process_remove_identity(e, 2);
768 break;
769 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
770 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100771 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000772#ifdef SMARTCARD
773 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000774 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000775 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100776 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000777 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
778 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100779 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000780#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100781 default:
782 /* Unknown message. Respond with failure. */
783 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000784 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100785 buffer_put_int(&e->output, 1);
786 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
787 break;
788 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000792new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000794 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000795
Damien Miller232711f2004-06-15 10:35:30 +1000796 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797
Damien Miller95def091999-11-25 00:26:21 +1100798 if (fd > max_fd)
799 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800
Damien Miller95def091999-11-25 00:26:21 +1100801 for (i = 0; i < sockets_alloc; i++)
802 if (sockets[i].type == AUTH_UNUSED) {
803 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100804 buffer_init(&sockets[i].input);
805 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000806 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000807 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100808 return;
809 }
810 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100812 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100814 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000815 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100816 sockets[old_alloc].fd = fd;
817 buffer_init(&sockets[old_alloc].input);
818 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000819 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000820 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821}
822
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000824prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000826 u_int i, sz;
827 int n = 0;
828
829 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100830 switch (sockets[i].type) {
831 case AUTH_SOCKET:
832 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000833 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100834 break;
835 case AUTH_UNUSED:
836 break;
837 default:
838 fatal("Unknown socket type %d", sockets[i].type);
839 break;
840 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000841 }
842
843 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000844 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000846 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000848 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000849 *fdrp = xmalloc(sz);
850 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000851 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000853 if (n < *fdl)
854 debug("XXX shrink: %d < %d", n, *fdl);
855 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000856 memset(*fdrp, 0, sz);
857 memset(*fdwp, 0, sz);
858
859 for (i = 0; i < sockets_alloc; i++) {
860 switch (sockets[i].type) {
861 case AUTH_SOCKET:
862 case AUTH_CONNECTION:
863 FD_SET(sockets[i].fd, *fdrp);
864 if (buffer_len(&sockets[i].output) > 0)
865 FD_SET(sockets[i].fd, *fdwp);
866 break;
867 default:
868 break;
869 }
870 }
871 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872}
873
Ben Lindstrombba81212001-06-25 05:01:22 +0000874static void
Damien Miller95def091999-11-25 00:26:21 +1100875after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000877 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100878 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100879 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000880 int len, sock;
881 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000882 uid_t euid;
883 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000884
Damien Miller95def091999-11-25 00:26:21 +1100885 for (i = 0; i < sockets_alloc; i++)
886 switch (sockets[i].type) {
887 case AUTH_UNUSED:
888 break;
889 case AUTH_SOCKET:
890 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100891 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000892 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100893 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100894 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100895 error("accept from AUTH_SOCKET: %s",
896 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100897 break;
898 }
Damien Miller538f1812002-09-12 09:51:10 +1000899 if (getpeereid(sock, &euid, &egid) < 0) {
900 error("getpeereid %d failed: %s",
901 sock, strerror(errno));
902 close(sock);
903 break;
904 }
Damien Milleraf9de382002-10-03 11:54:35 +1000905 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000906 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000907 "peer euid %u != uid %u",
908 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000909 close(sock);
910 break;
911 }
Damien Miller95def091999-11-25 00:26:21 +1100912 new_socket(AUTH_CONNECTION, sock);
913 }
914 break;
915 case AUTH_CONNECTION:
916 if (buffer_len(&sockets[i].output) > 0 &&
917 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000918 do {
919 len = write(sockets[i].fd,
920 buffer_ptr(&sockets[i].output),
921 buffer_len(&sockets[i].output));
922 if (len == -1 && (errno == EAGAIN ||
923 errno == EINTR))
924 continue;
925 break;
926 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100927 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000928 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100929 break;
930 }
931 buffer_consume(&sockets[i].output, len);
932 }
933 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000934 do {
935 len = read(sockets[i].fd, buf, sizeof(buf));
936 if (len == -1 && (errno == EAGAIN ||
937 errno == EINTR))
938 continue;
939 break;
940 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100941 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000942 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100943 break;
944 }
945 buffer_append(&sockets[i].input, buf, len);
946 process_message(&sockets[i]);
947 }
948 break;
949 default:
950 fatal("Unknown type %d", sockets[i].type);
951 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000952}
953
Ben Lindstrombba81212001-06-25 05:01:22 +0000954static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000955cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000956{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000957 if (socket_name[0])
958 unlink(socket_name);
959 if (socket_dir[0])
960 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000961}
962
Darren Tucker3e33cec2003-10-02 16:12:36 +1000963void
Damien Miller792c5111999-10-29 09:47:09 +1000964cleanup_exit(int i)
965{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000966 cleanup_socket();
967 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000968}
969
Damien Miller1c13bd82006-03-26 14:28:14 +1100970/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000971static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000972cleanup_handler(int sig)
973{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000974 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000975 _exit(2);
976}
977
Damien Miller1c13bd82006-03-26 14:28:14 +1100978/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000979static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000980check_parent_exists(int sig)
981{
982 int save_errno = errno;
983
984 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
985 /* printf("Parent has died - Authentication agent exiting.\n"); */
986 cleanup_handler(sig); /* safe */
987 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000988 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000989 alarm(10);
990 errno = save_errno;
991}
992
993static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000994usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000995{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000996 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000997 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000998 fprintf(stderr, "Options:\n");
999 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1000 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1001 fprintf(stderr, " -k Kill the current agent.\n");
1002 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001003 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001004 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001005 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001006}
1007
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008int
1009main(int ac, char **av)
1010{
Damien Miller6c711792003-01-24 11:36:23 +11001011 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001012 int sock, fd, ch;
1013 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001014 char *shell, *format, *pidstr, *agentsocket = NULL;
1015 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001016 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001017#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001018 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001019#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001020 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001021 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001022 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001023 pid_t pid;
1024 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001025
Darren Tuckerce321d82005-10-03 18:11:24 +10001026 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1027 sanitise_stdfd();
1028
Damien Miller6cffb9a2002-09-04 16:20:26 +10001029 /* drop */
1030 setegid(getgid());
1031 setgid(getgid());
1032
Damien Miller6c4914a2004-03-03 11:08:59 +11001033#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1034 /* Disable ptrace on Linux without sgid bit */
1035 prctl(PR_SET_DUMPABLE, 0);
1036#endif
1037
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001038 SSLeay_add_all_algorithms();
1039
Damien Miller59d3d5b2003-08-22 09:34:41 +10001040 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001041 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001042 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001043
Damien Miller53d81482003-01-22 11:47:19 +11001044 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001045 switch (ch) {
1046 case 'c':
1047 if (s_flag)
1048 usage();
1049 c_flag++;
1050 break;
1051 case 'k':
1052 k_flag++;
1053 break;
1054 case 's':
1055 if (c_flag)
1056 usage();
1057 s_flag++;
1058 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001059 case 'd':
1060 if (d_flag)
1061 usage();
1062 d_flag++;
1063 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001064 case 'a':
1065 agentsocket = optarg;
1066 break;
Damien Miller53d81482003-01-22 11:47:19 +11001067 case 't':
1068 if ((lifetime = convtime(optarg)) == -1) {
1069 fprintf(stderr, "Invalid lifetime\n");
1070 usage();
1071 }
1072 break;
Damien Miller95def091999-11-25 00:26:21 +11001073 default:
1074 usage();
1075 }
Damien Miller792c5111999-10-29 09:47:09 +10001076 }
Damien Miller95def091999-11-25 00:26:21 +11001077 ac -= optind;
1078 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001079
Ben Lindstromd94580c2001-07-04 03:48:02 +00001080 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001081 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001082
Ben Lindstromeecdf232002-04-02 21:03:51 +00001083 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001084 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001085 if (shell != NULL &&
1086 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001087 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001088 }
Damien Miller95def091999-11-25 00:26:21 +11001089 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001090 const char *errstr = NULL;
1091
Damien Miller95def091999-11-25 00:26:21 +11001092 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1093 if (pidstr == NULL) {
1094 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001095 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001096 exit(1);
1097 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001098 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1099 if (errstr) {
1100 fprintf(stderr,
1101 "%s=\"%s\", which is not a good PID: %s\n",
1102 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001103 exit(1);
1104 }
1105 if (kill(pid, SIGTERM) == -1) {
1106 perror("kill");
1107 exit(1);
1108 }
1109 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1110 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1111 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001112 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001113 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001114 }
Damien Miller95def091999-11-25 00:26:21 +11001115 parent_pid = getpid();
1116
Ben Lindstromb7788f32002-06-06 21:46:08 +00001117 if (agentsocket == NULL) {
1118 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001119 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001120 if (mkdtemp(socket_dir) == NULL) {
1121 perror("mkdtemp: private socket dir");
1122 exit(1);
1123 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001124 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1125 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001126 } else {
1127 /* Try to use specified agent socket */
1128 socket_dir[0] = '\0';
1129 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001130 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131
Damien Miller5428f641999-11-25 11:54:57 +11001132 /*
1133 * Create socket early so it will exist before command gets run from
1134 * the parent.
1135 */
Damien Miller95def091999-11-25 00:26:21 +11001136 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1137 if (sock < 0) {
1138 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001139 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001140 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001141 }
Damien Miller95def091999-11-25 00:26:21 +11001142 memset(&sunaddr, 0, sizeof(sunaddr));
1143 sunaddr.sun_family = AF_UNIX;
1144 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001145 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001146 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001147 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001148 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001149 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001150 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001151 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001152 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001153 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001154 perror("listen");
1155 cleanup_exit(1);
1156 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001157
Damien Miller5428f641999-11-25 11:54:57 +11001158 /*
1159 * Fork, and have the parent execute the command, if any, or present
1160 * the socket data. The child continues as the authentication agent.
1161 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001162 if (d_flag) {
1163 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1164 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1165 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1166 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001167 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001168 goto skip;
1169 }
Damien Miller95def091999-11-25 00:26:21 +11001170 pid = fork();
1171 if (pid == -1) {
1172 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001173 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001174 }
1175 if (pid != 0) { /* Parent - execute the given command. */
1176 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001177 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001178 if (ac == 0) {
1179 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1180 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001181 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001182 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001183 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001184 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001185 exit(0);
1186 }
Damien Millere4340be2000-09-16 13:29:08 +11001187 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1188 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1189 perror("setenv");
1190 exit(1);
1191 }
Damien Miller95def091999-11-25 00:26:21 +11001192 execvp(av[0], av);
1193 perror(av[0]);
1194 exit(1);
1195 }
Damien Millerc653b892002-02-08 22:05:41 +11001196 /* child */
1197 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001198
1199 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001200 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001201 cleanup_exit(1);
1202 }
1203
1204 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001205 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1206 /* XXX might close listen socket */
1207 (void)dup2(fd, STDIN_FILENO);
1208 (void)dup2(fd, STDOUT_FILENO);
1209 (void)dup2(fd, STDERR_FILENO);
1210 if (fd > 2)
1211 close(fd);
1212 }
Damien Miller95def091999-11-25 00:26:21 +11001213
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001214#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001215 /* deny core dumps, since memory contains unencrypted private keys */
1216 rlim.rlim_cur = rlim.rlim_max = 0;
1217 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001218 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001219 cleanup_exit(1);
1220 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001221#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001222
1223skip:
Damien Miller95def091999-11-25 00:26:21 +11001224 new_socket(AUTH_SOCKET, sock);
1225 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001226 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001227 alarm(10);
1228 }
Damien Millerad833b32000-08-23 10:46:23 +10001229 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001230 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001231 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001232 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001233 signal(SIGHUP, cleanup_handler);
1234 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001235 nalloc = 0;
1236
Damien Miller95def091999-11-25 00:26:21 +11001237 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001238 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001239 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001240 if (errno == EINTR)
1241 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001242 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001243 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001244 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001245 }
1246 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}