11

我正在尝试设置一个 git post-receive 钩子,这样当收到提交时,机器上的存储库的另一个克隆会被更新(即执行 a git pull origin master)。我正在使用 gitosis 为存储库提供服务,因此我相信一个接收后挂钩将以gitosis用户身份运行,而我想在接收时更新的存储库由www-data. 我该怎么做呢?

我听说过setuid脚本,但我不确定这是否会带来安全风险?如果这不是安全风险,我该怎么做呢?我猜我会做一些事情,比如让脚本拥有www-data并使其成为世界可执行文件并启用 setuid 位?我猜这个脚本几乎是无害的,因为它所做的只是更新存储库,但我想确定一下。谢谢!

编辑:有什么办法可以做到这一点sudo吗?那会比 更安全setuid吗?我的意思是,如果用户不是 root 用户,我认为不会有太大问题setuid,但无论如何,我似乎必须跳过几圈setuid才能运行脚本。

第二次编辑:似乎我可以用一些/etc/sudoers魔法和sudo -u. 也许我应该把它贴在 ServerFault 上,但至少我从这次努力中学到了一些东西。

4

2 回答 2

14

恕我直言,这应该是服务器故障,但这是答案;

添加:

gitosis ALL=(www-data) NOPASSWD: /path/to/git

到 /etc/sudoers

并将命令运行为sudo -u www-data <whatever the command is>

于 2009-12-23T07:50:20.377 回答
4

请注意,我使用的是git用户名,因此,如果您使用的是gitosis或任何其他用户名,请填写您的用户名!

root用户的控制台中执行以下命令:

visudo

“vi”编辑器将被打开。添加这些行:

Defaults:git    !authenticate
git ALL=(www-data) ALL

结果文件(通过调用“visudo”在“vi”编辑器中打开)应该如下所示:

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#

Defaults    env_reset
Defaults:git    !authenticate

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL
git ALL=(www-data) ALL


# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=(ALL) ALL
#
#includedir /etc/sudoers.d

然后按 CTRL+O 保存文件,然后按 Enter 接受文件名 (bla bla bla),然后按 CTRL+X 关闭“vi”编辑器。

瞧!现在git用户可以以www-data用户的身份执行命令:

sudo -u www-data git pull origin master
于 2012-06-21T17:35:04.903 回答