#!/bin/sh
#
#    Copyright (c) 2002 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   logout
#    Module name: fabos/src/security/utils.c
#
#    This script will logout all sessions of a particular TTY
#    on a switch.
#
#    Usage: cmd <switch instance> <TTY>
#    <switch instance> On a Ulysses is 0 or 1.  On a Terminator is 0.
#    <TTY> can be ttyS0(serial), ttyS1(modem), or pts(remote).
#
export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin

# Make sure there are exactly two arguments
if ! [ $# -eq 2 ]; then
	echo Invalid number of arguments: $#
	exit
fi

#	build the actual switch name. We must have received an integer (0 or 1)
switchName=switch$1

# Information for User
wall Security policy change: TTY $2 on switch instance $1 will be logged out.

# Loop through all 'w' entries with with correct TTY
for tty in `w | sed -e 's/root/switch/g' | sed -e 's/factory/switch/g' |
               sed -e 's/admin/switch/g' | sed -e 's/user/switch/g' |
               grep $2 | sed -e 's/    / /g' | sed -e 's/   / /g' | 
               sed -e 's/  / /g' | grep $switchName | cut -d' ' -f2`
do
	pid=`fuser /dev/$tty | sed -e "s,^/dev/[[:alnum:]/]*:[[:space:]]*,,g"`

	if test "$pid" = ""
	then
		echo "Error: Could not find sh or rbash for" $tty"."
		exit
	fi

#	echo Killing PID $pid
	kill -9 $pid
done
