vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Petr Lautrbach 5160c9
diff --git a/Makefile.in b/Makefile.in
Petr Lautrbach 190035
index 8e11217..9311e16 100644
Petr Lautrbach 5160c9
--- a/Makefile.in
Petr Lautrbach 5160c9
+++ b/Makefile.in
Petr Lautrbach 190035
@@ -92,7 +92,8 @@ LIBSSH_OBJS=${LIBOPENSSH_OBJS} \
Petr Lautrbach 5160c9
 	ssh-pkcs11.o krl.o smult_curve25519_ref.o \
Petr Lautrbach 5160c9
 	kexc25519.o kexc25519c.o poly1305.o chacha.o cipher-chachapoly.o \
Petr Lautrbach 163064
 	ssh-ed25519.o digest-openssl.o hmac.o utf8_stringprep.o \
Petr Lautrbach 5160c9
-	sc25519.o ge25519.o fe25519.o ed25519.o verify.o hash.o blocks.o
Petr Lautrbach 5160c9
+	sc25519.o ge25519.o fe25519.o ed25519.o verify.o hash.o blocks.o \
Petr Lautrbach 5160c9
+	auditstub.o
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 SSHOBJS= ssh.o readconf.o clientloop.o sshtty.o \
Petr Lautrbach d3d59d
 	sshconnect.o sshconnect1.o sshconnect2.o mux.o \
Petr Lautrbach 5160c9
diff --git a/audit-bsm.c b/audit-bsm.c
Petr Lautrbach 190035
index 6135591..c7a1b47 100644
Petr Lautrbach 5160c9
--- a/audit-bsm.c
Petr Lautrbach 5160c9
+++ b/audit-bsm.c
Petr Lautrbach 5160c9
@@ -375,10 +375,23 @@ audit_connection_from(const char *host, int port)
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	/* not implemented */
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_count_session_open(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not necessary */
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach 5160c9
@@ -393,6 +406,12 @@ audit_session_close(struct logininfo *li)
Petr Lautrbach d3d59d
 	/* not implemented */
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
@@ -454,4 +473,40 @@ audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 		debug("%s: unhandled event %d", __func__, event);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_generate_ephemeral_server_key(const char *fp)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
 #endif /* BSM */
Petr Lautrbach 5160c9
diff --git a/audit-linux.c b/audit-linux.c
Petr Lautrbach 190035
index b3ee2f4..bff8180 100644
Petr Lautrbach 5160c9
--- a/audit-linux.c
Petr Lautrbach 5160c9
+++ b/audit-linux.c
Petr Lautrbach 190035
@@ -35,13 +35,25 @@
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include "log.h"
Petr Lautrbach d3d59d
 #include "audit.h"
Petr Lautrbach d3d59d
+#include "key.h"
Petr Lautrbach d3d59d
+#include "hostfile.h"
Petr Lautrbach d3d59d
+#include "auth.h"
Petr Lautrbach 190035
+#include "misc.h"      /* servconf.h needs misc.h for struct ForwardOptions */
Petr Lautrbach d3d59d
+#include "servconf.h"
Petr Lautrbach d3d59d
 #include "canohost.h"
Petr Lautrbach d3d59d
+#include "packet.h"
Petr Lautrbach d3d59d
+#include "cipher.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach cf5c11
+#define AUDIT_LOG_SIZE 256
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+extern ServerOptions options;
Petr Lautrbach d3d59d
+extern Authctxt *the_authctxt;
Petr Lautrbach d3d59d
+extern u_int utmp_len;
Petr Lautrbach d3d59d
 const char* audit_username(void);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-int
Petr Lautrbach d3d59d
-linux_audit_record_event(int uid, const char *username,
Petr Lautrbach d3d59d
-    const char *hostname, const char *ip, const char *ttyn, int success)
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+linux_audit_user_logxxx(int uid, const char *username,
Petr Lautrbach d3d59d
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -49,11 +61,11 @@ linux_audit_record_event(int uid, const char *username,
Petr Lautrbach d3d59d
 	if (audit_fd < 0) {
Petr Lautrbach d3d59d
 		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
 		    errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
-			return 1; /* No audit support in kernel */
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Petr Lautrbach d3d59d
 		else
Petr Lautrbach d3d59d
-			return 0; /* Must prevent login */
Petr Lautrbach d3d59d
+			goto fatal_report; /* Must prevent login */
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
-	rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, event,
Petr Lautrbach d3d59d
 	    NULL, "login", username ? username : "(unknown)",
Petr Lautrbach d3d59d
 	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
Petr Lautrbach d3d59d
 	saved_errno = errno;
Petr Lautrbach 190035
@@ -65,35 +77,154 @@ linux_audit_record_event(int uid, const char *username,
Petr Lautrbach d3d59d
 	if ((rc == -EPERM) && (geteuid() != 0))
Petr Lautrbach d3d59d
 		rc = 0;
Petr Lautrbach d3d59d
 	errno = saved_errno;
Petr Lautrbach d3d59d
-	return (rc >= 0);
Petr Lautrbach d3d59d
+	if (rc < 0) {
Petr Lautrbach d3d59d
+fatal_report:
Petr Lautrbach d3d59d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+linux_audit_user_auth(int uid, const char *username,
Petr Lautrbach d3d59d
+    const char *hostname, const char *ip, const char *ttyn, int success, int event)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
+	static const char *event_name[] = {
Petr Lautrbach d3d59d
+		"maxtries exceeded",
Petr Lautrbach d3d59d
+		"root denied",
Petr Lautrbach d3d59d
+		"success",
Petr Lautrbach d3d59d
+		"none",
Petr Lautrbach d3d59d
+		"password",
Petr Lautrbach d3d59d
+		"challenge-response",
Petr Lautrbach d3d59d
+		"pubkey",
Petr Lautrbach d3d59d
+		"hostbased",
Petr Lautrbach d3d59d
+		"gssapi",
Petr Lautrbach d3d59d
+		"invalid user",
Petr Lautrbach d3d59d
+		"nologin",
Petr Lautrbach d3d59d
+		"connection closed",
Petr Lautrbach d3d59d
+		"connection abandoned",
Petr Lautrbach d3d59d
+		"unknown"
Petr Lautrbach d3d59d
+	};
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+		    errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Petr Lautrbach d3d59d
+		else
Petr Lautrbach d3d59d
+			goto fatal_report; /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	
Petr Lautrbach d3d59d
+	if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
Petr Lautrbach d3d59d
+		event = SSH_AUDIT_UNKNOWN;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
Petr Lautrbach d3d59d
+	    NULL, event_name[event], username ? username : "(unknown)",
Petr Lautrbach d3d59d
+	    username == NULL ? uid : -1, hostname, ip, ttyn, success);
Petr Lautrbach d3d59d
+	saved_errno = errno;
Petr Lautrbach d3d59d
+	close(audit_fd);
Petr Lautrbach d3d59d
+	/*
Petr Lautrbach d3d59d
+	 * Do not report error if the error is EPERM and sshd is run as non
Petr Lautrbach d3d59d
+	 * root user.
Petr Lautrbach d3d59d
+	 */
Petr Lautrbach d3d59d
+	if ((rc == -EPERM) && (geteuid() != 0))
Petr Lautrbach d3d59d
+		rc = 0;
Petr Lautrbach d3d59d
+	errno = saved_errno;
Petr Lautrbach d3d59d
+	if (rc < 0) {
Petr Lautrbach d3d59d
+fatal_report:
Petr Lautrbach d3d59d
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, rc, saved_errno;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+					 errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return 1; /* No audit support in kernel */
Petr Lautrbach d3d59d
+		else                                                                                                                                       
Petr Lautrbach d3d59d
+			return 0; /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "%s_auth rport=%d", host_user ? "pubkey" : "hostbased", get_remote_port());
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
Petr Lautrbach d3d59d
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
Petr Lautrbach d3d59d
+	if ((rc < 0) && ((rc != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		goto out;
Petr Lautrbach 190035
+	/* is the fingerprint_prefix() still needed? 
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s%s rport=%d",
Petr Lautrbach d3d59d
+			type, bits, key_fingerprint_prefix(), fp, get_remote_port());
Petr Lautrbach 190035
+	*/
Petr Lautrbach 190035
+	snprintf(buf, sizeof(buf), "key algo=%s size=%d fp=%s rport=%d",
Petr Lautrbach 190035
+			type, bits, fp, get_remote_port());
Petr Lautrbach d3d59d
+	rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH, NULL,
Petr Lautrbach d3d59d
+		buf, audit_username(), -1, NULL, get_remote_ipaddr(), NULL, rv);
Petr Lautrbach d3d59d
+out:
Petr Lautrbach d3d59d
+	saved_errno = errno;
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	errno = saved_errno;
Petr Lautrbach d3d59d
+	/* do not report error if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	return (rc >= 0) || ((rc == -EPERM) && (getuid() != 0));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static int user_login_count = 0;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* Below is the sshd audit API code */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_connection_from(const char *host, int port)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-}
Petr Lautrbach d3d59d
 	/* not implemented */
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	/* not implemented */
Petr Lautrbach d3d59d
+	if (!user_login_count++) 
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+		    NULL, "ssh", 1, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+	    NULL, "ssh", 1, AUDIT_USER_START);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+	    NULL, "ssh", 1, AUDIT_USER_END);
Petr Lautrbach d3d59d
+	if (user_login_count && !--user_login_count) 
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+		    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_count_session_open(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	user_login_count++;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_session_open(struct logininfo *li)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	if (linux_audit_record_event(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
-	    NULL, li->line, 1) == 0)
Petr Lautrbach d3d59d
-		fatal("linux_audit_write_entry failed: %s", strerror(errno));
Petr Lautrbach d3d59d
+	if (!user_login_count++) 
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+		    NULL, li->line, 1, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+	    NULL, li->line, 1, AUDIT_USER_START);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 audit_session_close(struct logininfo *li)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	/* not implemented */
Petr Lautrbach d3d59d
+	linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+	    NULL, li->line, 1, AUDIT_USER_END);
Petr Lautrbach d3d59d
+	if (user_login_count && !--user_login_count) 
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(li->uid, NULL, li->hostname,
Petr Lautrbach d3d59d
+		    NULL, li->line, 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach 190035
@@ -101,21 +232,43 @@ audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	switch(event) {
Petr Lautrbach d3d59d
 	case SSH_AUTH_SUCCESS:
Petr Lautrbach d3d59d
-	case SSH_CONNECTION_CLOSE:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
+			get_remote_ipaddr(), "ssh", 1, event);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	case SSH_NOLOGIN:
Petr Lautrbach d3d59d
-	case SSH_LOGIN_EXCEED_MAXTRIES:
Petr Lautrbach d3d59d
 	case SSH_LOGIN_ROOT_DENIED:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
+			get_remote_ipaddr(), "ssh", 0, event);
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
 		break;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	case SSH_LOGIN_EXCEED_MAXTRIES:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_NONE:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_PASSWD:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_KBDINT:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_PUBKEY:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_HOSTBASED:
Petr Lautrbach d3d59d
 	case SSH_AUTH_FAIL_GSSAPI:
Petr Lautrbach d3d59d
+		linux_audit_user_auth(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
+			get_remote_ipaddr(), "ssh", 0, event);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	case SSH_CONNECTION_CLOSE:
Petr Lautrbach d3d59d
+		if (user_login_count) {
Petr Lautrbach d3d59d
+			while (user_login_count--)
Petr Lautrbach d3d59d
+				linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+				    NULL, "ssh", 1, AUDIT_USER_END);
Petr Lautrbach d3d59d
+			linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
Petr Lautrbach d3d59d
+			    NULL, "ssh", 1, AUDIT_USER_LOGOUT);
Petr Lautrbach d3d59d
+		}
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	case SSH_CONNECTION_ABANDON:
Petr Lautrbach d3d59d
 	case SSH_INVALID_USER:
Petr Lautrbach d3d59d
-		linux_audit_record_event(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
-			get_remote_ipaddr(), "sshd", 0);
Petr Lautrbach d3d59d
+		linux_audit_user_logxxx(-1, audit_username(), NULL,
Petr Lautrbach d3d59d
+			get_remote_ipaddr(), "ssh", 0, AUDIT_USER_LOGIN);
Petr Lautrbach d3d59d
 		break;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	default:
Petr Lautrbach 190035
@@ -123,4 +276,135 @@ audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+#ifdef AUDIT_CRYPTO_SESSION
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	const static char *name[] = { "cipher", "mac", "comp" };
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+	int audit_fd;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=unsupported-%s direction=? cipher=? ksize=? rport=%d laddr=%s lport=%d ",
Petr Lautrbach d3d59d
+		name[what], get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())),
Petr Lautrbach d3d59d
+		get_local_port());
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0)
Petr Lautrbach d3d59d
+		/* no problem, the next instruction will be fatal() */
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+	audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
Petr Lautrbach d3d59d
+			buf, NULL, get_remote_ipaddr(), NULL, 0);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+const static char *direction[] = { "from-server", "from-client", "both" };
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
Petr Lautrbach d3d59d
+	       uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+#ifdef AUDIT_CRYPTO_SESSION
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Petr Lautrbach 190035
+	const Cipher *cipher = cipher_by_name(enc);
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
+	snprintf(buf, sizeof(buf), "op=start direction=%s cipher=%s ksize=%d mac=%s pfs=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Petr Lautrbach 190035
+		direction[ctos], enc, cipher ? 8 * cipher->key_len : 0, mac, pfs,
Petr Lautrbach d3d59d
+		(intmax_t)pid, (intmax_t)uid,
Petr Lautrbach d3d59d
+		get_remote_port(), (s = get_local_ipaddr(packet_get_connection_in())), get_local_port());
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno == EINVAL || errno == EPROTONOSUPPORT ||
Petr Lautrbach d3d59d
+					 errno == EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			return; /* No audit support in kernel */
Petr Lautrbach d3d59d
+		else                                                                                                                                       
Petr Lautrbach d3d59d
+			fatal("cannot open audit"); /* Must prevent login */
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_SESSION,
Petr Lautrbach d3d59d
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		fatal("cannot write into audit"); /* Must prevent login */
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Petr Lautrbach d3d59d
+	char *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=destroy kind=session fp=? direction=%s spid=%jd suid=%jd rport=%d laddr=%s lport=%d ",
Petr Lautrbach d3d59d
+		 direction[ctos], (intmax_t)pid, (intmax_t)uid,
Petr Lautrbach d3d59d
+		 get_remote_port(),
Petr Lautrbach d3d59d
+		 (s = get_local_ipaddr(packet_get_connection_in())),
Petr Lautrbach d3d59d
+		 get_local_port());
Petr Lautrbach 84822b
+	free(s);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Petr Lautrbach d3d59d
+					 errno != EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			error("cannot open audit");
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Petr Lautrbach d3d59d
+			buf, NULL, get_remote_ipaddr(), NULL, 1);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		error("cannot write into audit");
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd ",
Petr Lautrbach d3d59d
+		fp, (intmax_t)pid, (intmax_t)uid);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Petr Lautrbach d3d59d
+					 errno != EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			error("cannot open audit");
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Petr Lautrbach d3d59d
+			buf, NULL,
Petr Lautrbach d3d59d
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
Petr Lautrbach d3d59d
+			NULL, 1);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		error("cannot write into audit");
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_generate_ephemeral_server_key(const char *fp)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char buf[AUDIT_LOG_SIZE];
Petr Lautrbach d3d59d
+	int audit_fd, audit_ok;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=? ", fp);
Petr Lautrbach d3d59d
+	audit_fd = audit_open();
Petr Lautrbach d3d59d
+	if (audit_fd < 0) {
Petr Lautrbach d3d59d
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Petr Lautrbach d3d59d
+					 errno != EAFNOSUPPORT)
Petr Lautrbach d3d59d
+			error("cannot open audit");
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Petr Lautrbach d3d59d
+			buf, NULL, 0, NULL, 1);
Petr Lautrbach d3d59d
+	audit_close(audit_fd);
Petr Lautrbach d3d59d
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Petr Lautrbach d3d59d
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Petr Lautrbach d3d59d
+		error("cannot write into audit");
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
 #endif /* USE_LINUX_AUDIT */
