apache.conf.in: redirect www.repo.or.cz to repo.or.cz
[girocco.git] / bin / git-shell-verify
blobb4cdd887bcdd54f3ce80df1b94d1e0cd535c214e
1 #!/bin/sh
3 # Abort any push early if the pushing user doesn't have any push permissions
4 # at all. This avoids unnecessary traffic and unpacked object pollution.
6 # This script is intended for use from within the chroot jail and may or may
7 # not work properly outside it.
9 set -e
11 # git_add_config "some.var=value"
12 # every ' in value must be replaced with the 4-character sequence '\'' before
13 # calling this function or Git will barf. Will not be effective unless running
14 # Git version 1.7.3 or later.
15 git_add_config() {
16 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'$1'"
17 export GIT_CONFIG_PARAMETERS
20 TMPDIR="/tmp"
21 GIT_CONFIG_NOSYSTEM=1
22 GIT_ATTR_NOSYSTEM=1
23 GIT_NO_REPLACE_OBJECTS=1
24 GIT_TERMINAL_PROMPT=0
25 GIT_PAGER="cat"
26 PAGER="cat"
27 GIROCCO_SUPPRESS_AUTO_GC_UPDATE=1
29 if ! [ -x @perlbin@ ]; then
30 # We are INSIDE the chroot
31 reporoot=/@jailreporoot@
32 XDG_CONFIG_HOME=/var/empty
33 HOME=/etc/girocco
34 GIT_ASKPASS=/bin/git-askpass-password
35 else
36 # We are NOT INSIDE the chroot
37 reporoot=@reporoot@
38 XDG_CONFIG_HOME=@chroot@/var/empty
39 HOME=@chroot@/etc/girocco
40 GIT_ASKPASS=@basedir@/bin/git-askpass-password
42 mob=@mob@
43 webadmurl=@webadmurl@
44 ua=@git_server_ua@
45 defined_ua=@defined_git_server_ua@
46 cfg_git_no_mmap=@git_no_mmap@
47 var_big_file_threshold=@big_file_threshold@
48 var_upload_window=@upload_pack_window@
49 cfg_fetch_stash_refs=@fetch_stash_refs@
50 cfg_suppress_git_ssh_logging=@suppress_git_ssh_logging@
52 export XDG_CONFIG_HOME
53 export HOME
54 export TMPDIR
55 export GIT_CONFIG_NOSYSTEM
56 export GIT_ATTR_NOSYSTEM
57 export GIT_NO_REPLACE_OBJECTS
58 export GIT_TERMINAL_PROMPT
59 export GIT_PAGER
60 export PAGER
61 export GIT_ASKPASS
62 export GIROCCO_SUPPRESS_AUTO_GC_UPDATE
63 unset GIT_USER_AGENT
64 unset GIT_HTTP_USER_AGENT
65 if [ -n "$defined_ua" ]; then
66 GIT_USER_AGENT="$ua"
67 export GIT_USER_AGENT
69 unset GIT_CONFIG_PARAMETERS
70 git_add_config "core.ignoreCase=false"
71 git_add_config "core.pager=cat"
72 if [ -n "$cfg_git_no_mmap" ]; then
73 # Just like compiling with NO_MMAP
74 git_add_config "core.packedGitWindowSize=1m"
75 else
76 # Always use the 32-bit default (32m) even on 64-bit to avoid memory blowout
77 git_add_config "core.packedGitWindowSize=32m"
79 # Always use the 32-bit default (256m) even on 64-bit to avoid memory blowout
80 git_add_config "core.packedGitLimit=256m"
81 [ -z "$var_big_file_threshold" ] ||
82 git_add_config "core.bigFileThreshold=$var_big_file_threshold"
83 git_add_config "gc.auto=0"
85 if [ "${cfg_fetch_stash_refs:-0}" = "0" ]; then
86 git_add_config "uploadpack.hiderefs=refs/stash"
87 git_add_config "uploadpack.hiderefs=refs/tgstash"
90 fromip="${SSH_CLIENT%% *}"
91 fromip="${fromip:+$fromip }"
93 logmsg()
95 [ "${cfg_suppress_git_ssh_logging:-0}" = "0" ] || return 0
96 _msg="$(LC_ALL=C tr -c '\040-\176' '[?*]' <<EOT
97 $fromip$* $LOGNAME
98 EOT
99 )" && _msg="${_msg%[?]}" &&
100 logger -t "${0##*/}[$$]" <<EOT
101 $_msg
105 # When accessing Git via ssh, there should never be a terminal on 0, 1 or 2
106 # The "no-pty" flag should be set in all the ssh keys files to prevent this
107 # However, just in case, check for it here and die as a safety fallback
108 if [ -t 0 ] || [ -t 1 ] || [ -t 2 ]; then
109 logmsg "denied pty"
110 echo forbidden >&2
111 exit 1
114 # Only the following commands are allowed:
116 # git-shell -c "git-receive-pack 'dir'"
117 # git-shell -c "git receive-pack 'dir'"
118 # git-shell -c "git-upload-pack 'dir'"
119 # git-shell -c "git upload-pack 'dir'"
120 # git-shell -c "git-upload-archive 'dir'"
121 # git-shell -c "git upload-archive 'dir'"
123 # where dir must start with $reporoot/ but a leading/trailing '/' is optional
124 # as well as the final .git however if $dir does not start with $reporoot but
125 # adding a $reporoot prefix makes it work then the $reporoot prefix will be
126 # silently added.
128 if [ "$1" != "-c" ]; then
129 echo forbidden >&2
130 exit 1
133 dir="$2"
134 type=''
135 case "$2" in
136 "git-receive-pack "*) type='receive-pack'; dir="${dir#git-receive-pack }";;
137 "git receive-pack "*) type='receive-pack'; dir="${dir#git receive-pack }";;
138 "git-upload-pack "*) type='upload-pack'; dir="${dir#git-upload-pack }";;
139 "git upload-pack "*) type='upload-pack'; dir="${dir#git upload-pack }";;
140 "git-upload-archive "*) type='upload-archive'; dir="${dir#git-upload-archive }";;
141 "git upload-archive "*) type='upload-archive'; dir="${dir#git upload-archive }";;
143 echo forbidden >&2
144 exit 1
145 esac
147 # valid project names only allow 0-9A-Za-z._+- plus the / separator and they
148 # are always single quoted so the only valid directory names will always start
149 # with a single quote and end with a single quote and not contain any internal
150 # character that needs to be escaped.
152 case "$dir" in
153 "'"*) :;;
155 logmsg "denied $type $dir"
156 echo forbidden >&2
157 exit 1
158 esac
159 case "$dir" in
160 *"'") :;;
162 logmsg "denied $type $dir"
163 echo forbidden >&2
164 exit 1
165 esac
167 # Some shells do not properly handle quoting after # or % so we cannot
168 # put an explicit ' there in a way that works for all shells. Instead
169 # just remove a single character since we've already verified it's a '.
170 dir="${dir#?}"; dir="${dir%?}"
172 # add a missing leading /
173 case "$dir" in
174 /*) :;;
176 dir="/$dir"
177 esac
179 # remove a trailing /
180 case "$dir" in
181 *?/)
182 dir="${dir%/}"
183 esac
185 # add a missing trailing .git
186 case "$dir" in
187 *.git) :;;
189 dir="$dir.git"
190 esac
192 # do not allow any .. sequence
193 case "$dir" in *..*)
194 logmsg "denied $type $dir"
195 echo forbidden >&2
196 exit 1
197 esac
199 odir="$dir"
200 case "$dir" in
201 "$reporoot/"*) :;;
203 # Allow it if prefixing with $reporoot matches an existing directory
204 if [ -d "$reporoot$dir" ]; then
205 dir="$reporoot$dir"
206 else
207 logmsg "denied $type $dir"
208 echo forbidden >&2
209 exit 1
211 esac
213 # Valid project names never end in .git (we add that automagically), so a valid
214 # fork can never have .git at the end of any path component except the last.
215 # We check this to avoid a situation where a certain collection of pushed refs
216 # could be mistaken for a GIT_DIR. Git would ultimately complain, but some
217 # undesirable things could happen along the way.
219 # Remove the leading $reporoot and trailing .git to get a test string
220 testpath="${dir#$reporoot/}"
221 testpath="${testpath%.git}"
222 case "$testpath/" in *.[Gg][Ii][Tt]/*|_*)
223 logmsg "denied $type $odir"
224 echo forbidden >&2
225 exit 1
226 esac
228 if ! [ -d "$dir" ] || ! [ -f "$dir/HEAD" ] || ! [ -d "$dir/objects" ]; then
229 logmsg "denied $type $odir"
230 echo forbidden >&2
231 exit 1
234 proj="${dir#$reporoot/}"; projbare="${proj%.git}"
236 if [ "$type" = 'receive-pack' ] && [ "$LOGNAME" = 'git' ]; then
237 logmsg "denied $type $odir"
238 echo "The user '$LOGNAME' may only be used for fetches, sorry" >&2
239 exit 3
242 if [ "$type" = 'receive-pack' ] && ! [ -f "$dir/.nofetch" ]; then
243 logmsg "denied $type $odir"
244 echo "The $proj project is a mirror and may not be pushed to, sorry" >&2
245 exit 3
248 if [ "$type" = 'receive-pack' ]; then
249 git_add_config 'receive.unpackLimit=1'
250 # Note the git config documentation is wrong
251 # transfer.unpackLimit, if set, overrides receive.unpackLimit
252 git_add_config 'transfer.unpackLimit=1'
255 if ! [ -x @perlbin@ ] && [ "$type" = 'receive-pack' ]; then
256 # We are INSIDE the chroot trying to push
258 if ! can_user_push "$projbare"; then
259 # If mob is enabled and mob has push permissions and
260 # the current user is not the mob then it's a personal mob push
261 # presuming the special mob directory has been set up
262 if [ "$mob" = "mob" ] && [ "$LOGNAME" != "mob" ] && [ -d "$reporoot/$proj/mob" ] &&
263 can_user_push "$projbare" mob; then
265 umask 113
266 >"/etc/sshactive/$LOGNAME,"
267 mv -f "/etc/sshactive/$LOGNAME," "/etc/sshactive/$LOGNAME"
268 ! [ -e "$dir/.delaygc" ] || >"$dir/.allowgc" || :
270 logmsg "accepted $type $odir mob"
271 exec git-shell -c "git-receive-pack '$reporoot/$proj/mob'"
272 exit 1
274 logmsg "denied $type $odir noperms"
275 echo "The user '$LOGNAME' does not have push permissions for project '$proj'" >&2
276 echo "You may adjust push permissions at $webadmurl/editproj.cgi?name=$proj" >&2
277 exit 3
280 umask 113
281 >"/etc/sshactive/$LOGNAME,"
282 mv -f "/etc/sshactive/$LOGNAME," "/etc/sshactive/$LOGNAME"
283 ! [ -e "$dir/.delaygc" ] || >"$dir/.allowgc" || :
287 [ -z "$var_upload_window" ] || [ "$type" != "upload-pack" ] ||
288 git_add_config "pack.window=$var_upload_window"
290 logmsg "accepted $type $odir"
291 exec git-shell -c "git-$type '$dir'"
292 exit 1