summaryrefslogtreecommitdiff
path: root/authorized_keys.sh
blob: d16588fdbb180cd27770d7ff3c6e9355593e6c21 (about) (plain) (blame)
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
#!/bin/bash

set -o pipefail

cd /opt/autopeer
NOTIFY_TO="$(python3 -c 'import config; print(config.NOTIFY_TO)')"

user="$1"
if [ "$1" = "new" ]; then
	type="$2"
	key="$3"
	connection="$4"

	if user="$(grep -l -s -r -P '^\s*auth:\s*\Q'"$type"'\E\s+\Q'"$key"'\E$' /opt/autopeer/dn42-registry/data/mntner/ | perl -ne 's@^.*/@@; s@-MNT$@@; print lc;' | head -1)"; then
		perl -ne 'print if s@^\s*auth:\s*ssh-@restrict,pty ssh-@' </opt/autopeer/dn42-registry/data/mntner/"$(echo "$user" | perl -ne 's@$@-MNT@; print uc;')"
		if getent passwd "$user"; then
			exit 0
		else
			echo "[autopeer $(hostname)] New user being created: $user from $key $connection" | socat stdio "$NOTIFY_TO"
			/usr/sbin/adduser --disabled-password --quiet --comment "created at $(date +%s) by $key ${connection//:/_}" --ingroup autopeer "$user"
			/usr/sbin/adduser "$user" bird
		fi
	fi
else # not new user
	perl -ne 'print if s@^\s*auth:\s*ssh-@restrict,pty ssh-@' </opt/autopeer/dn42-registry/data/mntner/"$(echo "$user" | perl -ne 's@$@-MNT@; print uc;')"
fi

exit 0