3 # authrequired.cgi -- show certification authorization instructions on 401
4 # Copyright (C) 2014,2016,2017 Kyle J. McKay. All rights reserved.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 # We pretend like we don't exist. Unless this was an attempt to access a push
23 # URL over HTTP/HTTPS in which case we return a suitable error message.
25 # Some of this detection requires REQUEST_URI to be set which is an Apache
26 # extension. If REQUEST_URI is not set that portion of the smart detection
29 # Also note that we return a 403 error instead of a 401 error because we require
30 # a user push certificate. Returning a 401 error and having the client then
31 # provide a user name and password is completely pointless since we now are
32 # providing copious amounts of help text.
34 # If the client appears to be an older version of Git that will probably not
35 # display a text/plain error response to the user then the error message is
36 # sent as an 'ERR ' packet in smart protocol format instead. Git versions
37 # older than 1.8.3 as well as JGit and libgit require the 'ERR ' packet format.
38 # This 'ERR ' packet format support requires that REQUEST_URI be set to the
39 # original URI that was fetched or it will never trigger.
44 printf '%s\r\n' "Status: $1"
45 printf '%s\r\n' "Expires: Fri, 01 Jan 1980 00:00:00 GMT"
46 printf '%s\r\n' "Pragma: no-cache"
47 printf '%s\r\n' "Cache-Control: no-cache, max-age=0, must-revalidate"
48 printf '%s\r\n' "Content-Type: $2"
49 [ -z "$3" ] ||
printf "%s\r\n" "$3"
54 # Simulate a 404 error as though we do not exist
55 headers
404 "text/html; charset=utf-8"
57 [ -z "$REQUEST_URI" ] || SPACE
=" "
59 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
61 <title>404 Not Found</title>
64 <p>The requested URL $REQUEST_URI${SPACE}was not found on this server.</p>
72 # Set isoldgit if we've detected an old Git client that needs an 'ERR ' packet
74 case "$HTTP_USER_AGENT" in *[Gg
]it
/*)
75 case "$HTTP_USER_AGENT" in
80 suffix
="${HTTP_USER_AGENT##*[Gg]it/}"
81 gitvers
="${suffix%%[!0-9.]*}"
82 gitvers
="${gitvers%.}"
84 [1-9][0-9]*|
[2-9]|
[2-9].
*|
1.
[1-9][0-9]*|
1.9*| \
85 1.8.
[1-9][0-9]*|
1.8.
[3-9]|
1.8.
[3-9].
*) :;;
94 if [ -n "$REQUEST_URI" ]; then
95 # Try to detect whether or not it was something that needs auth
97 BASE
="${REQUEST_URI%%[?]*}"
98 QS
="${REQUEST_URI#$BASE}"
102 case "&$QS&" in *"&service=git-receive-pack&"*)
103 service
=git-receive-pack
108 service
=git-receive-pack
112 [ -n "$needsauth" ] || notfound
114 # Return a text/plain response WITHOUT any additional parameters (such as
115 # charset=) so that the Git client will display the result.
117 # If AUTHREQUIRED_MESSAGE is set, that's inserted as an extra line
121 if [ -n "$AUTHREQUIRED_MESSAGE" ]; then
122 xmsg
="$AUTHREQUIRED_MESSAGE$nl$nl"
125 # We need some config variables
129 ======================================================================
130 Authentication Required
131 ======================================================================
133 ${xmsg}In order to push using https, you must first
134 configure a user push certificate.
136 You may download a user push certificate from
137 the edit user page that may be accessed at:
139 $cfg_webadmurl/edituser.cgi
141 Instructions for configuring Git to use the
142 downloaded push certificate can be found at:
144 $cfg_htmlurl/httpspush.html
146 Do not forget to also configure the location
147 of your private key (see the above page).
149 ======================================================================
151 if [ -n "$isoldgit" ] && [ -n "$service" ]; then
152 l1
=$
(( 15 + ${#service} ))
153 l2
=$
(( 9 + ${#message} ))
154 headers
200 "application/x-$service-advertisement" \
155 "Content-Length: $(( $l1 + 4 + $l2 ))"
156 printf '%04x# service=%s\n0000%04xERR \n%s' $l1 "$service" \
159 # IMPORTANT: Do NOT add any extra parameters here!
160 # Older Git clients cannot handle anything other than
161 # EXACTLY "text/plain"!
162 headers
403 "text/plain"
163 printf '%s' "$message"