blob: 478f8d1ee3f73153ae20df244fb741b1faa3a32e [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: ssh-agent.c,v 1.149 2006/07/26 13:57: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 Millere7a1e5c2006-08-05 11:34:19 +100061#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100062#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100063#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100064#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110065
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "ssh.h"
67#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#include "buffer.h"
69#include "bufaux.h"
70#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100071#include "key.h"
72#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110073#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000074#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110075#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Ben Lindstrom3f471632001-07-04 03:53:15 +000077#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000078#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000079#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000080
Damien Miller6c4914a2004-03-03 11:08:59 +110081#if defined(HAVE_SYS_PRCTL_H)
82#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
83#endif
84
Ben Lindstrom65366a82001-12-06 16:32:47 +000085typedef enum {
86 AUTH_UNUSED,
87 AUTH_SOCKET,
88 AUTH_CONNECTION
89} sock_type;
90
Damien Miller95def091999-11-25 00:26:21 +110091typedef struct {
92 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000093 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110094 Buffer input;
95 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000096 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097} SocketEntry;
98
Ben Lindstrom46c16222000-12-22 01:43:59 +000099u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100SocketEntry *sockets = NULL;
101
Damien Miller1a534ae2002-01-22 23:26:13 +1100102typedef struct identity {
103 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000104 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100105 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000106 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100107 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108} Identity;
109
Damien Millerad833b32000-08-23 10:46:23 +1000110typedef struct {
111 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100112 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000113} Idtab;
114
115/* private key table, one per protocol version */
116Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
118int max_fd = 0;
119
120/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000121pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100124char socket_name[MAXPATHLEN];
125char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Ben Lindstrom2f717042002-06-06 21:52:03 +0000127/* locking */
128int locked = 0;
129char *lock_passwd = NULL;
130
Damien Miller95def091999-11-25 00:26:21 +1100131extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100132
Damien Miller53d81482003-01-22 11:47:19 +1100133/* Default lifetime (0 == forever) */
134static int lifetime = 0;
135
Ben Lindstrombba81212001-06-25 05:01:22 +0000136static void
Damien Miller58f34862002-09-04 16:31:21 +1000137close_socket(SocketEntry *e)
138{
Damien Miller58f34862002-09-04 16:31:21 +1000139 close(e->fd);
140 e->fd = -1;
141 e->type = AUTH_UNUSED;
142 buffer_free(&e->input);
143 buffer_free(&e->output);
144 buffer_free(&e->request);
145}
146
147static void
Damien Millerad833b32000-08-23 10:46:23 +1000148idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149{
Damien Millerad833b32000-08-23 10:46:23 +1000150 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000151
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000152 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100153 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000154 idtable[i].nentries = 0;
155 }
156}
157
158/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000160idtab_lookup(int version)
161{
162 if (version < 1 || version > 2)
163 fatal("internal error, bad protocol version %d", version);
164 return &idtable[version];
165}
166
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000167static void
168free_identity(Identity *id)
169{
170 key_free(id->key);
171 xfree(id->comment);
172 xfree(id);
173}
174
Damien Millerad833b32000-08-23 10:46:23 +1000175/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100176static Identity *
177lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000178{
Damien Miller1a534ae2002-01-22 23:26:13 +1100179 Identity *id;
180
Damien Millerad833b32000-08-23 10:46:23 +1000181 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100182 TAILQ_FOREACH(id, &tab->idlist, next) {
183 if (key_equal(key, id->key))
184 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000185 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100186 return (NULL);
187}
188
Damien Miller6c711792003-01-24 11:36:23 +1100189/* Check confirmation of keysign request */
190static int
191confirm_key(Identity *id)
192{
Darren Tuckerce327b62004-11-05 20:38:03 +1100193 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100194 int ret = -1;
195
196 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100197 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
198 id->comment, p))
199 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100200 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100201
Damien Miller6c711792003-01-24 11:36:23 +1100202 return (ret);
203}
204
Damien Millerad833b32000-08-23 10:46:23 +1000205/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000206static void
Damien Millerad833b32000-08-23 10:46:23 +1000207process_request_identities(SocketEntry *e, int version)
208{
209 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100210 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000211 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000212
Damien Miller95def091999-11-25 00:26:21 +1100213 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000214 buffer_put_char(&msg, (version == 1) ?
215 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
216 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100217 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100218 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000219 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
220 buffer_put_bignum(&msg, id->key->rsa->e);
221 buffer_put_bignum(&msg, id->key->rsa->n);
222 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000223 u_char *blob;
224 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100225 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000226 buffer_put_string(&msg, blob, blen);
227 xfree(blob);
228 }
229 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100230 }
231 buffer_put_int(&e->output, buffer_len(&msg));
232 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
233 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234}
235
Damien Millerad833b32000-08-23 10:46:23 +1000236/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static void
Damien Millerad833b32000-08-23 10:46:23 +1000238process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000240 u_char buf[32], mdbuf[16], session_id[16];
241 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000242 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000243 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000244 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100245 Buffer msg;
246 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000247 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100250 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100251 if ((challenge = BN_new()) == NULL)
252 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000253
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000254 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000255 buffer_get_bignum(&e->request, key->rsa->e);
256 buffer_get_bignum(&e->request, key->rsa->n);
257 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000258
Damien Millerad833b32000-08-23 10:46:23 +1000259 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000260 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000261 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000262 buffer_get(&e->request, session_id, 16);
263 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000264 if (response_type != 1)
265 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000266
Damien Miller1a534ae2002-01-22 23:26:13 +1100267 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100268 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100269 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000270 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100271 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
272 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100273
Damien Millerad833b32000-08-23 10:46:23 +1000274 /* The response is MD5 of decrypted challenge plus session id. */
275 len = BN_num_bytes(challenge);
276 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000277 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000278 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100279 }
Damien Millerad833b32000-08-23 10:46:23 +1000280 memset(buf, 0, 32);
281 BN_bn2bin(challenge, buf + 32 - len);
282 MD5_Init(&md);
283 MD5_Update(&md, buf, 32);
284 MD5_Update(&md, session_id, 16);
285 MD5_Final(mdbuf, &md);
286
287 /* Send the response. */
288 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
289 for (i = 0; i < 16; i++)
290 buffer_put_char(&msg, mdbuf[i]);
291 goto send;
292 }
293
294failure:
295 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100296 buffer_put_char(&msg, SSH_AGENT_FAILURE);
297send:
298 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000299 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
300 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100301 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000302 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303}
304
Damien Millerad833b32000-08-23 10:46:23 +1000305/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000306static void
Damien Millerad833b32000-08-23 10:46:23 +1000307process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000308{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000309 u_char *blob, *data, *signature = NULL;
310 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000311 extern int datafellows;
312 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000313 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000314 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000315
316 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000317
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000318 blob = buffer_get_string(&e->request, &blen);
319 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100320
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000321 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100322 if (flags & SSH_AGENT_OLD_SIGNATURE)
323 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000324
Damien Miller0bc1bd82000-11-13 22:57:25 +1100325 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000326 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100327 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100328 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100329 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000330 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000331 }
Damien Millerad833b32000-08-23 10:46:23 +1000332 buffer_init(&msg);
333 if (ok == 0) {
334 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
335 buffer_put_string(&msg, signature, slen);
336 } else {
337 buffer_put_char(&msg, SSH_AGENT_FAILURE);
338 }
339 buffer_put_int(&e->output, buffer_len(&msg));
340 buffer_append(&e->output, buffer_ptr(&msg),
341 buffer_len(&msg));
342 buffer_free(&msg);
343 xfree(data);
344 xfree(blob);
345 if (signature != NULL)
346 xfree(signature);
347}
348
349/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000350static void
Damien Millerad833b32000-08-23 10:46:23 +1000351process_remove_identity(SocketEntry *e, int version)
352{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000353 u_int blen, bits;
354 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100355 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000356 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100357
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000358 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000359 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100360 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000361 bits = buffer_get_int(&e->request);
362 buffer_get_bignum(&e->request, key->rsa->e);
363 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000364
Damien Millerad833b32000-08-23 10:46:23 +1000365 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000366 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000367 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000368 break;
369 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000370 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100371 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000372 xfree(blob);
373 break;
374 }
375 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100376 Identity *id = lookup_identity(key, version);
377 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100378 /*
379 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100380 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000381 * the array, we actually free the key there and move
382 * all the entries between the empty slot and the end
383 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100384 */
Damien Millerad833b32000-08-23 10:46:23 +1000385 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 if (tab->nentries < 1)
387 fatal("process_remove_identity: "
388 "internal error: tab->nentries %d",
389 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100390 TAILQ_REMOVE(&tab->idlist, id, next);
391 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000392 tab->nentries--;
393 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100394 }
Damien Millerad833b32000-08-23 10:46:23 +1000395 key_free(key);
396 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000397 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000398 buffer_put_char(&e->output,
399 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400}
401
Ben Lindstrombba81212001-06-25 05:01:22 +0000402static void
Damien Millerad833b32000-08-23 10:46:23 +1000403process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404{
Damien Millerad833b32000-08-23 10:46:23 +1000405 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100406 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Damien Miller95def091999-11-25 00:26:21 +1100408 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100409 for (id = TAILQ_FIRST(&tab->idlist); id;
410 id = TAILQ_FIRST(&tab->idlist)) {
411 TAILQ_REMOVE(&tab->idlist, id, next);
412 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100413 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000414
Damien Miller95def091999-11-25 00:26:21 +1100415 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000416 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
418 /* Send success. */
419 buffer_put_int(&e->output, 1);
420 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100421}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000422
Ben Lindstrombba81212001-06-25 05:01:22 +0000423static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000424reaper(void)
425{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000426 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000427 Identity *id, *nxt;
428 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000429 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000430
431 for (version = 1; version < 3; version++) {
432 tab = idtab_lookup(version);
433 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
434 nxt = TAILQ_NEXT(id, next);
435 if (id->death != 0 && now >= id->death) {
436 TAILQ_REMOVE(&tab->idlist, id, next);
437 free_identity(id);
438 tab->nentries--;
439 }
440 }
441 }
442}
443
444static void
Damien Millerad833b32000-08-23 10:46:23 +1000445process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100446{
Damien Millerad833b32000-08-23 10:46:23 +1000447 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100448 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000449 char *type_name, *comment;
450 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100451
Damien Millerad833b32000-08-23 10:46:23 +1000452 switch (version) {
453 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100454 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000455 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000456 buffer_get_bignum(&e->request, k->rsa->n);
457 buffer_get_bignum(&e->request, k->rsa->e);
458 buffer_get_bignum(&e->request, k->rsa->d);
459 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100460
Damien Millerad833b32000-08-23 10:46:23 +1000461 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000462 buffer_get_bignum(&e->request, k->rsa->q); /* p */
463 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100464
Damien Millerad833b32000-08-23 10:46:23 +1000465 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000466 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000467 break;
468 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000469 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000470 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000472 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100473 case KEY_DSA:
474 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000475 buffer_get_bignum2(&e->request, k->dsa->p);
476 buffer_get_bignum2(&e->request, k->dsa->q);
477 buffer_get_bignum2(&e->request, k->dsa->g);
478 buffer_get_bignum2(&e->request, k->dsa->pub_key);
479 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 break;
481 case KEY_RSA:
482 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000483 buffer_get_bignum2(&e->request, k->rsa->n);
484 buffer_get_bignum2(&e->request, k->rsa->e);
485 buffer_get_bignum2(&e->request, k->rsa->d);
486 buffer_get_bignum2(&e->request, k->rsa->iqmp);
487 buffer_get_bignum2(&e->request, k->rsa->p);
488 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100489
490 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000491 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 break;
493 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000494 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000495 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100496 }
Damien Millerad833b32000-08-23 10:46:23 +1000497 break;
498 }
Damien Millerc51d0732003-03-15 11:37:09 +1100499 /* enable blinding */
500 switch (k->type) {
501 case KEY_RSA:
502 case KEY_RSA1:
503 if (RSA_blinding_on(k->rsa, NULL) != 1) {
504 error("process_add_identity: RSA_blinding_on failed");
505 key_free(k);
506 goto send;
507 }
508 break;
509 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000510 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000511 if (k == NULL) {
512 xfree(comment);
513 goto send;
514 }
515 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000516 while (buffer_len(&e->request)) {
517 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000518 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000519 death = time(NULL) + buffer_get_int(&e->request);
520 break;
Damien Miller6c711792003-01-24 11:36:23 +1100521 case SSH_AGENT_CONSTRAIN_CONFIRM:
522 confirm = 1;
523 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000524 default:
525 break;
526 }
527 }
Damien Miller53d81482003-01-22 11:47:19 +1100528 if (lifetime && !death)
529 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000530 if (lookup_identity(k, version) == NULL) {
531 Identity *id = xmalloc(sizeof(Identity));
532 id->key = k;
533 id->comment = comment;
534 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100535 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000536 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
537 /* Increment the number of identities. */
538 tab->nentries++;
539 } else {
540 key_free(k);
541 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000542 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000543send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000544 buffer_put_int(&e->output, 1);
545 buffer_put_char(&e->output,
546 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
547}
548
Ben Lindstrom2f717042002-06-06 21:52:03 +0000549/* XXX todo: encrypt sensitive data with passphrase */
550static void
551process_lock_agent(SocketEntry *e, int lock)
552{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000553 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000554 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000555
556 passwd = buffer_get_string(&e->request, NULL);
557 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
558 locked = 0;
559 memset(lock_passwd, 0, strlen(lock_passwd));
560 xfree(lock_passwd);
561 lock_passwd = NULL;
562 success = 1;
563 } else if (!locked && lock) {
564 locked = 1;
565 lock_passwd = xstrdup(passwd);
566 success = 1;
567 }
568 memset(passwd, 0, strlen(passwd));
569 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000570
Ben Lindstrom2f717042002-06-06 21:52:03 +0000571 buffer_put_int(&e->output, 1);
572 buffer_put_char(&e->output,
573 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000574}
575
576static void
577no_identities(SocketEntry *e, u_int type)
578{
579 Buffer msg;
580
581 buffer_init(&msg);
582 buffer_put_char(&msg,
583 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
584 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
585 buffer_put_int(&msg, 0);
586 buffer_put_int(&e->output, buffer_len(&msg));
587 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
588 buffer_free(&msg);
589}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000590
591#ifdef SMARTCARD
592static void
593process_add_smartcard_key (SocketEntry *e)
594{
Ben Lindstromba72d302002-03-22 03:51:06 +0000595 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000596 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000597 Key **keys, *k;
598 Identity *id;
599 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100600
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000601 sc_reader_id = buffer_get_string(&e->request, NULL);
602 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000603
604 while (buffer_len(&e->request)) {
605 switch (buffer_get_char(&e->request)) {
606 case SSH_AGENT_CONSTRAIN_LIFETIME:
607 death = time(NULL) + buffer_get_int(&e->request);
608 break;
609 case SSH_AGENT_CONSTRAIN_CONFIRM:
610 confirm = 1;
611 break;
612 default:
613 break;
614 }
615 }
616 if (lifetime && !death)
617 death = time(NULL) + lifetime;
618
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000619 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000620 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000621 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000622
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000623 if (keys == NULL || keys[0] == NULL) {
624 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000625 goto send;
626 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000627 for (i = 0; keys[i] != NULL; i++) {
628 k = keys[i];
629 version = k->type == KEY_RSA1 ? 1 : 2;
630 tab = idtab_lookup(version);
631 if (lookup_identity(k, version) == NULL) {
632 id = xmalloc(sizeof(Identity));
633 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000634 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000635 id->death = death;
636 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000637 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
638 tab->nentries++;
639 success = 1;
640 } else {
641 key_free(k);
642 }
643 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000644 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000645 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000646send:
647 buffer_put_int(&e->output, 1);
648 buffer_put_char(&e->output,
649 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
650}
651
652static void
653process_remove_smartcard_key(SocketEntry *e)
654{
Ben Lindstromba72d302002-03-22 03:51:06 +0000655 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000656 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000657 Key **keys, *k = NULL;
658 Identity *id;
659 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000661 sc_reader_id = buffer_get_string(&e->request, NULL);
662 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000663 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000664 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000665 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000666
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000667 if (keys == NULL || keys[0] == NULL) {
668 error("sc_get_keys failed");
669 goto send;
670 }
671 for (i = 0; keys[i] != NULL; i++) {
672 k = keys[i];
673 version = k->type == KEY_RSA1 ? 1 : 2;
674 if ((id = lookup_identity(k, version)) != NULL) {
675 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000676 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100678 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000679 success = 1;
680 }
681 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000682 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000683 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000684 xfree(keys);
685send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000686 buffer_put_int(&e->output, 1);
687 buffer_put_char(&e->output,
688 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
689}
Ben Lindstromffce1472001-08-06 21:57:31 +0000690#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000691
Damien Millerad833b32000-08-23 10:46:23 +1000692/* dispatch incoming messages */
693
Ben Lindstrombba81212001-06-25 05:01:22 +0000694static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000695process_message(SocketEntry *e)
696{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000697 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000698 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000699
700 /* kill dead keys */
701 reaper();
702
Damien Miller95def091999-11-25 00:26:21 +1100703 if (buffer_len(&e->input) < 5)
704 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100705 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100706 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100707 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000708 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100709 return;
710 }
711 if (buffer_len(&e->input) < msg_len + 4)
712 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000713
714 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100715 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000716 buffer_clear(&e->request);
717 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
718 buffer_consume(&e->input, msg_len);
719 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720
Ben Lindstrom2f717042002-06-06 21:52:03 +0000721 /* check wheter agent is locked */
722 if (locked && type != SSH_AGENTC_UNLOCK) {
723 buffer_clear(&e->request);
724 switch (type) {
725 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
726 case SSH2_AGENTC_REQUEST_IDENTITIES:
727 /* send empty lists */
728 no_identities(e, type);
729 break;
730 default:
731 /* send a fail message for all other request types */
732 buffer_put_int(&e->output, 1);
733 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
734 }
735 return;
736 }
737
Ben Lindstrom3f471632001-07-04 03:53:15 +0000738 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100739 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000740 case SSH_AGENTC_LOCK:
741 case SSH_AGENTC_UNLOCK:
742 process_lock_agent(e, type == SSH_AGENTC_LOCK);
743 break;
Damien Millerad833b32000-08-23 10:46:23 +1000744 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100745 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000746 process_authentication_challenge1(e);
747 break;
748 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
749 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100750 break;
751 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000752 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000753 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100754 break;
755 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000756 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100757 break;
758 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000759 process_remove_all_identities(e, 1);
760 break;
761 /* ssh2 */
762 case SSH2_AGENTC_SIGN_REQUEST:
763 process_sign_request2(e);
764 break;
765 case SSH2_AGENTC_REQUEST_IDENTITIES:
766 process_request_identities(e, 2);
767 break;
768 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000769 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000770 process_add_identity(e, 2);
771 break;
772 case SSH2_AGENTC_REMOVE_IDENTITY:
773 process_remove_identity(e, 2);
774 break;
775 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
776 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100777 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000778#ifdef SMARTCARD
779 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000780 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000781 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100782 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000783 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
784 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100785 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000786#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100787 default:
788 /* Unknown message. Respond with failure. */
789 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000790 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100791 buffer_put_int(&e->output, 1);
792 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
793 break;
794 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000795}
796
Ben Lindstrombba81212001-06-25 05:01:22 +0000797static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000798new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000800 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000801
Damien Miller232711f2004-06-15 10:35:30 +1000802 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000803
Damien Miller95def091999-11-25 00:26:21 +1100804 if (fd > max_fd)
805 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806
Damien Miller95def091999-11-25 00:26:21 +1100807 for (i = 0; i < sockets_alloc; i++)
808 if (sockets[i].type == AUTH_UNUSED) {
809 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100810 buffer_init(&sockets[i].input);
811 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000812 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000813 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100814 return;
815 }
816 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000817 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100818 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000819 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100820 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000821 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100822 sockets[old_alloc].fd = fd;
823 buffer_init(&sockets[old_alloc].input);
824 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000825 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000826 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000827}
828
Ben Lindstrombba81212001-06-25 05:01:22 +0000829static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000830prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000831{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000832 u_int i, sz;
833 int n = 0;
834
835 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100836 switch (sockets[i].type) {
837 case AUTH_SOCKET:
838 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000839 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100840 break;
841 case AUTH_UNUSED:
842 break;
843 default:
844 fatal("Unknown socket type %d", sockets[i].type);
845 break;
846 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847 }
848
849 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000850 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000851 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000852 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000853 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000854 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000855 *fdrp = xmalloc(sz);
856 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000857 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000858 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000859 if (n < *fdl)
860 debug("XXX shrink: %d < %d", n, *fdl);
861 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000862 memset(*fdrp, 0, sz);
863 memset(*fdwp, 0, sz);
864
865 for (i = 0; i < sockets_alloc; i++) {
866 switch (sockets[i].type) {
867 case AUTH_SOCKET:
868 case AUTH_CONNECTION:
869 FD_SET(sockets[i].fd, *fdrp);
870 if (buffer_len(&sockets[i].output) > 0)
871 FD_SET(sockets[i].fd, *fdwp);
872 break;
873 default:
874 break;
875 }
876 }
877 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878}
879
Ben Lindstrombba81212001-06-25 05:01:22 +0000880static void
Damien Miller95def091999-11-25 00:26:21 +1100881after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000883 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100884 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100885 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000886 int len, sock;
887 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000888 uid_t euid;
889 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000890
Damien Miller95def091999-11-25 00:26:21 +1100891 for (i = 0; i < sockets_alloc; i++)
892 switch (sockets[i].type) {
893 case AUTH_UNUSED:
894 break;
895 case AUTH_SOCKET:
896 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100897 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000898 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100899 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100900 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100901 error("accept from AUTH_SOCKET: %s",
902 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100903 break;
904 }
Damien Miller538f1812002-09-12 09:51:10 +1000905 if (getpeereid(sock, &euid, &egid) < 0) {
906 error("getpeereid %d failed: %s",
907 sock, strerror(errno));
908 close(sock);
909 break;
910 }
Damien Milleraf9de382002-10-03 11:54:35 +1000911 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000912 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000913 "peer euid %u != uid %u",
914 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000915 close(sock);
916 break;
917 }
Damien Miller95def091999-11-25 00:26:21 +1100918 new_socket(AUTH_CONNECTION, sock);
919 }
920 break;
921 case AUTH_CONNECTION:
922 if (buffer_len(&sockets[i].output) > 0 &&
923 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000924 do {
925 len = write(sockets[i].fd,
926 buffer_ptr(&sockets[i].output),
927 buffer_len(&sockets[i].output));
928 if (len == -1 && (errno == EAGAIN ||
929 errno == EINTR))
930 continue;
931 break;
932 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100933 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000934 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100935 break;
936 }
937 buffer_consume(&sockets[i].output, len);
938 }
939 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000940 do {
941 len = read(sockets[i].fd, buf, sizeof(buf));
942 if (len == -1 && (errno == EAGAIN ||
943 errno == EINTR))
944 continue;
945 break;
946 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100947 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000948 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100949 break;
950 }
951 buffer_append(&sockets[i].input, buf, len);
952 process_message(&sockets[i]);
953 }
954 break;
955 default:
956 fatal("Unknown type %d", sockets[i].type);
957 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958}
959
Ben Lindstrombba81212001-06-25 05:01:22 +0000960static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000961cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000962{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000963 if (socket_name[0])
964 unlink(socket_name);
965 if (socket_dir[0])
966 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000967}
968
Darren Tucker3e33cec2003-10-02 16:12:36 +1000969void
Damien Miller792c5111999-10-29 09:47:09 +1000970cleanup_exit(int i)
971{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000972 cleanup_socket();
973 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000974}
975
Damien Miller1c13bd82006-03-26 14:28:14 +1100976/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000977static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000978cleanup_handler(int sig)
979{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000980 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000981 _exit(2);
982}
983
Damien Miller1c13bd82006-03-26 14:28:14 +1100984/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000985static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000986check_parent_exists(int sig)
987{
988 int save_errno = errno;
989
990 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
991 /* printf("Parent has died - Authentication agent exiting.\n"); */
992 cleanup_handler(sig); /* safe */
993 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000994 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000995 alarm(10);
996 errno = save_errno;
997}
998
999static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001000usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001001{
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001002 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001003 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001004 fprintf(stderr, "Options:\n");
1005 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1006 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1007 fprintf(stderr, " -k Kill the current agent.\n");
1008 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001009 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001010 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001011 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001012}
1013
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014int
1015main(int ac, char **av)
1016{
Damien Miller6c711792003-01-24 11:36:23 +11001017 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001018 int sock, fd, ch;
1019 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001020 char *shell, *format, *pidstr, *agentsocket = NULL;
1021 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001022 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001023#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001024 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001025#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001026 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001027 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001028 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001029 pid_t pid;
1030 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001031
Darren Tuckerce321d82005-10-03 18:11:24 +10001032 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1033 sanitise_stdfd();
1034
Damien Miller6cffb9a2002-09-04 16:20:26 +10001035 /* drop */
1036 setegid(getgid());
1037 setgid(getgid());
1038
Damien Miller6c4914a2004-03-03 11:08:59 +11001039#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1040 /* Disable ptrace on Linux without sgid bit */
1041 prctl(PR_SET_DUMPABLE, 0);
1042#endif
1043
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001044 SSLeay_add_all_algorithms();
1045
Damien Miller59d3d5b2003-08-22 09:34:41 +10001046 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001047 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001048 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001049
Damien Miller53d81482003-01-22 11:47:19 +11001050 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001051 switch (ch) {
1052 case 'c':
1053 if (s_flag)
1054 usage();
1055 c_flag++;
1056 break;
1057 case 'k':
1058 k_flag++;
1059 break;
1060 case 's':
1061 if (c_flag)
1062 usage();
1063 s_flag++;
1064 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001065 case 'd':
1066 if (d_flag)
1067 usage();
1068 d_flag++;
1069 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001070 case 'a':
1071 agentsocket = optarg;
1072 break;
Damien Miller53d81482003-01-22 11:47:19 +11001073 case 't':
1074 if ((lifetime = convtime(optarg)) == -1) {
1075 fprintf(stderr, "Invalid lifetime\n");
1076 usage();
1077 }
1078 break;
Damien Miller95def091999-11-25 00:26:21 +11001079 default:
1080 usage();
1081 }
Damien Miller792c5111999-10-29 09:47:09 +10001082 }
Damien Miller95def091999-11-25 00:26:21 +11001083 ac -= optind;
1084 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001085
Ben Lindstromd94580c2001-07-04 03:48:02 +00001086 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001087 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001088
Ben Lindstromeecdf232002-04-02 21:03:51 +00001089 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001090 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001091 if (shell != NULL &&
1092 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001093 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001094 }
Damien Miller95def091999-11-25 00:26:21 +11001095 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001096 const char *errstr = NULL;
1097
Damien Miller95def091999-11-25 00:26:21 +11001098 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1099 if (pidstr == NULL) {
1100 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001101 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001102 exit(1);
1103 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001104 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1105 if (errstr) {
1106 fprintf(stderr,
1107 "%s=\"%s\", which is not a good PID: %s\n",
1108 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001109 exit(1);
1110 }
1111 if (kill(pid, SIGTERM) == -1) {
1112 perror("kill");
1113 exit(1);
1114 }
1115 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1116 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1117 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001118 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001119 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001120 }
Damien Miller95def091999-11-25 00:26:21 +11001121 parent_pid = getpid();
1122
Ben Lindstromb7788f32002-06-06 21:46:08 +00001123 if (agentsocket == NULL) {
1124 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001125 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001126 if (mkdtemp(socket_dir) == NULL) {
1127 perror("mkdtemp: private socket dir");
1128 exit(1);
1129 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001130 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1131 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001132 } else {
1133 /* Try to use specified agent socket */
1134 socket_dir[0] = '\0';
1135 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001136 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001137
Damien Miller5428f641999-11-25 11:54:57 +11001138 /*
1139 * Create socket early so it will exist before command gets run from
1140 * the parent.
1141 */
Damien Miller95def091999-11-25 00:26:21 +11001142 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1143 if (sock < 0) {
1144 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001145 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001146 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001147 }
Damien Miller95def091999-11-25 00:26:21 +11001148 memset(&sunaddr, 0, sizeof(sunaddr));
1149 sunaddr.sun_family = AF_UNIX;
1150 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001151 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001152 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001153 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001154 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001155 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001156 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001158 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001159 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001160 perror("listen");
1161 cleanup_exit(1);
1162 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001163
Damien Miller5428f641999-11-25 11:54:57 +11001164 /*
1165 * Fork, and have the parent execute the command, if any, or present
1166 * the socket data. The child continues as the authentication agent.
1167 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001168 if (d_flag) {
1169 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1170 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1171 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1172 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001173 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001174 goto skip;
1175 }
Damien Miller95def091999-11-25 00:26:21 +11001176 pid = fork();
1177 if (pid == -1) {
1178 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001179 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001180 }
1181 if (pid != 0) { /* Parent - execute the given command. */
1182 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001183 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001184 if (ac == 0) {
1185 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1186 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001187 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001188 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001189 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001190 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001191 exit(0);
1192 }
Damien Millere4340be2000-09-16 13:29:08 +11001193 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1194 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1195 perror("setenv");
1196 exit(1);
1197 }
Damien Miller95def091999-11-25 00:26:21 +11001198 execvp(av[0], av);
1199 perror(av[0]);
1200 exit(1);
1201 }
Damien Millerc653b892002-02-08 22:05:41 +11001202 /* child */
1203 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001204
1205 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001206 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001207 cleanup_exit(1);
1208 }
1209
1210 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001211 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1212 /* XXX might close listen socket */
1213 (void)dup2(fd, STDIN_FILENO);
1214 (void)dup2(fd, STDOUT_FILENO);
1215 (void)dup2(fd, STDERR_FILENO);
1216 if (fd > 2)
1217 close(fd);
1218 }
Damien Miller95def091999-11-25 00:26:21 +11001219
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001220#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001221 /* deny core dumps, since memory contains unencrypted private keys */
1222 rlim.rlim_cur = rlim.rlim_max = 0;
1223 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001224 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001225 cleanup_exit(1);
1226 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001227#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001228
1229skip:
Damien Miller95def091999-11-25 00:26:21 +11001230 new_socket(AUTH_SOCKET, sock);
1231 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001232 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001233 alarm(10);
1234 }
Damien Millerad833b32000-08-23 10:46:23 +10001235 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001236 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001237 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001238 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001239 signal(SIGHUP, cleanup_handler);
1240 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001241 nalloc = 0;
1242
Damien Miller95def091999-11-25 00:26:21 +11001243 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001244 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001245 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001246 if (errno == EINTR)
1247 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001248 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001249 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001250 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001251 }
1252 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001253}