blob: c05c61468c93e22ce0d62413b72acf12aadbb414 [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 Miller56a0bb02003-06-18 20:28:40 +100038RCSID("$OpenBSD: ssh-agent.c,v 1.111 2003/06/12 19:12:03 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) {
Damien Miller996acd22003-04-09 20:59:48 +1000264 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000265 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))
Damien Miller996acd22003-04-09 20:59:48 +1000353 logit("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 }
Damien Millerc51d0732003-03-15 11:37:09 +1100486 /* enable blinding */
487 switch (k->type) {
488 case KEY_RSA:
489 case KEY_RSA1:
490 if (RSA_blinding_on(k->rsa, NULL) != 1) {
491 error("process_add_identity: RSA_blinding_on failed");
492 key_free(k);
493 goto send;
494 }
495 break;
496 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000497 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000498 if (k == NULL) {
499 xfree(comment);
500 goto send;
501 }
502 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000503 while (buffer_len(&e->request)) {
504 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000505 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000506 death = time(NULL) + buffer_get_int(&e->request);
507 break;
Damien Miller6c711792003-01-24 11:36:23 +1100508 case SSH_AGENT_CONSTRAIN_CONFIRM:
509 confirm = 1;
510 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000511 default:
512 break;
513 }
514 }
Damien Miller53d81482003-01-22 11:47:19 +1100515 if (lifetime && !death)
516 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000517 if (lookup_identity(k, version) == NULL) {
518 Identity *id = xmalloc(sizeof(Identity));
519 id->key = k;
520 id->comment = comment;
521 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100522 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000523 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
524 /* Increment the number of identities. */
525 tab->nentries++;
526 } else {
527 key_free(k);
528 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000529 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000530send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000531 buffer_put_int(&e->output, 1);
532 buffer_put_char(&e->output,
533 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
534}
535
Ben Lindstrom2f717042002-06-06 21:52:03 +0000536/* XXX todo: encrypt sensitive data with passphrase */
537static void
538process_lock_agent(SocketEntry *e, int lock)
539{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000540 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000541 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000542
543 passwd = buffer_get_string(&e->request, NULL);
544 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
545 locked = 0;
546 memset(lock_passwd, 0, strlen(lock_passwd));
547 xfree(lock_passwd);
548 lock_passwd = NULL;
549 success = 1;
550 } else if (!locked && lock) {
551 locked = 1;
552 lock_passwd = xstrdup(passwd);
553 success = 1;
554 }
555 memset(passwd, 0, strlen(passwd));
556 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000557
Ben Lindstrom2f717042002-06-06 21:52:03 +0000558 buffer_put_int(&e->output, 1);
559 buffer_put_char(&e->output,
560 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000561}
562
563static void
564no_identities(SocketEntry *e, u_int type)
565{
566 Buffer msg;
567
568 buffer_init(&msg);
569 buffer_put_char(&msg,
570 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
571 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
572 buffer_put_int(&msg, 0);
573 buffer_put_int(&e->output, buffer_len(&msg));
574 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
575 buffer_free(&msg);
576}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000577
578#ifdef SMARTCARD
579static void
580process_add_smartcard_key (SocketEntry *e)
581{
Ben Lindstromba72d302002-03-22 03:51:06 +0000582 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000583 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000584 Key **keys, *k;
585 Identity *id;
586 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100587
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000588 sc_reader_id = buffer_get_string(&e->request, NULL);
589 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000590
591 while (buffer_len(&e->request)) {
592 switch (buffer_get_char(&e->request)) {
593 case SSH_AGENT_CONSTRAIN_LIFETIME:
594 death = time(NULL) + buffer_get_int(&e->request);
595 break;
596 case SSH_AGENT_CONSTRAIN_CONFIRM:
597 confirm = 1;
598 break;
599 default:
600 break;
601 }
602 }
603 if (lifetime && !death)
604 death = time(NULL) + lifetime;
605
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000606 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000607 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000608 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000609
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000610 if (keys == NULL || keys[0] == NULL) {
611 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000612 goto send;
613 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000614 for (i = 0; keys[i] != NULL; i++) {
615 k = keys[i];
616 version = k->type == KEY_RSA1 ? 1 : 2;
617 tab = idtab_lookup(version);
618 if (lookup_identity(k, version) == NULL) {
619 id = xmalloc(sizeof(Identity));
620 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000621 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000622 id->death = death;
623 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000624 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
625 tab->nentries++;
626 success = 1;
627 } else {
628 key_free(k);
629 }
630 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000631 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000632 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000633send:
634 buffer_put_int(&e->output, 1);
635 buffer_put_char(&e->output,
636 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
637}
638
639static void
640process_remove_smartcard_key(SocketEntry *e)
641{
Ben Lindstromba72d302002-03-22 03:51:06 +0000642 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000643 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000644 Key **keys, *k = NULL;
645 Identity *id;
646 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000647
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000648 sc_reader_id = buffer_get_string(&e->request, NULL);
649 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000650 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000651 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000652 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000653
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000654 if (keys == NULL || keys[0] == NULL) {
655 error("sc_get_keys failed");
656 goto send;
657 }
658 for (i = 0; keys[i] != NULL; i++) {
659 k = keys[i];
660 version = k->type == KEY_RSA1 ? 1 : 2;
661 if ((id = lookup_identity(k, version)) != NULL) {
662 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000663 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000664 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100665 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000666 success = 1;
667 }
668 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000669 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000670 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000671 xfree(keys);
672send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000673 buffer_put_int(&e->output, 1);
674 buffer_put_char(&e->output,
675 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
676}
Ben Lindstromffce1472001-08-06 21:57:31 +0000677#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000678
Damien Millerad833b32000-08-23 10:46:23 +1000679/* dispatch incoming messages */
680
Ben Lindstrombba81212001-06-25 05:01:22 +0000681static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682process_message(SocketEntry *e)
683{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000684 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000685 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000686
687 /* kill dead keys */
688 reaper();
689
Damien Miller95def091999-11-25 00:26:21 +1100690 if (buffer_len(&e->input) < 5)
691 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100692 cp = buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100693 msg_len = GET_32BIT(cp);
694 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000695 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100696 return;
697 }
698 if (buffer_len(&e->input) < msg_len + 4)
699 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000700
701 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100702 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000703 buffer_clear(&e->request);
704 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
705 buffer_consume(&e->input, msg_len);
706 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707
Ben Lindstrom2f717042002-06-06 21:52:03 +0000708 /* check wheter agent is locked */
709 if (locked && type != SSH_AGENTC_UNLOCK) {
710 buffer_clear(&e->request);
711 switch (type) {
712 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
713 case SSH2_AGENTC_REQUEST_IDENTITIES:
714 /* send empty lists */
715 no_identities(e, type);
716 break;
717 default:
718 /* send a fail message for all other request types */
719 buffer_put_int(&e->output, 1);
720 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
721 }
722 return;
723 }
724
Ben Lindstrom3f471632001-07-04 03:53:15 +0000725 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100726 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000727 case SSH_AGENTC_LOCK:
728 case SSH_AGENTC_UNLOCK:
729 process_lock_agent(e, type == SSH_AGENTC_LOCK);
730 break;
Damien Millerad833b32000-08-23 10:46:23 +1000731 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100732 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000733 process_authentication_challenge1(e);
734 break;
735 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
736 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100737 break;
738 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000739 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000740 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100741 break;
742 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000743 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100744 break;
745 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000746 process_remove_all_identities(e, 1);
747 break;
748 /* ssh2 */
749 case SSH2_AGENTC_SIGN_REQUEST:
750 process_sign_request2(e);
751 break;
752 case SSH2_AGENTC_REQUEST_IDENTITIES:
753 process_request_identities(e, 2);
754 break;
755 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000756 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000757 process_add_identity(e, 2);
758 break;
759 case SSH2_AGENTC_REMOVE_IDENTITY:
760 process_remove_identity(e, 2);
761 break;
762 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
763 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100764 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000765#ifdef SMARTCARD
766 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000767 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000768 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100769 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000770 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
771 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100772 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000773#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100774 default:
775 /* Unknown message. Respond with failure. */
776 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000777 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100778 buffer_put_int(&e->output, 1);
779 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
780 break;
781 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000782}
783
Ben Lindstrombba81212001-06-25 05:01:22 +0000784static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000785new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000787 u_int i, old_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000788
Damien Miller95def091999-11-25 00:26:21 +1100789 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
790 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791
Damien Miller95def091999-11-25 00:26:21 +1100792 if (fd > max_fd)
793 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794
Damien Miller95def091999-11-25 00:26:21 +1100795 for (i = 0; i < sockets_alloc; i++)
796 if (sockets[i].type == AUTH_UNUSED) {
797 sockets[i].fd = fd;
798 sockets[i].type = type;
799 buffer_init(&sockets[i].input);
800 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000801 buffer_init(&sockets[i].request);
Damien Miller95def091999-11-25 00:26:21 +1100802 return;
803 }
804 old_alloc = sockets_alloc;
805 sockets_alloc += 10;
806 if (sockets)
807 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
808 else
809 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
810 for (i = old_alloc; i < sockets_alloc; i++)
811 sockets[i].type = AUTH_UNUSED;
812 sockets[old_alloc].type = type;
813 sockets[old_alloc].fd = fd;
814 buffer_init(&sockets[old_alloc].input);
815 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000816 buffer_init(&sockets[old_alloc].request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817}
818
Ben Lindstrombba81212001-06-25 05:01:22 +0000819static int
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000820prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000822 u_int i, sz;
823 int n = 0;
824
825 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100826 switch (sockets[i].type) {
827 case AUTH_SOCKET:
828 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000829 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100830 break;
831 case AUTH_UNUSED:
832 break;
833 default:
834 fatal("Unknown socket type %d", sockets[i].type);
835 break;
836 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000837 }
838
839 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000840 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000841 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000842 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000844 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 *fdrp = xmalloc(sz);
846 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000847 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000848 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000849 if (n < *fdl)
850 debug("XXX shrink: %d < %d", n, *fdl);
851 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 memset(*fdrp, 0, sz);
853 memset(*fdwp, 0, sz);
854
855 for (i = 0; i < sockets_alloc; i++) {
856 switch (sockets[i].type) {
857 case AUTH_SOCKET:
858 case AUTH_CONNECTION:
859 FD_SET(sockets[i].fd, *fdrp);
860 if (buffer_len(&sockets[i].output) > 0)
861 FD_SET(sockets[i].fd, *fdwp);
862 break;
863 default:
864 break;
865 }
866 }
867 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000868}
869
Ben Lindstrombba81212001-06-25 05:01:22 +0000870static void
Damien Miller95def091999-11-25 00:26:21 +1100871after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000873 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100874 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100875 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000876 int len, sock;
877 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000878 uid_t euid;
879 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880
Damien Miller95def091999-11-25 00:26:21 +1100881 for (i = 0; i < sockets_alloc; i++)
882 switch (sockets[i].type) {
883 case AUTH_UNUSED:
884 break;
885 case AUTH_SOCKET:
886 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100887 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000888 sock = accept(sockets[i].fd,
889 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100890 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100891 error("accept from AUTH_SOCKET: %s",
892 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100893 break;
894 }
Damien Miller538f1812002-09-12 09:51:10 +1000895 if (getpeereid(sock, &euid, &egid) < 0) {
896 error("getpeereid %d failed: %s",
897 sock, strerror(errno));
898 close(sock);
899 break;
900 }
Damien Milleraf9de382002-10-03 11:54:35 +1000901 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000902 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000903 "peer euid %u != uid %u",
904 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000905 close(sock);
906 break;
907 }
Damien Miller95def091999-11-25 00:26:21 +1100908 new_socket(AUTH_CONNECTION, sock);
909 }
910 break;
911 case AUTH_CONNECTION:
912 if (buffer_len(&sockets[i].output) > 0 &&
913 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000914 do {
915 len = write(sockets[i].fd,
916 buffer_ptr(&sockets[i].output),
917 buffer_len(&sockets[i].output));
918 if (len == -1 && (errno == EAGAIN ||
919 errno == EINTR))
920 continue;
921 break;
922 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100923 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000924 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100925 break;
926 }
927 buffer_consume(&sockets[i].output, len);
928 }
929 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000930 do {
931 len = read(sockets[i].fd, buf, sizeof(buf));
932 if (len == -1 && (errno == EAGAIN ||
933 errno == EINTR))
934 continue;
935 break;
936 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100937 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000938 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100939 break;
940 }
941 buffer_append(&sockets[i].input, buf, len);
942 process_message(&sockets[i]);
943 }
944 break;
945 default:
946 fatal("Unknown type %d", sockets[i].type);
947 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000948}
949
Ben Lindstrombba81212001-06-25 05:01:22 +0000950static void
Damien Millerc653b892002-02-08 22:05:41 +1100951cleanup_socket(void *p)
Damien Miller792c5111999-10-29 09:47:09 +1000952{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000953 if (socket_name[0])
954 unlink(socket_name);
955 if (socket_dir[0])
956 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957}
958
Ben Lindstrombba81212001-06-25 05:01:22 +0000959static void
Damien Miller792c5111999-10-29 09:47:09 +1000960cleanup_exit(int i)
961{
Damien Millerc653b892002-02-08 22:05:41 +1100962 cleanup_socket(NULL);
Damien Miller95def091999-11-25 00:26:21 +1100963 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000964}
965
Ben Lindstrombba81212001-06-25 05:01:22 +0000966static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000967cleanup_handler(int sig)
968{
Damien Millerc653b892002-02-08 22:05:41 +1100969 cleanup_socket(NULL);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000970 _exit(2);
971}
972
Ben Lindstrombba81212001-06-25 05:01:22 +0000973static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000974check_parent_exists(int sig)
975{
976 int save_errno = errno;
977
978 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
979 /* printf("Parent has died - Authentication agent exiting.\n"); */
980 cleanup_handler(sig); /* safe */
981 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000982 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000983 alarm(10);
984 errno = save_errno;
985}
986
987static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000988usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000989{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000990 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000991 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000992 fprintf(stderr, "Options:\n");
993 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
994 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
995 fprintf(stderr, " -k Kill the current agent.\n");
996 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000997 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100998 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100999 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001000}
1001
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002int
1003main(int ac, char **av)
1004{
Damien Miller6c711792003-01-24 11:36:23 +11001005 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
1006 int sock, fd, ch, 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
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001025 SSLeay_add_all_algorithms();
1026
Damien Miller59d3d5b2003-08-22 09:34:41 +10001027 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001028 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001029 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001030
Damien Miller53d81482003-01-22 11:47:19 +11001031 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001032 switch (ch) {
1033 case 'c':
1034 if (s_flag)
1035 usage();
1036 c_flag++;
1037 break;
1038 case 'k':
1039 k_flag++;
1040 break;
1041 case 's':
1042 if (c_flag)
1043 usage();
1044 s_flag++;
1045 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001046 case 'd':
1047 if (d_flag)
1048 usage();
1049 d_flag++;
1050 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001051 case 'a':
1052 agentsocket = optarg;
1053 break;
Damien Miller53d81482003-01-22 11:47:19 +11001054 case 't':
1055 if ((lifetime = convtime(optarg)) == -1) {
1056 fprintf(stderr, "Invalid lifetime\n");
1057 usage();
1058 }
1059 break;
Damien Miller95def091999-11-25 00:26:21 +11001060 default:
1061 usage();
1062 }
Damien Miller792c5111999-10-29 09:47:09 +10001063 }
Damien Miller95def091999-11-25 00:26:21 +11001064 ac -= optind;
1065 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001066
Ben Lindstromd94580c2001-07-04 03:48:02 +00001067 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001068 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001069
Ben Lindstromeecdf232002-04-02 21:03:51 +00001070 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001071 shell = getenv("SHELL");
1072 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
1073 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001074 }
Damien Miller95def091999-11-25 00:26:21 +11001075 if (k_flag) {
1076 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1077 if (pidstr == NULL) {
1078 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001079 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001080 exit(1);
1081 }
1082 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001083 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +11001084 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001085 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +11001086 exit(1);
1087 }
1088 if (kill(pid, SIGTERM) == -1) {
1089 perror("kill");
1090 exit(1);
1091 }
1092 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1093 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1094 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001095 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001096 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001097 }
Damien Miller95def091999-11-25 00:26:21 +11001098 parent_pid = getpid();
1099
Ben Lindstromb7788f32002-06-06 21:46:08 +00001100 if (agentsocket == NULL) {
1101 /* Create private directory for agent socket */
1102 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
1103 if (mkdtemp(socket_dir) == NULL) {
1104 perror("mkdtemp: private socket dir");
1105 exit(1);
1106 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001107 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1108 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001109 } else {
1110 /* Try to use specified agent socket */
1111 socket_dir[0] = '\0';
1112 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001113 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001114
Damien Miller5428f641999-11-25 11:54:57 +11001115 /*
1116 * Create socket early so it will exist before command gets run from
1117 * the parent.
1118 */
Damien Miller95def091999-11-25 00:26:21 +11001119 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1120 if (sock < 0) {
1121 perror("socket");
1122 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001123 }
Damien Miller95def091999-11-25 00:26:21 +11001124 memset(&sunaddr, 0, sizeof(sunaddr));
1125 sunaddr.sun_family = AF_UNIX;
1126 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001127#ifdef HAVE_CYGWIN
1128 prev_mask = umask(0177);
1129#endif
Damien Miller95def091999-11-25 00:26:21 +11001130 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
1131 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +00001132#ifdef HAVE_CYGWIN
1133 umask(prev_mask);
1134#endif
Damien Miller95def091999-11-25 00:26:21 +11001135 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001137#ifdef HAVE_CYGWIN
1138 umask(prev_mask);
1139#endif
Damien Miller4efdfff2002-09-04 16:28:18 +10001140 if (listen(sock, 128) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001141 perror("listen");
1142 cleanup_exit(1);
1143 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001144
Damien Miller5428f641999-11-25 11:54:57 +11001145 /*
1146 * Fork, and have the parent execute the command, if any, or present
1147 * the socket data. The child continues as the authentication agent.
1148 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001149 if (d_flag) {
1150 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1151 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1152 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1153 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001154 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001155 goto skip;
1156 }
Damien Miller95def091999-11-25 00:26:21 +11001157 pid = fork();
1158 if (pid == -1) {
1159 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001160 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001161 }
1162 if (pid != 0) { /* Parent - execute the given command. */
1163 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001164 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001165 if (ac == 0) {
1166 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1167 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001168 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001169 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001170 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001171 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001172 exit(0);
1173 }
Damien Millere4340be2000-09-16 13:29:08 +11001174 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1175 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1176 perror("setenv");
1177 exit(1);
1178 }
Damien Miller95def091999-11-25 00:26:21 +11001179 execvp(av[0], av);
1180 perror(av[0]);
1181 exit(1);
1182 }
Damien Millerc653b892002-02-08 22:05:41 +11001183 /* child */
1184 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001185
1186 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001187 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001188 cleanup_exit(1);
1189 }
1190
1191 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001192 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1193 /* XXX might close listen socket */
1194 (void)dup2(fd, STDIN_FILENO);
1195 (void)dup2(fd, STDOUT_FILENO);
1196 (void)dup2(fd, STDERR_FILENO);
1197 if (fd > 2)
1198 close(fd);
1199 }
Damien Miller95def091999-11-25 00:26:21 +11001200
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001201#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001202 /* deny core dumps, since memory contains unencrypted private keys */
1203 rlim.rlim_cur = rlim.rlim_max = 0;
1204 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001205 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001206 cleanup_exit(1);
1207 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001208#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001209
1210skip:
Damien Millerc653b892002-02-08 22:05:41 +11001211 fatal_add_cleanup(cleanup_socket, NULL);
Damien Miller95def091999-11-25 00:26:21 +11001212 new_socket(AUTH_SOCKET, sock);
1213 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001214 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001215 alarm(10);
1216 }
Damien Millerad833b32000-08-23 10:46:23 +10001217 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001218 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001219 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001220 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001221 signal(SIGHUP, cleanup_handler);
1222 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001223 nalloc = 0;
1224
Damien Miller95def091999-11-25 00:26:21 +11001225 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001226 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001227 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001228 if (errno == EINTR)
1229 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001230 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001231 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001232 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001233 }
1234 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001235}