#!/bin/sh -
#
# Generated by make-/b/mnt/src/ports/security/tripwire/work/pkg-install on Wed Nov 18 11:06:41 GMT 2015
#
case $2 in
POST-INSTALL)	;;
*)	exit 0;;
esac
PACKAGE_INSTALLER=yes
POLICYSRC=/tmp/6403.tmp
PREFIX=/usr/local
DO_NOT_CONFIG=yes
TW_CLOBBER=false
TW_PROMPT=false
sysconfdir=
prefix=/usr/local
sysconfdir=/usr/local/etc/tripwire
path_to_vi=/usr/bin/vi
path_to_sendmail=/usr/sbin/sendmail
BASE_DIR=/usr/local/
BIN_DIR=/usr/local/sbin
TW_SITE_PASS=FreeBSD
TW_LOCAL_PASS=FreeBSD
#
# install.cfg
#
# default install.cfg for:
# Tripwire(R) 2.4 Open Source
#
# NOTE:  This is a Bourne shell script that stores installation
#        parameters for your installation.  The installer will
#        execute this file to generate your config file and also to
#        locate any special configuration needs for your install.
#        Protect this file, because it is possible for
#        malicious code to be inserted here
#
# This version of Tripwire has been modified to conform to the FHS
# standard for Unix-like operating systems.
#
# To change the install directory for any tripwire files, modify
# the paths below as necessary.
#
#=======================================================

# If CLOBBER is true, then existing files are overwritten.
# If CLOBBER is false, existing files are not overwritten.
CLOBBER=false

# Tripwire binaries are stored in TWBIN.
TWBIN="${PREFIX}/sbin"

# Tripwire policy files are stored in TWPOLICY.
TWPOLICY="${TWPOLICY}"	# Set in the FreeBSD port Makefile

# Tripwire manual pages are stored in TWMAN.
TWMAN="${PREFIX}/man"

# Tripwire database files are stored in TWDB.
TWDB="${TWDB}"	# Set in the FreeBSD port Makefile

# Tripwire documents directory
TWDOCS="${PREFIX}/share/doc/tripwire"

# The Tripwire site key files are stored in TWSITEKEYDIR.
# TWSITEKEYDIR="${TWPOLICY}"

# The Tripwire local key files are stored in TWLOCALKEYDIR.
# TWLOCALKEYDIR="${TWPOLICY}"

# Tripwire report files are stored in TWREPORT.
# TWREPORT="${TWREPORT}"	# Set in the FreeBSD port Makefile

# This sets the default text editor for Tripwire.
# TWEDITOR="${path_to_vi}"

# TWLATEPROMTING controls the point when tripwire asks for a password.
TWLATEPROMPTING=false

# TWLOOSEDIRCHK selects whether the directory should be monitored for 
# properties that change when files in the directory are monitored.
TWLOOSEDIRCHK=false

# TWMAILNOVIOLATIONS determines whether Tripwire sends a no violation 
# report when integrity check is run with --email-report but no rule
# violations are found.  This lets the admin know that the integrity 
# was run, as opposed to having failed for some reason.
TWMAILNOVIOLATIONS=true

# TWEMAILREPORTLEVEL determines the verbosity of e-mail reports.
TWEMAILREPORTLEVEL=3

# TWREPORTLEVEL determines the verbosity of report printouts.
TWREPORTLEVEL=3

# TWSYSLOG determines whether Tripwire will log events to the system log
TWSYSLOG=false

#####################################
# Mail Options - Choose the appropriate
# method and comment the other section
#####################################

#####################################
# SENDMAIL options - DEFAULT
#
# Either SENDMAIL or SMTP can be used to send reports via TWMAILMETHOD.
# Specifies which sendmail program to use.
#####################################

TWMAILMETHOD=SENDMAIL

#####################################
# SMTP options
#
# TWSMTPHOST selects the SMTP host to be used to send reports.
# SMTPPORT selects the SMTP port for the SMTP mail program to use.
#####################################

#TWMAILMETHOD=SMTP
#TWSMTPHOST="mail.domain.com"
#TWSMTPPORT=25

# If sendmail wasn't found in the path, set then clear out 
# TWMAILPROGRAM so the install.sh does the right thing and
# tells the user about the missing configuration
#
path_to_sendmail=/usr/sbin/sendmail
if [ -z "$path_to_sendmail" ] ; then
   TWMAILPROGRAM=""
else
   TWMAILPROGRAM="${path_to_sendmail} -oi -t"
fi
TWPOLICY=/usr/local/etc/tripwire
TWSITEKEYDIR=/usr/local/etc/tripwire
TWLOCALKEYDIR=/usr/local/etc/tripwire
TWDB=/var/db/tripwire
TWREPORT=/var/db/tripwire/report
TWEDITOR=/usr/bin/vi
TW_SITE_PASS=FreeBSD
TW_LOCAL_PASS=FreeBSD
/bin/cat > /tmp/6464.tmp <<'EOF'
#
#                       Policy file for FreeBSD
#
# $FreeBSD: ports/security/tripwire/files/twpol.txt,v 1.2 2002/03/04 16:55:21 cy Exp $
# $Id: twpol-FreeBSD.txt,v 1.1 2003/06/08 02:00:06 pherman Exp $

#
# This is the example Tripwire Policy file.  It is intended as a place to
# start creating your own custom Tripwire Policy file.  Referring to it as
# well as the Tripwire Policy Guide should give you enough information to
# make a good custom Tripwire Policy file that better covers your
# configuration and security needs.  A text version of this policy file is
# called twpol.txt.
#
# Note that this file is tuned to an install of FreeBSD using
# buildworld.  If run unmodified, this file should create no errors on
# database creation, or violations on a subsiquent integrity check.
# However it is impossible for there to be one policy file for all machines,
# so this existing one errs on the side of security.  Your FreeBSD
# configuration will most likey differ from the one our policy file was
# tuned to, and will therefore require some editing of the default
# Tripwire Policy file.
#
# The example policy file is best run with 'Loose Directory Checking'
# enabled. Set LOOSEDIRECTORYCHECKING=TRUE in the Tripwire Configuration
# file.
#
# Email support is not included and must be added to this file.
# Add the 'emailto=' to the rule directive section of each rule (add a comma
# after the 'severity=' line and add an 'emailto=' and include the email
# addresses you want the violation reports to go to).  Addresses are
# semi-colon delimited.
#



