blob: 57c7380f3db0990e40220741562ffd5c6b10a694 [file] [log] [blame]
Damien Miller57cf6382006-07-10 21:13:46 +10001/* $OpenBSD: ssh-agent.c,v 1.142 2006/07/09 15:15:11 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 Miller42fb0682006-03-15 14:03:06 +110040#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100041#include <sys/socket.h>
Damien Miller574c41f2006-03-15 11:40:10 +110042#ifdef HAVE_SYS_UN_H
43# include <sys/un.h>
44#endif
Damien Miller9b481512002-09-12 10:43:29 +100045#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110046#include <sys/resource.h>
Damien Millere3b60b52006-07-10 21:08:03 +100047
Damien Miller57cf6382006-07-10 21:13:46 +100048#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110049#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110050# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110051#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110052#include <signal.h>
53
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include <openssl/evp.h>
55#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
57#include "ssh.h"
58#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059#include "buffer.h"
60#include "bufaux.h"
61#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "key.h"
63#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110064#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000065#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110066#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Ben Lindstrom3f471632001-07-04 03:53:15 +000068#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000069#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000070#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000071
Damien Miller6c4914a2004-03-03 11:08:59 +110072#if defined(HAVE_SYS_PRCTL_H)
73#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
74#endif
75
Ben Lindstrom65366a82001-12-06 16:32:47 +000076typedef enum {
77 AUTH_UNUSED,
78 AUTH_SOCKET,
79 AUTH_CONNECTION
80} sock_type;
81
Damien Miller95def091999-11-25 00:26:21 +110082typedef struct {
83 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000084 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110085 Buffer input;
86 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000087 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088} SocketEntry;
89
Ben Lindstrom46c16222000-12-22 01:43:59 +000090u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091SocketEntry *sockets = NULL;
92
Damien Miller1a534ae2002-01-22 23:26:13 +110093typedef struct identity {
94 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100095 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110096 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000097 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110098 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099} Identity;
100
Damien Millerad833b32000-08-23 10:46:23 +1000101typedef struct {
102 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100103 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000104} Idtab;
105
106/* private key table, one per protocol version */
107Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
109int max_fd = 0;
110
111/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000112pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
114/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100115char socket_name[MAXPATHLEN];
116char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Ben Lindstrom2f717042002-06-06 21:52:03 +0000118/* locking */
119int locked = 0;
120char *lock_passwd = NULL;
121
Damien Miller95def091999-11-25 00:26:21 +1100122extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100123
Damien Miller53d81482003-01-22 11:47:19 +1100124/* Default lifetime (0 == forever) */
125static int lifetime = 0;
126
Ben Lindstrombba81212001-06-25 05:01:22 +0000127static void
Damien Miller58f34862002-09-04 16:31:21 +1000128close_socket(SocketEntry *e)
129{
Damien Miller58f34862002-09-04 16:31:21 +1000130 close(e->fd);
131 e->fd = -1;
132 e->type = AUTH_UNUSED;
133 buffer_free(&e->input);
134 buffer_free(&e->output);
135 buffer_free(&e->request);
136}
137
138static void
Damien Millerad833b32000-08-23 10:46:23 +1000139idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Millerad833b32000-08-23 10:46:23 +1000141 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000142
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000143 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100144 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000145 idtable[i].nentries = 0;
146 }
147}
148
149/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000150static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000151idtab_lookup(int version)
152{
153 if (version < 1 || version > 2)
154 fatal("internal error, bad protocol version %d", version);
155 return &idtable[version];
156}
157
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000158static void
159free_identity(Identity *id)
160{
161 key_free(id->key);
162 xfree(id->comment);
163 xfree(id);
164}
165
Damien Millerad833b32000-08-23 10:46:23 +1000166/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100167static Identity *
168lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000169{
Damien Miller1a534ae2002-01-22 23:26:13 +1100170 Identity *id;
171
Damien Millerad833b32000-08-23 10:46:23 +1000172 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100173 TAILQ_FOREACH(id, &tab->idlist, next) {
174 if (key_equal(key, id->key))
175 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000176 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100177 return (NULL);
178}
179
Damien Miller6c711792003-01-24 11:36:23 +1100180/* Check confirmation of keysign request */
181static int
182confirm_key(Identity *id)
183{
Darren Tuckerce327b62004-11-05 20:38:03 +1100184 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100185 int ret = -1;
186
187 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100188 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
189 id->comment, p))
190 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100191 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100192
Damien Miller6c711792003-01-24 11:36:23 +1100193 return (ret);
194}
195
Damien Millerad833b32000-08-23 10:46:23 +1000196/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000197static void
Damien Millerad833b32000-08-23 10:46:23 +1000198process_request_identities(SocketEntry *e, int version)
199{
200 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100201 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000202 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203
Damien Miller95def091999-11-25 00:26:21 +1100204 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000205 buffer_put_char(&msg, (version == 1) ?
206 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
207 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100208 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000210 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
211 buffer_put_bignum(&msg, id->key->rsa->e);
212 buffer_put_bignum(&msg, id->key->rsa->n);
213 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000214 u_char *blob;
215 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100216 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000217 buffer_put_string(&msg, blob, blen);
218 xfree(blob);
219 }
220 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100221 }
222 buffer_put_int(&e->output, buffer_len(&msg));
223 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
224 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225}
226
Damien Millerad833b32000-08-23 10:46:23 +1000227/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000228static void
Damien Millerad833b32000-08-23 10:46:23 +1000229process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000231 u_char buf[32], mdbuf[16], session_id[16];
232 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000233 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000234 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000235 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100236 Buffer msg;
237 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000238 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Miller95def091999-11-25 00:26:21 +1100240 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100241 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100242 if ((challenge = BN_new()) == NULL)
243 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000244
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000245 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000246 buffer_get_bignum(&e->request, key->rsa->e);
247 buffer_get_bignum(&e->request, key->rsa->n);
248 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249
Damien Millerad833b32000-08-23 10:46:23 +1000250 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000251 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000252 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000253 buffer_get(&e->request, session_id, 16);
254 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000255 if (response_type != 1)
256 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Miller1a534ae2002-01-22 23:26:13 +1100258 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100259 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100260 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000261 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100262 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
263 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100264
Damien Millerad833b32000-08-23 10:46:23 +1000265 /* The response is MD5 of decrypted challenge plus session id. */
266 len = BN_num_bytes(challenge);
267 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000268 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000269 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100270 }
Damien Millerad833b32000-08-23 10:46:23 +1000271 memset(buf, 0, 32);
272 BN_bn2bin(challenge, buf + 32 - len);
273 MD5_Init(&md);
274 MD5_Update(&md, buf, 32);
275 MD5_Update(&md, session_id, 16);
276 MD5_Final(mdbuf, &md);
277
278 /* Send the response. */
279 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
280 for (i = 0; i < 16; i++)
281 buffer_put_char(&msg, mdbuf[i]);
282 goto send;
283 }
284
285failure:
286 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100287 buffer_put_char(&msg, SSH_AGENT_FAILURE);
288send:
289 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000290 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
291 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100292 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000293 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294}
295
Damien Millerad833b32000-08-23 10:46:23 +1000296/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000297static void
Damien Millerad833b32000-08-23 10:46:23 +1000298process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000300 u_char *blob, *data, *signature = NULL;
301 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000302 extern int datafellows;
303 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000304 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000305 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000306
307 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000308
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000309 blob = buffer_get_string(&e->request, &blen);
310 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100311
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000312 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100313 if (flags & SSH_AGENT_OLD_SIGNATURE)
314 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000315
Damien Miller0bc1bd82000-11-13 22:57:25 +1100316 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000317 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100318 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100319 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100320 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000321 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000322 }
Damien Millerad833b32000-08-23 10:46:23 +1000323 buffer_init(&msg);
324 if (ok == 0) {
325 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
326 buffer_put_string(&msg, signature, slen);
327 } else {
328 buffer_put_char(&msg, SSH_AGENT_FAILURE);
329 }
330 buffer_put_int(&e->output, buffer_len(&msg));
331 buffer_append(&e->output, buffer_ptr(&msg),
332 buffer_len(&msg));
333 buffer_free(&msg);
334 xfree(data);
335 xfree(blob);
336 if (signature != NULL)
337 xfree(signature);
338}
339
340/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000341static void
Damien Millerad833b32000-08-23 10:46:23 +1000342process_remove_identity(SocketEntry *e, int version)
343{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000344 u_int blen, bits;
345 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100346 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000347 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100348
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000349 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000350 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100351 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000352 bits = buffer_get_int(&e->request);
353 buffer_get_bignum(&e->request, key->rsa->e);
354 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Millerad833b32000-08-23 10:46:23 +1000356 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000357 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000358 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000359 break;
360 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000361 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100362 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000363 xfree(blob);
364 break;
365 }
366 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100367 Identity *id = lookup_identity(key, version);
368 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100369 /*
370 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100371 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000372 * the array, we actually free the key there and move
373 * all the entries between the empty slot and the end
374 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100375 */
Damien Millerad833b32000-08-23 10:46:23 +1000376 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100377 if (tab->nentries < 1)
378 fatal("process_remove_identity: "
379 "internal error: tab->nentries %d",
380 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100381 TAILQ_REMOVE(&tab->idlist, id, next);
382 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000383 tab->nentries--;
384 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100385 }
Damien Millerad833b32000-08-23 10:46:23 +1000386 key_free(key);
387 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000389 buffer_put_char(&e->output,
390 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391}
392
Ben Lindstrombba81212001-06-25 05:01:22 +0000393static void
Damien Millerad833b32000-08-23 10:46:23 +1000394process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395{
Damien Millerad833b32000-08-23 10:46:23 +1000396 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100397 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
Damien Miller95def091999-11-25 00:26:21 +1100399 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100400 for (id = TAILQ_FIRST(&tab->idlist); id;
401 id = TAILQ_FIRST(&tab->idlist)) {
402 TAILQ_REMOVE(&tab->idlist, id, next);
403 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100404 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
Damien Miller95def091999-11-25 00:26:21 +1100406 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000407 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000408
409 /* Send success. */
410 buffer_put_int(&e->output, 1);
411 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100412}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000413
Ben Lindstrombba81212001-06-25 05:01:22 +0000414static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000415reaper(void)
416{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000417 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000418 Identity *id, *nxt;
419 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000420 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000421
422 for (version = 1; version < 3; version++) {
423 tab = idtab_lookup(version);
424 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
425 nxt = TAILQ_NEXT(id, next);
426 if (id->death != 0 && now >= id->death) {
427 TAILQ_REMOVE(&tab->idlist, id, next);
428 free_identity(id);
429 tab->nentries--;
430 }
431 }
432 }
433}
434
435static void
Damien Millerad833b32000-08-23 10:46:23 +1000436process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100437{
Damien Millerad833b32000-08-23 10:46:23 +1000438 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100439 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000440 char *type_name, *comment;
441 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100442
Damien Millerad833b32000-08-23 10:46:23 +1000443 switch (version) {
444 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100445 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000446 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000447 buffer_get_bignum(&e->request, k->rsa->n);
448 buffer_get_bignum(&e->request, k->rsa->e);
449 buffer_get_bignum(&e->request, k->rsa->d);
450 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100451
Damien Millerad833b32000-08-23 10:46:23 +1000452 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000453 buffer_get_bignum(&e->request, k->rsa->q); /* p */
454 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100455
Damien Millerad833b32000-08-23 10:46:23 +1000456 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000457 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000458 break;
459 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000460 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000461 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100462 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000463 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100464 case KEY_DSA:
465 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000466 buffer_get_bignum2(&e->request, k->dsa->p);
467 buffer_get_bignum2(&e->request, k->dsa->q);
468 buffer_get_bignum2(&e->request, k->dsa->g);
469 buffer_get_bignum2(&e->request, k->dsa->pub_key);
470 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100471 break;
472 case KEY_RSA:
473 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000474 buffer_get_bignum2(&e->request, k->rsa->n);
475 buffer_get_bignum2(&e->request, k->rsa->e);
476 buffer_get_bignum2(&e->request, k->rsa->d);
477 buffer_get_bignum2(&e->request, k->rsa->iqmp);
478 buffer_get_bignum2(&e->request, k->rsa->p);
479 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480
481 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000482 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100483 break;
484 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000485 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000486 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100487 }
Damien Millerad833b32000-08-23 10:46:23 +1000488 break;
489 }
Damien Millerc51d0732003-03-15 11:37:09 +1100490 /* enable blinding */
491 switch (k->type) {
492 case KEY_RSA:
493 case KEY_RSA1:
494 if (RSA_blinding_on(k->rsa, NULL) != 1) {
495 error("process_add_identity: RSA_blinding_on failed");
496 key_free(k);
497 goto send;
498 }
499 break;
500 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000501 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000502 if (k == NULL) {
503 xfree(comment);
504 goto send;
505 }
506 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000507 while (buffer_len(&e->request)) {
508 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000509 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000510 death = time(NULL) + buffer_get_int(&e->request);
511 break;
Damien Miller6c711792003-01-24 11:36:23 +1100512 case SSH_AGENT_CONSTRAIN_CONFIRM:
513 confirm = 1;
514 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000515 default:
516 break;
517 }
518 }
Damien Miller53d81482003-01-22 11:47:19 +1100519 if (lifetime && !death)
520 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000521 if (lookup_identity(k, version) == NULL) {
522 Identity *id = xmalloc(sizeof(Identity));
523 id->key = k;
524 id->comment = comment;
525 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100526 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000527 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
528 /* Increment the number of identities. */
529 tab->nentries++;
530 } else {
531 key_free(k);
532 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000533 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000534send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000535 buffer_put_int(&e->output, 1);
536 buffer_put_char(&e->output,
537 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
538}
539
Ben Lindstrom2f717042002-06-06 21:52:03 +0000540/* XXX todo: encrypt sensitive data with passphrase */
541static void
542process_lock_agent(SocketEntry *e, int lock)
543{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000544 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000545 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000546
547 passwd = buffer_get_string(&e->request, NULL);
548 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
549 locked = 0;
550 memset(lock_passwd, 0, strlen(lock_passwd));
551 xfree(lock_passwd);
552 lock_passwd = NULL;
553 success = 1;
554 } else if (!locked && lock) {
555 locked = 1;
556 lock_passwd = xstrdup(passwd);
557 success = 1;
558 }
559 memset(passwd, 0, strlen(passwd));
560 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000561
Ben Lindstrom2f717042002-06-06 21:52:03 +0000562 buffer_put_int(&e->output, 1);
563 buffer_put_char(&e->output,
564 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000565}
566
567static void
568no_identities(SocketEntry *e, u_int type)
569{
570 Buffer msg;
571
572 buffer_init(&msg);
573 buffer_put_char(&msg,
574 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
575 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
576 buffer_put_int(&msg, 0);
577 buffer_put_int(&e->output, buffer_len(&msg));
578 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
579 buffer_free(&msg);
580}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000581
582#ifdef SMARTCARD
583static void
584process_add_smartcard_key (SocketEntry *e)
585{
Ben Lindstromba72d302002-03-22 03:51:06 +0000586 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000587 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000588 Key **keys, *k;
589 Identity *id;
590 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100591
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000592 sc_reader_id = buffer_get_string(&e->request, NULL);
593 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000594
595 while (buffer_len(&e->request)) {
596 switch (buffer_get_char(&e->request)) {
597 case SSH_AGENT_CONSTRAIN_LIFETIME:
598 death = time(NULL) + buffer_get_int(&e->request);
599 break;
600 case SSH_AGENT_CONSTRAIN_CONFIRM:
601 confirm = 1;
602 break;
603 default:
604 break;
605 }
606 }
607 if (lifetime && !death)
608 death = time(NULL) + lifetime;
609
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000610 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000611 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000612 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000613
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000614 if (keys == NULL || keys[0] == NULL) {
615 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000616 goto send;
617 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000618 for (i = 0; keys[i] != NULL; i++) {
619 k = keys[i];
620 version = k->type == KEY_RSA1 ? 1 : 2;
621 tab = idtab_lookup(version);
622 if (lookup_identity(k, version) == NULL) {
623 id = xmalloc(sizeof(Identity));
624 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000625 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000626 id->death = death;
627 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000628 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
629 tab->nentries++;
630 success = 1;
631 } else {
632 key_free(k);
633 }
634 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000635 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000636 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000637send:
638 buffer_put_int(&e->output, 1);
639 buffer_put_char(&e->output,
640 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
641}
642
643static void
644process_remove_smartcard_key(SocketEntry *e)
645{
Ben Lindstromba72d302002-03-22 03:51:06 +0000646 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000647 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000648 Key **keys, *k = NULL;
649 Identity *id;
650 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000651
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000652 sc_reader_id = buffer_get_string(&e->request, NULL);
653 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000654 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000655 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000656 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000657
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000658 if (keys == NULL || keys[0] == NULL) {
659 error("sc_get_keys failed");
660 goto send;
661 }
662 for (i = 0; keys[i] != NULL; i++) {
663 k = keys[i];
664 version = k->type == KEY_RSA1 ? 1 : 2;
665 if ((id = lookup_identity(k, version)) != NULL) {
666 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000667 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000668 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100669 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000670 success = 1;
671 }
672 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000673 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000674 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000675 xfree(keys);
676send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000677 buffer_put_int(&e->output, 1);
678 buffer_put_char(&e->output,
679 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
680}
Ben Lindstromffce1472001-08-06 21:57:31 +0000681#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000682
Damien Millerad833b32000-08-23 10:46:23 +1000683/* dispatch incoming messages */
684
Ben Lindstrombba81212001-06-25 05:01:22 +0000685static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000686process_message(SocketEntry *e)
687{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000688 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000689 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000690
691 /* kill dead keys */
692 reaper();
693
Damien Miller95def091999-11-25 00:26:21 +1100694 if (buffer_len(&e->input) < 5)
695 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100696 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100697 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100698 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000699 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100700 return;
701 }
702 if (buffer_len(&e->input) < msg_len + 4)
703 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000704
705 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100706 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000707 buffer_clear(&e->request);
708 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
709 buffer_consume(&e->input, msg_len);
710 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000711
Ben Lindstrom2f717042002-06-06 21:52:03 +0000712 /* check wheter agent is locked */
713 if (locked && type != SSH_AGENTC_UNLOCK) {
714 buffer_clear(&e->request);
715 switch (type) {
716 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
717 case SSH2_AGENTC_REQUEST_IDENTITIES:
718 /* send empty lists */
719 no_identities(e, type);
720 break;
721 default:
722 /* send a fail message for all other request types */
723 buffer_put_int(&e->output, 1);
724 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
725 }
726 return;
727 }
728
Ben Lindstrom3f471632001-07-04 03:53:15 +0000729 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100730 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000731 case SSH_AGENTC_LOCK:
732 case SSH_AGENTC_UNLOCK:
733 process_lock_agent(e, type == SSH_AGENTC_LOCK);
734 break;
Damien Millerad833b32000-08-23 10:46:23 +1000735 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100736 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000737 process_authentication_challenge1(e);
738 break;
739 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
740 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100741 break;
742 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000743 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000744 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100745 break;
746 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000747 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100748 break;
749 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000750 process_remove_all_identities(e, 1);
751 break;
752 /* ssh2 */
753 case SSH2_AGENTC_SIGN_REQUEST:
754 process_sign_request2(e);
755 break;
756 case SSH2_AGENTC_REQUEST_IDENTITIES:
757 process_request_identities(e, 2);
758 break;
759 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000760 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000761 process_add_identity(e, 2);
762 break;
763 case SSH2_AGENTC_REMOVE_IDENTITY:
764 process_remove_identity(e, 2);
765 break;
766 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
767 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100768 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000769#ifdef SMARTCARD
770 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000771 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000772 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100773 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000774 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
775 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100776 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000777#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100778 default:
779 /* Unknown message. Respond with failure. */
780 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000781 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100782 buffer_put_int(&e->output, 1);
783 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
784 break;
785 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000789new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000790{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000791 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000792
Damien Miller232711f2004-06-15 10:35:30 +1000793 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794
Damien Miller95def091999-11-25 00:26:21 +1100795 if (fd > max_fd)
796 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000797
Damien Miller95def091999-11-25 00:26:21 +1100798 for (i = 0; i < sockets_alloc; i++)
799 if (sockets[i].type == AUTH_UNUSED) {
800 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100801 buffer_init(&sockets[i].input);
802 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000803 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000804 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100805 return;
806 }
807 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000808 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100809 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000810 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100811 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000812 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100813 sockets[old_alloc].fd = fd;
814 buffer_init(&sockets[old_alloc].input);
815 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000816 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000817 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000818}
819
Ben Lindstrombba81212001-06-25 05:01:22 +0000820static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000821prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000823 u_int i, sz;
824 int n = 0;
825
826 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100827 switch (sockets[i].type) {
828 case AUTH_SOCKET:
829 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000830 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100831 break;
832 case AUTH_UNUSED:
833 break;
834 default:
835 fatal("Unknown socket type %d", sockets[i].type);
836 break;
837 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000838 }
839
840 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000841 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000842 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000843 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000844 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000845 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 *fdrp = xmalloc(sz);
847 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000848 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000849 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000850 if (n < *fdl)
851 debug("XXX shrink: %d < %d", n, *fdl);
852 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000853 memset(*fdrp, 0, sz);
854 memset(*fdwp, 0, sz);
855
856 for (i = 0; i < sockets_alloc; i++) {
857 switch (sockets[i].type) {
858 case AUTH_SOCKET:
859 case AUTH_CONNECTION:
860 FD_SET(sockets[i].fd, *fdrp);
861 if (buffer_len(&sockets[i].output) > 0)
862 FD_SET(sockets[i].fd, *fdwp);
863 break;
864 default:
865 break;
866 }
867 }
868 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000869}
870
Ben Lindstrombba81212001-06-25 05:01:22 +0000871static void
Damien Miller95def091999-11-25 00:26:21 +1100872after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000873{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000874 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100875 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100876 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000877 int len, sock;
878 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000879 uid_t euid;
880 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881
Damien Miller95def091999-11-25 00:26:21 +1100882 for (i = 0; i < sockets_alloc; i++)
883 switch (sockets[i].type) {
884 case AUTH_UNUSED:
885 break;
886 case AUTH_SOCKET:
887 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100888 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000889 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100890 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100891 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100892 error("accept from AUTH_SOCKET: %s",
893 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100894 break;
895 }
Damien Miller538f1812002-09-12 09:51:10 +1000896 if (getpeereid(sock, &euid, &egid) < 0) {
897 error("getpeereid %d failed: %s",
898 sock, strerror(errno));
899 close(sock);
900 break;
901 }
Damien Milleraf9de382002-10-03 11:54:35 +1000902 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000903 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000904 "peer euid %u != uid %u",
905 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000906 close(sock);
907 break;
908 }
Damien Miller95def091999-11-25 00:26:21 +1100909 new_socket(AUTH_CONNECTION, sock);
910 }
911 break;
912 case AUTH_CONNECTION:
913 if (buffer_len(&sockets[i].output) > 0 &&
914 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000915 do {
916 len = write(sockets[i].fd,
917 buffer_ptr(&sockets[i].output),
918 buffer_len(&sockets[i].output));
919 if (len == -1 && (errno == EAGAIN ||
920 errno == EINTR))
921 continue;
922 break;
923 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100924 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000925 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100926 break;
927 }
928 buffer_consume(&sockets[i].output, len);
929 }
930 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000931 do {
932 len = read(sockets[i].fd, buf, sizeof(buf));
933 if (len == -1 && (errno == EAGAIN ||
934 errno == EINTR))
935 continue;
936 break;
937 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100938 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000939 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100940 break;
941 }
942 buffer_append(&sockets[i].input, buf, len);
943 process_message(&sockets[i]);
944 }
945 break;
946 default:
947 fatal("Unknown type %d", sockets[i].type);
948 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000949}
950
Ben Lindstrombba81212001-06-25 05:01:22 +0000951static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000952cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000953{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000954 if (socket_name[0])
955 unlink(socket_name);
956 if (socket_dir[0])
957 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000958}
959
Darren Tucker3e33cec2003-10-02 16:12:36 +1000960void
Damien Miller792c5111999-10-29 09:47:09 +1000961cleanup_exit(int i)
962{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000963 cleanup_socket();
964 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000965}
966
Damien Miller1c13bd82006-03-26 14:28:14 +1100967/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000968static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000969cleanup_handler(int sig)
970{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000971 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000972 _exit(2);
973}
974
Damien Miller1c13bd82006-03-26 14:28:14 +1100975/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000976static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000977check_parent_exists(int sig)
978{
979 int save_errno = errno;
980
981 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
982 /* printf("Parent has died - Authentication agent exiting.\n"); */
983 cleanup_handler(sig); /* safe */
984 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000985 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000986 alarm(10);
987 errno = save_errno;
988}
989
990static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000991usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000992{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000993 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000994 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000995 fprintf(stderr, "Options:\n");
996 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
997 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
998 fprintf(stderr, " -k Kill the current agent.\n");
999 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001000 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001001 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001002 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001003}
1004
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001005int
1006main(int ac, char **av)
1007{
Damien Miller6c711792003-01-24 11:36:23 +11001008 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001009 int sock, fd, ch;
1010 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001011 char *shell, *format, *pidstr, *agentsocket = NULL;
1012 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001013 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001014#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001015 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001016#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001017 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001018 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001019 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001020 pid_t pid;
1021 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001022
Darren Tuckerce321d82005-10-03 18:11:24 +10001023 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1024 sanitise_stdfd();
1025
Damien Miller6cffb9a2002-09-04 16:20:26 +10001026 /* drop */
1027 setegid(getgid());
1028 setgid(getgid());
1029
Damien Miller6c4914a2004-03-03 11:08:59 +11001030#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1031 /* Disable ptrace on Linux without sgid bit */
1032 prctl(PR_SET_DUMPABLE, 0);
1033#endif
1034
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001035 SSLeay_add_all_algorithms();
1036
Damien Miller59d3d5b2003-08-22 09:34:41 +10001037 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001038 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001039 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001040
Damien Miller53d81482003-01-22 11:47:19 +11001041 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001042 switch (ch) {
1043 case 'c':
1044 if (s_flag)
1045 usage();
1046 c_flag++;
1047 break;
1048 case 'k':
1049 k_flag++;
1050 break;
1051 case 's':
1052 if (c_flag)
1053 usage();
1054 s_flag++;
1055 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001056 case 'd':
1057 if (d_flag)
1058 usage();
1059 d_flag++;
1060 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001061 case 'a':
1062 agentsocket = optarg;
1063 break;
Damien Miller53d81482003-01-22 11:47:19 +11001064 case 't':
1065 if ((lifetime = convtime(optarg)) == -1) {
1066 fprintf(stderr, "Invalid lifetime\n");
1067 usage();
1068 }
1069 break;
Damien Miller95def091999-11-25 00:26:21 +11001070 default:
1071 usage();
1072 }
Damien Miller792c5111999-10-29 09:47:09 +10001073 }
Damien Miller95def091999-11-25 00:26:21 +11001074 ac -= optind;
1075 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001076
Ben Lindstromd94580c2001-07-04 03:48:02 +00001077 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001078 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001079
Ben Lindstromeecdf232002-04-02 21:03:51 +00001080 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001081 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001082 if (shell != NULL &&
1083 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001084 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001085 }
Damien Miller95def091999-11-25 00:26:21 +11001086 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001087 const char *errstr = NULL;
1088
Damien Miller95def091999-11-25 00:26:21 +11001089 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1090 if (pidstr == NULL) {
1091 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001092 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001093 exit(1);
1094 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001095 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1096 if (errstr) {
1097 fprintf(stderr,
1098 "%s=\"%s\", which is not a good PID: %s\n",
1099 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001100 exit(1);
1101 }
1102 if (kill(pid, SIGTERM) == -1) {
1103 perror("kill");
1104 exit(1);
1105 }
1106 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1107 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1108 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001109 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001110 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001111 }
Damien Miller95def091999-11-25 00:26:21 +11001112 parent_pid = getpid();
1113
Ben Lindstromb7788f32002-06-06 21:46:08 +00001114 if (agentsocket == NULL) {
1115 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001116 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001117 if (mkdtemp(socket_dir) == NULL) {
1118 perror("mkdtemp: private socket dir");
1119 exit(1);
1120 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001121 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1122 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001123 } else {
1124 /* Try to use specified agent socket */
1125 socket_dir[0] = '\0';
1126 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001127 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001128
Damien Miller5428f641999-11-25 11:54:57 +11001129 /*
1130 * Create socket early so it will exist before command gets run from
1131 * the parent.
1132 */
Damien Miller95def091999-11-25 00:26:21 +11001133 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1134 if (sock < 0) {
1135 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001136 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001137 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001138 }
Damien Miller95def091999-11-25 00:26:21 +11001139 memset(&sunaddr, 0, sizeof(sunaddr));
1140 sunaddr.sun_family = AF_UNIX;
1141 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001142 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001143 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001144 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001145 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001146 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001147 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001148 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001149 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001150 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001151 perror("listen");
1152 cleanup_exit(1);
1153 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001154
Damien Miller5428f641999-11-25 11:54:57 +11001155 /*
1156 * Fork, and have the parent execute the command, if any, or present
1157 * the socket data. The child continues as the authentication agent.
1158 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001159 if (d_flag) {
1160 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1161 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1162 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1163 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001164 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001165 goto skip;
1166 }
Damien Miller95def091999-11-25 00:26:21 +11001167 pid = fork();
1168 if (pid == -1) {
1169 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001170 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001171 }
1172 if (pid != 0) { /* Parent - execute the given command. */
1173 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001174 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001175 if (ac == 0) {
1176 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1177 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001178 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001179 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001180 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001181 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001182 exit(0);
1183 }
Damien Millere4340be2000-09-16 13:29:08 +11001184 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1185 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1186 perror("setenv");
1187 exit(1);
1188 }
Damien Miller95def091999-11-25 00:26:21 +11001189 execvp(av[0], av);
1190 perror(av[0]);
1191 exit(1);
1192 }
Damien Millerc653b892002-02-08 22:05:41 +11001193 /* child */
1194 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001195
1196 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001197 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001198 cleanup_exit(1);
1199 }
1200
1201 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001202 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1203 /* XXX might close listen socket */
1204 (void)dup2(fd, STDIN_FILENO);
1205 (void)dup2(fd, STDOUT_FILENO);
1206 (void)dup2(fd, STDERR_FILENO);
1207 if (fd > 2)
1208 close(fd);
1209 }
Damien Miller95def091999-11-25 00:26:21 +11001210
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001211#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001212 /* deny core dumps, since memory contains unencrypted private keys */
1213 rlim.rlim_cur = rlim.rlim_max = 0;
1214 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001215 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001216 cleanup_exit(1);
1217 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001218#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001219
1220skip:
Damien Miller95def091999-11-25 00:26:21 +11001221 new_socket(AUTH_SOCKET, sock);
1222 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001223 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001224 alarm(10);
1225 }
Damien Millerad833b32000-08-23 10:46:23 +10001226 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001227 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001228 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001229 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001230 signal(SIGHUP, cleanup_handler);
1231 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001232 nalloc = 0;
1233
Damien Miller95def091999-11-25 00:26:21 +11001234 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001235 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001236 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001237 if (errno == EINTR)
1238 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001239 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001240 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001241 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001242 }
1243 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001244}