User/Project: implement some sane length limits for names
[girocco.git] / Girocco / Project.pm
blob2f3340f95ee2293c15d455a03b3990753325ebe2
1 package Girocco::Project;
3 use strict;
4 use warnings;
6 BEGIN {
7 use Girocco::CGI;
8 use Girocco::User;
9 use Girocco::Util;
10 use Girocco::HashUtil;
11 use Girocco::ProjPerm;
12 use Girocco::Config;
13 use base ('Girocco::ProjPerm::'.$Girocco::Config::permission_control); # mwahaha
16 BEGIN {
17 eval {
18 require Digest::SHA;
19 Digest::SHA->import(
20 qw(sha1_hex)
21 );1} ||
22 eval {
23 require Digest::SHA1;
24 Digest::SHA1->import(
25 qw(sha1_hex)
26 );1} ||
27 eval {
28 require Digest::SHA::PurePerl;
29 Digest::SHA::PurePerl->import(
30 qw(sha1_hex)
31 );1} ||
32 die "One of Digest::SHA or Digest::SHA1 or Digest::SHA::PurePerl "
33 . "must be available\n";
36 our $metadata_fields = {
37 homepage => ['Homepage URL', 'hp', 'text'],
38 shortdesc => ['Short description', 'desc', 'text'],
39 README => ['README (HTML, lt 8kb)', 'README', 'textarea'],
40 notifymail => ['Commit notify - mail to', 'notifymail', 'text'],
41 notifyjson => ['Commit notify - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
42 notifycia => ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> name', 'notifycia', 'text'],
45 sub _mkdir_forkees {
46 my $self = shift;
47 my @pelems = split('/', $self->{name});
48 pop @pelems; # do not create dir for the project itself
49 my $path = $self->{base_path};
50 foreach my $pelem (@pelems) {
51 $path .= "/$pelem";
52 (-d "$path") or mkdir $path or die "mkdir $path: $!";
53 chmod 02775, $path; # ok if fails (dir may already exist and be owned by someone else)
57 our %propmap = (
58 url => ':baseurl',
59 email => ':owner',
60 desc => 'description',
61 README => 'README.html',
62 hp => ':homepage',
63 notifymail => '%hooks.mailinglist',
64 notifytag => '%hooks.announcelist',
65 notifyjson => '%hooks.jsonurl',
66 notifycia => '%hooks.cianame',
69 our %propmapro = (
70 lastchange => ':lastchange',
71 lastactivity => 'info/lastactivity',
74 # Projects with any of these names will be disallowed to avoid possible
75 # collisions with cgi script paths
76 our %reservedprojectnames = (
77 c => 1,
78 h => 1,
79 r => 1,
80 w => 1,
83 sub _update_index {
84 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
87 sub _property_path {
88 my $self = shift;
89 my ($name) = @_;
90 $self->{path}.'/'.$name;
93 sub _property_fget {
94 my $self = shift;
95 my ($name) = @_;
96 my $pname = $propmap{$name};
97 $pname = $propmapro{$name} unless $pname;
98 $pname or die "unknown property: $name";
99 if ($pname =~ s/^://) {
100 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.$pname"`;
101 chomp $val;
102 return $val;
103 } elsif ($pname =~ s/^%//) {
104 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "$pname"`;
105 chomp $val;
106 return $val;
109 open P, '<', $self->_property_path($pname) or return undef;
110 my @value = <P>;
111 close P;
112 my $value = join('', @value); chomp $value;
113 $value;
116 sub _property_fput {
117 my $self = shift;
118 my ($name, $value) = @_;
119 my $pname = $propmap{$name};
120 $pname or die "unknown property: $name";
121 $value ||= '';
122 if ($pname =~ s/^://) {
123 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
124 return;
125 } elsif ($pname =~ s/^%//) {
126 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', $pname, $value);
127 return;
130 my $P = lock_file($self->_property_path($pname));
131 $value ne '' and print $P "$value\n";
132 close $P;
133 unlock_file($self->_property_path($pname));
136 sub _properties_load {
137 my $self = shift;
138 foreach my $prop (keys %propmap) {
139 $self->{$prop} = $self->_property_fget($prop);
141 foreach my $prop (keys %propmapro) {
142 $self->{$prop} = $self->_property_fget($prop);
144 my $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config --bool "gitweb.statusupdates" 2>/dev/null`;
145 chomp $val;
146 $val = ($val eq 'false') ? 0 : 1;
147 $self->{statusupdates} = $val;
148 delete $self->{auth};
149 $val = `"$Girocco::Config::git_bin" --git-dir="$self->{path}" config "gitweb.repoauth"`;
150 chomp $val;
151 if ($val =~ /^# ([A-Z]+)AUTH ([0-9a-f]+) (\d+)/) {
152 my $expire = $3;
153 if (time < $expire) {
154 $self->{authtype} = $1;
155 $self->{auth} = $2;
160 sub _properties_save {
161 my $self = shift;
162 foreach my $prop (keys %propmap) {
163 $self->_property_fput($prop, $self->{$prop});
165 $self->{statusupdates} = 1
166 unless defined($self->{statusupdates}) && $self->{statusupdates} =~ /^\d+$/;
167 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--bool',
168 "gitweb.statusupdates", $self->{statusupdates});
171 sub _nofetch_path {
172 my $self = shift;
173 $self->_property_path('.nofetch');
176 sub _nofetch {
177 my $self = shift;
178 my ($nofetch) = @_;
179 my $nf = $self->_nofetch_path;
180 if ($nofetch) {
181 open X, '>', $nf or die "nofetch failed: $!";
182 close X;
183 } else {
184 unlink $nf or die "yesfetch failed: $!";
188 sub _clonelog_path {
189 my $self = shift;
190 $self->_property_path('.clonelog');
193 sub _clonefail_path {
194 my $self = shift;
195 $self->_property_path('.clone_failed');
198 sub _clonep_path {
199 my $self = shift;
200 $self->_property_path('.clone_in_progress');
203 sub _clonep {
204 my $self = shift;
205 my ($nofetch) = @_;
206 my $np = $self->_clonep_path;
207 if ($nofetch) {
208 open X, '>', $np or die "clonep failed: $!";
209 close X;
210 } else {
211 unlink $np or die "clonef failed: $!";
214 sub _alternates_setup {
215 my $self = shift;
216 return unless $self->{name} =~ m#/#;
217 my $forkee_name = get_forkee_name($self->{name});
218 my $forkee_path = get_forkee_path($self->{name});
219 return unless -d $forkee_path;
220 mkdir $self->{path}.'/refs'; chmod 02775, $self->{path}.'/refs';
221 mkdir $self->{path}.'/objects'; chmod 02775, $self->{path}.'/objects';
222 mkdir $self->{path}.'/objects/info'; chmod 02775, $self->{path}.'/objects/info';
224 # We set up both alternates and http_alternates since we cannot use
225 # relative path in alternates - that doesn't work recursively.
227 my $filename = $self->{path}.'/objects/info/alternates';
228 open X, '>', $filename or die "alternates failed: $!";
229 print X "$forkee_path/objects\n";
230 close X;
231 chmod 0664, $filename or warn "cannot chmod $filename: $!";
233 if ($Girocco::Config::httppullurl) {
234 $filename = $self->{path}.'/objects/info/http-alternates';
235 open X, '>', $filename or die "http-alternates failed: $!";
236 my $upfork = $forkee_name;
237 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
238 close X;
239 chmod 0664, $filename or warn "cannot chmod $filename: $!";
242 # The symlink is problematic since git remote prune will traverse it.
243 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
245 # copy refs from parent project
246 # ownership information is changed by a root cronjob
247 system("cp -pR $forkee_path/refs $self->{path}/");
248 # initialize HEAD, hacky version
249 system("cp $forkee_path/HEAD $self->{path}/HEAD");
252 sub _ctags_setup {
253 my $self = shift;
254 my $perms = $Girocco::Config::permission_control eq 'Hooks' ? 02777 : 02775;
255 mkdir $self->{path}.'/ctags'; chmod $perms, $self->{path}.'/ctags';
258 sub _group_add {
259 my $self = shift;
260 my ($xtra) = @_;
261 $xtra .= join(',', @{$self->{users}});
262 filedb_atomic_append(jailed_file('/etc/group'),
263 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
266 sub _group_update {
267 my $self = shift;
268 my $xtra = join(',', @{$self->{users}});
269 filedb_atomic_edit(jailed_file('/etc/group'),
270 sub {
271 $_ = $_[0];
272 chomp;
273 if ($self->{name} eq (split /:/)[0]) {
274 # preserve readonly flag
275 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
276 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
277 } else {
278 return "$_\n";
284 sub _group_remove {
285 my $self = shift;
286 filedb_atomic_edit(jailed_file('/etc/group'),
287 sub {
288 $self->{name} ne (split /:/)[0] and return $_;
293 sub _hook_path {
294 my $self = shift;
295 my ($name) = @_;
296 $self->{path}.'/hooks/'.$name;
299 sub _hook_install {
300 my $self = shift;
301 my ($name) = @_;
302 open SRC, '<', "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
303 open DST, '>', $self->_hook_path($name) or die "cannot open hook $name for writing: $!";
304 while (<SRC>) { print DST $_; }
305 close DST;
306 close SRC;
307 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
310 sub _hooks_install {
311 my $self = shift;
312 foreach my $hook ('pre-receive', 'post-receive', 'update', 'post-update') {
313 $self->_hook_install($hook);
317 # private constructor, do not use
318 sub _new {
319 my $class = shift;
320 my ($name, $base_path, $path) = @_;
321 does_exist($name,1) || valid_name($name) or die "refusing to create project with invalid name ($name)!";
322 $path ||= "$base_path/$name.git";
323 my $proj = { name => $name, base_path => $base_path, path => $path };
325 bless $proj, $class;
328 # public constructor #0
329 # creates a virtual project not connected to disk image
330 # you can conjure() it later to disk
331 sub ghost {
332 my $class = shift;
333 my ($name, $mirror) = @_;
334 my $self = $class->_new($name, $Girocco::Config::reporoot);
335 $self->{users} = [];
336 $self->{mirror} = $mirror;
337 $self->{email} = $self->{orig_email} = '';
338 $self;
341 # public constructor #1
342 sub load {
343 my $class = shift;
344 my $name = shift || '';
346 open F, '<', jailed_file("/etc/group") or die "project load failed: $!";
347 while (<F>) {
348 chomp;
349 @_ = split /:+/;
350 next unless (shift eq $name);
352 my $self = $class->_new($name, $Girocco::Config::reporoot);
353 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
355 my $ulist;
356 ($self->{crypt}, $self->{gid}, $ulist) = @_;
357 $ulist ||= '';
358 $self->{users} = [split /,/, $ulist];
359 $self->{HEAD} = $self->get_HEAD;
360 $self->{orig_HEAD} = $self->{HEAD};
361 $self->{orig_users} = [@{$self->{users}}];
362 $self->{mirror} = ! -e $self->_nofetch_path;
363 $self->{clone_in_progress} = -e $self->_clonep_path;
364 $self->{clone_logged} = -e $self->_clonelog_path;
365 $self->{clone_failed} = -e $self->_clonefail_path;
366 $self->{ccrypt} = $self->{crypt};
368 $self->_properties_load;
369 $self->{orig_email} = $self->{email};
370 return $self;
372 close F;
373 undef;
376 # $proj may not be in sane state if this returns false!
377 sub cgi_fill {
378 my $self = shift;
379 my ($gcgi) = @_;
380 my $cgi = $gcgi->cgi;
382 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
383 # in case passwords are disabled
384 defined($pwd) or $pwd = ''; defined($pwd2) or $pwd = '';
385 if ($Girocco::Config::project_passwords and not $self->{crypt} and $pwd eq '' and $pwd2 eq '') {
386 $gcgi->err("Empty passwords are not permitted.");
388 if ($pwd ne '' or not $self->{crypt}) {
389 $self->{crypt} = scrypt_sha1($pwd);
391 if (($pwd ne '' || $pwd2 ne '') and $pwd ne $pwd2) {
392 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
395 $self->{cpwd} = $cgi->param('cpwd');
397 my ($forkee,$project) = ($self->{name} =~ m#^(.*/)?([^/]+)$#);
398 my $newtype = $forkee ? 'fork' : 'project';
399 length($project) <= 64
400 or $gcgi->err("The $newtype name is longer than 64 characters. Do you really need that much?");
402 if ($Girocco::Config::project_owners eq 'email') {
403 $self->{email} = $gcgi->wparam('email');
404 valid_email($self->{email})
405 or $gcgi->err("Your email sure looks weird...?");
406 length($self->{email}) <= 96
407 or $gcgi->err("Your email is longer than 96 characters. Do you really need that much?");
410 $self->{url} = $gcgi->wparam('url');
411 if ($self->{url}) {
412 valid_repo_url($self->{url})
413 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
416 $self->{desc} = $gcgi->wparam('desc');
417 length($self->{desc}) <= 1024
418 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
420 $self->{README} = $gcgi->wparam('README');
421 length($self->{README}) <= 8192
422 or $gcgi->err("README length &gt; 8kb!");
424 $self->{hp} = $gcgi->wparam('hp');
425 if ($self->{hp}) {
426 valid_web_url($self->{hp})
427 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
430 $self->{users} = [grep { Girocco::User::valid_name($_) && Girocco::User::does_exist($_) } $cgi->param('user')];
432 $self->{HEAD} = $cgi->param('HEAD') if $cgi->param('HEAD');
434 # schedule deletion of tags (will be committed by update() after auth)
435 $self->{tags_to_delete} = [$cgi->param('tags')];
437 $self->{notifymail} = $gcgi->wparam('notifymail');
438 if ($self->{notifymail}) {
439 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
440 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
443 $self->{notifyjson} = $gcgi->wparam('notifyjson');
444 if ($self->{notifyjson}) {
445 valid_web_url($self->{notifyjson})
446 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
449 $self->{notifycia} = $gcgi->wparam('notifycia');
450 if ($self->{notifycia}) {
451 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
452 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
455 if ($cgi->param('setstatusupdates')) {
456 my $val = $gcgi->wparam('statusupdates') || '0';
457 $self->{statusupdates} = $val ? 1 : 0;
460 not $gcgi->err_check;
463 sub form_defaults {
464 my $self = shift;
466 name => $self->{name},
467 email => $self->{email},
468 url => $self->{url},
469 desc => html_esc($self->{desc}),
470 README => html_esc($self->{README}),
471 hp => $self->{hp},
472 users => $self->{users},
473 notifymail => html_esc($self->{notifymail}),
474 notifyjson => html_esc($self->{notifyjson}),
475 notifycia => html_esc($self->{notifycia}),
479 # return true if $enc_passwd is a match for $plain_passwd
480 my $_check_passwd_match = sub {
481 my $enc_passwd = shift;
482 my $plain_passwd = shift;
483 defined($enc_passwd) or $enc_passwd = '';
484 defined($plain_passwd) or $plain_passwd = '';
485 # $enc_passwd may be crypt or crypt_sha1
486 if ($enc_passwd =~ m(^\$sha1\$(\d+)\$([./0-9A-Za-z]{1,64})\$[./0-9A-Za-z]{28}$)) {
487 # It's using sha1-crypt
488 return $enc_passwd eq crypt_sha1($plain_passwd, $2, -(0+$1));
489 } else {
490 # It's using crypt
491 return $enc_passwd eq crypt($plain_passwd, $enc_passwd);
495 sub authenticate {
496 my $self = shift;
497 my ($gcgi) = @_;
499 $self->{ccrypt} or die "Can't authenticate against a project with no password";
500 defined($self->{cpwd}) or $self->{cpwd} = '';
501 unless ($_check_passwd_match->($self->{ccrypt}, $self->{cpwd})) {
502 $gcgi->err("Your admin password does not match!");
503 return 0;
505 return 1;
508 sub _setup {
509 my $self = shift;
510 my ($pushers) = @_;
512 $self->_mkdir_forkees;
514 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
515 if ($Girocco::Config::owning_group) {
516 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
517 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
518 chmod(02775, $self->{path}) or die "chmod 02775 $self->{path} failed: $!";
519 } else {
520 chmod(02777, $self->{path}) or die "chmod 02777 $self->{path} failed: $!";
522 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
523 or die "git init $self->{path} failed: $?";
524 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
525 or die "disabling receive.denyNonFastforwards failed: $?";
526 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
527 or die "disabling gc.auto failed: $?";
529 # /info must have right permissions,
530 # and git init didn't do it for some reason.
531 if ($Girocco::Config::owning_group) {
532 chmod(02775, $self->{path}."/info") or die "chmod 02775 $self->{path}/info failed: $!";
533 } else {
534 chmod(02777, $self->{path}."/info") or die "chmod 02777 $self->{path}/info failed: $!";
537 $self->_properties_save;
538 $self->_alternates_setup;
539 $self->_ctags_setup;
540 $self->_group_remove;
541 $self->_group_add($pushers);
542 $self->_hooks_install;
543 #$self->perm_initialize;
544 $self->_update_index;
547 sub premirror {
548 my $self = shift;
550 $self->_setup(':');
551 $self->_clonep(1);
552 $self->perm_initialize;
555 sub conjure {
556 my $self = shift;
558 $self->_setup;
559 $self->_nofetch(1);
560 if ($Girocco::Config::mob && $Girocco::Config::mob eq "mob") {
561 system("$Girocco::Config::basedir/bin/create-personal-mob-area", $self->{name}) == 0
562 or die "create-personal-mob-area $self->{name} failed";
564 $self->perm_initialize;
567 sub clone {
568 my $self = shift;
570 unlink ($self->_clonefail_path()); # Ignore EEXIST error
571 unlink ($self->_clonelog_path()); # Ignore EEXIST error
573 use IO::Socket;
574 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
575 $sock->print("clone ".$self->{name}."\n");
576 # Just ignore reply, we are going to succeed anyway and the I/O
577 # would apparently get quite hairy.
578 $sock->flush();
579 sleep 2; # *cough*
580 $sock->close();
583 sub update {
584 my $self = shift;
586 $self->_properties_save;
587 $self->_group_update;
589 if (exists($self->{tags_to_delete})) {
590 $self->delete_ctag($_) foreach(@{$self->{tags_to_delete}});
593 $self->set_HEAD($self->{HEAD}) unless $self->{orig_HEAD} eq $self->{HEAD};
595 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
596 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
597 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
598 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
600 $self->_update_index if $self->{email} ne $self->{orig_email};
601 $self->{orig_email} = $self->{email};
606 sub update_password {
607 my $self = shift;
608 my ($pwd) = @_;
610 $self->{crypt} = scrypt_sha1($pwd);
611 $self->_group_update;
614 # You can explicitly do this just on a ghost() repository too.
615 sub delete {
616 my $self = shift;
618 if (-d $self->{path}) {
619 system('rm', '-rf', $self->{path}) == 0
620 or die "rm -rf $self->{path} failed: $?";
622 # attempt to clean up any empty fork directories by removing them
623 my @pelems = split('/', $self->{name});
624 while (@pelems > 1) {
625 pop @pelems;
626 # okay to fail
627 rmdir join('/', $Girocco::Config::reporoot, @pelems) or last;
629 $self->_group_remove;
630 $self->_update_index;
633 sub _contains_files {
634 my $dir = shift;
635 (-d $dir) or return 0;
636 opendir(my $dh, $dir) or die "opendir $dir failed: $!";
637 while (my $entry = readdir($dh)) {
638 next if $entry eq '' || $entry eq '.' || $entry eq '..';
639 closedir($dh), return 1
640 if -f "$dir/$entry" ||
641 -d "$dir/$entry" && _contains_files("$dir/$entry");
643 closedir($dh);
644 return 0;
647 sub has_forks {
648 my $self = shift;
650 return _contains_files($Girocco::Config::reporoot.'/'.$self->{name});
653 sub is_empty {
654 # A project is considered empty if the git repository does not
655 # have any refs. This means packed-refs does not exist or is
656 # empty or only has lines starting with '#' AND there are no
657 # files in the refs subdirectory hierarchy (no matter how deep).
659 my $self = shift;
661 (-d $self->{path}) or return 0;
662 if (-e $self->{path}.'/packed-refs') {
663 open(my $pr, '<', $self->{path}.'/packed-refs')
664 or die "open $self->{path}./packed-refs failed: $!";
665 my $foundref = 0;
666 while (my $ref = <$pr>) {
667 next if $ref =~ /^#/;
668 $foundref = 1;
669 last;
671 close($pr);
672 return 0 if $foundref;
674 (-d $self->{path}.'/refs') or return 1;
675 return !_contains_files($self->{path}.'/refs');
678 sub delete_ctag {
679 my $self = shift;
680 my ($ctag) = @_;
682 # sanity check, disallow filenames starting with . .. or /
683 unlink($self->{path}.'/ctags/'.$ctag) if($ctag !~ m|^(\.\.?)/|);
686 sub get_ctag_names {
687 my $self = shift;
688 my @ctags = ();
689 opendir(my $dh, $self->{path}.'/ctags')
690 or return @ctags;
691 @ctags = grep { -f "$self->{path}/ctags/$_" } readdir($dh);
692 closedir($dh);
693 return @ctags;
696 sub get_heads {
697 my $self = shift;
698 my $fh;
699 open($fh, '-|', "$Girocco::Config::git_bin --git-dir=$self->{path} show-ref --heads") or die "could not get list of heads";
700 my @res;
701 while (<$fh>) {
702 chomp;
703 next if !m#^[0-9a-f]{40}\s+refs/heads/(.+)$ #x;
704 push @res, $1;
706 close $fh;
707 @res;
710 sub get_HEAD {
711 my $self = shift;
712 my $HEAD = `$Girocco::Config::git_bin --git-dir=$self->{path} symbolic-ref HEAD`;
713 chomp $HEAD;
714 die "could not get HEAD" if ($HEAD !~ m{^refs/heads/(.+)$});
715 return $1;
718 sub set_HEAD {
719 my $self = shift;
720 my $newHEAD = shift;
721 # Cursory checks only -- if you want to break your HEAD, be my guest
722 if ($newHEAD =~ /^\/|['<>]|\.\.|\/$/) {
723 die "grossly invalid new HEAD: $newHEAD";
725 system($Girocco::Config::git_bin, "--git-dir=$self->{path}", 'symbolic-ref', 'HEAD', "refs/heads/$newHEAD");
726 die "could not set HEAD" if ($? >> 8);
727 ! -d "$self->{path}/mob" || $Girocco::Config::mob ne 'mob'
728 or system('cp', '-p', '-f', "$self->{path}/HEAD", "$self->{path}/mob/HEAD") == 0;
731 sub gen_auth {
732 my $self = shift;
733 my ($type) = @_;
734 $type = 'REPO' unless $type && $type =~ /^[A-Z]+$/;
736 $self->{authtype} = $type;
738 no warnings;
739 $self->{auth} = sha1_hex(time . $$ . rand() . join(':',%$self));
741 my $expire = time + 24 * 3600;
742 my $propval = "# ${type}AUTH $self->{auth} $expire";
743 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gitweb.repoauth', $propval);
744 $self->{auth};
747 sub del_auth {
748 my $self = shift;
750 delete $self->{auth};
751 delete $self->{authtype};
752 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', '--unset', 'gitweb.repoauth');
755 sub remove_user {
756 my $self = shift;
757 my ($username) = @_;
759 my $before_count = @{$self->{users}};
760 $self->{users} = [grep { $_ ne $username } @{$self->{users}}];
761 return @{$self->{users}} != $before_count;
764 ### static methods
766 sub get_forkee_name {
767 local $_ = $_[0];
768 (m#^(.*)/.*?$#)[0]; #
771 sub get_forkee_path {
772 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
773 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
774 -d $forkee ? $forkee : '';
777 sub _valid_name_characters {
778 local $_ = $_[0];
779 (not m#^/#)
780 and (not m#//#)
781 and (not m#\./#)
782 and (not m#/$#)
783 and m#^[a-zA-Z0-9+./_-]+$#;
786 sub valid_name {
787 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
788 local $_ = $_[0];
789 _valid_name_characters($_) and not exists($reservedprojectnames{$_})
790 and ((not m#/#) or -d get_forkee_path($_)); # will also catch ^/
793 # It's possible that some forks have been kept but the forkee is gone.
794 # In this case the standard valid_name check is too strict.
795 sub does_exist {
796 no warnings; # avoid silly 'unsuccessful stat on filename with \n' warning
797 my ($name, $nodie) = @_;
798 my $okay = (
799 _valid_name_characters($name)
800 and ((not $name =~ m#/#)
801 or -d get_forkee_path($name)
802 or -d $Girocco::Config::reporoot.'/'.get_forkee_name($name)));
803 (!$okay && $nodie) and return undef;
804 !$okay and die "tried to query for project with invalid name $name!";
805 (-d $Girocco::Config::reporoot."/$name.git");
808 sub get_full_list {
809 my $class = shift;
810 my @projects;
812 open F, '<', jailed_file("/etc/group") or die "getting project list failed: $!";
813 while (<F>) {
814 chomp;
815 @_ = split /:+/;
816 next if ($_[2] < 65536);
818 push @projects, $_[0];
820 close F;
821 @projects;