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