Girocco::Project::form_defaults(): Add notify fields
[girocco/testingthisout.git] / Girocco / Project.pm
blob0d92314ea57e2240fd45b05c139e1a6014dfbd97
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::ProjPerm;
11 use Girocco::Config;
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'],
19 notifymail => ['Commit notify - mail to', 'notifymail', 'text'],
20 notifyjson => ['Commit notify - POST JSON at', 'notifyjson', 'text'],
21 notifycia => ['Commit notify - CIA project name', 'notifycia', 'text'],
24 sub _mkdir_forkees {
25 my $self = shift;
26 my @pelems = split('/', $self->{name});
27 pop @pelems; # do not create dir for the project itself
28 my $path = $self->{base_path};
29 foreach my $pelem (@pelems) {
30 $path .= "/$pelem";
31 (-d "$path") or mkdir $path or die "mkdir $path: $!";
32 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
36 our %propmap = (
37 url => ':baseurl',
38 email => ':owner',
39 desc => 'description',
40 README => 'README.html',
41 hp => ':homepage',
42 notifymail => '%hooks.mailinglist',
43 notifyjson => '%hooks.jsonurl',
44 notifycia => '%hooks.cianame',
47 sub _property_path {
48 my $self = shift;
49 my ($name) = @_;
50 $self->{path}.'/'.$name;
53 sub _property_fget {
54 my $self = shift;
55 my ($name) = @_;
56 my $pname = $propmap{$name};
57 $pname or die "unknown property: $name";
58 if ($pname =~ s/^://) {
59 my $val = `git --git-dir="$self->{path}" config "gitweb.$pname"`;
60 chomp $val;
61 return $val;
62 } elsif ($pname =~ s/^%//) {
63 my $val = `git --git-dir="$self->{path}" config "$pname"`;
64 chomp $val;
65 return $val;
68 open P, $self->_property_path($pname) or return undef;
69 my @value = <P>;
70 close P;
71 my $value = join('', @value); chomp $value;
72 $value;
75 sub _property_fput {
76 my $self = shift;
77 my ($name, $value) = @_;
78 my $pname = $propmap{$name};
79 $pname or die "unknown property: $name";
80 $value ||= '';
81 if ($pname =~ s/^://) {
82 system('git', '--git-dir='.$self->{path}, 'config', "gitweb.$pname", $value);
83 } elsif ($pname =~ s/^%//) {
84 system('git', '--git-dir='.$self->{path}, 'config', $pname, $value);
87 my $P = lock_file($self->_property_path($pname));
88 $value ne '' and print $P "$value\n";
89 close $P;
90 unlock_file($self->_property_path($pname));
93 sub _properties_load {
94 my $self = shift;
95 foreach my $prop (keys %propmap) {
96 $self->{$prop} = $self->_property_fget($prop);
100 sub _properties_save {
101 my $self = shift;
102 foreach my $prop (keys %propmap) {
103 $self->_property_fput($prop, $self->{$prop});
107 sub _nofetch_path {
108 my $self = shift;
109 $self->_property_path('.nofetch');
112 sub _nofetch {
113 my $self = shift;
114 my ($nofetch) = @_;
115 my $np = $self->_nofetch_path;
116 if ($nofetch) {
117 open X, '>'.$np or die "nofetch failed: $!";
118 close X;
119 } else {
120 unlink $np or die "yesfetch failed: $!";
124 sub _clonelog_path {
125 my $self = shift;
126 $self->_property_path('.clonelog');
129 sub _clonefail_path {
130 my $self = shift;
131 $self->_property_path('.clone_failed');
134 sub _clonep_path {
135 my $self = shift;
136 $self->_property_path('.clone_in_progress');
139 sub _clonep {
140 my $self = shift;
141 my ($nofetch) = @_;
142 my $np = $self->_clonep_path;
143 if ($nofetch) {
144 open X, '>'.$np or die "clonep failed: $!";
145 close X;
146 } else {
147 unlink $np or die "clonef failed: $!";
150 sub _alternates_setup {
151 my $self = shift;
152 return unless $self->{name} =~ m#/#;
153 my $forkee_name = get_forkee_name($self->{name});
154 my $forkee_path = get_forkee_path($self->{name});
155 return unless -d $forkee_path;
156 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
157 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
158 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
160 # We set up both alternates and http_alternates since we cannot use
161 # relative path in alternates - that doesn't work recursively.
163 my $filename = $self->{path}.'/objects/info/alternates';
164 open X, '>'.$filename or die "alternates failed: $!";
165 print X "$forkee_path/objects\n";
166 close X;
167 chmod 0664, $filename or warn "cannot chmod $filename: $!";
169 if ($Girocco::Config::httppullurl) {
170 $filename = $self->{path}.'/objects/info/http-alternates';
171 open X, '>'.$filename or die "http-alternates failed: $!";
172 my $upfork = $forkee_name;
173 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
174 close X;
175 chmod 0664, $filename or warn "cannot chmod $filename: $!";
178 # The symlink is problematic since git remote prune will traverse it.
179 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
182 sub _ctags_setup {
183 my $self = shift;
184 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
187 sub _group_add {
188 my $self = shift;
189 my ($xtra) = @_;
190 $xtra .= join(',', @{$self->{users}});
191 filedb_atomic_append(jailed_file('/etc/group'),
192 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
195 sub _group_update {
196 my $self = shift;
197 my $xtra = join(',', @{$self->{users}});
198 filedb_atomic_edit(jailed_file('/etc/group'),
199 sub {
200 $_ = $_[0];
201 chomp;
202 if ($self->{name} eq (split /:/)[0]) {
203 # preserve readonly flag
204 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
205 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
206 } else {
207 return "$_\n";
213 sub _group_remove {
214 my $self = shift;
215 filedb_atomic_edit(jailed_file('/etc/group'),
216 sub {
217 $self->{name} ne (split /:/)[0] and return $_;
222 sub _hook_path {
223 my $self = shift;
224 my ($name) = @_;
225 $self->{path}.'/hooks/'.$name;
228 sub _hook_install {
229 my $self = shift;
230 my ($name) = @_;
231 open SRC, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
232 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
233 while (<SRC>) { print DST $_; }
234 close DST;
235 close SRC;
236 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
239 sub _hooks_install {
240 my $self = shift;
241 foreach my $hook ('post-receive', 'update') {
242 $self->_hook_install($hook);
246 # private constructor, do not use
247 sub _new {
248 my $class = shift;
249 my ($name, $base_path, $path) = @_;
250 valid_name($name) or die "refusing to create project with invalid name ($name)!";
251 $path ||= "$base_path/$name.git";
252 my $proj = { name => $name, base_path => $base_path, path => $path };
254 bless $proj, $class;
257 # public constructor #0
258 # creates a virtual project not connected to disk image
259 # you can conjure() it later to disk
260 sub ghost {
261 my $class = shift;
262 my ($name, $mirror) = @_;
263 my $self = $class->_new($name, $Girocco::Config::reporoot);
264 $self->{users} = [];
265 $self->{mirror} = $mirror;
266 $self;
269 # public constructor #1
270 sub load {
271 my $class = shift;
272 my ($name) = @_;
274 open F, jailed_file("/etc/group") or die "project load failed: $!";
275 while (<F>) {
276 chomp;
277 @_ = split /:+/;
278 next unless (shift eq $name);
280 my $self = $class->_new($name, $Girocco::Config::reporoot);
281 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
283 my $ulist;
284 ($self->{crypt}, $self->{gid}, $ulist) = @_;
285 $ulist ||= '';
286 $self->{users} = [split /,/, $ulist];
287 $self->{orig_users} = [@{$self->{users}}];
288 $self->{mirror} = ! -e $self->_nofetch_path;
289 $self->{clone_in_progress} = -e $self->_clonep_path;
290 $self->{clone_logged} = -e $self->_clonelog_path;
291 $self->{clone_failed} = -e $self->_clonefail_path;
292 $self->{ccrypt} = $self->{crypt};
294 $self->_properties_load;
295 return $self;
297 close F;
298 undef;
301 # $proj may not be in sane state if this returns false!
302 sub cgi_fill {
303 my $self = shift;
304 my ($gcgi) = @_;
305 my $cgi = $gcgi->cgi;
307 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
308 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
309 if ($pwd or not $self->{crypt}) {
310 $self->{crypt} = scrypt($pwd);
313 if ($pwd2 and $pwd ne $pwd2) {
314 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
317 $self->{cpwd} = $cgi->param('cpwd');
319 if ($Girocco::Config::project_owners eq 'email') {
320 $self->{email} = $gcgi->wparam('email');
321 valid_email($self->{email})
322 or $gcgi->err("Your email sure looks weird...?");
325 $self->{url} = $gcgi->wparam('url');
326 if ($self->{url}) {
327 valid_repo_url($self->{url})
328 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
331 $self->{desc} = $gcgi->wparam('desc');
332 length($self->{desc}) <= 1024
333 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
335 $self->{README} = $gcgi->wparam('README');
336 length($self->{README}) <= 8192
337 or $gcgi->err("README length &gt; 8kb!");
339 $self->{hp} = $gcgi->wparam('hp');
340 if ($self->{hp}) {
341 valid_web_url($self->{hp})
342 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
345 # FIXME: Permit only existing users
346 $self->{users} = [grep { Girocco::User::valid_name($_) } $cgi->param('user')];
348 not $gcgi->err_check;
351 sub form_defaults {
352 my $self = shift;
354 name => $self->{name},
355 email => $self->{email},
356 url => $self->{url},
357 desc => html_esc($self->{desc}),
358 README => html_esc($self->{README}),
359 hp => $self->{hp},
360 users => $self->{users},
361 notifymail => html_esc($self->{notifymail}),
362 notifyjson => html_esc($self->{notifyjson}),
363 notifycia => html_esc($self->{notifycia}),
367 sub authenticate {
368 my $self = shift;
369 my ($gcgi) = @_;
371 $self->{ccrypt} or die "Can't authenticate against a project with no password";
372 $self->{cpwd} ||= '';
373 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
374 $gcgi->err("Your admin password does not match!");
375 return 0;
377 return 1;
380 sub _setup {
381 my $self = shift;
382 my ($pushers) = @_;
384 $self->_mkdir_forkees;
386 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
387 if ($Girocco::Config::owning_group) {
388 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
389 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
390 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
391 } else {
392 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
394 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
395 or die "git init $self->{path} failed: $?";
396 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
397 or die "disabling receive.denyNonFastforwards failed: $?";
398 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
399 or die "disabling gc.auto failed: $?";
401 # /info must have right permissions even before the fixup job,
402 # and git init didn't do it for some reason.
403 if ($Girocco::Config::owning_group) {
404 chmod(02775, $self->{path}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
405 } else {
406 chmod(02777, $self->{path}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
409 $self->_properties_save;
410 $self->_alternates_setup;
411 $self->_ctags_setup;
412 $self->_group_add($pushers);
413 $self->_hooks_install;
414 $self->perm_initialize;
415 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
418 sub premirror {
419 my $self = shift;
421 $self->_setup(':');
422 $self->_clonep(1);
425 sub conjure {
426 my $self = shift;
428 $self->_setup;
429 $self->_nofetch(1);
432 sub clone {
433 my $self = shift;
435 unlink ($self->_clonefail_path()); # Ignore EEXIST error
436 unlink ($self->_clonelog_path()); # Ignore EEXIST error
438 use IO::Socket;
439 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
440 $sock->print("clone ".$self->{name}."\n");
441 # Just ignore reply, we are going to succeed anyway and the I/O
442 # would apparently get quite hairy.
443 $sock->flush();
444 sleep 2; # *cough*
445 $sock->close();
448 sub update {
449 my $self = shift;
451 $self->_properties_save;
452 $self->_group_update;
454 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
455 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
456 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
457 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
462 sub update_password {
463 my $self = shift;
464 my ($pwd) = @_;
466 $self->{crypt} = scrypt($pwd);
467 $self->_group_update;
470 # You can explicitly do this just on a ghost() repository too.
471 sub delete {
472 my $self = shift;
474 if (-d $self->{path}) {
475 system('rm', '-rf', $self->{path}) == 0
476 or die "rm -rf $self->{path} failed: $?";
478 $self->_group_remove;
481 sub has_forks {
482 my $self = shift;
484 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
488 ### static methods
490 sub get_forkee_name {
491 $_ = $_[0];
492 (m#^(.*)/.*?$#)[0]; #
494 sub get_forkee_path {
495 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
496 -d $forkee ? $forkee : '';
499 sub valid_name {
500 $_ = $_[0];
501 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
502 and (not m#\./#)
503 and (not m#/$#)
504 and m#^[a-zA-Z0-9+./_-]+$#;
507 sub does_exist {
508 my ($name) = @_;
509 valid_name($name) or die "tried to query for project with invalid name $name!";
510 (-d $Girocco::Config::reporoot."/$name.git");