From 18cb26fc56f3384564491dc155c104639695d550 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 9 Nov 2013 22:34:10 +0000 Subject: [PATCH] git-cache-proxy: more wip, copyright notice, before housekeeping etc. --- scripts/git-cache-proxy | 111 ++++++++++++++++++++++++++-------------- scripts/git-daemon.in | 87 ------------------------------- scripts/git-service.in | 51 ------------------ 3 files changed, 73 insertions(+), 176 deletions(-) delete mode 100755 scripts/git-daemon.in delete mode 100755 scripts/git-service.in diff --git a/scripts/git-cache-proxy b/scripts/git-cache-proxy index b3cf412..df522a0 100755 --- a/scripts/git-cache-proxy +++ b/scripts/git-cache-proxy @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # # git caching proxy -# + # usage: run it on some port, and then clone or fetch # "git://:/[ ]" # where is http:///... or git:///... @@ -14,6 +14,25 @@ # fetch=try use what is in the cache if the fetch/clone fails # timeout= length of time to allow for fetch/clone +# git-cache-proxy is free software; you can redistribute it and/or +# modify them under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3, or (at +# your option) any later version. +# +# git-cache-proxy is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, consult the Free Software Foundation's +# website at www.fsf.org, or the GNU Project website at www.gnu.org. +# +# (Some code taken from userv-utils's git-daemon.in and git-service.in +# which were written by Tony Finch and subsequently +# heavily modified by Ian Jackson +# and were released under CC0 1.0. The whole program is now GPLv3+.) + use strict; use warnings; @@ -23,60 +42,70 @@ use Sys::Syslog; use Fcntl qw(:flock SEEK_SET); our $us = 'git-cache-proxy'; + +#---------- error handling and logging ---------- + +# This is a bit fiddly, because we want to catch errors sent to stderr +# and dump them to syslog if we can, but only if we are running as an +# inetd service. + our $log; # filehandle (ref), or "1" meaning syslog -BEGIN { - open STDERR, ">/dev/null" or exit 255; - open TEMPERR, "+>", undef or exit 255; - open STDERR, ">&TEMPERR" or exit 255; +sub ntoa { + my $sockaddr = shift; + return ('(local)') unless defined $sockaddr; + my ($port,$addr) = sockaddr_in $sockaddr; + $addr = inet_ntoa $addr; + return ("[$addr]:$port",$addr,$port); +} - sub ntoa { - my $sockaddr = shift; - return ('(local)') unless defined $sockaddr; - my ($port,$addr) = sockaddr_in $sockaddr; - $addr = inet_ntoa $addr; - return ("[$addr]:$port",$addr,$port); - } +our ($client) = ntoa getpeername STDIN; +our ($server) = ntoa getsockname STDIN; - our ($client,$client_addr,$client_port) = ntoa getpeername STDIN; - our ($server,$server_addr,$server_port) = ntoa getsockname STDIN; +sub ensurelog () { + return if $log; + openlog $us, qw(pid), 'daemon'; + $log = 1; +} - sub ensurelog () { - return if $log; - openlog $us, qw(pid), 'daemon'; - $log = 1; +sub logm ($$) { + my ($pri, $msg) = @_; + if ($client eq '(local)') { + print STDERR "$us: $pri: $msg\n" or die $!; + exit 1; } - - sub logm ($$) { - my ($pri, $msg) = @_; - ensurelog(); - my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg; - if (ref $log) { - my $wholemsg = sprintf("%s [%d] %s: %s\n", - strftime("%Y-%m-%d %H:%M:%S Z", gmtime), - $$, - $pri, - $mainmsg); - print $log $wholemsg; - } else { - syslog $pri, $mainmsg; - } + ensurelog(); + my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg; + if (ref $log) { + my $wholemsg = sprintf("%s [%d] %s: %s\n", + strftime("%Y-%m-%d %H:%M:%S Z", gmtime), + $$, + $pri, + $mainmsg); + print $log $wholemsg; + } else { + syslog $pri, $mainmsg; } +} + +if ($client ne '(local)') { + open STDERR, ">/dev/null" or exit 255; + open TEMPERR, "+>", undef or exit 255; + open STDERR, ">&TEMPERR" or exit 255; +} - END { +END { + if ($client ne '(local)') { if ($?) { logm 'crit', "crashing ($?)"; } seek TEMPERR, 0, SEEK_SET; while () { chomp; logm 'crit', $_; } - exit $?; } + exit $?; } -our $fetchtimeout = 1800; -our $maxfetchtimeout = 3600; - sub fail ($) { my ($msg) = @_; logm 'err', $msg; @@ -95,6 +124,10 @@ sub gitfail ($) { exit 0; } +#---------- argument parsing ---------- + +our $fetchtimeout = 1800; +our $maxfetchtimeout = 3600; our $cachedir = '/var/cache/git-cache-proxy'; for (;;) { @@ -119,6 +152,8 @@ for (;;) { !@ARGV or fail "bad usage: no non-option arguments permitted"; +#---------- main program ---------- + chdir $cachedir or fail "chdir $cachedir: $!"; our ($service,$specpath,$spechost); diff --git a/scripts/git-daemon.in b/scripts/git-daemon.in deleted file mode 100755 index 1b1b540..0000000 --- a/scripts/git-daemon.in +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/perl -# -# A git daemon with an added userv security boundary. -# -# This was written by Tony Finch and subsequently -# heavily modified by Ian Jackson -# http://creativecommons.org/publicdomain/zero/1.0/ - -use strict; -use warnings; - -use POSIX; -use Socket; -use Sys::Syslog; - -BEGIN { - if ($ARGV[0] =~ s/^-L//) { - my $logfile= shift @ARGV; - open STDERR, ">> $logfile" or die $!; - } -} - -sub ntoa { - my $sockaddr = shift; - return ('(local)') unless defined $sockaddr; - my ($port,$addr) = sockaddr_in $sockaddr; - $addr = inet_ntoa $addr; - return ("[$addr]:$port",$addr,$port); -} -our ($client,$client_addr,$client_port) = ntoa getpeername STDIN; -our ($server,$server_addr,$server_port) = ntoa getsockname STDIN; -our ($service,$specpath,$spechost); - -printf STDERR "%s [$$] %s %s\n", - strftime("%Y-%m-%d %H:%M:%S %Z", localtime), $server, $client; - -openlog 'userv-git-daemon', 'pid', 'daemon'; -sub fail { syslog 'err', "$client @_"; exit } - -$SIG{ALRM} = sub { fail "timeout" }; -alarm 30; - -sub xread { - my $length = shift; - my $buffer = ""; - while ($length > length $buffer) { - my $ret = sysread STDIN, $buffer, $length, length $buffer; - fail "Expected $length bytes, got ".length $buffer - if defined $ret and $ret == 0; - fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN; - } - return $buffer; -} -my $hex_len = xread 4; -fail "Bad hex in packet length" unless $hex_len =~ m|^[0-9a-fA-F]{4}$|; -my $line = xread -4 + hex $hex_len; -unless (($service,$specpath,$spechost) = $line =~ - m|^(git-[a-z-]+) /*([!-~]+)\0host=([!-~]+)\0$|) { - $line =~ s|[^ -~]+| |g; - fail "Could not parse \"$line\"" -} - -@@READ_URLMAP@@ - -fail "No global mapping for $uri" unless defined $serve_user; - -my ($hn,$ha,$at,$naddrs,@addrs) = gethostbyname $spechost; -fail "hostname/address mismatch ($spechost $server_addr)" unless grep { - $server_addr eq inet_ntoa $_ - } @addrs; - -our @opts; - -push @opts, "-D$_=${$::{$_}}" - for qw(service specpath spechost - client client_addr client_port - server server_addr server_port); - -fail "no user $serve_user" unless getpwnam($serve_user); - -syslog 'notice', "$client $service $uri $serve_user"; - -my @cmd = ('userv', '-t300', @opts, $serve_user, $service); -no warnings; # suppress errors to stderr -exec @cmd or fail "exec userv: $!"; - -# end diff --git a/scripts/git-service.in b/scripts/git-service.in deleted file mode 100755 index 2b8aff3..0000000 --- a/scripts/git-service.in +++ /dev/null @@ -1,51 +0,0 @@ -#!/usr/bin/perl -# -# userv-git-daemon service script -# -# This was written by Tony Finch and subsequently -# heavily modified by Ian Jackson -# http://creativecommons.org/publicdomain/zero/1.0/ - -use strict; -use warnings; - -use POSIX; -use Sys::Syslog; - -our ($client,$service,$specpath,$spechost,@opts); - -${$::{$_}} = $ENV{"USERV_U_$_"} - for qw(service specpath spechost client); - -openlog "userv-$service:$ENV{USER}", 'pid', 'daemon'; -sub fail { syslog 'err', "$client @_"; exit } - -@@READ_URLMAP@@ - -fail "No user $ENV{USER} mapping for $uri" unless defined $serve_user; - -$serve_dir = "$ENV{HOME}/$serve_dir" unless $serve_dir =~ m|^/|; - -if (length $serve_repo) { - my $inspect= $serve_repo; - $inspect =~ s,^/,,; - fail "Bad subdirectory $serve_repo" unless $inspect =~ m/$repo_regexp/o; - fail "bad config - repo-regexp does not capture" unless defined $1; - $serve_repo= "/$1"; -} - -my $dir = $serve_dir.$serve_repo; - -my $path = $check_export ? "$dir/git-daemon-export-ok" : $dir; -fail "$! $path" unless -e $path; - -syslog 'notice', "$client $uri $dir"; - -@opts = qw( --strict ) - if @opts == 0 and $service eq 'git-upload-pack'; - -my @cmd = ($service =~ m|^(git)-(.*)$|, @opts, $dir); -no warnings; # suppress errors to stderr -exec @cmd or fail "exec $service: $!"; - -# end -- 2.30.2