chiark / gitweb /
git-daemon: overhaul configuration and add the userv service script
[userv-utils.git] / git-daemon / git-daemon-service.pl
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 my $service = $ENV{"USERV_SERVICE"};
15 $service =~ s|^git-||;
16
17 ${$cf::{$_}} = $ENV{"USERV_U_$_"}
18     for grep s/^USERV_U_//, keys %ENV;
19
20 openlog 'userv-git-daemon/$service', 'pid', 'daemon';
21
22 sub cf::fail {
23     syslog 'err', "$cf::client @_";
24     exit;
25 }
26
27 package cf;
28
29 our $check_repo = 1;
30 our $check_export = 0;
31 our ($dir,$repo,$client);
32 $_ = our $uri;
33 for my $cf (@ARGV) {
34     do $cf;
35 }
36
37 fail "no repository configured for $uri" unless defined $repo;
38 fail "no directory configured for $uri"  unless defined $dir;
39
40 my $home = (getpwuid $<)[7];
41 $dir = "$home/$dir" if $dir =~ m|^[^/]|
42                     or $dir =~ s|^~/||;
43 $dir = "$dir/$repo";
44
45 fail "bad repository in $uri"
46     if $check_repo and $repo !~ /^[0-9A-Za-z._-]+[.]git$/;
47 fail "repository not exported for $uri"
48     if $check_export and not -e "$dir/git-daemon-export-ok";
49
50 my @cmd = ('git', $service, '--strict', '--timeout=30', $dir);
51 no warnings; # suppress errors to stderr
52 exec @cmd or fail "exec @cmd: $!";
53
54 # end