blob: 6f8727b33066b5279c1705c992a8bb1a008712a3 [file] [log] [blame]
Damien Miller4c7728c2007-10-26 14:25:31 +10001/* $OpenBSD: ssh-agent.c,v 1.157 2007/09/25 23:48:57 canacar Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
Ben Lindstrom44697232001-07-04 03:32:30 +000014 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110035 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
37#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110038
39#include <sys/types.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100040#include <sys/param.h>
41#include <sys/resource.h>
Damien Miller42fb0682006-03-15 14:03:06 +110042#include <sys/stat.h>
Damien Millere3b60b52006-07-10 21:08:03 +100043#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100044#ifdef HAVE_SYS_TIME_H
45# include <sys/time.h>
46#endif
Damien Miller574c41f2006-03-15 11:40:10 +110047#ifdef HAVE_SYS_UN_H
48# include <sys/un.h>
49#endif
Damien Miller9b481512002-09-12 10:43:29 +100050#include "openbsd-compat/sys-queue.h"
Damien Millere3b60b52006-07-10 21:08:03 +100051
Damien Millere3476ed2006-07-24 14:13:33 +100052#include <openssl/evp.h>
53#include <openssl/md5.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110054#include "openbsd-compat/openssl-compat.h"
Damien Millere3476ed2006-07-24 14:13:33 +100055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100057#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110058#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110059# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110060#endif
Damien Miller6ff3cad2006-03-15 11:52:09 +110061#include <signal.h>
Damien Millerded319c2006-09-01 15:38:36 +100062#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100063#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100064#include <stdlib.h>
Damien Miller5598b4f2006-07-24 14:09:40 +100065#include <time.h>
Damien Millere3476ed2006-07-24 14:13:33 +100066#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100067#include <unistd.h>
Damien Miller6ff3cad2006-03-15 11:52:09 +110068
Damien Millerd7834352006-08-05 12:39:39 +100069#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070#include "ssh.h"
71#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072#include "buffer.h"
Damien Miller994cf142000-07-21 10:19:44 +100073#include "key.h"
74#include "authfd.h"
Damien Miller62cee002000-09-23 17:15:56 +110075#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
Damien Miller6c711792003-01-24 11:36:23 +110077#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Ben Lindstrom3f471632001-07-04 03:53:15 +000079#ifdef SMARTCARD
Ben Lindstrom3f471632001-07-04 03:53:15 +000080#include "scard.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000081#endif
Ben Lindstrom3f471632001-07-04 03:53:15 +000082
Damien Miller6c4914a2004-03-03 11:08:59 +110083#if defined(HAVE_SYS_PRCTL_H)
84#include <sys/prctl.h> /* For prctl() and PR_SET_DUMPABLE */
85#endif
86
Ben Lindstrom65366a82001-12-06 16:32:47 +000087typedef enum {
88 AUTH_UNUSED,
89 AUTH_SOCKET,
90 AUTH_CONNECTION
91} sock_type;
92
Damien Miller95def091999-11-25 00:26:21 +110093typedef struct {
94 int fd;
Ben Lindstrom65366a82001-12-06 16:32:47 +000095 sock_type type;
Damien Miller95def091999-11-25 00:26:21 +110096 Buffer input;
97 Buffer output;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +000098 Buffer request;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099} SocketEntry;
100
Ben Lindstrom46c16222000-12-22 01:43:59 +0000101u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102SocketEntry *sockets = NULL;
103
Damien Miller1a534ae2002-01-22 23:26:13 +1100104typedef struct identity {
105 TAILQ_ENTRY(identity) next;
Damien Millerad833b32000-08-23 10:46:23 +1000106 Key *key;
Damien Miller95def091999-11-25 00:26:21 +1100107 char *comment;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000108 u_int death;
Damien Miller6c711792003-01-24 11:36:23 +1100109 u_int confirm;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110} Identity;
111
Damien Millerad833b32000-08-23 10:46:23 +1000112typedef struct {
113 int nentries;
Damien Miller1a534ae2002-01-22 23:26:13 +1100114 TAILQ_HEAD(idqueue, identity) idlist;
Damien Millerad833b32000-08-23 10:46:23 +1000115} Idtab;
116
117/* private key table, one per protocol version */
118Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
120int max_fd = 0;
121
122/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +1000123pid_t parent_pid = -1;
Darren Tucker2812dc92007-03-21 20:45:06 +1100124u_int parent_alive_interval = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
126/* pathname and directory for AUTH_SOCKET */
Damien Miller07d86be2006-03-26 14:19:21 +1100127char socket_name[MAXPATHLEN];
128char socket_dir[MAXPATHLEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Ben Lindstrom2f717042002-06-06 21:52:03 +0000130/* locking */
131int locked = 0;
132char *lock_passwd = NULL;
133
Damien Miller95def091999-11-25 00:26:21 +1100134extern char *__progname;
Damien Miller95def091999-11-25 00:26:21 +1100135
Damien Miller53d81482003-01-22 11:47:19 +1100136/* Default lifetime (0 == forever) */
137static int lifetime = 0;
138
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void
Damien Miller58f34862002-09-04 16:31:21 +1000140close_socket(SocketEntry *e)
141{
Damien Miller58f34862002-09-04 16:31:21 +1000142 close(e->fd);
143 e->fd = -1;
144 e->type = AUTH_UNUSED;
145 buffer_free(&e->input);
146 buffer_free(&e->output);
147 buffer_free(&e->request);
148}
149
150static void
Damien Millerad833b32000-08-23 10:46:23 +1000151idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Millerad833b32000-08-23 10:46:23 +1000153 int i;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000154
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000155 for (i = 0; i <=2; i++) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100156 TAILQ_INIT(&idtable[i].idlist);
Damien Millerad833b32000-08-23 10:46:23 +1000157 idtable[i].nentries = 0;
158 }
159}
160
161/* return private key table for requested protocol version */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static Idtab *
Damien Millerad833b32000-08-23 10:46:23 +1000163idtab_lookup(int version)
164{
165 if (version < 1 || version > 2)
166 fatal("internal error, bad protocol version %d", version);
167 return &idtable[version];
168}
169
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000170static void
171free_identity(Identity *id)
172{
173 key_free(id->key);
174 xfree(id->comment);
175 xfree(id);
176}
177
Damien Millerad833b32000-08-23 10:46:23 +1000178/* return matching private key for given public key */
Damien Miller1a534ae2002-01-22 23:26:13 +1100179static Identity *
180lookup_identity(Key *key, int version)
Damien Millerad833b32000-08-23 10:46:23 +1000181{
Damien Miller1a534ae2002-01-22 23:26:13 +1100182 Identity *id;
183
Damien Millerad833b32000-08-23 10:46:23 +1000184 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100185 TAILQ_FOREACH(id, &tab->idlist, next) {
186 if (key_equal(key, id->key))
187 return (id);
Damien Millerad833b32000-08-23 10:46:23 +1000188 }
Damien Miller1a534ae2002-01-22 23:26:13 +1100189 return (NULL);
190}
191
Damien Miller6c711792003-01-24 11:36:23 +1100192/* Check confirmation of keysign request */
193static int
194confirm_key(Identity *id)
195{
Darren Tuckerce327b62004-11-05 20:38:03 +1100196 char *p;
Damien Miller6c711792003-01-24 11:36:23 +1100197 int ret = -1;
198
199 p = key_fingerprint(id->key, SSH_FP_MD5, SSH_FP_HEX);
Darren Tuckerce327b62004-11-05 20:38:03 +1100200 if (ask_permission("Allow use of key %s?\nKey fingerprint %s.",
201 id->comment, p))
202 ret = 0;
Damien Miller6c711792003-01-24 11:36:23 +1100203 xfree(p);
Darren Tuckerce327b62004-11-05 20:38:03 +1100204
Damien Miller6c711792003-01-24 11:36:23 +1100205 return (ret);
206}
207
Damien Millerad833b32000-08-23 10:46:23 +1000208/* send list of supported public keys to 'client' */
Ben Lindstrombba81212001-06-25 05:01:22 +0000209static void
Damien Millerad833b32000-08-23 10:46:23 +1000210process_request_identities(SocketEntry *e, int version)
211{
212 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100213 Identity *id;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000214 Buffer msg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000215
Damien Miller95def091999-11-25 00:26:21 +1100216 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000217 buffer_put_char(&msg, (version == 1) ?
218 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
219 buffer_put_int(&msg, tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100220 TAILQ_FOREACH(id, &tab->idlist, next) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100221 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000222 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
223 buffer_put_bignum(&msg, id->key->rsa->e);
224 buffer_put_bignum(&msg, id->key->rsa->n);
225 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000226 u_char *blob;
227 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100228 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000229 buffer_put_string(&msg, blob, blen);
230 xfree(blob);
231 }
232 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100233 }
234 buffer_put_int(&e->output, buffer_len(&msg));
235 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
236 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237}
238
Damien Millerad833b32000-08-23 10:46:23 +1000239/* ssh1 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000240static void
Damien Millerad833b32000-08-23 10:46:23 +1000241process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000243 u_char buf[32], mdbuf[16], session_id[16];
244 u_int response_type;
Damien Millerad833b32000-08-23 10:46:23 +1000245 BIGNUM *challenge;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000246 Identity *id;
Damien Millerad833b32000-08-23 10:46:23 +1000247 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100248 Buffer msg;
249 MD5_CTX md;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000250 Key *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000251
Damien Miller95def091999-11-25 00:26:21 +1100252 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100253 key = key_new(KEY_RSA1);
Damien Millerda755162002-01-22 23:09:22 +1100254 if ((challenge = BN_new()) == NULL)
255 fatal("process_authentication_challenge1: BN_new failed");
Damien Millerad833b32000-08-23 10:46:23 +1000256
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000257 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000258 buffer_get_bignum(&e->request, key->rsa->e);
259 buffer_get_bignum(&e->request, key->rsa->n);
260 buffer_get_bignum(&e->request, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Millerad833b32000-08-23 10:46:23 +1000262 /* Only protocol 1.1 is supported */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000263 if (buffer_len(&e->request) == 0)
Damien Millerad833b32000-08-23 10:46:23 +1000264 goto failure;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000265 buffer_get(&e->request, session_id, 16);
266 response_type = buffer_get_int(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000267 if (response_type != 1)
268 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller1a534ae2002-01-22 23:26:13 +1100270 id = lookup_identity(key, 1);
Damien Miller6c711792003-01-24 11:36:23 +1100271 if (id != NULL && (!id->confirm || confirm_key(id) == 0)) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100272 Key *private = id->key;
Damien Millerad833b32000-08-23 10:46:23 +1000273 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100274 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
275 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100276
Damien Millerad833b32000-08-23 10:46:23 +1000277 /* The response is MD5 of decrypted challenge plus session id. */
278 len = BN_num_bytes(challenge);
279 if (len <= 0 || len > 32) {
Damien Miller996acd22003-04-09 20:59:48 +1000280 logit("process_authentication_challenge: bad challenge length %d", len);
Damien Millerad833b32000-08-23 10:46:23 +1000281 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100282 }
Damien Millerad833b32000-08-23 10:46:23 +1000283 memset(buf, 0, 32);
284 BN_bn2bin(challenge, buf + 32 - len);
285 MD5_Init(&md);
286 MD5_Update(&md, buf, 32);
287 MD5_Update(&md, session_id, 16);
288 MD5_Final(mdbuf, &md);
289
290 /* Send the response. */
291 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
292 for (i = 0; i < 16; i++)
293 buffer_put_char(&msg, mdbuf[i]);
294 goto send;
295 }
296
297failure:
298 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100299 buffer_put_char(&msg, SSH_AGENT_FAILURE);
300send:
301 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000302 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
303 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100304 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000305 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Millerad833b32000-08-23 10:46:23 +1000308/* ssh2 only */
Ben Lindstrombba81212001-06-25 05:01:22 +0000309static void
Damien Millerad833b32000-08-23 10:46:23 +1000310process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000312 u_char *blob, *data, *signature = NULL;
313 u_int blen, dlen, slen = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000314 extern int datafellows;
315 int ok = -1, flags;
Damien Millerad833b32000-08-23 10:46:23 +1000316 Buffer msg;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000317 Key *key;
Damien Millerad833b32000-08-23 10:46:23 +1000318
319 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000320
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000321 blob = buffer_get_string(&e->request, &blen);
322 data = buffer_get_string(&e->request, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100323
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000324 flags = buffer_get_int(&e->request);
Damien Miller62cee002000-09-23 17:15:56 +1100325 if (flags & SSH_AGENT_OLD_SIGNATURE)
326 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000327
Damien Miller0bc1bd82000-11-13 22:57:25 +1100328 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000329 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100330 Identity *id = lookup_identity(key, 2);
Damien Miller6c711792003-01-24 11:36:23 +1100331 if (id != NULL && (!id->confirm || confirm_key(id) == 0))
Damien Miller1a534ae2002-01-22 23:26:13 +1100332 ok = key_sign(id->key, &signature, &slen, data, dlen);
Damien Miller40b59852006-06-13 13:00:25 +1000333 key_free(key);
Damien Millerad833b32000-08-23 10:46:23 +1000334 }
Damien Millerad833b32000-08-23 10:46:23 +1000335 buffer_init(&msg);
336 if (ok == 0) {
337 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
338 buffer_put_string(&msg, signature, slen);
339 } else {
340 buffer_put_char(&msg, SSH_AGENT_FAILURE);
341 }
342 buffer_put_int(&e->output, buffer_len(&msg));
343 buffer_append(&e->output, buffer_ptr(&msg),
344 buffer_len(&msg));
345 buffer_free(&msg);
346 xfree(data);
347 xfree(blob);
348 if (signature != NULL)
349 xfree(signature);
350}
351
352/* shared */
Ben Lindstrombba81212001-06-25 05:01:22 +0000353static void
Damien Millerad833b32000-08-23 10:46:23 +1000354process_remove_identity(SocketEntry *e, int version)
355{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000356 u_int blen, bits;
357 int success = 0;
Damien Miller1a534ae2002-01-22 23:26:13 +1100358 Key *key = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000359 u_char *blob;
Damien Miller7e8e8201999-11-16 13:37:16 +1100360
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000361 switch (version) {
Damien Millerad833b32000-08-23 10:46:23 +1000362 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100363 key = key_new(KEY_RSA1);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000364 bits = buffer_get_int(&e->request);
365 buffer_get_bignum(&e->request, key->rsa->e);
366 buffer_get_bignum(&e->request, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
Damien Millerad833b32000-08-23 10:46:23 +1000368 if (bits != key_size(key))
Damien Miller996acd22003-04-09 20:59:48 +1000369 logit("Warning: identity keysize mismatch: actual %u, announced %u",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000370 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000371 break;
372 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000373 blob = buffer_get_string(&e->request, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100374 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000375 xfree(blob);
376 break;
377 }
378 if (key != NULL) {
Damien Miller1a534ae2002-01-22 23:26:13 +1100379 Identity *id = lookup_identity(key, version);
380 if (id != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100381 /*
382 * We have this key. Free the old key. Since we
Damien Miller788f2122005-11-05 15:14:59 +1100383 * don't want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000384 * the array, we actually free the key there and move
385 * all the entries between the empty slot and the end
386 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100387 */
Damien Millerad833b32000-08-23 10:46:23 +1000388 Idtab *tab = idtab_lookup(version);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100389 if (tab->nentries < 1)
390 fatal("process_remove_identity: "
391 "internal error: tab->nentries %d",
392 tab->nentries);
Damien Miller1a534ae2002-01-22 23:26:13 +1100393 TAILQ_REMOVE(&tab->idlist, id, next);
394 free_identity(id);
Damien Millerad833b32000-08-23 10:46:23 +1000395 tab->nentries--;
396 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100397 }
Damien Millerad833b32000-08-23 10:46:23 +1000398 key_free(key);
399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000400 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000401 buffer_put_char(&e->output,
402 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000403}
404
Ben Lindstrombba81212001-06-25 05:01:22 +0000405static void
Damien Millerad833b32000-08-23 10:46:23 +1000406process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407{
Damien Millerad833b32000-08-23 10:46:23 +1000408 Idtab *tab = idtab_lookup(version);
Damien Miller1a534ae2002-01-22 23:26:13 +1100409 Identity *id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000410
Damien Miller95def091999-11-25 00:26:21 +1100411 /* Loop over all identities and clear the keys. */
Damien Miller1a534ae2002-01-22 23:26:13 +1100412 for (id = TAILQ_FIRST(&tab->idlist); id;
413 id = TAILQ_FIRST(&tab->idlist)) {
414 TAILQ_REMOVE(&tab->idlist, id, next);
415 free_identity(id);
Damien Miller95def091999-11-25 00:26:21 +1100416 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller95def091999-11-25 00:26:21 +1100418 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000419 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420
421 /* Send success. */
422 buffer_put_int(&e->output, 1);
423 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
Damien Miller95def091999-11-25 00:26:21 +1100424}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000425
Darren Tucker2812dc92007-03-21 20:45:06 +1100426/* removes expired keys and returns number of seconds until the next expiry */
427static u_int
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000428reaper(void)
429{
Darren Tucker2812dc92007-03-21 20:45:06 +1100430 u_int deadline = 0, now = time(NULL);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000431 Identity *id, *nxt;
432 int version;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000433 Idtab *tab;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000434
435 for (version = 1; version < 3; version++) {
436 tab = idtab_lookup(version);
437 for (id = TAILQ_FIRST(&tab->idlist); id; id = nxt) {
438 nxt = TAILQ_NEXT(id, next);
Darren Tucker2812dc92007-03-21 20:45:06 +1100439 if (id->death == 0)
440 continue;
441 if (now >= id->death) {
Darren Tuckercf0d2db2007-02-28 21:19:58 +1100442 debug("expiring key '%s'", id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000443 TAILQ_REMOVE(&tab->idlist, id, next);
444 free_identity(id);
445 tab->nentries--;
Darren Tucker2812dc92007-03-21 20:45:06 +1100446 } else
447 deadline = (deadline == 0) ? id->death :
448 MIN(deadline, id->death);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000449 }
450 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100451 if (deadline == 0 || deadline <= now)
452 return 0;
453 else
454 return (deadline - now);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000455}
456
457static void
Damien Millerad833b32000-08-23 10:46:23 +1000458process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100459{
Damien Millerad833b32000-08-23 10:46:23 +1000460 Idtab *tab = idtab_lookup(version);
Damien Miller4c7728c2007-10-26 14:25:31 +1000461 Identity *id;
Damien Miller6c711792003-01-24 11:36:23 +1100462 int type, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000463 char *type_name, *comment;
464 Key *k = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100465
Damien Millerad833b32000-08-23 10:46:23 +1000466 switch (version) {
467 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100468 k = key_new_private(KEY_RSA1);
Ben Lindstromc5a7f4f2002-06-25 23:19:13 +0000469 (void) buffer_get_int(&e->request); /* ignored */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000470 buffer_get_bignum(&e->request, k->rsa->n);
471 buffer_get_bignum(&e->request, k->rsa->e);
472 buffer_get_bignum(&e->request, k->rsa->d);
473 buffer_get_bignum(&e->request, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100474
Damien Millerad833b32000-08-23 10:46:23 +1000475 /* SSH and SSL have p and q swapped */
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000476 buffer_get_bignum(&e->request, k->rsa->q); /* p */
477 buffer_get_bignum(&e->request, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100478
Damien Millerad833b32000-08-23 10:46:23 +1000479 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000480 rsa_generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000481 break;
482 case 2:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000483 type_name = buffer_get_string(&e->request, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000484 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100485 xfree(type_name);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000486 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100487 case KEY_DSA:
488 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000489 buffer_get_bignum2(&e->request, k->dsa->p);
490 buffer_get_bignum2(&e->request, k->dsa->q);
491 buffer_get_bignum2(&e->request, k->dsa->g);
492 buffer_get_bignum2(&e->request, k->dsa->pub_key);
493 buffer_get_bignum2(&e->request, k->dsa->priv_key);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100494 break;
495 case KEY_RSA:
496 k = key_new_private(type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000497 buffer_get_bignum2(&e->request, k->rsa->n);
498 buffer_get_bignum2(&e->request, k->rsa->e);
499 buffer_get_bignum2(&e->request, k->rsa->d);
500 buffer_get_bignum2(&e->request, k->rsa->iqmp);
501 buffer_get_bignum2(&e->request, k->rsa->p);
502 buffer_get_bignum2(&e->request, k->rsa->q);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503
504 /* Generate additional parameters */
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000505 rsa_generate_additional_parameters(k->rsa);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100506 break;
507 default:
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000508 buffer_clear(&e->request);
Damien Millerad833b32000-08-23 10:46:23 +1000509 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100510 }
Damien Millerad833b32000-08-23 10:46:23 +1000511 break;
512 }
Damien Millerc51d0732003-03-15 11:37:09 +1100513 /* enable blinding */
514 switch (k->type) {
515 case KEY_RSA:
516 case KEY_RSA1:
517 if (RSA_blinding_on(k->rsa, NULL) != 1) {
518 error("process_add_identity: RSA_blinding_on failed");
519 key_free(k);
520 goto send;
521 }
522 break;
523 }
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000524 comment = buffer_get_string(&e->request, NULL);
Damien Millerad833b32000-08-23 10:46:23 +1000525 if (k == NULL) {
526 xfree(comment);
527 goto send;
528 }
529 success = 1;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000530 while (buffer_len(&e->request)) {
531 switch (buffer_get_char(&e->request)) {
Ben Lindstromc90f8a92002-06-21 00:06:54 +0000532 case SSH_AGENT_CONSTRAIN_LIFETIME:
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000533 death = time(NULL) + buffer_get_int(&e->request);
534 break;
Damien Miller6c711792003-01-24 11:36:23 +1100535 case SSH_AGENT_CONSTRAIN_CONFIRM:
536 confirm = 1;
537 break;
Ben Lindstrom4eb4c4e2002-06-21 00:04:48 +0000538 default:
539 break;
540 }
541 }
Damien Miller53d81482003-01-22 11:47:19 +1100542 if (lifetime && !death)
543 death = time(NULL) + lifetime;
Damien Miller4c7728c2007-10-26 14:25:31 +1000544 if ((id = lookup_identity(k, version)) == NULL) {
545 id = xmalloc(sizeof(Identity));
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000546 id->key = k;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000547 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
548 /* Increment the number of identities. */
549 tab->nentries++;
550 } else {
551 key_free(k);
Damien Miller4c7728c2007-10-26 14:25:31 +1000552 xfree(id->comment);
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000553 }
Damien Miller4c7728c2007-10-26 14:25:31 +1000554 id->comment = comment;
555 id->death = death;
556 id->confirm = confirm;
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000557send:
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000558 buffer_put_int(&e->output, 1);
559 buffer_put_char(&e->output,
560 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
561}
562
Ben Lindstrom2f717042002-06-06 21:52:03 +0000563/* XXX todo: encrypt sensitive data with passphrase */
564static void
565process_lock_agent(SocketEntry *e, int lock)
566{
Ben Lindstrom2f717042002-06-06 21:52:03 +0000567 int success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000568 char *passwd;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000569
570 passwd = buffer_get_string(&e->request, NULL);
571 if (locked && !lock && strcmp(passwd, lock_passwd) == 0) {
572 locked = 0;
573 memset(lock_passwd, 0, strlen(lock_passwd));
574 xfree(lock_passwd);
575 lock_passwd = NULL;
576 success = 1;
577 } else if (!locked && lock) {
578 locked = 1;
579 lock_passwd = xstrdup(passwd);
580 success = 1;
581 }
582 memset(passwd, 0, strlen(passwd));
583 xfree(passwd);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000584
Ben Lindstrom2f717042002-06-06 21:52:03 +0000585 buffer_put_int(&e->output, 1);
586 buffer_put_char(&e->output,
587 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Ben Lindstrom2f717042002-06-06 21:52:03 +0000588}
589
590static void
591no_identities(SocketEntry *e, u_int type)
592{
593 Buffer msg;
594
595 buffer_init(&msg);
596 buffer_put_char(&msg,
597 (type == SSH_AGENTC_REQUEST_RSA_IDENTITIES) ?
598 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
599 buffer_put_int(&msg, 0);
600 buffer_put_int(&e->output, buffer_len(&msg));
601 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
602 buffer_free(&msg);
603}
Ben Lindstrom3f471632001-07-04 03:53:15 +0000604
605#ifdef SMARTCARD
606static void
607process_add_smartcard_key (SocketEntry *e)
608{
Ben Lindstromba72d302002-03-22 03:51:06 +0000609 char *sc_reader_id = NULL, *pin;
Damien Millerd94f20d2003-06-11 22:06:33 +1000610 int i, version, success = 0, death = 0, confirm = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000611 Key **keys, *k;
612 Identity *id;
613 Idtab *tab;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100614
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000615 sc_reader_id = buffer_get_string(&e->request, NULL);
616 pin = buffer_get_string(&e->request, NULL);
Damien Millerd94f20d2003-06-11 22:06:33 +1000617
618 while (buffer_len(&e->request)) {
619 switch (buffer_get_char(&e->request)) {
620 case SSH_AGENT_CONSTRAIN_LIFETIME:
621 death = time(NULL) + buffer_get_int(&e->request);
622 break;
623 case SSH_AGENT_CONSTRAIN_CONFIRM:
624 confirm = 1;
625 break;
626 default:
627 break;
628 }
629 }
630 if (lifetime && !death)
631 death = time(NULL) + lifetime;
632
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000633 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000634 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000635 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000636
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000637 if (keys == NULL || keys[0] == NULL) {
638 error("sc_get_keys failed");
Ben Lindstrom3f471632001-07-04 03:53:15 +0000639 goto send;
640 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000641 for (i = 0; keys[i] != NULL; i++) {
642 k = keys[i];
643 version = k->type == KEY_RSA1 ? 1 : 2;
644 tab = idtab_lookup(version);
645 if (lookup_identity(k, version) == NULL) {
646 id = xmalloc(sizeof(Identity));
647 id->key = k;
Damien Miller56a0bb02003-06-18 20:28:40 +1000648 id->comment = sc_get_key_label(k);
Damien Millerd94f20d2003-06-11 22:06:33 +1000649 id->death = death;
650 id->confirm = confirm;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000651 TAILQ_INSERT_TAIL(&tab->idlist, id, next);
652 tab->nentries++;
653 success = 1;
654 } else {
655 key_free(k);
656 }
657 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000658 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000659 xfree(keys);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000660send:
661 buffer_put_int(&e->output, 1);
662 buffer_put_char(&e->output,
663 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
664}
665
666static void
667process_remove_smartcard_key(SocketEntry *e)
668{
Ben Lindstromba72d302002-03-22 03:51:06 +0000669 char *sc_reader_id = NULL, *pin;
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000670 int i, version, success = 0;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000671 Key **keys, *k = NULL;
672 Identity *id;
673 Idtab *tab;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000674
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000675 sc_reader_id = buffer_get_string(&e->request, NULL);
676 pin = buffer_get_string(&e->request, NULL);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000677 keys = sc_get_keys(sc_reader_id, pin);
Ben Lindstromf7db3bb2001-08-06 21:35:51 +0000678 xfree(sc_reader_id);
Ben Lindstromba72d302002-03-22 03:51:06 +0000679 xfree(pin);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000680
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000681 if (keys == NULL || keys[0] == NULL) {
682 error("sc_get_keys failed");
683 goto send;
684 }
685 for (i = 0; keys[i] != NULL; i++) {
686 k = keys[i];
687 version = k->type == KEY_RSA1 ? 1 : 2;
688 if ((id = lookup_identity(k, version)) != NULL) {
689 tab = idtab_lookup(version);
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000690 TAILQ_REMOVE(&tab->idlist, id, next);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000691 tab->nentries--;
Damien Miller1a534ae2002-01-22 23:26:13 +1100692 free_identity(id);
Ben Lindstrom3f471632001-07-04 03:53:15 +0000693 success = 1;
694 }
695 key_free(k);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000696 keys[i] = NULL;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000697 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000698 xfree(keys);
699send:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000700 buffer_put_int(&e->output, 1);
701 buffer_put_char(&e->output,
702 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
703}
Ben Lindstromffce1472001-08-06 21:57:31 +0000704#endif /* SMARTCARD */
Ben Lindstrom3f471632001-07-04 03:53:15 +0000705
Damien Millerad833b32000-08-23 10:46:23 +1000706/* dispatch incoming messages */
707
Ben Lindstrombba81212001-06-25 05:01:22 +0000708static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000709process_message(SocketEntry *e)
710{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000711 u_int msg_len, type;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000712 u_char *cp;
Ben Lindstrom61d328a2002-06-06 21:54:57 +0000713
Damien Miller95def091999-11-25 00:26:21 +1100714 if (buffer_len(&e->input) < 5)
715 return; /* Incomplete message. */
Damien Miller708d21c2002-01-22 23:18:15 +1100716 cp = buffer_ptr(&e->input);
Damien Miller3f941882006-03-31 23:13:02 +1100717 msg_len = get_u32(cp);
Damien Miller95def091999-11-25 00:26:21 +1100718 if (msg_len > 256 * 1024) {
Damien Miller58f34862002-09-04 16:31:21 +1000719 close_socket(e);
Damien Miller95def091999-11-25 00:26:21 +1100720 return;
721 }
722 if (buffer_len(&e->input) < msg_len + 4)
723 return;
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000724
725 /* move the current input to e->request */
Damien Miller95def091999-11-25 00:26:21 +1100726 buffer_consume(&e->input, 4);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000727 buffer_clear(&e->request);
728 buffer_append(&e->request, buffer_ptr(&e->input), msg_len);
729 buffer_consume(&e->input, msg_len);
730 type = buffer_get_char(&e->request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000731
Ben Lindstrom2f717042002-06-06 21:52:03 +0000732 /* check wheter agent is locked */
733 if (locked && type != SSH_AGENTC_UNLOCK) {
734 buffer_clear(&e->request);
735 switch (type) {
736 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
737 case SSH2_AGENTC_REQUEST_IDENTITIES:
738 /* send empty lists */
739 no_identities(e, type);
740 break;
741 default:
742 /* send a fail message for all other request types */
743 buffer_put_int(&e->output, 1);
744 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
745 }
746 return;
747 }
748
Ben Lindstrom3f471632001-07-04 03:53:15 +0000749 debug("type %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100750 switch (type) {
Ben Lindstrom2f717042002-06-06 21:52:03 +0000751 case SSH_AGENTC_LOCK:
752 case SSH_AGENTC_UNLOCK:
753 process_lock_agent(e, type == SSH_AGENTC_LOCK);
754 break;
Damien Millerad833b32000-08-23 10:46:23 +1000755 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100756 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000757 process_authentication_challenge1(e);
758 break;
759 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
760 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100761 break;
762 case SSH_AGENTC_ADD_RSA_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000763 case SSH_AGENTC_ADD_RSA_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000764 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100765 break;
766 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000767 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100768 break;
769 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000770 process_remove_all_identities(e, 1);
771 break;
772 /* ssh2 */
773 case SSH2_AGENTC_SIGN_REQUEST:
774 process_sign_request2(e);
775 break;
776 case SSH2_AGENTC_REQUEST_IDENTITIES:
777 process_request_identities(e, 2);
778 break;
779 case SSH2_AGENTC_ADD_IDENTITY:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000780 case SSH2_AGENTC_ADD_ID_CONSTRAINED:
Damien Millerad833b32000-08-23 10:46:23 +1000781 process_add_identity(e, 2);
782 break;
783 case SSH2_AGENTC_REMOVE_IDENTITY:
784 process_remove_identity(e, 2);
785 break;
786 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
787 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100788 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000789#ifdef SMARTCARD
790 case SSH_AGENTC_ADD_SMARTCARD_KEY:
Damien Millerd94f20d2003-06-11 22:06:33 +1000791 case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
Ben Lindstrom3f471632001-07-04 03:53:15 +0000792 process_add_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100793 break;
Ben Lindstrom3f471632001-07-04 03:53:15 +0000794 case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
795 process_remove_smartcard_key(e);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100796 break;
Ben Lindstromffce1472001-08-06 21:57:31 +0000797#endif /* SMARTCARD */
Damien Miller95def091999-11-25 00:26:21 +1100798 default:
799 /* Unknown message. Respond with failure. */
800 error("Unknown message %d", type);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000801 buffer_clear(&e->request);
Damien Miller95def091999-11-25 00:26:21 +1100802 buffer_put_int(&e->output, 1);
803 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
804 break;
805 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000806}
807
Ben Lindstrombba81212001-06-25 05:01:22 +0000808static void
Ben Lindstrom65366a82001-12-06 16:32:47 +0000809new_socket(sock_type type, int fd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000810{
Darren Tuckerfb16b242003-09-22 21:04:23 +1000811 u_int i, old_alloc, new_alloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +0000812
Damien Miller232711f2004-06-15 10:35:30 +1000813 set_nonblock(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000814
Damien Miller95def091999-11-25 00:26:21 +1100815 if (fd > max_fd)
816 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000817
Damien Miller95def091999-11-25 00:26:21 +1100818 for (i = 0; i < sockets_alloc; i++)
819 if (sockets[i].type == AUTH_UNUSED) {
820 sockets[i].fd = fd;
Damien Miller95def091999-11-25 00:26:21 +1100821 buffer_init(&sockets[i].input);
822 buffer_init(&sockets[i].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000823 buffer_init(&sockets[i].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000824 sockets[i].type = type;
Damien Miller95def091999-11-25 00:26:21 +1100825 return;
826 }
827 old_alloc = sockets_alloc;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000828 new_alloc = sockets_alloc + 10;
Damien Miller36812092006-03-26 14:22:47 +1100829 sockets = xrealloc(sockets, new_alloc, sizeof(sockets[0]));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000830 for (i = old_alloc; i < new_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +1100831 sockets[i].type = AUTH_UNUSED;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000832 sockets_alloc = new_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100833 sockets[old_alloc].fd = fd;
834 buffer_init(&sockets[old_alloc].input);
835 buffer_init(&sockets[old_alloc].output);
Ben Lindstrom21d1ed82002-06-06 21:48:57 +0000836 buffer_init(&sockets[old_alloc].request);
Darren Tuckerfb16b242003-09-22 21:04:23 +1000837 sockets[old_alloc].type = type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000838}
839
Ben Lindstrombba81212001-06-25 05:01:22 +0000840static int
Darren Tucker2812dc92007-03-21 20:45:06 +1100841prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl, u_int *nallocp,
842 struct timeval **tvpp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000843{
Darren Tucker2812dc92007-03-21 20:45:06 +1100844 u_int i, sz, deadline;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000845 int n = 0;
Darren Tucker2812dc92007-03-21 20:45:06 +1100846 static struct timeval tv;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000847
848 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100849 switch (sockets[i].type) {
850 case AUTH_SOCKET:
851 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000852 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100853 break;
854 case AUTH_UNUSED:
855 break;
856 default:
857 fatal("Unknown socket type %d", sockets[i].type);
858 break;
859 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000860 }
861
862 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000863 if (*fdrp == NULL || sz > *nallocp) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000864 if (*fdrp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000865 xfree(*fdrp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000866 if (*fdwp)
Ben Lindstrom86ebcb62001-04-04 01:53:20 +0000867 xfree(*fdwp);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000868 *fdrp = xmalloc(sz);
869 *fdwp = xmalloc(sz);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000870 *nallocp = sz;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000871 }
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +0000872 if (n < *fdl)
873 debug("XXX shrink: %d < %d", n, *fdl);
874 *fdl = n;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000875 memset(*fdrp, 0, sz);
876 memset(*fdwp, 0, sz);
877
878 for (i = 0; i < sockets_alloc; i++) {
879 switch (sockets[i].type) {
880 case AUTH_SOCKET:
881 case AUTH_CONNECTION:
882 FD_SET(sockets[i].fd, *fdrp);
883 if (buffer_len(&sockets[i].output) > 0)
884 FD_SET(sockets[i].fd, *fdwp);
885 break;
886 default:
887 break;
888 }
889 }
Darren Tucker2812dc92007-03-21 20:45:06 +1100890 deadline = reaper();
891 if (parent_alive_interval != 0)
892 deadline = (deadline == 0) ? parent_alive_interval :
893 MIN(deadline, parent_alive_interval);
894 if (deadline == 0) {
895 *tvpp = NULL;
896 } else {
897 tv.tv_sec = deadline;
898 tv.tv_usec = 0;
899 *tvpp = &tv;
900 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000901 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902}
903
Ben Lindstrombba81212001-06-25 05:01:22 +0000904static void
Damien Miller95def091999-11-25 00:26:21 +1100905after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000906{
Ben Lindstrom822b6342002-06-23 21:38:49 +0000907 struct sockaddr_un sunaddr;
Damien Miller7684ee12000-03-17 23:40:15 +1100908 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100909 char buf[1024];
Ben Lindstrom822b6342002-06-23 21:38:49 +0000910 int len, sock;
911 u_int i;
Damien Miller538f1812002-09-12 09:51:10 +1000912 uid_t euid;
913 gid_t egid;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000914
Damien Miller95def091999-11-25 00:26:21 +1100915 for (i = 0; i < sockets_alloc; i++)
916 switch (sockets[i].type) {
917 case AUTH_UNUSED:
918 break;
919 case AUTH_SOCKET:
920 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100921 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000922 sock = accept(sockets[i].fd,
Damien Miller90967402006-03-26 14:07:26 +1100923 (struct sockaddr *)&sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100924 if (sock < 0) {
Damien Millerc653b892002-02-08 22:05:41 +1100925 error("accept from AUTH_SOCKET: %s",
926 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100927 break;
928 }
Damien Miller538f1812002-09-12 09:51:10 +1000929 if (getpeereid(sock, &euid, &egid) < 0) {
930 error("getpeereid %d failed: %s",
931 sock, strerror(errno));
932 close(sock);
933 break;
934 }
Damien Milleraf9de382002-10-03 11:54:35 +1000935 if ((euid != 0) && (getuid() != euid)) {
Damien Miller538f1812002-09-12 09:51:10 +1000936 error("uid mismatch: "
Damien Millerdb30b122002-09-19 11:46:58 +1000937 "peer euid %u != uid %u",
938 (u_int) euid, (u_int) getuid());
Damien Miller538f1812002-09-12 09:51:10 +1000939 close(sock);
940 break;
941 }
Damien Miller95def091999-11-25 00:26:21 +1100942 new_socket(AUTH_CONNECTION, sock);
943 }
944 break;
945 case AUTH_CONNECTION:
946 if (buffer_len(&sockets[i].output) > 0 &&
947 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000948 do {
949 len = write(sockets[i].fd,
950 buffer_ptr(&sockets[i].output),
951 buffer_len(&sockets[i].output));
952 if (len == -1 && (errno == EAGAIN ||
953 errno == EINTR))
954 continue;
955 break;
956 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100957 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000958 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100959 break;
960 }
961 buffer_consume(&sockets[i].output, len);
962 }
963 if (FD_ISSET(sockets[i].fd, readset)) {
Ben Lindstromb3144e52001-03-06 03:31:34 +0000964 do {
965 len = read(sockets[i].fd, buf, sizeof(buf));
966 if (len == -1 && (errno == EAGAIN ||
967 errno == EINTR))
968 continue;
969 break;
970 } while (1);
Damien Miller95def091999-11-25 00:26:21 +1100971 if (len <= 0) {
Damien Miller58f34862002-09-04 16:31:21 +1000972 close_socket(&sockets[i]);
Damien Miller95def091999-11-25 00:26:21 +1100973 break;
974 }
975 buffer_append(&sockets[i].input, buf, len);
976 process_message(&sockets[i]);
977 }
978 break;
979 default:
980 fatal("Unknown type %d", sockets[i].type);
981 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982}
983
Ben Lindstrombba81212001-06-25 05:01:22 +0000984static void
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000985cleanup_socket(void)
Damien Miller792c5111999-10-29 09:47:09 +1000986{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000987 if (socket_name[0])
988 unlink(socket_name);
989 if (socket_dir[0])
990 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991}
992
Darren Tucker3e33cec2003-10-02 16:12:36 +1000993void
Damien Miller792c5111999-10-29 09:47:09 +1000994cleanup_exit(int i)
995{
Darren Tucker6fa8abd2003-09-22 21:10:21 +1000996 cleanup_socket();
997 _exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000998}
999
Damien Miller1c13bd82006-03-26 14:28:14 +11001000/*ARGSUSED*/
Ben Lindstrombba81212001-06-25 05:01:22 +00001001static void
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001002cleanup_handler(int sig)
1003{
Darren Tucker6fa8abd2003-09-22 21:10:21 +10001004 cleanup_socket();
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001005 _exit(2);
1006}
1007
Ben Lindstrombba81212001-06-25 05:01:22 +00001008static void
Darren Tucker2812dc92007-03-21 20:45:06 +11001009check_parent_exists(void)
Ben Lindstrom0250da02001-07-22 20:44:00 +00001010{
Ben Lindstrom0250da02001-07-22 20:44:00 +00001011 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
1012 /* printf("Parent has died - Authentication agent exiting.\n"); */
Darren Tucker2812dc92007-03-21 20:45:06 +11001013 cleanup_socket();
1014 _exit(2);
Ben Lindstrom0250da02001-07-22 20:44:00 +00001015 }
Ben Lindstrom0250da02001-07-22 20:44:00 +00001016}
1017
1018static void
Ben Lindstrom28072eb2001-02-10 23:13:41 +00001019usage(void)
Damien Miller792c5111999-10-29 09:47:09 +10001020{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001021 fprintf(stderr, "usage: %s [options] [command [arg ...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +00001022 __progname);
Ben Lindstromddfb1e32001-08-06 22:06:35 +00001023 fprintf(stderr, "Options:\n");
1024 fprintf(stderr, " -c Generate C-shell commands on stdout.\n");
1025 fprintf(stderr, " -s Generate Bourne shell commands on stdout.\n");
1026 fprintf(stderr, " -k Kill the current agent.\n");
1027 fprintf(stderr, " -d Debug mode.\n");
Ben Lindstromb7788f32002-06-06 21:46:08 +00001028 fprintf(stderr, " -a socket Bind agent socket to given name.\n");
Damien Miller53d81482003-01-22 11:47:19 +11001029 fprintf(stderr, " -t life Default identity lifetime (seconds).\n");
Damien Miller95def091999-11-25 00:26:21 +11001030 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001031}
1032
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001033int
1034main(int ac, char **av)
1035{
Damien Miller6c711792003-01-24 11:36:23 +11001036 int c_flag = 0, d_flag = 0, k_flag = 0, s_flag = 0;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001037 int sock, fd, ch, result, saved_errno;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001038 u_int nalloc;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001039 char *shell, *format, *pidstr, *agentsocket = NULL;
1040 fd_set *readsetp = NULL, *writesetp = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001041 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001042#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001043 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001044#endif
Ben Lindstrom3524d692001-05-03 22:59:24 +00001045 int prev_mask;
Damien Miller615f9392000-05-17 22:53:33 +10001046 extern int optind;
Ben Lindstrom883844d2002-06-23 00:20:50 +00001047 extern char *optarg;
Ben Lindstrom822b6342002-06-23 21:38:49 +00001048 pid_t pid;
1049 char pidstrbuf[1 + 3 * sizeof pid];
Darren Tucker2812dc92007-03-21 20:45:06 +11001050 struct timeval *tvp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +00001051
Darren Tuckerce321d82005-10-03 18:11:24 +10001052 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1053 sanitise_stdfd();
1054
Damien Miller6cffb9a2002-09-04 16:20:26 +10001055 /* drop */
1056 setegid(getgid());
1057 setgid(getgid());
1058
Damien Miller6c4914a2004-03-03 11:08:59 +11001059#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
1060 /* Disable ptrace on Linux without sgid bit */
1061 prctl(PR_SET_DUMPABLE, 0);
1062#endif
1063
Ben Lindstromd09fcf52001-03-29 00:29:54 +00001064 SSLeay_add_all_algorithms();
1065
Damien Miller59d3d5b2003-08-22 09:34:41 +10001066 __progname = ssh_get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001067 init_rng();
Damien Miller60bc5172001-03-19 09:38:15 +11001068 seed_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +00001069
Damien Miller53d81482003-01-22 11:47:19 +11001070 while ((ch = getopt(ac, av, "cdksa:t:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001071 switch (ch) {
1072 case 'c':
1073 if (s_flag)
1074 usage();
1075 c_flag++;
1076 break;
1077 case 'k':
1078 k_flag++;
1079 break;
1080 case 's':
1081 if (c_flag)
1082 usage();
1083 s_flag++;
1084 break;
Ben Lindstromd94580c2001-07-04 03:48:02 +00001085 case 'd':
1086 if (d_flag)
1087 usage();
1088 d_flag++;
1089 break;
Ben Lindstromb7788f32002-06-06 21:46:08 +00001090 case 'a':
1091 agentsocket = optarg;
1092 break;
Damien Miller53d81482003-01-22 11:47:19 +11001093 case 't':
1094 if ((lifetime = convtime(optarg)) == -1) {
1095 fprintf(stderr, "Invalid lifetime\n");
1096 usage();
1097 }
1098 break;
Damien Miller95def091999-11-25 00:26:21 +11001099 default:
1100 usage();
1101 }
Damien Miller792c5111999-10-29 09:47:09 +10001102 }
Damien Miller95def091999-11-25 00:26:21 +11001103 ac -= optind;
1104 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +10001105
Ben Lindstromd94580c2001-07-04 03:48:02 +00001106 if (ac > 0 && (c_flag || k_flag || s_flag || d_flag))
Damien Miller95def091999-11-25 00:26:21 +11001107 usage();
Damien Miller792c5111999-10-29 09:47:09 +10001108
Ben Lindstromeecdf232002-04-02 21:03:51 +00001109 if (ac == 0 && !c_flag && !s_flag) {
Damien Miller95def091999-11-25 00:26:21 +11001110 shell = getenv("SHELL");
Damien Miller89c3fe42006-03-31 23:11:28 +11001111 if (shell != NULL &&
1112 strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
Damien Miller95def091999-11-25 00:26:21 +11001113 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +10001114 }
Damien Miller95def091999-11-25 00:26:21 +11001115 if (k_flag) {
Damien Miller89c3fe42006-03-31 23:11:28 +11001116 const char *errstr = NULL;
1117
Damien Miller95def091999-11-25 00:26:21 +11001118 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
1119 if (pidstr == NULL) {
1120 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001121 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001122 exit(1);
1123 }
Damien Miller89c3fe42006-03-31 23:11:28 +11001124 pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
1125 if (errstr) {
1126 fprintf(stderr,
1127 "%s=\"%s\", which is not a good PID: %s\n",
1128 SSH_AGENTPID_ENV_NAME, pidstr, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001129 exit(1);
1130 }
1131 if (kill(pid, SIGTERM) == -1) {
1132 perror("kill");
1133 exit(1);
1134 }
1135 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
1136 printf(format, SSH_AUTHSOCKET_ENV_NAME);
1137 printf(format, SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001138 printf("echo Agent pid %ld killed;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001139 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +10001140 }
Damien Miller95def091999-11-25 00:26:21 +11001141 parent_pid = getpid();
1142
Ben Lindstromb7788f32002-06-06 21:46:08 +00001143 if (agentsocket == NULL) {
1144 /* Create private directory for agent socket */
Darren Tucker072a7b12003-10-15 16:10:25 +10001145 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXXXX", sizeof socket_dir);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001146 if (mkdtemp(socket_dir) == NULL) {
1147 perror("mkdtemp: private socket dir");
1148 exit(1);
1149 }
Ben Lindstromce0f6342002-06-11 16:42:49 +00001150 snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
1151 (long)parent_pid);
Ben Lindstromb7788f32002-06-06 21:46:08 +00001152 } else {
1153 /* Try to use specified agent socket */
1154 socket_dir[0] = '\0';
1155 strlcpy(socket_name, agentsocket, sizeof socket_name);
Damien Miller792c5111999-10-29 09:47:09 +10001156 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001157
Damien Miller5428f641999-11-25 11:54:57 +11001158 /*
1159 * Create socket early so it will exist before command gets run from
1160 * the parent.
1161 */
Damien Miller95def091999-11-25 00:26:21 +11001162 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1163 if (sock < 0) {
1164 perror("socket");
Darren Tucker1dee8682004-11-05 20:26:49 +11001165 *socket_name = '\0'; /* Don't unlink any existing file */
Damien Miller95def091999-11-25 00:26:21 +11001166 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +10001167 }
Damien Miller95def091999-11-25 00:26:21 +11001168 memset(&sunaddr, 0, sizeof(sunaddr));
1169 sunaddr.sun_family = AF_UNIX;
1170 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
Ben Lindstrom3524d692001-05-03 22:59:24 +00001171 prev_mask = umask(0177);
Damien Miller90967402006-03-26 14:07:26 +11001172 if (bind(sock, (struct sockaddr *) &sunaddr, sizeof(sunaddr)) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001173 perror("bind");
Darren Tucker1dee8682004-11-05 20:26:49 +11001174 *socket_name = '\0'; /* Don't unlink any existing file */
Ben Lindstrom3524d692001-05-03 22:59:24 +00001175 umask(prev_mask);
Damien Miller95def091999-11-25 00:26:21 +11001176 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177 }
Ben Lindstrom3524d692001-05-03 22:59:24 +00001178 umask(prev_mask);
Darren Tucker3175eb92003-12-09 19:15:11 +11001179 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller95def091999-11-25 00:26:21 +11001180 perror("listen");
1181 cleanup_exit(1);
1182 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001183
Damien Miller5428f641999-11-25 11:54:57 +11001184 /*
1185 * Fork, and have the parent execute the command, if any, or present
1186 * the socket data. The child continues as the authentication agent.
1187 */
Ben Lindstromd94580c2001-07-04 03:48:02 +00001188 if (d_flag) {
1189 log_init(__progname, SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 1);
1190 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1191 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
1192 SSH_AUTHSOCKET_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001193 printf("echo Agent pid %ld;\n", (long)parent_pid);
Ben Lindstromd94580c2001-07-04 03:48:02 +00001194 goto skip;
1195 }
Damien Miller95def091999-11-25 00:26:21 +11001196 pid = fork();
1197 if (pid == -1) {
1198 perror("fork");
Damien Millerc653b892002-02-08 22:05:41 +11001199 cleanup_exit(1);
Damien Miller95def091999-11-25 00:26:21 +11001200 }
1201 if (pid != 0) { /* Parent - execute the given command. */
1202 close(sock);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001203 snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001204 if (ac == 0) {
1205 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
1206 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001207 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +11001208 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001209 SSH_AGENTPID_ENV_NAME);
Ben Lindstromce0f6342002-06-11 16:42:49 +00001210 printf("echo Agent pid %ld;\n", (long)pid);
Damien Miller95def091999-11-25 00:26:21 +11001211 exit(0);
1212 }
Damien Millere4340be2000-09-16 13:29:08 +11001213 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
1214 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
1215 perror("setenv");
1216 exit(1);
1217 }
Damien Miller95def091999-11-25 00:26:21 +11001218 execvp(av[0], av);
1219 perror(av[0]);
1220 exit(1);
1221 }
Damien Millerc653b892002-02-08 22:05:41 +11001222 /* child */
1223 log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001224
1225 if (setsid() == -1) {
Damien Millerc653b892002-02-08 22:05:41 +11001226 error("setsid: %s", strerror(errno));
Ben Lindstrom3fdf8762001-07-22 20:40:24 +00001227 cleanup_exit(1);
1228 }
1229
1230 (void)chdir("/");
Damien Miller6c711792003-01-24 11:36:23 +11001231 if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
1232 /* XXX might close listen socket */
1233 (void)dup2(fd, STDIN_FILENO);
1234 (void)dup2(fd, STDOUT_FILENO);
1235 (void)dup2(fd, STDERR_FILENO);
1236 if (fd > 2)
1237 close(fd);
1238 }
Damien Miller95def091999-11-25 00:26:21 +11001239
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001240#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +00001241 /* deny core dumps, since memory contains unencrypted private keys */
1242 rlim.rlim_cur = rlim.rlim_max = 0;
1243 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
Damien Millerc653b892002-02-08 22:05:41 +11001244 error("setrlimit RLIMIT_CORE: %s", strerror(errno));
Ben Lindstromc72745a2000-12-02 19:03:54 +00001245 cleanup_exit(1);
1246 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +00001247#endif
Ben Lindstromd94580c2001-07-04 03:48:02 +00001248
1249skip:
Damien Miller95def091999-11-25 00:26:21 +11001250 new_socket(AUTH_SOCKET, sock);
Darren Tucker2812dc92007-03-21 20:45:06 +11001251 if (ac > 0)
1252 parent_alive_interval = 10;
Damien Millerad833b32000-08-23 10:46:23 +10001253 idtab_init();
Damien Miller48bfa9c2001-07-14 12:12:55 +10001254 if (!d_flag)
Ben Lindstromd94580c2001-07-04 03:48:02 +00001255 signal(SIGINT, SIG_IGN);
Damien Miller48bfa9c2001-07-14 12:12:55 +10001256 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +00001257 signal(SIGHUP, cleanup_handler);
1258 signal(SIGTERM, cleanup_handler);
Ben Lindstroma3d5a4c2001-07-18 15:58:08 +00001259 nalloc = 0;
1260
Damien Miller95def091999-11-25 00:26:21 +11001261 while (1) {
Darren Tucker2812dc92007-03-21 20:45:06 +11001262 prepare_select(&readsetp, &writesetp, &max_fd, &nalloc, &tvp);
1263 result = select(max_fd + 1, readsetp, writesetp, NULL, tvp);
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001264 saved_errno = errno;
Darren Tucker2812dc92007-03-21 20:45:06 +11001265 if (parent_alive_interval != 0)
1266 check_parent_exists();
1267 (void) reaper(); /* remove expired keys */
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001268 if (result < 0) {
1269 if (saved_errno == EINTR)
Damien Miller95def091999-11-25 00:26:21 +11001270 continue;
Darren Tuckercf0d2db2007-02-28 21:19:58 +11001271 fatal("select: %s", strerror(saved_errno));
1272 } else if (result > 0)
1273 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +11001274 }
1275 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001276}