#!/bin/sh
#----------------------------------------------------------------------------
# samba-netsend  - send message to SMB Hosts
#
# Copyright (c) 2001-2003 Thomas Bork, tom(at)fli4l(dot)de
#
# usage: samba-netsend
#    or: samba-netsend { all | active | host1 host2 ... } "message"
#
# Creation:     04.11.2001  tb
# Last Update:  $Id: samba-netsend,v 1.4 2003/09/27 14:01:14 knuffel Exp $
#
# 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.
#----------------------------------------------------------------------------

# set -x

case $#
in
     0)
       interactive='true'

       hosts=''
       message=''
       broadcast=''
       ;;
     2)
       interactive='false'

       hosts="$1"
       message="$2"
       broadcast=''
       ;;
     *)
       echo "usage: `basename $0`" >&2
       echo "   or: `basename $0` { all | active | host1 host2 ... } \"message\"" >&2
       exit 1
       ;;
esac


if [ "$interactive" = "true" ]
then
    clrhome
    colecho "Send Message to SMB Hosts" gn
    echo
fi
	    
if [ "$hosts" = "" ]
then
    echo -e "To which SMB Hosts the message should be send?"
    echo
    echo -e "Choice 1"
    echo -e "--------"
    echo -e "All SMB Hosts on configured Subnets on fli4l - type 'all'."
    echo
    echo -e "Choice 2"
    echo -e "--------"
    echo -e "fli4l Samba Clients with active connections - type 'active'."
    echo
    echo -e "Choice 3"
    echo -e "--------"
    echo -e "One ore more active SMB Hosts, type NETBIOS Names"
    echo -e "separated with a blank, for instance 'client1 client2':"
    echo
    read hosts
fi
	
if [ "$hosts" = "" ]
then
    echo
    colecho "No Host specified" br x br
    echo
    exit 1
fi

if [ "$interactive" = "true" ]
then
    clrhome
    colecho "Send Message to SMB Hosts" gn
    echo
fi

if [ "$message" = "" ]
then
    echo
    echo -e "Which Message should be send?"
    echo -e "For instance 'fli4l Samba Server is going down in 3 Minutes ...':"
    echo
    read message
fi
	
if [ "$message" = "" ]
then
    echo
    colecho "No Message specified" br x br
    echo
    exit 1
fi

if [ "$hosts" != "" -a "$message" != "" ]
then

    if [ "$hosts" = "all" -o "$hosts" = "All" -o "$hosts" = "ALL" -o "$hosts" = "active" -o "$hosts" = "Activ" -o "$hosts" = "ACTIV" ]
    then
        if [ "$hosts" = "all" -o "$hosts" = "All" -o "$hosts" = "ALL" ]
	then
            # hosts = all

            for i in `grep "   interfaces = " /etc/smb.conf | cut -c17-`
            do
                      ipaddr=`echo $i | cut -d"/" -f1`
 	              netmask=`echo $i | cut -d"/" -f2`
 	              network=`/usr/local/bin/netcalc network $ipaddr $netmask`
                      newbroadcast=`/usr/local/bin/netcalc broadcast $network $netmask`
 
                      if [ "$broadcast" = "" ]
		      then
		          broadcast=$newbroadcast
		      else
		          if [ -z "`echo $broadcast | grep $newbroadcast`" ]
		          then
		              broadcast="$broadcast $newbroadcast"
		          fi
		      fi
            done
 
            hostname=`hostname | sed y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/`

            for a in $broadcast
            do
                      echo
                      echo sending message, please wait ...
		      echo working on broadcast address $a ... :
		      echo

                      for x in `nmblookup '*' -B $a | grep -v "Sending queries to" | cut -d" " -f1`
                      do
                                for y in `nmblookup -A $x \
                                          | grep \<00\>   \
                                          | grep -v GROUP \
                                          | sed "s/\t//" \
                                          | sed "s/ .*//" \
				          | grep -v \$hostname`
                                do
	                                  echo sending message to "$y" ...
	                                  echo $message|smbclient -U "fli4l-Samba-Server" -M "$y" 1> /dev/null
                                done
	              done
		      echo
            done
        else
            # hosts = activ

            status=`smbstatus -b \
                    | grep -vi "Samba version 1.9.18p10" \
                    | grep -vi "PID     Username  Machine                       Time logged in" \
                    | grep -vi "-" | grep '[1-9]' | cut -c19-48`

            if [ "$status" = "" ]
            then
	        echo
	        colecho "No fli4l Samba Client activ" br x br
                echo
	        exit 1
            else
	        echo
                for i in $status
                do
                         echo sending message to "$i" ...
                         echo $message|smbclient -U "fli4l-Samba-Server" -M "$i" 1> /dev/null
                         if [ "$?" != "0" ]
	                 then
	                     echo
	                     colecho "Sending Message to one or more Clients failed" br x br
	                 fi
                done
	    fi
        fi
    else
        # hosts = separate list
	echo
        for i in $hosts
        do
	         echo sending message to "$i" ...
                 echo $message|smbclient -U "fli4l-Samba-Server" -M $i 1> /dev/null
                 if [ "$?" != "0" ]
	         then
                     echo
	             colecho "Sending Message to one or more SMB Hosts failed" br x br
	         fi
        done
    fi
fi
