-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsshd_force_command.sh
37 lines (36 loc) · 1.36 KB
/
sshd_force_command.sh
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
#!/usr/bin/env bash
# Copyright 2024 Louis Royer. All rights reserved.
# Use of this source code is governed by a MIT-style license that can be
# found in the LICENSE file.
# SPDX-License-Identifier: MIT
set -e
umask 0077
SSH_ORIGINAL_COMMAND=$1
VOLUME_GIT=$2
read -ra arr <<< "${SSH_ORIGINAL_COMMAND}"
if [ -z "$SSH_ORIGINAL_COMMAND" ]; then
git-shell
elif [ ${#arr[@]} -eq 2 ] && [[ "${arr[0]}" == "git-upload-pack" || "${arr[0]}" == "git-receive-pack" || "${arr[0]}" == "git-upload-archive" ]]; then
if [[ "${arr[1]}" =~ .*".git'" ]]; then
arr[1]="${arr[1]:0:1}${VOLUME_GIT}/${arr[1]:1:-1}${arr[1]:${#arr[1]}-1}"
else
arr[1]="${arr[1]:0:1}${VOLUME_GIT}/${arr[1]:1:-1}.git${arr[1]:${#arr[1]}-1}"
fi
if [ "${arr[0]}" == "git-upload-pack" ]; then
# check repo exists
if [ "$(find "$(dirname "${arr[1]:1:-1}")" -type d -iname "$(basename "${arr[1]:1:-1}")" 2> /dev/null| wc -l)" -eq 0 ]; then
if [[ $(dirname "${arr[1]:1:-1}") =~ .*".git/".* ]]; then
echo "remote: error: Forbidden path." > /dev/stderr
exit 1
fi
# push to create
echo "remote: This repository does not exists yet. Initialization…" > /dev/stderr
mkdir -p "$(dirname "${arr[1]:1:-1}")"
git -C "$(dirname "${arr[1]:1:-1}")" init --quiet --bare "$(basename "${arr[1]:1:-1}")"
echo "done." > /dev/stderr
fi
fi
git-shell -c "${arr[*]}"
else
git-shell -c "${SSH_ORIGINAL_COMMAND}"
fi