Blame SOURCES/krb5-1.12.1-pam.patch

963210
From 9a6c3d9c1f4286a1a17cd89a1225712606863da8 Mon Sep 17 00:00:00 2001
963210
From: Robbie Harwood <rharwood@redhat.com>
963210
Date: Mon, 18 Apr 2016 15:57:38 -0400
963210
Subject: [PATCH] krb5-1.12.1-pam.patch
963210
963210
---
167778
 src/aclocal.m4              |  67 +++++++
963210
 src/clients/ksu/Makefile.in |   8 +-
167778
 src/clients/ksu/main.c      |  88 +++++++-
167778
 src/clients/ksu/pam.c       | 389 ++++++++++++++++++++++++++++++++++++
167778
 src/clients/ksu/pam.h       |  57 ++++++
963210
 src/configure.in            |   2 +
963210
 6 files changed, 608 insertions(+), 3 deletions(-)
963210
 create mode 100644 src/clients/ksu/pam.c
963210
 create mode 100644 src/clients/ksu/pam.h
963210
963210
diff --git a/src/aclocal.m4 b/src/aclocal.m4
963210
index 9c46da4b5..508e5fe90 100644
963210
--- a/src/aclocal.m4
963210
+++ b/src/aclocal.m4
963210
@@ -1675,3 +1675,70 @@ AC_DEFUN(KRB5_AC_PERSISTENT_KEYRING,[
963210
       ]))
963210
 ])dnl
963210
 dnl
963210
+dnl
963210
+dnl Use PAM instead of local crypt() compare for checking local passwords,
963210
+dnl and perform PAM account, session management, and password-changing where
963210
+dnl appropriate.
963210
+dnl 
963210
+AC_DEFUN(KRB5_WITH_PAM,[
963210
+AC_ARG_WITH(pam,[AC_HELP_STRING(--with-pam,[compile with PAM support])],
963210
+	    withpam="$withval",withpam=auto)
963210
+AC_ARG_WITH(pam-ksu-service,[AC_HELP_STRING(--with-ksu-service,[PAM service name for ksu ["ksu"]])],
963210
+	    withksupamservice="$withval",withksupamservice=ksu)
963210
+old_LIBS="$LIBS"
963210
+if test "$withpam" != no ; then
963210
+	AC_MSG_RESULT([checking for PAM...])
963210
+	PAM_LIBS=
963210
+
963210
+	AC_CHECK_HEADERS(security/pam_appl.h)
963210
+	if test "x$ac_cv_header_security_pam_appl_h" != xyes ; then
963210
+		if test "$withpam" = auto ; then
963210
+			AC_MSG_RESULT([Unable to locate security/pam_appl.h.])
963210
+			withpam=no
963210
+		else
963210
+			AC_MSG_ERROR([Unable to locate security/pam_appl.h.])
963210
+		fi
963210
+	fi
963210
+
963210
+	LIBS=
963210
+	unset ac_cv_func_pam_start
963210
+	AC_CHECK_FUNCS(putenv pam_start)
963210
+	if test "x$ac_cv_func_pam_start" = xno ; then
963210
+		unset ac_cv_func_pam_start
963210
+		AC_CHECK_LIB(dl,dlopen)
963210
+		AC_CHECK_FUNCS(pam_start)
963210
+		if test "x$ac_cv_func_pam_start" = xno ; then
963210
+			AC_CHECK_LIB(pam,pam_start)
963210
+			unset ac_cv_func_pam_start
963210
+			unset ac_cv_func_pam_getenvlist
963210
+			AC_CHECK_FUNCS(pam_start pam_getenvlist)
963210
+			if test "x$ac_cv_func_pam_start" = xyes ; then
963210
+				PAM_LIBS="$LIBS"
963210
+			else
963210
+				if test "$withpam" = auto ; then
963210
+					AC_MSG_RESULT([Unable to locate libpam.])
963210
+					withpam=no
963210
+				else
963210
+					AC_MSG_ERROR([Unable to locate libpam.])
963210
+				fi
963210
+			fi
963210
+		fi
963210
+	fi
963210
+	if test "$withpam" != no ; then
963210
+		AC_MSG_NOTICE([building with PAM support])
963210
+		AC_DEFINE(USE_PAM,1,[Define if Kerberos-aware tools should support PAM])
963210
+		AC_DEFINE_UNQUOTED(KSU_PAM_SERVICE,"$withksupamservice",
963210
+				   [Define to the name of the PAM service name to be used by ksu.])
963210
+		PAM_LIBS="$LIBS"
963210
+		NON_PAM_MAN=".\\\" "
963210
+		PAM_MAN=
963210
+	else
963210
+		PAM_MAN=".\\\" "
963210
+		NON_PAM_MAN=
963210
+	fi
963210
+fi
963210
+LIBS="$old_LIBS"
963210
+AC_SUBST(PAM_LIBS)
963210
+AC_SUBST(PAM_MAN)
963210
+AC_SUBST(NON_PAM_MAN)
963210
+])dnl
963210
diff --git a/src/clients/ksu/Makefile.in b/src/clients/ksu/Makefile.in
963210
index b2fcbf240..5755bb58a 100644
963210
--- a/src/clients/ksu/Makefile.in
963210
+++ b/src/clients/ksu/Makefile.in
963210
@@ -3,12 +3,14 @@ BUILDTOP=$(REL)..$(S)..
963210
 DEFINES = -DGET_TGT_VIA_PASSWD -DPRINC_LOOK_AHEAD -DCMD_PATH='"/bin /local/bin"'
