Girocco::Project: when editing user list, filter non-existent users
[girocco/msimkins.git] / Girocco / Project.pm
blobe4bd91c233ef8e4800843750962daa8a2f46d115
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 - <a href="http://help.github.com/post-receive-hooks/">POST JSON</a> at', 'notifyjson', 'text'],
21 notifycia => ['Commit notify - <a href="http://cia.vc/doc/">CIA project</a> 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 return;
84 } elsif ($pname =~ s/^%//) {
85 system('git', '--git-dir='.$self->{path}, 'config', $pname, $value);
86 return;
89 my $P = lock_file($self->_property_path($pname));
90 $value ne '' and print $P "$value\n";
91 close $P;
92 unlock_file($self->_property_path($pname));
95 sub _properties_load {
96 my $self = shift;
97 foreach my $prop (keys %propmap) {
98 $self->{$prop} = $self->_property_fget($prop);
102 sub _properties_save {
103 my $self = shift;
104 foreach my $prop (keys %propmap) {
105 $self->_property_fput($prop, $self->{$prop});
109 sub _nofetch_path {
110 my $self = shift;
111 $self->_property_path('.nofetch');
114 sub _nofetch {
115 my $self = shift;
116 my ($nofetch) = @_;
117 my $np = $self->_nofetch_path;
118 if ($nofetch) {
119 open X, '>'.$np or die "nofetch failed: $!";
120 close X;
121 } else {
122 unlink $np or die "yesfetch failed: $!";
126 sub _clonelog_path {
127 my $self = shift;
128 $self->_property_path('.clonelog');
131 sub _clonefail_path {
132 my $self = shift;
133 $self->_property_path('.clone_failed');
136 sub _clonep_path {
137 my $self = shift;
138 $self->_property_path('.clone_in_progress');
141 sub _clonep {
142 my $self = shift;
143 my ($nofetch) = @_;
144 my $np = $self->_clonep_path;
145 if ($nofetch) {
146 open X, '>'.$np or die "clonep failed: $!";
147 close X;
148 } else {
149 unlink $np or die "clonef failed: $!";
152 sub _alternates_setup {
153 my $self = shift;
154 return unless $self->{name} =~ m#/#;
155 my $forkee_name = get_forkee_name($self->{name});
156 my $forkee_path = get_forkee_path($self->{name});
157 return unless -d $forkee_path;
158 mkdir $self->{path}.'/refs'; chmod 0775, $self->{path}.'/refs';
159 mkdir $self->{path}.'/objects'; chmod 0775, $self->{path}.'/objects';
160 mkdir $self->{path}.'/objects/info'; chmod 0775, $self->{path}.'/objects/info';
162 # We set up both alternates and http_alternates since we cannot use
163 # relative path in alternates - that doesn't work recursively.
165 my $filename = $self->{path}.'/objects/info/alternates';
166 open X, '>'.$filename or die "alternates failed: $!";
167 print X "$forkee_path/objects\n";
168 close X;
169 chmod 0664, $filename or warn "cannot chmod $filename: $!";
171 if ($Girocco::Config::httppullurl) {
172 $filename = $self->{path}.'/objects/info/http-alternates';
173 open X, '>'.$filename or die "http-alternates failed: $!";
174 my $upfork = $forkee_name;
175 do { print X "$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s#/?.+?$## and $upfork); #
176 close X;
177 chmod 0664, $filename or warn "cannot chmod $filename: $!";
180 # The symlink is problematic since git remote prune will traverse it.
181 #symlink "$forkee_path/refs", $self->{path}.'/refs/forkee';
183 # copy refs from parent project
184 system("$Girocco::Config::git_bin --git-dir=$forkee_path show-ref > $self->{path}/packed-refs");
185 # initialize HEAD, hacky version
186 system("cp $forkee_path/HEAD $self->{path}/HEAD");
189 sub _ctags_setup {
190 my $self = shift;
191 mkdir $self->{path}.'/ctags'; chmod 01777, $self->{path}.'/ctags';
194 sub _group_add {
195 my $self = shift;
196 my ($xtra) = @_;
197 $xtra .= join(',', @{$self->{users}});
198 filedb_atomic_append(jailed_file('/etc/group'),
199 join(':', $self->{name}, $self->{crypt}, '\i', $xtra));
202 sub _group_update {
203 my $self = shift;
204 my $xtra = join(',', @{$self->{users}});
205 filedb_atomic_edit(jailed_file('/etc/group'),
206 sub {
207 $_ = $_[0];
208 chomp;
209 if ($self->{name} eq (split /:/)[0]) {
210 # preserve readonly flag
211 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
212 return join(':', $self->{name}, $self->{crypt}, $self->{gid}, $xtra)."\n";
213 } else {
214 return "$_\n";
220 sub _group_remove {
221 my $self = shift;
222 filedb_atomic_edit(jailed_file('/etc/group'),
223 sub {
224 $self->{name} ne (split /:/)[0] and return $_;
229 sub _hook_path {
230 my $self = shift;
231 my ($name) = @_;
232 $self->{path}.'/hooks/'.$name;
235 sub _hook_install {
236 my $self = shift;
237 my ($name) = @_;
238 open SRC, "$Girocco::Config::basedir/hooks/$name" or die "cannot open hook $name: $!";
239 open DST, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
240 while (<SRC>) { print DST $_; }
241 close DST;
242 close SRC;
243 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
246 sub _hooks_install {
247 my $self = shift;
248 foreach my $hook ('post-receive', 'update', 'post-update') {
249 $self->_hook_install($hook);
253 # private constructor, do not use
254 sub _new {
255 my $class = shift;
256 my ($name, $base_path, $path) = @_;
257 valid_name($name) or die "refusing to create project with invalid name ($name)!";
258 $path ||= "$base_path/$name.git";
259 my $proj = { name => $name, base_path => $base_path, path => $path };
261 bless $proj, $class;
264 # public constructor #0
265 # creates a virtual project not connected to disk image
266 # you can conjure() it later to disk
267 sub ghost {
268 my $class = shift;
269 my ($name, $mirror) = @_;
270 my $self = $class->_new($name, $Girocco::Config::reporoot);
271 $self->{users} = [];
272 $self->{mirror} = $mirror;
273 $self;
276 # public constructor #1
277 sub load {
278 my $class = shift;
279 my ($name) = @_;
281 open F, jailed_file("/etc/group") or die "project load failed: $!";
282 while (<F>) {
283 chomp;
284 @_ = split /:+/;
285 next unless (shift eq $name);
287 my $self = $class->_new($name, $Girocco::Config::reporoot);
288 (-d $self->{path}) or die "invalid path (".$self->{path}.") for project ".$self->{name};
290 my $ulist;
291 ($self->{crypt}, $self->{gid}, $ulist) = @_;
292 $ulist ||= '';
293 $self->{users} = [split /,/, $ulist];
294 $self->{orig_users} = [@{$self->{users}}];
295 $self->{mirror} = ! -e $self->_nofetch_path;
296 $self->{clone_in_progress} = -e $self->_clonep_path;
297 $self->{clone_logged} = -e $self->_clonelog_path;
298 $self->{clone_failed} = -e $self->_clonefail_path;
299 $self->{ccrypt} = $self->{crypt};
301 $self->_properties_load;
302 return $self;
304 close F;
305 undef;
308 # $proj may not be in sane state if this returns false!
309 sub cgi_fill {
310 my $self = shift;
311 my ($gcgi) = @_;
312 my $cgi = $gcgi->cgi;
314 my ($pwd, $pwd2) = ($cgi->param('pwd'), $cgi->param('pwd2'));
315 $pwd ||= ''; $pwd2 ||= ''; # in case passwords are disabled
316 if ($pwd or not $self->{crypt}) {
317 $self->{crypt} = scrypt($pwd);
320 if ($pwd2 and $pwd ne $pwd2) {
321 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
324 $self->{cpwd} = $cgi->param('cpwd');
326 if ($Girocco::Config::project_owners eq 'email') {
327 $self->{email} = $gcgi->wparam('email');
328 valid_email($self->{email})
329 or $gcgi->err("Your email sure looks weird...?");
332 $self->{url} = $gcgi->wparam('url');
333 if ($self->{url}) {
334 valid_repo_url($self->{url})
335 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
338 $self->{desc} = $gcgi->wparam('desc');
339 length($self->{desc}) <= 1024
340 or $gcgi->err("<b>Short</b> description length &gt; 1kb!");
342 $self->{README} = $gcgi->wparam('README');
343 length($self->{README}) <= 8192
344 or $gcgi->err("README length &gt; 8kb!");
346 $self->{hp} = $gcgi->wparam('hp');
347 if ($self->{hp}) {
348 valid_web_url($self->{hp})
349 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
352 $self->{users} = [grep { Girocco::User::valid_name($_) && Girocco::User::does_exist($_) } $cgi->param('user')];
354 $self->{notifymail} = $gcgi->wparam('notifymail');
355 if ($self->{notifymail}) {
356 (valid_email_multi($self->{notifymail}) and length($self->{notifymail}) <= 512)
357 or $gcgi->err("Invalid notify e-mail address. Use mail,mail to specify multiple addresses; total length must not exceed 512 characters, however.");
360 $self->{notifyjson} = $gcgi->wparam('notifyjson');
361 if ($self->{notifyjson}) {
362 valid_web_url($self->{notifyjson})
363 or $gcgi->err("Invalid JSON notify URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
366 $self->{notifycia} = $gcgi->wparam('notifycia');
367 if ($self->{notifycia}) {
368 $self->{notifycia} =~ /^[a-zA-Z0-9._-]+$/
369 or $gcgi->err("Overly suspicious CIA notify project name. If it is actually valid, contact me.");
372 not $gcgi->err_check;
375 sub form_defaults {
376 my $self = shift;
378 name => $self->{name},
379 email => $self->{email},
380 url => $self->{url},
381 desc => html_esc($self->{desc}),
382 README => html_esc($self->{README}),
383 hp => $self->{hp},
384 users => $self->{users},
385 notifymail => html_esc($self->{notifymail}),
386 notifyjson => html_esc($self->{notifyjson}),
387 notifycia => html_esc($self->{notifycia}),
391 sub authenticate {
392 my $self = shift;
393 my ($gcgi) = @_;
395 $self->{ccrypt} or die "Can't authenticate against a project with no password";
396 $self->{cpwd} ||= '';
397 unless ($self->{ccrypt} eq crypt($self->{cpwd}, $self->{ccrypt})) {
398 $gcgi->err("Your admin password does not match!");
399 return 0;
401 return 1;
404 sub _setup {
405 my $self = shift;
406 my ($pushers) = @_;
408 $self->_mkdir_forkees;
410 mkdir($self->{path}) or die "mkdir $self->{path} failed: $!";
411 if ($Girocco::Config::owning_group) {
412 my $gid = scalar(getgrnam($Girocco::Config::owning_group));
413 chown(-1, $gid, $self->{path}) or die "chgrp $gid $self->{path} failed: $!";
414 chmod(02775, $self->{path}) or die "chmod 2775 $self->{path} failed: $!";
415 } else {
416 chmod(02777, $self->{path}) or die "chmod 2777 $self->{path} failed: $!";
418 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
419 or die "git init $self->{path} failed: $?";
420 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'receive.denyNonFastforwards', 'false') == 0
421 or die "disabling receive.denyNonFastforwards failed: $?";
422 system($Girocco::Config::git_bin, '--git-dir='.$self->{path}, 'config', 'gc.auto', '0') == 0
423 or die "disabling gc.auto failed: $?";
425 # /info must have right permissions even before the fixup job,
426 # and git init didn't do it for some reason.
427 if ($Girocco::Config::owning_group) {
428 chmod(02775, $self->{path}."/info") or die "chmod 2775 $self->{path}/info failed: $!";
429 } else {
430 chmod(02777, $self->{path}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
433 $self->_properties_save;
434 $self->_alternates_setup;
435 $self->_ctags_setup;
436 $self->_group_add($pushers);
437 $self->_hooks_install;
438 $self->perm_initialize;
439 system($Girocco::Config::basedir . '/gitweb/genindex.sh');
442 sub premirror {
443 my $self = shift;
445 $self->_setup(':');
446 $self->_clonep(1);
449 sub conjure {
450 my $self = shift;
452 $self->_setup;
453 $self->_nofetch(1);
456 sub clone {
457 my $self = shift;
459 unlink ($self->_clonefail_path()); # Ignore EEXIST error
460 unlink ($self->_clonelog_path()); # Ignore EEXIST error
462 use IO::Socket;
463 my $sock = IO::Socket::UNIX->new($Girocco::Config::chroot.'/etc/taskd.socket') or die "cannot connect to taskd.socket: $!";
464 $sock->print("clone ".$self->{name}."\n");
465 # Just ignore reply, we are going to succeed anyway and the I/O
466 # would apparently get quite hairy.
467 $sock->flush();
468 sleep 2; # *cough*
469 $sock->close();
472 sub update {
473 my $self = shift;
475 $self->_properties_save;
476 $self->_group_update;
478 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users} } $self->{users};
479 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users} } $self->{orig_users};
480 $self->perm_user_add($_, Girocco::User::resolve_uid($_)) foreach (@users_add);
481 $self->perm_user_del($_, Girocco::User::resolve_uid($_)) foreach (@users_del);
486 sub update_password {
487 my $self = shift;
488 my ($pwd) = @_;
490 $self->{crypt} = scrypt($pwd);
491 $self->_group_update;
494 # You can explicitly do this just on a ghost() repository too.
495 sub delete {
496 my $self = shift;
498 if (-d $self->{path}) {
499 system('rm', '-rf', $self->{path}) == 0
500 or die "rm -rf $self->{path} failed: $?";
502 $self->_group_remove;
505 sub has_forks {
506 my $self = shift;
508 return glob($Girocco::Config::reporoot.'/'.$self->{name}.'/*');
512 ### static methods
514 sub get_forkee_name {
515 $_ = $_[0];
516 (m#^(.*)/.*?$#)[0]; #
518 sub get_forkee_path {
519 my $forkee = $Girocco::Config::reporoot.'/'.get_forkee_name($_[0]).'.git';
520 -d $forkee ? $forkee : '';
523 sub valid_name {
524 $_ = $_[0];
525 (not m#/# or -d get_forkee_path($_)) # will also catch ^/
526 and (not m#\./#)
527 and (not m#/$#)
528 and m#^[a-zA-Z0-9+./_-]+$#;
531 sub does_exist {
532 my ($name) = @_;
533 valid_name($name) or die "tried to query for project with invalid name $name!";
534 (-d $Girocco::Config::reporoot."/$name.git");
537 sub get_full_list {
538 my $class = shift;
539 my @projects;
541 open F, jailed_file("/etc/group") or die "getting project list failed: $!";
542 while (<F>) {
543 chomp;
544 @_ = split /:+/;
545 next if ($_[2] < 65536);
547 push @projects, $_[0];
549 close F;
550 @projects;