#------------------------------------------------------------------------------
# /etc/rc.d/rc455.samba - start samba                              3.x.x
# was file /etc/rc.d/rc700.samba
#
# Creation   : 21.08.2000  fm
# Last Update: $Id: rc455.samba 9333 2005-07-10 02:54:43Z knuffel $
#------------------------------------------------------------------------------

if [ "`echo $OPT_SAMBA | tr 'A-Z' 'a-z'`" = "yes" ]
then

begin_script SAMBA "configuring samba on $HOSTNAME ..."

#----------------------------------------------------------------------------
# configuring parameters from config.txt:
#----------------------------------------------------------------------------

SAMBA_ADMINIP=''
interface=''
sambahosts=''
start_samba=false
logdir_error=true
data_mount=`cat /proc/mounts | grep "/data" | grep "rw"`

if [ -f /usr/bin/logger ]
then
    msgcmd="/usr/bin/logger -t samba_lpd -p local0"
    msgcmdfail="$msgcmd.error "
    msgcmdinfo="$msgcmd.info "
else
    msgcmd="echo "
    msgcmdfail="echo "
    msgcmdinfo="echo "
fi

idx='1'
idy='0'

mkdir -p /usr/local/samba/var/locks
mkdir -p /usr/local/samba/private
mkdir -p /var/lock/samba
>/var/lock/samba/STATUS..LCK
>/etc/smbpasswd

if [ ! -f /etc/smb.conf ]
then
    overwrite=true
else
    overwrite=false
fi