963210
 
963210
 KSU_LIBS=@KSU_LIBS@
963210
+PAM_LIBS=@PAM_LIBS@
963210
 
963210
 SRCS = \
963210
 	$(srcdir)/krb_auth_su.c \
963210
 	$(srcdir)/ccache.c \
963210
 	$(srcdir)/authorization.c \
963210
 	$(srcdir)/main.c \
963210
+	$(srcdir)/pam.c \
963210
 	$(srcdir)/heuristic.c \
963210
 	$(srcdir)/xmalloc.c \
963210
 	$(srcdir)/setenv.c
963210
@@ -17,13 +19,17 @@ OBJS = \
963210
 	ccache.o \
963210
 	authorization.o \
963210
 	main.o \
963210
+	pam.o \
963210
 	heuristic.o \
963210
 	xmalloc.o @SETENVOBJ@
963210
 
963210
 all: ksu
963210
 
963210
 ksu: $(OBJS) $(KRB5_BASE_DEPLIBS)
963210
-	$(CC_LINK) -o $@ $(OBJS) $(KRB5_BASE_LIBS) $(KSU_LIBS)
963210
+	$(CC_LINK) -o $@ $(OBJS) $(KRB5_BASE_LIBS) $(KSU_LIBS) $(PAM_LIBS)
963210
+
963210
+pam.o: pam.c
963210
+	$(CC) $(ALL_CFLAGS) -c $<
963210
 
963210
 clean:
963210
 	$(RM) ksu
963210
diff --git a/src/clients/ksu/main.c b/src/clients/ksu/main.c
963210
index 28342c2d7..cab0c1806 100644
963210
--- a/src/clients/ksu/main.c
963210
+++ b/src/clients/ksu/main.c
963210
@@ -26,6 +26,7 @@
963210
  * KSU was writen by:  Ari Medvinsky, ari@isi.edu
963210
  */
963210
 
963210
+#include "autoconf.h"
963210
 #include "ksu.h"
963210
 #include "adm_proto.h"
963210
 #include <sys/types.h>
963210
@@ -33,6 +34,10 @@
963210
 #include <signal.h>
963210
 #include <grp.h>
963210
 
963210
+#ifdef USE_PAM
963210
+#include "pam.h"
963210
+#endif
963210
+
963210
 /* globals */
963210
 char * prog_name;
963210
 int auth_debug =0;
963210
@@ -40,6 +45,7 @@ char k5login_path[MAXPATHLEN];
963210
 char k5users_path[MAXPATHLEN];