#
# Global Variable Definitions
#
# These are defined at install time by the installation script.  You may
# Manually edit these if you are using this file directly and not from the
# installation script itself.
#

@@section GLOBAL
TWROOT=;
TWBIN=;
TWPOL=;
TWDB=;
TWSKEY=;
TWLKEY=;
TWREPORT=;
HOSTNAME=;

@@section FS
SEC_CRIT      = $(IgnoreNone)-SHa ;  # Critical files that cannot change
SEC_SUID      = $(IgnoreNone)-SHa ;  # Binaries with the SUID or SGID flags set
SEC_BIN       = $(ReadOnly) ;        # Binaries that should not change
SEC_CONFIG    = $(Dynamic) ;         # Config files that are changed infrequently but accessed often
SEC_TTY    = $(Dynamic)-ugp ;        # Tty files that change ownership at login
SEC_LOG       = $(Growing) ;         # Files that grow, but that should never change ownership
SEC_INVARIANT = +tpug ;              # Directories that should never change permission or ownership
SIG_LOW       = 33 ;                 # Non-critical files that are of minimal security impact
SIG_MED       = 66 ;                 # Non-critical files that are of significant security impact
SIG_HI        = 100 ;                # Critical files that are significant points of vulnerability


# Tripwire Binaries
(
  rulename = "Tripwire Binaries",
  severity = $(SIG_HI)
)
{
  $(TWBIN)/siggen                      -> $(SEC_BIN) ;
  $(TWBIN)/tripwire                    -> $(SEC_BIN) ;
  $(TWBIN)/twadmin                     -> $(SEC_BIN) ;
  $(TWBIN)/twprint                     -> $(SEC_BIN) ;
}

# Tripwire Data Files - Configuration Files, Policy Files, Keys, Reports, Databases
(
  rulename = "Tripwire Data Files",
  severity = $(SIG_HI)
)
{
  # NOTE: We remove the inode attribute because when Tripwire creates a backup,
  # it does so by renaming the old file and creating a new one (which will
  # have a new inode number).  Inode is left turned on for keys, which shouldn't
  # ever change.

  # NOTE: The first integrity check triggers this rule and each integrity check
  # afterward triggers this rule until a database update is run, since the
  # database file does not exist before that point.

  $(TWDB)                              -> $(SEC_CONFIG) -i ;
  $(TWPOL)/tw.pol                      -> $(SEC_BIN) -i ;
  $(TWPOL)/tw.cfg                      -> $(SEC_BIN) -i ;
  $(TWLKEY)/$(HOSTNAME)-local.key      -> $(SEC_BIN) ;
  $(TWSKEY)/site.key                   -> $(SEC_BIN) ;

  #don't scan the individual reports
  $(TWREPORT)                          -> $(SEC_CONFIG) (recurse=0) ;
}

#
# First, root's "home"
#

(
  rulename = "Root's home",
  severity = $(SIG_HI)
)
{
  /root					-> $(SEC_CRIT) (recurse = true) ;
  !/root/.zsh_history ;
}

#
# Compadmin
#

(
  rulename = "Compadmin",
  severity = $(SIG_HI)
)
{
  /compadmin					-> $(SEC_CRIT) (recurse = true) ;
}

#
# FreeBSD Kernel
#

(
  rulename = "FreeBSD Kernel",
  severity = $(SIG_HI)
)
{
  /boot				-> $(SEC_CRIT) ;
}


#
# FreeBSD Modules
#

(
  rulename = "FreeBSD Modules",
  severity = $(SIG_HI)
)
{
  /boot/kernel.amd64				-> $(SEC_CRIT)(recurse = true) ;
}


#
# System Administration Programs
#

(
  rulename = "System Administration Programs",
  severity = $(SIG_HI)
)
{
  /sbin					-> $(SEC_CRIT) (recurse = true) ;
  /usr/sbin				-> $(SEC_CRIT) (recurse = true) ;
}


#
# User Utilities
#

(
  rulename = "User Utilities",
  severity = $(SIG_HI)
)
{
  /bin					-> $(SEC_CRIT) (recurse = true) ;
  /usr/bin				-> $(SEC_CRIT) (recurse = true) ;
}


#
# /dev
#

(
  rulename = "/dev",
  severity = $(SIG_HI)
)
{
  /dev					-> $(Device) (recurse = true) ;
  /dev/console				-> $(SEC_TTY) ;
  /dev/ttyv0				-> $(SEC_TTY) ;
  /dev/ttyv1				-> $(SEC_TTY) ;
  /dev/ttyv2				-> $(SEC_TTY) ;
  /dev/ttyv3				-> $(SEC_TTY) ;
  /dev/ttyv4				-> $(SEC_TTY) ;
  /dev/ttyv5				-> $(SEC_TTY) ;
  /dev/ttyv6				-> $(SEC_TTY) ;
  /dev/ttyv7				-> $(SEC_TTY) ;
  /dev/ttyv8				-> $(SEC_TTY) ;
  /dev/ttyv9				-> $(SEC_TTY) ;
  /dev/ttyva				-> $(SEC_TTY) ;
  /dev/ttyvb				-> $(SEC_TTY) ;
  /dev/ttyvc				-> $(SEC_TTY) ;
  /dev/ttyvd				-> $(SEC_TTY) ;
  /dev/ttyve				-> $(SEC_TTY) ;
  /dev/ttyvf				-> $(SEC_TTY) ;
  /dev/ttyp0				-> $(SEC_TTY) ;
  /dev/ttyp1				-> $(SEC_TTY) ;
  /dev/cuad0				-> $(SEC_TTY) ;	
  /dev/cuad1				-> $(SEC_TTY) ;	
}


#
# /etc
#

