リモートでラッパースクリプトを記述し、authorized_keysの行の前にcommand = "/ path / to/wrapper"を追加できます。
command="/usr/local/bin/gitaccess" ssh-rsa ...
このラッパーでは、SSH_ORIGINAL_COMMANDを確認します。Gitは次のコマンドを発行します。
git receive-pack '/path/provided' # when pushing
git upload-pack '/path/provided' # when pulling
SSH_ORIGINAL_COMMANDが空ではなく、これらのいずれかで始まる場合は、パスを確認し、必要に応じてリポジトリを作成し、必要な構成をインストールして、コマンドを実行します。
SSH_ORIGINAL_COMMANDが空で、ユーザーにシェルアクセスを提供する場合は、シェルを呼び出します。
SSH_ORIGINAL_COMMANDが空ではないが、gitコマンドで開始されていない場合、ユーザーにシェルアクセスを許可する場合は、提供されたコマンドを実行するだけです。
これがデモンストレーションするRubyコードのビットです。私はそれをテストしておらず、改善の余地があることに注意してください(たとえば、/ bin / bashをハードコーディングしないでください)。
#!/usr/bin/env ruby
orig_command = ENV['SSH_ORIGINAL_COMMAND']
if orig_command.nil?
# If you want to preserve shell access
exec '/bin/bash' # not tested, I hope it's interactive if executed this way
end
cmd_parts = orig_command.match /([a-z-]+) '([a-z0-9.\/]+)'/
if cmd_parts.nil?
# If you want to preserve shell access
exec '/bin/bash', '-c', orig_command
end
command = cmd_parts[1]
path = '/var/repositories/'+cmd_parts[2] # not secured (could contain '..')
if command == 'git-receive-pack' || command == 'git-upload-pack'
if not File.directory?(path)
`git init --bare #{path}` # not secured, I didn't escape path
# Do any configuration here
end
exec 'git-shell', '-c', "#{command} '#{path}'"
end
# If you want to preserve shell access
exec '/bin/bash', '-c', orig_command
また、authorized_keysでこのスクリプトに引数を渡して、ユーザーを識別し、ユーザーにシェルアクセスを許可するかどうかを選択することもできます。また、リポジトリごとにアクセスを制御するためにもこれを行います。この引数をgitフックに伝えたい場合は、環境変数を作成するだけです。