From 214716561bc27c38b414e93e1dc28e77a6951c5c Mon Sep 17 00:00:00 2001
From: tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>
Date: Wed, 21 Sep 2011 07:55:44 +0000
Subject: Enhance smartauthmon VT detection on login

git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/smartcardauth@1254814 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
---
 Makefile                    |  1 +
 src/smartauth_displayfinder | 30 ++++++++++++++++++++++++++++
 src/smartauthmon.cpp        | 48 +++++++++++++++++++++++++++------------------
 3 files changed, 60 insertions(+), 19 deletions(-)
 create mode 100755 src/smartauth_displayfinder

diff --git a/Makefile b/Makefile
index 9259b90..2301ea0 100755
--- a/Makefile
+++ b/Makefile
@@ -16,6 +16,7 @@ install:
 	mkdir -p $(DESTDIR)/usr
 	cp -Rp src/ckpasswd usr/bin/smartauthckpasswd
 	cp -Rp src/smartauthmon usr/bin/smartauthmon
+	cp -Rp src/smartauth_displayfinder usr/bin/smartauth_displayfinder
 	cp -Rp usr/*  $(DESTDIR)/usr/
 
 	mkdir -p $(DESTDIR)/etc
diff --git a/src/smartauth_displayfinder b/src/smartauth_displayfinder
new file mode 100755
index 0000000..cbe9dec
--- /dev/null
+++ b/src/smartauth_displayfinder
@@ -0,0 +1,30 @@
+#!/bin/bash								
+
+mkdir -p /tmp/smartauthmon/						
+ls /var/run/xdmctl > /tmp/smartauthmon/originalxdm			
+										
+# Set loop separator to end of line					
+BAKIFS=$IFS								
+IFS=$(echo -en "\n\b")						
+exec 3<&0								
+exec 0</tmp/smartauthmon/originalxdm					
+newdisplayfound=0							
+newdisplay=-1								
+while read -r line							
+do									
+	# use $line variable to process lines				
+	line=$(echo $line | grep 'xdmctl-:' | sed -e 's/xdmctl-://')	
+	if [ "`expr $line - $line 2>/dev/null`" == "0" ]; then	
+		echo "Found active display on $line"			
+		if [[ $newdisplayfound -eq 0 ]]; then			
+			tempnewdisplay=$((newdisplay + 1))		
+			if [[ $line -eq $tempnewdisplay ]]; then	
+				echo "Sequential display $line found after display $newdisplay..."
+				newdisplay=$line			
+			fi						
+		fi							
+	fi								
+done									
+exec 0<&3								
+newdisplay=$(($newdisplay + 1))					
+rm -rf /tmp/smartauthmon/						
diff --git a/src/smartauthmon.cpp b/src/smartauthmon.cpp
index db109a5..f122cc0 100644
--- a/src/smartauthmon.cpp
+++ b/src/smartauthmon.cpp
@@ -573,7 +573,7 @@ int main (int argc, char *argv[])
 							usebasedisplay=1;
 						}
 					}
-					if (!resultbkp.contains(",vt")) {
+					if (!resultbkp.contains(":")) {
 						newdisplay = exec("ls /var/run/xdmctl/ | grep 'xdmctl-:0'");
 						printf("[DEBUG 400.d] %s\n\r", newdisplay.ascii());
 						if (newdisplay != "") {
@@ -582,30 +582,40 @@ int main (int argc, char *argv[])
 					}
 
 					printf("[DEBUG 400.e] Creating new session\n\r");
+					if (usebasedisplay) {
+						printf("[DEBUG 400.e] Using base display\n\r");
+					}
 					// Attempt login
 
-					// Find next sequential inactive display
-					// FIXME
-					// This assumes the original VT is on display 0 at all times
-					int minvt = 0;
-					TQStringList sessionList = TQStringList::split('\t', result, false);
-					for (newdisplayint = minvt; newdisplayint<MAXIMUM_VTS; newdisplayint++) {
-						bool displayfound = false;
-						for ( TQStringList::Iterator it = sessionList.begin(); it != sessionList.end(); ++it ) {
-							TQStringList sessionInfoList = TQStringList::split(',', *it, true);
-							if ((*(sessionInfoList.at(0))).startsWith(TQString(":%1").tqarg(newdisplayint))) {
-								displayfound = true;
-							}
-						}
-						if (displayfound == false) {
-							break;
-						}
-					}
+// 					// Find next sequential inactive display
+// 					// FIXME
+// 					// This assumes the original VT is on display 0 at all times,
+// 					// which is not always true
+// 					int minvt = 0;
+// 					TQStringList sessionList = TQStringList::split('\t', result, false);
+// 					for (newdisplayint = minvt; newdisplayint<MAXIMUM_VTS; newdisplayint++) {
+// 						bool displayfound = false;
+// 						for ( TQStringList::Iterator it = sessionList.begin(); it != sessionList.end(); ++it ) {
+// 							TQStringList sessionInfoList = TQStringList::split(',', *it, true);
+// 							if ((*(sessionInfoList.at(0))).startsWith(TQString(":%1").tqarg(newdisplayint))) {
+// 								displayfound = true;
+// 							}
+// 						}
+// 						if (displayfound == false) {
+// 							break;
+// 						}
+// 					}
+
+					// This external bash code does not assume that the original VT is on display 0 at all times,
+					// but it is a bit sloppy to have to call it from here.  FIXME
+					newdisplayint = systemexec("smartauth_displayfinder");
 
 					newdisplay = TQString(":%1").tqarg(newdisplayint);
 					printf("[DEBUG 400.f] The next display to start will be %s\n\r", newdisplay.ascii());
 
-					systemexec(TRINITY_BIN_PREFIX "kdmctl -g reserve");
+					if (!usebasedisplay) {
+						systemexec(TRINITY_BIN_PREFIX "kdmctl -g reserve");
+					}
 					TQString kdmctl_command = TQString("login\t%1\tnow\t%2\t%3\n").tqarg(newdisplay).tqarg(smartcard_username).tqarg(smartcard_password);
 					FILE* kdmctlpipe = popen(TRINITY_BIN_PREFIX "kdmctl -g -", "w");
 					if (pipe) {
-- 
cgit v1.2.3

