Merge branch 'master' into rorcz
[girocco.git] / bin / git-shell-verify
blobd85d16b4fd35cf7e1f438ca6c1eea52933c6bda7
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@
51 cfg_max_file_size512=@max_file_size512@
53 export XDG_CONFIG_HOME
54 export HOME
55 export TMPDIR
56 export GIT_CONFIG_NOSYSTEM
57 export GIT_ATTR_NOSYSTEM
58 export GIT_NO_REPLACE_OBJECTS
59 export GIT_TERMINAL_PROMPT
60 export GIT_PAGER
61 export PAGER
62 export GIT_ASKPASS
63 export GIROCCO_SUPPRESS_AUTO_GC_UPDATE
64 unset GIT_USER_AGENT
65 unset GIT_HTTP_USER_AGENT
66 if [ -n "$defined_ua" ]; then
67 GIT_USER_AGENT="$ua"
68 export GIT_USER_AGENT
70 unset GIT_CONFIG_PARAMETERS
71 git_add_config "core.ignoreCase=false"
72 git_add_config "core.pager=cat"
73 if [ -n "$cfg_git_no_mmap" ]; then
74 # Just like compiling with NO_MMAP
75 git_add_config "core.packedGitWindowSize=1m"
76 else
77 # Always use the 32-bit default (32m) even on 64-bit to avoid memory blowout
78 git_add_config "core.packedGitWindowSize=32m"
80 # Always use the 32-bit default (256m) even on 64-bit to avoid memory blowout
81 git_add_config "core.packedGitLimit=256m"
82 [ -z "$var_big_file_threshold" ] ||
83 git_add_config "core.bigFileThreshold=$var_big_file_threshold"
84 git_add_config "gc.auto=0"
86 if [ "${cfg_fetch_stash_refs:-0}" = "0" ]; then
87 git_add_config "uploadpack.hiderefs=refs/stash"
88 git_add_config "uploadpack.hiderefs=refs/tgstash"
91 fromip="${SSH_CLIENT%% *}"
92 fromip="${fromip:+$fromip }"
94 logmsg()
96 [ "${cfg_suppress_git_ssh_logging:-0}" = "0" ] || return 0
97 _msg="$(LC_ALL=C tr -c '\040-\176' '[?*]' <<EOT
98 $fromip$* $LOGNAME
99 EOT
100 )" && _msg="${_msg%[?]}" &&
101 logger -t "${0##*/}[$$]" <<EOT
102 $_msg
106 # When accessing Git via ssh, there should never be a terminal on 0, 1 or 2
107 # The "no-pty" flag should be set in all the ssh keys files to prevent this
108 # However, just in case, check for it here and die as a safety fallback
109 if [ -t 0 ] || [ -t 1 ] || [ -t 2 ]; then
110 logmsg "denied pty"
111 echo forbidden >&2
112 exit 1
115 # Only the following commands are allowed:
117 # git-shell -c "git-receive-pack 'dir'"
118 # git-shell -c "git receive-pack 'dir'"
119 # git-shell -c "git-upload-pack 'dir'"
120 # git-shell -c "git upload-pack 'dir'"
121 # git-shell -c "git-upload-archive 'dir'"
122 # git-shell -c "git upload-archive 'dir'"
124 # where dir must start with $reporoot/ but a leading/trailing '/' is optional
125 # as well as the final .git however if $dir does not start with $reporoot but
126 # adding a $reporoot prefix makes it work then the $reporoot prefix will be
127 # silently added.
129 if [ "$1" != "-c" ]; then
130 echo forbidden >&2
131 exit 1
134 dir="$2"
135 type=''
136 case "$2" in
137 "git-receive-pack "*) type='receive-pack'; dir="${dir#git-receive-pack }";;
138 "git receive-pack "*) type='receive-pack'; dir="${dir#git receive-pack }";;
139 "git-upload-pack "*) type='upload-pack'; dir="${dir#git-upload-pack }";;
140 "git upload-pack "*) type='upload-pack'; dir="${dir#git upload-pack }";;
141 "git-upload-archive "*) type='upload-archive'; dir="${dir#git-upload-archive }";;
142 "git upload-archive "*) type='upload-archive'; dir="${dir#git upload-archive }";;
144 echo forbidden >&2
145 exit 1
146 esac
148 # valid project names only allow 0-9A-Za-z._+- plus the / separator and they
149 # are always single quoted so the only valid directory names will always start
150 # with a single quote and end with a single quote and not contain any internal
151 # character that needs to be escaped.
153 case "$dir" in
154 "'"*) :;;
156 logmsg "denied $type $dir"
157 echo forbidden >&2
158 exit 1
159 esac
160 case "$dir" in
161 *"'") :;;
163 logmsg "denied $type $dir"
164 echo forbidden >&2
165 exit 1
166 esac
168 # Some shells do not properly handle quoting after # or % so we cannot
169 # put an explicit ' there in a way that works for all shells. Instead
170 # just remove a single character since we've already verified it's a '.
171 dir="${dir#?}"; dir="${dir%?}"
173 # add a missing leading /
174 case "$dir" in
175 /*) :;;
177 dir="/$dir"
178 esac
180 # remove a trailing /
181 case "$dir" in
182 *?/)
183 dir="${dir%/}"
184 esac
186 # add a missing trailing .git
187 case "$dir" in
188 *.git) :;;
190 dir="$dir.git"
191 esac
193 # do not allow any .. sequence
194 case "$dir" in *..*)
195 logmsg "denied $type $dir"
196 echo forbidden >&2
197 exit 1
198 esac
200 odir="$dir"
201 case "$dir" in
202 "$reporoot/"*) :;;
204 # Allow it if prefixing with $reporoot matches an existing directory
205 if [ -d "$reporoot$dir" ]; then
206 dir="$reporoot$dir"
207 else
208 logmsg "denied $type $dir"
209 echo forbidden >&2
210 exit 1
212 esac
214 # Valid project names never end in .git (we add that automagically), so a valid
215 # fork can never have .git at the end of any path component except the last.
216 # We check this to avoid a situation where a certain collection of pushed refs
217 # could be mistaken for a GIT_DIR. Git would ultimately complain, but some
218 # undesirable things could happen along the way.
220 # Remove the leading $reporoot and trailing .git to get a test string
221 testpath="${dir#$reporoot/}"
222 testpath="${testpath%.git}"
223 case "$testpath/" in *.[Gg][Ii][Tt]/*|_*)
224 logmsg "denied $type $odir"
225 echo forbidden >&2
226 exit 1
227 esac
229 if ! [ -d "$dir" ] || ! [ -f "$dir/HEAD" ] || ! [ -d "$dir/objects" ]; then
230 logmsg "denied $type $odir"
231 echo forbidden >&2
232 exit 1
235 proj="${dir#$reporoot/}"; projbare="${proj%.git}"
237 if [ "$type" = 'receive-pack' ] && [ "$LOGNAME" = 'git' ]; then
238 logmsg "denied $type $odir"
239 echo "The user '$LOGNAME' may only be used for fetches, sorry" >&2
240 exit 3
243 if [ "$type" = 'receive-pack' ] && ! [ -f "$dir/.nofetch" ]; then
244 logmsg "denied $type $odir"
245 echo "The $proj project is a mirror and may not be pushed to, sorry" >&2
246 exit 3
249 GIT_SHELL='git-shell'
250 if [ "$type" = 'receive-pack' ]; then
251 git_add_config 'receive.unpackLimit=1'
252 # Note the git config documentation is wrong
253 # transfer.unpackLimit, if set, overrides receive.unpackLimit
254 git_add_config 'transfer.unpackLimit=1'
255 # set up the correct git-shell command if cfg_max_file_size512 > 0
256 if [ "${cfg_max_file_size512:-0}" != "0" ]; then
257 GIT_SHELL='ulimit512 -i -f "$cfg_max_file_size512" -- git-shell'
261 if ! [ -x @perlbin@ ] && [ "$type" = 'receive-pack' ]; then
262 # We are INSIDE the chroot trying to push
264 if ! can_user_push "$projbare"; then
265 # If mob is enabled and mob has push permissions and
266 # the current user is not the mob then it's a personal mob push
267 # presuming the special mob directory has been set up
268 if [ "$mob" = "mob" ] && [ "$LOGNAME" != "mob" ] && [ -d "$reporoot/$proj/mob" ] &&
269 can_user_push "$projbare" mob; then
271 umask 113
272 >"/etc/sshactive/$LOGNAME,"
273 mv -f "/etc/sshactive/$LOGNAME," "/etc/sshactive/$LOGNAME"
274 ! [ -e "$dir/.delaygc" ] || >"$dir/.allowgc" || :
276 logmsg "accepted $type $odir mob"
277 eval 'exec '"$GIT_SHELL"' -c "git-receive-pack '\''$reporoot/$proj/mob'\''"'
278 exit 1
280 logmsg "denied $type $odir noperms"
281 echo "The user '$LOGNAME' does not have push permissions for project '$proj'" >&2
282 echo "You may adjust push permissions at $webadmurl/editproj.cgi?name=$proj" >&2
283 exit 3
286 umask 113
287 >"/etc/sshactive/$LOGNAME,"
288 mv -f "/etc/sshactive/$LOGNAME," "/etc/sshactive/$LOGNAME"
289 ! [ -e "$dir/.delaygc" ] || >"$dir/.allowgc" || :
293 [ -z "$var_upload_window" ] || [ "$type" != "upload-pack" ] ||
294 git_add_config "pack.window=$var_upload_window"
296 logmsg "accepted $type $odir"
297 eval 'exec '"$GIT_SHELL"' -c "git-$type '\''$dir'\''"'
298 exit 1