Anonymous_Read_Access_with_Git_Daemon.mdwn 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. Gitolite itself works by default with Secure Shell (SSH) authentication to
  2. control user access to the repositories. First the user's key is compared with
  3. the keys stored on the server (under the 'keydir' folder in the gitolite-admin
  4. repository), and then the configuration file (conf/gitolie.conf) is checked to
  5. determine whether the user is allowed to do the operation they asked to do.
  6. However, if your project is not private, e.g. it is a free software project, you
  7. may wish to enable anonymous read-access of your repositories, so anyone with
  8. internet access can look at the source code, download it, improve it, compile it
  9. and run the program. Without this kind of access, only people whose keys have
  10. been stored on the server, i.e. in the gitolite-admin repository, have access.
  11. Git supports three protocols for anonymous read access: filesystem (the remote
  12. repository is either on the same local filesystem or is really remote but is
  13. accessed through a network filesystem), HTTP (the same protocol used for
  14. downloading website content) and the git protocol, which was created for git.
  15. We'll start with the git protocol.
  16. Just like Gitolite uses an SSH server which waits for connection requests from
  17. clients (team members), the git protocol access requires a server: This is the
  18. git daemon. It is provided by the git package, so you already have it installed,
  19. but it is not running. A separate package is needed, which will setup the daemon
  20. as a service which runs automatically when you turn on the computer. One of
  21. these packages needs to be installed:
  22. - git-daemon-sysvinit
  23. - git-daemon-run
  24. If you have some knowledge about init systems, choose what's best for you.
  25. Otherwise, let's go with what the gitolite package recommends to install:
  26. git-daemon-run. Don't worry, it probably won't make much difference which one
  27. you install. To install git-daemon-run, run this as root:
  28. # apt-get install git-daemon-run
  29. Thanks to Gitolite's integration with the git daemon, the only thing left to do
  30. is to enable repositories to be accessible via the git protocol. Gitolite
  31. creates a special predefined user called 'daemon', which controls git protocol
  32. access by having read access to repositories. Let's go back to our
  33. gitolite.conf, which may look like this:
  34. repo gitolite-admin
  35. RW+ = alice
  36. repo cool-project
  37. RW+ = alice h4ck3r
  38. The steps to add git daemon access to the cool-project repository are:
  39. 1. Give the 'daemon' user read access to it
  40. 2. Commit the change
  41. 3. Push
  42. After the change the config file will look like this:
  43. repo gitolite-admin
  44. RW+ = alice
  45. repo cool-project
  46. RW+ = alice h4ck3r
  47. R = daemon
  48. The anonymous cloning address depends on the options given to the git-daemon
  49. command. Let's go see them. If you installed git-daemon-run, the script that
  50. runs the git-daemon service is /etc/sv/git-daemon/run and it should look like
  51. this:
  52. #!/bin/sh
  53. exec 2>&1
  54. echo 'git-daemon starting.'
  55. exec chpst -ugitdaemon \
  56. "$(git --exec-path)"/git-daemon --verbose --reuseaddr \
  57. --base-path=/var/cache /var/cache/git
  58. It means 3 things:
  59. 1. The repositories are looked for in the folder /var/cache/git
  60. 2. When cloning, there's no need to specify the /var/cache prefix, e.g. just
  61. /git/cool-project.git is enough
  62. 3. The daemon will run as a user named 'gitdaemon'
  63. Of course this won't work because our repositories are not there. Also, the
  64. gitdaemon user doesn't have any access to the git repositories, owned by the
  65. __git__ user. We'll need to add gitdaemon to the __git__ group and make sure
  66. users in the git group have read access to the repositories. And it's not all:
  67. We'll need to make sure repositories give read access to the __git__ group.
  68. First, let's fix the git-daemon configuration. Open the file
  69. /etc/sv/git-daemon/run with your favorite text editor as root (for example
  70. 'nano', install with 'apt-get install nano') and change the base-path and the
  71. repo directory. Now it should look like this:
  72. #!/bin/sh
  73. exec 2>&1
  74. echo 'git-daemon starting.'
  75. exec chpst -ugitdaemon:git \
  76. "$(git --exec-path)"/git-daemon --verbose --reuseaddr \
  77. --base-path=/home/git/repositories /home/git/repositories
  78. It is also possible to use a non-standard port (9418 is git daemon's default) by
  79. providing a port to the `git-daemon` command in the configuration above as a
  80. command-line option, e.g. `--port=1234`.
  81. After the change, the path to a repository cool-project is
  82. /home/git/repositories/cool-project.git, but a base path is automatically
  83. prepended so just cool-project.git is enough. In other words, cloning will look
  84. like this, assuming the domain name of your git server is git.mysite.org:
  85. $ git clone git://git.mysite.org/cool-project.git
  86. It also puts the gitdaemon user in the __git__ group. However, cloning still
  87. doesn't work because gitolite creates repositories with permissions only for
  88. itself (i.e. the __git__ user) and no permissions for the group. So what's left
  89. is to:
  90. 1. Tell Gitolite to give the __git__ group read access to every new repository
  91. 2. Update existing repositories' access permissions (if necessary)
  92. Note: Some repositories probably shouldn't have anonymous access, e.g. the
  93. gitolite-admin repository. Even though the read access for 'daemon' in
  94. gitolite.conf controls that, not giving gitdaemon read permissions adds an extra
  95. security layer. Even if git daemon's authorization is bypassed somehow, e.g. by
  96. mistake, it still cannot access gitolite-admin. However it depends on your
  97. personal preference and situation. If you're the only one with SSH access to the
  98. server and access to 'gitolite-admin', you're probably safe, especially if you
  99. run a home server you physically own.
  100. Let's tell gitolite to give new repositories permissions which will allow
  101. members of the git group read them. Open the file /home/git/.gitolite.rc (if the
  102. server is remote, use SSH to get there) and find, under "most often used/changed
  103. variables", the following line:
  104. $REPO_UMASK = 0077;
  105. This mask sets the default permissions for new repositories. It means the owner
  106. (the __git__ user) has full access and everyone else has no access at all. To
  107. give the git group members read access, change that line to the following and
  108. save:
  109. $REPO_UMASK = 0027;
  110. Now one last thing, let's see how to fix the permissions of existing
  111. repositories. This is how it's done for the cool-project repository (must be
  112. done on the server/ssh as the git user or as root):
  113. cd /home/git/repositories
  114. chmod g+rX .
  115. chmod -R g+rX cool-project.git
  116. This applies the change to all existing repositories:
  117. cd /home/git/repositories
  118. chmod -R g+rX .
  119. After the git daemon is restarted, it will start serving your repositories -
  120. just the ones to whom 'daemon' has read access in gitolite.conf - anonymously
  121. via the git protocol.
  122. Note that if you want to serve your repositories via I2P, this is not enough. If
  123. your server's firewall allows port 9418, the repositories will immediately
  124. become available for git-protocol anonymous cloning via the clearnet, i.e. the
  125. regular internet and not I2P. So if you want to use I2P, block port 9418 on your
  126. server (if you run the server at home, it would be your home computer's
  127. firewall. However if you use a router, it probably just blocks everything unless
  128. you tell it to allow something, so don't allow port 9418. In any case you can
  129. try cloning from another machine and see if it works). Later we'll see how to
  130. create a server tunnel for I2P which forwards connections to port 9418 locally.
  131. This will make your git protocol access work (only) via I2P.