blob: ed48c56d4c682e2cecf992a3bda7c59afcb25476 [file] [log] [blame]
Damien Miller8dbffe72006-08-05 11:02:17 +10001/* $OpenBSD: ssh-agent.c,v 1.148 2006/07/26 02:35:17 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 Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <openssl/evp.h>
53#include <openssl/md5.h>
54
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110057#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110058# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110059#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110060#include <signal.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100061#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100062#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100063#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110064
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "ssh.h"
66#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "buffer.h"
68#include "bufaux.h"
69#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100070#include "key.h"
71#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110072#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000073#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110074#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Ben Lindstrom3f471632001-07-04 03:53:15 +000076#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000077#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000078#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000079
Damien Miller6c4914a2004-03-03 11:08:59 +110080#if defined(HAVE_SYS_PRCTL_H)
81#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
82#endif
83
Ben Lindstrom65366a82001-12-06 16:32:47 +000084typedef enum {
85 AUTH_UNUSED,
86 AUTH_SOCKET,
87 AUTH_CONNECTION
88} sock_type;
89
Damien Miller95def091999-11-25 00:26:21 +110090typedef struct {
91 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000092 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110093 Buffer input;
94 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000095 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096} SocketEntry;
97
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099SocketEntry *sockets = NULL;
100
Damien Miller1a534ae2002-01-22 23:26:13 +1100101typedef struct identity {
102 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000103 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100104 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000105 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100106 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107} Identity;
108
Damien Millerad833b32000-08-23 10:46:23 +1000109typedef struct {
110 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100111 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000112} Idtab;
113
114/* private key table, one per protocol version */
115Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
117int max_fd = 0;
118
119/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000120pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100123char socket_name[MAXPATHLEN];
124char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Ben Lindstrom2f717042002-06-06 21:52:03 +0000126/* locking */
127int locked = 0;
128char *lock_passwd = NULL;
129
Damien Miller95def091999-11-25 00:26:21 +1100130extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100131
Damien Miller53d81482003-01-22 11:47:19 +1100132/* Default lifetime (0 == forever) */
133static int lifetime = 0;
134
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void
Damien Miller58f34862002-09-04 16:31:21 +1000136close_socket(SocketEntry *e)
137{
Damien Miller58f34862002-09-04 16:31:21 +1000138 close(e->fd);
139 e->fd = -1;
140 e->type = AUTH_UNUSED;
141 buffer_free(&e->input);
142 buffer_free(&e->output);
143 buffer_free(&e->request);
144}
145
146static void
Damien Millerad833b32000-08-23 10:46:23 +1000147idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148{
Damien Millerad833b32000-08-23 10:46:23 +1000149 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000150
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000151 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100152 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000153 idtable[i].nentries = 0;
154 }
155}
156
157/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000158static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000159idtab_lookup(int version)
160{
161 if (version < 1 || version > 2)
162 fatal("internal error, bad protocol version %d", version);
163 return &idtable[version];
164}
165
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000166static void
167free_identity(Identity *id)
168{
169 key_free(id->key);
170 xfree(id->comment);
171 xfree(id);
172}
173
Damien Millerad833b32000-08-23 10:46:23 +1000174/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100175static Identity *
176lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000177{
Damien Miller1a534ae2002-01-22 23:26:13 +1100178 Identity *id;
179
Damien Millerad833b32000-08-23 10:46:23 +1000180 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100181 TAILQ_FOREACH(id, &tab->idlist, next) {
182 if (key_equal(key, id->key))
183 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000184 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 return (NULL);
186}
187
Damien Miller6c711792003-01-24 11:36:23 +1100188/* Check confirmation of keysign request */
189static int
190confirm_key(Identity *id)
191{
Darren Tuckerce327b62004-11-05 20:38:03 +1100192 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100193 int ret = -1;
194
195 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100196 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
197 id->comment, p))
198 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100199 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100200
Damien Miller6c711792003-01-24 11:36:23 +1100201 return (ret);
202}
203
Damien Millerad833b32000-08-23 10:46:23 +1000204/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static void
Damien Millerad833b32000-08-23 10:46:23 +1000206process_request_identities(SocketEntry *e, int version)
207{
208 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100209 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000210 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000211
Damien Miller95def091999-11-25 00:26:21 +1100212 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000213 buffer_put_char(&msg, (version == 1) ?
214 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
215 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100216 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100217 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000218 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
219 buffer_put_bignum(&msg, id->key->rsa->e);
220 buffer_put_bignum(&msg, id->key->rsa->n);
221 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000222 u_char *blob;
223 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100224 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000225 buffer_put_string(&msg, blob, blen);
226 xfree(blob);
227 }
228 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100229 }
230 buffer_put_int(&e->output, buffer_len(&msg));
231 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
232 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233}
234
Damien Millerad833b32000-08-23 10:46:23 +1000235/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static void
Damien Millerad833b32000-08-23 10:46:23 +1000237process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000239 u_char buf[32], mdbuf[16], session_id[16];
240 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000241 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000242 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000243 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100244 Buffer msg;
245 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000246 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000247
Damien Miller95def091999-11-25 00:26:21 +1100248 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100249 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100250 if ((challenge = BN_new()) == NULL)
251 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000252
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000253 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000254 buffer_get_bignum(&e->request, key->rsa->e);
255 buffer_get_bignum(&e->request, key->rsa->n);
256 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Millerad833b32000-08-23 10:46:23 +1000258 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000259 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000260 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000261 buffer_get(&e->request, session_id, 16);
262 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000263 if (response_type != 1)
264 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265
Damien Miller1a534ae2002-01-22 23:26:13 +1100266 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100267 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100268 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000269 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100270 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
271 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100272
Damien Millerad833b32000-08-23 10:46:23 +1000273 /* The response is MD5 of decrypted challenge plus session id. */
274 len = BN_num_bytes(challenge);
275 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000276 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000277 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100278 }
Damien Millerad833b32000-08-23 10:46:23 +1000279 memset(buf, 0, 32);
280 BN_bn2bin(challenge, buf + 32 - len);
281 MD5_Init(&md);
282 MD5_Update(&md, buf, 32);
283 MD5_Update(&md, session_id, 16);
284 MD5_Final(mdbuf, &md);
285
286 /* Send the response. */
287 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
288 for (i = 0; i < 16; i++)
289 buffer_put_char(&msg, mdbuf[i]);
290 goto send;
291 }
292
293failure:
294 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100295 buffer_put_char(&msg, SSH_AGENT_FAILURE);
296send:
297 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000298 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
299 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100300 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000301 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302}
303
Damien Millerad833b32000-08-23 10:46:23 +1000304/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000305static void
Damien Millerad833b32000-08-23 10:46:23 +1000306process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000307{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000308 u_char *blob, *data, *signature = NULL;
309 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000310 extern int datafellows;
311 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000312 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000313 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000314
315 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000316
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000317 blob = buffer_get_string(&e->request, &blen);
318 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100319
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000320 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100321 if (flags & SSH_AGENT_OLD_SIGNATURE)
322 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000323
Damien Miller0bc1bd82000-11-13 22:57:25 +1100324 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000325 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100326 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100327 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100328 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000329 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000330 }
Damien Millerad833b32000-08-23 10:46:23 +1000331 buffer_init(&msg);
332 if (ok == 0) {
333 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
334 buffer_put_string(&msg, signature, slen);
335 } else {
336 buffer_put_char(&msg, SSH_AGENT_FAILURE);
337 }
338 buffer_put_int(&e->output, buffer_len(&msg));
339 buffer_append(&e->output, buffer_ptr(&msg),
340 buffer_len(&msg));
341 buffer_free(&msg);
342 xfree(data);
343 xfree(blob);
344 if (signature != NULL)
345 xfree(signature);
346}
347
348/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000349static void
Damien Millerad833b32000-08-23 10:46:23 +1000350process_remove_identity(SocketEntry *e, int version)
351{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000352 u_int blen, bits;
353 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100354 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000355 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100356
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000357 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000358 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000360 bits = buffer_get_int(&e->request);
361 buffer_get_bignum(&e->request, key->rsa->e);
362 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Millerad833b32000-08-23 10:46:23 +1000364 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000365 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000366 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000367 break;
368 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000369 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100370 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000371 xfree(blob);
372 break;
373 }
374 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100375 Identity *id = lookup_identity(key, version);
376 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100377 /*
378 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100379 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000380 * the array, we actually free the key there and move
381 * all the entries between the empty slot and the end
382 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100383 */
Damien Millerad833b32000-08-23 10:46:23 +1000384 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100385 if (tab->nentries < 1)
386 fatal("process_remove_identity: "
387 "internal error: tab->nentries %d",
388 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100389 TAILQ_REMOVE(&tab->idlist, id, next);
390 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000391 tab->nentries--;
392 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100393 }
Damien Millerad833b32000-08-23 10:46:23 +1000394 key_free(key);
395 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000397 buffer_put_char(&e->output,
398 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399}
400
Ben Lindstrombba81212001-06-25 05:01:22 +0000401static void
Damien Millerad833b32000-08-23 10:46:23 +1000402process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403{
Damien Millerad833b32000-08-23 10:46:23 +1000404 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100405 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406
Damien Miller95def091999-11-25 00:26:21 +1100407 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100408 for (id = TAILQ_FIRST(&tab->idlist); id;
409 id = TAILQ_FIRST(&tab->idlist)) {
410 TAILQ_REMOVE(&tab->idlist, id, next);
411 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100412 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Damien Miller95def091999-11-25 00:26:21 +1100414 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000415 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
417 /* Send success. */
418 buffer_put_int(&e->output, 1);
419 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100420}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000421
Ben Lindstrombba81212001-06-25 05:01:22 +0000422static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000423reaper(void)
424{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000425 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000426 Identity *id, *nxt;
427 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000428 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000429
430 for (version = 1; version < 3; version++) {
431 tab = idtab_lookup(version);
432 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
433 nxt = TAILQ_NEXT(id, next);
434 if (id->death != 0 && now >= id->death) {
435 TAILQ_REMOVE(&tab->idlist, id, next);
436 free_identity(id);
437 tab->nentries--;
438 }
439 }
440 }
441}
442
443static void
Damien Millerad833b32000-08-23 10:46:23 +1000444process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100445{
Damien Millerad833b32000-08-23 10:46:23 +1000446 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100447 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000448 char *type_name, *comment;
449 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100450
Damien Millerad833b32000-08-23 10:46:23 +1000451 switch (version) {
452 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000454 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000455 buffer_get_bignum(&e->request, k->rsa->n);
456 buffer_get_bignum(&e->request, k->rsa->e);
457 buffer_get_bignum(&e->request, k->rsa->d);
458 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100459
Damien Millerad833b32000-08-23 10:46:23 +1000460 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000461 buffer_get_bignum(&e->request, k->rsa->q); /* p */
462 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100463
Damien Millerad833b32000-08-23 10:46:23 +1000464 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000465 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000466 break;
467 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000468 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000469 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100470 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000471 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 case KEY_DSA:
473 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000474 buffer_get_bignum2(&e->request, k->dsa->p);
475 buffer_get_bignum2(&e->request, k->dsa->q);
476 buffer_get_bignum2(&e->request, k->dsa->g);
477 buffer_get_bignum2(&e->request, k->dsa->pub_key);
478 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479 break;
480 case KEY_RSA:
481 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000482 buffer_get_bignum2(&e->request, k->rsa->n);
483 buffer_get_bignum2(&e->request, k->rsa->e);
484 buffer_get_bignum2(&e->request, k->rsa->d);
485 buffer_get_bignum2(&e->request, k->rsa->iqmp);
486 buffer_get_bignum2(&e->request, k->rsa->p);
487 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488
489 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000490 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100491 break;
492 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000493 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000494 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100495 }
Damien Millerad833b32000-08-23 10:46:23 +1000496 break;
497 }
Damien Millerc51d0732003-03-15 11:37:09 +1100498 /* enable blinding */
499 switch (k->type) {
500 case KEY_RSA:
501 case KEY_RSA1:
502 if (RSA_blinding_on(k->rsa, NULL) != 1) {
503 error("process_add_identity: RSA_blinding_on failed");
504 key_free(k);
505 goto send;
506 }
507 break;
508 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000509 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000510 if (k == NULL) {
511 xfree(comment);
512 goto send;
513 }
514 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000515 while (buffer_len(&e->request)) {
516 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000517 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000518 death = time(NULL) + buffer_get_int(&e->request);
519 break;
Damien Miller6c711792003-01-24 11:36:23 +1100520 case SSH_AGENT_CONSTRAIN_CONFIRM:
521 confirm = 1;
522 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000523 default:
524 break;
525 }
526 }
Damien Miller53d81482003-01-22 11:47:19 +1100527 if (lifetime && !death)
528 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000529 if (lookup_identity(k, version) == NULL) {
530 Identity *id = xmalloc(sizeof(Identity));
531 id->key = k;
532 id->comment = comment;
533 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100534 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000535 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
536 /* Increment the number of identities. */
537 tab->nentries++;
538 } else {
539 key_free(k);
540 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000541 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000542send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000543 buffer_put_int(&e->output, 1);
544 buffer_put_char(&e->output,
545 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
546}
547
Ben Lindstrom2f717042002-06-06 21:52:03 +0000548/* XXX todo: encrypt sensitive data with passphrase */
549static void
550process_lock_agent(SocketEntry *e, int lock)
551{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000552 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000553 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000554
555 passwd = buffer_get_string(&e->request, NULL);
556 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
557 locked = 0;
558 memset(lock_passwd, 0, strlen(lock_passwd));
559 xfree(lock_passwd);
560 lock_passwd = NULL;
561 success = 1;
562 } else if (!locked && lock) {
563 locked = 1;
564 lock_passwd = xstrdup(passwd);
565 success = 1;
566 }
567 memset(passwd, 0, strlen(passwd));
568 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000569
Ben Lindstrom2f717042002-06-06 21:52:03 +0000570 buffer_put_int(&e->output, 1);
571 buffer_put_char(&e->output,
572 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000573}
574
575static void
576no_identities(SocketEntry *e, u_int type)
577{
578 Buffer msg;
579
580 buffer_init(&msg);
581 buffer_put_char(&msg,
582 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
583 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
584 buffer_put_int(&msg, 0);
585 buffer_put_int(&e->output, buffer_len(&msg));
586 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
587 buffer_free(&msg);
588}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000589
590#ifdef SMARTCARD
591static void
592process_add_smartcard_key (SocketEntry *e)
593{
Ben Lindstromba72d302002-03-22 03:51:06 +0000594 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000595 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000596 Key **keys, *k;
597 Identity *id;
598 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100599
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000600 sc_reader_id = buffer_get_string(&e->request, NULL);
601 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000602
603 while (buffer_len(&e->request)) {
604 switch (buffer_get_char(&e->request)) {
605 case SSH_AGENT_CONSTRAIN_LIFETIME:
606 death = time(NULL) + buffer_get_int(&e->request);
607 break;
608 case SSH_AGENT_CONSTRAIN_CONFIRM:
609 confirm = 1;
610 break;
611 default:
612 break;
613 }
614 }
615 if (lifetime && !death)
616 death = time(NULL) + lifetime;
617
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000618 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000619 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000620 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000621
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000622 if (keys == NULL || keys[0] == NULL) {
623 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000624 goto send;
625 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000626 for (i = 0; keys[i] != NULL; i++) {
627 k = keys[i];
628 version = k->type == KEY_RSA1 ? 1 : 2;
629 tab = idtab_lookup(version);
630 if (lookup_identity(k, version) == NULL) {
631 id = xmalloc(sizeof(Identity));
632 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000633 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000634 id->death = death;
635 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000636 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
637 tab->nentries++;
638 success = 1;
639 } else {
640 key_free(k);
641 }
642 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000643 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000645send:
646 buffer_put_int(&e->output, 1);
647 buffer_put_char(&e->output,
648 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
649}
650
651static void
652process_remove_smartcard_key(SocketEntry *e)
653{
Ben Lindstromba72d302002-03-22 03:51:06 +0000654 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000655 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000656 Key **keys, *k = NULL;
657 Identity *id;
658 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000659
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000660 sc_reader_id = buffer_get_string(&e->request, NULL);
661 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000662 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000663 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000664 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000665
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000666 if (keys == NULL || keys[0] == NULL) {
667 error("sc_get_keys failed");
668 goto send;
669 }
670 for (i = 0; keys[i] != NULL; i++) {
671 k = keys[i];
672 version = k->type == KEY_RSA1 ? 1 : 2;
673 if ((id = lookup_identity(k, version)) != NULL) {
674 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000675 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000676 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100677 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000678 success = 1;
679 }
680 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000681 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000682 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000683 xfree(keys);
684send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000685 buffer_put_int(&e->output, 1);
686 buffer_put_char(&e->output,
687 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
688}
Ben Lindstromffce1472001-08-06 21:57:31 +0000689#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000690
Damien Millerad833b32000-08-23 10:46:23 +1000691/* dispatch incoming messages */
692
Ben Lindstrombba81212001-06-25 05:01:22 +0000693static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000694process_message(SocketEntry *e)
695{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000696 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000697 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000698
699 /* kill dead keys */
700 reaper();
701
Damien Miller95def091999-11-25 00:26:21 +1100702 if (buffer_len(&e->input) < 5)
703 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100704 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100705 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100706 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000707 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100708 return;
709 }
710 if (buffer_len(&e->input) < msg_len + 4)
711 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000712
713 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100714 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000715 buffer_clear(&e->request);
716 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
717 buffer_consume(&e->input, msg_len);
718 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000719
Ben Lindstrom2f717042002-06-06 21:52:03 +0000720 /* check wheter agent is locked */
721 if (locked && type != SSH_AGENTC_UNLOCK) {
722 buffer_clear(&e->request);
723 switch (type) {
724 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
725 case SSH2_AGENTC_REQUEST_IDENTITIES:
726 /* send empty lists */
727 no_identities(e, type);
728 break;
729 default:
730 /* send a fail message for all other request types */
731 buffer_put_int(&e->output, 1);
732 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
733 }
734 return;
735 }
736
Ben Lindstrom3f471632001-07-04 03:53:15 +0000737 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100738 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000739 case SSH_AGENTC_LOCK:
740 case SSH_AGENTC_UNLOCK:
741 process_lock_agent(e, type == SSH_AGENTC_LOCK);
742 break;
Damien Millerad833b32000-08-23 10:46:23 +1000743 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100744 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000745 process_authentication_challenge1(e);
746 break;
747 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
748 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100749 break;
750 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000751 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000752 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100753 break;
754 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000755 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100756 break;
757 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000758 process_remove_all_identities(e, 1);
759 break;
760 /* ssh2 */
761 case SSH2_AGENTC_SIGN_REQUEST:
762 process_sign_request2(e);
763 break;
764 case SSH2_AGENTC_REQUEST_IDENTITIES:
765 process_request_identities(e, 2);
766 break;
767 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000768 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000769 process_add_identity(e, 2);
770 break;
771 case SSH2_AGENTC_REMOVE_IDENTITY:
772 process_remove_identity(e, 2);
773 break;
774 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
775 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100776 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000777#ifdef SMARTCARD
778 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000779 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000780 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100781 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000782 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
783 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100784 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000785#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100786 default:
787 /* Unknown message. Respond with failure. */
788 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000789 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100790 buffer_put_int(&e->output, 1);
791 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
792 break;
793 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794}
795
Ben Lindstrombba81212001-06-25 05:01:22 +0000796static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000797new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000798{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000799 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000800
Damien Miller232711f2004-06-15 10:35:30 +1000801 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000802
Damien Miller95def091999-11-25 00:26:21 +1100803 if (fd > max_fd)
804 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000805
Damien Miller95def091999-11-25 00:26:21 +1100806 for (i = 0; i < sockets_alloc; i++)
807 if (sockets[i].type == AUTH_UNUSED) {
808 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100809 buffer_init(&sockets[i].input);
810 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000811 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000812 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100813 return;
814 }
815 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000816 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100817 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000818 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100819 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000820 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100821 sockets[old_alloc].fd = fd;
822 buffer_init(&sockets[old_alloc].input);
823 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000824 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000825 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000826}
827
Ben Lindstrombba81212001-06-25 05:01:22 +0000828static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000829prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000830{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000831 u_int i, sz;
832 int n = 0;
833
834 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100835 switch (sockets[i].type) {
836 case AUTH_SOCKET:
837 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000838 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100839 break;
840 case AUTH_UNUSED:
841 break;
842 default:
843 fatal("Unknown socket type %d", sockets[i].type);
844 break;
845 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 }
847
848 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000849 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000851 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000853 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000854 *fdrp = xmalloc(sz);
855 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000856 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000857 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000858 if (n < *fdl)
859 debug("XXX shrink: %d < %d", n, *fdl);
860 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000861 memset(*fdrp, 0, sz);
862 memset(*fdwp, 0, sz);
863
864 for (i = 0; i < sockets_alloc; i++) {
865 switch (sockets[i].type) {
866 case AUTH_SOCKET:
867 case AUTH_CONNECTION:
868 FD_SET(sockets[i].fd, *fdrp);
869 if (buffer_len(&sockets[i].output) > 0)
870 FD_SET(sockets[i].fd, *fdwp);
871 break;
872 default:
873 break;
874 }
875 }
876 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000877}
878
Ben Lindstrombba81212001-06-25 05:01:22 +0000879static void
Damien Miller95def091999-11-25 00:26:21 +1100880after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000882 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100883 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100884 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000885 int len, sock;
886 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000887 uid_t euid;
888 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000889
Damien Miller95def091999-11-25 00:26:21 +1100890 for (i = 0; i < sockets_alloc; i++)
891 switch (sockets[i].type) {
892 case AUTH_UNUSED:
893 break;
894 case AUTH_SOCKET:
895 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100896 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000897 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100898 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100899 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100900 error("accept from AUTH_SOCKET: %s",
901 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100902 break;
903 }
Damien Miller538f1812002-09-12 09:51:10 +1000904 if (getpeereid(sock, &euid, &egid) < 0) {
905 error("getpeereid %d failed: %s",
906 sock, strerror(errno));
907 close(sock);
908 break;
909 }
Damien Milleraf9de382002-10-03 11:54:35 +1000910 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000911 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000912 "peer euid %u != uid %u",
913 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000914 close(sock);
915 break;
916 }
Damien Miller95def091999-11-25 00:26:21 +1100917 new_socket(AUTH_CONNECTION, sock);
918 }
919 break;
920 case AUTH_CONNECTION:
921 if (buffer_len(&sockets[i].output) > 0 &&
922 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000923 do {
924 len = write(sockets[i].fd,
925 buffer_ptr(&sockets[i].output),
926 buffer_len(&sockets[i].output));
927 if (len == -1 && (errno == EAGAIN ||
928 errno == EINTR))
929 continue;
930 break;
931 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100932 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000933 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100934 break;
935 }
936 buffer_consume(&sockets[i].output, len);
937 }
938 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000939 do {
940 len = read(sockets[i].fd, buf, sizeof(buf));
941 if (len == -1 && (errno == EAGAIN ||
942 errno == EINTR))
943 continue;
944 break;
945 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100946 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000947 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100948 break;
949 }
950 buffer_append(&sockets[i].input, buf, len);
951 process_message(&sockets[i]);
952 }
953 break;
954 default:
955 fatal("Unknown type %d", sockets[i].type);
956 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000957}
958
Ben Lindstrombba81212001-06-25 05:01:22 +0000959static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000960cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000961{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000962 if (socket_name[0])
963 unlink(socket_name);
964 if (socket_dir[0])
965 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000966}
967
Darren Tucker3e33cec2003-10-02 16:12:36 +1000968void
Damien Miller792c5111999-10-29 09:47:09 +1000969cleanup_exit(int i)
970{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000971 cleanup_socket();
972 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000973}
974
Damien Miller1c13bd82006-03-26 14:28:14 +1100975/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000976static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000977cleanup_handler(int sig)
978{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000979 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000980 _exit(2);
981}
982
Damien Miller1c13bd82006-03-26 14:28:14 +1100983/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000984static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000985check_parent_exists(int sig)
986{
987 int save_errno = errno;
988
989 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
990 /* printf("Parent has died - Authentication agent exiting.\n"); */
991 cleanup_handler(sig); /* safe */
992 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000993 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000994 alarm(10);
995 errno = save_errno;
996}
997
998static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000999usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001000{
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001001 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001002 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001003 fprintf(stderr, "Options:\n");
1004 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1005 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1006 fprintf(stderr, " -k Kill the current agent.\n");
1007 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001008 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001009 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001010 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001011}
1012
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013int
1014main(int ac, char **av)
1015{
Damien Miller6c711792003-01-24 11:36:23 +11001016 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001017 int sock, fd, ch;
1018 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001019 char *shell, *format, *pidstr, *agentsocket = NULL;
1020 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001021 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001022#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001023 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001024#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001025 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001026 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001027 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001028 pid_t pid;
1029 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001030
Darren Tuckerce321d82005-10-03 18:11:24 +10001031 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1032 sanitise_stdfd();
1033
Damien Miller6cffb9a2002-09-04 16:20:26 +10001034 /* drop */
1035 setegid(getgid());
1036 setgid(getgid());
1037
Damien Miller6c4914a2004-03-03 11:08:59 +11001038#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1039 /* Disable ptrace on Linux without sgid bit */
1040 prctl(PR_SET_DUMPABLE, 0);
1041#endif
1042
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001043 SSLeay_add_all_algorithms();
1044
Damien Miller59d3d5b2003-08-22 09:34:41 +10001045 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001046 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001047 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001048
Damien Miller53d81482003-01-22 11:47:19 +11001049 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001050 switch (ch) {
1051 case 'c':
1052 if (s_flag)
1053 usage();
1054 c_flag++;
1055 break;
1056 case 'k':
1057 k_flag++;
1058 break;
1059 case 's':
1060 if (c_flag)
1061 usage();
1062 s_flag++;
1063 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001064 case 'd':
1065 if (d_flag)
1066 usage();
1067 d_flag++;
1068 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001069 case 'a':
1070 agentsocket = optarg;
1071 break;
Damien Miller53d81482003-01-22 11:47:19 +11001072 case 't':
1073 if ((lifetime = convtime(optarg)) == -1) {
1074 fprintf(stderr, "Invalid lifetime\n");
1075 usage();
1076 }
1077 break;
Damien Miller95def091999-11-25 00:26:21 +11001078 default:
1079 usage();
1080 }
Damien Miller792c5111999-10-29 09:47:09 +10001081 }
Damien Miller95def091999-11-25 00:26:21 +11001082 ac -= optind;
1083 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001084
Ben Lindstromd94580c2001-07-04 03:48:02 +00001085 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001086 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001087
Ben Lindstromeecdf232002-04-02 21:03:51 +00001088 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001089 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001090 if (shell != NULL &&
1091 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001092 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001093 }
Damien Miller95def091999-11-25 00:26:21 +11001094 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001095 const char *errstr = NULL;
1096
Damien Miller95def091999-11-25 00:26:21 +11001097 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1098 if (pidstr == NULL) {
1099 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001100 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001101 exit(1);
1102 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001103 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1104 if (errstr) {
1105 fprintf(stderr,
1106 "%s=\"%s\", which is not a good PID: %s\n",
1107 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001108 exit(1);
1109 }
1110 if (kill(pid, SIGTERM) == -1) {
1111 perror("kill");
1112 exit(1);
1113 }
1114 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1115 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1116 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001117 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001118 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001119 }
Damien Miller95def091999-11-25 00:26:21 +11001120 parent_pid = getpid();
1121
Ben Lindstromb7788f32002-06-06 21:46:08 +00001122 if (agentsocket == NULL) {
1123 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001124 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001125 if (mkdtemp(socket_dir) == NULL) {
1126 perror("mkdtemp: private socket dir");
1127 exit(1);
1128 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001129 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1130 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001131 } else {
1132 /* Try to use specified agent socket */
1133 socket_dir[0] = '\0';
1134 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001135 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
Damien Miller5428f641999-11-25 11:54:57 +11001137 /*
1138 * Create socket early so it will exist before command gets run from
1139 * the parent.
1140 */
Damien Miller95def091999-11-25 00:26:21 +11001141 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1142 if (sock < 0) {
1143 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001144 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001145 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001146 }
Damien Miller95def091999-11-25 00:26:21 +11001147 memset(&sunaddr, 0, sizeof(sunaddr));
1148 sunaddr.sun_family = AF_UNIX;
1149 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001150 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001151 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001152 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001153 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001154 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001155 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001157 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001158 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001159 perror("listen");
1160 cleanup_exit(1);
1161 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001162
Damien Miller5428f641999-11-25 11:54:57 +11001163 /*
1164 * Fork, and have the parent execute the command, if any, or present
1165 * the socket data. The child continues as the authentication agent.
1166 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001167 if (d_flag) {
1168 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1169 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1170 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1171 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001172 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001173 goto skip;
1174 }
Damien Miller95def091999-11-25 00:26:21 +11001175 pid = fork();
1176 if (pid == -1) {
1177 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001178 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001179 }
1180 if (pid != 0) { /* Parent - execute the given command. */
1181 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001182 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001183 if (ac == 0) {
1184 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1185 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001186 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001187 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001188 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001189 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001190 exit(0);
1191 }
Damien Millere4340be2000-09-16 13:29:08 +11001192 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1193 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1194 perror("setenv");
1195 exit(1);
1196 }
Damien Miller95def091999-11-25 00:26:21 +11001197 execvp(av[0], av);
1198 perror(av[0]);
1199 exit(1);
1200 }
Damien Millerc653b892002-02-08 22:05:41 +11001201 /* child */
1202 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001203
1204 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001205 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001206 cleanup_exit(1);
1207 }
1208
1209 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001210 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1211 /* XXX might close listen socket */
1212 (void)dup2(fd, STDIN_FILENO);
1213 (void)dup2(fd, STDOUT_FILENO);
1214 (void)dup2(fd, STDERR_FILENO);
1215 if (fd > 2)
1216 close(fd);
1217 }
Damien Miller95def091999-11-25 00:26:21 +11001218
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001219#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001220 /* deny core dumps, since memory contains unencrypted private keys */
1221 rlim.rlim_cur = rlim.rlim_max = 0;
1222 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001223 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001224 cleanup_exit(1);
1225 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001226#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001227
1228skip:
Damien Miller95def091999-11-25 00:26:21 +11001229 new_socket(AUTH_SOCKET, sock);
1230 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001231 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001232 alarm(10);
1233 }
Damien Millerad833b32000-08-23 10:46:23 +10001234 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001235 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001236 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001237 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001238 signal(SIGHUP, cleanup_handler);
1239 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001240 nalloc = 0;
1241
Damien Miller95def091999-11-25 00:26:21 +11001242 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001243 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001244 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001245 if (errno == EINTR)
1246 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001247 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001248 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001249 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001250 }
1251 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001252}