Skip to content

Commit 37874a3

Browse files
committed
tools: Various fixes to password set script
The exit status of wget was not checked properly since it would check the exit status of the 'echo' command. Also fix some indentation in the script and remove whitespace
1 parent 1966871 commit 37874a3

1 file changed

Lines changed: 38 additions & 46 deletions

File tree

setup/bindir/cloud-set-guest-password.in

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# to you under the Apache License, Version 2.0 (the
1313
# "License"); you may not use this file except in compliance
1414
# with the License. You may obtain a copy of the License at
15-
#
15+
#
1616
# http://www.apache.org/licenses/LICENSE-2.0
17-
#
17+
#
1818
# Unless required by applicable law or agreed to in writing,
1919
# software distributed under the License is distributed on an
2020
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -28,49 +28,46 @@ user=root
2828

2929
# Add your DHCP lease folders here
3030
DHCP_FOLDERS="/var/lib/dhclient/* /var/lib/dhcp3/* /var/lib/dhcp/*"
31+
PASSWORD_SERVER_PORT=8080
3132
password_received=0
3233
file_count=0
3334
error_count=0
3435

35-
for DHCP_FILE in $DHCP_FOLDERS
36-
do
37-
if [ -f $DHCP_FILE ]
38-
then
36+
for DHCP_FILE in $DHCP_FOLDERS; do
37+
if [ -f $DHCP_FILE ]; then
3938
file_count=$((file_count+1))
4039
PASSWORD_SERVER_IP=$(grep dhcp-server-identifier $DHCP_FILE | tail -1 | awk '{print $NF}' | tr -d '\;')
4140

42-
if [ -n "$PASSWORD_SERVER_IP" ]
43-
then
41+
if [ -n "$PASSWORD_SERVER_IP" ]; then
4442
logger -t "cloud" "Found password server IP $PASSWORD_SERVER_IP in $DHCP_FILE"
4543
logger -t "cloud" "Sending request to password server at $PASSWORD_SERVER_IP"
46-
password=$(wget -q -t 3 -T 20 -O - --header "DomU_Request: send_my_password" $PASSWORD_SERVER_IP:8080)
47-
password=$(echo $password | tr -d '\r')
4844

49-
if [ $? -eq 0 ]
50-
then
45+
password=$(wget -q -t 3 -T 20 -O - --header "DomU_Request: send_my_password" $PASSWORD_SERVER_IP:$PASSWORD_SERVER_PORT)
46+
47+
if [ $? -eq 0 ]; then
48+
password=$(echo $password | tr -d '\r')
5149
logger -t "cloud" "Got response from server at $PASSWORD_SERVER_IP"
5250

5351
case $password in
54-
55-
"") logger -t "cloud" "Password server at $PASSWORD_SERVER_IP did not have any password for the VM"
56-
continue
57-
;;
58-
59-
"bad_request") logger -t "cloud" "VM sent an invalid request to password server at $PASSWORD_SERVER_IP"
60-
error_count=$((error_count+1))
61-
continue
62-
;;
63-
64-
"saved_password") logger -t "cloud" "VM has already saved a password from the password server at $PASSWORD_SERVER_IP"
65-
continue
66-
;;
67-
68-
*) logger -t "cloud" "VM got a valid password from server at $PASSWORD_SERVER_IP"
69-
password_received=1
70-
break
71-
;;
72-
73-
esac
52+
"")
53+
logger -t "cloud" "Password server at $PASSWORD_SERVER_IP did not have any password for the VM"
54+
continue
55+
;;
56+
"bad_request")
57+
logger -t "cloud" "VM sent an invalid request to password server at $PASSWORD_SERVER_IP"
58+
error_count=$((error_count+1))
59+
continue
60+
;;
61+
"saved_password")
62+
logger -t "cloud" "VM has already saved a password from the password server at $PASSWORD_SERVER_IP"
63+
continue
64+
;;
65+
*)
66+
logger -t "cloud" "VM got a valid password from server at $PASSWORD_SERVER_IP"
67+
password_received=1
68+
break
69+
;;
70+
esac
7471
else
7572
logger -t "cloud" "Failed to send request to password server at $PASSWORD_SERVER_IP"
7673
error_count=$((error_count+1))
@@ -82,10 +79,8 @@ do
8279
fi
8380
done
8481

85-
if [ "$password_received" == "0" ]
86-
then
87-
if [ "$error_count" == "$file_count" ]
88-
then
82+
if [ "$password_received" == "0" ]; then
83+
if [ "$error_count" == "$file_count" ]; then
8984
logger -t "cloud" "Failed to get password from any server"
9085
exit 1
9186
else
@@ -94,23 +89,20 @@ then
9489
fi
9590
fi
9691

97-
logger -t "cloud" "Changing password ..."
92+
logger -t "cloud" "Changing password for user $user"
9893
echo $user:$password | chpasswd
99-
100-
if [ $? -gt 0 ]
101-
then
94+
95+
if [ $? -gt 0 ]; then
10296
usermod -p `mkpasswd -m SHA-512 $password` $user
103-
104-
if [ $? -gt 0 ]
105-
then
97+
98+
if [ $? -gt 0 ]; then
10699
logger -t "cloud" "Failed to change password for user $user"
107100
exit 1
108101
else
109102
logger -t "cloud" "Successfully changed password for user $user"
110103
fi
111104
fi
112-
105+
113106
logger -t "cloud" "Sending acknowledgment to password server at $PASSWORD_SERVER_IP"
114-
wget -t 3 -T 20 -O - --header "DomU_Request: saved_password" $PASSWORD_SERVER_IP:8080
107+
wget -q -t 3 -T 20 -O - --header "DomU_Request: saved_password" $PASSWORD_SERVER_IP:$PASSWORD_SERVER_PORT
115108
exit 0
116-

0 commit comments

Comments
 (0)