#!/bin/bash
#
#Copyright 2012 CurlyMo <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian is free software: you can redistribute it and/or modify it under the
#terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or (at your option) any later
#version.
#
#XBian is distributed in the hope that it will be useful, but WITHOUT ANY
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
#details.
#
#You should have received a copy of the GNU General Public License along
#with XBian. If not, see <http://www.gnu.org/licenses/>

# Checks if user root has a password set
# Returns
#  *[Integer]
#  - 1: No root password is set
#  - 0: Root password is set
function rootPasswordSetFn() {
	if [ "$(sed -ne 's/root:\([\$A-Za-z0-9*]\{1,\}\).*/\1/p' /etc/shadow)" = "*" ]; then
		return 0;
	else
		return 1;
	fi
}

# Enables root ssh login
# Returns
#  $?[Integer]
#  - 1: Success
#  - 0: Failed
function enableSSHRootLoginFn() {
	sed 's/^[# \t]*//' /etc/ssh/sshd_config | grep -q "^PermitRootLogin" || echo "PermitRootLogin" >> /etc/ssh/sshd_config
	sed -i 's/^[# \t]*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config
	isSSHRootLoginEnabledFn
	return $?;
}

# Disables root ssh login
# Returns
#  $?[Integer]
#  - 1: Success
#  - 0: Failed
function disableSSHRootLoginFn() {
	sed 's/^[# \t]*//' /etc/ssh/sshd_config | grep -q "^PermitRootLogin" || echo "PermitRootLogin" >> /etc/ssh/sshd_config
	sed -i 's/^[# \t]*PermitRootLogin.*/PermitRootLogin no/g' /etc/ssh/sshd_config
	isSSHRootLoginEnabledFn
	if [ $? -eq 0 ]; then
		return 1;
	else
		return 0;
	fi
}

# Checks if root ssh login is enabled
# Returns
#  $?[Integer]
#  - 1: Enabled
#  - 0: Disabled
function isSSHRootLoginEnabledFn() {
	if [ $(sed 's/^[ \t]*//' /etc/ssh/sshd_config | grep -c "^PermitRootLogin yes") -eq 0 ]; then
		return 0;
	else
		return 1;
	fi
}
