blob: bc4d8d33a2875da09cfe30b799dc5209eeb61893 [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"
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100038RCSID("$OpenBSD: ssh-agent.c,v 1.120 2004/08/11 21:43:05 avsm 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{
171 char *p, prompt[1024];
172 int ret = -1;
173
174 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
175 snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n"
176 "Key fingerprint %s.", id->comment, p);
177 xfree(p);
178 p = read_passphrase(prompt, RP_ALLOW_EOF);
179 if (p != NULL) {
180 /*
Damien Millera8e06ce2003-11-21 23:48:55 +1100181 * Accept empty responses and responses consisting
Damien Miller6c711792003-01-24 11:36:23 +1100182 * of the word "yes" as affirmative.
183 */
184 if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0)
185 ret = 0;
186 xfree(p);
187 }
188 return (ret);
189}
190
Damien Millerad833b32000-08-23 10:46:23 +1000191/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Damien Millerad833b32000-08-23 10:46:23 +1000193process_request_identities(SocketEntry *e, int version)
194{
195 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100196 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000197 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198
Damien Miller95def091999-11-25 00:26:21 +1100199 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000200 buffer_put_char(&msg, (version == 1) ?
201 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
202 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100203 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100204 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000205 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
206 buffer_put_bignum(&msg, id->key->rsa->e);
207 buffer_put_bignum(&msg, id->key->rsa->n);
208 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000209 u_char *blob;
210 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000212 buffer_put_string(&msg, blob, blen);
213 xfree(blob);
214 }
215 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100216 }
217 buffer_put_int(&e->output, buffer_len(&msg));
218 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
219 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000220}
221
Damien Millerad833b32000-08-23 10:46:23 +1000222/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000223static void
Damien Millerad833b32000-08-23 10:46:23 +1000224process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000226 u_char buf[32], mdbuf[16], session_id[16];
227 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000228 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000229 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000230 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100231 Buffer msg;
232 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000233 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234
Damien Miller95def091999-11-25 00:26:21 +1100235 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100236 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100237 if ((challenge = BN_new()) == NULL)
238 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000239
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000240 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000241 buffer_get_bignum(&e->request, key->rsa->e);
242 buffer_get_bignum(&e->request, key->rsa->n);
243 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244
Damien Millerad833b32000-08-23 10:46:23 +1000245 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000246 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000247 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000248 buffer_get(&e->request, session_id, 16);
249 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000250 if (response_type != 1)
251 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller1a534ae2002-01-22 23:26:13 +1100253 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100254 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100255 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000256 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100257 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
258 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100259
Damien Millerad833b32000-08-23 10:46:23 +1000260 /* The response is MD5 of decrypted challenge plus session id. */
261 len = BN_num_bytes(challenge);
262 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000263 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000264 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100265 }
Damien Millerad833b32000-08-23 10:46:23 +1000266 memset(buf, 0, 32);
267 BN_bn2bin(challenge, buf + 32 - len);
268 MD5_Init(&md);
269 MD5_Update(&md, buf, 32);
270 MD5_Update(&md, session_id, 16);
271 MD5_Final(mdbuf, &md);
272
273 /* Send the response. */
274 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
275 for (i = 0; i < 16; i++)
276 buffer_put_char(&msg, mdbuf[i]);
277 goto send;
278 }
279
280failure:
281 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100282 buffer_put_char(&msg, SSH_AGENT_FAILURE);
283send:
284 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000285 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
286 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100287 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000288 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289}
290
Damien Millerad833b32000-08-23 10:46:23 +1000291/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Damien Millerad833b32000-08-23 10:46:23 +1000293process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000295 u_char *blob, *data, *signature = NULL;
296 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000297 extern int datafellows;
298 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000299 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000300 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000301
302 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000303
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000304 blob = buffer_get_string(&e->request, &blen);
305 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100306
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000307 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100308 if (flags & SSH_AGENT_OLD_SIGNATURE)
309 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000310
Damien Miller0bc1bd82000-11-13 22:57:25 +1100311 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000312 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100313 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100314 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100315 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000316 }
317 key_free(key);
318 buffer_init(&msg);
319 if (ok == 0) {
320 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
321 buffer_put_string(&msg, signature, slen);
322 } else {
323 buffer_put_char(&msg, SSH_AGENT_FAILURE);
324 }
325 buffer_put_int(&e->output, buffer_len(&msg));
326 buffer_append(&e->output, buffer_ptr(&msg),
327 buffer_len(&msg));
328 buffer_free(&msg);
329 xfree(data);
330 xfree(blob);
331 if (signature != NULL)
332 xfree(signature);
333}
334
335/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000336static void
Damien Millerad833b32000-08-23 10:46:23 +1000337process_remove_identity(SocketEntry *e, int version)
338{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000339 u_int blen, bits;
340 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100341 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000342 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100343
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000344 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000345 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100346 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000347 bits = buffer_get_int(&e->request);
348 buffer_get_bignum(&e->request, key->rsa->e);
349 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Millerad833b32000-08-23 10:46:23 +1000351 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000352 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000353 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000354 break;
355 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000356 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100357 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000358 xfree(blob);
359 break;
360 }
361 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100362 Identity *id = lookup_identity(key, version);
363 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100364 /*
365 * We have this key. Free the old key. Since we
366 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000367 * the array, we actually free the key there and move
368 * all the entries between the empty slot and the end
369 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100370 */
Damien Millerad833b32000-08-23 10:46:23 +1000371 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372 if (tab->nentries < 1)
373 fatal("process_remove_identity: "
374 "internal error: tab->nentries %d",
375 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100376 TAILQ_REMOVE(&tab->idlist, id, next);
377 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000378 tab->nentries--;
379 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100380 }
Damien Millerad833b32000-08-23 10:46:23 +1000381 key_free(key);
382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000383 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000384 buffer_put_char(&e->output,
385 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386}
387
Ben Lindstrombba81212001-06-25 05:01:22 +0000388static void
Damien Millerad833b32000-08-23 10:46:23 +1000389process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390{
Damien Millerad833b32000-08-23 10:46:23 +1000391 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100392 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000393
Damien Miller95def091999-11-25 00:26:21 +1100394 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100395 for (id = TAILQ_FIRST(&tab->idlist); id;
396 id = TAILQ_FIRST(&tab->idlist)) {
397 TAILQ_REMOVE(&tab->idlist, id, next);
398 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400
Damien Miller95def091999-11-25 00:26:21 +1100401 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000402 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403
404 /* Send success. */
405 buffer_put_int(&e->output, 1);
406 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100407}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Ben Lindstrombba81212001-06-25 05:01:22 +0000409static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000410reaper(void)
411{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000412 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000413 Identity *id, *nxt;
414 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000415 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000416
417 for (version = 1; version < 3; version++) {
418 tab = idtab_lookup(version);
419 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
420 nxt = TAILQ_NEXT(id, next);
421 if (id->death != 0 && now >= id->death) {
422 TAILQ_REMOVE(&tab->idlist, id, next);
423 free_identity(id);
424 tab->nentries--;
425 }
426 }
427 }
428}
429
430static void
Damien Millerad833b32000-08-23 10:46:23 +1000431process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100432{
Damien Millerad833b32000-08-23 10:46:23 +1000433 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100434 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000435 char *type_name, *comment;
436 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100437
Damien Millerad833b32000-08-23 10:46:23 +1000438 switch (version) {
439 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100440 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000441 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000442 buffer_get_bignum(&e->request, k->rsa->n);
443 buffer_get_bignum(&e->request, k->rsa->e);
444 buffer_get_bignum(&e->request, k->rsa->d);
445 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100446
Damien Millerad833b32000-08-23 10:46:23 +1000447 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000448 buffer_get_bignum(&e->request, k->rsa->q); /* p */
449 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100450
Damien Millerad833b32000-08-23 10:46:23 +1000451 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000452 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000453 break;
454 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000455 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000456 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100457 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000458 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 case KEY_DSA:
460 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000461 buffer_get_bignum2(&e->request, k->dsa->p);
462 buffer_get_bignum2(&e->request, k->dsa->q);
463 buffer_get_bignum2(&e->request, k->dsa->g);
464 buffer_get_bignum2(&e->request, k->dsa->pub_key);
465 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 break;
467 case KEY_RSA:
468 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000469 buffer_get_bignum2(&e->request, k->rsa->n);
470 buffer_get_bignum2(&e->request, k->rsa->e);
471 buffer_get_bignum2(&e->request, k->rsa->d);
472 buffer_get_bignum2(&e->request, k->rsa->iqmp);
473 buffer_get_bignum2(&e->request, k->rsa->p);
474 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100475
476 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000477 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 break;
479 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000480 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000481 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100482 }
Damien Millerad833b32000-08-23 10:46:23 +1000483 break;
484 }
Damien Millerc51d0732003-03-15 11:37:09 +1100485 /* enable blinding */
486 switch (k->type) {
487 case KEY_RSA:
488 case KEY_RSA1:
489 if (RSA_blinding_on(k->rsa, NULL) != 1) {
490 error("process_add_identity: RSA_blinding_on failed");
491 key_free(k);
492 goto send;
493 }
494 break;
495 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000496 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000497 if (k == NULL) {
498 xfree(comment);
499 goto send;
500 }
501 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000502 while (buffer_len(&e->request)) {
503 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000504 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000505 death = time(NULL) + buffer_get_int(&e->request);
506 break;
Damien Miller6c711792003-01-24 11:36:23 +1100507 case SSH_AGENT_CONSTRAIN_CONFIRM:
508 confirm = 1;
509 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000510 default:
511 break;
512 }
513 }
Damien Miller53d81482003-01-22 11:47:19 +1100514 if (lifetime && !death)
515 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000516 if (lookup_identity(k, version) == NULL) {
517 Identity *id = xmalloc(sizeof(Identity));
518 id->key = k;
519 id->comment = comment;
520 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100521 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000522 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
523 /* Increment the number of identities. */
524 tab->nentries++;
525 } else {
526 key_free(k);
527 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000528 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000529send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000530 buffer_put_int(&e->output, 1);
531 buffer_put_char(&e->output,
532 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
533}
534
Ben Lindstrom2f717042002-06-06 21:52:03 +0000535/* XXX todo: encrypt sensitive data with passphrase */
536static void
537process_lock_agent(SocketEntry *e, int lock)
538{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000539 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000540 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000541
542 passwd = buffer_get_string(&e->request, NULL);
543 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
544 locked = 0;
545 memset(lock_passwd, 0, strlen(lock_passwd));
546 xfree(lock_passwd);
547 lock_passwd = NULL;
548 success = 1;
549 } else if (!locked && lock) {
550 locked = 1;
551 lock_passwd = xstrdup(passwd);
552 success = 1;
553 }
554 memset(passwd, 0, strlen(passwd));
555 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000556
Ben Lindstrom2f717042002-06-06 21:52:03 +0000557 buffer_put_int(&e->output, 1);
558 buffer_put_char(&e->output,
559 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000560}
561
562static void
563no_identities(SocketEntry *e, u_int type)
564{
565 Buffer msg;
566
567 buffer_init(&msg);
568 buffer_put_char(&msg,
569 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
570 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
571 buffer_put_int(&msg, 0);
572 buffer_put_int(&e->output, buffer_len(&msg));
573 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
574 buffer_free(&msg);
575}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000576
577#ifdef SMARTCARD
578static void
579process_add_smartcard_key (SocketEntry *e)
580{
Ben Lindstromba72d302002-03-22 03:51:06 +0000581 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000582 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000583 Key **keys, *k;
584 Identity *id;
585 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100586
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000587 sc_reader_id = buffer_get_string(&e->request, NULL);
588 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000589
590 while (buffer_len(&e->request)) {
591 switch (buffer_get_char(&e->request)) {
592 case SSH_AGENT_CONSTRAIN_LIFETIME:
593 death = time(NULL) + buffer_get_int(&e->request);
594 break;
595 case SSH_AGENT_CONSTRAIN_CONFIRM:
596 confirm = 1;
597 break;
598 default:
599 break;
600 }
601 }
602 if (lifetime && !death)
603 death = time(NULL) + lifetime;
604
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000605 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000606 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000607 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000608
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000609 if (keys == NULL || keys[0] == NULL) {
610 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000611 goto send;
612 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000613 for (i = 0; keys[i] != NULL; i++) {
614 k = keys[i];
615 version = k->type == KEY_RSA1 ? 1 : 2;
616 tab = idtab_lookup(version);
617 if (lookup_identity(k, version) == NULL) {
618 id = xmalloc(sizeof(Identity));
619 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000620 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000621 id->death = death;
622 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000623 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
624 tab->nentries++;
625 success = 1;
626 } else {
627 key_free(k);
628 }
629 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000630 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000631 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000632send:
633 buffer_put_int(&e->output, 1);
634 buffer_put_char(&e->output,
635 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
636}
637
638static void
639process_remove_smartcard_key(SocketEntry *e)
640{
Ben Lindstromba72d302002-03-22 03:51:06 +0000641 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000642 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000643 Key **keys, *k = NULL;
644 Identity *id;
645 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000646
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000647 sc_reader_id = buffer_get_string(&e->request, NULL);
648 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000649 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000650 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000651 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000652
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000653 if (keys == NULL || keys[0] == NULL) {
654 error("sc_get_keys failed");
655 goto send;
656 }
657 for (i = 0; keys[i] != NULL; i++) {
658 k = keys[i];
659 version = k->type == KEY_RSA1 ? 1 : 2;
660 if ((id = lookup_identity(k, version)) != NULL) {
661 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000662 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000663 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100664 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000665 success = 1;
666 }
667 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000668 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000670 xfree(keys);
671send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000672 buffer_put_int(&e->output, 1);
673 buffer_put_char(&e->output,
674 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
675}
Ben Lindstromffce1472001-08-06 21:57:31 +0000676#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677
Damien Millerad833b32000-08-23 10:46:23 +1000678/* dispatch incoming messages */
679
Ben Lindstrombba81212001-06-25 05:01:22 +0000680static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681process_message(SocketEntry *e)
682{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000683 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000684 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000685
686 /* kill dead keys */
687 reaper();
688
Damien Miller95def091999-11-25 00:26:21 +1100689 if (buffer_len(&e->input) < 5)
690 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100691 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100692 msg_len = GET_32BIT(cp);
693 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000694 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100695 return;
696 }
697 if (buffer_len(&e->input) < msg_len + 4)
698 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000699
700 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100701 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000702 buffer_clear(&e->request);
703 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
704 buffer_consume(&e->input, msg_len);
705 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706
Ben Lindstrom2f717042002-06-06 21:52:03 +0000707 /* check wheter agent is locked */
708 if (locked && type != SSH_AGENTC_UNLOCK) {
709 buffer_clear(&e->request);
710 switch (type) {
711 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
712 case SSH2_AGENTC_REQUEST_IDENTITIES:
713 /* send empty lists */
714 no_identities(e, type);
715 break;
716 default:
717 /* send a fail message for all other request types */
718 buffer_put_int(&e->output, 1);
719 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
720 }
721 return;
722 }
723
Ben Lindstrom3f471632001-07-04 03:53:15 +0000724 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100725 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000726 case SSH_AGENTC_LOCK:
727 case SSH_AGENTC_UNLOCK:
728 process_lock_agent(e, type == SSH_AGENTC_LOCK);
729 break;
Damien Millerad833b32000-08-23 10:46:23 +1000730 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100731 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000732 process_authentication_challenge1(e);
733 break;
734 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
735 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100736 break;
737 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000738 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000739 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100740 break;
741 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000742 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100743 break;
744 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000745 process_remove_all_identities(e, 1);
746 break;
747 /* ssh2 */
748 case SSH2_AGENTC_SIGN_REQUEST:
749 process_sign_request2(e);
750 break;
751 case SSH2_AGENTC_REQUEST_IDENTITIES:
752 process_request_identities(e, 2);
753 break;
754 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000755 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000756 process_add_identity(e, 2);
757 break;
758 case SSH2_AGENTC_REMOVE_IDENTITY:
759 process_remove_identity(e, 2);
760 break;
761 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
762 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100763 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000764#ifdef SMARTCARD
765 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000766 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000767 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100768 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000769 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
770 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100771 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000772#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100773 default:
774 /* Unknown message. Respond with failure. */
775 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000776 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100777 buffer_put_int(&e->output, 1);
778 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
779 break;
780 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000781}
782
Ben Lindstrombba81212001-06-25 05:01:22 +0000783static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000784new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000786 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000787
Damien Miller232711f2004-06-15 10:35:30 +1000788 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789
Damien Miller95def091999-11-25 00:26:21 +1100790 if (fd > max_fd)
791 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000792
Damien Miller95def091999-11-25 00:26:21 +1100793 for (i = 0; i < sockets_alloc; i++)
794 if (sockets[i].type == AUTH_UNUSED) {
795 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100796 buffer_init(&sockets[i].input);
797 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000798 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000799 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100800 return;
801 }
802 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000803 new_alloc = sockets_alloc + 10;
Damien Miller95def091999-11-25 00:26:21 +1100804 if (sockets)
Darren Tuckerfb16b242003-09-22 21:04:23 +1000805 sockets = xrealloc(sockets, new_alloc * sizeof(sockets[0]));
Damien Miller95def091999-11-25 00:26:21 +1100806 else
Darren Tuckerfb16b242003-09-22 21:04:23 +1000807 sockets = xmalloc(new_alloc * sizeof(sockets[0]));
808 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100809 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000810 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100811 sockets[old_alloc].fd = fd;
812 buffer_init(&sockets[old_alloc].input);
813 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000814 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000815 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000816}
817
Ben Lindstrombba81212001-06-25 05:01:22 +0000818static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000819prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000820{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000821 u_int i, sz;
822 int n = 0;
823
824 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100825 switch (sockets[i].type) {
826 case AUTH_SOCKET:
827 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000828 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100829 break;
830 case AUTH_UNUSED:
831 break;
832 default:
833 fatal("Unknown socket type %d", sockets[i].type);
834 break;
835 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000836 }
837
838 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000839 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000840 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000841 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000843 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000844 *fdrp = xmalloc(sz);
845 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000846 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000848 if (n < *fdl)
849 debug("XXX shrink: %d < %d", n, *fdl);
850 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000851 memset(*fdrp, 0, sz);
852 memset(*fdwp, 0, sz);
853
854 for (i = 0; i < sockets_alloc; i++) {
855 switch (sockets[i].type) {
856 case AUTH_SOCKET:
857 case AUTH_CONNECTION:
858 FD_SET(sockets[i].fd, *fdrp);
859 if (buffer_len(&sockets[i].output) > 0)
860 FD_SET(sockets[i].fd, *fdwp);
861 break;
862 default:
863 break;
864 }
865 }
866 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000867}
868
Ben Lindstrombba81212001-06-25 05:01:22 +0000869static void
Damien Miller95def091999-11-25 00:26:21 +1100870after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000871{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000872 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100873 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100874 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000875 int len, sock;
876 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000877 uid_t euid;
878 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879
Damien Miller95def091999-11-25 00:26:21 +1100880 for (i = 0; i < sockets_alloc; i++)
881 switch (sockets[i].type) {
882 case AUTH_UNUSED:
883 break;
884 case AUTH_SOCKET:
885 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100886 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000887 sock = accept(sockets[i].fd,
888 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100889 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100890 error("accept from AUTH_SOCKET: %s",
891 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100892 break;
893 }
Damien Miller538f1812002-09-12 09:51:10 +1000894 if (getpeereid(sock, &euid, &egid) < 0) {
895 error("getpeereid %d failed: %s",
896 sock, strerror(errno));
897 close(sock);
898 break;
899 }
Damien Milleraf9de382002-10-03 11:54:35 +1000900 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000901 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000902 "peer euid %u != uid %u",
903 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000904 close(sock);
905 break;
906 }
Damien Miller95def091999-11-25 00:26:21 +1100907 new_socket(AUTH_CONNECTION, sock);
908 }
909 break;
910 case AUTH_CONNECTION:
911 if (buffer_len(&sockets[i].output) > 0 &&
912 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000913 do {
914 len = write(sockets[i].fd,
915 buffer_ptr(&sockets[i].output),
916 buffer_len(&sockets[i].output));
917 if (len == -1 && (errno == EAGAIN ||
918 errno == EINTR))
919 continue;
920 break;
921 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100922 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000923 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100924 break;
925 }
926 buffer_consume(&sockets[i].output, len);
927 }
928 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000929 do {
930 len = read(sockets[i].fd, buf, sizeof(buf));
931 if (len == -1 && (errno == EAGAIN ||
932 errno == EINTR))
933 continue;
934 break;
935 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100936 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000937 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100938 break;
939 }
940 buffer_append(&sockets[i].input, buf, len);
941 process_message(&sockets[i]);
942 }
943 break;
944 default:
945 fatal("Unknown type %d", sockets[i].type);
946 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000947}
948
Ben Lindstrombba81212001-06-25 05:01:22 +0000949static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000950cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000951{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000952 if (socket_name[0])
953 unlink(socket_name);
954 if (socket_dir[0])
955 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956}
957
Darren Tucker3e33cec2003-10-02 16:12:36 +1000958void
Damien Miller792c5111999-10-29 09:47:09 +1000959cleanup_exit(int i)
960{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000961 cleanup_socket();
962 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000963}
964
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000966cleanup_handler(int sig)
967{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000968 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000969 _exit(2);
970}
971
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000973check_parent_exists(int sig)
974{
975 int save_errno = errno;
976
977 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
978 /* printf("Parent has died - Authentication agent exiting.\n"); */
979 cleanup_handler(sig); /* safe */
980 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000981 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000982 alarm(10);
983 errno = save_errno;
984}
985
986static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000987usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000988{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000989 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000990 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000991 fprintf(stderr, "Options:\n");
992 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
993 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
994 fprintf(stderr, " -k Kill the current agent.\n");
995 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000996 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100997 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100998 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000999}
1000
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001001int
1002main(int ac, char **av)
1003{
Damien Miller6c711792003-01-24 11:36:23 +11001004 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001005 int sock, fd, ch;
1006 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001007 char *shell, *format, *pidstr, *agentsocket = NULL;
1008 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001009 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001010#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001011 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001012#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001013#ifdef HAVE_CYGWIN
1014 int prev_mask;
1015#endif
Damien Miller615f9392000-05-17 22:53:33 +10001016 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001017 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001018 pid_t pid;
1019 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001020
Damien Miller6cffb9a2002-09-04 16:20:26 +10001021 /* drop */
1022 setegid(getgid());
1023 setgid(getgid());
1024
Damien Miller6c4914a2004-03-03 11:08:59 +11001025#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1026 /* Disable ptrace on Linux without sgid bit */
1027 prctl(PR_SET_DUMPABLE, 0);
1028#endif
1029
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001030 SSLeay_add_all_algorithms();
1031
Damien Miller59d3d5b2003-08-22 09:34:41 +10001032 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001033 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001034 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001035
Damien Miller53d81482003-01-22 11:47:19 +11001036 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001037 switch (ch) {
1038 case 'c':
1039 if (s_flag)
1040 usage();
1041 c_flag++;
1042 break;
1043 case 'k':
1044 k_flag++;
1045 break;
1046 case 's':
1047 if (c_flag)
1048 usage();
1049 s_flag++;
1050 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001051 case 'd':
1052 if (d_flag)
1053 usage();
1054 d_flag++;
1055 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001056 case 'a':
1057 agentsocket = optarg;
1058 break;
Damien Miller53d81482003-01-22 11:47:19 +11001059 case 't':
1060 if ((lifetime = convtime(optarg)) == -1) {
1061 fprintf(stderr, "Invalid lifetime\n");
1062 usage();
1063 }
1064 break;
Damien Miller95def091999-11-25 00:26:21 +11001065 default:
1066 usage();
1067 }
Damien Miller792c5111999-10-29 09:47:09 +10001068 }
Damien Miller95def091999-11-25 00:26:21 +11001069 ac -= optind;
1070 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001071
Ben Lindstromd94580c2001-07-04 03:48:02 +00001072 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001073 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001074
Ben Lindstromeecdf232002-04-02 21:03:51 +00001075 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001076 shell = getenv("SHELL");
1077 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1078 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001079 }
Damien Miller95def091999-11-25 00:26:21 +11001080 if (k_flag) {
1081 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1082 if (pidstr == NULL) {
1083 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001084 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001085 exit(1);
1086 }
1087 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001088 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001089 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001090 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001091 exit(1);
1092 }
1093 if (kill(pid, SIGTERM) == -1) {
1094 perror("kill");
1095 exit(1);
1096 }
1097 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1098 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1099 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001100 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001101 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001102 }
Damien Miller95def091999-11-25 00:26:21 +11001103 parent_pid = getpid();
1104
Ben Lindstromb7788f32002-06-06 21:46:08 +00001105 if (agentsocket == NULL) {
1106 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001107 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001108 if (mkdtemp(socket_dir) == NULL) {
1109 perror("mkdtemp: private socket dir");
1110 exit(1);
1111 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001112 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1113 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001114 } else {
1115 /* Try to use specified agent socket */
1116 socket_dir[0] = '\0';
1117 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001118 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001119
Damien Miller5428f641999-11-25 11:54:57 +11001120 /*
1121 * Create socket early so it will exist before command gets run from
1122 * the parent.
1123 */
Damien Miller95def091999-11-25 00:26:21 +11001124 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1125 if (sock < 0) {
1126 perror("socket");
1127 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001128 }
Damien Miller95def091999-11-25 00:26:21 +11001129 memset(&sunaddr, 0, sizeof(sunaddr));
1130 sunaddr.sun_family = AF_UNIX;
1131 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001132#ifdef HAVE_CYGWIN
1133 prev_mask = umask(0177);
1134#endif
Damien Miller95def091999-11-25 00:26:21 +11001135 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1136 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001137#ifdef HAVE_CYGWIN
1138 umask(prev_mask);
1139#endif
Damien Miller95def091999-11-25 00:26:21 +11001140 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001142#ifdef HAVE_CYGWIN
1143 umask(prev_mask);
1144#endif
Darren Tucker3175eb92003-12-09 19:15:11 +11001145 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001146 perror("listen");
1147 cleanup_exit(1);
1148 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001149
Damien Miller5428f641999-11-25 11:54:57 +11001150 /*
1151 * Fork, and have the parent execute the command, if any, or present
1152 * the socket data. The child continues as the authentication agent.
1153 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001154 if (d_flag) {
1155 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1156 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1157 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1158 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001159 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001160 goto skip;
1161 }
Damien Miller95def091999-11-25 00:26:21 +11001162 pid = fork();
1163 if (pid == -1) {
1164 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001165 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001166 }
1167 if (pid != 0) { /* Parent - execute the given command. */
1168 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001169 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001170 if (ac == 0) {
1171 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1172 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001173 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001174 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001176 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001177 exit(0);
1178 }
Damien Millere4340be2000-09-16 13:29:08 +11001179 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1180 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1181 perror("setenv");
1182 exit(1);
1183 }
Damien Miller95def091999-11-25 00:26:21 +11001184 execvp(av[0], av);
1185 perror(av[0]);
1186 exit(1);
1187 }
Damien Millerc653b892002-02-08 22:05:41 +11001188 /* child */
1189 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001190
1191 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001192 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001193 cleanup_exit(1);
1194 }
1195
1196 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001197 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1198 /* XXX might close listen socket */
1199 (void)dup2(fd, STDIN_FILENO);
1200 (void)dup2(fd, STDOUT_FILENO);
1201 (void)dup2(fd, STDERR_FILENO);
1202 if (fd > 2)
1203 close(fd);
1204 }
Damien Miller95def091999-11-25 00:26:21 +11001205
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001206#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001207 /* deny core dumps, since memory contains unencrypted private keys */
1208 rlim.rlim_cur = rlim.rlim_max = 0;
1209 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001210 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001211 cleanup_exit(1);
1212 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001213#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001214
1215skip:
Damien Miller95def091999-11-25 00:26:21 +11001216 new_socket(AUTH_SOCKET, sock);
1217 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001218 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001219 alarm(10);
1220 }
Damien Millerad833b32000-08-23 10:46:23 +10001221 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001222 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001223 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001224 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001225 signal(SIGHUP, cleanup_handler);
1226 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001227 nalloc = 0;
1228
Damien Miller95def091999-11-25 00:26:21 +11001229 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001230 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001231 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001232 if (errno == EINTR)
1233 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001234 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001235 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001236 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001237 }
1238 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001239}