mirror of
https://github.com/m8tin/cis.git
synced 2025-12-06 07:48:26 +01:00
Further development into CIS (core infrastructure system)
This commit is contained in:
@@ -6,10 +6,10 @@
|
||||
|
||||
|
||||
function checkPermissions(){
|
||||
local _FOLDER _REPOSITORY
|
||||
local _FOLDER _RIGHTS
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_RIGHTS="${2:?"Missing second parameter RIGHTS"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
readonly _FOLDER _RIGHTS
|
||||
|
||||
[ "${_RIGHTS}" == "readonly" ] \
|
||||
&& [ -d "${_FOLDER}/.git" ] \
|
||||
@@ -21,30 +21,9 @@ function checkPermissions(){
|
||||
&& git -C "${_FOLDER}" push --dry-run &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The rights of the repository are incorrect: ("$(readlink -f ${0})")"
|
||||
echo " - '${_FOLDER}' is not '${_RIGHTS}'"
|
||||
echo " - check the settings of gitea."
|
||||
return 1
|
||||
}
|
||||
|
||||
function checkRemoteRepository() {
|
||||
local _FOLDER _REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
#Should exist after successful clone only, therefore the remote repository exists and was accessible.
|
||||
[ -d "${_FOLDER}/.git" ] \
|
||||
&& return 0
|
||||
|
||||
#Checks if repository exists and is accessible.
|
||||
! [ -d "${_FOLDER}/.git" ] \
|
||||
&& git ls-remote "${_REPOSITORY}" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The remote repository is not accessible: ("$(readlink -f ${0})")"
|
||||
echo " - '${_REPOSITORY}'"
|
||||
echo " - check the settings of gitea."
|
||||
echo "FAIL: The rights of the repository are incorrect: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_FOLDER}' is not '${_RIGHTS}'" >&2
|
||||
echo " - check the settings of gitea." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -54,42 +33,67 @@ function cloneOrPull {
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY"}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
|
||||
! [ -d "${_FOLDER}/.git" ] \
|
||||
&& git clone "${_REPOSITORY}" "${_FOLDER}" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
[ -d "${_FOLDER}/.git" ] \
|
||||
&& git -C "${_FOLDER}" pull &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The local repository is not updatable: ("$(readlink -f ${0})")"
|
||||
echo " - '${_FOLDER}'"
|
||||
echo " - check your network and the permissions in gitea."
|
||||
! [ -d "${_FOLDER}/.git" ] \
|
||||
&& git clone "${_REPOSITORY}" "${_FOLDER}" &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The local repository is not updatable: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_FOLDER}'" >&2
|
||||
echo " - check your network and the permissions in gitea." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
function printRepository(){
|
||||
local _FOLDER _CONFIGURED_REPOSITORY _SUGGESTED_REPOSITORY
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_CONFIGURED_REPOSITORY="$(git -C "${_FOLDER:?"Missing FOLDER"}" config --get remote.origin.url 2> /dev/null)"
|
||||
_SUGGESTED_REPOSITORY="${2}"
|
||||
readonly _FOLDER _CONFIGURED_REPOSITORY _SUGGESTED_REPOSITORY
|
||||
|
||||
! [ -z "${_CONFIGURED_REPOSITORY}" ] \
|
||||
&& echo "${_CONFIGURED_REPOSITORY}" \
|
||||
&& return 0
|
||||
|
||||
while true; do
|
||||
read -e -p "Enter ssh URL to clone Repository: " -i "${_SUGGESTED_REPOSITORY}" _REPOSITORY
|
||||
echo "${_REPOSITORY}" | grep -F 'git@' &> /dev/null \
|
||||
&& git ls-remote "${_REPOSITORY}" &> /dev/null \
|
||||
&& echo "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
|
||||
&& return 0
|
||||
done
|
||||
|
||||
echo "FAIL: The remote repository is not accessible: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_REPOSITORY}'" >&2
|
||||
echo " - check the settings of gitea." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
# Note that an unprivileged user can use this script successfully,
|
||||
# if no user has to be added to the host because it already exists.
|
||||
function addAndCheckGitRepository() {
|
||||
local _FOLDER _REPOSITORY
|
||||
local _FOLDER _REPOSITORY _RIGHTS
|
||||
_FOLDER="${1:?"Missing first parameter FOLDER"}"
|
||||
_REPOSITORY="${2:?"Missing second parameter REPOSITORY: e.g. ssh://git@your.domain.com/iss.git "}"
|
||||
_RIGHTS="${3:?"Missing third parameter RIGHTS: (readonly, writable) "}"
|
||||
readonly _FOLDER _REPOSITORY
|
||||
_RIGHTS="${2:?"Missing second parameter RIGHTS: (readonly, writable) "}"
|
||||
_REPOSITORY="$(printRepository "${_FOLDER}" "${3}")"
|
||||
readonly _FOLDER _REPOSITORY _RIGHTS
|
||||
|
||||
checkRemoteRepository "${_FOLDER}" "${_REPOSITORY}" \
|
||||
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY}" \
|
||||
echo \
|
||||
&& cloneOrPull "${_FOLDER}" "${_REPOSITORY:?"Missing REPOSITORY: e.g. ssh://git@your.domain.com/cis.git"}" \
|
||||
&& checkPermissions "${_FOLDER}" "${_RIGHTS}" \
|
||||
&& echo "SUCCESS: The git repository is usable. ("$(readlink -f ${0})")" \
|
||||
&& echo " - remote repository: '${_REPOSITORY}'" \
|
||||
&& echo " - local repository: '${_FOLDER}' (${_RIGHTS})" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The repository is not functional: ("$(readlink -f ${0})")"
|
||||
echo " - remote repository: '${_REPOSITORY}'"
|
||||
echo " - local repository: '${_FOLDER}'"
|
||||
echo " - due to an error or insufficient rights or"
|
||||
echo " - one check failed."
|
||||
echo "FAIL: The repository is not functional: ("$(readlink -f ${0})")" >&2
|
||||
echo " - remote repository: '${_REPOSITORY}'" >&2
|
||||
echo " - local repository: '${_FOLDER}'" >&2
|
||||
echo " - due to an error or insufficient rights or" >&2
|
||||
echo " - one check failed." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -98,4 +102,6 @@ addAndCheckGitRepository \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${3} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
&& exit 0
|
||||
|
||||
exit 1
|
||||
|
||||
@@ -18,6 +18,8 @@ function addNormalUser() {
|
||||
&& echo " - '${_USER}'" \
|
||||
&& return 0
|
||||
|
||||
# useradd is a low level utility ... use adduser(8) instead.
|
||||
# See: https://askubuntu.com/questions/345974/what-is-the-difference-between-adduser-and-useradd
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& adduser --gecos 'Normal user' --disabled-password "${_USER}" \
|
||||
&& chown -R "${_USER}:${_USER}" "/home/${_USER}" \
|
||||
@@ -27,13 +29,14 @@ function addNormalUser() {
|
||||
&& echo " - existing home directories were taken over" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The user could not be created: ("$(readlink -f ${0})")"
|
||||
echo " - '${_USER}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
echo "FAIL: The user could not be created: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_USER}'" >&2
|
||||
echo " - due to an error or insufficient rights." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
# sanitizes all parameters
|
||||
addNormalUser \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
addNormalUser "$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0
|
||||
|
||||
exit 1
|
||||
|
||||
@@ -5,14 +5,17 @@
|
||||
|
||||
|
||||
|
||||
# Folders always ends with an tailing '/'
|
||||
_SCRIPT="$(readlink -f "${0}" 2> /dev/null)"
|
||||
_CIS_ROOT="${_SCRIPT%%/core/*}/" #Removes longest matching pattern '/core/*' from the end
|
||||
|
||||
# Note that an unprivileged user can use this script successfully,
|
||||
# if no user has to be added to the host because it already exists.
|
||||
function addToCrontabEveryHour() {
|
||||
local _ROOT _MINUTE_VALUE _STRING
|
||||
_ROOT="${0%%/core/*}/" #Removes longest matching pattern '/core/*' from the end
|
||||
local _MINUTE_VALUE _STRING
|
||||
! [ -z "${2##*[!0-9]*}" ] && _MINUTE_VALUE=$((${2}%60)) # if second parameter is integer then (minute-value % 60) as safe guard
|
||||
_STRING="${_MINUTE_VALUE:?"Missing MINUTE_VALUE"} * * * * ${1:?"Missing first parameter COMMAND"} > /dev/null 2>&1"
|
||||
readonly _ROOT _MINUTE_VALUE _STRING
|
||||
readonly _MINUTE_VALUE _STRING
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& crontab -l | grep -qF "${_STRING:?"Missing CRON_STRING"}" \
|
||||
@@ -21,11 +24,11 @@ function addToCrontabEveryHour() {
|
||||
&& return 0
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
&& echo "${_ROOT:?"Missing ROOT"}" | grep "home" &> /dev/null \
|
||||
&& echo "${_CIS_ROOT:?"Missing CIS_ROOT"}" | grep -F 'home' &> /dev/null \
|
||||
&& echo "SUCCESS: Although the entry will be skipped: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_STRING}'" \
|
||||
&& echo " that is because the current environment is:" \
|
||||
&& echo " - ${_ROOT}" \
|
||||
&& echo " - ${_CIS_ROOT}" \
|
||||
&& return 0
|
||||
|
||||
[ "$(id -u)" == "0" ] \
|
||||
@@ -37,9 +40,9 @@ function addToCrontabEveryHour() {
|
||||
&& echo " - '${_STRING}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: Entry could not be registered to crontab: ("$(readlink -f ${0})")"
|
||||
echo " - '${_STRING:?"Missing CRON_STRING"}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
echo "FAIL: Entry could not be registered to crontab: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_STRING:?"Missing CRON_STRING"}'" >&2
|
||||
echo " - due to an error or insufficient rights." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -47,4 +50,6 @@ function addToCrontabEveryHour() {
|
||||
addToCrontabEveryHour \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
&& exit 0
|
||||
|
||||
exit 1
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
Cmnd_Alias C_JENKINS = \
|
||||
/cis/updateRepositories.sh --core, \
|
||||
/cis/updateRepositories.sh --scripts, \
|
||||
/cis/updateRepositories.sh --definitions, \
|
||||
/cis/updateRepositories.sh --states
|
||||
jenkins ALL = (root) NOPASSWD: C_JENKINS
|
||||
@@ -44,24 +44,24 @@ function prepareFolder() {
|
||||
&& echo " - '${_SSH_FOLDER}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The ssh folder could not be prepared: ("$(readlink -f ${0})")"
|
||||
echo " - '${_SSH_FOLDER}'"
|
||||
echo " - due to an error or insufficient rights."
|
||||
echo "FAIL: The ssh folder could not be prepared: ("$(readlink -f ${0})")" >&2
|
||||
echo " - '${_SSH_FOLDER}'" >&2
|
||||
echo " - due to an error or insufficient rights." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
function defineAuthorizedKeysOfUser() {
|
||||
local _ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
local _CIS_ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
||||
_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
_CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
#Build from components for safety
|
||||
_DEFINITIONS="${_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}"
|
||||
_DEFINITIONS="${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}"
|
||||
|
||||
_USER="${2:?"Missing second parameter USER"}"
|
||||
_CORE_SCRIPTS="${_ROOT:?"Missing ROOT"}core/"
|
||||
readonly _ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
_CORE_SCRIPTS="${_CIS_ROOT:?"Missing ROOT"}core/"
|
||||
readonly _CIS_ROOT _CORE_SCRIPTS _DOMAIN _DEFINITIONS _USER
|
||||
|
||||
case "${_USER:?"Missing USER"}" in
|
||||
root)
|
||||
@@ -83,4 +83,6 @@ function defineAuthorizedKeysOfUser() {
|
||||
defineAuthorizedKeysOfUser \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
&& exit 0
|
||||
|
||||
exit 1
|
||||
|
||||
@@ -12,46 +12,76 @@ function printIfEqual() {
|
||||
}
|
||||
|
||||
function isCoreDefinition() {
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/root/.ssh/authorized_keys" &> /dev/null \
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep -F '/root/.ssh/authorized_keys' &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/home/jenkins/.ssh/authorized_keys" &> /dev/null \
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep -F '/home/jenkins/.ssh/authorized_keys' &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep "/etc/sudoers.d/allow-jenkins-updateRepositories" &> /dev/null \
|
||||
echo "${1:?"Missing first parameter FILE"}" | grep -F '/etc/sudoers.d/allow-jenkins-updateRepositories' &> /dev/null \
|
||||
&& return 0
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
function filterInvalidAuthorizedKeysFilesOfRoot() {
|
||||
local _FILE_DEFINED
|
||||
_FILE_DEFINED="${1:?"Missing DEFINITION FILE"}"
|
||||
readonly _FILE_DEFINED
|
||||
|
||||
#If the full filename contains 'root/.ssh/authorized_keys' then check the content.
|
||||
#Skip lines starting with '#' and if at least one remaining line contains 'ssh' and '@' then print the filename.
|
||||
echo "${_FILE_DEFINED}" | grep -F 'root/.ssh/authorized_keys' &> /dev/null \
|
||||
&& grep -vE '^[[:blank:]]*#' "${_FILE_DEFINED}" | grep -F 'ssh' | grep -F '@' &> /dev/null \
|
||||
&& echo "${_FILE_DEFINED}" \
|
||||
&& return 0
|
||||
|
||||
#If the full filename contains 'root/.ssh/authorized_keys' print nothing because the file has to be invalid.
|
||||
echo "${_FILE_DEFINED}" | grep -F 'root/.ssh/authorized_keys' &> /dev/null \
|
||||
&& echo \
|
||||
&& return 0
|
||||
|
||||
#Print the full filename because it does not contain 'root/.ssh/authorized_keys'
|
||||
echo "${_FILE_DEFINED}"
|
||||
return 0
|
||||
}
|
||||
|
||||
function printSelectedDefinition() {
|
||||
local _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
_CORE_FILE_DEFINED_ALL_HOSTS="${1:?"Missing DEFINITIONS"}/core/all${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_CORE_FILE_DEFINED_THIS_HOST="${1:?"Missing DEFINITIONS"}/core/$(hostname -s)${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_ALL_HOSTS="${1:?"Missing DEFINITIONS"}/hosts/all${2:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_THIS_HOST="${1:?"Missing DEFINITIONS"}/hosts/$(hostname -s)${2:?"Missing CURRENT_FULLFILE"}"
|
||||
readonly _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
local _DEFINITIONS _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
_DEFINITIONS="${1:?"Missing CIS_ROOT"}definitions/${2:?"Missing DOMAIN"}/"
|
||||
_CORE_DEFAULT_ALL_HOSTS="${1:?"Missing CIS_ROOT"}core/default${3:?"Missing CURRENT_FULLFILE"}"
|
||||
_CORE_FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}core/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||
_CORE_FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}core/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_ALL_HOSTS="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/all${3:?"Missing CURRENT_FULLFILE"}"
|
||||
_FILE_DEFINED_THIS_HOST="${_DEFINITIONS:?"Missing DEFINITIONS"}hosts/$(hostname -s)${3:?"Missing CURRENT_FULLFILE"}"
|
||||
readonly _DEFINITIONS _CORE_FILE_DEFINED_ALL_HOSTS _CORE_FILE_DEFINED_THIS_HOST _FILE_DEFINED_ALL_HOSTS _FILE_DEFINED_THIS_HOST
|
||||
|
||||
#The following are special definitions that affect the core functionality.
|
||||
#Try this host first because it should be priorized.
|
||||
isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
isCoreDefinition "${3:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_CORE_FILE_DEFINED_THIS_HOST}" ] \
|
||||
&& echo "${_CORE_FILE_DEFINED_THIS_HOST}" \
|
||||
&& filterInvalidAuthorizedKeysFilesOfRoot "${_CORE_FILE_DEFINED_THIS_HOST}" \
|
||||
&& return 0
|
||||
|
||||
#The following are special definitions that affect the core functionality.
|
||||
isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
isCoreDefinition "${3:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_CORE_FILE_DEFINED_ALL_HOSTS}" ] \
|
||||
&& echo "${_CORE_FILE_DEFINED_ALL_HOSTS}" \
|
||||
&& filterInvalidAuthorizedKeysFilesOfRoot "${_CORE_FILE_DEFINED_ALL_HOSTS}" \
|
||||
&& return 0
|
||||
|
||||
#The following are special definitions that affect the core functionality.
|
||||
isCoreDefinition "${3:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_CORE_DEFAULT_ALL_HOSTS}" ] \
|
||||
&& filterInvalidAuthorizedKeysFilesOfRoot "${_CORE_DEFAULT_ALL_HOSTS}" \
|
||||
&& return 0
|
||||
|
||||
#Try this host first because it should be priorized.
|
||||
! isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
! isCoreDefinition "${3:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_FILE_DEFINED_THIS_HOST}" ] \
|
||||
&& echo "${_FILE_DEFINED_THIS_HOST}" \
|
||||
&& return 0
|
||||
|
||||
! isCoreDefinition "${2:?"Missing CURRENT_FULLFILE"}" \
|
||||
! isCoreDefinition "${3:?"Missing CURRENT_FULLFILE"}" \
|
||||
&& [ -s "${_FILE_DEFINED_ALL_HOSTS}" ] \
|
||||
&& echo "${_FILE_DEFINED_ALL_HOSTS}" \
|
||||
&& return 0
|
||||
@@ -71,11 +101,6 @@ function createSymlinkToDefinition() {
|
||||
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
||||
&& echo "The content of the current file already matches the definition, but it will be replaced by a symlink..."
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& [ "$(sha256sum "${_DEFINED_FULLFILE}" | cut -d' ' -f1)" == "$(sha256sum "${_CURRENT_FULLFILE}" | cut -d' ' -f1)" ] \
|
||||
&& echo "The content of the current file already matches the definition, but it will be replaced by a symlink..."
|
||||
|
||||
|
||||
[ -f "${_CURRENT_FULLFILE}" ] \
|
||||
&& mv "${_CURRENT_FULLFILE:?"Missing CURRENT_FULLFILE"}" "${_SAVED_FULLFILE:?"Missing SAVED_FULLFILE"}" \
|
||||
&& echo "Current file has been backed up to: '${_SAVED_FULLFILE}'"
|
||||
@@ -92,17 +117,17 @@ function createSymlinkToDefinition() {
|
||||
}
|
||||
|
||||
function ensureUsageOfDefinitions() {
|
||||
local _ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")"
|
||||
_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
local _CIS_ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
_DEFINITIONS="$(realpath -s "${1:?"Missing first parameter DEFINITIONS: 'ROOT/definitions/DOMAIN'"}")/"
|
||||
_CIS_ROOT="${_DEFINITIONS%%/definitions/*}/" #Removes longest matching pattern '/definitions/*' from the end
|
||||
_DOMAIN="${_DEFINITIONS##*/definitions/}" #Removes longest matching pattern '*/definitions/' from the begin
|
||||
_DOMAIN="${_DOMAIN%/}" #Removes shortest matching pattern '/' from the end
|
||||
#Build from components for safety
|
||||
_DEFINITIONS="$(printIfEqual "${_DEFINITIONS}" "${_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}")"
|
||||
_DEFINITIONS="$(printIfEqual "${_DEFINITIONS}" "${_CIS_ROOT:?"Missing ROOT"}definitions/${_DOMAIN:?"Missing DOMAIN"}/")"
|
||||
|
||||
|
||||
_CURRENT_FOLDER="$(dirname "${2:?"Missing second parameter CURRENT_FULLFILE"}")"
|
||||
_CURRENT_FOLDER="${_CURRENT_FOLDER%/}/" #Removes shortest matching pattern '/' from the end
|
||||
_CURRENT_FULLFILE="${2:?"Missing second parameter CURRENT_FULLFILE"}"
|
||||
_CURRENT_FOLDER="${_CURRENT_FULLFILE%/*}/" #Removes shortest matching pattern '/*' from the end
|
||||
! [ -d "${_CURRENT_FOLDER}" ] \
|
||||
&& echo "FAIL: The folder cannot be read: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_CURRENT_FOLDER}'" \
|
||||
@@ -118,10 +143,16 @@ function ensureUsageOfDefinitions() {
|
||||
_CURRENT_FULLFILE="${_CURRENT_FOLDER:?"Missing CURRENT_FOLDER"}${_CURRENT_FILE:?"Missing CURRENT_FILE"}"
|
||||
|
||||
|
||||
_DEFINED_FULLFILE="$(printSelectedDefinition "${_DEFINITIONS}" "${_CURRENT_FULLFILE}")"
|
||||
_DEFINED_FULLFILE="$(printSelectedDefinition "${_CIS_ROOT}" "${_DOMAIN}" "${_CURRENT_FULLFILE}")"
|
||||
_NOW="$(date +%Y%m%d_%H%M)"
|
||||
_SAVED_FULLFILE="${_CURRENT_FULLFILE}-backup@${_NOW:?"Missing NOW"}"
|
||||
readonly _ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
_SAVED_FULLFILE="${_CURRENT_FULLFILE}.backup@${_NOW:?"Missing NOW"}"
|
||||
readonly _CIS_ROOT _CURRENT_FILE _CURRENT_FOLDER _CURRENT_FULLFILE _DEFINITIONS _DOMAIN _DEFINED_FULLFILE _NOW _SAVED_FULLFILE
|
||||
|
||||
[ -z "${_DEFINED_FULLFILE}" ] \
|
||||
&& echo \
|
||||
&& echo "URGENT WARNING: If an 'authorized_keys' file of root is replaced by an invalid version," \
|
||||
&& echo " you may lose access to this host!" \
|
||||
&& echo
|
||||
|
||||
! [ -f "${_DEFINED_FULLFILE}" ] \
|
||||
&& echo "FAIL: No definition available for this file: ("$(readlink -f ${0})")" \
|
||||
@@ -138,11 +169,11 @@ function ensureUsageOfDefinitions() {
|
||||
&& echo " - '${_DEFINED_FULLFILE}'" \
|
||||
&& return 0
|
||||
|
||||
echo "${_ROOT:?"Missing ROOT"}" | grep "home" &> /dev/null \
|
||||
echo "${_CIS_ROOT:?"Missing CIS_ROOT"}" | grep -F 'home' &> /dev/null \
|
||||
&& echo "SUCCESS: Although this definition will be skipped: ("$(readlink -f ${0})")" \
|
||||
&& echo " - '${_DEFINED_FULLFILE}'" \
|
||||
&& echo " that is because the current environment is:" \
|
||||
&& echo " - ${_ROOT}" \
|
||||
&& echo " - ${_CIS_ROOT}" \
|
||||
&& echo " following file is in use:" \
|
||||
&& echo " - $(readlink -f "${_CURRENT_FULLFILE}")" \
|
||||
&& return 0
|
||||
@@ -165,8 +196,8 @@ function ensureUsageOfDefinitions() {
|
||||
&& echo "- '${_DEFINED_FULLFILE}'" \
|
||||
&& return 0
|
||||
|
||||
echo "FAIL: The definition could not be ensured: ("$(readlink -f ${0})")"
|
||||
echo " - due to an error or insufficient rights."
|
||||
echo "FAIL: The definition could not be ensured: ("$(readlink -f ${0})")" >&2
|
||||
echo " - due to an error or insufficient rights." >&2
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -174,4 +205,6 @@ function ensureUsageOfDefinitions() {
|
||||
ensureUsageOfDefinitions \
|
||||
"$(echo ${1} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
"$(echo ${2} | sed -E 's|[^a-zA-Z0-9/:@._-]*||g')" \
|
||||
&& exit 0 || exit 1
|
||||
&& exit 0
|
||||
|
||||
exit 1
|
||||
|
||||
13
core/printCisRoot.sh
Executable file
13
core/printCisRoot.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
|
||||
_SCRIPT="$(readlink -f "${0}" 2> /dev/null)"
|
||||
_CIS_ROOT="${_SCRIPT%%/core/*}/" #Removes longest matching pattern '/core/*' from the end
|
||||
|
||||
[ -d "${_CIS_ROOT}" ] \
|
||||
&& [ -d "${_CIS_ROOT}definitions/" ] \
|
||||
&& [ -d "${_CIS_ROOT}states/" ] \
|
||||
&& echo "${_CIS_ROOT}" \
|
||||
&& exit 0
|
||||
|
||||
echo "FAIL: Unable to detect CIS_ROOT" >&2
|
||||
exit 1
|
||||
30
core/printOwnDomain.sh
Executable file
30
core/printOwnDomain.sh
Executable file
@@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
|
||||
#WARNING: Used for core functionality in setup.sh
|
||||
# DO NOT rename the script and test changes well!
|
||||
|
||||
|
||||
|
||||
# Folders always ends with an tailing '/'
|
||||
_SCRIPT="$(readlink -f "${0}" 2> /dev/null)"
|
||||
_CIS_ROOT="${_SCRIPT%%/core/*}/" #Removes longest matching pattern '/core/*' from the end
|
||||
_OVERRIDE_DOMAIN_FILE="${_CIS_ROOT:?"Missing CIS_ROOT"}overrideOwnDomain"
|
||||
|
||||
# There has to be one dot at least.
|
||||
_BOOT_DOMAIN="$(hostname -b | grep -F '.' | cut -d. -f2-)"
|
||||
|
||||
# Take OVERRIDING_DOMAIN_FILE without empty lines and comments, then take the first line without leading spaces
|
||||
_OVERRIDE_DOMAIN="$(grep -vE '^[[:space:]]*$|^[[:space:]]*#' "${_OVERRIDE_DOMAIN_FILE}" 2> /dev/null | head -n 1 | xargs)"
|
||||
|
||||
! [ -z "${_OVERRIDE_DOMAIN}" ] \
|
||||
&& [ "${_OVERRIDE_DOMAIN}" != "${_BOOT_DOMAIN}" ] \
|
||||
&& echo "WARNING: Domain has been overridden by: ${_OVERRIDE_DOMAIN_FILE}" >&2 \
|
||||
&& echo "${_OVERRIDE_DOMAIN}" \
|
||||
&& exit 0
|
||||
|
||||
! [ -z "${_BOOT_DOMAIN}" ] \
|
||||
&& echo "${_BOOT_DOMAIN}" \
|
||||
&& exit 0
|
||||
|
||||
echo "It was impossible to find out the domain of this host, please prepare this host first." >&2
|
||||
exit 1
|
||||
Reference in New Issue
Block a user