b95810
Index: shadow-4.5/src/chgpasswd.c
b95810
===================================================================
b95810
--- shadow-4.5.orig/src/chgpasswd.c
b95810
+++ shadow-4.5/src/chgpasswd.c
9b7f02
@@ -39,6 +39,13 @@
9b7f02
 #include <pwd.h>
9b7f02
 #include <stdio.h>
9b7f02
 #include <stdlib.h>
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+#include <selinux/selinux.h>
9b7f02
+#include <selinux/avc.h>
9b7f02
+#endif
9b7f02
+#ifdef WITH_LIBAUDIT
9b7f02
+#include <libaudit.h>
9b7f02
+#endif
9b7f02
 #ifdef ACCT_TOOLS_SETUID
9b7f02
 #ifdef USE_PAM
9b7f02
 #include "pam_defs.h"
9b7f02
@@ -76,6 +83,9 @@ static bool sgr_locked = false;
9b7f02
 #endif
9b7f02
 static bool gr_locked = false;
9b7f02
 
9b7f02
+/* The name of the caller */
9b7f02
+static char *myname = NULL;
9b7f02
+
9b7f02
 /* local function prototypes */
9b7f02
 static void fail_exit (int code);
9b7f02
 static /*@noreturn@*/void usage (int status);
9b7f02
@@ -300,6 +310,63 @@ static void check_perms (void)
9b7f02
 #endif				/* ACCT_TOOLS_SETUID */
9b7f02
 }
9b7f02
 
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+static int
9b7f02
+log_callback (int type, const char *fmt, ...)
9b7f02
+{
9b7f02
+    int audit_fd;
9b7f02
+    va_list ap;
9b7f02
+
9b7f02
+    va_start(ap, fmt);
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+    audit_fd = audit_open();
9b7f02
+
9b7f02
+    if (audit_fd >= 0) {
9b7f02
+	char *buf;
9b7f02
+
9b7f02
+	if (vasprintf (&buf, fmt, ap) < 0)
9b7f02
+		goto ret;
9b7f02
+	audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
9b7f02
+				   NULL, 0);
9b7f02
+	audit_close(audit_fd);
9b7f02
+	free(buf);
9b7f02
+	goto ret;
9b7f02
+    }
9b7f02
+
9b7f02
+#endif
9b7f02
+    vsyslog (LOG_USER | LOG_INFO, fmt, ap);
9b7f02
+ret:
9b7f02
+    va_end(ap);
9b7f02
+    return 0;
9b7f02
+}
9b7f02
+
9b7f02
+static void
9b7f02
+selinux_check_root (void)
9b7f02
+{
9b7f02
+    int status = -1;
9b7f02
+    security_context_t user_context;
9b7f02
+    union selinux_callback old_callback;
9b7f02
+
9b7f02
+    if (is_selinux_enabled() < 1)
9b7f02
+	return;
9b7f02
+
9b7f02
+    old_callback = selinux_get_callback(SELINUX_CB_LOG);
9b7f02
+    /* setup callbacks */
9b7f02
+    selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
9b7f02
+    if ((status = getprevcon(&user_context)) < 0) {
9b7f02
+	selinux_set_callback(SELINUX_CB_LOG, old_callback);
9b7f02
+	exit(1);
9b7f02
+    }
9b7f02
+
9b7f02
+    status = selinux_check_access(user_context, user_context, "passwd", "passwd", NULL);
9b7f02
+
9b7f02
+    selinux_set_callback(SELINUX_CB_LOG, old_callback);
9b7f02
+    freecon(user_context);
9b7f02
+    if (status != 0 && security_getenforce() != 0)
9b7f02
+	exit(1);
9b7f02
+}
9b7f02
+#endif
9b7f02
+
9b7f02
 /*
9b7f02
  * open_files - lock and open the group databases
9b7f02
  */
9b7f02
@@ -393,6 +460,7 @@ int main (int argc, char **argv)
9b7f02
 
9b7f02
 	const struct group *gr;
9b7f02
 	struct group newgr;
9b7f02
+	struct passwd *pw = NULL;
9b7f02
 	int errors = 0;
9b7f02
 	int line = 0;
9b7f02
 
