blob: 656e399d46a43b9b42dc7697bae8c10a1ac88055 [file] [log] [blame]
Damien Miller40b59852006-06-13 13:00:25 +10001/* $OpenBSD: ssh-agent.c,v 1.138 2006/05/17 12:43:34 markus 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 Miller574c41f2006-03-15 11:40:10 +110041#ifdef HAVE_SYS_UN_H
42# include <sys/un.h>
43#endif
Damien Miller9b481512002-09-12 10:43:29 +100044#include "openbsd-compat/sys-queue.h"
Damien Millercd4223c2006-03-15 11:22:47 +110045#include <sys/resource.h>
Damien Miller03e20032006-03-15 11:16:59 +110046#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110047# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110048#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110049#include <signal.h>
50
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include <openssl/evp.h>
52#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54#include "ssh.h"
55#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056#include "buffer.h"
57#include "bufaux.h"
58#include "xmalloc.h"
Damien Miller994cf142000-07-21 10:19:44 +100059#include "key.h"
60#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110061#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110063#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Ben Lindstrom3f471632001-07-04 03:53:15 +000065#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000066#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000067#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000068
Damien Miller6c4914a2004-03-03 11:08:59 +110069#if defined(HAVE_SYS_PRCTL_H)
70#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
71#endif
72
Ben Lindstrom65366a82001-12-06 16:32:47 +000073typedef enum {
74 AUTH_UNUSED,
75 AUTH_SOCKET,
76 AUTH_CONNECTION
77} sock_type;
78
Damien Miller95def091999-11-25 00:26:21 +110079typedef struct {
80 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000081 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110082 Buffer input;
83 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000084 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085} SocketEntry;
86
Ben Lindstrom46c16222000-12-22 01:43:59 +000087u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088SocketEntry *sockets = NULL;
89
Damien Miller1a534ae2002-01-22 23:26:13 +110090typedef struct identity {
91 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +100092 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110093 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +000094 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +110095 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096} Identity;
97
Damien Millerad833b32000-08-23 10:46:23 +100098typedef struct {
99 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100100 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000101} Idtab;
102
103/* private key table, one per protocol version */
104Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106int max_fd = 0;
107
108/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000109pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
111/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100112char socket_name[MAXPATHLEN];
113char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Ben Lindstrom2f717042002-06-06 21:52:03 +0000115/* locking */
116int locked = 0;
117char *lock_passwd = NULL;
118
Damien Miller95def091999-11-25 00:26:21 +1100119extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100120
Damien Miller53d81482003-01-22 11:47:19 +1100121/* Default lifetime (0 == forever) */
122static int lifetime = 0;
123
Ben Lindstrombba81212001-06-25 05:01:22 +0000124static void
Damien Miller58f34862002-09-04 16:31:21 +1000125close_socket(SocketEntry *e)
126{
Damien Miller58f34862002-09-04 16:31:21 +1000127 close(e->fd);
128 e->fd = -1;
129 e->type = AUTH_UNUSED;
130 buffer_free(&e->input);
131 buffer_free(&e->output);
132 buffer_free(&e->request);
133}
134
135static void
Damien Millerad833b32000-08-23 10:46:23 +1000136idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137{
Damien Millerad833b32000-08-23 10:46:23 +1000138 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000139
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000140 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100141 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000142 idtable[i].nentries = 0;
143 }
144}
145
146/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000147static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000148idtab_lookup(int version)
149{
150 if (version < 1 || version > 2)
151 fatal("internal error, bad protocol version %d", version);
152 return &idtable[version];
153}
154
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000155static void
156free_identity(Identity *id)
157{
158 key_free(id->key);
159 xfree(id->comment);
160 xfree(id);
161}
162
Damien Millerad833b32000-08-23 10:46:23 +1000163/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100164static Identity *
165lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000166{
Damien Miller1a534ae2002-01-22 23:26:13 +1100167 Identity *id;
168
Damien Millerad833b32000-08-23 10:46:23 +1000169 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100170 TAILQ_FOREACH(id, &tab->idlist, next) {
171 if (key_equal(key, id->key))
172 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000173 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100174 return (NULL);
175}
176
Damien Miller6c711792003-01-24 11:36:23 +1100177/* Check confirmation of keysign request */
178static int
179confirm_key(Identity *id)
180{
Darren Tuckerce327b62004-11-05 20:38:03 +1100181 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100182 int ret = -1;
183
184 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100185 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
186 id->comment, p))
187 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100188 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100189
Damien Miller6c711792003-01-24 11:36:23 +1100190 return (ret);
191}
192
Damien Millerad833b32000-08-23 10:46:23 +1000193/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000194static void
Damien Millerad833b32000-08-23 10:46:23 +1000195process_request_identities(SocketEntry *e, int version)
196{
197 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100198 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000199 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200
Damien Miller95def091999-11-25 00:26:21 +1100201 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000202 buffer_put_char(&msg, (version == 1) ?
203 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
204 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100205 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100206 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000207 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
208 buffer_put_bignum(&msg, id->key->rsa->e);
209 buffer_put_bignum(&msg, id->key->rsa->n);
210 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000211 u_char *blob;
212 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100213 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000214 buffer_put_string(&msg, blob, blen);
215 xfree(blob);
216 }
217 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100218 }
219 buffer_put_int(&e->output, buffer_len(&msg));
220 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
221 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000222}
223
Damien Millerad833b32000-08-23 10:46:23 +1000224/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000225static void
Damien Millerad833b32000-08-23 10:46:23 +1000226process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000227{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000228 u_char buf[32], mdbuf[16], session_id[16];
229 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000230 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000231 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000232 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100233 Buffer msg;
234 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000235 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Damien Miller95def091999-11-25 00:26:21 +1100237 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100238 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100239 if ((challenge = BN_new()) == NULL)
240 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000241
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000242 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000243 buffer_get_bignum(&e->request, key->rsa->e);
244 buffer_get_bignum(&e->request, key->rsa->n);
245 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Millerad833b32000-08-23 10:46:23 +1000247 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000248 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000249 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000250 buffer_get(&e->request, session_id, 16);
251 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000252 if (response_type != 1)
253 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Miller1a534ae2002-01-22 23:26:13 +1100255 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100256 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100257 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000258 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100259 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
260 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100261
Damien Millerad833b32000-08-23 10:46:23 +1000262 /* The response is MD5 of decrypted challenge plus session id. */
263 len = BN_num_bytes(challenge);
264 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000265 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000266 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100267 }
Damien Millerad833b32000-08-23 10:46:23 +1000268 memset(buf, 0, 32);
269 BN_bn2bin(challenge, buf + 32 - len);
270 MD5_Init(&md);
271 MD5_Update(&md, buf, 32);
272 MD5_Update(&md, session_id, 16);
273 MD5_Final(mdbuf, &md);
274
275 /* Send the response. */
276 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
277 for (i = 0; i < 16; i++)
278 buffer_put_char(&msg, mdbuf[i]);
279 goto send;
280 }
281
282failure:
283 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100284 buffer_put_char(&msg, SSH_AGENT_FAILURE);
285send:
286 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000287 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
288 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100289 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000290 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291}
292
Damien Millerad833b32000-08-23 10:46:23 +1000293/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000294static void
Damien Millerad833b32000-08-23 10:46:23 +1000295process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000297 u_char *blob, *data, *signature = NULL;
298 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000299 extern int datafellows;
300 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000301 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000302 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000303
304 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000305
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000306 blob = buffer_get_string(&e->request, &blen);
307 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100308
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000309 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100310 if (flags & SSH_AGENT_OLD_SIGNATURE)
311 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000312
Damien Miller0bc1bd82000-11-13 22:57:25 +1100313 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000314 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100315 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100316 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100317 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000318 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000319 }
Damien Millerad833b32000-08-23 10:46:23 +1000320 buffer_init(&msg);
321 if (ok == 0) {
322 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
323 buffer_put_string(&msg, signature, slen);
324 } else {
325 buffer_put_char(&msg, SSH_AGENT_FAILURE);
326 }
327 buffer_put_int(&e->output, buffer_len(&msg));
328 buffer_append(&e->output, buffer_ptr(&msg),
329 buffer_len(&msg));
330 buffer_free(&msg);
331 xfree(data);
332 xfree(blob);
333 if (signature != NULL)
334 xfree(signature);
335}
336
337/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000338static void
Damien Millerad833b32000-08-23 10:46:23 +1000339process_remove_identity(SocketEntry *e, int version)
340{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000341 u_int blen, bits;
342 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100343 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000344 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100345
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000346 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000347 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100348 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000349 bits = buffer_get_int(&e->request);
350 buffer_get_bignum(&e->request, key->rsa->e);
351 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000352
Damien Millerad833b32000-08-23 10:46:23 +1000353 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000354 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000355 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000356 break;
357 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000358 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100359 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000360 xfree(blob);
361 break;
362 }
363 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100364 Identity *id = lookup_identity(key, version);
365 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100366 /*
367 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100368 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000369 * the array, we actually free the key there and move
370 * all the entries between the empty slot and the end
371 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100372 */
Damien Millerad833b32000-08-23 10:46:23 +1000373 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 if (tab->nentries < 1)
375 fatal("process_remove_identity: "
376 "internal error: tab->nentries %d",
377 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100378 TAILQ_REMOVE(&tab->idlist, id, next);
379 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000380 tab->nentries--;
381 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100382 }
Damien Millerad833b32000-08-23 10:46:23 +1000383 key_free(key);
384 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000385 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000386 buffer_put_char(&e->output,
387 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000388}
389
Ben Lindstrombba81212001-06-25 05:01:22 +0000390static void
Damien Millerad833b32000-08-23 10:46:23 +1000391process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000392{
Damien Millerad833b32000-08-23 10:46:23 +1000393 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100394 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000395
Damien Miller95def091999-11-25 00:26:21 +1100396 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100397 for (id = TAILQ_FIRST(&tab->idlist); id;
398 id = TAILQ_FIRST(&tab->idlist)) {
399 TAILQ_REMOVE(&tab->idlist, id, next);
400 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100401 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402
Damien Miller95def091999-11-25 00:26:21 +1100403 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000404 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
406 /* Send success. */
407 buffer_put_int(&e->output, 1);
408 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100409}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Ben Lindstrombba81212001-06-25 05:01:22 +0000411static void
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000412reaper(void)
413{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000414 u_int now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000415 Identity *id, *nxt;
416 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000417 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000418
419 for (version = 1; version < 3; version++) {
420 tab = idtab_lookup(version);
421 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
422 nxt = TAILQ_NEXT(id, next);
423 if (id->death != 0 && now >= id->death) {
424 TAILQ_REMOVE(&tab->idlist, id, next);
425 free_identity(id);
426 tab->nentries--;
427 }
428 }
429 }
430}
431
432static void
Damien Millerad833b32000-08-23 10:46:23 +1000433process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100434{
Damien Millerad833b32000-08-23 10:46:23 +1000435 Idtab *tab = idtab_lookup(version);
Damien Miller6c711792003-01-24 11:36:23 +1100436 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000437 char *type_name, *comment;
438 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100439
Damien Millerad833b32000-08-23 10:46:23 +1000440 switch (version) {
441 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100442 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000443 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000444 buffer_get_bignum(&e->request, k->rsa->n);
445 buffer_get_bignum(&e->request, k->rsa->e);
446 buffer_get_bignum(&e->request, k->rsa->d);
447 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100448
Damien Millerad833b32000-08-23 10:46:23 +1000449 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000450 buffer_get_bignum(&e->request, k->rsa->q); /* p */
451 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100452
Damien Millerad833b32000-08-23 10:46:23 +1000453 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000454 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000455 break;
456 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000457 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000458 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100459 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000460 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100461 case KEY_DSA:
462 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000463 buffer_get_bignum2(&e->request, k->dsa->p);
464 buffer_get_bignum2(&e->request, k->dsa->q);
465 buffer_get_bignum2(&e->request, k->dsa->g);
466 buffer_get_bignum2(&e->request, k->dsa->pub_key);
467 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100468 break;
469 case KEY_RSA:
470 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000471 buffer_get_bignum2(&e->request, k->rsa->n);
472 buffer_get_bignum2(&e->request, k->rsa->e);
473 buffer_get_bignum2(&e->request, k->rsa->d);
474 buffer_get_bignum2(&e->request, k->rsa->iqmp);
475 buffer_get_bignum2(&e->request, k->rsa->p);
476 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100477
478 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000479 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 break;
481 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000482 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000483 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100484 }
Damien Millerad833b32000-08-23 10:46:23 +1000485 break;
486 }
Damien Millerc51d0732003-03-15 11:37:09 +1100487 /* enable blinding */
488 switch (k->type) {
489 case KEY_RSA:
490 case KEY_RSA1:
491 if (RSA_blinding_on(k->rsa, NULL) != 1) {
492 error("process_add_identity: RSA_blinding_on failed");
493 key_free(k);
494 goto send;
495 }
496 break;
497 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000498 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000499 if (k == NULL) {
500 xfree(comment);
501 goto send;
502 }
503 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000504 while (buffer_len(&e->request)) {
505 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000506 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000507 death = time(NULL) + buffer_get_int(&e->request);
508 break;
Damien Miller6c711792003-01-24 11:36:23 +1100509 case SSH_AGENT_CONSTRAIN_CONFIRM:
510 confirm = 1;
511 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000512 default:
513 break;
514 }
515 }
Damien Miller53d81482003-01-22 11:47:19 +1100516 if (lifetime && !death)
517 death = time(NULL) + lifetime;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000518 if (lookup_identity(k, version) == NULL) {
519 Identity *id = xmalloc(sizeof(Identity));
520 id->key = k;
521 id->comment = comment;
522 id->death = death;
Damien Miller6c711792003-01-24 11:36:23 +1100523 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000524 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
525 /* Increment the number of identities. */
526 tab->nentries++;
527 } else {
528 key_free(k);
529 xfree(comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000530 }
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000531send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000532 buffer_put_int(&e->output, 1);
533 buffer_put_char(&e->output,
534 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
535}
536
Ben Lindstrom2f717042002-06-06 21:52:03 +0000537/* XXX todo: encrypt sensitive data with passphrase */
538static void
539process_lock_agent(SocketEntry *e, int lock)
540{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000541 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000542 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000543
544 passwd = buffer_get_string(&e->request, NULL);
545 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
546 locked = 0;
547 memset(lock_passwd, 0, strlen(lock_passwd));
548 xfree(lock_passwd);
549 lock_passwd = NULL;
550 success = 1;
551 } else if (!locked && lock) {
552 locked = 1;
553 lock_passwd = xstrdup(passwd);
554 success = 1;
555 }
556 memset(passwd, 0, strlen(passwd));
557 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000558
Ben Lindstrom2f717042002-06-06 21:52:03 +0000559 buffer_put_int(&e->output, 1);
560 buffer_put_char(&e->output,
561 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000562}
563
564static void
565no_identities(SocketEntry *e, u_int type)
566{
567 Buffer msg;
568
569 buffer_init(&msg);
570 buffer_put_char(&msg,
571 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
572 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
573 buffer_put_int(&msg, 0);
574 buffer_put_int(&e->output, buffer_len(&msg));
575 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
576 buffer_free(&msg);
577}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000578
579#ifdef SMARTCARD
580static void
581process_add_smartcard_key (SocketEntry *e)
582{
Ben Lindstromba72d302002-03-22 03:51:06 +0000583 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000584 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000585 Key **keys, *k;
586 Identity *id;
587 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100588
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000589 sc_reader_id = buffer_get_string(&e->request, NULL);
590 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000591
592 while (buffer_len(&e->request)) {
593 switch (buffer_get_char(&e->request)) {
594 case SSH_AGENT_CONSTRAIN_LIFETIME:
595 death = time(NULL) + buffer_get_int(&e->request);
596 break;
597 case SSH_AGENT_CONSTRAIN_CONFIRM:
598 confirm = 1;
599 break;
600 default:
601 break;
602 }
603 }
604 if (lifetime && !death)
605 death = time(NULL) + lifetime;
606
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000607 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000608 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000609 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000610
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000611 if (keys == NULL || keys[0] == NULL) {
612 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000613 goto send;
614 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000615 for (i = 0; keys[i] != NULL; i++) {
616 k = keys[i];
617 version = k->type == KEY_RSA1 ? 1 : 2;
618 tab = idtab_lookup(version);
619 if (lookup_identity(k, version) == NULL) {
620 id = xmalloc(sizeof(Identity));
621 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000622 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000623 id->death = death;
624 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000625 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
626 tab->nentries++;
627 success = 1;
628 } else {
629 key_free(k);
630 }
631 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000632 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000633 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000634send:
635 buffer_put_int(&e->output, 1);
636 buffer_put_char(&e->output,
637 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
638}
639
640static void
641process_remove_smartcard_key(SocketEntry *e)
642{
Ben Lindstromba72d302002-03-22 03:51:06 +0000643 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000644 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000645 Key **keys, *k = NULL;
646 Identity *id;
647 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000648
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000649 sc_reader_id = buffer_get_string(&e->request, NULL);
650 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000651 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000652 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000653 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000654
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000655 if (keys == NULL || keys[0] == NULL) {
656 error("sc_get_keys failed");
657 goto send;
658 }
659 for (i = 0; keys[i] != NULL; i++) {
660 k = keys[i];
661 version = k->type == KEY_RSA1 ? 1 : 2;
662 if ((id = lookup_identity(k, version)) != NULL) {
663 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000664 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000665 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100666 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000667 success = 1;
668 }
669 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000670 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000671 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000672 xfree(keys);
673send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000674 buffer_put_int(&e->output, 1);
675 buffer_put_char(&e->output,
676 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
677}
Ben Lindstromffce1472001-08-06 21:57:31 +0000678#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000679
Damien Millerad833b32000-08-23 10:46:23 +1000680/* dispatch incoming messages */
681
Ben Lindstrombba81212001-06-25 05:01:22 +0000682static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000683process_message(SocketEntry *e)
684{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000685 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000686 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000687
688 /* kill dead keys */
689 reaper();
690
Damien Miller95def091999-11-25 00:26:21 +1100691 if (buffer_len(&e->input) < 5)
692 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100693 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100694 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100695 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000696 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100697 return;
698 }
699 if (buffer_len(&e->input) < msg_len + 4)
700 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000701
702 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100703 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000704 buffer_clear(&e->request);
705 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
706 buffer_consume(&e->input, msg_len);
707 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000708
Ben Lindstrom2f717042002-06-06 21:52:03 +0000709 /* check wheter agent is locked */
710 if (locked && type != SSH_AGENTC_UNLOCK) {
711 buffer_clear(&e->request);
712 switch (type) {
713 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
714 case SSH2_AGENTC_REQUEST_IDENTITIES:
715 /* send empty lists */
716 no_identities(e, type);
717 break;
718 default:
719 /* send a fail message for all other request types */
720 buffer_put_int(&e->output, 1);
721 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
722 }
723 return;
724 }
725
Ben Lindstrom3f471632001-07-04 03:53:15 +0000726 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100727 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000728 case SSH_AGENTC_LOCK:
729 case SSH_AGENTC_UNLOCK:
730 process_lock_agent(e, type == SSH_AGENTC_LOCK);
731 break;
Damien Millerad833b32000-08-23 10:46:23 +1000732 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100733 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000734 process_authentication_challenge1(e);
735 break;
736 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
737 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100738 break;
739 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000740 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000741 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100742 break;
743 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000744 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100745 break;
746 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000747 process_remove_all_identities(e, 1);
748 break;
749 /* ssh2 */
750 case SSH2_AGENTC_SIGN_REQUEST:
751 process_sign_request2(e);
752 break;
753 case SSH2_AGENTC_REQUEST_IDENTITIES:
754 process_request_identities(e, 2);
755 break;
756 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000757 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000758 process_add_identity(e, 2);
759 break;
760 case SSH2_AGENTC_REMOVE_IDENTITY:
761 process_remove_identity(e, 2);
762 break;
763 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
764 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100765 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000766#ifdef SMARTCARD
767 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000768 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000769 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100770 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000771 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
772 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100773 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000774#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100775 default:
776 /* Unknown message. Respond with failure. */
777 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000778 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100779 buffer_put_int(&e->output, 1);
780 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
781 break;
782 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000783}
784
Ben Lindstrombba81212001-06-25 05:01:22 +0000785static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000786new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000787{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000788 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000789
Damien Miller232711f2004-06-15 10:35:30 +1000790 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000791
Damien Miller95def091999-11-25 00:26:21 +1100792 if (fd > max_fd)
793 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000794
Damien Miller95def091999-11-25 00:26:21 +1100795 for (i = 0; i < sockets_alloc; i++)
796 if (sockets[i].type == AUTH_UNUSED) {
797 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100798 buffer_init(&sockets[i].input);
799 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000800 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000801 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100802 return;
803 }
804 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000805 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100806 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000807 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100808 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000809 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100810 sockets[old_alloc].fd = fd;
811 buffer_init(&sockets[old_alloc].input);
812 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000813 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000814 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815}
816
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static int
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000818prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000819{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000820 u_int i, sz;
821 int n = 0;
822
823 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100824 switch (sockets[i].type) {
825 case AUTH_SOCKET:
826 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000827 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100828 break;
829 case AUTH_UNUSED:
830 break;
831 default:
832 fatal("Unknown socket type %d", sockets[i].type);
833 break;
834 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000835 }
836
837 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000838 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000839 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000840 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000841 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000842 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000843 *fdrp = xmalloc(sz);
844 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000845 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000846 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000847 if (n < *fdl)
848 debug("XXX shrink: %d < %d", n, *fdl);
849 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000850 memset(*fdrp, 0, sz);
851 memset(*fdwp, 0, sz);
852
853 for (i = 0; i < sockets_alloc; i++) {
854 switch (sockets[i].type) {
855 case AUTH_SOCKET:
856 case AUTH_CONNECTION:
857 FD_SET(sockets[i].fd, *fdrp);
858 if (buffer_len(&sockets[i].output) > 0)
859 FD_SET(sockets[i].fd, *fdwp);
860 break;
861 default:
862 break;
863 }
864 }
865 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000866}
867
Ben Lindstrombba81212001-06-25 05:01:22 +0000868static void
Damien Miller95def091999-11-25 00:26:21 +1100869after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000870{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000871 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100872 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100873 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000874 int len, sock;
875 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000876 uid_t euid;
877 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000878
Damien Miller95def091999-11-25 00:26:21 +1100879 for (i = 0; i < sockets_alloc; i++)
880 switch (sockets[i].type) {
881 case AUTH_UNUSED:
882 break;
883 case AUTH_SOCKET:
884 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100885 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000886 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100887 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100888 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100889 error("accept from AUTH_SOCKET: %s",
890 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100891 break;
892 }
Damien Miller538f1812002-09-12 09:51:10 +1000893 if (getpeereid(sock, &euid, &egid) < 0) {
894 error("getpeereid %d failed: %s",
895 sock, strerror(errno));
896 close(sock);
897 break;
898 }
Damien Milleraf9de382002-10-03 11:54:35 +1000899 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000900 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000901 "peer euid %u != uid %u",
902 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000903 close(sock);
904 break;
905 }
Damien Miller95def091999-11-25 00:26:21 +1100906 new_socket(AUTH_CONNECTION, sock);
907 }
908 break;
909 case AUTH_CONNECTION:
910 if (buffer_len(&sockets[i].output) > 0 &&
911 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000912 do {
913 len = write(sockets[i].fd,
914 buffer_ptr(&sockets[i].output),
915 buffer_len(&sockets[i].output));
916 if (len == -1 && (errno == EAGAIN ||
917 errno == EINTR))
918 continue;
919 break;
920 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100921 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000922 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100923 break;
924 }
925 buffer_consume(&sockets[i].output, len);
926 }
927 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000928 do {
929 len = read(sockets[i].fd, buf, sizeof(buf));
930 if (len == -1 && (errno == EAGAIN ||
931 errno == EINTR))
932 continue;
933 break;
934 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100935 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000936 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100937 break;
938 }
939 buffer_append(&sockets[i].input, buf, len);
940 process_message(&sockets[i]);
941 }
942 break;
943 default:
944 fatal("Unknown type %d", sockets[i].type);
945 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000946}
947
Ben Lindstrombba81212001-06-25 05:01:22 +0000948static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000949cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000950{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000951 if (socket_name[0])
952 unlink(socket_name);
953 if (socket_dir[0])
954 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000955}
956
Darren Tucker3e33cec2003-10-02 16:12:36 +1000957void
Damien Miller792c5111999-10-29 09:47:09 +1000958cleanup_exit(int i)
959{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000960 cleanup_socket();
961 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000962}
963
Damien Miller1c13bd82006-03-26 14:28:14 +1100964/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000965static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000966cleanup_handler(int sig)
967{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000968 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000969 _exit(2);
970}
971
Damien Miller1c13bd82006-03-26 14:28:14 +1100972/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +0000973static void
Ben Lindstrom0250da02001-07-22 20:44:00 +0000974check_parent_exists(int sig)
975{
976 int save_errno = errno;
977
978 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
979 /* printf("Parent has died - Authentication agent exiting.\n"); */
980 cleanup_handler(sig); /* safe */
981 }
Damien Miller0cbb9de2003-06-04 22:56:15 +1000982 mysignal(SIGALRM, check_parent_exists);
Ben Lindstrom0250da02001-07-22 20:44:00 +0000983 alarm(10);
984 errno = save_errno;
985}
986
987static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +0000988usage(void)
Damien Miller792c5111999-10-29 09:47:09 +1000989{
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000990 fprintf(stderr, "Usage: %s [options] [command [args ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000991 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +0000992 fprintf(stderr, "Options:\n");
993 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
994 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
995 fprintf(stderr, " -k Kill the current agent.\n");
996 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +0000997 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +1100998 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +1100999 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001000}
1001
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001002int
1003main(int ac, char **av)
1004{
Damien Miller6c711792003-01-24 11:36:23 +11001005 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001006 int sock, fd, ch;
1007 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001008 char *shell, *format, *pidstr, *agentsocket = NULL;
1009 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001010 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001011#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001012 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001013#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001014 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001015 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001016 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001017 pid_t pid;
1018 char pidstrbuf[1 + 3 * sizeof pid];
Kevin Stevesde41bc62000-12-14 00:04:08 +00001019
Darren Tuckerce321d82005-10-03 18:11:24 +10001020 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1021 sanitise_stdfd();
1022
Damien Miller6cffb9a2002-09-04 16:20:26 +10001023 /* drop */
1024 setegid(getgid());
1025 setgid(getgid());
1026
Damien Miller6c4914a2004-03-03 11:08:59 +11001027#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1028 /* Disable ptrace on Linux without sgid bit */
1029 prctl(PR_SET_DUMPABLE, 0);
1030#endif
1031
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001032 SSLeay_add_all_algorithms();
1033
Damien Miller59d3d5b2003-08-22 09:34:41 +10001034 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001035 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001036 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001037
Damien Miller53d81482003-01-22 11:47:19 +11001038 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001039 switch (ch) {
1040 case 'c':
1041 if (s_flag)
1042 usage();
1043 c_flag++;
1044 break;
1045 case 'k':
1046 k_flag++;
1047 break;
1048 case 's':
1049 if (c_flag)
1050 usage();
1051 s_flag++;
1052 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001053 case 'd':
1054 if (d_flag)
1055 usage();
1056 d_flag++;
1057 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001058 case 'a':
1059 agentsocket = optarg;
1060 break;
Damien Miller53d81482003-01-22 11:47:19 +11001061 case 't':
1062 if ((lifetime = convtime(optarg)) == -1) {
1063 fprintf(stderr, "Invalid lifetime\n");
1064 usage();
1065 }
1066 break;
Damien Miller95def091999-11-25 00:26:21 +11001067 default:
1068 usage();
1069 }
Damien Miller792c5111999-10-29 09:47:09 +10001070 }
Damien Miller95def091999-11-25 00:26:21 +11001071 ac -= optind;
1072 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001073
Ben Lindstromd94580c2001-07-04 03:48:02 +00001074 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001075 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001076
Ben Lindstromeecdf232002-04-02 21:03:51 +00001077 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001078 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001079 if (shell != NULL &&
1080 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001081 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001082 }
Damien Miller95def091999-11-25 00:26:21 +11001083 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001084 const char *errstr = NULL;
1085
Damien Miller95def091999-11-25 00:26:21 +11001086 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1087 if (pidstr == NULL) {
1088 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001089 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001090 exit(1);
1091 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001092 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1093 if (errstr) {
1094 fprintf(stderr,
1095 "%s=\"%s\", which is not a good PID: %s\n",
1096 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001097 exit(1);
1098 }
1099 if (kill(pid, SIGTERM) == -1) {
1100 perror("kill");
1101 exit(1);
1102 }
1103 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1104 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1105 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001106 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001107 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001108 }
Damien Miller95def091999-11-25 00:26:21 +11001109 parent_pid = getpid();
1110
Ben Lindstromb7788f32002-06-06 21:46:08 +00001111 if (agentsocket == NULL) {
1112 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001113 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001114 if (mkdtemp(socket_dir) == NULL) {
1115 perror("mkdtemp: private socket dir");
1116 exit(1);
1117 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001118 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1119 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001120 } else {
1121 /* Try to use specified agent socket */
1122 socket_dir[0] = '\0';
1123 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001124 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125
Damien Miller5428f641999-11-25 11:54:57 +11001126 /*
1127 * Create socket early so it will exist before command gets run from
1128 * the parent.
1129 */
Damien Miller95def091999-11-25 00:26:21 +11001130 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1131 if (sock < 0) {
1132 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001133 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001134 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001135 }
Damien Miller95def091999-11-25 00:26:21 +11001136 memset(&sunaddr, 0, sizeof(sunaddr));
1137 sunaddr.sun_family = AF_UNIX;
1138 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001139 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001140 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001141 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001142 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001143 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001144 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001145 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001146 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001147 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001148 perror("listen");
1149 cleanup_exit(1);
1150 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001151
Damien Miller5428f641999-11-25 11:54:57 +11001152 /*
1153 * Fork, and have the parent execute the command, if any, or present
1154 * the socket data. The child continues as the authentication agent.
1155 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001156 if (d_flag) {
1157 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1158 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1159 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1160 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001161 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001162 goto skip;
1163 }
Damien Miller95def091999-11-25 00:26:21 +11001164 pid = fork();
1165 if (pid == -1) {
1166 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001167 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001168 }
1169 if (pid != 0) { /* Parent - execute the given command. */
1170 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001171 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001172 if (ac == 0) {
1173 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1174 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001175 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001176 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001177 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001178 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001179 exit(0);
1180 }
Damien Millere4340be2000-09-16 13:29:08 +11001181 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1182 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1183 perror("setenv");
1184 exit(1);
1185 }
Damien Miller95def091999-11-25 00:26:21 +11001186 execvp(av[0], av);
1187 perror(av[0]);
1188 exit(1);
1189 }
Damien Millerc653b892002-02-08 22:05:41 +11001190 /* child */
1191 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001192
1193 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001194 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001195 cleanup_exit(1);
1196 }
1197
1198 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001199 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1200 /* XXX might close listen socket */
1201 (void)dup2(fd, STDIN_FILENO);
1202 (void)dup2(fd, STDOUT_FILENO);
1203 (void)dup2(fd, STDERR_FILENO);
1204 if (fd > 2)
1205 close(fd);
1206 }
Damien Miller95def091999-11-25 00:26:21 +11001207
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001208#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001209 /* deny core dumps, since memory contains unencrypted private keys */
1210 rlim.rlim_cur = rlim.rlim_max = 0;
1211 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001212 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001213 cleanup_exit(1);
1214 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001215#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001216
1217skip:
Damien Miller95def091999-11-25 00:26:21 +11001218 new_socket(AUTH_SOCKET, sock);
1219 if (ac > 0) {
Damien Miller0cbb9de2003-06-04 22:56:15 +10001220 mysignal(SIGALRM, check_parent_exists);
Damien Miller95def091999-11-25 00:26:21 +11001221 alarm(10);
1222 }
Damien Millerad833b32000-08-23 10:46:23 +10001223 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001224 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001225 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001226 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001227 signal(SIGHUP, cleanup_handler);
1228 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001229 nalloc = 0;
1230
Damien Miller95def091999-11-25 00:26:21 +11001231 while (1) {
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001232 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc);
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001233 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001234 if (errno == EINTR)
1235 continue;
Damien Millerc653b892002-02-08 22:05:41 +11001236 fatal("select: %s", strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +11001237 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001238 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001239 }
1240 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001241}