Petr Lautrbach 5160c9
diff --git a/audit.c b/audit.c
Petr Lautrbach 190035
index ced57fa..18908b4 100644
Petr Lautrbach 5160c9
--- a/audit.c
Petr Lautrbach 5160c9
+++ b/audit.c
Petr Lautrbach d3d59d
@@ -28,6 +28,7 @@
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include <stdarg.h>
Petr Lautrbach d3d59d
 #include <string.h>
Petr Lautrbach d3d59d
+#include <unistd.h>
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -36,6 +37,11 @@
Petr Lautrbach d3d59d
 #include "key.h"
Petr Lautrbach d3d59d
 #include "hostfile.h"
Petr Lautrbach d3d59d
 #include "auth.h"
Petr Lautrbach d3d59d
+#include "ssh-gss.h"
Petr Lautrbach d3d59d
+#include "monitor_wrap.h"
Petr Lautrbach d3d59d
+#include "xmalloc.h"
Petr Lautrbach 190035
+#include "misc.h"
Petr Lautrbach 190035
+#include "servconf.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /*
Petr Lautrbach d3d59d
  * Care must be taken when using this since it WILL NOT be initialized when
Petr Lautrbach 190035
@@ -43,6 +49,7 @@
Petr Lautrbach 190035
  * audit_event(CONNECTION_ABANDON) is called.  Test for NULL before using.
Petr Lautrbach 190035
  */
Petr Lautrbach 190035
 extern Authctxt *the_authctxt;
Petr Lautrbach 190035
+extern ServerOptions options;
Petr Lautrbach 190035
 
Petr Lautrbach 190035
 /* Maybe add the audit class to struct Authmethod? */
Petr Lautrbach 190035
 ssh_audit_event_t
Petr Lautrbach 190035
@@ -71,13 +78,10 @@ audit_classify_auth(const char *method)
Petr Lautrbach 414bfa
 const char *
Petr Lautrbach 414bfa
 audit_username(void)
Petr Lautrbach 414bfa
 {
Petr Lautrbach 414bfa
-	static const char unknownuser[] = "(unknown user)";
Petr Lautrbach 414bfa
-	static const char invaliduser[] = "(invalid user)";
Petr Lautrbach 414bfa
+	static const char unknownuser[] = "(unknown)";
Petr Lautrbach 414bfa
 
Petr Lautrbach 414bfa
-	if (the_authctxt == NULL || the_authctxt->user == NULL)
Petr Lautrbach 414bfa
+	if (the_authctxt == NULL || the_authctxt->user == NULL || !the_authctxt->valid)
Petr Lautrbach 414bfa
 		return (unknownuser);
Petr Lautrbach 414bfa
-	if (!the_authctxt->valid)
Petr Lautrbach 414bfa
-		return (invaliduser);
Petr Lautrbach 414bfa
 	return (the_authctxt->user);
Petr Lautrbach 414bfa
 }
Petr Lautrbach 414bfa
 
Petr Lautrbach 190035
@@ -111,6 +115,40 @@ audit_event_lookup(ssh_audit_event_t ev)
Petr Lautrbach d3d59d
 	return(event_lookup[i].name);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_key(int host_user, int *rv, const Key *key)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	char *fp;
Petr Lautrbach d3d59d
+	const char *crypto_name;
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
+	fp = key_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+	if (key->type == KEY_RSA1)
Petr Lautrbach d3d59d
+		crypto_name = "ssh-rsa1";
Petr Lautrbach d3d59d
+	else
Petr Lautrbach d3d59d
+		crypto_name = key_ssh_name(key);
Petr Lautrbach d3d59d
+	if (audit_keyusage(host_user, crypto_name, key_size(key), fp, *rv) == 0)
Petr Lautrbach d3d59d
+		*rv = 0;
Petr Lautrbach 84822b
+	free(fp);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	PRIVSEP(audit_unsupported_body(what));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
Petr Lautrbach d3d59d
+{
Petr Lautrbach 190035
+	PRIVSEP(audit_kex_body(ctos, enc, mac, comp, pfs, getpid(), getuid()));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free(int ctos)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	PRIVSEP(audit_session_key_free_body(ctos, getpid(), getuid()));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 # ifndef CUSTOM_SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 /*
Petr Lautrbach d3d59d
  * Null implementations of audit functions.
Petr Lautrbach 190035
@@ -140,6 +178,17 @@ audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /*
Petr Lautrbach d3d59d
+ * Called when a child process has called, or will soon call,
Petr Lautrbach d3d59d
+ * audit_session_open.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_count_session_open(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit count session open euid %d user %s", geteuid(),
Petr Lautrbach d3d59d
+	      audit_username());
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
  * Called when a user session is started.  Argument is the tty allocated to
Petr Lautrbach d3d59d
  * the session, or NULL if no tty was allocated.
Petr Lautrbach d3d59d
  *
Petr Lautrbach 190035
@@ -174,13 +223,91 @@ audit_session_close(struct logininfo *li)
Petr Lautrbach d3d59d
 /*
Petr Lautrbach d3d59d
  * This will be called when a user runs a non-interactive command.  Note that
Petr Lautrbach d3d59d
  * it may be called multiple times for a single connection since SSH2 allows
Petr Lautrbach d3d59d
- * multiple sessions within a single connection.
Petr Lautrbach d3d59d
+ * multiple sessions within a single connection.  Returns a "handle" for
Petr Lautrbach d3d59d
+ * audit_end_command.
Petr Lautrbach d3d59d
  */
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	debug("audit run command euid %d user %s command '%.200s'", geteuid(),
Petr Lautrbach d3d59d
 	    audit_username(), command);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called when the non-interactive command finishes.  Note that
Petr Lautrbach d3d59d
+ * it may be called multiple times for a single connection since SSH2 allows
Petr Lautrbach d3d59d
+ * multiple sessions within a single connection.  "handle" should come from
Petr Lautrbach d3d59d
+ * the corresponding audit_run_command.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit end nopty exec  euid %d user %s command '%.200s'", geteuid(),
Petr Lautrbach d3d59d
+	    audit_username(), command);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called when user is successfully autherized by the RSA1/RSA/DSA key.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * Type is the key type, len is the key length(byte) and fp is the fingerprint of the key.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+audit_keyusage(int host_user, const char *type, unsigned bits, char *fp, int rv)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit %s key usage euid %d user %s key type %s key length %d fingerprint %s%s, result %d", 
Petr Lautrbach d3d59d
+		host_user ? "pubkey" : "hostbased", geteuid(), audit_username(), type, bits,
Petr Lautrbach d3d59d
+		key_fingerprint_prefix(), fp, rv);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called when the protocol negotiation fails.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit unsupported protocol euid %d type %d", geteuid(), what);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called on succesfull protocol negotiation.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex_body(int ctos, char *enc, char *mac, char *compress, char *pfs, pid_t pid,
Petr Lautrbach d3d59d
+	       uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach 190035
+	debug("audit protocol negotiation euid %d direction %d cipher %s mac %s compresion %s pfs %s from pid %ld uid %u",
Petr Lautrbach 190035
+		(unsigned)geteuid(), ctos, enc, mac, compress, pfs, (long)pid,
Petr Lautrbach d3d59d
+	        (unsigned)uid);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called on succesfull session key discard
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Petr Lautrbach d3d59d
+		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called on destroy private part of the server key
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
Petr Lautrbach d3d59d
+		geteuid(), fp, (long)pid, (unsigned)uid);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * This will be called on generation of the ephemeral server key
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_generate_ephemeral_server_key(const char *)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Petr Lautrbach d3d59d
 #endif /* SSH_AUDIT_EVENTS */
Petr Lautrbach 5160c9
diff --git a/audit.h b/audit.h
Petr Lautrbach 190035
index 92ede5b..903df66 100644
Petr Lautrbach 5160c9
--- a/audit.h
Petr Lautrbach 5160c9
+++ b/audit.h
Petr Lautrbach d3d59d
@@ -28,6 +28,7 @@
Petr Lautrbach d3d59d
 # define _SSH_AUDIT_H
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include "loginrec.h"
Petr Lautrbach d3d59d
+#include "key.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 enum ssh_audit_event_type {
Petr Lautrbach d3d59d
 	SSH_LOGIN_EXCEED_MAXTRIES,
Petr Lautrbach d3d59d
@@ -47,11 +48,25 @@ enum ssh_audit_event_type {
Petr Lautrbach d3d59d
 };
Petr Lautrbach d3d59d
 typedef enum ssh_audit_event_type ssh_audit_event_t;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int	listening_for_clients(void);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void	audit_connection_from(const char *, int);
Petr Lautrbach d3d59d
 void	audit_event(ssh_audit_event_t);
Petr Lautrbach d3d59d
+void	audit_count_session_open(void);
Petr Lautrbach d3d59d
 void	audit_session_open(struct logininfo *);
Petr Lautrbach d3d59d
 void	audit_session_close(struct logininfo *);
Petr Lautrbach d3d59d
-void	audit_run_command(const char *);
Petr Lautrbach d3d59d
+int	audit_run_command(const char *);
Petr Lautrbach d3d59d
+void 	audit_end_command(int, const char *);
Petr Lautrbach d3d59d
 ssh_audit_event_t audit_classify_auth(const char *);
