#!/bin/sh
#----------------------------------------------------------------------------
# samba-netsend - send message to SMB Hosts
#
# Copyright (c) 2004 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 8056 2005-03-21 18:47:59Z babel $
#
# 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

    if [ "$hosts" = "" ]
    then
    echo
    colecho "No Host specified" br x br
    echo
    exit 1
    fi

    hosts="`echo $hosts | tr 'A-X' 'a-x'`"
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

    if [ "$message" = "" ]
    then
    echo
    colecho "No Message specified" br x br
    echo
    exit 1
    fi
fi

case "$hosts"
in
    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=`netcalc network $ipaddr $netmask`
                  newbroadcast=`netcalc broadcast $network $netmask`

                  case $broadcast
                  in
                       *$newbroadcast*)
                                       ;;
                                     *)
                                       broadcast="$broadcast $newbroadcast"
                                       ;;
                  esac
        done

        hostname=`hostname | tr 'a-x' 'A-X'`

        for a in $broadcast
        do
                 /usr/local/bin/colecho "Sending messages on broadcast address $a..." bl x br

                 for x in `nmblookup '*' -B $a | grep -v "name_query failed to find name" | grep -v "Sending queries to" | cut -d" " -f1`
                 do
                           for y in `nmblookup -A $x \
                                     | grep \<00\>   \
                                     | grep -v GROUP \
                                     | grep -v 'IS~' \
                                     | sed "s/\t//" \
                                     | sed "s/ .*//"`
                           do
                                     name=`echo $y | sed "s/ //" | sed "s/\t//"`       

                                     if [ "$name" != "$hostname" -a "$name" != "" -a "$x" != "" ]
                                     then
                                         echo "Sending message to netbios name $name ($x)..."
                                         echo $message|smbclient -U "$hostname-Samba-Server" -M "$y" -I "$x" 1> /dev/null

                                         if [ "$?" != "0" ]
                                         then
                                             /usr/local/bin/colecho "Sending Message to netbios name $name ($x) failed" br x br
                                         fi
                                     fi
                           done
                 done
        done
        ;;
  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 "$hostname-Samba-Server" -M "$i" 1> /dev/null

                     if [ "$?" != "0" ]
                     then
                         colecho "Sending Message to netbios name $i failed" br x br
                     fi
            done
        fi
        ;;
      *)
        # hosts = separate list
        echo
        for i in $hosts
        do
                 echo "Sending message to "$i"..."
                 echo $message|smbclient -U "$hostname-Samba-Server" -M $i 1> /dev/null

                 if [ "$?" != "0" ]
                 then
                     colecho "Sending Message to netbios name $i failed" br x br
                 fi
        done
        ;;
esac
