blob: 87e94b0ea2bfdbe4cc4c0294b509dad4baa8d2d3 [file] [log] [blame]
Damien Millere3b60b52006-07-10 21:08:03 +10001/* $OpenBSD: ssh-agent.c,v 1.141 2006/07/08 21:47:12 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller42fb0682006-03-15 14:03:06 +110040#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100041#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110042#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#endif
Damien Miller9b481512002-09-12 10:43:29 +100045#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110046#include <sys/resource.h>
Damien Millere3b60b52006-07-10 21:08:03 +100047
Damien Miller03e20032006-03-15 11:16:59 +110048#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110049# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110050#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110051#include <signal.h>
52
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include <openssl/evp.h>
54#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
56#include "ssh.h"
57#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "buffer.h"
59#include "bufaux.h"
60#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "key.h"
62#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110063#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000064#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110065#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Ben Lindstrom3f471632001-07-04 03:53:15 +000067#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000068#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000069#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000070
Damien Miller6c4914a2004-03-03 11:08:59 +110071#if defined(HAVE_SYS_PRCTL_H)
72#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
73#endif
74
Ben Lindstrom65366a82001-12-06 16:32:47 +000075typedef enum {
76 AUTH_UNUSED,
77 AUTH_SOCKET,
78 AUTH_CONNECTION
79} sock_type;
80
Damien Miller95def091999-11-25 00:26:21 +110081typedef struct {
82 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000083 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110084 Buffer input;
85 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000086 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087} SocketEntry;
88
Ben Lindstrom46c16222000-12-22 01:43:59 +000089u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090SocketEntry *sockets = NULL;
91
Damien Miller1a534ae2002-01-22 23:26:13 +110092typedef struct identity {
93 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100094 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110095 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000096 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110097 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098} Identity;
99
Damien Millerad833b32000-08-23 10:46:23 +1000100typedef struct {
101 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100102 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000103} Idtab;
104
105/* private key table, one per protocol version */
106Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
108int max_fd = 0;
109
110/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000111pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
113/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100114char socket_name[MAXPATHLEN];
115char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Ben Lindstrom2f717042002-06-06 21:52:03 +0000117/* locking */
118int locked = 0;
119char *lock_passwd = NULL;
120
Damien Miller95def091999-11-25 00:26:21 +1100121extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100122
Damien Miller53d81482003-01-22 11:47:19 +1100123/* Default lifetime (0 == forever) */
124static int lifetime = 0;
125
Ben Lindstrombba81212001-06-25 05:01:22 +0000126static void
Damien Miller58f34862002-09-04 16:31:21 +1000127close_socket(SocketEntry *e)
128{
Damien Miller58f34862002-09-04 16:31:21 +1000129 close(e->fd);
130 e->fd = -1;
131 e->type = AUTH_UNUSED;
132 buffer_free(&e->input);
133 buffer_free(&e->output);
134 buffer_free(&e->request);
135}
136
137static void
Damien Millerad833b32000-08-23 10:46:23 +1000138idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139{
Damien Millerad833b32000-08-23 10:46:23 +1000140 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000141
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000142 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100143 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000144 idtable[i].nentries = 0;
145 }
146}
147
148/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000149static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000150idtab_lookup(int version)
151{
152 if (version < 1 || version > 2)
153 fatal("internal error, bad protocol version %d", version);
154 return &idtable[version];
155}
156
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000157static void
158free_identity(Identity *id)
159{
160 key_free(id->key);
161 xfree(id->comment);
162 xfree(id);
163}
164
Damien Millerad833b32000-08-23 10:46:23 +1000165/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100166static Identity *
167lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000168{
Damien Miller1a534ae2002-01-22 23:26:13 +1100169 Identity *id;
170
Damien Millerad833b32000-08-23 10:46:23 +1000171 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100172 TAILQ_FOREACH(id, &tab->idlist, next) {
173 if (key_equal(key, id->key))
174 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000175 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100176 return (NULL);
177}
178
Damien Miller6c711792003-01-24 11:36:23 +1100179/* Check confirmation of keysign request */
180static int
181confirm_key(Identity *id)
182{
Darren Tuckerce327b62004-11-05 20:38:03 +1100183 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100184 int ret = -1;
185
186 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100187 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
188 id->comment, p))
189 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100190 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100191
Damien Miller6c711792003-01-24 11:36:23 +1100192 return (ret);
193}
194
Damien Millerad833b32000-08-23 10:46:23 +1000195/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000196static void
Damien Millerad833b32000-08-23 10:46:23 +1000197process_request_identities(SocketEntry *e, int version)
198{
199 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100200 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000201 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202
Damien Miller95def091999-11-25 00:26:21 +1100203 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000204 buffer_put_char(&msg, (version == 1) ?
205 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
206 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100207 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100208 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000209 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
210 buffer_put_bignum(&msg, id->key->rsa->e);
211 buffer_put_bignum(&msg, id->key->rsa->n);
212 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000213 u_char *blob;
214 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100215 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000216 buffer_put_string(&msg, blob, blen);
217 xfree(blob);
218 }
219 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100220 }
221 buffer_put_int(&e->output, buffer_len(&msg));
222 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
223 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000224}
225
Damien Millerad833b32000-08-23 10:46:23 +1000226/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000227static void
Damien Millerad833b32000-08-23 10:46:23 +1000228process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000230 u_char buf[32], mdbuf[16], session_id[16];
231 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000232 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000233 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000234 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100235 Buffer msg;
236 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000237 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Damien Miller95def091999-11-25 00:26:21 +1100239 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100240 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100241 if ((challenge = BN_new()) == NULL)
242 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000243
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000244 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000245 buffer_get_bignum(&e->request, key->rsa->e);
246 buffer_get_bignum(&e->request, key->rsa->n);
247 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Millerad833b32000-08-23 10:46:23 +1000249 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000250 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000251 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000252 buffer_get(&e->request, session_id, 16);
253 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000254 if (response_type != 1)
255 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller1a534ae2002-01-22 23:26:13 +1100257 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100258 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100259 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000260 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100261 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
262 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100263
Damien Millerad833b32000-08-23 10:46:23 +1000264 /* The response is MD5 of decrypted challenge plus session id. */
265 len = BN_num_bytes(challenge);
266 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000267 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000268 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100269 }
Damien Millerad833b32000-08-23 10:46:23 +1000270 memset(buf, 0, 32);
271 BN_bn2bin(challenge, buf + 32 - len);
272 MD5_Init(&md);
273 MD5_Update(&md, buf, 32);
274 MD5_Update(&md, session_id, 16);
275 MD5_Final(mdbuf, &md);
276
277 /* Send the response. */
278 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
279 for (i = 0; i < 16; i++)
280 buffer_put_char(&msg, mdbuf[i]);
281 goto send;
282 }
283
284failure:
285 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100286 buffer_put_char(&msg, SSH_AGENT_FAILURE);
287send:
288 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000289 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
290 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100291 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000292 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293}
294
Damien Millerad833b32000-08-23 10:46:23 +1000295/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000296static void
Damien Millerad833b32000-08-23 10:46:23 +1000297process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000299 u_char *blob, *data, *signature = NULL;
300 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000301 extern int datafellows;
302 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000303 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000304 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000305
306 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000307
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000308 blob = buffer_get_string(&e->request, &blen);
309 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100310
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000311 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100312 if (flags & SSH_AGENT_OLD_SIGNATURE)
313 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000314
Damien Miller0bc1bd82000-11-13 22:57:25 +1100315 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000316 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100317 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100318 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100319 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000320 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000321 }
Damien Millerad833b32000-08-23 10:46:23 +1000322 buffer_init(&msg);
323 if (ok == 0) {
324 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
325 buffer_put_string(&msg, signature, slen);
326 } else {
327 buffer_put_char(&msg, SSH_AGENT_FAILURE);
328 }
329 buffer_put_int(&e->output, buffer_len(&msg));
330 buffer_append(&e->output, buffer_ptr(&msg),
331 buffer_len(&msg));
332 buffer_free(&msg);
333 xfree(data);
334 xfree(blob);
335 if (signature != NULL)
336 xfree(signature);
337}
338
339/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000340static void
Damien Millerad833b32000-08-23 10:46:23 +1000341process_remove_identity(SocketEntry *e, int version)
342{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000343 u_int blen, bits;
344 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100345 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000346 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100347
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000348 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000349 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100350 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000351 bits = buffer_get_int(&e->request);
352 buffer_get_bignum(&e->request, key->rsa->e);
353 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354
Damien Millerad833b32000-08-23 10:46:23 +1000355 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000356 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000357 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000358 break;
359 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000360 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100361 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000362 xfree(blob);
363 break;
364 }
365 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100366 Identity *id = lookup_identity(key, version);
367 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100368 /*
369 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100370 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000371 * the array, we actually free the key there and move
372 * all the entries between the empty slot and the end
373 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100374 */
Damien Millerad833b32000-08-23 10:46:23 +1000375 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100376 if (tab->nentries < 1)
377 fatal("process_remove_identity: "
378 "internal error: tab->nentries %d",
379 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100380 TAILQ_REMOVE(&tab->idlist, id, next);
381 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000382 tab->nentries--;
383 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100384 }
Damien Millerad833b32000-08-23 10:46:23 +1000385 key_free(key);
386 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000387 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000388 buffer_put_char(&e->output,
389 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390}
391
Ben Lindstrombba81212001-06-25 05:01:22 +0000392static void
Damien Millerad833b32000-08-23 10:46:23 +1000393process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394{
Damien Millerad833b32000-08-23 10:46:23 +1000395 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100396 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397
Damien Miller95def091999-11-25 00:26:21 +1100398 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100399 for (id = TAILQ_FIRST(&tab->idlist); id;
400 id = TAILQ_FIRST(&tab->idlist)) {
401 TAILQ_REMOVE(&tab->idlist, id, next);
402 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100403 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404
Damien Miller95def091999-11-25 00:26:21 +1100405 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000406 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
408 /* Send success. */
409 buffer_put_int(&e->output, 1);
410 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100411}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Ben Lindstrombba81212001-06-25 05:01:22 +0000413static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000414reaper(void)
415{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000416 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000417 Identity *id, *nxt;
418 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000419 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000420
421 for (version = 1; version < 3; version++) {
422 tab = idtab_lookup(version);
423 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
424 nxt = TAILQ_NEXT(id, next);
425 if (id->death != 0 && now >= id->death) {
426 TAILQ_REMOVE(&tab->idlist, id, next);
427 free_identity(id);
428 tab->nentries--;
429 }
430 }
431 }
432}
433
434static void
Damien Millerad833b32000-08-23 10:46:23 +1000435process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100436{
Damien Millerad833b32000-08-23 10:46:23 +1000437 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100438 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000439 char *type_name, *comment;
440 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100441
Damien Millerad833b32000-08-23 10:46:23 +1000442 switch (version) {
443 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100444 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000445 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000446 buffer_get_bignum(&e->request, k->rsa->n);
447 buffer_get_bignum(&e->request, k->rsa->e);
448 buffer_get_bignum(&e->request, k->rsa->d);
449 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100450
Damien Millerad833b32000-08-23 10:46:23 +1000451 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000452 buffer_get_bignum(&e->request, k->rsa->q); /* p */
453 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100454
Damien Millerad833b32000-08-23 10:46:23 +1000455 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000456 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000457 break;
458 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000459 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000460 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100461 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000462 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100463 case KEY_DSA:
464 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000465 buffer_get_bignum2(&e->request, k->dsa->p);
466 buffer_get_bignum2(&e->request, k->dsa->q);
467 buffer_get_bignum2(&e->request, k->dsa->g);
468 buffer_get_bignum2(&e->request, k->dsa->pub_key);
469 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 break;
471 case KEY_RSA:
472 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000473 buffer_get_bignum2(&e->request, k->rsa->n);
474 buffer_get_bignum2(&e->request, k->rsa->e);
475 buffer_get_bignum2(&e->request, k->rsa->d);
476 buffer_get_bignum2(&e->request, k->rsa->iqmp);
477 buffer_get_bignum2(&e->request, k->rsa->p);
478 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479
480 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000481 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100482 break;
483 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000484 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000485 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100486 }
Damien Millerad833b32000-08-23 10:46:23 +1000487 break;
488 }
Damien Millerc51d0732003-03-15 11:37:09 +1100489 /* enable blinding */
490 switch (k->type) {
491 case KEY_RSA:
492 case KEY_RSA1:
493 if (RSA_blinding_on(k->rsa, NULL) != 1) {
494 error("process_add_identity: RSA_blinding_on failed");
495 key_free(k);
496 goto send;
497 }
498 break;
499 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000500 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000501 if (k == NULL) {
502 xfree(comment);
503 goto send;
504 }
505 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000506 while (buffer_len(&e->request)) {
507 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000508 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000509 death = time(NULL) + buffer_get_int(&e->request);
510 break;
Damien Miller6c711792003-01-24 11:36:23 +1100511 case SSH_AGENT_CONSTRAIN_CONFIRM:
512 confirm = 1;
513 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000514 default:
515 break;
516 }
517 }
Damien Miller53d81482003-01-22 11:47:19 +1100518 if (lifetime && !death)
519 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000520 if (lookup_identity(k, version) == NULL) {
521 Identity *id = xmalloc(sizeof(Identity));
522 id->key = k;
523 id->comment = comment;
524 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100525 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000526 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
527 /* Increment the number of identities. */
528 tab->nentries++;
529 } else {
530 key_free(k);
531 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000533send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000534 buffer_put_int(&e->output, 1);
535 buffer_put_char(&e->output,
536 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
537}
538
Ben Lindstrom2f717042002-06-06 21:52:03 +0000539/* XXX todo: encrypt sensitive data with passphrase */
540static void
541process_lock_agent(SocketEntry *e, int lock)
542{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000543 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000544 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000545
546 passwd = buffer_get_string(&e->request, NULL);
547 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
548 locked = 0;
549 memset(lock_passwd, 0, strlen(lock_passwd));
550 xfree(lock_passwd);
551 lock_passwd = NULL;
552 success = 1;
553 } else if (!locked && lock) {
554 locked = 1;
555 lock_passwd = xstrdup(passwd);
556 success = 1;
557 }
558 memset(passwd, 0, strlen(passwd));
559 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000560
Ben Lindstrom2f717042002-06-06 21:52:03 +0000561 buffer_put_int(&e->output, 1);
562 buffer_put_char(&e->output,
563 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000564}
565
566static void
567no_identities(SocketEntry *e, u_int type)
568{
569 Buffer msg;
570
571 buffer_init(&msg);
572 buffer_put_char(&msg,
573 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
574 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
575 buffer_put_int(&msg, 0);
576 buffer_put_int(&e->output, buffer_len(&msg));
577 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
578 buffer_free(&msg);
579}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000580
581#ifdef SMARTCARD
582static void
583process_add_smartcard_key (SocketEntry *e)
584{
Ben Lindstromba72d302002-03-22 03:51:06 +0000585 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000586 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000587 Key **keys, *k;
588 Identity *id;
589 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100590
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000591 sc_reader_id = buffer_get_string(&e->request, NULL);
592 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000593
594 while (buffer_len(&e->request)) {
595 switch (buffer_get_char(&e->request)) {
596 case SSH_AGENT_CONSTRAIN_LIFETIME:
597 death = time(NULL) + buffer_get_int(&e->request);
598 break;
599 case SSH_AGENT_CONSTRAIN_CONFIRM:
600 confirm = 1;
601 break;
602 default:
603 break;
604 }
605 }
606 if (lifetime && !death)
607 death = time(NULL) + lifetime;
608
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000609 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000610 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000611 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000612
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000613 if (keys == NULL || keys[0] == NULL) {
614 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615 goto send;
616 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000617 for (i = 0; keys[i] != NULL; i++) {
618 k = keys[i];
619 version = k->type == KEY_RSA1 ? 1 : 2;
620 tab = idtab_lookup(version);
621 if (lookup_identity(k, version) == NULL) {
622 id = xmalloc(sizeof(Identity));
623 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000624 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000625 id->death = death;
626 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000627 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
628 tab->nentries++;
629 success = 1;
630 } else {
631 key_free(k);
632 }
633 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000634 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000635 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000636send:
637 buffer_put_int(&e->output, 1);
638 buffer_put_char(&e->output,
639 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
640}
641
642static void
643process_remove_smartcard_key(SocketEntry *e)
644{
Ben Lindstromba72d302002-03-22 03:51:06 +0000645 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000646 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000647 Key **keys, *k = NULL;
648 Identity *id;
649 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000650
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000651 sc_reader_id = buffer_get_string(&e->request, NULL);
652 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000653 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000654 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000655 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000656
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000657 if (keys == NULL || keys[0] == NULL) {
658 error("sc_get_keys failed");
659 goto send;
660 }
661 for (i = 0; keys[i] != NULL; i++) {
662 k = keys[i];
663 version = k->type == KEY_RSA1 ? 1 : 2;
664 if ((id = lookup_identity(k, version)) != NULL) {
665 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000666 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000667 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100668 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669 success = 1;
670 }
671 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000672 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000673 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000674 xfree(keys);
675send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000676 buffer_put_int(&e->output, 1);
677 buffer_put_char(&e->output,
678 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
679}
Ben Lindstromffce1472001-08-06 21:57:31 +0000680#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000681
Damien Millerad833b32000-08-23 10:46:23 +1000682/* dispatch incoming messages */
683
Ben Lindstrombba81212001-06-25 05:01:22 +0000684static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000685process_message(SocketEntry *e)
686{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000687 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000688 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000689
690 /* kill dead keys */
691 reaper();
692
Damien Miller95def091999-11-25 00:26:21 +1100693 if (buffer_len(&e->input) < 5)
694 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100695 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100696 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100697 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000698 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100699 return;
700 }
701 if (buffer_len(&e->input) < msg_len + 4)
702 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000703
704 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100705 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000706 buffer_clear(&e->request);
707 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
708 buffer_consume(&e->input, msg_len);
709 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000710
Ben Lindstrom2f717042002-06-06 21:52:03 +0000711 /* check wheter agent is locked */
712 if (locked && type != SSH_AGENTC_UNLOCK) {
713 buffer_clear(&e->request);
714 switch (type) {
715 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
716 case SSH2_AGENTC_REQUEST_IDENTITIES:
717 /* send empty lists */
718 no_identities(e, type);
719 break;
720 default:
721 /* send a fail message for all other request types */
722 buffer_put_int(&e->output, 1);
723 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
724 }
725 return;
726 }
727
Ben Lindstrom3f471632001-07-04 03:53:15 +0000728 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100729 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000730 case SSH_AGENTC_LOCK:
731 case SSH_AGENTC_UNLOCK:
732 process_lock_agent(e, type == SSH_AGENTC_LOCK);
733 break;
Damien Millerad833b32000-08-23 10:46:23 +1000734 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100735 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000736 process_authentication_challenge1(e);
737 break;
738 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
739 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100740 break;
741 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000742 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000743 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100744 break;
745 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000746 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100747 break;
748 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000749 process_remove_all_identities(e, 1);
750 break;
751 /* ssh2 */
752 case SSH2_AGENTC_SIGN_REQUEST:
753 process_sign_request2(e);
754 break;
755 case SSH2_AGENTC_REQUEST_IDENTITIES:
756 process_request_identities(e, 2);
757 break;
758 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000759 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000760 process_add_identity(e, 2);
761 break;
762 case SSH2_AGENTC_REMOVE_IDENTITY:
763 process_remove_identity(e, 2);
764 break;
765 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
766 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100767 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000768#ifdef SMARTCARD
769 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000770 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000771 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100772 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000773 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
774 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100775 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000776#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100777 default:
778 /* Unknown message. Respond with failure. */
779 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000780 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100781 buffer_put_int(&e->output, 1);
782 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
783 break;
784 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000785}
786
Ben Lindstrombba81212001-06-25 05:01:22 +0000787static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000788new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000789{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000790 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000791
Damien Miller232711f2004-06-15 10:35:30 +1000792 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793
Damien Miller95def091999-11-25 00:26:21 +1100794 if (fd > max_fd)
795 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796
Damien Miller95def091999-11-25 00:26:21 +1100797 for (i = 0; i < sockets_alloc; i++)
798 if (sockets[i].type == AUTH_UNUSED) {
799 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100800 buffer_init(&sockets[i].input);
801 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000802 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000803 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100804 return;
805 }
806 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000807 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100808 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000809 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100810 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100812 sockets[old_alloc].fd = fd;
813 buffer_init(&sockets[old_alloc].input);
814 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000815 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000816 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817}
818
Ben Lindstrombba81212001-06-25 05:01:22 +0000819static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000820prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_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,
Damien Miller90967402006-03-26 14:07:26 +1100889 (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
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000951cleanup_socket(void)
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
Darren Tucker3e33cec2003-10-02 16:12:36 +1000959void
Damien Miller792c5111999-10-29 09:47:09 +1000960cleanup_exit(int i)
961{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000962 cleanup_socket();
963 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000964}
965
Damien Miller1c13bd82006-03-26 14:28:14 +1100966/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000967static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000968cleanup_handler(int sig)
969{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000970 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000971 _exit(2);
972}
973
Damien Miller1c13bd82006-03-26 14:28:14 +1100974/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000976check_parent_exists(int sig)
977{
978 int save_errno = errno;
979
980 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
981 /* printf("Parent has died - Authentication agent exiting.\n"); */
982 cleanup_handler(sig); /* safe */
983 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000984 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000985 alarm(10);
986 errno = save_errno;
987}
988
989static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000990usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000991{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000992 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000993 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000994 fprintf(stderr, "Options:\n");
995 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
996 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
997 fprintf(stderr, " -k Kill the current agent.\n");
998 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000999 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001000 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001001 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001002}
1003
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004int
1005main(int ac, char **av)
1006{
Damien Miller6c711792003-01-24 11:36:23 +11001007 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001008 int sock, fd, ch;
1009 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001010 char *shell, *format, *pidstr, *agentsocket = NULL;
1011 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001012 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001014 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001015#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001016 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001017 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001018 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001019 pid_t pid;
1020 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001021
Darren Tuckerce321d82005-10-03 18:11:24 +10001022 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1023 sanitise_stdfd();
1024
Damien Miller6cffb9a2002-09-04 16:20:26 +10001025 /* drop */
1026 setegid(getgid());
1027 setgid(getgid());
1028
Damien Miller6c4914a2004-03-03 11:08:59 +11001029#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1030 /* Disable ptrace on Linux without sgid bit */
1031 prctl(PR_SET_DUMPABLE, 0);
1032#endif
1033
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001034 SSLeay_add_all_algorithms();
1035
Damien Miller59d3d5b2003-08-22 09:34:41 +10001036 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001037 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001038 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001039
Damien Miller53d81482003-01-22 11:47:19 +11001040 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001041 switch (ch) {
1042 case 'c':
1043 if (s_flag)
1044 usage();
1045 c_flag++;
1046 break;
1047 case 'k':
1048 k_flag++;
1049 break;
1050 case 's':
1051 if (c_flag)
1052 usage();
1053 s_flag++;
1054 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001055 case 'd':
1056 if (d_flag)
1057 usage();
1058 d_flag++;
1059 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001060 case 'a':
1061 agentsocket = optarg;
1062 break;
Damien Miller53d81482003-01-22 11:47:19 +11001063 case 't':
1064 if ((lifetime = convtime(optarg)) == -1) {
1065 fprintf(stderr, "Invalid lifetime\n");
1066 usage();
1067 }
1068 break;
Damien Miller95def091999-11-25 00:26:21 +11001069 default:
1070 usage();
1071 }
Damien Miller792c5111999-10-29 09:47:09 +10001072 }
Damien Miller95def091999-11-25 00:26:21 +11001073 ac -= optind;
1074 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001075
Ben Lindstromd94580c2001-07-04 03:48:02 +00001076 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001077 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001078
Ben Lindstromeecdf232002-04-02 21:03:51 +00001079 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001080 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001081 if (shell != NULL &&
1082 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001083 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001084 }
Damien Miller95def091999-11-25 00:26:21 +11001085 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001086 const char *errstr = NULL;
1087
Damien Miller95def091999-11-25 00:26:21 +11001088 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1089 if (pidstr == NULL) {
1090 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001091 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001092 exit(1);
1093 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001094 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1095 if (errstr) {
1096 fprintf(stderr,
1097 "%s=\"%s\", which is not a good PID: %s\n",
1098 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001099 exit(1);
1100 }
1101 if (kill(pid, SIGTERM) == -1) {
1102 perror("kill");
1103 exit(1);
1104 }
1105 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1106 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1107 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001108 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001109 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001110 }
Damien Miller95def091999-11-25 00:26:21 +11001111 parent_pid = getpid();
1112
Ben Lindstromb7788f32002-06-06 21:46:08 +00001113 if (agentsocket == NULL) {
1114 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001115 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001116 if (mkdtemp(socket_dir) == NULL) {
1117 perror("mkdtemp: private socket dir");
1118 exit(1);
1119 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001120 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1121 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001122 } else {
1123 /* Try to use specified agent socket */
1124 socket_dir[0] = '\0';
1125 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001126 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
Damien Miller5428f641999-11-25 11:54:57 +11001128 /*
1129 * Create socket early so it will exist before command gets run from
1130 * the parent.
1131 */
Damien Miller95def091999-11-25 00:26:21 +11001132 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1133 if (sock < 0) {
1134 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001135 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001136 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001137 }
Damien Miller95def091999-11-25 00:26:21 +11001138 memset(&sunaddr, 0, sizeof(sunaddr));
1139 sunaddr.sun_family = AF_UNIX;
1140 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001141 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001142 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001143 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001144 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001145 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001146 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001148 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001149 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001150 perror("listen");
1151 cleanup_exit(1);
1152 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001153
Damien Miller5428f641999-11-25 11:54:57 +11001154 /*
1155 * Fork, and have the parent execute the command, if any, or present
1156 * the socket data. The child continues as the authentication agent.
1157 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001158 if (d_flag) {
1159 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1160 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1161 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1162 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001163 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001164 goto skip;
1165 }
Damien Miller95def091999-11-25 00:26:21 +11001166 pid = fork();
1167 if (pid == -1) {
1168 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001169 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001170 }
1171 if (pid != 0) { /* Parent - execute the given command. */
1172 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001173 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001174 if (ac == 0) {
1175 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1176 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001177 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001178 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001179 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001180 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001181 exit(0);
1182 }
Damien Millere4340be2000-09-16 13:29:08 +11001183 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1184 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1185 perror("setenv");
1186 exit(1);
1187 }
Damien Miller95def091999-11-25 00:26:21 +11001188 execvp(av[0], av);
1189 perror(av[0]);
1190 exit(1);
1191 }
Damien Millerc653b892002-02-08 22:05:41 +11001192 /* child */
1193 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001194
1195 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001196 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001197 cleanup_exit(1);
1198 }
1199
1200 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001201 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1202 /* XXX might close listen socket */
1203 (void)dup2(fd, STDIN_FILENO);
1204 (void)dup2(fd, STDOUT_FILENO);
1205 (void)dup2(fd, STDERR_FILENO);
1206 if (fd > 2)
1207 close(fd);
1208 }
Damien Miller95def091999-11-25 00:26:21 +11001209
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001210#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001211 /* deny core dumps, since memory contains unencrypted private keys */
1212 rlim.rlim_cur = rlim.rlim_max = 0;
1213 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001214 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001215 cleanup_exit(1);
1216 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001217#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001218
1219skip:
Damien Miller95def091999-11-25 00:26:21 +11001220 new_socket(AUTH_SOCKET, sock);
1221 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001222 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001223 alarm(10);
1224 }
Damien Millerad833b32000-08-23 10:46:23 +10001225 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001226 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001227 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001228 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001229 signal(SIGHUP, cleanup_handler);
1230 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001231 nalloc = 0;
1232
Damien Miller95def091999-11-25 00:26:21 +11001233 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001234 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001235 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001236 if (errno == EINTR)
1237 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001238 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001239 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001240 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001241 }
1242 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243}