#!/bin/bash

# This script turns on root SSH access as required by Starcluster.
#
# After downloading this file, reference it in your Starcluster config file
# for the configuration setting "USERDATA_SCRIPTS"
#
# version 1.0 - 12/23/2015

exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
echo "Changing SSH config and restarting daemaon."
date '+%Y-%m-%d %H:%M:%S'
SSHD_CONF=/etc/ssh/sshd_config
CONF_LINE="^[[:space:]]*PermitRootLogin[[:space:]]+yes$"
SED_PROGRAM='s/^[# ]*PermitRootLogin .*$/PermitRootLogin yes/'
grep -q -E "${CONF_LINE}" ${SSHD_CONF} && return 0
sed -i.orig "${SED_PROGRAM}" ${SSHD_CONF} || return $?
service ssh restart
