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

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