openssh/openssh-5.8p1-audit1.patch

192 lines
5.4 KiB
Diff
Raw Normal View History

2011-02-16 16:30:51 +00:00
diff -up openssh-5.8p1/audit-linux.c.audit1 openssh-5.8p1/audit-linux.c
2011-02-16 22:36:59 +00:00
--- openssh-5.8p1/audit-linux.c.audit1 2011-01-17 11:15:30.000000000 +0100
2011-02-21 19:24:29 +00:00
+++ openssh-5.8p1/audit-linux.c 2011-02-21 20:01:00.000000000 +0100
@@ -35,13 +35,20 @@
2011-02-16 16:30:51 +00:00
2011-02-21 19:24:29 +00:00
#include "log.h"
#include "audit.h"
+#include "key.h"
+#include "hostfile.h"
+#include "auth.h"
+#include "servconf.h"
#include "canohost.h"
+extern ServerOptions options;
+extern Authctxt *the_authctxt;
+extern u_int utmp_len;
2011-02-16 16:30:51 +00:00
const char* audit_username(void);
-int
-linux_audit_record_event(int uid, const char *username,
2011-02-21 19:24:29 +00:00
- const char *hostname, const char *ip, const char *ttyn, int success)
2011-02-16 22:36:59 +00:00
+static void
2011-02-21 19:24:29 +00:00
+linux_audit_user_logxxx(int uid, const char *username,
+ const char *hostname, const char *ip, const char *ttyn, int success, int event)
2011-02-16 16:30:51 +00:00
{
int audit_fd, rc, saved_errno;
2011-02-21 19:24:29 +00:00
@@ -49,11 +56,11 @@ linux_audit_record_event(int uid, const
2011-02-16 22:36:59 +00:00
if (audit_fd < 0) {
if (errno == EINVAL || errno == EPROTONOSUPPORT ||
errno == EAFNOSUPPORT)
- return 1; /* No audit support in kernel */
+ return; /* No audit support in kernel */
else
- return 0; /* Must prevent login */
+ goto fatal_report; /* Must prevent login */
}
2011-02-21 19:24:29 +00:00
- rc = audit_log_acct_message(audit_fd, AUDIT_USER_LOGIN,
+ rc = audit_log_acct_message(audit_fd, event,
2011-02-16 22:36:59 +00:00
NULL, "login", username ? username : "(unknown)",
2011-02-21 19:24:29 +00:00
username == NULL ? uid : -1, hostname, ip, ttyn, success);
saved_errno = errno;
@@ -65,35 +72,102 @@ linux_audit_record_event(int uid, const
2011-02-16 22:36:59 +00:00
if ((rc == -EPERM) && (geteuid() != 0))
rc = 0;
errno = saved_errno;
- return (rc >= 0);
+ if (rc < 0) {
+fatal_report:
+ fatal("linux_audit_write_entry failed: %s", strerror(errno));
+ }
+}
+
+static void
2011-02-16 16:30:51 +00:00
+linux_audit_user_auth(int uid, const char *username,
+ const char *hostname, const char *ip, const char *ttyn, int success, int event)
+{
+ int audit_fd, rc, saved_errno;
+ static const char *event_name[] = {
2011-02-21 19:24:29 +00:00
+ "maxtries exceeded",
2011-02-16 16:30:51 +00:00
+ "root denied",
+ "success",
+ "none",
2011-02-21 19:24:29 +00:00
+ "password",
+ "challenge-response",
2011-02-16 16:30:51 +00:00
+ "pubkey",
+ "hostbased",
+ "gssapi",
+ "invalid user",
+ "nologin",
2011-02-21 19:24:29 +00:00
+ "connection closed",
+ "connection abandoned",
2011-02-16 16:30:51 +00:00
+ "unknown"
+ };
+
+ audit_fd = audit_open();
+ if (audit_fd < 0) {
+ if (errno == EINVAL || errno == EPROTONOSUPPORT ||
+ errno == EAFNOSUPPORT)
2011-02-16 22:36:59 +00:00
+ return; /* No audit support in kernel */
2011-02-16 16:30:51 +00:00
+ else
2011-02-16 22:36:59 +00:00
+ goto fatal_report; /* Must prevent login */
2011-02-16 16:30:51 +00:00
+ }
+
+ if ((event < 0) || (event > SSH_AUDIT_UNKNOWN))
+ event = SSH_AUDIT_UNKNOWN;
+
+ rc = audit_log_acct_message(audit_fd, AUDIT_USER_AUTH,
+ NULL, event_name[event], username ? username : "(unknown)",
+ username == NULL ? uid : -1, hostname, ip, ttyn, success);
+ saved_errno = errno;
+ close(audit_fd);
+ /*
+ * Do not report error if the error is EPERM and sshd is run as non
+ * root user.
+ */
+ if ((rc == -EPERM) && (geteuid() != 0))
+ rc = 0;
+ errno = saved_errno;
2011-02-16 22:36:59 +00:00
+ if (rc < 0) {
+fatal_report:
+ fatal("linux_audit_write_entry failed: %s", strerror(errno));
+ }
}
2011-02-16 16:30:51 +00:00
2011-02-21 19:24:29 +00:00
+static int user_login_count = 0;
+
2011-02-16 22:36:59 +00:00
/* Below is the sshd audit API code */
2011-02-21 19:24:29 +00:00
2011-02-16 16:30:51 +00:00
void
audit_connection_from(const char *host, int port)
{
-}
/* not implemented */
+}
void
audit_run_command(const char *command)
2011-02-21 19:24:29 +00:00
{
- /* not implemented */
+ linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
+ NULL, "ssh", 1, AUDIT_USER_START);
+ if (!user_login_count++)
+ linux_audit_user_logxxx(the_authctxt->pw->pw_uid, NULL, get_remote_name_or_ip(utmp_len, options.use_dns),
+ NULL, "ssh", 1, AUDIT_USER_LOGIN);
}
2011-02-16 16:30:51 +00:00
void
audit_session_open(struct logininfo *li)
{
- if (linux_audit_record_event(li->uid, NULL, li->hostname,
2011-02-16 22:36:59 +00:00
- NULL, li->line, 1) == 0)
- fatal("linux_audit_write_entry failed: %s", strerror(errno));
2011-02-21 19:24:29 +00:00
+ linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+ NULL, li->line, 1, AUDIT_USER_START);
+ if (!user_login_count++)
+ linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+ NULL, li->line, 1, AUDIT_USER_LOGIN);
}
void
audit_session_close(struct logininfo *li)
{
- /* not implemented */
+ linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+ NULL, li->line, 1, AUDIT_USER_END);
+ if (!--user_login_count)
+ linux_audit_user_logxxx(li->uid, NULL, li->hostname,
+ NULL, li->line, 1, AUDIT_USER_LOGOUT);
2011-02-16 16:30:51 +00:00
}
2011-02-16 22:36:59 +00:00
void
2011-02-21 19:24:29 +00:00
@@ -101,21 +175,34 @@ audit_event(ssh_audit_event_t event)
2011-02-16 16:30:51 +00:00
{
switch(event) {
case SSH_AUTH_SUCCESS:
- case SSH_CONNECTION_CLOSE:
2011-02-16 22:36:59 +00:00
+ linux_audit_user_auth(-1, audit_username(), NULL,
+ get_remote_ipaddr(), "sshd", 1, event);
2011-02-16 16:30:51 +00:00
+ break;
+
case SSH_NOLOGIN:
2011-02-16 22:36:59 +00:00
- case SSH_LOGIN_EXCEED_MAXTRIES:
2011-02-16 16:30:51 +00:00
case SSH_LOGIN_ROOT_DENIED:
+ linux_audit_user_auth(-1, audit_username(), NULL,
+ get_remote_ipaddr(), "sshd", 0, event);
2011-02-21 19:24:29 +00:00
+ linux_audit_user_logxxx(-1, audit_username(), NULL,
+ get_remote_ipaddr(), "sshd", 0, AUDIT_USER_LOGIN);
2011-02-16 16:30:51 +00:00
break;
2011-02-16 22:36:59 +00:00
+ case SSH_LOGIN_EXCEED_MAXTRIES:
2011-02-16 16:30:51 +00:00
case SSH_AUTH_FAIL_NONE:
2011-02-16 22:36:59 +00:00
case SSH_AUTH_FAIL_PASSWD:
case SSH_AUTH_FAIL_KBDINT:
2011-02-16 16:30:51 +00:00
case SSH_AUTH_FAIL_PUBKEY:
case SSH_AUTH_FAIL_HOSTBASED:
case SSH_AUTH_FAIL_GSSAPI:
+ linux_audit_user_auth(-1, audit_username(), NULL,
+ get_remote_ipaddr(), "sshd", 0, event);
+ break;
+
+ case SSH_CONNECTION_CLOSE:
+ case SSH_CONNECTION_ABANDON:
case SSH_INVALID_USER:
- linux_audit_record_event(-1, audit_username(), NULL,
2011-02-21 19:24:29 +00:00
- get_remote_ipaddr(), "sshd", 0);
+ linux_audit_user_logxxx(-1, audit_username(), NULL,
+ get_remote_ipaddr(), "sshd", 0, AUDIT_USER_LOGIN);
2011-02-16 16:30:51 +00:00
break;
2011-02-21 19:24:29 +00:00
default: