OpenSolaris_b135/pkgdefs/common_files/i.ftpaccess

#!/bin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# ident	"%Z%%M%	%I%	%E% SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
# This script serves two purposes.  First, it saves the original copy
# of ftpaccess if there is one, and writes a log entry if this action
# was taken.
# Second, it parses the etc/default file for BANNER and UMASK directives
# and re-writes the values into the etc/inet/ftpaccess file IFF the
# basename of the file is ftpaccess (just in case this class script is
# reused for other files).
#

tag=new
CLEANUP_FILE=/tmp/CLEANUP
#
# By default, remove the default/ftpd file.  If expansions cause
# problems, this option will be turned off.
RMDEFAULT=1
defaultfile=/etc/default/ftpd
defaultpath=${PKG_INSTALL_ROOT}${defaultfile}
bannermsg="Could not update greeting directive"
resolvmsg="Please manually convert BANNER in %s into greeting text in %s and then remove %s"

if [ -f $defaultpath ]; then
	# Check for UMASK and BANNER strings in the default/ftpd file
	# The UMASK string should be all numeric, but is still correctly
	# parsed if there are trailing non-numeric characters.  Also
	# note that only trailing characters and dumped
	# It is still OK to delete the DEFAULT file as any value
	# other than numeric was previously ignored.

	defumask=`sed -n '/^[	 ]*UMASK=.*$/{
		s/^[	 ]*UMASK=\([0-9]*\).*/\1/p
		q
	}' $defaultpath`

	# The BANNER value is a little more complex, as we need to look
	# at each character for the "Special" characters: "`", "$", "\".
	# Note that the special characters need to be inline below, and
	# must each be escaped inside the single quotes.

	banner=`sed -n '/^[	 ]*BANNER=.*$/{
		s/^[	 ]*BANNER=//p
		q
	}' $defaultpath`
	greeting=`echo "$banner" | sed "s/^[\'\"]//" | sed "s/[\'\"]$//"`

	if [ -n "$banner" ]; then
		echo "$greeting" | egrep -s '[\$\`\\]'
		RMDEFAULT=$?
		if [ $RMDEFAULT -eq 0 ]; then
			greeting=""
		fi
	fi
fi

while read src dest; do
	#
	# Put the defumask and greeting values into the ftpaccess file
	#
	target=`basename $src`
	if [ $target = ftpaccess ]; then
		edit=$src
		if [ -n "$greeting" ]; then
			nawk '$1 == "greeting" \
			    {print "greeting	text	" greeting; next}\
			    {print $0}' greeting="$greeting" $edit > /tmp/d1.$$

			edit=/tmp/d1.$$
		fi
		if [ -n "$defumask" ]; then
			cat $edit > /tmp/d2.$$
			echo "defumask	$defumask" >> /tmp/d2.$$
			edit=/tmp/d2.$$
		fi
		src=$edit
		if [ $RMDEFAULT -eq 0 ]; then
			printf "%s: $bannermsg\n" $dest >> $CLEANUP_FILE
			printf "%s: $resolvmsg\n" $dest $defaultfile $dest \
				$defaultfile >> $CLEANUP_FILE
		else
			rm -f $defaultpath
		fi
	fi
	if [ ! -f $dest ]; then
		cp $src $dest
	else
		cmp -s $src $dest
		if [ $? -ne 0 ]; then
			cp $src $dest.$tag
			echo "EXISTING_FILE_PRESERVED: $dest $dest.$tag" \
			    >> $CLEANUP_FILE
		fi
	fi
	rm -f /tmp/d1.$$ /tmp/d2.$$
done

exit 0