apache.conf.in: include input byte count if available
[girocco.git] / bin / git-daemon-verify
blob915e2d9cc1350865b43f030758117f1ee53ebd8f
1 #!/bin/sh
3 # Abort any fetch early if the fetch is invalid or times out.
4 # This avoids unnecessary traffic and unpacked object pollution.
6 # This script is called for fetch, archive and push.
7 # Push requests are outright rejected and so are paths starting with ~.
9 set -e
11 . @basedir@/shlib.sh
13 unset GIT_USER_AGENT
14 unset GIT_HTTP_USER_AGENT
15 if [ -n "$defined_cfg_git_server_ua" ]; then
16 GIT_USER_AGENT="$cfg_git_server_ua"
17 export GIT_USER_AGENT
18 GIT_HTTP_USER_AGENT="$cfg_git_server_ua"
19 export GIT_HTTP_USER_AGENT
22 [ -z "$GIT_DAEMON_BIN" ] || cfg_git_daemon_bin="$GIT_DAEMON_BIN"
23 [ -n "$cfg_git_daemon_bin" ] || \
24 cfg_git_daemon_bin="$var_git_exec_path/git-daemon"
26 errormsg()
28 _l="$*"
29 printf '%04xERR %s' $(( 8 + ${#_l} )) "$_l"
32 invalbaderr()
34 errormsg "invalid or incomplete request"
37 invalerr()
39 errormsg "invalid or unsupported request"
42 denied()
44 errormsg "access denied or no such repository"
47 internalerr()
49 echo "git-daemon-verify: $*" >&2
50 errormsg "internal server error"
53 # A quick sanity check
54 if [ -z "$cfg_git_daemon_bin" ] || ! [ -x "$cfg_git_daemon_bin" ]; then
55 internalerr "bad cfg_git_daemon_bin: $cfg_git_daemon_bin"
56 exit 1
58 case "$cfg_reporoot" in /?*) :;; *)
59 internalerr "bad reporoot: $cfg_reporoot"
60 exit 1
61 esac
63 PATH="$(dirname "$cfg_git_daemon_bin"):$PATH"
64 export PATH
66 if ! request="$("$cfg_basedir/bin/peek_packet")"; then
67 invalbaderr
68 exit 1
71 # The request should look like one of the following
73 # git-upload-pack /dir
74 # git-upload-pack ~name/dir
75 # git-upload-archive /dir
76 # git-upload-archive ~name/dir
77 # git-receive-pack /dir
78 # git-receive-pack ~name/dir
80 # Where the '~' forms are relative to a user's home directory.
81 # A trailing '/' is optional as well as a final '.git'.
82 # git-receive-pack and paths starting with '~' are rejected outright.
84 type=
85 dir=
86 case "$request" in
87 "git-upload-pack "*) type='upload-pack'; dir="${request#git-upload-pack }";;
88 "git-upload-archive "*) type='upload-archive'; dir="${request#git-upload-archive }";;
89 "git-receive-pack "*) type='receive-pack'; dir="${request#git-receive-pack }";;
91 invalerr
92 exit 1
93 esac
94 if [ "$type" = 'receive-pack' ]; then
95 invalerr
96 exit 1
98 case "$dir" in /*) :;; *)
99 invalerr
100 exit 1
101 esac
103 # remove extraneous '/' chars
104 proj="${dir#/}"
105 proj="${proj%/}"
106 # add a missing .git
107 case "$proj" in
108 *.git) :;;
110 proj="$proj.git"
111 esac
113 # Reject any project names that start with _ or contain ..
114 case "$proj" in _*|*..*)
115 denied
116 exit 1
117 esac
119 reporoot="$cfg_reporoot"
120 dir="$reporoot/$proj"
122 # Valid project names never end in .git (we add that automagically), so a valid
123 # fork can never have .git at the end of any path component except the last.
124 # We check this to avoid a situation where a certain collection of pushed refs
125 # could be mistaken for a GIT_DIR. Git would ultimately complain, but some
126 # undesirable things could happen along the way.
128 # Remove the leading $reporoot and trailing .git to get a test string
129 testpath="${dir#$reporoot/}"
130 testpath="${testpath%.git}"
131 case "$testpath/" in *.[Gg][Ii][Tt]/*|_*)
132 denied
133 exit 1
134 esac
136 if ! [ -d "$dir" ] || ! [ -f "$dir/HEAD" ] || ! [ -d "$dir/objects" ]; then
137 denied
138 exit 1
141 [ -z "$var_upload_window" ] || [ "$type" != "upload-pack" ] || \
142 git_add_config "pack.window=$var_upload_window"
144 exec "$cfg_git_daemon_bin" --inetd --verbose --export-all --enable=upload-archive --base-path="$cfg_reporoot"
145 internalerr "exec failed: $cfg_git_daemon_bin"
146 exit 1