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(-relative
=> 1);
64 if ($cginame =~ /^[a-zA-Z_.-]+\.cgi$/) {
67 <a href="http://repo.or.cz/gitweb.cgi/repo.git?a=blob;f=cgi/$cginame">(view source)</a>
84 print "<p style=\"text-color: red\">@_</p>\n";
90 my $err = $self->{err
};
91 $err and print "<p>Operation aborted due to $err errors.</p>\n";
98 my $val = $self->{cgi
}->param($param);
99 $val =~ s/^\s*(.*?)\s*$/$1/;
104 ### Random utility functions
108 crypt($pwd, join ('', ('.', '/', 2..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64]));
114 $str =~ s/</</g; $str =~ s/>/>/g;
115 $str =~ s/"/"/g;
120 "/home/repo/j/$filename";
128 use Errno
qw(EEXIST);
130 my $handle = new IO
::Handle
;
132 unless (sysopen($handle, $path, O_WRITE
|O_CREAT
|O_EXCL
)) {
134 while (not sysopen($handle, $path, O_WRITE
|O_CREAT
|O_EXCL
)) {
135 ($! == EEXIST
) or die "$path open failed: $!";
136 ($cnt++ < 16) or die "$path open failed: cannot open lockfile";
140 # XXX: filedb-specific
141 chmod 0664, $path or die "$path g+w failed: $!";
149 rename "$path.lock", $path or die "$path unlock failed: $!";
152 sub filedb_atomic_append
{
153 my ($file, $line) = @_;
156 open my $src, $file or die "$file open for reading failed: $!";
157 my $dst = lock_file
($file);
160 my $aid = (split /:/)[2];
161 $id = $aid + 1 if ($aid >= $id);
163 print $dst $_ or die "$file(l) write failed: $!";
166 $line =~ s/\\i/$id/g;
167 print $dst "$line\n" or die "$file(l) write failed: $!";
169 close $dst or die "$file(l) close failed: $!";
177 sub filedb_atomic_edit
{
178 my ($file, $fn) = @_;
180 open my $src, $file or die "$file open for reading failed: $!";
181 my $dst = lock_file
($file);
184 print $dst $fn($_) or die "$file(l) write failed: $!";
187 close $dst or die "$file(l) close failed: $!";
193 # BOTH user AND project name!
200 /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9-.]+$/;
204 /^http:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+(#[a-zA-Z0-9._-]+)?$/;
208 /^http:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+$/ or
209 /^git:\/\
/[a-zA-Z0-9-.]+\/[_\
%a-zA
-Z0
-9.\
/~-]+$/;
215 package Git
::RepoCGI
::Project
;
220 desc
=> 'description',
227 $self->{path
}.'/'.$propmap{$name};
234 $propmap{$name} or die "unknown property: $name";
235 open P
, $self->_property_path($name) or die "$name get failed: $!";
243 my ($name, $value) = @_;
244 $propmap{$name} or die "unknown property: $name";
245 open P
, '>'.$self->_property_path($name) or die "$name put failed: $!";
246 $value ne '' and print P
"$value\n";
248 chmod 0664, $self->_property_path($name) or die "$name chmod failed: $!";
251 sub _properties_load
{
253 foreach my $prop (keys %propmap) {
254 $self->{$prop} = $self->_property_fget($prop);
258 sub _properties_save
{
260 foreach my $prop (keys %propmap) {
261 $self->_property_fput($prop, $self->{$prop});
268 my $np = $self->_property_path('.nofetch');
270 open X
, '>'.$np or die "nofetch failed: $!";
273 unlink $np or die "yesfetch failed: $!";
280 $xtra .= join(',', @users);
281 filedb_atomic_append
(jailed_file
('/etc/group'),
282 join(':', $self->{name
}, $self->{crypt}, '\i', $xtra));
287 my $xtra = join(',', @users);
288 filedb_atomic_edit
(jailed_file
('/etc/group'),
292 if ($self->{name
} eq (split /:/)[0]) {
293 # preserve readonly flag
294 s/::([^:]*)$/:$1/ and $xtra = ":$xtra";
295 return join(':', $self->{name
}, $self->{crypt}, $self->{gid
}, $xtra)."\n";
303 # private constructor, do not use
306 my ($name, $path) = @_;
307 valid_name
($name) or die "refusing to create project with invalid name ($name)!";
308 my $proj = { name
=> $name, path
=> $path };
313 # public constructor #0
314 # creates a virtual project not connected to disk image
315 # you can conjure() it later to disk
318 my ($name, $mirror) = @_;
319 my $self = $class->_new($name, $mirror ?
"/home/repo/repodata/to-clone/$name" : "/srv/git/$name.git");
321 $self->{mirror
} = $mirror;
325 # public constructor #1
330 open F
, jailed_file
("/etc/group") or die "project load failed: $!";
334 next unless (shift eq $name);
336 my $self = $class->_new($name, "/srv/git/$name.git");
337 (-d
$self->{path
}) or die "invalid path (".$self->{path
}.") for project ".$self->{name
};
340 ($self->{crypt}, $self->{gid
}, $ulist) = @_;
341 $self->{users
} = [split /,/, $ulist];
343 $self->_properties_load;
350 # $proj may not be in sane state if this returns false!
354 my $cgi = $repo->cgi;
356 my $pwd = $cgi->param('pwd');
357 if ($pwd ne '' or not $self->{crypt}) {
358 $self->{crypt} = scrypt
($pwd);
361 $self->{email
} = $repo->wparam('email');
362 valid_email
($self->{email
})
363 or $repo->err "Your email sure looks weird...?";
365 $self->{url
} = $repo->wparam('url');
367 valid_repo_url
($self->{url
})
368 or $repo->err "Invalid URL. Note that only HTTP and Git protocol is supported. If the URL contains funny characters, contact me.";
371 $self->{desc
} = $repo->wparam('desc');
372 length($self->{desc
}) <= 1024
373 or $repo->err "<b>Short</b> description length > 1kb!";
375 $self->{hp
} = $repo->wparam('hp');
377 valid_web_url
($self->{hp
})
378 or $repo->err "Invalid homepage URL. Note that only HTTP protocol is supported. If the URL contains funny characters, contact me.";
381 # FIXME: Permit only existing users
382 $self->{users
} = grep { valid_name
($_) } $cgi->param('user');
390 name
=> $self->{name
},
391 email
=> $self->{email
},
393 desc
=> html_esc
($self->{desc
}),
395 users
=> $self->{users
},
402 mkdir $self->{path
} or die "mkdir failed: $!";
403 $self->_properties_save;
410 system('cg-admin-setuprepo', '-g', 'repo', $self->{path
}) == 0
411 or die "cg-admin-setuprepo failed: $?";
413 $self->_properties_save;
414 chmod 0775, $self->{path
} or die "chmod failed: $!";
415 $self->_group_add(':');
421 $self->_properties_save;
422 $self->_group_update;
428 valid_name
($name) or die "tried to query for project with invalid name $name!";
430 or -d
"/home/repo/repodata/cloning/$name"
431 or -d
"/home/repo/repodata/to-clone/$name");
435 valid_name
($name) or die "tried to query for project with invalid name $name!";
436 (-d
"/srv/git/$name.git");
442 package Git
::RepoCGI
::User
;
446 filedb_atomic_append
(jailed_file
('/etc/passwd'),
447 join(':', $self->{name
}, 'x', '\i', $self->{email
}, '/', '/bin/git-shell'));
452 '/etc/sshkeys/'.$self->{name
};
457 open F
, ">".jailed_file
($self->_sshkey_path) or die "sshkey failed: $!";
458 print F
$self->{keys}."\n";
460 chmod 0664, jailed_file
($self->_sshkey_path);
463 # private constructor, do not use
467 valid_name
($name) or die "refusing to create user with invalid name ($name)!";
468 my $proj = { name
=> $name };
473 # public constructor #0
474 # creates a virtual user not connected to disk record
475 # you can conjure() it later to disk
479 my $self = $class->_new($name);
483 # $user may not be in sane state if this returns false!
487 my $cgi = $repo->cgi;
489 $self->{name
} = $repo->wparam('name');
490 valid_name
($self->{name
})
491 or $repo->err "Name contains invalid characters.";
493 $self->{email
} = $repo->wparam('email');
494 valid_email
($self->{email
})
495 or $repo->err "Your email sure looks weird...?";
497 $self->{keys} = $cgi->param('keys');
498 length($self->{keys}) <= 4096
499 or $repo->err "The list of keys is more than 4kb. Do you really need that much?";
514 valid_name
($name) or die "tried to query for user with invalid name $name!";
515 (-e jailed_file
("/etc/sshkeys/$name"));