(
  rulename = "/etc",
  severity = $(SIG_HI)
)
{
  /etc					-> $(SEC_CRIT) (recurse = true) ;
  /etc/mail/aliases			-> $(SEC_CONFIG) ;
  /etc/dumpdates			-> $(SEC_CONFIG) ;
  /etc/motd				-> $(SEC_CONFIG) ;
  !/etc/ppp/connect-errors ;
  /etc/passwd				-> $(SEC_CONFIG) ;
  /etc/master.passwd			-> $(SEC_CONFIG) ;
  /etc/pwd.db				-> $(SEC_CONFIG) ;
  /etc/spwd.db				-> $(SEC_CONFIG) ;
}

#
# Libraries, include files, and other system files
#

(
  rulename = "Libraries, include files, and other system files",
  severity = $(SIG_HI)
)
{
  /usr/include				-> $(SEC_CRIT) (recurse = true) ;
  /usr/lib				-> $(SEC_CRIT) (recurse = true) ;
  /usr/libdata				-> $(SEC_CRIT) (recurse = true) ;
  /usr/libexec				-> $(SEC_CRIT) (recurse = true) ;
  /usr/share				-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man			-> $(SEC_CONFIG) ;
  !/usr/share/man/whatis ;
  /usr/share/man/man1			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man2			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man3			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man4			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man5			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man6			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man7			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man8			-> $(SEC_CRIT) (recurse = true) ;
  /usr/share/man/man9			-> $(SEC_CRIT) (recurse = true) ;
  ! /usr/share/man/cat1 ;
  ! /usr/share/man/cat2 ;
  ! /usr/share/man/cat3 ;
  ! /usr/share/man/cat4 ;
  ! /usr/share/man/cat5 ;
  ! /usr/share/man/cat6 ;
  ! /usr/share/man/cat7 ;
  ! /usr/share/man/cat8 ;
  ! /usr/share/man/cat9 ;
}


#
# X11R6
#

(
  rulename = "X11R6",
  severity = $(SIG_HI)
)
{
  /usr/X11R6				-> $(SEC_CRIT) (recurse = true) ;
}

#
# NIS
#

(
  rulename = "NIS",
  severity = $(SIG_HI)
)
{
  /var/yp				-> $(SEC_CRIT) (recurse = true) ;
  !/var/yp/binding ;
}


#
# Temporary directories
#
(
  rulename = "Temporary directories",
  recurse = false,
  severity = $(SIG_LOW)
)
{
  /var/tmp                             -> $(SEC_INVARIANT) ;
  /var/preserve                        -> $(SEC_INVARIANT) ;
  /tmp                                 -> $(SEC_INVARIANT) ;
}

#
# Local files
#

(
  rulename = "Local files",
  severity = $(SIG_MED)
)
{
  /usr/local/bin			-> $(SEC_BIN) (recurse = true) ;
  /usr/local/sbin			-> $(SEC_BIN) (recurse = true) ;
  /usr/local/etc		 	-> $(SEC_BIN) (recurse = true) ;
  /usr/local/lib			-> $(SEC_BIN) (recurse = true ) ;
  /usr/local/libexec			-> $(SEC_BIN) (recurse = true ) ;
  /usr/local/share			-> $(SEC_BIN) (recurse = true ) ;
  /usr/local/man			-> $(SEC_CONFIG) ;
  !/usr/local/man/whatis ;
  /usr/local/man/man1			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man2			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man3			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man4			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man5			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man6			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man7			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man8			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/man9			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/manl			-> $(SEC_CRIT) (recurse = true) ;
  /usr/local/man/mann			-> $(SEC_CRIT) (recurse = true) ;
  ! /usr/local/man/cat1 ;
  ! /usr/local/man/cat2 ;
  ! /usr/local/man/cat3 ;
  ! /usr/local/man/cat4 ;
  ! /usr/local/man/cat5 ;
  ! /usr/local/man/cat6 ;
  ! /usr/local/man/cat7 ;
  ! /usr/local/man/cat8 ;
  ! /usr/local/man/cat9 ;
  ! /usr/local/man/catl ;
  ! /usr/local/man/catn ;
  /usr/local/www			-> $(SEC_CONFIG) (recurse = true) ;
  /usr/local/SupportIQ			-> $(SEC_BIN) (recurse = true) ;
  /usr/local/apache2			-> $(SEC_BIN) (recurse = true) ;
  /usr/local/aspera			-> $(SEC_BIN) (recurse = true) ;
  /usr/local/isi_check_cluster		-> $(SEC_BIN) (recurse = true) ;
}


(
  rulename = "Security Control",
  severity = $(SIG_HI)
)
{
  /etc/group                           -> $(SEC_CRIT) ;
  /etc/crontab                         -> $(SEC_CRIT) ;
}

#=============================================================================
#
# Copyright 2000 Tripwire, Inc. Tripwire is a registered trademark of Tripwire,
# Inc. in the United States and other countries. All rights reserved.
#
# FreeBSD is a registered trademark of the FreeBSD Project Inc.
#
# UNIX is a registered trademark of The Open Group.
#
#=============================================================================
#
# Permission is granted to make and distribute verbatim copies of this document
# provided the copyright notice and this permission notice are preserved on all
# copies.
#
# Permission is granted to copy and distribute modified versions of this
# document under the conditions for verbatim copying, provided that the entire
# resulting derived work is distributed under the terms of a permission notice
# identical to this one.
#
# Permission is granted to copy and distribute translations of this document
# into another language, under the above conditions for modified versions,
# except that this permission notice may be stated in a translation approved by
# Tripwire, Inc.
#
# DCM
EOF
#!/bin/sh

#########################################################
#########################################################
##
## Tripwire(R) 2.4 Open Source install script
##
#########################################################
#########################################################

##=======================================================
## Setup
##=======================================================

##-------------------------------------------------------
## Sanity checks, mostly stolen (with permission)
## from Larry Wall's metaconfig.
##-------------------------------------------------------

PATH='.:/bin:/usr/bin'
export PATH || (echo 'You must use sh to run this script'; kill $$)
#if [ ! -t 0 ] ; then
#	echo "Say 'sh install.sh', not 'sh < install.sh'"
#	exit 1
#fi

##-------------------------------------------------------
## The usage message.
##-------------------------------------------------------

USAGE="install.sh [<configfile>] [-n] [-f] [-s <sitepassphrase>] [-l <localpassphrase>]"

