aboutsummaryrefslogtreecommitdiffstats
path: root/git-remote-gcrypt
blob: 49fed5fbd90c81192e46c264ea368c8265c1eb6f (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#!/bin/sh

# git-remote-gcrypt
# Copyright 2013  by Ulrik
# License: GPLv2 or any later version, see http://www.gnu.org/licenses/
#
# See README

#set -x
set -e

Did_find_repo=  # yes for connected, no for no repo
Localdir="${GIT_DIR:=.git}/remote-gcrypt"
export GITCEPTION="${GITCEPTION:-}+" # Reuse $Gref except when stacked
Gref="refs/gcrypt/gitception$GITCEPTION"
Gref_rbranch="refs/heads/master"
Repoid=
Hashpfx=":SHA224:"
UrlTag="G."
Packpfx="pack $Hashpfx"
Packkey_bytes=33

Branchlist=
Packlist=
Extension_list=

# compat/utility functions
xecho()
{
	cat <<EOF
$@
EOF
}
xecho_n() { xecho "$@" | tr -d \\n ; } # kill newlines
echo_git() { xecho "$@" ; }  # Code clarity
echo_info() { xecho "gcrypt:" "$@" >&2; }
echo_die() { echo_info "$@" ; exit 1; }

isnull() { case "${1}" in "") true;; *) false;; esac; }
isnonnull() { ! isnull "$1"; }
iseq() { isnull "${1#"$2"}"; }
isnoteq() { ! iseq "$@"; }

# Append $2 to $1 with a newline separator
append() { isnull "$1" || xecho "$1" && xecho "$2"; }
isurl() { isnull "${2%%$1://*}"; }

xgrep() { command grep "$@" || : ; }
sort_C() { LC_ALL=C command sort "$@"; }
sort_stable_k2()
{
	awk '{ printf("%08d\t%s\n", NR, $0) }' | sort_C -k 3,3 -k 1,1 |cut -f 2-
}

tac() { sed '1!G;h;$!d'; }

