-
Notifications
You must be signed in to change notification settings - Fork 1
/
commons
153 lines (135 loc) · 3.37 KB
/
commons
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
#!/usr/bin/env bash
################################################################################
#
# commons
# This is part of the con-test framework
#
# Copyright (C) 2019 Stefan Venz
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
################################################################################
# version numbers
MAJOR=0
MINOR=2
RELEASE=0
# output options
ERROR="$(tput bold; tput setaf 1)[Error]:$(tput sgr0)"
WARNING="$(tput bold ; tput setaf 3)[Warning]:$(tput sgr0)"
INFO="$(tput bold ; tput setaf 2) [INFO]:$(tput sgr0)"
SUCCESS=0
FAILURE=1
CLIENT=0
SERVER=1
PASSWD=1
CERT=0
ON=1
OFF=0
# Test if getopt works as expected
#
test_get_opt()
{
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
return $FAILURE
fi
}
# Check if value equals zero
# exits with FAILURE on mismatch
#
# Arguments:
# val - value to compare
# string - output string on mismatch
#
check_ret_val()
{
val="$1"
string="$2"
if [[ ! "$val" == "0" ]]; then
printf "$ERROR $string\n"
exit $FAILURE
fi
}
# Check which authentication and side to use
#
# Arguments:
# side - 0 for client, 1 for server
#
# returns:
# 0 if certificate
# 1 on password
#
check_auth()
{
side=$1
if (( $side == $CLIENT )); then
ret=1
if [ -z $CLIENT_PASSWORD ]; then
if [ ! -f $CLIENT_CERTIFICATE ] ||
[ -z $CLIENT_CERTIFICATE ];then
printf "No client password set, no certificate"
printf " provided/accessible.\n"
exit $FAILURE
fi
ret=0
fi
else
ret=1
if [ -z $SERVER_PASSWORD ]; then
if [ ! -f $SERVER_CERTIFICATE ] ||
[ -z $SERVER_CERTIFICATE ]; then
printf "No server password set, no certificate"
printf " provided/accessible.\n"
exit $FAILURE
fi
ret=0
fi
fi
echo $ret
}
# print version of con-test
#
print_version()
{
printf "${INFO} You are using Version %d.%d-%d\n" $MAJOR $MINOR $RELEASE
}
# check if all dependencies are installed
#
check_dependecies()
{
printf "${INFO} Checking dependencies on $HOSTNAME\n"
# check if getopt is installed
test_get_opt
check_ret_val "$?" "`getopt --test` failed in this environment."
# check provided credentials
if [ -z $SERVER_CERTIFICATE ] || [ -z $CLIENT_CERTIFICATE ]; then
hash sshpass 2>/dev/null
if (( $? != 0 )); then
printf "Please provide client/server certificate, or"
printf " install sshpass and provide passwords\n"
exit $FAILURE
fi
fi
# Test for root or sudo
if (( $EUID != 0 )); then
hash sudo 2>/dev/null
check_ret_val "$?" "Please run as root, or _install_ sudo."
SUDO='sudo'
fi
# check the path to attenuation programm
hash $ATTTENUATOR_PATH 2>/dev/null
check_ret_val "$?" "Could not find $ATTENUATOR_PATH"
printf "${INFO} All checks passed on $HOSTNAME\n"
}