#!/bin/sh
#
# CGI-script for logged-in users list...
#
# Copyright 2000 Wilmer van der Gaast (lintux@dds.nl)
#

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

. $WEBSERVE_ROOT/cgi-bin/functions
. $WEBSERVE_ROOT/smart.conf

if [ "$2" != "x" ]; then
	echo Content-Type: text/html
	echo;
fi

#checkrefer
#checkparams $*

declare -i i

if [ -n "$*" ]; then
	EXPR="`echo $* | cut -d\& -f1`"
	if [ "$EXPR" = "$*" ]; then
		export $*;
	else
		i=1
		while [ -n "$EXPR" ]; do
			export $EXPR
			EXPR=`echo $* | cut -d\& -f $i`
			i=i+1;
		done;
	fi;
fi

if [ -n "$LOGOUT" ]; then
	ps aux | grep nwconn | grep -v grep | {
		read LINE
		while [ -n "$LINE" ]; do
			PID=`echo $LINE | gawk '{ print $2; }'`
			CONN=`cat /proc/$PID/cmdline | tr '\000' '.' | cut -d. -f2`
			if [ $CONN = $USER ]; then
				kill $PID;
			fi
			read LINE
		done;
	}
	unset LOGOUT
	$0 list x
	exit;
	
elif [ $1 = "list" ]; then
	cat<<EOF
<HTML>
<HEAD>
<TITLE>SMArT - Security - User list</TITLE>
</HEAD>
<BODY BGCOLOR=#C0C0C0>
<FORM ACTION="/cgi-bin/logins" METHOD=GET>
<TABLE BORDER>
<TR>
<TH COLSPAN=3 BGCOLOR=$HEADER_BACK ALIGN=LEFT>
<TABLE BORDER=0 FRAME=0 CELLSPACING=0 CELLPADDING=0>
<TR>
<TD><IMG SRC="/smart_icon.jpg"></TD>
<TD><FONT COLOR=$HEADER_BACK>-</FONT></TD>
<TH><FONT COLOR=$HEADER_FORE>Users</FONT></TH>
</TR>
</TABLE>
<TR><TH>Sel<TH>Connection number<TH>NetWare user name
`nwuserlist $SERVER_LOGIN | cut -b-27 | {
	read LINE
	while [ -n "$LINE" ]; do
		i=\`echo $LINE | cut -d: -f1\`
		NAME=\`echo $LINE | cut -d: -f2\`
		echo -n \<TR\>\<TD\>
		if [ -z "$NAME" ]; then
			NAME="-";
		else
			echo -n \<INPUT TYPE=RADIO NAME=\"USER\" VALUE=\"$i\"\>
		fi
		echo -n \<TD\>
		echo -n $i
		echo -n \<TD\>
		echo    $NAME
		read LINE
	done;
}`
<TR>
<TD COLSPAN=3 ALIGN=CENTER>
<INPUT TYPE=SUBMIT NAME="LOGOUT" VALUE="Logout">
</TABLE>
</BODY>
</HTML>
EOF
fi
