#!/bin/bash
# REM
echo -e "SCO'S  -  P R O P E R T Y ?!  -  ALARMOMETER  - v0.6\n"

# config
me=`echo $0 | cut -d "/" -f2`
# targets = scripts von asciilab, die auf patent-verletzungen mit targetfiles (linux-src) verglichen werden
targetpath="/home/asciilab/scripts/"	# other scripts
tmptargetfile=".target.tmp"				# tempfile fuer kommentare aus targetfile
# origs = linux-src files mit angeblich patentiertem inhalt
origfiles="/usr/include/asm/errno.h /usr/include/asm/signal.h /usr/include/linux/ctype.h /usr/include/asm/ioctl.h /usr/include/asm/ioctls.h /usr/include/linux/ipc.h /usr/include/linux/acct.h /usr/include/linux/a.out.h"
tmporigfile=".orig.tmp"					# tempfile fuer kommentare aus origfile
tmplsoffile=".lsof.tmp"


# intro (inkl. log)
echo -e "\n\"the developers' comments in the code are basically your DNA, or \nfingerprints, for a particular piece of source code\" (Laura DiDio for SCO)"
echo -e "starting to compare running asciilab scripts with linux source files \nviolating SCO's rights in UNIX as they contain certain copyrighted application \nbinary interfaces ('ABI Code'). \nto compare the software uses this great \ncomment=DNA/fingerprint logic."
logger -t $me "starting to compare running asciilab scripts with linux source files ($origfiles) violating SCO's rights in UNIX as they contain certain copyrighted application binary interfaces ('ABI Code')."
echo -e "let's start...\n"
sleep 8



# kommentare (/* */) aus origfiles in tmporigfile schreiben
# file leeren
echo -n "" > $tmporigfile
# schlaufe ueber mehrere files
for f in $origfiles; do
	# einzeilen-kommentare version
	#origs=`cat $f | grep -o "/\*.*\*/" | cut -d "*" -f2`
	# mehrzeilen-kommentare version
	origs=`./sed.sh $f`
	echo "$origs" >> $tmporigfile
done
# log ($0=scriptname)
logger -t $me "$tmporigfile written; it contains comments of linux source files ($origfiles)."



# anzahl targetfiles: targetfiles-liste aus laufenden prozessen auslesen und snapshot in file schreiben
filenr=1
targetfilesnap=`/usr/sbin/lsof-4.7 +c 0 -u asciilab | grep -v $me | egrep "DIR.*$targetpath" | egrep "(\.sh\>|\.py\>)"`
echo "$targetfilesnap" > $tmplsoffile
# files zaehlen und anzahl ausgeben
if [ "$targetfilesnap" = "" ] ; then
	# kein file (aber wegen \n wuerde faelschlicherweise 1 file gezaehlt)
	fileamount=0
else
	# n files
	fileamount=`echo "$targetfilesnap" | wc -l`
fi
# log
echo ""
logger -st $me "amount of running asciilab scripts: $fileamount"
logger -t $me "running asciilab scripts: $targetfilesnap"



