Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapper for service manipulation #323

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions tools/json/config.software.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
],
"status": "Stable",
"author": "@igorpecovnik",
"condition": "systemctl is-active --quiet service display-manager"
"condition": "srv_active display-manager"
}
]
},
Expand Down Expand Up @@ -348,7 +348,7 @@
"command": [
"get_user_continue \"This operation will purge avahi-daemon \nDo you wish to continue?\" process_input",
"check_if_installed avahi-daemon",
"systemctl stop avahi-daemon avahi-daemon.socket",
"srv_stop avahi-daemon avahi-daemon.socket",
"debconf-apt-progress -- apt-get -y purge avahi-daemon"
],
"status": "Stable",
Expand Down Expand Up @@ -1161,22 +1161,22 @@
"id": "MAN003",
"description": "Start Cockpit Service",
"command": [
"sudo systemctl enable --now cockpit.socket | show_infobox "
"srv_enable --now cockpit.socket | show_infobox "
],
"status": "Stable",
"author": "@schwar3kat",
"condition": "check_if_installed cockpit && ! systemctl is-enabled cockpit.socket > /dev/null 2>&1"
"condition": "check_if_installed cockpit && ! srv_enabled cockpit.socket > /dev/null 2>&1"
},
{
"id": "MAN004",
"description": "Stop Cockpit Service",
"command": [
"systemctl stop cockpit cockpit.socket",
"systemctl disable cockpit.socket | show_infobox "
"srv_stop cockpit cockpit.socket",
"srv_disable cockpit.socket | show_infobox "
],
"status": "Stable",
"author": "@schwar3kat",
"condition": "check_if_installed cockpit && systemctl is-enabled cockpit.socket > /dev/null 2>&1"
"condition": "check_if_installed cockpit && srv_enabled cockpit.socket > /dev/null 2>&1"
},
{
"id": "MAN005",
Expand Down
20 changes: 10 additions & 10 deletions tools/json/config.system.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"description": "Disable root login",
"command": [
"sed -i \"s|^#\\?PermitRootLogin.*|PermitRootLogin no|\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -180,7 +180,7 @@
"description": "Enable root login",
"command": [
"sed -i \"s/^#\\?PermitRootLogin.*/PermitRootLogin yes/\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -191,7 +191,7 @@
"description": "Disable password login",
"command": [
"sed -i \"s/^#\\?PasswordAuthentication.*/PasswordAuthentication no/\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -202,7 +202,7 @@
"description": "Enable password login",
"command": [
"sed -i \"s/^#\\?PasswordAuthentication.*/PasswordAuthentication yes/\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -213,7 +213,7 @@
"description": "Disable Public key authentication login",
"command": [
"sed -i \"s/^#\\?PubkeyAuthentication.*/PubkeyAuthentication no/\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -224,7 +224,7 @@
"description": "Enable Public key authentication login",
"command": [
"sed -i \"s/^#\\?PubkeyAuthentication.*/PubkeyAuthentication yes/\" /etc/ssh/sshd_config",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -238,7 +238,7 @@
"! check_if_installed libpam-google-authenticator && ! check_if_installed qrencode || debconf-apt-progress -- apt-get -y purge libpam-google-authenticator qrencode",
"sed -i \"s/^#\\?ChallengeResponseAuthentication.*/ChallengeResponseAuthentication no/\" /etc/ssh/sshd_config || sed -i \"0,/KbdInteractiveAuthentication/s//ChallengeResponseAuthentication yes/\" /etc/ssh/sshd_config",
"sed -i '/^auth required pam_google_authenticator.so nullok/ d' /etc/pam.d/sshd",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -253,7 +253,7 @@
"sed -i \"s/^#\\?ChallengeResponseAuthentication.*/ChallengeResponseAuthentication yes/\" /etc/ssh/sshd_config",
"sed -i $'/KbdInteractiveAuthentication/{iChallengeResponseAuthentication yes\\n:a;n;ba}' /etc/ssh/sshd_config || sed -n -i '/password updating/{p;:a;N;/@include common-password/!ba;s/.*\\n/auth required pam_google_authenticator.so nullok\\nauth required pam_permit.so\\n/};p' /etc/pam.d/sshd",
"[ ! -f /root/.google_authenticator ] && qr_code generate",
"systemctl restart sshd.service 2>/dev/null | systemctl restart ssh.service 2>/dev/null"
"srv_restart sshd.service 2>/dev/null | srv_restart ssh.service 2>/dev/null"
],
"status": "Stable",
"author": "@igorpecovnik",
Expand Down Expand Up @@ -284,7 +284,7 @@
"description": "Disable last login banner",
"command": [
"sed -i \"s/^#\\?PrintLastLog.*/PrintLastLog no/\" /etc/ssh/sshd_config",
"systemctl restart ssh.service "
"srv_restart ssh.service "
],
"status": "Stable",
"author": "@igorpecovnik",
Expand All @@ -295,7 +295,7 @@
"description": "Enable last login banner",
"command": [
"sed -i \"s/^#\\?PrintLastLog.*/PrintLastLog yes/\" /etc/ssh/sshd_config",
"systemctl restart ssh.service "
"srv_restart ssh.service "
],
"status": "Stable",
"author": "@igorpecovnik",
Expand Down
125 changes: 125 additions & 0 deletions tools/modules/functions/service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# service.sh

# internal function
_srv_inside_jail() { systemd-detect-virt -qc; }

declare -A module_options
module_options+=(
["srv_active,author"]="@dimitry-ishenko"
["srv_active,desc"]="Check if service is active"
["srv_active,example"]="srv_active ssh.service"
["srv_active,feature"]="srv_active"
["srv_active,status"]="Interface"
)

srv_active()
{
# ignore inside container
_srv_inside_jail && return 1 || systemctl is-active --quiet "$@"
}

module_options+=(
["srv_disable,author"]="@dimitry-ishenko"
["srv_disable,desc"]="Disable service"
["srv_disable,example"]="srv_disable ssh.service"
["srv_disable,feature"]="srv_disable"
["srv_disable,status"]="Interface"
)

srv_disable() { systemctl disable "$@"; }

module_options+=(
["srv_enable,author"]="@dimitry-ishenko"
["srv_enable,desc"]="Enable service"
["srv_enable,example"]="srv_enable ssh.service"
["srv_enable,feature"]="srv_enable"
["srv_enable,status"]="Interface"
)

srv_enable() { systemctl enable "$@"; }

module_options+=(
["srv_enabled,author"]="@dimitry-ishenko"
["srv_enabled,desc"]="Check if service is enabled"
["srv_enabled,example"]="srv_enabled ssh.service"
["srv_enabled,feature"]="srv_enabled"
["srv_enabled,status"]="Interface"
)

srv_enabled() { systemctl is-enabled "$@"; }

module_options+=(
["srv_mask,author"]="@dimitry-ishenko"
["srv_mask,desc"]="Mask service"
["srv_mask,example"]="srv_mask ssh.service"
["srv_mask,feature"]="srv_mask"
["srv_mask,status"]="Interface"
)

srv_mask() { systemctl mask "$@"; }

module_options+=(
["srv_reload,author"]="@dimitry-ishenko"
["srv_reload,desc"]="Reload service"
["srv_reload,example"]="srv_reload ssh.service"
["srv_reload,feature"]="srv_reload"
["srv_reload,status"]="Interface"
)

srv_reload()
{
# ignore inside container
_srv_inside_jail || systemctl reload "$@"
}

module_options+=(
["srv_restart,author"]="@dimitry-ishenko"
["srv_restart,desc"]="Restart service"
["srv_restart,example"]="srv_restart ssh.service"
["srv_restart,feature"]="srv_restart"
["srv_restart,status"]="Interface"
)

srv_restart()
{
# ignore inside container
_srv_inside_jail || systemctl restart "$@"
}

module_options+=(
["srv_start,author"]="@dimitry-ishenko"
["srv_start,desc"]="Start service"
["srv_start,example"]="srv_start ssh.service"
["srv_start,feature"]="srv_start"
["srv_start,status"]="Interface"
)

srv_start()
{
# ignore inside container
_srv_inside_jail || systemctl start "$@"
}

module_options+=(
["srv_stop,author"]="@dimitry-ishenko"
["srv_stop,desc"]="Stop service"
["srv_stop,example"]="srv_stop ssh.service"
["srv_stop,feature"]="srv_stop"
["srv_stop,status"]="Interface"
)

srv_stop()
{
# ignore inside container
_srv_inside_jail || systemctl stop "$@"
}

module_options+=(
["srv_unmask,author"]="@dimitry-ishenko"
["srv_unmask,desc"]="Unmask service"
["srv_unmask,example"]="srv_unmask ssh.service"
["srv_unmask,feature"]="srv_unmask"
["srv_unmask,status"]="Interface"
)

srv_unmask() { systemctl unmask "$@"; }
Loading