Petr Lautrbach d3d59d
+int	audit_keyusage(int, const char *, unsigned, char *, int);
Petr Lautrbach d3d59d
+void	audit_key(int, int *, const Key *);
Petr Lautrbach d3d59d
+void	audit_unsupported(int);
Petr Lautrbach 190035
+void	audit_kex(int, char *, char *, char *, char *);
Petr Lautrbach d3d59d
+void	audit_unsupported_body(int);
Petr Lautrbach 190035
+void	audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
Petr Lautrbach d3d59d
+void	audit_session_key_free(int ctos);
Petr Lautrbach d3d59d
+void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Petr Lautrbach d3d59d
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Petr Lautrbach d3d59d
+void	audit_generate_ephemeral_server_key(const char *);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #endif /* _SSH_AUDIT_H */
Petr Lautrbach 5160c9
diff --git a/auditstub.c b/auditstub.c
Petr Lautrbach 5160c9
new file mode 100644
Petr Lautrbach 190035
index 0000000..116f460
Petr Lautrbach 5160c9
--- /dev/null
Petr Lautrbach 5160c9
+++ b/auditstub.c
Petr Lautrbach d3d59d
@@ -0,0 +1,50 @@
Petr Lautrbach d3d59d
+/* $Id: auditstub.c,v 1.1 jfch Exp $ */
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Copyright 2010 Red Hat, Inc.  All rights reserved.
Petr Lautrbach d3d59d
+ * Use is subject to license terms.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * Redistribution and use in source and binary forms, with or without
Petr Lautrbach d3d59d
+ * modification, are permitted provided that the following conditions
Petr Lautrbach d3d59d
+ * are met:
Petr Lautrbach d3d59d
+ * 1. Redistributions of source code must retain the above copyright
Petr Lautrbach d3d59d
+ *    notice, this list of conditions and the following disclaimer.
Petr Lautrbach d3d59d
+ * 2. Redistributions in binary form must reproduce the above copyright
Petr Lautrbach d3d59d
+ *    notice, this list of conditions and the following disclaimer in the
Petr Lautrbach d3d59d
+ *    documentation and/or other materials provided with the distribution.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Petr Lautrbach d3d59d
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Petr Lautrbach d3d59d
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Petr Lautrbach d3d59d
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Petr Lautrbach d3d59d
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Petr Lautrbach d3d59d
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Petr Lautrbach d3d59d
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Petr Lautrbach d3d59d
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Petr Lautrbach d3d59d
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Petr Lautrbach d3d59d
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Petr Lautrbach d3d59d
+ *
Petr Lautrbach d3d59d
+ * Red Hat author: Jan F. Chadima <jchadima@redhat.com>
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+#include <sys/types.h>
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_unsupported(int n)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+audit_kex(int ctos, char *enc, char *mac, char *comp, char *pfs)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free(int ctos)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+}
Petr Lautrbach 5160c9
diff --git a/auth-rsa.c b/auth-rsa.c
Petr Lautrbach 190035
index ff7a132..1e12515 100644
Petr Lautrbach 5160c9
--- a/auth-rsa.c
Petr Lautrbach 5160c9
+++ b/auth-rsa.c
Petr Lautrbach 5160c9
@@ -93,7 +93,10 @@ auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	u_char buf[32], mdbuf[16];
Petr Lautrbach 5160c9
 	struct ssh_digest_ctx *md;
Petr Lautrbach d3d59d
-	int len;
Petr Lautrbach d3d59d
+	int len, rv;
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	char *fp;
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* don't allow short keys */
Petr Lautrbach d3d59d
 	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
Petr Lautrbach 5160c9
@@ -117,12 +120,18 @@ auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
Petr Lautrbach 5160c9
 	ssh_digest_free(md);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Verify that the response is the original challenge. */
Petr Lautrbach d3d59d
-	if (timingsafe_bcmp(response, mdbuf, 16) != 0) {
Petr Lautrbach d3d59d
-		/* Wrong answer. */
Petr Lautrbach d3d59d
-		return (0);
Petr Lautrbach d3d59d
+	rv = timingsafe_bcmp(response, mdbuf, 16) == 0;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 190035
+	fp = key_fingerprint(key, options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+	if (audit_keyusage(1, "ssh-rsa1", RSA_size(key->rsa) * 8, fp, rv) == 0) {
Petr Lautrbach d3d59d
+		debug("unsuccessful audit");
Petr Lautrbach d3d59d
+		rv = 0;
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
-	/* Correct answer. */
Petr Lautrbach d3d59d
-	return (1);
Petr Lautrbach 84822b
+	free(fp);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return rv;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /*
Petr Lautrbach 414bfa
diff --git a/auth.c b/auth.c
Petr Lautrbach 190035
index 5a9acd3..7eba5d4 100644
Petr Lautrbach 414bfa
--- a/auth.c
Petr Lautrbach 414bfa
+++ b/auth.c
Petr Lautrbach 190035
@@ -642,9 +642,6 @@ getpwnamallow(const char *user)
Petr Lautrbach 414bfa
 		record_failed_login(user,
Petr Lautrbach 414bfa
 		    get_canonical_hostname(options.use_dns), "ssh");
Petr Lautrbach 414bfa
 #endif
Petr Lautrbach 414bfa
-#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 414bfa
-		audit_event(SSH_INVALID_USER);
Petr Lautrbach 414bfa
-#endif /* SSH_AUDIT_EVENTS */
Petr Lautrbach 414bfa
 		return (NULL);
Petr Lautrbach 414bfa
 	}
Petr Lautrbach 414bfa
 	if (!allowed_user(pw))
Petr Lautrbach 5160c9
diff --git a/auth.h b/auth.h
Petr Lautrbach 190035
index 847cffd..19fbcf5 100644
Petr Lautrbach 5160c9
--- a/auth.h
Petr Lautrbach 5160c9
+++ b/auth.h
Petr Lautrbach 190035
@@ -187,6 +187,7 @@ void	abandon_challenge_response(Authctxt *);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 char	*expand_authorized_keys(const char *, struct passwd *pw);
Petr Lautrbach d3d59d
 char	*authorized_principals_file(struct passwd *);
Petr Lautrbach d3d59d
+int	 user_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 FILE	*auth_openkeyfile(const char *, struct passwd *, int);
Petr Lautrbach d3d59d
 FILE	*auth_openprincipals(const char *, struct passwd *, int);
Petr Lautrbach 190035
@@ -204,6 +205,7 @@ Key	*get_hostkey_private_by_type(int);
Petr Lautrbach d3d59d
 int	 get_hostkey_index(Key *);
Petr Lautrbach d3d59d
 int	 ssh1_session_key(BIGNUM *);
Petr Lautrbach 84822b
 void	 sshd_hostkey_sign(Key *, Key *, u_char **, u_int *, u_char *, u_int);
Petr Lautrbach d3d59d
+int	 hostbased_key_verify(const Key *, const u_char *, u_int, const u_char *, u_int);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* debug messages during authentication */
Petr Lautrbach d3d59d
 void	 auth_debug_add(const char *fmt,...) __attribute__((format(printf, 1, 2)));
Petr Lautrbach 5160c9
diff --git a/auth2-hostbased.c b/auth2-hostbased.c
Petr Lautrbach 190035
index 41f1a3f..80d9802 100644
Petr Lautrbach 5160c9
--- a/auth2-hostbased.c
Petr Lautrbach 5160c9
+++ b/auth2-hostbased.c
Petr Lautrbach 190035
@@ -138,7 +138,7 @@ userauth_hostbased(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	/* test for allowed key and correct signature */
Petr Lautrbach d3d59d
 	authenticated = 0;
Petr Lautrbach d3d59d
 	if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
Petr Lautrbach d3d59d
-	    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
+	    PRIVSEP(hostbased_key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
 			buffer_len(&b))) == 1)
Petr Lautrbach d3d59d
 		authenticated = 1;
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -155,6 +155,18 @@ done:
Petr Lautrbach d3d59d
 	return authenticated;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+hostbased_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int rv;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rv = key_verify(key, sig, slen, data, datalen);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	audit_key(0, &rv, key);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	return rv;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* return 1 if given hostkey is allowed */
Petr Lautrbach d3d59d
 int
Petr Lautrbach d3d59d
 hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
Petr Lautrbach 5160c9
diff --git a/auth2-pubkey.c b/auth2-pubkey.c
Petr Lautrbach 190035
index 269e642..110ec48 100644
Petr Lautrbach 5160c9
--- a/auth2-pubkey.c
Petr Lautrbach 5160c9
+++ b/auth2-pubkey.c
Petr Lautrbach 5160c9
@@ -160,7 +160,7 @@ userauth_pubkey(Authctxt *authctxt)
Petr Lautrbach d3d59d
 		/* test for correct signature */
Petr Lautrbach d3d59d
 		authenticated = 0;
Petr Lautrbach d3d59d
 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
Petr Lautrbach d3d59d
-		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
+		    PRIVSEP(user_key_verify(key, sig, slen, buffer_ptr(&b),
Petr Lautrbach d3d59d
 		    buffer_len(&b))) == 1)
Petr Lautrbach d3d59d
 			authenticated = 1;
Petr Lautrbach d3d59d
 		buffer_free(&b);
