forked from dokku-alt/dokku-alt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshcommand
More file actions
executable file
·80 lines (64 loc) · 2.15 KB
/
Copy pathsshcommand
File metadata and controls
executable file
·80 lines (64 loc) · 2.15 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
79
#!/bin/bash
set -e
shopt -s nocasematch #For case insensitive string matching, for the first parameter
SELF=`which $0`
HOME_DIR=${HOME_DIR:-"/home"}
case "$1" in
create) # sshcommand create <user> <command>
if [[ $# -ne 3 ]]; then
echo "Usage : sshcommand create user command"
exit -1
fi
USER="$2"; COMMAND="$3"
getent passwd $USER > /dev/null || USERHOME="$HOME_DIR/$USER"
if [ -z $USERHOME ]; then
USERHOME=$(bash <<< "echo ~$USER")
else
useradd -m -d $USERHOME $USER || true
fi
mkdir -p "$USERHOME/.ssh"
touch $USERHOME/.ssh/authorized_keys
echo "$COMMAND" > "$USERHOME/.sshcommand"
chown $USER "$USERHOME"
chown -R $USER "$USERHOME/.ssh"
;;
acl-add) # sshcommand acl-add <user> <identifier>
if [[ $# -ne 3 ]]; then
echo "Usage : sshcommand acl-add user identifier"
exit -1
fi
USER="$2"; NAME="$3"
getent passwd $USER > /dev/null || false
USERHOME=$(bash <<< "echo ~$USER")
KEY_FILE=$(mktemp)
KEY=$(tee "$KEY_FILE")
delete_key_file() {
rm -f "$KEY_FILE"
}
trap delete_key_file INT EXIT
FINGERPRINT=$(ssh-keygen -lf "$KEY_FILE" | awk '{print $2}')
if [[ "$FINGERPRINT" == "" ]]; then
echo "Invalid ssh public key"
exit -1
fi
KEY_PREFIX="command=\"FINGERPRINT=$FINGERPRINT NAME=$NAME \`cat $USERHOME/.sshcommand\` \$SSH_ORIGINAL_COMMAND\",no-agent-forwarding,no-user-rc,no-X11-forwarding,no-port-forwarding"
echo "$KEY_PREFIX $KEY" >> "$USERHOME/.ssh/authorized_keys"
echo $FINGERPRINT
;;
acl-remove) # sshcommand acl-remove <user> <identifier>
if [[ $# -ne 3 ]]; then
echo "Usage : sshcommand acl-remove user identifier"
exit -1
fi
USER="$2"; NAME="$3"
getent passwd $USER > /dev/null || false
USERHOME=$(bash <<< "echo ~$USER")
sed --in-place "/ NAME=$NAME /d" "$USERHOME/.ssh/authorized_keys"
;;
help|*) # sshcommand help
echo "Usage : sshcommand create user command"
echo " sshcommand acl-add user identifier"
echo " sshcommand acl-remove user identifier"
echo " sshcommand help # shows this usage message"
;;
esac