Blame SOURCES/pam-1.1.8-pwhistory-helper.patch

904b9f
diff --git a/modules/pam_pwhistory/Makefile.am b/modules/pam_pwhistory/Makefile.am
0ec004
index 4bb4d6d..9157b91 100644
904b9f
--- a/modules/pam_pwhistory/Makefile.am
904b9f
+++ b/modules/pam_pwhistory/Makefile.am
904b9f
@@ -1,5 +1,6 @@
904b9f
 #
904b9f
 # Copyright (c) 2008, 2009 Thorsten Kukuk <kukuk@suse.de>
904b9f
+# Copyright (c) 2013 Red Hat, Inc.
904b9f
 #
904b9f
 
904b9f
 CLEANFILES = *~
904b9f
@@ -9,25 +10,33 @@ EXTRA_DIST = README $(MANS) $(XMLS) tst-pam_pwhistory
904b9f
 
904b9f
 TESTS = tst-pam_pwhistory
904b9f
 
904b9f
-man_MANS = pam_pwhistory.8
904b9f
+man_MANS = pam_pwhistory.8 pwhistory_helper.8
904b9f
 
904b9f
-XMLS = README.xml pam_pwhistory.8.xml
904b9f
+XMLS = README.xml pam_pwhistory.8.xml pwhistory_helper.8.xml
904b9f
 
904b9f
 securelibdir = $(SECUREDIR)
904b9f
 secureconfdir = $(SCONFIGDIR)
904b9f
 
904b9f
-AM_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include
904b9f
-AM_LDFLAGS = -no-undefined -avoid-version -module
904b9f
+AM_CFLAGS = -I$(top_srcdir)/libpam/include -I$(top_srcdir)/libpamc/include \
904b9f
+	    -DPWHISTORY_HELPER=\"$(sbindir)/pwhistory_helper\"
904b9f
+
904b9f
+pam_pwhistory_la_LDFLAGS = -no-undefined -avoid-version -module
904b9f
 if HAVE_VERSIONING
904b9f
-  AM_LDFLAGS += -Wl,--version-script=$(srcdir)/../modules.map
904b9f
+  pam_pwhistory_la_LDFLAGS += -Wl,--version-script=$(srcdir)/../modules.map
904b9f
 endif
904b9f
 
904b9f
 noinst_HEADERS = opasswd.h
904b9f
 
904b9f
 securelib_LTLIBRARIES = pam_pwhistory.la
0ec004
-pam_pwhistory_la_LIBADD = $(top_builddir)/libpam/libpam.la @LIBCRYPT@
904b9f
+pam_pwhistory_la_CFLAGS = $(AM_CFLAGS)
0ec004
+pam_pwhistory_la_LIBADD = $(top_builddir)/libpam/libpam.la @LIBCRYPT@ @LIBSELINUX@
904b9f
 pam_pwhistory_la_SOURCES = pam_pwhistory.c opasswd.c
904b9f
 
904b9f
+sbin_PROGRAMS = pwhistory_helper
904b9f
+pwhistory_helper_CFLAGS = $(AM_CFLAGS) -DHELPER_COMPILE=\"pwhistory_helper\"
904b9f
+pwhistory_helper_SOURCES = pwhistory_helper.c opasswd.c
904b9f
+pwhistory_helper_LDADD = $(top_builddir)/libpam/libpam.la @LIBCRYPT@
904b9f
+
904b9f
 if ENABLE_REGENERATE_MAN
904b9f
 noinst_DATA = README
904b9f
 README: pam_pwhistory.8.xml
