11 our @ISA = qw(Exporter);
12 our @EXPORT = qw(scrypt html_esc jailed_file
13 filedb_atomic_append filedb_atomic_edit
14 valid_name valid_email valid_repo_url valid_web_url);
16 use CGI
qw(:standard :escapeHTML -nosticky);
17 use CGI
::Util
qw(unescape);
18 use CGI
::Carp
qw(fatalsToBrowser);
29 $repo->{cgi
} = CGI
->new;
31 print $repo->{cgi
}->header(-type
=>'text/html', -charset
=> 'utf-8');
34 <?xml version="1.0" encoding="utf-8"?>
35 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
36 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
39 <title>repo.or.cz :: $heading</title>
40 <link rel="stylesheet" type="text/css" href="/gitweb.css"/>
41 <link rel="shortcut icon" href="/git-favicon.png" type="image/png"/>
46 <div class="page_header">
47 <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>
48 <a href="/">repo.or.cz</a>
50 Administration Interface
63 my $cginame = $cgi->url(-absolute
=> 1);
65 if ($cginame =~ /^[a-zA-Z0-9_.\/-]+\
.cgi
$/) {
68 <a href="http://repo.or.cz/gitweb.cgi/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;
121 "/home/repo/j/$filename";
129 use Errno
qw(EEXIST);
130 use Fcntl
qw(O_WRONLY O_CREAT O_EXCL);
132 my $handle = new IO
::Handle
;
134 unless (sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
136 while (not sysopen($handle, $path, O_WRONLY
|O_CREAT
|O_EXCL
)) {
137 ($! == EEXIST
) or die "$path open failed: $!";
138 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
142 # XXX: filedb-specific
143 chmod 0664, $path or die "$path g+w failed: $!";
151 rename "$path.lock", $path or die "$path unlock failed: $!";
154 sub filedb_atomic_append
{
155 my ($file, $line) = @_;
158 open my $src, $file or die "$file open for reading failed: $!";
159 my $dst = lock_file
($file);
162 my $aid = (split /:/)[2];
163 $id = $aid + 1 if ($aid >= $id);
165 print $dst $_ or die "$file(l) write failed: $!";
168 $line =~ s/\\i/$id/g;
169 print $dst "$line\n" or die "$file(l) write failed: $!";
171 close $dst or die "$file(l) close failed: $!";
179 sub filedb_atomic_edit
{
180 my ($file, $fn) = @_;
182 open my $src, $file or die "$file open for reading failed: $!";
183 my $dst = lock_file
($file);
186 print $dst $fn->($_) or die "$file(l) write failed: $!";
189 close $dst or die "$file(l) close failed: $!";
195 # BOTH user AND project name!
202 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
206 /^http:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+(#[a-zA-Z0-9._-]+)?$/;
210 /^http:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+$/ or
211 /^git:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+$/;
217 package Git
::RepoCGI
::Project
;
219 BEGIN { use Git
::RepoCGI
; }
224 desc
=> 'description',
231 $self->{path
}.'/'.$propmap{$name};
238 $propmap{$name} or die "unknown property: $name";
239 open P
, $self->_property_path($name) or die "$name get failed: $!";
247 my ($name, $value) = @_;
248 $propmap{$name} or die "unknown property: $name";
249 open P
, '>'.$self->_property_path($name) or die "$name put failed: $!";
250 $value ne '' and print P
"$value\n";
252 chmod 0664, $self->_property_path($name) or die "$name chmod failed: $!";
255 sub _properties_load
{
257 foreach my $prop (keys %propmap) {
258 $self->{$prop} = $self->_property_fget($prop);
262 sub _properties_save
{
264 foreach my $prop (keys %propmap) {
265 $self->_property_fput($prop, $self->{$prop});
272 my $np = $self->_property_path('.nofetch');
274 open X
, '>'.$np or die "nofetch failed: $!";
277 unlink $np or die "yesfetch failed: $!";
284 $xtra .= join(',', @
{$self->{users
}});
285 filedb_atomic_append
(jailed_file
('/etc/group'),
286 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
291 my $xtra = join(',', @
{$self->{users
}});
292 filedb_atomic_edit
(jailed_file
('/etc/group'),
296 if ($self->{name
} eq (split /:/)[0]) {
297 # preserve readonly flag
298 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
299 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
307 # private constructor, do not use
310 my ($name, $path) = @_;
311 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
312 my $proj = { name
=> $name, path
=> $path };
317 # public constructor #0
318 # creates a virtual project not connected to disk image
319 # you can conjure() it later to disk
322 my ($name, $mirror) = @_;
323 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
325 $self->{mirror
} = $mirror;
329 # public constructor #1
334 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
338 next unless (shift eq $name);
340 my $self = $class->_new($name, "/srv/git/$name.git");
341 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
344 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
345 $self->{users
} = [split /,/, $ulist];
347 $self->_properties_load;
354 # $proj may not be in sane state if this returns false!
358 my $cgi = $repo->cgi;
360 my $pwd = $cgi->param('pwd');
361 if ($pwd ne '' or not $self->{crypt}) {
362 $self->{crypt} = scrypt
($pwd);
365 $self->{email
} = $repo->wparam('email');
366 valid_email
($self->{email
})
367 or $repo->err("Your email sure looks weird...?");
369 $self->{url
} = $repo->wparam('url');
371 valid_repo_url
($self->{url
})
372 or $repo->err("Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.");
375 $self->{desc
} = $repo->wparam('desc');
376 length($self->{desc
}) <= 1024
377 or $repo->err("<b>Short</b> description length > 1kb!");
379 $self->{hp
} = $repo->wparam('hp');
381 valid_web_url
($self->{hp
})
382 or $repo->err("Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.");
385 # FIXME: Permit only existing users
386 $self->{users
} = grep { valid_name
($_) } $cgi->param('user');
394 name
=> $self->{name
},
395 email
=> $self->{email
},
397 desc
=> html_esc
($self->{desc
}),
399 users
=> $self->{users
},
406 mkdir $self->{path
} or die "mkdir failed: $!";
407 $self->_properties_save;
414 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
415 or die "cg-admin-setuprepo failed: $?";
417 $self->_properties_save;
418 chmod 0775, $self->{path
} or die "chmod failed: $!";
419 $self->_group_add(':');
425 $self->_properties_save;
426 $self->_group_update;
432 valid_name
($name) or die "tried to query for project with invalid name $name!";
434 or -d
"/home/repo/repodata/cloning/$name"
435 or -d
"/home/repo/repodata/to-clone/$name");
439 valid_name
($name) or die "tried to query for project with invalid name $name!";
440 (-d
"/srv/git/$name.git");
446 package Git
::RepoCGI
::User
;
448 BEGIN { use Git
::RepoCGI
; }
452 filedb_atomic_append
(jailed_file
('/etc/passwd'),
453 join(':', $self->{name
}, 'x', '\i', $self->{email
}, '/', '/bin/git-shell'));
458 '/etc/sshkeys/'.$self->{name
};
463 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
464 print F
$self->{keys}."\n";
466 chmod 0664, jailed_file
($self->_sshkey_path);
469 # private constructor, do not use
473 valid_name
($name) or die "refusing to create user with invalid name ($name)!";
474 my $proj = { name
=> $name };
479 # public constructor #0
480 # creates a virtual user not connected to disk record
481 # you can conjure() it later to disk
485 my $self = $class->_new($name);
489 # $user may not be in sane state if this returns false!
493 my $cgi = $repo->cgi;
495 $self->{name
} = $repo->wparam('name');
496 valid_name
($self->{name
})
497 or $repo->err("Name contains invalid characters.");
499 $self->{email
} = $repo->wparam('email');
500 valid_email
($self->{email
})
501 or $repo->err("Your email sure looks weird...?");
503 $self->{keys} = $cgi->param('keys');
504 length($self->{keys}) <= 4096
505 or $repo->err("The list of keys is more than 4kb. Do you really need that much?");
520 valid_name
($name) or die "tried to query for user with invalid name $name!";
521 (-e jailed_file
("/etc/sshkeys/$name"));