9b7f02
@@ -408,8 +476,33 @@ int main (int argc, char **argv)
9b7f02
 
9b7f02
 	OPENLOG ("chgpasswd");
9b7f02
 
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+	audit_help_open ();
9b7f02
+#endif
9b7f02
+
9b7f02
+	/*
9b7f02
+	 * Determine the name of the user that invoked this command. This
9b7f02
+	 * is really hit or miss because there are so many ways that command
9b7f02
+	 * can be executed and so many ways to trip up the routines that
9b7f02
+	 * report the user name.
9b7f02
+	 */
9b7f02
+	pw = get_my_pwent ();
9b7f02
+	if (NULL == pw) {
9b7f02
+		fprintf (stderr, _("%s: Cannot determine your user name.\n"),
9b7f02
+		         Prog);
9b7f02
+		SYSLOG ((LOG_WARN,
9b7f02
+		         "Cannot determine the user name of the caller (UID %lu)",
9b7f02
+		         (unsigned long) getuid ()));
9b7f02
+		exit (E_NOPERM);
9b7f02
+	}
9b7f02
+	myname = xstrdup (pw->pw_name);
9b7f02
+
9b7f02
 	check_perms ();
9b7f02
 
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+	selinux_check_root ();
9b7f02
+#endif
9b7f02
+
9b7f02
 #ifdef SHADOWGRP
9b7f02
 	is_shadow_grp = sgr_file_present ();
9b7f02
 #endif
b95810
@@ -536,6 +629,15 @@ int main (int argc, char **argv)
9b7f02
 			newgr.gr_passwd = cp;
9b7f02
 		}
9b7f02
 
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+		{
9b7f02
+
9b7f02
+			audit_logger_with_group (AUDIT_GRP_CHAUTHTOK, Prog,
9b7f02
+		              "change-password",
9b7f02
+		              myname, AUDIT_NO_ID, gr->gr_name,
9b7f02
+		              SHADOW_AUDIT_SUCCESS);
9b7f02
+		}
9b7f02
+#endif
9b7f02
 		/* 
9b7f02
 		 * The updated group file entry is then put back and will
9b7f02
 		 * be written to the group file later, after all the
b95810
Index: shadow-4.5/src/chpasswd.c
b95810
===================================================================
b95810
--- shadow-4.5.orig/src/chpasswd.c
b95810
+++ shadow-4.5/src/chpasswd.c
9b7f02
@@ -39,6 +39,13 @@
9b7f02
 #include <pwd.h>
9b7f02
 #include <stdio.h>
9b7f02
 #include <stdlib.h>
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+#include <selinux/selinux.h>
9b7f02
+#include <selinux/avc.h>
9b7f02
+#endif
9b7f02
+#ifdef WITH_LIBAUDIT
9b7f02
+#include <libaudit.h>
9b7f02
+#endif
9b7f02
 #ifdef USE_PAM
9b7f02
 #include "pam_defs.h"
9b7f02
 #endif				/* USE_PAM */
9b7f02
@@ -297,6 +304,63 @@ static void check_perms (void)
9b7f02
 #endif				/* USE_PAM */
9b7f02
 }