##-------------------------------------------------------
## Figure out how to do an echo without newline.
##-------------------------------------------------------

if [ "`echo -n`" = "-n" ] ; then
	n=""
	c="\c"
else
	n=" -n"
	c=""
fi

##-------------------------------------------------------
## Better have a copy of tar!
## If /bin/sh does not exist or is not readable (seems
## fairly unlikely), then this will fail.
##-------------------------------------------------------

(tar cvf /dev/null /bin/sh) 2> /dev/null 1>&2
if [ $? -ne 0 ]; then
    echo "tar command not found -- aborting install."
	exit 1
fi

##-------------------------------------------------------
## Can't live without sed.
##-------------------------------------------------------

(echo 'abc' | sed 's/abc/def/') 2> /dev/null 1>&2
if [ $? -ne 0 ]; then
    echo "sed command not found -- aborting install."
	exit 1
fi

##-------------------------------------------------------
## Does this system have a copy of awk we can use?
##-------------------------------------------------------

AWK=""
awknames="awk nawk gawk"
for p in $awknames; do
	($p '{ exit 0 }' < /dev/null) 2> /dev/null 1>&2
	if [ $? -eq 0 ]; then
		AWK=$p
		break
	fi
done

##-------------------------------------------------------
## Does this system have a copy of grep we can use?
## Some greps don't return status (amazing, huh?),
## so we look for a copy of grep that
## returns 0 status for an exact match
## returns 0 status for a case-insensitive match
## returns 0 status for a wildcard match
## returns non-zero status for a failed match
##-------------------------------------------------------

GREP=""
grepnames="grep egrep"
lcgrepstr="findensiemich"     # all lower case
mcgrepstr="FindenSieMich"     # mixed case
wcgrepstr="sie.ich$"          # wild card match
nogrepstr="WoBistDu"          # should not be able to find this
for p in $grepnames; do
	(echo "$lcgrepstr" | $p "$lcgrepstr") 2> /dev/null 1>&2
	if [ $? -eq 0 ]; then
		(echo "$lcgrepstr" | $p -i "$mcgrepstr") 2> /dev/null 1>&2
		if [ $? -eq 0 ]; then
			(echo "$lcgrepstr" | $p "$wcgrepstr") 2> /dev/null 1>&2
			if [ $? -eq 0 ]; then
				(echo "$lcgrepstr" | $p "$nogrepstr") 2> /dev/null 1>&2
				if [ $? -ne 0 ]; then
					GREP=$p
					break
				fi
			fi
		fi
	fi
done

##-------------------------------------------------------
## Does this system have a pager that we can use?
## Use cat if desperate.
##-------------------------------------------------------

MORE="cat"
morenames="more less cat"
for p in $morenames; do
	($p $0 < /dev/null) 2> /dev/null 1>&2
	if [ $? -eq 0 ]; then
		MORE=$p
		break
	fi
done

##-------------------------------------------------------
## Does this system have a copy of uname we can use?
##-------------------------------------------------------

UNAME=""
(uname -s -r -v) 2> /dev/null 1>&2
if [ $? -eq 0 ]; then
	UNAME=uname
fi

##=======================================================
## Command line
##=======================================================

##-------------------------------------------------------
## Miscellaneous configuration parameters.
##-------------------------------------------------------

# prefix
prefix="${prefix:=/usr}"

# License File name
TWLICENSEFILE="COPYING"

# Default Tripwire configuration file.
TW_CONFIG_FILE="tw.cfg"

# Name of initial cleartext Tripwire config file.
CLR_CONFIG_FILE="twcfg.txt"

# Name of initial cleartext Tripwire policy file.
CLR_POLICY_FILE="twpol.txt"

# Default installation script configuration file.
INSTALL_CONFIG_FILE="./install/install.cfg"

# Relative path to dir that contains the pkg files.
PKG_DIR="pkg"

# Names of the package files.
# See also copy-files section below.
BIN_PKG="bin.pkg"
POL_PKG="policy.pkg"
MAN_PKG="man.pkg"

# Starting directory.
START_DIR=`pwd`

# If clobber==true, overwrite files; if false, do not overwrite files.
CLOBBER=${TW_CLOBBER:-"false"}

# If prompt==true, ask for confirmation before continuing with install.
PROMPT=${TW_PROMPT:-"true"}

# Guess where the toplevel for the distribution is.
# A bad guess is the current directory
TAR_DIR=${TAR_DIR:-${START_DIR}}

OS=`uname -s`
if [ ! -r ${TAR_DIR}/policy/${POLICYSRC} ]
then POLICYSRC="twpol-GENERIC.txt"
fi

##-------------------------------------------------------
## Parse the command line.
##-------------------------------------------------------

test "$PACKAGE_INSTALLER" != "yes" && {
while [ "x$1" != "x" ] ; do
    case "$1" in
	-n) PROMPT="false"; xCLOBBER="true" ;;
	-f) xCLOBBER="true" ;;
	-s) case "$2" in
	    "" | -*)
		echo "Error: missing sitepassphrase with -s option." 1>&2
		echo "$USAGE"
		exit 1 ;;
	    *) TW_SITE_PASS="$2"; shift ;;
	    esac ;;
	-l) case "$2" in
	    "" | -*)
		echo "Error: missing localpassphrase with -l option." 1>&2
		echo "$USAGE"
		exit 1 ;;
	    *) TW_LOCAL_PASS="$2"; shift ;;
	    esac ;;
	-*) echo "Error: unknown argument $1" 1>&2
	    echo "$USAGE"
	    exit 1 ;;
	*) INSTALL_CONFIG_FILE="$1"
	    if [ $xgot ] ; then
		echo "Error: incorrect number of arguments" 1>&2
		echo "$USAGE"
		exit 1
	    fi
	    xgot="true"
	    ;;
    esac
    shift
done
}

##-------------------------------------------------------
## Print the sign-on banner here before the first
## non-error message is displayed.
##-------------------------------------------------------

cat << END_OF_TEXT

Installer program for:
Tripwire(R) 2.4 Open Source

Copyright (C) 1998-2000 Tripwire (R) Security Systems, Inc.  Tripwire (R)
is a registered trademark of the Purdue Research Foundation and is
licensed exclusively to Tripwire (R) Security Systems, Inc.

