#!/bin/bash

user=tluser

for i in {1..1024}
do
    # generated a $i length password
    echo "Testing with pasword length $i"
    PASS=""
    while [ $i -gt 0  ]; do
	PASS=$PASS"x"
	i=$(($i-1))
    done

    # set generated password for ${user}
    coproc passwd { passwd ${user} 2>&1 ;}
    PID=$passwd_PID
    echo $PASS >&${passwd[1]}
    echo $PASS >&${passwd[1]}
    wait $PID

    # run pamtester with the generated password
    coproc pamtester { sudo -u ${user} -- /opt/thinlinc/libexec/pamtester -v --echofirst thinlinc ${user} authenticate acct_mgmt 2>&1 ;}
    PID=$pamtester_PID
    while read -ru ${pamtester[0]} line; do
	if [ "$line" == "pamtester: performing operation - authenticate" ]; then
	    echo "$PASS"  >& ${pamtester[1]}
	fi
    done
    wait $PID
    pamtester_EXIT_STATUS=$?
    if [ $pamtester_EXIT_STATUS -ne 0 ]; then
	echo "ERROR: Authentication Failed. Exit status $pamtester_EXIT_STATUS"
    fi
done


