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
This commit is contained in:
Wido den Hollander 2014-05-27 11:36:20 +02:00
parent 19668713ed
commit 37874a3fa8

View File

@ -12,9 +12,9 @@
# to you under the Apache License, Version 2.0 (the # to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance # "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at # with the License. You may obtain a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, # Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an # software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -28,49 +28,46 @@ user=root
# Add your DHCP lease folders here # Add your DHCP lease folders here
DHCP_FOLDERS="/var/lib/dhclient/* /var/lib/dhcp3/* /var/lib/dhcp/*" DHCP_FOLDERS="/var/lib/dhclient/* /var/lib/dhcp3/* /var/lib/dhcp/*"
PASSWORD_SERVER_PORT=8080
password_received=0 password_received=0
file_count=0 file_count=0
error_count=0 error_count=0
for DHCP_FILE in $DHCP_FOLDERS for DHCP_FILE in $DHCP_FOLDERS; do
do if [ -f $DHCP_FILE ]; then
if [ -f $DHCP_FILE ]
then
file_count=$((file_count+1)) file_count=$((file_count+1))
PASSWORD_SERVER_IP=$(grep dhcp-server-identifier $DHCP_FILE | tail -1 | awk '{print $NF}' | tr -d '\;') PASSWORD_SERVER_IP=$(grep dhcp-server-identifier $DHCP_FILE | tail -1 | awk '{print $NF}' | tr -d '\;')
if [ -n "$PASSWORD_SERVER_IP" ] if [ -n "$PASSWORD_SERVER_IP" ]; then
then
logger -t "cloud" "Found password server IP $PASSWORD_SERVER_IP in $DHCP_FILE" logger -t "cloud" "Found password server IP $PASSWORD_SERVER_IP in $DHCP_FILE"
logger -t "cloud" "Sending request to password server at $PASSWORD_SERVER_IP" logger -t "cloud" "Sending request to password server at $PASSWORD_SERVER_IP"
password=$(wget -q -t 3 -T 20 -O - --header "DomU_Request: send_my_password" $PASSWORD_SERVER_IP:8080)
password=$(echo $password | tr -d '\r')
if [ $? -eq 0 ] password=$(wget -q -t 3 -T 20 -O - --header "DomU_Request: send_my_password" $PASSWORD_SERVER_IP:$PASSWORD_SERVER_PORT)
then
if [ $? -eq 0 ]; then
password=$(echo $password | tr -d '\r')
logger -t "cloud" "Got response from server at $PASSWORD_SERVER_IP" logger -t "cloud" "Got response from server at $PASSWORD_SERVER_IP"
case $password in case $password in
"")
"") logger -t "cloud" "Password server at $PASSWORD_SERVER_IP did not have any password for the VM" logger -t "cloud" "Password server at $PASSWORD_SERVER_IP did not have any password for the VM"
continue continue
;; ;;
"bad_request")
"bad_request") logger -t "cloud" "VM sent an invalid request to password server at $PASSWORD_SERVER_IP" logger -t "cloud" "VM sent an invalid request to password server at $PASSWORD_SERVER_IP"
error_count=$((error_count+1)) error_count=$((error_count+1))
continue continue
;; ;;
"saved_password")
"saved_password") logger -t "cloud" "VM has already saved a password from the password server at $PASSWORD_SERVER_IP" logger -t "cloud" "VM has already saved a password from the password server at $PASSWORD_SERVER_IP"
continue continue
;; ;;
*)
*) logger -t "cloud" "VM got a valid password from server at $PASSWORD_SERVER_IP" logger -t "cloud" "VM got a valid password from server at $PASSWORD_SERVER_IP"
password_received=1 password_received=1
break break
;; ;;
esac
esac
else else
logger -t "cloud" "Failed to send request to password server at $PASSWORD_SERVER_IP" logger -t "cloud" "Failed to send request to password server at $PASSWORD_SERVER_IP"
error_count=$((error_count+1)) error_count=$((error_count+1))
@ -82,10 +79,8 @@ do
fi fi
done done
if [ "$password_received" == "0" ] if [ "$password_received" == "0" ]; then
then if [ "$error_count" == "$file_count" ]; then
if [ "$error_count" == "$file_count" ]
then
logger -t "cloud" "Failed to get password from any server" logger -t "cloud" "Failed to get password from any server"
exit 1 exit 1
else else
@ -94,23 +89,20 @@ then
fi fi
fi fi
logger -t "cloud" "Changing password ..." logger -t "cloud" "Changing password for user $user"
echo $user:$password | chpasswd echo $user:$password | chpasswd
if [ $? -gt 0 ] if [ $? -gt 0 ]; then
then
usermod -p `mkpasswd -m SHA-512 $password` $user usermod -p `mkpasswd -m SHA-512 $password` $user
if [ $? -gt 0 ] if [ $? -gt 0 ]; then
then
logger -t "cloud" "Failed to change password for user $user" logger -t "cloud" "Failed to change password for user $user"
exit 1 exit 1
else else
logger -t "cloud" "Successfully changed password for user $user" logger -t "cloud" "Successfully changed password for user $user"
fi fi
fi fi
logger -t "cloud" "Sending acknowledgment to password server at $PASSWORD_SERVER_IP"
wget -t 3 -T 20 -O - --header "DomU_Request: saved_password" $PASSWORD_SERVER_IP:8080
exit 0
logger -t "cloud" "Sending acknowledgment to password server at $PASSWORD_SERVER_IP"
wget -q -t 3 -T 20 -O - --header "DomU_Request: saved_password" $PASSWORD_SERVER_IP:$PASSWORD_SERVER_PORT
exit 0