END_OF_TEXT


##-------------------------------------------------------
## Print a message if the user aborts the install.
##-------------------------------------------------------

trap "echo ; echo 'Installation has been halted.' ; exit 1" 1 2 15

##-------------------------------------------------------
## The pathname variables we expect to find in the
## install.cfg file.
##-------------------------------------------------------

paths="TWBIN TWMAN TWPOLICY TWREPORT TWDB TWSITEKEYDIR TWLOCALKEYDIR"
path2="TWBIN TWPOLICY TWREPORT TWDB TWSITEKEYDIR TWLOCALKEYDIR"
path3="TWMAN TWDOCS"

##=======================================================
## User License Agreement
##=======================================================

##-------------------------------------------------------
## Where is the license agreement file?
##-------------------------------------------------------
EULA_PATH="$TWLICENSEFILE"

##-------------------------------------------------------
## If prompting is turned on, get acceptance from user before moving on.
##-------------------------------------------------------

if [ "$PROMPT" = "true" ] ; then
  echo
  echo "LICENSE AGREEMENT for Tripwire(R) 2.4 Open Source"
  echo
  echo "Please read the following license agreement.  You must accept the"
  echo "agreement to continue installing Tripwire."
  echo
  echo "Press ENTER to view the License Agreement."
  echo
  read inputstring

  # Show the user the license:
  ${PAGER:-${MORE}} $EULA_PATH

  # Now confirm that the user agrees to the license:
  echo
  echo 'Please type "accept" to indicate your acceptance of this'
        (echo $n "license agreement. [do not accept] " $c) 1>&2
	read ans
	case "$ans" in
		accept|Accept|ACCEPT) ;;
		*) echo "YOU DID NOT ACCEPT THE LICENSE AGREEMENT.  INSTALLATION TERMINATED."
			exit 1
			;;
	esac
fi

##-------------------------------------------------------
## Execute the installation configuration file
##-------------------------------------------------------

echo "Using configuration file $INSTALL_CONFIG_FILE"
echo
: . $INSTALL_CONFIG_FILE

##=======================================================
## Process the configuration parameters.
##=======================================================

##-------------------------------------------------------
## Value on command line, if present, overrides value in
## config file.  Value must either be "true" or "false"
## exactly; if it's not the former, make it the latter.
##-------------------------------------------------------

CLOBBER=${xCLOBBER-$CLOBBER}
if [ ! "$CLOBBER" = "true" ] ; then
	CLOBBER="false"
fi

##-------------------------------------------------------
## If no prompting was selected, both site and local
## passphrases must be specified on the command line.
##-------------------------------------------------------

if [ "$DO_NOT_CONFIG" != "yes" -a "$PROMPT" = "false" ] ; then
	if [ -z "$TW_SITE_PASS" ] || [ -z "$TW_LOCAL_PASS" ] ; then
		echo "Error: You must specify site and local passphrase" 1>&2
		echo "if no prompting is chosen." 1>&2
		echo "$USAGE"
		exit 1
    fi
fi

##-------------------------------------------------------
## For each pathname variable:
## Make sure it's defined.
## Strip trailing slashes from each of the directory variables.
## Determine length of longest parameter name.
##-------------------------------------------------------

len=0
for i in $paths; do

# Is it defined?
	eval "test \"\$${i}\""
	if [ $? -ne 0 ] ; then
		echo "Error: configuration parameter \$$i undefined." 1>&2
		echo "There is an error in the configuration file ${INSTALL_CONFIG_FILE}." 1>&2
		exit 1
	fi

# Strip trailing slashes.
# Squash multiple internal slashes down to one.
	eval "xtmp=\$${i}"
	xtmp=`echo $xtmp | sed 's/\/*$//'`
	xtmp=`echo $xtmp | sed 's/\/\/*/\//g'`
	eval "${i}=\"$xtmp\""

# Does it start with a slash (i.e. is it an absolute pathname)?
	ytmp=`echo $xtmp | sed 's/^\///'`
	if [ "$xtmp" = "$ytmp" ] ; then
		echo "Error: \$$i is not an absolute pathname." 1>&2
		echo 'Relative pathnames may not be used.' 1>&2
		exit 1
	fi

# What is the length of the longest variable name?
	if [ "$AWK" != "" ] ; then
		xlen=`echo "${i}" | $AWK '{ print length }'`
		if [ $xlen -gt $len ] ; then
			len=$xlen
		fi
	fi
done

##-------------------------------------------------------
## Check Mailmethod for SMTP.  If SMTP ignore MAILPROGRAM.
## If SENDMAIL is specified, verify that the specified 
## mail program exists
##-------------------------------------------------------
echo "Checking for programs specified in install configuration file...."
echo

TWMAILMETHOD=${TWMAILMETHOD:-'SENDMAIL'}
if [ "$TWMAILMETHOD" = "SENDMAIL" ] ; then

	if [ -n "$TWMAILPROGRAM" ] ; then
        	echo "${TWMAILPROGRAM} exists.  Continuing installation."
        	echo
	else
	        echo ""
        	echo "*** No sendmail found. Edit install.cfg and set"
		echo "*** TWMAILPROGRAM to full path to sendmail, or"
		echo "*** change TWMAILMETHOD to SMTP and set SMTP"
		echo "*** settings appropriately."
		echo ""
        	exit 1
	fi
else
        echo "Using SMTP mail protocol."
	echo "MAILPROGRAM variable will be ignored."
        echo "Continuing installation."
        echo
        fi

##-------------------------------------------------------
## Verify that the specified editor program exists
##-------------------------------------------------------
 
TWEDITOR=${TWEDITOR:-'/bin/vi'}

if [ -x ${TWEDITOR} ]; then
        echo "${TWEDITOR} exists.  Continuing installation."
        echo
else
        echo "${TWEDITOR} does not exist.  Exiting."
        exit 1
fi


##-------------------------------------------------------
## Where are the bin files?  Did they build okay?  Don't
## bother to install if they don't exist.
##-------------------------------------------------------

echo
echo "----------------------------------------------"
echo "Verifying existence of binaries..."
echo

