Girocco::Project::cgi_fill(): Load and verify notify parameters
[girocco/testingthisout.git] / Girocco / Project.pm
blob8f0f8e8921351fc56113bebb8db6d5288394e321
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 $self->{notifymail} = $gcgi->wparam('notifymail');
349 if ($self->{notifymail}) {
350 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
351 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
354 $self->{notifyjson} = $gcgi->wparam('notifyjson');
355 if ($self->{notifyjson}) {
356 valid_web_url($self->{notifyjson})
357 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
360 $self->{notifycia} = $gcgi->wparam('notifycia');
361 if ($self->{notifycia}) {
362 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
363 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
366 not $gcgi->err_check;
369 sub form_defaults {
370 my $self = shift;
372 name => $self->{name},
373 email => $self->{email},
374 url => $self->{url},
375 desc => html_esc($self->{desc}),
376 README => html_esc($self->{README}),
377 hp => $self->{hp},
378 users => $self->{users},
379 notifymail => html_esc($self->{notifymail}),
380 notifyjson => html_esc($self->{notifyjson}),
381 notifycia => html_esc($self->{notifycia}),
385 sub authenticate {
386 my $self = shift;
387 my ($gcgi) = @_;
389 $self->{ccrypt} or die "Can't authenticate against a project with no password";
390 $self->{cpwd} ||= '';
391 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
392 $gcgi->err("Your admin password does not match!");
393 return 0;
395 return 1;
398 sub _setup {
399 my $self = shift;
400 my ($pushers) = @_;
402 $self->_mkdir_forkees;
404 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
405 if ($Girocco::Config::owning_group) {
406 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
407 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
408 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
409 } else {
410 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
412 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
413 or die "git init $self->{path} failed: $?";
414 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
415 or die "disabling receive.denyNonFastforwards failed: $?";
416 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
417 or die "disabling gc.auto failed: $?";
419 # /info must have right permissions even before the fixup job,
420 # and git init didn't do it for some reason.
421 if ($Girocco::Config::owning_group) {
422 chmod(02775, $self->{path}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
423 } else {
424 chmod(02777, $self->{path}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
427 $self->_properties_save;
428 $self->_alternates_setup;
429 $self->_ctags_setup;
430 $self->_group_add($pushers);
431 $self->_hooks_install;
432 $self->perm_initialize;
433 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
436 sub premirror {
437 my $self = shift;
439 $self->_setup(':');
440 $self->_clonep(1);
443 sub conjure {
444 my $self = shift;
446 $self->_setup;
447 $self->_nofetch(1);
450 sub clone {
451 my $self = shift;
453 unlink ($self->_clonefail_path()); # Ignore EEXIST error
454 unlink ($self->_clonelog_path()); # Ignore EEXIST error
456 use IO::Socket;
457 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
458 $sock->print("clone ".$self->{name}."\n");
459 # Just ignore reply, we are going to succeed anyway and the I/O
460 # would apparently get quite hairy.
461 $sock->flush();
462 sleep 2; # *cough*
463 $sock->close();
466 sub update {
467 my $self = shift;
469 $self->_properties_save;
470 $self->_group_update;
472 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
473 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
474 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
475 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
480 sub update_password {
481 my $self = shift;
482 my ($pwd) = @_;
484 $self->{crypt} = scrypt($pwd);
485 $self->_group_update;
488 # You can explicitly do this just on a ghost() repository too.
489 sub delete {
490 my $self = shift;
492 if (-d $self->{path}) {
493 system('rm', '-rf', $self->{path}) == 0
494 or die "rm -rf $self->{path} failed: $?";
496 $self->_group_remove;
499 sub has_forks {
500 my $self = shift;
502 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
506 ### static methods
508 sub get_forkee_name {
509 $_ = $_[0];
510 (m#^(.*)/.*?$#)[0]; #
512 sub get_forkee_path {
513 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
514 -d $forkee ? $forkee : '';
517 sub valid_name {
518 $_ = $_[0];
519 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
520 and (not m#\./#)
521 and (not m#/$#)
522 and m#^[a-zA-Z0-9+./_-]+$#;
525 sub does_exist {
526 my ($name) = @_;
527 valid_name($name) or die "tried to query for project with invalid name $name!";
528 (-d $Girocco::Config::reporoot."/$name.git");