gitprep / script / gitprep-shell /
Newer Older
112 lines | 2.824kb
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
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
gitprep authored on 2014-05-16
12
my $debug = 0;
13

            
write gitprep-shell
gitprep authored on 2014-05-16
14
# Project name pattern
15
my $project_re = qr/[a-zA-Z0-9_\-\.]+$/;
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
16

            
write gitprep-shell
gitprep authored on 2014-05-16
17
# User
add collaboration feature to...
gitprep authored on 2014-05-20
18
my $session_user = shift;
19
die "User not specifed" unless defined $session_user;
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
20

            
write gitprep-shell
gitprep authored on 2014-05-16
21
# Application
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
22
my $app = Gitprep->new;
write gitprep-shell
gitprep authored on 2014-05-16
23

            
24
# Git
25
my $git = $app->git;
26

            
27
# DBI
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
28
my $dbi = $app->dbi;
29

            
write gitprep-shell
gitprep authored on 2014-05-16
30
# SSH connection
31
my $ssh_connection = $ENV{SSH_CONNECTION};
Complete gitprep-shell
gitprep authored on 2014-05-16
32
warn "ssh_connection: $ssh_connection" if $debug;
add collaboration feature to...
gitprep authored on 2014-05-20
33
die "who the *heck* are you?\n" unless defined $ssh_connection;
write gitprep-shell
gitprep authored on 2014-05-16
34

            
35
# SSH original command
36
my $ssh_original_command = $ENV{SSH_ORIGINAL_COMMAND} || '';
fix ssh key authentication b...
gitprep authored on 2014-05-20
37
warn "ssh_original_command: $ssh_original_command" if $debug;
write gitprep-shell
gitprep authored on 2014-05-16
38

            
39
# IP address
40
my $ip = $ssh_connection || '(no-IP)';
fix ssh key authentication b...
gitprep authored on 2014-05-20
41
warn "ip: $ip" if $debug;
write gitprep-shell
gitprep authored on 2014-05-16
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...
gitprep authored on 2014-05-20
50
# Project
51
my ($verb, $user, $project) = parse_ssh_original_command($ssh_original_command);
write gitprep-shell
gitprep authored on 2014-05-16
52
sanity($project);
53

            
add collaboration feature to...
gitprep authored on 2014-05-20
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
gitprep authored on 2014-05-16
70
my $rep_home = $git->rep_home;
Complete gitprep-shell
gitprep authored on 2014-05-16
71
my $repository = "'$rep_home/$user/$project.git'";
write gitprep-shell
gitprep authored on 2014-05-16
72
my @git_shell_cmd = ("git", "shell", "-c", "$verb $repository");
Complete gitprep-shell
gitprep authored on 2014-05-16
73
warn "@git_shell_cmd" if $debug;
74
unless ($debug) {
75
  system(@git_shell_cmd) == 0
add collaboration feature to...
gitprep authored on 2014-05-20
76
    or die "Can't execute command: @git_shell_cmd\n" ;
Complete gitprep-shell
gitprep authored on 2014-05-16
77
}
write gitprep-shell
gitprep authored on 2014-05-16
78

            
79
sub parse_ssh_original_command {
80
  my $ssh_original_command = shift;
81

            
Complete gitprep-shell
gitprep authored on 2014-05-16
82
  $ssh_original_command ||= '';
write gitprep-shell
gitprep authored on 2014-05-16
83

            
84
  my $git_commands = "git-upload-pack|git-receive-pack|git-upload-archive";
add collaboration feature to...
gitprep authored on 2014-05-20
85
  if ($ssh_original_command =~ m(^($git_commands) '.*/([a-zA-Z1-9_]+)/([^\/]+?)\.git'$)) {
fix gitprep-shell
gitprep authored on 2014-05-19
86
    my ($verb, $user, $project) = ($1, $2, $3);
fix ssh key authentication b...
gitprep authored on 2014-05-20
87
    warn "User:$user, Project:$project" if $debug;
add collaboration feature to...
gitprep authored on 2014-05-20
88
    die "invalid repo name: '$project'\n" if $project !~ $project_re;
fix gitprep-shell
gitprep authored on 2014-05-19
89
    return ($verb, $user, $project);
write gitprep-shell
gitprep authored on 2014-05-16
90
  }
91
  else {
add collaboration feature to...
gitprep authored on 2014-05-20
92
    die "Invalid command: $ssh_original_command\n";
write gitprep-shell
gitprep authored on 2014-05-16
93
  }
94
}
95

            
96
sub sanity {
97
  my $project = shift;
add collaboration feature to...
gitprep authored on 2014-05-20
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
gitprep authored on 2014-05-16
101
}
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
102

            
103
=head1 NAME
104

            
write gitprep-shell
gitprep authored on 2014-05-16
105
gitprep-shell - AuthorizedKeysCommand for sshd
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
106

            
107
=head1 USAGE
108

            
write gitprep-shell
gitprep authored on 2014-05-16
109
  ./gitprep-shell kimoto
fix bug that dispaly blog im...
Yuki Kimoto authored on 2014-03-17
110

            
111
This command return user public_key
112