if [ ! -z "$BASE_DIR" ] ; then
	: BIN_DIR="${BASE_DIR}bin"
fi

CWD=`pwd`
cd "$TAR_DIR"
TAR_DIR=`pwd`
cd "$BASE_DIR"
files="$BIN_DIR/siggen $BIN_DIR/tripwire $BIN_DIR/twprint $BIN_DIR/twadmin"
for i in $files; do
    if [ -s "$i" ] ; then
        echo "$i found"
    else
        echo "$i missing.  Build did not complete successfully."
        exit 1
    fi
done
cd "$CWD"

##-------------------------------------------------------
## Print the list of target directories.
##-------------------------------------------------------

echo
echo "This program will copy Tripwire files to the following directories:"
echo

for i in $paths; do
	if [ "$AWK" != "" ] ; then
		eval "echo \"${i}\"	| $AWK '{printf \"%${len}s: \", \$1}'"
		eval "echo \"\$${i}\""
	else
		eval "echo \"\$${i}\""
	fi
done

##-------------------------------------------------------
## Display value of clobber.
##-------------------------------------------------------

echo
echo "CLOBBER is $CLOBBER."

##-------------------------------------------------------
## Prompt to continue.
##-------------------------------------------------------

if [ "$PROMPT" = "true" ] ; then
	echo
	(echo $n "Continue with installation? [y/n] " $c) 1>&2
	read ans
	case "$ans" in
		[yY]*) ;;
		*) echo "Installation has been halted."
			exit 1;
			;;
	esac
fi

##=======================================================
## Create directories.
##=======================================================

echo
echo "----------------------------------------------"
echo "Creating directories..."
echo

##-------------------------------------------------------
## Create only directories that do not already exist.
## Change permissions only on directories we create.
## Exit if mkdir fails.
##-------------------------------------------------------

for i in $path2; do
	eval "d=\$${i}"
	if [ ! -d "$d" ] ; then
		mkdir -p "$d"
		if [ ! -d "$d" ] ; then
			echo "Error: unable to create directory $d"
			exit 1
		else
			echo "$d: created"
			chmod 0750 "$d" > /dev/null
		fi
	else
		test "$PACKAGE_INSTALLER" != "yes" && echo "$d: already exists"
	fi
done

for i in $path3; do
	eval "d=\$${i}"
	if [ ! -d "$d" ] ; then
		mkdir -p "$d"
		if [ ! -d "$d" ] ; then
			echo "Error: unable to create directory $d"
			exit 1
		else
			echo "$d: created"
			chmod 0755 "$d" > /dev/null
		fi
	else
		test "$PACKAGE_INSTALLER" != "yes" && echo "$d: already exists"
	fi
done


##=======================================================
## Copy all files to the location specified.
##=======================================================

test "$PACKAGE_INSTALLER" != "yes" && {
echo
echo "----------------------------------------------"
echo "Copying files..."
echo

##-------------------------------------------------------
## Find all the relevant files. For each
## file in the list, if it exists with size greater
## than zero AND (clobber == false), then don't
## overwrite that file.  
## Associate loose file names with directories.
## Special case the Release_Notes file through README_LOC.
## This is used in the install script's closing message.
## Make sure README_LOC and fil1 stay in sync.
##-------------------------------------------------------

#f1=' ff=$README ; d="" ; dd=$TWDOCS ; rr=0444 '
#f2=' ff=$REL_NOTES ; d="" ; dd=$TWDOCS ; rr=0444 '
f3=' ff=$TWLICENSEFILE ; d="" ; dd=$TWDOCS ; rr=0444 '
#f4=' ff=tripwire ; d="/bin" ; dd=$TWBIN ; rr=0550 '
#f5=' ff=twadmin ; d="/bin" ; dd=$TWBIN ; rr=0550 '
#f6=' ff=twprint ; d="/bin" ; dd=$TWBIN ; rr=0550 '
#f7=' ff=siggen ; d="/bin" ; dd=$TWBIN ; rr=0550 '
f8=' ff=TRADEMARK ; d="" ; dd=$TWDOCS ; rr=0444 '
f9=' ff=policyguide.txt ; d="/policy" ; dd=$TWDOCS ; rr=0444 '
f10=' ff=${POLICYSRC} ; d="/policy" ; dd=$TWPOLICY ; rr=0640 '
f11=' ff=twpolicy.5 ; d="/man/man5" ; dd=$TWMAN/man5 ; rr=0444 '
f12=' ff=twconfig.5 ; d="/man/man5" ; dd=$TWMAN/man5 ; rr=0444 '
f13=' ff=twfiles.5 ; d="/man/man5" ; dd=$TWMAN/man5 ; rr=0444 '
f14=' ff=siggen.8 ; d="/man/man8" ; dd=$TWMAN/man8 ; rr=0444 '
f15=' ff=tripwire.8 ; d="/man/man8" ; dd=$TWMAN/man8 ; rr=0444 '
f16=' ff=twadmin.8 ; d="/man/man8" ; dd=$TWMAN/man8 ; rr=0444 '
f17=' ff=twintro.8 ; d="/man/man8" ; dd=$TWMAN/man8 ; rr=0444 '
f18=' ff=twprint.8 ; d="/man/man8" ; dd=$TWMAN/man8 ; rr=0444 '
# Binaries and manpages are already installed by the install target
loosefiles="f3 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18"

for i in $loosefiles; do
	eval "eval \"\$$i\""
	f=${TAR_DIR}$d/$ff
	ff=${dd}/$ff
	if [ -s $ff ] && [ "$CLOBBER" = "false" ] ; then
		test "$PACKAGE_INSTALLER" != "yes" && echo "$ff: file already exists"
	else
		cp "$f" "$dd"
                if [ $? -eq 0 ]; then
			echo "$ff: copied"
			    chmod "$rr" "$ff" > /dev/null
                        else
			echo "$ff: copy failed"
		fi
       	fi
done
}

case $DO_NOT_CONFIG in
yes)	exit;;
*)	;;
esac

##-------------------------------------------------------
## Copy isi_sec_tw_gendb and isi_sec_tw_chpass to
## /usr/local/sbin.
##-------------------------------------------------------

