1 package Girocco
::Project
;
10 use Girocco
::ProjPerm
;
12 use base
('Girocco::ProjPerm::'.$Girocco::Config
::permission_control
); # mwahaha
17 my @pelems = split('/', $self->{name
});
18 pop @pelems; # do not create dir for the project itself
19 my $path = $self->{base_path
};
20 foreach my $pelem (@pelems) {
22 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
23 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
30 desc
=> 'description',
31 README
=> 'README.html',
38 $self->{path
}.'/'.$name;
44 $propmap{$name} or die "unknown property: $name";
45 open P
, $self->_property_path($propmap{$name}) or return undef;
48 my $value = join('', @value); chomp $value;
54 my ($name, $value) = @_;
55 $propmap{$name} or die "unknown property: $name";
58 my $P = lock_file
($self->_property_path($propmap{$name}));
59 $value ne '' and print $P "$value\n";
61 unlock_file
($self->_property_path($propmap{$name}));
64 sub _properties_load
{
66 foreach my $prop (keys %propmap) {
67 $self->{$prop} = $self->_property_fget($prop);
71 sub _properties_save
{
73 foreach my $prop (keys %propmap) {
74 $self->_property_fput($prop, $self->{$prop});
80 $self->_property_path('.nofetch');
86 my $np = $self->_nofetch_path;
88 open X
, '>'.$np or die "nofetch failed: $!";
91 unlink $np or die "yesfetch failed: $!";
97 $self->_property_path('.clonelog');
100 sub _clonefail_path
{
102 $self->_property_path('.clone_failed');
107 $self->_property_path('.clone_in_progress');
113 my $np = $self->_clonep_path;
115 open X
, '>'.$np or die "clonep failed: $!";
118 unlink $np or die "clonef failed: $!";
121 sub _alternates_setup
{
123 return unless $self->{name
} =~ m
#/#;
124 my $forkee_name = get_forkee_name
($self->{name
});
125 my $forkee_path = get_forkee_path
($self->{name
});
126 return unless -d
$forkee_path;
127 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
128 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
129 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
131 # We set up both alternates and http_alternates since we cannot use
132 # relative path in alternates - that doesn't work recursively.
134 my $filename = $self->{path
}.'/objects/info/alternates';
135 open X
, '>'.$filename or die "alternates failed: $!";
136 print X
"$forkee_path/objects\n";
138 chmod 0664, $filename or warn "cannot chmod $filename: $!";
140 if ($Girocco::Config
::httppullurl
) {
141 $filename = $self->{path
}.'/objects/info/http-alternates';
142 open X
, '>'.$filename or die "http-alternates failed: $!";
143 my $upfork = $forkee_name;
144 do { print X
"$Girocco::Config::httppullurl/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork); #
146 chmod 0664, $filename or warn "cannot chmod $filename: $!";
149 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
154 mkdir $self->{path
}.'/ctags'; chmod 01777, $self->{path
}.'/ctags';
160 $xtra .= join(',', @
{$self->{users
}});
161 filedb_atomic_append
(jailed_file
('/etc/group'),
162 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
167 my $xtra = join(',', @
{$self->{users
}});
168 filedb_atomic_edit
(jailed_file
('/etc/group'),
172 if ($self->{name
} eq (split /:/)[0]) {
173 # preserve readonly flag
174 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
175 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
185 filedb_atomic_edit
(jailed_file
('/etc/group'),
187 $self->{name
} ne (split /:/)[0] and return $_;
195 $self->{path
}.'/hooks/'.$name;
201 open SRC
, "$Girocco::Config::basedir/$name-hook" or die "cannot open hook $name: $!";
202 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
203 while (<SRC
>) { print DST
$_; }
206 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
211 foreach my $hook ('update') {
212 $self->_hook_install($hook);
216 # private constructor, do not use
219 my ($name, $base_path, $path) = @_;
220 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
221 $path ||= "$base_path/$name.git";
222 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
227 # public constructor #0
228 # creates a virtual project not connected to disk image
229 # you can conjure() it later to disk
232 my ($name, $mirror) = @_;
233 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
235 $self->{mirror
} = $mirror;
239 # public constructor #1
244 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
248 next unless (shift eq $name);
250 my $self = $class->_new($name, $Girocco::Config
::reporoot
);
251 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
254 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
256 $self->{users
} = [split /,/, $ulist];
257 $self->{orig_users
} = [@
{$self->{users
}}];
258 $self->{mirror
} = ! -e
$self->_nofetch_path;
259 $self->{clone_in_progress
} = -e
$self->_clonep_path;
260 $self->{clone_logged
} = -e
$self->_clonelog_path;
261 $self->{clone_failed
} = -e
$self->_clonefail_path;
262 $self->{ccrypt
} = $self->{crypt};
264 $self->_properties_load;
271 # $proj may not be in sane state if this returns false!
275 my $cgi = $gcgi->cgi;
277 my $pwd = $cgi->param('pwd');
278 if ($pwd ne '' or not $self->{crypt}) {
279 $self->{crypt} = scrypt
($pwd);
282 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
283 $gcgi->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
286 $self->{cpwd
} = $cgi->param('cpwd');
288 $self->{email
} = $gcgi->wparam('email');
289 valid_email
($self->{email
})
290 or $gcgi->err("Your email sure looks weird...?");
292 $self->{url
} = $gcgi->wparam('url');
294 valid_repo_url
($self->{url
})
295 or $gcgi->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
298 $self->{desc
} = $gcgi->wparam('desc');
299 length($self->{desc
}) <= 1024
300 or $gcgi->err("<b>Short</b> description length > 1kb!");
302 $self->{README
} = $gcgi->wparam('README');
303 length($self->{README
}) <= 8192
304 or $gcgi->err("README length > 8kb!");
306 $self->{hp
} = $gcgi->wparam('hp');
308 valid_web_url
($self->{hp
})
309 or $gcgi->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
312 # FIXME: Permit only existing users
313 $self->{users
} = [grep { Girocco
::User
::valid_name
($_) } $cgi->param('user')];
315 not $gcgi->err_check;
321 name
=> $self->{name
},
322 email
=> $self->{email
},
324 desc
=> html_esc
($self->{desc
}),
325 README
=> html_esc
($self->{README
}),
327 users
=> $self->{users
},
335 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
336 $self->{cpwd
} or $gcgi->err("No password entered.");
337 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
338 $gcgi->err("Your admin password does not match!");
348 $self->_mkdir_forkees;
350 mkdir($self->{path
}) or die "mkdir $self->{path} failed: $!";
351 my $gid = scalar(getgrnam($Girocco::Config
::owning_group
));
352 chown(-1, $gid, $self->{path
}) or die "chgrp $gid $self->{path} failed: $!";
353 chmod(02775, $self->{path
}) or die "chmod 2775 $self->{path} failed: $!";
354 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'init', '--bare', '--shared='.$self->shared_mode()) == 0
355 or die "git init $self->{path} failed: $?";
356 system($Girocco::Config
::git_bin
, '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false') == 0
357 or die "disabling receive.denyNonFastforwards failed: $?";
359 $self->_properties_save;
360 $self->_alternates_setup;
362 $self->_group_add($pushers);
363 $self->_hooks_install;
364 $self->perm_initialize;
384 unlink ($self->_clonefail_path()); # Ignore EEXIST error
387 my $sock = IO
::Socket
::UNIX
->new('/tmp/girocco.cloned') or die "cannot connect to girocco.cloned: $!";
388 $sock->print($self->{name
}.".git\n");
389 # Just ignore reply, we are going to succeed anyway and the I/O
390 # would apparently get quite hairy.
399 $self->_properties_save;
400 $self->_group_update;
402 my @users_add = grep { $a = $_; not scalar grep { $a eq $_ } $self->{orig_users
} } $self->{users
};
403 my @users_del = grep { $a = $_; not scalar grep { $a eq $_ } $self->{users
} } $self->{orig_users
};
404 $self->perm_user_add($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_add);
405 $self->perm_user_del($_, Girocco
::User
::resolve_uid
($_)) foreach (@users_del);
408 sub update_password
{
412 $self->{crypt} = scrypt
($pwd);
413 $self->_group_update;
416 # You can explicitly do this just on a ghost() repository too.
420 if (-d
$self->{path
}) {
421 system('rm', '-rf', $self->{path
}) == 0
422 or die "rm -rf $self->{path} failed: $?";
424 $self->_group_remove;
430 return glob($Girocco::Config
::reporoot
.'/'.$self->{name
}.'/*');
436 sub get_forkee_name
{
438 (m
#^(.*)/.*?$#)[0]; #
440 sub get_forkee_path
{
441 my $forkee = $Girocco::Config
::reporoot
.'/'.get_forkee_name
($_[0]).'.git';
442 -d
$forkee ?
$forkee : '';
447 (not m
#/# or -d get_forkee_path($_)) # will also catch ^/
450 and m
#^[a-zA-Z0-9+./_-]+$#;
455 valid_name
($name) or die "tried to query for project with invalid name $name!";
460 valid_name
($name) or die "tried to query for project with invalid name $name!";
461 (-d
$Girocco::Config
::reporoot
."/$name.git");