#!/bin/bash
# kiosk.create_users.sh
# part of the kiosk-shell-scripts
#
# create users with password but without a home directory 
# use with pam_mkhomedir.so to create the home directories on-the-fly

# reads ascii file in the form:
# loginname password credit
# to create users with the given amount of credit
#  passwords are encrypted with md5!
#  min_userid is set from kiosk.conf 
# 
# Usage: $0 <file>

# this script needs root privileges

. /etc/kiosk.conf
[[ ! `whoami` == root ]] && echo "root privileges required!"

[[ -z "$1" ]] && echo "Usage: $0 input-file" && exit

while read LOGIN PASSWD CREDIT
do 
	echo  "->>"$LOGIN	
	useradd   -K UID_MIN=$MIN_UID "$LOGIN"
	echo "$LOGIN:$PASSWD" |chpasswd --md5
	echo  $CREDIT > $KIOSKDIR/$LOGIN
done< $1
kiosk.delete_account.sh
#!/bin/bash
# kiosk.delete.account.sh 
# part of the kiosk-shell-scripts
#
# delete a user account that did not log in for $DAYS
# skips users that never logged in
# creates an entry in the logfile

. /etc/kiosk.conf

# list of users that did not log in (but did once)
USERS=$(lastlog -b $DAYS |grep -v "**Never logged in**"|sed '1d'|cut -f 1 -d " ")

# delete users  
for user in $USERS
do
	if awk -F: '$1=="'"$user"'" && $3>'$MIN_UID' {print $1}' /etc/passwd	
	then
		echo `date` "deleted user: $user " >> "$LOGFILE"
		# delete account and home-folder
		userdel -r "$user"
	fi
done





kiosk.manage_account.sh
#!/bin/bash
# kiosk.addcredit.sh
# part of the kiosk-shell-scripts
#

# Add credits to a user account 
# Usage: $0 [<username>]


. /etc/kiosk.conf

[[ ! `whoami` == root ]] && echo "root privileges required!"

echo "To manage credit files directly, edit $KIOSKDIR/<user>"
echo
USER=$1
if [[ -z "$USER" ]] ;then
echo "Choose a user:"
	select USER in `awk -F: '$3>'$MIN_UID' {print $1}' /etc/passwd`
	do
	break
	done
fi

# print user information
echo
echo "User: $USER"
lastlog -u $USER
echo
status=$(passwd -S $USER |cut -d ' ' -f 2)
echo "Current credit: " $(kiosk.sum.sh $USER)
echo "Account status: " $status
echo

# prompt for additional credit
select ACTION in add_credits lock_passwd set_passwd unlock_passwd exit
do 
case $ACTION in 
add_credits)
read -p "Add credit: " credit
echo
echo "Additional credit: $credit"
read -p "             ok? [y|n]" ok
	if [[ "$ok" == y ]]; then
		echo "$credit" >> $KIOSKDIR/$USER
		echo "New total credit: " $(kiosk.sum.sh $USER)
	fi
;;
unlock_passwd) # password locked
	read -p "Unlock account : [y|n]" ok
	[[ "$ok" == y ]] && passwd $USER -u
	;;
set_passwd) # no passwd
	read -p "Set passwd : [y|n]" ok
	[[ "$ok" == y ]] && passwd $USER 
	;;
lock_passwd) # password locked
	read -p "Lock account : [y|n]" ok
	[[ "$ok" == y ]] && passwd $USER -l
	;;
exit) exit
;;
esac
status=$(passwd -S $USER |cut -d ' ' -f 2)
echo "Current credit: " $(kiosk.sum.sh $USER)
echo "Account status: " $status

done
kiosk.message.sh
#!/bin/bash
# kiosk.message.sh
# part of the kiosk-shell-scripts
#
# shows a warning message on the screen of the user at certian intervals
# this script has to placed in the Autostart folder (kde)
#+ or mentioned in the  startup script of every user to display correctly


. /etc/kiosk.conf

while true
do 
	sum=$(kiosk.sum.sh `whoami`)
	# loop through all the warning messages 
	i=1
	while [[ ! -z "${WARN[$i]}" ]]
	do
		# display warning message 
		if [[ ${WARN[$i]} -eq $sum ]]; then
			xmessage -center "${MESSAGE[$i]}"
		fi
		((i++))
	done
	# wait a minute and check again	
	sleep 60
done 
kiosk.minus.sh
#!/bin/bash
# kiosk.minus.sh
# part of the kiosk-shell-scripts
#
# script to deducts one unit of credit from all users that are logged in
# it a user's credit reaches $GRACE, his/her session is terminated 
#
# it should be run by crontab every minute (or whatever time unit you choose)



. /etc/kiosk.conf

# create list of logged in users 
USERS=$(ps axo euid,user|sort |uniq|awk -F ' ' '$1>'$MIN_UID' {print $2}')

for i in $USERS 
do 
	# add -1 to each user's credit file
	echo "-1" >> $KIOSKDIR/$i 
	# kick user out if credit is below $MIN_CREDIT and lock password
	ID=$(id -g $i)
	SUM=$(kiosk.sum.sh $i)
	if [[ $SUM -lt $GRACE ]]
	then
		passwd -l $i
		# kill session first, it any
		pkill -u $i session
		# kill everything of user
		pkill -u $i			
	fi
done

kiosk.showcredit.sh
#!/bin/bash
# kiosk.showcredit.sh
# part of the kiosk-shell-scripts
#
# show credit on screen in a xmessage box
# for the user to let him/her check his/her credits
# 
# this script should be accessible via a button on the 
# menu bar, clearly visible


. /etc/kiosk.conf

USER=`whoami`

credit=$(kiosk.sum.sh $USER)

xmessage -center "
   Your credit is :  $credit

"
kiosk.sum.sh
#!/bin/bash
# kiosk.sum.sh
# part of the kiosk-shell-scripts
#

# output current amount of credit given user 
# Usage: $0 <username>

. /etc/kiosk.conf

USER=$1 
# calulate credit
credit=0
for i in `cat $KIOSKDIR/$USER`
do 
	let "credit = credit + $i" 
done

# return amount of credit
echo $credit