blob: 54b375fcec8de24d05a2f76afa66e093292c38b7 [file] [log] [blame]
Ben Lindstrom44697232001-07-04 03:32:30 +00001/* $OpenBSD: ssh-agent.c,v 1.56 2001/06/25 08:25:40 markus 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 *
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Ben Lindstrom44697232001-07-04 03:32:30 +000039RCSID("$OpenBSD: ssh-agent.c,v 1.56 2001/06/25 08:25:40 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000040
41#include <openssl/evp.h>
42#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "ssh.h"
45#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "buffer.h"
47#include "bufaux.h"
48#include "xmalloc.h"
49#include "packet.h"
50#include "getput.h"
51#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100052#include "key.h"
53#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100055#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110056#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Damien Miller95def091999-11-25 00:26:21 +110059typedef struct {
60 int fd;
61 enum {
62 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
63 } type;
64 Buffer input;
65 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066} SocketEntry;
67
Ben Lindstrom46c16222000-12-22 01:43:59 +000068u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069SocketEntry *sockets = NULL;
70
Damien Miller95def091999-11-25 00:26:21 +110071typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100072 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110073 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074} Identity;
75
Damien Millerad833b32000-08-23 10:46:23 +100076typedef struct {
77 int nentries;
78 Identity *identities;
79} Idtab;
80
81/* private key table, one per protocol version */
82Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
84int max_fd = 0;
85
86/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100087pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* pathname and directory for AUTH_SOCKET */
90char socket_name[1024];
91char socket_dir[1024];
92
Damien Miller95def091999-11-25 00:26:21 +110093#ifdef HAVE___PROGNAME
94extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000095#else
96char *__progname;
97#endif
Damien Miller95def091999-11-25 00:26:21 +110098
Ben Lindstrombba81212001-06-25 05:01:22 +000099static void
Damien Millerad833b32000-08-23 10:46:23 +1000100idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101{
Damien Millerad833b32000-08-23 10:46:23 +1000102 int i;
103 for (i = 0; i <=2; i++){
104 idtable[i].identities = NULL;
105 idtable[i].nentries = 0;
106 }
107}
108
109/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000110static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000111idtab_lookup(int version)
112{
113 if (version < 1 || version > 2)
114 fatal("internal error, bad protocol version %d", version);
115 return &idtable[version];
116}
117
118/* return matching private key for given public key */
Ben Lindstrombba81212001-06-25 05:01:22 +0000119static Key *
Damien Millerad833b32000-08-23 10:46:23 +1000120lookup_private_key(Key *key, int *idx, int version)
121{
122 int i;
123 Idtab *tab = idtab_lookup(version);
124 for (i = 0; i < tab->nentries; i++) {
125 if (key_equal(key, tab->identities[i].key)) {
126 if (idx != NULL)
127 *idx = i;
128 return tab->identities[i].key;
129 }
130 }
131 return NULL;
132}
133
134/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Damien Millerad833b32000-08-23 10:46:23 +1000136process_request_identities(SocketEntry *e, int version)
137{
138 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100139 Buffer msg;
140 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000143 buffer_put_char(&msg, (version == 1) ?
144 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
145 buffer_put_int(&msg, tab->nentries);
146 for (i = 0; i < tab->nentries; i++) {
147 Identity *id = &tab->identities[i];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100148 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000149 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
150 buffer_put_bignum(&msg, id->key->rsa->e);
151 buffer_put_bignum(&msg, id->key->rsa->n);
152 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000153 u_char *blob;
154 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100155 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000156 buffer_put_string(&msg, blob, blen);
157 xfree(blob);
158 }
159 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100160 }
161 buffer_put_int(&e->output, buffer_len(&msg));
162 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
163 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164}
165
Damien Millerad833b32000-08-23 10:46:23 +1000166/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000167static void
Damien Millerad833b32000-08-23 10:46:23 +1000168process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169{
Damien Millerad833b32000-08-23 10:46:23 +1000170 Key *key, *private;
171 BIGNUM *challenge;
172 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100173 Buffer msg;
174 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000175 u_char buf[32], mdbuf[16], session_id[16];
176 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177
Damien Miller95def091999-11-25 00:26:21 +1100178 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100179 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100180 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000181
182 buffer_get_int(&e->input); /* ignored */
183 buffer_get_bignum(&e->input, key->rsa->e);
184 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100185 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186
Damien Millerad833b32000-08-23 10:46:23 +1000187 /* Only protocol 1.1 is supported */
188 if (buffer_len(&e->input) == 0)
189 goto failure;
190 buffer_get(&e->input, (char *) session_id, 16);
191 response_type = buffer_get_int(&e->input);
192 if (response_type != 1)
193 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194
Damien Millerad833b32000-08-23 10:46:23 +1000195 private = lookup_private_key(key, NULL, 1);
196 if (private != NULL) {
197 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100198 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
199 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100200
Damien Millerad833b32000-08-23 10:46:23 +1000201 /* The response is MD5 of decrypted challenge plus session id. */
202 len = BN_num_bytes(challenge);
203 if (len <= 0 || len > 32) {
204 log("process_authentication_challenge: bad challenge length %d", len);
205 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100206 }
Damien Millerad833b32000-08-23 10:46:23 +1000207 memset(buf, 0, 32);
208 BN_bn2bin(challenge, buf + 32 - len);
209 MD5_Init(&md);
210 MD5_Update(&md, buf, 32);
211 MD5_Update(&md, session_id, 16);
212 MD5_Final(mdbuf, &md);
213
214 /* Send the response. */
215 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
216 for (i = 0; i < 16; i++)
217 buffer_put_char(&msg, mdbuf[i]);
218 goto send;
219 }
220
221failure:
222 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100223 buffer_put_char(&msg, SSH_AGENT_FAILURE);
224send:
225 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000226 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
227 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100228 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000229 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230}
231
Damien Millerad833b32000-08-23 10:46:23 +1000232/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000233static void
Damien Millerad833b32000-08-23 10:46:23 +1000234process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235{
Damien Millerad833b32000-08-23 10:46:23 +1000236 extern int datafellows;
237 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000238 u_char *blob, *data, *signature = NULL;
239 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100240 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000241 Buffer msg;
242 int ok = -1;
243
244 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000245
Damien Millerad833b32000-08-23 10:46:23 +1000246 blob = buffer_get_string(&e->input, &blen);
247 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100248
249 flags = buffer_get_int(&e->input);
250 if (flags & SSH_AGENT_OLD_SIGNATURE)
251 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000252
Damien Miller0bc1bd82000-11-13 22:57:25 +1100253 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000254 if (key != NULL) {
255 private = lookup_private_key(key, NULL, 2);
256 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100257 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000258 }
259 key_free(key);
260 buffer_init(&msg);
261 if (ok == 0) {
262 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
263 buffer_put_string(&msg, signature, slen);
264 } else {
265 buffer_put_char(&msg, SSH_AGENT_FAILURE);
266 }
267 buffer_put_int(&e->output, buffer_len(&msg));
268 buffer_append(&e->output, buffer_ptr(&msg),
269 buffer_len(&msg));
270 buffer_free(&msg);
271 xfree(data);
272 xfree(blob);
273 if (signature != NULL)
274 xfree(signature);
275}
276
277/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000278static void
Damien Millerad833b32000-08-23 10:46:23 +1000279process_remove_identity(SocketEntry *e, int version)
280{
281 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000282 u_char *blob;
283 u_int blen;
284 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000285 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100286
Damien Millerad833b32000-08-23 10:46:23 +1000287 switch(version){
288 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100289 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000290 bits = buffer_get_int(&e->input);
291 buffer_get_bignum(&e->input, key->rsa->e);
292 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293
Damien Millerad833b32000-08-23 10:46:23 +1000294 if (bits != key_size(key))
295 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000296 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000297 break;
298 case 2:
299 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100300 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000301 xfree(blob);
302 break;
303 }
304 if (key != NULL) {
305 int idx;
306 private = lookup_private_key(key, &idx, version);
307 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100308 /*
309 * We have this key. Free the old key. Since we
310 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000311 * the array, we actually free the key there and move
312 * all the entries between the empty slot and the end
313 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100314 */
Damien Millerad833b32000-08-23 10:46:23 +1000315 Idtab *tab = idtab_lookup(version);
316 key_free(tab->identities[idx].key);
317 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100318 if (tab->nentries < 1)
319 fatal("process_remove_identity: "
320 "internal error: tab->nentries %d",
321 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000322 if (idx != tab->nentries - 1) {
323 int i;
324 for (i = idx; i < tab->nentries - 1; i++)
325 tab->identities[i] = tab->identities[i+1];
326 }
327 tab->identities[tab->nentries - 1].key = NULL;
328 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000329 tab->nentries--;
330 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100331 }
Damien Millerad833b32000-08-23 10:46:23 +1000332 key_free(key);
333 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000335 buffer_put_char(&e->output,
336 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337}
338
Ben Lindstrombba81212001-06-25 05:01:22 +0000339static void
Damien Millerad833b32000-08-23 10:46:23 +1000340process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000342 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000343 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344
Damien Miller95def091999-11-25 00:26:21 +1100345 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000346 for (i = 0; i < tab->nentries; i++) {
347 key_free(tab->identities[i].key);
348 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100349 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350
Damien Miller95def091999-11-25 00:26:21 +1100351 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000352 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
354 /* Send success. */
355 buffer_put_int(&e->output, 1);
356 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
357 return;
Damien Miller95def091999-11-25 00:26:21 +1100358}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000359
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static void
Damien Millerad833b32000-08-23 10:46:23 +1000361process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100362{
Damien Millerad833b32000-08-23 10:46:23 +1000363 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100364 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000365 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100366 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000367 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100368
Damien Millerad833b32000-08-23 10:46:23 +1000369 switch (version) {
370 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100371 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000372 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100373 buffer_get_bignum(&e->input, k->rsa->n);
374 buffer_get_bignum(&e->input, k->rsa->e);
375 buffer_get_bignum(&e->input, k->rsa->d);
376 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100377
Damien Millerad833b32000-08-23 10:46:23 +1000378 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379 buffer_get_bignum(&e->input, k->rsa->q); /* p */
380 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100381
Damien Millerad833b32000-08-23 10:46:23 +1000382 /* Generate additional parameters */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383 generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000384 break;
385 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000387 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100388 xfree(type_name);
389 switch(type) {
390 case KEY_DSA:
391 k = key_new_private(type);
392 buffer_get_bignum2(&e->input, k->dsa->p);
393 buffer_get_bignum2(&e->input, k->dsa->q);
394 buffer_get_bignum2(&e->input, k->dsa->g);
395 buffer_get_bignum2(&e->input, k->dsa->pub_key);
396 buffer_get_bignum2(&e->input, k->dsa->priv_key);
397 break;
398 case KEY_RSA:
399 k = key_new_private(type);
400 buffer_get_bignum2(&e->input, k->rsa->n);
401 buffer_get_bignum2(&e->input, k->rsa->e);
402 buffer_get_bignum2(&e->input, k->rsa->d);
403 buffer_get_bignum2(&e->input, k->rsa->iqmp);
404 buffer_get_bignum2(&e->input, k->rsa->p);
405 buffer_get_bignum2(&e->input, k->rsa->q);
406
407 /* Generate additional parameters */
408 generate_additional_parameters(k->rsa);
409 break;
410 default:
Damien Millerad833b32000-08-23 10:46:23 +1000411 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000412 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100413 }
Damien Millerad833b32000-08-23 10:46:23 +1000414 break;
415 }
Damien Millerad833b32000-08-23 10:46:23 +1000416 comment = buffer_get_string(&e->input, NULL);
417 if (k == NULL) {
418 xfree(comment);
419 goto send;
420 }
421 success = 1;
422 if (lookup_private_key(k, NULL, version) == NULL) {
423 if (tab->nentries == 0)
424 tab->identities = xmalloc(sizeof(Identity));
425 else
426 tab->identities = xrealloc(tab->identities,
427 (tab->nentries + 1) * sizeof(Identity));
428 tab->identities[tab->nentries].key = k;
429 tab->identities[tab->nentries].comment = comment;
430 /* Increment the number of identities. */
431 tab->nentries++;
432 } else {
433 key_free(k);
434 xfree(comment);
435 }
436send:
Damien Miller95def091999-11-25 00:26:21 +1100437 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000438 buffer_put_char(&e->output,
439 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440}
441
Damien Millerad833b32000-08-23 10:46:23 +1000442/* dispatch incoming messages */
443
Ben Lindstrombba81212001-06-25 05:01:22 +0000444static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000445process_message(SocketEntry *e)
446{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 u_int msg_len;
448 u_int type;
449 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100450 if (buffer_len(&e->input) < 5)
451 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000452 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100453 msg_len = GET_32BIT(cp);
454 if (msg_len > 256 * 1024) {
455 shutdown(e->fd, SHUT_RDWR);
456 close(e->fd);
457 e->type = AUTH_UNUSED;
458 return;
459 }
460 if (buffer_len(&e->input) < msg_len + 4)
461 return;
462 buffer_consume(&e->input, 4);
463 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000466 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100467 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000468 process_authentication_challenge1(e);
469 break;
470 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
471 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100472 break;
473 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000474 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100475 break;
476 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000477 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100478 break;
479 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000480 process_remove_all_identities(e, 1);
481 break;
482 /* ssh2 */
483 case SSH2_AGENTC_SIGN_REQUEST:
484 process_sign_request2(e);
485 break;
486 case SSH2_AGENTC_REQUEST_IDENTITIES:
487 process_request_identities(e, 2);
488 break;
489 case SSH2_AGENTC_ADD_IDENTITY:
490 process_add_identity(e, 2);
491 break;
492 case SSH2_AGENTC_REMOVE_IDENTITY:
493 process_remove_identity(e, 2);
494 break;
495 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
496 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100497 break;
498 default:
499 /* Unknown message. Respond with failure. */
500 error("Unknown message %d", type);
501 buffer_clear(&e->input);
502 buffer_put_int(&e->output, 1);
503 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
504 break;
505 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000506}
507
Ben Lindstrombba81212001-06-25 05:01:22 +0000508static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000509new_socket(int type, int fd)
510{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000511 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100512 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
513 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000514
Damien Miller95def091999-11-25 00:26:21 +1100515 if (fd > max_fd)
516 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000517
Damien Miller95def091999-11-25 00:26:21 +1100518 for (i = 0; i < sockets_alloc; i++)
519 if (sockets[i].type == AUTH_UNUSED) {
520 sockets[i].fd = fd;
521 sockets[i].type = type;
522 buffer_init(&sockets[i].input);
523 buffer_init(&sockets[i].output);
524 return;
525 }
526 old_alloc = sockets_alloc;
527 sockets_alloc += 10;
528 if (sockets)
529 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
530 else
531 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
532 for (i = old_alloc; i < sockets_alloc; i++)
533 sockets[i].type = AUTH_UNUSED;
534 sockets[old_alloc].type = type;
535 sockets[old_alloc].fd = fd;
536 buffer_init(&sockets[old_alloc].input);
537 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538}
539
Ben Lindstrombba81212001-06-25 05:01:22 +0000540static int
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000541prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000542{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000543 u_int i, sz;
544 int n = 0;
545
546 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100547 switch (sockets[i].type) {
548 case AUTH_SOCKET:
549 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000550 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100551 break;
552 case AUTH_UNUSED:
553 break;
554 default:
555 fatal("Unknown socket type %d", sockets[i].type);
556 break;
557 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000558 }
559
560 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
561 if (*fdrp == NULL || n > *fdl) {
562 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000563 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000564 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000565 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000566 *fdrp = xmalloc(sz);
567 *fdwp = xmalloc(sz);
568 *fdl = n;
569 }
570 memset(*fdrp, 0, sz);
571 memset(*fdwp, 0, sz);
572
573 for (i = 0; i < sockets_alloc; i++) {
574 switch (sockets[i].type) {
575 case AUTH_SOCKET:
576 case AUTH_CONNECTION:
577 FD_SET(sockets[i].fd, *fdrp);
578 if (buffer_len(&sockets[i].output) > 0)
579 FD_SET(sockets[i].fd, *fdwp);
580 break;
581 default:
582 break;
583 }
584 }
585 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000586}
587
Ben Lindstrombba81212001-06-25 05:01:22 +0000588static void
Damien Miller95def091999-11-25 00:26:21 +1100589after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000590{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000591 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100592 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100593 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100594 char buf[1024];
595 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000596
Damien Miller95def091999-11-25 00:26:21 +1100597 for (i = 0; i < sockets_alloc; i++)
598 switch (sockets[i].type) {
599 case AUTH_UNUSED:
600 break;
601 case AUTH_SOCKET:
602 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100603 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000604 sock = accept(sockets[i].fd,
605 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100606 if (sock < 0) {
607 perror("accept from AUTH_SOCKET");
608 break;
609 }
610 new_socket(AUTH_CONNECTION, sock);
611 }
612 break;
613 case AUTH_CONNECTION:
614 if (buffer_len(&sockets[i].output) > 0 &&
615 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000616 do {
617 len = write(sockets[i].fd,
618 buffer_ptr(&sockets[i].output),
619 buffer_len(&sockets[i].output));
620 if (len == -1 && (errno == EAGAIN ||
621 errno == EINTR))
622 continue;
623 break;
624 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100625 if (len <= 0) {
626 shutdown(sockets[i].fd, SHUT_RDWR);
627 close(sockets[i].fd);
628 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000629 buffer_free(&sockets[i].input);
630 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100631 break;
632 }
633 buffer_consume(&sockets[i].output, len);
634 }
635 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000636 do {
637 len = read(sockets[i].fd, buf, sizeof(buf));
638 if (len == -1 && (errno == EAGAIN ||
639 errno == EINTR))
640 continue;
641 break;
642 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100643 if (len <= 0) {
644 shutdown(sockets[i].fd, SHUT_RDWR);
645 close(sockets[i].fd);
646 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000647 buffer_free(&sockets[i].input);
648 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100649 break;
650 }
651 buffer_append(&sockets[i].input, buf, len);
652 process_message(&sockets[i]);
653 }
654 break;
655 default:
656 fatal("Unknown type %d", sockets[i].type);
657 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000658}
659
Ben Lindstrombba81212001-06-25 05:01:22 +0000660static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000661check_parent_exists(int sig)
662{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000663 int save_errno = errno;
664
Damien Miller166fca82000-04-20 07:42:21 +1000665 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100666 /* printf("Parent has died - Authentication agent exiting.\n"); */
667 exit(1);
668 }
669 signal(SIGALRM, check_parent_exists);
670 alarm(10);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000671 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000672}
673
Ben Lindstrombba81212001-06-25 05:01:22 +0000674static void
Damien Miller792c5111999-10-29 09:47:09 +1000675cleanup_socket(void)
676{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000677 if (socket_name[0])
678 unlink(socket_name);
679 if (socket_dir[0])
680 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000681}
682
Ben Lindstrombba81212001-06-25 05:01:22 +0000683static void
Damien Miller792c5111999-10-29 09:47:09 +1000684cleanup_exit(int i)
685{
Damien Miller95def091999-11-25 00:26:21 +1100686 cleanup_socket();
687 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000688}
689
Ben Lindstrombba81212001-06-25 05:01:22 +0000690static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000691cleanup_handler(int sig)
692{
693 cleanup_socket();
694 _exit(2);
695}
696
Ben Lindstrombba81212001-06-25 05:01:22 +0000697static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000698usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000699{
Damien Miller95def091999-11-25 00:26:21 +1100700 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
701 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000702 __progname);
Damien Miller95def091999-11-25 00:26:21 +1100703 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000704}
705
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000706int
707main(int ac, char **av)
708{
Damien Miller95def091999-11-25 00:26:21 +1100709 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
710 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000711#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000712 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000713#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +0000714#ifdef HAVE_CYGWIN
715 int prev_mask;
716#endif
Damien Miller95def091999-11-25 00:26:21 +1100717 pid_t pid;
718 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000719 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000720 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000721
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000722 SSLeay_add_all_algorithms();
723
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000724 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000725 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +1100726 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000727
Damien Millerd0cff3e2000-04-20 23:12:58 +1000728#ifdef __GNU_LIBRARY__
729 while ((ch = getopt(ac, av, "+cks")) != -1) {
730#else /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100731 while ((ch = getopt(ac, av, "cks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000732#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100733 switch (ch) {
734 case 'c':
735 if (s_flag)
736 usage();
737 c_flag++;
738 break;
739 case 'k':
740 k_flag++;
741 break;
742 case 's':
743 if (c_flag)
744 usage();
745 s_flag++;
746 break;
747 default:
748 usage();
749 }
Damien Miller792c5111999-10-29 09:47:09 +1000750 }
Damien Miller95def091999-11-25 00:26:21 +1100751 ac -= optind;
752 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000753
Damien Miller95def091999-11-25 00:26:21 +1100754 if (ac > 0 && (c_flag || k_flag || s_flag))
755 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000756
Damien Miller95def091999-11-25 00:26:21 +1100757 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
758 shell = getenv("SHELL");
759 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
760 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000761 }
Damien Miller95def091999-11-25 00:26:21 +1100762 if (k_flag) {
763 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
764 if (pidstr == NULL) {
765 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000766 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100767 exit(1);
768 }
769 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000770 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100771 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000772 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100773 exit(1);
774 }
775 if (kill(pid, SIGTERM) == -1) {
776 perror("kill");
777 exit(1);
778 }
779 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
780 printf(format, SSH_AUTHSOCKET_ENV_NAME);
781 printf(format, SSH_AGENTPID_ENV_NAME);
782 printf("echo Agent pid %d killed;\n", pid);
783 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000784 }
Damien Miller95def091999-11-25 00:26:21 +1100785 parent_pid = getpid();
786
787 /* Create private directory for agent socket */
788 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
789 if (mkdtemp(socket_dir) == NULL) {
790 perror("mkdtemp: private socket dir");
791 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000792 }
Damien Miller95def091999-11-25 00:26:21 +1100793 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000794 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795
Damien Miller5428f641999-11-25 11:54:57 +1100796 /*
797 * Create socket early so it will exist before command gets run from
798 * the parent.
799 */
Damien Miller95def091999-11-25 00:26:21 +1100800 sock = socket(AF_UNIX, SOCK_STREAM, 0);
801 if (sock < 0) {
802 perror("socket");
803 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000804 }
Damien Miller95def091999-11-25 00:26:21 +1100805 memset(&sunaddr, 0, sizeof(sunaddr));
806 sunaddr.sun_family = AF_UNIX;
807 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +0000808#ifdef HAVE_CYGWIN
809 prev_mask = umask(0177);
810#endif
Damien Miller95def091999-11-25 00:26:21 +1100811 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
812 perror("bind");
Ben Lindstrom3524d692001-05-03 22:59:24 +0000813#ifdef HAVE_CYGWIN
814 umask(prev_mask);
815#endif
Damien Miller95def091999-11-25 00:26:21 +1100816 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817 }
Ben Lindstrom3524d692001-05-03 22:59:24 +0000818#ifdef HAVE_CYGWIN
819 umask(prev_mask);
820#endif
Damien Miller95def091999-11-25 00:26:21 +1100821 if (listen(sock, 5) < 0) {
822 perror("listen");
823 cleanup_exit(1);
824 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000825
Damien Miller5428f641999-11-25 11:54:57 +1100826 /*
827 * Fork, and have the parent execute the command, if any, or present
828 * the socket data. The child continues as the authentication agent.
829 */
Damien Miller95def091999-11-25 00:26:21 +1100830 pid = fork();
831 if (pid == -1) {
832 perror("fork");
833 exit(1);
834 }
835 if (pid != 0) { /* Parent - execute the given command. */
836 close(sock);
837 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
838 if (ac == 0) {
839 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
840 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000841 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100842 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100844 printf("echo Agent pid %d;\n", pid);
845 exit(0);
846 }
Damien Millere4340be2000-09-16 13:29:08 +1100847 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
848 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
849 perror("setenv");
850 exit(1);
851 }
Damien Miller95def091999-11-25 00:26:21 +1100852 execvp(av[0], av);
853 perror(av[0]);
854 exit(1);
855 }
856 close(0);
857 close(1);
858 close(2);
859
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000860#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000861 /* deny core dumps, since memory contains unencrypted private keys */
862 rlim.rlim_cur = rlim.rlim_max = 0;
863 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
864 perror("setrlimit rlimit_core failed");
865 cleanup_exit(1);
866 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000867#endif
Damien Miller95def091999-11-25 00:26:21 +1100868 if (setsid() == -1) {
869 perror("setsid");
870 cleanup_exit(1);
871 }
872 if (atexit(cleanup_socket) < 0) {
873 perror("atexit");
874 cleanup_exit(1);
875 }
876 new_socket(AUTH_SOCKET, sock);
877 if (ac > 0) {
878 signal(SIGALRM, check_parent_exists);
879 alarm(10);
880 }
Damien Millerad833b32000-08-23 10:46:23 +1000881 idtab_init();
Damien Miller95def091999-11-25 00:26:21 +1100882 signal(SIGINT, SIG_IGN);
883 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000884 signal(SIGHUP, cleanup_handler);
885 signal(SIGTERM, cleanup_handler);
Damien Miller95def091999-11-25 00:26:21 +1100886 while (1) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000887 prepare_select(&readsetp, &writesetp, &max_fd);
888 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100889 if (errno == EINTR)
890 continue;
891 exit(1);
892 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000893 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100894 }
895 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000896}