| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # userv-git-daemon service script |
| 4 | # |
| 5 | # This was written by Tony Finch <dot@dotat.at> |
| 6 | # You may do anything with it, at your own risk. |
| 7 | # http://creativecommons.org/publicdomain/zero/1.0/ |
| 8 | |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | |
| 12 | use Sys::Syslog; |
| 13 | |
| 14 | ${$::{$_}} = $ENV{"USERV_U_$_"} |
| 15 | for grep s|^USERV_U_([a-z_]+)$|$1|, keys %ENV; |
| 16 | |
| 17 | our ($client,$service,$path,$host,@opts); |
| 18 | |
| 19 | openlog "userv-$service", 'pid', 'daemon'; |
| 20 | sub fail { syslog 'err', "$client @_"; exit } |
| 21 | |
| 22 | our ($check_repo,$check_export,$dir,$repo) = (1,0); |
| 23 | our $uri = $_ = "git://$host/$path"; |
| 24 | for my $cf (@ARGV) { do $cf } |
| 25 | |
| 26 | my $home = (getpwuid $<)[7]; |
| 27 | $dir = "$home/$dir" if $dir =~ m|^[^/]| |
| 28 | or $dir =~ s|^~/||; |
| 29 | fail "Bad filename $repo" if $check_repo |
| 30 | and $repo !~ m|^\w[\w.=+-]*\.git$|; |
| 31 | $dir = "$dir/$repo" if defined $repo; |
| 32 | $path = $check_export ? "$dir/git-daemon-export-ok" : $dir; |
| 33 | fail "$! $path" unless -e $path; |
| 34 | syslog 'notice', "$client $dir"; |
| 35 | |
| 36 | @opts = qw( --strict --timeout-30 ) |
| 37 | if @opts == 0 and $service eq 'git-upload-pack'; |
| 38 | |
| 39 | my @cmd = ($service =~ m|^(git)-(.*)$|, @opts, $dir); |
| 40 | no warnings; # suppress errors to stderr |
| 41 | exec @cmd or fail "exec $service: $!"; |
| 42 | |
| 43 | # end |