blob: e4b620483ead2e9735c54394e4ed8d39b9f49d25 [file] [log] [blame]
Damien Miller9aec9192006-08-05 10:57:45 +10001/* $OpenBSD: ssh-agent.c,v 1.147 2006/07/25 02:59:21 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 Miller9aec9192006-08-05 10:57:45 +100042#ifdef HAVE_SYS_TIME_H
43# include <sys/time.h>
44#endif
Damien Miller574c41f2006-03-15 11:40:10 +110045#ifdef HAVE_SYS_UN_H
46# include <sys/un.h>
47#endif
Damien Miller9b481512002-09-12 10:43:29 +100048#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110049#include <sys/resource.h>
Damien Millere3b60b52006-07-10 21:08:03 +100050
Damien Millere3476ed2006-07-24 14:13:33 +100051#include <openssl/evp.h>
52#include <openssl/md5.h>
53
Darren Tucker39972492006-07-12 22:22:46 +100054#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100055#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110056#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110057# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110058#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110059#include <signal.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100060#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100061#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100062#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110063
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "ssh.h"
65#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "buffer.h"
67#include "bufaux.h"
68#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100069#include "key.h"
70#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110071#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110073#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Ben Lindstrom3f471632001-07-04 03:53:15 +000075#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000076#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000077#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000078
Damien Miller6c4914a2004-03-03 11:08:59 +110079#if defined(HAVE_SYS_PRCTL_H)
80#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
81#endif
82
Ben Lindstrom65366a82001-12-06 16:32:47 +000083typedef enum {
84 AUTH_UNUSED,
85 AUTH_SOCKET,
86 AUTH_CONNECTION
87} sock_type;
88
Damien Miller95def091999-11-25 00:26:21 +110089typedef struct {
90 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000091 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110092 Buffer input;
93 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000094 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095} SocketEntry;
96
Ben Lindstrom46c16222000-12-22 01:43:59 +000097u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098SocketEntry *sockets = NULL;
99
Damien Miller1a534ae2002-01-22 23:26:13 +1100100typedef struct identity {
101 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000102 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100103 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000104 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100105 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106} Identity;
107
Damien Millerad833b32000-08-23 10:46:23 +1000108typedef struct {
109 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100110 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000111} Idtab;
112
113/* private key table, one per protocol version */
114Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
116int max_fd = 0;
117
118/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000119pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
121/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100122char socket_name[MAXPATHLEN];
123char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
Ben Lindstrom2f717042002-06-06 21:52:03 +0000125/* locking */
126int locked = 0;
127char *lock_passwd = NULL;
128
Damien Miller95def091999-11-25 00:26:21 +1100129extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100130
Damien Miller53d81482003-01-22 11:47:19 +1100131/* Default lifetime (0 == forever) */
132static int lifetime = 0;
133
Ben Lindstrombba81212001-06-25 05:01:22 +0000134static void
Damien Miller58f34862002-09-04 16:31:21 +1000135close_socket(SocketEntry *e)
136{
Damien Miller58f34862002-09-04 16:31:21 +1000137 close(e->fd);
138 e->fd = -1;
139 e->type = AUTH_UNUSED;
140 buffer_free(&e->input);
141 buffer_free(&e->output);
142 buffer_free(&e->request);
143}
144
145static void
Damien Millerad833b32000-08-23 10:46:23 +1000146idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147{
Damien Millerad833b32000-08-23 10:46:23 +1000148 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000149
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000150 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100151 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000152 idtable[i].nentries = 0;
153 }
154}
155
156/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000157static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000158idtab_lookup(int version)
159{
160 if (version < 1 || version > 2)
161 fatal("internal error, bad protocol version %d", version);
162 return &idtable[version];
163}
164
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000165static void
166free_identity(Identity *id)
167{
168 key_free(id->key);
169 xfree(id->comment);
170 xfree(id);
171}
172
Damien Millerad833b32000-08-23 10:46:23 +1000173/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100174static Identity *
175lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000176{
Damien Miller1a534ae2002-01-22 23:26:13 +1100177 Identity *id;
178
Damien Millerad833b32000-08-23 10:46:23 +1000179 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100180 TAILQ_FOREACH(id, &tab->idlist, next) {
181 if (key_equal(key, id->key))
182 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000183 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100184 return (NULL);
185}
186
Damien Miller6c711792003-01-24 11:36:23 +1100187/* Check confirmation of keysign request */
188static int
189confirm_key(Identity *id)
190{
Darren Tuckerce327b62004-11-05 20:38:03 +1100191 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100192 int ret = -1;
193
194 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100195 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
196 id->comment, p))
197 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100198 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100199
Damien Miller6c711792003-01-24 11:36:23 +1100200 return (ret);
201}
202
Damien Millerad833b32000-08-23 10:46:23 +1000203/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000204static void
Damien Millerad833b32000-08-23 10:46:23 +1000205process_request_identities(SocketEntry *e, int version)
206{
207 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100208 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000209 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Miller95def091999-11-25 00:26:21 +1100211 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000212 buffer_put_char(&msg, (version == 1) ?
213 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
214 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100215 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100216 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000217 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
218 buffer_put_bignum(&msg, id->key->rsa->e);
219 buffer_put_bignum(&msg, id->key->rsa->n);
220 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000221 u_char *blob;
222 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100223 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000224 buffer_put_string(&msg, blob, blen);
225 xfree(blob);
226 }
227 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100228 }
229 buffer_put_int(&e->output, buffer_len(&msg));
230 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
231 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232}
233
Damien Millerad833b32000-08-23 10:46:23 +1000234/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000235static void
Damien Millerad833b32000-08-23 10:46:23 +1000236process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000238 u_char buf[32], mdbuf[16], session_id[16];
239 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000240 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000241 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000242 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100243 Buffer msg;
244 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000245 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller95def091999-11-25 00:26:21 +1100247 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100248 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100249 if ((challenge = BN_new()) == NULL)
250 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000251
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000252 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000253 buffer_get_bignum(&e->request, key->rsa->e);
254 buffer_get_bignum(&e->request, key->rsa->n);
255 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Millerad833b32000-08-23 10:46:23 +1000257 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000258 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000259 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000260 buffer_get(&e->request, session_id, 16);
261 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000262 if (response_type != 1)
263 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller1a534ae2002-01-22 23:26:13 +1100265 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100266 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100267 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000268 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100269 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
270 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100271
Damien Millerad833b32000-08-23 10:46:23 +1000272 /* The response is MD5 of decrypted challenge plus session id. */
273 len = BN_num_bytes(challenge);
274 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000275 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000276 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100277 }
Damien Millerad833b32000-08-23 10:46:23 +1000278 memset(buf, 0, 32);
279 BN_bn2bin(challenge, buf + 32 - len);
280 MD5_Init(&md);
281 MD5_Update(&md, buf, 32);
282 MD5_Update(&md, session_id, 16);
283 MD5_Final(mdbuf, &md);
284
285 /* Send the response. */
286 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
287 for (i = 0; i < 16; i++)
288 buffer_put_char(&msg, mdbuf[i]);
289 goto send;
290 }
291
292failure:
293 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100294 buffer_put_char(&msg, SSH_AGENT_FAILURE);
295send:
296 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000297 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
298 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100299 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000300 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301}
302
Damien Millerad833b32000-08-23 10:46:23 +1000303/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static void
Damien Millerad833b32000-08-23 10:46:23 +1000305process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000307 u_char *blob, *data, *signature = NULL;
308 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000309 extern int datafellows;
310 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000311 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000312 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000313
314 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000315
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000316 blob = buffer_get_string(&e->request, &blen);
317 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100318
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000319 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100320 if (flags & SSH_AGENT_OLD_SIGNATURE)
321 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000322
Damien Miller0bc1bd82000-11-13 22:57:25 +1100323 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000324 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100325 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100326 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100327 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000328 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000329 }
Damien Millerad833b32000-08-23 10:46:23 +1000330 buffer_init(&msg);
331 if (ok == 0) {
332 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
333 buffer_put_string(&msg, signature, slen);
334 } else {
335 buffer_put_char(&msg, SSH_AGENT_FAILURE);
336 }
337 buffer_put_int(&e->output, buffer_len(&msg));
338 buffer_append(&e->output, buffer_ptr(&msg),
339 buffer_len(&msg));
340 buffer_free(&msg);
341 xfree(data);
342 xfree(blob);
343 if (signature != NULL)
344 xfree(signature);
345}
346
347/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000348static void
Damien Millerad833b32000-08-23 10:46:23 +1000349process_remove_identity(SocketEntry *e, int version)
350{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000351 u_int blen, bits;
352 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100353 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000354 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100355
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000356 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000357 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100358 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000359 bits = buffer_get_int(&e->request);
360 buffer_get_bignum(&e->request, key->rsa->e);
361 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Millerad833b32000-08-23 10:46:23 +1000363 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000364 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000365 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000366 break;
367 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000368 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100369 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000370 xfree(blob);
371 break;
372 }
373 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100374 Identity *id = lookup_identity(key, version);
375 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100376 /*
377 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100378 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000379 * the array, we actually free the key there and move
380 * all the entries between the empty slot and the end
381 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100382 */
Damien Millerad833b32000-08-23 10:46:23 +1000383 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100384 if (tab->nentries < 1)
385 fatal("process_remove_identity: "
386 "internal error: tab->nentries %d",
387 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100388 TAILQ_REMOVE(&tab->idlist, id, next);
389 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000390 tab->nentries--;
391 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100392 }
Damien Millerad833b32000-08-23 10:46:23 +1000393 key_free(key);
394 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000396 buffer_put_char(&e->output,
397 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398}
399
Ben Lindstrombba81212001-06-25 05:01:22 +0000400static void
Damien Millerad833b32000-08-23 10:46:23 +1000401process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402{
Damien Millerad833b32000-08-23 10:46:23 +1000403 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100404 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
Damien Miller95def091999-11-25 00:26:21 +1100406 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100407 for (id = TAILQ_FIRST(&tab->idlist); id;
408 id = TAILQ_FIRST(&tab->idlist)) {
409 TAILQ_REMOVE(&tab->idlist, id, next);
410 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100411 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000412
Damien Miller95def091999-11-25 00:26:21 +1100413 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000414 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
416 /* Send success. */
417 buffer_put_int(&e->output, 1);
418 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100419}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Ben Lindstrombba81212001-06-25 05:01:22 +0000421static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000422reaper(void)
423{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000424 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000425 Identity *id, *nxt;
426 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000427 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000428
429 for (version = 1; version < 3; version++) {
430 tab = idtab_lookup(version);
431 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
432 nxt = TAILQ_NEXT(id, next);
433 if (id->death != 0 && now >= id->death) {
434 TAILQ_REMOVE(&tab->idlist, id, next);
435 free_identity(id);
436 tab->nentries--;
437 }
438 }
439 }
440}
441
442static void
Damien Millerad833b32000-08-23 10:46:23 +1000443process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100444{
Damien Millerad833b32000-08-23 10:46:23 +1000445 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100446 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000447 char *type_name, *comment;
448 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100449
Damien Millerad833b32000-08-23 10:46:23 +1000450 switch (version) {
451 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000453 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000454 buffer_get_bignum(&e->request, k->rsa->n);
455 buffer_get_bignum(&e->request, k->rsa->e);
456 buffer_get_bignum(&e->request, k->rsa->d);
457 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100458
Damien Millerad833b32000-08-23 10:46:23 +1000459 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000460 buffer_get_bignum(&e->request, k->rsa->q); /* p */
461 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100462
Damien Millerad833b32000-08-23 10:46:23 +1000463 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000464 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000465 break;
466 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000467 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000468 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100469 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000470 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 case KEY_DSA:
472 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000473 buffer_get_bignum2(&e->request, k->dsa->p);
474 buffer_get_bignum2(&e->request, k->dsa->q);
475 buffer_get_bignum2(&e->request, k->dsa->g);
476 buffer_get_bignum2(&e->request, k->dsa->pub_key);
477 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 break;
479 case KEY_RSA:
480 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000481 buffer_get_bignum2(&e->request, k->rsa->n);
482 buffer_get_bignum2(&e->request, k->rsa->e);
483 buffer_get_bignum2(&e->request, k->rsa->d);
484 buffer_get_bignum2(&e->request, k->rsa->iqmp);
485 buffer_get_bignum2(&e->request, k->rsa->p);
486 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100487
488 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000489 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 break;
491 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000492 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000493 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100494 }
Damien Millerad833b32000-08-23 10:46:23 +1000495 break;
496 }
Damien Millerc51d0732003-03-15 11:37:09 +1100497 /* enable blinding */
498 switch (k->type) {
499 case KEY_RSA:
500 case KEY_RSA1:
501 if (RSA_blinding_on(k->rsa, NULL) != 1) {
502 error("process_add_identity: RSA_blinding_on failed");
503 key_free(k);
504 goto send;
505 }
506 break;
507 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000508 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000509 if (k == NULL) {
510 xfree(comment);
511 goto send;
512 }
513 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000514 while (buffer_len(&e->request)) {
515 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000516 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000517 death = time(NULL) + buffer_get_int(&e->request);
518 break;
Damien Miller6c711792003-01-24 11:36:23 +1100519 case SSH_AGENT_CONSTRAIN_CONFIRM:
520 confirm = 1;
521 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000522 default:
523 break;
524 }
525 }
Damien Miller53d81482003-01-22 11:47:19 +1100526 if (lifetime && !death)
527 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000528 if (lookup_identity(k, version) == NULL) {
529 Identity *id = xmalloc(sizeof(Identity));
530 id->key = k;
531 id->comment = comment;
532 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100533 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000534 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
535 /* Increment the number of identities. */
536 tab->nentries++;
537 } else {
538 key_free(k);
539 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000540 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000541send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000542 buffer_put_int(&e->output, 1);
543 buffer_put_char(&e->output,
544 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
545}
546
Ben Lindstrom2f717042002-06-06 21:52:03 +0000547/* XXX todo: encrypt sensitive data with passphrase */
548static void
549process_lock_agent(SocketEntry *e, int lock)
550{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000551 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000552 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000553
554 passwd = buffer_get_string(&e->request, NULL);
555 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
556 locked = 0;
557 memset(lock_passwd, 0, strlen(lock_passwd));
558 xfree(lock_passwd);
559 lock_passwd = NULL;
560 success = 1;
561 } else if (!locked && lock) {
562 locked = 1;
563 lock_passwd = xstrdup(passwd);
564 success = 1;
565 }
566 memset(passwd, 0, strlen(passwd));
567 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000568
Ben Lindstrom2f717042002-06-06 21:52:03 +0000569 buffer_put_int(&e->output, 1);
570 buffer_put_char(&e->output,
571 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000572}
573
574static void
575no_identities(SocketEntry *e, u_int type)
576{
577 Buffer msg;
578
579 buffer_init(&msg);
580 buffer_put_char(&msg,
581 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
582 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
583 buffer_put_int(&msg, 0);
584 buffer_put_int(&e->output, buffer_len(&msg));
585 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
586 buffer_free(&msg);
587}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000588
589#ifdef SMARTCARD
590static void
591process_add_smartcard_key (SocketEntry *e)
592{
Ben Lindstromba72d302002-03-22 03:51:06 +0000593 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000594 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000595 Key **keys, *k;
596 Identity *id;
597 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100598
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000599 sc_reader_id = buffer_get_string(&e->request, NULL);
600 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000601
602 while (buffer_len(&e->request)) {
603 switch (buffer_get_char(&e->request)) {
604 case SSH_AGENT_CONSTRAIN_LIFETIME:
605 death = time(NULL) + buffer_get_int(&e->request);
606 break;
607 case SSH_AGENT_CONSTRAIN_CONFIRM:
608 confirm = 1;
609 break;
610 default:
611 break;
612 }
613 }
614 if (lifetime && !death)
615 death = time(NULL) + lifetime;
616
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000617 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000618 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000619 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000620
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000621 if (keys == NULL || keys[0] == NULL) {
622 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000623 goto send;
624 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000625 for (i = 0; keys[i] != NULL; i++) {
626 k = keys[i];
627 version = k->type == KEY_RSA1 ? 1 : 2;
628 tab = idtab_lookup(version);
629 if (lookup_identity(k, version) == NULL) {
630 id = xmalloc(sizeof(Identity));
631 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000632 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000633 id->death = death;
634 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000635 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
636 tab->nentries++;
637 success = 1;
638 } else {
639 key_free(k);
640 }
641 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000642 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000643 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000644send:
645 buffer_put_int(&e->output, 1);
646 buffer_put_char(&e->output,
647 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
648}
649
650static void
651process_remove_smartcard_key(SocketEntry *e)
652{
Ben Lindstromba72d302002-03-22 03:51:06 +0000653 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000654 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000655 Key **keys, *k = NULL;
656 Identity *id;
657 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000659 sc_reader_id = buffer_get_string(&e->request, NULL);
660 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000661 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000662 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000663 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000664
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000665 if (keys == NULL || keys[0] == NULL) {
666 error("sc_get_keys failed");
667 goto send;
668 }
669 for (i = 0; keys[i] != NULL; i++) {
670 k = keys[i];
671 version = k->type == KEY_RSA1 ? 1 : 2;
672 if ((id = lookup_identity(k, version)) != NULL) {
673 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000674 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000675 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100676 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677 success = 1;
678 }
679 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000680 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000681 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000682 xfree(keys);
683send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000684 buffer_put_int(&e->output, 1);
685 buffer_put_char(&e->output,
686 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
687}
Ben Lindstromffce1472001-08-06 21:57:31 +0000688#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000689
Damien Millerad833b32000-08-23 10:46:23 +1000690/* dispatch incoming messages */
691
Ben Lindstrombba81212001-06-25 05:01:22 +0000692static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000693process_message(SocketEntry *e)
694{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000695 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000696 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000697
698 /* kill dead keys */
699 reaper();
700
Damien Miller95def091999-11-25 00:26:21 +1100701 if (buffer_len(&e->input) < 5)
702 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100703 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100704 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100705 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000706 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100707 return;
708 }
709 if (buffer_len(&e->input) < msg_len + 4)
710 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000711
712 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100713 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000714 buffer_clear(&e->request);
715 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
716 buffer_consume(&e->input, msg_len);
717 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000718
Ben Lindstrom2f717042002-06-06 21:52:03 +0000719 /* check wheter agent is locked */
720 if (locked && type != SSH_AGENTC_UNLOCK) {
721 buffer_clear(&e->request);
722 switch (type) {
723 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
724 case SSH2_AGENTC_REQUEST_IDENTITIES:
725 /* send empty lists */
726 no_identities(e, type);
727 break;
728 default:
729 /* send a fail message for all other request types */
730 buffer_put_int(&e->output, 1);
731 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
732 }
733 return;
734 }
735
Ben Lindstrom3f471632001-07-04 03:53:15 +0000736 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100737 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000738 case SSH_AGENTC_LOCK:
739 case SSH_AGENTC_UNLOCK:
740 process_lock_agent(e, type == SSH_AGENTC_LOCK);
741 break;
Damien Millerad833b32000-08-23 10:46:23 +1000742 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100743 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000744 process_authentication_challenge1(e);
745 break;
746 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
747 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100748 break;
749 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000750 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000751 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100752 break;
753 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000754 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100755 break;
756 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000757 process_remove_all_identities(e, 1);
758 break;
759 /* ssh2 */
760 case SSH2_AGENTC_SIGN_REQUEST:
761 process_sign_request2(e);
762 break;
763 case SSH2_AGENTC_REQUEST_IDENTITIES:
764 process_request_identities(e, 2);
765 break;
766 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000767 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000768 process_add_identity(e, 2);
769 break;
770 case SSH2_AGENTC_REMOVE_IDENTITY:
771 process_remove_identity(e, 2);
772 break;
773 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
774 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100775 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000776#ifdef SMARTCARD
777 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000778 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000779 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100780 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000781 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
782 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100783 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000784#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100785 default:
786 /* Unknown message. Respond with failure. */
787 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000788 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100789 buffer_put_int(&e->output, 1);
790 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
791 break;
792 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000793}
794
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000796new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000798 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000799
Damien Miller232711f2004-06-15 10:35:30 +1000800 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000801
Damien Miller95def091999-11-25 00:26:21 +1100802 if (fd > max_fd)
803 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Damien Miller95def091999-11-25 00:26:21 +1100805 for (i = 0; i < sockets_alloc; i++)
806 if (sockets[i].type == AUTH_UNUSED) {
807 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100808 buffer_init(&sockets[i].input);
809 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000810 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100812 return;
813 }
814 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000815 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100816 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000817 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100818 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000819 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100820 sockets[old_alloc].fd = fd;
821 buffer_init(&sockets[old_alloc].input);
822 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000823 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000824 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825}
826
Ben Lindstrombba81212001-06-25 05:01:22 +0000827static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000828prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000829{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000830 u_int i, sz;
831 int n = 0;
832
833 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100834 switch (sockets[i].type) {
835 case AUTH_SOCKET:
836 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000837 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100838 break;
839 case AUTH_UNUSED:
840 break;
841 default:
842 fatal("Unknown socket type %d", sockets[i].type);
843 break;
844 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 }
846
847 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000848 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000849 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000850 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000851 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000852 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000853 *fdrp = xmalloc(sz);
854 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000855 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000856 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000857 if (n < *fdl)
858 debug("XXX shrink: %d < %d", n, *fdl);
859 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000860 memset(*fdrp, 0, sz);
861 memset(*fdwp, 0, sz);
862
863 for (i = 0; i < sockets_alloc; i++) {
864 switch (sockets[i].type) {
865 case AUTH_SOCKET:
866 case AUTH_CONNECTION:
867 FD_SET(sockets[i].fd, *fdrp);
868 if (buffer_len(&sockets[i].output) > 0)
869 FD_SET(sockets[i].fd, *fdwp);
870 break;
871 default:
872 break;
873 }
874 }
875 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000876}
877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static void
Damien Miller95def091999-11-25 00:26:21 +1100879after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000880{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000881 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100882 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100883 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000884 int len, sock;
885 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000886 uid_t euid;
887 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000888
Damien Miller95def091999-11-25 00:26:21 +1100889 for (i = 0; i < sockets_alloc; i++)
890 switch (sockets[i].type) {
891 case AUTH_UNUSED:
892 break;
893 case AUTH_SOCKET:
894 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100895 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000896 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100897 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100898 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100899 error("accept from AUTH_SOCKET: %s",
900 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100901 break;
902 }
Damien Miller538f1812002-09-12 09:51:10 +1000903 if (getpeereid(sock, &euid, &egid) < 0) {
904 error("getpeereid %d failed: %s",
905 sock, strerror(errno));
906 close(sock);
907 break;
908 }
Damien Milleraf9de382002-10-03 11:54:35 +1000909 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000910 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000911 "peer euid %u != uid %u",
912 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000913 close(sock);
914 break;
915 }
Damien Miller95def091999-11-25 00:26:21 +1100916 new_socket(AUTH_CONNECTION, sock);
917 }
918 break;
919 case AUTH_CONNECTION:
920 if (buffer_len(&sockets[i].output) > 0 &&
921 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000922 do {
923 len = write(sockets[i].fd,
924 buffer_ptr(&sockets[i].output),
925 buffer_len(&sockets[i].output));
926 if (len == -1 && (errno == EAGAIN ||
927 errno == EINTR))
928 continue;
929 break;
930 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100931 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000932 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100933 break;
934 }
935 buffer_consume(&sockets[i].output, len);
936 }
937 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000938 do {
939 len = read(sockets[i].fd, buf, sizeof(buf));
940 if (len == -1 && (errno == EAGAIN ||
941 errno == EINTR))
942 continue;
943 break;
944 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100945 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000946 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100947 break;
948 }
949 buffer_append(&sockets[i].input, buf, len);
950 process_message(&sockets[i]);
951 }
952 break;
953 default:
954 fatal("Unknown type %d", sockets[i].type);
955 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000956}
957
Ben Lindstrombba81212001-06-25 05:01:22 +0000958static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000959cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000960{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000961 if (socket_name[0])
962 unlink(socket_name);
963 if (socket_dir[0])
964 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000965}
966
Darren Tucker3e33cec2003-10-02 16:12:36 +1000967void
Damien Miller792c5111999-10-29 09:47:09 +1000968cleanup_exit(int i)
969{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000970 cleanup_socket();
971 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000972}
973
Damien Miller1c13bd82006-03-26 14:28:14 +1100974/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000976cleanup_handler(int sig)
977{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000978 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000979 _exit(2);
980}
981
Damien Miller1c13bd82006-03-26 14:28:14 +1100982/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000983static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000984check_parent_exists(int sig)
985{
986 int save_errno = errno;
987
988 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
989 /* printf("Parent has died - Authentication agent exiting.\n"); */
990 cleanup_handler(sig); /* safe */
991 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000992 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000993 alarm(10);
994 errno = save_errno;
995}
996
997static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000998usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000999{
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001000 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001001 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001002 fprintf(stderr, "Options:\n");
1003 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1004 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1005 fprintf(stderr, " -k Kill the current agent.\n");
1006 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001007 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001008 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001009 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001010}
1011
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001012int
1013main(int ac, char **av)
1014{
Damien Miller6c711792003-01-24 11:36:23 +11001015 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001016 int sock, fd, ch;
1017 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001018 char *shell, *format, *pidstr, *agentsocket = NULL;
1019 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001020 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001021#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001022 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001023#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001024 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001025 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001026 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001027 pid_t pid;
1028 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001029
Darren Tuckerce321d82005-10-03 18:11:24 +10001030 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1031 sanitise_stdfd();
1032
Damien Miller6cffb9a2002-09-04 16:20:26 +10001033 /* drop */
1034 setegid(getgid());
1035 setgid(getgid());
1036
Damien Miller6c4914a2004-03-03 11:08:59 +11001037#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1038 /* Disable ptrace on Linux without sgid bit */
1039 prctl(PR_SET_DUMPABLE, 0);
1040#endif
1041
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001042 SSLeay_add_all_algorithms();
1043
Damien Miller59d3d5b2003-08-22 09:34:41 +10001044 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001045 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001046 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001047
Damien Miller53d81482003-01-22 11:47:19 +11001048 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001049 switch (ch) {
1050 case 'c':
1051 if (s_flag)
1052 usage();
1053 c_flag++;
1054 break;
1055 case 'k':
1056 k_flag++;
1057 break;
1058 case 's':
1059 if (c_flag)
1060 usage();
1061 s_flag++;
1062 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001063 case 'd':
1064 if (d_flag)
1065 usage();
1066 d_flag++;
1067 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001068 case 'a':
1069 agentsocket = optarg;
1070 break;
Damien Miller53d81482003-01-22 11:47:19 +11001071 case 't':
1072 if ((lifetime = convtime(optarg)) == -1) {
1073 fprintf(stderr, "Invalid lifetime\n");
1074 usage();
1075 }
1076 break;
Damien Miller95def091999-11-25 00:26:21 +11001077 default:
1078 usage();
1079 }
Damien Miller792c5111999-10-29 09:47:09 +10001080 }
Damien Miller95def091999-11-25 00:26:21 +11001081 ac -= optind;
1082 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001083
Ben Lindstromd94580c2001-07-04 03:48:02 +00001084 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001085 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001086
Ben Lindstromeecdf232002-04-02 21:03:51 +00001087 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001088 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001089 if (shell != NULL &&
1090 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001091 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001092 }
Damien Miller95def091999-11-25 00:26:21 +11001093 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001094 const char *errstr = NULL;
1095
Damien Miller95def091999-11-25 00:26:21 +11001096 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1097 if (pidstr == NULL) {
1098 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001099 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001100 exit(1);
1101 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001102 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1103 if (errstr) {
1104 fprintf(stderr,
1105 "%s=\"%s\", which is not a good PID: %s\n",
1106 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001107 exit(1);
1108 }
1109 if (kill(pid, SIGTERM) == -1) {
1110 perror("kill");
1111 exit(1);
1112 }
1113 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1114 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1115 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001116 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001117 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001118 }
Damien Miller95def091999-11-25 00:26:21 +11001119 parent_pid = getpid();
1120
Ben Lindstromb7788f32002-06-06 21:46:08 +00001121 if (agentsocket == NULL) {
1122 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001123 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001124 if (mkdtemp(socket_dir) == NULL) {
1125 perror("mkdtemp: private socket dir");
1126 exit(1);
1127 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001128 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1129 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001130 } else {
1131 /* Try to use specified agent socket */
1132 socket_dir[0] = '\0';
1133 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001134 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001135
Damien Miller5428f641999-11-25 11:54:57 +11001136 /*
1137 * Create socket early so it will exist before command gets run from
1138 * the parent.
1139 */
Damien Miller95def091999-11-25 00:26:21 +11001140 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1141 if (sock < 0) {
1142 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001143 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001144 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001145 }
Damien Miller95def091999-11-25 00:26:21 +11001146 memset(&sunaddr, 0, sizeof(sunaddr));
1147 sunaddr.sun_family = AF_UNIX;
1148 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001149 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001150 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001151 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001152 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001153 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001154 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001156 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001157 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001158 perror("listen");
1159 cleanup_exit(1);
1160 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001161
Damien Miller5428f641999-11-25 11:54:57 +11001162 /*
1163 * Fork, and have the parent execute the command, if any, or present
1164 * the socket data. The child continues as the authentication agent.
1165 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001166 if (d_flag) {
1167 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1168 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1169 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1170 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001171 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001172 goto skip;
1173 }
Damien Miller95def091999-11-25 00:26:21 +11001174 pid = fork();
1175 if (pid == -1) {
1176 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001177 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001178 }
1179 if (pid != 0) { /* Parent - execute the given command. */
1180 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001181 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001182 if (ac == 0) {
1183 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1184 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001185 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001186 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001187 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001188 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001189 exit(0);
1190 }
Damien Millere4340be2000-09-16 13:29:08 +11001191 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1192 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1193 perror("setenv");
1194 exit(1);
1195 }
Damien Miller95def091999-11-25 00:26:21 +11001196 execvp(av[0], av);
1197 perror(av[0]);
1198 exit(1);
1199 }
Damien Millerc653b892002-02-08 22:05:41 +11001200 /* child */
1201 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001202
1203 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001204 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001205 cleanup_exit(1);
1206 }
1207
1208 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001209 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1210 /* XXX might close listen socket */
1211 (void)dup2(fd, STDIN_FILENO);
1212 (void)dup2(fd, STDOUT_FILENO);
1213 (void)dup2(fd, STDERR_FILENO);
1214 if (fd > 2)
1215 close(fd);
1216 }
Damien Miller95def091999-11-25 00:26:21 +11001217
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001218#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001219 /* deny core dumps, since memory contains unencrypted private keys */
1220 rlim.rlim_cur = rlim.rlim_max = 0;
1221 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001222 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001223 cleanup_exit(1);
1224 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001225#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001226
1227skip:
Damien Miller95def091999-11-25 00:26:21 +11001228 new_socket(AUTH_SOCKET, sock);
1229 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001230 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001231 alarm(10);
1232 }
Damien Millerad833b32000-08-23 10:46:23 +10001233 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001234 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001235 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001236 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001237 signal(SIGHUP, cleanup_handler);
1238 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001239 nalloc = 0;
1240
Damien Miller95def091999-11-25 00:26:21 +11001241 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001242 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001243 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001244 if (errno == EINTR)
1245 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001246 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001247 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001248 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001249 }
1250 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251}