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'],
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'],
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) {
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)
39 desc
=> 'description',
40 README
=> 'README.html',
42 notifymail
=> '%hooks.mailinglist',
43 notifyjson
=> '%hooks.jsonurl',
44 notifycia
=> '%hooks.cianame',
50 $self->{path
}.'/'.$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"`;
62 } elsif ($pname =~ s/^%//) {
63 my $val = `git --git-dir="$self->{path}" config "$pname"`;
68 open P
, $self->_property_path($pname) or return undef;
71 my $value = join('', @value); chomp $value;
77 my ($name, $value) = @_;
78 my $pname = $propmap{$name};
79 $pname or die "unknown property: $name";
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";
90 unlock_file
($self->_property_path($pname));
93 sub _properties_load
{
95 foreach my $prop (keys %propmap) {
96 $self->{$prop} = $self->_property_fget($prop);
100 sub _properties_save
{
102 foreach my $prop (keys %propmap) {
103 $self->_property_fput($prop, $self->{$prop});
109 $self->_property_path('.nofetch');
115 my $np = $self->_nofetch_path;
117 open X
, '>'.$np or die "nofetch failed: $!";
120 unlink $np or die "yesfetch failed: $!";
126 $self->_property_path('.clonelog');
129 sub _clonefail_path
{
131 $self->_property_path('.clone_failed');
136 $self->_property_path('.clone_in_progress');
142 my $np = $self->_clonep_path;
144 open X
, '>'.$np or die "clonep failed: $!";
147 unlink $np or die "clonef failed: $!";
150 sub _alternates_setup
{
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";
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); #
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';
184 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
190 $xtra .= join(',', @
{$self->{users
}});
191 filedb_atomic_append
(jailed_file
('/etc/group'),
192 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
197 my $xtra = join(',', @
{$self->{users
}});
198 filedb_atomic_edit
(jailed_file
('/etc/group'),
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";
215 filedb_atomic_edit
(jailed_file
('/etc/group'),
217 $self->{name
} ne (split /:/)[0] and return $_;
225 $self->{path
}.'/hooks/'.$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
$_; }
236 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
241 foreach my $hook ('post-receive', 'update') {
242 $self->_hook_install($hook);
246 # private constructor, do not use
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 };
257 # public constructor #0
258 # creates a virtual project not connected to disk image
259 # you can conjure() it later to disk
262 my ($name, $mirror) = @_;
263 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
265 $self->{mirror
} = $mirror;
269 # public constructor #1
274 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
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
};
284 ($self->{crypt}, $self->{gid
}, $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;
301 # $proj may not be in sane state if this returns false!
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');
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 > 1kb!");
335 $self->{README
} = $gcgi->wparam('README');
336 length($self->{README
}) <= 8192
337 or $gcgi->err("README length > 8kb!");
339 $self->{hp
} = $gcgi->wparam('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;
372 name
=> $self->{name
},
373 email
=> $self->{email
},
375 desc
=> html_esc
($self->{desc
}),
376 README
=> html_esc
($self->{README
}),
378 users
=> $self->{users
},
379 notifymail
=> html_esc
($self->{notifymail
}),
380 notifyjson
=> html_esc
($self->{notifyjson
}),
381 notifycia
=> html_esc
($self->{notifycia
}),
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!");
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: $!";
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: $!";
424 chmod(02777, $self->{path
}."/info") or die "chmod 2777 $self->{path}/info failed: $!";
427 $self->_properties_save;
428 $self->_alternates_setup;
430 $self->_group_add($pushers);
431 $self->_hooks_install;
432 $self->perm_initialize;
433 system($Girocco::Config
::basedir
. '/gitweb/genindex.sh');
453 unlink ($self->_clonefail_path()); # Ignore EEXIST error
454 unlink ($self->_clonelog_path()); # Ignore EEXIST error
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.
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
{
484 $self->{crypt} = scrypt
($pwd);
485 $self->_group_update;
488 # You can explicitly do this just on a ghost() repository too.
492 if (-d
$self->{path
}) {
493 system('rm', '-rf', $self->{path
}) == 0
494 or die "rm -rf $self->{path} failed: $?";
496 $self->_group_remove;
502 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
508 sub get_forkee_name
{
510 (m
#^(.*)/.*?$#)[0]; #
512 sub get_forkee_path
{
513 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
514 -d
$forkee ?
$forkee : '';
519 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
522 and m
#^[a-zA-Z0-9+./_-]+$#;
527 valid_name
($name) or die "tried to query for project with invalid name $name!";
528 (-d
$Girocco::Config
::reporoot
."/$name.git");