From f789f9ed8bf17e374cb079eae5a5639885b90ae6 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= <git@ameliathe1st.gay>
Date: Thu, 5 Dec 2024 00:41:45 +0100
Subject: [PATCH] Prepare to add pushing (protected by gnunet-identity)

---
 git-remote-gnunet |  3 ++-
 handle-request.sh | 27 +++++++++++++++++++--------
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/git-remote-gnunet b/git-remote-gnunet
index 796a88c..edccd49 100755
--- a/git-remote-gnunet
+++ b/git-remote-gnunet
@@ -14,6 +14,7 @@ if test "$?" != "0"; then
 	echo >&2 "URI format: \`[gnunet://][user@]gns.name[/path/to/repo]'."
 	exit 1
 fi
+pkey="$(gnunet-identity -qde "$username")"
 
 while read -r cmd arg; do
 	case "$cmd" in
@@ -42,4 +43,4 @@ echo >&2 "Trying service \`$service' on peer \`$peer'."
 ip="$(gnunet-vpn -s "$service" -p "$peer" -t)"
 echo >&2 "Using tunnel ip \`$ip'."
 
-{ printf '%s\n' "$path" "$arg"; cat; } | socat stdio tcp4:"$ip":9418
+{ printf '%s\n' "$path" "$arg" "$pkey"; cat; } | socat stdio tcp4:"$ip":9418
diff --git a/handle-request.sh b/handle-request.sh
index 743a99e..4894910 100755
--- a/handle-request.sh
+++ b/handle-request.sh
@@ -8,20 +8,31 @@ base_path="$1"
 
 read -r path || { echo "No path specified." | tee /dev/stderr; exit 1; }
 read -r comm || { echo "No command specified." | tee /dev/stderr; exit 1; }
+read -r pkey || { echo "No pkey specified." | tee /dev/stderr; exit 1; }
 
 full_path="$(realpath -sm "$base_path$path")"
 case "$full_path" in
-	"$base_path") ;;
-	"$base_path/"*) ;;
-	*)	echo "Invalid path: \`$full_path'." | tee /dev/stderr
-		exit 1
-		;;
+"$base_path") ;;
+"$base_path/"*) ;;
+*)	echo "Invalid path: \`$full_path'." | tee /dev/stderr
+	exit 1
+	;;
 esac
 
-if test "$comm" != "git-upload-pack"; then
-	echo "Command \`$comm' has to be git-upload-pack." | tee /dev/stderr
+case "$comm" in
+git-upload-pack)
+	# No pkey checking.
+	;;
+git-receive-pack)
+	# Accept any valid pkey. FIXME
+	echo "Command \`$comm' not supported yet." | tee /dev/stderr
 	exit 1
-fi
+	;;
+*)
+	echo "Unknown command: \`$comm'." | tee /dev/stderr
+	exit 1
+	;;
+esac
 
 git_dir="$(git -C "$full_path" rev-parse --path-format=absolute --git-dir)" || { echo "Not a git repository: \`$full_path'." | tee /dev/stderr; exit 1; }
 
-- 
2.46.0