forked from shankerbalan/cloudstack-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloud-set-guest-password-sles
executable file
·120 lines (100 loc) · 4.43 KB
/
cloud-set-guest-password-sles
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
#!/bin/sh
# Copyright (c) 2013 Apache CloudStack
#
# Author: Apache CloudStack
#
# /etc/init.d/cloud-set-guest-password
#
# Init file for Password Download Client
#
### BEGIN INIT INFO
# Provides: cloud-set-guest-password
# Required-Start: $network $local_fs $syslog
# Required-Stop: $network $local_fs $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Password Download Client
# Description: CloudStack Password Download Client
### END INIT INFO
. /etc/rc.status
# First reset status of this service
rc_reset
# Modify this line to specify the user (default is root)
user=root
# Add your DHCP lease folders here
DHCP_FOLDERS="/var/lib/dhcpcd/*"
password_received=0
file_count=0
error_count=0
case "$1" in
start)
echo -n "Starting cloud-guest-set-password"
for DHCP_FILE in $DHCP_FOLDERS; do
if [ -f $DHCP_FILE ]; then
file_count=$((file_count+1))
. /$DHCP_FILE
PASSWORD_SERVER_IP=$DHCPSIADDR
if [ -n $PASSWORD_SERVER_IP ]; then
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"
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 ]; then
logger -t "cloud" "Got response from server at $PASSWORD_SERVER_IP"
case $password in
"") logger -t "cloud" "Password server at $PASSWORD_SERVER_IP did not have any password for the VM"
continue
;;
"bad_request") logger -t "cloud" "VM sent an invalid request to password server at $PASSWORD_SERVER_IP"
error_count=$((error_count+1))
continue
;;
"saved_password") logger -t "cloud" "VM has already saved a password from the password server at $PASSWORD_SERVER_IP"
continue
;;
*) logger -t "cloud" "VM got a valid password from server at $PASSWORD_SERVER_IP"
password_received=1
break
;;
esac
else
logger -t "cloud" "Failed to send request to password server at $PASSWORD_SERVER_IP"
error_count=$((error_count+1))
fi
else
logger -t "cloud" "Could not find password server IP in $DHCP_FILE"
error_count=$((error_count+1))
fi
fi
done
if [ "$password_received" == "0" ]; then
if [ "$error_count" == "$file_count" ]; then
logger -t "cloud" "Failed to get password from any server"
exit 1
else
logger -t "cloud" "Did not need to change password."
exit 0
fi
fi
logger -t "cloud" "Changing password ..."
echo $password | passwd --stdin $user
if [ $? -gt 0 ]; then
usermod -p `mkpasswd -m SHA-512 $password` $user
if [ $? -gt 0 ]; then
logger -t "cloud" "Failed to change password for user $user"
exit 1
else
logger -t "cloud" "Successfully changed password for user $user"
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
# Remember status and be verbose
rc_status -v
;;
stop)
echo -n "Shutting down cloud-set-guest-password"
rc_status -v
;;
esac
rc_exit