if [ "$overwrite" = "true" ]
then
    #----------------------------------------------------------------------------
    # configuring interfaces and networks for samba:
    #----------------------------------------------------------------------------

    for idx in `seq 1 $IP_NET_N`
    do
        eval iptmp='$IP_NET_'$idx''
        eval ethx='$IP_NET_'$idx'_DEV'
        ipaddr=`echo $iptmp | sed -e 's/\/.*//'`

        dhciface=no

        for i in $DHCLIENT_INTERFACES $DHCPCD_INTERFACES
        do
              if [ "$i" = "$ethx" ]
              then
                  $msgcmdinfo "$ethx is an dhcp client interface, don't configuring samba for it..."
                  dhciface=yes
              fi
        done

        if [ "$dhciface" = "no" ]
        then
            if [ "$ipaddr" != "0.0.0.0" -a "$ipaddr" != "" ]
            then
                eval net='$IP_NET_'$idx''
                netmask=`netcalc netmask $net`
                network=`netcalc network $net`

                if [ -z "$interface" ]
                then
                    interface="$ipaddr/$netmask"
                    sambahosts="$network/$netmask"
                else
                    interface="$interface $ipaddr/$netmask"
                    sambahosts="$sambahosts $network/$netmask"
                fi
            fi
        fi
    done

    if [ "`echo $OPT_PPP | tr 'A-Z' 'a-z'`" = "yes" ]
    then
        eval network='$PPP_NETWORK'
        eval netmask='$PPP_NETMASK'

        if [ -z "$interface" ]
        then
            interface="$PPP_IPADDR/$netmask"
            sambahosts="$network/$netmask"
        else
            interface="$interface $PPP_IPADDR/$netmask"
            sambahosts="$sambahosts $network/$netmask"
        fi
    fi

    if [ -n "$SAMBA_TRUSTED_NETS" ]
    then
        for i in $SAMBA_TRUSTED_NETS
        do
            network=`netcalc network $i`
            netmask=`netcalc netmask $i`
            var=$network/$netmask

            if [ -z "$sambahosts" ]
            then
                sambahosts="$var"
            else
                if [ -z "`echo $sambahosts | grep $var`" ]
                then
                    sambahosts="$sambahosts $var"
                fi
            fi
        done
    fi

    if [ -z "$interface" ]
    then
        start_samba="false"
        colecho "*** ERROR: no interfaces available - samba will be not activated! ***" br x br
        $msgcmdfail "*** ERROR: no interfaces available - samba will be not activated! ***"
        sleep 5
    else
        start_samba="true"
        $msgcmdinfo "samba on $HOSTNAME will be activated for interface(s):"
        $msgcmdinfo "$interface"
        $msgcmdinfo "samba on $HOSTNAME will be activated for network(s):"
        $msgcmdinfo "$sambahosts"
    fi

    wins_support="no"
    wins_proxy="no"
    wins_server=''

    if [ "`echo $OPT_NMBD | tr 'A-Z' 'a-z'`" = "yes" ]
    then
        if [ "`echo $NMBD_MASTERBROWSER | tr 'A-Z' 'a-z'`" = "yes" ]
        then
            oslevel="255"
            localmaster="yes"
            preferredmaster="yes"

            idx='1'
            announce=''
            sync=''

            while [ "$idx" -le "$NMBD_REMOTE_ANNOUNCE_N" ]
            do
                  eval network='$NMBD_REMOTE_ANNOUNCE_'$idx'_NETWORK'
                  eval netmask='$NMBD_REMOTE_ANNOUNCE_'$idx'_NETMASK'
                  broadcast=`netcalc broadcast $network $netmask`
                  eval workgroup='$NMBD_REMOTE_ANNOUNCE_'$idx'_WORKGROUP'

                  if [ -z "$announce" ]
                  then
                      announce="$broadcast/$workgroup"
                      sync="$broadcast"
                  else
                      announce="$announce $broadcast/$workgroup"
                      sync="$sync $broadcast"
                  fi
                  idx=`expr $idx + 1`
            done

            if [ "`echo $NMBD_DOMAIN_MASTERBROWSER | tr 'A-Z' 'a-z'`" = "yes" ]
            then
                domainmaster="yes"
            else
                domainmaster="no"
            fi
        else
            oslevel="0"
            localmaster="no"
            preferredmaster="no"
            domainmaster="no"
        fi

        #----------------------------------------------------------------------------
        # create wins options:
        #----------------------------------------------------------------------------

        if [ "`echo $NMBD_WINSSERVER | tr 'A-Z' 'a-z'`" = "yes" ]
        then
            wins_support="yes"
            $msgcmdinfo "samba on $HOSTNAME will act as an WINS-Server"
        else
            if [ "$NMBD_EXTWINSIP" != "" ]
            then
                wins_server="$NMBD_EXTWINSIP"
                wins_proxy="yes"

                $msgcmdinfo "external WINS-Server for $HOSTNAME is:"
                $msgcmdinfo "$wins_server"
                $msgcmdinfo "samba on $HOSTNAME will act as an WINS-Proxy of:"
                $msgcmdinfo "$wins_server"
            fi
        fi
    else
        oslevel="0"
        localmaster="no"
        preferredmaster="no"
        domainmaster="no"
    fi

    #----------------------------------------------------------------------------
    # create global config, could be overwrite with shares:
    #----------------------------------------------------------------------------

   {
    echo "#----------------------------------------------------------------------------"
    echo "# /etc/smb.conf"
    echo "# automatically created by rc455.samba"
    echo "#----------------------------------------------------------------------------"
    echo "   "
    echo "[global]"
    echo "   workgroup = $SAMBA_WORKGROUP"
    echo "   serverstring = samba-server on $HOSTNAME"
    echo "   interfaces = $interface"
    echo "   os level = $oslevel"
    echo "   local master = $localmaster"
    echo "   preferred master = $preferredmaster"
    echo "   domain master = $domainmaster"
    echo "   wins support = $wins_support"
    echo "   wins server = $wins_server"
    echo "   wins proxy = $wins_proxy"
    echo "   dns proxy = no"
    echo "   time server = yes"
    echo "   hosts allow = localhost $sambahosts"
    echo "   guest account = nobody"
    echo "   keep alive = 30"
    echo "   browseable = yes"
    echo "   public = yes"
    echo "   security = share"
    echo "   printing = bsd"
    echo "   printcap name = /etc/printcap"
    echo "   load printers = no"
    echo "   bind interfaces only = yes"
    echo "   socket options = TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE SO_RCVBUF=8192 SO_SNDBUF=8192"
    echo "   case sensitive = no"
    echo "   default case = lower"    
    echo "   preserve case = yes"
    echo "   short preserve case = yes"
    echo "   dos filetimes = yes"
    echo "   dos filetime resolution = yes"
    echo "   deadtime = 5"
    echo "   dfree command = /usr/bin/dfree"
    echo "   remote announce = $announce"
    echo "   remote browse sync = $sync"
    echo "   encrypt passwords = yes"
    echo "   delete readonly = yes"
    echo "   message command = rm %s &"
    echo "   unix charset = ISO8859-1"
    echo "   dos charset = 850"
    echo "   "
   } > /etc/smb.conf

    #----------------------------------------------------------------------------
    # create share for fli4l root directory:
    #----------------------------------------------------------------------------

    if [ "$SAMBA_ADMINIP" != '' ]
    then
        rootavailable="yes"
        rootbrowseable="yes"
        adminip=$SAMBA_ADMINIP
        $msgcmdinfo "root-directory on $HOSTNAME will be accessable for network(s):"
        $msgcmdinfo "$adminip"

       {
        echo "[root]"
        echo "   comment = root-filesystem on $HOSTNAME"
        echo "   browseable = $rootbrowseable"
        echo "   available = $rootavailable"
        echo "   read only = no"
        echo "   path = /"
        echo "   hosts allow = $adminip"
        echo "   force user = root"
        echo "   dont descend = proc"
        echo "   "
       } >> /etc/smb.conf
    fi

    #----------------------------------------------------------------------------
    # samba file server
    #----------------------------------------------------------------------------   
   
    if [ -f /etc/rc.d/file_shares.sh ]
     then
        . /etc/rc.d/file_shares.sh
    fi

    #----------------------------------------------------------------------------
    # configuring printer share(s):
    #----------------------------------------------------------------------------

    eval do_printing='$do_printing'

    if [ "`echo $OPT_LPD | tr 'A-Z' 'a-z'`" = "yes" ]
    then
        if [ "$do_printing" = "yes" ]
        then
            if [ "`echo $OPT_LPD_PARPORT | tr 'A-Z' 'a-z'`" = "yes" ]
            then
                #----------------------------------------------------------------------------
                # create share(s) for local parallel printer(s):
                #----------------------------------------------------------------------------

                idx='1'

                while [ "$idx" -le "$LPD_PARPORT_N" ]
                do
                      eval lpname='$SAMBA_LPD_PARPORT_'$idx'_NAME'
                      eval lpnet='$SAMBA_LPD_PARPORT_'$idx'_NET'
                      eval port='$LPD_PARPORT_'$idx'_IO'

                      if [ "$lpname" != '' ]
                      then
                          name="$lpname"
                      else
                          name=pr$idx
                      fi

                      if [ "$lpnet" != '' ]
                      then
                          lpnet1=$lpnet
                          $msgcmdinfo "printer pr$idx on $HOSTNAME at port $port will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      else
                          lpnet1=$sambahosts
                          $msgcmdinfo "printer pr$idx on $HOSTNAME at port $port will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      fi

                     {
                      echo "[$name]"
                      echo "   comment = local printer pr$idx on $HOSTNAME at port $port"
                      echo "   browseable = yes"
                      echo "   printable = yes"
                      echo "   print command = lpr -h -r -P%p %s"
                      echo "   lprm command = lprm -P%p %j"
                      echo "   lpq command = lpq -P%p"
                      echo "   create mode = 0700"
                      echo "   printer = pr$idx"
                      echo "   path = /var/spool/lp/pr$idx"
                      echo "   hosts allow = $lpnet1"
                      echo "   "
                     } >> /etc/smb.conf

                      idx=`expr $idx + 1`
                done
            fi

            if [ "`echo $OPT_LPD_USBPORT | tr 'A-Z' 'a-z'`" = "yes" ]
            then
                #----------------------------------------------------------------------------
                # create share(s) for local usb printer(s):
                #----------------------------------------------------------------------------

                idx='1'

                while [ "$idx" -le "$LPD_USBPORT_N" ]
                do
                      eval lpname='$SAMBA_LPD_USBPORT_'$idx'_NAME'
                      eval lpnet='$SAMBA_LPD_USBPORT_'$idx'_NET'

                      if [ "$lpname" != '' ]
                      then
                          name="$lpname"
                      else
                          name=usbpr$idx
                      fi

                      if [ "$lpnet" != '' ]
                      then
                          lpnet1=$lpnet
                          $msgcmdinfo "printer usbpr$idx on $HOSTNAME will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      else
                          lpnet1=$sambahosts
                          $msgcmdinfo "printer usbpr$idx on $HOSTNAME will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      fi

                     {
                      echo "[$name]"
                      echo "   comment = local printer usbpr$idx on $HOSTNAME"
                      echo "   browseable = yes"
                      echo "   printable = yes"
                      echo "   print command = lpr -h -r -P%p %s"
                      echo "   lprm command = lprm -P%p %j"
                      echo "   lpq command = lpq -P%p"
                      echo "   create mode = 0700"
                      echo "   printer = usbpr$idx"
                      echo "   path = /var/spool/lp/usbpr$idx"
                      echo "   hosts allow = $lpnet1"
                      echo "   "
                     } >> /etc/smb.conf

                      idx=`expr $idx + 1`
                done
            fi

            if [ "`echo $OPT_LPD_REMOTE | tr 'A-Z' 'a-z'`" = "yes" ]
            then
                #----------------------------------------------------------------------------
                # create share(s) for remote printer(s):
                #----------------------------------------------------------------------------

                idx='1'

                while [ "$idx" -le "$LPD_REMOTE_N" ]
                do
                      eval lpname='$SAMBA_LPD_REMOTE_'$idx'_NAME'
                      eval lpnet='$SAMBA_LPD_REMOTE_'$idx'_NET'
                      eval ip='$LPD_REMOTE_'$idx'_IP'
                      eval remotequeuename='$LPD_REMOTE_'$idx'_QUEUENAME'

                      if [ "$lpname" != '' ]
                      then
                          name="$lpname"
                      else
                          name=repr$idx
                      fi

                      if [ "$lpnet" != '' ]
                      then
                          lpnet1=$lpnet
                          $msgcmdinfo "printer repr$idx on $HOSTNAME for ip $ip will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      else
                          lpnet1=$sambahosts
                          $msgcmdinfo "printer repr$idx on $HOSTNAME for ip $ip will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      fi

                     {
                      echo "[$name]"
                      echo "   comment = remote printer repr$idx on $HOSTNAME for ip $ip queue $remotequeuename"
                      echo "   browseable = yes"
                      echo "   printable = yes"
                      echo "   print command = lpr -h -r -P%p %s"
                      echo "   lprm command = lprm -P%p %j"
                      echo "   lpq command = lpq -P%p"
                      echo "   create mode = 0700"
                      echo "   printer = repr$idx"
                      echo "   path = /var/spool/lp/repr$idx"
                      echo "   hosts allow = $lpnet1"
                      echo "   "
                     } >> /etc/smb.conf

                      idx=`expr $idx + 1`
                done
            fi

            if [ "`echo $OPT_LPD_SMBREMOTE | tr 'A-Z' 'a-z'`" = "yes" ]
            then
                #----------------------------------------------------------------------------
                # create share(s) for remote smb printer(s):
                #----------------------------------------------------------------------------

                idx='1'

                while [ "$idx" -le "$LPD_SMBREMOTE_N" ]
                do
                      eval lpname='$SAMBA_LPD_SMBREMOTE_'$idx'_NAME'
                      eval lpnet='$SAMBA_LPD_SMBREMOTE_'$idx'_NET'
                      eval server='$LPD_SMBREMOTE_'$idx'_SERVER'
                      eval service='$LPD_SMBREMOTE_'$idx'_SERVICE'

                      if [ "$lpname" != '' ]
                      then
                          name="$lpname"
                      else
                          name=smbrepr$idx
                      fi

                      if [ "$lpnet" != '' ]
                      then
                          lpnet1=$lpnet
                          $msgcmdinfo "printer smbrepr$idx on $HOSTNAME for winprinter $service will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      else
                          lpnet1=$sambahosts
                          $msgcmdinfo "printer smbrepr$idx on $HOSTNAME for winprinter $service will be accessable for network(s):"
                          $msgcmdinfo "$lpnet1"
                      fi

                     {
                      echo "[$name]"
                      echo "   comment = smb remote printer smbrepr$idx on $HOSTNAME for smb server $server winprinter $service"
                      echo "   browseable = yes"
                      echo "   printable = yes"
                      echo "   print command = lpr -h -r -P%p %s; rm %s"
                      echo "   lprm command = lprm -P%p %j"
                      echo "   lpq command = lpq -P%p"
                      echo "   create mode = 0700"
                      echo "   printer = smbrepr$idx"
                      echo "   path = /var/spool/lp/smbrepr$idx"
                      echo "   hosts allow = $lpnet1"
                      echo "   "
                     } >> /etc/smb.conf

                      idx=`expr $idx + 1`
                done
            fi
        fi
    fi
    cd /usr/local/samba/lib
    ln -s /etc/smb.conf smb.conf
    #----------------------------------------------------------------------------
    # starting nmbd and smbd, if configured from fli4l:
    #----------------------------------------------------------------------------

    if [ "$start_samba" = "true" ]
    then
        if [ -n "$data_mount" ]
        then
            for i in minix ext2 ext3
            do
                if [ -n "`echo $data_mount | grep $i`" ]
                then
                    logdir_error=false
                fi
            done
        fi

        if [ "$logdir_error" = "false" ]
        then
            if [ "$SAMBA_LOGDIR" = "" ]
            then
                SAMBA_LOGDIR="/data"
            fi
        else
            if [ "$SAMBA_LOGDIR" = "" ]
            then
                SAMBA_LOGDIR="/var/log"
            fi
        fi

        nmblog="-l $SAMBA_LOGDIR"
        smblog="-l $SAMBA_LOGDIR"
        nmblognull="$SAMBA_LOGDIR/log.nmbd"
        smblognull="$SAMBA_LOGDIR/log.smbd"

        #----------------------------------------------------------------------------
        # disable logging:
        #----------------------------------------------------------------------------

        if [ "`echo $SAMBA_LOG | tr 'A-Z' 'a-z'`" = "no" ]
        then
            rm $nmblognull
	    ln -s /dev/null $nmblognull
            rm $smblognull
            ln -s /dev/null $smblognull
        fi

        if [ -f /usr/sbin/nmbd ]
        then
            colecho "starting netbios nameserver on $HOSTNAME ..." gn
            /usr/sbin/nmbd -D $nmblog -s /etc/smb.conf
            if [ "$?" != "0" ]
            then 
                colecho "*** ERROR: cannot start nmbd on $HOSTNAME! ***" br x br
                $msgcmdfail "*** ERROR: cannot start nmbd on $HOSTNAME! ***"
                colecho "*** ERROR: check OPT_NMBD and if HOSTNAME of fli4l exists under HOST_x in base.txt! ***" br x br
                $msgcmdfail "*** ERROR: check OPT_NMBD and if HOSTNAME of fli4l exists under HOST_x in base.txt! ***"
            fi
        fi

        if [ -f /usr/sbin/smbd ]
        then
            colecho "starting samba on $HOSTNAME ..." gn
            /usr/sbin/smbd -D $smblog -s /etc/smb.conf
            if [ "$?" != "0" ]
            then 
                colecho "*** ERROR: cannot start samba on $HOSTNAME! ***" br x br
                $msgcmdfail "*** ERROR: cannot start samba on $HOSTNAME! ***"
                colecho "*** ERROR: check OPT_SAMBA! ***" br x br
                $msgcmdfail "*** ERROR: check OPT_SAMBA! ***"
            fi
        fi

        #----------------------------------------------------------------------------
        # load smbfs.o and nls-files, if exists:
        #----------------------------------------------------------------------------

        do_modprobe_if_exists kernel/fs/smbfs smbfs
        # do_modprobe_if_exists kernel/fs/nls nls_iso8859-1 included since kernel 2.6
        # do_modprobe_if_exists kernel/fs/nls nls_cp850     included since kernel 2.6

        #----------------------------------------------------------------------------
        # show samba start message:
        #----------------------------------------------------------------------------

        if [ -f /bin/samba-netsend ]
        then
            samba-netsend all "fli4l-Samba-Server is up now ..." &
        fi
    fi