904b9f
diff --git a/modules/pam_pwhistory/opasswd.c b/modules/pam_pwhistory/opasswd.c
0ec004
index 836d713..e319ff3 100644
904b9f
--- a/modules/pam_pwhistory/opasswd.c
904b9f
+++ b/modules/pam_pwhistory/opasswd.c
0ec004
@@ -1,5 +1,6 @@
0ec004
 /*
0ec004
  * Copyright (c) 2008 Thorsten Kukuk <kukuk@suse.de>
0ec004
+ * Copyright (c) 2013 Red Hat, Inc.
0ec004
  *
0ec004
  * Redistribution and use in source and binary forms, with or without
0ec004
  * modification, are permitted provided that the following conditions
0ec004
@@ -38,6 +39,7 @@
904b9f
 #endif
904b9f
 
904b9f
 #include <pwd.h>
904b9f
+#include <shadow.h>
904b9f
 #include <time.h>
904b9f
 #include <ctype.h>
904b9f
 #include <errno.h>
0ec004
@@ -47,6 +49,7 @@
904b9f
 #include <string.h>
904b9f
 #include <stdlib.h>
904b9f
 #include <syslog.h>
904b9f
+#include <stdarg.h>
904b9f
 #include <sys/stat.h>
904b9f
 
904b9f
 #if defined (HAVE_XCRYPT_H)
0ec004
@@ -55,7 +58,14 @@
904b9f
 #include <crypt.h>
904b9f
 #endif
904b9f
 
904b9f
+#ifdef HELPER_COMPILE
904b9f
+#define pam_modutil_getpwnam(h,n) getpwnam(n)
904b9f
+#define pam_modutil_getspnam(h,n) getspnam(n)
904b9f
+#define pam_syslog(h,a,...) helper_log_err(a,__VA_ARGS__)
904b9f
+#else
904b9f
+#include <security/pam_modutil.h>
904b9f
 #include <security/pam_ext.h>
904b9f
+#endif
904b9f
 #include <security/pam_modules.h>
904b9f
 
904b9f
 #include "opasswd.h"
0ec004
@@ -76,6 +86,19 @@ typedef struct {
904b9f
   char *old_passwords;
904b9f
 } opwd;
904b9f
 
904b9f
+#ifdef HELPER_COMPILE
904b9f
+void
904b9f
+helper_log_err(int err, const char *format, ...)
904b9f
+{
904b9f
+  va_list args;
904b9f
+
904b9f
+  va_start(args, format);
904b9f
+  openlog(HELPER_COMPILE, LOG_CONS | LOG_PID, LOG_AUTHPRIV);
904b9f
+  vsyslog(err, format, args);
904b9f
+  va_end(args);
904b9f
+  closelog();
904b9f
+}
904b9f
+#endif
904b9f
 
904b9f
 static int
904b9f
 parse_entry (char *line, opwd *data)
0ec004
@@ -112,8 +135,8 @@ compare_password(const char *newpass, const char *oldpass)
904b9f
 }
904b9f
 
904b9f
 /* Check, if the new password is already in the opasswd file.  */
