2019-06-28 15:35:04 +02:00
|
|
|
/* $OpenBSD: ssh-agent.c,v 1.237 2019/06/28 13:35:04 deraadt Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
1999-11-24 14:26:21 +01:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* The authentication agent program.
|
2000-08-23 02:46:23 +02:00
|
|
|
*
|
2000-09-16 04:29:08 +02:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*
|
2001-07-04 05:32:30 +02:00
|
|
|
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
|
2000-09-16 04:29:08 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 01:40:10 +01:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-08-05 03:02:17 +02:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resource.h>
|
2006-03-15 04:03:06 +01:00
|
|
|
#include <sys/stat.h>
|
2006-07-10 13:08:03 +02:00
|
|
|
#include <sys/socket.h>
|
2006-08-05 02:57:45 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2006-03-15 01:40:10 +01:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
2002-09-12 02:43:29 +02:00
|
|
|
#include "openbsd-compat/sys-queue.h"
|
2006-07-10 13:08:03 +02:00
|
|
|
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_OPENSSL
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <openssl/evp.h>
|
2008-02-28 09:13:52 +01:00
|
|
|
#include "openbsd-compat/openssl-compat.h"
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2006-07-24 06:13:33 +02:00
|
|
|
|
2006-07-12 14:22:46 +02:00
|
|
|
#include <errno.h>
|
2006-07-10 13:13:46 +02:00
|
|
|
#include <fcntl.h>
|
2015-01-16 07:40:12 +01:00
|
|
|
#include <limits.h>
|
2006-03-15 01:16:59 +01:00
|
|
|
#ifdef HAVE_PATHS_H
|
2006-03-15 01:18:26 +01:00
|
|
|
# include <paths.h>
|
2006-03-15 01:16:59 +01:00
|
|
|
#endif
|
2017-07-19 03:15:02 +02:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
# include <poll.h>
|
|
|
|
#endif
|
2006-03-15 01:52:09 +01:00
|
|
|
#include <signal.h>
|
2006-09-01 07:38:36 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-08-05 03:34:19 +02:00
|
|
|
#include <stdlib.h>
|
2006-07-24 06:09:40 +02:00
|
|
|
#include <time.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-07-24 06:01:23 +02:00
|
|
|
#include <unistd.h>
|
2015-05-21 09:55:15 +02:00
|
|
|
#ifdef HAVE_UTIL_H
|
|
|
|
# include <util.h>
|
|
|
|
#endif
|
2006-03-15 01:52:09 +01:00
|
|
|
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "xmalloc.h"
|
1999-10-27 05:42:43 +02:00
|
|
|
#include "ssh.h"
|
2015-01-14 14:09:09 +01:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
2000-07-21 02:19:44 +02:00
|
|
|
#include "authfd.h"
|
2000-09-23 08:15:56 +02:00
|
|
|
#include "compat.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
2003-01-24 01:36:23 +01:00
|
|
|
#include "misc.h"
|
2014-02-04 01:03:36 +01:00
|
|
|
#include "digest.h"
|
2015-01-14 14:09:09 +01:00
|
|
|
#include "ssherr.h"
|
2016-11-30 04:07:37 +01:00
|
|
|
#include "match.h"
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2010-02-11 23:21:02 +01:00
|
|
|
#ifdef ENABLE_PKCS11
|
|
|
|
#include "ssh-pkcs11.h"
|
2001-08-06 23:59:25 +02:00
|
|
|
#endif
|
2001-07-04 05:53:15 +02:00
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
#ifndef DEFAULT_PKCS11_WHITELIST
|
2017-01-04 03:21:43 +01:00
|
|
|
# define DEFAULT_PKCS11_WHITELIST "/usr/lib*/*,/usr/local/lib*/*"
|
2016-11-30 04:07:37 +01:00
|
|
|
#endif
|
|
|
|
|
2017-07-19 03:15:02 +02:00
|
|
|
/* Maximum accepted message length */
|
|
|
|
#define AGENT_MAX_LEN (256*1024)
|
2019-01-22 23:58:50 +01:00
|
|
|
/* Maximum bytes to read from client socket */
|
|
|
|
#define AGENT_RBUF_LEN (4096)
|
2017-07-19 03:15:02 +02:00
|
|
|
|
2001-12-06 17:32:47 +01:00
|
|
|
typedef enum {
|
|
|
|
AUTH_UNUSED,
|
|
|
|
AUTH_SOCKET,
|
|
|
|
AUTH_CONNECTION
|
|
|
|
} sock_type;
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
typedef struct {
|
|
|
|
int fd;
|
2001-12-06 17:32:47 +01:00
|
|
|
sock_type type;
|
2015-01-14 14:09:09 +01:00
|
|
|
struct sshbuf *input;
|
|
|
|
struct sshbuf *output;
|
|
|
|
struct sshbuf *request;
|
1999-10-27 05:42:43 +02:00
|
|
|
} SocketEntry;
|
|
|
|
|
2000-12-22 02:43:59 +01:00
|
|
|
u_int sockets_alloc = 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
SocketEntry *sockets = NULL;
|
|
|
|
|
2002-01-22 13:26:13 +01:00
|
|
|
typedef struct identity {
|
|
|
|
TAILQ_ENTRY(identity) next;
|
2015-01-14 14:09:09 +01:00
|
|
|
struct sshkey *key;
|
1999-11-24 14:26:21 +01:00
|
|
|
char *comment;
|
2010-02-11 23:21:02 +01:00
|
|
|
char *provider;
|
2013-06-01 23:43:59 +02:00
|
|
|
time_t death;
|
2003-01-24 01:36:23 +01:00
|
|
|
u_int confirm;
|
1999-10-27 05:42:43 +02:00
|
|
|
} Identity;
|
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
struct idtable {
|
2000-08-23 02:46:23 +02:00
|
|
|
int nentries;
|
2002-01-22 13:26:13 +01:00
|
|
|
TAILQ_HEAD(idqueue, identity) idlist;
|
2017-05-01 01:29:10 +02:00
|
|
|
};
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
/* private key table */
|
|
|
|
struct idtable *idtab;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
int max_fd = 0;
|
|
|
|
|
|
|
|
/* pid of shell == parent of agent */
|
2000-04-19 23:42:21 +02:00
|
|
|
pid_t parent_pid = -1;
|
2013-06-02 15:47:11 +02:00
|
|
|
time_t parent_alive_interval = 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2014-07-03 13:22:40 +02:00
|
|
|
/* pid of process for which cleanup_socket is applicable */
|
|
|
|
pid_t cleanup_pid = 0;
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* pathname and directory for AUTH_SOCKET */
|
2015-01-16 07:40:12 +01:00
|
|
|
char socket_name[PATH_MAX];
|
|
|
|
char socket_dir[PATH_MAX];
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
/* PKCS#11 path whitelist */
|
|
|
|
static char *pkcs11_whitelist;
|
|
|
|
|
2002-06-06 23:52:03 +02:00
|
|
|
/* locking */
|
2015-05-15 07:44:21 +02:00
|
|
|
#define LOCK_SIZE 32
|
|
|
|
#define LOCK_SALT_SIZE 16
|
|
|
|
#define LOCK_ROUNDS 1
|
2002-06-06 23:52:03 +02:00
|
|
|
int locked = 0;
|
2016-05-02 10:49:03 +02:00
|
|
|
u_char lock_pwhash[LOCK_SIZE];
|
|
|
|
u_char lock_salt[LOCK_SALT_SIZE];
|
2002-06-06 23:52:03 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
extern char *__progname;
|
|
|
|
|
2013-06-01 23:43:59 +02:00
|
|
|
/* Default lifetime in seconds (0 == forever) */
|
|
|
|
static long lifetime = 0;
|
2003-01-22 01:47:19 +01:00
|
|
|
|
2014-12-21 23:27:55 +01:00
|
|
|
static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
|
|
|
|
|
2002-09-04 08:31:21 +02:00
|
|
|
static void
|
|
|
|
close_socket(SocketEntry *e)
|
|
|
|
{
|
|
|
|
close(e->fd);
|
|
|
|
e->fd = -1;
|
|
|
|
e->type = AUTH_UNUSED;
|
2015-01-14 14:09:09 +01:00
|
|
|
sshbuf_free(e->input);
|
|
|
|
sshbuf_free(e->output);
|
|
|
|
sshbuf_free(e->request);
|
2002-09-04 08:31:21 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-08-23 02:46:23 +02:00
|
|
|
idtab_init(void)
|
|
|
|
{
|
2017-05-01 01:29:10 +02:00
|
|
|
idtab = xcalloc(1, sizeof(*idtab));
|
|
|
|
TAILQ_INIT(&idtab->idlist);
|
|
|
|
idtab->nentries = 0;
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
|
|
|
|
2002-06-06 23:54:57 +02:00
|
|
|
static void
|
|
|
|
free_identity(Identity *id)
|
|
|
|
{
|
2015-01-14 14:09:09 +01:00
|
|
|
sshkey_free(id->key);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(id->provider);
|
|
|
|
free(id->comment);
|
|
|
|
free(id);
|
2002-06-06 23:54:57 +02:00
|
|
|
}
|
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
/* return matching private key for given public key */
|
2002-01-22 13:26:13 +01:00
|
|
|
static Identity *
|
2017-05-01 01:29:10 +02:00
|
|
|
lookup_identity(struct sshkey *key)
|
2000-08-23 02:46:23 +02:00
|
|
|
{
|
2002-01-22 13:26:13 +01:00
|
|
|
Identity *id;
|
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
TAILQ_FOREACH(id, &idtab->idlist, next) {
|
2015-01-14 14:09:09 +01:00
|
|
|
if (sshkey_equal(key, id->key))
|
2002-01-22 13:26:13 +01:00
|
|
|
return (id);
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2002-01-22 13:26:13 +01:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2003-01-24 01:36:23 +01:00
|
|
|
/* Check confirmation of keysign request */
|
|
|
|
static int
|
|
|
|
confirm_key(Identity *id)
|
|
|
|
{
|
2004-11-05 10:38:03 +01:00
|
|
|
char *p;
|
2003-01-24 01:36:23 +01:00
|
|
|
int ret = -1;
|
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
p = sshkey_fingerprint(id->key, fingerprint_hash, SSH_FP_DEFAULT);
|
2015-01-28 23:36:00 +01:00
|
|
|
if (p != NULL &&
|
|
|
|
ask_permission("Allow use of key %s?\nKey fingerprint %s.",
|
2004-11-05 10:38:03 +01:00
|
|
|
id->comment, p))
|
|
|
|
ret = 0;
|
2013-06-01 23:31:17 +02:00
|
|
|
free(p);
|
2004-11-05 10:38:03 +01:00
|
|
|
|
2003-01-24 01:36:23 +01:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
static void
|
|
|
|
send_status(SocketEntry *e, int success)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_u32(e->output, 1)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(e->output, success ?
|
|
|
|
SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
}
|
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
/* send list of supported public keys to 'client' */
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2017-05-01 01:29:10 +02:00
|
|
|
process_request_identities(SocketEntry *e)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2002-01-22 13:26:13 +01:00
|
|
|
Identity *id;
|
2015-01-14 14:09:09 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, idtab->nentries)) != 0)
|
2015-01-14 14:09:09 +01:00
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
2017-05-01 01:29:10 +02:00
|
|
|
TAILQ_FOREACH(id, &idtab->idlist, next) {
|
2018-02-23 16:58:37 +01:00
|
|
|
if ((r = sshkey_puts_opts(id->key, msg, SSHKEY_SERIALIZE_INFO))
|
|
|
|
!= 0 ||
|
2017-05-01 01:29:10 +02:00
|
|
|
(r = sshbuf_put_cstring(msg, id->comment)) != 0) {
|
|
|
|
error("%s: put key/comment: %s", __func__,
|
2017-05-01 01:18:44 +02:00
|
|
|
ssh_err(r));
|
|
|
|
continue;
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
sshbuf_free(msg);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2015-12-04 17:41:28 +01:00
|
|
|
static char *
|
|
|
|
agent_decode_alg(struct sshkey *key, u_int flags)
|
|
|
|
{
|
|
|
|
if (key->type == KEY_RSA) {
|
|
|
|
if (flags & SSH_AGENT_RSA_SHA2_256)
|
|
|
|
return "rsa-sha2-256";
|
|
|
|
else if (flags & SSH_AGENT_RSA_SHA2_512)
|
|
|
|
return "rsa-sha2-512";
|
2019-06-14 05:51:47 +02:00
|
|
|
} else if (key->type == KEY_RSA_CERT) {
|
|
|
|
if (flags & SSH_AGENT_RSA_SHA2_256)
|
|
|
|
return "rsa-sha2-256-cert-v01@openssh.com";
|
|
|
|
else if (flags & SSH_AGENT_RSA_SHA2_512)
|
|
|
|
return "rsa-sha2-512-cert-v01@openssh.com";
|
2015-12-04 17:41:28 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
/* ssh2 only */
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2000-08-23 02:46:23 +02:00
|
|
|
process_sign_request2(SocketEntry *e)
|
|
|
|
{
|
2017-05-01 01:29:10 +02:00
|
|
|
const u_char *data;
|
|
|
|
u_char *signature = NULL;
|
|
|
|
size_t dlen, slen = 0;
|
2015-01-14 14:09:09 +01:00
|
|
|
u_int compat = 0, flags;
|
|
|
|
int r, ok = -1;
|
|
|
|
struct sshbuf *msg;
|
2017-05-01 01:29:10 +02:00
|
|
|
struct sshkey *key = NULL;
|
2015-01-14 20:33:41 +01:00
|
|
|
struct identity *id;
|
2015-01-14 14:09:09 +01:00
|
|
|
|
2015-01-14 20:33:41 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((r = sshkey_froms(e->request, &key)) != 0 ||
|
|
|
|
(r = sshbuf_get_string_direct(e->request, &data, &dlen)) != 0 ||
|
2017-11-15 01:13:40 +01:00
|
|
|
(r = sshbuf_get_u32(e->request, &flags)) != 0) {
|
|
|
|
error("%s: couldn't parse request: %s", __func__, ssh_err(r));
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((id = lookup_identity(key)) == NULL) {
|
2015-01-14 20:33:41 +01:00
|
|
|
verbose("%s: %s key not found", __func__, sshkey_type(key));
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
if (id->confirm && confirm_key(id) != 0) {
|
|
|
|
verbose("%s: user refused key", __func__);
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_sign(id->key, &signature, &slen,
|
2015-12-04 17:41:28 +01:00
|
|
|
data, dlen, agent_decode_alg(key, flags), compat)) != 0) {
|
2015-12-11 03:20:28 +01:00
|
|
|
error("%s: sshkey_sign: %s", __func__, ssh_err(r));
|
2015-01-14 20:33:41 +01:00
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
/* Success */
|
|
|
|
ok = 0;
|
|
|
|
send:
|
|
|
|
sshkey_free(key);
|
2000-08-23 02:46:23 +02:00
|
|
|
if (ok == 0) {
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(msg, signature, slen)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
} else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
|
|
|
|
if ((r = sshbuf_put_stringb(e->output, msg)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
|
|
|
|
sshbuf_free(msg);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(signature);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
/* shared */
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2017-05-01 01:29:10 +02:00
|
|
|
process_remove_identity(SocketEntry *e)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 14:09:09 +01:00
|
|
|
int r, success = 0;
|
|
|
|
struct sshkey *key = NULL;
|
2017-05-01 01:29:10 +02:00
|
|
|
Identity *id;
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((r = sshkey_froms(e->request, &key)) != 0) {
|
|
|
|
error("%s: get key: %s", __func__, ssh_err(r));
|
|
|
|
goto done;
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((id = lookup_identity(key)) == NULL) {
|
|
|
|
debug("%s: key not found", __func__);
|
|
|
|
goto done;
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2017-05-01 01:29:10 +02:00
|
|
|
/* We have this key, free it. */
|
|
|
|
if (idtab->nentries < 1)
|
|
|
|
fatal("%s: internal error: nentries %d",
|
|
|
|
__func__, idtab->nentries);
|
|
|
|
TAILQ_REMOVE(&idtab->idlist, id, next);
|
|
|
|
free_identity(id);
|
|
|
|
idtab->nentries--;
|
|
|
|
sshkey_free(key);
|
|
|
|
success = 1;
|
|
|
|
done:
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, success);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2017-05-01 01:29:10 +02:00
|
|
|
process_remove_all_identities(SocketEntry *e)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2002-01-22 13:26:13 +01:00
|
|
|
Identity *id;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Loop over all identities and clear the keys. */
|
2017-05-01 01:29:10 +02:00
|
|
|
for (id = TAILQ_FIRST(&idtab->idlist); id;
|
|
|
|
id = TAILQ_FIRST(&idtab->idlist)) {
|
|
|
|
TAILQ_REMOVE(&idtab->idlist, id, next);
|
2002-01-22 13:26:13 +01:00
|
|
|
free_identity(id);
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
/* Mark that there are no identities. */
|
2017-05-01 01:29:10 +02:00
|
|
|
idtab->nentries = 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
|
|
|
|
/* Send success. */
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, 1);
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
|
|
|
|
2007-03-21 10:45:06 +01:00
|
|
|
/* removes expired keys and returns number of seconds until the next expiry */
|
2013-06-01 23:43:59 +02:00
|
|
|
static time_t
|
2002-06-06 23:54:57 +02:00
|
|
|
reaper(void)
|
|
|
|
{
|
2013-06-01 23:46:16 +02:00
|
|
|
time_t deadline = 0, now = monotime();
|
2002-06-06 23:54:57 +02:00
|
|
|
Identity *id, *nxt;
|
2017-05-01 01:29:10 +02:00
|
|
|
|
|
|
|
for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
|
|
|
|
nxt = TAILQ_NEXT(id, next);
|
|
|
|
if (id->death == 0)
|
|
|
|
continue;
|
|
|
|
if (now >= id->death) {
|
|
|
|
debug("expiring key '%s'", id->comment);
|
|
|
|
TAILQ_REMOVE(&idtab->idlist, id, next);
|
|
|
|
free_identity(id);
|
|
|
|
idtab->nentries--;
|
|
|
|
} else
|
|
|
|
deadline = (deadline == 0) ? id->death :
|
|
|
|
MINIMUM(deadline, id->death);
|
2002-06-06 23:54:57 +02:00
|
|
|
}
|
2007-03-21 10:45:06 +01:00
|
|
|
if (deadline == 0 || deadline <= now)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return (deadline - now);
|
2002-06-06 23:54:57 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2017-05-01 01:29:10 +02:00
|
|
|
process_add_identity(SocketEntry *e)
|
2000-11-13 12:57:25 +01:00
|
|
|
{
|
2007-10-26 06:25:31 +02:00
|
|
|
Identity *id;
|
2015-01-14 14:09:09 +01:00
|
|
|
int success = 0, confirm = 0;
|
2018-02-23 16:58:37 +01:00
|
|
|
u_int seconds, maxsign;
|
2015-01-14 14:09:09 +01:00
|
|
|
char *comment = NULL;
|
2013-06-01 23:43:59 +02:00
|
|
|
time_t death = 0;
|
2015-01-14 14:09:09 +01:00
|
|
|
struct sshkey *k = NULL;
|
|
|
|
u_char ctype;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((r = sshkey_private_deserialize(e->request, &k)) != 0 ||
|
|
|
|
k == NULL ||
|
2015-01-14 14:09:09 +01:00
|
|
|
(r = sshbuf_get_cstring(e->request, &comment, NULL)) != 0) {
|
|
|
|
error("%s: decode private key: %s", __func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
2019-06-21 06:21:04 +02:00
|
|
|
if ((r = sshkey_shield_private(k)) != 0) {
|
|
|
|
error("%s: shield private key: %s", __func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
while (sshbuf_len(e->request)) {
|
|
|
|
if ((r = sshbuf_get_u8(e->request, &ctype)) != 0) {
|
|
|
|
error("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
switch (ctype) {
|
2002-06-21 02:08:39 +02:00
|
|
|
case SSH_AGENT_CONSTRAIN_LIFETIME:
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
|
|
|
|
error("%s: bad lifetime constraint: %s",
|
|
|
|
__func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
death = monotime() + seconds;
|
2002-06-21 02:08:39 +02:00
|
|
|
break;
|
2003-01-24 01:36:23 +01:00
|
|
|
case SSH_AGENT_CONSTRAIN_CONFIRM:
|
|
|
|
confirm = 1;
|
|
|
|
break;
|
2018-02-23 16:58:37 +01:00
|
|
|
case SSH_AGENT_CONSTRAIN_MAXSIGN:
|
|
|
|
if ((r = sshbuf_get_u32(e->request, &maxsign)) != 0) {
|
|
|
|
error("%s: bad maxsign constraint: %s",
|
|
|
|
__func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_enable_maxsign(k, maxsign)) != 0) {
|
|
|
|
error("%s: cannot enable maxsign: %s",
|
|
|
|
__func__, ssh_err(r));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2002-06-21 02:08:39 +02:00
|
|
|
default:
|
2015-01-14 14:09:09 +01:00
|
|
|
error("%s: Unknown constraint %d", __func__, ctype);
|
|
|
|
err:
|
|
|
|
sshbuf_reset(e->request);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(comment);
|
2015-01-14 14:09:09 +01:00
|
|
|
sshkey_free(k);
|
2008-06-29 16:05:21 +02:00
|
|
|
goto send;
|
2002-06-21 02:08:39 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
|
2008-06-29 16:05:21 +02:00
|
|
|
success = 1;
|
2003-01-22 01:47:19 +01:00
|
|
|
if (lifetime && !death)
|
2013-06-01 23:46:16 +02:00
|
|
|
death = monotime() + lifetime;
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((id = lookup_identity(k)) == NULL) {
|
2010-02-11 23:21:02 +01:00
|
|
|
id = xcalloc(1, sizeof(Identity));
|
2017-05-01 01:29:10 +02:00
|
|
|
TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
|
2000-08-23 02:46:23 +02:00
|
|
|
/* Increment the number of identities. */
|
2017-05-01 01:29:10 +02:00
|
|
|
idtab->nentries++;
|
2000-08-23 02:46:23 +02:00
|
|
|
} else {
|
2018-02-23 16:58:37 +01:00
|
|
|
/* key state might have been updated */
|
|
|
|
sshkey_free(id->key);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(id->comment);
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2018-02-23 16:58:37 +01:00
|
|
|
id->key = k;
|
2007-10-26 06:25:31 +02:00
|
|
|
id->comment = comment;
|
|
|
|
id->death = death;
|
|
|
|
id->confirm = confirm;
|
2000-08-23 02:46:23 +02:00
|
|
|
send:
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, success);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2002-06-06 23:52:03 +02:00
|
|
|
/* XXX todo: encrypt sensitive data with passphrase */
|
|
|
|
static void
|
|
|
|
process_lock_agent(SocketEntry *e, int lock)
|
|
|
|
{
|
2015-05-15 07:44:21 +02:00
|
|
|
int r, success = 0, delay;
|
2016-05-02 10:49:03 +02:00
|
|
|
char *passwd;
|
|
|
|
u_char passwdhash[LOCK_SIZE];
|
2015-05-15 07:44:21 +02:00
|
|
|
static u_int fail_count = 0;
|
|
|
|
size_t pwlen;
|
2002-06-06 23:52:03 +02:00
|
|
|
|
2017-11-15 03:10:16 +01:00
|
|
|
/*
|
|
|
|
* This is deliberately fatal: the user has requested that we lock,
|
|
|
|
* but we can't parse their request properly. The only safe thing to
|
|
|
|
* do is abort.
|
|
|
|
*/
|
2015-05-15 07:44:21 +02:00
|
|
|
if ((r = sshbuf_get_cstring(e->request, &passwd, &pwlen)) != 0)
|
2015-01-14 14:09:09 +01:00
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
2015-05-15 07:44:21 +02:00
|
|
|
if (pwlen == 0) {
|
|
|
|
debug("empty password not supported");
|
|
|
|
} else if (locked && !lock) {
|
|
|
|
if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
|
|
|
|
passwdhash, sizeof(passwdhash), LOCK_ROUNDS) < 0)
|
|
|
|
fatal("bcrypt_pbkdf");
|
2016-05-02 10:49:03 +02:00
|
|
|
if (timingsafe_bcmp(passwdhash, lock_pwhash, LOCK_SIZE) == 0) {
|
2015-05-15 07:44:21 +02:00
|
|
|
debug("agent unlocked");
|
|
|
|
locked = 0;
|
|
|
|
fail_count = 0;
|
2016-05-02 10:49:03 +02:00
|
|
|
explicit_bzero(lock_pwhash, sizeof(lock_pwhash));
|
2015-05-15 07:44:21 +02:00
|
|
|
success = 1;
|
|
|
|
} else {
|
|
|
|
/* delay in 0.1s increments up to 10s */
|
|
|
|
if (fail_count < 100)
|
|
|
|
fail_count++;
|
|
|
|
delay = 100000 * fail_count;
|
|
|
|
debug("unlock failed, delaying %0.1lf seconds",
|
|
|
|
(double)delay/1000000);
|
|
|
|
usleep(delay);
|
|
|
|
}
|
|
|
|
explicit_bzero(passwdhash, sizeof(passwdhash));
|
2002-06-06 23:52:03 +02:00
|
|
|
} else if (!locked && lock) {
|
2015-05-15 07:44:21 +02:00
|
|
|
debug("agent locked");
|
2002-06-06 23:52:03 +02:00
|
|
|
locked = 1;
|
2015-05-15 07:44:21 +02:00
|
|
|
arc4random_buf(lock_salt, sizeof(lock_salt));
|
|
|
|
if (bcrypt_pbkdf(passwd, pwlen, lock_salt, sizeof(lock_salt),
|
2016-05-02 10:49:03 +02:00
|
|
|
lock_pwhash, sizeof(lock_pwhash), LOCK_ROUNDS) < 0)
|
2015-05-15 07:44:21 +02:00
|
|
|
fatal("bcrypt_pbkdf");
|
2002-06-06 23:52:03 +02:00
|
|
|
success = 1;
|
|
|
|
}
|
2015-05-15 07:44:21 +02:00
|
|
|
explicit_bzero(passwd, pwlen);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(passwd);
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, success);
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-05-01 01:29:10 +02:00
|
|
|
no_identities(SocketEntry *e)
|
2002-06-06 23:52:03 +02:00
|
|
|
{
|
2015-01-14 14:09:09 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
2002-06-06 23:52:03 +02:00
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2017-05-01 01:29:10 +02:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
|
2015-01-14 14:09:09 +01:00
|
|
|
(r = sshbuf_put_u32(msg, 0)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(e->output, msg)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
sshbuf_free(msg);
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
2001-07-04 05:53:15 +02:00
|
|
|
|
2010-02-11 23:21:02 +01:00
|
|
|
#ifdef ENABLE_PKCS11
|
2001-07-04 05:53:15 +02:00
|
|
|
static void
|
2008-06-29 16:05:21 +02:00
|
|
|
process_add_smartcard_key(SocketEntry *e)
|
2001-07-04 05:53:15 +02:00
|
|
|
{
|
2017-11-15 03:10:16 +01:00
|
|
|
char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
|
2017-05-01 01:29:10 +02:00
|
|
|
int r, i, count = 0, success = 0, confirm = 0;
|
2015-01-14 14:09:09 +01:00
|
|
|
u_int seconds;
|
2013-06-01 23:43:59 +02:00
|
|
|
time_t death = 0;
|
2015-01-14 14:09:09 +01:00
|
|
|
u_char type;
|
|
|
|
struct sshkey **keys = NULL, *k;
|
2002-06-23 23:38:49 +02:00
|
|
|
Identity *id;
|
2001-12-21 04:45:46 +01:00
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
|
2017-11-15 03:10:16 +01:00
|
|
|
(r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
|
|
|
|
error("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
goto send;
|
|
|
|
}
|
2003-06-11 14:06:33 +02:00
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
while (sshbuf_len(e->request)) {
|
2017-11-15 03:10:16 +01:00
|
|
|
if ((r = sshbuf_get_u8(e->request, &type)) != 0) {
|
|
|
|
error("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
goto send;
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
switch (type) {
|
2003-06-11 14:06:33 +02:00
|
|
|
case SSH_AGENT_CONSTRAIN_LIFETIME:
|
2017-11-15 03:10:16 +01:00
|
|
|
if ((r = sshbuf_get_u32(e->request, &seconds)) != 0) {
|
|
|
|
error("%s: buffer error: %s",
|
2015-01-14 14:09:09 +01:00
|
|
|
__func__, ssh_err(r));
|
2017-11-15 03:10:16 +01:00
|
|
|
goto send;
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
death = monotime() + seconds;
|
2003-06-11 14:06:33 +02:00
|
|
|
break;
|
|
|
|
case SSH_AGENT_CONSTRAIN_CONFIRM:
|
|
|
|
confirm = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2017-05-01 01:29:10 +02:00
|
|
|
error("%s: Unknown constraint type %d", __func__, type);
|
2008-06-29 16:05:21 +02:00
|
|
|
goto send;
|
2003-06-11 14:06:33 +02:00
|
|
|
}
|
|
|
|
}
|
2016-11-30 04:07:37 +01:00
|
|
|
if (realpath(provider, canonical_provider) == NULL) {
|
|
|
|
verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
|
|
|
|
provider, strerror(errno));
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
if (match_pattern_list(canonical_provider, pkcs11_whitelist, 0) != 1) {
|
|
|
|
verbose("refusing PKCS#11 add of \"%.100s\": "
|
|
|
|
"provider not whitelisted", canonical_provider);
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
debug("%s: add %.100s", __func__, canonical_provider);
|
2003-06-11 14:06:33 +02:00
|
|
|
if (lifetime && !death)
|
2013-06-01 23:46:16 +02:00
|
|
|
death = monotime() + lifetime;
|
2003-06-11 14:06:33 +02:00
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
count = pkcs11_add_provider(canonical_provider, pin, &keys);
|
2010-02-11 23:21:02 +01:00
|
|
|
for (i = 0; i < count; i++) {
|
2002-03-26 04:17:42 +01:00
|
|
|
k = keys[i];
|
2017-05-01 01:29:10 +02:00
|
|
|
if (lookup_identity(k) == NULL) {
|
2010-02-11 23:21:02 +01:00
|
|
|
id = xcalloc(1, sizeof(Identity));
|
2002-03-26 04:17:42 +01:00
|
|
|
id->key = k;
|
2016-11-30 04:07:37 +01:00
|
|
|
id->provider = xstrdup(canonical_provider);
|
|
|
|
id->comment = xstrdup(canonical_provider); /* XXX */
|
2003-06-11 14:06:33 +02:00
|
|
|
id->death = death;
|
|
|
|
id->confirm = confirm;
|
2017-05-01 01:29:10 +02:00
|
|
|
TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
|
|
|
|
idtab->nentries++;
|
2002-03-26 04:17:42 +01:00
|
|
|
success = 1;
|
|
|
|
} else {
|
2015-01-14 14:09:09 +01:00
|
|
|
sshkey_free(k);
|
2002-03-26 04:17:42 +01:00
|
|
|
}
|
|
|
|
keys[i] = NULL;
|
2001-07-04 05:53:15 +02:00
|
|
|
}
|
|
|
|
send:
|
2013-06-01 23:31:17 +02:00
|
|
|
free(pin);
|
|
|
|
free(provider);
|
|
|
|
free(keys);
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, success);
|
2001-07-04 05:53:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_remove_smartcard_key(SocketEntry *e)
|
|
|
|
{
|
2017-03-15 03:25:09 +01:00
|
|
|
char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
|
2017-05-01 01:29:10 +02:00
|
|
|
int r, success = 0;
|
2010-02-11 23:21:02 +01:00
|
|
|
Identity *id, *nxt;
|
2001-07-04 05:53:15 +02:00
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((r = sshbuf_get_cstring(e->request, &provider, NULL)) != 0 ||
|
2017-11-15 03:10:16 +01:00
|
|
|
(r = sshbuf_get_cstring(e->request, &pin, NULL)) != 0) {
|
|
|
|
error("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
goto send;
|
|
|
|
}
|
2013-06-01 23:31:17 +02:00
|
|
|
free(pin);
|
2001-07-04 05:53:15 +02:00
|
|
|
|
2017-03-15 03:25:09 +01:00
|
|
|
if (realpath(provider, canonical_provider) == NULL) {
|
|
|
|
verbose("failed PKCS#11 add of \"%.100s\": realpath: %s",
|
|
|
|
provider, strerror(errno));
|
|
|
|
goto send;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: remove %.100s", __func__, canonical_provider);
|
2017-05-01 01:29:10 +02:00
|
|
|
for (id = TAILQ_FIRST(&idtab->idlist); id; id = nxt) {
|
|
|
|
nxt = TAILQ_NEXT(id, next);
|
|
|
|
/* Skip file--based keys */
|
|
|
|
if (id->provider == NULL)
|
|
|
|
continue;
|
|
|
|
if (!strcmp(canonical_provider, id->provider)) {
|
|
|
|
TAILQ_REMOVE(&idtab->idlist, id, next);
|
|
|
|
free_identity(id);
|
|
|
|
idtab->nentries--;
|
2001-07-04 05:53:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-15 03:25:09 +01:00
|
|
|
if (pkcs11_del_provider(canonical_provider) == 0)
|
2010-02-11 23:21:02 +01:00
|
|
|
success = 1;
|
|
|
|
else
|
2017-05-01 01:29:10 +02:00
|
|
|
error("%s: pkcs11_del_provider failed", __func__);
|
2017-03-15 04:52:30 +01:00
|
|
|
send:
|
2013-06-01 23:31:17 +02:00
|
|
|
free(provider);
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, success);
|
2001-07-04 05:53:15 +02:00
|
|
|
}
|
2010-02-11 23:21:02 +01:00
|
|
|
#endif /* ENABLE_PKCS11 */
|
2001-07-04 05:53:15 +02:00
|
|
|
|
2000-08-23 02:46:23 +02:00
|
|
|
/* dispatch incoming messages */
|
|
|
|
|
2017-07-19 03:15:02 +02:00
|
|
|
static int
|
|
|
|
process_message(u_int socknum)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 14:09:09 +01:00
|
|
|
u_int msg_len;
|
|
|
|
u_char type;
|
|
|
|
const u_char *cp;
|
|
|
|
int r;
|
2017-07-19 03:15:02 +02:00
|
|
|
SocketEntry *e;
|
|
|
|
|
|
|
|
if (socknum >= sockets_alloc) {
|
|
|
|
fatal("%s: socket number %u >= allocated %u",
|
|
|
|
__func__, socknum, sockets_alloc);
|
|
|
|
}
|
|
|
|
e = &sockets[socknum];
|
2002-06-06 23:54:57 +02:00
|
|
|
|
2015-01-14 14:09:09 +01:00
|
|
|
if (sshbuf_len(e->input) < 5)
|
2017-07-19 03:15:02 +02:00
|
|
|
return 0; /* Incomplete message header. */
|
2015-01-14 14:09:09 +01:00
|
|
|
cp = sshbuf_ptr(e->input);
|
|
|
|
msg_len = PEEK_U32(cp);
|
2017-07-19 03:15:02 +02:00
|
|
|
if (msg_len > AGENT_MAX_LEN) {
|
|
|
|
debug("%s: socket %u (fd=%d) message too long %u > %u",
|
|
|
|
__func__, socknum, e->fd, msg_len, AGENT_MAX_LEN);
|
|
|
|
return -1;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
if (sshbuf_len(e->input) < msg_len + 4)
|
2017-07-19 03:15:02 +02:00
|
|
|
return 0; /* Incomplete message body. */
|
2002-06-06 23:48:57 +02:00
|
|
|
|
|
|
|
/* move the current input to e->request */
|
2015-01-14 14:09:09 +01:00
|
|
|
sshbuf_reset(e->request);
|
|
|
|
if ((r = sshbuf_get_stringb(e->input, e->request)) != 0 ||
|
2017-07-19 03:15:02 +02:00
|
|
|
(r = sshbuf_get_u8(e->request, &type)) != 0) {
|
|
|
|
if (r == SSH_ERR_MESSAGE_INCOMPLETE ||
|
|
|
|
r == SSH_ERR_STRING_TOO_LARGE) {
|
|
|
|
debug("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
2017-07-19 03:15:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug("%s: socket %u (fd=%d) type %d", __func__, socknum, e->fd, type);
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2018-04-10 02:10:49 +02:00
|
|
|
/* check whether agent is locked */
|
2002-06-06 23:52:03 +02:00
|
|
|
if (locked && type != SSH_AGENTC_UNLOCK) {
|
2015-01-14 14:09:09 +01:00
|
|
|
sshbuf_reset(e->request);
|
2002-06-06 23:52:03 +02:00
|
|
|
switch (type) {
|
|
|
|
case SSH2_AGENTC_REQUEST_IDENTITIES:
|
|
|
|
/* send empty lists */
|
2017-05-01 01:29:10 +02:00
|
|
|
no_identities(e);
|
2002-06-06 23:52:03 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* send a fail message for all other request types */
|
2015-01-14 14:09:09 +01:00
|
|
|
send_status(e, 0);
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
2017-07-19 03:15:02 +02:00
|
|
|
return 0;
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
switch (type) {
|
2002-06-06 23:52:03 +02:00
|
|
|
case SSH_AGENTC_LOCK:
|
|
|
|
case SSH_AGENTC_UNLOCK:
|
|
|
|
process_lock_agent(e, type == SSH_AGENTC_LOCK);
|
|
|
|
break;
|
1999-11-24 14:26:21 +01:00
|
|
|
case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
|
2017-05-01 01:29:10 +02:00
|
|
|
process_remove_all_identities(e); /* safe for !WITH_SSH1 */
|
2000-08-23 02:46:23 +02:00
|
|
|
break;
|
|
|
|
/* ssh2 */
|
|
|
|
case SSH2_AGENTC_SIGN_REQUEST:
|
|
|
|
process_sign_request2(e);
|
|
|
|
break;
|
|
|
|
case SSH2_AGENTC_REQUEST_IDENTITIES:
|
2017-05-01 01:29:10 +02:00
|
|
|
process_request_identities(e);
|
2000-08-23 02:46:23 +02:00
|
|
|
break;
|
|
|
|
case SSH2_AGENTC_ADD_IDENTITY:
|
2002-06-21 02:08:39 +02:00
|
|
|
case SSH2_AGENTC_ADD_ID_CONSTRAINED:
|
2017-05-01 01:29:10 +02:00
|
|
|
process_add_identity(e);
|
2000-08-23 02:46:23 +02:00
|
|
|
break;
|
|
|
|
case SSH2_AGENTC_REMOVE_IDENTITY:
|
2017-05-01 01:29:10 +02:00
|
|
|
process_remove_identity(e);
|
2000-08-23 02:46:23 +02:00
|
|
|
break;
|
|
|
|
case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
|
2017-05-01 01:29:10 +02:00
|
|
|
process_remove_all_identities(e);
|
1999-11-24 14:26:21 +01:00
|
|
|
break;
|
2010-02-11 23:21:02 +01:00
|
|
|
#ifdef ENABLE_PKCS11
|
2001-07-04 05:53:15 +02:00
|
|
|
case SSH_AGENTC_ADD_SMARTCARD_KEY:
|
2003-06-11 14:06:33 +02:00
|
|
|
case SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED:
|
2001-07-04 05:53:15 +02:00
|
|
|
process_add_smartcard_key(e);
|
2001-12-21 04:45:46 +01:00
|
|
|
break;
|
2001-07-04 05:53:15 +02:00
|
|
|
case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
|
|
|
|
process_remove_smartcard_key(e);
|
2001-12-21 04:45:46 +01:00
|
|
|
break;
|
2010-02-11 23:21:02 +01:00
|
|
|
#endif /* ENABLE_PKCS11 */
|
1999-11-24 14:26:21 +01:00
|
|
|
default:
|
|
|
|
/* Unknown message. Respond with failure. */
|
|
|
|
error("Unknown message %d", type);
|
2015-01-14 14:09:09 +01:00
|
|
|
sshbuf_reset(e->request);
|
|
|
|
send_status(e, 0);
|
1999-11-24 14:26:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-07-19 03:15:02 +02:00
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2001-12-06 17:32:47 +01:00
|
|
|
new_socket(sock_type type, int fd)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2003-09-22 13:04:23 +02:00
|
|
|
u_int i, old_alloc, new_alloc;
|
2002-06-23 23:38:49 +02:00
|
|
|
|
2004-06-15 02:35:30 +02:00
|
|
|
set_nonblock(fd);
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
if (fd > max_fd)
|
|
|
|
max_fd = fd;
|
|
|
|
|
|
|
|
for (i = 0; i < sockets_alloc; i++)
|
|
|
|
if (sockets[i].type == AUTH_UNUSED) {
|
|
|
|
sockets[i].fd = fd;
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((sockets[i].input = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
|
|
|
if ((sockets[i].output = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
|
|
|
if ((sockets[i].request = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2003-09-22 13:04:23 +02:00
|
|
|
sockets[i].type = type;
|
1999-11-24 14:26:21 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
old_alloc = sockets_alloc;
|
2003-09-22 13:04:23 +02:00
|
|
|
new_alloc = sockets_alloc + 10;
|
2015-04-24 03:36:00 +02:00
|
|
|
sockets = xreallocarray(sockets, new_alloc, sizeof(sockets[0]));
|
2003-09-22 13:04:23 +02:00
|
|
|
for (i = old_alloc; i < new_alloc; i++)
|
1999-11-24 14:26:21 +01:00
|
|
|
sockets[i].type = AUTH_UNUSED;
|
2003-09-22 13:04:23 +02:00
|
|
|
sockets_alloc = new_alloc;
|
1999-11-24 14:26:21 +01:00
|
|
|
sockets[old_alloc].fd = fd;
|
2015-01-14 14:09:09 +01:00
|
|
|
if ((sockets[old_alloc].input = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
|
|
|
if ((sockets[old_alloc].output = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
|
|
|
if ((sockets[old_alloc].request = sshbuf_new()) == NULL)
|
|
|
|
fatal("%s: sshbuf_new failed", __func__);
|
2003-09-22 13:04:23 +02:00
|
|
|
sockets[old_alloc].type = type;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2017-07-19 03:15:02 +02:00
|
|
|
handle_socket_read(u_int socknum)
|
|
|
|
{
|
|
|
|
struct sockaddr_un sunaddr;
|
|
|
|
socklen_t slen;
|
|
|
|
uid_t euid;
|
|
|
|
gid_t egid;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
slen = sizeof(sunaddr);
|
|
|
|
fd = accept(sockets[socknum].fd, (struct sockaddr *)&sunaddr, &slen);
|
2019-06-28 15:35:04 +02:00
|
|
|
if (fd == -1) {
|
2017-07-19 03:15:02 +02:00
|
|
|
error("accept from AUTH_SOCKET: %s", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2019-06-28 15:35:04 +02:00
|
|
|
if (getpeereid(fd, &euid, &egid) == -1) {
|
2017-07-19 03:15:02 +02:00
|
|
|
error("getpeereid %d failed: %s", fd, strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((euid != 0) && (getuid() != euid)) {
|
|
|
|
error("uid mismatch: peer euid %u != uid %u",
|
|
|
|
(u_int) euid, (u_int) getuid());
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
new_socket(AUTH_CONNECTION, fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
handle_conn_read(u_int socknum)
|
|
|
|
{
|
2019-01-22 23:58:50 +01:00
|
|
|
char buf[AGENT_RBUF_LEN];
|
2017-07-19 03:15:02 +02:00
|
|
|
ssize_t len;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((len = read(sockets[socknum].fd, buf, sizeof(buf))) <= 0) {
|
|
|
|
if (len == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
return 0;
|
|
|
|
error("%s: read error on socket %u (fd %d): %s",
|
|
|
|
__func__, socknum, sockets[socknum].fd,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put(sockets[socknum].input, buf, len)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
explicit_bzero(buf, sizeof(buf));
|
|
|
|
process_message(socknum);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
handle_conn_write(u_int socknum)
|
|
|
|
{
|
|
|
|
ssize_t len;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (sshbuf_len(sockets[socknum].output) == 0)
|
|
|
|
return 0; /* shouldn't happen */
|
|
|
|
if ((len = write(sockets[socknum].fd,
|
|
|
|
sshbuf_ptr(sockets[socknum].output),
|
|
|
|
sshbuf_len(sockets[socknum].output))) <= 0) {
|
|
|
|
if (len == -1) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
return 0;
|
|
|
|
error("%s: read error on socket %u (fd %d): %s",
|
|
|
|
__func__, socknum, sockets[socknum].fd,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_consume(sockets[socknum].output, len)) != 0)
|
|
|
|
fatal("%s: buffer error: %s", __func__, ssh_err(r));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-05-11 05:38:51 +02:00
|
|
|
after_poll(struct pollfd *pfd, size_t npfd, u_int maxfds)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2017-07-19 03:15:02 +02:00
|
|
|
size_t i;
|
2018-05-11 05:38:51 +02:00
|
|
|
u_int socknum, activefds = npfd;
|
2017-07-19 03:15:02 +02:00
|
|
|
|
|
|
|
for (i = 0; i < npfd; i++) {
|
|
|
|
if (pfd[i].revents == 0)
|
|
|
|
continue;
|
|
|
|
/* Find sockets entry */
|
|
|
|
for (socknum = 0; socknum < sockets_alloc; socknum++) {
|
|
|
|
if (sockets[socknum].type != AUTH_SOCKET &&
|
|
|
|
sockets[socknum].type != AUTH_CONNECTION)
|
|
|
|
continue;
|
|
|
|
if (pfd[i].fd == sockets[socknum].fd)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (socknum >= sockets_alloc) {
|
|
|
|
error("%s: no socket for fd %d", __func__, pfd[i].fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Process events */
|
|
|
|
switch (sockets[socknum].type) {
|
|
|
|
case AUTH_SOCKET:
|
2018-05-11 05:38:51 +02:00
|
|
|
if ((pfd[i].revents & (POLLIN|POLLERR)) == 0)
|
|
|
|
break;
|
|
|
|
if (npfd > maxfds) {
|
|
|
|
debug3("out of fds (active %u >= limit %u); "
|
|
|
|
"skipping accept", activefds, maxfds);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (handle_socket_read(socknum) == 0)
|
|
|
|
activefds++;
|
2017-07-19 03:15:02 +02:00
|
|
|
break;
|
|
|
|
case AUTH_CONNECTION:
|
|
|
|
if ((pfd[i].revents & (POLLIN|POLLERR)) != 0 &&
|
|
|
|
handle_conn_read(socknum) != 0) {
|
2018-05-11 05:38:51 +02:00
|
|
|
goto close_sock;
|
2017-07-19 03:15:02 +02:00
|
|
|
}
|
|
|
|
if ((pfd[i].revents & (POLLOUT|POLLHUP)) != 0 &&
|
2018-05-11 05:38:51 +02:00
|
|
|
handle_conn_write(socknum) != 0) {
|
|
|
|
close_sock:
|
|
|
|
if (activefds == 0)
|
|
|
|
fatal("activefds == 0 at close_sock");
|
2017-07-19 03:15:02 +02:00
|
|
|
close_socket(&sockets[socknum]);
|
2018-05-11 05:38:51 +02:00
|
|
|
activefds--;
|
|
|
|
break;
|
|
|
|
}
|
2017-07-19 03:15:02 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-05-11 05:38:51 +02:00
|
|
|
prepare_poll(struct pollfd **pfdp, size_t *npfdp, int *timeoutp, u_int maxfds)
|
2017-07-19 03:15:02 +02:00
|
|
|
{
|
|
|
|
struct pollfd *pfd = *pfdp;
|
|
|
|
size_t i, j, npfd = 0;
|
2013-06-01 23:43:59 +02:00
|
|
|
time_t deadline;
|
2019-01-22 23:58:50 +01:00
|
|
|
int r;
|
2001-01-22 06:34:40 +01:00
|
|
|
|
2017-07-19 03:15:02 +02:00
|
|
|
/* Count active sockets */
|
2001-01-22 06:34:40 +01:00
|
|
|
for (i = 0; i < sockets_alloc; i++) {
|
1999-11-24 14:26:21 +01:00
|
|
|
switch (sockets[i].type) {
|
|
|
|
case AUTH_SOCKET:
|
|
|
|
case AUTH_CONNECTION:
|
2017-07-19 03:15:02 +02:00
|
|
|
npfd++;
|
1999-11-24 14:26:21 +01:00
|
|
|
break;
|
|
|
|
case AUTH_UNUSED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fatal("Unknown socket type %d", sockets[i].type);
|
|
|
|
break;
|
|
|
|
}
|
2001-01-22 06:34:40 +01:00
|
|
|
}
|
2017-07-19 03:15:02 +02:00
|
|
|
if (npfd != *npfdp &&
|
|
|
|
(pfd = recallocarray(pfd, *npfdp, npfd, sizeof(*pfd))) == NULL)
|
|
|
|
fatal("%s: recallocarray failed", __func__);
|
|
|
|
*pfdp = pfd;
|
|
|
|
*npfdp = npfd;
|
2001-01-22 06:34:40 +01:00
|
|
|
|
2017-07-19 03:15:02 +02:00
|
|
|
for (i = j = 0; i < sockets_alloc; i++) {
|
2001-01-22 06:34:40 +01:00
|
|
|
switch (sockets[i].type) {
|
|
|
|
case AUTH_SOCKET:
|
2018-05-11 05:38:51 +02:00
|
|
|
if (npfd > maxfds) {
|
|
|
|
debug3("out of fds (active %zu >= limit %u); "
|
|
|
|
"skipping arming listener", npfd, maxfds);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pfd[j].fd = sockets[i].fd;
|
|
|
|
pfd[j].revents = 0;
|
|
|
|
pfd[j].events = POLLIN;
|
|
|
|
j++;
|
|
|
|
break;
|
2001-01-22 06:34:40 +01:00
|
|
|
case AUTH_CONNECTION:
|
2017-07-19 03:15:02 +02:00
|
|
|
pfd[j].fd = sockets[i].fd;
|
|
|
|
pfd[j].revents = 0;
|
2019-01-22 23:58:50 +01:00
|
|
|
/*
|
|
|
|
* Only prepare to read if we can handle a full-size
|
|
|
|
* input read buffer and enqueue a max size reply..
|
|
|
|
*/
|
|
|
|
if ((r = sshbuf_check_reserve(sockets[i].input,
|
|
|
|
AGENT_RBUF_LEN)) == 0 &&
|
|
|
|
(r = sshbuf_check_reserve(sockets[i].output,
|
|
|
|
AGENT_MAX_LEN)) == 0)
|
|
|
|
pfd[j].events = POLLIN;
|
|
|
|
else if (r != SSH_ERR_NO_BUFFER_SPACE) {
|
|
|
|
fatal("%s: buffer error: %s",
|
|
|
|
__func__, ssh_err(r));
|
|
|
|
}
|
2015-01-14 14:09:09 +01:00
|
|
|
if (sshbuf_len(sockets[i].output) > 0)
|
2017-07-19 03:15:02 +02:00
|
|
|
pfd[j].events |= POLLOUT;
|
|
|
|
j++;
|
2001-01-22 06:34:40 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-21 10:45:06 +01:00
|
|
|
deadline = reaper();
|
|
|
|
if (parent_alive_interval != 0)
|
|
|
|
deadline = (deadline == 0) ? parent_alive_interval :
|
2016-09-12 03:22:38 +02:00
|
|
|
MINIMUM(deadline, parent_alive_interval);
|
2007-03-21 10:45:06 +01:00
|
|
|
if (deadline == 0) {
|
2017-07-24 06:34:28 +02:00
|
|
|
*timeoutp = -1; /* INFTIM */
|
2007-03-21 10:45:06 +01:00
|
|
|
} else {
|
2017-07-19 03:15:02 +02:00
|
|
|
if (deadline > INT_MAX / 1000)
|
|
|
|
*timeoutp = INT_MAX / 1000;
|
|
|
|
else
|
|
|
|
*timeoutp = deadline * 1000;
|
2007-03-21 10:45:06 +01:00
|
|
|
}
|
2001-01-22 06:34:40 +01:00
|
|
|
return (1);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2003-09-22 13:10:21 +02:00
|
|
|
cleanup_socket(void)
|
1999-10-29 01:47:09 +02:00
|
|
|
{
|
2014-07-03 13:22:40 +02:00
|
|
|
if (cleanup_pid != 0 && getpid() != cleanup_pid)
|
|
|
|
return;
|
|
|
|
debug("%s: cleanup", __func__);
|
2001-01-26 06:10:34 +01:00
|
|
|
if (socket_name[0])
|
|
|
|
unlink(socket_name);
|
|
|
|
if (socket_dir[0])
|
|
|
|
rmdir(socket_dir);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2003-10-02 08:12:36 +02:00
|
|
|
void
|
1999-10-29 01:47:09 +02:00
|
|
|
cleanup_exit(int i)
|
|
|
|
{
|
2003-09-22 13:10:21 +02:00
|
|
|
cleanup_socket();
|
|
|
|
_exit(i);
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
|
|
|
|
2006-03-26 05:28:14 +02:00
|
|
|
/*ARGSUSED*/
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2001-01-26 06:10:34 +01:00
|
|
|
cleanup_handler(int sig)
|
|
|
|
{
|
2003-09-22 13:10:21 +02:00
|
|
|
cleanup_socket();
|
2010-02-11 23:21:02 +01:00
|
|
|
#ifdef ENABLE_PKCS11
|
|
|
|
pkcs11_terminate();
|
|
|
|
#endif
|
2001-01-26 06:10:34 +01:00
|
|
|
_exit(2);
|
|
|
|
}
|
|
|
|
|
2001-07-22 22:44:00 +02:00
|
|
|
static void
|
2007-03-21 10:45:06 +01:00
|
|
|
check_parent_exists(void)
|
2001-07-22 22:44:00 +02:00
|
|
|
{
|
2011-06-03 06:14:16 +02:00
|
|
|
/*
|
|
|
|
* If our parent has exited then getppid() will return (pid_t)1,
|
|
|
|
* so testing for that should be safe.
|
|
|
|
*/
|
|
|
|
if (parent_pid != -1 && getppid() != parent_pid) {
|
2001-07-22 22:44:00 +02:00
|
|
|
/* printf("Parent has died - Authentication agent exiting.\n"); */
|
2007-03-21 10:45:06 +01:00
|
|
|
cleanup_socket();
|
|
|
|
_exit(2);
|
2001-07-22 22:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:01:22 +02:00
|
|
|
static void
|
2001-02-11 00:13:41 +01:00
|
|
|
usage(void)
|
1999-10-29 01:47:09 +02:00
|
|
|
{
|
2014-04-20 05:01:30 +02:00
|
|
|
fprintf(stderr,
|
2015-04-24 08:26:49 +02:00
|
|
|
"usage: ssh-agent [-c | -s] [-Dd] [-a bind_address] [-E fingerprint_hash]\n"
|
2016-11-30 04:07:37 +01:00
|
|
|
" [-P pkcs11_whitelist] [-t life] [command [arg ...]]\n"
|
2014-04-20 05:01:30 +02:00
|
|
|
" ssh-agent [-c | -s] -k\n");
|
1999-11-24 14:26:21 +01:00
|
|
|
exit(1);
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
int
|
|
|
|
main(int ac, char **av)
|
|
|
|
{
|
2015-04-24 07:26:44 +02:00
|
|
|
int c_flag = 0, d_flag = 0, D_flag = 0, k_flag = 0, s_flag = 0;
|
2007-02-28 11:19:58 +01:00
|
|
|
int sock, fd, ch, result, saved_errno;
|
2002-06-23 23:38:49 +02:00
|
|
|
char *shell, *format, *pidstr, *agentsocket = NULL;
|
2000-12-27 05:57:41 +01:00
|
|
|
#ifdef HAVE_SETRLIMIT
|
2000-12-02 20:03:54 +01:00
|
|
|
struct rlimit rlim;
|
2001-05-04 00:59:24 +02:00
|
|
|
#endif
|
2000-05-17 14:53:33 +02:00
|
|
|
extern int optind;
|
2002-06-23 02:20:50 +02:00
|
|
|
extern char *optarg;
|
2002-06-23 23:38:49 +02:00
|
|
|
pid_t pid;
|
|
|
|
char pidstrbuf[1 + 3 * sizeof pid];
|
2009-06-21 09:50:15 +02:00
|
|
|
size_t len;
|
2014-07-18 07:04:47 +02:00
|
|
|
mode_t prev_mask;
|
2017-07-24 06:34:28 +02:00
|
|
|
int timeout = -1; /* INFTIM */
|
2017-07-19 03:15:02 +02:00
|
|
|
struct pollfd *pfd = NULL;
|
|
|
|
size_t npfd = 0;
|
2018-05-11 05:38:51 +02:00
|
|
|
u_int maxfds;
|
2000-12-14 01:04:08 +01:00
|
|
|
|
2005-10-03 10:11:24 +02:00
|
|
|
/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
|
|
|
|
sanitise_stdfd();
|
|
|
|
|
2002-09-04 08:20:26 +02:00
|
|
|
/* drop */
|
|
|
|
setegid(getgid());
|
|
|
|
setgid(getgid());
|
|
|
|
|
2016-06-09 08:23:07 +02:00
|
|
|
platform_disable_tracing(0); /* strict=no */
|
2004-03-03 01:08:59 +01:00
|
|
|
|
2018-05-11 05:38:51 +02:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
|
|
|
|
fatal("%s: getrlimit: %s", __progname, strerror(errno));
|
|
|
|
|
2003-08-22 01:34:41 +02:00
|
|
|
__progname = ssh_get_progname(av[0]);
|
2001-03-18 23:38:15 +01:00
|
|
|
seed_rng();
|
2001-02-05 13:42:17 +01:00
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
while ((ch = getopt(ac, av, "cDdksE:a:P:t:")) != -1) {
|
1999-11-24 14:26:21 +01:00
|
|
|
switch (ch) {
|
2014-12-21 23:27:55 +01:00
|
|
|
case 'E':
|
|
|
|
fingerprint_hash = ssh_digest_alg_by_name(optarg);
|
|
|
|
if (fingerprint_hash == -1)
|
|
|
|
fatal("Invalid hash algorithm \"%s\"", optarg);
|
|
|
|
break;
|
1999-11-24 14:26:21 +01:00
|
|
|
case 'c':
|
|
|
|
if (s_flag)
|
|
|
|
usage();
|
|
|
|
c_flag++;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
k_flag++;
|
|
|
|
break;
|
2016-11-30 04:07:37 +01:00
|
|
|
case 'P':
|
|
|
|
if (pkcs11_whitelist != NULL)
|
|
|
|
fatal("-P option already specified");
|
|
|
|
pkcs11_whitelist = xstrdup(optarg);
|
|
|
|
break;
|
1999-11-24 14:26:21 +01:00
|
|
|
case 's':
|
|
|
|
if (c_flag)
|
|
|
|
usage();
|
|
|
|
s_flag++;
|
|
|
|
break;
|
2001-07-04 05:48:02 +02:00
|
|
|
case 'd':
|
2015-04-24 07:26:44 +02:00
|
|
|
if (d_flag || D_flag)
|
2001-07-04 05:48:02 +02:00
|
|
|
usage();
|
|
|
|
d_flag++;
|
|
|
|
break;
|
2015-04-24 07:26:44 +02:00
|
|
|
case 'D':
|
|
|
|
if (d_flag || D_flag)
|
|
|
|
usage();
|
|
|
|
D_flag++;
|
|
|
|
break;
|
2002-06-06 23:46:08 +02:00
|
|
|
case 'a':
|
|
|
|
agentsocket = optarg;
|
|
|
|
break;
|
2003-01-22 01:47:19 +01:00
|
|
|
case 't':
|
|
|
|
if ((lifetime = convtime(optarg)) == -1) {
|
|
|
|
fprintf(stderr, "Invalid lifetime\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
1999-11-24 14:26:21 +01:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
ac -= optind;
|
|
|
|
av += optind;
|
|
|
|
|
2015-04-24 07:26:44 +02:00
|
|
|
if (ac > 0 && (c_flag || k_flag || s_flag || d_flag || D_flag))
|
1999-11-24 14:26:21 +01:00
|
|
|
usage();
|
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
if (pkcs11_whitelist == NULL)
|
|
|
|
pkcs11_whitelist = xstrdup(DEFAULT_PKCS11_WHITELIST);
|
|
|
|
|
2002-04-02 23:03:51 +02:00
|
|
|
if (ac == 0 && !c_flag && !s_flag) {
|
1999-11-24 14:26:21 +01:00
|
|
|
shell = getenv("SHELL");
|
2009-06-21 09:50:15 +02:00
|
|
|
if (shell != NULL && (len = strlen(shell)) > 2 &&
|
|
|
|
strncmp(shell + len - 3, "csh", 3) == 0)
|
1999-11-24 14:26:21 +01:00
|
|
|
c_flag = 1;
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
if (k_flag) {
|
2006-03-31 14:11:28 +02:00
|
|
|
const char *errstr = NULL;
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
pidstr = getenv(SSH_AGENTPID_ENV_NAME);
|
|
|
|
if (pidstr == NULL) {
|
|
|
|
fprintf(stderr, "%s not set, cannot kill agent\n",
|
2001-01-22 06:34:40 +01:00
|
|
|
SSH_AGENTPID_ENV_NAME);
|
1999-11-24 14:26:21 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2006-03-31 14:11:28 +02:00
|
|
|
pid = (int)strtonum(pidstr, 2, INT_MAX, &errstr);
|
|
|
|
if (errstr) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s=\"%s\", which is not a good PID: %s\n",
|
|
|
|
SSH_AGENTPID_ENV_NAME, pidstr, errstr);
|
1999-11-24 14:26:21 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (kill(pid, SIGTERM) == -1) {
|
|
|
|
perror("kill");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
|
|
|
|
printf(format, SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
printf(format, SSH_AGENTPID_ENV_NAME);
|
2002-06-11 18:42:49 +02:00
|
|
|
printf("echo Agent pid %ld killed;\n", (long)pid);
|
1999-11-24 14:26:21 +01:00
|
|
|
exit(0);
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
2018-05-11 05:38:51 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimum file descriptors:
|
|
|
|
* stdio (3) + listener (1) + syslog (1 maybe) + connection (1) +
|
|
|
|
* a few spare for libc / stack protectors / sanitisers, etc.
|
|
|
|
*/
|
|
|
|
#define SSH_AGENT_MIN_FDS (3+1+1+1+4)
|
|
|
|
if (rlim.rlim_cur < SSH_AGENT_MIN_FDS)
|
2018-11-09 03:57:58 +01:00
|
|
|
fatal("%s: file descriptor rlimit %lld too low (minimum %u)",
|
2018-05-11 05:38:51 +02:00
|
|
|
__progname, (long long)rlim.rlim_cur, SSH_AGENT_MIN_FDS);
|
|
|
|
maxfds = rlim.rlim_cur - SSH_AGENT_MIN_FDS;
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
parent_pid = getpid();
|
|
|
|
|
2002-06-06 23:46:08 +02:00
|
|
|
if (agentsocket == NULL) {
|
|
|
|
/* Create private directory for agent socket */
|
2010-12-01 01:50:35 +01:00
|
|
|
mktemp_proto(socket_dir, sizeof(socket_dir));
|
2002-06-06 23:46:08 +02:00
|
|
|
if (mkdtemp(socket_dir) == NULL) {
|
|
|
|
perror("mkdtemp: private socket dir");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-06-11 18:42:49 +02:00
|
|
|
snprintf(socket_name, sizeof socket_name, "%s/agent.%ld", socket_dir,
|
|
|
|
(long)parent_pid);
|
2002-06-06 23:46:08 +02:00
|
|
|
} else {
|
|
|
|
/* Try to use specified agent socket */
|
|
|
|
socket_dir[0] = '\0';
|
|
|
|
strlcpy(socket_name, agentsocket, sizeof socket_name);
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Create socket early so it will exist before command gets run from
|
|
|
|
* the parent.
|
|
|
|
*/
|
2014-07-18 07:04:47 +02:00
|
|
|
prev_mask = umask(0177);
|
2014-07-18 06:11:24 +02:00
|
|
|
sock = unix_listener(socket_name, SSH_LISTEN_BACKLOG, 0);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (sock < 0) {
|
2014-07-18 06:11:24 +02:00
|
|
|
/* XXX - unix_listener() calls error() not perror() */
|
2004-11-05 10:26:49 +01:00
|
|
|
*socket_name = '\0'; /* Don't unlink any existing file */
|
1999-11-24 14:26:21 +01:00
|
|
|
cleanup_exit(1);
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
2014-07-18 07:04:47 +02:00
|
|
|
umask(prev_mask);
|
2001-01-22 06:34:40 +01:00
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
|
|
|
* Fork, and have the parent execute the command, if any, or present
|
|
|
|
* the socket data. The child continues as the authentication agent.
|
|
|
|
*/
|
2015-04-24 07:26:44 +02:00
|
|
|
if (D_flag || d_flag) {
|
|
|
|
log_init(__progname,
|
|
|
|
d_flag ? SYSLOG_LEVEL_DEBUG3 : SYSLOG_LEVEL_INFO,
|
|
|
|
SYSLOG_FACILITY_AUTH, 1);
|
2001-07-04 05:48:02 +02:00
|
|
|
format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
|
|
|
|
printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
|
|
|
|
SSH_AUTHSOCKET_ENV_NAME);
|
2002-06-11 18:42:49 +02:00
|
|
|
printf("echo Agent pid %ld;\n", (long)parent_pid);
|
2015-12-11 03:29:03 +01:00
|
|
|
fflush(stdout);
|
2001-07-04 05:48:02 +02:00
|
|
|
goto skip;
|
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
perror("fork");
|
2002-02-08 12:05:41 +01:00
|
|
|
cleanup_exit(1);
|
1999-10-29 01:47:09 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
if (pid != 0) { /* Parent - execute the given command. */
|
|
|
|
close(sock);
|
2002-06-11 18:42:49 +02:00
|
|
|
snprintf(pidstrbuf, sizeof pidstrbuf, "%ld", (long)pid);
|
1999-11-24 14:26:21 +01:00
|
|
|
if (ac == 0) {
|
|
|
|
format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
|
|
|
|
printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
|
2001-01-22 06:34:40 +01:00
|
|
|
SSH_AUTHSOCKET_ENV_NAME);
|
1999-11-24 14:26:21 +01:00
|
|
|
printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
|
2001-01-22 06:34:40 +01:00
|
|
|
SSH_AGENTPID_ENV_NAME);
|
2002-06-11 18:42:49 +02:00
|
|
|
printf("echo Agent pid %ld;\n", (long)pid);
|
1999-11-24 14:26:21 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
2000-09-16 04:29:08 +02:00
|
|
|
if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
|
|
|
|
setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
|
|
|
|
perror("setenv");
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
execvp(av[0], av);
|
|
|
|
perror(av[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-02-08 12:05:41 +01:00
|
|
|
/* child */
|
|
|
|
log_init(__progname, SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_AUTH, 0);
|
2001-07-22 22:40:24 +02:00
|
|
|
|
|
|
|
if (setsid() == -1) {
|
2002-02-08 12:05:41 +01:00
|
|
|
error("setsid: %s", strerror(errno));
|
2001-07-22 22:40:24 +02:00
|
|
|
cleanup_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)chdir("/");
|
2003-01-24 01:36:23 +01:00
|
|
|
if ((fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
|
|
|
|
/* XXX might close listen socket */
|
|
|
|
(void)dup2(fd, STDIN_FILENO);
|
|
|
|
(void)dup2(fd, STDOUT_FILENO);
|
|
|
|
(void)dup2(fd, STDERR_FILENO);
|
|
|
|
if (fd > 2)
|
|
|
|
close(fd);
|
|
|
|
}
|
1999-10-29 01:47:09 +02:00
|
|
|
|
2000-12-27 05:57:41 +01:00
|
|
|
#ifdef HAVE_SETRLIMIT
|
2000-12-02 20:03:54 +01:00
|
|
|
/* deny core dumps, since memory contains unencrypted private keys */
|
|
|
|
rlim.rlim_cur = rlim.rlim_max = 0;
|
2019-06-28 15:35:04 +02:00
|
|
|
if (setrlimit(RLIMIT_CORE, &rlim) == -1) {
|
2002-02-08 12:05:41 +01:00
|
|
|
error("setrlimit RLIMIT_CORE: %s", strerror(errno));
|
2000-12-02 20:03:54 +01:00
|
|
|
cleanup_exit(1);
|
|
|
|
}
|
2000-12-27 05:57:41 +01:00
|
|
|
#endif
|
2001-07-04 05:48:02 +02:00
|
|
|
|
|
|
|
skip:
|
2010-02-11 23:21:02 +01:00
|
|
|
|
2014-07-03 13:22:40 +02:00
|
|
|
cleanup_pid = getpid();
|
|
|
|
|
2010-02-11 23:21:02 +01:00
|
|
|
#ifdef ENABLE_PKCS11
|
|
|
|
pkcs11_init(0);
|
|
|
|
#endif
|
1999-11-24 14:26:21 +01:00
|
|
|
new_socket(AUTH_SOCKET, sock);
|
2007-03-21 10:45:06 +01:00
|
|
|
if (ac > 0)
|
|
|
|
parent_alive_interval = 10;
|
2000-08-23 02:46:23 +02:00
|
|
|
idtab_init();
|
2001-07-14 04:12:55 +02:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2015-04-24 07:26:44 +02:00
|
|
|
signal(SIGINT, (d_flag | D_flag) ? cleanup_handler : SIG_IGN);
|
2001-01-26 06:10:34 +01:00
|
|
|
signal(SIGHUP, cleanup_handler);
|
|
|
|
signal(SIGTERM, cleanup_handler);
|
2001-07-18 17:58:08 +02:00
|
|
|
|
2016-11-30 04:07:37 +01:00
|
|
|
if (pledge("stdio rpath cpath unix id proc exec", NULL) == -1)
|
2015-12-02 00:29:24 +01:00
|
|
|
fatal("%s: pledge: %s", __progname, strerror(errno));
|
2016-01-08 04:24:56 +01:00
|
|
|
platform_pledge_agent();
|
2015-12-02 00:29:24 +01:00
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
while (1) {
|
2018-05-11 05:38:51 +02:00
|
|
|
prepare_poll(&pfd, &npfd, &timeout, maxfds);
|
2017-07-19 03:15:02 +02:00
|
|
|
result = poll(pfd, npfd, timeout);
|
2007-02-28 11:19:58 +01:00
|
|
|
saved_errno = errno;
|
2007-03-21 10:45:06 +01:00
|
|
|
if (parent_alive_interval != 0)
|
|
|
|
check_parent_exists();
|
|
|
|
(void) reaper(); /* remove expired keys */
|
2019-06-28 15:35:04 +02:00
|
|
|
if (result == -1) {
|
2007-02-28 11:19:58 +01:00
|
|
|
if (saved_errno == EINTR)
|
1999-11-24 14:26:21 +01:00
|
|
|
continue;
|
2017-07-19 03:15:02 +02:00
|
|
|
fatal("poll: %s", strerror(saved_errno));
|
2007-02-28 11:19:58 +01:00
|
|
|
} else if (result > 0)
|
2018-05-11 05:38:51 +02:00
|
|
|
after_poll(pfd, npfd, maxfds);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
1999-11-24 14:26:21 +01:00
|
|
|
/* NOTREACHED */
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|