#!/bin/bash
#Scan for shares and make Rox apps for mount points. Pubic domain.
# merged with 01micko's update for slacko 20121028 (0.5)
[ $(id -u) -ne 0 ] && exec gtksu "Scan for Samba Shares" "$0" "$@"
PARAMS="$@"
APPDIR=`dirname "$0"` #find where we are, not $HOME.
cd "$APPDIR"
APPDIR="`pwd`"

#$1 directory
build_icons() {
	cp  $APPDIR/icons/*  "$1"
	cat > "$1/mounted.svg" << EOF
<svg width="48" height="48" id="svg1" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs id="defs1">
<linearGradient id="LG1">
   <stop style="stop-color:#00ff00;stop-opacity:1;" id="stop1"/>
   <stop style="stop-color:#333333;stop-opacity:1;" offset="1" id="stop2"/>
</linearGradient>
<radialGradient xlink:href="#LG1" id="RG1" cx="0.5" cy="0.5" r="0.5" fx="0.5" fy="0.5"/>
</defs>
<image xlink:href="$1/drive48.png" width="48" height="48" id="image1"/>
<circle cx="40" cy="8" r="8" style="fill:url(#RG1);" id="ellipse1"/>
<image xlink:href="$1/smb_overlay.png" width="48" height="48" id="image2"/>
</svg>
EOF
}

#options
TIMEOUT="-t 5"
. $APPDIR/options
if [ "$PARAMS" = "options" ] ; then
		if choice1=$(Xdialog --stdout --title "SMB browser" --checklist \
		"Options:" 20 60 6 \
         1 "Scan on port 445 instead of port 139." $PT\
         2 "Use the utf-8 character set." $CS\
         3 "Specify a different subnet to scan." $SN\
         4 "Specify the timeout for the port scan." $TO\
         ) ; then
          
				if [ "$( echo $choice1 | grep 1 )" != "" ] ; then  
				PT="on"
				else
				PT="off"
				fi
				if [ "$( echo $choice1 | grep 2 )" != "" ] ; then  
				CS="on"
				else
				CS="off"
				fi
				if [ "$( echo $choice1 | grep 3)" != "" ] ; then  
			    SN="on"
					if choice=$(Xdialog --title "SMB browser" --stdout --separator : --inputbox "    Enter a subnet to scan, just the first 3 octets.     \n     For example: 192.168.1    " 0 0 ); then
					set -- $choice
					SUBNETSALT=$choice
					else
					#cancel clicked
					exit
					fi	
				else
				SN="off"
				fi
				if [ "$( echo $choice1 | grep 4)" != "" ] ; then  
					if choice=$(Xdialog --title "SMB browser" --stdout --separator : --inputbox "    Enter a new timeout for the port scan. \n     The default is 5    " 0 0 ); then
					set -- $choice
					if [[ "$choice" = "" || "$choice" = 0 ]] ; then 
					 choice=5
					fi
					TIMEOUT="-t "$choice
					else
					#cancel clicked
					exit
					fi	
				fi
        else
        #cancel clicked
        exit
        fi	
        echo "PT="$PT > $APPDIR/options
        echo "CS="$CS >> $APPDIR/options
        echo "SN="$SN >> $APPDIR/options
        echo "TO=off" >> $APPDIR/options
        echo "SUBNETSALT="$SUBNETSALT >> $APPDIR/options
        echo "TIMEOUT=\"$TIMEOUT\"" >> $APPDIR/options
exit
fi
[ "$PT" = "on" ] && PORT=445
[ "$PT" = "off" ] && PORT=139
[ "$CS" = "on" ] && IOCHARSET=",iocharset=utf8"
[ "$CS" = "off" ] && IOCHARSET=""
[ "$SN" = "off" ] && SUBNETS="$(ifconfig | grep inet | grep -v 127.0.0.1 | cut -d ":" -f 2 | cut -n -d "." -f 1-3 | cut -d " " -f 1)" #find our subnet.
[ "$SN" = "on" ] && SUBNETS=$SUBNETSALT

#set up some variables.
[ ! -d  /tmp/sambabrowser-tmp-mount ] && mkdir /tmp/sambabrowser-tmp-mount
MNTDIR=$(echo $APPDIR | sed 's,/*[^/]\+/*$,,')
SCANSPEED=1
TMP="/tmp/smbscanner-"
rm ${TMP}*
if [ "$SUBNETS" = "" ] ; then
Xdialog --title "$APPTITLE" --stdout --ok-label "OK" --title "SMB browser" --infobox "It dosen't seem like you are connected to your network. \n You need to be on a network to share files." 0 0 90000
exit
fi
#scan for servers, hacked mpscan to time out faster.
yaf-splash -fg black -transparent -placement top -text "Scanning Network, please wait." &
YAFPID=$!


for SUBNET in $SUBNETS; do
  mpscan $TIMEOUT -p $PORT $SUBNET.1 - $SUBNET.254 | cut -d " " -f 2 >> ${TMP}temp
done

#scan servers for shares
for IP in $(cat ${TMP}temp) ; do
PWFL=""; Username="" ; Password="" #note that Samba on linux with user level security will reject Username="guest" and causes problems with smbclient-3.6.12 
if [ "$(smbclient -g -N -L $IP | grep 'failed')" != "" ] ; then #need password ## 620-need to grep for 'failed' instead of 'Error returning browse list'
	if choice=$(Xdialog --title "$APPTITLE" --stdout --yesno "The server named $(nmblookup -A $IP | grep "<20>" | cut -d " " -f 1 | sed 's/^[ \t]*//') seems to need a password. Do you want to enter one?" 0 0) ; then 
		msg1="Enter the user name and password."
		msg2="User name"
		msg3="Password"
		if choice=$(Xdialog --title "$APPTITLE" --stdout --separator : --2inputsbox "$msg1" 0 0 "$msg2" "$Username" "$msg3" "$Password"); then
		set -- $choice
		Username=${1%%:*}
		Password=${1#*:}
		PWFL=true
        else
        #cancel clicked
		kill $YAFPID
        exit
        fi	  
	fi
fi
#[ "$Username" = "" ] && Username="guest" # added by 01micko but not working for jamesbond
[ "$(smbclient -U $Username%$Password -g -L $IP | grep Disk | cut -d "|" -f 2)" = "" ] && continue  #no shared folders

smbclient -U $Username%$Password -g -L $IP | grep Disk | cut -d "|" -f 2 |grep -v "[A-Z]\$$"|grep -v "[a-z]\$$"> ${TMP}shares #filter some garbage here
NAME=$(nmblookup -A $IP | grep "<20>" | cut -d " " -f 1 | sed 's/^[ \t]*//') #couldn't reliably grep server name from smbclient.
for SHARE in $(cat ${TMP}shares|sed 's/ /___/g') ; do #Make rox app for each share.
SHARE2=$(echo "$SHARE" | sed -e 's/[!@#\$%^&*()]//g' \
                             -e 's/___/ /g') #remove some ugly characters and replace space with underscore
                             
#check for bogus Windows share or Samba linux share that needs a password.
[ "$Username" = "" ] && Username="guest"                     
#echo "mount.cifs "//$IP/\"$SHARE2\"" "/tmp/sambabrowser-tmp-mount" -o "username=$Username,password=$Password,sec=ntlm,noserverino$IOCHARSET"" > /tmp/trymount #Linux-3.8 now needs sec=ntlm
echo "mount.cifs "//$IP/\"$SHARE2\"" "/tmp/sambabrowser-tmp-mount" -o "username=$Username,password=$Password,noserverino$IOCHARSET"" > /tmp/trymount #removing sec=ntlm for 3.9 onwards
    . /tmp/trymount
    retval=$?
    rm /tmp/trymount
if [ $retval -ne 0 ] ; then
	if [ "$PWFL" != "" ] ; then
	umount.cifs -n /tmp/sambabrowser-tmp-mount
	continue
	fi   
		if choice=$(Xdialog --title "$APPTITLE" --stdout --yesno "The share named $SHARE2 seems to need a password. Do you want to enter one?" 0 0) ; then 
			msg1="Enter the user name and password."
			msg2="User name"
			msg3="Password"
			if choice=$(Xdialog --title "$APPTITLE" --stdout --separator : --2inputsbox "$msg1" 0 0 "$msg2" "$Username" "$msg3" "$Password"); then
			set -- $choice
			Username=${1%%:*}
			Password=${1#*:}
			else
			#cancel clicked
			kill $YAFPID
			exit						
			fi	
		else
		umount.cifs -n /tmp/sambabrowser-tmp-mount
		continue	
		fi
fi

umount.cifs -n /tmp/sambabrowser-tmp-mount

SHAREAPPDIR="${SHARE2// /_}" #get rid of spaces,replace with underscore $APPDIR only!
SHAREAPPDIR=${SHAREAPPDIR//\'/} #get rid of apostrophes if there
mkdir -p $MNTDIR/$NAME"--""$SHAREAPPDIR"/mnt-point
build_icons $MNTDIR/$NAME"--""$SHAREAPPDIR"
ln -sfT drive48.png $MNTDIR/$NAME"--""$SHAREAPPDIR"/.DirIcon
cat $APPDIR/scripts/part1 > $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
echo "Name="${NAME[$I]} >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
echo "Share=""\"$SHARE2\"" >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
echo "Username="$Username >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
if [[ "$Password" = "guest" || "$Password" = "" ]];then
  echo "Password="$Password >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
  else #encrypt
  echo "Password="$(printf "$Password"|base64) >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
fi
echo "IOCHARSET="$IOCHARSET >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
echo "PORT"=$PORT >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
[ "$SN" = "on" ] && echo "SUBNETSALT="$SUBNETSALT >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
cat $APPDIR/scripts/part2 >> $MNTDIR/$NAME"--""$SHAREAPPDIR"/AppRun
cp $APPDIR/scripts/AppInfo.xml $MNTDIR/$NAME"--""$SHAREAPPDIR"/
chmod -R 755 $MNTDIR/$NAME"--""$SHAREAPPDIR"
done
done
kill $YAFPID


