chiark / gitweb /
Bump version to 7.0.1~iwj0
[chiark-utils.git] / scripts / git-cache-proxy
1 #!/usr/bin/perl -w
2 #
3 # git caching proxy
4
5 # Suitable only for exposing to semi-trusted clients: clients are not
6 # supposed to be able to take over the server.  However, clients can
7 # probably deny service to each other because the current
8 # implementation is not very good at handling various out-of-course
9 # situations (notably, clients which are too slow).
10
11 # usage: run it on some port, and then clone or fetch
12 #  "git://<realhost>:<realport>/<real-git-url>[ <options>]"
13 # where <real-git-url> is http://<host>/... or git://<host>/...
14 # and <options> is zero or more (whitespace-separated) of
15 #    [<some-option>]      will be ignored if not recognised
16 #    {<some-option>}      error if not recognised
17 # options currently known:
18 #    fetch=must           fail if the fetch/clone from upstream fails
19 #    fetch=no             just use what is in the cache
20 #    fetch=try            use what is in the cache if the fetch/clone fails
21 #    timeout=<seconds>    length of time to allow for fetch/clone
22 #    housekeeping-interval-days=<integer>  } housekeeping tuning parameters
23 #    tree-expire-days=<integer>            }
24 #    gc-interval-days=<integer>            }
25
26 # example inetd.conf line:
27 #  9419 stream tcp nowait git-cache /usr/bin/git-cache-proxy git-cache-proxy
28 # you'll need to 
29 #  adduser git-cache
30 #  mkdir /var/cache/git-cache-proxy
31 #  chown git-cache /var/cache/git-cache-proxy
32
33 # git-cache-proxy
34 # Copyright 2010 Tony Finch
35 # Copyright 2013,2014 Ian Jackson
36 # Copyright 2017 Citrix
37
38 # git-cache-proxy is free software; you can redistribute it and/or
39 # modify them under the terms of the GNU General Public License as
40 # published by the Free Software Foundation; either version 3, or (at
41 # your option) any later version.
42 #
43 # git-cache-proxy is distributed in the hope that it will be useful,
44 # but WITHOUT ANY WARRANTY; without even the implied warranty of
45 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
46 # General Public License for more details.
47
48 # You should have received a copy of the GNU General Public License along
49 # with this program; if not, consult the Free Software Foundation's
50 # website at www.fsf.org, or the GNU Project website at www.gnu.org.
51
52 # (Some code taken from userv-utils's git-daemon.in and git-service.in
53 # which were written by Tony Finch <dot@dotat.at> and subsequently
54 # heavily modified by Ian Jackson <ijackson@chiark.greenend.org.uk>
55 # and were released under CC0 1.0.  The whole program is now GPLv3+.)
56
57 use strict;
58 use warnings;
59
60 use POSIX;
61 use Socket;
62 use Sys::Syslog;
63 use Fcntl qw(:flock SEEK_SET);
64 use File::Path qw(remove_tree);
65
66 our $us = 'git-cache-proxy';
67
68 our $debug = 0;
69 our $housekeepingeverydays = 1;
70 our $gcintervaldays = 10;
71 our $treeexpiredays = 21;
72 our $fetchtimeout = 3600;
73 our $maxfetchtimeout = 7200;
74 our $servetimeout = 3600;
75 our $cachedir = '/var/cache/git-cache-proxy';
76 our $housekeepingonly = 0;
77
78 #---------- error handling and logging ----------
79
80 # This is a bit fiddly, because we want to catch errors sent to stderr
81 # and dump them to syslog if we can, but only if we are running as an
82 # inetd service.
83
84 our $log; # filehandle (ref), or "1" meaning syslog
85
86 sub ntoa {
87     my $sockaddr = shift;
88     return ('(local)') unless defined $sockaddr;
89     my ($port,$addr) = sockaddr_in $sockaddr;
90     $addr = inet_ntoa $addr;
91     return ("[$addr]:$port",$addr,$port);
92 }
93
94 our ($client) = ntoa getpeername STDIN;
95 our ($server) = ntoa getsockname STDIN;
96
97 sub ensurelog () {
98     return if $log;
99     openlog $us, qw(pid), 'daemon';
100     $log = 1;
101 }
102
103 sub logm ($$) {
104     my ($pri, $msg) = @_;
105     return if $pri eq 'debug' && !$debug;
106     if ($client eq '(local)') {
107         print STDERR "$us: $pri: $msg\n" or die $!;
108         return;
109     }
110     ensurelog();
111     my $mainmsg = sprintf "%s-%s: %s", $server, $client, $msg;
112     if (ref $log) {
113         my $wholemsg = sprintf("%s [%d] %s: %s\n",
114                                strftime("%Y-%m-%d %H:%M:%S Z", gmtime),
115                                $$,
116                                $pri eq 'err' ? 'error' : $pri,
117                                $mainmsg);
118         print $log $wholemsg;
119     } else {
120         syslog $pri, "%s", "$pri $mainmsg";
121     }
122 }
123
124 if ($client ne '(local)') {
125     open STDERR, ">/dev/null" or exit 255;
126     open TEMPERR, "+>", undef or exit 255;
127     open STDERR, ">&TEMPERR" or exit 255;
128 }
129
130 END {
131     if ($client ne '(local)') {
132         if ($?) { logm 'crit', "crashing ($?)"; }
133         seek TEMPERR, 0, SEEK_SET;
134         while (<TEMPERR>) {
135             chomp;
136             logm 'crit', $_;
137         }
138     }
139     exit $?;
140 }
141
142 sub fail ($) {
143     my ($msg) = @_;
144     logm 'err', $msg;
145     exit 0;
146 }
147
148 $SIG{ALRM} = sub { fail "timeout" };
149
150 sub gitfail ($) {
151     my ($msg) = @_;
152     close LOCK;
153     alarm 60;
154     logm 'notice', $msg;
155     my $gitmsg = "ERR $us: $msg";
156     $gitmsg = substr($gitmsg,0,65535); # just in case
157     printf "%04x%s", length($gitmsg)+4, $gitmsg;
158     flush STDOUT;
159     exit 0;
160 }
161
162 #---------- argument parsing ----------
163
164 for (;;) {
165     last unless @ARGV;
166     last unless $ARGV[0] =~ m/^-/;
167     $_ = shift @ARGV;
168     for (;;) {
169         last unless m/^-./;
170         if (s/^-H/-/) {
171             $housekeepingonly++;
172         } elsif (s/^-D/-/) {
173             $debug++;
174         } elsif (s/^-L(.*)$//) {
175             my $logfile = $_;
176             open STDERR, ">>", $logfile or fail "open $logfile: $!";
177             $log = \*STDERR;
178         } elsif (s/^-d(.*)$//) {
179             $cachedir = $1;
180         } elsif (s/^--( max-fetch-timeout
181                       | fetch-timeout
182                       | serve-timeout
183                       | tree-expire-days
184                       | housekeeping-interval-days
185                       | gc-interval-days
186                       )=(\d+)$//x) {
187             my $vn = $1;
188             $vn =~ y/-//d;
189             die $vn unless defined ${ $::{$vn} };
190             ${ $::{$vn} } = $2;
191         } else {
192             fail "bad usage: unknown option `$_'";
193         }
194     }
195 }
196
197 !@ARGV or fail "bad usage: no non-option arguments permitted";
198
199 #---------- utility functions ----------
200
201 sub lockfile ($$$$) {
202     my ($fh, $fn, $flockmode, $update_ts) = @_;
203     my $what = $fn.(($flockmode & ~LOCK_NB) == LOCK_SH ? " (shared)" : "");
204     for (;;) {
205         close $fh;
206         open $fh, ($update_ts ? '+>' : '+>>'), $fn
207             or fail "open/create $fn for lock: $!";
208         logm 'debug', "lock $what: acquiring";
209         if (!flock $fh, $flockmode) {
210             if ($flockmode & LOCK_NB && $! == EWOULDBLOCK) {
211                 return 0; # ok then
212             }
213             fail "lock $what: $!";
214         }
215         stat $fh or fail "stat opened $fn: $!";
216         my $fh_ino = ((stat _)[1]);
217         if (!stat $fn) {
218             $! == ENOENT or fail "stat $fn: $!";
219             next;
220         }
221         my $fn_ino = ((stat _)[1]);
222         if ($fn_ino == $fh_ino) {
223             logm 'debug', "lock $what: acquired";
224             return 1;
225         }
226         logm 'debug', "lock $what: deleted, need to loop again";
227         # oh dear
228     }
229 }
230
231 sub xread {
232     my $length = shift;
233     my $buffer = "";
234     while ($length > length $buffer) {
235         my $ret = sysread STDIN, $buffer, $length, length $buffer;
236         fail "expected $length bytes, got ".length $buffer
237                             if defined $ret and $ret == 0;
238         fail "read: $!" if not defined $ret and $! != EINTR and $! != EAGAIN;
239     }
240     return $buffer;
241 }
242
243 #---------- main program ----------
244
245 chdir $cachedir or fail "chdir $cachedir: $!";
246
247 our ($service,$specpath,$spechost,$subdir);
248 our ($tmpd,$gitd,$lock);
249 our ($fetch,$url);
250
251 sub servinfo ($) {
252     my ($msg) = @_;
253     logm 'info', "service `$specpath': $msg";
254 }
255
256 sub readcommand () {
257     alarm 30;
258
259     my $hex_len = xread 4;
260     fail "Bad hex in packet length" unless $hex_len =~ m|^[0-9a-fA-F]{4}$|;
261     my $line = xread -4 + hex $hex_len;
262     unless (($service,$specpath,$spechost) = $line =~
263             m|^(git-[a-z-]+) /*([!-~ ]+)\0host=([!-~]+)\0|) {
264         $line =~ s|[^ -~]+| |g;
265         gitfail "unknown/unsupported instruction `$line'"
266     }
267
268     alarm 0;
269
270     $service eq 'git-upload-pack'
271         or gitfail "unknown/unsupported service `$service'";
272
273     $fetch = 2; # 0:don't; 1:try; 2:force
274     $url = $specpath;
275
276     while ($url =~ s#\s+(\[)([^][{}]+)\]$## ||
277            $url =~ s#\s+(\{)([^][{}]+)\}$##) {
278         $_ = $2;
279         my $must = $1 eq '{';
280         if (m/^fetch=try$/) {
281             $fetch = 1;
282         } elsif (m/^fetch=no$/) {
283             $fetch = 0;
284         } elsif (m/^fetch=must$/) {
285             $fetch = 2; # the default
286         } elsif (m/^timeout=(\d+)$/ && $1 >= 1) {
287             $fetchtimeout = $1 <= $maxfetchtimeout ? $1 : $maxfetchtimeout;
288         } elsif ($must) {
289             gitfail "unknown/unsupported option `$_'";
290         }
291     }
292
293     $url =~ m{^(?:https?|git)://[-.0-9a-z]+/}
294         or gitfail "unknown/unsupported url scheme or format `$url'";
295
296     $subdir = $url;
297     $subdir =~ s|\\|\\\\|g;
298     $subdir =~ s|,|\\,|g;
299     $subdir =~ s|/|,|g;
300
301     $tmpd= "$subdir\\.tmp";
302     $gitd= "$subdir\\.git";
303     $lock = "$subdir\\.lock";
304
305     servinfo "locking";
306 }
307
308 sub update_gcstamp ($) {
309     my ($gitdir) = (@_);
310     my $gcdone = "$gitdir/cache-proxy-gc.stamp";
311     if (open GCSTAMP, '>', $gcdone) {
312         close GCSTAMP;
313     } else {
314         $!==ENOENT or fail "create $gcdone: $!";
315     }
316 }
317
318 sub clonefetch () {
319     lockfile \*LOCK, $lock, LOCK_EX, 1;
320
321     my $exists = lstat $gitd;
322     $exists or $!==ENOENT or fail "lstat $gitd: $!";
323
324     our $fetchfail = '';
325
326     if ($fetch) {
327
328         my $rbits = '';
329         vec($rbits,0,1) = 1;
330         my $ebits = $rbits;
331         my $r=select $rbits,undef,$ebits,0;
332         $r>=0 or fail "select recheck STDOUT failed: $!";
333         if ($r) {
334             servinfo 'client disconnected (stdin unexpectedly'.
335                 (vec($rbits,0,1) ? ' readable' : '').
336                 (vec($ebits,0,1) ? ' exception' : '').
337                 ')';
338             exit 0;
339         }
340
341         our @cmd;
342
343         if (!$exists) {
344             system qw(rm -rf --), $tmpd;
345             @cmd = (qw(git clone -q --mirror), $url, $tmpd);
346             servinfo "cloning";
347         } else {
348             @cmd = (qw(git remote update --prune));
349             servinfo "fetching";
350         }
351         my $cmd = "@cmd[0..1]";
352
353         my $child = open FETCHERR, "-|";
354         defined $child or fail "fork: $!";
355         if (!$child) {
356             if ($exists) {
357                 chdir $gitd or fail "chdir $gitd: $!";
358             }
359             setpgrp or fail "setpgrp: $!";
360             open STDERR, ">&STDOUT" or fail "redirect stderr: $!";
361             exec @cmd or fail "exec $cmd[0]: $!";
362         }
363
364         my $fetcherr = '';
365         my $timedout = 0;
366         {
367             local $SIG{ALRM} = sub {
368                 servinfo "fetch/clone timeout";
369                 $timedout=1; kill 9, -$child;
370             };
371             alarm($fetchtimeout);
372             $!=0; { local $/=undef; $fetcherr = <FETCHERR>; }
373             !FETCHERR->error or fail "read pipe from fetch/clone: $!";
374             alarm(10);
375         }
376
377         kill -9, $child or fail "kill fetch/clone: $!";
378         $!=0; $?=0; if (!close FETCHERR) {
379             fail "reap fetch/clone: $!" if $!;
380             my $fetchfail =
381                 !($? & 255) ? "$cmd died with error exit code ".($? >> 8) :
382                 $? != 9 ? "$cmd died due to fatal signa, status $?" :
383                 $timedout ? "$cmd timed out (${fetchtimeout}s)" :
384                 "$cmd died due to unexpected SIGKILL";
385             if (length $fetcherr) {
386                 $fetchfail .= "\n$fetcherr";
387                 $fetchfail =~ s/\n$//;
388                 $fetchfail =~ s{\n}{ // }g;
389             }
390             if ($fetch >= 2) {
391                 gitfail $fetchfail;
392             } else {
393                 servinfo "fetch/clone failed: $fetchfail";
394             }
395         }
396         alarm 0;
397
398         if (!$exists) {
399             update_gcstamp($tmpd);
400             rename $tmpd, $gitd or fail "rename fresh $tmpd to $gitd: $!";
401             $exists = 1;
402         }
403     } else {
404         $fetchfail = 'not attempted';
405     }
406
407     if (!$exists) {
408         gitfail "no cached data, and not cloned: $fetchfail";
409     }
410
411     servinfo "sharing";
412     lockfile \*LOCK, $lock, LOCK_SH, 1; # NB releases and relocks
413
414     if (stat $gitd) {
415         return 1;
416     }
417     $!==ENOENT or fail "stat $gitd: $!";
418
419     # Well, err, someone must have taken the lock in between
420     # and garbage collected it.  How annoying.
421     return 0;
422 }
423
424 sub hkfail ($) { my ($msg) = @_; fail "housekeeping: $msg"; }
425
426 sub housekeeping () {
427     logm 'info', "housekeeping started";
428     foreach $lock (<[a-z]*\\.lock>) {
429         my $subdir = $lock;  $subdir =~ s/\\.lock$//;
430         my $gcdone = "$subdir\\.git/cache-proxy-gc.stamp";
431         if (!lstat $lock) {
432             $! == ENOENT or hkfail "$lock: lstat: $!";
433             next;
434         }
435         my ($mode_what,$mode_locknb,$mode_action);
436         if (-M _ <= $treeexpiredays) {
437             my $gccheck = sub {
438                 if (!lstat "$gcdone") {
439                     $! == ENOENT or hkfail "$gcdone: lstat: $!";
440                     return 1, "touched recently, never gc'd!";
441                 } elsif (-M _ <= $gcintervaldays) {
442                     return 0, "touched recently, gc'd recently";
443                 } else {
444                     return 1, "touched recently, needs gc";
445                 }
446             };
447             my ($needsgc, $gcmsg) = $gccheck->();
448             logm 'debug', "housekeeping: subdirs $subdir: $gcmsg";
449             next unless $needsgc;
450             $mode_what = 'garbage collecting';
451             $mode_locknb = 0;
452             $mode_action = sub {
453                 my ($needsgc, $gcmsg) = $gccheck->();
454                 if (!$needsgc) {
455                     logm 'info',
456                         "housekeeping: subdirs $subdir: someone else has gc'd";
457                     return;
458                 }
459                 logm 'debug', "housekeeping: subdirs $subdir: $gcmsg (2)";
460                 my $gclog = "$subdir/gc.log";
461                 unlink $gclog or $!==ENOENT or hkfail "remove $gclog: $!";
462                 my $child = fork // hkfail "fork (for $subdir): $!";
463                 if (!$child) {
464                     if (!chdir "$subdir\\.git") {
465                         exit 0 if $!==ENOENT;
466                         die "for gc: chdir $subdir: $!\n";
467                     }
468                     exec qw(git gc --quiet);
469                     die "exec git gc (for $subdir): $!\n";
470                 }
471                 waitpid($child, 0) == $child or hkfail "waitpid failed! $!";
472                 if ($?) {
473                     logm 'err',
474  "housekeeping: subdirs $subdir: gc failed (wait status $?)";
475                 } else {
476                     update_gcstamp("$subdir\\.git");
477                     logm 'debug',
478                         "housekeeping: subdirs $subdir: gc done";
479                 }
480             };
481         } else {
482             $mode_what = 'cleaning';
483             $mode_locknb = LOCK_NB;
484             $mode_action = sub {
485                 eval {
486                     foreach my $suffix (qw(tmp git)) {
487                         my $dir = "${subdir}\\.$suffix";
488                         my $tdir = "${subdir}\\.tmp";
489                         if ($dir ne $tdir) {
490                             if (!rename $dir,$tdir) {
491                                 next if $! == ENOENT;
492                                 die "$dir: cannot rename to $tdir: $!\n";
493                             }
494                         }
495                         system qw(rm -rf --), $tdir;
496                         if (stat $tdir) {
497  die "$dir: problem deleting file(s), rm exited $?\n";
498                         } elsif ($! != ENOENT) {
499                             die "$tdir: cannot stat after deletion: $!\n";
500                         }
501                     }
502                 };
503                 if (length $@) {
504                     chomp $@;
505  logm 'warning', "housekeeping: $subdir: cleanup prevented: $@";
506                 } else {
507                     unlink $lock or hkfail "remove $lock: $!";
508                 }
509             };
510         }
511         if (!lockfile \*LOCK, $lock, LOCK_EX|$mode_locknb, 0) {
512             die $! unless $mode_locknb;
513             logm 'info', "housekeeping: subdirs $subdir: lock busy, skipping";
514             next;
515         }
516         logm 'info', "housekeeping: subdirs $subdir: $mode_what";
517         $mode_action->();
518     }
519     open HS, ">", "Housekeeping.stamp" or hkfail "touch Housekeeping.stamp: $!";
520     close HS or hkfail "close Housekeeping.stamp: $!";
521     logm 'info', "housekeeping finished";
522 }
523
524 sub housekeepingcheck ($$) {
525     my ($dofork, $force) = @_;
526     if (!$force) {
527         if (!lockfile \*HLOCK, "Housekeeping.lock", LOCK_EX|LOCK_NB, 1) {
528             logm 'debug', "housekeeping lock taken, not running";
529             close HLOCK;
530             return 0;
531         }
532     }
533     if ($force) {
534         logm 'info', "housekeeping forced";
535     } elsif (!lstat "Housekeeping.stamp") {
536         $! == ENOENT or fail "lstat Housekeeping.stamp: $!";
537         logm 'info', "housekeeping not done yet, will run";
538     } elsif (-M _ <= $housekeepingeverydays) {
539         logm 'debug', "housekeeping done recently";
540         close HLOCK;
541         return 0;
542     }
543     if ($dofork) {
544         my $child = fork;
545         defined $child or fail "fork: $!";
546         if (!$child) {
547             open STDERR, "|logger -p daemon.warning -t '$us(housekeeping)'"
548                 or die "fork: logger $!";
549             housekeeping();
550             exit 0;
551         }
552     } else {
553         housekeeping();
554     }
555     close HLOCK;
556     return 1;
557 }
558
559 sub runcommand () {
560     servinfo "serving";
561
562     chdir $gitd or fail "chdir $gitd: $!";
563
564     exec qw(git-upload-pack --strict), "--timeout=$servetimeout", qw(.)
565         or fail "exec git-upload-pack: $!";
566 }
567
568 sub daemonservice () {
569     readcommand();
570     while (!clonefetch()) { }
571     housekeepingcheck(1,0);
572     runcommand();
573 }
574
575 if ($housekeepingonly) {
576     housekeepingcheck(0, $housekeepingonly>=2);
577 } else {
578     daemonservice();
579 }