9b7f02
 
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+static int
9b7f02
+log_callback (int type, const char *fmt, ...)
9b7f02
+{
9b7f02
+    int audit_fd;
9b7f02
+    va_list ap;
9b7f02
+
9b7f02
+    va_start(ap, fmt);
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+    audit_fd = audit_open();
9b7f02
+
9b7f02
+    if (audit_fd >= 0) {
9b7f02
+	char *buf;
9b7f02
+
9b7f02
+	if (vasprintf (&buf, fmt, ap) < 0)
9b7f02
+		goto ret;
9b7f02
+	audit_log_user_avc_message(audit_fd, AUDIT_USER_AVC, buf, NULL, NULL,
9b7f02
+				   NULL, 0);
9b7f02
+	audit_close(audit_fd);
9b7f02
+	free(buf);
9b7f02
+	goto ret;
9b7f02
+    }
9b7f02
+
9b7f02
+#endif
9b7f02
+    vsyslog (LOG_USER | LOG_INFO, fmt, ap);
9b7f02
+ret:
9b7f02
+    va_end(ap);
9b7f02
+    return 0;
9b7f02
+}
9b7f02
+
9b7f02
+static void
9b7f02
+selinux_check_root (void)
9b7f02
+{
9b7f02
+    int status = -1;
9b7f02
+    security_context_t user_context;
9b7f02
+    union selinux_callback old_callback;
9b7f02
+
9b7f02
+    if (is_selinux_enabled() < 1)
9b7f02
+	return;
9b7f02
+
9b7f02
+    old_callback = selinux_get_callback(SELINUX_CB_LOG);
9b7f02
+    /* setup callbacks */
9b7f02
+    selinux_set_callback(SELINUX_CB_LOG, (union selinux_callback) &log_callback);
9b7f02
+    if ((status = getprevcon(&user_context)) < 0) {
9b7f02
+	selinux_set_callback(SELINUX_CB_LOG, old_callback);
9b7f02
+	exit(1);
9b7f02
+    }
9b7f02
+
9b7f02
+    status = selinux_check_access(user_context, user_context, "passwd", "passwd", NULL);
9b7f02
+
9b7f02
+    selinux_set_callback(SELINUX_CB_LOG, old_callback);
9b7f02
+    freecon(user_context);
9b7f02
+    if (status != 0 && security_getenforce() != 0)
9b7f02
+	exit(1);
9b7f02
+}
9b7f02
+#endif
9b7f02
+
9b7f02
 /*
9b7f02
  * open_files - lock and open the password databases
9b7f02
  */
9b7f02
@@ -405,8 +469,16 @@ int main (int argc, char **argv)
9b7f02
 
9b7f02
 	OPENLOG ("chpasswd");
9b7f02
 
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+	audit_help_open ();
9b7f02
+#endif
9b7f02
+
9b7f02
 	check_perms ();
9b7f02
 
9b7f02
+#ifdef WITH_SELINUX
9b7f02
+	selinux_check_root ();
9b7f02
+#endif
9b7f02
+
9b7f02
 #ifdef USE_PAM
9b7f02
 	if (!use_pam)
9b7f02
 #endif				/* USE_PAM */
b95810
@@ -566,6 +638,11 @@ int main (int argc, char **argv)
9b7f02
 			newpw.pw_passwd = cp;
9b7f02
 		}
9b7f02
 
9b7f02
+#ifdef WITH_AUDIT
9b7f02
+		audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
9b7f02
+		              "updating-password",
9b7f02
+		              pw->pw_name, (unsigned int) pw->pw_uid, 1);
9b7f02
+#endif
9b7f02
 		/* 
9b7f02
 		 * The updated password file entry is then put back and will
9b7f02
 		 * be written to the password file later, after all the
b95810
Index: shadow-4.5/src/Makefile.am
b95810
===================================================================
b95810
--- shadow-4.5.orig/src/Makefile.am
b95810
+++ shadow-4.5/src/Makefile.am
b95810
@@ -87,9 +87,9 @@ chage_LDADD    = $(LDADD) $(LIBPAM_SUID)
b95810
 newuidmap_LDADD    = $(LDADD) $(LIBSELINUX)
b95810
 newgidmap_LDADD    = $(LDADD) $(LIBSELINUX)
9b7f02
 chfn_LDADD     = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT_NOPAM) $(LIBSKEY) $(LIBMD)
9b7f02
-chgpasswd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBSELINUX) $(LIBCRYPT)
9b7f02
+chgpasswd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBSELINUX) $(LIBAUDIT) $(LIBCRYPT)
9b7f02
 chsh_LDADD     = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT_NOPAM) $(LIBSKEY) $(LIBMD)
9b7f02
-chpasswd_LDADD = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBCRYPT)
9b7f02
+chpasswd_LDADD = $(LDADD) $(LIBPAM) $(LIBSELINUX) $(LIBAUDIT) $(LIBCRYPT)
9b7f02
 gpasswd_LDADD  = $(LDADD) $(LIBAUDIT) $(LIBSELINUX) $(LIBCRYPT)
9b7f02
 groupadd_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBAUDIT) $(LIBSELINUX)
9b7f02
 groupdel_LDADD = $(LDADD) $(LIBPAM_SUID) $(LIBAUDIT) $(LIBSELINUX)