else                                        # overwrite is not true
    #----------------------------------------------------------------------------
    # starting nmbd and smbd, if configured by individual smb.conf:
    #----------------------------------------------------------------------------

    if [ -f /usr/sbin/nmbd ]
    then
        colecho "starting netbios nameserver on $HOSTNAME ..." gn
        /usr/sbin/nmbd -D $nmblog -s /etc/smb.conf
        if [ "$?" != "0" ]
        then 
            colecho "*** ERROR: cannot start nmbd on $HOSTNAME! ***" br x br
            $msgcmdfail "*** ERROR: cannot start nmbd on $HOSTNAME! ***"
            colecho "*** ERROR: check OPT_NMBD and if HOSTNAME of fli4l exists under HOST_x in base.txt! ***" br x br
            $msgcmdfail "*** ERROR: check OPT_NMBD and if HOSTNAME of fli4l exists under HOST_x in base.txt! ***"
        fi
    fi

    if [ -f /usr/sbin/smbd ]
    then
        colecho "starting samba on $HOSTNAME ..." gn
        /usr/sbin/smbd -D $smblog -s /etc/smb.conf
        if [ "$?" != "0" ]
        then 
                colecho "*** ERROR: cannot start samba on $HOSTNAME! ***" br x br
                $msgcmdfail "*** ERROR: cannot start samba on $HOSTNAME! ***"
                colecho "*** ERROR: check OPT_SAMBA and your smb.conf! ***" br x br
                $msgcmdfail "*** ERROR: check OPT_SAMBA and your smb.conf! ***"
        fi
    fi
fi

end_script

fi
