blob: 621b09771685828cd3e786569ea1c5d7e59f934f [file] [log] [blame]
Damien Millere6b3b612006-07-24 14:01:23 +10001/* $OpenBSD: ssh-agent.c,v 1.144 2006/07/17 01:31:10 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 Millere6b3b612006-07-24 14:01:23 +100054#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110055
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include <openssl/evp.h>
57#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
59#include "ssh.h"
60#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "buffer.h"
62#include "bufaux.h"
63#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100064#include "key.h"
65#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110066#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110068#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Ben Lindstrom3f471632001-07-04 03:53:15 +000070#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000071#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000072#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000073
Damien Miller6c4914a2004-03-03 11:08:59 +110074#if defined(HAVE_SYS_PRCTL_H)
75#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
76#endif
77
Ben Lindstrom65366a82001-12-06 16:32:47 +000078typedef enum {
79 AUTH_UNUSED,
80 AUTH_SOCKET,
81 AUTH_CONNECTION
82} sock_type;
83
Damien Miller95def091999-11-25 00:26:21 +110084typedef struct {
85 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000086 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110087 Buffer input;
88 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000089 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090} SocketEntry;
91
Ben Lindstrom46c16222000-12-22 01:43:59 +000092u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093SocketEntry *sockets = NULL;
94
Damien Miller1a534ae2002-01-22 23:26:13 +110095typedef struct identity {
96 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100097 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110098 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000099 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100100 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101} Identity;
102
Damien Millerad833b32000-08-23 10:46:23 +1000103typedef struct {
104 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100105 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000106} Idtab;
107
108/* private key table, one per protocol version */
109Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111int max_fd = 0;
112
113/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000114pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
116/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100117char socket_name[MAXPATHLEN];
118char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Ben Lindstrom2f717042002-06-06 21:52:03 +0000120/* locking */
121int locked = 0;
122char *lock_passwd = NULL;
123
Damien Miller95def091999-11-25 00:26:21 +1100124extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100125
Damien Miller53d81482003-01-22 11:47:19 +1100126/* Default lifetime (0 == forever) */
127static int lifetime = 0;
128
Ben Lindstrombba81212001-06-25 05:01:22 +0000129static void
Damien Miller58f34862002-09-04 16:31:21 +1000130close_socket(SocketEntry *e)
131{
Damien Miller58f34862002-09-04 16:31:21 +1000132 close(e->fd);
133 e->fd = -1;
134 e->type = AUTH_UNUSED;
135 buffer_free(&e->input);
136 buffer_free(&e->output);
137 buffer_free(&e->request);
138}
139
140static void
Damien Millerad833b32000-08-23 10:46:23 +1000141idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Millerad833b32000-08-23 10:46:23 +1000143 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000144
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000145 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100146 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000147 idtable[i].nentries = 0;
148 }
149}
150
151/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000152static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000153idtab_lookup(int version)
154{
155 if (version < 1 || version > 2)
156 fatal("internal error, bad protocol version %d", version);
157 return &idtable[version];
158}
159
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000160static void
161free_identity(Identity *id)
162{
163 key_free(id->key);
164 xfree(id->comment);
165 xfree(id);
166}
167
Damien Millerad833b32000-08-23 10:46:23 +1000168/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100169static Identity *
170lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000171{
Damien Miller1a534ae2002-01-22 23:26:13 +1100172 Identity *id;
173
Damien Millerad833b32000-08-23 10:46:23 +1000174 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100175 TAILQ_FOREACH(id, &tab->idlist, next) {
176 if (key_equal(key, id->key))
177 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000178 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100179 return (NULL);
180}
181
Damien Miller6c711792003-01-24 11:36:23 +1100182/* Check confirmation of keysign request */
183static int
184confirm_key(Identity *id)
185{
Darren Tuckerce327b62004-11-05 20:38:03 +1100186 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100187 int ret = -1;
188
189 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100190 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
191 id->comment, p))
192 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100193 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100194
Damien Miller6c711792003-01-24 11:36:23 +1100195 return (ret);
196}
197
Damien Millerad833b32000-08-23 10:46:23 +1000198/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000199static void
Damien Millerad833b32000-08-23 10:46:23 +1000200process_request_identities(SocketEntry *e, int version)
201{
202 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100203 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000204 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205
Damien Miller95def091999-11-25 00:26:21 +1100206 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000207 buffer_put_char(&msg, (version == 1) ?
208 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
209 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100210 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000212 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
213 buffer_put_bignum(&msg, id->key->rsa->e);
214 buffer_put_bignum(&msg, id->key->rsa->n);
215 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000216 u_char *blob;
217 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100218 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000219 buffer_put_string(&msg, blob, blen);
220 xfree(blob);
221 }
222 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100223 }
224 buffer_put_int(&e->output, buffer_len(&msg));
225 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
226 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227}
228
Damien Millerad833b32000-08-23 10:46:23 +1000229/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000230static void
Damien Millerad833b32000-08-23 10:46:23 +1000231process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000233 u_char buf[32], mdbuf[16], session_id[16];
234 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000235 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000236 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000237 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100238 Buffer msg;
239 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000240 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
Damien Miller95def091999-11-25 00:26:21 +1100242 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100243 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100244 if ((challenge = BN_new()) == NULL)
245 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000246
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000247 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000248 buffer_get_bignum(&e->request, key->rsa->e);
249 buffer_get_bignum(&e->request, key->rsa->n);
250 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Millerad833b32000-08-23 10:46:23 +1000252 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000253 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000254 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000255 buffer_get(&e->request, session_id, 16);
256 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000257 if (response_type != 1)
258 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller1a534ae2002-01-22 23:26:13 +1100260 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100261 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100262 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000263 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100264 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
265 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100266
Damien Millerad833b32000-08-23 10:46:23 +1000267 /* The response is MD5 of decrypted challenge plus session id. */
268 len = BN_num_bytes(challenge);
269 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000270 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000271 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100272 }
Damien Millerad833b32000-08-23 10:46:23 +1000273 memset(buf, 0, 32);
274 BN_bn2bin(challenge, buf + 32 - len);
275 MD5_Init(&md);
276 MD5_Update(&md, buf, 32);
277 MD5_Update(&md, session_id, 16);
278 MD5_Final(mdbuf, &md);
279
280 /* Send the response. */
281 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
282 for (i = 0; i < 16; i++)
283 buffer_put_char(&msg, mdbuf[i]);
284 goto send;
285 }
286
287failure:
288 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100289 buffer_put_char(&msg, SSH_AGENT_FAILURE);
290send:
291 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000292 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
293 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100294 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000295 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296}
297
Damien Millerad833b32000-08-23 10:46:23 +1000298/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000299static void
Damien Millerad833b32000-08-23 10:46:23 +1000300process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000302 u_char *blob, *data, *signature = NULL;
303 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000304 extern int datafellows;
305 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000306 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000307 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000308
309 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000310
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000311 blob = buffer_get_string(&e->request, &blen);
312 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100313
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000314 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100315 if (flags & SSH_AGENT_OLD_SIGNATURE)
316 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000317
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000319 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100320 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100321 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100322 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000323 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000324 }
Damien Millerad833b32000-08-23 10:46:23 +1000325 buffer_init(&msg);
326 if (ok == 0) {
327 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
328 buffer_put_string(&msg, signature, slen);
329 } else {
330 buffer_put_char(&msg, SSH_AGENT_FAILURE);
331 }
332 buffer_put_int(&e->output, buffer_len(&msg));
333 buffer_append(&e->output, buffer_ptr(&msg),
334 buffer_len(&msg));
335 buffer_free(&msg);
336 xfree(data);
337 xfree(blob);
338 if (signature != NULL)
339 xfree(signature);
340}
341
342/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000343static void
Damien Millerad833b32000-08-23 10:46:23 +1000344process_remove_identity(SocketEntry *e, int version)
345{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000346 u_int blen, bits;
347 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100348 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000349 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100350
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000351 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000352 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100353 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000354 bits = buffer_get_int(&e->request);
355 buffer_get_bignum(&e->request, key->rsa->e);
356 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357
Damien Millerad833b32000-08-23 10:46:23 +1000358 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000359 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000360 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000361 break;
362 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000363 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100364 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000365 xfree(blob);
366 break;
367 }
368 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100369 Identity *id = lookup_identity(key, version);
370 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100371 /*
372 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100373 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000374 * the array, we actually free the key there and move
375 * all the entries between the empty slot and the end
376 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100377 */
Damien Millerad833b32000-08-23 10:46:23 +1000378 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379 if (tab->nentries < 1)
380 fatal("process_remove_identity: "
381 "internal error: tab->nentries %d",
382 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100383 TAILQ_REMOVE(&tab->idlist, id, next);
384 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000385 tab->nentries--;
386 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100387 }
Damien Millerad833b32000-08-23 10:46:23 +1000388 key_free(key);
389 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000391 buffer_put_char(&e->output,
392 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393}
394
Ben Lindstrombba81212001-06-25 05:01:22 +0000395static void
Damien Millerad833b32000-08-23 10:46:23 +1000396process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397{
Damien Millerad833b32000-08-23 10:46:23 +1000398 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100399 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100402 for (id = TAILQ_FIRST(&tab->idlist); id;
403 id = TAILQ_FIRST(&tab->idlist)) {
404 TAILQ_REMOVE(&tab->idlist, id, next);
405 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100406 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Damien Miller95def091999-11-25 00:26:21 +1100408 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000409 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
411 /* Send success. */
412 buffer_put_int(&e->output, 1);
413 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100414}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000417reaper(void)
418{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000419 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000420 Identity *id, *nxt;
421 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000422 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000423
424 for (version = 1; version < 3; version++) {
425 tab = idtab_lookup(version);
426 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
427 nxt = TAILQ_NEXT(id, next);
428 if (id->death != 0 && now >= id->death) {
429 TAILQ_REMOVE(&tab->idlist, id, next);
430 free_identity(id);
431 tab->nentries--;
432 }
433 }
434 }
435}
436
437static void
Damien Millerad833b32000-08-23 10:46:23 +1000438process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100439{
Damien Millerad833b32000-08-23 10:46:23 +1000440 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100441 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000442 char *type_name, *comment;
443 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100444
Damien Millerad833b32000-08-23 10:46:23 +1000445 switch (version) {
446 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100447 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000448 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000449 buffer_get_bignum(&e->request, k->rsa->n);
450 buffer_get_bignum(&e->request, k->rsa->e);
451 buffer_get_bignum(&e->request, k->rsa->d);
452 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100453
Damien Millerad833b32000-08-23 10:46:23 +1000454 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000455 buffer_get_bignum(&e->request, k->rsa->q); /* p */
456 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100457
Damien Millerad833b32000-08-23 10:46:23 +1000458 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000459 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000460 break;
461 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000462 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000463 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000465 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 case KEY_DSA:
467 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000468 buffer_get_bignum2(&e->request, k->dsa->p);
469 buffer_get_bignum2(&e->request, k->dsa->q);
470 buffer_get_bignum2(&e->request, k->dsa->g);
471 buffer_get_bignum2(&e->request, k->dsa->pub_key);
472 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 break;
474 case KEY_RSA:
475 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000476 buffer_get_bignum2(&e->request, k->rsa->n);
477 buffer_get_bignum2(&e->request, k->rsa->e);
478 buffer_get_bignum2(&e->request, k->rsa->d);
479 buffer_get_bignum2(&e->request, k->rsa->iqmp);
480 buffer_get_bignum2(&e->request, k->rsa->p);
481 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482
483 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000484 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 break;
486 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000487 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000488 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100489 }
Damien Millerad833b32000-08-23 10:46:23 +1000490 break;
491 }
Damien Millerc51d0732003-03-15 11:37:09 +1100492 /* enable blinding */
493 switch (k->type) {
494 case KEY_RSA:
495 case KEY_RSA1:
496 if (RSA_blinding_on(k->rsa, NULL) != 1) {
497 error("process_add_identity: RSA_blinding_on failed");
498 key_free(k);
499 goto send;
500 }
501 break;
502 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000503 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000504 if (k == NULL) {
505 xfree(comment);
506 goto send;
507 }
508 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000509 while (buffer_len(&e->request)) {
510 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000511 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000512 death = time(NULL) + buffer_get_int(&e->request);
513 break;
Damien Miller6c711792003-01-24 11:36:23 +1100514 case SSH_AGENT_CONSTRAIN_CONFIRM:
515 confirm = 1;
516 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000517 default:
518 break;
519 }
520 }
Damien Miller53d81482003-01-22 11:47:19 +1100521 if (lifetime && !death)
522 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000523 if (lookup_identity(k, version) == NULL) {
524 Identity *id = xmalloc(sizeof(Identity));
525 id->key = k;
526 id->comment = comment;
527 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100528 id->confirm = confirm;
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);
534 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000535 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000536send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000537 buffer_put_int(&e->output, 1);
538 buffer_put_char(&e->output,
539 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
540}
541
Ben Lindstrom2f717042002-06-06 21:52:03 +0000542/* XXX todo: encrypt sensitive data with passphrase */
543static void
544process_lock_agent(SocketEntry *e, int lock)
545{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000546 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000547 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000548
549 passwd = buffer_get_string(&e->request, NULL);
550 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
551 locked = 0;
552 memset(lock_passwd, 0, strlen(lock_passwd));
553 xfree(lock_passwd);
554 lock_passwd = NULL;
555 success = 1;
556 } else if (!locked && lock) {
557 locked = 1;
558 lock_passwd = xstrdup(passwd);
559 success = 1;
560 }
561 memset(passwd, 0, strlen(passwd));
562 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000563
Ben Lindstrom2f717042002-06-06 21:52:03 +0000564 buffer_put_int(&e->output, 1);
565 buffer_put_char(&e->output,
566 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000567}
568
569static void
570no_identities(SocketEntry *e, u_int type)
571{
572 Buffer msg;
573
574 buffer_init(&msg);
575 buffer_put_char(&msg,
576 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
577 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
578 buffer_put_int(&msg, 0);
579 buffer_put_int(&e->output, buffer_len(&msg));
580 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
581 buffer_free(&msg);
582}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000583
584#ifdef SMARTCARD
585static void
586process_add_smartcard_key (SocketEntry *e)
587{
Ben Lindstromba72d302002-03-22 03:51:06 +0000588 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000589 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000590 Key **keys, *k;
591 Identity *id;
592 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100593
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000594 sc_reader_id = buffer_get_string(&e->request, NULL);
595 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000596
597 while (buffer_len(&e->request)) {
598 switch (buffer_get_char(&e->request)) {
599 case SSH_AGENT_CONSTRAIN_LIFETIME:
600 death = time(NULL) + buffer_get_int(&e->request);
601 break;
602 case SSH_AGENT_CONSTRAIN_CONFIRM:
603 confirm = 1;
604 break;
605 default:
606 break;
607 }
608 }
609 if (lifetime && !death)
610 death = time(NULL) + lifetime;
611
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000612 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000613 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000614 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000616 if (keys == NULL || keys[0] == NULL) {
617 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000618 goto send;
619 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000620 for (i = 0; keys[i] != NULL; i++) {
621 k = keys[i];
622 version = k->type == KEY_RSA1 ? 1 : 2;
623 tab = idtab_lookup(version);
624 if (lookup_identity(k, version) == NULL) {
625 id = xmalloc(sizeof(Identity));
626 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000627 id->comment = sc_get_key_label(k);
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 Lindstrom0936a5b2002-03-26 03:17:42 +0000638 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000639send:
640 buffer_put_int(&e->output, 1);
641 buffer_put_char(&e->output,
642 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
643}
644
645static void
646process_remove_smartcard_key(SocketEntry *e)
647{
Ben Lindstromba72d302002-03-22 03:51:06 +0000648 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000649 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000650 Key **keys, *k = NULL;
651 Identity *id;
652 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000653
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000654 sc_reader_id = buffer_get_string(&e->request, NULL);
655 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000656 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000657 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000658 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000659
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000660 if (keys == NULL || keys[0] == NULL) {
661 error("sc_get_keys failed");
662 goto send;
663 }
664 for (i = 0; keys[i] != NULL; i++) {
665 k = keys[i];
666 version = k->type == KEY_RSA1 ? 1 : 2;
667 if ((id = lookup_identity(k, version)) != NULL) {
668 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000669 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000670 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100671 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000672 success = 1;
673 }
674 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000675 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000676 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000677 xfree(keys);
678send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000679 buffer_put_int(&e->output, 1);
680 buffer_put_char(&e->output,
681 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
682}
Ben Lindstromffce1472001-08-06 21:57:31 +0000683#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000684
Damien Millerad833b32000-08-23 10:46:23 +1000685/* dispatch incoming messages */
686
Ben Lindstrombba81212001-06-25 05:01:22 +0000687static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000688process_message(SocketEntry *e)
689{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000690 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000691 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000692
693 /* kill dead keys */
694 reaper();
695
Damien Miller95def091999-11-25 00:26:21 +1100696 if (buffer_len(&e->input) < 5)
697 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100698 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100699 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100700 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000701 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100702 return;
703 }
704 if (buffer_len(&e->input) < msg_len + 4)
705 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000706
707 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100708 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000709 buffer_clear(&e->request);
710 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
711 buffer_consume(&e->input, msg_len);
712 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000713
Ben Lindstrom2f717042002-06-06 21:52:03 +0000714 /* check wheter agent is locked */
715 if (locked && type != SSH_AGENTC_UNLOCK) {
716 buffer_clear(&e->request);
717 switch (type) {
718 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
719 case SSH2_AGENTC_REQUEST_IDENTITIES:
720 /* send empty lists */
721 no_identities(e, type);
722 break;
723 default:
724 /* send a fail message for all other request types */
725 buffer_put_int(&e->output, 1);
726 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
727 }
728 return;
729 }
730
Ben Lindstrom3f471632001-07-04 03:53:15 +0000731 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100732 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000733 case SSH_AGENTC_LOCK:
734 case SSH_AGENTC_UNLOCK:
735 process_lock_agent(e, type == SSH_AGENTC_LOCK);
736 break;
Damien Millerad833b32000-08-23 10:46:23 +1000737 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100738 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000739 process_authentication_challenge1(e);
740 break;
741 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
742 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100743 break;
744 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000745 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000746 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100747 break;
748 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000749 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100750 break;
751 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000752 process_remove_all_identities(e, 1);
753 break;
754 /* ssh2 */
755 case SSH2_AGENTC_SIGN_REQUEST:
756 process_sign_request2(e);
757 break;
758 case SSH2_AGENTC_REQUEST_IDENTITIES:
759 process_request_identities(e, 2);
760 break;
761 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000762 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000763 process_add_identity(e, 2);
764 break;
765 case SSH2_AGENTC_REMOVE_IDENTITY:
766 process_remove_identity(e, 2);
767 break;
768 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
769 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100770 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000771#ifdef SMARTCARD
772 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000773 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000774 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100775 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000776 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
777 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100778 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000779#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100780 default:
781 /* Unknown message. Respond with failure. */
782 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000783 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100784 buffer_put_int(&e->output, 1);
785 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
786 break;
787 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000788}
789
Ben Lindstrombba81212001-06-25 05:01:22 +0000790static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000791new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000793 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000794
Damien Miller232711f2004-06-15 10:35:30 +1000795 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Miller95def091999-11-25 00:26:21 +1100797 if (fd > max_fd)
798 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799
Damien Miller95def091999-11-25 00:26:21 +1100800 for (i = 0; i < sockets_alloc; i++)
801 if (sockets[i].type == AUTH_UNUSED) {
802 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100803 buffer_init(&sockets[i].input);
804 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000805 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000806 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100807 return;
808 }
809 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000810 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100811 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000812 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100813 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000814 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100815 sockets[old_alloc].fd = fd;
816 buffer_init(&sockets[old_alloc].input);
817 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000818 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000819 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820}
821
Ben Lindstrombba81212001-06-25 05:01:22 +0000822static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000823prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000824{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000825 u_int i, sz;
826 int n = 0;
827
828 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100829 switch (sockets[i].type) {
830 case AUTH_SOCKET:
831 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000832 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100833 break;
834 case AUTH_UNUSED:
835 break;
836 default:
837 fatal("Unknown socket type %d", sockets[i].type);
838 break;
839 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000840 }
841
842 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000843 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000844 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000845 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000847 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000848 *fdrp = xmalloc(sz);
849 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000850 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000851 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000852 if (n < *fdl)
853 debug("XXX shrink: %d < %d", n, *fdl);
854 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000855 memset(*fdrp, 0, sz);
856 memset(*fdwp, 0, sz);
857
858 for (i = 0; i < sockets_alloc; i++) {
859 switch (sockets[i].type) {
860 case AUTH_SOCKET:
861 case AUTH_CONNECTION:
862 FD_SET(sockets[i].fd, *fdrp);
863 if (buffer_len(&sockets[i].output) > 0)
864 FD_SET(sockets[i].fd, *fdwp);
865 break;
866 default:
867 break;
868 }
869 }
870 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871}
872
Ben Lindstrombba81212001-06-25 05:01:22 +0000873static void
Damien Miller95def091999-11-25 00:26:21 +1100874after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000876 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100877 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100878 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000879 int len, sock;
880 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000881 uid_t euid;
882 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883
Damien Miller95def091999-11-25 00:26:21 +1100884 for (i = 0; i < sockets_alloc; i++)
885 switch (sockets[i].type) {
886 case AUTH_UNUSED:
887 break;
888 case AUTH_SOCKET:
889 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100890 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000891 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100892 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100893 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100894 error("accept from AUTH_SOCKET: %s",
895 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100896 break;
897 }
Damien Miller538f1812002-09-12 09:51:10 +1000898 if (getpeereid(sock, &euid, &egid) < 0) {
899 error("getpeereid %d failed: %s",
900 sock, strerror(errno));
901 close(sock);
902 break;
903 }
Damien Milleraf9de382002-10-03 11:54:35 +1000904 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000905 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000906 "peer euid %u != uid %u",
907 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000908 close(sock);
909 break;
910 }
Damien Miller95def091999-11-25 00:26:21 +1100911 new_socket(AUTH_CONNECTION, sock);
912 }
913 break;
914 case AUTH_CONNECTION:
915 if (buffer_len(&sockets[i].output) > 0 &&
916 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000917 do {
918 len = write(sockets[i].fd,
919 buffer_ptr(&sockets[i].output),
920 buffer_len(&sockets[i].output));
921 if (len == -1 && (errno == EAGAIN ||
922 errno == EINTR))
923 continue;
924 break;
925 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100926 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000927 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100928 break;
929 }
930 buffer_consume(&sockets[i].output, len);
931 }
932 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000933 do {
934 len = read(sockets[i].fd, buf, sizeof(buf));
935 if (len == -1 && (errno == EAGAIN ||
936 errno == EINTR))
937 continue;
938 break;
939 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100940 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000941 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100942 break;
943 }
944 buffer_append(&sockets[i].input, buf, len);
945 process_message(&sockets[i]);
946 }
947 break;
948 default:
949 fatal("Unknown type %d", sockets[i].type);
950 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000951}
952
Ben Lindstrombba81212001-06-25 05:01:22 +0000953static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000954cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000955{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000956 if (socket_name[0])
957 unlink(socket_name);
958 if (socket_dir[0])
959 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000960}
961
Darren Tucker3e33cec2003-10-02 16:12:36 +1000962void
Damien Miller792c5111999-10-29 09:47:09 +1000963cleanup_exit(int i)
964{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000965 cleanup_socket();
966 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000967}
968
Damien Miller1c13bd82006-03-26 14:28:14 +1100969/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000970static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000971cleanup_handler(int sig)
972{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000973 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000974 _exit(2);
975}
976
Damien Miller1c13bd82006-03-26 14:28:14 +1100977/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000978static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000979check_parent_exists(int sig)
980{
981 int save_errno = errno;
982
983 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
984 /* printf("Parent has died - Authentication agent exiting.\n"); */
985 cleanup_handler(sig); /* safe */
986 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000987 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000988 alarm(10);
989 errno = save_errno;
990}
991
992static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000993usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000994{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000995 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000996 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000997 fprintf(stderr, "Options:\n");
998 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
999 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1000 fprintf(stderr, " -k Kill the current agent.\n");
1001 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001002 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001003 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001004 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001005}
1006
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001007int
1008main(int ac, char **av)
1009{
Damien Miller6c711792003-01-24 11:36:23 +11001010 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001011 int sock, fd, ch;
1012 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001013 char *shell, *format, *pidstr, *agentsocket = NULL;
1014 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001015 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001016#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001017 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001018#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001019 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001020 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001021 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001022 pid_t pid;
1023 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001024
Darren Tuckerce321d82005-10-03 18:11:24 +10001025 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1026 sanitise_stdfd();
1027
Damien Miller6cffb9a2002-09-04 16:20:26 +10001028 /* drop */
1029 setegid(getgid());
1030 setgid(getgid());
1031
Damien Miller6c4914a2004-03-03 11:08:59 +11001032#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1033 /* Disable ptrace on Linux without sgid bit */
1034 prctl(PR_SET_DUMPABLE, 0);
1035#endif
1036
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001037 SSLeay_add_all_algorithms();
1038
Damien Miller59d3d5b2003-08-22 09:34:41 +10001039 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001040 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001041 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001042
Damien Miller53d81482003-01-22 11:47:19 +11001043 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001044 switch (ch) {
1045 case 'c':
1046 if (s_flag)
1047 usage();
1048 c_flag++;
1049 break;
1050 case 'k':
1051 k_flag++;
1052 break;
1053 case 's':
1054 if (c_flag)
1055 usage();
1056 s_flag++;
1057 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001058 case 'd':
1059 if (d_flag)
1060 usage();
1061 d_flag++;
1062 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001063 case 'a':
1064 agentsocket = optarg;
1065 break;
Damien Miller53d81482003-01-22 11:47:19 +11001066 case 't':
1067 if ((lifetime = convtime(optarg)) == -1) {
1068 fprintf(stderr, "Invalid lifetime\n");
1069 usage();
1070 }
1071 break;
Damien Miller95def091999-11-25 00:26:21 +11001072 default:
1073 usage();
1074 }
Damien Miller792c5111999-10-29 09:47:09 +10001075 }
Damien Miller95def091999-11-25 00:26:21 +11001076 ac -= optind;
1077 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001078
Ben Lindstromd94580c2001-07-04 03:48:02 +00001079 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001080 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001081
Ben Lindstromeecdf232002-04-02 21:03:51 +00001082 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001083 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001084 if (shell != NULL &&
1085 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001086 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001087 }
Damien Miller95def091999-11-25 00:26:21 +11001088 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001089 const char *errstr = NULL;
1090
Damien Miller95def091999-11-25 00:26:21 +11001091 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1092 if (pidstr == NULL) {
1093 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001094 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001095 exit(1);
1096 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001097 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1098 if (errstr) {
1099 fprintf(stderr,
1100 "%s=\"%s\", which is not a good PID: %s\n",
1101 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001102 exit(1);
1103 }
1104 if (kill(pid, SIGTERM) == -1) {
1105 perror("kill");
1106 exit(1);
1107 }
1108 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1109 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1110 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001111 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001112 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001113 }
Damien Miller95def091999-11-25 00:26:21 +11001114 parent_pid = getpid();
1115
Ben Lindstromb7788f32002-06-06 21:46:08 +00001116 if (agentsocket == NULL) {
1117 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001118 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001119 if (mkdtemp(socket_dir) == NULL) {
1120 perror("mkdtemp: private socket dir");
1121 exit(1);
1122 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001123 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1124 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001125 } else {
1126 /* Try to use specified agent socket */
1127 socket_dir[0] = '\0';
1128 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001129 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001130
Damien Miller5428f641999-11-25 11:54:57 +11001131 /*
1132 * Create socket early so it will exist before command gets run from
1133 * the parent.
1134 */
Damien Miller95def091999-11-25 00:26:21 +11001135 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1136 if (sock < 0) {
1137 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001138 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001139 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001140 }
Damien Miller95def091999-11-25 00:26:21 +11001141 memset(&sunaddr, 0, sizeof(sunaddr));
1142 sunaddr.sun_family = AF_UNIX;
1143 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001144 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001145 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001146 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001147 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001148 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001149 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001150 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001151 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001152 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001153 perror("listen");
1154 cleanup_exit(1);
1155 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001156
Damien Miller5428f641999-11-25 11:54:57 +11001157 /*
1158 * Fork, and have the parent execute the command, if any, or present
1159 * the socket data. The child continues as the authentication agent.
1160 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001161 if (d_flag) {
1162 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1163 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1164 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1165 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001166 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001167 goto skip;
1168 }
Damien Miller95def091999-11-25 00:26:21 +11001169 pid = fork();
1170 if (pid == -1) {
1171 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001172 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001173 }
1174 if (pid != 0) { /* Parent - execute the given command. */
1175 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001176 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001177 if (ac == 0) {
1178 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1179 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001180 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001181 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001182 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001183 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001184 exit(0);
1185 }
Damien Millere4340be2000-09-16 13:29:08 +11001186 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1187 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1188 perror("setenv");
1189 exit(1);
1190 }
Damien Miller95def091999-11-25 00:26:21 +11001191 execvp(av[0], av);
1192 perror(av[0]);
1193 exit(1);
1194 }
Damien Millerc653b892002-02-08 22:05:41 +11001195 /* child */
1196 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001197
1198 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001199 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001200 cleanup_exit(1);
1201 }
1202
1203 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001204 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1205 /* XXX might close listen socket */
1206 (void)dup2(fd, STDIN_FILENO);
1207 (void)dup2(fd, STDOUT_FILENO);
1208 (void)dup2(fd, STDERR_FILENO);
1209 if (fd > 2)
1210 close(fd);
1211 }
Damien Miller95def091999-11-25 00:26:21 +11001212
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001213#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001214 /* deny core dumps, since memory contains unencrypted private keys */
1215 rlim.rlim_cur = rlim.rlim_max = 0;
1216 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001217 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001218 cleanup_exit(1);
1219 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001220#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001221
1222skip:
Damien Miller95def091999-11-25 00:26:21 +11001223 new_socket(AUTH_SOCKET, sock);
1224 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001225 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001226 alarm(10);
1227 }
Damien Millerad833b32000-08-23 10:46:23 +10001228 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001229 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001230 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001231 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001232 signal(SIGHUP, cleanup_handler);
1233 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001234 nalloc = 0;
1235
Damien Miller95def091999-11-25 00:26:21 +11001236 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001237 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001238 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001239 if (errno == EINTR)
1240 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001241 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001242 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001243 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001244 }
1245 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246}