vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Jan F 003cb0
diff -up openssh-5.8p1/audit-bsm.c.audit5 openssh-5.8p1/audit-bsm.c
Jan F a864d6
--- openssh-5.8p1/audit-bsm.c.audit5	2011-03-10 11:58:54.075979941 +0100
Jan F a864d6
+++ openssh-5.8p1/audit-bsm.c	2011-03-10 11:58:55.029855000 +0100
Jan F edc172
@@ -414,4 +414,22 @@ audit_session_key_free_body(int ctos, pi
Jan F 003cb0
 {
Jan F 003cb0
 	/* not implemented */
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+void
Jan F 2c1a4a
+audit_destroy_sensitive_data(const char *fp)
Jan F 003cb0
+{
Jan F 003cb0
+	/* not implemented */
Jan F 003cb0
+}
Jan F c2b009
+
Jan F c2b009
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F c2b009
+{
Jan F c2b009
+	/* not implemented */
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *fp)
Jan F c2b009
+{
Jan F c2b009
+	/* not implemented */
Jan F c2b009
+}
Jan F 003cb0
 #endif /* BSM */
Jan F 003cb0
diff -up openssh-5.8p1/audit.c.audit5 openssh-5.8p1/audit.c
Jan F a864d6
--- openssh-5.8p1/audit.c.audit5	2011-03-10 11:58:54.121980174 +0100
Jan F a864d6
+++ openssh-5.8p1/audit.c	2011-03-10 11:58:55.077854964 +0100
Jan F edc172
@@ -290,5 +290,24 @@ audit_session_key_free_body(int ctos, pi
Jan F 9cefae
 	debug("audit session key discard euid %u direction %d from pid %ld uid %u",
Jan F 9cefae
 		(unsigned)geteuid(), ctos, (long)pid, (unsigned)uid);
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+/*
Jan F 003cb0
+ * This will be called on destroy private part of the server key
Jan F 003cb0
+ */
Jan F 003cb0
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F 003cb0
+{
Jan F c2b009
+	debug("audit destroy sensitive data euid %d fingerprint %s from pid %ld uid %u",
Jan F c2b009
+		geteuid(), fp, (long)pid, (unsigned)uid);
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+/*
Jan F c2b009
+ * This will be called on generation of the ephemeral server key
Jan F c2b009
+ */
Jan F c2b009
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *)
Jan F c2b009
+{
Jan F c2b009
+	debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp);
Jan F 003cb0
+}
Jan F 003cb0
 # endif  /* !defined CUSTOM_SSH_AUDIT_EVENTS */
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/audit.h.audit5 openssh-5.8p1/audit.h
Jan F a864d6
--- openssh-5.8p1/audit.h.audit5	2011-03-10 11:58:54.167979998 +0100
Jan F a864d6
+++ openssh-5.8p1/audit.h	2011-03-10 11:58:55.126854627 +0100
Jan F c2b009
@@ -48,6 +48,8 @@ enum ssh_audit_event_type {
Jan F c2b009
 };
Jan F c2b009
 typedef enum ssh_audit_event_type ssh_audit_event_t;
Jan F c2b009
 
Jan F c2b009
+int	listening_for_clients(void);
Jan F c2b009
+
Jan F c2b009
 void	audit_connection_from(const char *, int);
Jan F c2b009
 void	audit_event(ssh_audit_event_t);
Jan F edc172
 void	audit_count_session_open(void);
Jan F edc172
@@ -64,5 +66,7 @@ void	audit_unsupported_body(int);
Jan F 9cefae
 void	audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F 003cb0
 void	audit_session_key_free(int ctos);
Jan F 9cefae
 void	audit_session_key_free_body(int ctos, pid_t, uid_t);
Jan F c2b009
+void	audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F c2b009
+void	audit_generate_ephemeral_server_key(const char *);
Jan F 003cb0
 
Jan F 003cb0
 #endif /* _SSH_AUDIT_H */
Jan F 003cb0
diff -up openssh-5.8p1/audit-linux.c.audit5 openssh-5.8p1/audit-linux.c
Jan F a864d6
--- openssh-5.8p1/audit-linux.c.audit5	2011-03-10 11:58:54.215855001 +0100
Jan F a864d6
+++ openssh-5.8p1/audit-linux.c	2011-03-10 11:58:55.170980918 +0100
Jan F edc172
@@ -350,4 +350,50 @@ audit_session_key_free_body(int ctos, pi
Jan F 1732b0
 		error("cannot write into audit");
Jan F 1732b0
 }
Jan F 1732b0
 
Jan F c2b009
+void
Jan F c2b009
+audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F c2b009
+{
Jan F c2b009
+	char buf[AUDIT_LOG_SIZE];
Jan F c2b009
+	int audit_fd, audit_ok;
Jan F c2b009
+
Jan F c2b009
+	snprintf(buf, sizeof(buf), "op=destroy kind=server fp=%s direction=? spid=%jd suid=%jd",
Jan F c2b009
+		fp, (intmax_t)pid, (intmax_t)uid);
Jan F c2b009
+	audit_fd = audit_open();
Jan F c2b009
+	if (audit_fd < 0) {
Jan F c2b009
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F c2b009
+					 errno != EAFNOSUPPORT)
Jan F c2b009
+			error("cannot open audit");
Jan F c2b009
+		return;
Jan F c2b009
+	}
Jan F c2b009
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F 1732b0
+			buf, NULL,
Jan F 1732b0
+			listening_for_clients() ? NULL : get_remote_ipaddr(),
Jan F 1732b0
+			NULL, 1);
Jan F 1732b0
+	audit_close(audit_fd);
Jan F 1732b0
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F 1732b0
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F 1732b0
+		error("cannot write into audit");
Jan F 1732b0
+}
Jan F 1732b0
+
Jan F 003cb0
+void
Jan F c2b009
+audit_generate_ephemeral_server_key(const char *fp)
Jan F 003cb0
+{
Jan F 003cb0
+	char buf[AUDIT_LOG_SIZE];
Jan F 003cb0
+	int audit_fd, audit_ok;
Jan F 003cb0
+
Jan F c2b009
+	snprintf(buf, sizeof(buf), "op=create kind=server fp=%s direction=?", fp);
Jan F 003cb0
+	audit_fd = audit_open();
Jan F 003cb0
+	if (audit_fd < 0) {
Jan F 003cb0
+		if (errno != EINVAL && errno != EPROTONOSUPPORT &&
Jan F 003cb0
+					 errno != EAFNOSUPPORT)
Jan F 003cb0
+			error("cannot open audit");
Jan F 003cb0
+		return;
Jan F 003cb0
+	}
Jan F 2c1a4a
+	audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_KEY_USER,
Jan F c2b009
+			buf, NULL, 0, NULL, 1);
Jan F 003cb0
+	audit_close(audit_fd);
Jan F 003cb0
+	/* do not abort if the error is EPERM and sshd is run as non root user */
Jan F 003cb0
+	if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0)))
Jan F 003cb0
+		error("cannot write into audit");
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* USE_LINUX_AUDIT */
Jan F c2b009
diff -up openssh-5.8p1/key.c.audit5 openssh-5.8p1/key.c
Jan F a864d6
--- openssh-5.8p1/key.c.audit5	2011-03-10 11:58:51.406979467 +0100
Jan F a864d6
+++ openssh-5.8p1/key.c	2011-03-10 11:58:55.229855219 +0100
Jan F 1499a2
@@ -1797,6 +1797,30 @@ key_demote(const Key *k)
Jan F c2b009
 }