for p in "isi_sec_tw_gendb" "isi_sec_tw_chpass" ; do
        CWD=`pwd`
	cd ../../
	fpath="$p"
         if [ -s "$fpath" ] ; then
                cp "$fpath" "${TWBIN}/$p"
                        if [ $? -eq 0 ]; then
                                chmod 0500 ${TWBIN}/$p > /dev/null
                                echo "${TWBIN}/$p: copied "
                        else
                                echo "${TWBIN}/$p copy failed"
                        fi
	else
		echo "$fpath does not exist"
        fi
	cd "$CWD"
done

##=======================================================
## Files are now present on user's system.
## Begin Tripwire configuration.
##=======================================================

##-------------------------------------------------------
## Set default values for Tripwire file names.
##-------------------------------------------------------

HOST_NAME=`uname -n 2>/dev/null`

LOCAL_KEY="${TWLOCALKEYDIR}/${HOST_NAME:=localhost}-local.key"
SITE_KEY="${TWSITEKEYDIR}/site.key"
CONFIG_FILE="${TWPOLICY}/$TW_CONFIG_FILE"  # Signed config file
POLICY_FILE="${TWPOLICY}/tw.pol"        # Signed policy file
TXT_CFG="${TWPOLICY}/${CLR_CONFIG_FILE}"   # Cleartext config file
TXT_POL="${TWPOLICY}/$CLR_POLICY_FILE"  # Cleartext policy file
TWADMIN="${TWBIN}/twadmin"

##-------------------------------------------------------
## If user has to enter a passphrase, give some
## advice about what is appropriate.
##-------------------------------------------------------

if [ -z "$TW_SITE_PASS" ] || [ -z "$TW_LOCAL_PASS" ]; then
cat << END_OF_TEXT

----------------------------------------------
The Tripwire site and local passphrases are used to
sign a variety of files, such as the configuration,
policy, and database files.

Passphrases should be at least 8 characters in length
and contain both letters and numbers.

See the Tripwire manual for more information.
END_OF_TEXT
fi

##=======================================================
## Generate keys.
##=======================================================

echo
echo "----------------------------------------------"
echo "Creating key files..."

##-------------------------------------------------------
## Site key file.
##-------------------------------------------------------

# If clobber is true, and prompting is off (unattended operation)
# and the key file already exists, remove it.  Otherwise twadmin
# will prompt with an "are you sure?" message.

if [ "$CLOBBER" = "true" ] && [ "$PROMPT" = "false" ] && [ -f "$SITE_KEY" ] ; then
        rm -f "$SITE_KEY"
fi

if [ -f "$SITE_KEY" ] && [ "$CLOBBER" = "false" ] ; then
	echo "The site key file \"$SITE_KEY\""
	echo 'exists and will not be overwritten.'
else
	cmdargs="--generate-keys --site-keyfile \"$SITE_KEY\""
	if [ -n "$TW_SITE_PASS" ] ; then
		cmdargs="$cmdargs --site-passphrase \"$TW_SITE_PASS\""
     	fi
	eval "\"$TWADMIN\" $cmdargs"
	if [ $? -ne 0 ] ; then
		echo "Error: site key generation failed"
		exit 1
        else chmod 640 "$SITE_KEY"
	fi
fi

##-------------------------------------------------------
## Local key file.
##-------------------------------------------------------

# If clobber is true, and prompting is off (unattended operation)
# and the key file already exists, remove it.  Otherwise twadmin
# will prompt with an "are you sure?" message.

if [ "$CLOBBER" = "true" ] && [ "$PROMPT" = "false" ] && [ -f "$LOCAL_KEY" ] ; then
        rm -f "$LOCAL_KEY"
fi

if [ -f "$LOCAL_KEY" ] && [ "$CLOBBER" = "false" ] ; then
	echo "The site key file \"$LOCAL_KEY\""
	echo 'exists and will not be overwritten.'
else
	cmdargs="--generate-keys --local-keyfile \"$LOCAL_KEY\""
	if [ -n "$TW_LOCAL_PASS" ] ; then
		cmdargs="$cmdargs --local-passphrase \"$TW_LOCAL_PASS\""
        fi
	eval "\"$TWADMIN\" $cmdargs"
	if [ $? -ne 0 ] ; then
		echo "Error: local key generation failed"
		exit 1
        else chmod 640 "$LOCAL_KEY"
	fi
fi

##=======================================================
## Generate tripwire configuration file.
##=======================================================

echo
echo "----------------------------------------------"
echo "Generating Tripwire configuration file..."

cat << END_OF_TEXT > "$TXT_CFG"
ROOT          =$TWBIN
POLFILE       =$POLICY_FILE
DBFILE        =$TWDB/\$(HOSTNAME).twd
REPORTFILE    =$TWREPORT/\$(HOSTNAME)-\$(DATE).twr
SITEKEYFILE   =$SITE_KEY
LOCALKEYFILE  =$LOCAL_KEY
EDITOR        =$TWEDITOR
LATEPROMPTING =${TWLATEPROMPTING:-false}
LOOSEDIRECTORYCHECKING =${TWLOOSEDIRCHK:-false}
MAILNOVIOLATIONS =${TWMAILNOVIOLATIONS:-true}
EMAILREPORTLEVEL =${TWEMAILREPORTLEVEL:-3}
REPORTLEVEL   =${TWREPORTLEVEL:-3}
MAILMETHOD    =${TWMAILMETHOD:-SENDMAIL}
SYSLOGREPORTING =${TWSYSLOG:=true}
END_OF_TEXT

if [ "$TWMAILMETHOD" = "SMTP" ] ; then
cat << SMTP_TEXT >> "$TXT_CFG"
SMTPHOST      =${TWSMTPHOST:-mail.domain.com}
SMTPPORT      =${TWSMTPPORT:-"25"}
SMTP_TEXT
else
cat << SENDMAIL_TEXT >> "$TXT_CFG"
MAILPROGRAM   =$TWMAILPROGRAM
SENDMAIL_TEXT
fi

if [ ! -s "$TXT_CFG" ] ; then
	echo "Error: unable to create $TXT_CFG"
	exit 1
fi

