fix bug that dispaly blog im...
|
1 |
#!/usr/bin/env perl |
2 | ||
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use utf8; |
|
6 |
use FindBin; |
|
7 |
use lib "$FindBin::Bin/../mojo/lib"; |
|
8 |
use lib "$FindBin::Bin/../lib"; |
|
9 |
use lib "$FindBin::Bin/../extlib/lib/perl5"; |
|
10 |
use Gitprep; |
|
11 | ||
Complete gitprep-shell
|
12 |
my $debug = 0; |
13 | ||
write gitprep-shell
|
14 |
# Project name pattern |
15 |
my $project_re = qr/[a-zA-Z0-9_\-\.]+$/; |
|
fix bug that dispaly blog im...
|
16 | |
write gitprep-shell
|
17 |
# User |
add collaboration feature to...
|
18 |
my $session_user = shift; |
19 |
die "User not specifed" unless defined $session_user; |
|
fix bug that dispaly blog im...
|
20 | |
write gitprep-shell
|
21 |
# Application |
fix bug that dispaly blog im...
|
22 |
my $app = Gitprep->new; |
write gitprep-shell
|
23 | |
24 |
# Git |
|
25 |
my $git = $app->git; |
|
26 | ||
27 |
# DBI |
|
fix bug that dispaly blog im...
|
28 |
my $dbi = $app->dbi; |
29 | ||
write gitprep-shell
|
30 |
# SSH connection |
31 |
my $ssh_connection = $ENV{SSH_CONNECTION}; |
|
Complete gitprep-shell
|
32 |
warn "ssh_connection: $ssh_connection" if $debug; |
add collaboration feature to...
|
33 |
die "who the *heck* are you?\n" unless defined $ssh_connection; |
write gitprep-shell
|
34 | |
35 |
# SSH original command |
|
36 |
my $ssh_original_command = $ENV{SSH_ORIGINAL_COMMAND} || ''; |
|
fix ssh key authentication b...
|
37 |
warn "ssh_original_command: $ssh_original_command" if $debug; |
write gitprep-shell
|
38 | |
39 |
# IP address |
|
40 |
my $ip = $ssh_connection || '(no-IP)'; |
|
fix ssh key authentication b...
|
41 |
warn "ip: $ip" if $debug; |
write gitprep-shell
|
42 |
$ip =~ s/ .*//; |
43 | ||
44 |
# Check new line of SSH original command |
|
45 |
my $ssh_original_command_tmp = $ssh_original_command; |
|
46 |
$ssh_original_command_tmp =~ s/[\n\r]+/<<newline>>/g; |
|
47 |
die "I don't like newlines in the command: $ssh_original_command\n" |
|
48 |
if $ssh_original_command ne $ssh_original_command_tmp; |
|
49 | ||
add collaboration feature to...
|
50 |
# Project |
51 |
my ($verb, $user, $project) = parse_ssh_original_command($ssh_original_command); |
|
write gitprep-shell
|
52 |
sanity($project); |
53 | ||
add collaboration feature to...
|
54 |
# Can access |
55 |
my $can_access; |
|
56 |
if ($session_user eq $user) { |
|
57 |
$can_access = 1; |
|
58 |
} |
|
59 |
else { |
|
60 |
my $row = $app->dbi->model('collaboration')->select( |
|
61 |
id => [$user, $project, $session_user] |
|
62 |
)->one; |
|
63 |
|
|
64 |
$can_access = $row ? 1 : 0; |
|
65 |
} |
|
66 |
die qq|User "$session_user" can't access repository "$user/$project.git"\n| |
|
67 |
unless $can_access; |
|
68 | ||
69 |
# Command |
|
write gitprep-shell
|
70 |
my $rep_home = $git->rep_home; |
Complete gitprep-shell
|
71 |
my $repository = "'$rep_home/$user/$project.git'"; |
write gitprep-shell
|
72 |
my @git_shell_cmd = ("git", "shell", "-c", "$verb $repository"); |
Complete gitprep-shell
|
73 |
warn "@git_shell_cmd" if $debug; |
74 |
unless ($debug) { |
|
75 |
system(@git_shell_cmd) == 0 |
|
add collaboration feature to...
|
76 |
or die "Can't execute command: @git_shell_cmd\n" ; |
Complete gitprep-shell
|
77 |
} |
write gitprep-shell
|
78 | |
79 |
sub parse_ssh_original_command { |
|
80 |
my $ssh_original_command = shift; |
|
81 | ||
Complete gitprep-shell
|
82 |
$ssh_original_command ||= ''; |
write gitprep-shell
|
83 | |
84 |
my $git_commands = "git-upload-pack|git-receive-pack|git-upload-archive"; |
|
add collaboration feature to...
|
85 |
if ($ssh_original_command =~ m(^($git_commands) '.*/([a-zA-Z1-9_]+)/([^\/]+?)\.git'$)) { |
fix gitprep-shell
|
86 |
my ($verb, $user, $project) = ($1, $2, $3); |
fix ssh key authentication b...
|
87 |
warn "User:$user, Project:$project" if $debug; |
add collaboration feature to...
|
88 |
die "invalid repo name: '$project'\n" if $project !~ $project_re; |
fix gitprep-shell
|
89 |
return ($verb, $user, $project); |
write gitprep-shell
|
90 |
} |
91 |
else { |
|
add collaboration feature to...
|
92 |
die "Invalid command: $ssh_original_command\n"; |
write gitprep-shell
|
93 |
} |
94 |
} |
|
95 | ||
96 |
sub sanity { |
|
97 |
my $project = shift; |
|
add collaboration feature to...
|
98 |
die "'$project' contains bad characters\n" if $project !~ $project_re; |
99 |
die "'$project' ends with a '/'\n" if $project =~ m(/$); |
|
100 |
die "'$project' contains '..'\n" if $project =~ m(\.\.); |
|
write gitprep-shell
|
101 |
} |
fix bug that dispaly blog im...
|
102 | |
103 |
=head1 NAME |
|
104 | ||
write gitprep-shell
|
105 |
gitprep-shell - AuthorizedKeysCommand for sshd |
fix bug that dispaly blog im...
|
106 | |
107 |
=head1 USAGE |
|
108 | ||
write gitprep-shell
|
109 |
./gitprep-shell kimoto |
fix bug that dispaly blog im...
|
110 | |
111 |
This command return user public_key |
|
112 |