-
Notifications
You must be signed in to change notification settings - Fork 24
/
samba-setup-checkup.sh
executable file
·342 lines (293 loc) · 11.9 KB
/
samba-setup-checkup.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
# This script is use to get system info so we can use this to make an ultimate checkup script.
#
# If new things are added, create 3 functions.
# function get_, get info
# function check_, run the checkup againt the info (get_) and (show_) output.
# function show_, show info
# try to keep the functions clear.
# get_host_ : involves only host related info, like hostnames and ipnumbers and resolving.
# get_samba_ : involves only samba related info,
# get_etc_ : involves only configuration files
# Note!
# for example : get_etc_samba_smbconf should not be get_samba_etc_smbconf
# get_samba_ should only show output of a running samba and test.
# like get_samba_fsmo
# the script needs root or sudo to get all info.
if [ "$EUID" -ne 0 ]
then echo "Please run as root, or use sudo. Exiting now..."
exit 1
fi
# ToDo 1: Get all system info and show system info.
# set some colors to outline Ok Warn en errors more.
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
UNDERLINE=$(tput smul)
function good {
# printf "%40s\n" "${GREEN}$@${NORMAL}"
printf "${GREEN}$@${NORMAL}\n"
}
function error {
printf "${RED}$@${NORMAL}\n"
}
function warning {
printf "${YELLOW}$@${NORMAL}\n"
}
function warning_underline {
printf "%40s\n" "${YELLOW}${UNDERLINE}$@${NORMAL}"
}
function check_run_as_sudo_root {
if ! [[ $EUID -eq 0 ]]; then
error "This script should be run using sudo or by root."
exit 1
fi
}
function check_error {
if [ $? -eq 0 ]; then
good "Ok"
elif [ $? -ge 1 ]; then
error "Error"
fi
}
function check_etc_hosts {
# count lines with the servers hostname
CHECK_ETC_HOSTS1=$(cat /etc/hosts | grep $HOST_NAME_SHORT | wc -l )
# count lines with the servers hostname and detected ipnumber
CHECK_ETC_HOSTS2=$(cat /etc/hosts | grep $HOST_NAME_SHORT | grep $HOST_IP| wc -l )
# check if FQDN is in position 2 in the detected line.
CHECK_ETC_HOSTS3=$(cat /etc/hosts | grep $HOST_NAME_SHORT | grep $HOST_IP| cut -d" " -f2)
# check if host does not contain 127.0.1.1 due to dhcp IPnumber at OS install.
CHECK_ETC_HOSTS_LOCALHOST1=$(cat /etc/hosts | grep $HOST_NAME_SHORT | grep 127.0.1.1 | wc -l )
if [ $CHECK_ETC_HOSTS_LOCALHOST1 -eq 1 ]; then
warning "Detected ip withing localhost range, asuming server install with DHCP enabled."
warning "You /etc/hosts contains : $HOST_NAME_SHORT and/or $HOST_NAME_FQDN with ip 127.0.1.1"
warning "expected was : $HOST_IP $HOST_NAME_FQDN $HOST_NAME_SHORT"
# the next check show what we expect to have.
fi
if [ $CHECK_ETC_HOSTS1 -eq $CHECK_ETC_HOSTS2 ]; then
if [ $CHECK_ETC_HOSTS3 != $HOST_NAME_FQDN ]; then
error "Possible error detected in /etc/hosts, mismatch FQDN and detected IP $HOST_IP for the host."
warning "expected was : $HOST_IP $HOST_NAME_FQDN $HOST_NAME_SHORT"
fi
fi
}
function get_host_info {
# Get all info of the server.
# short hostname (single word)
HOST_NAME_SHORT="$(hostname -s)"
# the domainname of the host (something.example.com)
HOST_NAME_DOMAIN="$(hostname -d)"
# Fully Qualified hostname
HOST_NAME_FQDN="$(hostname -f)"
# the server ipnumbers, check for multiple interfaces.
HOST_IP1="$(hostname -i)"
HOST_IP2="$(hostname -I)"
HOST_GATEWAY=$(ip route | grep default | cut -d" " -f3)
if [ $HOST_IP1 = $HOST_IP2 ]; then
HOST_IP="${HOST_IP1}"
HOST_PRIMARY_INTERFACE="$(ip route | grep ${HOST_IP} | cut -d" " -f3)"
HOST_IP2="Only one interface detected"
else
echo "TODO, not finished"
echo "Detected multiple ipnumbers"
# multiple ipnumbers detected, get primary interfaces
HOST_PRIMARY_INTERFACE="$(ip route | grep ${HOST_IP} | cut -d" " -f3)"
# set ipadres of primary interface.
HOST_IP=
# check if primary ip adres is in range 10. 172.16. 192.168.
fi
# Resolv.conf info.
HOST_RESOLV_DOMAIN="$(cat /etc/resolv.conf | grep domain)"
HOST_RESOLV_SEARCH="$(cat /etc/resolv.conf | grep search)"
# count nameservers
HOST_RESOLV_NAMESERV_COUNTER=$(cat /etc/resolv.conf | grep nameserver| wc -l)
if [ $HOST_RESOLV_NAMESERV_COUNTER -eq 1 ]; then
HOST_RESOLV_NAMESERV1="$(cat /etc/resolv.conf | grep nameserver| head -n1 | cut -d" " -f2)"
HOST_
fi
if [ $HOST_RESOLV_NAMESERV_COUNTER -eq 2 ]; then
HOST_RESOLV_NAMESERV1="$(cat /etc/resolv.conf | grep nameserver| head -n1 | cut -d" " -f2)"
HOST_RESOLV_NAMESERV2="$(cat /etc/resolv.conf | grep nameserver| tail -n1 | cut -d" " -f2)"
fi
if [ $HOST_RESOLV_NAMESERV_COUNTER -eq 3 ]; then
HOST_RESOLV_NAMESERV3="$(cat /etc/resolv.conf | grep nameserver| tail -n1 | cut -d" " -f2)"
else
HOST_RESOLV_NAMESERV3=""
fi
}
function show_host_info {
echo "HOST_NAME_SHORT: ${HOST_NAME_SHORT}"
# the domainname of the host (something.example.com)
echo "HOST_NAME_DOMAIN: ${HOST_NAME_DOMAIN}"
# Fully Qualified hostname
echo "HOST_NAME_FQDN: ${HOST_NAME_FQDN}"
# the server ipnumbers, check for multiple interfaces.
echo "HOST_IP1: ${HOST_IP1}"
echo "HOST_IP2: ${HOST_IP2}"
echo "HOST_GATEWAY: ${HOST_GATEWAY}"
echo "HOST_PRIMARY_INTERFACE: ${HOST_PRIMARY_INTERFACE}"
# Resolv.conf info.
echo "HOST_RESOLV_DOMAIN: ${HOST_RESOLV_DOMAIN}"
echo "HOST_RESOLV_SEARCH: ${HOST_RESOLV_SEARCH}"
# nameservers
#echo "HOST_RESOLV_NAMESERV_COUNTER: ${HOST_RESOLV_NAMESERV_COUNTER}"
echo "HOST_RESOLV_NAMESERV1: ${HOST_RESOLV_NAMESERV1}"
echo "HOST_RESOLV_NAMESERV2: ${HOST_RESOLV_NAMESERV2}"
echo "HOST_RESOLV_NAMESERV3: ${HOST_RESOLV_NAMESERV3}"
}
function check_host_info {
get_host_info
# check if hostname setup is correct.
local HOSTNAME_SHORT_WITH_DOMAIN="$HOST_NAME_SHORT.${HOST_NAME_DOMAIN}"
echo -n "Check hostnames : "
if [ $HOSTNAME_SHORT_WITH_DOMAIN = $HOST_NAME_FQDN ]; then
good "Ok"
else
warning "Mismatch in hostname definitions"
echo "please check : "
show_host_info
fi
unset HOSTNAME_SHORT_WITH_DOMAIN
check_etc_hosts
echo "Checking detected host ipnumbers from resolv.conf and default gateway"
check_host_ip
}
function check_host_ip {
# check resolving and check for internet.
if [ ! -z ${HOST_GATEWAY} ]; then
echo -n "Ping gateway ip : "
check_ping ${HOST_GATEWAY}
warning "Warning, no ping to gateway, this might be firewalled."
warning "check you internet connection, AD DNS might need it."
fi
if [ ! -z ${HOST_RESOLV_NAMESERV1} ]; then
echo -n "ping nameserver1: "
check_ping ${HOST_RESOLV_NAMESERV1}
fi
if [ ! -z ${HOST_RESOLV_NAMESERV2} ]; then
echo -n "ping nameserver2: "
check_ping ${HOST_RESOLV_NAMESERV2}
fi
if [ ! -z ${HOST_RESOLV_NAMESERV3} ]; then
echo -n "ping nameserver3: "
check_ping ${HOST_RESOLV_NAMESERV3}
fi
echo -n "Check ping google dns : "
check_ping 8.8.8.8
warning "Warning, no ping to internet dns 8.8.8.8, this might be firewalled."
warning "Check you internet connection, AD DNS might need it."
}
function check_ping {
echo -n "$1 : "
ping -q -c1 $1 >/dev/null
check_error
}
function get_samba_base_info {
SAMBA_SERVER_ROLE="$(samba-tool testparm --parameter-name="server role" 2>/dev/null | tail -1)"
SAMBA_SERVER_SERVICES="$(samba-tool testparm --parameter-name="server services" 2>/dev/null | tail -1)"
SAMBA_DCERPC_ENDPOINT_SERVERS="$(samba-tool testparm --parameter-name="dcerpc endpoint servers" 2>/dev/null | tail -1)"
}
function show_samba_base_info {
echo "SAMBA_SERVER_ROLE: ${SAMBA_SERVER_ROLE}"
echo "SAMBA_SERVER_SERVICES: ${SAMBA_SERVER_SERVICES}"
echo "SAMBA_DCERPC_ENDPOINT_SERVERS: ${SAMBA_DCERPC_ENDPOINT_SERVERS}"
}
function get_samba_build_info {
# create array of variables from the installed samba version.
#GET_SMB_CFG_ARRAY=($(smbd -b | grep ": /" | sed 's/\ //g' | sed 's/:/=/g'))
#debug#declare -p $GET_SMB_CFG_ARRAY
WHICH_SMBD=$(which smbd)
WHICH_NMBD=$(which nmbd)
WHICH_SAMBA=$(which samba)
WHICH_SAMBA_ADDC=$(which samba-ad-dc)
WHICH_WINBIND=$(which winbind)
# set empty variable, used in test imported variable
Builtusing=""
for detect_samba_bin in $WHICH_SMBD $WHICH_NMBD $WHICH_SAMBA $WHICH_SAMBA_ADDC $WHICH_WINBIND ; do
if [ -f $detect_samba_bin ]; then
$detect_samba_bin -b | grep ": /" | sed 's/\ //g' | sed 's/:/=/g' > /tmp/samba-buildvar.output
# import variables
source /tmp/samba-buildvar.output
# remove imported file
rm /tmp/samba-buildvar.output
# import only once, break for statment
break
else
warning "Detected $detect_samba_bin but not found with test -f."
warning "This is possible with for example a winbind only install, continue testing."
fi
done
unset detect_samba_bin
# test files and folders, and show rights group and owner.
for check_file_owner in $CONFIGFILE $LMHOSTSFILE $SMB_PASSWD_FILE ; do
echo "Checking file owner.. "
if [ -f $check_file_owner ]; then
ls -l $check_file_owner | awk '{ print $1,$3,$4,"\t",$9 }'
else
warning "Missing file $check_file_owner"
fi
done
unset check_file_owner
for check_dir_owner in $BINDIR $CACHEDIR $LIBDIR $LOCKDIR $LOGFILEBASE $MODULESDIR $PIDDIR $PRIVATE_DIR $SBINDIR $STATEDIR ; do
if [ -d $check_dir_owner ]; then
ls -ld $check_dir_owner | awk '{ print $1,$3,$4,"\t",$9 }'
else
warning "Missing folder $check_dir_owner"
fi
done
unset check_dir_owner
}
function get_samba_fsmo {
SAMBA_DC_FSMO=$(samba-tool fsmo show | cut -d"," -f2 | head -n1 | cut -c4-100)
SAMBA_DC_FSMO_SITE=$(samba-tool fsmo show | cut -d"," -f4 | head -n1 | cut -c4-100)
SAMBA_DC_NC=$(samba-tool fsmo show | cut -d"," -f7,8,9 | head -n1)
## detect multiple DC's if there are more.
SAMBA_DCS=$(host -t SRV _kerberos._udp.${HOST_NAME_DOMAIN} | awk '{print $NF}'| sed 's/.$//')
SAMBA_DC1=$(echo "$SAMBA_DCS" | sed -n 1p)
SAMBA_DC2=$(echo "$SAMBA_DCS" | sed -n 2p)
echo "DCS ${SAMBA_DCS}"
echo "DC1 ${SAMBA_DC1}"
echo "DC2 ${SAMBA_DC2}"
## get the ip of the DC's
if [ -z "${SAMBA_DC1}" ] && [ -z "${SAMBA_DC2}" ]; then
echo "Could not obtain an ipaddress for any AD DC.. Exiting"
exit 1
fi
SAMBA_NT_DOMAINNAME=$(samba-tool domain info ${SAMBA_DC1} | grep Netbios | cut -d":" -f2 | cut -c2-100)
SAMBA_KERBEROS_NAME=$(cat /etc/krb5.conf | grep default_realm | cut -d"=" -f2 | cut -c2-100)
echo "Samba AD DC info: = detected (command and where to look)"
echo "This server hostname = ${HOST_NAME_SHORT} (hostname -s and /etc/hosts and DNS server)"
echo "This server FQDN (hostname) = ${HOST_NAME_FQDN} (hostname -f and /etc/hosts and DNS server)"
echo "This server primary dnsdomain = ${HOST_NAME_DOMAIN} (hostname -d and /etc/resolv.conf and DNS server)"
echo "This server IP address(ses) = ${HOST_IP1} ${HOST_IP2} (hostname -i (-I) and /etc/networking/interfaces and DNS server"
echo "The DC with FSMO roles = ${SAMBA_DC_FSMO} (samba-tool fsmo show)"
echo "The DC (with FSMO) Site name = ${SAMBA_DC_FSMO_SITE} (samba-tool fsmo show)"
echo "The Default Naming Context = ${SAMBA_DC_NC} (samba-tool fsmo show)"
echo "The Kerberos REALM name used = ${SAMBA_KERBEROS_NAME} (kinit and /etc/krb5.conf and resolving)"
if [ -z "${SAMBA_DC2}" ]; then
SAMBA_DC1_IP=$(host -t A ${SAMBA_DC1} | awk '{print $NF}')
echo "The Ipadres of DC ${SAMBA_DC1} = ${SAMBA_DC1_IP}"
else
SAMBA_DC1_IP=$(host -t A ${SAMBA_DC1} | awk '{print $NF}')
SAMBA_DC2_IP=$(host -t A ${SAMBA_DC2} | awk '{print $NF}')
echo "The Ipadres of DC ${SAMBA_DC1} = ${SAMBA_DC1_IP}"
echo "The Ipadres of DC ${SAMBA_DC2} = ${SAMBA_DC2_IP}"
fi
}
# check host info, show when errors are found.
check_host_info
# samba build related info
get_samba_build_info
get_samba_fsmo
get_samba_base_info
show_samba_base_info