forked from aknostic/ostiary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users
executable file
·152 lines (125 loc) · 3.32 KB
/
users
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
#!/bin/bash
#
# /etc/rc.d/init.d/users
#
# adding/deleting (disabling) users
#
# chkconfig: 02345 30 80
# description: adding/deleting (disabling) users
#
# Source function library.
. /etc/init.d/functions
manage=$(cat<<EOF
import sys
import os
import pwd
import json
import boto
import boto.utils
userdata = json.loads(boto.utils.get_instance_userdata())
# we always succeed when there is nothing to do
if not ('security' in userdata and 'users' in userdata['security']):
exit(0)
s3 = boto.connect_s3()
keys = s3.get_bucket(userdata['security']['bucket'], validate=False)
key = boto.s3.key.Key(keys)
privatekey = boto.s3.key.Key(keys)
if "provision" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
os.system("/usr/sbin/adduser -c \"{0}\" -G {1} {2}".format(
user['comment'],
",".join(user['groups']),
username))
key.key = user['email']
os.system("/bin/mkdir --mode=0700 -p /home/{0}/.ssh".format(username))
key.get_contents_to_filename(
"/home/{0}/.ssh/authorized_keys".format(username))
try:
privatekey.key = "{0}.private".format(user['email'])
privatekey.get_contents_to_filename(
"/home/{0}/.ssh/id_rsa".format(username))
os.system("chmod 600 /home/{0}/.ssh/id_rsa".format(username))
except:
sys.stdout.write(" {0} has no private key to provision".format(username))
os.system("chown -R {0}.{0} /home/{0}".format(username))
elif "remove" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
os.system("/usr/sbin/userdel -fr {0}".format(username))
elif "update" == sys.argv[1]:
for username in userdata['security']['users']:
user = userdata['security']['users'][username]
sys.stdout.write(" {0}".format(username))
key.key = user['email']
# also delete the file when it is gone
try:
key.exists()
key.get_contents_to_filename(
"/home/{0}/.ssh/authorized_keys".format(username))
except:
os.remove("/home/{0}/.ssh/authorized_keys".format(username))
try:
privatekey.key = "{0}.private".format(user['email'])
privatekey.exists()
privatekey.get_contents_to_filename(
"/home/{0}/.ssh/id_rsa".format(username))
os.system("chmod 600 /home/{0}/.ssh/id_rsa".format(username))
except:
# also remove
os.remove("/home/{0}/.ssh/id_rsa".format(username))
os.system("chown -R {0}.{0} /home/{0}".format(username))
else:
pass
EOF
)
# for some reason it 'stops' on runlevel 6, so we ignore that one
runlevel=`runlevel`
if [ "3 6" == "${runlevel}" ]; then
exit 0
fi
service=users
start() {
echo -n "Provisioning ${service}: "
/usr/bin/python -c "${manage}" provision
provisioned=$?
touch /var/lock/subsys/${service}
return ${provisioned}
}
stop() {
echo -n "Removing ${service}: "
/usr/bin/python -c "${manage}" remove
removed=$?
rm -f /var/lock/subsys/${service}
return ${removed}
}
restart() {
echo -n "Updating ${service}: "
/usr/bin/python -c "${manage}" update
updated=$?
return ${updated}
}
case "$1" in
start)
echo "start"
start
echo
;;
stop)
echo "stop"
stop
echo
;;
restart)
echo "restart"
restart
echo
;;
*)
echo "Usage: ${service} {start|stop|restart}"
exit 1
;;
esac
exit $?