# schlaufe fuer jedes targetfile
while [ $filenr -le $fileamount ] ; do

	# targetfiles aus laufenden prozessen auslesen
	targetfileline=`echo "$targetfilesnap" | head -$filenr | tail -1`
	targetfileline=`echo -n $targetfileline | awk '{print $2 " " $9"/"$1}'`
	
	targetfilepid=`echo $targetfileline | awk '{print $1}'`
	targetfile=`echo $targetfileline | awk '{print $2}'`
	echo ""
	echo "--------- $filenr - $targetfile - $targetfilepid ---------"
	
	# kommentare (#) aus targetfile in tmptargetfile
	targets=`cat $targetfile | grep -o "#.*" | grep -v '#!' | cut -d "#" -f2`
	echo "$targets" > $tmptargetfile
	# log ($0=scriptname)
	logger -t $me "$tmptargetfile written; it contains comments of running asciilab script nr. $filenr: $targetfile."
	echo -e "loading file and extract its comments...\n"
	# lesepause
	sleep 1.5
	
	# inc
	filenr=`expr $filenr + 1`
	
	
	# file vergleichen...
	
	# linecount auf startzeile
	linec=1
	# linien und woerter zŠhlen
	lineamount=`echo "$targets" | wc -l`
	
	# count variable fuer anzahl uebereinstimmungen
	itotal=0
	# schlaufe Ÿber linien des tmptargetfiles
	while [ $linec -le $lineamount ] ; do
		
		# wordcount startwort-position (in line)
		wordc=1
		
		# debug
		#echo "linec: $linec, lineamount: $lineamount"
		
		# linie aus file holen
		theline=`echo "$targets" | head -$linec | tail -1`	# trim?
		# anzahl wšrter in linie zŠhlen
		wordamount=`echo $theline | wc -w`
		
		# debug: anzahl wšrter und startwort-position ausgeben
		#echo "wordc: $wordc, wordamount: $wordamount"
		
		# schlaufe durch die zeile und jedes wort mit origfile vergleichen
		while [ $wordc -le $wordamount ] ; do
			#echo "----"
			theword=`echo $theline | cut -d " " -f $wordc`
			#echo "scanning for word: '$theword'"
			# anzahl identischer woerter
			thewordamount=`grep -c -e "$theword" $tmporigfile`
			#echo "thewordamount=\`grep -c -e \"$theword\" $tmporigfile\`"
			#echo "-> found it $thewordamount times"
			# inc
			wordc=`expr $wordc + 1`
			
			# schlaufe zur ausgabe des alarmometers fuer thewordamount: n+1 stern fuer gefundene woerter
			i=0
			while [ $i -le $thewordamount ] ; do
				echo -n "*"
				i=`expr $i + 1`			# inc
				sleep 0.05
			done
			echo " - '$theword'"
			# anzahl uebereinstimmungen nachfuehren
			i=`expr $i - 1`
			if [ $i -gt 0 ] ; then
				itotal=`expr $itotal + $i`	# inc
				# log
				logger -t $me "$targetfile (PID $targetfilepid): word '$theword' matched $i times."
			fi
			#echo ""
			#sleep 0.1
		done
		# words of line
		
		# inc
		linec=`expr $linec + 1`
	done
	# lines of file
	
	# anzahl uebereinstimmungen ausgeben
	echo -e "\033[1m----------------------------- \nrights-violation-value: $itotal \n-----------------------------\033[0m"
	# log
	logger -t $me "script $targetfile (PID $targetfilepid) has a rights-violation-value of $itotal."
	
	# display reset
	tput sgr0
	
	# warnung an shell ausgeben
	if [ $itotal -ge 50 ] ; then
		# ttyp bzw. pts rauslesen
		pts=`ps -A -eo "%p %y" | grep $targetfilepid | awk '{print $2}'`
		#debug
		#pts=pts/8
		# ttyp/pts unbekannt?
		if [ "$pts" = "?" -o "$pts" = "" ] ; then
			# ttyp/pts unbekannt, warnung kann nicht geschickt werden. stattdessen ausgabe und such-aufruf?
			echo -e '\E[37;45m'"\033[1m\nANONYMOUS ALARM: \nSCO tried to send a warning to the holder of script \n$targetfile (PID $targetfilepid) \nbecause its rights-violation-value ($itotal) is too high. \nbut unfortunately the holder isn't identifiable.\033[0m"
			# log
			logger -t $me "ANONYMOUS ALARM: SCO tried to send a warning to the holder of script $targetfile (PID $targetfilepid), because its rights-violation-value ($itotal) is too high. \nbut the holder isn't identifiable."
		else
			# ttyp/pts bekannt, also ip-adresse/url auslesen
			ip=`who --lookup | grep "$pts" | awk '{print $6}' | tr -d "(" | tr -d ")"`
			# farbige warnung schreiben
			warning="\n  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n  *  Dear Linux User,                                                       *\n     You ($ip) have been identified\n     by SCO as the holder of the script                                      \n     '$targetfile'\n     running on $HOSTNAME with PID $targetfilepid.\n     In May 2003, SCO warned about enterprise use of the Linux operating     \n     system in violation of its intellectual property rights in UNIX         \n     technology.                                                             \n     The Linux version of this computer ($HOSTNAME) is violaiting SCO's\n     rights in UNIX as it contains certain copyrighted application binary    \n     interfaces ('ABI Code').                                                \n     Ongoing reviews have disclosed that your script                         \n     '$targetfile' (PID $targetfilepid)\n     is in inflated compliance with the concerned sourcecode.                \n     Your script has a potential rights-violation-value of $itotal.\n     SCO demandes the cancelation of this script within the next seconds.    \n                                                                             \n     Thank you for your attention to these matters.                          \n  *  Sincerely, THE SCO GROUP, INC. Febraury, 2004                          *\n  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"
			tput sgr0; echo -e '\E[37;41m'"\033[1m$warning\033[0m" > /dev/$pts
			echo -e '\E[37;41m'"\033[1m\nALARM: \nSCO sent warning to /dev/$pts ($ip), \nholder of script \n$targetfile (PID $targetfilepid), \nbecause its rights-violation-value of $itotal is too high. \nthe holder is asked to shutdown the script.\033[0m"
			# log
			logger -t $me "ALARM: SCO sent warning to /dev/$pts, holder of script $targetfile (PID $targetfilepid), because its rights-violation-value ($itotal) is too high: $warning"
		fi
	else
		# log
		echo -e '\E[37;42m'"\033[1m\nPASSED: \nwith a rights-violation-value of $itotal, script \n$targetfile (PID $targetfilepid) \nis unsuspicious.\033[0m"
		logger -t $me "PASSED: with a rights-violation-value of $itotal, script $targetfile (PID $targetfilepid) is unsuspicious."
	fi
	
	# visuelle trennung fuer naechstes targetfile
	echo -e "---------------------------------------------------------------------\n---------------------------------------------------------------------\npause...\n\n"
	# lesepause
	sleep 4
	
done
# while targetfile

echo "finished."