#!/bin/sh

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if [ -r /etc/ssh/sshd_config ]
then
		logger -t xcat "Install: setup /etc/ssh/sshd_config"
		cp /etc/ssh/sshd_config /etc/ssh/sshd_config.ORIG
		perl -pi -e 's/^X11Forwarding .*$/X11Forwarding yes/' /etc/ssh/sshd_config
		perl -pi -e 's/^KeyRegenerationInterval .*$/KeyRegenerationInterval 0/' /etc/ssh/sshd_config
		perl -pi -e 's/(.*MaxStartups.*)/#\1/' /etc/ssh/sshd_config
	
		cp /etc/ssh/sshd_config /etc/ssh/external_sshd_config
		perl -pi -e 's/.*PidFile .*$/PidFile \/var\/run\/ext_sshd.pid/' /etc/ssh/external_sshd_config

fi

if [ -d /post/.ssh ]
then
		logger -t xcat "Install: setup root .ssh"
		cd /post/.ssh
		mkdir -p /root/.ssh
		cp -f * /root/.ssh
		chmod 700 /root/.ssh
		chmod 600 /root/.ssh/*
fi

logger -t xcat "Install: writing rc.local"
# make rc.local to change "ListenAddress" in sshd's configs accordingly to network interfaces values
# so it will happen on reboot
echo "" >> /etc/rc.local
echo "
IP0=\$(ifconfig eth0 | grep 'inet addr' | awk '{print \$2}' | awk -F: '{print \$2}')
IP1=\$(ifconfig eth1 | grep 'inet addr' | awk '{print \$2}' | awk -F: '{print \$2}')
perl -pi -e 's/^AllowUsers .*$newline//' /etc/ssh/sshd_config
perl -pi -e 's/^AllowUsers .*$newline//' /etc/ssh/external_sshd_config
perl -pi -e 's/^ListenAddress .*$newline//' /etc/ssh/sshd_config
perl -pi -e 's/^ListenAddress .*$newline//' /etc/ssh/external_sshd_config
echo \"AllowUsers root\" >> /etc/ssh/sshd_config
echo \"ListenAddress \$IP0\" >> /etc/ssh/sshd_config
echo \"ListenAddress \$IP1\" >> /etc/ssh/external_sshd_config
/etc/rc.d/init.d/ext_sshd stop
/etc/rc.d/init.d/sshd stop
sleep 2
/etc/rc.d/init.d/sshd start

" >> /etc/rc.local

rm -f /etc/issue.net

exit 0

