-
Notifications
You must be signed in to change notification settings - Fork 0
/
tauth-manager.sh
213 lines (202 loc) · 4.69 KB
/
tauth-manager.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
VERSION="1.4"
#Colors for display
NOCOLOR='\033[0m'
TAUTHROOT="/usr/local/tauth"
red() { CRED='\033[0;31m'; echo -e ${CRED}$1${NOCOLOR}; }
blue() { CBLUE='\033[0;34m'; echo -e ${CBLUE}$1${NOCOLOR}; }
green() { CGREEN='\033[0;32m'; echo -e ${CGREEN}$1${NOCOLOR}; }
check_root() {
#check root
if [ $(whoami) != "root" ]; then
red "restart as root!"
red "Exiting...."
exit
fi
}
PhoneInfo="$TAUTHROOT/Phoneinfo"
search_carrier(){
if [[ ! -f $PhoneInfo ]]; then
red "No carrier database found!"
exit
fi
if [ -z $1 ]; then
read -p "Enter phone carrier: " pc
searchA=${pc,,}
else
searchA=$1
searchA=${searchA,,}
fi
resultA=( $(cat $PhoneInfo | cut -d':' -f1 | grep "$searchA") )
count=$(echo ${#resultA[@]})
blue "Searching for [$searchA]..."
if [ $count -lt 1 ]; then
red "No results found for [$searchA]"
elif [ $count -eq 1 ]; then
green "Match found"
PhoneCarrier=$(cat $PhoneInfo | grep "$resultA:" | cut -d':' -f2)
green "Carrier set to [$resultA]"
else
red "More then one carrier found!"
for i in $(seq 0 $count); do
if [ $i -eq $count ]; then
red "$count. EXIT"
else
red "$i. [${resultA[i]}]"
fi
done
re="^[0-9]+$"
while true; do
read -p "Please choose a number: " choice
if [ $choice -eq $count ]; then
exit
elif [[ $choice =~ $re ]]; then
PhoneCarrier=$(cat $PhoneInfo | grep "${resultA[$choice]}:" | cut -d':' -f2 )
green "Carrier set to [${resultA[$choice]}]"
break
fi
done
fi
}
add_user() {
blue "Adding $1 to tauth"
USER_CONF="/home/$1/.tauth/user_config"
USER_DIR="/home/$1/.tauth"
#check if user has home directory
if [[ ! -d /home/$1 ]]; then
red "User does not exist or has no home directory!"
exit
fi
#check is .tauth folder exists and makes one if not
if [[ ! -d $USER_DIR ]]; then
mkdir $USER_DIR
fi
#if config file exists then delete it
if [[ -f $USER_CONF ]]; then
#print out previous user data
blue "User has previous tauth data:"
prev_email=$(echo $(cat $USER_CONF | grep "Email " | awk '{print $2}'))
prev_phone=$(echo $(cat $USER_CONF | grep "Phone " | awk '{print $2}'))
blue "[ Email: $prev_email ] [ Phone: $prev_phone ]"
chattr -i $USER_CONF
fi
#gets user input
read -p "Enter user's SMS number: " num
if [[ -f $PhoneInfo ]]; then
search_carrier
fi
read -p "Enter user's Email: " em
echo "Phone "$num > $USER_CONF
echo "Email "$em >> $USER_CONF
echo "Carrier "$PhoneCarrier >> $USER_CONF
#echo $1 >> $USERS
chattr +i $USER_CONF
green $1" added to tauth!"
}
view_user() {
USER_CONF="/home/$1/.tauth/user_config"
USER_DIR="/home/$1/.tauth"
#check if user has home directory
if [[ ! -d /home/$1 ]]; then
red "User does not exist or has no home directory!"
exit
fi
#if config file exists then view it
if [[ -f $USER_CONF ]]; then
#print out previous user data
blue "$1's tauth data:"
prev_email=$(echo $(cat $USER_CONF | grep "Email " | awk '{print $2}'))
prev_phone=$(echo $(cat $USER_CONF | grep "Phone " | awk '{print $2}'))
blue "[ Email: $prev_email ] [ Phone: $prev_phone ]"
else
red "User is not registered with TAUTH!"
fi
}
remove_user() {
USER_CONF="/home/$1/.tauth/user_config"
USER_DIR="/home/$1/.tauth"
#check if user has home directory
#if so removes .tauth and .tauth/user_config
if [[ ! -d /home/$1 ]]; then
red "$1 does not exist or has no home directory!"
fi
if [[ -f $USER_CONF ]]; then
chattr -i $USER_CONF
rm $USER_CONF
fi
if [[ -d $USER_DIR ]]; then
rmdir $USER_DIR
green "$1 removed from tauth"
fi
}
uninstall(){
users_a=( $(ls -1 /home) )
for i in ${users_a[@]}; do
if [[ -d "/home/$i/.tauth" ]]; then
remove_user $i
fi
done
if [[ -d "/etc/tauth" ]]; then
rm -rf "/etc/tauth"
green "Removed /etc/tauth"
fi
if [[ -d "/usr/local/tauth" ]]; then
rm -rf "/usr/local/tauth"
green "Removed /usr/local/tauth"
fi
cat /etc/ssh/sshd_config.bac > /etc/ssh/sshd_config
if [[ -f "/usr/local/sbin/tauth" ]]; then
rm "/usr/local/sbin/tauth"
fi
}
nm=$(basename $0)
case $1 in
add)
check_root
add_user $2
;;
view)
check_root
view_user $2
;;
remove)
check_root
remove_user $2
;;
uninstall)
while true; do
read -p "Do you wish to uninstall? [Y/n] " yn
case $yn in
[Yy]* ) break;;
* ) exit;;
esac
done
check_root
uninstall
exit 0
;;
version)
echo "TAUTH v${VERSION}"
exit 0
;;
*)
cat <<__EOF__
Usage: $nm <command> <arguments>
VERSION $VERSION
Available commands:
add
Enables a user with tauth. Prompts for users email and phone.
$nm add [USER]
view
View the settings of a tauth user.
$nm view [USER]
remove
Removes tauth from a users account
$nm remove [USER]
uninstall
Uninstalls tauth from the computer and all users
version
prints the tauth version
__EOF__
;;
esac