blob: 6d94c6c29547a3f5b6400f6b2f2ceb91c934ed93 [file] [log] [blame]
Ben Lindstrombba81212001-06-25 05:01:22 +00001/* $OpenBSD: ssh-agent.c,v 1.55 2001/06/23 15:12:20 itojun Exp $ */
Damien Miller792c5111999-10-29 09:47:09 +10002
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11007 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
Damien Millerad833b32000-08-23 10:46:23 +100015 * SSH2 implementation,
Ben Lindstrom92a2e382001-03-05 06:59:27 +000016 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Ben Lindstrombba81212001-06-25 05:01:22 +000040RCSID("$OpenBSD: ssh-agent.c,v 1.55 2001/06/23 15:12:20 itojun Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041
42#include <openssl/evp.h>
43#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
46#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "buffer.h"
48#include "bufaux.h"
49#include "xmalloc.h"
50#include "packet.h"
51#include "getput.h"
52#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100053#include "key.h"
54#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100056#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller95def091999-11-25 00:26:21 +110060typedef struct {
61 int fd;
62 enum {
63 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
64 } type;
65 Buffer input;
66 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067} SocketEntry;
68
Ben Lindstrom46c16222000-12-22 01:43:59 +000069u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070SocketEntry *sockets = NULL;
71
Damien Miller95def091999-11-25 00:26:21 +110072typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100073 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110074 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075} Identity;
76
Damien Millerad833b32000-08-23 10:46:23 +100077typedef struct {
78 int nentries;
79 Identity *identities;
80} Idtab;
81
82/* private key table, one per protocol version */
83Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85int max_fd = 0;
86
87/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100088pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* pathname and directory for AUTH_SOCKET */
91char socket_name[1024];
92char socket_dir[1024];
93
Damien Miller95def091999-11-25 00:26:21 +110094#ifdef HAVE___PROGNAME
95extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000096#else
97char *__progname;
98#endif
Damien Miller95def091999-11-25 00:26:21 +110099
Ben Lindstrombba81212001-06-25 05:01:22 +0000100static void
Damien Millerad833b32000-08-23 10:46:23 +1000101idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102{
Damien Millerad833b32000-08-23 10:46:23 +1000103 int i;
104 for (i = 0; i <=2; i++){
105 idtable[i].identities = NULL;
106 idtable[i].nentries = 0;
107 }
108}
109
110/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000111static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000112idtab_lookup(int version)
113{
114 if (version < 1 || version > 2)
115 fatal("internal error, bad protocol version %d", version);
116 return &idtable[version];
117}
118
119/* return matching private key for given public key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000120static Key *
Damien Millerad833b32000-08-23 10:46:23 +1000121lookup_private_key(Key *key, int *idx, int version)
122{
123 int i;
124 Idtab *tab = idtab_lookup(version);
125 for (i = 0; i < tab->nentries; i++) {
126 if (key_equal(key, tab->identities[i].key)) {
127 if (idx != NULL)
128 *idx = i;
129 return tab->identities[i].key;
130 }
131 }
132 return NULL;
133}
134
135/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void
Damien Millerad833b32000-08-23 10:46:23 +1000137process_request_identities(SocketEntry *e, int version)
138{
139 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100140 Buffer msg;
141 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142
Damien Miller95def091999-11-25 00:26:21 +1100143 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000144 buffer_put_char(&msg, (version == 1) ?
145 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
146 buffer_put_int(&msg, tab->nentries);
147 for (i = 0; i < tab->nentries; i++) {
148 Identity *id = &tab->identities[i];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100149 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000150 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
151 buffer_put_bignum(&msg, id->key->rsa->e);
152 buffer_put_bignum(&msg, id->key->rsa->n);
153 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000154 u_char *blob;
155 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100156 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000157 buffer_put_string(&msg, blob, blen);
158 xfree(blob);
159 }
160 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100161 }
162 buffer_put_int(&e->output, buffer_len(&msg));
163 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
164 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
Damien Millerad833b32000-08-23 10:46:23 +1000167/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000168static void
Damien Millerad833b32000-08-23 10:46:23 +1000169process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Millerad833b32000-08-23 10:46:23 +1000171 Key *key, *private;
172 BIGNUM *challenge;
173 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100174 Buffer msg;
175 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000176 u_char buf[32], mdbuf[16], session_id[16];
177 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller95def091999-11-25 00:26:21 +1100179 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100180 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100181 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000182
183 buffer_get_int(&e->input); /* ignored */
184 buffer_get_bignum(&e->input, key->rsa->e);
185 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100186 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187
Damien Millerad833b32000-08-23 10:46:23 +1000188 /* Only protocol 1.1 is supported */
189 if (buffer_len(&e->input) == 0)
190 goto failure;
191 buffer_get(&e->input, (char *) session_id, 16);
192 response_type = buffer_get_int(&e->input);
193 if (response_type != 1)
194 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195
Damien Millerad833b32000-08-23 10:46:23 +1000196 private = lookup_private_key(key, NULL, 1);
197 if (private != NULL) {
198 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100199 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
200 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100201
Damien Millerad833b32000-08-23 10:46:23 +1000202 /* The response is MD5 of decrypted challenge plus session id. */
203 len = BN_num_bytes(challenge);
204 if (len <= 0 || len > 32) {
205 log("process_authentication_challenge: bad challenge length %d", len);
206 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100207 }
Damien Millerad833b32000-08-23 10:46:23 +1000208 memset(buf, 0, 32);
209 BN_bn2bin(challenge, buf + 32 - len);
210 MD5_Init(&md);
211 MD5_Update(&md, buf, 32);
212 MD5_Update(&md, session_id, 16);
213 MD5_Final(mdbuf, &md);
214
215 /* Send the response. */
216 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
217 for (i = 0; i < 16; i++)
218 buffer_put_char(&msg, mdbuf[i]);
219 goto send;
220 }
221
222failure:
223 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100224 buffer_put_char(&msg, SSH_AGENT_FAILURE);
225send:
226 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000227 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
228 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100229 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000230 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000231}
232
Damien Millerad833b32000-08-23 10:46:23 +1000233/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000234static void
Damien Millerad833b32000-08-23 10:46:23 +1000235process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236{
Damien Millerad833b32000-08-23 10:46:23 +1000237 extern int datafellows;
238 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000239 u_char *blob, *data, *signature = NULL;
240 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100241 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000242 Buffer msg;
243 int ok = -1;
244
245 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000246
Damien Millerad833b32000-08-23 10:46:23 +1000247 blob = buffer_get_string(&e->input, &blen);
248 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100249
250 flags = buffer_get_int(&e->input);
251 if (flags & SSH_AGENT_OLD_SIGNATURE)
252 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000253
Damien Miller0bc1bd82000-11-13 22:57:25 +1100254 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000255 if (key != NULL) {
256 private = lookup_private_key(key, NULL, 2);
257 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100258 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000259 }
260 key_free(key);
261 buffer_init(&msg);
262 if (ok == 0) {
263 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
264 buffer_put_string(&msg, signature, slen);
265 } else {
266 buffer_put_char(&msg, SSH_AGENT_FAILURE);
267 }
268 buffer_put_int(&e->output, buffer_len(&msg));
269 buffer_append(&e->output, buffer_ptr(&msg),
270 buffer_len(&msg));
271 buffer_free(&msg);
272 xfree(data);
273 xfree(blob);
274 if (signature != NULL)
275 xfree(signature);
276}
277
278/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000279static void
Damien Millerad833b32000-08-23 10:46:23 +1000280process_remove_identity(SocketEntry *e, int version)
281{
282 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000283 u_char *blob;
284 u_int blen;
285 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000286 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100287
Damien Millerad833b32000-08-23 10:46:23 +1000288 switch(version){
289 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100290 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000291 bits = buffer_get_int(&e->input);
292 buffer_get_bignum(&e->input, key->rsa->e);
293 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Damien Millerad833b32000-08-23 10:46:23 +1000295 if (bits != key_size(key))
296 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000297 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000298 break;
299 case 2:
300 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100301 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000302 xfree(blob);
303 break;
304 }
305 if (key != NULL) {
306 int idx;
307 private = lookup_private_key(key, &idx, version);
308 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100309 /*
310 * We have this key. Free the old key. Since we
311 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000312 * the array, we actually free the key there and move
313 * all the entries between the empty slot and the end
314 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100315 */
Damien Millerad833b32000-08-23 10:46:23 +1000316 Idtab *tab = idtab_lookup(version);
317 key_free(tab->identities[idx].key);
318 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100319 if (tab->nentries < 1)
320 fatal("process_remove_identity: "
321 "internal error: tab->nentries %d",
322 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000323 if (idx != tab->nentries - 1) {
324 int i;
325 for (i = idx; i < tab->nentries - 1; i++)
326 tab->identities[i] = tab->identities[i+1];
327 }
328 tab->identities[tab->nentries - 1].key = NULL;
329 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000330 tab->nentries--;
331 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100332 }
Damien Millerad833b32000-08-23 10:46:23 +1000333 key_free(key);
334 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000336 buffer_put_char(&e->output,
337 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000338}
339
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static void
Damien Millerad833b32000-08-23 10:46:23 +1000341process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000343 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000344 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller95def091999-11-25 00:26:21 +1100346 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000347 for (i = 0; i < tab->nentries; i++) {
348 key_free(tab->identities[i].key);
349 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100350 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000351
Damien Miller95def091999-11-25 00:26:21 +1100352 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000353 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
355 /* Send success. */
356 buffer_put_int(&e->output, 1);
357 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
358 return;
Damien Miller95def091999-11-25 00:26:21 +1100359}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000360
Ben Lindstrombba81212001-06-25 05:01:22 +0000361static void
Damien Millerad833b32000-08-23 10:46:23 +1000362process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100363{
Damien Millerad833b32000-08-23 10:46:23 +1000364 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100365 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000366 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100367 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000368 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100369
Damien Millerad833b32000-08-23 10:46:23 +1000370 switch (version) {
371 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000373 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 buffer_get_bignum(&e->input, k->rsa->n);
375 buffer_get_bignum(&e->input, k->rsa->e);
376 buffer_get_bignum(&e->input, k->rsa->d);
377 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100378
Damien Millerad833b32000-08-23 10:46:23 +1000379 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380 buffer_get_bignum(&e->input, k->rsa->q); /* p */
381 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100382
Damien Millerad833b32000-08-23 10:46:23 +1000383 /* Generate additional parameters */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000385 break;
386 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100387 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000388 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 xfree(type_name);
390 switch(type) {
391 case KEY_DSA:
392 k = key_new_private(type);
393 buffer_get_bignum2(&e->input, k->dsa->p);
394 buffer_get_bignum2(&e->input, k->dsa->q);
395 buffer_get_bignum2(&e->input, k->dsa->g);
396 buffer_get_bignum2(&e->input, k->dsa->pub_key);
397 buffer_get_bignum2(&e->input, k->dsa->priv_key);
398 break;
399 case KEY_RSA:
400 k = key_new_private(type);
401 buffer_get_bignum2(&e->input, k->rsa->n);
402 buffer_get_bignum2(&e->input, k->rsa->e);
403 buffer_get_bignum2(&e->input, k->rsa->d);
404 buffer_get_bignum2(&e->input, k->rsa->iqmp);
405 buffer_get_bignum2(&e->input, k->rsa->p);
406 buffer_get_bignum2(&e->input, k->rsa->q);
407
408 /* Generate additional parameters */
409 generate_additional_parameters(k->rsa);
410 break;
411 default:
Damien Millerad833b32000-08-23 10:46:23 +1000412 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000413 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100414 }
Damien Millerad833b32000-08-23 10:46:23 +1000415 break;
416 }
Damien Millerad833b32000-08-23 10:46:23 +1000417 comment = buffer_get_string(&e->input, NULL);
418 if (k == NULL) {
419 xfree(comment);
420 goto send;
421 }
422 success = 1;
423 if (lookup_private_key(k, NULL, version) == NULL) {
424 if (tab->nentries == 0)
425 tab->identities = xmalloc(sizeof(Identity));
426 else
427 tab->identities = xrealloc(tab->identities,
428 (tab->nentries + 1) * sizeof(Identity));
429 tab->identities[tab->nentries].key = k;
430 tab->identities[tab->nentries].comment = comment;
431 /* Increment the number of identities. */
432 tab->nentries++;
433 } else {
434 key_free(k);
435 xfree(comment);
436 }
437send:
Damien Miller95def091999-11-25 00:26:21 +1100438 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000439 buffer_put_char(&e->output,
440 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441}
442
Damien Millerad833b32000-08-23 10:46:23 +1000443/* dispatch incoming messages */
444
Ben Lindstrombba81212001-06-25 05:01:22 +0000445static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000446process_message(SocketEntry *e)
447{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000448 u_int msg_len;
449 u_int type;
450 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100451 if (buffer_len(&e->input) < 5)
452 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000453 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100454 msg_len = GET_32BIT(cp);
455 if (msg_len > 256 * 1024) {
456 shutdown(e->fd, SHUT_RDWR);
457 close(e->fd);
458 e->type = AUTH_UNUSED;
459 return;
460 }
461 if (buffer_len(&e->input) < msg_len + 4)
462 return;
463 buffer_consume(&e->input, 4);
464 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller95def091999-11-25 00:26:21 +1100466 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000467 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100468 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000469 process_authentication_challenge1(e);
470 break;
471 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
472 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100473 break;
474 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000475 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100476 break;
477 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000478 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100479 break;
480 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000481 process_remove_all_identities(e, 1);
482 break;
483 /* ssh2 */
484 case SSH2_AGENTC_SIGN_REQUEST:
485 process_sign_request2(e);
486 break;
487 case SSH2_AGENTC_REQUEST_IDENTITIES:
488 process_request_identities(e, 2);
489 break;
490 case SSH2_AGENTC_ADD_IDENTITY:
491 process_add_identity(e, 2);
492 break;
493 case SSH2_AGENTC_REMOVE_IDENTITY:
494 process_remove_identity(e, 2);
495 break;
496 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
497 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100498 break;
499 default:
500 /* Unknown message. Respond with failure. */
501 error("Unknown message %d", type);
502 buffer_clear(&e->input);
503 buffer_put_int(&e->output, 1);
504 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
505 break;
506 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000507}
508
Ben Lindstrombba81212001-06-25 05:01:22 +0000509static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000510new_socket(int type, int fd)
511{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000512 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100513 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
514 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000515
Damien Miller95def091999-11-25 00:26:21 +1100516 if (fd > max_fd)
517 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000518
Damien Miller95def091999-11-25 00:26:21 +1100519 for (i = 0; i < sockets_alloc; i++)
520 if (sockets[i].type == AUTH_UNUSED) {
521 sockets[i].fd = fd;
522 sockets[i].type = type;
523 buffer_init(&sockets[i].input);
524 buffer_init(&sockets[i].output);
525 return;
526 }
527 old_alloc = sockets_alloc;
528 sockets_alloc += 10;
529 if (sockets)
530 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
531 else
532 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
533 for (i = old_alloc; i < sockets_alloc; i++)
534 sockets[i].type = AUTH_UNUSED;
535 sockets[old_alloc].type = type;
536 sockets[old_alloc].fd = fd;
537 buffer_init(&sockets[old_alloc].input);
538 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539}
540
Ben Lindstrombba81212001-06-25 05:01:22 +0000541static int
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000542prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000544 u_int i, sz;
545 int n = 0;
546
547 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100548 switch (sockets[i].type) {
549 case AUTH_SOCKET:
550 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000551 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100552 break;
553 case AUTH_UNUSED:
554 break;
555 default:
556 fatal("Unknown socket type %d", sockets[i].type);
557 break;
558 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000559 }
560
561 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
562 if (*fdrp == NULL || n > *fdl) {
563 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000564 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000565 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000566 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000567 *fdrp = xmalloc(sz);
568 *fdwp = xmalloc(sz);
569 *fdl = n;
570 }
571 memset(*fdrp, 0, sz);
572 memset(*fdwp, 0, sz);
573
574 for (i = 0; i < sockets_alloc; i++) {
575 switch (sockets[i].type) {
576 case AUTH_SOCKET:
577 case AUTH_CONNECTION:
578 FD_SET(sockets[i].fd, *fdrp);
579 if (buffer_len(&sockets[i].output) > 0)
580 FD_SET(sockets[i].fd, *fdwp);
581 break;
582 default:
583 break;
584 }
585 }
586 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000587}
588
Ben Lindstrombba81212001-06-25 05:01:22 +0000589static void
Damien Miller95def091999-11-25 00:26:21 +1100590after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000591{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000592 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100593 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100594 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100595 char buf[1024];
596 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000597
Damien Miller95def091999-11-25 00:26:21 +1100598 for (i = 0; i < sockets_alloc; i++)
599 switch (sockets[i].type) {
600 case AUTH_UNUSED:
601 break;
602 case AUTH_SOCKET:
603 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100604 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000605 sock = accept(sockets[i].fd,
606 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100607 if (sock < 0) {
608 perror("accept from AUTH_SOCKET");
609 break;
610 }
611 new_socket(AUTH_CONNECTION, sock);
612 }
613 break;
614 case AUTH_CONNECTION:
615 if (buffer_len(&sockets[i].output) > 0 &&
616 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000617 do {
618 len = write(sockets[i].fd,
619 buffer_ptr(&sockets[i].output),
620 buffer_len(&sockets[i].output));
621 if (len == -1 && (errno == EAGAIN ||
622 errno == EINTR))
623 continue;
624 break;
625 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100626 if (len <= 0) {
627 shutdown(sockets[i].fd, SHUT_RDWR);
628 close(sockets[i].fd);
629 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000630 buffer_free(&sockets[i].input);
631 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100632 break;
633 }
634 buffer_consume(&sockets[i].output, len);
635 }
636 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000637 do {
638 len = read(sockets[i].fd, buf, sizeof(buf));
639 if (len == -1 && (errno == EAGAIN ||
640 errno == EINTR))
641 continue;
642 break;
643 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100644 if (len <= 0) {
645 shutdown(sockets[i].fd, SHUT_RDWR);
646 close(sockets[i].fd);
647 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000648 buffer_free(&sockets[i].input);
649 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100650 break;
651 }
652 buffer_append(&sockets[i].input, buf, len);
653 process_message(&sockets[i]);
654 }
655 break;
656 default:
657 fatal("Unknown type %d", sockets[i].type);
658 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000659}
660
Ben Lindstrombba81212001-06-25 05:01:22 +0000661static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000662check_parent_exists(int sig)
663{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000664 int save_errno = errno;
665
Damien Miller166fca82000-04-20 07:42:21 +1000666 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100667 /* printf("Parent has died - Authentication agent exiting.\n"); */
668 exit(1);
669 }
670 signal(SIGALRM, check_parent_exists);
671 alarm(10);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000672 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000673}
674
Ben Lindstrombba81212001-06-25 05:01:22 +0000675static void
Damien Miller792c5111999-10-29 09:47:09 +1000676cleanup_socket(void)
677{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000678 if (socket_name[0])
679 unlink(socket_name);
680 if (socket_dir[0])
681 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682}
683
Ben Lindstrombba81212001-06-25 05:01:22 +0000684static void
Damien Miller792c5111999-10-29 09:47:09 +1000685cleanup_exit(int i)
686{
Damien Miller95def091999-11-25 00:26:21 +1100687 cleanup_socket();
688 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000689}
690
Ben Lindstrombba81212001-06-25 05:01:22 +0000691static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000692cleanup_handler(int sig)
693{
694 cleanup_socket();
695 _exit(2);
696}
697
Ben Lindstrombba81212001-06-25 05:01:22 +0000698static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000699usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000700{
Damien Miller95def091999-11-25 00:26:21 +1100701 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
702 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000703 __progname);
Damien Miller95def091999-11-25 00:26:21 +1100704 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000705}
706
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707int
708main(int ac, char **av)
709{
Damien Miller95def091999-11-25 00:26:21 +1100710 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
711 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000712#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000713 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000714#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000715#ifdef HAVE_CYGWIN
716 int prev_mask;
717#endif
Damien Miller95def091999-11-25 00:26:21 +1100718 pid_t pid;
719 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000720 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000721 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000722
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000723 SSLeay_add_all_algorithms();
724
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000725 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000726 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100727 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000728
Damien Millerd0cff3e2000-04-20 23:12:58 +1000729#ifdef __GNU_LIBRARY__
730 while ((ch = getopt(ac, av, "+cks")) != -1) {
731#else /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100732 while ((ch = getopt(ac, av, "cks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000733#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100734 switch (ch) {
735 case 'c':
736 if (s_flag)
737 usage();
738 c_flag++;
739 break;
740 case 'k':
741 k_flag++;
742 break;
743 case 's':
744 if (c_flag)
745 usage();
746 s_flag++;
747 break;
748 default:
749 usage();
750 }
Damien Miller792c5111999-10-29 09:47:09 +1000751 }
Damien Miller95def091999-11-25 00:26:21 +1100752 ac -= optind;
753 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000754
Damien Miller95def091999-11-25 00:26:21 +1100755 if (ac > 0 && (c_flag || k_flag || s_flag))
756 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000757
Damien Miller95def091999-11-25 00:26:21 +1100758 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
759 shell = getenv("SHELL");
760 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
761 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000762 }
Damien Miller95def091999-11-25 00:26:21 +1100763 if (k_flag) {
764 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
765 if (pidstr == NULL) {
766 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000767 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100768 exit(1);
769 }
770 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000771 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100772 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000773 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100774 exit(1);
775 }
776 if (kill(pid, SIGTERM) == -1) {
777 perror("kill");
778 exit(1);
779 }
780 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
781 printf(format, SSH_AUTHSOCKET_ENV_NAME);
782 printf(format, SSH_AGENTPID_ENV_NAME);
783 printf("echo Agent pid %d killed;\n", pid);
784 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000785 }
Damien Miller95def091999-11-25 00:26:21 +1100786 parent_pid = getpid();
787
788 /* Create private directory for agent socket */
789 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
790 if (mkdtemp(socket_dir) == NULL) {
791 perror("mkdtemp: private socket dir");
792 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000793 }
Damien Miller95def091999-11-25 00:26:21 +1100794 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000795 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Miller5428f641999-11-25 11:54:57 +1100797 /*
798 * Create socket early so it will exist before command gets run from
799 * the parent.
800 */
Damien Miller95def091999-11-25 00:26:21 +1100801 sock = socket(AF_UNIX, SOCK_STREAM, 0);
802 if (sock < 0) {
803 perror("socket");
804 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000805 }
Damien Miller95def091999-11-25 00:26:21 +1100806 memset(&sunaddr, 0, sizeof(sunaddr));
807 sunaddr.sun_family = AF_UNIX;
808 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000809#ifdef HAVE_CYGWIN
810 prev_mask = umask(0177);
811#endif
Damien Miller95def091999-11-25 00:26:21 +1100812 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
813 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000814#ifdef HAVE_CYGWIN
815 umask(prev_mask);
816#endif
Damien Miller95def091999-11-25 00:26:21 +1100817 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000819#ifdef HAVE_CYGWIN
820 umask(prev_mask);
821#endif
Damien Miller95def091999-11-25 00:26:21 +1100822 if (listen(sock, 5) < 0) {
823 perror("listen");
824 cleanup_exit(1);
825 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000826
Damien Miller5428f641999-11-25 11:54:57 +1100827 /*
828 * Fork, and have the parent execute the command, if any, or present
829 * the socket data. The child continues as the authentication agent.
830 */
Damien Miller95def091999-11-25 00:26:21 +1100831 pid = fork();
832 if (pid == -1) {
833 perror("fork");
834 exit(1);
835 }
836 if (pid != 0) { /* Parent - execute the given command. */
837 close(sock);
838 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
839 if (ac == 0) {
840 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
841 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100843 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000844 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100845 printf("echo Agent pid %d;\n", pid);
846 exit(0);
847 }
Damien Millere4340be2000-09-16 13:29:08 +1100848 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
849 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
850 perror("setenv");
851 exit(1);
852 }
Damien Miller95def091999-11-25 00:26:21 +1100853 execvp(av[0], av);
854 perror(av[0]);
855 exit(1);
856 }
857 close(0);
858 close(1);
859 close(2);
860
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000861#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000862 /* deny core dumps, since memory contains unencrypted private keys */
863 rlim.rlim_cur = rlim.rlim_max = 0;
864 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
865 perror("setrlimit rlimit_core failed");
866 cleanup_exit(1);
867 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000868#endif
Damien Miller95def091999-11-25 00:26:21 +1100869 if (setsid() == -1) {
870 perror("setsid");
871 cleanup_exit(1);
872 }
873 if (atexit(cleanup_socket) < 0) {
874 perror("atexit");
875 cleanup_exit(1);
876 }
877 new_socket(AUTH_SOCKET, sock);
878 if (ac > 0) {
879 signal(SIGALRM, check_parent_exists);
880 alarm(10);
881 }
Damien Millerad833b32000-08-23 10:46:23 +1000882 idtab_init();
Damien Miller95def091999-11-25 00:26:21 +1100883 signal(SIGINT, SIG_IGN);
884 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000885 signal(SIGHUP, cleanup_handler);
886 signal(SIGTERM, cleanup_handler);
Damien Miller95def091999-11-25 00:26:21 +1100887 while (1) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000888 prepare_select(&readsetp, &writesetp, &max_fd);
889 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100890 if (errno == EINTR)
891 continue;
892 exit(1);
893 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000894 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100895 }
896 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000897}