summaryrefslogtreecommitdiff
path: root/src/endofossil
blob: c4ae60dc57da171dbb6baaaabb8c8f3acad75211 (plain)
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
#!/bin/bash
set -e -o pipefail

IFS=/ read n pid uid <<< "$1"
[ "$pid" ]

[ "$uid" -gt 0 ]
IFS=: read username realname _ _ gecos homedir shell < <(getent passwd "$uid")

authtype=
while read -d ''
do
    case "${REPLY%%=*}" in
        'SSH_USER_AUTH' ) read authtype keytype keyvalue < "${REPLY#*=}" ;;
        'SSH_ORIGINAL_COMMAND' ) SSH_ORIGINAL_COMMAND=${REPLY#*=} ;;
    esac
done < /proc/$pid/environ
[ "$authtype" = publickey ]

keyhash=
while read
do
    set -- $REPLY
    if [ "$3 $5" = 'SSHFP 2' ]
    then
        keyhash=$6
        break
    fi
done < <(ssh-keygen -f <(printf '%s\n' "$keytype $keyvalue") -r .)
[ "$keyhash" ]

case "$SSH_ORIGINAL_COMMAND" in
    *\"* ) exit 1 ;;
    fossil\ test-http\ * ) ;;
    *) exit 1 ;;
esac

# This is no way to do it.
# Let's just share /home/*/src and /srv/src and /usr/src.
fossil_dir=$homedir/src/fossil
upstreamDatabase=$fossil_dir/db
readWriteDbName=db.fossil # Must end in .fossil for 'fossil test-http' to find it.
readWriteDir=$fossil_dir/remotes/$keyhash

as_user()
{
    setpriv --reuid="$username" --init-groups --inh-caps=-all "$@"
}
if ! [ -d "$readWriteDir" ]
then
        as_user mkdir "$readWriteDir"
fi
as_user cp -n --reflink -- "$upstreamDatabase" "$readWriteDir"/"$readWriteDbName"

exec systemd-run -P \
     --property=User="$username" \
     --property=ReadOnlyPaths=/ \
     --property=ReadWritePaths="$readWriteDir" \
     --property=WorkingDirectory="$readWriteDir" \
     -- fossil test-http "$readWriteDbName"