1c90146
diff -up openssh-5.3p1/auth.c.audit openssh-5.3p1/auth.c
1c90146
--- openssh-5.3p1/auth.c.audit	2008-11-05 06:12:54.000000000 +0100
1c90146
+++ openssh-5.3p1/auth.c	2009-10-11 13:02:47.000000000 +0200
15914f2
@@ -287,6 +287,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));
15914f2
@@ -533,6 +539,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 */
1c90146
diff -up openssh-5.3p1/config.h.in.audit openssh-5.3p1/config.h.in
1c90146
--- openssh-5.3p1/config.h.in.audit	2009-09-26 08:31:14.000000000 +0200
1c90146
+++ openssh-5.3p1/config.h.in	2009-10-11 13:09:41.000000000 +0200
1c90146
@@ -533,6 +533,9 @@
15914f2
 /* Define to 1 if you have the <lastlog.h> header file. */
15914f2
 #undef HAVE_LASTLOG_H
15914f2
 
15914f2
+/* Define to 1 if you have the <libaudit.h> header file. */
15914f2
+#undef HAVE_LIBAUDIT_H
15914f2
+
15914f2
 /* Define to 1 if you have the `bsm' library (-lbsm). */
15914f2
 #undef HAVE_LIBBSM
15914f2
 
1c90146
@@ -572,6 +575,9 @@
15914f2
 /* Define to 1 if you have the <limits.h> header file. */
15914f2
 #undef HAVE_LIMITS_H
15914f2
 
15914f2
+/* Define if you want Linux audit support. */
15914f2
+#undef HAVE_LINUX_AUDIT
15914f2
+
15914f2
 /* Define to 1 if you have the <linux/if_tun.h> header file. */
15914f2
 #undef HAVE_LINUX_IF_TUN_H
15914f2
 
1c90146
@@ -768,6 +774,9 @@
15914f2
 /* Define to 1 if you have the `setgroups' function. */
15914f2
 #undef HAVE_SETGROUPS
15914f2
 
15914f2
+/* Define to 1 if you have the `setkeycreatecon' function. */
15914f2
+#undef HAVE_SETKEYCREATECON
15914f2
+
15914f2
 /* Define to 1 if you have the `setlogin' function. */
15914f2
 #undef HAVE_SETLOGIN
15914f2
 
1c90146
@@ -1348,6 +1357,10 @@
15914f2
 /* Prepend the address family to IP tunnel traffic */
15914f2
 #undef SSH_TUN_PREPEND_AF
15914f2
 
15914f2
+/* Define to your vendor patch level, if it has been modified from the
15914f2
+   upstream source release. */
15914f2
+#undef SSH_VENDOR_PATCHLEVEL
15914f2
+
15914f2
 /* Define to 1 if you have the ANSI C header files. */
15914f2
 #undef STDC_HEADERS
15914f2
 
1c90146
diff -up openssh-5.3p1/configure.ac.audit openssh-5.3p1/configure.ac
1c90146
--- openssh-5.3p1/configure.ac.audit	2009-09-11 06:56:08.000000000 +0200
1c90146
+++ openssh-5.3p1/configure.ac	2009-10-11 13:08:03.000000000 +0200
1c90146
@@ -3407,6 +3407,18 @@ AC_ARG_WITH(selinux,
15914f2
 	fi ]
15914f2
 )
15914f2
 
15914f2
+# Check whether user wants Linux audit support
15914f2
+LINUX_AUDIT_MSG="no"
15914f2
+AC_ARG_WITH(linux-audit,
15914f2
+	[  --with-linux-audit   Enable Linux audit support],
15914f2
+	[ if test "x$withval" != "xno" ; then
15914f2
+		AC_DEFINE(HAVE_LINUX_AUDIT,1,[Define if you want Linux audit support.])
15914f2
+		LINUX_AUDIT_MSG="yes"
15914f2
+		AC_CHECK_HEADERS(libaudit.h)
15914f2
+		SSHDLIBS="$SSHDLIBS -laudit"
15914f2
+	fi ]
15914f2
+)
15914f2
+
15914f2
 # Check whether user wants Kerberos 5 support
