11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
14 filedb_atomic_append filedb_atomic_edit
15 proj_get_forkee_name proj_get_forkee_path
16 valid_proj_name valid_user_name valid_email valid_repo_url valid_web_url);
18 use CGI
qw(:standard :escapeHTML -nosticky);
19 use CGI
::Util
qw(unescape);
20 use CGI
::Carp
qw(fatalsToBrowser);
22 $ENV{PATH
} = '/home/pasky/bin:'.$ENV{PATH
};
33 $repo->{cgi
} = CGI
->new;
35 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
38 <?xml version="1.0" encoding="utf-8"?>
39 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
40 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
43 <title>repo.or.cz :: $heading</title>
44 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
45 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
50 <div class="page_header">
51 <a href="http://git.or.cz/" title="Git homepage"><img src="/git-logo.png" width="72" height="27" alt="git" style="float:right; border-width:0px;"/></a>
52 <a href="/">repo.or.cz</a> / administration / $heading
63 my $cginame = $cgi->url(-absolute
=> 1);
65 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
68 <a href="http://repo.or.cz/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
85 print "<p style=\"text-color: red\">@_</p>\n";
91 my $err = $self->{err
};
92 $err and print "<p>Operation aborted due to $err errors.</p>\n";
99 my $val = $self->{cgi
}->param($param);
100 $val =~ s/^\s*(.*?)\s*$/$1/;
105 ### Random utility functions
109 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
115 $str =~ s/</</g; $str =~ s/>/>/g;
116 $str =~ s/"/"/g;
122 "/home/repo/j/$filename";
130 use Errno
qw(EEXIST);
131 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
133 my $handle = new IO
::Handle
;
135 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
137 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
138 ($! == EEXIST
) or die "$path open failed: $!";
139 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
143 # XXX: filedb-specific
144 chmod 0664, $path or die "$path g+w failed: $!";
152 rename "$path.lock", $path or die "$path unlock failed: $!";
155 sub filedb_atomic_append
{
156 my ($file, $line) = @_;
159 open my $src, $file or die "$file open for reading failed: $!";
160 my $dst = lock_file
($file);
163 my $aid = (split /:/)[2];
164 $id = $aid + 1 if ($aid >= $id);
166 print $dst $_ or die "$file(l) write failed: $!";
169 $line =~ s/\\i/$id/g;
170 print $dst "$line\n" or die "$file(l) write failed: $!";
172 close $dst or die "$file(l) close failed: $!";
180 sub filedb_atomic_edit
{
181 my ($file, $fn) = @_;
183 open my $src, $file or die "$file open for reading failed: $!";
184 my $dst = lock_file
($file);
187 print $dst $fn->($_) or die "$file(l) write failed: $!";
190 close $dst or die "$file(l) close failed: $!";
196 sub proj_get_forkee_name
{
200 sub proj_get_forkee_path
{
201 my $forkee = '/srv/git/'.proj_get_forkee_name
($_[0]).'.git';
202 -d
$forkee ?
$forkee : '';
204 sub valid_proj_name
{
206 (not m
#/# or -d proj_get_forkee_path($_)) # will also catch ^/
209 and m
#^[a-zA-Z0-9+./_-]+$#;
211 sub valid_user_name
{
213 /^[a-zA-Z0-9+._-]+$/;
217 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
221 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
225 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
226 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
232 package Git
::RepoCGI
::Project
;
234 BEGIN { use Git
::RepoCGI
; }
238 my @pelems = split('/', $self->{name
});
239 pop @pelems; # do not create dir for the project itself
240 my $path = $self->{base_path
};
241 foreach my $pelem (@pelems) {
243 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
244 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
251 desc
=> 'description',
252 README
=> 'README.html',
259 $self->{path
}.'/'.$name;
265 $propmap{$name} or die "unknown property: $name";
266 open P
, $self->_property_path($propmap{$name}) or return undef;
269 my $value = join('', @value); chomp $value;
275 my ($name, $value) = @_;
276 $propmap{$name} or die "unknown property: $name";
278 my $P = lock_file
($self->_property_path($propmap{$name}));
279 $value ne '' and print $P "$value\n";
281 unlock_file
($self->_property_path($propmap{$name}));
284 sub _properties_load
{
286 foreach my $prop (keys %propmap) {
287 $self->{$prop} = $self->_property_fget($prop);
291 sub _properties_save
{
293 foreach my $prop (keys %propmap) {
294 $self->_property_fput($prop, $self->{$prop});
300 $self->_property_path('.nofetch');
306 my $np = $self->_nofetch_path;
308 open X
, '>'.$np or die "nofetch failed: $!";
311 unlink $np or die "yesfetch failed: $!";
315 sub _alternates_setup
{
317 return unless $self->{name
} =~ m
#/#;
318 my $forkee_name = proj_get_forkee_name
($self->{name
});
319 my $forkee_path = proj_get_forkee_path
($self->{name
});
320 return unless -d
$forkee_path;
321 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
322 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
323 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
325 # We set up both alternates and http_alternates since we cannot use
326 # relative path in alternates - that doesn't work recursively.
328 my $filename = $self->{path
}.'/objects/info/alternates';
329 open X
, '>'.$filename or die "alternates failed: $!";
330 print X
"$forkee_path/objects\n";
332 chmod 0664, $filename or warn "cannot chmod $filename: $!";
334 $filename = $self->{path
}.'/objects/info/http-alternates';
335 open X
, '>'.$filename or die "http-alternates failed: $!";
336 my $upfork = $forkee_name;
337 do { print X
"/r/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork);
339 chmod 0664, $filename or warn "cannot chmod $filename: $!";
341 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
347 $xtra .= join(',', @
{$self->{users
}});
348 filedb_atomic_append
(jailed_file
('/etc/group'),
349 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
354 my $xtra = join(',', @
{$self->{users
}});
355 filedb_atomic_edit
(jailed_file
('/etc/group'),
359 if ($self->{name
} eq (split /:/)[0]) {
360 # preserve readonly flag
361 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
362 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
372 filedb_atomic_edit
(jailed_file
('/etc/group'),
374 $self->{name
} ne (split /:/)[0] and return $_;
382 $self->{path
}.'/hooks/'.$name;
388 open SRC
, "/home/repo/repomgr/$name-hook" or die "cannot open hook $name: $!";
389 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
390 while (<SRC
>) { print DST
$_; }
393 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
398 foreach my $hook ('update') {
399 $self->_hook_install($hook);
403 # private constructor, do not use
406 my ($name, $base_path, $path) = @_;
407 valid_proj_name
($name) or die "refusing to create project with invalid name ($name)!";
408 $path ||= "$base_path/$name.git";
409 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
414 # public constructor #0
415 # creates a virtual project not connected to disk image
416 # you can conjure() it later to disk
419 my ($name, $mirror) = @_;
420 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone" : "/srv/git",
421 $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
423 $self->{mirror
} = $mirror;
427 # public constructor #1
432 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
436 next unless (shift eq $name);
438 my $self = $class->_new($name, "/srv/git");
439 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
442 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
444 $self->{users
} = [split /,/, $ulist];
445 $self->{mirror
} = ! -e
$self->_nofetch_path;
446 $self->{ccrypt
} = $self->{crypt};
448 $self->_properties_load;
455 # $proj may not be in sane state if this returns false!
459 my $cgi = $repo->cgi;
461 my $pwd = $cgi->param('pwd');
462 if ($pwd ne '' or not $self->{crypt}) {
463 $self->{crypt} = scrypt
($pwd);
466 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
467 $repo->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
470 $self->{cpwd
} = $cgi->param('cpwd');
472 $self->{email
} = $repo->wparam('email');
473 valid_email
($self->{email
})
474 or $repo->err("Your email sure looks weird...?");
476 $self->{url
} = $repo->wparam('url');
478 valid_repo_url
($self->{url
})
479 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
482 $self->{desc
} = $repo->wparam('desc');
483 length($self->{desc
}) <= 1024
484 or $repo->err("<b>Short</b> description length > 1kb!");
486 $self->{README
} = $repo->wparam('README');
487 length($self->{README
}) <= 8192
488 or $repo->err("README length > 8kb!");
490 $self->{hp
} = $repo->wparam('hp');
492 valid_web_url
($self->{hp
})
493 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
496 # FIXME: Permit only existing users
497 $self->{users
} = [grep { valid_user_name
($_) } $cgi->param('user')];
499 not $repo->err_check;
505 name
=> $self->{name
},
506 email
=> $self->{email
},
508 desc
=> html_esc
($self->{desc
}),
509 README
=> html_esc
($self->{README
}),
511 users
=> $self->{users
},
519 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
520 $self->{cpwd
} or $repo->err("No password entered.");
521 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
522 $repo->err("Your admin password does not match!");
531 $self->_mkdir_forkees;
532 mkdir $self->{path
} or die "mkdir failed: $!";
533 chmod 0775, $self->{path
} or die "chmod failed: $!";
534 $self->_properties_save;
535 $self->_alternates_setup;
536 $self->_group_add(':');
542 $self->_mkdir_forkees;
543 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
544 or die "cg-admin-setuprepo failed: $?";
545 system('git', '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false');
547 $self->_properties_save;
548 $self->_alternates_setup;
550 $self->_hooks_install;
556 $self->_properties_save;
557 $self->_group_update;
560 # You can explicitly do this just on a ghost() repository too.
564 if (-d
$self->{path
}) {
565 system('rm', '-r', $self->{path
}) == 0
566 or die "rm -r failed: $?";
568 $self->_group_remove;
574 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
576 or -d
"/home/repo/repodata/cloning/$name"
577 or -d
"/home/repo/repodata/to-clone/$name");
581 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
582 (-d
"/srv/git/$name.git");
588 package Git
::RepoCGI
::User
;
590 BEGIN { use Git
::RepoCGI
; }
594 filedb_atomic_append
(jailed_file
('/etc/passwd'),
595 join(':', $self->{name
}, 'x', '\i', 65534, $self->{email
}, '/', '/bin/git-shell'));
600 '/etc/sshkeys/'.$self->{name
};
605 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
606 print F
$self->{keys}."\n";
608 chmod 0664, jailed_file
($self->_sshkey_path);
611 # private constructor, do not use
615 valid_user_name
($name) or die "refusing to create user with invalid name ($name)!";
616 my $proj = { name
=> $name };
621 # public constructor #0
622 # creates a virtual user not connected to disk record
623 # you can conjure() it later to disk
627 my $self = $class->_new($name);
631 # $user may not be in sane state if this returns false!
635 my $cgi = $repo->cgi;
637 $self->{name
} = $repo->wparam('name');
638 valid_user_name
($self->{name
})
639 or $repo->err("Name contains invalid characters.");
641 $self->{email
} = $repo->wparam('email');
642 valid_email
($self->{email
})
643 or $repo->err("Your email sure looks weird...?");
645 $self->{keys} = $cgi->param('keys');
646 length($self->{keys}) <= 4096
647 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
649 not $repo->err_check;
662 valid_user_name
($name) or die "tried to query for user with invalid name $name!";
663 (-e jailed_file
("/etc/sshkeys/$name"));