blob: c3d5e5a757d9409a4e6289c8c43841ab44f2c6da [file] [log] [blame]
Darren Tucker2812dc92007-03-21 20:45:06 +11001/* $OpenBSD: ssh-agent.c,v 1.155 2007/03/19 12:16:42 dtucker 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 Millerded319c2006-09-01 15:38:36 +100061#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100062#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100063#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100064#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100065#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100066#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110067
Damien Millerd7834352006-08-05 12:39:39 +100068#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "ssh.h"
70#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100072#include "key.h"
73#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110074#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000075#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110076#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Ben Lindstrom3f471632001-07-04 03:53:15 +000078#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000079#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000080#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000081
Damien Miller6c4914a2004-03-03 11:08:59 +110082#if defined(HAVE_SYS_PRCTL_H)
83#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
84#endif
85
Ben Lindstrom65366a82001-12-06 16:32:47 +000086typedef enum {
87 AUTH_UNUSED,
88 AUTH_SOCKET,
89 AUTH_CONNECTION
90} sock_type;
91
Damien Miller95def091999-11-25 00:26:21 +110092typedef struct {
93 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000094 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110095 Buffer input;
96 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000097 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098} SocketEntry;
99
Ben Lindstrom46c16222000-12-22 01:43:59 +0000100u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101SocketEntry *sockets = NULL;
102
Damien Miller1a534ae2002-01-22 23:26:13 +1100103typedef struct identity {
104 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000105 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100106 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000107 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100108 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109} Identity;
110
Damien Millerad833b32000-08-23 10:46:23 +1000111typedef struct {
112 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100113 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000114} Idtab;
115
116/* private key table, one per protocol version */
117Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118
119int max_fd = 0;
120
121/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000122pid_t parent_pid = -1;
Darren Tucker2812dc92007-03-21 20:45:06 +1100123u_int parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
125/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100126char socket_name[MAXPATHLEN];
127char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128
Ben Lindstrom2f717042002-06-06 21:52:03 +0000129/* locking */
130int locked = 0;
131char *lock_passwd = NULL;
132
Damien Miller95def091999-11-25 00:26:21 +1100133extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100134
Damien Miller53d81482003-01-22 11:47:19 +1100135/* Default lifetime (0 == forever) */
136static int lifetime = 0;
137
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void
Damien Miller58f34862002-09-04 16:31:21 +1000139close_socket(SocketEntry *e)
140{
Damien Miller58f34862002-09-04 16:31:21 +1000141 close(e->fd);
142 e->fd = -1;
143 e->type = AUTH_UNUSED;
144 buffer_free(&e->input);
145 buffer_free(&e->output);
146 buffer_free(&e->request);
147}
148
149static void
Damien Millerad833b32000-08-23 10:46:23 +1000150idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151{
Damien Millerad833b32000-08-23 10:46:23 +1000152 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000153
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000154 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100155 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000156 idtable[i].nentries = 0;
157 }
158}
159
160/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000162idtab_lookup(int version)
163{
164 if (version < 1 || version > 2)
165 fatal("internal error, bad protocol version %d", version);
166 return &idtable[version];
167}
168
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000169static void
170free_identity(Identity *id)
171{
172 key_free(id->key);
173 xfree(id->comment);
174 xfree(id);
175}
176
Damien Millerad833b32000-08-23 10:46:23 +1000177/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100178static Identity *
179lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000180{
Damien Miller1a534ae2002-01-22 23:26:13 +1100181 Identity *id;
182
Damien Millerad833b32000-08-23 10:46:23 +1000183 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100184 TAILQ_FOREACH(id, &tab->idlist, next) {
185 if (key_equal(key, id->key))
186 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000187 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100188 return (NULL);
189}
190
Damien Miller6c711792003-01-24 11:36:23 +1100191/* Check confirmation of keysign request */
192static int
193confirm_key(Identity *id)
194{
Darren Tuckerce327b62004-11-05 20:38:03 +1100195 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100196 int ret = -1;
197
198 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100199 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
200 id->comment, p))
201 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100202 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100203
Damien Miller6c711792003-01-24 11:36:23 +1100204 return (ret);
205}
206
Damien Millerad833b32000-08-23 10:46:23 +1000207/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000208static void
Damien Millerad833b32000-08-23 10:46:23 +1000209process_request_identities(SocketEntry *e, int version)
210{
211 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100212 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000213 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller95def091999-11-25 00:26:21 +1100215 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000216 buffer_put_char(&msg, (version == 1) ?
217 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
218 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100219 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100220 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000221 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
222 buffer_put_bignum(&msg, id->key->rsa->e);
223 buffer_put_bignum(&msg, id->key->rsa->n);
224 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000225 u_char *blob;
226 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100227 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000228 buffer_put_string(&msg, blob, blen);
229 xfree(blob);
230 }
231 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100232 }
233 buffer_put_int(&e->output, buffer_len(&msg));
234 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
235 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236}
237
Damien Millerad833b32000-08-23 10:46:23 +1000238/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static void
Damien Millerad833b32000-08-23 10:46:23 +1000240process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000242 u_char buf[32], mdbuf[16], session_id[16];
243 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000244 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000245 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000246 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100247 Buffer msg;
248 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000249 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000250
Damien Miller95def091999-11-25 00:26:21 +1100251 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100252 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100253 if ((challenge = BN_new()) == NULL)
254 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000255
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000256 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000257 buffer_get_bignum(&e->request, key->rsa->e);
258 buffer_get_bignum(&e->request, key->rsa->n);
259 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260
Damien Millerad833b32000-08-23 10:46:23 +1000261 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000262 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000263 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000264 buffer_get(&e->request, session_id, 16);
265 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000266 if (response_type != 1)
267 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268
Damien Miller1a534ae2002-01-22 23:26:13 +1100269 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100270 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100271 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000272 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100273 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
274 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100275
Damien Millerad833b32000-08-23 10:46:23 +1000276 /* The response is MD5 of decrypted challenge plus session id. */
277 len = BN_num_bytes(challenge);
278 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000279 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000280 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100281 }
Damien Millerad833b32000-08-23 10:46:23 +1000282 memset(buf, 0, 32);
283 BN_bn2bin(challenge, buf + 32 - len);
284 MD5_Init(&md);
285 MD5_Update(&md, buf, 32);
286 MD5_Update(&md, session_id, 16);
287 MD5_Final(mdbuf, &md);
288
289 /* Send the response. */
290 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
291 for (i = 0; i < 16; i++)
292 buffer_put_char(&msg, mdbuf[i]);
293 goto send;
294 }
295
296failure:
297 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100298 buffer_put_char(&msg, SSH_AGENT_FAILURE);
299send:
300 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000301 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
302 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100303 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000304 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305}
306
Damien Millerad833b32000-08-23 10:46:23 +1000307/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000308static void
Damien Millerad833b32000-08-23 10:46:23 +1000309process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000310{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000311 u_char *blob, *data, *signature = NULL;
312 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000313 extern int datafellows;
314 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000315 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000316 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000317
318 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000319
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000320 blob = buffer_get_string(&e->request, &blen);
321 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100322
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000323 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100324 if (flags & SSH_AGENT_OLD_SIGNATURE)
325 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000326
Damien Miller0bc1bd82000-11-13 22:57:25 +1100327 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000328 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100329 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100330 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100331 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000332 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000333 }
Damien Millerad833b32000-08-23 10:46:23 +1000334 buffer_init(&msg);
335 if (ok == 0) {
336 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
337 buffer_put_string(&msg, signature, slen);
338 } else {
339 buffer_put_char(&msg, SSH_AGENT_FAILURE);
340 }
341 buffer_put_int(&e->output, buffer_len(&msg));
342 buffer_append(&e->output, buffer_ptr(&msg),
343 buffer_len(&msg));
344 buffer_free(&msg);
345 xfree(data);
346 xfree(blob);
347 if (signature != NULL)
348 xfree(signature);
349}
350
351/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000352static void
Damien Millerad833b32000-08-23 10:46:23 +1000353process_remove_identity(SocketEntry *e, int version)
354{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000355 u_int blen, bits;
356 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100357 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000358 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100359
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000360 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000361 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100362 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000363 bits = buffer_get_int(&e->request);
364 buffer_get_bignum(&e->request, key->rsa->e);
365 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Millerad833b32000-08-23 10:46:23 +1000367 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000368 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000369 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000370 break;
371 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000372 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100373 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000374 xfree(blob);
375 break;
376 }
377 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100378 Identity *id = lookup_identity(key, version);
379 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100380 /*
381 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100382 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000383 * the array, we actually free the key there and move
384 * all the entries between the empty slot and the end
385 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100386 */
Damien Millerad833b32000-08-23 10:46:23 +1000387 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100388 if (tab->nentries < 1)
389 fatal("process_remove_identity: "
390 "internal error: tab->nentries %d",
391 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100392 TAILQ_REMOVE(&tab->idlist, id, next);
393 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000394 tab->nentries--;
395 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100396 }
Damien Millerad833b32000-08-23 10:46:23 +1000397 key_free(key);
398 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000399 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000400 buffer_put_char(&e->output,
401 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402}
403
Ben Lindstrombba81212001-06-25 05:01:22 +0000404static void
Damien Millerad833b32000-08-23 10:46:23 +1000405process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406{
Damien Millerad833b32000-08-23 10:46:23 +1000407 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100408 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409
Damien Miller95def091999-11-25 00:26:21 +1100410 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100411 for (id = TAILQ_FIRST(&tab->idlist); id;
412 id = TAILQ_FIRST(&tab->idlist)) {
413 TAILQ_REMOVE(&tab->idlist, id, next);
414 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100415 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000416
Damien Miller95def091999-11-25 00:26:21 +1100417 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000418 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000419
420 /* Send success. */
421 buffer_put_int(&e->output, 1);
422 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100423}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000424
Darren Tucker2812dc92007-03-21 20:45:06 +1100425/* removes expired keys and returns number of seconds until the next expiry */
426static u_int
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000427reaper(void)
428{
Darren Tucker2812dc92007-03-21 20:45:06 +1100429 u_int deadline = 0, now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000430 Identity *id, *nxt;
431 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000432 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000433
434 for (version = 1; version < 3; version++) {
435 tab = idtab_lookup(version);
436 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
437 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100438 if (id->death == 0)
439 continue;
440 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100441 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000442 TAILQ_REMOVE(&tab->idlist, id, next);
443 free_identity(id);
444 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100445 } else
446 deadline = (deadline == 0) ? id->death :
447 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000448 }
449 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100450 if (deadline == 0 || deadline <= now)
451 return 0;
452 else
453 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000454}
455
456static void
Damien Millerad833b32000-08-23 10:46:23 +1000457process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100458{
Damien Millerad833b32000-08-23 10:46:23 +1000459 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100460 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000461 char *type_name, *comment;
462 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100463
Damien Millerad833b32000-08-23 10:46:23 +1000464 switch (version) {
465 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100466 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000467 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000468 buffer_get_bignum(&e->request, k->rsa->n);
469 buffer_get_bignum(&e->request, k->rsa->e);
470 buffer_get_bignum(&e->request, k->rsa->d);
471 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100472
Damien Millerad833b32000-08-23 10:46:23 +1000473 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000474 buffer_get_bignum(&e->request, k->rsa->q); /* p */
475 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100476
Damien Millerad833b32000-08-23 10:46:23 +1000477 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000478 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000479 break;
480 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000481 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000482 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000484 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 case KEY_DSA:
486 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000487 buffer_get_bignum2(&e->request, k->dsa->p);
488 buffer_get_bignum2(&e->request, k->dsa->q);
489 buffer_get_bignum2(&e->request, k->dsa->g);
490 buffer_get_bignum2(&e->request, k->dsa->pub_key);
491 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 break;
493 case KEY_RSA:
494 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000495 buffer_get_bignum2(&e->request, k->rsa->n);
496 buffer_get_bignum2(&e->request, k->rsa->e);
497 buffer_get_bignum2(&e->request, k->rsa->d);
498 buffer_get_bignum2(&e->request, k->rsa->iqmp);
499 buffer_get_bignum2(&e->request, k->rsa->p);
500 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100501
502 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000503 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504 break;
505 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000506 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000507 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100508 }
Damien Millerad833b32000-08-23 10:46:23 +1000509 break;
510 }
Damien Millerc51d0732003-03-15 11:37:09 +1100511 /* enable blinding */
512 switch (k->type) {
513 case KEY_RSA:
514 case KEY_RSA1:
515 if (RSA_blinding_on(k->rsa, NULL) != 1) {
516 error("process_add_identity: RSA_blinding_on failed");
517 key_free(k);
518 goto send;
519 }
520 break;
521 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000522 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000523 if (k == NULL) {
524 xfree(comment);
525 goto send;
526 }
527 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000528 while (buffer_len(&e->request)) {
529 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000530 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000531 death = time(NULL) + buffer_get_int(&e->request);
532 break;
Damien Miller6c711792003-01-24 11:36:23 +1100533 case SSH_AGENT_CONSTRAIN_CONFIRM:
534 confirm = 1;
535 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000536 default:
537 break;
538 }
539 }
Damien Miller53d81482003-01-22 11:47:19 +1100540 if (lifetime && !death)
541 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000542 if (lookup_identity(k, version) == NULL) {
543 Identity *id = xmalloc(sizeof(Identity));
544 id->key = k;
545 id->comment = comment;
546 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100547 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000548 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
549 /* Increment the number of identities. */
550 tab->nentries++;
551 } else {
552 key_free(k);
553 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000554 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000555send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000556 buffer_put_int(&e->output, 1);
557 buffer_put_char(&e->output,
558 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
559}
560
Ben Lindstrom2f717042002-06-06 21:52:03 +0000561/* XXX todo: encrypt sensitive data with passphrase */
562static void
563process_lock_agent(SocketEntry *e, int lock)
564{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000565 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000566 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000567
568 passwd = buffer_get_string(&e->request, NULL);
569 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
570 locked = 0;
571 memset(lock_passwd, 0, strlen(lock_passwd));
572 xfree(lock_passwd);
573 lock_passwd = NULL;
574 success = 1;
575 } else if (!locked && lock) {
576 locked = 1;
577 lock_passwd = xstrdup(passwd);
578 success = 1;
579 }
580 memset(passwd, 0, strlen(passwd));
581 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000582
Ben Lindstrom2f717042002-06-06 21:52:03 +0000583 buffer_put_int(&e->output, 1);
584 buffer_put_char(&e->output,
585 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000586}
587
588static void
589no_identities(SocketEntry *e, u_int type)
590{
591 Buffer msg;
592
593 buffer_init(&msg);
594 buffer_put_char(&msg,
595 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
596 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
597 buffer_put_int(&msg, 0);
598 buffer_put_int(&e->output, buffer_len(&msg));
599 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
600 buffer_free(&msg);
601}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000602
603#ifdef SMARTCARD
604static void
605process_add_smartcard_key (SocketEntry *e)
606{
Ben Lindstromba72d302002-03-22 03:51:06 +0000607 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000608 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000609 Key **keys, *k;
610 Identity *id;
611 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100612
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000613 sc_reader_id = buffer_get_string(&e->request, NULL);
614 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000615
616 while (buffer_len(&e->request)) {
617 switch (buffer_get_char(&e->request)) {
618 case SSH_AGENT_CONSTRAIN_LIFETIME:
619 death = time(NULL) + buffer_get_int(&e->request);
620 break;
621 case SSH_AGENT_CONSTRAIN_CONFIRM:
622 confirm = 1;
623 break;
624 default:
625 break;
626 }
627 }
628 if (lifetime && !death)
629 death = time(NULL) + lifetime;
630
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000631 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000632 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000633 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000634
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000635 if (keys == NULL || keys[0] == NULL) {
636 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000637 goto send;
638 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000639 for (i = 0; keys[i] != NULL; i++) {
640 k = keys[i];
641 version = k->type == KEY_RSA1 ? 1 : 2;
642 tab = idtab_lookup(version);
643 if (lookup_identity(k, version) == NULL) {
644 id = xmalloc(sizeof(Identity));
645 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000646 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000647 id->death = death;
648 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000649 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
650 tab->nentries++;
651 success = 1;
652 } else {
653 key_free(k);
654 }
655 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000656 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000657 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658send:
659 buffer_put_int(&e->output, 1);
660 buffer_put_char(&e->output,
661 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
662}
663
664static void
665process_remove_smartcard_key(SocketEntry *e)
666{
Ben Lindstromba72d302002-03-22 03:51:06 +0000667 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000668 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000669 Key **keys, *k = NULL;
670 Identity *id;
671 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000672
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000673 sc_reader_id = buffer_get_string(&e->request, NULL);
674 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000675 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000676 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000677 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000678
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000679 if (keys == NULL || keys[0] == NULL) {
680 error("sc_get_keys failed");
681 goto send;
682 }
683 for (i = 0; keys[i] != NULL; i++) {
684 k = keys[i];
685 version = k->type == KEY_RSA1 ? 1 : 2;
686 if ((id = lookup_identity(k, version)) != NULL) {
687 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000688 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000689 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100690 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000691 success = 1;
692 }
693 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000694 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000695 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000696 xfree(keys);
697send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000698 buffer_put_int(&e->output, 1);
699 buffer_put_char(&e->output,
700 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
701}
Ben Lindstromffce1472001-08-06 21:57:31 +0000702#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000703
Damien Millerad833b32000-08-23 10:46:23 +1000704/* dispatch incoming messages */
705
Ben Lindstrombba81212001-06-25 05:01:22 +0000706static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000707process_message(SocketEntry *e)
708{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000709 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000710 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000711
Damien Miller95def091999-11-25 00:26:21 +1100712 if (buffer_len(&e->input) < 5)
713 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100714 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100715 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100716 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000717 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100718 return;
719 }
720 if (buffer_len(&e->input) < msg_len + 4)
721 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000722
723 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100724 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000725 buffer_clear(&e->request);
726 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
727 buffer_consume(&e->input, msg_len);
728 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000729
Ben Lindstrom2f717042002-06-06 21:52:03 +0000730 /* check wheter agent is locked */
731 if (locked && type != SSH_AGENTC_UNLOCK) {
732 buffer_clear(&e->request);
733 switch (type) {
734 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
735 case SSH2_AGENTC_REQUEST_IDENTITIES:
736 /* send empty lists */
737 no_identities(e, type);
738 break;
739 default:
740 /* send a fail message for all other request types */
741 buffer_put_int(&e->output, 1);
742 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
743 }
744 return;
745 }
746
Ben Lindstrom3f471632001-07-04 03:53:15 +0000747 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100748 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000749 case SSH_AGENTC_LOCK:
750 case SSH_AGENTC_UNLOCK:
751 process_lock_agent(e, type == SSH_AGENTC_LOCK);
752 break;
Damien Millerad833b32000-08-23 10:46:23 +1000753 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100754 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000755 process_authentication_challenge1(e);
756 break;
757 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
758 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100759 break;
760 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000761 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000762 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100763 break;
764 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000765 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100766 break;
767 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000768 process_remove_all_identities(e, 1);
769 break;
770 /* ssh2 */
771 case SSH2_AGENTC_SIGN_REQUEST:
772 process_sign_request2(e);
773 break;
774 case SSH2_AGENTC_REQUEST_IDENTITIES:
775 process_request_identities(e, 2);
776 break;
777 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000778 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000779 process_add_identity(e, 2);
780 break;
781 case SSH2_AGENTC_REMOVE_IDENTITY:
782 process_remove_identity(e, 2);
783 break;
784 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
785 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100786 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000787#ifdef SMARTCARD
788 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000789 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000790 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100791 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000792 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
793 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100794 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000795#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100796 default:
797 /* Unknown message. Respond with failure. */
798 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000799 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100800 buffer_put_int(&e->output, 1);
801 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
802 break;
803 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804}
805
Ben Lindstrombba81212001-06-25 05:01:22 +0000806static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000807new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000808{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000809 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000810
Damien Miller232711f2004-06-15 10:35:30 +1000811 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000812
Damien Miller95def091999-11-25 00:26:21 +1100813 if (fd > max_fd)
814 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815
Damien Miller95def091999-11-25 00:26:21 +1100816 for (i = 0; i < sockets_alloc; i++)
817 if (sockets[i].type == AUTH_UNUSED) {
818 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100819 buffer_init(&sockets[i].input);
820 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000821 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000822 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100823 return;
824 }
825 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000826 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100827 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000828 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100829 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000830 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100831 sockets[old_alloc].fd = fd;
832 buffer_init(&sockets[old_alloc].input);
833 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000834 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000835 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000836}
837
Ben Lindstrombba81212001-06-25 05:01:22 +0000838static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100839prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
840 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000841{
Darren Tucker2812dc92007-03-21 20:45:06 +1100842 u_int i, sz, deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100844 static struct timeval tv;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845
846 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100847 switch (sockets[i].type) {
848 case AUTH_SOCKET:
849 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100851 break;
852 case AUTH_UNUSED:
853 break;
854 default:
855 fatal("Unknown socket type %d", sockets[i].type);
856 break;
857 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000858 }
859
860 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000861 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000862 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000863 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000864 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000865 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000866 *fdrp = xmalloc(sz);
867 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000868 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000869 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000870 if (n < *fdl)
871 debug("XXX shrink: %d < %d", n, *fdl);
872 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000873 memset(*fdrp, 0, sz);
874 memset(*fdwp, 0, sz);
875
876 for (i = 0; i < sockets_alloc; i++) {
877 switch (sockets[i].type) {
878 case AUTH_SOCKET:
879 case AUTH_CONNECTION:
880 FD_SET(sockets[i].fd, *fdrp);
881 if (buffer_len(&sockets[i].output) > 0)
882 FD_SET(sockets[i].fd, *fdwp);
883 break;
884 default:
885 break;
886 }
887 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100888 deadline = reaper();
889 if (parent_alive_interval != 0)
890 deadline = (deadline == 0) ? parent_alive_interval :
891 MIN(deadline, parent_alive_interval);
892 if (deadline == 0) {
893 *tvpp = NULL;
894 } else {
895 tv.tv_sec = deadline;
896 tv.tv_usec = 0;
897 *tvpp = &tv;
898 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000899 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000900}
901
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static void
Damien Miller95def091999-11-25 00:26:21 +1100903after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000904{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000905 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100906 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100907 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000908 int len, sock;
909 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000910 uid_t euid;
911 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000912
Damien Miller95def091999-11-25 00:26:21 +1100913 for (i = 0; i < sockets_alloc; i++)
914 switch (sockets[i].type) {
915 case AUTH_UNUSED:
916 break;
917 case AUTH_SOCKET:
918 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100919 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000920 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100921 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100922 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100923 error("accept from AUTH_SOCKET: %s",
924 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100925 break;
926 }
Damien Miller538f1812002-09-12 09:51:10 +1000927 if (getpeereid(sock, &euid, &egid) < 0) {
928 error("getpeereid %d failed: %s",
929 sock, strerror(errno));
930 close(sock);
931 break;
932 }
Damien Milleraf9de382002-10-03 11:54:35 +1000933 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000934 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000935 "peer euid %u != uid %u",
936 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000937 close(sock);
938 break;
939 }
Damien Miller95def091999-11-25 00:26:21 +1100940 new_socket(AUTH_CONNECTION, sock);
941 }
942 break;
943 case AUTH_CONNECTION:
944 if (buffer_len(&sockets[i].output) > 0 &&
945 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000946 do {
947 len = write(sockets[i].fd,
948 buffer_ptr(&sockets[i].output),
949 buffer_len(&sockets[i].output));
950 if (len == -1 && (errno == EAGAIN ||
951 errno == EINTR))
952 continue;
953 break;
954 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100955 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000956 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100957 break;
958 }
959 buffer_consume(&sockets[i].output, len);
960 }
961 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000962 do {
963 len = read(sockets[i].fd, buf, sizeof(buf));
964 if (len == -1 && (errno == EAGAIN ||
965 errno == EINTR))
966 continue;
967 break;
968 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100969 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000970 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100971 break;
972 }
973 buffer_append(&sockets[i].input, buf, len);
974 process_message(&sockets[i]);
975 }
976 break;
977 default:
978 fatal("Unknown type %d", sockets[i].type);
979 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980}
981
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000983cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000984{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000985 if (socket_name[0])
986 unlink(socket_name);
987 if (socket_dir[0])
988 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000989}
990
Darren Tucker3e33cec2003-10-02 16:12:36 +1000991void
Damien Miller792c5111999-10-29 09:47:09 +1000992cleanup_exit(int i)
993{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000994 cleanup_socket();
995 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000996}
997
Damien Miller1c13bd82006-03-26 14:28:14 +1100998/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000999static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001000cleanup_handler(int sig)
1001{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001002 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001003 _exit(2);
1004}
1005
Ben Lindstrombba81212001-06-25 05:01:22 +00001006static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001007check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001008{
Ben Lindstrom0250da02001-07-22 20:44:00 +00001009 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1010 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001011 cleanup_socket();
1012 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001013 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001014}
1015
1016static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001017usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001018{
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001019 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001020 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001021 fprintf(stderr, "Options:\n");
1022 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1023 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1024 fprintf(stderr, " -k Kill the current agent.\n");
1025 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001026 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001027 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001028 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001029}
1030
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031int
1032main(int ac, char **av)
1033{
Damien Miller6c711792003-01-24 11:36:23 +11001034 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001035 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001036 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001037 char *shell, *format, *pidstr, *agentsocket = NULL;
1038 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001039 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001040#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001041 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001042#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001043 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001044 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001045 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001046 pid_t pid;
1047 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001048 struct timeval *tvp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001049
Darren Tuckerce321d82005-10-03 18:11:24 +10001050 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1051 sanitise_stdfd();
1052
Damien Miller6cffb9a2002-09-04 16:20:26 +10001053 /* drop */
1054 setegid(getgid());
1055 setgid(getgid());
1056
Damien Miller6c4914a2004-03-03 11:08:59 +11001057#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1058 /* Disable ptrace on Linux without sgid bit */
1059 prctl(PR_SET_DUMPABLE, 0);
1060#endif
1061
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001062 SSLeay_add_all_algorithms();
1063
Damien Miller59d3d5b2003-08-22 09:34:41 +10001064 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001065 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001066 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001067
Damien Miller53d81482003-01-22 11:47:19 +11001068 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001069 switch (ch) {
1070 case 'c':
1071 if (s_flag)
1072 usage();
1073 c_flag++;
1074 break;
1075 case 'k':
1076 k_flag++;
1077 break;
1078 case 's':
1079 if (c_flag)
1080 usage();
1081 s_flag++;
1082 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001083 case 'd':
1084 if (d_flag)
1085 usage();
1086 d_flag++;
1087 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001088 case 'a':
1089 agentsocket = optarg;
1090 break;
Damien Miller53d81482003-01-22 11:47:19 +11001091 case 't':
1092 if ((lifetime = convtime(optarg)) == -1) {
1093 fprintf(stderr, "Invalid lifetime\n");
1094 usage();
1095 }
1096 break;
Damien Miller95def091999-11-25 00:26:21 +11001097 default:
1098 usage();
1099 }
Damien Miller792c5111999-10-29 09:47:09 +10001100 }
Damien Miller95def091999-11-25 00:26:21 +11001101 ac -= optind;
1102 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001103
Ben Lindstromd94580c2001-07-04 03:48:02 +00001104 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001105 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001106
Ben Lindstromeecdf232002-04-02 21:03:51 +00001107 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001108 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001109 if (shell != NULL &&
1110 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001111 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001112 }
Damien Miller95def091999-11-25 00:26:21 +11001113 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001114 const char *errstr = NULL;
1115
Damien Miller95def091999-11-25 00:26:21 +11001116 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1117 if (pidstr == NULL) {
1118 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001119 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001120 exit(1);
1121 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001122 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1123 if (errstr) {
1124 fprintf(stderr,
1125 "%s=\"%s\", which is not a good PID: %s\n",
1126 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001127 exit(1);
1128 }
1129 if (kill(pid, SIGTERM) == -1) {
1130 perror("kill");
1131 exit(1);
1132 }
1133 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1134 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1135 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001136 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001137 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001138 }
Damien Miller95def091999-11-25 00:26:21 +11001139 parent_pid = getpid();
1140
Ben Lindstromb7788f32002-06-06 21:46:08 +00001141 if (agentsocket == NULL) {
1142 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001143 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001144 if (mkdtemp(socket_dir) == NULL) {
1145 perror("mkdtemp: private socket dir");
1146 exit(1);
1147 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001148 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1149 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001150 } else {
1151 /* Try to use specified agent socket */
1152 socket_dir[0] = '\0';
1153 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001154 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001155
Damien Miller5428f641999-11-25 11:54:57 +11001156 /*
1157 * Create socket early so it will exist before command gets run from
1158 * the parent.
1159 */
Damien Miller95def091999-11-25 00:26:21 +11001160 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1161 if (sock < 0) {
1162 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001163 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001164 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001165 }
Damien Miller95def091999-11-25 00:26:21 +11001166 memset(&sunaddr, 0, sizeof(sunaddr));
1167 sunaddr.sun_family = AF_UNIX;
1168 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001169 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001170 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001171 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001172 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001173 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001174 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001175 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001176 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001177 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001178 perror("listen");
1179 cleanup_exit(1);
1180 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001181
Damien Miller5428f641999-11-25 11:54:57 +11001182 /*
1183 * Fork, and have the parent execute the command, if any, or present
1184 * the socket data. The child continues as the authentication agent.
1185 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001186 if (d_flag) {
1187 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1188 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1189 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1190 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001191 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001192 goto skip;
1193 }
Damien Miller95def091999-11-25 00:26:21 +11001194 pid = fork();
1195 if (pid == -1) {
1196 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001197 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001198 }
1199 if (pid != 0) { /* Parent - execute the given command. */
1200 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001201 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001202 if (ac == 0) {
1203 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1204 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001205 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001206 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001207 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001208 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001209 exit(0);
1210 }
Damien Millere4340be2000-09-16 13:29:08 +11001211 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1212 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1213 perror("setenv");
1214 exit(1);
1215 }
Damien Miller95def091999-11-25 00:26:21 +11001216 execvp(av[0], av);
1217 perror(av[0]);
1218 exit(1);
1219 }
Damien Millerc653b892002-02-08 22:05:41 +11001220 /* child */
1221 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001222
1223 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001224 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001225 cleanup_exit(1);
1226 }
1227
1228 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001229 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1230 /* XXX might close listen socket */
1231 (void)dup2(fd, STDIN_FILENO);
1232 (void)dup2(fd, STDOUT_FILENO);
1233 (void)dup2(fd, STDERR_FILENO);
1234 if (fd > 2)
1235 close(fd);
1236 }
Damien Miller95def091999-11-25 00:26:21 +11001237
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001238#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001239 /* deny core dumps, since memory contains unencrypted private keys */
1240 rlim.rlim_cur = rlim.rlim_max = 0;
1241 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001242 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001243 cleanup_exit(1);
1244 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001245#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001246
1247skip:
Damien Miller95def091999-11-25 00:26:21 +11001248 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001249 if (ac > 0)
1250 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001251 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001252 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001253 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001254 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001255 signal(SIGHUP, cleanup_handler);
1256 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001257 nalloc = 0;
1258
Damien Miller95def091999-11-25 00:26:21 +11001259 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001260 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1261 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001262 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001263 if (parent_alive_interval != 0)
1264 check_parent_exists();
1265 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001266 if (result < 0) {
1267 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001268 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001269 fatal("select: %s", strerror(saved_errno));
1270 } else if (result > 0)
1271 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001272 }
1273 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001274}