1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
15 our $metadata_fields = {
16 homepage
=> ['Homepage URL', 'hp', 'text'],
17 shortdesc
=> ['Short description', 'desc', 'text'],
18 README
=> ['README (HTML, lt 8kb)', 'README', 'textarea'],
23 my @pelems = split('/', $self->{name
});
24 pop @pelems; # do not create dir for the project itself
25 my $path = $self->{base_path
};
26 foreach my $pelem (@pelems) {
28 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
29 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
36 desc
=> 'description',
37 README
=> 'README.html',
39 notifymail
=> '%hooks.mailinglist',
40 notifyjson
=> '%hooks.jsonurl',
46 $self->{path
}.'/'.$name;
52 my $pname = $propmap{$name};
53 $pname or die "unknown property: $name";
54 if ($pname =~ s/^://) {
55 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
58 } elsif ($pname =~ s/^%//) {
59 my $val = `git --git-dir="$self->{path}" config "$pname"`;
64 open P
, $self->_property_path($pname) or return undef;
67 my $value = join('', @value); chomp $value;
73 my ($name, $value) = @_;
74 my $pname = $propmap{$name};
75 $pname or die "unknown property: $name";
77 if ($pname =~ s/^://) {
78 system('git', '--git-dir='.$self->{path
}, 'config', "gitweb.$pname", $value);
79 } elsif ($pname =~ s/^%//) {
80 system('git', '--git-dir='.$self->{path
}, 'config', $pname, $value);
83 my $P = lock_file
($self->_property_path($pname));
84 $value ne '' and print $P "$value\n";
86 unlock_file
($self->_property_path($pname));
89 sub _properties_load
{
91 foreach my $prop (keys %propmap) {
92 $self->{$prop} = $self->_property_fget($prop);
96 sub _properties_save
{
98 foreach my $prop (keys %propmap) {
99 $self->_property_fput($prop, $self->{$prop});
105 $self->_property_path('.nofetch');
111 my $np = $self->_nofetch_path;
113 open X
, '>'.$np or die "nofetch failed: $!";
116 unlink $np or die "yesfetch failed: $!";
122 $self->_property_path('.clonelog');
125 sub _clonefail_path
{
127 $self->_property_path('.clone_failed');
132 $self->_property_path('.clone_in_progress');
138 my $np = $self->_clonep_path;
140 open X
, '>'.$np or die "clonep failed: $!";
143 unlink $np or die "clonef failed: $!";
146 sub _alternates_setup
{
148 return unless $self->{name
} =~ m
#/#;
149 my $forkee_name = get_forkee_name
($self->{name
});
150 my $forkee_path = get_forkee_path
($self->{name
});
151 return unless -d
$forkee_path;
152 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
153 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
154 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
156 # We set up both alternates and http_alternates since we cannot use
157 # relative path in alternates - that doesn't work recursively.
159 my $filename = $self->{path
}.'/objects/info/alternates';
160 open X
, '>'.$filename or die "alternates failed: $!";
161 print X
"$forkee_path/objects\n";
163 chmod 0664, $filename or warn "cannot chmod $filename: $!";
165 if ($Girocco::Config
::httppullurl
) {
166 $filename = $self->{path
}.'/objects/info/http-alternates';
167 open X
, '>'.$filename or die "http-alternates failed: $!";
168 my $upfork = $forkee_name;
169 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
171 chmod 0664, $filename or warn "cannot chmod $filename: $!";
174 # The symlink is problematic since git remote prune will traverse it.
175 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
180 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
186 $xtra .= join(',', @
{$self->{users
}});
187 filedb_atomic_append
(jailed_file
('/etc/group'),
188 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
193 my $xtra = join(',', @
{$self->{users
}});
194 filedb_atomic_edit
(jailed_file
('/etc/group'),
198 if ($self->{name
} eq (split /:/)[0]) {
199 # preserve readonly flag
200 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
201 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
211 filedb_atomic_edit
(jailed_file
('/etc/group'),
213 $self->{name
} ne (split /:/)[0] and return $_;
221 $self->{path
}.'/hooks/'.$name;
227 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
228 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
229 while (<SRC
>) { print DST
$_; }
232 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
237 foreach my $hook ('post-receive', 'update') {
238 $self->_hook_install($hook);
242 # private constructor, do not use
245 my ($name, $base_path, $path) = @_;
246 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
247 $path ||= "$base_path/$name.git";
248 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
253 # public constructor #0
254 # creates a virtual project not connected to disk image
255 # you can conjure() it later to disk
258 my ($name, $mirror) = @_;
259 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
261 $self->{mirror
} = $mirror;
265 # public constructor #1
270 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
274 next unless (shift eq $name);
276 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
277 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
280 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
282 $self->{users
} = [split /,/, $ulist];
283 $self->{orig_users
} = [@
{$self->{users
}}];
284 $self->{mirror
} = ! -e
$self->_nofetch_path;
285 $self->{clone_in_progress
} = -e
$self->_clonep_path;
286 $self->{clone_logged
} = -e
$self->_clonelog_path;
287 $self->{clone_failed
} = -e
$self->_clonefail_path;
288 $self->{ccrypt
} = $self->{crypt};
290 $self->_properties_load;
297 # $proj may not be in sane state if this returns false!
301 my $cgi = $gcgi->cgi;
303 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
304 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
305 if ($pwd or not $self->{crypt}) {
306 $self->{crypt} = scrypt
($pwd);
309 if ($pwd2 and $pwd ne $pwd2) {
310 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
313 $self->{cpwd
} = $cgi->param('cpwd');
315 if ($Girocco::Config
::project_owners
eq 'email') {
316 $self->{email
} = $gcgi->wparam('email');
317 valid_email
($self->{email
})
318 or $gcgi->err("Your email sure looks weird...?");
321 $self->{url
} = $gcgi->wparam('url');
323 valid_repo_url
($self->{url
})
324 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
327 $self->{desc
} = $gcgi->wparam('desc');
328 length($self->{desc
}) <= 1024
329 or $gcgi->err("<b>Short</b> description length > 1kb!");
331 $self->{README
} = $gcgi->wparam('README');
332 length($self->{README
}) <= 8192
333 or $gcgi->err("README length > 8kb!");
335 $self->{hp
} = $gcgi->wparam('hp');
337 valid_web_url
($self->{hp
})
338 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
341 # FIXME: Permit only existing users
342 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
344 not $gcgi->err_check;
350 name
=> $self->{name
},
351 email
=> $self->{email
},
353 desc
=> html_esc
($self->{desc
}),
354 README
=> html_esc
($self->{README
}),
356 users
=> $self->{users
},
364 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
365 $self->{cpwd
} ||= '';
366 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
367 $gcgi->err("Your admin password does not match!");
377 $self->_mkdir_forkees;
379 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
380 if ($Girocco::Config
::owning_group
) {
381 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
382 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
383 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
385 chmod(02777, $self->{path
}) or die "chmod 2777 $self->{path} failed: $!";
387 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
388 or die "git init $self->{path} failed: $?";
389 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
390 or die "disabling receive.denyNonFastforwards failed: $?";
391 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'gc.auto', '0') == 0
392 or die "disabling gc.auto failed: $?";
394 # /info must have right permissions even before the fixup job,
395 # and git init didn't do it for some reason.
396 if ($Girocco::Config
::owning_group
) {
397 chmod(02775, $self->{path
}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
399 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
402 $self->_properties_save;
403 $self->_alternates_setup;
405 $self->_group_add($pushers);
406 $self->_hooks_install;
407 $self->perm_initialize;
408 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
428 unlink ($self->_clonefail_path()); # Ignore EEXIST error
429 unlink ($self->_clonelog_path()); # Ignore EEXIST error
432 my $sock = IO
::Socket
::UNIX
->new($Girocco::Config
::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
433 $sock->print("clone ".$self->{name
}."\n");
434 # Just ignore reply, we are going to succeed anyway and the I/O
435 # would apparently get quite hairy.
444 $self->_properties_save;
445 $self->_group_update;
447 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
448 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
449 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
450 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
455 sub update_password
{
459 $self->{crypt} = scrypt
($pwd);
460 $self->_group_update;
463 # You can explicitly do this just on a ghost() repository too.
467 if (-d
$self->{path
}) {
468 system('rm', '-rf', $self->{path
}) == 0
469 or die "rm -rf $self->{path} failed: $?";
471 $self->_group_remove;
477 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
483 sub get_forkee_name
{
485 (m
#^(.*)/.*?$#)[0]; #
487 sub get_forkee_path
{
488 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
489 -d
$forkee ?
$forkee : '';
494 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
497 and m
#^[a-zA-Z0-9+./_-]+$#;
502 valid_name
($name) or die "tried to query for project with invalid name $name!";
503 (-d
$Girocco::Config
::reporoot
."/$name.git");