-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsamba-dnf-pkg-update
More file actions
273 lines (238 loc) · 8.15 KB
/
Copy pathsamba-dnf-pkg-update
File metadata and controls
273 lines (238 loc) · 8.15 KB
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
#!/bin/bash
#samba-dnf-pkg-update
#Patch1.0
TEXTRESET=$(tput sgr0)
RED=$(tput setaf 1)
YELLOW=$(tput setaf 3)
GREEN=$(tput setaf 2)
DOMAIN=$(hostname | sed 's/^[^.:]*[.:]//' | sed -e 's/\(.*\)/\U\1/')
FQDN=$(hostname)
user=$(whoami)
DNSSERVER=208.67.222.222
IP=$(hostname -I)
INTERFACE=$(nmcli | grep "connected to" | cut -d " " -f4)
#Checking for user permissions
if [ "$user" != "root" ]; then
echo ${red}"This program must be run as root ${textreset}"
echo "Exiting"
exit
else
echo "Running Program"
fi
#Cleanup
rm -f /root/samba*.rpm
echo ${GREEN}"Restarting NetworkManager"${TEXTRESET}
systemctl restart NetworkManager
sleep 5
#Set DNS to Something other than itself(Use ODNS)
echo ${YELLOW}"Setting DNS to OpenDNS Server (Temporarily) for Name Resolution"${TEXTRESET}
nmcli con mod $INTERFACE ipv4.dns $DNSSERVER
systemctl restart NetworkManager
sleep 1
#Check for Network Connectivity
echo "Checking for Internet Connectivity"
echo " "
sleep 3
# Function to check DNS resolution
check_dns_resolution() {
local domain=$1
ping -c 1 $domain &> /dev/null
return $?
}
# Function to ping an address
ping_address() {
local address=$1
ping -c 1 $address &> /dev/null
return $?
}
# Flag to track if any test fails
test_failed=false
# Check DNS resolution for google.com
echo "Checking DNS resolution for google.com via ping..."
if check_dns_resolution "google.com"; then
echo "DNS resolution for google.com is successful."
else
echo "DNS resolution for google.com failed."
test_failed=true
fi
# Ping 8.8.8.8
echo "Trying to ping 8.8.8.8..."
if ping_address "8.8.8.8"; then
echo "Successfully reached 8.8.8.8."
else
echo "Cannot reach 8.8.8.8."
test_failed=true
fi
# Provide final results summary
echo
echo "===== TEST RESULTS ====="
echo "DNS Resolution for google.com: $(if check_dns_resolution "google.com"; then echo "${GREEN}Passed"${TEXTRESET}; else echo "${RED}Failed"${TEXTRESET}; fi)"
echo "Ping to 8.8.8.8: $(if ping_address "8.8.8.8"; then echo "${GREEN}Passed"${TEXTRESET}; else echo "${RED}Failed"${TEXTRESET}; fi)"
echo "========================"
echo
# Prompt the user only if any test fails
if $test_failed; then
read -p "One or more tests failed. Please make sure you can resolve DNS names before Proceeding. Do you want to continue the script? (y/n): " user_input
if [[ $user_input == "y" || $user_input == "Y" ]]; then
echo "Continuing the script with failures"
sleep 1
# Place additional script logic here
else
echo "Please make sure that you have full Connectivty to the Internet Before Proceeding."
exit 1
fi
else
echo "All tests passed successfully."
echo " "
sleep 3
# Continue with the script or exit as needed
fi
#In the event a samba update arrives from Rocky Linux, rebuild Samba packages
dnfremote=$(dnf provides samba | grep Provide | sed 's/.dc//' | sed '1d')
dnflocal=$(dnf provides samba | grep Provide | sed 's/.dc//' | sed '$d')
mocksmbver=$(dnf provides samba | grep 'Provide' | awk -F' = ' '{print $2}' | sort -V | tail -n 1)
majoros=$(cat /etc/redhat-release | grep -Eo "[0-9]" | sed '$d')
minoros=$(cat /etc/redhat-release | grep -Eo "[0-9]" | sed '1d')
cat <<EOF
This Utility is going to update your compiled Samba version to a newer version
The Version in the upstream repository is:
$dnfremote
EOF
echo ${YELLOW}"The Last log entry for the Smaba replication log is:${TEXTRESET}"
tail -1 /var/log/dnf-smb-mon.log
echo " "
cat <<EOF
This process will:
-Remove local packages
-Download the new samba source
-Use mock to compile the source rpm
-Recreate the repo in the same location
-Run dnf update to the new version of Samba, with dependencies
EOF
while true; do
read -p "Would you like to update the system now? (y/n) " yn
case $yn in
[yY])
dnf -y update
dnf -y install bind-utils
# Delete all packages
rm -rf /root/.samba/*.rpm
# Download Samba source package
dnf download samba --source
# Validate the downloaded RPM file
if ls /root/samba-*.rpm 1> /dev/null 2>&1; then
echo ${GREEN}"Samba RPM file found. Continuing with the script..."${TEXTRESET}
mock -r rocky-"$majoros"-x86_64 --enablerepo=devel --define 'dist .el'"$majoros"'_'"$minoros"'.dc' --with dc samba-"$mocksmbver".src.rpm
cp /var/lib/mock/rocky-"$majoros"-x86_64/result/*.rpm /root/.samba
createrepo /root/.samba
dnf config-manager --add-repo /root/.samba
dnf -y update --nogpgcheck --repofrompath=samba,/root/.samba
dnf -y install --nogpgcheck samba-dc
# Reset MOTD
sed -i d /etc/motd
else
echo ${RED}"The Samba source rpm file did not download correctly. Please check your network settings."${TEXTRESET}
# Check DNS resolution
if nslookup google.com > /dev/null 2>&1; then
echo ${GREEN}"DNS is operational."${TEXTRESET}
else
echo ${RED}"DNS is not operational. Please check your DNS settings."${TEXTRESET}
fi
exit 1
fi
break
;;
[nN])
echo "Exiting..."
exit
;;
*)
echo "Invalid response"
;;
esac
done
#Cleanup
rm -f /root/samba*.rpm
echo ${GREEN}"Restarting NetworkManager"${TEXTRESET}
systemctl restart NetworkManager
sleep 5
#Set DNS back to AD
echo ${YELLOW}"Setting DNS entry back to the local interface from OPENDNS to AD"${TEXTRESET}
nmcli con mod $INTERFACE ipv4.dns $IP
echo " "
sleep 2
echo ${GREEN}"Restarting NetworkManager"${TEXTRESET}
systemctl restart NetworkManager
sleep 5
echo ${GREEN}"Attempting to enable samba.service"${TEXTRESET}
sleep 1
systemctl enable samba.service
echo " "
echo ${GREEN}"Attempting to start samba.service"${TEXTRESET}
systemctl start samba.service
sleep 1
#return status
# Check the status of the samba service
SERVICE="samba"
STATUS=$(systemctl is-active "$SERVICE")
# Provide feedback based on the service status
if [ "$STATUS" = "active" ]; then
echo ${GREEN}"The $SERVICE service is running."${TEXTRESET}
else
echo ${RED}"The $SERVICE service is not running."${TEXTRESET}
fi
#Validate that the server can resolve SRV records
echo ${GREEN}"Setting up Query for SRV records"${TEXTRESET}
sleep 1
host -t SRV _ldap._tcp.${DOMAIN}.
host -t SRV _kerberos._udp.${DOMAIN}.
host -t A ${FQDN}.
# Extract hostname from FQDN
hostname_part=$(echo "$FQDN" | cut -d '.' -f 1)
# Function to extract the target hostnames from SRV records
get_srv_hostnames() {
local srv_records=$1
echo "$srv_records" | awk '{print $NF}' | cut -d '.' -f 1 # Get the last field and extract the hostname part
}
# Get SRV records for LDAP and Kerberos
ldap_srv=$(host -t SRV _ldap._tcp."$DOMAIN")
kerberos_srv=$(host -t SRV _kerberos._udp."$DOMAIN")
# Extract target hostnames from SRV records
ldap_hostnames=$(get_srv_hostnames "$ldap_srv")
kerberos_hostnames=$(get_srv_hostnames "$kerberos_srv")
# Function to check if at least one hostname matches
check_any_hostnames_match() {
local hostnames=$1
local hostname=$2
for hn in $hostnames; do
if [ "$hn" == "$hostname" ]; then
echo "At least one hostname matches: ${GREEN}$hn"${TEXTRESET}
return 0
fi
done
return 1
}
# Check if any LDAP or Kerberos hostnames match the hostname part of FQDN
if check_any_hostnames_match "$ldap_hostnames" "$hostname_part" || check_any_hostnames_match "$kerberos_hostnames" "$hostname_part"; then
echo "${GREEN}Success:${TEXTRESET}Hostname from SRV record matches.."
echo "AD is resolvable"
exit_status=0
else
echo ${RED}"Error:${TEXTRESET} No hostnames from SRV records match the hostname part of FQDN. The samba service has failed to start or DNS is not configured correctly."
echo "AD Failed to resolve"
exit_status=1
read -p "Press Enter"
fi
# Handle exit status
if [ $exit_status -eq 0 ]; then
read -p "It is reccomended to restart the server. Would you like to do that now? (y/n): " user_input
if [ "$user_input" == "y" ] || [ "$user_input" == "Y" ]; then
echo ${RED}"Restarting the server..."${TEXTRESET}
sleep 2
sudo shutdown -r now
else
echo "Server restart canceled."
fi
else
exit 1
fi