-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmake-pubkey-users
More file actions
executable file
·78 lines (73 loc) · 2.32 KB
/
Copy pathmake-pubkey-users
File metadata and controls
executable file
·78 lines (73 loc) · 2.32 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
#!/bin/bash
#
# A script to bulk create users using pubkey-only auth.
#
# If a file ./pubkeys/$user exists, use it to populate the authorized_keys.
# If /etc/security/authorized_keys exists, put the pubkey in here (and if
# monkeysphere is installed, refresh its live config). Otherwise, put it in
# ~/.ssh/authorized_keys
set -eo pipefail
err_report() {
echo "errexit on line $(caller)" >&2
}
trap err_report ERR
WHEEL=wheel
DEBIAN=
if [ -d /etc/apt ]; then
# Debian admin group is sudo, not wheel
WHEEL=sudo
DEBIAN=true
fi
if [ ! -f /etc/skel/.ssh/authorized_keys ]; then
if [ ! -d /etc/skel/.ssh ]; then
mkdir /etc/skel/.ssh
fi
touch /etc/skel/.ssh/authorized_keys
chmod -R og= /etc/skel/.ssh
fi
_PSAA=""
if [ -d /etc/security/authorized_keys ]; then
_PSAA=1
# Make sure we can't log in on console with empty passwords!
# *-ac are Redhat files; /var/lib/pam/* are Debian
for file in /etc/pam.d/password-auth-ac /etc/pam.d/system-auth-ac /usr/share/pam-configs/unix; do
if grep -s nullok "$file"; then
# Debian uses nullok_secure, but it's still not good enough
perl -pi.bak -e 's/\s+nullok(_secure)?\s+/ /' "$file"
fi
done
if [ -n "$DEBIAN" ]; then
# Regenerate pam config from updated template
pam-auth-update --force
fi
fi
for user in $*; do
/usr/sbin/adduser --disabled-password --gecos "" "$user"
if [ -n "$_PSAA" ]; then
if [[ -f "pubkeys/$user" ]]; then
# use awk 1 instead of cat to normalize line endings
TEMPFILE=/etc/security/authorized_keys/$user.temp.$$
awk 1 "/etc/security/authorized_keys/$user" > "$TEMPFILE" || true
awk 1 "pubkeys/$user" >> "$TEMPFILE"
mv "$TEMPFILE" "/etc/security/authorized_keys/$user"
# If we are using monkeysphere, we need to update the live config
if [ -x /usr/sbin/monkeysphere-authentication ]; then
monkeysphere-authentication update-users
fi
fi
# Now empty the user's password and expire it
for user in $*; do
/usr/sbin/usermod -p "" "$user"
/usr/bin/chage -d 0 "$user"
done
else
# use awk 1 instead of cat to normalize line endings
TEMPFILE=/home/$user/.ssh/authorized_keys.temp.$$
awk 1 "/home/$user/.ssh/authorized_keys" > "$TEMPFILE" || true
chown "$user" "$TEMPFILE"
chmod og= "$TEMPFILE"
awk 1 pubkeys/$user >> "$TEMPFILE"
mv "$TEMPFILE" "/home/$user/.ssh/authorized_keys"
fi
/usr/sbin/usermod -aG "$WHEEL" "$user"
done