2023-11-13 14:33:28 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2024-07-09 17:42:07 +02:00
|
|
|
# SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com>
|
2023-11-13 14:33:28 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
die(){
|
2023-11-21 14:57:47 +00:00
|
|
|
echo "error: ${1}" >&2
|
2023-11-13 14:33:28 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if ! command -v git >/dev/null; then
|
|
|
|
die "Command not found: git"
|
|
|
|
fi
|
|
|
|
|
2024-06-25 22:16:26 +02:00
|
|
|
## TODO: subdirectory? dir+repo
|
2023-11-21 14:57:47 +00:00
|
|
|
untrusted_repo="${QREXEC_SERVICE_ARGUMENT}"
|
|
|
|
|
|
|
|
if test -z "${untrusted_repo}"; then
|
|
|
|
die "Repository name is empty"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! (echo "${untrusted_repo}" | grep -q "^[A-Za-z0-9][A-Za-z0-9_.-]\+$")
|
|
|
|
then
|
2024-07-09 17:42:07 +02:00
|
|
|
msg="Forbidden characters in agent name."
|
|
|
|
msg="${msg} Allowed chars: letters, numbers, hyphen, underscore and dot."
|
|
|
|
msg="${msg} Name cannot begin with hyphen, underscore or dot"
|
|
|
|
die "${msg}"
|
2023-11-21 14:57:47 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
## Length arbitrarily set.
|
|
|
|
if test "${#untrusted_repo}" -gt 128; then
|
|
|
|
die "Repository name is too long: ${#untrusted_repo}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
base_path="$HOME/src"
|
|
|
|
repo="${untrusted_repo}"
|
|
|
|
|
|
|
|
case "${repo}" in
|
2023-11-13 14:33:28 +00:00
|
|
|
*".git") ;;
|
2023-11-21 14:57:47 +00:00
|
|
|
*) repo="${repo}.git";;
|
2023-11-13 14:33:28 +00:00
|
|
|
esac
|
|
|
|
|
2023-11-21 14:57:47 +00:00
|
|
|
path="${base_path}/${repo}"
|
2023-11-13 14:33:28 +00:00
|
|
|
action="${0##*.Git}"
|
|
|
|
|
2023-11-21 14:57:47 +00:00
|
|
|
case "${action}" in
|
2023-11-13 14:33:28 +00:00
|
|
|
Fetch) service=git-upload-pack;;
|
|
|
|
Push) service=git-receive-pack;;
|
|
|
|
Init) service="git init --bare";;
|
|
|
|
*) die "Invalid RPC name: ${0##*/}";;
|
|
|
|
esac
|
|
|
|
|
2023-11-21 14:57:47 +00:00
|
|
|
if test "${action}" != "Init"; then
|
|
|
|
test -d "${path}" || die "Directory doesn't exist: ${repo}"
|
2024-07-09 17:42:07 +02:00
|
|
|
git -C "${path}" rev-parse >/dev/null 2>&1 ||
|
|
|
|
die "Not a git repository: ${repo}"
|
2023-11-21 14:57:47 +00:00
|
|
|
is_bare="$(git -C "${path}" rev-parse --is-bare-repository)"
|
|
|
|
test "${is_bare}" = "true" || die "Not a bare repository: ${repo}"
|
2023-11-13 14:33:28 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-21 14:57:47 +00:00
|
|
|
if ! test -d "${base_path}"; then
|
2023-11-13 14:33:28 +00:00
|
|
|
# shellcheck disable=SC2174
|
2023-11-21 14:57:47 +00:00
|
|
|
mkdir -m 0700 -p "${base_path}" >/dev/null 2>&1 ||
|
|
|
|
die "Cannot create directory: ${base_path}"
|
2023-11-13 14:33:28 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# shellcheck disable=SC2086
|
2023-11-21 14:57:47 +00:00
|
|
|
exec ${service} -- "${path}"
|