738028
diff -Naur libreswan-3.15-orig/programs/pluto/ikev1_xauth.c libreswan-3.15/programs/pluto/ikev1_xauth.c
738028
--- libreswan-3.15-orig/programs/pluto/ikev1_xauth.c	2015-08-24 16:52:43.000000000 -0400
738028
+++ libreswan-3.15/programs/pluto/ikev1_xauth.c	2015-09-03 11:32:27.525000000 -0400
738028
@@ -1001,81 +1001,6 @@
738028
 	return STF_OK;
738028
 }
738028
 
738028
-#ifdef XAUTH_HAVE_PAM
738028
-
738028
-/** Do authentication via PAM (Plugable Authentication Modules)
738028
- *
738028
- * We try to authenticate the user in our own PAM session.
738028
- *
738028
- * @return bool success
738028
- */
738028
-/* IN AN AUTH THREAD */
738028
-static bool do_pam_authentication(void *varg)
738028
-{
738028
-	struct xauth_thread_arg *arg = varg;
738028
-	int retval;
738028
-	pam_handle_t *pamh = NULL;
738028
-	struct pam_conv conv;
738028
-	const char *what;
738028
-
738028
-	/* This do-while structure is designed to allow a logical cascade
738028
-	 * without excessive indentation.  No actual looping happens.
738028
-	 * Failure is handled by "break".
738028
-	 */
738028
-	do {
738028
-		ipstr_buf ra;
738028
-
738028
-		conv.conv = pam_conv;
738028
-		conv.appdata_ptr = varg;
738028
-
738028
-		what = "pam_start";
738028
-		retval = pam_start("pluto", arg->name, &conv, &pamh);
738028
-		if (retval != PAM_SUCCESS)
738028
-			break;
738028
-
738028
-		DBG(DBG_CONTROL, DBG_log("pam_start SUCCESS"));
738028
-
738028
-		/* Send the remote host address to PAM */
738028
-		what = "pam_set_item";
738028
-		retval = pam_set_item(pamh, PAM_RHOST,
738028
-				      ipstr(&arg->st->st_remoteaddr, &ra);;
738028
-		if (retval != PAM_SUCCESS)
738028
-			break;
738028
-
738028
-		DBG(DBG_CONTROL, DBG_log("pam_set_item SUCCESS"));
738028
-
738028
-		/* Two factor authentication - Check that the user is valid,
738028
-		 * and then check if they are permitted access
738028
-		 */
738028
-		what = "pam_authenticate";
738028
-		retval = pam_authenticate(pamh, PAM_SILENT); /* is user really user? */
738028
-
738028
-		if (retval != PAM_SUCCESS)
738028
-			break;
738028
-
738028
-		DBG(DBG_CONTROL, DBG_log("pam_authenticate SUCCESS"));
738028
-
738028
-		what = "pam_acct_mgmt";
738028
-		retval = pam_acct_mgmt(pamh, 0); /* permitted access? */
738028
-		if (retval != PAM_SUCCESS)
738028
-			break;
738028
-
738028
-		/* success! */
738028
-		libreswan_log("XAUTH: PAM_SUCCESS");
738028
-		pam_end(pamh, PAM_SUCCESS);
738028
-		return TRUE;
738028
-	} while (FALSE);
738028
-
738028
-	/* common failure code */
738028
-
738028
-	DBG(DBG_CONTROL,
738028
-	    DBG_log("%s failed with '%s", what, pam_strerror(pamh, retval)));
738028
-	libreswan_log("XAUTH: %s failed with '%s'", what, pam_strerror(pamh, retval));
738028
-	pam_end(pamh, retval);
738028
-	return FALSE;
738028
-}
738028
-#endif /* XAUTH_HAVE_PAM */
738028
-
738028
 /** Do authentication via /etc/ipsec.d/passwd file using MD5 passwords
738028
  *
738028
  * Structure is one entry per line.
738028
@@ -1256,6 +1181,43 @@
738028
 	return win;
738028
 }
738028
 
738028
+/* IN AN AUTH THREAD */
738028
+static bool ikev1_do_pam_authentication(const struct xauth_thread_arg *arg)
738028
+{
738028
+	struct state *st = arg->st;
738028
+	libreswan_log("XAUTH: pam authentication being called to authenticate user %s",
738028
+			arg->name);
738028
+	struct pam_thread_arg parg;
738028
+	ipstr_buf ra;
738028
+	struct timeval start_time;
738028
+	struct timeval served_time;
738028
+	struct timeval served_delta;
738028
+	bool results = FALSE;
738028
+
738028
+	parg.name = arg->name;
738028
+	parg.password =  arg->password;
738028
+	parg.c_name = arg->connname;
738028
+	parg.ra = clone_str(ipstr(&st->st_remoteaddr, &ra), "st remote address");
738028
+	parg.st_serialno = st->st_serialno;
738028
+	parg.c_instance_serial = st->st_connection->instance_serial;
738028
+	parg.atype = "XAUTH";
738028
+	gettimeofday(&start_time, NULL);
738028
+	results = do_pam_authentication(&parg;;
738028
+	gettimeofday(&served_time, NULL);
738028
+	timersub(&served_time, &start_time, &served_delta);
738028
+	DBG(DBG_CONTROL, DBG_log("XAUTH PAM helper thread call "
738028
+				"state #%lu, %s[%lu] user=%s %s. "
738028
+				"elapsed time %lu.%06lu",
738028
+				parg.st_serialno, parg.c_name,
738028
+				parg.c_instance_serial, parg.name,
738028
+				results ? "SUCCESS" : "FAIL",
738028
+				(unsigned long)served_delta.tv_sec,
738028
+				(unsigned long)(served_delta.tv_usec * 1000000)));
738028
+
738028
+	pfreeany(parg.ra);
738028
+	return (results);
738028
+}
738028
+
738028
 /*
738028
  * Main authentication routine will then call the actual compiled-in
738028
  * method to verify the user/password
738028
@@ -1330,10 +1292,7 @@
738028
 	switch (st->st_connection->xauthby) {
738028
 #ifdef XAUTH_HAVE_PAM
738028
 	case XAUTHBY_PAM:
738028
-		libreswan_log(
738028
-			"XAUTH: pam authentication being called to authenticate user %s",
738028
-			arg->name);
738028
-		results = do_pam_authentication(varg);
738028
+		results = ikev1_do_pam_authentication(arg);
738028
 		break;
738028
 #endif
738028
 	case XAUTHBY_FILE:
738028
diff -Naur libreswan-3.15-orig/programs/pluto/ikev2_parent.c libreswan-3.15/programs/pluto/ikev2_parent.c
738028
--- libreswan-3.15-orig/programs/pluto/ikev2_parent.c	2015-09-03 11:29:05.518000000 -0400
738028
+++ libreswan-3.15/programs/pluto/ikev2_parent.c	2015-09-03 11:32:27.526000000 -0400
738028
@@ -2417,7 +2417,7 @@
738028
 	pthread_setcanceltype  (PTHREAD_CANCEL_ASYNCHRONOUS,  NULL);
738028
 	pthread_setcancelstate (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
738028
 
738028
-	p->pam_status = ikev2_do_pam_authentication(&p->pam);
738028
+	p->pam_status = do_pam_authentication(&p->pam);
738028
 	gettimeofday(&p->done_time, NULL);
738028
 	timersub(&p->done_time, &p->start_time, &done_delta);
738028
 
738028
@@ -2580,6 +2580,7 @@
738028
 	p->pam.ra = clone_str(ipstr(&st->st_remoteaddr, &ra), "st remote address");
738028
 	p->pam.c_instance_serial = st->st_connection->instance_serial;
738028
 	p->pam.st_serialno = st->st_serialno;
738028
+	p->pam.atype = "IKEv2";
738028
 
738028
 	p->next = pluto_v2_pam_helpers;
738028
 	pluto_v2_pam_helpers = p;
738028
diff -Naur libreswan-3.15-orig/programs/pluto/pam_conv.c libreswan-3.15/programs/pluto/pam_conv.c
738028
--- libreswan-3.15-orig/programs/pluto/pam_conv.c	2015-08-24 16:52:43.000000000 -0400
738028
+++ libreswan-3.15/programs/pluto/pam_conv.c	2015-09-03 11:32:27.526000000 -0400
738028
@@ -10,7 +10,7 @@
738028
  * Copyright (C) 2012-2013 Paul Wouters <pwouters@redhat.com>
738028
  * Copyright (C) 2012-2013 Philippe Vouters <philippe.vouters@laposte.net>
738028
  * Copyright (C) 2013 David McCullough <ucdevel@gmail.com>
738028
- * Copyright (C) 2013 Antony Antony <antony@phenome.org>
738028
+ * Copyright (C) 2013-2015 Antony Antony <antony@phenome.org>
738028
  *
738028
  * This program is free software; you can redistribute it and/or modify it
738028
  * under the terms of the GNU General Public License as published by the
738028
@@ -109,15 +109,25 @@
738028
 	return PAM_SUCCESS;
738028
 }
738028
 
738028
+static void log_pam_step(const struct pam_thread_arg *arg, const char *what,
738028
+		const char *how)
738028
+{
738028
+	DBG(DBG_CONTROL, DBG_log("%s helper thread %s %s for "
738028
+                                "state #%lu, %s[%lu] user=%s.",
738028
+				arg->atype, what, how,
738028
+				arg->st_serialno, arg->c_name,
738028
+				arg->c_instance_serial, arg->name));
738028
+
738028
+}
738028
+
738028
 /*
738028
  * Do IKEv2 second authentication via PAM (Plugable Authentication Modules)
738028
  *
738028
  * @return bool success
738028
  */
738028
 /* IN AN AUTH THREAD */
738028
-bool ikev2_do_pam_authentication(void *varg)
738028
+bool do_pam_authentication(struct pam_thread_arg *arg)
738028
 {
738028
-	struct pam_thread_arg *arg = varg;
738028
 	int retval;
738028
 	pam_handle_t *pamh = NULL;
738028
 	struct pam_conv conv;
738028
@@ -129,22 +139,20 @@
738028
 	 */
738028
 	do {
738028
 		conv.conv = pam_conv;
738028
-		conv.appdata_ptr = varg;
738028
+		conv.appdata_ptr = arg;
738028
 
738028
 		what = "pam_start";
738028
 		retval = pam_start("pluto", arg->name, &conv, &pamh);
738028
 		if (retval != PAM_SUCCESS)
738028
 			break;
738028
-
738028
-		DBG(DBG_CONTROL, DBG_log("pam_start SUCCESS"));
738028
+		log_pam_step(arg, what, "SUCCESS");
738028
 
738028
 		/* Send the remote host address to PAM */
738028
 		what = "pam_set_item";
738028
 		retval = pam_set_item(pamh, PAM_RHOST, arg->ra);
738028
 		if (retval != PAM_SUCCESS)
738028
 			break;
738028
-
738028
-		DBG(DBG_CONTROL, DBG_log("pam_set_item SUCCESS"));
738028
+		log_pam_step(arg, what, "SUCCESS");
738028
 
738028
 		/* Two factor authentication - Check that the user is valid,
738028
 		 * and then check if they are permitted access
738028
@@ -154,16 +162,16 @@
738028
 
738028
 		if (retval != PAM_SUCCESS)
738028
 			break;
738028
+		log_pam_step(arg, what, "SUCCESS");
738028
 
738028
-		DBG(DBG_CONTROL, DBG_log("pam_authenticate SUCCESS"));
738028
-
738028
-		what = "pam_acct_mgmt";
738028
 		retval = pam_acct_mgmt(pamh, 0); /* permitted access? */
738028
 		if (retval != PAM_SUCCESS)
738028
 			break;
738028
 
738028
+		what = "pam";
738028
+		log_pam_step(arg, what, "SUCCESS");
738028
+
738028
 		/* success! */
738028
-		libreswan_log("IKEv2: PAM_SUCCESS");
738028
 		pam_end(pamh, PAM_SUCCESS);
738028
 		return TRUE;
738028
 	} while (FALSE);
738028
diff -Naur libreswan-3.15-orig/programs/pluto/pam_conv.h libreswan-3.15/programs/pluto/pam_conv.h
738028
--- libreswan-3.15-orig/programs/pluto/pam_conv.h	2015-08-24 16:52:43.000000000 -0400
738028
+++ libreswan-3.15/programs/pluto/pam_conv.h	2015-09-03 11:32:27.527000000 -0400
738028
@@ -23,9 +23,10 @@
738028
 	char *ra;
738028
 	so_serial_t st_serialno;
738028
 	unsigned long c_instance_serial;
738028
+	char *atype;  /* string XAUTH or IKEv2 */
738028
 };
738028
 
738028
-extern bool ikev2_do_pam_authentication(void *varg);
738028
+extern bool do_pam_authentication(struct pam_thread_arg *arg);
738028
 int pam_conv(int num_msg, const struct pam_message **msgm,
738028
 					  struct pam_response **response, void
738028
 					  *appdata_ptr);