c9833c9
diff -up openssh-4.7p1/auth.c.audit openssh-4.7p1/auth.c
c9833c9
--- openssh-4.7p1/auth.c.audit	2007-03-26 18:35:28.000000000 +0200
c9833c9
+++ openssh-4.7p1/auth.c	2007-09-06 17:07:44.000000000 +0200
c9833c9
@@ -286,6 +286,12 @@ auth_log(Authctxt *authctxt, int authent
c9833c9
 		    get_canonical_hostname(options.use_dns), "ssh", &loginmsg);
c9833c9
 # endif
c9833c9
 #endif
c9833c9
+#if HAVE_LINUX_AUDIT
c9833c9
+	if (authenticated == 0 && !authctxt->postponed) {
c9833c9
+		linux_audit_record_event(-1, authctxt->user, NULL,
c9833c9
+			get_remote_ipaddr(), "sshd", 0);
c9833c9
+	}
c9833c9
+#endif
c9833c9
 #ifdef SSH_AUDIT_EVENTS
c9833c9
 	if (authenticated == 0 && !authctxt->postponed)
c9833c9
 		audit_event(audit_classify_auth(method));
c9833c9
@@ -492,6 +498,10 @@ getpwnamallow(const char *user)
c9833c9
 		record_failed_login(user,
c9833c9
 		    get_canonical_hostname(options.use_dns), "ssh");
c9833c9
 #endif
c9833c9
+#ifdef HAVE_LINUX_AUDIT
c9833c9
+		linux_audit_record_event(-1, user, NULL, get_remote_ipaddr(),
c9833c9
+			"sshd", 0);
c9833c9
+#endif
c9833c9
 #ifdef SSH_AUDIT_EVENTS
c9833c9
 		audit_event(SSH_INVALID_USER);
c9833c9
 #endif /* SSH_AUDIT_EVENTS */
c9833c9
diff -up openssh-4.7p1/loginrec.c.audit openssh-4.7p1/loginrec.c
c9833c9
--- openssh-4.7p1/loginrec.c.audit	2007-04-29 04:10:58.000000000 +0200
c9833c9
+++ openssh-4.7p1/loginrec.c	2007-09-06 17:07:44.000000000 +0200
c9833c9
@@ -176,6 +176,10 @@
ad07b99
 #include "auth.h"
ad07b99
 #include "buffer.h"
ad07b99
 
ad07b99
+#ifdef HAVE_LINUX_AUDIT
ad07b99
+# include <libaudit.h>
ad07b99
+#endif
ad07b99
+
ad07b99
 #ifdef HAVE_UTIL_H
ad07b99
 # include <util.h>
ad07b99
 #endif
c9833c9
@@ -202,6 +206,9 @@ int utmp_write_entry(struct logininfo *l
ad07b99
 int utmpx_write_entry(struct logininfo *li);
ad07b99
 int wtmp_write_entry(struct logininfo *li);
ad07b99
 int wtmpx_write_entry(struct logininfo *li);
ad07b99
+#ifdef HAVE_LINUX_AUDIT
ad07b99
+int linux_audit_write_entry(struct logininfo *li);
ad07b99
+#endif
ad07b99
 int lastlog_write_entry(struct logininfo *li);
ad07b99
 int syslogin_write_entry(struct logininfo *li);
ad07b99
 
c9833c9
@@ -440,6 +447,10 @@ login_write(struct logininfo *li)
ad07b99
 
ad07b99
 	/* set the timestamp */
ad07b99
 	login_set_current_time(li);
ad07b99
+#ifdef HAVE_LINUX_AUDIT
ad07b99
+	if (linux_audit_write_entry(li) == 0)
ad07b99
+		fatal("linux_audit_write_entry failed: %s", strerror(errno));
ad07b99
+#endif
ad07b99
 #ifdef USE_LOGIN
ad07b99
 	syslogin_write_entry(li);
ad07b99
 #endif
c9833c9
@@ -1394,6 +1405,51 @@ wtmpx_get_entry(struct logininfo *li)
ad07b99
 }
ad07b99
 #endif /* USE_WTMPX */
ad07b99
 