Petr Lautrbach 190035
@@ -232,6 +232,18 @@ pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Petr Lautrbach 84822b
 	free(extra);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+user_key_verify(const Key *key, const u_char *sig, u_int slen, const u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int rv;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	rv = key_verify(key, sig, slen, data, datalen);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	audit_key(1, &rv, key);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	return rv;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 static int
Petr Lautrbach 190035
 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 5160c9
diff --git a/auth2.c b/auth2.c
Petr Lautrbach 190035
index ec4ff8a..9e6e815 100644
Petr Lautrbach 5160c9
--- a/auth2.c
Petr Lautrbach 5160c9
+++ b/auth2.c
Petr Lautrbach 190035
@@ -250,9 +250,6 @@ input_userauth_request(int type, u_int32_t seq, void *ctxt)
Petr Lautrbach d3d59d
 		} else {
Petr Lautrbach d3d59d
 			logit("input_userauth_request: invalid user %s", user);
Petr Lautrbach d3d59d
 			authctxt->pw = fakepw();
Petr Lautrbach d3d59d
-#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
-			PRIVSEP(audit_event(SSH_INVALID_USER));
Petr Lautrbach d3d59d
-#endif
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
 #ifdef USE_PAM
Petr Lautrbach d3d59d
 		if (options.use_pam)
Petr Lautrbach 5160c9
diff --git a/cipher.c b/cipher.c
Petr Lautrbach 190035
index 638ca2d..9cc7cf8 100644
Petr Lautrbach 5160c9
--- a/cipher.c
Petr Lautrbach 5160c9
+++ b/cipher.c
Petr Lautrbach 190035
@@ -57,26 +57,6 @@ extern const EVP_CIPHER *evp_ssh1_3des(void);
Petr Lautrbach 190035
 extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
Petr Lautrbach 190035
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
-struct sshcipher {
Petr Lautrbach 9f526c
-	char	*name;
Petr Lautrbach 9f526c
-	int	number;		/* for ssh1 only */
Petr Lautrbach 9f526c
-	u_int	block_size;
Petr Lautrbach 9f526c
-	u_int	key_len;
Petr Lautrbach 9f526c
-	u_int	iv_len;		/* defaults to block_size */
Petr Lautrbach 9f526c
-	u_int	auth_len;
Petr Lautrbach 9f526c
-	u_int	discard_len;
Petr Lautrbach 9f526c
-	u_int	flags;
Petr Lautrbach 9f526c
-#define CFLAG_CBC		(1<<0)
Petr Lautrbach 9f526c
-#define CFLAG_CHACHAPOLY	(1<<1)
Petr Lautrbach 190035
-#define CFLAG_AESCTR		(1<<2)
Petr Lautrbach 190035
-#define CFLAG_NONE		(1<<3)
Petr Lautrbach 190035
-#ifdef WITH_OPENSSL
Petr Lautrbach 9f526c
-	const EVP_CIPHER	*(*evptype)(void);
Petr Lautrbach 190035
-#else
Petr Lautrbach 190035
-	void	*ignored;
Petr Lautrbach 190035
-#endif
Petr Lautrbach 9f526c
-};
Petr Lautrbach 9f526c
-
Petr Lautrbach 190035
 static const struct sshcipher ciphers[] = {
Petr Lautrbach 190035
 #ifdef WITH_SSH1
Petr Lautrbach 9f526c
 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
Petr Lautrbach 5160c9
diff --git a/cipher.h b/cipher.h
Petr Lautrbach 190035
index de74c1e..26ed4cb 100644
Petr Lautrbach 5160c9
--- a/cipher.h
Petr Lautrbach 5160c9
+++ b/cipher.h
Petr Lautrbach 190035
@@ -62,7 +62,26 @@
Petr Lautrbach 190035
 #define CIPHER_ENCRYPT		1
Petr Lautrbach 190035
 #define CIPHER_DECRYPT		0
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
-struct sshcipher;
Petr Lautrbach 190035
+struct sshcipher {
Petr Lautrbach d3d59d
+	char	*name;
Petr Lautrbach d3d59d
+	int	number;		/* for ssh1 only */
Petr Lautrbach d3d59d
+	u_int	block_size;
Petr Lautrbach d3d59d
+	u_int	key_len;
Petr Lautrbach d3d59d
+	u_int	iv_len;		/* defaults to block_size */
Petr Lautrbach d3d59d
+	u_int	auth_len;
Petr Lautrbach d3d59d
+	u_int	discard_len;
Petr Lautrbach 5160c9
+	u_int	flags;
Petr Lautrbach 5160c9
+#define CFLAG_CBC		(1<<0)
Petr Lautrbach 5160c9
+#define CFLAG_CHACHAPOLY	(1<<1)
Petr Lautrbach 190035
+#define CFLAG_AESCTR		(1<<2)
Petr Lautrbach 190035
+#define CFLAG_NONE		(1<<3)
Petr Lautrbach 190035
+#ifdef WITH_OPENSSL
Petr Lautrbach d3d59d
+	const EVP_CIPHER	*(*evptype)(void);
Petr Lautrbach 190035
+#else
Petr Lautrbach 190035
+	void	*ignored;
Petr Lautrbach 190035
+#endif
Petr Lautrbach d3d59d
+};
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
 struct sshcipher_ctx {
Petr Lautrbach d3d59d
 	int	plaintext;
Petr Lautrbach d3d59d
 	int	encrypt;
Petr Lautrbach 5160c9
diff --git a/kex.c b/kex.c
Petr Lautrbach 190035
index 4563920..e0cf3de 100644
Petr Lautrbach 5160c9
--- a/kex.c
Petr Lautrbach 5160c9
+++ b/kex.c
Petr Lautrbach 190035
@@ -52,6 +52,7 @@
Petr Lautrbach d3d59d
 #include "monitor.h"
Petr Lautrbach d3d59d
 #include "roaming.h"
Petr Lautrbach 5160c9
 #include "digest.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach 5160c9
 #ifdef GSSAPI
Petr Lautrbach 5160c9
 #include "ssh-gss.h"
Petr Lautrbach 190035
@@ -370,9 +371,13 @@ static void
Petr Lautrbach d3d59d
 choose_enc(Enc *enc, char *client, char *server)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+		audit_unsupported(0);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 		fatal("no matching cipher found: client %s server %s",
Petr Lautrbach d3d59d
 		    client, server);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if ((enc->cipher = cipher_by_name(name)) == NULL)
Petr Lautrbach d3d59d
 		fatal("matching cipher is not supported: %s", name);
Petr Lautrbach d3d59d
 	enc->name = name;
Petr Lautrbach 190035
@@ -388,9 +393,13 @@ static void
Petr Lautrbach d3d59d
 choose_mac(Mac *mac, char *client, char *server)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+		audit_unsupported(1);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 		fatal("no matching mac found: client %s server %s",
Petr Lautrbach d3d59d
 		    client, server);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if (mac_setup(mac, name) < 0)
Petr Lautrbach d3d59d
 		fatal("unsupported mac %s", name);
Petr Lautrbach d3d59d
 	/* truncate the key */
Petr Lautrbach 190035
@@ -405,8 +414,12 @@ static void
Petr Lautrbach d3d59d
 choose_comp(Comp *comp, char *client, char *server)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	char *name = match_list(client, server, NULL);
Petr Lautrbach d3d59d
-	if (name == NULL)
Petr Lautrbach d3d59d
+	if (name == NULL) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+		audit_unsupported(2);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 		fatal("no matching comp found: client %s server %s", client, server);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	if (strcmp(name, "zlib@openssh.com") == 0) {
Petr Lautrbach d3d59d
 		comp->type = COMP_DELAYED;
Petr Lautrbach d3d59d
 	} else if (strcmp(name, "zlib") == 0) {
Petr Lautrbach 190035
@@ -522,9 +535,11 @@ kex_choose_conf(Kex *kex)
Petr Lautrbach d3d59d
 		    authlen == 0 ? newkeys->mac.name : "<implicit>",
Petr Lautrbach d3d59d
 		    newkeys->comp.name);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach 190035
+
Petr Lautrbach d3d59d
 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
Petr Lautrbach d3d59d
 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
Petr Lautrbach 190035
 	    sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
Petr Lautrbach 190035
+
Petr Lautrbach 190035
 	need = dh_need = 0;
Petr Lautrbach 190035
 	for (mode = 0; mode < MODE_MAX; mode++) {
Petr Lautrbach 190035
 		newkeys = kex->newkeys[mode];
Petr Lautrbach 190035
@@ -536,11 +551,16 @@ kex_choose_conf(Kex *kex)
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->enc.block_size);
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->enc.iv_len);
Petr Lautrbach 190035
 		dh_need = MAX(dh_need, newkeys->mac.key_len);
Petr Lautrbach 190035
+		debug("kex: %s need=%d dh_need=%d", kex->name, need, dh_need);
Petr Lautrbach 190035
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 190035
+		audit_kex(mode, newkeys->enc.name, newkeys->mac.name, newkeys->comp.name, kex->name);
Petr Lautrbach 190035
+#endif
Petr Lautrbach 190035
 	}
Petr Lautrbach 190035
 	/* XXX need runden? */
Petr Lautrbach 190035
 	kex->we_need = need;
Petr Lautrbach 190035
 	kex->dh_need = dh_need;
Petr Lautrbach 190035
 
Petr Lautrbach 190035
+
Petr Lautrbach 190035
 	/* ignore the next message if the proposals do not match */
Petr Lautrbach 190035
 	if (first_kex_follows && !proposals_match(my, peer) &&
Petr Lautrbach 190035
 	    !(datafellows & SSH_BUG_FIRSTKEX)) {
Petr Lautrbach 190035
@@ -710,3 +730,34 @@ dump_digest(char *msg, u_char *digest, int len)
Petr Lautrbach d3d59d
 	fprintf(stderr, "\n");
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+enc_destroy(Enc *enc)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (enc == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (enc->key) {
Petr Lautrbach d3d59d
+		memset(enc->key, 0, enc->key_len);
Petr Lautrbach 84822b
+		free(enc->key);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (enc->iv) {
Petr Lautrbach d3d59d
+		memset(enc->iv,  0, enc->block_size);
Petr Lautrbach 84822b
+		free(enc->iv);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	memset(enc, 0, sizeof(*enc));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+newkeys_destroy(Newkeys *newkeys)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (newkeys == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	enc_destroy(&newkeys->enc);
Petr Lautrbach d3d59d
+	mac_destroy(&newkeys->mac);
Petr Lautrbach d3d59d
+	memset(&newkeys->comp, 0, sizeof(newkeys->comp));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach 5160c9
diff --git a/kex.h b/kex.h
Petr Lautrbach 190035
index 1c76c08..e015d27 100644
Petr Lautrbach 5160c9
--- a/kex.h
Petr Lautrbach 5160c9
+++ b/kex.h
Petr Lautrbach 5160c9
@@ -182,6 +182,8 @@ void	 kexgss_client(Kex *);
Petr Lautrbach 5160c9
 void	 kexgss_server(Kex *);
Petr Lautrbach 5160c9
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void	newkeys_destroy(Newkeys *newkeys);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 kex_dh_hash(char *, char *, char *, int, char *, int, u_char *, int,
Petr Lautrbach d3d59d
     BIGNUM *, BIGNUM *, BIGNUM *, u_char **, u_int *);
Petr Lautrbach 5160c9
diff --git a/key.h b/key.h
Petr Lautrbach 190035
index e1a3625..4a90e1e 100644
Petr Lautrbach 5160c9
--- a/key.h
Petr Lautrbach 5160c9
+++ b/key.h
Petr Lautrbach 190035
@@ -52,6 +52,7 @@ typedef struct sshkey Key;
Petr Lautrbach 190035
 #define key_ecdsa_key_to_nid	sshkey_ecdsa_key_to_nid
Petr Lautrbach 190035
 #define key_names_valid2	sshkey_names_valid2
Petr Lautrbach 190035
 #define key_is_cert		sshkey_is_cert
Petr Lautrbach 190035
+#define key_is_private		sshkey_is_private
Petr Lautrbach 190035
 #define key_type_plain		sshkey_type_plain
Petr Lautrbach 190035
 #define key_cert_is_legacy	sshkey_cert_is_legacy
Petr Lautrbach 190035
 #define key_curve_name_to_nid	sshkey_curve_name_to_nid
Petr Lautrbach 5160c9
diff --git a/mac.c b/mac.c
Petr Lautrbach 190035
index 402dc98..fd07bf2 100644
Petr Lautrbach 5160c9
--- a/mac.c
Petr Lautrbach 5160c9
+++ b/mac.c
Petr Lautrbach 190035
@@ -223,6 +223,20 @@ mac_clear(Mac *mac)
Petr Lautrbach d3d59d
 	mac->umac_ctx = NULL;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mac_destroy(Mac *mac)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (mac == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	if (mac->key) {
Petr Lautrbach d3d59d
+		memset(mac->key, 0, mac->key_len);
Petr Lautrbach 84822b
+		free(mac->key);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	memset(mac, 0, sizeof(*mac));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* XXX copied from ciphers_valid */
Petr Lautrbach d3d59d
 #define	MAC_SEP	","
Petr Lautrbach d3d59d
 int
Petr Lautrbach 5160c9
diff --git a/mac.h b/mac.h
Petr Lautrbach 5160c9
index fbe18c4..7dc7f43 100644
Petr Lautrbach 5160c9
--- a/mac.h
Petr Lautrbach 5160c9
+++ b/mac.h
Petr Lautrbach 84822b
@@ -29,3 +29,4 @@ int	 mac_setup(Mac *, char *);
Petr Lautrbach d3d59d
 int	 mac_init(Mac *);
Petr Lautrbach d3d59d
 u_char	*mac_compute(Mac *, u_int32_t, u_char *, int);
Petr Lautrbach d3d59d
 void	 mac_clear(Mac *);
Petr Lautrbach d3d59d
+void	 mac_destroy(Mac *);
Petr Lautrbach 5160c9
diff --git a/monitor.c b/monitor.c
Petr Lautrbach 190035
index d97e640..07fa655 100644
Petr Lautrbach 5160c9
--- a/monitor.c
Petr Lautrbach 5160c9
+++ b/monitor.c
Petr Lautrbach 190035
@@ -100,6 +100,7 @@
Petr Lautrbach 5160c9
 #include "ssh2.h"
Petr Lautrbach d3d59d
 #include "roaming.h"
Petr Lautrbach 84822b
 #include "authfd.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #ifdef GSSAPI
Petr Lautrbach d3d59d
 static Gssctxt *gsscontext = NULL;
Petr Lautrbach 190035
@@ -116,6 +117,8 @@ extern Buffer auth_debug;
Petr Lautrbach d3d59d
 extern int auth_debug_init;
Petr Lautrbach d3d59d
 extern Buffer loginmsg;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+extern void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* State exported from the child */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 struct {
Petr Lautrbach 190035
@@ -188,6 +191,11 @@ int mm_answer_gss_updatecreds(int, Buffer *);
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 int mm_answer_audit_event(int, Buffer *);
Petr Lautrbach d3d59d
 int mm_answer_audit_command(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_end_command(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_unsupported_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_kex_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_session_key_free_body(int, Buffer *);
Petr Lautrbach d3d59d
+int mm_answer_audit_server_key_free(int, Buffer *);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 static int monitor_read_log(struct monitor *);
Petr Lautrbach 190035
@@ -247,6 +255,10 @@ struct mon_table mon_dispatch_proto20[] = {
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef BSD_AUTH
Petr Lautrbach d3d59d
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Petr Lautrbach 190035
@@ -285,6 +297,11 @@ struct mon_table mon_dispatch_postauth20[] = {
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
     {0, 0, NULL}
Petr Lautrbach d3d59d
 };
Petr Lautrbach 190035
@@ -317,6 +334,10 @@ struct mon_table mon_dispatch_proto15[] = {
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach 190035
 #endif /* WITH_SSH1 */
Petr Lautrbach d3d59d
     {0, 0, NULL}
Petr Lautrbach 190035
@@ -330,6 +351,11 @@ struct mon_table mon_dispatch_postauth15[] = {
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
Petr Lautrbach d3d59d
     {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Petr Lautrbach d3d59d
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach 190035
 #endif /* WITH_SSH1 */
Petr Lautrbach d3d59d
     {0, 0, NULL}
Petr Lautrbach 190035
@@ -1416,9 +1442,11 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	Key *key;
Petr Lautrbach d3d59d
 	u_char *signature, *data, *blob;
Petr Lautrbach d3d59d
 	u_int signaturelen, datalen, bloblen;
Petr Lautrbach d3d59d
+	int type = 0;
Petr Lautrbach d3d59d
 	int verified = 0;
Petr Lautrbach d3d59d
 	int valid_data = 0;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	type = buffer_get_int(m);
Petr Lautrbach d3d59d
 	blob = buffer_get_string(m, &bloblen);
Petr Lautrbach d3d59d
 	signature = buffer_get_string(m, &signaturelen);
Petr Lautrbach d3d59d
 	data = buffer_get_string(m, &datalen);
Petr Lautrbach 190035
@@ -1426,6 +1454,8 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	if (hostbased_cuser == NULL || hostbased_chost == NULL ||
Petr Lautrbach d3d59d
 	  !monitor_allowed_key(blob, bloblen))
Petr Lautrbach d3d59d
 		fatal("%s: bad key, not previously allowed", __func__);
Petr Lautrbach d3d59d
+	if (type != key_blobtype)
Petr Lautrbach d3d59d
+		fatal("%s: bad key type", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	key = key_from_blob(blob, bloblen);
Petr Lautrbach d3d59d
 	if (key == NULL)
Petr Lautrbach 190035
@@ -1446,7 +1476,17 @@ mm_answer_keyverify(int sock, Buffer *m)
Petr Lautrbach d3d59d
 	if (!valid_data)
Petr Lautrbach d3d59d
 		fatal("%s: bad signature data blob", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-	verified = key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+	switch (key_blobtype) {
Petr Lautrbach d3d59d
+	case MM_USERKEY:
Petr Lautrbach d3d59d
+		verified = user_key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	case MM_HOSTKEY:
Petr Lautrbach d3d59d
+		verified = hostbased_key_verify(key, signature, signaturelen, data, datalen);
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	default:
Petr Lautrbach d3d59d
+		verified = 0;
Petr Lautrbach d3d59d
+		break;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 	debug3("%s: key %p signature %s",
Petr Lautrbach d3d59d
 	    __func__, key, (verified == 1) ? "verified" : "unverified");
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -1499,6 +1539,12 @@ mm_session_close(Session *s)
Petr Lautrbach d3d59d
 		debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
Petr Lautrbach d3d59d
 		session_pty_cleanup2(s);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command != NULL) {
Petr Lautrbach d3d59d
+		debug3("%s: command %d", __func__, s->command_handle);
Petr Lautrbach d3d59d
+		session_end_command2(s);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 	session_unused(s->self);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -1781,6 +1827,8 @@ mm_answer_term(int sock, Buffer *req)
Petr Lautrbach d3d59d
 		sshpam_cleanup();
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	destroy_sensitive_data(0);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Petr Lautrbach d3d59d
 		if (errno != EINTR)
Petr Lautrbach d3d59d
 			exit(1);
Petr Lautrbach 190035
@@ -1823,11 +1871,43 @@ mm_answer_audit_command(int socket, Buffer *m)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	u_int len;
Petr Lautrbach d3d59d
 	char *cmd;
Petr Lautrbach d3d59d
+	Session *s;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	debug3("%s entering", __func__);
Petr Lautrbach d3d59d
 	cmd = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	/* sanity check command, if so how? */
Petr Lautrbach d3d59d
-	audit_run_command(cmd);
Petr Lautrbach d3d59d
+	s = session_new();
Petr Lautrbach d3d59d
+	if (s == NULL)
Petr Lautrbach d3d59d
+		fatal("%s: error allocating a session", __func__);
Petr Lautrbach d3d59d
+	s->command = cmd;
Petr Lautrbach d3d59d
+	s->command_handle = audit_run_command(cmd);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+	buffer_put_int(m, s->self);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return (0);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_end_command(int socket, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int handle;
Petr Lautrbach d3d59d
+	u_int len;
Petr Lautrbach d3d59d
+	char *cmd;
Petr Lautrbach d3d59d
+	Session *s;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	debug3("%s entering", __func__);
Petr Lautrbach d3d59d
+	handle = buffer_get_int(m);
Petr Lautrbach d3d59d
+	cmd = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	s = session_by_id(handle);
Petr Lautrbach d3d59d
+	if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
Petr Lautrbach d3d59d
+	    strcmp(s->command, cmd) != 0)
Petr Lautrbach d3d59d
+		fatal("%s: invalid handle", __func__);
Petr Lautrbach d3d59d
+	mm_session_close(s);
Petr Lautrbach 84822b
 	free(cmd);
Petr Lautrbach d3d59d
 	return (0);
Petr Lautrbach d3d59d
 }
Petr Lautrbach 190035
@@ -1975,11 +2055,13 @@ mm_get_keystate(struct monitor *pmonitor)
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	blob = buffer_get_string(&m, &bloblen);
Petr Lautrbach d3d59d
 	current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen);
Petr Lautrbach d3d59d
+	memset(blob, 0, bloblen);
Petr Lautrbach 84822b
 	free(blob);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	debug3("%s: Waiting for second key", __func__);
Petr Lautrbach d3d59d
 	blob = buffer_get_string(&m, &bloblen);
Petr Lautrbach d3d59d
 	current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen);
Petr Lautrbach d3d59d
+	memset(blob, 0, bloblen);
Petr Lautrbach 84822b
 	free(blob);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Now get sequence numbers for the packets */
Petr Lautrbach 190035
@@ -2025,6 +2107,21 @@ mm_get_keystate(struct monitor *pmonitor)
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	buffer_free(&m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (compat20) {
Petr Lautrbach d3d59d
+		buffer_init(&m);
Petr Lautrbach d3d59d
+		mm_request_receive_expect(pmonitor->m_sendfd,
Petr Lautrbach d3d59d
+					  MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Petr Lautrbach d3d59d
+		mm_answer_audit_session_key_free_body(pmonitor->m_sendfd, &m);
Petr Lautrbach d3d59d
+		buffer_free(&m);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* Drain any buffered messages from the child */
Petr Lautrbach d3d59d
+	while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
Petr Lautrbach d3d59d
+		;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -2321,3 +2418,87 @@ mm_answer_gss_updatecreds(int socket, Buffer *m) {
Petr Lautrbach 5160c9
 
Petr Lautrbach 5160c9
 #endif /* GSSAPI */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_unsupported_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int what;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	what = buffer_get_int(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_unsupported_body(what);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_kex_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int ctos, len;
Petr Lautrbach 190035
+	char *cipher, *mac, *compress, *pfs;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	ctos = buffer_get_int(m);
Petr Lautrbach d3d59d
+	cipher = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	mac = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	compress = buffer_get_string(m, &len;;
Petr Lautrbach 190035
+	pfs = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach 190035
+	audit_kex_body(ctos, cipher, mac, compress, pfs, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(cipher);
Petr Lautrbach 84822b
+	free(mac);
Petr Lautrbach 84822b
+	free(compress);
Petr Lautrbach 190035
+	free(pfs);
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_session_key_free_body(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int ctos;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	ctos = buffer_get_int(m);
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_session_key_free_body(ctos, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_answer_audit_server_key_free(int sock, Buffer *m)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	int len;
Petr Lautrbach d3d59d
+	char *fp;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	fp = buffer_get_string(m, &len;;
Petr Lautrbach d3d59d
+	pid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+	uid = buffer_get_int64(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	audit_destroy_sensitive_data(fp, pid, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(fp);
Petr Lautrbach d3d59d
+	buffer_clear(m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SERVER_KEY_FREE, m);
Petr Lautrbach d3d59d
+	return 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif /* SSH_AUDIT_EVENTS */
Petr Lautrbach 5160c9
diff --git a/monitor.h b/monitor.h
Petr Lautrbach 190035
index 00c2028..cc8da6a 100644
Petr Lautrbach 5160c9
--- a/monitor.h
Petr Lautrbach 5160c9
+++ b/monitor.h
Petr Lautrbach 5160c9
@@ -69,7 +69,13 @@ enum monitor_reqtype {
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_QUERY = 106, MONITOR_ANS_PAM_QUERY = 107,
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_RESPOND = 108, MONITOR_ANS_PAM_RESPOND = 109,
Petr Lautrbach d3d59d
 	MONITOR_REQ_PAM_FREE_CTX = 110, MONITOR_ANS_PAM_FREE_CTX = 111,
Petr Lautrbach d3d59d
-	MONITOR_REQ_AUDIT_EVENT = 112, MONITOR_REQ_AUDIT_COMMAND = 113,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_EVENT = 112,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_COMMAND = 114, MONITOR_ANS_AUDIT_COMMAND = 115,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_END_COMMAND = 116,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_UNSUPPORTED = 118, MONITOR_ANS_AUDIT_UNSUPPORTED = 119,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_KEX = 120, MONITOR_ANS_AUDIT_KEX = 121,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_SESSION_KEY_FREE = 122, MONITOR_ANS_AUDIT_SESSION_KEY_FREE = 123,
Petr Lautrbach d3d59d
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE = 124, MONITOR_ANS_AUDIT_SERVER_KEY_FREE = 125
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 };
Petr Lautrbach d3d59d
 
Petr Lautrbach 5160c9
diff --git a/monitor_wrap.c b/monitor_wrap.c
Petr Lautrbach 190035
index 7e991e6..ba4ecd7 100644
Petr Lautrbach 5160c9
--- a/monitor_wrap.c
Petr Lautrbach 5160c9
+++ b/monitor_wrap.c
Petr Lautrbach 190035
@@ -456,7 +456,7 @@ mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key)
Petr Lautrbach d3d59d
  */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 int
Petr Lautrbach d3d59d
-mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+mm_key_verify(enum mm_keytype type, Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Buffer m;
Petr Lautrbach d3d59d
 	u_char *blob;
Petr Lautrbach 190035
@@ -470,6 +470,7 @@ mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
 		return (0);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, type);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, blob, len);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, sig, siglen);
Petr Lautrbach d3d59d
 	buffer_put_string(&m, data, datalen);
Petr Lautrbach 190035
@@ -487,6 +488,19 @@ mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
 	return (verified);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_hostbased_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return mm_key_verify(MM_HOSTKEY, key, sig, siglen, data, datalen);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
+mm_user_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return mm_key_verify(MM_USERKEY, key, sig, siglen, data, datalen);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* Export key state after authentication */
Petr Lautrbach d3d59d
 Newkeys *
Petr Lautrbach d3d59d
 mm_newkeys_from_blob(u_char *blob, int blen)
Petr Lautrbach 190035
@@ -665,12 +679,14 @@ mm_send_keystate(struct monitor *monitor)
Petr Lautrbach d3d59d
 		fatal("%s: conversion of newkeys failed", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	buffer_put_string(&m, blob, bloblen);
Petr Lautrbach d3d59d
+	memset(blob, 0, bloblen);
Petr Lautrbach 84822b
 	free(blob);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen))
Petr Lautrbach d3d59d
 		fatal("%s: conversion of newkeys failed", __func__);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	buffer_put_string(&m, blob, bloblen);
Petr Lautrbach d3d59d
+	memset(blob, 0, bloblen);
Petr Lautrbach 84822b
 	free(blob);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
Petr Lautrbach 190035
@@ -1218,10 +1234,11 @@ mm_audit_event(ssh_audit_event_t event)
Petr Lautrbach d3d59d
 	buffer_free(&m);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-void
Petr Lautrbach d3d59d
+int
Petr Lautrbach d3d59d
 mm_audit_run_command(const char *command)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Buffer m;
Petr Lautrbach d3d59d
+	int handle;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	debug3("%s entering command %s", __func__, command);
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -1229,6 +1246,26 @@ mm_audit_run_command(const char *command)
Petr Lautrbach d3d59d
 	buffer_put_cstring(&m, command);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_COMMAND, &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	handle = buffer_get_int(&m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	return (handle);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_end_command(int handle, const char *command)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	debug3("%s entering command %s", __func__, command);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, handle);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, command);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_END_COMMAND, &m);
Petr Lautrbach d3d59d
 	buffer_free(&m);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 #endif /* SSH_AUDIT_EVENTS */
Petr Lautrbach 190035
@@ -1364,3 +1401,72 @@ mm_ssh_gssapi_update_creds(ssh_gssapi_ccache *store)
Petr Lautrbach 5160c9
 
Petr Lautrbach 5160c9
 #endif /* GSSAPI */
Petr Lautrbach 5160c9
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_unsupported_body(int what)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, what);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_UNSUPPORTED, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_UNSUPPORTED,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach 190035
+mm_audit_kex_body(int ctos, char *cipher, char *mac, char *compress, char *fps, pid_t pid,
Petr Lautrbach d3d59d
+		  uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, ctos);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, cipher);
Petr Lautrbach 014fe8
+	buffer_put_cstring(&m, (mac ? mac : ""));
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, compress);
Petr Lautrbach 190035
+	buffer_put_cstring(&m, fps);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_KEX, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_KEX,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_session_key_free_body(int ctos, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_int(&m, ctos);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SESSION_KEY_FREE, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	Buffer m;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_init(&m);
Petr Lautrbach d3d59d
+	buffer_put_cstring(&m, fp);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, pid);
Petr Lautrbach d3d59d
+	buffer_put_int64(&m, uid);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Petr Lautrbach d3d59d
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
Petr Lautrbach d3d59d
+				  &m);
Petr Lautrbach d3d59d
+	buffer_free(&m);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif /* SSH_AUDIT_EVENTS */
Petr Lautrbach 5160c9
diff --git a/monitor_wrap.h b/monitor_wrap.h
Petr Lautrbach 190035
index 93929e0..e43109f 100644
Petr Lautrbach 5160c9
--- a/monitor_wrap.h
Petr Lautrbach 5160c9
+++ b/monitor_wrap.h
Petr Lautrbach 5160c9
@@ -52,7 +52,8 @@ int mm_key_allowed(enum mm_keytype, char *, char *, Key *);
Petr Lautrbach d3d59d
 int mm_user_key_allowed(struct passwd *, Key *);
Petr Lautrbach d3d59d
 int mm_hostbased_key_allowed(struct passwd *, char *, char *, Key *);
Petr Lautrbach d3d59d
 int mm_auth_rhosts_rsa_key_allowed(struct passwd *, char *, char *, Key *);
Petr Lautrbach d3d59d
-int mm_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
+int mm_hostbased_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
+int mm_user_key_verify(Key *, u_char *, u_int, u_char *, u_int);
Petr Lautrbach d3d59d
 int mm_auth_rsa_key_allowed(struct passwd *, BIGNUM *, Key **);
Petr Lautrbach d3d59d
 int mm_auth_rsa_verify_response(Key *, BIGNUM *, u_char *);
Petr Lautrbach d3d59d
 BIGNUM *mm_auth_rsa_generate_challenge(Key *);
Petr Lautrbach 5160c9
@@ -79,7 +80,12 @@ void mm_sshpam_free_ctx(void *);
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 #include "audit.h"
Petr Lautrbach d3d59d
 void mm_audit_event(ssh_audit_event_t);
Petr Lautrbach d3d59d
-void mm_audit_run_command(const char *);
Petr Lautrbach d3d59d
+int mm_audit_run_command(const char *);
Petr Lautrbach d3d59d
+void mm_audit_end_command(int, const char *);
Petr Lautrbach d3d59d
+void mm_audit_unsupported_body(int);
Petr Lautrbach 190035
+void mm_audit_kex_body(int, char *, char *, char *, char *, pid_t, uid_t);
Petr Lautrbach d3d59d
+void mm_audit_session_key_free_body(int, pid_t, uid_t);
Petr Lautrbach d3d59d
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 struct Session;
Petr Lautrbach 5160c9
diff --git a/packet.c b/packet.c
Petr Lautrbach 190035
index 9b7abd1..f1e00f7 100644
Petr Lautrbach 5160c9
--- a/packet.c
Petr Lautrbach 5160c9
+++ b/packet.c
Petr Lautrbach 84822b
@@ -61,6 +61,7 @@
Petr Lautrbach 84822b
 #include <time.h>
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #include "xmalloc.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 #include "buffer.h"
Petr Lautrbach d3d59d
 #include "packet.h"
Petr Lautrbach d3d59d
 #include "crc32.h"
Petr Lautrbach 190035
@@ -483,6 +484,13 @@ packet_get_connection_out(void)
Petr Lautrbach d3d59d
 	return active_state->connection_out;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static int
Petr Lautrbach d3d59d
+packet_state_has_keys (const struct session_state *state)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return state != NULL &&
Petr Lautrbach d3d59d
+		(state->newkeys[MODE_IN] != NULL || state->newkeys[MODE_OUT] != NULL);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /* Closes the connection and clears and frees internal data structures. */
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void
Petr Lautrbach 190035
@@ -491,13 +499,6 @@ packet_close(void)
Petr Lautrbach d3d59d
 	if (!active_state->initialized)
Petr Lautrbach d3d59d
 		return;
Petr Lautrbach d3d59d
 	active_state->initialized = 0;
Petr Lautrbach d3d59d
-	if (active_state->connection_in == active_state->connection_out) {
Petr Lautrbach d3d59d
-		shutdown(active_state->connection_out, SHUT_RDWR);
Petr Lautrbach d3d59d
-		close(active_state->connection_out);
Petr Lautrbach d3d59d
-	} else {
Petr Lautrbach d3d59d
-		close(active_state->connection_in);
Petr Lautrbach d3d59d
-		close(active_state->connection_out);
Petr Lautrbach d3d59d
-	}
Petr Lautrbach d3d59d
 	buffer_free(&active_state->input);
Petr Lautrbach d3d59d
 	buffer_free(&active_state->output);
Petr Lautrbach d3d59d
 	buffer_free(&active_state->outgoing_packet);
Petr Lautrbach 190035
@@ -506,8 +507,18 @@ packet_close(void)
Petr Lautrbach d3d59d
 		buffer_free(&active_state->compression_buffer);
Petr Lautrbach d3d59d
 		buffer_compress_uninit();
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
-	cipher_cleanup(&active_state->send_context);
Petr Lautrbach d3d59d
-	cipher_cleanup(&active_state->receive_context);
Petr Lautrbach d3d59d
+	if (packet_state_has_keys(active_state)) {
Petr Lautrbach d3d59d
+		cipher_cleanup(&active_state->send_context);
Petr Lautrbach d3d59d
+		cipher_cleanup(&active_state->receive_context);
Petr Lautrbach d3d59d
+		audit_session_key_free(2);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	if (active_state->connection_in == active_state->connection_out) {
Petr Lautrbach d3d59d
+		shutdown(active_state->connection_out, SHUT_RDWR);
Petr Lautrbach d3d59d
+		close(active_state->connection_out);
Petr Lautrbach d3d59d
+	} else {
Petr Lautrbach d3d59d
+		close(active_state->connection_in);
Petr Lautrbach d3d59d
+		close(active_state->connection_out);
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* Sets remote side protocol flags. */
Petr Lautrbach 190035
@@ -747,6 +758,25 @@ packet_send1(void)
Petr Lautrbach d3d59d
 	 */
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+newkeys_destroy_and_free(Newkeys *newkeys)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (newkeys == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(newkeys->enc.name);
Petr Lautrbach d3d59d
+
Petr Lautrbach 014fe8
+	if (newkeys->mac.enabled) {
Petr Lautrbach 014fe8
+		mac_clear(&newkeys->mac);
Petr Lautrbach 84822b
+		free(newkeys->mac.name);
Petr Lautrbach 014fe8
+	}
Petr Lautrbach d3d59d
+
Petr Lautrbach 84822b
+	free(newkeys->comp.name);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	newkeys_destroy(newkeys);
Petr Lautrbach 84822b
+	free(newkeys);
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 set_newkeys(int mode)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 190035
@@ -772,6 +802,7 @@ set_newkeys(int mode)
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 	if (active_state->newkeys[mode] != NULL) {
Petr Lautrbach d3d59d
 		debug("set_newkeys: rekeying");
Petr Lautrbach d3d59d
+		audit_session_key_free(mode);
Petr Lautrbach d3d59d
 		cipher_cleanup(cc);
Petr Lautrbach 5160c9
 		enc  = &active_state->newkeys[mode]->enc;
Petr Lautrbach 5160c9
 		mac  = &active_state->newkeys[mode]->mac;
Jakub Jelen 021326
@@ -2025,6 +2056,48 @@ packet_get_newkeys(int mode)
Petr Lautrbach d3d59d
 	return (void *)active_state->newkeys[mode];
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+packet_destroy_state(struct session_state *state)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (state == NULL)
Petr Lautrbach d3d59d
+		return;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	cipher_cleanup(&state->receive_context);
Petr Lautrbach d3d59d
+	cipher_cleanup(&state->send_context);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	buffer_free(&state->input);
Petr Lautrbach d3d59d
+	buffer_free(&state->output);
Petr Lautrbach d3d59d
+	buffer_free(&state->outgoing_packet);
Petr Lautrbach d3d59d
+	buffer_free(&state->incoming_packet);
Jakub Jelen 021326
+	if( state->compression_buffer_ready )
Jakub Jelen 021326
+		buffer_free(&state->compression_buffer);
Petr Lautrbach d3d59d
+	newkeys_destroy_and_free(state->newkeys[MODE_IN]);
Petr Lautrbach d3d59d
+	state->newkeys[MODE_IN] = NULL;
Petr Lautrbach d3d59d
+	newkeys_destroy_and_free(state->newkeys[MODE_OUT]);
Petr Lautrbach d3d59d
+	state->newkeys[MODE_OUT] = NULL;
Petr Lautrbach d3d59d
+	mac_destroy(state->packet_discard_mac);
Petr Lautrbach d3d59d
+//	TAILQ_HEAD(, packet) outgoing;
Petr Lautrbach d3d59d
+//	memset(state, 0, sizeof(state));
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+packet_destroy_all(int audit_it, int privsep)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (audit_it)
Petr Lautrbach d3d59d
+		audit_it = packet_state_has_keys (active_state) ||
Petr Lautrbach d3d59d
+			packet_state_has_keys (backup_state);
Petr Lautrbach d3d59d
+	packet_destroy_state(active_state);
Petr Lautrbach d3d59d
+	packet_destroy_state(backup_state);
Petr Lautrbach d3d59d
+	if (audit_it) {
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+		if (privsep)
Petr Lautrbach d3d59d
+			audit_session_key_free(2);
Petr Lautrbach d3d59d
+		else
Petr Lautrbach d3d59d
+			audit_session_key_free_body(2, getpid(), getuid());
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 /*
Petr Lautrbach d3d59d
  * Save the state for the real connection, and use a separate state when
Petr Lautrbach d3d59d
  * resuming a suspended connection.
Petr Lautrbach 190035
@@ -2032,18 +2104,12 @@ packet_get_newkeys(int mode)
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 packet_backup_state(void)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
-	struct session_state *tmp;
Petr Lautrbach d3d59d
-
Petr Lautrbach d3d59d
 	close(active_state->connection_in);
Petr Lautrbach d3d59d
 	active_state->connection_in = -1;
Petr Lautrbach d3d59d
 	close(active_state->connection_out);
Petr Lautrbach d3d59d
 	active_state->connection_out = -1;
Petr Lautrbach d3d59d
-	if (backup_state)
Petr Lautrbach d3d59d
-		tmp = backup_state;
Petr Lautrbach d3d59d
-	else
Petr Lautrbach d3d59d
-		tmp = alloc_session_state();
Petr Lautrbach d3d59d
 	backup_state = active_state;
Petr Lautrbach d3d59d
-	active_state = tmp;
Petr Lautrbach d3d59d
+	active_state = alloc_session_state();
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /*
Petr Lautrbach 190035
@@ -2060,9 +2126,7 @@ packet_restore_state(void)
Petr Lautrbach d3d59d
 	backup_state = active_state;
Petr Lautrbach d3d59d
 	active_state = tmp;
Petr Lautrbach d3d59d
 	active_state->connection_in = backup_state->connection_in;
Petr Lautrbach d3d59d
-	backup_state->connection_in = -1;
Petr Lautrbach d3d59d
 	active_state->connection_out = backup_state->connection_out;
Petr Lautrbach d3d59d
-	backup_state->connection_out = -1;
Petr Lautrbach d3d59d
 	len = buffer_len(&backup_state->input);
Petr Lautrbach d3d59d
 	if (len > 0) {
Petr Lautrbach d3d59d
 		buf = buffer_ptr(&backup_state->input);
Petr Lautrbach 190035
@@ -2070,6 +2134,11 @@ packet_restore_state(void)
Petr Lautrbach d3d59d
 		buffer_clear(&backup_state->input);
Petr Lautrbach d3d59d
 		add_recv_bytes(len);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
+	backup_state->connection_in = -1;
Petr Lautrbach d3d59d
+	backup_state->connection_out = -1;
Petr Lautrbach d3d59d
+	packet_destroy_state(backup_state);
Petr Lautrbach 84822b
+	free(backup_state);
Petr Lautrbach d3d59d
+	backup_state = NULL;
Petr Lautrbach d3d59d
 }
Petr Lautrbach 190035
 
Petr Lautrbach 190035
 /* Reset after_authentication and reset compression in post-auth privsep */
Petr Lautrbach 5160c9
diff --git a/packet.h b/packet.h
Petr Lautrbach 190035
index e7b5fcb..45a6ce6 100644
Petr Lautrbach 5160c9
--- a/packet.h
Petr Lautrbach 5160c9
+++ b/packet.h
Petr Lautrbach 190035
@@ -125,4 +125,5 @@ void	 packet_set_postauth(void);
Petr Lautrbach d3d59d
 void	*packet_get_input(void);
Petr Lautrbach d3d59d
 void	*packet_get_output(void);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+void	 packet_destroy_all(int, int);
Petr Lautrbach d3d59d
 #endif				/* PACKET_H */
Petr Lautrbach 5160c9
diff --git a/session.c b/session.c
Petr Lautrbach 190035
index 40a681e..acd87d5 100644
Petr Lautrbach 5160c9
--- a/session.c
Petr Lautrbach 5160c9
+++ b/session.c
Petr Lautrbach 163064
@@ -138,7 +138,7 @@ extern int log_stderr;
Petr Lautrbach d3d59d
 extern int debug_flag;
Petr Lautrbach d3d59d
 extern u_int utmp_len;
Petr Lautrbach d3d59d
 extern int startup_pipe;
Petr Lautrbach d3d59d
-extern void destroy_sensitive_data(void);
Petr Lautrbach d3d59d
+extern void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
 extern Buffer loginmsg;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* original command from peer. */
Petr Lautrbach 190035
@@ -730,6 +730,14 @@ do_exec_pty(Session *s, const char *command)
Petr Lautrbach d3d59d
 	/* Parent.  Close the slave side of the pseudo tty. */
Petr Lautrbach d3d59d
 	close(ttyfd);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifndef HAVE_OSF_SIA
Petr Lautrbach d3d59d
+	/* do_login in the child did not affect state in this process,
Petr Lautrbach d3d59d
+	   compensate.  From an architectural standpoint, this is extremely
Petr Lautrbach d3d59d
+	   ugly. */
Petr Lautrbach d3d59d
+	if (!(options.use_login && command == NULL))
Petr Lautrbach d3d59d
+		audit_count_session_open();
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	/* Enter interactive session. */
Petr Lautrbach d3d59d
 	s->ptymaster = ptymaster;
Petr Lautrbach d3d59d
 	packet_set_interactive(1, 
Petr Lautrbach 190035
@@ -852,15 +860,19 @@ do_exec(Session *s, const char *command)
Petr Lautrbach 5160c9
 	    get_remote_port());
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command != NULL || s->command_handle != -1)
Petr Lautrbach d3d59d
+		fatal("do_exec: command already set");
Petr Lautrbach d3d59d
 	if (command != NULL)
Petr Lautrbach d3d59d
-		PRIVSEP(audit_run_command(command));
Petr Lautrbach d3d59d
+		s->command = xstrdup(command);
Petr Lautrbach d3d59d
 	else if (s->ttyfd == -1) {
Petr Lautrbach d3d59d
 		char *shell = s->pw->pw_shell;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 		if (shell[0] == '\0')	/* empty shell means /bin/sh */
Petr Lautrbach d3d59d
 			shell =_PATH_BSHELL;
Petr Lautrbach d3d59d
-		PRIVSEP(audit_run_command(shell));
Petr Lautrbach d3d59d
+		s->command = xstrdup(shell);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
+	if (s->command != NULL)
Petr Lautrbach d3d59d
+		s->command_handle = PRIVSEP(audit_run_command(s->command));
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 	if (s->ttyfd != -1)
Petr Lautrbach d3d59d
 		ret = do_exec_pty(s, command);
Petr Lautrbach 190035
@@ -1703,7 +1715,10 @@ do_child(Session *s, const char *command)
Petr Lautrbach d3d59d
 	int r = 0;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* remove hostkey from the child's memory */
Petr Lautrbach d3d59d
-	destroy_sensitive_data();
Petr Lautrbach d3d59d
+	destroy_sensitive_data(1);
Petr Lautrbach d3d59d
+	/* Don't audit this - both us and the parent would be talking to the
Petr Lautrbach d3d59d
+	   monitor over a single socket, with no synchronization. */
Petr Lautrbach d3d59d
+	packet_destroy_all(0, 1);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Force a password change */
Petr Lautrbach d3d59d
 	if (s->authctxt->force_pwchange) {
Petr Lautrbach 414bfa
@@ -1933,6 +1948,7 @@ session_unused(int id)
Petr Lautrbach d3d59d
 	sessions[id].ttyfd = -1;
Petr Lautrbach d3d59d
 	sessions[id].ptymaster = -1;
Petr Lautrbach d3d59d
 	sessions[id].x11_chanids = NULL;
Petr Lautrbach d3d59d
+	sessions[id].command_handle = -1;
Petr Lautrbach d3d59d
 	sessions[id].next_unused = sessions_first_unused;
Petr Lautrbach d3d59d
 	sessions_first_unused = id;
Petr Lautrbach d3d59d
 }
Petr Lautrbach 414bfa
@@ -2015,6 +2031,19 @@ session_open(Authctxt *authctxt, int chanid)
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 Session *
Petr Lautrbach d3d59d
+session_by_id(int id)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (id >= 0 && id < sessions_nalloc) {
Petr Lautrbach d3d59d
+		Session *s = &sessions[id];
Petr Lautrbach d3d59d
+		if (s->used)
Petr Lautrbach d3d59d
+			return s;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+	debug("session_by_id: unknown id %d", id);
Petr Lautrbach d3d59d
+	session_dump();
Petr Lautrbach d3d59d
+	return NULL;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+Session *
Petr Lautrbach d3d59d
 session_by_tty(char *tty)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int i;
Petr Lautrbach 414bfa
@@ -2531,6 +2560,30 @@ session_exit_message(Session *s, int status)
Petr Lautrbach d3d59d
 		chan_write_failed(c);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+void
Petr Lautrbach d3d59d
+session_end_command2(Session *s)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (s->command != NULL) {
Petr Lautrbach d3d59d
+		audit_end_command(s->command_handle, s->command);
Petr Lautrbach 84822b
+		free(s->command);
Petr Lautrbach d3d59d
+		s->command = NULL;
Petr Lautrbach d3d59d
+		s->command_handle = -1;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+session_end_command(Session *s)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	if (s->command != NULL) {
Petr Lautrbach d3d59d
+		PRIVSEP(audit_end_command(s->command_handle, s->command));
Petr Lautrbach 84822b
+		free(s->command);
Petr Lautrbach d3d59d
+		s->command = NULL;
Petr Lautrbach d3d59d
+		s->command_handle = -1;
Petr Lautrbach d3d59d
+	}
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 session_close(Session *s)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 414bfa
@@ -2539,6 +2592,10 @@ session_close(Session *s)
Petr Lautrbach d3d59d
 	debug("session_close: session %d pid %ld", s->self, (long)s->pid);
Petr Lautrbach d3d59d
 	if (s->ttyfd != -1)
Petr Lautrbach d3d59d
 		session_pty_cleanup(s);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	if (s->command)
Petr Lautrbach d3d59d
+		session_end_command(s);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach 84822b
 	free(s->term);
Petr Lautrbach 84822b
 	free(s->display);
Petr Lautrbach 84822b
 	free(s->x11_chanids);
Petr Lautrbach 414bfa
@@ -2753,6 +2810,15 @@ do_authenticated2(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	server_loop2(authctxt);
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+static void
Petr Lautrbach d3d59d
+do_cleanup_one_session(Session *s)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	session_pty_cleanup2(s);
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	session_end_command2(s);
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 do_cleanup(Authctxt *authctxt)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 414bfa
@@ -2801,5 +2867,5 @@ do_cleanup(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	 * or if running in monitor.
Petr Lautrbach d3d59d
 	 */
Petr Lautrbach d3d59d
 	if (!use_privsep || mm_is_monitor())
Petr Lautrbach d3d59d
-		session_destroy_all(session_pty_cleanup2);
Petr Lautrbach d3d59d
+		session_destroy_all(do_cleanup_one_session);
Petr Lautrbach d3d59d
 }
Petr Lautrbach 5160c9
diff --git a/session.h b/session.h
Petr Lautrbach 5160c9
index 6a2f35e..e9b312e 100644
Petr Lautrbach 5160c9
--- a/session.h
Petr Lautrbach 5160c9
+++ b/session.h
Petr Lautrbach 5160c9
@@ -61,6 +61,12 @@ struct Session {
Petr Lautrbach d3d59d
 		char	*name;
Petr Lautrbach d3d59d
 		char	*val;
Petr Lautrbach d3d59d
 	} *env;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* exec */
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
+	int	command_handle;
Petr Lautrbach d3d59d
+	char	*command;
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
 };
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 void	 do_authenticated(Authctxt *);
Petr Lautrbach 5160c9
@@ -73,8 +79,10 @@ void	 session_close_by_pid(pid_t, int);
Petr Lautrbach d3d59d
 void	 session_close_by_channel(int, void *);
Petr Lautrbach d3d59d
 void	 session_destroy_all(void (*)(Session *));
Petr Lautrbach d3d59d
 void	 session_pty_cleanup2(Session *);
Petr Lautrbach d3d59d
+void	 session_end_command2(Session *);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 Session	*session_new(void);
Petr Lautrbach d3d59d
+Session *session_by_id(int);
Petr Lautrbach d3d59d
 Session	*session_by_tty(char *);
Petr Lautrbach d3d59d
 void	 session_close(Session *);
Petr Lautrbach d3d59d
 void	 do_setusercontext(struct passwd *);
Petr Lautrbach 5160c9
diff --git a/sshd.c b/sshd.c
Petr Lautrbach 190035
index ca55d7f..db23ce2 100644
Petr Lautrbach 5160c9
--- a/sshd.c
Petr Lautrbach 5160c9
+++ b/sshd.c
Petr Lautrbach 190035
@@ -120,6 +120,7 @@
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 #include "monitor_wrap.h"
Petr Lautrbach d3d59d
 #include "roaming.h"
Petr Lautrbach d3d59d
+#include "audit.h"
Petr Lautrbach d3d59d
 #include "ssh-sandbox.h"
Petr Lautrbach d3d59d
 #include "version.h"
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -254,7 +255,7 @@ Buffer loginmsg;
Petr Lautrbach d3d59d
 struct passwd *privsep_pw = NULL;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 /* Prototypes for various functions defined later in this file. */
Petr Lautrbach d3d59d
-void destroy_sensitive_data(void);
Petr Lautrbach d3d59d
+void destroy_sensitive_data(int);
Petr Lautrbach d3d59d
 void demote_sensitive_data(void);
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
 #ifdef WITH_SSH1
Petr Lautrbach 190035
@@ -275,6 +276,15 @@ close_listen_socks(void)
Petr Lautrbach d3d59d
 	num_listen_socks = -1;
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Is this process listening for clients (i.e. not specific to any specific
Petr Lautrbach d3d59d
+ * client connection?)
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
+int listening_for_clients(void)
Petr Lautrbach d3d59d
+{
Petr Lautrbach d3d59d
+	return num_listen_socks > 0;
Petr Lautrbach d3d59d
+}
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 static void
Petr Lautrbach d3d59d
 close_startup_pipes(void)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 190035
@@ -554,22 +564,45 @@ sshd_exchange_identification(int sock_in, int sock_out)
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
-/* Destroy the host and server keys.  They will no longer be needed. */
Petr Lautrbach d3d59d
+/*
Petr Lautrbach d3d59d
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
Petr Lautrbach d3d59d
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
Petr Lautrbach d3d59d
+ */
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
-destroy_sensitive_data(void)
Petr Lautrbach d3d59d
+destroy_sensitive_data(int privsep)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	int i;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	if (sensitive_data.server_key) {
Petr Lautrbach d3d59d
 		key_free(sensitive_data.server_key);
Petr Lautrbach d3d59d
 		sensitive_data.server_key = NULL;
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
+	pid = getpid();
Petr Lautrbach d3d59d
+	uid = getuid();
Petr Lautrbach d3d59d
 	for (i = 0; i < options.num_host_key_files; i++) {
Petr Lautrbach d3d59d
 		if (sensitive_data.host_keys[i]) {
Petr Lautrbach d3d59d
+			char *fp;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+			if (key_is_private(sensitive_data.host_keys[i]))
Petr Lautrbach 190035
+				fp = key_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+			else
Petr Lautrbach d3d59d
+				fp = NULL;
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_keys[i] = NULL;
Petr Lautrbach d3d59d
+			if (fp != NULL) {
Petr Lautrbach d3d59d
+				if (privsep)
Petr Lautrbach d3d59d
+					PRIVSEP(audit_destroy_sensitive_data(fp,
Petr Lautrbach d3d59d
+						pid, uid));
Petr Lautrbach d3d59d
+				else
Petr Lautrbach d3d59d
+					audit_destroy_sensitive_data(fp,
Petr Lautrbach d3d59d
+						pid, uid);
Petr Lautrbach 84822b
+				free(fp);
Petr Lautrbach d3d59d
+			}
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
-		if (sensitive_data.host_certificates[i]) {
Petr Lautrbach d3d59d
+		if (sensitive_data.host_certificates
Petr Lautrbach d3d59d
+		    && sensitive_data.host_certificates[i]) {
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_certificates[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_certificates[i] = NULL;
Petr Lautrbach d3d59d
 		}
Petr Lautrbach 190035
@@ -583,6 +616,8 @@ void
Petr Lautrbach d3d59d
 demote_sensitive_data(void)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
 	Key *tmp;
Petr Lautrbach d3d59d
+	pid_t pid;
Petr Lautrbach d3d59d
+	uid_t uid;
Petr Lautrbach d3d59d
 	int i;
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	if (sensitive_data.server_key) {
Petr Lautrbach 190035
@@ -591,13 +626,25 @@ demote_sensitive_data(void)
Petr Lautrbach d3d59d
 		sensitive_data.server_key = tmp;
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+	pid = getpid();
Petr Lautrbach d3d59d
+	uid = getuid();
Petr Lautrbach d3d59d
 	for (i = 0; i < options.num_host_key_files; i++) {
Petr Lautrbach d3d59d
 		if (sensitive_data.host_keys[i]) {
Petr Lautrbach d3d59d
+			char *fp;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+			if (key_is_private(sensitive_data.host_keys[i]))
Petr Lautrbach 190035
+				fp = key_fingerprint(sensitive_data.host_keys[i], options.fingerprint_hash, SSH_FP_HEX);
Petr Lautrbach d3d59d
+			else
Petr Lautrbach d3d59d
+				fp = NULL;
Petr Lautrbach d3d59d
 			tmp = key_demote(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			key_free(sensitive_data.host_keys[i]);
Petr Lautrbach d3d59d
 			sensitive_data.host_keys[i] = tmp;
Petr Lautrbach d3d59d
 			if (tmp->type == KEY_RSA1)
Petr Lautrbach d3d59d
 				sensitive_data.ssh1_host_key = tmp;
Petr Lautrbach d3d59d
+			if (fp != NULL) {
Petr Lautrbach d3d59d
+				audit_destroy_sensitive_data(fp, pid, uid);
Petr Lautrbach 84822b
+				free(fp);
Petr Lautrbach d3d59d
+			}
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
 		/* Certs do not need demotion */
Petr Lautrbach d3d59d
 	}
Petr Lautrbach 190035
@@ -667,7 +714,7 @@ privsep_preauth(Authctxt *authctxt)
Petr Lautrbach 82d2be
 
Petr Lautrbach 82d2be
 	if (use_privsep == PRIVSEP_ON)
Petr Lautrbach 5160c9
 		box = ssh_sandbox_init(pmonitor);
Petr Lautrbach 82d2be
-	pid = fork();
Petr Lautrbach 82d2be
+	pmonitor->m_pid = pid = fork();
Petr Lautrbach 82d2be
 	if (pid == -1) {
Petr Lautrbach 82d2be
 		fatal("fork of unprivileged child failed");
Petr Lautrbach 82d2be
 	} else if (pid != 0) {
Petr Lautrbach 190035
@@ -721,6 +768,8 @@ privsep_preauth(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 }
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+extern Newkeys *current_keys[];
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 static void
Petr Lautrbach d3d59d
 privsep_postauth(Authctxt *authctxt)
Petr Lautrbach d3d59d
 {
Petr Lautrbach 190035
@@ -745,6 +794,10 @@ privsep_postauth(Authctxt *authctxt)
Petr Lautrbach d3d59d
 	else if (pmonitor->m_pid != 0) {
Petr Lautrbach d3d59d
 		verbose("User child is on pid %ld", (long)pmonitor->m_pid);
Petr Lautrbach d3d59d
 		buffer_clear(&loginmsg);
Petr Lautrbach d3d59d
+ 		newkeys_destroy(current_keys[MODE_OUT]);
Petr Lautrbach d3d59d
+		newkeys_destroy(current_keys[MODE_IN]);
Petr Lautrbach d3d59d
+		audit_session_key_free_body(2, getpid(), getuid());
Petr Lautrbach d3d59d
+		packet_destroy_all(0, 0);
Petr Lautrbach d3d59d
 		monitor_child_postauth(pmonitor);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 		/* NEVERREACHED */
Petr Lautrbach 190035
@@ -1222,6 +1275,7 @@ server_accept_loop(int *sock_in, int *sock_out, int *newsock, int *config_s)
Petr Lautrbach d3d59d
 		if (received_sigterm) {
Petr Lautrbach d3d59d
 			logit("Received signal %d; terminating.",
Petr Lautrbach d3d59d
 			    (int) received_sigterm);
Petr Lautrbach d3d59d
+			destroy_sensitive_data(0);
Petr Lautrbach d3d59d
 			close_listen_socks();
Petr Lautrbach d3d59d
 			unlink(options.pid_file);
Petr Lautrbach d3d59d
 			exit(received_sigterm == SIGTERM ? 0 : 255);
Petr Lautrbach 190035
@@ -2141,6 +2195,7 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 	 */
Petr Lautrbach d3d59d
 	if (use_privsep) {
Petr Lautrbach d3d59d
 		mm_send_keystate(pmonitor);
Petr Lautrbach d3d59d
+		packet_destroy_all(1, 1);
Petr Lautrbach d3d59d
 		exit(0);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Petr Lautrbach 190035
@@ -2186,7 +2241,7 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 		privsep_postauth(authctxt);
Petr Lautrbach d3d59d
 		/* the monitor process [priv] will not return */
Petr Lautrbach d3d59d
 		if (!compat20)
Petr Lautrbach d3d59d
-			destroy_sensitive_data();
Petr Lautrbach d3d59d
+			destroy_sensitive_data(0);
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	packet_set_timeout(options.client_alive_interval,
Petr Lautrbach 190035
@@ -2196,6 +2251,9 @@ main(int ac, char **av)
Petr Lautrbach d3d59d
 	do_authenticated(authctxt);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* The connection has been terminated. */
Petr Lautrbach d3d59d
+	packet_destroy_all(1, 1);
Petr Lautrbach d3d59d
+	destroy_sensitive_data(1);
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
Petr Lautrbach d3d59d
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
Petr Lautrbach d3d59d
 	verbose("Transferred: sent %llu, received %llu bytes",
Petr Lautrbach 190035
@@ -2355,6 +2413,10 @@ do_ssh1_kex(void)
Petr Lautrbach d3d59d
 		if (cookie[i] != packet_get_char())
Petr Lautrbach d3d59d
 			packet_disconnect("IP Spoofing check bytes do not match.");
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
+#ifdef SSH_AUDIT_EVENTS
Petr Lautrbach 190035
+	audit_kex(2, cipher_name(cipher_type), "crc", "none", "none");
Petr Lautrbach d3d59d
+#endif
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	debug("Encryption type: %.200s", cipher_name(cipher_type));
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	/* Get the encrypted integer. */
Petr Lautrbach 190035
@@ -2427,7 +2489,7 @@ do_ssh1_kex(void)
Petr Lautrbach d3d59d
 			session_id[i] = session_key[i] ^ session_key[i + 16];
Petr Lautrbach d3d59d
 	}
Petr Lautrbach d3d59d
 	/* Destroy the private and public keys. No longer. */
Petr Lautrbach d3d59d
-	destroy_sensitive_data();
Petr Lautrbach d3d59d
+	destroy_sensitive_data(0);
Petr Lautrbach d3d59d
 
Petr Lautrbach d3d59d
 	if (use_privsep)
Petr Lautrbach d3d59d
 		mm_ssh1_session_id(session_id);
Petr Lautrbach 190035
@@ -2598,6 +2660,16 @@ do_ssh2_kex(void)
Petr Lautrbach d3d59d
 void
Petr Lautrbach d3d59d
 cleanup_exit(int i)
Petr Lautrbach d3d59d
 {
Petr Lautrbach d3d59d
+	static int in_cleanup = 0;
Petr Lautrbach d3d59d
+	int is_privsep_child;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
+	/* cleanup_exit can be called at the very least from the privsep
Petr Lautrbach d3d59d
+	   wrappers used for auditing.  Make sure we don't recurse
Petr Lautrbach d3d59d
+	   indefinitely. */
Petr Lautrbach d3d59d
+	if (in_cleanup)
Petr Lautrbach d3d59d
+		_exit(i);
Petr Lautrbach d3d59d
+	in_cleanup = 1;
Petr Lautrbach d3d59d
+
Petr Lautrbach d3d59d
 	if (the_authctxt) {
Petr Lautrbach d3d59d
 		do_cleanup(the_authctxt);
Petr Lautrbach 190035
 		if (use_privsep && privsep_is_preauth &&
Petr Lautrbach 190035
@@ -2609,9 +2681,14 @@ cleanup_exit(int i)
Petr Lautrbach d3d59d
 				    pmonitor->m_pid, strerror(errno));
Petr Lautrbach d3d59d
 		}
Petr Lautrbach d3d59d
 	}
Petr Lautrbach 82d2be
+	is_privsep_child = use_privsep && pmonitor != NULL && pmonitor->m_pid == 0;
Petr Lautrbach d3d59d
+	if (sensitive_data.host_keys != NULL)
Petr Lautrbach d3d59d
+		destroy_sensitive_data(is_privsep_child);
Petr Lautrbach d3d59d
+	packet_destroy_all(1, is_privsep_child);
Petr Lautrbach d3d59d
 #ifdef SSH_AUDIT_EVENTS
Petr Lautrbach d3d59d
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */
Petr Lautrbach d3d59d
-	if (!use_privsep || mm_is_monitor())
Petr Lautrbach d3d59d
+	if ((the_authctxt == NULL || !the_authctxt->authenticated) &&
Petr Lautrbach d3d59d
+	    (!use_privsep || mm_is_monitor()))
Petr Lautrbach d3d59d
 		audit_event(SSH_CONNECTION_ABANDON);
Petr Lautrbach d3d59d
 #endif
Petr Lautrbach d3d59d
 	_exit(i);
Petr Lautrbach 190035
diff --git a/sshkey.c b/sshkey.c
Petr Lautrbach 190035
index 70df758..f078e11 100644
Petr Lautrbach 190035
--- a/sshkey.c
Petr Lautrbach 190035
+++ b/sshkey.c
Petr Lautrbach 190035
@@ -291,6 +291,33 @@ sshkey_type_is_valid_ca(int type)
Petr Lautrbach 190035
 }
Petr Lautrbach 190035
 
Petr Lautrbach 190035
 int
Petr Lautrbach 190035
+sshkey_is_private(const struct sshkey *k)
Petr Lautrbach 190035
+{
Petr Lautrbach 190035
+      switch (k->type) {
Petr Lautrbach 190035
+      case KEY_RSA_CERT_V00:
Petr Lautrbach 190035
+      case KEY_RSA_CERT:
Petr Lautrbach 190035
+      case KEY_RSA1:
Petr Lautrbach 190035
+      case KEY_RSA:
Petr Lautrbach 190035
+              return k->rsa->d != NULL;
Petr Lautrbach 190035
+      case KEY_DSA_CERT_V00:
Petr Lautrbach 190035
+      case KEY_DSA_CERT:
Petr Lautrbach 190035
+      case KEY_DSA:
Petr Lautrbach 190035
+              return k->dsa->priv_key != NULL;
Petr Lautrbach 190035
+#ifdef OPENSSL_HAS_ECC
Petr Lautrbach 190035
+      case KEY_ECDSA_CERT:
Petr Lautrbach 190035
+      case KEY_ECDSA:
Petr Lautrbach 190035
+              return EC_KEY_get0_private_key(k->ecdsa) != NULL;
Petr Lautrbach 190035
+#endif
Petr Lautrbach 190035
+      case KEY_ED25519_CERT:
Petr Lautrbach 190035
+      case KEY_ED25519:
Petr Lautrbach 190035
+              return (k->ed25519_pk != NULL);
Petr Lautrbach 190035
+      default:
Petr Lautrbach 190035
+              /* fatal("key_is_private: bad key type %d", k->type); */
Petr Lautrbach 190035
+              return 0;
Petr Lautrbach 190035
+      }
Petr Lautrbach 190035
+}
Petr Lautrbach 190035
+
Petr Lautrbach 190035
+int
Petr Lautrbach 190035
 sshkey_is_cert(const struct sshkey *k)
Petr Lautrbach 190035
 {
Petr Lautrbach 190035
 	if (k == NULL)
Petr Lautrbach 190035
diff --git a/sshkey.h b/sshkey.h
Petr Lautrbach 190035
index 4554b09..226a494 100644
Petr Lautrbach 190035
--- a/sshkey.h
Petr Lautrbach 190035
+++ b/sshkey.h
Petr Lautrbach 190035
@@ -134,6 +134,7 @@ u_int		 sshkey_size(const struct sshkey *);
Petr Lautrbach 190035
 int		 sshkey_generate(int type, u_int bits, struct sshkey **keyp);
Petr Lautrbach 190035
 int		 sshkey_from_private(const struct sshkey *, struct sshkey **);
Petr Lautrbach 190035
 int	 sshkey_type_from_name(const char *);
Petr Lautrbach 190035
+int	 sshkey_is_private(const struct sshkey *);
Petr Lautrbach 190035
 int	 sshkey_is_cert(const struct sshkey *);
Petr Lautrbach 190035
 int	 sshkey_type_is_cert(int);
Petr Lautrbach 190035
 int	 sshkey_type_plain(int);
Jakub Jelen 0a4ac4
Jakub Jelen 0a4ac4
diff -U3 openssh-6.6p1/sandbox-seccomp-filter.c openssh-6.6p1.seccomp/sandbox-seccomp-filter.c
Jakub Jelen 0a4ac4
--- openssh-6.6p1/sandbox-seccomp-filter.c	2014-02-06 01:17:50.000000000 +0100
Jakub Jelen 0a4ac4
+++ openssh-6.6p1.seccomp/sandbox-seccomp-filter.c	2015-02-11 09:07:10.885000000 +0100
Jakub Jelen e3a625
@@ -95,6 +95,12 @@
Jakub Jelen 0a4ac4
 #ifdef __NR_time /* not defined on EABI ARM */
Jakub Jelen 0a4ac4
 	SC_ALLOW(time),
Jakub Jelen 0a4ac4
 #endif
Jakub Jelen 0a4ac4
+#ifdef SSH_AUDIT_EVENTS
Jakub Jelen 0a4ac4
+	SC_ALLOW(getuid),
Jakub Jelen e3a625
+#ifdef __NR_getuid32 /* not defined on x86_64 */
Jakub Jelen d5a800
+	SC_ALLOW(getuid32),
Jakub Jelen 0a4ac4
+#endif
Jakub Jelen e3a625
+#endif
Jakub Jelen 0a4ac4
 	SC_ALLOW(read),
Jakub Jelen 0a4ac4
 	SC_ALLOW(write),
Jakub Jelen 0a4ac4
 	SC_ALLOW(close),