963210
 char * gb_err = NULL;
963210
 int quiet = 0;
963210
+int force_fork = 0;
963210
 /***********/
963210
 
963210
 #define KS_TEMPORARY_CACHE "MEMORY:_ksu"
963210
@@ -515,6 +521,23 @@ main (argc, argv)
963210
                prog_name,target_user,client_name,
963210
                source_user,ontty());
963210
 
963210
+#ifdef USE_PAM
963210
+        if (appl_pam_enabled(ksu_context, "ksu")) {
963210
+            if (appl_pam_acct_mgmt(KSU_PAM_SERVICE, 1, target_user, NULL,
963210
+                                   NULL, source_user,
963210
+                                   ttyname(STDERR_FILENO)) != 0) {
963210
+                fprintf(stderr, "Access denied for %s.\n", target_user);
963210
+                exit(1);
963210
+            }
963210
+            if (appl_pam_requires_chauthtok()) {
963210
+                fprintf(stderr, "Password change required for %s.\n",
963210
+                        target_user);
963210
+                exit(1);
963210
+            }
963210
+            force_fork++;
963210
+        }
963210
+#endif
963210
+
963210
         /* Run authorization as target.*/
963210
         if (krb5_seteuid(target_uid)) {
963210
             com_err(prog_name, errno, _("while switching to target for "
963210
@@ -575,6 +598,24 @@ main (argc, argv)
963210
 
963210
             exit(1);
963210
         }
963210
+#ifdef USE_PAM
963210
+    } else {
963210
+        /* we always do PAM account management, even for root */
963210
+        if (appl_pam_enabled(ksu_context, "ksu")) {
963210
+            if (appl_pam_acct_mgmt(KSU_PAM_SERVICE, 1, target_user, NULL,
963210
+                                   NULL, source_user,
963210
+                                   ttyname(STDERR_FILENO)) != 0) {
963210
+                fprintf(stderr, "Access denied for %s.\n", target_user);
963210
+                exit(1);
963210
+            }
963210
+            if (appl_pam_requires_chauthtok()) {
963210
+                fprintf(stderr, "Password change required for %s.\n",
963210
+                        target_user);
963210
+                exit(1);
963210
+            }
963210
+            force_fork++;
963210
+        }
963210
+#endif
963210
     }
963210
 
963210
     if( some_rest_copy){
963210
@@ -632,6 +673,30 @@ main (argc, argv)
963210
         exit(1);
963210
     }
963210
 
963210
+#ifdef USE_PAM
963210
+    if (appl_pam_enabled(ksu_context, "ksu")) {
963210
+        if (appl_pam_session_open() != 0) {
963210
+            fprintf(stderr, "Error opening session for %s.\n", target_user);
963210
+            exit(1);
963210
+        }
963210
+#ifdef DEBUG
963210
+        if (auth_debug){
963210
+            printf(" Opened PAM session.\n");
963210
+        }
963210
+#endif
963210
+        if (appl_pam_cred_init()) {
963210
+            fprintf(stderr, "Error initializing credentials for %s.\n",
963210
+                    target_user);
963210
+            exit(1);
963210
+        }
963210
+#ifdef DEBUG
963210
+        if (auth_debug){
963210
+            printf(" Initialized PAM credentials.\n");
963210
+        }
963210
+#endif
963210
+    }
963210
+#endif
963210
+
963210
     /* set permissions */
963210
     if (setgid(target_pwd->pw_gid) < 0) {
963210
         perror("ksu: setgid");
963210
@@ -729,7 +794,7 @@ main (argc, argv)
963210
         fprintf(stderr, "program to be execed %s\n",params[0]);
963210
     }
963210
 
963210
-    if( keep_target_cache ) {
963210
+    if( keep_target_cache && !force_fork ) {
963210
         execv(params[0], params);
963210
         com_err(prog_name, errno, _("while trying to execv %s"), params[0]);
963210
         sweep_up(ksu_context, cc_target);
963210
@@ -759,16 +824,35 @@ main (argc, argv)
963210
             if (ret_pid == -1) {
963210
                 com_err(prog_name, errno, _("while calling waitpid"));
963210
             }
963210
-            sweep_up(ksu_context, cc_target);
963210
+            if( !keep_target_cache ) {
963210
+                sweep_up(ksu_context, cc_target);
963210
+            }
963210
             exit (statusp);
963210
         case -1:
963210
             com_err(prog_name, errno, _("while trying to fork."));
963210
             sweep_up(ksu_context, cc_target);
963210
             exit (1);
963210
         case 0:
963210
+#ifdef USE_PAM
963210
+            if (appl_pam_enabled(ksu_context, "ksu")) {
963210
+                if (appl_pam_setenv() != 0) {
963210
+                    fprintf(stderr, "Error setting up environment for %s.\n",
963210
+                            target_user);
963210
+                    exit (1);
963210
+                }
963210
+#ifdef DEBUG
963210
+                if (auth_debug){
963210
+                    printf(" Set up PAM environment.\n");
963210
+                }
963210
+#endif
963210
+            }
963210
+#endif
963210
             execv(params[0], params);
963210
             com_err(prog_name, errno, _("while trying to execv %s"),
963210
                     params[0]);
963210
+            if( keep_target_cache ) {
963210
+                sweep_up(ksu_context, cc_target);
963210
+            }
963210
             exit (1);
963210
         }
963210
     }
963210
diff --git a/src/clients/ksu/pam.c b/src/clients/ksu/pam.c
963210
new file mode 100644
963210
index 000000000..cbfe48704
963210
--- /dev/null
963210
+++ b/src/clients/ksu/pam.c
963210
@@ -0,0 +1,389 @@
963210
+/*
963210
+ * src/clients/ksu/pam.c
963210
+ *
963210
+ * Copyright 2007,2009,2010 Red Hat, Inc.
963210
+ *
963210
+ * All Rights Reserved.
963210
+ *
963210
+ * Redistribution and use in source and binary forms, with or without
963210
+ * modification, are permitted provided that the following conditions are met:
963210
+ *
963210
+ *  Redistributions of source code must retain the above copyright notice, this
963210
+ *  list of conditions and the following disclaimer.
963210
+ *
963210
+ *  Redistributions in binary form must reproduce the above copyright notice,
963210
+ *  this list of conditions and the following disclaimer in the documentation
963210
+ *  and/or other materials provided with the distribution.
963210
+ *
963210
+ *  Neither the name of Red Hat, Inc. nor the names of its contributors may be
963210
+ *  used to endorse or promote products derived from this software without
963210
+ *  specific prior written permission.
963210
+ *
963210
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
963210
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
963210
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
963210
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
963210
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
963210
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
963210
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
963210
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
963210
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
963210
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
963210
+ * POSSIBILITY OF SUCH DAMAGE.
963210
+ * 
963210
+ * Convenience wrappers for using PAM.
963210
+ */
963210
+
963210
+#include "autoconf.h"
963210
+#ifdef USE_PAM
963210
+#include <sys/types.h>
963210
+#include <stdio.h>
963210
+#include <stdlib.h>
963210
+#include <string.h>
963210
+#include <unistd.h>
963210
+#include "k5-int.h"
963210
+#include "pam.h"
963210
+
963210
+#ifndef MAXPWSIZE
963210
+#define MAXPWSIZE 128
963210
+#endif
963210
+
963210
+static int appl_pam_started;
963210
+static pid_t appl_pam_starter = -1;
963210
+static int appl_pam_session_opened;
963210
+static int appl_pam_creds_initialized;
963210
+static int appl_pam_pwchange_required;
963210
+static pam_handle_t *appl_pamh;
963210
+static struct pam_conv appl_pam_conv;
963210
+static char *appl_pam_user;
963210
+struct appl_pam_non_interactive_args {
963210
+	const char *user;
963210
+	const char *password;
963210
+};
963210
+
963210
+int
963210
+appl_pam_enabled(krb5_context context, const char *section)
963210
+{
963210
+	int enabled = 1;
963210
+	if ((context != NULL) && (context->profile != NULL)) {
963210
+		if (profile_get_boolean(context->profile,
963210
+					section,
963210
+					USE_PAM_CONFIGURATION_KEYWORD,
963210
+					NULL,
963210
+					enabled, &enabled) != 0) {
963210
+			enabled = 1;
963210
+		}
963210
+	}
963210
+	return enabled;
963210
+}
963210
+
963210
+void
963210
+appl_pam_cleanup(void)
963210
+{
963210
+	if (getpid() != appl_pam_starter) {
963210
+		return;
963210
+	}
963210
+#ifdef DEBUG
963210
+	printf("Called to clean up PAM.\n");
963210
+#endif
963210
+	if (appl_pam_creds_initialized) {
963210
+#ifdef DEBUG
963210
+		printf("Deleting PAM credentials.\n");
963210
+#endif
963210
+		pam_setcred(appl_pamh, PAM_DELETE_CRED);
963210
+		appl_pam_creds_initialized = 0;
963210
+	}
963210
+	if (appl_pam_session_opened) {
963210
+#ifdef DEBUG
963210
+		printf("Closing PAM session.\n");
963210
+#endif
963210
+		pam_close_session(appl_pamh, 0);
963210
+		appl_pam_session_opened = 0;
963210
+	}
963210
+	appl_pam_pwchange_required = 0;
963210
+	if (appl_pam_started) {
963210
+#ifdef DEBUG
963210
+		printf("Shutting down PAM.\n");
963210
+#endif
963210
+		pam_end(appl_pamh, 0);
963210
+		appl_pam_started = 0;
963210
+		appl_pam_starter = -1;
963210
+		free(appl_pam_user);
963210
+		appl_pam_user = NULL;
963210
+	}
963210
+}
963210
+static int
963210
+appl_pam_interactive_converse(int num_msg, const struct pam_message **msg,
963210
+			      struct pam_response **presp, void *appdata_ptr)
963210
+{
963210
+	const struct pam_message *message;
963210
+	struct pam_response *resp;
963210
+	int i, code;
963210
+	char *pwstring, pwbuf[MAXPWSIZE];
963210
+	unsigned int pwsize;
963210
+	resp = malloc(sizeof(struct pam_response) * num_msg);
963210
+	if (resp == NULL) {
963210
+		return PAM_BUF_ERR;
963210
+	}
963210
+	memset(resp, 0, sizeof(struct pam_response) * num_msg);
963210
+	code = PAM_SUCCESS;
963210
+	for (i = 0; i < num_msg; i++) {
963210
+		message = &(msg[0][i]); /* XXX */
963210
+		message = msg[i]; /* XXX */
963210
+		pwstring = NULL;
963210
+		switch (message->msg_style) {
963210
+		case PAM_TEXT_INFO:
963210
+		case PAM_ERROR_MSG:
963210
+			printf("[%s]\n", message->msg ? message->msg : "");
963210
+			fflush(stdout);
963210
+			resp[i].resp = NULL;
963210
+			resp[i].resp_retcode = PAM_SUCCESS;
963210
+			break;
963210
+		case PAM_PROMPT_ECHO_ON:
963210
+		case PAM_PROMPT_ECHO_OFF:
963210
+			if (message->msg_style == PAM_PROMPT_ECHO_ON) {
963210
+				if (fgets(pwbuf, sizeof(pwbuf),
963210
+					  stdin) != NULL) {
963210
+					pwbuf[strcspn(pwbuf, "\r\n")] = '\0';
963210
+					pwstring = pwbuf;
963210
+				}
963210
+			} else {
963210
+				pwstring = getpass(message->msg ?
963210
+						   message->msg :
963210
+						   "");
963210
+			}
963210
+			if ((pwstring != NULL) && (pwstring[0] != '\0')) {
963210
+				pwsize = strlen(pwstring);
963210
+				resp[i].resp = malloc(pwsize + 1);
963210
+				if (resp[i].resp == NULL) {
963210
+					resp[i].resp_retcode = PAM_BUF_ERR;
963210
+				} else {
963210
+					memcpy(resp[i].resp, pwstring, pwsize);
963210
+					resp[i].resp[pwsize] = '\0';
963210
+					resp[i].resp_retcode = PAM_SUCCESS;
963210
+				}
963210
+			} else {
963210
+				resp[i].resp_retcode = PAM_CONV_ERR;
963210
+				code = PAM_CONV_ERR;
963210
+			}
963210
+			break;
963210
+		default:
963210
+			break;
963210
+		}
963210
+	}
963210
+	*presp = resp;
963210
+	return code;
963210
+}
963210
+static int
963210
+appl_pam_non_interactive_converse(int num_msg,
963210
+				  const struct pam_message **msg,
963210
+				  struct pam_response **presp,
963210
+				  void *appdata_ptr)
963210
+{
963210
+	const struct pam_message *message;
963210
+	struct pam_response *resp;
963210
+	int i, code;
963210
+	unsigned int pwsize;
963210
+	struct appl_pam_non_interactive_args *args;
963210
+	const char *pwstring;
963210
+	resp = malloc(sizeof(struct pam_response) * num_msg);
963210
+	if (resp == NULL) {
963210
+		return PAM_BUF_ERR;
963210
+	}
963210
+	args = appdata_ptr;
963210
+	memset(resp, 0, sizeof(struct pam_response) * num_msg);
963210
+	code = PAM_SUCCESS;
963210
+	for (i = 0; i < num_msg; i++) {
963210
+		message = &((*msg)[i]);
963210
+		message = msg[i];
963210
+		pwstring = NULL;
963210
+		switch (message->msg_style) {
963210
+		case PAM_TEXT_INFO:
963210
+		case PAM_ERROR_MSG:
963210
+			break;
963210
+		case PAM_PROMPT_ECHO_ON:
963210
+		case PAM_PROMPT_ECHO_OFF:
963210
+			if (message->msg_style == PAM_PROMPT_ECHO_ON) {
963210
+				/* assume "user" */
963210
+				pwstring = args->user;
963210
+			} else {
963210
+				/* assume "password" */
963210
+				pwstring = args->password;
963210
+			}
963210
+			if ((pwstring != NULL) && (pwstring[0] != '\0')) {
963210
+				pwsize = strlen(pwstring);
963210
+				resp[i].resp = malloc(pwsize + 1);
963210
+				if (resp[i].resp == NULL) {
963210
+					resp[i].resp_retcode = PAM_BUF_ERR;
963210
+				} else {
963210
+					memcpy(resp[i].resp, pwstring, pwsize);
963210
+					resp[i].resp[pwsize] = '\0';
963210
+					resp[i].resp_retcode = PAM_SUCCESS;
963210
+				}
963210
+			} else {
963210
+				resp[i].resp_retcode = PAM_CONV_ERR;
963210
+				code = PAM_CONV_ERR;
963210
+			}
963210
+			break;
963210
+		default:
963210
+			break;
963210
+		}
963210
+	}
963210
+	*presp = resp;
963210
+	return code;
963210
+}
963210
+static int
963210
+appl_pam_start(const char *service, int interactive,
963210
+	       const char *login_username,
963210
+	       const char *non_interactive_password,
963210
+	       const char *hostname,
963210
+	       const char *ruser,
963210
+	       const char *tty)
963210
+{
963210
+	static int exit_handler_registered;
963210
+	static struct appl_pam_non_interactive_args args;
963210
+	int ret = 0;
963210
+	if (appl_pam_started &&
963210
+	    (strcmp(login_username, appl_pam_user) != 0)) {
963210
+		appl_pam_cleanup();
963210
+		appl_pam_user = NULL;
963210
+	}
963210
+	if (!appl_pam_started) {
963210
+#ifdef DEBUG
963210
+		printf("Starting PAM up (service=\"%s\",user=\"%s\").\n",
963210
+		       service, login_username);
963210
+#endif
963210
+		memset(&appl_pam_conv, 0, sizeof(appl_pam_conv));
963210
+		appl_pam_conv.conv = interactive ?
963210
+				     &appl_pam_interactive_converse :
963210
+				     &appl_pam_non_interactive_converse;
963210
+		memset(&args, 0, sizeof(args));
963210
+		args.user = strdup(login_username);
963210
+		args.password = non_interactive_password ?
963210
+				strdup(non_interactive_password) :
963210
+				NULL;
963210
+		appl_pam_conv.appdata_ptr = &arg;;
963210
+		ret = pam_start(service, login_username,
963210
+				&appl_pam_conv, &appl_pamh);
963210
+		if (ret == 0) {
963210
+			if (hostname != NULL) {
963210
+#ifdef DEBUG
963210
+				printf("Setting PAM_RHOST to \"%s\".\n", hostname);
963210
+#endif
963210
+				pam_set_item(appl_pamh, PAM_RHOST, hostname);
963210
+			}
963210
+			if (ruser != NULL) {
963210
+#ifdef DEBUG
963210
+				printf("Setting PAM_RUSER to \"%s\".\n", ruser);
963210
+#endif
963210
+				pam_set_item(appl_pamh, PAM_RUSER, ruser);
963210
+			}
963210
+			if (tty != NULL) {
963210
+#ifdef DEBUG
963210
+				printf("Setting PAM_TTY to \"%s\".\n", tty);
963210
+#endif
963210
+				pam_set_item(appl_pamh, PAM_TTY, tty);
963210
+			}
963210
+			if (!exit_handler_registered &&
963210
+			    (atexit(appl_pam_cleanup) != 0)) {
963210
+				pam_end(appl_pamh, 0);
963210
+				appl_pamh = NULL;
963210
+				ret = -1;
963210
+			} else {
963210
+				appl_pam_started = 1;
963210
+				appl_pam_starter = getpid();
963210
+				appl_pam_user = strdup(login_username);
963210
+				exit_handler_registered = 1;
963210
+			}
963210
+		}
963210
+	}
963210
+	return ret;
963210
+}
963210
+int
963210
+appl_pam_acct_mgmt(const char *service, int interactive,
963210
+		   const char *login_username,
963210
+		   const char *non_interactive_password,
963210
+		   const char *hostname,
963210
+		   const char *ruser,
963210
+		   const char *tty)
963210
+{
963210
+	int ret;
963210
+	appl_pam_pwchange_required = 0;
963210
+	ret = appl_pam_start(service, interactive, login_username,
963210
+			     non_interactive_password, hostname, ruser, tty);
963210
+	if (ret == 0) {
963210
+#ifdef DEBUG
963210
+		printf("Calling pam_acct_mgmt().\n");
963210
+#endif
963210
+		ret = pam_acct_mgmt(appl_pamh, 0);
963210
+		switch (ret) {
963210
+		case PAM_IGNORE:
963210
+			ret = 0;
963210
+			break;
963210
+		case PAM_NEW_AUTHTOK_REQD:
963210
+			appl_pam_pwchange_required = 1;
963210
+			ret = 0;
963210
+			break;
963210
+		default:
963210
+			break;
963210
+		}
963210
+	}
963210
+	return ret;
963210
+}
963210
+int
963210
+appl_pam_requires_chauthtok(void)
963210
+{
963210
+	return appl_pam_pwchange_required;
963210
+}
963210
+int
963210
+appl_pam_session_open(void)
963210
+{
963210
+	int ret = 0;
963210
+	if (appl_pam_started) {
963210
+#ifdef DEBUG
963210
+		printf("Opening PAM session.\n");
963210
+#endif
963210
+		ret = pam_open_session(appl_pamh, 0);
963210
+		if (ret == 0) {
963210
+			appl_pam_session_opened = 1;
963210
+		}
963210
+	}
963210
+	return ret;
963210
+}
963210
+int
963210
+appl_pam_setenv(void)
963210
+{
963210
+	int ret = 0;
963210
+#ifdef HAVE_PAM_GETENVLIST
963210
+#ifdef HAVE_PUTENV
963210
+	int i;
963210
+	char **list;
963210
+	if (appl_pam_started) {
963210
+		list = pam_getenvlist(appl_pamh);
963210
+		for (i = 0; ((list != NULL) && (list[i] != NULL)); i++) {
963210
+#ifdef DEBUG
963210
+			printf("Setting \"%s\" in environment.\n", list[i]);
963210
+#endif
963210
+			putenv(list[i]);
963210
+		}
963210
+	}
963210
+#endif
963210
+#endif
963210
+	return ret;
963210
+}
963210
+int
963210
+appl_pam_cred_init(void)
963210
+{
963210
+	int ret = 0;
963210
+	if (appl_pam_started) {
963210
+#ifdef DEBUG
963210
+		printf("Initializing PAM credentials.\n");
963210
+#endif
963210
+		ret = pam_setcred(appl_pamh, PAM_ESTABLISH_CRED);
963210
+		if (ret == 0) {
963210
+			appl_pam_creds_initialized = 1;
963210
+		}
963210
+	}
963210
+	return ret;
963210
+}
963210
+#endif
963210
diff --git a/src/clients/ksu/pam.h b/src/clients/ksu/pam.h
963210
new file mode 100644
963210
index 000000000..0ab76569c
963210
--- /dev/null
963210
+++ b/src/clients/ksu/pam.h
963210
@@ -0,0 +1,57 @@
963210
+/*
963210
+ * src/clients/ksu/pam.h
963210
+ *
963210
+ * Copyright 2007,2009,2010 Red Hat, Inc.
963210
+ *
963210
+ * All Rights Reserved.
963210
+ *
963210
+ * Redistribution and use in source and binary forms, with or without
963210
+ * modification, are permitted provided that the following conditions are met:
963210
+ *
963210
+ *  Redistributions of source code must retain the above copyright notice, this
963210
+ *  list of conditions and the following disclaimer.
963210
+ *
963210
+ *  Redistributions in binary form must reproduce the above copyright notice,
963210
+ *  this list of conditions and the following disclaimer in the documentation
963210
+ *  and/or other materials provided with the distribution.
963210
+ *
963210
+ *  Neither the name of Red Hat, Inc. nor the names of its contributors may be
963210
+ *  used to endorse or promote products derived from this software without
963210
+ *  specific prior written permission.
963210
+ *
963210
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
963210
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
963210
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
963210
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
963210
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
963210
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
963210
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
963210
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
963210
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
963210
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
963210
+ * POSSIBILITY OF SUCH DAMAGE.
963210
+ * 
963210
+ * Convenience wrappers for using PAM.
963210
+ */
963210
+
963210
+#include <krb5.h>
963210
+#ifdef HAVE_SECURITY_PAM_APPL_H
963210
+#include <security/pam_appl.h>
963210
+#endif
963210
+
963210
+#define USE_PAM_CONFIGURATION_KEYWORD "use_pam"
963210
+
963210
+#ifdef USE_PAM
963210
+int appl_pam_enabled(krb5_context context, const char *section);
963210
+int appl_pam_acct_mgmt(const char *service, int interactive,
963210
+		       const char *local_username,
963210
+		       const char *non_interactive_password,
963210
+		       const char *hostname,
963210
+		       const char *ruser,
963210
+		       const char *tty);
963210
+int appl_pam_requires_chauthtok(void);
963210
+int appl_pam_session_open(void);
963210
+int appl_pam_setenv(void);
963210
+int appl_pam_cred_init(void);
963210
+void appl_pam_cleanup(void);
963210
+#endif
963210
diff --git a/src/configure.in b/src/configure.in
963210
index 037c9f316..daabd12c8 100644
963210
--- a/src/configure.in
963210
+++ b/src/configure.in
963210
@@ -1336,6 +1336,8 @@ AC_SUBST([VERTO_VERSION])
963210
 
963210
 AC_PATH_PROG(GROFF, groff)
963210
 
963210
+KRB5_WITH_PAM
963210
+
963210
 # Make localedir work in autoconf 2.5x.
963210
 if test "${localedir+set}" != set; then
963210
     localedir='$(datadir)/locale'