#! /bin/sh -e

# correct map file when switching between DCC and DCC Reputation code.

# Copyright (c) 2017 by Rhyolite Software, LLC
#
# This agreement is not applicable to any entity which sells anti-spam
# solutions to others or provides an anti-spam solution as part of a
# security solution sold to other entities, or to a private network
# which employs the DCC or uses data provided by operation of the DCC
# but does not provide corresponding data to other users.
#
# Permission to use, copy, modify, and distribute this software without
# changes for any purpose with or without fee is hereby granted, provided
# that the above copyright notice and this permission notice appear in all
# copies and any distributed versions or copies are either unchanged
# or not called anything similar to "DCC" or "Distributed Checksum
# Clearinghouse".
#
# Parties not eligible to receive a license under this agreement can
# obtain a commercial license to use DCC by contacting Rhyolite Software
# at sales@rhyolite.com.
#
# A commercial license would be for Distributed Checksum and Reputation
# Clearinghouse software.  That software includes additional features.  This
# free license for Distributed ChecksumClearinghouse Software does not in any
# way grant permision to use Distributed Checksum and Reputation Clearinghouse
# software
#
# THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
# BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
# OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Rhyolite Software DCC 1.3.159-1.24 $Revision$
# Generated automatically from fix-map.in by configure.


set -e

#NO_SUID=	#set-UID cdcc, dccproc, and dccsight to DCCUID
# 
# PKG_MAKE prevents fix-map from making the necessary permission changes when
# running dcc* as user dcc and using NetBSD pkgsrc rc scripts.
# PKG_MAKE=--enable-pkg-make

DCC_HOMEDIR=/var/dcc
CDCC=/usr/pkg/bin/cdcc
NEW=			# always create new files with this suffix if not null
IDS=			# look for or put ids file here
P_IDS=ids		# prototype ids file
MAP=			# look for or put map file here
MAPTXT=			# look for or put map.txt file here
P_MAPTXT=map.txt	# prototype map.txt
NEW_MAPTXT=		# yes=need to generate a new map.txt

USAGE="$0: [-x] [-n new] [-c cdcc] [-h homedir] -i ids [-I prototype-ids] \
    -m map -t map.txt [-T prototype-map.txt]"
while getopts "xn:c:h:i:I:m:t:T:p:" c; do
    case $c in
	x) set -x;;
	n) NEW="$OPTARG";;
	c) CDCC="$OPTARG";;
	h) DCC_HOMEDIR="$OPTARG";;
	i) IDS="$OPTARG";;
	I) P_IDS="$OPTARG";;
	m) MAP="$OPTARG";;
	t) MAPTXT="$OPTARG";;
	T) P_MAPTXT="$OPTARG";;
	*) echo "$USAGE" 1>&2; exit 1;;
    esac
done
shift `expr $OPTIND - 1 || true`
if test "$#" -ne 0; then
    echo "$USAGE" 1>&2
    exit 1
fi
if test ! -d "$DCC_HOMEDIR"; then
    echo "$0: bad DCC home directory" 1>&2; echo "$USAGE" 1>&2; exit 1
fi
if test -z "$IDS"; then
    echo "$0: -i ids not used" 1>&2
    echo "-$USAGE" 1>&2
    exit 1
fi
if test -z "$MAP"; then
    echo "$0: -m map not used" 1>&2
    echo "-$USAGE" 1>&2
    exit 1
fi
if test -z "$MAPTXT"; then
    echo "$0: -t map.txt not used" 1>&2
    echo "$USAGE" 1>&2
    exit 1
fi


if test ! -x "$CDCC"; then
    echo "$0: cdcc does not seem to have been built in $CDCC" 1>&2
    echo "$USAGE" 1>&2
    exit 1
fi
CDCC_CMD="$CDCC -qh$DCC_HOMEDIR file=$MAP"
$CDCC_CMD >/dev/null </dev/null			# see that cdcc works