Jan F c2b009
 
Jan F c2b009
 int
Jan F c2b009
+key_is_private(const Key *k)
Jan F c2b009
+{
Jan F c2b009
+	switch (k->type) {
Jan F c2b009
+	case KEY_RSA_CERT_V00:
Jan F c2b009
+	case KEY_RSA_CERT:
Jan F c2b009
+	case KEY_RSA1:
Jan F c2b009
+	case KEY_RSA:
Jan F c2b009
+		return k->rsa->d != NULL;
Jan F c2b009
+	case KEY_DSA_CERT_V00:
Jan F c2b009
+	case KEY_DSA_CERT:
Jan F c2b009
+	case KEY_DSA:
Jan F c2b009
+		return k->dsa->priv_key != NULL;
Jan F c2b009
+#ifdef OPENSSL_HAS_ECC
Jan F c2b009
+	case KEY_ECDSA_CERT:
Jan F c2b009
+	case KEY_ECDSA:
Jan F c2b009
+		return EC_KEY_get0_private_key(k->ecdsa) != NULL;
Jan F c2b009
+#endif
Jan F c2b009
+	default:
Jan F c2b009
+		fatal("key_is_private: bad key type %d", k->type);
Jan F c2b009
+		return 1;
Jan F c2b009
+	}
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
+int
Jan F c2b009
 key_is_cert(const Key *k)
Jan F c2b009
 {
Jan F c2b009
 	if (k == NULL)
Jan F c2b009
diff -up openssh-5.8p1/key.h.audit5 openssh-5.8p1/key.h
Jan F a864d6
--- openssh-5.8p1/key.h.audit5	2011-03-10 11:58:51.452981181 +0100
Jan F a864d6
+++ openssh-5.8p1/key.h	2011-03-10 11:58:55.276854914 +0100
Jan F f9ff10
@@ -109,6 +109,7 @@ Key	*key_generate(int, u_int);
Jan F c2b009
 Key	*key_from_private(const Key *);
Jan F c2b009
 int	 key_type_from_name(char *);
Jan F c2b009
 int	 key_is_cert(const Key *);
Jan F c2b009
+int	 key_is_private(const Key *k);
Jan F c2b009
 int	 key_type_plain(int);
Jan F c2b009
 int	 key_to_certified(Key *, int);
Jan F c2b009
 int	 key_drop_cert(Key *);
Jan F 003cb0
diff -up openssh-5.8p1/monitor.c.audit5 openssh-5.8p1/monitor.c
Jan F a864d6
--- openssh-5.8p1/monitor.c.audit5	2011-03-10 11:58:54.503854586 +0100
Jan F a864d6
+++ openssh-5.8p1/monitor.c	2011-03-10 11:58:55.333855125 +0100
Jan F 1499a2
@@ -106,6 +106,8 @@ extern Buffer auth_debug;
Jan F 1499a2
 extern int auth_debug_init;
Jan F 1499a2
 extern Buffer loginmsg;
Jan F 1499a2
 
Jan F 1499a2
+extern void destroy_sensitive_data(int);
Jan F 1499a2
+
Jan F 1499a2
 /* State exported from the child */
Jan F 1499a2
 
Jan F 1499a2
 struct {
Jan F 1499a2
@@ -182,6 +184,7 @@ int mm_answer_audit_end_command(int, Buf
Jan F 003cb0
 int mm_answer_audit_unsupported_body(int, Buffer *);
Jan F 003cb0
 int mm_answer_audit_kex_body(int, Buffer *);
Jan F 003cb0
 int mm_answer_audit_session_key_free_body(int, Buffer *);
Jan F 003cb0
+int mm_answer_audit_server_key_free(int, Buffer *);
Jan F 003cb0
 #endif
Jan F 003cb0
 
Jan F 003cb0
 static Authctxt *authctxt;
Jan F 1499a2
@@ -233,6 +236,7 @@ struct mon_table mon_dispatch_proto20[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
 #ifdef BSD_AUTH
Jan F 003cb0
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
Jan F 1499a2
@@ -273,6 +277,7 @@ struct mon_table mon_dispatch_postauth20
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F 1499a2
@@ -307,6 +312,7 @@ struct mon_table mon_dispatch_proto15[] 
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F 1499a2
@@ -322,6 +328,7 @@ struct mon_table mon_dispatch_postauth15
Jan F 003cb0
     {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
Jan F 003cb0
     {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
Jan F 003cb0
+    {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
Jan F 003cb0
 #endif
Jan F 003cb0
     {0, 0, NULL}
Jan F 003cb0
 };
Jan F edc172
@@ -1607,6 +1614,8 @@ mm_answer_term(int sock, Buffer *req)
Jan F 1499a2
 		sshpam_cleanup();
Jan F 1499a2
 #endif
Jan F 1499a2
 
Jan F 1499a2
+	destroy_sensitive_data(0);
Jan F 1499a2
+
Jan F 1499a2
 	while (waitpid(pmonitor->m_pid, &status, 0) == -1)
Jan F 1499a2
 		if (errno != EINTR)
Jan F 1499a2
 			exit(1);
Jan F edc172
@@ -2338,4 +2347,24 @@ mm_answer_audit_session_key_free_body(in
Jan F 003cb0
 	mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
Jan F 003cb0
 	return 0;
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+int
Jan F 003cb0
+mm_answer_audit_server_key_free(int sock, Buffer *m)
Jan F 003cb0
+{
Jan F 2c1a4a
+	int len;
Jan F 2c1a4a
+	char *fp;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F 2c1a4a
+
Jan F 2c1a4a
+	fp = buffer_get_string(m, &len;;
Jan F c2b009
+	pid = buffer_get_int64(m);
Jan F c2b009
+	uid = buffer_get_int64(m);
Jan F 2c1a4a
+
Jan F c2b009
+	audit_destroy_sensitive_data(fp, pid, uid);
Jan F 003cb0
+
Jan F 003cb0
+	buffer_clear(m);
Jan F 003cb0
+
Jan F 003cb0
+	mm_request_send(sock, MONITOR_ANS_AUDIT_SERVER_KEY_FREE, m);
Jan F 003cb0
+	return 0;
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor.h.audit5 openssh-5.8p1/monitor.h
Jan F a864d6
--- openssh-5.8p1/monitor.h.audit5	2011-03-10 11:58:54.545980156 +0100
Jan F a864d6
+++ openssh-5.8p1/monitor.h	2011-03-10 11:58:55.378854631 +0100
Jan F 1732b0
@@ -70,6 +70,7 @@ enum monitor_reqtype {
Jan F 003cb0
 	MONITOR_REQ_AUDIT_UNSUPPORTED, MONITOR_ANS_AUDIT_UNSUPPORTED,
Jan F 003cb0
 	MONITOR_REQ_AUDIT_KEX, MONITOR_ANS_AUDIT_KEX,
Jan F 003cb0
 	MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MONITOR_ANS_AUDIT_SESSION_KEY_FREE,
Jan F 003cb0
+	MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
Jan F 003cb0
 };
Jan F 003cb0
 
Jan F 003cb0
 struct mm_master;
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.c.audit5 openssh-5.8p1/monitor_wrap.c
Jan F a864d6
--- openssh-5.8p1/monitor_wrap.c.audit5	2011-03-10 11:58:54.602854916 +0100
Jan F a864d6
+++ openssh-5.8p1/monitor_wrap.c	2011-03-10 11:58:55.432980073 +0100
Jan F edc172
@@ -1501,4 +1501,20 @@ mm_audit_session_key_free_body(int ctos,
Jan F 003cb0
 				  &m);
Jan F 003cb0
 	buffer_free(&m);
Jan F 003cb0
 }
Jan F 003cb0
+
Jan F 003cb0
+void
Jan F c2b009
+mm_audit_destroy_sensitive_data(const char *fp, pid_t pid, uid_t uid)
Jan F 003cb0
+{
Jan F 003cb0
+	Buffer m;
Jan F 003cb0
+
Jan F 003cb0
+	buffer_init(&m);
Jan F 2c1a4a
+	buffer_put_cstring(&m, fp);
Jan F c2b009
+	buffer_put_int64(&m, pid);
Jan F c2b009
+	buffer_put_int64(&m, uid);
Jan F 2c1a4a
+
Jan F 003cb0
+	mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_SERVER_KEY_FREE, &m);
Jan F 003cb0
+	mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUDIT_SERVER_KEY_FREE,
Jan F 003cb0
+				  &m);
Jan F 003cb0
+	buffer_free(&m);
Jan F 003cb0
+}
Jan F 003cb0
 #endif /* SSH_AUDIT_EVENTS */
Jan F 003cb0
diff -up openssh-5.8p1/monitor_wrap.h.audit5 openssh-5.8p1/monitor_wrap.h
Jan F a864d6
--- openssh-5.8p1/monitor_wrap.h.audit5	2011-03-10 11:58:54.648980503 +0100
Jan F a864d6
+++ openssh-5.8p1/monitor_wrap.h	2011-03-10 11:58:55.480855200 +0100
Jan F a864d6
@@ -79,6 +79,7 @@ void mm_audit_end_command(int, const cha
Jan F 003cb0
 void mm_audit_unsupported_body(int);
Jan F 9cefae
 void mm_audit_kex_body(int, char *, char *, char *, pid_t, uid_t);
Jan F 9cefae
 void mm_audit_session_key_free_body(int, pid_t, uid_t);
Jan F c2b009
+void mm_audit_destroy_sensitive_data(const char *, pid_t, uid_t);
Jan F 003cb0
 #endif
Jan F 003cb0
 
Jan F 003cb0
 struct Session;
Jan F 003cb0
diff -up openssh-5.8p1/session.c.audit5 openssh-5.8p1/session.c
Jan F a864d6
--- openssh-5.8p1/session.c.audit5	2011-03-10 11:58:54.800854988 +0100
Jan F a864d6
+++ openssh-5.8p1/session.c	2011-03-10 11:58:55.540854785 +0100
Jan F 2c1a4a
@@ -132,7 +132,7 @@ extern int log_stderr;
Jan F 2c1a4a
 extern int debug_flag;
Jan F 2c1a4a
 extern u_int utmp_len;
Jan F 2c1a4a
 extern int startup_pipe;
Jan F 2c1a4a
-extern void destroy_sensitive_data(void);
Jan F 2c1a4a
+extern void destroy_sensitive_data(int);
Jan F 2c1a4a
 extern Buffer loginmsg;
Jan F 2c1a4a
 
Jan F 2c1a4a
 /* original command from peer. */
Jan F edc172
@@ -1626,7 +1626,7 @@ do_child(Session *s, const char *command
Jan F 2c1a4a
 	int r = 0;
Jan F 003cb0
 
Jan F 003cb0
 	/* remove hostkey from the child's memory */
Jan F 2c1a4a
-	destroy_sensitive_data();
Jan F 2c1a4a
+	destroy_sensitive_data(1);
Jan F f9ff10
 	/* Don't audit this - both us and the parent would be talking to the
Jan F f9ff10
 	   monitor over a single socket, with no synchronization. */
Jan F f9ff10
 	packet_destroy_all(0, 1);
Jan F 003cb0
diff -up openssh-5.8p1/sshd.c.audit5 openssh-5.8p1/sshd.c
Jan F a864d6
--- openssh-5.8p1/sshd.c.audit5	2011-03-10 11:58:54.856979886 +0100
Jan F a864d6
+++ openssh-5.8p1/sshd.c	2011-03-10 11:58:55.728855905 +0100
Jan F 2c1a4a
@@ -253,7 +253,7 @@ Buffer loginmsg;
Jan F 2c1a4a
 struct passwd *privsep_pw = NULL;
Jan F 2c1a4a
 
Jan F 2c1a4a
 /* Prototypes for various functions defined later in this file. */
Jan F 2c1a4a
-void destroy_sensitive_data(void);
Jan F 2c1a4a
+void destroy_sensitive_data(int);
Jan F 2c1a4a
 void demote_sensitive_data(void);
Jan F 2c1a4a
 
Jan F 2c1a4a
 static void do_ssh1_kex(void);
Jan F c2b009
@@ -272,6 +272,15 @@ close_listen_socks(void)
Jan F c2b009
 	num_listen_socks = -1;
Jan F c2b009
 }
Jan F c2b009
 
Jan F c2b009
+/*
Jan F c2b009
+ * Is this process listening for clients (i.e. not specific to any specific
Jan F c2b009
+ * client connection?)
Jan F c2b009
+ */
Jan F c2b009
+int listening_for_clients(void)
Jan F c2b009
+{
Jan F c2b009
+	return num_listen_socks > 0;
Jan F c2b009
+}
Jan F c2b009
+
Jan F c2b009
 static void
Jan F c2b009
 close_startup_pipes(void)
Jan F c2b009
 {
Jan F c2b009
@@ -532,22 +541,47 @@ sshd_exchange_identification(int sock_in
Jan F c2b009
 	}
Jan F c2b009
 }
Jan F 2c1a4a
 
Jan F c2b009
-/* Destroy the host and server keys.  They will no longer be needed. */
Jan F c2b009
+/*
Jan F c2b009
+ * Destroy the host and server keys.  They will no longer be needed.  Careful,
Jan F c2b009
+ * this can be called from cleanup_exit() - i.e. from just about anywhere.
Jan F c2b009
+ */
Jan F 2c1a4a
 void
Jan F 2c1a4a
-destroy_sensitive_data(void)
Jan F 2c1a4a
+destroy_sensitive_data(int privsep)
Jan F 2c1a4a
 {
Jan F 2c1a4a
 	int i;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F 2c1a4a
 
Jan F c2b009
 	if (sensitive_data.server_key) {
Jan F c2b009
 		key_free(sensitive_data.server_key);
Jan F c2b009
 		sensitive_data.server_key = NULL;
Jan F 2c1a4a
 	}
Jan F c2b009
+	pid = getpid();
Jan F c2b009
+	uid = getuid();
Jan F 2c1a4a
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F 2c1a4a
 		if (sensitive_data.host_keys[i]) {
Jan F 2c1a4a
+			char *fp;
Jan F 2c1a4a
+
Jan F c2b009
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F c2b009
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F c2b009
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F c2b009
+					SSH_FP_HEX);
Jan F c2b009
+			else
Jan F c2b009
+				fp = NULL;
Jan F 2c1a4a
 			key_free(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			sensitive_data.host_keys[i] = NULL;
Jan F c2b009
+			if (fp != NULL) {
Jan F c2b009
+				if (privsep)
Jan F c2b009
+					PRIVSEP(audit_destroy_sensitive_data(fp,
Jan F c2b009
+						pid, uid));
Jan F c2b009
+				else
Jan F c2b009
+					audit_destroy_sensitive_data(fp,
Jan F c2b009
+						pid, uid);
Jan F c2b009
+				xfree(fp);
Jan F c2b009
+			}
Jan F 2c1a4a
 		}
Jan F c2b009
-		if (sensitive_data.host_certificates[i]) {
Jan F c2b009
+		if (sensitive_data.host_certificates
Jan F c2b009
+		    && sensitive_data.host_certificates[i]) {
Jan F 2c1a4a
 			key_free(sensitive_data.host_certificates[i]);
Jan F c2b009
 			sensitive_data.host_certificates[i] = NULL;
Jan F c2b009
 		}
Jan F c2b009
@@ -561,6 +595,8 @@ void
Jan F c2b009
 demote_sensitive_data(void)
Jan F c2b009
 {
Jan F c2b009
 	Key *tmp;
Jan F c2b009
+	pid_t pid;
Jan F c2b009
+	uid_t uid;
Jan F c2b009
 	int i;
Jan F 2c1a4a
 
Jan F c2b009
 	if (sensitive_data.server_key) {
Jan F c2b009
@@ -569,13 +605,27 @@ demote_sensitive_data(void)
Jan F c2b009
 		sensitive_data.server_key = tmp;
Jan F c2b009
 	}
Jan F c2b009
 
Jan F c2b009
+	pid = getpid();
Jan F c2b009
+	uid = getuid();
Jan F 2c1a4a
 	for (i = 0; i < options.num_host_key_files; i++) {
Jan F 2c1a4a
 		if (sensitive_data.host_keys[i]) {
Jan F 2c1a4a
+			char *fp;
Jan F 2c1a4a
+
Jan F c2b009
+			if (key_is_private(sensitive_data.host_keys[i]))
Jan F c2b009
+				fp = key_fingerprint(sensitive_data.host_keys[i],
Jan F c2b009
+					FIPS_mode() ? SSH_FP_SHA1 : SSH_FP_MD5,
Jan F c2b009
+					SSH_FP_HEX);
Jan F c2b009
+			else
Jan F c2b009
+				fp = NULL;
Jan F 2c1a4a
 			tmp = key_demote(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			key_free(sensitive_data.host_keys[i]);
Jan F 2c1a4a
 			sensitive_data.host_keys[i] = tmp;
Jan F 2c1a4a
 			if (tmp->type == KEY_RSA1)
Jan F 2c1a4a
 				sensitive_data.ssh1_host_key = tmp;
Jan F c2b009
+			if (fp != NULL) {
Jan F c2b009
+				audit_destroy_sensitive_data(fp, pid, uid);
Jan F c2b009
+				xfree(fp);
Jan F c2b009
+			}
Jan F 003cb0
 		}
Jan F 003cb0
 		/* Certs do not need demotion */
Jan F 003cb0
 	}
Jan F 1499a2
@@ -1120,6 +1170,7 @@ server_accept_loop(int *sock_in, int *so
Jan F c2b009
 		if (received_sigterm) {
Jan F c2b009
 			logit("Received signal %d; terminating.",
Jan F c2b009
 			    (int) received_sigterm);
Jan F c2b009
+			destroy_sensitive_data(0);
Jan F c2b009
 			close_listen_socks();
Jan F c2b009
 			unlink(options.pid_file);
Jan F c2b009
 			exit(255);
Jan F 1499a2
@@ -2023,7 +2074,7 @@ main(int ac, char **av)
Jan F 003cb0
 		privsep_postauth(authctxt);
Jan F 003cb0
 		/* the monitor process [priv] will not return */
Jan F 2c1a4a
 		if (!compat20)
Jan F 2c1a4a
-			destroy_sensitive_data();
Jan F 2c1a4a
+			destroy_sensitive_data(0);
Jan F 003cb0
 	}
Jan F 003cb0
 
Jan F 003cb0
 	packet_set_timeout(options.client_alive_interval,
Jan F a864d6
@@ -2034,6 +2085,7 @@ main(int ac, char **av)
Jan F a864d6
 
Jan F a864d6
 	/* The connection has been terminated. */
Jan F a864d6
 	packet_destroy_all(1, 1);
Jan F a864d6
+	destroy_sensitive_data(1);
Jan F a864d6
 
Jan F a864d6
 	packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes);
Jan F a864d6
 	packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes);
Jan F a864d6
@@ -2262,7 +2314,7 @@ do_ssh1_kex(void)
Jan F 2c1a4a
 			session_id[i] = session_key[i] ^ session_key[i + 16];
Jan F 003cb0
 	}
Jan F 003cb0
 	/* Destroy the private and public keys. No longer. */
Jan F 2c1a4a
-	destroy_sensitive_data();
Jan F 2c1a4a
+	destroy_sensitive_data(0);
Jan F 003cb0
 
Jan F 003cb0
 	if (use_privsep)
Jan F 003cb0
 		mm_ssh1_session_id(session_id);
Jan F a864d6
@@ -2352,8 +2404,22 @@ do_ssh2_kex(void)
Jan F 1732b0
 void
Jan F 1732b0
 cleanup_exit(int i)
Jan F c2b009
 {
Jan F 1732b0
+	static int in_cleanup;
Jan F 1732b0
+
Jan F 1732b0
+	int is_privsep_child;
Jan F 1732b0
+
Jan F 1732b0
+	/* cleanup_exit can be called at the very least from the privsep
Jan F 1732b0
+	   wrappers used for auditing.  Make sure we don't recurse
Jan F 1732b0
+	   indefinitely. */
Jan F 1732b0
+	if (in_cleanup)
Jan F 1732b0
+		_exit(i);
Jan F 1732b0
+	in_cleanup = 1;
Jan F 1732b0
+
Jan F c2b009
 	if (the_authctxt)
Jan F c2b009
 		do_cleanup(the_authctxt);
Jan F 1732b0
+	is_privsep_child = use_privsep && pmonitor != NULL && !mm_is_monitor();
Jan F c2b009
+	if (sensitive_data.host_keys != NULL)
Jan F 1732b0
+		destroy_sensitive_data(is_privsep_child);
Jan F f9ff10
 	packet_destroy_all(1, is_privsep_child);
Jan F c2b009
 #ifdef SSH_AUDIT_EVENTS
Jan F c2b009
 	/* done after do_cleanup so it can cancel the PAM auth 'thread' */