chmod 640 "$TXT_CFG"

##=======================================================
## Create signed tripwire configuration file.
##=======================================================

echo
echo "----------------------------------------------"
echo "Creating signed configuration file..."

##-------------------------------------------------------
## If noclobber, then backup any existing config file.
##-------------------------------------------------------

if [ "$CLOBBER" = "false" ] && [ -s "$CONFIG_FILE" ] ; then
	backup="${CONFIG_FILE}.$$.bak"
	echo "Backing up $CONFIG_FILE"
	echo "        to $backup"
	`mv "$CONFIG_FILE" "$backup"`
	if [ $? -ne 0 ] ; then
		echo "Error: backup of configuration file failed."
		exit 1
	fi
fi

##-------------------------------------------------------
## Build command line.
##-------------------------------------------------------

cmdargs="--create-cfgfile"
cmdargs="$cmdargs --cfgfile \"$CONFIG_FILE\""
cmdargs="$cmdargs --site-keyfile \"$SITE_KEY\""
if [ -n "$TW_SITE_PASS" ] ; then
	cmdargs="$cmdargs --site-passphrase \"$TW_SITE_PASS\""
fi

##-------------------------------------------------------
## Sign the file.
##-------------------------------------------------------

eval "\"$TWADMIN\" $cmdargs \"$TXT_CFG\""
if [ $? -ne 0 ] ; then
	echo "Error: signing of configuration file failed."
	exit 1
fi

# Set the rights properly
chmod 640 "$CONFIG_FILE"

echo "---------------------------------------------------" 
cat << END_OF_TEXT

Removing clear-text version of the Tripwire configuration file
$TXT_CFG.Which generated during installation. You can
generate text version configuration file anytime by using
twadmin.

END_OF_TEXT

##=======================================================
## Clean-up.
##=======================================================

 if [ -s "$TXT_CFG" ] ; then
        rm -f "$TXT_CFG"
                if [ $? -eq 0 ]; then
                        echo "$TXT_CFG : removed "
                        else
                         echo "$TXT_CFG : removed failed"
                fi

fi

##=======================================================
## Modify default policy file with file locations
##=======================================================

echo
echo "----------------------------------------------"
echo "Customizing default policy file..."

sed '/@@section GLOBAL/,/@@section FS/  {
  s?^\(TWROOT=\).*$?TWDOCS='\""$TWDOCS"\"';?
  s?^\(TWBIN=\).*$?\1'\""$TWBIN"\"';?
  s?^\(TWPOL=\).*$?\1'\""$TWPOLICY"\"';?
  s?^\(TWDB=\).*$?\1'\""$TWDB"\"';?
  s?^\(TWSKEY=\).*$?\1'\""$TWSITEKEYDIR"\"';?
  s?^\(TWLKEY=\).*$?\1'\""$TWLOCALKEYDIR"\"';?
  s?^\(TWREPORT=\).*$?\1'\""$TWREPORT"\"';?
  s?^\(HOSTNAME=\).*$?\1'"$HOST_NAME"';?
}' "${TWPOLICY}/${POLICYSRC}" > "${TXT_POL}.tmp"

# copy the tmp file back over the default policy
[ -f "${TXT_POL}" ] && cp "${TXT_POL}" "${TXT_POL}.bak"
mv "${TXT_POL}.tmp" "${TXT_POL}"
rm -f "${TWPOLICY}/${POLICYSRC}"

# reset rights on the policy files to 640
[ -f "${TXT_POL}" ] && chmod 640 "$TXT_POL"
[ -f "${TXT_POL}.bak" ] && chmod 640 "${TXT_POL}.bak"


##=======================================================
## Create signed tripwire policy file.
##=======================================================

echo
echo "----------------------------------------------"
echo "Creating signed policy file..."

##-------------------------------------------------------
## If noclobber, then backup any existing policy file.
##-------------------------------------------------------

if [ "$CLOBBER" = "false" ] && [ -s "$POLICY_FILE" ] ; then
	backup="${POLICY_FILE}.$$.bak"
	echo "Backing up $POLICY_FILE"
	echo "        to $backup"
	mv "$POLICY_FILE" "$backup"
	if [ $? -ne 0 ] ; then
		echo "Error: backup of policy file failed."
		exit 1
	fi
fi

##-------------------------------------------------------
## Build command line.
##-------------------------------------------------------

cmdargs="--create-polfile"
cmdargs="$cmdargs --cfgfile \"$CONFIG_FILE\""
cmdargs="$cmdargs --site-keyfile \"$SITE_KEY\""
if [ -n "$TW_SITE_PASS" ] ; then
	cmdargs="$cmdargs --site-passphrase \"$TW_SITE_PASS\""
fi

##-------------------------------------------------------
## Sign the file.
##-------------------------------------------------------

eval "\"$TWADMIN\" $cmdargs \"$TXT_POL\""
if [ $? -ne 0 ] ; then
	echo "Error: signing of policy file failed."
	exit 1
fi

# Set the proper rights on the newly signed policy file.
chmod 0640 "$POLICY_FILE"

echo "---------------------------------------------------"
cat << END_OF_TEXT

Removing clear-text version of the Tripwire policy file
$TXT_POL.Which generated during installation. You can
generate text version policy file anytime by using 
twadmin. 

END_OF_TEXT

##=======================================================
## Clean-up.
##=======================================================

 if [ -s "$TXT_POL" ] ; then
	rm -f "$TXT_POL" 
		if [ $? -eq 0 ]; then
                        echo "$TXT_POL : removed "
                        else
 			 echo "$TXT_POL : removed failed"	                       
                fi

 fi

echo "--------------------------------------------------"

##--------------------------------------------------------
## Set the proper rights on the twadmin binary file .
##-------------------------------------------------------
 
echo "Setting proper rights on the twadmin ........ "  
 if [ -s "$TWBIN/twadmin" ] ; then
        chmod 0500 "$TWBIN/twadmin"
 fi

cat << END_OF_TEXT

----------------------------------------------
The installation succeeded.

Please refer to $README_LOC
for release information and to the printed user documentation
for further instructions on using Tripwire 2.4 Open Source.

END_OF_TEXT

cd "$START_DIR"

