-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.sh
246 lines (195 loc) · 7.04 KB
/
common.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#
## =============[ Constants ]============== ##
#APP_PATH=$(readlink -f $0)
#APP_BASE=$(dirname "${APP_PATH}")
#APP_NAME=$(basename "${APP_PATH}")
LINES=$(tput lines)
COLS=$(tput cols)
## =============[ Default Settings ]============== ##
APP_SETTINGS="${HOME}/.config/penbuilder/settings.conf"
APP_SETTINGS_DIR=$(dirname ${APP_SETTINGS})
### SYSTEM SHAPING VARIABLES
# Can specify the user account for installation scripts, but not needed.
INSTALL_USER=${USER}
LSB_RELEASE=$(lsb_release -cs)
## ==========[ TEXT COLORS ]============= ##
GREEN="\033[01;32m" # Success
YELLOW="\033[01;33m" # Warnings/Information
RED="\033[01;31m" # Issues/Errors
BLUE="\033[01;34m" # Heading
ORANGE="\033[38;5;208m" # Debugging
PURPLE="\033[01;35m" # Other
GREY="\e[90m" # Subdued Text
BOLD="\033[01;01m" # Highlight
RESET="\033[00m" # Normal
## ========================================================================== ##
## ===============[ START :: INIT/PROJECT SETUP FUNCTIONS ]=============== ##
function init_settings() {
#
#
#
#
#
if [[ ! -f "${APP_SETTINGS}" ]]; then
mkdir -p $(dirname ${APP_SETTINGS})
echo -e "${GREEN}[*]${RESET} Creating personal settings file"
cat <<EOF > "${APP_SETTINGS}"
### PERSONAL SYSTEM BUILD SETTINGS ###
#
#
EOF
else
echo -e "${GREEN}[*]${RESET} Reading from settings file, please wait..."
source "${APP_SETTINGS}"
[[ ${DEBUG} -eq 1 ]] && echo -e "${ORANGE}[DEBUG] App Settings Path: ${APP_SETTINGS}${RESET}"
fi
}
# ==================[ END :: INIT/PROJECT SETUP FUNCTIONS ]================== #
# ==================[ START :: ROOT/SUDO CHECK FUNCTIONS ]================== #
# Env variables
# The standard env var is $USER. This is regular user in normal state/root in sudo state
# CURRENT_USER=${USER}
# This would only be run if within sudo state
# ACTUAL_USER=$(env | grep SUDO_USER | cut -d= -f 2)
#
#
function install_sudo() {
[[ ${INSTALL_USER} ]] || INSTALL_USER=${USER}
[[ "$DEBUG" = true ]] && echo -e "${ORANGE}[DEBUG] Running 'install_sudo' function${RESET}"
echo -e "${GREEN}[*]${RESET} Now installing 'sudo' package via apt-get..."
echo -e "\n${GREEN}[INPUT]${RESET} Enter root password below"
su -c "apt-get -y install sudo" root
[[ $? -eq 1 ]] && echo -e "${RED}[ERROR] Unable to install sudo via apt-get${RESET}" && exit 1
echo -e "${GREEN}[INPUT]${RESET} Adding user ${BLUE}${INSTALL_USER}${RESET} to sudo group. Enter root password below"
su -c "usermod -a -G sudo ${INSTALL_USER}" root
[[ $? -eq 1 ]] && echo -e "${RED}[ERROR] Unable to add original user to sudoers${RESET}" && exit 1
echo -e "\n\n${YELLOW}[WARN]${RESET} Rebooting system to take effect!"
echo -e "${YELLOW}[INFO]${RESET} Restart this script after login!\n\n"
sleep 5s
su -c "init 6" root
exit 0
}
function check_root() {
if [[ $EUID -ne 0 ]]; then
# If not root, check if sudo package is installed
if [[ $(which sudo) ]]; then
# This accounts for both root and sudo. If normal user, it'll use sudo.
# If you run script as root, $SUDO is blank and script will soldier on.
export SUDO="sudo"
# Test to ensure this user is able to use sudo
sudo -l >/dev/null
if [[ $? -eq 1 ]]; then
# sudo pkg is installed but current user is not in sudoers group to use it
echo -e "${RED}[ERROR]${RESET} You are not able to use sudo. Running install to fix."
read -r -t 5
install_sudo
fi
else
echo -e "${YELLOW}[WARN]${RESET} The 'sudo' package is not installed."
echo -e "${YELLOW}[+]${RESET} Press any key to install it (*You'll be prompted to enter sudo password). Otherwise, manually cancel script now..."
read -r -t 5
install_sudo
fi
fi
}
function check_root_old() {
ACTUAL_USER=$(env | grep SUDO_USER | cut -d= -f 2)
#
# Check if current user is root. If not and sudo is installed, $SUDO can be used.
#
#
if [[ $EUID -ne 0 ]];then
if [[ $(dpkg-query -s sudo) ]];then
export SUDO="sudo"
# $SUDO - run commands with this prefix now to account for either scenario.
else
echo -e "[ERROR] Please install sudo or run this as root."
exit 1
fi
fi
}
function check_root_kali() {
if [[ $EUID -ne 0 ]]; then
if [[ $(dpkg-query -s sudo) ]]; then
export SUDO="sudo"
# $SUDO - run commands with this prefix now to account for either scenario.
else
echo -e "[ERROR] Please install sudo or run this as root."
exit 1
fi
fi
}
# ===================[ END :: ROOT/SUDO CHECK FUNCTIONS ]=================== #
# --------- All functions below this line need to be verified ---------------
function test_networking() {
$SUDO apt-get -qq update
if [[ "$?" -ne 0 ]]; then
echo -e "${RED} [ERROR]${RESET} Network issues preventing apt-get. Check and try again."
exit 1
fi
}
# =====================[ START :: STYLING/FORMATTING ]===================== #
function print_banner() {
#
# argv1 = title text of banner
# argv2 = program version number
#
length=${#1}
echo -e "\n${BLUE}===================[ ${RESET}${BOLD}$1 ${RESET}${BLUE}]===================${RESET}\n"
echo -e "${BLUE}================================<${RESET} version: ${__version__} ${BLUE}>================================${RESET}\n"
}
function center_text() {
width=$(tput cols)
height=$(tput lines)
str="$1"
length=${#str}
clear
tput cup $((height / 2)) $(((width / 2) - (length / 2)))
echo -e "$str"
}
# ======================[ END :: STYLING/FORMATTING ]====================== #
# ============================[ START :: XFCE ]============================ #
function xfce4_default_layout() {
# Copy default xfce4 desktop layout folder shell over
cp -R "${APP_BASE}"/config/includes/. "${BUILD_DIR}/config/includes.chroot/"
}
# =============================[ END :: XFCE ]============================= #
# =============================[ START :: GIT ]============================= #
function install_git() {
#TODO: Function to clone git repo
CLONE_PATH='/opt/git'
git clone -q ${1} || echo -e '[ERROR] Problem cloning ${1}'
}
# ==============================[ END :: GIT ]============================== #
function version () {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}
function version_check () {
# Compare to versions and return 0 if newer, 1 if older
#
# In this case, I'm checking the installed version of OpenSSH
#
# Usage: version_check 7.5 6.5 (or) version_check $openssh_version 6.5
#
if [ $(version $1) -ge $(version $2) ]; then
echo "$1 is newer than $2" >/dev/null
return 0
elif [ $(version $1) -lt $(version $2) ]; then
echo "$1 is older than $2" >/dev/null
return 1
fi
}
function md5_compare() {
# Compare MD5 Hash of 2 files
#
# Usage: md5_compare <file1> <file2>
#
echo -e "\t-- ${RED}OLD KEYS${RESET} --"
#openssl md5 /etc/ssh/insecure_original_kali_keys/ssh_host_*
$SUDO openssl md5 ${1}
echo -e "\n\t-- ${GREEN}NEW KEYS${RESET} --"
#openssl md5 /etc/ssh/ssh_host_*
$SUDO openssl md5 ${2}
echo -e "\n\n"
sleep 10
}