904b9f
-int
904b9f
-check_old_pass (pam_handle_t *pamh, const char *user,
904b9f
+PAMH_ARG_DECL(int
904b9f
+check_old_pass, const char *user,
904b9f
 		const char *newpass, int debug)
904b9f
 {
904b9f
   int retval = PAM_SUCCESS;
0ec004
@@ -123,6 +146,11 @@ check_old_pass (pam_handle_t *pamh, const char *user,
0ec004
   opwd entry;
0ec004
   int found = 0;
0ec004
 
0ec004
+#ifndef HELPER_COMPILE
0ec004
+  if (SELINUX_ENABLED)
0ec004
+    return PAM_PWHISTORY_RUN_HELPER;
0ec004
+#endif
0ec004
+
0ec004
   if ((oldpf = fopen (OLD_PASSWORDS_FILE, "r")) == NULL)
0ec004
     {
0ec004
       if (errno != ENOENT)
0ec004
@@ -208,9 +236,9 @@ check_old_pass (pam_handle_t *pamh, const char *user,
904b9f
   return retval;
904b9f
 }
904b9f
 
904b9f
-int
904b9f
-save_old_pass (pam_handle_t *pamh, const char *user, uid_t uid,
904b9f
-	       const char *oldpass, int howmany, int debug UNUSED)
904b9f
+PAMH_ARG_DECL(int
904b9f
+save_old_pass, const char *user,
904b9f
+	       int howmany, int debug UNUSED)
904b9f
 {
904b9f
   char opasswd_tmp[] = TMP_PASSWORDS_FILE;
904b9f
   struct stat opasswd_stat;
0ec004
@@ -221,10 +249,35 @@ save_old_pass (pam_handle_t *pamh, const char *user, uid_t uid,
904b9f
   char *buf = NULL;
904b9f
   size_t buflen = 0;
904b9f
   int found = 0;
904b9f
+  struct passwd *pwd;
904b9f
+  const char *oldpass;
904b9f
+
904b9f
+  pwd = pam_modutil_getpwnam (pamh, user);
904b9f
+  if (pwd == NULL)
904b9f
+    return PAM_USER_UNKNOWN;
904b9f
 
904b9f
   if (howmany <= 0)
904b9f
     return PAM_SUCCESS;
904b9f
 
0ec004
+#ifndef HELPER_COMPILE
0ec004
+  if (SELINUX_ENABLED)
0ec004
+    return PAM_PWHISTORY_RUN_HELPER;
0ec004
+#endif
0ec004
+
904b9f
+  if ((strcmp(pwd->pw_passwd, "x") == 0)  ||
904b9f
+      ((pwd->pw_passwd[0] == '#') &&
904b9f
+       (pwd->pw_passwd[1] == '#') &&
904b9f
+       (strcmp(pwd->pw_name, pwd->pw_passwd + 2) == 0)))
904b9f
+    {
904b9f
+      struct spwd *spw = pam_modutil_getspnam (pamh, user);
904b9f
+
904b9f
+      if (spw == NULL)
904b9f
+        return PAM_USER_UNKNOWN;
904b9f
+      oldpass = spw->sp_pwdp;
904b9f
+    }
904b9f
+  else
904b9f
+      oldpass = pwd->pw_passwd;
904b9f
+
904b9f
   if (oldpass == NULL || *oldpass == '\0')
904b9f
     return PAM_SUCCESS;
904b9f
 
0ec004
@@ -447,7 +500,7 @@ save_old_pass (pam_handle_t *pamh, const char *user, uid_t uid,
904b9f
     {
904b9f
       char *out;
904b9f
 
904b9f
-      if (asprintf (&out, "%s:%d:1:%s\n", user, uid, oldpass) < 0)
904b9f
+      if (asprintf (&out, "%s:%d:1:%s\n", user, pwd->pw_uid, oldpass) < 0)
904b9f
 	{
904b9f
 	  retval = PAM_AUTHTOK_ERR;
904b9f
 	  if (oldpf)
904b9f
diff --git a/modules/pam_pwhistory/opasswd.h b/modules/pam_pwhistory/opasswd.h
0ec004
index db3e656..1b08699 100644
904b9f
--- a/modules/pam_pwhistory/opasswd.h
904b9f
+++ b/modules/pam_pwhistory/opasswd.h
0ec004
@@ -1,5 +1,6 @@
0ec004
 /*
0ec004
  * Copyright (c) 2008 Thorsten Kukuk <kukuk@suse.de>
0ec004
+ * Copyright (c) 2013 Red Hat, Inc.
0ec004
  *
0ec004
  * Redistribution and use in source and binary forms, with or without
0ec004
  * modification, are permitted provided that the following conditions
0ec004
@@ -36,10 +37,32 @@
904b9f
 #ifndef __OPASSWD_H__
904b9f
 #define __OPASSWD_H__
904b9f
 
904b9f
-extern int check_old_pass (pam_handle_t *pamh, const char *user,
904b9f
-			   const char *newpass, int debug);
904b9f
-extern int save_old_pass (pam_handle_t *pamh, const char *user,
904b9f
-			  uid_t uid, const char *oldpass,
904b9f
-			  int howmany, int debug);
904b9f
+#define PAM_PWHISTORY_RUN_HELPER PAM_CRED_INSUFFICIENT
904b9f
+
0ec004
+#ifdef WITH_SELINUX
0ec004
+#include <selinux/selinux.h>
0ec004
+#define SELINUX_ENABLED is_selinux_enabled()>0
0ec004
+#else
0ec004
+#define SELINUX_ENABLED 0
0ec004
+#endif
0ec004
+
904b9f
+#ifdef HELPER_COMPILE
904b9f
+#define PAMH_ARG_DECL(fname, ...) fname(__VA_ARGS__)
904b9f
+#define PAMH_ARG(...)               __VA_ARGS__
904b9f
+#else
904b9f
+#define PAMH_ARG_DECL(fname, ...) fname(pam_handle_t *pamh, __VA_ARGS__)
904b9f
+#define PAMH_ARG(...)               pamh, __VA_ARGS__
904b9f
+#endif
904b9f
+
904b9f
+#ifdef HELPER_COMPILE
904b9f
+void
904b9f
+helper_log_err(int err, const char *format, ...);
904b9f
+#endif
904b9f
+
904b9f
+PAMH_ARG_DECL(int
904b9f
+check_old_pass, const char *user, const char *newpass, int debug);
904b9f
+
904b9f
+PAMH_ARG_DECL(int
904b9f
+save_old_pass, const char *user, int howmany, int debug);
904b9f
 
904b9f
 #endif /* __OPASSWD_H__ */
904b9f
diff --git a/modules/pam_pwhistory/pam_pwhistory.c b/modules/pam_pwhistory/pam_pwhistory.c
0ec004
index 654edd3..d6c5c47 100644
904b9f
--- a/modules/pam_pwhistory/pam_pwhistory.c
904b9f
+++ b/modules/pam_pwhistory/pam_pwhistory.c
0ec004
@@ -1,6 +1,7 @@
0ec004
 /*
0ec004
  * Copyright (c) 2008, 2012 Thorsten Kukuk
0ec004
  * Author: Thorsten Kukuk <kukuk@thkukuk.de>
0ec004
+ * Copyright (c) 2013 Red Hat, Inc.
0ec004
  *
0ec004
  * Redistribution and use in source and binary forms, with or without
0ec004
  * modification, are permitted provided that the following conditions
0ec004
@@ -46,10 +47,14 @@
904b9f
 #include <stdlib.h>
904b9f
 #include <string.h>
904b9f
 #include <unistd.h>
904b9f
-#include <shadow.h>
904b9f
 #include <syslog.h>
904b9f
 #include <sys/types.h>
904b9f
 #include <sys/stat.h>
904b9f
+#include <sys/time.h>
904b9f
+#include <sys/resource.h>
904b9f
+#include <sys/wait.h>
904b9f
+#include <signal.h>
904b9f
+#include <fcntl.h>
904b9f
 
904b9f
 #include <security/pam_modules.h>
904b9f
 #include <security/pam_modutil.h>
0ec004
@@ -59,6 +64,7 @@
904b9f
 #include "opasswd.h"
904b9f
 
904b9f
 #define DEFAULT_BUFLEN 2048
904b9f
+#define MAX_FD_NO 20000
904b9f
 
904b9f
 struct options_t {
904b9f
   int debug;
0ec004
@@ -102,6 +108,184 @@ parse_option (pam_handle_t *pamh, const char *argv, options_t *options)
904b9f
     pam_syslog (pamh, LOG_ERR, "pam_pwhistory: unknown option: %s", argv);
904b9f
 }
904b9f
 
904b9f
+static int
904b9f
+run_save_helper(pam_handle_t *pamh, const char *user,
904b9f
+		int howmany, int debug)
904b9f
+{
904b9f
+  int retval, child;
904b9f
+  struct sigaction newsa, oldsa;
904b9f
+
904b9f
+  memset(&newsa, '\0', sizeof(newsa));
904b9f
+  newsa.sa_handler = SIG_DFL;
904b9f
+  sigaction(SIGCHLD, &newsa, &oldsa);
904b9f
+
904b9f
+  child = fork();
904b9f
+  if (child == 0)
904b9f
+    {
904b9f
+      int i = 0;
904b9f
+      struct rlimit rlim;
904b9f
+      int dummyfds[2];
904b9f
+      static char *envp[] = { NULL };
904b9f
+      char *args[] = { NULL, NULL, NULL, NULL, NULL, NULL };
904b9f
+
904b9f
+      /* replace std file descriptors with a dummy pipe */
904b9f
+      if (pipe2(dummyfds, O_NONBLOCK) == 0)
904b9f
+        {
904b9f
+          dup2(dummyfds[0], STDIN_FILENO);
904b9f
+          dup2(dummyfds[1], STDOUT_FILENO);
904b9f
+          dup2(dummyfds[1], STDERR_FILENO);
904b9f
+        }
904b9f
+
904b9f
+      if (getrlimit(RLIMIT_NOFILE,&rlim) == 0)
904b9f
+        {
904b9f
+          if (rlim.rlim_max >= MAX_FD_NO)
904b9f
+            rlim.rlim_max = MAX_FD_NO;
904b9f
+	  for (i = STDERR_FILENO + 1; i < (int)rlim.rlim_max; i++)
904b9f
+             {
904b9f
+		if (i != dummyfds[0])
904b9f
+		  close(i);
904b9f
+	     }
904b9f
+	}
904b9f
+
904b9f
+      /* exec binary helper */
904b9f
+      args[0] = strdup(PWHISTORY_HELPER);
904b9f
+      args[1] = strdup("save");
904b9f
+      args[2] = x_strdup(user);
904b9f
+      asprintf(&args[3], "%d", howmany);
904b9f
+      asprintf(&args[4], "%d", debug);
904b9f
+
904b9f
+      execve(args[0], args, envp);
904b9f
+
904b9f
+      _exit(PAM_SYSTEM_ERR);
904b9f
+    }
904b9f
+  else if (child > 0)
904b9f
+    {
904b9f
+      /* wait for child */
904b9f
+      int rc = 0;
904b9f
+      rc = waitpid(child, &retval, 0);  /* wait for helper to complete */
904b9f
+      if (rc < 0)
904b9f
+        {
904b9f
+	  pam_syslog(pamh, LOG_ERR, "pwhistory_helper save waitpid returned %d: %m", rc);
904b9f
+	  retval = PAM_SYSTEM_ERR;
904b9f
+	}
904b9f
+      else if (!WIFEXITED(retval))
904b9f
+        {
904b9f
+	  pam_syslog(pamh, LOG_ERR, "pwhistory_helper save abnormal exit: %d", retval);
904b9f
+	  retval = PAM_SYSTEM_ERR;
904b9f
+	}
904b9f
+      else
904b9f
+        {
904b9f
+	  retval = WEXITSTATUS(retval);
904b9f
+	}
904b9f
+    } 
904b9f
+  else
904b9f
+    {
904b9f
+	retval = PAM_SYSTEM_ERR;
904b9f
+    }
904b9f
+
904b9f
+  sigaction(SIGCHLD, &oldsa, NULL);   /* restore old signal handler */
904b9f
+
904b9f
+  return retval;
904b9f
+}
904b9f
+
904b9f
+static int
904b9f
+run_check_helper(pam_handle_t *pamh, const char *user,
904b9f
+		 const char *newpass, int debug)
904b9f
+{
904b9f
+  int retval, child, fds[2];
904b9f
+  struct sigaction newsa, oldsa;
904b9f
+
904b9f
+  /* create a pipe for the password */
904b9f
+  if (pipe(fds) != 0)
904b9f
+    return PAM_SYSTEM_ERR;
904b9f
+
904b9f
+  memset(&newsa, '\0', sizeof(newsa));
904b9f
+  newsa.sa_handler = SIG_DFL;
904b9f
+  sigaction(SIGCHLD, &newsa, &oldsa);
904b9f
+
904b9f
+  child = fork();
904b9f
+  if (child == 0)
904b9f
+    {
904b9f
+      int i = 0;
904b9f
+      struct rlimit rlim;
904b9f
+      int dummyfds[2];
904b9f
+      static char *envp[] = { NULL };
904b9f
+      char *args[] = { NULL, NULL, NULL, NULL, NULL };
904b9f
+
904b9f
+      /* reopen stdin as pipe */
904b9f
+      dup2(fds[0], STDIN_FILENO);
904b9f
+
904b9f
+      /* replace std file descriptors with a dummy pipe */
904b9f
+      if (pipe2(dummyfds, O_NONBLOCK) == 0)
904b9f
+        {
904b9f
+          dup2(dummyfds[1], STDOUT_FILENO);
904b9f
+          dup2(dummyfds[1], STDERR_FILENO);
904b9f
+        }
904b9f
+
904b9f
+      if (getrlimit(RLIMIT_NOFILE,&rlim) == 0)
904b9f
+        {
904b9f
+          if (rlim.rlim_max >= MAX_FD_NO)
904b9f
+            rlim.rlim_max = MAX_FD_NO;
904b9f
+	  for (i = STDERR_FILENO + 1; i < (int)rlim.rlim_max; i++)
904b9f
+             {
904b9f
+		if (i != dummyfds[0])
904b9f
+		  close(i);
904b9f
+	     }
904b9f
+	}
904b9f
+
904b9f
+      /* exec binary helper */
904b9f
+      args[0] = strdup(PWHISTORY_HELPER);
904b9f
+      args[1] = strdup("check");
904b9f
+      args[2] = x_strdup(user);
904b9f
+      asprintf(&args[3], "%d", debug);
904b9f
+
904b9f
+      execve(args[0], args, envp);
904b9f
+
904b9f
+      _exit(PAM_SYSTEM_ERR);
904b9f
+    }
904b9f
+  else if (child > 0)
904b9f
+    {
904b9f
+      /* wait for child */
904b9f
+      int rc = 0;
904b9f
+      if (newpass == NULL)
904b9f
+        newpass = "";
904b9f
+ 
904b9f
+      /* send the password to the child */
904b9f
+      if (write(fds[1], newpass, strlen(newpass)+1) == -1)
904b9f
+        {
904b9f
+	  pam_syslog(pamh, LOG_ERR, "Cannot send password to helper: %m");
904b9f
+	  retval = PAM_SYSTEM_ERR;
904b9f
+	}
904b9f
+      newpass = NULL;
904b9f
+      close(fds[0]);       /* close here to avoid possible SIGPIPE above */
904b9f
+      close(fds[1]);
904b9f
+      rc = waitpid(child, &retval, 0);  /* wait for helper to complete */
904b9f
+      if (rc < 0)
904b9f
+        {
904b9f
+	  pam_syslog(pamh, LOG_ERR, "pwhistory_helper check waitpid returned %d: %m", rc);
904b9f
+	  retval = PAM_SYSTEM_ERR;
904b9f
+	}
904b9f
+      else if (!WIFEXITED(retval))
904b9f
+        {
904b9f
+	  pam_syslog(pamh, LOG_ERR, "pwhistory_helper check abnormal exit: %d", retval);
904b9f
+	  retval = PAM_SYSTEM_ERR;
904b9f
+	}
904b9f
+      else
904b9f
+        {
904b9f
+	  retval = WEXITSTATUS(retval);
904b9f
+	}
904b9f
+    } 
904b9f
+  else
904b9f
+    {
904b9f
+	close(fds[0]);
904b9f
+	close(fds[1]);
904b9f
+	retval = PAM_SYSTEM_ERR;
904b9f
+    }
904b9f
+
904b9f
+  sigaction(SIGCHLD, &oldsa, NULL);   /* restore old signal handler */
904b9f
+
904b9f
+  return retval;
904b9f
+}
904b9f
 
904b9f
 /* This module saves the current crypted password in /etc/security/opasswd
904b9f
    and then compares the new password with all entries in this file. */
0ec004
@@ -109,7 +293,6 @@ parse_option (pam_handle_t *pamh, const char *argv, options_t *options)
904b9f
 PAM_EXTERN int
904b9f
 pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc, const char **argv)
904b9f
 {
904b9f
-  struct passwd *pwd;
904b9f
   const char *newpass;
904b9f
   const char *user;
904b9f
     int retval, tries;
0ec004
@@ -154,31 +337,13 @@ pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc, const char **argv)
904b9f
       return PAM_SUCCESS;
904b9f
     }
904b9f
 
904b9f
-  pwd = pam_modutil_getpwnam (pamh, user);
904b9f
-  if (pwd == NULL)
904b9f
-    return PAM_USER_UNKNOWN;
904b9f
+  retval = save_old_pass (pamh, user, options.remember, options.debug);
904b9f
 
904b9f
-  if ((strcmp(pwd->pw_passwd, "x") == 0)  ||
904b9f
-      ((pwd->pw_passwd[0] == '#') &&
904b9f
-       (pwd->pw_passwd[1] == '#') &&
904b9f
-       (strcmp(pwd->pw_name, pwd->pw_passwd + 2) == 0)))
904b9f
-    {
904b9f
-      struct spwd *spw = pam_modutil_getspnam (pamh, user);
904b9f
-      if (spw == NULL)
904b9f
-	return PAM_USER_UNKNOWN;
904b9f
+  if (retval == PAM_PWHISTORY_RUN_HELPER) 
904b9f
+      retval = run_save_helper(pamh, user, options.remember, options.debug);
904b9f
 
904b9f
-      retval = save_old_pass (pamh, user, pwd->pw_uid, spw->sp_pwdp,
904b9f
-			      options.remember, options.debug);
904b9f
-      if (retval != PAM_SUCCESS)
904b9f
-	return retval;
904b9f
-    }
904b9f
-  else
904b9f
-    {
904b9f
-      retval = save_old_pass (pamh, user, pwd->pw_uid, pwd->pw_passwd,
904b9f
-			      options.remember, options.debug);
904b9f
-      if (retval != PAM_SUCCESS)
904b9f
-	return retval;
904b9f
-    }
904b9f
+  if (retval != PAM_SUCCESS)
904b9f
+    return retval;
904b9f
 
904b9f
   newpass = NULL;
904b9f
   tries = 0;
0ec004
@@ -207,8 +372,11 @@ pam_sm_chauthtok (pam_handle_t *pamh, int flags, int argc, const char **argv)
904b9f
       if (options.debug)
904b9f
 	pam_syslog (pamh, LOG_DEBUG, "check against old password file");
904b9f
 
904b9f
-      if (check_old_pass (pamh, user, newpass,
904b9f
-			  options.debug) != PAM_SUCCESS)
904b9f
+      retval = check_old_pass (pamh, user, newpass, options.debug);
904b9f
+      if (retval == PAM_PWHISTORY_RUN_HELPER)
904b9f
+	  retval = run_check_helper(pamh, user, newpass, options.debug);
904b9f
+
904b9f
+      if (retval != PAM_SUCCESS)
904b9f
 	{
904b9f
 	  if (getuid() || options.enforce_for_root ||
904b9f
 	      (flags & PAM_CHANGE_EXPIRED_AUTHTOK))
904b9f
diff --git a/modules/pam_pwhistory/pwhistory_helper.8.xml b/modules/pam_pwhistory/pwhistory_helper.8.xml
904b9f
new file mode 100644
0ec004
index 0000000..a030176
904b9f
--- /dev/null
904b9f
+++ b/modules/pam_pwhistory/pwhistory_helper.8.xml
904b9f
@@ -0,0 +1,68 @@
904b9f
+
904b9f
+
904b9f
+	"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
904b9f
+
904b9f
+<refentry id="pwhistory_helper">
904b9f
+
904b9f
+  <refmeta>
904b9f
+    <refentrytitle>pwhistory_helper</refentrytitle>
904b9f
+    <manvolnum>8</manvolnum>
904b9f
+    <refmiscinfo class="sectdesc">Linux-PAM Manual</refmiscinfo>
904b9f
+  </refmeta>
904b9f
+
904b9f
+  <refnamediv id="pwhistory_helper-name">
904b9f
+    <refname>pwhistory_helper</refname>
904b9f
+    <refpurpose>Helper binary that transfers password hashes from passwd or shadow to opasswd</refpurpose>
904b9f
+  </refnamediv>
904b9f
+
904b9f
+  <refsynopsisdiv>
904b9f
+    <cmdsynopsis id="pwhistory_helper-cmdsynopsis">
904b9f
+      <command>pwhistory_helper</command>
904b9f
+      <arg choice="opt">
904b9f
+        ...
904b9f
+      </arg>
904b9f
+    </cmdsynopsis>
904b9f
+  </refsynopsisdiv>
904b9f
+
904b9f
+  <refsect1 id="pwhistory_helper-description">
904b9f
+
904b9f
+    <title>DESCRIPTION</title>
904b9f
+
904b9f
+    <para>
904b9f
+      <emphasis>pwhistory_helper</emphasis> is a helper program for the
904b9f
+      <emphasis>pam_pwhistory</emphasis> module that transfers password hashes
904b9f
+      from passwd or shadow file to the opasswd file and checks a password
904b9f
+      supplied by user against the existing hashes in the opasswd file.
904b9f
+    </para>
904b9f
+
904b9f
+    <para>
904b9f
+      The purpose of the helper is to enable tighter confinement of
904b9f
+      login and password changing services. The helper is thus called only
904b9f
+      when SELinux is enabled on the system.
904b9f
+    </para>
904b9f
+
904b9f
+    <para>
904b9f
+      The interface of the helper - command line options, and input/output
904b9f
+      data format are internal to the <emphasis>pam_pwhistory</emphasis>
904b9f
+      module and it should not be called directly from applications.
904b9f
+    </para>
904b9f
+  </refsect1>
904b9f
+
904b9f
+  <refsect1 id='pwhistory_helper-see_also'>
904b9f
+    <title>SEE ALSO</title>
904b9f
+    <para>
904b9f
+      <citerefentry>
904b9f
+	<refentrytitle>pam_pwhistory</refentrytitle><manvolnum>8</manvolnum>
904b9f
+      </citerefentry>
904b9f
+    </para>
904b9f
+  </refsect1>
904b9f
+
904b9f
+  <refsect1 id='pwhistory_helper-author'>
904b9f
+    <title>AUTHOR</title>
904b9f
+      <para>
904b9f
+        Written by Tomas Mraz based on the code originally in
0ec004
+        <emphasis>pam_pwhistory and pam_unix</emphasis> modules.
904b9f
+      </para>
904b9f
+  </refsect1>
904b9f
+
904b9f
+</refentry>
904b9f
diff --git a/modules/pam_pwhistory/pwhistory_helper.c b/modules/pam_pwhistory/pwhistory_helper.c
904b9f
new file mode 100644
904b9f
index 0000000..b07ab81
904b9f
--- /dev/null
904b9f
+++ b/modules/pam_pwhistory/pwhistory_helper.c
904b9f
@@ -0,0 +1,209 @@
904b9f
+/* 
904b9f
+ * Copyright (c) 2013 Red Hat, Inc.
904b9f
+ * Author: Tomas Mraz <tmraz@redhat.com>
904b9f
+ *
904b9f
+ * Redistribution and use in source and binary forms, with or without
904b9f
+ * modification, are permitted provided that the following conditions
904b9f
+ * are met:
904b9f
+ * 1. Redistributions of source code must retain the above copyright
904b9f
+ *    notice, and the entire permission notice in its entirety,
904b9f
+ *    including the disclaimer of warranties.
904b9f
+ * 2. Redistributions in binary form must reproduce the above copyright
904b9f
+ *    notice, this list of conditions and the following disclaimer in the
904b9f
+ *    documentation and/or other materials provided with the distribution.
904b9f
+ * 3. The name of the author may not be used to endorse or promote
904b9f
+ *    products derived from this software without specific prior
904b9f
+ *    written permission.
904b9f
+ *
904b9f
+ * ALTERNATIVELY, this product may be distributed under the terms of
904b9f
+ * the GNU Public License, in which case the provisions of the GPL are
904b9f
+ * required INSTEAD OF the above restrictions.  (This clause is
904b9f
+ * necessary due to a potential bad interaction between the GPL and
904b9f
+ * the restrictions contained in a BSD-style copyright.)
904b9f
+ *
904b9f
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
904b9f
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
904b9f
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
904b9f
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
904b9f
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
904b9f
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
904b9f
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
904b9f
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
904b9f
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
904b9f
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
904b9f
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
904b9f
+ */
904b9f
+
904b9f
+#include "config.h"
904b9f
+
904b9f
+#include <stdio.h>
904b9f
+#include <stdlib.h>
904b9f
+#include <string.h>
904b9f
+#include <syslog.h>
904b9f
+#include <errno.h>
904b9f
+#include <unistd.h>
904b9f
+#include <signal.h>
904b9f
+#include <security/_pam_types.h>
904b9f
+#include <security/_pam_macros.h>
904b9f
+#include "opasswd.h"
904b9f
+
904b9f
+#define MAXPASS 200
904b9f
+
904b9f
+static void
904b9f
+su_sighandler(int sig)
904b9f
+{
904b9f
+#ifndef SA_RESETHAND
904b9f
+        /* emulate the behaviour of the SA_RESETHAND flag */
904b9f
+        if ( sig == SIGILL || sig == SIGTRAP || sig == SIGBUS || sig = SIGSERV ) {
904b9f
+		struct sigaction sa;
904b9f
+		memset(&sa, '\0', sizeof(sa));
904b9f
+		sa.sa_handler = SIG_DFL;
904b9f
+                sigaction(sig, &sa, NULL);
904b9f
+	}
904b9f
+#endif
904b9f
+        if (sig > 0) {
904b9f
+                _exit(sig);
904b9f
+        }
904b9f
+}
904b9f
+
904b9f
+static void
904b9f
+setup_signals(void)
904b9f
+{
904b9f
+  struct sigaction action;        /* posix signal structure */
904b9f
+         
904b9f
+  /*
904b9f
+   * Setup signal handlers
904b9f
+   */
904b9f
+  (void) memset((void *) &action, 0, sizeof(action));
904b9f
+  action.sa_handler = su_sighandler;
904b9f
+#ifdef SA_RESETHAND
904b9f
+  action.sa_flags = SA_RESETHAND;
904b9f
+#endif
904b9f
+  (void) sigaction(SIGILL, &action, NULL);
904b9f
+  (void) sigaction(SIGTRAP, &action, NULL);
904b9f
+  (void) sigaction(SIGBUS, &action, NULL);
904b9f
+  (void) sigaction(SIGSEGV, &action, NULL);
904b9f
+  action.sa_handler = SIG_IGN;
904b9f
+  action.sa_flags = 0;
904b9f
+  (void) sigaction(SIGTERM, &action, NULL);
904b9f
+  (void) sigaction(SIGHUP, &action, NULL);
904b9f
+  (void) sigaction(SIGINT, &action, NULL);
904b9f
+  (void) sigaction(SIGQUIT, &action, NULL);
904b9f
+}
904b9f
+
904b9f
+static int
904b9f
+read_passwords(int fd, int npass, char **passwords)
904b9f
+{
904b9f
+  int rbytes = 0;
904b9f
+  int offset = 0;
904b9f
+  int i = 0;
904b9f
+  char *pptr;
904b9f
+  while (npass > 0)
904b9f
+    {
904b9f
+      rbytes = read(fd, passwords[i]+offset, MAXPASS-offset);
904b9f
+
904b9f
+      if (rbytes < 0)
904b9f
+        {
904b9f
+          if (errno == EINTR) continue;
904b9f
+          break;
904b9f
+        }
904b9f
+      if (rbytes == 0)
904b9f
+          break;
904b9f
+
904b9f
+      while (npass > 0 && (pptr=memchr(passwords[i]+offset, '\0', rbytes))
904b9f
+             != NULL)
904b9f
+        {
904b9f
+          rbytes -= pptr - (passwords[i]+offset) + 1;
904b9f
+          i++;
904b9f
+          offset = 0;
904b9f
+          npass--;
904b9f
+          if (rbytes > 0)
904b9f
+            {
904b9f
+              if (npass > 0)
904b9f
+                memcpy(passwords[i], pptr+1, rbytes);
904b9f
+              memset(pptr+1, '\0', rbytes);
904b9f
+            }
904b9f
+        }
904b9f
+      offset += rbytes;
904b9f
+    }
904b9f
+
904b9f
+    /* clear up */
904b9f
+    if (offset > 0 && npass > 0) 
904b9f
+      memset(passwords[i], '\0', offset);
904b9f
+
904b9f
+   return i;
904b9f
+}
904b9f
+
904b9f
+
904b9f
+static int
904b9f
+check_history(const char *user, const char *debug)
904b9f
+{
904b9f
+  char pass[MAXPASS + 1];
904b9f
+  char *passwords[] = { pass };
904b9f
+  int npass;
904b9f
+  int dbg = atoi(debug); /* no need to be too fancy here */
904b9f
+  int retval;
904b9f
+
904b9f
+  /* read the password from stdin (a pipe from the pam_pwhistory module) */
904b9f
+  npass = read_passwords(STDIN_FILENO, 1, passwords);
904b9f
+
904b9f
+  if (npass != 1)
904b9f
+    { /* is it a valid password? */
904b9f
+      helper_log_err(LOG_DEBUG, "no password supplied");
904b9f
+      return PAM_AUTHTOK_ERR;
904b9f
+    }
904b9f
+
904b9f
+  retval = check_old_pass(user, pass, dbg);
904b9f
+
904b9f
+  memset(pass, '\0', MAXPASS);	/* clear memory of the password */
904b9f
+
904b9f
+  return retval;
904b9f
+}
904b9f
+
904b9f
+static int
904b9f
+save_history(const char *user, const char *howmany, const char *debug)
904b9f
+{
904b9f
+  int num = atoi(howmany);
904b9f
+  int dbg = atoi(debug); /* no need to be too fancy here */
904b9f
+  int retval;
904b9f
+
904b9f
+  retval = save_old_pass(user, num, dbg);
904b9f
+
904b9f
+  return retval;
904b9f
+}
904b9f
+
904b9f
+int
904b9f
+main(int argc, char *argv[])
904b9f
+{
904b9f
+  const char *option;
904b9f
+  const char *user;
904b9f
+
904b9f
+  /*
904b9f
+   * Catch or ignore as many signal as possible.
904b9f
+   */
904b9f
+  setup_signals();
904b9f
+
904b9f
+  /*
904b9f
+   * we establish that this program is running with non-tty stdin.
904b9f
+   * this is to discourage casual use.
904b9f
+   */
904b9f
+
904b9f
+  if (isatty(STDIN_FILENO) || argc < 4)
904b9f
+    {
904b9f
+      fprintf(stderr,
904b9f
+		"This binary is not designed for running in this way.\n");
904b9f
+      sleep(10);	/* this should discourage/annoy the user */
904b9f
+      return PAM_SYSTEM_ERR;
904b9f
+    }
904b9f
+
904b9f
+  option = argv[1];
904b9f
+  user = argv[2];
904b9f
+
904b9f
+  if (strcmp(option, "check") == 0 && argc == 4)
904b9f
+    return check_history(user, argv[3]);
904b9f
+  else if (strcmp(option, "save") == 0 && argc == 5)
904b9f
+    return save_history(user, argv[3], argv[4]);
904b9f
+
904b9f
+  return PAM_SYSTEM_ERR;
904b9f
+}
904b9f
+