Git Repositories

Fix ssh authentication.
[simple-git-host.git] / homegit / git-shell-commands / check
1 #!/bin/sh
2 GITUSER="$1"
3 if [ -z "$SSH_ORIGINAL_COMMAND" ]; then
4   USER=$GITUSER
5   export USER
6   exec git-shell
7 fi
8 ispublic() {
9   [ -e "$1"/git-daemon-export-ok ]
10 }
11 rungitcmd() {
12   exec /usr/bin/git-shell -c "$SSH_ORIGINAL_COMMAND"
13 }
14 error() {
15   echo "Error: $1" >&2
16   exit 1
17 }
18 eval set -- $SSH_ORIGINAL_COMMAND
19 # $1 = command
20 # $2 = repo path
21 CMD="$1"
22 REPO="$2"
23 PUSHCMD="git-receive-pack"
24 if [ -d "$REPO" ] && [ -r "$REPO"/.users ]; then
25   if grep -q "^$GITUSER:.*\$" "$REPO"/.users; then
26     RIGHT=$(sed -r -n "/^$GITUSER:/{s/^$GITUSER:(.*)/\1/;p}" "$REPO"/.users)
27     case $RIGHT in
28       admin|user)
29         # admin and regular user have the same right on the git repo
30         rungitcmd
31         ;;
32       readonly)
33         if [ "$CMD" = "$PUSHCMD" ]; then
34           # push command is forbidden to readonly users
35           error "$GITUSER not authorized to push on $REPO"
36         else
37           # other commands are allowed
38           rungitcmd
39         fi
40         ;;
41       *)
42         error "$GITUSER has the unexpected right $RIGHT"
43     esac
44   else
45     if [ "$CMD" != "$PUSHCMD" ] && [ -e "$REPO"/git-daemon-export-ok ]; then
46       rungitcmd
47     else
48       # not a member and the command is push or
49       # not a member and the repo is not public
50       error "$GITUSER not authorized on $REPO"
51     fi
52   fi
53 else
54   # will fail as the repo does not exist
55   rungitcmd
56 fi