ad07b99
+#ifdef HAVE_LINUX_AUDIT
ad07b99
+int
ad07b99
+linux_audit_record_event(int uid, const char *username,
ad07b99
+	const char *hostname, const char *ip, const char *ttyn, int success)
ad07b99
+{
ad07b99
+	char buf[64];
ad07b99
+	int audit_fd, rc;
ad07b99
+
ad07b99
+	audit_fd = audit_open();
ad07b99
+	if (audit_fd < 0) {
ad07b99
+	 	if (errno == EINVAL || errno == EPROTONOSUPPORT ||
ad07b99
+					errno == EAFNOSUPPORT)
ad07b99
+			return 1; /* No audit support in kernel */
ad07b99
+		else
ad07b99
+			return 0; /* Must prevent login */
ad07b99
+	}
ad07b99
+	if (username == NULL)
ad07b99
+		snprintf(buf, sizeof(buf), "uid=%d", uid);
ad07b99
+	else
ad07b99
+		snprintf(buf, sizeof(buf), "acct=%s", username);
ad07b99
+	rc = audit_log_user_message(audit_fd, AUDIT_USER_LOGIN,
ad07b99
+		buf, hostname, ip, ttyn, success);
ad07b99
+	close(audit_fd);
ad07b99
+	if (rc >= 0)
ad07b99
+		return 1;
ad07b99
+	else
ad07b99
+		return 0;
ad07b99
+}
ad07b99
+
ad07b99
+int
ad07b99
+linux_audit_write_entry(struct logininfo *li)
ad07b99
+{
ad07b99
+	switch(li->type) {
ad07b99
+	case LTYPE_LOGIN:
ad07b99
+		return (linux_audit_record_event(li->uid, NULL, li->hostname,
ad07b99
+			NULL, li->line, 1));
ad07b99
+	case LTYPE_LOGOUT:
ad07b99
+		return (1);	/* We only care about logins */
ad07b99
+	default:
ad07b99
+		logit("%s: invalid type field", __func__);
ad07b99
+		return (0);
ad07b99
+	}
ad07b99
+}
ad07b99
+#endif /* HAVE_LINUX_AUDIT */
ad07b99
+
ad07b99
 /**
ad07b99
  ** Low-level libutil login() functions
ad07b99
  **/
c9833c9
diff -up openssh-4.7p1/config.h.in.audit openssh-4.7p1/config.h.in
c9833c9
--- openssh-4.7p1/config.h.in.audit	2007-09-04 08:50:04.000000000 +0200
c9833c9
+++ openssh-4.7p1/config.h.in	2007-09-06 17:07:44.000000000 +0200
c9833c9
@@ -1334,6 +1334,9 @@
ad07b99
 /* Define if you want SELinux support. */
ad07b99
 #undef WITH_SELINUX
ad07b99
 
ad07b99
+/* Define if you want Linux audit support. */
ad07b99
+#undef HAVE_LINUX_AUDIT
ad07b99
+
ad07b99
 /* Define to 1 if your processor stores words with the most significant byte
ad07b99
    first (like Motorola and SPARC, unlike Intel and VAX). */
ad07b99
 #undef WORDS_BIGENDIAN
c9833c9
diff -up openssh-4.7p1/loginrec.h.audit openssh-4.7p1/loginrec.h
c9833c9
--- openssh-4.7p1/loginrec.h.audit	2006-08-05 04:39:40.000000000 +0200
c9833c9
+++ openssh-4.7p1/loginrec.h	2007-09-06 17:07:44.000000000 +0200
c9833c9
@@ -127,5 +127,9 @@ char *line_stripname(char *dst, const ch
c9833c9
 char *line_abbrevname(char *dst, const char *src, int dstsize);
c9833c9
 
c9833c9
 void record_failed_login(const char *, const char *, const char *);
c9833c9
+#ifdef HAVE_LINUX_AUDIT
c9833c9
+int linux_audit_record_event(int uid, const char *username,
c9833c9
+	const char *hostname, const char *ip, const char *ttyn, int success);
c9833c9
+#endif /* HAVE_LINUX_AUDIT */
c9833c9
 
c9833c9
 #endif /* _HAVE_LOGINREC_H_ */
c9833c9
diff -up openssh-4.7p1/configure.ac.audit openssh-4.7p1/configure.ac
c9833c9
--- openssh-4.7p1/configure.ac.audit	2007-09-06 17:07:44.000000000 +0200
c9833c9
+++ openssh-4.7p1/configure.ac	2007-09-06 17:15:23.000000000 +0200
c9833c9
@@ -3216,6 +3216,18 @@ AC_ARG_WITH(selinux,
c9833c9
 	fi ]
ad07b99
 )
ad07b99
 
ad07b99
+# Check whether user wants Linux audit support
ad07b99
+LINUX_AUDIT_MSG="no"
ad07b99
+AC_ARG_WITH(linux-audit,
ad07b99
+	[  --with-linux-audit   Enable Linux audit support],
ad07b99
+	[ if test "x$withval" != "xno" ; then
ad07b99
+		AC_DEFINE(HAVE_LINUX_AUDIT,1,[Define if you want Linux audit support.])
ad07b99
+		LINUX_AUDIT_MSG="yes"
ad07b99
+		AC_CHECK_HEADERS(libaudit.h)
c9833c9
+		SSHDLIBS="$SSHDLIBS -laudit"
c9833c9
+	fi ]
c9833c9
+)
ad07b99
+
ad07b99
 # Check whether user wants Kerberos 5 support
ad07b99
 KRB5_MSG="no"
ad07b99
 AC_ARG_WITH(kerberos5,
c9833c9
@@ -4037,6 +4049,7 @@ echo "                       PAM support
ad07b99
 echo "                   OSF SIA support: $SIA_MSG"
ad07b99
 echo "                 KerberosV support: $KRB5_MSG"
ad07b99
 echo "                   SELinux support: $SELINUX_MSG"
ad07b99
+echo "               Linux audit support: $LINUX_AUDIT_MSG"
ad07b99
 echo "                 Smartcard support: $SCARD_MSG"
ad07b99
 echo "                     S/KEY support: $SKEY_MSG"
ad07b99
 echo "              TCP Wrappers support: $TCPW_MSG"