8 my ( $bin_path, $jail_path, $repomgr_path, $repodata_path, $repo_path );
9 my ( $site_domain, $group_file, $user_file, $sshkeys_path );
12 our @ISA = qw(Exporter);
13 our @EXPORT = qw(scrypt html_esc jailed_file
15 filedb_atomic_append filedb_atomic_edit
16 proj_get_forkee_name proj_get_forkee_path
17 valid_proj_name valid_user_name valid_email valid_repo_url valid_web_url);
19 use CGI
qw(:standard :escapeHTML -nosticky);
20 use CGI
::Util
qw(unescape);
21 use CGI
::Carp
qw(fatalsToBrowser);
22 use Digest
::SHA1
qw(sha1_hex);
24 # TODO: remove hard-coded paths by using a config file
25 $bin_path = '/home/pasky/bin';
26 $jail_path = '/home/repo/j';
27 $repomgr_path = '/home/repo/repomgr';
28 $repodata_path = '/home/repo/repodata';
29 $repo_path = '/srv/git';
31 $group_file = '/etc/group';
32 $user_file = '/etc/passwd';
33 $sshkeys_path = '/etc/sshkeys';
35 $site_domain = 'repo.or.cz';
37 $ENV{PATH
} = $bin_path . ':' . $ENV{PATH
};
48 $repo->{cgi
} = CGI
->new;
50 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
53 <?xml version="1.0" encoding="utf-8"?>
54 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
55 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
58 <title>$site_domain :: $heading</title>
59 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
60 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
65 <div class="page_header">
66 <a href="http://git.or.cz/" title="Git homepage"><img src="/git-logo.png" width="72" height="27" alt="git" style="float:right; border-width:0px;"/></a>
67 <a href="/">$site_domain</a> / administration / $heading
78 my $cginame = $cgi->url(-absolute
=> 1);
80 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
83 <a href="http://$site_domain/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
100 print "<p>", @_, "</p>\n" if @_;
106 print "<p style=\"color: red\">@_</p>\n";
112 my $err = $self->{err
};
113 $err and print "<p style=\"font-weight: bold\">Operation aborted due to $err errors.</p>\n";
118 my ( $self, $param ) = @_;
119 $self->{cgi
}->param($param) || '';
123 my $val = $self->sparam(@_);
124 $val =~ s/^\s*(.*?)\s*$/$1/;
129 ### Random utility functions
133 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
139 $str =~ s/</</g; $str =~ s/>/>/g;
140 $str =~ s/"/"/g;
146 "$jail_path/$filename";
154 use Errno
qw(EEXIST);
155 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
157 my $handle = new IO
::Handle
;
159 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
161 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
162 ($! == EEXIST
) or die "$path open failed: $!";
163 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
167 # XXX: filedb-specific
168 chmod 0664, $path or die "$path g+w failed: $!";
176 rename "$path.lock", $path or die "$path unlock failed: $!";
179 sub filedb_atomic_append
{
180 my ($file, $line) = @_;
183 open my $src, $file or die "$file open for reading failed: $!";
184 my $dst = lock_file
($file);
187 my $aid = (split /:/)[2];
188 $id = $aid + 1 if ($aid >= $id);
190 print $dst $_ or die "$file(l) write failed: $!";
193 $line =~ s/\\i/$id/g;
194 print $dst "$line\n" or die "$file(l) write failed: $!";
196 close $dst or die "$file(l) close failed: $!";
204 sub filedb_atomic_edit
{
205 my ($file, $fn) = @_;
207 open my $src, $file or die "$file open for reading failed: $!";
208 my $dst = lock_file
($file);
211 print $dst $fn->($_) or die "$file(l) write failed: $!";
214 close $dst or die "$file(l) close failed: $!";
220 sub proj_get_forkee_name
{
224 sub proj_get_forkee_path
{
225 my $forkee = $repo_path . proj_get_forkee_name
($_[0]).'.git';
226 -d
$forkee ?
$forkee : '';
228 sub valid_proj_name
{
230 (not m
#/# or -d proj_get_forkee_path($_)) # will also catch ^/
233 and m
#^[a-zA-Z0-9+./_-]+$#;
235 sub valid_user_name
{
237 /^[a-zA-Z0-9+._-]+$/;
241 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
245 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
249 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
250 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
254 my ( $self, $name ) = @_;
256 $self->bye("I need the project name as an argument now.")
259 $self->bye("Invalid project name. Go away, sorcerer.")
260 unless valid_proj_name
($name);
262 $self->bye("Sorry but this project does not exist. "
263 . "Now, how did you <em>get</em> here?!")
264 unless Git
::RepoCGI
::Project
::does_exist
($name);
266 $self->bye("Sorry but your project has not finished mirroring yet. "
267 . "If this process takes excessive time, "
268 . "please inform the site administrator.")
269 unless Git
::RepoCGI
::Project
::available
($name);
271 my $proj = Git
::RepoCGI
::Project
->load($name);
272 die "not found project $name, that's really weird!" unless $proj;
277 my ( $self, $name ) = @_;
278 $self->bye("User '$name' is not registered.")
279 unless valid_user_name
($name)
280 && Git
::RepoCGI
::User
::does_exist
($name);
282 my $user = Git
::RepoCGI
::User
->load($name);
283 $self->bye("User '$name' failed to load") unless $user;
289 package Git
::RepoCGI
::Project
;
291 BEGIN { use Git
::RepoCGI
; }
295 my @pelems = split('/', $self->{name
});
296 pop @pelems; # do not create dir for the project itself
297 my $path = $self->{base_path
};
298 foreach my $pelem (@pelems) {
300 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
301 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
308 desc
=> 'description',
309 README
=> 'README.html',
316 $self->{path
}.'/'.$name;
322 $propmap{$name} or die "unknown property: $name";
323 open P
, $self->_property_path($propmap{$name}) or return undef;
326 my $value = join('', @value); chomp $value;
332 my ($name, $value) = @_;
333 $propmap{$name} or die "unknown property: $name";
335 my $P = lock_file
($self->_property_path($propmap{$name}));
336 $value ne '' and print $P "$value\n";
338 unlock_file
($self->_property_path($propmap{$name}));
341 sub _properties_load
{
343 foreach my $prop (keys %propmap) {
344 $self->{$prop} = $self->_property_fget($prop);
348 sub _properties_save
{
350 foreach my $prop (keys %propmap) {
351 $self->_property_fput($prop, $self->{$prop});
357 $self->_property_path('.nofetch');
363 my $np = $self->_nofetch_path;
365 open X
, '>'.$np or die "nofetch failed: $!";
368 unlink $np or die "yesfetch failed: $!";
372 sub _alternates_setup
{
374 return unless $self->{name
} =~ m
#/#;
375 my $forkee_name = proj_get_forkee_name
($self->{name
});
376 my $forkee_path = proj_get_forkee_path
($self->{name
});
377 return unless -d
$forkee_path;
378 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
379 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
380 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
382 # We set up both alternates and http_alternates since we cannot use
383 # relative path in alternates - that doesn't work recursively.
385 my $filename = $self->{path
}.'/objects/info/alternates';
386 open X
, '>'.$filename or die "alternates failed: $!";
387 print X
"$forkee_path/objects\n";
389 chmod 0664, $filename or warn "cannot chmod $filename: $!";
391 $filename = $self->{path
}.'/objects/info/http-alternates';
392 open X
, '>'.$filename or die "http-alternates failed: $!";
393 my $upfork = $forkee_name;
394 do { print X
"/r/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork);
396 chmod 0664, $filename or warn "cannot chmod $filename: $!";
398 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
403 mkdir $self->{path
}.'/ctags'; chmod 0775, $self->{path
}.'/ctags';
409 $xtra .= join(',', @
{$self->{users
}});
410 filedb_atomic_append
(jailed_file
($group_file),
411 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
416 my $xtra = join(',', @
{$self->{users
}});
417 filedb_atomic_edit
(jailed_file
($group_file),
421 if ($self->{name
} eq (split /:/)[0]) {
422 # preserve readonly flag
423 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
424 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
434 filedb_atomic_edit
(jailed_file
($group_file),
436 $self->{name
} ne (split /:/)[0] and return $_;
444 $self->{path
}.'/hooks/'.$name;
450 open SRC
, "$repomgr_path/$name-hook" or die "cannot open hook $name: $!";
451 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
452 while (<SRC
>) { print DST
$_; }
455 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
460 foreach my $hook ('update') {
461 $self->_hook_install($hook);
465 # private constructor, do not use
468 my ($name, $base_path, $path) = @_;
469 valid_proj_name
($name) or die "refusing to create project with invalid name ($name)!";
470 $path ||= "$base_path/$name.git";
471 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
476 # public constructor #0
477 # creates a virtual project not connected to disk image
478 # you can conjure() it later to disk
481 my ($name, $mirror) = @_;
483 my $path = $mirror ?
"$repodata_path/to-clone" : $repo_path;
484 my $repo = "$path/$name";
485 $repo .= '.git' unless $mirror;
487 my $self = $class->_new($name, $path, $repo);
489 $self->{mirror
} = $mirror;
493 # public constructor #1
498 open F
, jailed_file
($group_file) or die "project load failed: $!";
502 next unless (shift eq $name);
504 my $self = $class->_new($name, $repo_path);
505 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
508 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
510 $self->{users
} = [split /,/, $ulist];
511 $self->{mirror
} = ! -e
$self->_nofetch_path;
512 $self->{ccrypt
} = $self->{crypt};
514 $self->_properties_load;
521 # $proj may not be in sane state if this returns false!
525 my $cgi = $repo->cgi;
527 my $pwd = $cgi->param('pwd');
528 if ($pwd ne '' or not $self->{crypt}) {
529 $self->{crypt} = scrypt
($pwd);
532 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
533 $repo->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
536 $self->{cpwd
} = $cgi->param('cpwd');
538 $self->{email
} = $repo->wparam('email');
539 valid_email
($self->{email
})
540 or $repo->err("Your email sure looks weird...?");
542 $self->{url
} = $repo->wparam('url');
544 valid_repo_url
($self->{url
})
545 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
548 $self->{desc
} = $repo->wparam('desc');
549 length($self->{desc
}) <= 1024
550 or $repo->err("<b>Short</b> description length > 1kb!");
552 $self->{README
} = $repo->wparam('README');
553 length($self->{README
}) <= 8192
554 or $repo->err("README length > 8kb!");
556 $self->{hp
} = $repo->wparam('hp');
558 valid_web_url
($self->{hp
})
559 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
562 # FIXME: Permit only existing users
563 $self->{users
} = [grep { valid_user_name
($_) } $cgi->param('user')];
565 not $repo->err_check;
571 name
=> $self->{name
},
572 email
=> $self->{email
},
574 desc
=> html_esc
($self->{desc
}),
575 README
=> html_esc
($self->{README
}),
577 users
=> $self->{users
},
585 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
586 $self->{cpwd
} or $repo->err("No password entered.");
587 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
588 $repo->err("Your admin password does not match!");
597 $self->_mkdir_forkees;
598 mkdir $self->{path
} or die "mkdir failed: $!";
599 chmod 0775, $self->{path
} or die "chmod failed: $!";
600 $self->_properties_save;
601 $self->_alternates_setup;
603 $self->_group_add(':');
609 $self->_mkdir_forkees;
610 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
611 or die "cg-admin-setuprepo failed: $?";
612 system('git', '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false');
614 $self->_properties_save;
615 $self->_alternates_setup;
618 $self->_hooks_install;
624 $self->_properties_save;
625 $self->_group_update;
628 sub update_password
{
632 $self->{crypt} = scrypt
($pwd);
633 $self->_group_update;
636 # You can explicitly do this just on a ghost() repository too.
640 if (-d
$self->{path
}) {
641 system('rm', '-r', $self->{path
}) == 0
642 or die "rm -r failed: $?";
644 $self->_group_remove;
650 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
652 or -d
"$repodata_path/cloning/$name"
653 or -d
"$repodata_path/to-clone/$name");
657 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
658 (-d
"$repo_path/$name.git");
664 package Git
::RepoCGI
::User
;
666 BEGIN { use Git
::RepoCGI
; }
670 join(':', $self->{name
}, $self->{crypt}, $self->{uid
}, 65534, $self->{email
}, '/', '/bin/git-shell')
675 my $xtra = join(',', @
{$self->{users
}});
676 filedb_atomic_edit
(jailed_file
($user_file),
680 if ($self->{name
} eq (split /:/)[0]) {
681 return $self->_passwd_text . "\n";
692 filedb_atomic_append
(jailed_file
($user_file), $self->_passwd_text);
695 sub update_password
{
699 $self->{crypt} = scrypt
($pwd);
706 $self->{ccrypt
} or die "Can't authenticate against a user with no password";
707 $self->{cpwd
} or $repo->err("No password entered.");
708 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
709 $repo->err("Your admin password does not match!");
717 $sshkeys_path . '/' . $self->{name
};
722 open F
, "<".jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
727 if (/^ssh-(?:dss|rsa) /) {
729 } elsif (/^# REPOAUTH ([0-9a-f]+) (\d+)/) {
731 $auth = $1 unless (time >= $expire);
735 my $keys = join('', @keys); chomp $keys;
741 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
742 if (defined($self->{auth
}) && $self->{auth
}) {
743 my $expire = time + 24 * 3600;
744 print F
"# REPOAUTH $self->{auth} $expire\n";
746 print F
$self->{keys}."\n";
748 chmod 0664, jailed_file
($self->_sshkey_path);
751 # private constructor, do not use
755 valid_user_name
($name) or die "refusing to create user with invalid name ($name)!";
756 my $proj = { name
=> $name };
761 # public constructor #0
762 # creates a virtual user not connected to disk record
763 # you can conjure() it later to disk
767 my $self = $class->_new($name);
771 # public constructor #1
776 open F
, jailed_file
($user_file) or die "user load failed: $!";
780 next unless (shift eq $name);
782 my $self = $class->_new($name);
784 ($self->{crypt}, $self->{uid
}, undef, $self->{email
}) = @_;
785 ($self->{keys}, $self->{auth
}) = $self->_sshkey_load;
786 $self->{ccrypt
} = $self->{crypt};
794 # $user may not be in sane state if this returns false!
799 $self->{name
} = $repo->wparam('name');
800 valid_user_name
($self->{name
})
801 or $repo->err("Name contains invalid characters.");
803 $self->{email
} = $repo->wparam('email');
804 valid_email
($self->{email
})
805 or $repo->err("Your email sure looks weird...?");
807 my $pwd = $repo->sparam('pwd');
808 if ($pwd ne '' or not $self->{crypt}) {
809 $self->{crypt} = scrypt
($pwd);
812 if ($repo->sparam('pwd2') and $pwd ne $repo->sparam('pwd2')) {
813 $repo->err("Our high-paid security consultants have determined that the passwords you have entered do not match each other.");
816 $self->{cpwd
} = $repo->sparam('cpwd');
818 $self->keys_fill($repo);
824 my $cgi = $repo->cgi;
826 $self->{keys} = $cgi->param('keys');
827 length($self->{keys}) <= 4096
828 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
829 foreach (split /\r?\n/, $self->{keys}) {
830 /^ssh-(?:dss|rsa) .* \S+@\S+$/ or $repo->err("Your ssh key (\"$_\") appears to have invalid format (does not start by ssh-dss|rsa or does not end with @-identifier) - maybe your browser has split a single key to multiple lines?");
833 not $repo->err_check;
845 $self->{auth
} = Digest
::SHA1
::sha1_hex
(time . $$ . rand() . $self->{keys});
853 delete $self->{auth
};
866 valid_user_name
($name) or die "tried to query for user with invalid name $name!";
867 (-e jailed_file
("$sshkeys_path/$name"));