blob: b18dd980c2a3a8f8fd4f98921fb21803a39770a2 [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 Miller6c711792003-01-24 11:36:23 +110038RCSID("$OpenBSD: ssh-agent.c,v 1.107 2003/01/23 13:50:27 markus 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 "readpass.h"
54#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Ben Lindstrom3f471632001-07-04 03:53:15 +000056#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000057#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000058#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000059
Ben Lindstrom65366a82001-12-06 16:32:47 +000060typedef enum {
61 AUTH_UNUSED,
62 AUTH_SOCKET,
63 AUTH_CONNECTION
64} sock_type;
65
Damien Miller95def091999-11-25 00:26:21 +110066typedef struct {
67 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000068 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110069 Buffer input;
70 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000071 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072} SocketEntry;
73
Ben Lindstrom46c16222000-12-22 01:43:59 +000074u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075SocketEntry *sockets = NULL;
76
Damien Miller1a534ae2002-01-22 23:26:13 +110077typedef struct identity {
78 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100079 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110080 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000081 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110082 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083} Identity;
84
Damien Millerad833b32000-08-23 10:46:23 +100085typedef struct {
86 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +110087 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +100088} Idtab;
89
90/* private key table, one per protocol version */
91Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93int max_fd = 0;
94
95/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100096pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
98/* pathname and directory for AUTH_SOCKET */
99char socket_name[1024];
100char socket_dir[1024];
101
Ben Lindstrom2f717042002-06-06 21:52:03 +0000102/* locking */
103int locked = 0;
104char *lock_passwd = NULL;
105
Damien Miller95def091999-11-25 00:26:21 +1100106#ifdef HAVE___PROGNAME
107extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000108#else
109char *__progname;
110#endif
Damien Miller95def091999-11-25 00:26:21 +1100111
Damien Miller53d81482003-01-22 11:47:19 +1100112/* Default lifetime (0 == forever) */
113static int lifetime = 0;
114
Ben Lindstrombba81212001-06-25 05:01:22 +0000115static void
Damien Miller58f34862002-09-04 16:31:21 +1000116close_socket(SocketEntry *e)
117{
Damien Miller58f34862002-09-04 16:31:21 +1000118 close(e->fd);
119 e->fd = -1;
120 e->type = AUTH_UNUSED;
121 buffer_free(&e->input);
122 buffer_free(&e->output);
123 buffer_free(&e->request);
124}
125
126static void
Damien Millerad833b32000-08-23 10:46:23 +1000127idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Damien Millerad833b32000-08-23 10:46:23 +1000129 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000130
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000131 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100132 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000133 idtable[i].nentries = 0;
134 }
135}
136
137/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000139idtab_lookup(int version)
140{
141 if (version < 1 || version > 2)
142 fatal("internal error, bad protocol version %d", version);
143 return &idtable[version];
144}
145
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000146static void
147free_identity(Identity *id)
148{
149 key_free(id->key);
150 xfree(id->comment);
151 xfree(id);
152}
153
Damien Millerad833b32000-08-23 10:46:23 +1000154/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100155static Identity *
156lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000157{
Damien Miller1a534ae2002-01-22 23:26:13 +1100158 Identity *id;
159
Damien Millerad833b32000-08-23 10:46:23 +1000160 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100161 TAILQ_FOREACH(id, &tab->idlist, next) {
162 if (key_equal(key, id->key))
163 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000164 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100165 return (NULL);
166}
167
Damien Miller6c711792003-01-24 11:36:23 +1100168/* Check confirmation of keysign request */
169static int
170confirm_key(Identity *id)
171{
172 char *p, prompt[1024];
173 int ret = -1;
174
175 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
176 snprintf(prompt, sizeof(prompt), "Allow use of key %s?\n"
177 "Key fingerprint %s.", id->comment, p);
178 xfree(p);
179 p = read_passphrase(prompt, RP_ALLOW_EOF);
180 if (p != NULL) {
181 /*
182 * Accept empty responses and responses consisting
183 * of the word "yes" as affirmative.
184 */
185 if (*p == '\0' || *p == '\n' || strcasecmp(p, "yes") == 0)
186 ret = 0;
187 xfree(p);
188 }
189 return (ret);
190}
191
Damien Millerad833b32000-08-23 10:46:23 +1000192/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static void
Damien Millerad833b32000-08-23 10:46:23 +1000194process_request_identities(SocketEntry *e, int version)
195{
196 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100197 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000198 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
Damien Miller95def091999-11-25 00:26:21 +1100200 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000201 buffer_put_char(&msg, (version == 1) ?
202 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
203 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100204 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000206 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
207 buffer_put_bignum(&msg, id->key->rsa->e);
208 buffer_put_bignum(&msg, id->key->rsa->n);
209 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000210 u_char *blob;
211 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100212 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000213 buffer_put_string(&msg, blob, blen);
214 xfree(blob);
215 }
216 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100217 }
218 buffer_put_int(&e->output, buffer_len(&msg));
219 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
220 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221}
222
Damien Millerad833b32000-08-23 10:46:23 +1000223/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000224static void
Damien Millerad833b32000-08-23 10:46:23 +1000225process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000227 u_char buf[32], mdbuf[16], session_id[16];
228 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000229 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000230 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000231 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100232 Buffer msg;
233 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000234 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Damien Miller95def091999-11-25 00:26:21 +1100236 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100237 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100238 if ((challenge = BN_new()) == NULL)
239 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000240
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000241 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000242 buffer_get_bignum(&e->request, key->rsa->e);
243 buffer_get_bignum(&e->request, key->rsa->n);
244 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245
Damien Millerad833b32000-08-23 10:46:23 +1000246 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000247 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000248 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000249 buffer_get(&e->request, session_id, 16);
250 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000251 if (response_type != 1)
252 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller1a534ae2002-01-22 23:26:13 +1100254 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100255 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100256 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000257 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100258 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
259 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100260
Damien Millerad833b32000-08-23 10:46:23 +1000261 /* The response is MD5 of decrypted challenge plus session id. */
262 len = BN_num_bytes(challenge);
263 if (len <= 0 || len > 32) {
264 log("process_authentication_challenge: bad challenge length %d", len);
265 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100266 }
Damien Millerad833b32000-08-23 10:46:23 +1000267 memset(buf, 0, 32);
268 BN_bn2bin(challenge, buf + 32 - len);
269 MD5_Init(&md);
270 MD5_Update(&md, buf, 32);
271 MD5_Update(&md, session_id, 16);
272 MD5_Final(mdbuf, &md);
273
274 /* Send the response. */
275 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
276 for (i = 0; i < 16; i++)
277 buffer_put_char(&msg, mdbuf[i]);
278 goto send;
279 }
280
281failure:
282 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100283 buffer_put_char(&msg, SSH_AGENT_FAILURE);
284send:
285 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000286 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
287 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100288 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000289 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000290}
291
Damien Millerad833b32000-08-23 10:46:23 +1000292/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000293static void
Damien Millerad833b32000-08-23 10:46:23 +1000294process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000295{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000296 u_char *blob, *data, *signature = NULL;
297 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000298 extern int datafellows;
299 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000300 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000301 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000302
303 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000304
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000305 blob = buffer_get_string(&e->request, &blen);
306 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100307
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000308 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100309 if (flags & SSH_AGENT_OLD_SIGNATURE)
310 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000311
Damien Miller0bc1bd82000-11-13 22:57:25 +1100312 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000313 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100314 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100315 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100316 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000317 }
318 key_free(key);
319 buffer_init(&msg);
320 if (ok == 0) {
321 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
322 buffer_put_string(&msg, signature, slen);
323 } else {
324 buffer_put_char(&msg, SSH_AGENT_FAILURE);
325 }
326 buffer_put_int(&e->output, buffer_len(&msg));
327 buffer_append(&e->output, buffer_ptr(&msg),
328 buffer_len(&msg));
329 buffer_free(&msg);
330 xfree(data);
331 xfree(blob);
332 if (signature != NULL)
333 xfree(signature);
334}
335
336/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000337static void
Damien Millerad833b32000-08-23 10:46:23 +1000338process_remove_identity(SocketEntry *e, int version)
339{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000340 u_int blen, bits;
341 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100342 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000343 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100344
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000345 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000346 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100347 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000348 bits = buffer_get_int(&e->request);
349 buffer_get_bignum(&e->request, key->rsa->e);
350 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Millerad833b32000-08-23 10:46:23 +1000352 if (bits != key_size(key))
Ben Lindstrom822b6342002-06-23 21:38:49 +0000353 log("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000354 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000355 break;
356 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000357 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100358 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000359 xfree(blob);
360 break;
361 }
362 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100363 Identity *id = lookup_identity(key, version);
364 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100365 /*
366 * We have this key. Free the old key. Since we
367 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000368 * the array, we actually free the key there and move
369 * all the entries between the empty slot and the end
370 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100371 */
Damien Millerad833b32000-08-23 10:46:23 +1000372 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100373 if (tab->nentries < 1)
374 fatal("process_remove_identity: "
375 "internal error: tab->nentries %d",
376 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100377 TAILQ_REMOVE(&tab->idlist, id, next);
378 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000379 tab->nentries--;
380 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100381 }
Damien Millerad833b32000-08-23 10:46:23 +1000382 key_free(key);
383 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000384 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000385 buffer_put_char(&e->output,
386 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387}
388
Ben Lindstrombba81212001-06-25 05:01:22 +0000389static void
Damien Millerad833b32000-08-23 10:46:23 +1000390process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391{
Damien Millerad833b32000-08-23 10:46:23 +1000392 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100393 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100396 for (id = TAILQ_FIRST(&tab->idlist); id;
397 id = TAILQ_FIRST(&tab->idlist)) {
398 TAILQ_REMOVE(&tab->idlist, id, next);
399 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100400 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401
Damien Miller95def091999-11-25 00:26:21 +1100402 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000403 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
405 /* Send success. */
406 buffer_put_int(&e->output, 1);
407 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100408}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Ben Lindstrombba81212001-06-25 05:01:22 +0000410static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000411reaper(void)
412{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000413 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000414 Identity *id, *nxt;
415 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000416 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000417
418 for (version = 1; version < 3; version++) {
419 tab = idtab_lookup(version);
420 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
421 nxt = TAILQ_NEXT(id, next);
422 if (id->death != 0 && now >= id->death) {
423 TAILQ_REMOVE(&tab->idlist, id, next);
424 free_identity(id);
425 tab->nentries--;
426 }
427 }
428 }
429}
430
431static void
Damien Millerad833b32000-08-23 10:46:23 +1000432process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100433{
Damien Millerad833b32000-08-23 10:46:23 +1000434 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100435 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000436 char *type_name, *comment;
437 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100438
Damien Millerad833b32000-08-23 10:46:23 +1000439 switch (version) {
440 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100441 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000442 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000443 buffer_get_bignum(&e->request, k->rsa->n);
444 buffer_get_bignum(&e->request, k->rsa->e);
445 buffer_get_bignum(&e->request, k->rsa->d);
446 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100447
Damien Millerad833b32000-08-23 10:46:23 +1000448 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000449 buffer_get_bignum(&e->request, k->rsa->q); /* p */
450 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100451
Damien Millerad833b32000-08-23 10:46:23 +1000452 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000453 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000454 break;
455 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000457 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100458 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000459 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100460 case KEY_DSA:
461 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000462 buffer_get_bignum2(&e->request, k->dsa->p);
463 buffer_get_bignum2(&e->request, k->dsa->q);
464 buffer_get_bignum2(&e->request, k->dsa->g);
465 buffer_get_bignum2(&e->request, k->dsa->pub_key);
466 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100467 break;
468 case KEY_RSA:
469 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000470 buffer_get_bignum2(&e->request, k->rsa->n);
471 buffer_get_bignum2(&e->request, k->rsa->e);
472 buffer_get_bignum2(&e->request, k->rsa->d);
473 buffer_get_bignum2(&e->request, k->rsa->iqmp);
474 buffer_get_bignum2(&e->request, k->rsa->p);
475 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100476
477 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000478 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479 break;
480 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000481 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000482 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100483 }
Damien Millerad833b32000-08-23 10:46:23 +1000484 break;
485 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000486 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000487 if (k == NULL) {
488 xfree(comment);
489 goto send;
490 }
491 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000492 while (buffer_len(&e->request)) {
493 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000494 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000495 death = time(NULL) + buffer_get_int(&e->request);
496 break;
Damien Miller6c711792003-01-24 11:36:23 +1100497 case SSH_AGENT_CONSTRAIN_CONFIRM:
498 confirm = 1;
499 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000500 default:
501 break;
502 }
503 }
Damien Miller53d81482003-01-22 11:47:19 +1100504 if (lifetime && !death)
505 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000506 if (lookup_identity(k, version) == NULL) {
507 Identity *id = xmalloc(sizeof(Identity));
508 id->key = k;
509 id->comment = comment;
510 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100511 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000512 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
513 /* Increment the number of identities. */
514 tab->nentries++;
515 } else {
516 key_free(k);
517 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000518 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000519send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000520 buffer_put_int(&e->output, 1);
521 buffer_put_char(&e->output,
522 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
523}
524
Ben Lindstrom2f717042002-06-06 21:52:03 +0000525/* XXX todo: encrypt sensitive data with passphrase */
526static void
527process_lock_agent(SocketEntry *e, int lock)
528{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000529 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000530 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000531
532 passwd = buffer_get_string(&e->request, NULL);
533 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
534 locked = 0;
535 memset(lock_passwd, 0, strlen(lock_passwd));
536 xfree(lock_passwd);
537 lock_passwd = NULL;
538 success = 1;
539 } else if (!locked && lock) {
540 locked = 1;
541 lock_passwd = xstrdup(passwd);
542 success = 1;
543 }
544 memset(passwd, 0, strlen(passwd));
545 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000546
Ben Lindstrom2f717042002-06-06 21:52:03 +0000547 buffer_put_int(&e->output, 1);
548 buffer_put_char(&e->output,
549 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000550}
551
552static void
553no_identities(SocketEntry *e, u_int type)
554{
555 Buffer msg;
556
557 buffer_init(&msg);
558 buffer_put_char(&msg,
559 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
560 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
561 buffer_put_int(&msg, 0);
562 buffer_put_int(&e->output, buffer_len(&msg));
563 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
564 buffer_free(&msg);
565}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000566
567#ifdef SMARTCARD
568static void
569process_add_smartcard_key (SocketEntry *e)
570{
Ben Lindstromba72d302002-03-22 03:51:06 +0000571 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000572 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000573 Key **keys, *k;
574 Identity *id;
575 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100576
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000577 sc_reader_id = buffer_get_string(&e->request, NULL);
578 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000579 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000580 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000581 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000582
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000583 if (keys == NULL || keys[0] == NULL) {
584 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000585 goto send;
586 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000587 for (i = 0; keys[i] != NULL; i++) {
588 k = keys[i];
589 version = k->type == KEY_RSA1 ? 1 : 2;
590 tab = idtab_lookup(version);
591 if (lookup_identity(k, version) == NULL) {
592 id = xmalloc(sizeof(Identity));
593 id->key = k;
594 id->comment = xstrdup("smartcard key");
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000595 id->death = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100596 id->confirm = 0;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000597 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
598 tab->nentries++;
599 success = 1;
600 } else {
601 key_free(k);
602 }
603 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000604 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000605 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000606send:
607 buffer_put_int(&e->output, 1);
608 buffer_put_char(&e->output,
609 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
610}
611
612static void
613process_remove_smartcard_key(SocketEntry *e)
614{
Ben Lindstromba72d302002-03-22 03:51:06 +0000615 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000616 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000617 Key **keys, *k = NULL;
618 Identity *id;
619 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000620
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000621 sc_reader_id = buffer_get_string(&e->request, NULL);
622 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000623 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000624 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000625 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000626
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000627 if (keys == NULL || keys[0] == NULL) {
628 error("sc_get_keys failed");
629 goto send;
630 }
631 for (i = 0; keys[i] != NULL; i++) {
632 k = keys[i];
633 version = k->type == KEY_RSA1 ? 1 : 2;
634 if ((id = lookup_identity(k, version)) != NULL) {
635 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000636 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000637 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100638 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000639 success = 1;
640 }
641 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000642 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000643 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 xfree(keys);
645send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000646 buffer_put_int(&e->output, 1);
647 buffer_put_char(&e->output,
648 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
649}
Ben Lindstromffce1472001-08-06 21:57:31 +0000650#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000651
Damien Millerad833b32000-08-23 10:46:23 +1000652/* dispatch incoming messages */
653
Ben Lindstrombba81212001-06-25 05:01:22 +0000654static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000655process_message(SocketEntry *e)
656{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000657 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000658 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000659
660 /* kill dead keys */
661 reaper();
662
Damien Miller95def091999-11-25 00:26:21 +1100663 if (buffer_len(&e->input) < 5)
664 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100665 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100666 msg_len = GET_32BIT(cp);
667 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000668 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100669 return;
670 }
671 if (buffer_len(&e->input) < msg_len + 4)
672 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000673
674 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100675 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000676 buffer_clear(&e->request);
677 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
678 buffer_consume(&e->input, msg_len);
679 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000680
Ben Lindstrom2f717042002-06-06 21:52:03 +0000681 /* check wheter agent is locked */
682 if (locked && type != SSH_AGENTC_UNLOCK) {
683 buffer_clear(&e->request);
684 switch (type) {
685 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
686 case SSH2_AGENTC_REQUEST_IDENTITIES:
687 /* send empty lists */
688 no_identities(e, type);
689 break;
690 default:
691 /* send a fail message for all other request types */
692 buffer_put_int(&e->output, 1);
693 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
694 }
695 return;
696 }
697
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100699 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000700 case SSH_AGENTC_LOCK:
701 case SSH_AGENTC_UNLOCK:
702 process_lock_agent(e, type == SSH_AGENTC_LOCK);
703 break;
Damien Millerad833b32000-08-23 10:46:23 +1000704 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100705 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000706 process_authentication_challenge1(e);
707 break;
708 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
709 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100710 break;
711 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000712 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000713 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100714 break;
715 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000716 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100717 break;
718 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000719 process_remove_all_identities(e, 1);
720 break;
721 /* ssh2 */
722 case SSH2_AGENTC_SIGN_REQUEST:
723 process_sign_request2(e);
724 break;
725 case SSH2_AGENTC_REQUEST_IDENTITIES:
726 process_request_identities(e, 2);
727 break;
728 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000729 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000730 process_add_identity(e, 2);
731 break;
732 case SSH2_AGENTC_REMOVE_IDENTITY:
733 process_remove_identity(e, 2);
734 break;
735 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
736 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100737 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000738#ifdef SMARTCARD
739 case SSH_AGENTC_ADD_SMARTCARD_KEY:
740 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100741 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000742 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
743 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100744 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000745#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100746 default:
747 /* Unknown message. Respond with failure. */
748 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000749 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100750 buffer_put_int(&e->output, 1);
751 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
752 break;
753 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000754}
755
Ben Lindstrombba81212001-06-25 05:01:22 +0000756static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000757new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000758{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000759 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000760
Damien Miller95def091999-11-25 00:26:21 +1100761 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
762 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000763
Damien Miller95def091999-11-25 00:26:21 +1100764 if (fd > max_fd)
765 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000766
Damien Miller95def091999-11-25 00:26:21 +1100767 for (i = 0; i < sockets_alloc; i++)
768 if (sockets[i].type == AUTH_UNUSED) {
769 sockets[i].fd = fd;
770 sockets[i].type = type;
771 buffer_init(&sockets[i].input);
772 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000773 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100774 return;
775 }
776 old_alloc = sockets_alloc;
777 sockets_alloc += 10;
778 if (sockets)
779 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
780 else
781 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
782 for (i = old_alloc; i < sockets_alloc; i++)
783 sockets[i].type = AUTH_UNUSED;
784 sockets[old_alloc].type = type;
785 sockets[old_alloc].fd = fd;
786 buffer_init(&sockets[old_alloc].input);
787 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000788 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789}
790
Ben Lindstrombba81212001-06-25 05:01:22 +0000791static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000792prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000794 u_int i, sz;
795 int n = 0;
796
797 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100798 switch (sockets[i].type) {
799 case AUTH_SOCKET:
800 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000801 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100802 break;
803 case AUTH_UNUSED:
804 break;
805 default:
806 fatal("Unknown socket type %d", sockets[i].type);
807 break;
808 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000809 }
810
811 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000812 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000813 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000814 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000815 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000816 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000817 *fdrp = xmalloc(sz);
818 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000819 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000820 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000821 if (n < *fdl)
822 debug("XXX shrink: %d < %d", n, *fdl);
823 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000824 memset(*fdrp, 0, sz);
825 memset(*fdwp, 0, sz);
826
827 for (i = 0; i < sockets_alloc; i++) {
828 switch (sockets[i].type) {
829 case AUTH_SOCKET:
830 case AUTH_CONNECTION:
831 FD_SET(sockets[i].fd, *fdrp);
832 if (buffer_len(&sockets[i].output) > 0)
833 FD_SET(sockets[i].fd, *fdwp);
834 break;
835 default:
836 break;
837 }
838 }
839 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000840}
841
Ben Lindstrombba81212001-06-25 05:01:22 +0000842static void
Damien Miller95def091999-11-25 00:26:21 +1100843after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000844{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000845 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100846 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100847 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000848 int len, sock;
849 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000850 uid_t euid;
851 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000852
Damien Miller95def091999-11-25 00:26:21 +1100853 for (i = 0; i < sockets_alloc; i++)
854 switch (sockets[i].type) {
855 case AUTH_UNUSED:
856 break;
857 case AUTH_SOCKET:
858 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100859 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000860 sock = accept(sockets[i].fd,
861 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100862 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100863 error("accept from AUTH_SOCKET: %s",
864 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100865 break;
866 }
Damien Miller538f1812002-09-12 09:51:10 +1000867 if (getpeereid(sock, &euid, &egid) < 0) {
868 error("getpeereid %d failed: %s",
869 sock, strerror(errno));
870 close(sock);
871 break;
872 }
Damien Milleraf9de382002-10-03 11:54:35 +1000873 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000874 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000875 "peer euid %u != uid %u",
876 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000877 close(sock);
878 break;
879 }
Damien Miller95def091999-11-25 00:26:21 +1100880 new_socket(AUTH_CONNECTION, sock);
881 }
882 break;
883 case AUTH_CONNECTION:
884 if (buffer_len(&sockets[i].output) > 0 &&
885 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000886 do {
887 len = write(sockets[i].fd,
888 buffer_ptr(&sockets[i].output),
889 buffer_len(&sockets[i].output));
890 if (len == -1 && (errno == EAGAIN ||
891 errno == EINTR))
892 continue;
893 break;
894 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100895 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000896 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100897 break;
898 }
899 buffer_consume(&sockets[i].output, len);
900 }
901 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000902 do {
903 len = read(sockets[i].fd, buf, sizeof(buf));
904 if (len == -1 && (errno == EAGAIN ||
905 errno == EINTR))
906 continue;
907 break;
908 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100909 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000910 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100911 break;
912 }
913 buffer_append(&sockets[i].input, buf, len);
914 process_message(&sockets[i]);
915 }
916 break;
917 default:
918 fatal("Unknown type %d", sockets[i].type);
919 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000920}
921
Ben Lindstrombba81212001-06-25 05:01:22 +0000922static void
Damien Millerc653b892002-02-08 22:05:41 +1100923cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000924{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000925 if (socket_name[0])
926 unlink(socket_name);
927 if (socket_dir[0])
928 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000929}
930
Ben Lindstrombba81212001-06-25 05:01:22 +0000931static void
Damien Miller792c5111999-10-29 09:47:09 +1000932cleanup_exit(int i)
933{
Damien Millerc653b892002-02-08 22:05:41 +1100934 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100935 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000936}
937
Ben Lindstrombba81212001-06-25 05:01:22 +0000938static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000939cleanup_handler(int sig)
940{
Damien Millerc653b892002-02-08 22:05:41 +1100941 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000942 _exit(2);
943}
944
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000946check_parent_exists(int sig)
947{
948 int save_errno = errno;
949
950 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
951 /* printf("Parent has died - Authentication agent exiting.\n"); */
952 cleanup_handler(sig); /* safe */
953 }
954 signal(SIGALRM, check_parent_exists);
955 alarm(10);
956 errno = save_errno;
957}
958
959static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000960usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000961{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000962 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000963 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000964 fprintf(stderr, "Options:\n");
965 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
966 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
967 fprintf(stderr, " -k Kill the current agent.\n");
968 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000969 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100970 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100971 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000972}
973
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000974int
975main(int ac, char **av)
976{
Damien Miller6c711792003-01-24 11:36:23 +1100977 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
978 int sock, fd, ch, nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000979 char *shell, *format, *pidstr, *agentsocket = NULL;
980 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100981 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000982#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000983 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000984#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000985#ifdef HAVE_CYGWIN
986 int prev_mask;
987#endif
Damien Miller615f9392000-05-17 22:53:33 +1000988 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +0000989 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000990 pid_t pid;
991 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +0000992
Damien Miller6cffb9a2002-09-04 16:20:26 +1000993 /* drop */
994 setegid(getgid());
995 setgid(getgid());
996
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000997 SSLeay_add_all_algorithms();
998
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000999 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001000 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001001 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001002
Damien Miller53d81482003-01-22 11:47:19 +11001003 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001004 switch (ch) {
1005 case 'c':
1006 if (s_flag)
1007 usage();
1008 c_flag++;
1009 break;
1010 case 'k':
1011 k_flag++;
1012 break;
1013 case 's':
1014 if (c_flag)
1015 usage();
1016 s_flag++;
1017 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001018 case 'd':
1019 if (d_flag)
1020 usage();
1021 d_flag++;
1022 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001023 case 'a':
1024 agentsocket = optarg;
1025 break;
Damien Miller53d81482003-01-22 11:47:19 +11001026 case 't':
1027 if ((lifetime = convtime(optarg)) == -1) {
1028 fprintf(stderr, "Invalid lifetime\n");
1029 usage();
1030 }
1031 break;
Damien Miller95def091999-11-25 00:26:21 +11001032 default:
1033 usage();
1034 }
Damien Miller792c5111999-10-29 09:47:09 +10001035 }
Damien Miller95def091999-11-25 00:26:21 +11001036 ac -= optind;
1037 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001038
Ben Lindstromd94580c2001-07-04 03:48:02 +00001039 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001040 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001041
Ben Lindstromeecdf232002-04-02 21:03:51 +00001042 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001043 shell = getenv("SHELL");
1044 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1045 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001046 }
Damien Miller95def091999-11-25 00:26:21 +11001047 if (k_flag) {
1048 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1049 if (pidstr == NULL) {
1050 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001051 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001052 exit(1);
1053 }
1054 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001055 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001056 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001057 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001058 exit(1);
1059 }
1060 if (kill(pid, SIGTERM) == -1) {
1061 perror("kill");
1062 exit(1);
1063 }
1064 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1065 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1066 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001067 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001068 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001069 }
Damien Miller95def091999-11-25 00:26:21 +11001070 parent_pid = getpid();
1071
Ben Lindstromb7788f32002-06-06 21:46:08 +00001072 if (agentsocket == NULL) {
1073 /* Create private directory for agent socket */
1074 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1075 if (mkdtemp(socket_dir) == NULL) {
1076 perror("mkdtemp: private socket dir");
1077 exit(1);
1078 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001079 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1080 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001081 } else {
1082 /* Try to use specified agent socket */
1083 socket_dir[0] = '\0';
1084 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001085 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001086
Damien Miller5428f641999-11-25 11:54:57 +11001087 /*
1088 * Create socket early so it will exist before command gets run from
1089 * the parent.
1090 */
Damien Miller95def091999-11-25 00:26:21 +11001091 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1092 if (sock < 0) {
1093 perror("socket");
1094 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001095 }
Damien Miller95def091999-11-25 00:26:21 +11001096 memset(&sunaddr, 0, sizeof(sunaddr));
1097 sunaddr.sun_family = AF_UNIX;
1098 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001099#ifdef HAVE_CYGWIN
1100 prev_mask = umask(0177);
1101#endif
Damien Miller95def091999-11-25 00:26:21 +11001102 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1103 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001104#ifdef HAVE_CYGWIN
1105 umask(prev_mask);
1106#endif
Damien Miller95def091999-11-25 00:26:21 +11001107 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001108 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001109#ifdef HAVE_CYGWIN
1110 umask(prev_mask);
1111#endif
Damien Miller4efdfff2002-09-04 16:28:18 +10001112 if (listen(sock, 128) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001113 perror("listen");
1114 cleanup_exit(1);
1115 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001116
Damien Miller5428f641999-11-25 11:54:57 +11001117 /*
1118 * Fork, and have the parent execute the command, if any, or present
1119 * the socket data. The child continues as the authentication agent.
1120 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001121 if (d_flag) {
1122 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1123 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1124 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1125 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001126 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001127 goto skip;
1128 }
Damien Miller95def091999-11-25 00:26:21 +11001129 pid = fork();
1130 if (pid == -1) {
1131 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001132 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001133 }
1134 if (pid != 0) { /* Parent - execute the given command. */
1135 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001136 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001137 if (ac == 0) {
1138 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1139 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001140 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001141 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001142 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001143 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001144 exit(0);
1145 }
Damien Millere4340be2000-09-16 13:29:08 +11001146 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1147 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1148 perror("setenv");
1149 exit(1);
1150 }
Damien Miller95def091999-11-25 00:26:21 +11001151 execvp(av[0], av);
1152 perror(av[0]);
1153 exit(1);
1154 }
Damien Millerc653b892002-02-08 22:05:41 +11001155 /* child */
1156 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001157
1158 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001159 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001160 cleanup_exit(1);
1161 }
1162
1163 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001164 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1165 /* XXX might close listen socket */
1166 (void)dup2(fd, STDIN_FILENO);
1167 (void)dup2(fd, STDOUT_FILENO);
1168 (void)dup2(fd, STDERR_FILENO);
1169 if (fd > 2)
1170 close(fd);
1171 }
Damien Miller95def091999-11-25 00:26:21 +11001172
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001173#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001174 /* deny core dumps, since memory contains unencrypted private keys */
1175 rlim.rlim_cur = rlim.rlim_max = 0;
1176 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001177 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001178 cleanup_exit(1);
1179 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001180#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001181
1182skip:
Damien Millerc653b892002-02-08 22:05:41 +11001183 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001184 new_socket(AUTH_SOCKET, sock);
1185 if (ac > 0) {
1186 signal(SIGALRM, check_parent_exists);
1187 alarm(10);
1188 }
Damien Millerad833b32000-08-23 10:46:23 +10001189 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001190 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001191 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001192 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001193 signal(SIGHUP, cleanup_handler);
1194 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001195 nalloc = 0;
1196
Damien Miller95def091999-11-25 00:26:21 +11001197 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001198 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001199 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001200 if (errno == EINTR)
1201 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001202 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001203 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001204 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001205 }
1206 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207}