blob: f77dea3a63655ccae4dabc9c117b9b5252d98d5d [file] [log] [blame]
Darren Tucker90133232009-06-21 17:50:15 +10001/* $OpenBSD: ssh-agent.c,v 1.161 2009/03/23 19:38:04 tobias 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>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Millere3476ed2006-07-24 14:13:33 +100055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100057#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110058#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110059# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110060#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110061#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100062#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100063#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100064#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100065#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100067#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110068
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "ssh.h"
71#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "key.h"
74#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110075#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110077#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Ben Lindstrom3f471632001-07-04 03:53:15 +000079#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000080#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000081#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000082
Damien Miller6c4914a2004-03-03 11:08:59 +110083#if defined(HAVE_SYS_PRCTL_H)
84#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
85#endif
86
Ben Lindstrom65366a82001-12-06 16:32:47 +000087typedef enum {
88 AUTH_UNUSED,
89 AUTH_SOCKET,
90 AUTH_CONNECTION
91} sock_type;
92
Damien Miller95def091999-11-25 00:26:21 +110093typedef struct {
94 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000095 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110096 Buffer input;
97 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000098 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099} SocketEntry;
100
Ben Lindstrom46c16222000-12-22 01:43:59 +0000101u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102SocketEntry *sockets = NULL;
103
Damien Miller1a534ae2002-01-22 23:26:13 +1100104typedef struct identity {
105 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000106 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100107 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000108 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100109 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110} Identity;
111
Damien Millerad833b32000-08-23 10:46:23 +1000112typedef struct {
113 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100114 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000115} Idtab;
116
117/* private key table, one per protocol version */
118Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
120int max_fd = 0;
121
122/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000123pid_t parent_pid = -1;
Darren Tucker2812dc92007-03-21 20:45:06 +1100124u_int parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
126/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100127char socket_name[MAXPATHLEN];
128char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Ben Lindstrom2f717042002-06-06 21:52:03 +0000130/* locking */
131int locked = 0;
132char *lock_passwd = NULL;
133
Damien Miller95def091999-11-25 00:26:21 +1100134extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100135
Damien Miller53d81482003-01-22 11:47:19 +1100136/* Default lifetime (0 == forever) */
137static int lifetime = 0;
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void
Damien Miller58f34862002-09-04 16:31:21 +1000140close_socket(SocketEntry *e)
141{
Damien Miller58f34862002-09-04 16:31:21 +1000142 close(e->fd);
143 e->fd = -1;
144 e->type = AUTH_UNUSED;
145 buffer_free(&e->input);
146 buffer_free(&e->output);
147 buffer_free(&e->request);
148}
149
150static void
Damien Millerad833b32000-08-23 10:46:23 +1000151idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Millerad833b32000-08-23 10:46:23 +1000153 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000154
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000155 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000157 idtable[i].nentries = 0;
158 }
159}
160
161/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000163idtab_lookup(int version)
164{
165 if (version < 1 || version > 2)
166 fatal("internal error, bad protocol version %d", version);
167 return &idtable[version];
168}
169
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000170static void
171free_identity(Identity *id)
172{
173 key_free(id->key);
174 xfree(id->comment);
175 xfree(id);
176}
177
Damien Millerad833b32000-08-23 10:46:23 +1000178/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100179static Identity *
180lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000181{
Damien Miller1a534ae2002-01-22 23:26:13 +1100182 Identity *id;
183
Damien Millerad833b32000-08-23 10:46:23 +1000184 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 TAILQ_FOREACH(id, &tab->idlist, next) {
186 if (key_equal(key, id->key))
187 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000188 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100189 return (NULL);
190}
191
Damien Miller6c711792003-01-24 11:36:23 +1100192/* Check confirmation of keysign request */
193static int
194confirm_key(Identity *id)
195{
Darren Tuckerce327b62004-11-05 20:38:03 +1100196 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100197 int ret = -1;
198
199 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100200 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
201 id->comment, p))
202 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100203 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100204
Damien Miller6c711792003-01-24 11:36:23 +1100205 return (ret);
206}
207
Damien Millerad833b32000-08-23 10:46:23 +1000208/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Millerad833b32000-08-23 10:46:23 +1000210process_request_identities(SocketEntry *e, int version)
211{
212 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100213 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000214 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000217 buffer_put_char(&msg, (version == 1) ?
218 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
219 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100220 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000222 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
223 buffer_put_bignum(&msg, id->key->rsa->e);
224 buffer_put_bignum(&msg, id->key->rsa->n);
225 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000226 u_char *blob;
227 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100228 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000229 buffer_put_string(&msg, blob, blen);
230 xfree(blob);
231 }
232 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100233 }
234 buffer_put_int(&e->output, buffer_len(&msg));
235 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
236 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237}
238
Damien Millerad833b32000-08-23 10:46:23 +1000239/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
Damien Millerad833b32000-08-23 10:46:23 +1000241process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000243 u_char buf[32], mdbuf[16], session_id[16];
244 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000245 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000246 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000247 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100248 Buffer msg;
249 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000250 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100253 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100254 if ((challenge = BN_new()) == NULL)
255 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000256
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000257 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000258 buffer_get_bignum(&e->request, key->rsa->e);
259 buffer_get_bignum(&e->request, key->rsa->n);
260 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Millerad833b32000-08-23 10:46:23 +1000262 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000263 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000264 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000265 buffer_get(&e->request, session_id, 16);
266 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000267 if (response_type != 1)
268 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller1a534ae2002-01-22 23:26:13 +1100270 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100271 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100272 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000273 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100274 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
275 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100276
Damien Millerad833b32000-08-23 10:46:23 +1000277 /* The response is MD5 of decrypted challenge plus session id. */
278 len = BN_num_bytes(challenge);
279 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000280 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000281 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100282 }
Damien Millerad833b32000-08-23 10:46:23 +1000283 memset(buf, 0, 32);
284 BN_bn2bin(challenge, buf + 32 - len);
285 MD5_Init(&md);
286 MD5_Update(&md, buf, 32);
287 MD5_Update(&md, session_id, 16);
288 MD5_Final(mdbuf, &md);
289
290 /* Send the response. */
291 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
292 for (i = 0; i < 16; i++)
293 buffer_put_char(&msg, mdbuf[i]);
294 goto send;
295 }
296
297failure:
298 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100299 buffer_put_char(&msg, SSH_AGENT_FAILURE);
300send:
301 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000302 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
303 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100304 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000305 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Millerad833b32000-08-23 10:46:23 +1000308/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static void
Damien Millerad833b32000-08-23 10:46:23 +1000310process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000312 u_char *blob, *data, *signature = NULL;
313 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000314 extern int datafellows;
Damien Miller471db5c2008-06-30 00:05:48 +1000315 int odatafellows;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000316 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000317 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000318 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000319
320 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000321
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000322 blob = buffer_get_string(&e->request, &blen);
323 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100324
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000325 flags = buffer_get_int(&e->request);
Damien Miller471db5c2008-06-30 00:05:48 +1000326 odatafellows = datafellows;
Damien Miller62cee002000-09-23 17:15:56 +1100327 if (flags & SSH_AGENT_OLD_SIGNATURE)
328 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000329
Damien Miller0bc1bd82000-11-13 22:57:25 +1100330 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000331 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100332 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100333 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100334 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000335 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000336 }
Damien Millerad833b32000-08-23 10:46:23 +1000337 buffer_init(&msg);
338 if (ok == 0) {
339 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
340 buffer_put_string(&msg, signature, slen);
341 } else {
342 buffer_put_char(&msg, SSH_AGENT_FAILURE);
343 }
344 buffer_put_int(&e->output, buffer_len(&msg));
345 buffer_append(&e->output, buffer_ptr(&msg),
346 buffer_len(&msg));
347 buffer_free(&msg);
348 xfree(data);
349 xfree(blob);
350 if (signature != NULL)
351 xfree(signature);
Damien Miller471db5c2008-06-30 00:05:48 +1000352 datafellows = odatafellows;
Damien Millerad833b32000-08-23 10:46:23 +1000353}
354
355/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000356static void
Damien Millerad833b32000-08-23 10:46:23 +1000357process_remove_identity(SocketEntry *e, int version)
358{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000359 u_int blen, bits;
360 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100361 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000362 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100363
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000364 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000365 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100366 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000367 bits = buffer_get_int(&e->request);
368 buffer_get_bignum(&e->request, key->rsa->e);
369 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Damien Millerad833b32000-08-23 10:46:23 +1000371 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000372 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000373 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000374 break;
375 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000376 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000378 xfree(blob);
379 break;
380 }
381 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100382 Identity *id = lookup_identity(key, version);
383 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100384 /*
385 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100386 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000387 * the array, we actually free the key there and move
388 * all the entries between the empty slot and the end
389 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100390 */
Damien Millerad833b32000-08-23 10:46:23 +1000391 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100392 if (tab->nentries < 1)
393 fatal("process_remove_identity: "
394 "internal error: tab->nentries %d",
395 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100396 TAILQ_REMOVE(&tab->idlist, id, next);
397 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000398 tab->nentries--;
399 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100400 }
Damien Millerad833b32000-08-23 10:46:23 +1000401 key_free(key);
402 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000404 buffer_put_char(&e->output,
405 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000406}
407
Ben Lindstrombba81212001-06-25 05:01:22 +0000408static void
Damien Millerad833b32000-08-23 10:46:23 +1000409process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410{
Damien Millerad833b32000-08-23 10:46:23 +1000411 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100412 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Damien Miller95def091999-11-25 00:26:21 +1100414 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100415 for (id = TAILQ_FIRST(&tab->idlist); id;
416 id = TAILQ_FIRST(&tab->idlist)) {
417 TAILQ_REMOVE(&tab->idlist, id, next);
418 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100419 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
Damien Miller95def091999-11-25 00:26:21 +1100421 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000422 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
424 /* Send success. */
425 buffer_put_int(&e->output, 1);
426 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100427}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428
Darren Tucker2812dc92007-03-21 20:45:06 +1100429/* removes expired keys and returns number of seconds until the next expiry */
430static u_int
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000431reaper(void)
432{
Darren Tucker2812dc92007-03-21 20:45:06 +1100433 u_int deadline = 0, now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000434 Identity *id, *nxt;
435 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000436 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000437
438 for (version = 1; version < 3; version++) {
439 tab = idtab_lookup(version);
440 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
441 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100442 if (id->death == 0)
443 continue;
444 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100445 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000446 TAILQ_REMOVE(&tab->idlist, id, next);
447 free_identity(id);
448 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100449 } else
450 deadline = (deadline == 0) ? id->death :
451 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000452 }
453 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100454 if (deadline == 0 || deadline <= now)
455 return 0;
456 else
457 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000458}
459
460static void
Damien Millerad833b32000-08-23 10:46:23 +1000461process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100462{
Damien Millerad833b32000-08-23 10:46:23 +1000463 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000464 Identity *id;
Damien Miller6c711792003-01-24 11:36:23 +1100465 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000466 char *type_name, *comment;
467 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100468
Damien Millerad833b32000-08-23 10:46:23 +1000469 switch (version) {
470 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000472 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000473 buffer_get_bignum(&e->request, k->rsa->n);
474 buffer_get_bignum(&e->request, k->rsa->e);
475 buffer_get_bignum(&e->request, k->rsa->d);
476 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100477
Damien Millerad833b32000-08-23 10:46:23 +1000478 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000479 buffer_get_bignum(&e->request, k->rsa->q); /* p */
480 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100481
Damien Millerad833b32000-08-23 10:46:23 +1000482 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000483 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000484 break;
485 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000486 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000487 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100488 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000489 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 case KEY_DSA:
491 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000492 buffer_get_bignum2(&e->request, k->dsa->p);
493 buffer_get_bignum2(&e->request, k->dsa->q);
494 buffer_get_bignum2(&e->request, k->dsa->g);
495 buffer_get_bignum2(&e->request, k->dsa->pub_key);
496 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100497 break;
498 case KEY_RSA:
499 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000500 buffer_get_bignum2(&e->request, k->rsa->n);
501 buffer_get_bignum2(&e->request, k->rsa->e);
502 buffer_get_bignum2(&e->request, k->rsa->d);
503 buffer_get_bignum2(&e->request, k->rsa->iqmp);
504 buffer_get_bignum2(&e->request, k->rsa->p);
505 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100506
507 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000508 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100509 break;
510 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000511 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000512 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100513 }
Damien Millerad833b32000-08-23 10:46:23 +1000514 break;
515 }
Damien Millerc51d0732003-03-15 11:37:09 +1100516 /* enable blinding */
517 switch (k->type) {
518 case KEY_RSA:
519 case KEY_RSA1:
520 if (RSA_blinding_on(k->rsa, NULL) != 1) {
521 error("process_add_identity: RSA_blinding_on failed");
522 key_free(k);
523 goto send;
524 }
525 break;
526 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000527 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000528 if (k == NULL) {
529 xfree(comment);
530 goto send;
531 }
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000532 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000533 switch ((type = buffer_get_char(&e->request))) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000534 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000535 death = time(NULL) + buffer_get_int(&e->request);
536 break;
Damien Miller6c711792003-01-24 11:36:23 +1100537 case SSH_AGENT_CONSTRAIN_CONFIRM:
538 confirm = 1;
539 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000540 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000541 error("process_add_identity: "
542 "Unknown constraint type %d", type);
543 xfree(comment);
544 key_free(k);
545 goto send;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000546 }
547 }
Damien Miller1cfadab2008-06-30 00:05:21 +1000548 success = 1;
Damien Miller53d81482003-01-22 11:47:19 +1100549 if (lifetime && !death)
550 death = time(NULL) + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000551 if ((id = lookup_identity(k, version)) == NULL) {
552 id = xmalloc(sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000553 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000554 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
555 /* Increment the number of identities. */
556 tab->nentries++;
557 } else {
558 key_free(k);
Damien Miller4c7728c2007-10-26 14:25:31 +1000559 xfree(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000560 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000561 id->comment = comment;
562 id->death = death;
563 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000564send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000565 buffer_put_int(&e->output, 1);
566 buffer_put_char(&e->output,
567 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
568}
569
Ben Lindstrom2f717042002-06-06 21:52:03 +0000570/* XXX todo: encrypt sensitive data with passphrase */
571static void
572process_lock_agent(SocketEntry *e, int lock)
573{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000574 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000575 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000576
577 passwd = buffer_get_string(&e->request, NULL);
578 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
579 locked = 0;
580 memset(lock_passwd, 0, strlen(lock_passwd));
581 xfree(lock_passwd);
582 lock_passwd = NULL;
583 success = 1;
584 } else if (!locked && lock) {
585 locked = 1;
586 lock_passwd = xstrdup(passwd);
587 success = 1;
588 }
589 memset(passwd, 0, strlen(passwd));
590 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000591
Ben Lindstrom2f717042002-06-06 21:52:03 +0000592 buffer_put_int(&e->output, 1);
593 buffer_put_char(&e->output,
594 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000595}
596
597static void
598no_identities(SocketEntry *e, u_int type)
599{
600 Buffer msg;
601
602 buffer_init(&msg);
603 buffer_put_char(&msg,
604 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
605 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
606 buffer_put_int(&msg, 0);
607 buffer_put_int(&e->output, buffer_len(&msg));
608 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
609 buffer_free(&msg);
610}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000611
612#ifdef SMARTCARD
613static void
Damien Miller1cfadab2008-06-30 00:05:21 +1000614process_add_smartcard_key(SocketEntry *e)
Ben Lindstrom3f471632001-07-04 03:53:15 +0000615{
Ben Lindstromba72d302002-03-22 03:51:06 +0000616 char *sc_reader_id = NULL, *pin;
Damien Miller1cfadab2008-06-30 00:05:21 +1000617 int i, type, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000618 Key **keys, *k;
619 Identity *id;
620 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100621
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000622 sc_reader_id = buffer_get_string(&e->request, NULL);
623 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000624
625 while (buffer_len(&e->request)) {
Damien Miller1cfadab2008-06-30 00:05:21 +1000626 switch ((type = buffer_get_char(&e->request))) {
Damien Millerd94f20d2003-06-11 22:06:33 +1000627 case SSH_AGENT_CONSTRAIN_LIFETIME:
628 death = time(NULL) + buffer_get_int(&e->request);
629 break;
630 case SSH_AGENT_CONSTRAIN_CONFIRM:
631 confirm = 1;
632 break;
633 default:
Damien Miller1cfadab2008-06-30 00:05:21 +1000634 error("process_add_smartcard_key: "
635 "Unknown constraint type %d", type);
636 xfree(sc_reader_id);
637 xfree(pin);
638 goto send;
Damien Millerd94f20d2003-06-11 22:06:33 +1000639 }
640 }
641 if (lifetime && !death)
642 death = time(NULL) + lifetime;
643
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000645 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000646 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000647
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000648 if (keys == NULL || keys[0] == NULL) {
649 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000650 goto send;
651 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000652 for (i = 0; keys[i] != NULL; i++) {
653 k = keys[i];
654 version = k->type == KEY_RSA1 ? 1 : 2;
655 tab = idtab_lookup(version);
656 if (lookup_identity(k, version) == NULL) {
657 id = xmalloc(sizeof(Identity));
658 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000659 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000660 id->death = death;
661 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000662 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
663 tab->nentries++;
664 success = 1;
665 } else {
666 key_free(k);
667 }
668 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000669 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000670 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000671send:
672 buffer_put_int(&e->output, 1);
673 buffer_put_char(&e->output,
674 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
675}
676
677static void
678process_remove_smartcard_key(SocketEntry *e)
679{
Ben Lindstromba72d302002-03-22 03:51:06 +0000680 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000681 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000682 Key **keys, *k = NULL;
683 Identity *id;
684 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000685
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000686 sc_reader_id = buffer_get_string(&e->request, NULL);
687 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000688 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000689 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000690 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000691
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000692 if (keys == NULL || keys[0] == NULL) {
693 error("sc_get_keys failed");
694 goto send;
695 }
696 for (i = 0; keys[i] != NULL; i++) {
697 k = keys[i];
698 version = k->type == KEY_RSA1 ? 1 : 2;
699 if ((id = lookup_identity(k, version)) != NULL) {
700 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000701 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000702 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100703 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000704 success = 1;
705 }
706 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000707 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000708 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000709 xfree(keys);
710send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000711 buffer_put_int(&e->output, 1);
712 buffer_put_char(&e->output,
713 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
714}
Ben Lindstromffce1472001-08-06 21:57:31 +0000715#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000716
Damien Millerad833b32000-08-23 10:46:23 +1000717/* dispatch incoming messages */
718
Ben Lindstrombba81212001-06-25 05:01:22 +0000719static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000720process_message(SocketEntry *e)
721{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000722 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000723 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000724
Damien Miller95def091999-11-25 00:26:21 +1100725 if (buffer_len(&e->input) < 5)
726 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100727 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100728 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100729 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000730 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100731 return;
732 }
733 if (buffer_len(&e->input) < msg_len + 4)
734 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000735
736 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100737 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000738 buffer_clear(&e->request);
739 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
740 buffer_consume(&e->input, msg_len);
741 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000742
Ben Lindstrom2f717042002-06-06 21:52:03 +0000743 /* check wheter agent is locked */
744 if (locked && type != SSH_AGENTC_UNLOCK) {
745 buffer_clear(&e->request);
746 switch (type) {
747 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
748 case SSH2_AGENTC_REQUEST_IDENTITIES:
749 /* send empty lists */
750 no_identities(e, type);
751 break;
752 default:
753 /* send a fail message for all other request types */
754 buffer_put_int(&e->output, 1);
755 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
756 }
757 return;
758 }
759
Ben Lindstrom3f471632001-07-04 03:53:15 +0000760 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100761 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000762 case SSH_AGENTC_LOCK:
763 case SSH_AGENTC_UNLOCK:
764 process_lock_agent(e, type == SSH_AGENTC_LOCK);
765 break;
Damien Millerad833b32000-08-23 10:46:23 +1000766 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100767 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000768 process_authentication_challenge1(e);
769 break;
770 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
771 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100772 break;
773 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000774 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000775 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100776 break;
777 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000778 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100779 break;
780 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000781 process_remove_all_identities(e, 1);
782 break;
783 /* ssh2 */
784 case SSH2_AGENTC_SIGN_REQUEST:
785 process_sign_request2(e);
786 break;
787 case SSH2_AGENTC_REQUEST_IDENTITIES:
788 process_request_identities(e, 2);
789 break;
790 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000791 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000792 process_add_identity(e, 2);
793 break;
794 case SSH2_AGENTC_REMOVE_IDENTITY:
795 process_remove_identity(e, 2);
796 break;
797 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
798 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100799 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000800#ifdef SMARTCARD
801 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000802 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000803 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100804 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000805 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
806 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100807 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000808#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100809 default:
810 /* Unknown message. Respond with failure. */
811 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000812 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100813 buffer_put_int(&e->output, 1);
814 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
815 break;
816 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817}
818
Ben Lindstrombba81212001-06-25 05:01:22 +0000819static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000820new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000821{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000822 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000823
Damien Miller232711f2004-06-15 10:35:30 +1000824 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000825
Damien Miller95def091999-11-25 00:26:21 +1100826 if (fd > max_fd)
827 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828
Damien Miller95def091999-11-25 00:26:21 +1100829 for (i = 0; i < sockets_alloc; i++)
830 if (sockets[i].type == AUTH_UNUSED) {
831 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100832 buffer_init(&sockets[i].input);
833 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000834 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000835 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100836 return;
837 }
838 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000839 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100840 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000841 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100842 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000843 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100844 sockets[old_alloc].fd = fd;
845 buffer_init(&sockets[old_alloc].input);
846 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000847 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000848 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000849}
850
Ben Lindstrombba81212001-06-25 05:01:22 +0000851static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100852prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
853 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000854{
Darren Tucker2812dc92007-03-21 20:45:06 +1100855 u_int i, sz, deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000856 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100857 static struct timeval tv;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000858
859 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100860 switch (sockets[i].type) {
861 case AUTH_SOCKET:
862 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000863 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100864 break;
865 case AUTH_UNUSED:
866 break;
867 default:
868 fatal("Unknown socket type %d", sockets[i].type);
869 break;
870 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000871 }
872
873 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000874 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000875 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000876 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000877 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000878 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000879 *fdrp = xmalloc(sz);
880 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000881 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000882 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000883 if (n < *fdl)
884 debug("XXX shrink: %d < %d", n, *fdl);
885 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000886 memset(*fdrp, 0, sz);
887 memset(*fdwp, 0, sz);
888
889 for (i = 0; i < sockets_alloc; i++) {
890 switch (sockets[i].type) {
891 case AUTH_SOCKET:
892 case AUTH_CONNECTION:
893 FD_SET(sockets[i].fd, *fdrp);
894 if (buffer_len(&sockets[i].output) > 0)
895 FD_SET(sockets[i].fd, *fdwp);
896 break;
897 default:
898 break;
899 }
900 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100901 deadline = reaper();
902 if (parent_alive_interval != 0)
903 deadline = (deadline == 0) ? parent_alive_interval :
904 MIN(deadline, parent_alive_interval);
905 if (deadline == 0) {
906 *tvpp = NULL;
907 } else {
908 tv.tv_sec = deadline;
909 tv.tv_usec = 0;
910 *tvpp = &tv;
911 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000912 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000913}
914
Ben Lindstrombba81212001-06-25 05:01:22 +0000915static void
Damien Miller95def091999-11-25 00:26:21 +1100916after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000917{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000918 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100919 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100920 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000921 int len, sock;
922 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000923 uid_t euid;
924 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000925
Damien Miller95def091999-11-25 00:26:21 +1100926 for (i = 0; i < sockets_alloc; i++)
927 switch (sockets[i].type) {
928 case AUTH_UNUSED:
929 break;
930 case AUTH_SOCKET:
931 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100932 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000933 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100934 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100935 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100936 error("accept from AUTH_SOCKET: %s",
937 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100938 break;
939 }
Damien Miller538f1812002-09-12 09:51:10 +1000940 if (getpeereid(sock, &euid, &egid) < 0) {
941 error("getpeereid %d failed: %s",
942 sock, strerror(errno));
943 close(sock);
944 break;
945 }
Damien Milleraf9de382002-10-03 11:54:35 +1000946 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000947 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000948 "peer euid %u != uid %u",
949 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000950 close(sock);
951 break;
952 }
Damien Miller95def091999-11-25 00:26:21 +1100953 new_socket(AUTH_CONNECTION, sock);
954 }
955 break;
956 case AUTH_CONNECTION:
957 if (buffer_len(&sockets[i].output) > 0 &&
958 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000959 do {
960 len = write(sockets[i].fd,
961 buffer_ptr(&sockets[i].output),
962 buffer_len(&sockets[i].output));
963 if (len == -1 && (errno == EAGAIN ||
Damien Millerd8968ad2008-07-04 23:10:49 +1000964 errno == EINTR ||
965 errno == EWOULDBLOCK))
Ben Lindstromb3144e52001-03-06 03:31:34 +0000966 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_consume(&sockets[i].output, len);
974 }
975 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000976 do {
977 len = read(sockets[i].fd, buf, sizeof(buf));
978 if (len == -1 && (errno == EAGAIN ||
Damien Millerd8968ad2008-07-04 23:10:49 +1000979 errno == EINTR ||
980 errno == EWOULDBLOCK))
Ben Lindstromb3144e52001-03-06 03:31:34 +0000981 continue;
982 break;
983 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100984 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000985 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100986 break;
987 }
988 buffer_append(&sockets[i].input, buf, len);
989 process_message(&sockets[i]);
990 }
991 break;
992 default:
993 fatal("Unknown type %d", sockets[i].type);
994 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000995}
996
Ben Lindstrombba81212001-06-25 05:01:22 +0000997static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000998cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000999{
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001000 if (socket_name[0])
1001 unlink(socket_name);
1002 if (socket_dir[0])
1003 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001004}
1005
Darren Tucker3e33cec2003-10-02 16:12:36 +10001006void
Damien Miller792c5111999-10-29 09:47:09 +10001007cleanup_exit(int i)
1008{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001009 cleanup_socket();
1010 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +10001011}
1012
Damien Miller1c13bd82006-03-26 14:28:14 +11001013/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001014static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001015cleanup_handler(int sig)
1016{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001017 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001018 _exit(2);
1019}
1020
Ben Lindstrombba81212001-06-25 05:01:22 +00001021static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001022check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001023{
Ben Lindstrom0250da02001-07-22 20:44:00 +00001024 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1025 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001026 cleanup_socket();
1027 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001028 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001029}
1030
1031static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001032usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001033{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001034 fprintf(stderr, "usage: %s [options] [command [arg ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001035 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001036 fprintf(stderr, "Options:\n");
1037 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1038 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1039 fprintf(stderr, " -k Kill the current agent.\n");
1040 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001041 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001042 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001043 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001044}
1045
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046int
1047main(int ac, char **av)
1048{
Damien Miller6c711792003-01-24 11:36:23 +11001049 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001050 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001051 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001052 char *shell, *format, *pidstr, *agentsocket = NULL;
1053 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001054 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001055#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001056 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001057#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001058 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001059 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001060 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001061 pid_t pid;
1062 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001063 struct timeval *tvp = NULL;
Darren Tucker90133232009-06-21 17:50:15 +10001064 size_t len;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001065
Darren Tuckerce321d82005-10-03 18:11:24 +10001066 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1067 sanitise_stdfd();
1068
Damien Miller6cffb9a2002-09-04 16:20:26 +10001069 /* drop */
1070 setegid(getgid());
1071 setgid(getgid());
1072
Damien Miller6c4914a2004-03-03 11:08:59 +11001073#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1074 /* Disable ptrace on Linux without sgid bit */
1075 prctl(PR_SET_DUMPABLE, 0);
1076#endif
1077
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001078 SSLeay_add_all_algorithms();
1079
Damien Miller59d3d5b2003-08-22 09:34:41 +10001080 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001081 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001082 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001083
Damien Miller53d81482003-01-22 11:47:19 +11001084 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001085 switch (ch) {
1086 case 'c':
1087 if (s_flag)
1088 usage();
1089 c_flag++;
1090 break;
1091 case 'k':
1092 k_flag++;
1093 break;
1094 case 's':
1095 if (c_flag)
1096 usage();
1097 s_flag++;
1098 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001099 case 'd':
1100 if (d_flag)
1101 usage();
1102 d_flag++;
1103 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001104 case 'a':
1105 agentsocket = optarg;
1106 break;
Damien Miller53d81482003-01-22 11:47:19 +11001107 case 't':
1108 if ((lifetime = convtime(optarg)) == -1) {
1109 fprintf(stderr, "Invalid lifetime\n");
1110 usage();
1111 }
1112 break;
Damien Miller95def091999-11-25 00:26:21 +11001113 default:
1114 usage();
1115 }
Damien Miller792c5111999-10-29 09:47:09 +10001116 }
Damien Miller95def091999-11-25 00:26:21 +11001117 ac -= optind;
1118 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001119
Ben Lindstromd94580c2001-07-04 03:48:02 +00001120 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001121 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001122
Ben Lindstromeecdf232002-04-02 21:03:51 +00001123 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001124 shell = getenv("SHELL");
Darren Tucker90133232009-06-21 17:50:15 +10001125 if (shell != NULL && (len = strlen(shell)) > 2 &&
1126 strncmp(shell + len - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001127 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001128 }
Damien Miller95def091999-11-25 00:26:21 +11001129 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001130 const char *errstr = NULL;
1131
Damien Miller95def091999-11-25 00:26:21 +11001132 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1133 if (pidstr == NULL) {
1134 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001135 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001136 exit(1);
1137 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001138 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1139 if (errstr) {
1140 fprintf(stderr,
1141 "%s=\"%s\", which is not a good PID: %s\n",
1142 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001143 exit(1);
1144 }
1145 if (kill(pid, SIGTERM) == -1) {
1146 perror("kill");
1147 exit(1);
1148 }
1149 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1150 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1151 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001152 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001153 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001154 }
Damien Miller95def091999-11-25 00:26:21 +11001155 parent_pid = getpid();
1156
Ben Lindstromb7788f32002-06-06 21:46:08 +00001157 if (agentsocket == NULL) {
1158 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001159 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001160 if (mkdtemp(socket_dir) == NULL) {
1161 perror("mkdtemp: private socket dir");
1162 exit(1);
1163 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001164 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1165 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001166 } else {
1167 /* Try to use specified agent socket */
1168 socket_dir[0] = '\0';
1169 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001170 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171
Damien Miller5428f641999-11-25 11:54:57 +11001172 /*
1173 * Create socket early so it will exist before command gets run from
1174 * the parent.
1175 */
Damien Miller95def091999-11-25 00:26:21 +11001176 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1177 if (sock < 0) {
1178 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001179 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001180 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001181 }
Damien Miller95def091999-11-25 00:26:21 +11001182 memset(&sunaddr, 0, sizeof(sunaddr));
1183 sunaddr.sun_family = AF_UNIX;
1184 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001185 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001186 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001187 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001188 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001189 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001190 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001191 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001192 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001193 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001194 perror("listen");
1195 cleanup_exit(1);
1196 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001197
Damien Miller5428f641999-11-25 11:54:57 +11001198 /*
1199 * Fork, and have the parent execute the command, if any, or present
1200 * the socket data. The child continues as the authentication agent.
1201 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001202 if (d_flag) {
1203 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1204 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1205 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1206 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001207 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001208 goto skip;
1209 }
Damien Miller95def091999-11-25 00:26:21 +11001210 pid = fork();
1211 if (pid == -1) {
1212 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001213 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001214 }
1215 if (pid != 0) { /* Parent - execute the given command. */
1216 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001217 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001218 if (ac == 0) {
1219 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1220 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001221 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001222 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001223 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001224 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001225 exit(0);
1226 }
Damien Millere4340be2000-09-16 13:29:08 +11001227 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1228 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1229 perror("setenv");
1230 exit(1);
1231 }
Damien Miller95def091999-11-25 00:26:21 +11001232 execvp(av[0], av);
1233 perror(av[0]);
1234 exit(1);
1235 }
Damien Millerc653b892002-02-08 22:05:41 +11001236 /* child */
1237 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001238
1239 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001240 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001241 cleanup_exit(1);
1242 }
1243
1244 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001245 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1246 /* XXX might close listen socket */
1247 (void)dup2(fd, STDIN_FILENO);
1248 (void)dup2(fd, STDOUT_FILENO);
1249 (void)dup2(fd, STDERR_FILENO);
1250 if (fd > 2)
1251 close(fd);
1252 }
Damien Miller95def091999-11-25 00:26:21 +11001253
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001254#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001255 /* deny core dumps, since memory contains unencrypted private keys */
1256 rlim.rlim_cur = rlim.rlim_max = 0;
1257 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001258 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001259 cleanup_exit(1);
1260 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001261#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001262
1263skip:
Damien Miller95def091999-11-25 00:26:21 +11001264 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001265 if (ac > 0)
1266 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001267 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001268 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001269 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001270 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001271 signal(SIGHUP, cleanup_handler);
1272 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001273 nalloc = 0;
1274
Damien Miller95def091999-11-25 00:26:21 +11001275 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001276 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1277 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001278 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001279 if (parent_alive_interval != 0)
1280 check_parent_exists();
1281 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001282 if (result < 0) {
1283 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001284 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001285 fatal("select: %s", strerror(saved_errno));
1286 } else if (result > 0)
1287 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001288 }
1289 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290}