From 7b9b353435bfc676e037652041effa21a3bec9df Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 10:57:53 -0700 Subject: [PATCH 1/8] convert if to case --- modules/sshkey.sh | 236 +++++++++++++++++++++++++--------------------- 1 file changed, 126 insertions(+), 110 deletions(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 3800c8b6f..0f1d76820 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -1,125 +1,141 @@ function sshkey () { local keys githubusername auth_files teams team_id members checkroot - if [ "$1" == "add" ]; then - checkargn $# 4 - shift - temp_file=$(mktemp) - echo "$@" >> $temp_file - if ssh-keygen -l -f $temp_file 2>/dev/null <<< y >/dev/null; then - rm $temp_file - echo "$@" >> /root/.ssh/authorized_keys - chmod 600 /root/.ssh/authorized_keys - if [ "$(detectrpi)" != "nonrpi" ]; then - mkdir -p /root/.ssh /home/pi/.ssh - chmod 700 /root/.ssh /home/pi/.ssh - echo "$@" >> /home/pi/.ssh/authorized_keys - chmod 600 /home/pi/.ssh/authorized_keys - chown -R pi:pi /home/pi/.ssh - echo "====== Added to 'pi' and 'root' user's authorized_keys ======" + argument="$1" + arg2="$2" + + case $argument in + + "add") + checkargn $# 4 + shift + temp_file=$(mktemp) + echo "$@" >> $temp_file + if ssh-keygen -l -f $temp_file 2>/dev/null <<< y >/dev/null; then + rm $temp_file + echo "$@" >> /root/.ssh/authorized_keys + chmod 600 /root/.ssh/authorized_keys + if [ "$(detectrpi)" != "nonrpi" ]; then + mkdir -p /root/.ssh /home/pi/.ssh + chmod 700 /root/.ssh /home/pi/.ssh + echo "$@" >> /home/pi/.ssh/authorized_keys + chmod 600 /home/pi/.ssh/authorized_keys + chown -R pi:pi /home/pi/.ssh + echo "====== Added to 'pi' and 'root' user's authorized_keys ======" + else + echo "====== Added to 'root' user's authorized_keys ======" + fi + echo "$@" else - echo "====== Added to 'root' user's authorized_keys ======" + rm $temp_file + log_and_exit1 "ERROR: invalid public key" fi - echo "$@" - else - rm $temp_file - log_and_exit1 "ERROR: invalid public key" - fi - elif [ "$1" == "list" ]; then - checkargn $# 1 - echo "==== root keys ====" - cat /root/.ssh/authorized_keys - if [ "$(detectrpi)" != "nonrpi" ]; then - echo "==== pi keys ====" - cat /home/pi/.ssh/authorized_keys - fi - elif [ "$1" == "delete" ]; then - checkargn $# 2 - if [ -z "$2" ]; then - echo "Error: missing argument" - log_and_exit1 "Usage: $BASENAME sshkey delete \"\"" - fi - keys="$(echo "$@" | sed 's/delete //')" - if grep -Fxq "$keys" /root/.ssh/authorized_keys; then - sed -i "\:$keys:d" /root/.ssh/authorized_keys - echo "Key deleted from root keys." - else - echo "Key not found in root keys." - fi - if [ "$(detectrpi)" != "nonrpi" ]; then - if grep -Fxq "$keys" /home/pi/.ssh/authorized_keys; then - sed -i "\:$keys:d" /home/pi/.ssh/authorized_keys - echo "Key deleted from pi keys." - else - echo "Key not found in pi keys." + ;; + "list") + checkargn $# 1 + echo "==== root keys ====" + cat /root/.ssh/authorized_keys + if [ "$(detectrpi)" != "nonrpi" ]; then + echo "==== pi keys ====" + cat /home/pi/.ssh/authorized_keys fi - fi - elif [ "$1" == "deleteall" ]; then - checkargn $# 1 - rm /root/.ssh/authorized_keys - if [ "$(detectrpi)" != "nonrpi" ]; then - rm /home/pi/.ssh/authorized_keys - fi - echo "all sshkeys are deleted." - elif [ "$1" == "github" ]; then - if [ -z "$2" ]; then - echo "Error: missing arguments" - log_and_exit1 "Usage: $BASENAME sshkey github " - fi - if [ "$2" == "adduser" ]; then - if [ -z "$3" ]; then + "delete") + checkargn $# 2 + if [ -z "$2" ]; then echo "Error: missing argument" - log_and_exit1 "Usage: $BASENAME sshkey adduser " + log_and_exit1 "Usage: $BASENAME sshkey delete \"\"" fi - shift; shift - for user in "$@"; do - echo " Attempting to add the following user: $user" - keys=$(curl -s "https://github.com/$user.keys") - if [ ! -z "$keys" ]; then - keys=$(sed 's#$# '$user'#' <<< $keys) - sshkey add "$keys" - fi - echo " Successfully added user: $user" - done - elif [ "$2" == "deleteuser" ]; then - if [ -z "$3" ]; then - echo "Error: missing argument" - log_and_exit1 "Usage: $BASENAME sshkey deleteuser " + keys="$(echo "$@" | sed 's/delete //')" + if grep -Fxq "$keys" /root/.ssh/authorized_keys; then + sed -i "\:$keys:d" /root/.ssh/authorized_keys + echo "Key deleted from root keys." + else + echo "Key not found in root keys." fi - githubusername="$3" - auth_files="/root/.ssh/authorized_keys /home/pi/.ssh/authorized_keys" - for file in $auth_files; do - if [ -f "$file" ]; then - if grep -q " $githubusername$" $file; then - sed -i "/ $githubusername$/d" $file - echo "$githubusername's key(s) deleted from $file" - else - echo "$githubusername does not exist" - fi + if [ "$(detectrpi)" != "nonrpi" ]; then + if grep -Fxq "$keys" /home/pi/.ssh/authorized_keys; then + sed -i "\:$keys:d" /home/pi/.ssh/authorized_keys + echo "Key deleted from pi keys." else - echo "$file does not exist." + echo "Key not found in pi keys." fi - done - elif [ "$2" == "addteam" ]; then - checkargn $# 5 - if [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]; then - echo "Error: missing arguments" - log_and_exit1 "Usage: $BASENAME sshkey github addteam " fi - teams=$(curl -s -X GET "https://api.github.com/orgs/$3/teams" -H "Authorization: token $5") - team_id=$(echo "$teams" | jq ".[] | select(.name==\"$4\").id") - members=$(curl -s -X GET "https://api.github.com/teams/$team_id/members" -H "Authorization: token $5" | jq ".[].login" -r) - while read -r member; do - sshkey github adduser "$member" - done <<< "$members" - else - echo "Error: unsupported command" - log_and_exit1 "Usage: $BASENAME sshkey github " - fi - else - echo "Error: unsupported command" - log_and_exit1 "Usage: $BASENAME sshkey " - fi + ;; + "deleteall") + checkargn $# 1 + rm /root/.ssh/authorized_keys + if [ "$(detectrpi)" != "nonrpi" ]; then + rm /home/pi/.ssh/authorized_keys + fi + echo "all sshkeys are deleted." + ;; + "github") + + case $arg2 in + + "") + echo "Error: missing arguments" + log_and_exit1 "Usage: $BASENAME sshkey github " + ;; + "adduser") + if [ -z "$3" ]; then + echo "Error: missing argument" + log_and_exit1 "Usage: $BASENAME sshkey adduser " + fi + shift; shift + for user in "$@"; do + echo " Attempting to add the following user: $user" + keys=$(curl -s "https://github.com/$user.keys") + if [ ! -z "$keys" ]; then + keys=$(sed 's#$# '$user'#' <<< $keys) + sshkey add "$keys" + fi + echo " Successfully added user: $user" + done + ;; + "deleteuser") + if [ -z "$3" ]; then + echo "Error: missing argument" + log_and_exit1 "Usage: $BASENAME sshkey deleteuser " + fi + githubusername="$3" + auth_files="/root/.ssh/authorized_keys /home/pi/.ssh/authorized_keys" + for file in $auth_files; do + if [ -f "$file" ]; then + if grep -q " $githubusername$" $file; then + sed -i "/ $githubusername$/d" $file + echo "$githubusername's key(s) deleted from $file" + else + echo "$githubusername does not exist" + fi + else + echo "$file does not exist." + fi + done + ;; + "addteam") + checkargn $# 5 + if [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]; then + echo "Error: missing arguments" + log_and_exit1 "Usage: $BASENAME sshkey github addteam " + fi + teams=$(curl -s -X GET "https://api.github.com/orgs/$3/teams" -H "Authorization: token $5") + team_id=$(echo "$teams" | jq ".[] | select(.name==\"$4\").id") + members=$(curl -s -X GET "https://api.github.com/teams/$team_id/members" -H "Authorization: token $5" | jq ".[].login" -r) + while read -r member; do + sshkey github adduser "$member" + done <<< "$members" + ;; + *) + echo "Error: unsupported command" + log_and_exit1 "Usage: $BASENAME sshkey github " + ;; + esac + *) + echo "Error: unsupported command" + log_and_exit1 "Usage: $BASENAME sshkey " + ;; + esac } function sshkey_help () { From 5cd8d35e5bda7b0673e0b14507ac6e44bd2861f6 Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:11:07 -0700 Subject: [PATCH 2/8] add missing ;; --- modules/sshkey.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 0f1d76820..862ff7764 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -39,6 +39,7 @@ function sshkey () { echo "==== pi keys ====" cat /home/pi/.ssh/authorized_keys fi + ;; "delete") checkargn $# 2 if [ -z "$2" ]; then From 029a13e52767eb957a67b1840951eca6eb53251c Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:20:54 -0700 Subject: [PATCH 3/8] fix position for ;; --- modules/sshkey.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 862ff7764..3473c272e 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -130,7 +130,7 @@ function sshkey () { *) echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey github " - ;; + ;; esac *) echo "Error: unsupported command" From d1853d44f7b133bbd3c5761dcc3013bb358175a5 Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:38:07 -0700 Subject: [PATCH 4/8] fix position for ;; --- modules/sshkey.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 3473c272e..fbc4ba1c6 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -131,12 +131,12 @@ function sshkey () { echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey github " ;; - esac + esac *) echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey " ;; - esac + esac } function sshkey_help () { From 0673274ec1e0952ef5d741d395f23a476d2b752c Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:41:09 -0700 Subject: [PATCH 5/8] fix position for ;; --- modules/sshkey.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index fbc4ba1c6..862ff7764 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -130,13 +130,13 @@ function sshkey () { *) echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey github " - ;; - esac + ;; + esac *) echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey " ;; - esac + esac } function sshkey_help () { From 2dbb4407e9f59eda0292c02944d44f6ae3232c2e Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:44:28 -0700 Subject: [PATCH 6/8] fix position for ;; --- modules/sshkey.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 862ff7764..43af55e97 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -132,11 +132,11 @@ function sshkey () { log_and_exit1 "Usage: $BASENAME sshkey github " ;; esac - *) - echo "Error: unsupported command" - log_and_exit1 "Usage: $BASENAME sshkey " - ;; - esac + *) + echo "Error: unsupported command" + log_and_exit1 "Usage: $BASENAME sshkey " + ;; + esac } function sshkey_help () { From ee8ed1e63668750c0df148378511210e2075b7ef Mon Sep 17 00:00:00 2001 From: JLKwong Date: Mon, 7 Jun 2021 12:49:49 -0700 Subject: [PATCH 7/8] add missing ;; --- modules/sshkey.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 43af55e97..812d540e0 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -132,6 +132,7 @@ function sshkey () { log_and_exit1 "Usage: $BASENAME sshkey github " ;; esac + ;; *) echo "Error: unsupported command" log_and_exit1 "Usage: $BASENAME sshkey " From 52d82c68e12587f71fc95a2b8362bd4d5fb3818b Mon Sep 17 00:00:00 2001 From: dogi Date: Wed, 9 Jun 2021 19:53:09 -0400 Subject: [PATCH 8/8] Update sshkey.sh --- modules/sshkey.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/sshkey.sh b/modules/sshkey.sh index 812d540e0..bc810fe49 100644 --- a/modules/sshkey.sh +++ b/modules/sshkey.sh @@ -5,7 +5,6 @@ function sshkey () { arg2="$2" case $argument in - "add") checkargn $# 4 shift