# Split $1 into $prefix_:$suffix_
splitcolon()
{
	prefix_=${1%%:*}
	suffix_=${1#*:}
}
repoidstr() { xecho "repo $Hashpfx$Repoid 1"; }

## gitception part
# Fetch giturl $1, file $2
gitception_get()
{
	# Take care to preserve FETCH_HEAD
	local ret_=: obj_id= f_head="$GIT_DIR/FETCH_HEAD"
	[ -e "$f_head" ] && command mv -f "$f_head" "$f_head.$$~" || :
	git fetch -q -f "$1" "$Gref_rbranch:$Gref" 2>/dev/tty >/dev/null &&
		obj_id="$(git ls-tree "$Gref" | xgrep -E '\b'"$2"'$' | awk '{print $3}')" &&
		isnonnull "$obj_id" && git cat-file blob "$obj_id" && ret_=: ||
		{ ret_=false && : ; }
	[ -e "$f_head.$$~" ] && command mv -f "$f_head.$$~" "$f_head" || :
	$ret_
}

anon_commit()
{
	GIT_AUTHOR_NAME="root" GIT_AUTHOR_EMAIL="root@localhost" \
	GIT_AUTHOR_DATE="1356994801 -0400" GIT_COMMITTER_NAME="root" \
	GIT_COMMITTER_EMAIL="root@localhost" \
	GIT_COMMITTER_DATE="1356994801 -0400" \
		git commit-tree -m "Initial commit" "$@"
}

# Get 'tree' from $1, change file $2 to obj id $3
update_tree()
{
	local tab_="	"
	# $2 is a filename from the repo format
	(git ls-tree "$1" | xgrep -v -E '\b'"$2"'$';
		xecho "100644 blob $3$tab_$2") | git mktree
}

# Put giturl $1, file $2
# depends on previous GET to set $Gref and depends on PUT_FINAL later
gitception_put()
{
	local obj_id= tree_id= commit_id=
	obj_id=$(git hash-object -w --stdin) &&
		tree_id=$(update_tree "$Gref" "$2" "$obj_id") &&
		commit_id=$(anon_commit "$tree_id") &&
		git update-ref "$Gref" "$commit_id"
}

gitception_new_repo()
{
	local empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
	# get any file to update Gref, and if it's not updated we create empty
	git update-ref -d "$Gref" || :
	gitception_get "$1" "x" || :
	git rev-parse -q --verify "$Gref" >/dev/null && return 0 ||
		commit_id=$(anon_commit "$empty_tree") &&
		git update-ref "$Gref" "$commit_id"
}
## end gitception

# Fetch repo $1, file $2
GET()
{
	if isurl ssh "$1"
	then
		splitcolon "${1#ssh://}"
		(exec 0>&-; ssh "$prefix_" "cat $suffix_/$2")
	elif isurl sftp "$1"
	then
		(exec 0>&-; curl -s -S -k "$1/$2")
	elif isurl gitception "$1"
	then
		gitception_get "${1#gitception://}" "$2"
	else
		cat "$1/$2"
	fi
}

# Put repo $1, file $2 or fail
PUT()
{
	if isurl ssh "$1"
	then
		splitcolon "${1#ssh://}"
		ssh "$prefix_" "cat > $suffix_/$2"
	elif isurl sftp "$1"
	then
		curl -s -S -k --ftp-create-dirs -T - "$1/$2"
	elif isurl gitception "$1"
	then
		gitception_put "${1#gitception://}" "$2"
	else
		cat > "$1/$2"
	fi
}

# Put all PUT changes for repo $1 at once
PUT_FINAL()
{
	if isurl gitception "$1"
	then
		git push --quiet -f "${1#gitception://}" "$Gref:$Gref_rbranch"
	else
		:
	fi
}

# Put directory for repo $1
PUTREPO()
{
	if isurl ssh "$1"
	then
		splitcolon "${1#ssh://}"
		(exec 0>&- ; ssh "$prefix_" "mkdir -p $suffix_")
	elif isurl sftp "$1"
	then
		:
	elif isurl gitception "$1"
	then
		gitception_new_repo "${1#gitception://}"
	else
		mkdir -p "$1"
	fi
}

CLEAN_FINAL()
{
	isurl gitception "$1" && git update-ref -d "$Gref" || :
}

addsignkeyparam()
{
	if isnull "$Conf_signkey"; then
		"$@"
	else
		"$@" -u "$Conf_signkey"
	fi
}


ENCRYPT()
{
	gpg --batch --force-mdc --compress-algo none --passphrase-fd 3 -c 3<<EOF
$1
EOF
}

DECRYPT()
{
	gpg -q --batch --no-default-keyring --secret-keyring /dev/null \
		--keyring /dev/null --passphrase-fd 3 -d  3<<EOF
$1
EOF
}

# Encrypt to recipients $1
PRIVENCRYPT()
{
	addsignkeyparam gpg --no-default-keyring --keyring "$Conf_keyring" \
		--compress-algo none -se $1
}

PRIVDECRYPT()
{
	local status_=
	exec 4>&1 &&
	status_=$(gpg --no-default-keyring --keyring "$Conf_keyring" \
		--status-fd 3 -q -d 3>&1 1>&4) &&
	xecho "$status_" | grep "^\[GNUPG:\] ENC_TO " >/dev/null &&
	(xecho "$status_" | grep "^\[GNUPG:\] GOODSIG " >/dev/null || {
		echo_info "Failed to verify manifest signature!" && return 1
	})
}

# Generate $1 random bytes
genkey()
{
	gpg --armor --gen-rand 1 "$1"
}

pack_hash()
{
	local hash_=
	hash_=$(gpg --with-colons --print-md SHA224 | tr A-F a-f)
	hash_=${hash_#:*:}
	xecho "${hash_%:}"
}

# Pass the branch/ref by pipe to git
safe_git_rev_parse()
{
	local out_=
	out_=$(xecho "$1" | git cat-file --batch-check)
	out_=${out_%%*missing}
	xecho "${out_%% *}"
}

check_recipients()
{
	# Find which keys in the keyring we can encrypt to
	Recipients=$(gpg --no-default-keyring --keyring "$Conf_keyring" \
		--with-colons --fast-list -k | xgrep ^pub | \
		while read rc_line; do
			cap_=$(xecho "$rc_line" | cut -f 12 -d :)
			keyid_=$(xecho "$rc_line" | cut -f 5 -d :)
			iseq "${cap_#*E}" "$cap_" || xecho_n "-R $keyid_ "
		done)
	if isnull "$Recipients"
	then
		echo_info "You must configure a keyring for the repository."
		echo_info "Use ::"
		echo_info "  gpg --export KEYID1 > <path-to-keyring>"
		echo_info "  git config gcrypt.keyring <path-to-keyring>"
		exit 1
	fi
}

make_new_repo()
{
	local urlid_= fix_config=
	echo_info "Setting up new repository at $URL"
	PUTREPO "$URL"

	# We need a relatively short ID for URL+REPO
	# The manifest will be stored at SHA224(urlid_)
	# Needed assumption: the same user should have no duplicate urlid_
	# For now, we arbitrarily use 9 random bytes (72 bits)
	urlid_=$(genkey 9 | tr '/+' 'Zz')
	Repoid=$(xecho_n "$urlid_" | pack_hash)
	echo_info "Repository ID  is" "$urlid_"
	iseq "${NAME#gcrypt::}" "$URL" || {
		git config "remote.$NAME.url" "gcrypt::$URL/$UrlTag$urlid_"
		fix_config=1
	}
	echo_info "Repository URL is" "gcrypt::$URL/$UrlTag$urlid_"
	Extension_list=$(xecho "extn comment")
	isnull "$fix_config" || echo_info "(configuration for $NAME updated)"
}


read_config()
{
	Conf_keyring=$(git config --path gcrypt.keyring || xecho "/dev/null")
	Conf_signkey=$(git config --path user.signingkey || :)
}

ensure_connected()
{
	local manifest_= rcv_repoid= url_id=

	if isnonnull "$Did_find_repo"
	then
		return
	fi
	Did_find_repo=no
	read_config

	# split out Repoid from URL
	url_id=${URL##*/"$UrlTag"}
	isnoteq "$url_id" "$URL" || return 0

	URL=${URL%/"$UrlTag$url_id"}
	Repoid=$(xecho_n "$url_id" | pack_hash)

	TmpManifest_Enc="$Localdir/manifest.$$"
	trap 'rm -f "$TmpManifest_Enc"' EXIT
	GET "$URL" "$Repoid" 2>/dev/null > "$TmpManifest_Enc" ||
		echo_die "Repository not found: $url_id at $URL"

	Did_find_repo=yes
	echo_info "Decrypting manifest"
	manifest_=$(PRIVDECRYPT < "$TmpManifest_Enc") && 
		isnonnull "$manifest_" || {
		echo_info "Failed to decrypt manifest!"
		echo_info "Using keyring $Conf_keyring"
		if iseq "$Conf_keyring" "/dev/null"
		then
			echo_info "NOTE: Please configure gcrypt.keyring"
		fi
		exit 1
	}
	rm -f "$TmpManifest_Enc"
	trap - EXIT

	Branchlist=$(xecho "$manifest_" | xgrep -E '^[0-9a-f]{40} ')
	Packlist=$(xecho "$manifest_" | xgrep "^$Packpfx")
	Extension_list=$(xecho "$manifest_" | xgrep "^extn ")
	rcv_repoid=$(xecho "$manifest_" | xgrep "^repo ")
	iseq "$(repoidstr)" "$rcv_repoid" || echo_die "Repository id mismatch!"
}

do_capabilities()
{
	echo_git fetch
	echo_git push
	echo_git
}

do_list()
{
	local obj_id= ref_name= line_=
	ensure_connected

	xecho "$Branchlist" | while read line_
	do
		isnonnull "$line_" || break
		obj_id=${line_%% *}
		ref_name=${line_##* }
		echo_git "$obj_id" "$ref_name"
		if iseq "$ref_name" "refs/heads/master"
		then
			echo_git "@refs/heads/master HEAD"
		fi
	done

	# end with blank line
	echo_git
}

do_fetch()
{
	# Security protocol:
	# The PACK id is the hash of the encrypted git packfile.
	# We only download packs mentioned in the encrypted manifest,
	# and check their digest when received.
	local pack_= rcv_id= packline_= pneed_= pboth_= phave_= premote_= key_=

	ensure_connected

	if isnull "$Packlist"
	then
		echo_git # end with blank line
		return
	fi

	TmpPack_Encrypted="$Localdir/tmp_pack_ENCRYPTED_.$$"
	trap 'rm -f "$TmpPack_Encrypted"' EXIT

	premote_=$(xecho "$Packlist" | cut -f 1-2 -d ' ')
	# Needed packs is  Packlist - (phave & Packlist)
	# The `+` for $GITCEPTION is pointless but we will be safe for stacking
	phave_="$(cat "$Localdir/have_packs+" 2>/dev/null || :)"
	pboth_="$( (xecho "$premote_"; xecho "$phave_") | sort_C | uniq -d)"
	pneed_="$( (xecho "$premote_"; xecho "$pboth_") | sort_C | uniq -u)"

	xecho "$pneed_" | while read packline_
	do
		isnonnull "$packline_" || continue
		pack_=${packline_#"$Packpfx"}
		rcv_id="$(GET "$URL" "$pack_" | \
			tee "$TmpPack_Encrypted" | pack_hash)"
		if isnoteq "$rcv_id" "$pack_"
		then
			echo_die "Packfile $pack_ does not match digest!"
		fi
		key_=$(xecho "$Packlist" | grep "$pack_" | cut -f 3 -d ' ')
		DECRYPT "$key_" < "$TmpPack_Encrypted" |
			git index-pack -v --stdin >/dev/null
		# add to local pack list
		xecho "$Packpfx$pack_" >> "$Localdir/have_packs$GITCEPTION"
	done

	rm -f "$TmpPack_Encrypted"
	trap - EXIT
	echo_git # end with blank line
}

# do_push PUSHARGS (multiple lines like +src:dst, with both + and src opt.)
do_push()
{
	# Security protocol:
	# Each git packfile is encrypted and then named for the encrypted
	# file's hash. The manifest is updated with the pack id.
	# The manifest is encrypted.
	local remote_has= remote_want= prefix_= suffix_= line_= pack_id= key_=

	ensure_connected
	check_recipients

	if iseq "$Did_find_repo" "no"
	then
		make_new_repo
	fi

	trap 'rm -f "$TmpPack_Encrypted" "$TmpObjlist"' EXIT
	if isnonnull "$Branchlist"
	then
		remote_has=$(xecho "$Branchlist" |
			cut -f 1 -d ' ' | sed -e 's/^\(.\)/^&/')
	fi

	while read line_ # from <<
	do
		# +src:dst -- remove leading + then split at :
		splitcolon "${line_#+}"
		if isnonnull "$prefix_"
		then
			remote_want=$(append "$remote_want" "$prefix_")
			Branchlist=$(append "$Branchlist" \
				"$(safe_git_rev_parse "$prefix_") $suffix_")
		else
			:  # FIXME delete branch
		fi
	done <<EOF
$1
EOF

	Branchlist=$(xecho "$Branchlist" | sort_stable_k2 | tac | uniq -s 40)

	TmpPack_Encrypted="$Localdir/tmp_pack_ENCRYPTED_.$$"
	TmpObjlist="$Localdir/tmp_packrevlist.$$"
	key_=$(genkey "$Packkey_bytes")

	append "$remote_has" "$remote_want" |
		git rev-list --objects --stdin -- |
		tee "$TmpObjlist" |
		git pack-objects --stdout | ENCRYPT "$key_">"$TmpPack_Encrypted"
	# Only send pack if we have any objects to send
	if [ -s "$TmpObjlist" ]
	then
		pack_id=$(pack_hash < "$TmpPack_Encrypted")
		Packlist=$(append "$Packlist" "$Packpfx$pack_id $key_")
		PUT "$URL" "$pack_id" < "$TmpPack_Encrypted"
	fi

	rm -f "$TmpPack_Encrypted"
	rm -f "$TmpObjlist"
	trap - EXIT

	# Update manifest
	echo_info "Encrypting manifest to \"$Recipients\""
	echo_info "Requesting manifest signature"

	TmpManifest_Enc="$Localdir/manifest.$$"
	trap 'rm -f "$TmpManifest_Enc"' EXIT

	(xecho "$Branchlist"; xecho "$Packlist";
	repoidstr; xecho "$Extension_list") |
		PRIVENCRYPT "$Recipients" > "$TmpManifest_Enc"

	PUT "$URL" "$Repoid" < "$TmpManifest_Enc"

	PUT_FINAL "$URL"

	rm -f "$TmpManifest_Enc"
	trap - EXIT

	# ok all updates (not deletes)
	xecho "$1" | while read line_
	do
		# +src:dst -- remove leading + then split at :
		splitcolon "${line_#+}"
		if isnull "$prefix_"
		then
			echo_git "error $suffix_ delete not supported yet"
		else
			echo_git "ok $suffix_"
		fi
	done
	
	echo_git 
}

# Main program, check $URL is supported
NAME=$1
URL=$2
( isurl ssh "$URL" || isurl sftp "$URL" ||
	isurl gitception "$URL" || isnull "${URL##/*}" ) ||
	echo_die "Supported URLs: gitception://<giturl>, Absolute path, sftp://, ssh://"

mkdir -p "$Localdir"

while read Input
do
	#echo_info "Got: $Input ($GITCEPTION)"
	case "$Input" in
	capabilities)
		do_capabilities
		;;
	list|list\ for-push)
		do_list
		;;
	fetch\ *)
		args_="${Input##fetch }"
		while read InputX
		do
			case "$InputX" in
			fetch*)
			args_= #ignored
			;;
			*)
			break
			;;
			esac
		done
		do_fetch "$args_"
		;;
	push\ *)
		args_="${Input##push }"
		while read InputX
		do
			#echo_info "Got: (for push) $InputX"
			case "$InputX" in
			push\ *)
			args_=$(append "$args_" "${InputX#push }")
			;;
			*)
			break
			;;
			esac
		done
		do_push "$args_"
		;;
	?*)
		echo_die "Unknown input!"
		;;
	*)
		#echo_info "Blank line, we are done"
		CLEAN_FINAL "$URL"
		exit 0
		;;
	esac
done