blob: 08b07212e048b3ac84c0a2b1ea4e5bf615a9e52a [file] [log] [blame]
Damien Miller9ab00b42006-08-05 12:40:11 +10001/* $OpenBSD: ssh-agent.c,v 1.152 2006/08/04 20:46:05 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <openssl/evp.h>
53#include <openssl/md5.h>
54
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100056#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110057#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110058# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110059#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110060#include <signal.h>
Damien 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;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
124/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100125char socket_name[MAXPATHLEN];
126char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Ben Lindstrom2f717042002-06-06 21:52:03 +0000128/* locking */
129int locked = 0;
130char *lock_passwd = NULL;
131
Damien Miller95def091999-11-25 00:26:21 +1100132extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100133
Damien Miller53d81482003-01-22 11:47:19 +1100134/* Default lifetime (0 == forever) */
135static int lifetime = 0;
136
Ben Lindstrombba81212001-06-25 05:01:22 +0000137static void
Damien Miller58f34862002-09-04 16:31:21 +1000138close_socket(SocketEntry *e)
139{
Damien Miller58f34862002-09-04 16:31:21 +1000140 close(e->fd);
141 e->fd = -1;
142 e->type = AUTH_UNUSED;
143 buffer_free(&e->input);
144 buffer_free(&e->output);
145 buffer_free(&e->request);
146}
147
148static void
Damien Millerad833b32000-08-23 10:46:23 +1000149idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Damien Millerad833b32000-08-23 10:46:23 +1000151 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000152
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000153 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100154 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000155 idtable[i].nentries = 0;
156 }
157}
158
159/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000161idtab_lookup(int version)
162{
163 if (version < 1 || version > 2)
164 fatal("internal error, bad protocol version %d", version);
165 return &idtable[version];
166}
167
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000168static void
169free_identity(Identity *id)
170{
171 key_free(id->key);
172 xfree(id->comment);
173 xfree(id);
174}
175
Damien Millerad833b32000-08-23 10:46:23 +1000176/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100177static Identity *
178lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000179{
Damien Miller1a534ae2002-01-22 23:26:13 +1100180 Identity *id;
181
Damien Millerad833b32000-08-23 10:46:23 +1000182 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100183 TAILQ_FOREACH(id, &tab->idlist, next) {
184 if (key_equal(key, id->key))
185 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000186 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100187 return (NULL);
188}
189
Damien Miller6c711792003-01-24 11:36:23 +1100190/* Check confirmation of keysign request */
191static int
192confirm_key(Identity *id)
193{
Darren Tuckerce327b62004-11-05 20:38:03 +1100194 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100195 int ret = -1;
196
197 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100198 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
199 id->comment, p))
200 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100201 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100202
Damien Miller6c711792003-01-24 11:36:23 +1100203 return (ret);
204}
205
Damien Millerad833b32000-08-23 10:46:23 +1000206/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000207static void
Damien Millerad833b32000-08-23 10:46:23 +1000208process_request_identities(SocketEntry *e, int version)
209{
210 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100211 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000212 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000215 buffer_put_char(&msg, (version == 1) ?
216 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
217 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100218 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100219 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000220 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
221 buffer_put_bignum(&msg, id->key->rsa->e);
222 buffer_put_bignum(&msg, id->key->rsa->n);
223 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000224 u_char *blob;
225 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100226 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000227 buffer_put_string(&msg, blob, blen);
228 xfree(blob);
229 }
230 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100231 }
232 buffer_put_int(&e->output, buffer_len(&msg));
233 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
234 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
Damien Millerad833b32000-08-23 10:46:23 +1000237/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000238static void
Damien Millerad833b32000-08-23 10:46:23 +1000239process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000241 u_char buf[32], mdbuf[16], session_id[16];
242 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000243 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000244 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000245 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100246 Buffer msg;
247 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000248 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Damien Miller95def091999-11-25 00:26:21 +1100250 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100251 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100252 if ((challenge = BN_new()) == NULL)
253 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000254
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000255 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000256 buffer_get_bignum(&e->request, key->rsa->e);
257 buffer_get_bignum(&e->request, key->rsa->n);
258 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Millerad833b32000-08-23 10:46:23 +1000260 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000261 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000262 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000263 buffer_get(&e->request, session_id, 16);
264 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000265 if (response_type != 1)
266 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller1a534ae2002-01-22 23:26:13 +1100268 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100269 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100270 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000271 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100272 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
273 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100274
Damien Millerad833b32000-08-23 10:46:23 +1000275 /* The response is MD5 of decrypted challenge plus session id. */
276 len = BN_num_bytes(challenge);
277 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000278 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000279 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100280 }
Damien Millerad833b32000-08-23 10:46:23 +1000281 memset(buf, 0, 32);
282 BN_bn2bin(challenge, buf + 32 - len);
283 MD5_Init(&md);
284 MD5_Update(&md, buf, 32);
285 MD5_Update(&md, session_id, 16);
286 MD5_Final(mdbuf, &md);
287
288 /* Send the response. */
289 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
290 for (i = 0; i < 16; i++)
291 buffer_put_char(&msg, mdbuf[i]);
292 goto send;
293 }
294
295failure:
296 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100297 buffer_put_char(&msg, SSH_AGENT_FAILURE);
298send:
299 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000300 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
301 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100302 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000303 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000304}
305
Damien Millerad833b32000-08-23 10:46:23 +1000306/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000307static void
Damien Millerad833b32000-08-23 10:46:23 +1000308process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000310 u_char *blob, *data, *signature = NULL;
311 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000312 extern int datafellows;
313 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000314 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000315 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000316
317 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000318
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000319 blob = buffer_get_string(&e->request, &blen);
320 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100321
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000322 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100323 if (flags & SSH_AGENT_OLD_SIGNATURE)
324 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000325
Damien Miller0bc1bd82000-11-13 22:57:25 +1100326 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000327 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100328 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100329 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100330 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000331 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000332 }
Damien Millerad833b32000-08-23 10:46:23 +1000333 buffer_init(&msg);
334 if (ok == 0) {
335 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
336 buffer_put_string(&msg, signature, slen);
337 } else {
338 buffer_put_char(&msg, SSH_AGENT_FAILURE);
339 }
340 buffer_put_int(&e->output, buffer_len(&msg));
341 buffer_append(&e->output, buffer_ptr(&msg),
342 buffer_len(&msg));
343 buffer_free(&msg);
344 xfree(data);
345 xfree(blob);
346 if (signature != NULL)
347 xfree(signature);
348}
349
350/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000351static void
Damien Millerad833b32000-08-23 10:46:23 +1000352process_remove_identity(SocketEntry *e, int version)
353{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000354 u_int blen, bits;
355 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100356 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000357 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100358
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000359 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000360 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100361 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000362 bits = buffer_get_int(&e->request);
363 buffer_get_bignum(&e->request, key->rsa->e);
364 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000365
Damien Millerad833b32000-08-23 10:46:23 +1000366 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000367 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000368 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000369 break;
370 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000371 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100372 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000373 xfree(blob);
374 break;
375 }
376 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100377 Identity *id = lookup_identity(key, version);
378 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100379 /*
380 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100381 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000382 * the array, we actually free the key there and move
383 * all the entries between the empty slot and the end
384 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100385 */
Damien Millerad833b32000-08-23 10:46:23 +1000386 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100387 if (tab->nentries < 1)
388 fatal("process_remove_identity: "
389 "internal error: tab->nentries %d",
390 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100391 TAILQ_REMOVE(&tab->idlist, id, next);
392 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000393 tab->nentries--;
394 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100395 }
Damien Millerad833b32000-08-23 10:46:23 +1000396 key_free(key);
397 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000399 buffer_put_char(&e->output,
400 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
Ben Lindstrombba81212001-06-25 05:01:22 +0000403static void
Damien Millerad833b32000-08-23 10:46:23 +1000404process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405{
Damien Millerad833b32000-08-23 10:46:23 +1000406 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100407 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
Damien Miller95def091999-11-25 00:26:21 +1100409 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100410 for (id = TAILQ_FIRST(&tab->idlist); id;
411 id = TAILQ_FIRST(&tab->idlist)) {
412 TAILQ_REMOVE(&tab->idlist, id, next);
413 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100414 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000415
Damien Miller95def091999-11-25 00:26:21 +1100416 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000417 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000418
419 /* Send success. */
420 buffer_put_int(&e->output, 1);
421 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100422}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Ben Lindstrombba81212001-06-25 05:01:22 +0000424static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000425reaper(void)
426{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000427 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000428 Identity *id, *nxt;
429 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000430 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000431
432 for (version = 1; version < 3; version++) {
433 tab = idtab_lookup(version);
434 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
435 nxt = TAILQ_NEXT(id, next);
436 if (id->death != 0 && now >= id->death) {
437 TAILQ_REMOVE(&tab->idlist, id, next);
438 free_identity(id);
439 tab->nentries--;
440 }
441 }
442 }
443}
444
445static void
Damien Millerad833b32000-08-23 10:46:23 +1000446process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100447{
Damien Millerad833b32000-08-23 10:46:23 +1000448 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100449 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000450 char *type_name, *comment;
451 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100452
Damien Millerad833b32000-08-23 10:46:23 +1000453 switch (version) {
454 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100455 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000456 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000457 buffer_get_bignum(&e->request, k->rsa->n);
458 buffer_get_bignum(&e->request, k->rsa->e);
459 buffer_get_bignum(&e->request, k->rsa->d);
460 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100461
Damien Millerad833b32000-08-23 10:46:23 +1000462 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000463 buffer_get_bignum(&e->request, k->rsa->q); /* p */
464 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100465
Damien Millerad833b32000-08-23 10:46:23 +1000466 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000467 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000468 break;
469 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000470 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000471 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100472 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000473 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100474 case KEY_DSA:
475 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000476 buffer_get_bignum2(&e->request, k->dsa->p);
477 buffer_get_bignum2(&e->request, k->dsa->q);
478 buffer_get_bignum2(&e->request, k->dsa->g);
479 buffer_get_bignum2(&e->request, k->dsa->pub_key);
480 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100481 break;
482 case KEY_RSA:
483 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000484 buffer_get_bignum2(&e->request, k->rsa->n);
485 buffer_get_bignum2(&e->request, k->rsa->e);
486 buffer_get_bignum2(&e->request, k->rsa->d);
487 buffer_get_bignum2(&e->request, k->rsa->iqmp);
488 buffer_get_bignum2(&e->request, k->rsa->p);
489 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490
491 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000492 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 break;
494 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000495 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000496 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100497 }
Damien Millerad833b32000-08-23 10:46:23 +1000498 break;
499 }
Damien Millerc51d0732003-03-15 11:37:09 +1100500 /* enable blinding */
501 switch (k->type) {
502 case KEY_RSA:
503 case KEY_RSA1:
504 if (RSA_blinding_on(k->rsa, NULL) != 1) {
505 error("process_add_identity: RSA_blinding_on failed");
506 key_free(k);
507 goto send;
508 }
509 break;
510 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000511 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000512 if (k == NULL) {
513 xfree(comment);
514 goto send;
515 }
516 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000517 while (buffer_len(&e->request)) {
518 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000519 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000520 death = time(NULL) + buffer_get_int(&e->request);
521 break;
Damien Miller6c711792003-01-24 11:36:23 +1100522 case SSH_AGENT_CONSTRAIN_CONFIRM:
523 confirm = 1;
524 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000525 default:
526 break;
527 }
528 }
Damien Miller53d81482003-01-22 11:47:19 +1100529 if (lifetime && !death)
530 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000531 if (lookup_identity(k, version) == NULL) {
532 Identity *id = xmalloc(sizeof(Identity));
533 id->key = k;
534 id->comment = comment;
535 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100536 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000537 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
538 /* Increment the number of identities. */
539 tab->nentries++;
540 } else {
541 key_free(k);
542 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000543 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000544send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000545 buffer_put_int(&e->output, 1);
546 buffer_put_char(&e->output,
547 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
548}
549
Ben Lindstrom2f717042002-06-06 21:52:03 +0000550/* XXX todo: encrypt sensitive data with passphrase */
551static void
552process_lock_agent(SocketEntry *e, int lock)
553{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000554 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000555 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000556
557 passwd = buffer_get_string(&e->request, NULL);
558 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
559 locked = 0;
560 memset(lock_passwd, 0, strlen(lock_passwd));
561 xfree(lock_passwd);
562 lock_passwd = NULL;
563 success = 1;
564 } else if (!locked && lock) {
565 locked = 1;
566 lock_passwd = xstrdup(passwd);
567 success = 1;
568 }
569 memset(passwd, 0, strlen(passwd));
570 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000571
Ben Lindstrom2f717042002-06-06 21:52:03 +0000572 buffer_put_int(&e->output, 1);
573 buffer_put_char(&e->output,
574 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000575}
576
577static void
578no_identities(SocketEntry *e, u_int type)
579{
580 Buffer msg;
581
582 buffer_init(&msg);
583 buffer_put_char(&msg,
584 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
585 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
586 buffer_put_int(&msg, 0);
587 buffer_put_int(&e->output, buffer_len(&msg));
588 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
589 buffer_free(&msg);
590}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000591
592#ifdef SMARTCARD
593static void
594process_add_smartcard_key (SocketEntry *e)
595{
Ben Lindstromba72d302002-03-22 03:51:06 +0000596 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000597 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000598 Key **keys, *k;
599 Identity *id;
600 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100601
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000602 sc_reader_id = buffer_get_string(&e->request, NULL);
603 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000604
605 while (buffer_len(&e->request)) {
606 switch (buffer_get_char(&e->request)) {
607 case SSH_AGENT_CONSTRAIN_LIFETIME:
608 death = time(NULL) + buffer_get_int(&e->request);
609 break;
610 case SSH_AGENT_CONSTRAIN_CONFIRM:
611 confirm = 1;
612 break;
613 default:
614 break;
615 }
616 }
617 if (lifetime && !death)
618 death = time(NULL) + lifetime;
619
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000620 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000621 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000622 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000623
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000624 if (keys == NULL || keys[0] == NULL) {
625 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000626 goto send;
627 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000628 for (i = 0; keys[i] != NULL; i++) {
629 k = keys[i];
630 version = k->type == KEY_RSA1 ? 1 : 2;
631 tab = idtab_lookup(version);
632 if (lookup_identity(k, version) == NULL) {
633 id = xmalloc(sizeof(Identity));
634 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000635 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000636 id->death = death;
637 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000638 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
639 tab->nentries++;
640 success = 1;
641 } else {
642 key_free(k);
643 }
644 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000645 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000646 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000647send:
648 buffer_put_int(&e->output, 1);
649 buffer_put_char(&e->output,
650 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
651}
652
653static void
654process_remove_smartcard_key(SocketEntry *e)
655{
Ben Lindstromba72d302002-03-22 03:51:06 +0000656 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000657 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000658 Key **keys, *k = NULL;
659 Identity *id;
660 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000661
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000662 sc_reader_id = buffer_get_string(&e->request, NULL);
663 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000664 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000665 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000666 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000667
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000668 if (keys == NULL || keys[0] == NULL) {
669 error("sc_get_keys failed");
670 goto send;
671 }
672 for (i = 0; keys[i] != NULL; i++) {
673 k = keys[i];
674 version = k->type == KEY_RSA1 ? 1 : 2;
675 if ((id = lookup_identity(k, version)) != NULL) {
676 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000677 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000678 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100679 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000680 success = 1;
681 }
682 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000683 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000684 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000685 xfree(keys);
686send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000687 buffer_put_int(&e->output, 1);
688 buffer_put_char(&e->output,
689 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
690}
Ben Lindstromffce1472001-08-06 21:57:31 +0000691#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000692
Damien Millerad833b32000-08-23 10:46:23 +1000693/* dispatch incoming messages */
694
Ben Lindstrombba81212001-06-25 05:01:22 +0000695static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000696process_message(SocketEntry *e)
697{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000698 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000699 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000700
701 /* kill dead keys */
702 reaper();
703
Damien Miller95def091999-11-25 00:26:21 +1100704 if (buffer_len(&e->input) < 5)
705 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100706 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100707 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100708 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000709 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100710 return;
711 }
712 if (buffer_len(&e->input) < msg_len + 4)
713 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000714
715 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100716 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000717 buffer_clear(&e->request);
718 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
719 buffer_consume(&e->input, msg_len);
720 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000721
Ben Lindstrom2f717042002-06-06 21:52:03 +0000722 /* check wheter agent is locked */
723 if (locked && type != SSH_AGENTC_UNLOCK) {
724 buffer_clear(&e->request);
725 switch (type) {
726 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
727 case SSH2_AGENTC_REQUEST_IDENTITIES:
728 /* send empty lists */
729 no_identities(e, type);
730 break;
731 default:
732 /* send a fail message for all other request types */
733 buffer_put_int(&e->output, 1);
734 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
735 }
736 return;
737 }
738
Ben Lindstrom3f471632001-07-04 03:53:15 +0000739 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100740 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000741 case SSH_AGENTC_LOCK:
742 case SSH_AGENTC_UNLOCK:
743 process_lock_agent(e, type == SSH_AGENTC_LOCK);
744 break;
Damien Millerad833b32000-08-23 10:46:23 +1000745 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100746 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000747 process_authentication_challenge1(e);
748 break;
749 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
750 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100751 break;
752 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000753 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000754 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100755 break;
756 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000757 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100758 break;
759 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000760 process_remove_all_identities(e, 1);
761 break;
762 /* ssh2 */
763 case SSH2_AGENTC_SIGN_REQUEST:
764 process_sign_request2(e);
765 break;
766 case SSH2_AGENTC_REQUEST_IDENTITIES:
767 process_request_identities(e, 2);
768 break;
769 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000770 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000771 process_add_identity(e, 2);
772 break;
773 case SSH2_AGENTC_REMOVE_IDENTITY:
774 process_remove_identity(e, 2);
775 break;
776 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
777 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100778 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000779#ifdef SMARTCARD
780 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000781 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000782 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100783 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000784 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
785 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100786 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000787#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100788 default:
789 /* Unknown message. Respond with failure. */
790 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000791 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100792 buffer_put_int(&e->output, 1);
793 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
794 break;
795 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000796}
797
Ben Lindstrombba81212001-06-25 05:01:22 +0000798static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000799new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000800{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000801 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000802
Damien Miller232711f2004-06-15 10:35:30 +1000803 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000804
Damien Miller95def091999-11-25 00:26:21 +1100805 if (fd > max_fd)
806 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000807
Damien Miller95def091999-11-25 00:26:21 +1100808 for (i = 0; i < sockets_alloc; i++)
809 if (sockets[i].type == AUTH_UNUSED) {
810 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100811 buffer_init(&sockets[i].input);
812 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000813 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000814 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100815 return;
816 }
817 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000818 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100819 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000820 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100821 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000822 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100823 sockets[old_alloc].fd = fd;
824 buffer_init(&sockets[old_alloc].input);
825 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000826 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000827 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000828}
829
Ben Lindstrombba81212001-06-25 05:01:22 +0000830static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000831prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000832{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000833 u_int i, sz;
834 int n = 0;
835
836 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100837 switch (sockets[i].type) {
838 case AUTH_SOCKET:
839 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000840 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100841 break;
842 case AUTH_UNUSED:
843 break;
844 default:
845 fatal("Unknown socket type %d", sockets[i].type);
846 break;
847 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000848 }
849
850 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000851 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000853 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000854 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000855 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000856 *fdrp = xmalloc(sz);
857 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000858 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000859 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000860 if (n < *fdl)
861 debug("XXX shrink: %d < %d", n, *fdl);
862 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000863 memset(*fdrp, 0, sz);
864 memset(*fdwp, 0, sz);
865
866 for (i = 0; i < sockets_alloc; i++) {
867 switch (sockets[i].type) {
868 case AUTH_SOCKET:
869 case AUTH_CONNECTION:
870 FD_SET(sockets[i].fd, *fdrp);
871 if (buffer_len(&sockets[i].output) > 0)
872 FD_SET(sockets[i].fd, *fdwp);
873 break;
874 default:
875 break;
876 }
877 }
878 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879}
880
Ben Lindstrombba81212001-06-25 05:01:22 +0000881static void
Damien Miller95def091999-11-25 00:26:21 +1100882after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000883{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000884 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100885 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100886 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000887 int len, sock;
888 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000889 uid_t euid;
890 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891
Damien Miller95def091999-11-25 00:26:21 +1100892 for (i = 0; i < sockets_alloc; i++)
893 switch (sockets[i].type) {
894 case AUTH_UNUSED:
895 break;
896 case AUTH_SOCKET:
897 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100898 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000899 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100900 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100901 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100902 error("accept from AUTH_SOCKET: %s",
903 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100904 break;
905 }
Damien Miller538f1812002-09-12 09:51:10 +1000906 if (getpeereid(sock, &euid, &egid) < 0) {
907 error("getpeereid %d failed: %s",
908 sock, strerror(errno));
909 close(sock);
910 break;
911 }
Damien Milleraf9de382002-10-03 11:54:35 +1000912 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000913 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000914 "peer euid %u != uid %u",
915 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000916 close(sock);
917 break;
918 }
Damien Miller95def091999-11-25 00:26:21 +1100919 new_socket(AUTH_CONNECTION, sock);
920 }
921 break;
922 case AUTH_CONNECTION:
923 if (buffer_len(&sockets[i].output) > 0 &&
924 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000925 do {
926 len = write(sockets[i].fd,
927 buffer_ptr(&sockets[i].output),
928 buffer_len(&sockets[i].output));
929 if (len == -1 && (errno == EAGAIN ||
930 errno == EINTR))
931 continue;
932 break;
933 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100934 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000935 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100936 break;
937 }
938 buffer_consume(&sockets[i].output, len);
939 }
940 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000941 do {
942 len = read(sockets[i].fd, buf, sizeof(buf));
943 if (len == -1 && (errno == EAGAIN ||
944 errno == EINTR))
945 continue;
946 break;
947 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100948 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000949 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100950 break;
951 }
952 buffer_append(&sockets[i].input, buf, len);
953 process_message(&sockets[i]);
954 }
955 break;
956 default:
957 fatal("Unknown type %d", sockets[i].type);
958 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000959}
960
Ben Lindstrombba81212001-06-25 05:01:22 +0000961static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000962cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000963{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000964 if (socket_name[0])
965 unlink(socket_name);
966 if (socket_dir[0])
967 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000968}
969
Darren Tucker3e33cec2003-10-02 16:12:36 +1000970void
Damien Miller792c5111999-10-29 09:47:09 +1000971cleanup_exit(int i)
972{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000973 cleanup_socket();
974 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000975}
976
Damien Miller1c13bd82006-03-26 14:28:14 +1100977/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000978static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000979cleanup_handler(int sig)
980{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000981 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000982 _exit(2);
983}
984
Damien Miller1c13bd82006-03-26 14:28:14 +1100985/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000986static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000987check_parent_exists(int sig)
988{
989 int save_errno = errno;
990
991 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
992 /* printf("Parent has died - Authentication agent exiting.\n"); */
993 cleanup_handler(sig); /* safe */
994 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000995 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000996 alarm(10);
997 errno = save_errno;
998}
999
1000static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001001usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001002{
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001003 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001004 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001005 fprintf(stderr, "Options:\n");
1006 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1007 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1008 fprintf(stderr, " -k Kill the current agent.\n");
1009 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001010 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001011 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001012 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001013}
1014
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001015int
1016main(int ac, char **av)
1017{
Damien Miller6c711792003-01-24 11:36:23 +11001018 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Damien Miller9ab00b42006-08-05 12:40:11 +10001019 int sock, fd, ch;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001020 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001021 char *shell, *format, *pidstr, *agentsocket = NULL;
1022 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001023 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001024#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001025 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001026#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001027 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001028 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001029 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001030 pid_t pid;
1031 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001032
Darren Tuckerce321d82005-10-03 18:11:24 +10001033 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1034 sanitise_stdfd();
1035
Damien Miller6cffb9a2002-09-04 16:20:26 +10001036 /* drop */
1037 setegid(getgid());
1038 setgid(getgid());
1039
Damien Miller6c4914a2004-03-03 11:08:59 +11001040#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1041 /* Disable ptrace on Linux without sgid bit */
1042 prctl(PR_SET_DUMPABLE, 0);
1043#endif
1044
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001045 SSLeay_add_all_algorithms();
1046
Damien Miller59d3d5b2003-08-22 09:34:41 +10001047 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001048 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001049 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001050
Damien Miller53d81482003-01-22 11:47:19 +11001051 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001052 switch (ch) {
1053 case 'c':
1054 if (s_flag)
1055 usage();
1056 c_flag++;
1057 break;
1058 case 'k':
1059 k_flag++;
1060 break;
1061 case 's':
1062 if (c_flag)
1063 usage();
1064 s_flag++;
1065 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001066 case 'd':
1067 if (d_flag)
1068 usage();
1069 d_flag++;
1070 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001071 case 'a':
1072 agentsocket = optarg;
1073 break;
Damien Miller53d81482003-01-22 11:47:19 +11001074 case 't':
1075 if ((lifetime = convtime(optarg)) == -1) {
1076 fprintf(stderr, "Invalid lifetime\n");
1077 usage();
1078 }
1079 break;
Damien Miller95def091999-11-25 00:26:21 +11001080 default:
1081 usage();
1082 }
Damien Miller792c5111999-10-29 09:47:09 +10001083 }
Damien Miller95def091999-11-25 00:26:21 +11001084 ac -= optind;
1085 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001086
Ben Lindstromd94580c2001-07-04 03:48:02 +00001087 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001088 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001089
Ben Lindstromeecdf232002-04-02 21:03:51 +00001090 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001091 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001092 if (shell != NULL &&
1093 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001094 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001095 }
Damien Miller95def091999-11-25 00:26:21 +11001096 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001097 const char *errstr = NULL;
1098
Damien Miller95def091999-11-25 00:26:21 +11001099 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1100 if (pidstr == NULL) {
1101 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001102 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001103 exit(1);
1104 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001105 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1106 if (errstr) {
1107 fprintf(stderr,
1108 "%s=\"%s\", which is not a good PID: %s\n",
1109 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001110 exit(1);
1111 }
1112 if (kill(pid, SIGTERM) == -1) {
1113 perror("kill");
1114 exit(1);
1115 }
1116 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1117 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1118 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001119 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001120 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001121 }
Damien Miller95def091999-11-25 00:26:21 +11001122 parent_pid = getpid();
1123
Ben Lindstromb7788f32002-06-06 21:46:08 +00001124 if (agentsocket == NULL) {
1125 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001126 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001127 if (mkdtemp(socket_dir) == NULL) {
1128 perror("mkdtemp: private socket dir");
1129 exit(1);
1130 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001131 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1132 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001133 } else {
1134 /* Try to use specified agent socket */
1135 socket_dir[0] = '\0';
1136 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001137 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001138
Damien Miller5428f641999-11-25 11:54:57 +11001139 /*
1140 * Create socket early so it will exist before command gets run from
1141 * the parent.
1142 */
Damien Miller95def091999-11-25 00:26:21 +11001143 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1144 if (sock < 0) {
1145 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001146 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001147 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001148 }
Damien Miller95def091999-11-25 00:26:21 +11001149 memset(&sunaddr, 0, sizeof(sunaddr));
1150 sunaddr.sun_family = AF_UNIX;
1151 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001152 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001153 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001154 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001155 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001156 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001157 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001159 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001160 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001161 perror("listen");
1162 cleanup_exit(1);
1163 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001164
Damien Miller5428f641999-11-25 11:54:57 +11001165 /*
1166 * Fork, and have the parent execute the command, if any, or present
1167 * the socket data. The child continues as the authentication agent.
1168 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001169 if (d_flag) {
1170 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1171 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1172 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1173 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001174 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001175 goto skip;
1176 }
Damien Miller95def091999-11-25 00:26:21 +11001177 pid = fork();
1178 if (pid == -1) {
1179 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001180 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001181 }
1182 if (pid != 0) { /* Parent - execute the given command. */
1183 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001184 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001185 if (ac == 0) {
1186 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1187 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001188 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001189 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001190 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001191 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001192 exit(0);
1193 }
Damien Millere4340be2000-09-16 13:29:08 +11001194 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1195 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1196 perror("setenv");
1197 exit(1);
1198 }
Damien Miller95def091999-11-25 00:26:21 +11001199 execvp(av[0], av);
1200 perror(av[0]);
1201 exit(1);
1202 }
Damien Millerc653b892002-02-08 22:05:41 +11001203 /* child */
1204 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001205
1206 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001207 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001208 cleanup_exit(1);
1209 }
1210
1211 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001212 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1213 /* XXX might close listen socket */
1214 (void)dup2(fd, STDIN_FILENO);
1215 (void)dup2(fd, STDOUT_FILENO);
1216 (void)dup2(fd, STDERR_FILENO);
1217 if (fd > 2)
1218 close(fd);
1219 }
Damien Miller95def091999-11-25 00:26:21 +11001220
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001221#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001222 /* deny core dumps, since memory contains unencrypted private keys */
1223 rlim.rlim_cur = rlim.rlim_max = 0;
1224 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001225 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001226 cleanup_exit(1);
1227 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001228#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001229
1230skip:
Damien Miller95def091999-11-25 00:26:21 +11001231 new_socket(AUTH_SOCKET, sock);
1232 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001233 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001234 alarm(10);
1235 }
Damien Millerad833b32000-08-23 10:46:23 +10001236 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001237 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001238 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001239 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001240 signal(SIGHUP, cleanup_handler);
1241 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001242 nalloc = 0;
1243
Damien Miller95def091999-11-25 00:26:21 +11001244 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001245 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001246 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001247 if (errno == EINTR)
1248 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001249 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001250 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001251 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001252 }
1253 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254}