blob: a69c25eec6bef2548081e54ec1273562b859f0d3 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110034 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035
36#include "includes.h"
Damien Miller9b481512002-09-12 10:43:29 +100037#include "openbsd-compat/sys-queue.h"
Damien Miller788f2122005-11-05 15:14:59 +110038RCSID("$OpenBSD: ssh-agent.c,v 1.124 2005/10/30 08:52:18 djm Exp $");
Damien Millerec52d7c2002-01-22 23:52:17 +110039
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040#include <openssl/evp.h>
41#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43#include "ssh.h"
44#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045#include "buffer.h"
46#include "bufaux.h"
47#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "getput.h"
Damien Miller994cf142000-07-21 10:19:44 +100049#include "key.h"
50#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110051#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110053#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Ben Lindstrom3f471632001-07-04 03:53:15 +000055#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000056#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000057#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000058
Damien Miller6c4914a2004-03-03 11:08:59 +110059#if defined(HAVE_SYS_PRCTL_H)
60#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
61#endif
62
Ben Lindstrom65366a82001-12-06 16:32:47 +000063typedef enum {
64 AUTH_UNUSED,
65 AUTH_SOCKET,
66 AUTH_CONNECTION
67} sock_type;
68
Damien Miller95def091999-11-25 00:26:21 +110069typedef struct {
70 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000071 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110072 Buffer input;
73 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000074 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075} SocketEntry;
76
Ben Lindstrom46c16222000-12-22 01:43:59 +000077u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078SocketEntry *sockets = NULL;
79
Damien Miller1a534ae2002-01-22 23:26:13 +110080typedef struct identity {
81 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100082 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110083 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000084 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110085 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086} Identity;
87
Damien Millerad833b32000-08-23 10:46:23 +100088typedef struct {
89 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110090 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100091} Idtab;
92
93/* private key table, one per protocol version */
94Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96int max_fd = 0;
97
98/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100099pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
101/* pathname and directory for AUTH_SOCKET */
102char socket_name[1024];
103char socket_dir[1024];
104
Ben Lindstrom2f717042002-06-06 21:52:03 +0000105/* locking */
106int locked = 0;
107char *lock_passwd = NULL;
108
Damien Miller95def091999-11-25 00:26:21 +1100109extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100110
Damien Miller53d81482003-01-22 11:47:19 +1100111/* Default lifetime (0 == forever) */
112static int lifetime = 0;
113
Ben Lindstrombba81212001-06-25 05:01:22 +0000114static void
Damien Miller58f34862002-09-04 16:31:21 +1000115close_socket(SocketEntry *e)
116{
Damien Miller58f34862002-09-04 16:31:21 +1000117 close(e->fd);
118 e->fd = -1;
119 e->type = AUTH_UNUSED;
120 buffer_free(&e->input);
121 buffer_free(&e->output);
122 buffer_free(&e->request);
123}
124
125static void
Damien Millerad833b32000-08-23 10:46:23 +1000126idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127{
Damien Millerad833b32000-08-23 10:46:23 +1000128 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000129
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000130 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100131 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000132 idtable[i].nentries = 0;
133 }
134}
135
136/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000137static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000138idtab_lookup(int version)
139{
140 if (version < 1 || version > 2)
141 fatal("internal error, bad protocol version %d", version);
142 return &idtable[version];
143}
144
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000145static void
146free_identity(Identity *id)
147{
148 key_free(id->key);
149 xfree(id->comment);
150 xfree(id);
151}
152
Damien Millerad833b32000-08-23 10:46:23 +1000153/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100154static Identity *
155lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000156{
Damien Miller1a534ae2002-01-22 23:26:13 +1100157 Identity *id;
158
Damien Millerad833b32000-08-23 10:46:23 +1000159 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100160 TAILQ_FOREACH(id, &tab->idlist, next) {
161 if (key_equal(key, id->key))
162 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000163 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100164 return (NULL);
165}
166
Damien Miller6c711792003-01-24 11:36:23 +1100167/* Check confirmation of keysign request */
168static int
169confirm_key(Identity *id)
170{
Darren Tuckerce327b62004-11-05 20:38:03 +1100171 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100172 int ret = -1;
173
174 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100175 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
176 id->comment, p))
177 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100178 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100179
Damien Miller6c711792003-01-24 11:36:23 +1100180 return (ret);
181}
182
Damien Millerad833b32000-08-23 10:46:23 +1000183/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000184static void
Damien Millerad833b32000-08-23 10:46:23 +1000185process_request_identities(SocketEntry *e, int version)
186{
187 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100188 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000189 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190
Damien Miller95def091999-11-25 00:26:21 +1100191 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000192 buffer_put_char(&msg, (version == 1) ?
193 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
194 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100195 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100196 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000197 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
198 buffer_put_bignum(&msg, id->key->rsa->e);
199 buffer_put_bignum(&msg, id->key->rsa->n);
200 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000201 u_char *blob;
202 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100203 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000204 buffer_put_string(&msg, blob, blen);
205 xfree(blob);
206 }
207 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100208 }
209 buffer_put_int(&e->output, buffer_len(&msg));
210 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
211 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212}
213
Damien Millerad833b32000-08-23 10:46:23 +1000214/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000215static void
Damien Millerad833b32000-08-23 10:46:23 +1000216process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000218 u_char buf[32], mdbuf[16], session_id[16];
219 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000220 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000221 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000222 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100223 Buffer msg;
224 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000225 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226
Damien Miller95def091999-11-25 00:26:21 +1100227 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100228 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100229 if ((challenge = BN_new()) == NULL)
230 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000231
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000232 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000233 buffer_get_bignum(&e->request, key->rsa->e);
234 buffer_get_bignum(&e->request, key->rsa->n);
235 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Millerad833b32000-08-23 10:46:23 +1000237 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000238 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000239 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000240 buffer_get(&e->request, session_id, 16);
241 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000242 if (response_type != 1)
243 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Miller1a534ae2002-01-22 23:26:13 +1100245 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100246 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100247 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000248 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100249 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
250 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100251
Damien Millerad833b32000-08-23 10:46:23 +1000252 /* The response is MD5 of decrypted challenge plus session id. */
253 len = BN_num_bytes(challenge);
254 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000255 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000256 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100257 }
Damien Millerad833b32000-08-23 10:46:23 +1000258 memset(buf, 0, 32);
259 BN_bn2bin(challenge, buf + 32 - len);
260 MD5_Init(&md);
261 MD5_Update(&md, buf, 32);
262 MD5_Update(&md, session_id, 16);
263 MD5_Final(mdbuf, &md);
264
265 /* Send the response. */
266 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
267 for (i = 0; i < 16; i++)
268 buffer_put_char(&msg, mdbuf[i]);
269 goto send;
270 }
271
272failure:
273 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100274 buffer_put_char(&msg, SSH_AGENT_FAILURE);
275send:
276 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000277 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
278 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100279 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000280 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281}
282
Damien Millerad833b32000-08-23 10:46:23 +1000283/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000284static void
Damien Millerad833b32000-08-23 10:46:23 +1000285process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000287 u_char *blob, *data, *signature = NULL;
288 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000289 extern int datafellows;
290 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000291 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000292 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000293
294 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000295
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000296 blob = buffer_get_string(&e->request, &blen);
297 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100298
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000299 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100300 if (flags & SSH_AGENT_OLD_SIGNATURE)
301 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000302
Damien Miller0bc1bd82000-11-13 22:57:25 +1100303 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000304 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100305 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100306 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100307 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000308 }
309 key_free(key);
310 buffer_init(&msg);
311 if (ok == 0) {
312 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
313 buffer_put_string(&msg, signature, slen);
314 } else {
315 buffer_put_char(&msg, SSH_AGENT_FAILURE);
316 }
317 buffer_put_int(&e->output, buffer_len(&msg));
318 buffer_append(&e->output, buffer_ptr(&msg),
319 buffer_len(&msg));
320 buffer_free(&msg);
321 xfree(data);
322 xfree(blob);
323 if (signature != NULL)
324 xfree(signature);
325}
326
327/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000328static void
Damien Millerad833b32000-08-23 10:46:23 +1000329process_remove_identity(SocketEntry *e, int version)
330{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000331 u_int blen, bits;
332 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100333 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000334 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100335
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000336 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000337 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100338 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000339 bits = buffer_get_int(&e->request);
340 buffer_get_bignum(&e->request, key->rsa->e);
341 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Millerad833b32000-08-23 10:46:23 +1000343 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000344 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000345 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000346 break;
347 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000348 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100349 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000350 xfree(blob);
351 break;
352 }
353 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100354 Identity *id = lookup_identity(key, version);
355 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100356 /*
357 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100358 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000359 * the array, we actually free the key there and move
360 * all the entries between the empty slot and the end
361 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100362 */
Damien Millerad833b32000-08-23 10:46:23 +1000363 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100364 if (tab->nentries < 1)
365 fatal("process_remove_identity: "
366 "internal error: tab->nentries %d",
367 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100368 TAILQ_REMOVE(&tab->idlist, id, next);
369 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000370 tab->nentries--;
371 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100372 }
Damien Millerad833b32000-08-23 10:46:23 +1000373 key_free(key);
374 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000375 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000376 buffer_put_char(&e->output,
377 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000378}
379
Ben Lindstrombba81212001-06-25 05:01:22 +0000380static void
Damien Millerad833b32000-08-23 10:46:23 +1000381process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000382{
Damien Millerad833b32000-08-23 10:46:23 +1000383 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100384 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385
Damien Miller95def091999-11-25 00:26:21 +1100386 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100387 for (id = TAILQ_FIRST(&tab->idlist); id;
388 id = TAILQ_FIRST(&tab->idlist)) {
389 TAILQ_REMOVE(&tab->idlist, id, next);
390 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100391 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392
Damien Miller95def091999-11-25 00:26:21 +1100393 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000394 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
396 /* Send success. */
397 buffer_put_int(&e->output, 1);
398 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100399}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Ben Lindstrombba81212001-06-25 05:01:22 +0000401static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000402reaper(void)
403{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000404 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000405 Identity *id, *nxt;
406 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000407 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000408
409 for (version = 1; version < 3; version++) {
410 tab = idtab_lookup(version);
411 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
412 nxt = TAILQ_NEXT(id, next);
413 if (id->death != 0 && now >= id->death) {
414 TAILQ_REMOVE(&tab->idlist, id, next);
415 free_identity(id);
416 tab->nentries--;
417 }
418 }
419 }
420}
421
422static void
Damien Millerad833b32000-08-23 10:46:23 +1000423process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100424{
Damien Millerad833b32000-08-23 10:46:23 +1000425 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100426 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000427 char *type_name, *comment;
428 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100429
Damien Millerad833b32000-08-23 10:46:23 +1000430 switch (version) {
431 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100432 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000433 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000434 buffer_get_bignum(&e->request, k->rsa->n);
435 buffer_get_bignum(&e->request, k->rsa->e);
436 buffer_get_bignum(&e->request, k->rsa->d);
437 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100438
Damien Millerad833b32000-08-23 10:46:23 +1000439 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000440 buffer_get_bignum(&e->request, k->rsa->q); /* p */
441 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100442
Damien Millerad833b32000-08-23 10:46:23 +1000443 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000444 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000445 break;
446 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000447 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000448 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000450 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451 case KEY_DSA:
452 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000453 buffer_get_bignum2(&e->request, k->dsa->p);
454 buffer_get_bignum2(&e->request, k->dsa->q);
455 buffer_get_bignum2(&e->request, k->dsa->g);
456 buffer_get_bignum2(&e->request, k->dsa->pub_key);
457 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100458 break;
459 case KEY_RSA:
460 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000461 buffer_get_bignum2(&e->request, k->rsa->n);
462 buffer_get_bignum2(&e->request, k->rsa->e);
463 buffer_get_bignum2(&e->request, k->rsa->d);
464 buffer_get_bignum2(&e->request, k->rsa->iqmp);
465 buffer_get_bignum2(&e->request, k->rsa->p);
466 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467
468 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000469 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 break;
471 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000472 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000473 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100474 }
Damien Millerad833b32000-08-23 10:46:23 +1000475 break;
476 }
Damien Millerc51d0732003-03-15 11:37:09 +1100477 /* enable blinding */
478 switch (k->type) {
479 case KEY_RSA:
480 case KEY_RSA1:
481 if (RSA_blinding_on(k->rsa, NULL) != 1) {
482 error("process_add_identity: RSA_blinding_on failed");
483 key_free(k);
484 goto send;
485 }
486 break;
487 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000488 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000489 if (k == NULL) {
490 xfree(comment);
491 goto send;
492 }
493 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000494 while (buffer_len(&e->request)) {
495 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000496 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000497 death = time(NULL) + buffer_get_int(&e->request);
498 break;
Damien Miller6c711792003-01-24 11:36:23 +1100499 case SSH_AGENT_CONSTRAIN_CONFIRM:
500 confirm = 1;
501 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000502 default:
503 break;
504 }
505 }
Damien Miller53d81482003-01-22 11:47:19 +1100506 if (lifetime && !death)
507 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000508 if (lookup_identity(k, version) == NULL) {
509 Identity *id = xmalloc(sizeof(Identity));
510 id->key = k;
511 id->comment = comment;
512 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100513 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000514 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
515 /* Increment the number of identities. */
516 tab->nentries++;
517 } else {
518 key_free(k);
519 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000520 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000521send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000522 buffer_put_int(&e->output, 1);
523 buffer_put_char(&e->output,
524 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
525}
526
Ben Lindstrom2f717042002-06-06 21:52:03 +0000527/* XXX todo: encrypt sensitive data with passphrase */
528static void
529process_lock_agent(SocketEntry *e, int lock)
530{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000531 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000532 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000533
534 passwd = buffer_get_string(&e->request, NULL);
535 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
536 locked = 0;
537 memset(lock_passwd, 0, strlen(lock_passwd));
538 xfree(lock_passwd);
539 lock_passwd = NULL;
540 success = 1;
541 } else if (!locked && lock) {
542 locked = 1;
543 lock_passwd = xstrdup(passwd);
544 success = 1;
545 }
546 memset(passwd, 0, strlen(passwd));
547 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000548
Ben Lindstrom2f717042002-06-06 21:52:03 +0000549 buffer_put_int(&e->output, 1);
550 buffer_put_char(&e->output,
551 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000552}
553
554static void
555no_identities(SocketEntry *e, u_int type)
556{
557 Buffer msg;
558
559 buffer_init(&msg);
560 buffer_put_char(&msg,
561 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
562 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
563 buffer_put_int(&msg, 0);
564 buffer_put_int(&e->output, buffer_len(&msg));
565 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
566 buffer_free(&msg);
567}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000568
569#ifdef SMARTCARD
570static void
571process_add_smartcard_key (SocketEntry *e)
572{
Ben Lindstromba72d302002-03-22 03:51:06 +0000573 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000574 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000575 Key **keys, *k;
576 Identity *id;
577 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100578
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000579 sc_reader_id = buffer_get_string(&e->request, NULL);
580 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000581
582 while (buffer_len(&e->request)) {
583 switch (buffer_get_char(&e->request)) {
584 case SSH_AGENT_CONSTRAIN_LIFETIME:
585 death = time(NULL) + buffer_get_int(&e->request);
586 break;
587 case SSH_AGENT_CONSTRAIN_CONFIRM:
588 confirm = 1;
589 break;
590 default:
591 break;
592 }
593 }
594 if (lifetime && !death)
595 death = time(NULL) + lifetime;
596
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000597 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000598 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000599 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000600
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000601 if (keys == NULL || keys[0] == NULL) {
602 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000603 goto send;
604 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000605 for (i = 0; keys[i] != NULL; i++) {
606 k = keys[i];
607 version = k->type == KEY_RSA1 ? 1 : 2;
608 tab = idtab_lookup(version);
609 if (lookup_identity(k, version) == NULL) {
610 id = xmalloc(sizeof(Identity));
611 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000612 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000613 id->death = death;
614 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000615 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
616 tab->nentries++;
617 success = 1;
618 } else {
619 key_free(k);
620 }
621 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000622 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000623 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000624send:
625 buffer_put_int(&e->output, 1);
626 buffer_put_char(&e->output,
627 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
628}
629
630static void
631process_remove_smartcard_key(SocketEntry *e)
632{
Ben Lindstromba72d302002-03-22 03:51:06 +0000633 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000634 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000635 Key **keys, *k = NULL;
636 Identity *id;
637 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000638
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000639 sc_reader_id = buffer_get_string(&e->request, NULL);
640 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000641 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000642 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000643 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000644
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000645 if (keys == NULL || keys[0] == NULL) {
646 error("sc_get_keys failed");
647 goto send;
648 }
649 for (i = 0; keys[i] != NULL; i++) {
650 k = keys[i];
651 version = k->type == KEY_RSA1 ? 1 : 2;
652 if ((id = lookup_identity(k, version)) != NULL) {
653 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000654 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000655 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100656 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000657 success = 1;
658 }
659 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000660 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000662 xfree(keys);
663send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000664 buffer_put_int(&e->output, 1);
665 buffer_put_char(&e->output,
666 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
667}
Ben Lindstromffce1472001-08-06 21:57:31 +0000668#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669
Damien Millerad833b32000-08-23 10:46:23 +1000670/* dispatch incoming messages */
671
Ben Lindstrombba81212001-06-25 05:01:22 +0000672static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673process_message(SocketEntry *e)
674{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000675 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000676 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000677
678 /* kill dead keys */
679 reaper();
680
Damien Miller95def091999-11-25 00:26:21 +1100681 if (buffer_len(&e->input) < 5)
682 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100683 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100684 msg_len = GET_32BIT(cp);
685 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000686 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100687 return;
688 }
689 if (buffer_len(&e->input) < msg_len + 4)
690 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000691
692 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100693 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000694 buffer_clear(&e->request);
695 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
696 buffer_consume(&e->input, msg_len);
697 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000698
Ben Lindstrom2f717042002-06-06 21:52:03 +0000699 /* check wheter agent is locked */
700 if (locked && type != SSH_AGENTC_UNLOCK) {
701 buffer_clear(&e->request);
702 switch (type) {
703 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
704 case SSH2_AGENTC_REQUEST_IDENTITIES:
705 /* send empty lists */
706 no_identities(e, type);
707 break;
708 default:
709 /* send a fail message for all other request types */
710 buffer_put_int(&e->output, 1);
711 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
712 }
713 return;
714 }
715
Ben Lindstrom3f471632001-07-04 03:53:15 +0000716 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100717 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000718 case SSH_AGENTC_LOCK:
719 case SSH_AGENTC_UNLOCK:
720 process_lock_agent(e, type == SSH_AGENTC_LOCK);
721 break;
Damien Millerad833b32000-08-23 10:46:23 +1000722 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100723 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000724 process_authentication_challenge1(e);
725 break;
726 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
727 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100728 break;
729 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000730 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000731 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100732 break;
733 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000734 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100735 break;
736 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000737 process_remove_all_identities(e, 1);
738 break;
739 /* ssh2 */
740 case SSH2_AGENTC_SIGN_REQUEST:
741 process_sign_request2(e);
742 break;
743 case SSH2_AGENTC_REQUEST_IDENTITIES:
744 process_request_identities(e, 2);
745 break;
746 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000747 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000748 process_add_identity(e, 2);
749 break;
750 case SSH2_AGENTC_REMOVE_IDENTITY:
751 process_remove_identity(e, 2);
752 break;
753 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
754 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100755 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000756#ifdef SMARTCARD
757 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000758 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000759 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100760 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000761 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
762 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100763 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000764#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100765 default:
766 /* Unknown message. Respond with failure. */
767 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000768 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100769 buffer_put_int(&e->output, 1);
770 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
771 break;
772 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000773}
774
Ben Lindstrombba81212001-06-25 05:01:22 +0000775static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000776new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000777{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000778 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000779
Damien Miller232711f2004-06-15 10:35:30 +1000780 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781
Damien Miller95def091999-11-25 00:26:21 +1100782 if (fd > max_fd)
783 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000784
Damien Miller95def091999-11-25 00:26:21 +1100785 for (i = 0; i < sockets_alloc; i++)
786 if (sockets[i].type == AUTH_UNUSED) {
787 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100788 buffer_init(&sockets[i].input);
789 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000790 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000791 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100792 return;
793 }
794 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000795 new_alloc = sockets_alloc + 10;
Damien Miller95def091999-11-25 00:26:21 +1100796 if (sockets)
Darren Tuckerfb16b242003-09-22 21:04:23 +1000797 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
Damien Miller95def091999-11-25 00:26:21 +1100798 else
Darren Tuckerfb16b242003-09-22 21:04:23 +1000799 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
800 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100801 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000802 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100803 sockets[old_alloc].fd = fd;
804 buffer_init(&sockets[old_alloc].input);
805 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000806 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000807 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808}
809
Ben Lindstrombba81212001-06-25 05:01:22 +0000810static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000811prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000813 u_int i, sz;
814 int n = 0;
815
816 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100817 switch (sockets[i].type) {
818 case AUTH_SOCKET:
819 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000820 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100821 break;
822 case AUTH_UNUSED:
823 break;
824 default:
825 fatal("Unknown socket type %d", sockets[i].type);
826 break;
827 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000828 }
829
830 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000831 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000832 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000833 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000834 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000835 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000836 *fdrp = xmalloc(sz);
837 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000838 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000839 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000840 if (n < *fdl)
841 debug("XXX shrink: %d < %d", n, *fdl);
842 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 memset(*fdrp, 0, sz);
844 memset(*fdwp, 0, sz);
845
846 for (i = 0; i < sockets_alloc; i++) {
847 switch (sockets[i].type) {
848 case AUTH_SOCKET:
849 case AUTH_CONNECTION:
850 FD_SET(sockets[i].fd, *fdrp);
851 if (buffer_len(&sockets[i].output) > 0)
852 FD_SET(sockets[i].fd, *fdwp);
853 break;
854 default:
855 break;
856 }
857 }
858 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000859}
860
Ben Lindstrombba81212001-06-25 05:01:22 +0000861static void
Damien Miller95def091999-11-25 00:26:21 +1100862after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000863{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000864 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100865 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100866 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000867 int len, sock;
868 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000869 uid_t euid;
870 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871
Damien Miller95def091999-11-25 00:26:21 +1100872 for (i = 0; i < sockets_alloc; i++)
873 switch (sockets[i].type) {
874 case AUTH_UNUSED:
875 break;
876 case AUTH_SOCKET:
877 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100878 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000879 sock = accept(sockets[i].fd,
880 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100881 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100882 error("accept from AUTH_SOCKET: %s",
883 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100884 break;
885 }
Damien Miller538f1812002-09-12 09:51:10 +1000886 if (getpeereid(sock, &euid, &egid) < 0) {
887 error("getpeereid %d failed: %s",
888 sock, strerror(errno));
889 close(sock);
890 break;
891 }
Damien Milleraf9de382002-10-03 11:54:35 +1000892 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000893 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000894 "peer euid %u != uid %u",
895 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000896 close(sock);
897 break;
898 }
Damien Miller95def091999-11-25 00:26:21 +1100899 new_socket(AUTH_CONNECTION, sock);
900 }
901 break;
902 case AUTH_CONNECTION:
903 if (buffer_len(&sockets[i].output) > 0 &&
904 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000905 do {
906 len = write(sockets[i].fd,
907 buffer_ptr(&sockets[i].output),
908 buffer_len(&sockets[i].output));
909 if (len == -1 && (errno == EAGAIN ||
910 errno == EINTR))
911 continue;
912 break;
913 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100914 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000915 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100916 break;
917 }
918 buffer_consume(&sockets[i].output, len);
919 }
920 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000921 do {
922 len = read(sockets[i].fd, buf, sizeof(buf));
923 if (len == -1 && (errno == EAGAIN ||
924 errno == EINTR))
925 continue;
926 break;
927 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100928 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000929 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100930 break;
931 }
932 buffer_append(&sockets[i].input, buf, len);
933 process_message(&sockets[i]);
934 }
935 break;
936 default:
937 fatal("Unknown type %d", sockets[i].type);
938 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000939}
940
Ben Lindstrombba81212001-06-25 05:01:22 +0000941static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000942cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000943{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000944 if (socket_name[0])
945 unlink(socket_name);
946 if (socket_dir[0])
947 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948}
949
Darren Tucker3e33cec2003-10-02 16:12:36 +1000950void
Damien Miller792c5111999-10-29 09:47:09 +1000951cleanup_exit(int i)
952{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000953 cleanup_socket();
954 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000955}
956
Ben Lindstrombba81212001-06-25 05:01:22 +0000957static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000958cleanup_handler(int sig)
959{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000960 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000961 _exit(2);
962}
963
Ben Lindstrombba81212001-06-25 05:01:22 +0000964static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000965check_parent_exists(int sig)
966{
967 int save_errno = errno;
968
969 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
970 /* printf("Parent has died - Authentication agent exiting.\n"); */
971 cleanup_handler(sig); /* safe */
972 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000973 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000974 alarm(10);
975 errno = save_errno;
976}
977
978static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000979usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000980{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000981 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000982 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000983 fprintf(stderr, "Options:\n");
984 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
985 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
986 fprintf(stderr, " -k Kill the current agent.\n");
987 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000988 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100989 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100990 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000991}
992
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000993int
994main(int ac, char **av)
995{
Damien Miller6c711792003-01-24 11:36:23 +1100996 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000997 int sock, fd, ch;
998 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000999 char *shell, *format, *pidstr, *agentsocket = NULL;
1000 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001001 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001002#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001003 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001004#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001005 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001006 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001007 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001008 pid_t pid;
1009 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001010
Darren Tuckerce321d82005-10-03 18:11:24 +10001011 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1012 sanitise_stdfd();
1013
Damien Miller6cffb9a2002-09-04 16:20:26 +10001014 /* drop */
1015 setegid(getgid());
1016 setgid(getgid());
1017
Damien Miller6c4914a2004-03-03 11:08:59 +11001018#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1019 /* Disable ptrace on Linux without sgid bit */
1020 prctl(PR_SET_DUMPABLE, 0);
1021#endif
1022
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001023 SSLeay_add_all_algorithms();
1024
Damien Miller59d3d5b2003-08-22 09:34:41 +10001025 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001026 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001027 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001028
Damien Miller53d81482003-01-22 11:47:19 +11001029 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001030 switch (ch) {
1031 case 'c':
1032 if (s_flag)
1033 usage();
1034 c_flag++;
1035 break;
1036 case 'k':
1037 k_flag++;
1038 break;
1039 case 's':
1040 if (c_flag)
1041 usage();
1042 s_flag++;
1043 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001044 case 'd':
1045 if (d_flag)
1046 usage();
1047 d_flag++;
1048 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001049 case 'a':
1050 agentsocket = optarg;
1051 break;
Damien Miller53d81482003-01-22 11:47:19 +11001052 case 't':
1053 if ((lifetime = convtime(optarg)) == -1) {
1054 fprintf(stderr, "Invalid lifetime\n");
1055 usage();
1056 }
1057 break;
Damien Miller95def091999-11-25 00:26:21 +11001058 default:
1059 usage();
1060 }
Damien Miller792c5111999-10-29 09:47:09 +10001061 }
Damien Miller95def091999-11-25 00:26:21 +11001062 ac -= optind;
1063 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001064
Ben Lindstromd94580c2001-07-04 03:48:02 +00001065 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001066 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001067
Ben Lindstromeecdf232002-04-02 21:03:51 +00001068 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001069 shell = getenv("SHELL");
1070 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1071 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 if (k_flag) {
1074 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1075 if (pidstr == NULL) {
1076 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001077 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001078 exit(1);
1079 }
1080 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001081 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001082 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001083 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001084 exit(1);
1085 }
1086 if (kill(pid, SIGTERM) == -1) {
1087 perror("kill");
1088 exit(1);
1089 }
1090 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1091 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1092 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001093 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001094 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001095 }
Damien Miller95def091999-11-25 00:26:21 +11001096 parent_pid = getpid();
1097
Ben Lindstromb7788f32002-06-06 21:46:08 +00001098 if (agentsocket == NULL) {
1099 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001100 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001101 if (mkdtemp(socket_dir) == NULL) {
1102 perror("mkdtemp: private socket dir");
1103 exit(1);
1104 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001105 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1106 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001107 } else {
1108 /* Try to use specified agent socket */
1109 socket_dir[0] = '\0';
1110 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001111 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001112
Damien Miller5428f641999-11-25 11:54:57 +11001113 /*
1114 * Create socket early so it will exist before command gets run from
1115 * the parent.
1116 */
Damien Miller95def091999-11-25 00:26:21 +11001117 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1118 if (sock < 0) {
1119 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001120 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001121 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001122 }
Damien Miller95def091999-11-25 00:26:21 +11001123 memset(&sunaddr, 0, sizeof(sunaddr));
1124 sunaddr.sun_family = AF_UNIX;
1125 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001126 prev_mask = umask(0177);
Damien Miller95def091999-11-25 00:26:21 +11001127 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1128 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001129 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001130 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001131 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001132 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001133 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001134 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001135 perror("listen");
1136 cleanup_exit(1);
1137 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001138
Damien Miller5428f641999-11-25 11:54:57 +11001139 /*
1140 * Fork, and have the parent execute the command, if any, or present
1141 * the socket data. The child continues as the authentication agent.
1142 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001143 if (d_flag) {
1144 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1145 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1146 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1147 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001148 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001149 goto skip;
1150 }
Damien Miller95def091999-11-25 00:26:21 +11001151 pid = fork();
1152 if (pid == -1) {
1153 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001154 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001155 }
1156 if (pid != 0) { /* Parent - execute the given command. */
1157 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001158 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001159 if (ac == 0) {
1160 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1161 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001162 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001163 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001165 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001166 exit(0);
1167 }
Damien Millere4340be2000-09-16 13:29:08 +11001168 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1169 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1170 perror("setenv");
1171 exit(1);
1172 }
Damien Miller95def091999-11-25 00:26:21 +11001173 execvp(av[0], av);
1174 perror(av[0]);
1175 exit(1);
1176 }
Damien Millerc653b892002-02-08 22:05:41 +11001177 /* child */
1178 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001179
1180 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001181 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001182 cleanup_exit(1);
1183 }
1184
1185 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001186 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1187 /* XXX might close listen socket */
1188 (void)dup2(fd, STDIN_FILENO);
1189 (void)dup2(fd, STDOUT_FILENO);
1190 (void)dup2(fd, STDERR_FILENO);
1191 if (fd > 2)
1192 close(fd);
1193 }
Damien Miller95def091999-11-25 00:26:21 +11001194
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001195#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001196 /* deny core dumps, since memory contains unencrypted private keys */
1197 rlim.rlim_cur = rlim.rlim_max = 0;
1198 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001199 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001200 cleanup_exit(1);
1201 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001202#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001203
1204skip:
Damien Miller95def091999-11-25 00:26:21 +11001205 new_socket(AUTH_SOCKET, sock);
1206 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001207 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001208 alarm(10);
1209 }
Damien Millerad833b32000-08-23 10:46:23 +10001210 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001211 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001212 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001213 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001214 signal(SIGHUP, cleanup_handler);
1215 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001216 nalloc = 0;
1217
Damien Miller95def091999-11-25 00:26:21 +11001218 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001219 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001220 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001221 if (errno == EINTR)
1222 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001223 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001224 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001225 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001226 }
1227 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228}