15914f2
 KRB5_MSG="no"
15914f2
 AC_ARG_WITH(kerberos5,
1c90146
@@ -4226,6 +4238,7 @@ echo "                       PAM support
15914f2
 echo "                   OSF SIA support: $SIA_MSG"
15914f2
 echo "                 KerberosV support: $KRB5_MSG"
15914f2
 echo "                   SELinux support: $SELINUX_MSG"
15914f2
+echo "               Linux audit support: $LINUX_AUDIT_MSG"
15914f2
 echo "                 Smartcard support: $SCARD_MSG"
15914f2
 echo "                     S/KEY support: $SKEY_MSG"
15914f2
 echo "              TCP Wrappers support: $TCPW_MSG"
1c90146
diff -up openssh-5.3p1/loginrec.c.audit openssh-5.3p1/loginrec.c
1c90146
--- openssh-5.3p1/loginrec.c.audit	2009-02-12 03:12:22.000000000 +0100
1c90146
+++ openssh-5.3p1/loginrec.c	2009-10-11 13:06:16.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
15914f2
@@ -1394,6 +1405,87 @@ wtmpx_get_entry(struct logininfo *li)
ad07b99
 }
ad07b99
 #endif /* USE_WTMPX */
ad07b99
 
ad07b99
+#ifdef HAVE_LINUX_AUDIT
15914f2
+static void
15914f2
+_audit_hexscape(const char *what, char *where, unsigned int size)
15914f2
+{
15914f2
+	const char *ptr = what;
15914f2
+	const char *hex = "0123456789ABCDEF";
15914f2
+
15914f2
+	while (*ptr) {
15914f2
+		if (*ptr == '"' || *ptr < 0x21 || *ptr > 0x7E) {
15914f2
+			unsigned int i;
15914f2
+			ptr = what;
15914f2
+			for (i = 0; *ptr && i+2 < size; i += 2) {
15914f2
+				where[i] = hex[((unsigned)*ptr & 0xF0)>>4]; /* Upper nibble */
15914f2
+				where[i+1] = hex[(unsigned)*ptr & 0x0F];   /* Lower nibble */
15914f2
+				ptr++;
15914f2
+			}
15914f2
+			where[i] = '\0';
15914f2
+			return;
15914f2
+		}
15914f2
+		ptr++;
15914f2
+	}
15914f2
+	where[0] = '"';
15914f2
+	if ((unsigned)(ptr - what) < size - 3)
15914f2
+	{
15914f2
+		size = ptr - what + 3;
15914f2
+	}
15914f2
+	strncpy(where + 1, what, size - 3);
15914f2
+	where[size-2] = '"';
15914f2
+	where[size-1] = '\0';
15914f2
+}
15914f2
+
15914f2
+#define AUDIT_LOG_SIZE 128
15914f2
+#define AUDIT_ACCT_SIZE (AUDIT_LOG_SIZE - 8)
15914f2
+
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
+{
15914f2
+	char buf[AUDIT_LOG_SIZE];
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);
15914f2
+	else {
15914f2
+		char encoded[AUDIT_ACCT_SIZE];
15914f2
+		_audit_hexscape(username, encoded, sizeof(encoded));
15914f2
+		snprintf(buf, sizeof(buf), "acct=%s", encoded);
15914f2
+	}
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
  **/
1c90146
diff -up openssh-5.3p1/loginrec.h.audit openssh-5.3p1/loginrec.h
1c90146
--- openssh-5.3p1/loginrec.h.audit	2006-08-05 04:39:40.000000000 +0200
1c90146
+++ openssh-5.3p1/loginrec.h	2009-10-11 13:04:28.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_ */