# create new ids and map files or check old files
if test -n "$NEW" || test ! -s "$IDS" -a ! -s "$MAP"; then
    if test ! -s "$P_IDS"; then
	echo "$0: cannot create $IDS if prototype $P_IDS does not exist" 1>&2
	exit 1
    fi
    if test ! -s "$P_MAPTXT"; then
	echo "$0: cannot create $MAP if prototype $P_MAPTXT does not exist" 1>&2
	exit 1
    fi

    # /dev/random is overkill for the security needed here,
    #	but use it if it exists.  Use /dev/urandom to avoid blocking
    PASSWD1=`(ps; date; dd if=/dev/urandom count=1 2>&1 || true)	\
	| cksum | tr '	 ' xy`
    PASSWD2=`(echo "$PASSWD1";						\
	ps; date; dd if=/dev/urandom count=1 2>&1 || true)		\
	| cksum | tr '	 ' xy`

    rm -f "$IDS" "$MAP" "$MAPTXT" || true

    sed -e "/^[^#]/s/secret1[0-9a-z]*/secret1$PASSWD1/"		\
	    -e "/^[^#]/s/secret2[0-9a-z]*/secret2$PASSWD2/" "$P_IDS" >"$IDS"

    sed -e "/^[^#]/s/secret1[0-9a-z]*/secret1$PASSWD1/" "$P_MAPTXT"	\
	| $CDCC_CMD "new map $MAP; load -; info" >$MAPTXT
else
    if test ! -s "$MAP" -a -s "$MAPTXT"; then
	$CDCC_CMD "new map; load $MAPTXT"
    fi
    if test ! -s "$MAP"; then
	echo "$0: cannot re-create $MAP if $IDS exists" 1>&2
    fi
    if test ! -s "$IDS"; then
	echo "$0: cannot re-create $IDS if $MAP exists" 1>&2
	IDS=
    fi
fi

if test ! -w "$MAP" -o ! -r "$MAP"; then
    echo "$0: $MAP not readable and writable" 1>&2
    echo "$USAGE" 1>&2
    exit 1
fi

RSRVR=
RID=
RPASSWD=
if test -r $DCC_HOMEDIR/.updatedcc_pfile				\
	    -a -s $DCC_HOMEDIR/.updatedcc_pfile; then
    . $DCC_HOMEDIR/.updatedcc_pfile
fi
if test -r .updatedcc_pfile -a -s .updatedcc_pfile; then
    . ./.updatedcc_pfile
fi

USING_DCC=`$CDCC_CMD info						\
    | sed -n -e 's/^\([-a-z0-9]*\.dcc-servers\.net\),-.*/\1/p'`
USING_RHYOLITE=`$CDCC_CMD info						\
    | sed -n -e 's/^\([-a-z0-9]*\.rhyolite\.com\),-.*/\1/p'`


# Replace references to the common DCC Reputation servers when installing the
#   free DCC client software because the common DCC Reputation servers have
#   probably forgotten this client's ID and so will refuse to answer.
#   When we remove the common DCC Reputation servers, add the public servers
#   as low priority backups to avoid leaving things broken.
if test -n "$USING_RHYOLITE"; then
    for s in $USING_RHYOLITE; do
	$CDCC_CMD "delete $s"
    done
    $CDCC_CMD "add dcc1.dcc-servers.net RTT+1000 ms"			\
	"add dcc2.dcc-servers.net RTT+1000 ms"				\
	"add dcc3.dcc-servers.net RTT+1000 ms"				\
	"add dcc4.dcc-servers.net RTT+1000 ms"				\
	"add dcc5.dcc-servers.net RTT+1000 ms"
    NEW_MAPTXT=yes
fi

# create map.txt
if test -n "$NEW_MAPTXT" -o ! -s "$MAPTXT"; then
    rm -f $MAPTXT
    echo '# map.txt is merely the output of `cdcc -q info`' >$MAPTXT
    $CDCC_CMD info >>$MAPTXT
fi

# make the files private
chmod 0600 $MAPTXT $MAP $IDS

if test -n "dcc" -a -z "$NO_SUID$PKG_MAKE"			\
				&& id "dcc" >/dev/null 2>&1; then
    chown "dcc" $MAPTXT $MAP $IDS $DCC_HOMEDIR $DCC_HOMEDIR/log
    for F in $DCC_HOMEDIR/whiteclnt $DCC_HOMEDIR/dcc_db $DCC_HOMEDIR/grey_db; do
	if test -f $F; then
	    chown dcc $F
	fi
    done
fi
