8 my ( $bin_path, $jail_path, $repomgr_path, $repodata_path, $repo_path );
9 my ( $site_domain, $group_file, $user_file, $sshkeys_path );
12 our @ISA = qw(Exporter);
13 our @EXPORT = qw(genpwd scrypt html_esc jailed_file
15 filedb_atomic_append filedb_atomic_edit
16 proj_get_forkee_name proj_get_forkee_path
17 valid_proj_name valid_user_name valid_email valid_repo_url valid_web_url);
19 use CGI
qw(:standard :escapeHTML -nosticky);
20 use CGI
::Util
qw(unescape);
21 use CGI
::Carp
qw(fatalsToBrowser);
22 use Digest
::SHA1
qw(sha1_hex);
23 use Git
::RepoCGI
::Config
;
25 $bin_path = $Git::RepoCGI
::Config
::defaults
{bin_path
};
26 $jail_path = $Git::RepoCGI
::Config
::defaults
{jail_path
};
27 $repomgr_path = $Git::RepoCGI
::Config
::defaults
{repomgr_path
};
28 $repodata_path = $Git::RepoCGI
::Config
::defaults
{repodata_path
};
29 $repo_path = $Git::RepoCGI
::Config
::defaults
{repo_path
};
31 $group_file = $Git::RepoCGI
::Config
::defaults
{group_path
};
32 $user_file = $Git::RepoCGI
::Config
::defaults
{user_file
};
33 $sshkeys_path = $Git::RepoCGI
::Config
::defaults
{sshkeys_path
};
35 $site_domain = $Git::RepoCGI
::Config
::defaults
{site_domain
};
37 $ENV{PATH
} = $bin_path . ':' . $ENV{PATH
};
48 $repo->{cgi
} = CGI
->new;
50 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
53 <?xml version="1.0" encoding="utf-8"?>
54 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
55 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
58 <title>$site_domain :: $heading</title>
59 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
60 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
65 <div class="page_header">
66 <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>
67 <a href="/">$site_domain</a> / administration / $heading
78 my $cginame = $cgi->url(-absolute
=> 1);
80 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
83 <a href="http://$site_domain/w/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
100 print "<p>", @_, "</p>\n" if @_;
106 print "<p style=\"color: red\">@_</p>\n";
112 my $err = $self->{err
};
113 $err and print "<p style=\"font-weight: bold\">Operation aborted due to $err errors.</p>\n";
118 my ( $self, $param ) = @_;
119 $self->{cgi
}->param($param) || '';
123 my $val = $self->sparam(@_);
124 $val =~ s/^\s*(.*?)\s*$/$1/;
129 ### Random utility functions
133 substr(crypt(rand, rand), 2);
138 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
144 $str =~ s/</</g; $str =~ s/>/>/g;
145 $str =~ s/"/"/g;
151 "$jail_path/$filename";
159 use Errno
qw(EEXIST);
160 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
162 my $handle = new IO
::Handle
;
164 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
166 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
167 ($! == EEXIST
) or die "$path open failed: $!";
168 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
172 # XXX: filedb-specific
173 chmod 0664, $path or die "$path g+w failed: $!";
181 rename "$path.lock", $path or die "$path unlock failed: $!";
184 sub filedb_atomic_append
{
185 my ($file, $line) = @_;
188 open my $src, $file or die "$file open for reading failed: $!";
189 my $dst = lock_file
($file);
192 my $aid = (split /:/)[2];
193 $id = $aid + 1 if ($aid >= $id);
195 print $dst $_ or die "$file(l) write failed: $!";
198 $line =~ s/\\i/$id/g;
199 print $dst "$line\n" or die "$file(l) write failed: $!";
201 close $dst or die "$file(l) close failed: $!";
209 sub filedb_atomic_edit
{
210 my ($file, $fn) = @_;
212 open my $src, $file or die "$file open for reading failed: $!";
213 my $dst = lock_file
($file);
216 print $dst $fn->($_) or die "$file(l) write failed: $!";
219 close $dst or die "$file(l) close failed: $!";
225 sub proj_get_forkee_name
{
229 sub proj_get_forkee_path
{
230 my $forkee = $repo_path . proj_get_forkee_name
($_[0]).'.git';
231 -d
$forkee ?
$forkee : '';
233 sub valid_proj_name
{
235 (not m
#/# or -d proj_get_forkee_path($_)) # will also catch ^/
238 and m
#^[a-zA-Z0-9+./_-]+$#;
240 sub valid_user_name
{
242 /^[a-zA-Z0-9+._-]+$/;
246 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
250 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?(#[a-zA-Z0-9._-]+)?$/;
254 /^http:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/ or
255 /^git:\/\
/[a-zA-Z0-9-.]+(\/[_\
%a-zA
-Z0
-9.\
/~-]*)?$/;
259 my ( $self, $name ) = @_;
261 $self->bye("I need the project name as an argument now.")
264 $self->bye("Invalid project name. Go away, sorcerer.")
265 unless valid_proj_name
($name);
267 $self->bye("Sorry but this project does not exist. "
268 . "Now, how did you <em>get</em> here?!")
269 unless Git
::RepoCGI
::Project
::does_exist
($name);
271 $self->bye("Sorry but your project has not finished mirroring yet. "
272 . "If this process takes excessive time, "
273 . "please inform the site administrator.")
274 unless Git
::RepoCGI
::Project
::available
($name);
276 my $proj = Git
::RepoCGI
::Project
->load($name);
277 die "not found project $name, that's really weird!" unless $proj;
282 my ( $self, $name ) = @_;
283 $self->bye("User '$name' is not registered.")
284 unless valid_user_name
($name)
285 && Git
::RepoCGI
::User
::does_exist
($name);
287 my $user = Git
::RepoCGI
::User
->load($name);
288 $self->bye("User '$name' failed to load") unless $user;
294 package Git
::RepoCGI
::Project
;
296 BEGIN { use Git
::RepoCGI
; }
300 my @pelems = split('/', $self->{name
});
301 pop @pelems; # do not create dir for the project itself
302 my $path = $self->{base_path
};
303 foreach my $pelem (@pelems) {
305 (-d
"$path") or mkdir $path or die "mkdir $path: $!";
306 chmod 0775, $path; # ok if fails (dir may already exist and be owned by someone else)
313 desc
=> 'description',
314 README
=> 'README.html',
321 $self->{path
}.'/'.$name;
327 $propmap{$name} or die "unknown property: $name";
328 open P
, $self->_property_path($propmap{$name}) or return undef;
331 my $value = join('', @value); chomp $value;
337 my ($name, $value) = @_;
338 $propmap{$name} or die "unknown property: $name";
340 my $P = lock_file
($self->_property_path($propmap{$name}));
341 $value ne '' and print $P "$value\n";
343 unlock_file
($self->_property_path($propmap{$name}));
346 sub _properties_load
{
348 foreach my $prop (keys %propmap) {
349 $self->{$prop} = $self->_property_fget($prop);
353 sub _properties_save
{
355 foreach my $prop (keys %propmap) {
356 $self->_property_fput($prop, $self->{$prop});
362 $self->_property_path('.nofetch');
368 my $np = $self->_nofetch_path;
370 open X
, '>'.$np or die "nofetch failed: $!";
373 unlink $np or die "yesfetch failed: $!";
377 sub _alternates_setup
{
379 return unless $self->{name
} =~ m
#/#;
380 my $forkee_name = proj_get_forkee_name
($self->{name
});
381 my $forkee_path = proj_get_forkee_path
($self->{name
});
382 return unless -d
$forkee_path;
383 mkdir $self->{path
}.'/refs'; chmod 0775, $self->{path
}.'/refs';
384 mkdir $self->{path
}.'/objects'; chmod 0775, $self->{path
}.'/objects';
385 mkdir $self->{path
}.'/objects/info'; chmod 0775, $self->{path
}.'/objects/info';
387 # We set up both alternates and http_alternates since we cannot use
388 # relative path in alternates - that doesn't work recursively.
390 my $filename = $self->{path
}.'/objects/info/alternates';
391 open X
, '>'.$filename or die "alternates failed: $!";
392 print X
"$forkee_path/objects\n";
394 chmod 0664, $filename or warn "cannot chmod $filename: $!";
396 $filename = $self->{path
}.'/objects/info/http-alternates';
397 open X
, '>'.$filename or die "http-alternates failed: $!";
398 my $upfork = $forkee_name;
399 do { print X
"/r/$upfork.git/objects\n"; } while ($upfork =~ s
#/?.+?$## and $upfork);
401 chmod 0664, $filename or warn "cannot chmod $filename: $!";
403 symlink "$forkee_path/refs", $self->{path
}.'/refs/forkee';
408 mkdir $self->{path
}.'/ctags'; chmod 0775, $self->{path
}.'/ctags';
414 $xtra .= join(',', @
{$self->{users
}});
415 filedb_atomic_append
(jailed_file
($group_file),
416 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
421 my $xtra = join(',', @
{$self->{users
}});
422 filedb_atomic_edit
(jailed_file
($group_file),
426 if ($self->{name
} eq (split /:/)[0]) {
427 # preserve readonly flag
428 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
429 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
439 filedb_atomic_edit
(jailed_file
($group_file),
441 $self->{name
} ne (split /:/)[0] and return $_;
449 $self->{path
}.'/hooks/'.$name;
455 open SRC
, "$repomgr_path/$name-hook" or die "cannot open hook $name: $!";
456 open DST
, '>'.$self->_hook_path($name) or die "cannot open hook $name for writing: $!";
457 while (<SRC
>) { print DST
$_; }
460 chmod 0775, $self->_hook_path($name) or die "cannot chmod hook $name: $!";
465 foreach my $hook ('update') {
466 $self->_hook_install($hook);
470 # private constructor, do not use
473 my ($name, $base_path, $path) = @_;
474 valid_proj_name
($name) or die "refusing to create project with invalid name ($name)!";
475 $path ||= "$base_path/$name.git";
476 my $proj = { name
=> $name, base_path
=> $base_path, path
=> $path };
481 # public constructor #0
482 # creates a virtual project not connected to disk image
483 # you can conjure() it later to disk
486 my ($name, $mirror) = @_;
488 my $path = $mirror ?
"$repodata_path/to-clone" : $repo_path;
489 my $repo = "$path/$name";
490 $repo .= '.git' unless $mirror;
492 my $self = $class->_new($name, $path, $repo);
494 $self->{mirror
} = $mirror;
498 # public constructor #1
503 open F
, jailed_file
($group_file) or die "project load failed: $!";
507 next unless (shift eq $name);
509 my $self = $class->_new($name, $repo_path);
510 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
513 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
515 $self->{users
} = [split /,/, $ulist];
516 $self->{mirror
} = ! -e
$self->_nofetch_path;
517 $self->{ccrypt
} = $self->{crypt};
519 $self->_properties_load;
526 # $proj may not be in sane state if this returns false!
530 my $cgi = $repo->cgi;
532 my $pwd = $cgi->param('pwd');
533 if ($pwd ne '' or not $self->{crypt}) {
534 $self->{crypt} = scrypt
($pwd);
537 if ($cgi->param('pwd2') and $pwd ne $cgi->param('pwd2')) {
538 $repo->err("Our high-paid security consultants have determined that the admin passwords you have entered do not match each other.");
541 $self->{cpwd
} = $cgi->param('cpwd');
543 $self->{email
} = $repo->wparam('email');
544 valid_email
($self->{email
})
545 or $repo->err("Your email sure looks weird...?");
547 $self->{url
} = $repo->wparam('url');
549 valid_repo_url
($self->{url
})
550 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
553 $self->{desc
} = $repo->wparam('desc');
554 length($self->{desc
}) <= 1024
555 or $repo->err("<b>Short</b> description length > 1kb!");
557 $self->{README
} = $repo->wparam('README');
558 length($self->{README
}) <= 8192
559 or $repo->err("README length > 8kb!");
561 $self->{hp
} = $repo->wparam('hp');
563 valid_web_url
($self->{hp
})
564 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
567 # FIXME: Permit only existing users
568 $self->{users
} = [grep { valid_user_name
($_) } $cgi->param('user')];
570 not $repo->err_check;
576 name
=> $self->{name
},
577 email
=> $self->{email
},
579 desc
=> html_esc
($self->{desc
}),
580 README
=> html_esc
($self->{README
}),
582 users
=> $self->{users
},
590 $self->{ccrypt
} or die "Can't authenticate against a project with no password";
591 $self->{cpwd
} or $repo->err("No password entered.");
592 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
593 $repo->err("Your admin password does not match!");
602 $self->_mkdir_forkees;
603 mkdir $self->{path
} or die "mkdir failed: $!";
604 chmod 0775, $self->{path
} or die "chmod failed: $!";
605 $self->_properties_save;
606 $self->_alternates_setup;
608 $self->_group_add(':');
614 $self->_mkdir_forkees;
615 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
616 or die "cg-admin-setuprepo failed: $?";
617 system('git', '--git-dir='.$self->{path
}, 'config', 'receive.denyNonFastforwards', 'false');
619 $self->_properties_save;
620 $self->_alternates_setup;
623 $self->_hooks_install;
629 $self->_properties_save;
630 $self->_group_update;
633 sub update_password
{
637 $self->{crypt} = scrypt
($pwd);
638 $self->_group_update;
641 # You can explicitly do this just on a ghost() repository too.
645 if (-d
$self->{path
}) {
646 system('rm', '-r', $self->{path
}) == 0
647 or die "rm -r failed: $?";
649 $self->_group_remove;
655 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
657 or -d
"$repodata_path/cloning/$name"
658 or -d
"$repodata_path/to-clone/$name");
662 valid_proj_name
($name) or die "tried to query for project with invalid name $name!";
663 (-d
"$repo_path/$name.git");
669 package Git
::RepoCGI
::User
;
671 BEGIN { use Git
::RepoCGI
; }
675 join(':', $self->{name
}, $self->{crypt}, $self->{uid
}, 65534, $self->{email
}, '/', '/bin/git-shell')
680 my $xtra = join(',', @
{$self->{users
}});
681 filedb_atomic_edit
(jailed_file
($user_file),
685 if ($self->{name
} eq (split /:/)[0]) {
686 return $self->_passwd_text . "\n";
697 filedb_atomic_append
(jailed_file
($user_file), $self->_passwd_text);
700 sub update_password
{
704 $self->{crypt} = scrypt
($pwd);
711 $self->{ccrypt
} or die "Can't authenticate against a user with no password";
712 $self->{cpwd
} or $repo->err("No password entered.");
713 unless ($self->{ccrypt
} eq crypt($self->{cpwd
}, $self->{ccrypt
})) {
714 $repo->err("Your admin password does not match!");
722 $sshkeys_path . '/' . $self->{name
};
727 open F
, "<".jailed_file
($self->_sshkey_path) or die "sshkey load failed: $!";
732 if (/^ssh-(?:dss|rsa) /) {
734 } elsif (/^# REPOAUTH ([0-9a-f]+) (\d+)/) {
736 $auth = $1 unless (time >= $expire);
740 my $keys = join('', @keys); chomp $keys;
746 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
747 if (defined($self->{auth
}) && $self->{auth
}) {
748 my $expire = time + 24 * 3600;
749 print F
"# REPOAUTH $self->{auth} $expire\n";
751 print F
$self->{keys}."\n";
753 chmod 0664, jailed_file
($self->_sshkey_path);
756 # private constructor, do not use
760 valid_user_name
($name) or die "refusing to create user with invalid name ($name)!";
761 my $proj = { name
=> $name };
766 # public constructor #0
767 # creates a virtual user not connected to disk record
768 # you can conjure() it later to disk
772 my $self = $class->_new($name);
776 # public constructor #1
781 open F
, jailed_file
($user_file) or die "user load failed: $!";
785 next unless (shift eq $name);
787 my $self = $class->_new($name);
789 ($self->{crypt}, $self->{uid
}, undef, $self->{email
}) = @_;
790 ($self->{keys}, $self->{auth
}) = $self->_sshkey_load;
791 $self->{ccrypt
} = $self->{crypt};
799 # $user may not be in sane state if this returns false!
804 $self->{name
} = $repo->wparam('name');
805 valid_user_name
($self->{name
})
806 or $repo->err("Name contains invalid characters.");
808 $self->{email
} = $repo->wparam('email');
809 valid_email
($self->{email
})
810 or $repo->err("Your email sure looks weird...?");
812 my $pwd = $repo->sparam('pwd');
813 if ($pwd ne '' or not $self->{crypt}) {
814 $self->{crypt} = scrypt
($pwd);
817 if ($repo->sparam('pwd2') and $pwd ne $repo->sparam('pwd2')) {
818 $repo->err("Our high-paid security consultants have determined that the passwords you have entered do not match each other.");
821 $self->{cpwd
} = $repo->sparam('cpwd');
823 $self->keys_fill($repo);
829 my $cgi = $repo->cgi;
831 $self->{keys} = $cgi->param('keys');
832 length($self->{keys}) <= 4096
833 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
834 foreach (split /\r?\n/, $self->{keys}) {
835 /^ssh-(?:dss|rsa) .* \S+@\S+$/ or $repo->err("Your ssh key (\"$_\") appears to have invalid format (does not start by ssh-dss|rsa or does not end with @-identifier) - maybe your browser has split a single key to multiple lines?");
838 not $repo->err_check;
850 $self->{auth
} = Digest
::SHA1
::sha1_hex
(time . $$ . rand() . $self->{keys});
858 delete $self->{auth
};
871 valid_user_name
($name) or die "tried to query for user with invalid name $name!";
872 (-e jailed_file
("$sshkeys_path/$name"));