|
de66eea…
|
drh
|
1 |
/* |
|
de66eea…
|
drh
|
2 |
** Copyright (c) 2025 D. Richard Hipp |
|
de66eea…
|
drh
|
3 |
** |
|
de66eea…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
de66eea…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
de66eea…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
de66eea…
|
drh
|
7 |
|
|
de66eea…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
de66eea…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
de66eea…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
de66eea…
|
drh
|
11 |
** |
|
de66eea…
|
drh
|
12 |
** Author contact information: |
|
de66eea…
|
drh
|
13 |
** [email protected] |
|
de66eea…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
de66eea…
|
drh
|
15 |
** |
|
de66eea…
|
drh
|
16 |
******************************************************************************* |
|
de66eea…
|
drh
|
17 |
** |
|
de66eea…
|
drh
|
18 |
** This file contains code that attempts to prevent robots and |
|
de66eea…
|
drh
|
19 |
** especially bot-nets from consume excess CPU and bandwidth when |
|
de66eea…
|
drh
|
20 |
** Fossil is run as a service. |
|
de66eea…
|
drh
|
21 |
*/ |
|
de66eea…
|
drh
|
22 |
#include "config.h" |
|
de66eea…
|
drh
|
23 |
#include "robot.h" |
|
de66eea…
|
drh
|
24 |
#include <assert.h> |
|
de66eea…
|
drh
|
25 |
#include <time.h> |
|
de66eea…
|
drh
|
26 |
|
|
dc2232c…
|
drh
|
27 |
/* |
|
dc2232c…
|
drh
|
28 |
** The name of the cookie used to demonstrate that the client has been |
|
dc2232c…
|
drh
|
29 |
** tested and is believed to be operated by a human, not by a robot. |
|
dc2232c…
|
drh
|
30 |
*/ |
|
dc2232c…
|
drh
|
31 |
#if INTERFACE |
|
dc2232c…
|
drh
|
32 |
#define ROBOT_COOKIE "fossil-client-ok" |
|
dc2232c…
|
drh
|
33 |
#endif |
|
dc2232c…
|
drh
|
34 |
|
|
dc2232c…
|
drh
|
35 |
/* |
|
4fa618f…
|
drh
|
36 |
** Values computed only once and then cached. |
|
4fa618f…
|
drh
|
37 |
*/ |
|
4fa618f…
|
drh
|
38 |
static struct RobotCache { |
|
4fa618f…
|
drh
|
39 |
unsigned int h1, h2; /* Proof-of-work hash values */ |
|
4fa618f…
|
drh
|
40 |
unsigned int resultCache; /* 0: unknown. 1: human 2: might-be-robot */ |
|
4fa618f…
|
drh
|
41 |
} robot = { 0, 0, 0 }; |
|
4fa618f…
|
drh
|
42 |
|
|
4fa618f…
|
drh
|
43 |
/* |
|
4d198d0…
|
drh
|
44 |
** Allowed values for robot.resultCache. |
|
4d198d0…
|
drh
|
45 |
** |
|
4d198d0…
|
drh
|
46 |
** The names are slightly misleading. KNOWN_NOT_ROBOT might be set even |
|
4d198d0…
|
drh
|
47 |
** if the client is a robot, but only if the robot is an approved robot. |
|
4d198d0…
|
drh
|
48 |
** A better name might be "KNOWN_NOT_UNAUTHORIZED_ROBOT", but that is too |
|
4d198d0…
|
drh
|
49 |
** long of a name. |
|
4fa618f…
|
drh
|
50 |
*/ |
|
4d198d0…
|
drh
|
51 |
#define KNOWN_NOT_ROBOT 1 /* Approved to consume CPU and bandwidth */ |
|
4d198d0…
|
drh
|
52 |
#define MIGHT_BE_ROBOT 2 /* Might be an unapproved robot */ |
|
4fa618f…
|
drh
|
53 |
|
|
4fa618f…
|
drh
|
54 |
/* |
|
4fa618f…
|
drh
|
55 |
** Compute two hashes, robot.h1 and robot.h2, that are used as |
|
4fa618f…
|
drh
|
56 |
** part of determining whether or not the HTTP client is a robot. |
|
4fa618f…
|
drh
|
57 |
** These hashes are based on current time, client IP address, |
|
4fa618f…
|
drh
|
58 |
** and User-Agent. robot.h1 is for the current time slot and |
|
4fa618f…
|
drh
|
59 |
** robot.h2 is the previous. |
|
de66eea…
|
drh
|
60 |
** |
|
4fa618f…
|
drh
|
61 |
** The hashes are integer values between 100,000,000 and 999,999,999 |
|
4fa618f…
|
drh
|
62 |
** inclusive. |
|
de66eea…
|
drh
|
63 |
*/ |
|
4fa618f…
|
drh
|
64 |
static void robot_pow_hash(void){ |
|
4fa618f…
|
drh
|
65 |
const char *az[2], *z; |
|
de66eea…
|
drh
|
66 |
sqlite3_int64 tm; |
|
4fa618f…
|
drh
|
67 |
unsigned int h1, h2, k; |
|
4fa618f…
|
drh
|
68 |
|
|
4fa618f…
|
drh
|
69 |
if( robot.h1 ) return; /* Already computed */ |
|
de66eea…
|
drh
|
70 |
|
|
de66eea…
|
drh
|
71 |
/* Construct a proof-of-work value based on the IP address of the |
|
de66eea…
|
drh
|
72 |
** sender and the sender's user-agent string. The current time also |
|
de66eea…
|
drh
|
73 |
** affects the pow value, so actually compute two values, one for the |
|
de66eea…
|
drh
|
74 |
** current 900-second interval and one for the previous. Either can |
|
de66eea…
|
drh
|
75 |
** match. The pow-value is an integer between 100,000,000 and |
|
4fa618f…
|
drh
|
76 |
** 999,999,999. |
|
4fa618f…
|
drh
|
77 |
*/ |
|
de66eea…
|
drh
|
78 |
az[0] = P("REMOTE_ADDR"); |
|
de66eea…
|
drh
|
79 |
az[1] = P("HTTP_USER_AGENT"); |
|
de66eea…
|
drh
|
80 |
tm = time(0); |
|
4fa618f…
|
drh
|
81 |
h1 = (unsigned)(tm/900)&0xffffffff; |
|
de66eea…
|
drh
|
82 |
h2 = h1 - 1; |
|
de66eea…
|
drh
|
83 |
for(k=0; k<2; k++){ |
|
de66eea…
|
drh
|
84 |
z = az[k]; |
|
de66eea…
|
drh
|
85 |
if( z==0 ) continue; |
|
de66eea…
|
drh
|
86 |
while( *z ){ |
|
de66eea…
|
drh
|
87 |
h1 = (h1 + *(unsigned char*)z)*0x9e3779b1; |
|
de66eea…
|
drh
|
88 |
h2 = (h2 + *(unsigned char*)z)*0x9e3779b1; |
|
de66eea…
|
drh
|
89 |
z++; |
|
de66eea…
|
drh
|
90 |
} |
|
de66eea…
|
drh
|
91 |
} |
|
4fa618f…
|
drh
|
92 |
robot.h1 = (h1 % 900000000) + 100000000; |
|
4fa618f…
|
drh
|
93 |
robot.h2 = (h2 % 900000000) + 100000000; |
|
4d198d0…
|
drh
|
94 |
} |
|
4d198d0…
|
drh
|
95 |
|
|
4d198d0…
|
drh
|
96 |
/* |
|
fc8cefb…
|
drh
|
97 |
** Remember that the client is human, as evidenced by the presence of |
|
fc8cefb…
|
drh
|
98 |
** the fossil-client-ok or similar proof of work. Enable hyperlinks if |
|
fc8cefb…
|
drh
|
99 |
** auto-hyperlink is turned on. |
|
fc8cefb…
|
drh
|
100 |
*/ |
|
fc8cefb…
|
drh
|
101 |
static void remember_is_human(void){ |
|
fc8cefb…
|
drh
|
102 |
robot.resultCache = KNOWN_NOT_ROBOT; |
|
fc8cefb…
|
drh
|
103 |
if( g.perm.Hyperlink==0 ){ |
|
fc8cefb…
|
drh
|
104 |
int autoLink = db_get_int("auto-hyperlink",1); |
|
fc8cefb…
|
drh
|
105 |
if( autoLink==1 ){ |
|
fc8cefb…
|
drh
|
106 |
g.jsHref = 1; |
|
fc8cefb…
|
drh
|
107 |
g.perm.Hyperlink = 1; |
|
fc8cefb…
|
drh
|
108 |
}else if( autoLink==2 ){ |
|
fc8cefb…
|
drh
|
109 |
g.perm.Hyperlink = 1; |
|
fc8cefb…
|
drh
|
110 |
} |
|
fc8cefb…
|
drh
|
111 |
} |
|
fc8cefb…
|
drh
|
112 |
} |
|
fc8cefb…
|
drh
|
113 |
|
|
fc8cefb…
|
drh
|
114 |
/* |
|
4fa618f…
|
drh
|
115 |
** Return true if the HTTP client has not demonstrated that it is |
|
4fa618f…
|
drh
|
116 |
** human interactive. Return false is the HTTP client might be |
|
4fa618f…
|
drh
|
117 |
** a non-interactive robot. |
|
4fa618f…
|
drh
|
118 |
** |
|
4fa618f…
|
drh
|
119 |
** For this routine, any of the following is considered proof that |
|
4fa618f…
|
drh
|
120 |
** the HTTP client is not a robot: |
|
4fa618f…
|
drh
|
121 |
** |
|
4fa618f…
|
drh
|
122 |
** 1. There is a valid login, including "anonymous". User "nobody" |
|
4fa618f…
|
drh
|
123 |
** is not a valid login, but every other user is. |
|
4fa618f…
|
drh
|
124 |
** |
|
4fa618f…
|
drh
|
125 |
** 2. There exists a ROBOT_COOKIE with the correct proof-of-work |
|
4fa618f…
|
drh
|
126 |
** value. |
|
4fa618f…
|
drh
|
127 |
** |
|
4fa618f…
|
drh
|
128 |
** 3. There exists a proof=VALUE query parameter where VALUE is |
|
4fa618f…
|
drh
|
129 |
** a correct proof-of-work value. |
|
4fa618f…
|
drh
|
130 |
** |
|
4fa618f…
|
drh
|
131 |
** 4. There exists a valid token=VALUE query parameter. |
|
4fa618f…
|
drh
|
132 |
** |
|
4fa618f…
|
drh
|
133 |
** After being run once, this routine caches its findings and |
|
4fa618f…
|
drh
|
134 |
** returns very quickly on subsequent invocations. |
|
4fa618f…
|
drh
|
135 |
*/ |
|
4fa618f…
|
drh
|
136 |
int client_might_be_a_robot(void){ |
|
4fa618f…
|
drh
|
137 |
const char *z; |
|
4fa618f…
|
drh
|
138 |
|
|
4fa618f…
|
drh
|
139 |
/* Only do this computation once, then cache the results for future |
|
4fa618f…
|
drh
|
140 |
** use */ |
|
4fa618f…
|
drh
|
141 |
if( robot.resultCache ){ |
|
4fa618f…
|
drh
|
142 |
return robot.resultCache==MIGHT_BE_ROBOT; |
|
4fa618f…
|
drh
|
143 |
} |
|
4fa618f…
|
drh
|
144 |
|
|
4fa618f…
|
drh
|
145 |
/* Condition 1: Is there a valid login? |
|
4fa618f…
|
drh
|
146 |
*/ |
|
4fa618f…
|
drh
|
147 |
if( g.userUid==0 ){ |
|
4fa618f…
|
drh
|
148 |
login_check_credentials(); |
|
4fa618f…
|
drh
|
149 |
} |
|
4fa618f…
|
drh
|
150 |
if( g.zLogin!=0 ){ |
|
4fa618f…
|
drh
|
151 |
robot.resultCache = KNOWN_NOT_ROBOT; |
|
4fa618f…
|
drh
|
152 |
return 0; |
|
4fa618f…
|
drh
|
153 |
} |
|
4fa618f…
|
drh
|
154 |
|
|
4fa618f…
|
drh
|
155 |
/* Condition 2: If there is already a proof-of-work cookie |
|
4fa618f…
|
drh
|
156 |
** with a correct value, then the user agent has been authenticated. |
|
4fa618f…
|
drh
|
157 |
*/ |
|
4fa618f…
|
drh
|
158 |
z = P(ROBOT_COOKIE); |
|
4fa618f…
|
drh
|
159 |
if( z ){ |
|
4fa618f…
|
drh
|
160 |
unsigned h = atoi(z); |
|
4fa618f…
|
drh
|
161 |
robot_pow_hash(); |
|
4fa618f…
|
drh
|
162 |
if( (h==robot.h1 || h==robot.h2) && !cgi_is_qp(ROBOT_COOKIE) ){ |
|
fc8cefb…
|
drh
|
163 |
remember_is_human(); |
|
4fa618f…
|
drh
|
164 |
return 0; |
|
4fa618f…
|
drh
|
165 |
} |
|
4fa618f…
|
drh
|
166 |
} |
|
4fa618f…
|
drh
|
167 |
|
|
4fa618f…
|
drh
|
168 |
/* Condition 3: There is a "proof=VALUE" query parameter with a valid |
|
4fa618f…
|
drh
|
169 |
** VALUE attached. If this is the case, also set the robot cookie |
|
4fa618f…
|
drh
|
170 |
** so that future requests will hit condition 2 above. |
|
4fa618f…
|
drh
|
171 |
*/ |
|
4fa618f…
|
drh
|
172 |
z = P("proof"); |
|
4fa618f…
|
drh
|
173 |
if( z ){ |
|
4fa618f…
|
drh
|
174 |
unsigned h = atoi(z); |
|
4fa618f…
|
drh
|
175 |
robot_pow_hash(); |
|
4fa618f…
|
drh
|
176 |
if( h==robot.h1 || h==robot.h2 ){ |
|
4fa618f…
|
drh
|
177 |
cgi_set_cookie(ROBOT_COOKIE,z,"/",900); |
|
fc8cefb…
|
drh
|
178 |
remember_is_human(); |
|
4fa618f…
|
drh
|
179 |
return 0; |
|
4fa618f…
|
drh
|
180 |
} |
|
4fa618f…
|
drh
|
181 |
cgi_tag_query_parameter("proof"); |
|
4fa618f…
|
drh
|
182 |
} |
|
4fa618f…
|
drh
|
183 |
|
|
4fa618f…
|
drh
|
184 |
/* Condition 4: If there is a "token=VALUE" query parameter with a |
|
4fa618f…
|
drh
|
185 |
** valid VALUE argument, then assume that the request is coming from |
|
4fa618f…
|
drh
|
186 |
** either an interactive human session, or an authorized robot that we |
|
4231598…
|
drh
|
187 |
** want to treat as human. Allow it through and also set the robot cookie. |
|
4fa618f…
|
drh
|
188 |
*/ |
|
4fa618f…
|
drh
|
189 |
z = P("token"); |
|
4fa618f…
|
drh
|
190 |
if( z!=0 ){ |
|
4fa618f…
|
drh
|
191 |
if( db_exists("SELECT 1 FROM config" |
|
4fa618f…
|
drh
|
192 |
" WHERE name='token-%q'" |
|
4fa618f…
|
drh
|
193 |
" AND json_valid(value,6)" |
|
4fa618f…
|
drh
|
194 |
" AND value->>'user' IS NOT NULL", z) |
|
4fa618f…
|
drh
|
195 |
){ |
|
4fa618f…
|
drh
|
196 |
char *zVal; |
|
4fa618f…
|
drh
|
197 |
robot_pow_hash(); |
|
4fa618f…
|
drh
|
198 |
zVal = mprintf("%u", robot.h1); |
|
4fa618f…
|
drh
|
199 |
cgi_set_cookie(ROBOT_COOKIE,zVal,"/",900); |
|
4fa618f…
|
drh
|
200 |
fossil_free(zVal); |
|
4fa618f…
|
drh
|
201 |
robot.resultCache = KNOWN_NOT_ROBOT; |
|
4fa618f…
|
drh
|
202 |
return 0; /* There is a valid token= query parameter */ |
|
4fa618f…
|
drh
|
203 |
} |
|
4fa618f…
|
drh
|
204 |
cgi_tag_query_parameter("token"); |
|
4fa618f…
|
drh
|
205 |
} |
|
4fa618f…
|
drh
|
206 |
|
|
4fa618f…
|
drh
|
207 |
/* We have no proof that the request is coming from an interactive |
|
4fa618f…
|
drh
|
208 |
** human session, so assume the request comes from a robot. |
|
4fa618f…
|
drh
|
209 |
*/ |
|
4fa618f…
|
drh
|
210 |
robot.resultCache = MIGHT_BE_ROBOT; |
|
4fa618f…
|
drh
|
211 |
return 1; |
|
4fa618f…
|
drh
|
212 |
} |
|
4fa618f…
|
drh
|
213 |
|
|
4fa618f…
|
drh
|
214 |
/* |
|
4fa618f…
|
drh
|
215 |
** Rewrite the current page with content that attempts |
|
4fa618f…
|
drh
|
216 |
** to prove that the client is not a robot. |
|
4fa618f…
|
drh
|
217 |
*/ |
|
4fa618f…
|
drh
|
218 |
static void ask_for_proof_that_client_is_not_robot(void){ |
|
4fa618f…
|
drh
|
219 |
unsigned p1, p2, p3, p4, p5, k2, k3; |
|
4fa618f…
|
drh
|
220 |
int k; |
|
de66eea…
|
drh
|
221 |
|
|
de66eea…
|
drh
|
222 |
/* Ask the client to present proof-of-work */ |
|
de66eea…
|
drh
|
223 |
cgi_reset_content(); |
|
de66eea…
|
drh
|
224 |
cgi_set_content_type("text/html"); |
|
16b3309…
|
drh
|
225 |
style_header("Browser Verification"); |
|
16b3309…
|
drh
|
226 |
@ <h1 id="x1">Checking to see if you are a robot<span id="x2"></span></h1> |
|
e5b00c6…
|
drh
|
227 |
@ <form method="GET" id="x6"><p> |
|
e5b00c6…
|
drh
|
228 |
@ <span id="x3" style="visibility:hidden;">\ |
|
e5b00c6…
|
drh
|
229 |
@ Press <input type="submit" id="x5" value="Ok" focus> to continue</span> |
|
4c4bce3…
|
drh
|
230 |
@ <span id="x7" style="visibility:hidden;">You appear to be a robot.</span>\ |
|
4c4bce3…
|
drh
|
231 |
@ </p> |
|
96fab06…
|
andybradford
|
232 |
if( g.zExtra && g.zExtra[0] ) cgi_tag_query_parameter("name"); |
|
16b3309…
|
drh
|
233 |
cgi_query_parameters_to_hidden(); |
|
16b3309…
|
drh
|
234 |
@ <input id="x4" type="hidden" name="proof" value="0"> |
|
16b3309…
|
drh
|
235 |
@ </form> |
|
16b3309…
|
drh
|
236 |
@ <script nonce='%s(style_nonce())'> |
|
4c4bce3…
|
drh
|
237 |
@ function aaa(x){return document.getElementById(x);}\ |
|
e5b00c6…
|
drh
|
238 |
@ function bbb(h,a){\ |
|
e5b00c6…
|
drh
|
239 |
@ aaa("x4").value=h;\ |
|
e5b00c6…
|
drh
|
240 |
@ if((a%%75)==0){\ |
|
e5b00c6…
|
drh
|
241 |
@ aaa("x2").textContent=aaa("x2").textContent+".";\ |
|
3d32a10…
|
drh
|
242 |
@ }var z;\ |
|
e5b00c6…
|
drh
|
243 |
@ if(a>0){\ |
|
e5b00c6…
|
drh
|
244 |
@ setTimeout(bbb,1,h+a,a-1);\ |
|
3d32a10…
|
drh
|
245 |
@ }else if((z=window.getComputedStyle(document.body).zIndex)==='0'||z===0){\ |
|
e5b00c6…
|
drh
|
246 |
@ aaa("x3").style.visibility="visible";\ |
|
e5b00c6…
|
drh
|
247 |
@ aaa("x2").textContent="";\ |
|
e5b00c6…
|
drh
|
248 |
@ aaa("x1").textContent="All clear";\ |
|
e5b00c6…
|
drh
|
249 |
@ aaa("x6").onsubmit=function(){aaa("x3").style.visibility="hidden";};\ |
|
e5b00c6…
|
drh
|
250 |
@ aaa("x5").focus();\ |
|
e5b00c6…
|
drh
|
251 |
@ }else{\ |
|
e5b00c6…
|
drh
|
252 |
@ aaa("x7").style.visibility="visible";\ |
|
e5b00c6…
|
drh
|
253 |
@ aaa("x2").textContent="";\ |
|
e5b00c6…
|
drh
|
254 |
@ aaa("x3").style.display="none";\ |
|
e5b00c6…
|
drh
|
255 |
@ aaa("x1").textContent="Access Denied";\ |
|
e5b00c6…
|
drh
|
256 |
@ }\ |
|
4c4bce3…
|
drh
|
257 |
@ }\ |
|
4fa618f…
|
drh
|
258 |
robot_pow_hash(); |
|
4fa618f…
|
drh
|
259 |
k = 400 + robot.h2%299; |
|
4fa618f…
|
drh
|
260 |
k2 = (robot.h2/299)%99 + 973; |
|
4fa618f…
|
drh
|
261 |
k3 = (robot.h2/(299*99))%99 + 811; |
|
4c4bce3…
|
drh
|
262 |
p1 = (k*k + k)/2; |
|
4fa618f…
|
drh
|
263 |
p2 = robot.h1-p1; |
|
4c4bce3…
|
drh
|
264 |
p3 = p2%k2; |
|
4c4bce3…
|
drh
|
265 |
p4 = (p2/k2)%k3; |
|
4c4bce3…
|
drh
|
266 |
p5 = p2/(k2*k3); |
|
4c4bce3…
|
drh
|
267 |
@ function ccc(a,b,c){return (a*%u(k3)+b)*%u(k2)+c;}\ |
|
4c4bce3…
|
drh
|
268 |
@ window.addEventListener('load',function(){\ |
|
4c4bce3…
|
drh
|
269 |
@ bbb(ccc(%u(p5),%u(p4),%u(p3)),%u(k));},false); |
|
4cc0881…
|
florian
|
270 |
/* Prevent successfully completed robot checks from reappearing and force |
|
4cc0881…
|
florian
|
271 |
** incomplete checks to start over when navigating back and forward. More |
|
4cc0881…
|
florian
|
272 |
** information: <https://stackoverflow.com/a/43043658>. */ |
|
b12e52a…
|
florian
|
273 |
@ window.addEventListener('pageshow',function(e){if(e.persisted)\ |
|
4cc0881…
|
florian
|
274 |
@ window.location.reload();}); |
|
16b3309…
|
drh
|
275 |
@ </script> |
|
16b3309…
|
drh
|
276 |
style_finish_page(); |
|
16b3309…
|
drh
|
277 |
} |
|
16b3309…
|
drh
|
278 |
|
|
16b3309…
|
drh
|
279 |
/* |
|
16b3309…
|
drh
|
280 |
** SETTING: robot-restrict width=40 block-text |
|
16b3309…
|
drh
|
281 |
** The VALUE of this setting is a list of GLOB patterns that match |
|
7db623e…
|
danield
|
282 |
** pages for which complex HTTP requests from unauthenticated clients |
|
16b3309…
|
drh
|
283 |
** should be disallowed. "Unauthenticated" means the user is "nobody". |
|
16b3309…
|
drh
|
284 |
** The recommended value for this setting is: |
|
e5b00c6…
|
drh
|
285 |
** |
|
d55227e…
|
florian
|
286 |
** timelineX,diff,annotate,fileage,file,finfo,reports,tree,hexdump,download |
|
4231598…
|
drh
|
287 |
** |
|
4231598…
|
drh
|
288 |
** Usually the tag should exactly match the page name. The "diff" tag |
|
4231598…
|
drh
|
289 |
** covers all diffing pages such as /vdiff, /fdiff, and /vpatch. The |
|
4231598…
|
drh
|
290 |
** "annotate" tag also covers /blame and /praise. "zip" also covers |
|
4231598…
|
drh
|
291 |
** /tarball and /sqlar. If a tag has an "X" character appended then it |
|
4231598…
|
drh
|
292 |
** only applies if query parameters are such that the page is particularly |
|
4231598…
|
drh
|
293 |
** difficult to compute. Useful "X" tags include "timelineX" and "zipX". |
|
4231598…
|
drh
|
294 |
** The "ext" tag matches all extension, but a tag of the form "ext/PATH" |
|
4231598…
|
drh
|
295 |
** only matches the extension at PATH. |
|
db69c47…
|
drh
|
296 |
** |
|
4231598…
|
drh
|
297 |
** See the [[robot-zip-leaf]] and [[robot-zip-tag]] settings |
|
4d198d0…
|
drh
|
298 |
** for additional controls associated with the "zipX" restriction. |
|
db69c47…
|
drh
|
299 |
** |
|
db69c47…
|
drh
|
300 |
** Change this setting "off" to disable all robot restrictions. |
|
86b6ef7…
|
drh
|
301 |
*/ |
|
86b6ef7…
|
drh
|
302 |
/* |
|
86b6ef7…
|
drh
|
303 |
** SETTING: robot-exception width=40 block-text |
|
86b6ef7…
|
drh
|
304 |
** |
|
86b6ef7…
|
drh
|
305 |
** The value of this setting should be a regular expression. |
|
86b6ef7…
|
drh
|
306 |
** If it matches the REQUEST_URI without the SCRIPT_NAME prefix |
|
86b6ef7…
|
drh
|
307 |
** matches this regular expression, then the request is an exception |
|
86b6ef7…
|
drh
|
308 |
** to anti-robot defenses and should be allowed through. For |
|
86b6ef7…
|
drh
|
309 |
** example, to allow robots to download tarballs or ZIP archives |
|
86b6ef7…
|
drh
|
310 |
** for named versions and releases, you could use an expression like |
|
86b6ef7…
|
drh
|
311 |
** this: |
|
86b6ef7…
|
drh
|
312 |
** |
|
8d32812…
|
drh
|
313 |
** ^/tarball/(version-[0-9.]+|release)/ |
|
86b6ef7…
|
drh
|
314 |
** |
|
86b6ef7…
|
drh
|
315 |
** This setting can hold multiple regular expressions, one |
|
86b6ef7…
|
drh
|
316 |
** regular expression per line. The input URL is exempted from |
|
86b6ef7…
|
drh
|
317 |
** anti-robot defenses if any of the multiple regular expressions |
|
86b6ef7…
|
drh
|
318 |
** matches. |
|
86b6ef7…
|
drh
|
319 |
*/ |
|
4d198d0…
|
drh
|
320 |
/* |
|
4d198d0…
|
drh
|
321 |
** SETTING: robot-zip-leaf boolean |
|
4d198d0…
|
drh
|
322 |
** |
|
4d198d0…
|
drh
|
323 |
** If this setting is true, the robots are allowed to download tarballs, |
|
4d198d0…
|
drh
|
324 |
** ZIP-archives, and SQL-archives even though "zipX" is found in |
|
6ce705b…
|
drh
|
325 |
** the [[robot-restrict]] setting as long as the specific check-in being |
|
4d198d0…
|
drh
|
326 |
** downloaded is a leaf check-in. |
|
4d198d0…
|
drh
|
327 |
*/ |
|
4d198d0…
|
drh
|
328 |
/* |
|
4d198d0…
|
drh
|
329 |
** SETTING: robot-zip-tag width=40 block-text |
|
4d198d0…
|
drh
|
330 |
** |
|
4d198d0…
|
drh
|
331 |
** If this setting is a list of GLOB patterns matching tags, |
|
4d198d0…
|
drh
|
332 |
** then robots are allowed to download tarballs, ZIP-archives, and |
|
6ce705b…
|
drh
|
333 |
** SQL-archives even though "zipX" appears in [[robot-restrict]], as long as |
|
4d198d0…
|
drh
|
334 |
** the specific check-in being downloaded has a tags that matches |
|
4d198d0…
|
drh
|
335 |
** the GLOB list of this setting. Recommended value: |
|
4d198d0…
|
drh
|
336 |
** "release,robot-access". |
|
4d198d0…
|
drh
|
337 |
*/ |
|
16b3309…
|
drh
|
338 |
|
|
16b3309…
|
drh
|
339 |
/* |
|
16b3309…
|
drh
|
340 |
** Return the default restriction GLOB |
|
16b3309…
|
drh
|
341 |
*/ |
|
16b3309…
|
drh
|
342 |
const char *robot_restrict_default(void){ |
|
67726b6…
|
florian
|
343 |
/* NOTE: The default value is also mentioned in the online help screen of |
|
67726b6…
|
florian
|
344 |
** the "robot-restrict" setting, and in the www/antibot.wiki document. */ |
|
6ce705b…
|
drh
|
345 |
return "timelineX,diff,annotate,fileage,file,finfo,reports," |
|
6ce705b…
|
drh
|
346 |
"tree,hexdump,download"; |
|
8784c60…
|
drh
|
347 |
} |
|
8784c60…
|
drh
|
348 |
|
|
8784c60…
|
drh
|
349 |
/* |
|
8784c60…
|
drh
|
350 |
** Return true if zTag matches one of the tags in the robot-restrict |
|
8784c60…
|
drh
|
351 |
** setting. |
|
4231598…
|
drh
|
352 |
** |
|
4231598…
|
drh
|
353 |
** A zTag of "*" matches anything. |
|
8784c60…
|
drh
|
354 |
*/ |
|
6ce705b…
|
drh
|
355 |
static int robot_restrict_has_tag(const char *zTag){ |
|
8784c60…
|
drh
|
356 |
static const char *zGlob = 0; |
|
8784c60…
|
drh
|
357 |
if( zGlob==0 ){ |
|
8784c60…
|
drh
|
358 |
zGlob = db_get("robot-restrict",robot_restrict_default()); |
|
8784c60…
|
drh
|
359 |
if( zGlob==0 ) zGlob = ""; |
|
8784c60…
|
drh
|
360 |
} |
|
8784c60…
|
drh
|
361 |
if( zGlob[0]==0 || fossil_strcmp(zGlob, "off")==0 ){ |
|
8784c60…
|
drh
|
362 |
return 0; |
|
4231598…
|
drh
|
363 |
} |
|
4231598…
|
drh
|
364 |
if( zTag==0 || (zTag[0]=='*' && zTag[1]==0) ){ |
|
4231598…
|
drh
|
365 |
return 1; |
|
86b6ef7…
|
drh
|
366 |
} |
|
8784c60…
|
drh
|
367 |
return glob_multi_match(zGlob,zTag); |
|
86b6ef7…
|
drh
|
368 |
} |
|
86b6ef7…
|
drh
|
369 |
|
|
86b6ef7…
|
drh
|
370 |
/* |
|
86b6ef7…
|
drh
|
371 |
** Check the request URI to see if it matches one of the URI |
|
86b6ef7…
|
drh
|
372 |
** exceptions listed in the robot-exception setting. Return true |
|
86b6ef7…
|
drh
|
373 |
** if it does. Return false if it does not. |
|
86b6ef7…
|
drh
|
374 |
** |
|
86b6ef7…
|
drh
|
375 |
** For the purposes of this routine, the "request URI" means |
|
86b6ef7…
|
drh
|
376 |
** the REQUEST_URI value with the SCRIPT_NAME prefix removed and |
|
86b6ef7…
|
drh
|
377 |
** with QUERY_STRING appended with a "?" separator if QUERY_STRING |
|
86b6ef7…
|
drh
|
378 |
** is not empty. |
|
86b6ef7…
|
drh
|
379 |
** |
|
86b6ef7…
|
drh
|
380 |
** If the robot-exception setting does not exist or is an empty |
|
86b6ef7…
|
drh
|
381 |
** string, then return false. |
|
86b6ef7…
|
drh
|
382 |
*/ |
|
86b6ef7…
|
drh
|
383 |
int robot_exception(void){ |
|
86b6ef7…
|
drh
|
384 |
const char *zRE = db_get("robot-exception",0); |
|
86b6ef7…
|
drh
|
385 |
const char *zQS; /* QUERY_STRING */ |
|
86b6ef7…
|
drh
|
386 |
const char *zURI; /* REQUEST_URI */ |
|
86b6ef7…
|
drh
|
387 |
const char *zSN; /* SCRIPT_NAME */ |
|
86b6ef7…
|
drh
|
388 |
const char *zNL; /* Next newline character */ |
|
86b6ef7…
|
drh
|
389 |
char *zRequest; /* REQUEST_URL w/o SCRIPT_NAME prefix + QUERY_STRING */ |
|
86b6ef7…
|
drh
|
390 |
int nRequest; /* Length of zRequest in bytes */ |
|
86b6ef7…
|
drh
|
391 |
size_t nURI, nSN; /* Length of zURI and zSN */ |
|
86b6ef7…
|
drh
|
392 |
int bMatch = 0; /* True if there is a match */ |
|
86b6ef7…
|
drh
|
393 |
|
|
86b6ef7…
|
drh
|
394 |
if( zRE==0 ) return 0; |
|
86b6ef7…
|
drh
|
395 |
if( zRE[0]==0 ) return 0; |
|
86b6ef7…
|
drh
|
396 |
zURI = PD("REQUEST_URI",""); |
|
86b6ef7…
|
drh
|
397 |
nURI = strlen(zURI); |
|
86b6ef7…
|
drh
|
398 |
zSN = PD("SCRIPT_NAME",""); |
|
86b6ef7…
|
drh
|
399 |
nSN = strlen(zSN); |
|
86b6ef7…
|
drh
|
400 |
if( nSN<=nURI ) zURI += nSN; |
|
86b6ef7…
|
drh
|
401 |
zQS = P("QUERY_STRING"); |
|
86b6ef7…
|
drh
|
402 |
if( zQS && zQS[0] ){ |
|
86b6ef7…
|
drh
|
403 |
zRequest = mprintf("%s?%s", zURI, zQS); |
|
86b6ef7…
|
drh
|
404 |
}else{ |
|
86b6ef7…
|
drh
|
405 |
zRequest = fossil_strdup(zURI); |
|
86b6ef7…
|
drh
|
406 |
} |
|
86b6ef7…
|
drh
|
407 |
nRequest = (int)strlen(zRequest); |
|
86b6ef7…
|
drh
|
408 |
while( zRE[0] && bMatch==0 ){ |
|
86b6ef7…
|
drh
|
409 |
char *z; |
|
86b6ef7…
|
drh
|
410 |
const char *zErr; |
|
86b6ef7…
|
drh
|
411 |
size_t n; |
|
86b6ef7…
|
drh
|
412 |
ReCompiled *pRe; |
|
86b6ef7…
|
drh
|
413 |
zNL = strchr(zRE,'\n'); |
|
86b6ef7…
|
drh
|
414 |
if( zNL ){ |
|
86b6ef7…
|
drh
|
415 |
n = (size_t)(zNL - zRE)+1; |
|
86b6ef7…
|
drh
|
416 |
while( zNL>zRE && fossil_isspace(zNL[0]) ) zNL--; |
|
86b6ef7…
|
drh
|
417 |
if( zNL==zRE ){ |
|
86b6ef7…
|
drh
|
418 |
zRE += n; |
|
86b6ef7…
|
drh
|
419 |
continue; |
|
86b6ef7…
|
drh
|
420 |
} |
|
86b6ef7…
|
drh
|
421 |
}else{ |
|
86b6ef7…
|
drh
|
422 |
n = strlen(zRE); |
|
86b6ef7…
|
drh
|
423 |
} |
|
86b6ef7…
|
drh
|
424 |
z = mprintf("%.*s", (int)(zNL - zRE)+1, zRE); |
|
86b6ef7…
|
drh
|
425 |
zRE += n; |
|
82888a0…
|
drh
|
426 |
zErr = fossil_re_compile(&pRe, z, 0); |
|
86b6ef7…
|
drh
|
427 |
if( zErr ){ |
|
86b6ef7…
|
drh
|
428 |
fossil_warning("robot-exception error \"%s\" in expression \"%s\"\n", |
|
86b6ef7…
|
drh
|
429 |
zErr, z); |
|
86b6ef7…
|
drh
|
430 |
fossil_free(z); |
|
86b6ef7…
|
drh
|
431 |
continue; |
|
86b6ef7…
|
drh
|
432 |
} |
|
86b6ef7…
|
drh
|
433 |
fossil_free(z); |
|
86b6ef7…
|
drh
|
434 |
bMatch = re_match(pRe, (const unsigned char*)zRequest, nRequest); |
|
86b6ef7…
|
drh
|
435 |
re_free(pRe); |
|
86b6ef7…
|
drh
|
436 |
} |
|
86b6ef7…
|
drh
|
437 |
fossil_free(zRequest); |
|
86b6ef7…
|
drh
|
438 |
return bMatch; |
|
86b6ef7…
|
drh
|
439 |
} |
|
86b6ef7…
|
drh
|
440 |
|
|
86b6ef7…
|
drh
|
441 |
/* |
|
6ce705b…
|
drh
|
442 |
** Return true if one or more of the conditions below are true. |
|
6ce705b…
|
drh
|
443 |
** Return false if all of the following are false: |
|
6ce705b…
|
drh
|
444 |
** |
|
35a09ae…
|
drh
|
445 |
** * The zTag is on the robot-restrict list and there exists |
|
35a09ae…
|
drh
|
446 |
** some tags that do not match robot-restrict or else zTag is |
|
35a09ae…
|
drh
|
447 |
** neither "/" or "xfer" or "login". |
|
35a09ae…
|
drh
|
448 |
** |
|
35a09ae…
|
drh
|
449 |
** The complication of the previous rule (everything after the |
|
35a09ae…
|
drh
|
450 |
** first "and") prevents a robot-restrict setting that contains |
|
35a09ae…
|
drh
|
451 |
** "*" from trying to bring up a robot-captcha for a "sync" |
|
35a09ae…
|
drh
|
452 |
** operation (the "/" or "xfer" page) or for the "login" page. |
|
6ce705b…
|
drh
|
453 |
** |
|
6ce705b…
|
drh
|
454 |
** * The client that submitted the HTTP request might be |
|
6ce705b…
|
drh
|
455 |
** a robot |
|
6ce705b…
|
drh
|
456 |
** |
|
6ce705b…
|
drh
|
457 |
** * The Request URI does not match any of the exceptions |
|
6ce705b…
|
drh
|
458 |
** in the robot-exception setting. |
|
6ce705b…
|
drh
|
459 |
** |
|
6ce705b…
|
drh
|
460 |
** In other words, return true if a call to robot_restrict() would |
|
6ce705b…
|
drh
|
461 |
** return true and false if a call to robot_restrict() would return |
|
6ce705b…
|
drh
|
462 |
** false. |
|
86b6ef7…
|
drh
|
463 |
** |
|
6ce705b…
|
drh
|
464 |
** The difference between this routine an robot_restrict() is that |
|
6ce705b…
|
drh
|
465 |
** this routine does not generate a proof-of-work captcha. This |
|
6ce705b…
|
drh
|
466 |
** routine does not change the HTTP reply in any way. It simply |
|
6ce705b…
|
drh
|
467 |
** returns true or false. |
|
86b6ef7…
|
drh
|
468 |
*/ |
|
6ce705b…
|
drh
|
469 |
int robot_would_be_restricted(const char *zTag){ |
|
86b6ef7…
|
drh
|
470 |
if( robot.resultCache==KNOWN_NOT_ROBOT ) return 0; |
|
86b6ef7…
|
drh
|
471 |
if( !robot_restrict_has_tag(zTag) ) return 0; |
|
86b6ef7…
|
drh
|
472 |
if( !client_might_be_a_robot() ) return 0; |
|
86b6ef7…
|
drh
|
473 |
if( robot_exception() ){ |
|
86b6ef7…
|
drh
|
474 |
robot.resultCache = KNOWN_NOT_ROBOT; |
|
35a09ae…
|
drh
|
475 |
return 0; |
|
35a09ae…
|
drh
|
476 |
} |
|
35a09ae…
|
drh
|
477 |
if( robot_restrict_has_tag("-not-a-real-tag-") |
|
35a09ae…
|
drh
|
478 |
&& (strcmp(zTag,"login")==0 || strcmp(zTag,"xfer")==0 || zTag[0]=='/') |
|
35a09ae…
|
drh
|
479 |
){ |
|
6ce705b…
|
drh
|
480 |
return 0; |
|
6ce705b…
|
drh
|
481 |
} |
|
4d198d0…
|
drh
|
482 |
return 1; |
|
6ce705b…
|
drh
|
483 |
} |
|
6ce705b…
|
drh
|
484 |
|
|
6ce705b…
|
drh
|
485 |
/* |
|
6ce705b…
|
drh
|
486 |
** Check to see if the page named in the argument is on the |
|
6ce705b…
|
drh
|
487 |
** robot-restrict list. If it is on the list and if the user |
|
6ce705b…
|
drh
|
488 |
** is might be a robot, then bring up a captcha to test to make |
|
6ce705b…
|
drh
|
489 |
** sure that client is not a robot. |
|
6ce705b…
|
drh
|
490 |
** |
|
6ce705b…
|
drh
|
491 |
** This routine returns true if a captcha was rendered and if subsequent |
|
6ce705b…
|
drh
|
492 |
** page generation should be aborted. It returns false if the page |
|
6ce705b…
|
drh
|
493 |
** should not be restricted and should be rendered normally. |
|
6ce705b…
|
drh
|
494 |
*/ |
|
6ce705b…
|
drh
|
495 |
int robot_restrict(const char *zTag){ |
|
6ce705b…
|
drh
|
496 |
if( robot_would_be_restricted(zTag) ){ |
|
6ce705b…
|
drh
|
497 |
/* Generate the proof-of-work captcha */ |
|
6ce705b…
|
drh
|
498 |
ask_for_proof_that_client_is_not_robot(); |
|
6ce705b…
|
drh
|
499 |
return 1; |
|
6ce705b…
|
drh
|
500 |
}else{ |
|
6ce705b…
|
drh
|
501 |
return 0; |
|
6ce705b…
|
drh
|
502 |
} |
|
4d198d0…
|
drh
|
503 |
} |
|
4d198d0…
|
drh
|
504 |
|
|
4d198d0…
|
drh
|
505 |
/* |
|
4d198d0…
|
drh
|
506 |
** Check to see if a robot is allowed to download a tarball, ZIP archive, |
|
4d198d0…
|
drh
|
507 |
** or SQL Archive for a particular check-in identified by the "rid" |
|
4d198d0…
|
drh
|
508 |
** argument. Return true to block the download. Return false to |
|
4d198d0…
|
drh
|
509 |
** continue. Prior to returning true, a captcha is presented to the user. |
|
4d198d0…
|
drh
|
510 |
** No output is generated when returning false. |
|
4d198d0…
|
drh
|
511 |
** |
|
4d198d0…
|
drh
|
512 |
** The rules: |
|
4d198d0…
|
drh
|
513 |
** |
|
4d198d0…
|
drh
|
514 |
** (1) If "zipX" is missing from the robot-restrict setting, then robots |
|
4d198d0…
|
drh
|
515 |
** are allowed to download any archive. None of the remaining rules |
|
4d198d0…
|
drh
|
516 |
** below are consulted unless "zipX" is on the robot-restrict setting. |
|
4d198d0…
|
drh
|
517 |
** |
|
4d198d0…
|
drh
|
518 |
** (2) If the robot-zip-leaf setting is true, then robots are allowed |
|
4d198d0…
|
drh
|
519 |
** to download archives for any leaf check-in. This allows URL like |
|
4d198d0…
|
drh
|
520 |
** /tarball/trunk/archive.tar.gz to work since branch labels like "trunk" |
|
4d198d0…
|
drh
|
521 |
** always resolve to a leaf. |
|
4d198d0…
|
drh
|
522 |
** |
|
4d198d0…
|
drh
|
523 |
** (3) If the robot-zip-tag setting is a comma-separated tags, then any |
|
4d198d0…
|
drh
|
524 |
** check-in that contains one of the tags on that list is allowed to |
|
4d198d0…
|
drh
|
525 |
** be downloaded. This allows check-ins with tags like "release" or |
|
4d198d0…
|
drh
|
526 |
** "robot-access" to be downloaded by robots. |
|
4d198d0…
|
drh
|
527 |
*/ |
|
4d198d0…
|
drh
|
528 |
int robot_restrict_zip(int rid){ |
|
4d198d0…
|
drh
|
529 |
const char *zTag; |
|
4d198d0…
|
drh
|
530 |
if( !robot_restrict_has_tag("zipX") || !client_might_be_a_robot() ){ |
|
4d198d0…
|
drh
|
531 |
return 0; /* Rule (1) */ |
|
4d198d0…
|
drh
|
532 |
} |
|
4d198d0…
|
drh
|
533 |
|
|
4d198d0…
|
drh
|
534 |
if( db_get_boolean("robot-zip-leaf",0) && is_a_leaf(rid) ){ |
|
4d198d0…
|
drh
|
535 |
return 0; /* Rule (2) */ |
|
4d198d0…
|
drh
|
536 |
} |
|
4d198d0…
|
drh
|
537 |
|
|
4d198d0…
|
drh
|
538 |
zTag = db_get("robot-zip-tag",0); |
|
4d198d0…
|
drh
|
539 |
if( zTag && zTag[0] && fossil_strcmp(zTag,"off")!=0 ){ |
|
4d198d0…
|
drh
|
540 |
int ok = 0; |
|
4d198d0…
|
drh
|
541 |
Stmt q; |
|
4d198d0…
|
drh
|
542 |
db_prepare(&q, |
|
4d198d0…
|
drh
|
543 |
"SELECT substr(tagname,5) FROM tagxref, tag" |
|
4d198d0…
|
drh
|
544 |
" WHERE tagxref.rid=%d" |
|
4d198d0…
|
drh
|
545 |
" AND tag.tagid=tagxref.tagid" |
|
4d198d0…
|
drh
|
546 |
" AND tagxref.tagtype=1" |
|
4d198d0…
|
drh
|
547 |
" AND tag.tagname GLOB 'sym-*'", |
|
4d198d0…
|
drh
|
548 |
rid |
|
4d198d0…
|
drh
|
549 |
); |
|
4d198d0…
|
drh
|
550 |
while( !ok && db_step(&q)==SQLITE_ROW ){ |
|
4d198d0…
|
drh
|
551 |
if( glob_multi_match(zTag, db_column_text(&q,0)) ) ok = 1; |
|
4d198d0…
|
drh
|
552 |
} |
|
4d198d0…
|
drh
|
553 |
db_finalize(&q); |
|
4d198d0…
|
drh
|
554 |
if( ok ) return 0; /* Rule (3) */ |
|
12d871a…
|
stephan
|
555 |
} |
|
8784c60…
|
drh
|
556 |
|
|
12d871a…
|
stephan
|
557 |
/* Generate the proof-of-work captcha */ |
|
4fa618f…
|
drh
|
558 |
ask_for_proof_that_client_is_not_robot(); |
|
4fa618f…
|
drh
|
559 |
return 1; |
|
4fa618f…
|
drh
|
560 |
} |
|
16b3309…
|
drh
|
561 |
|
|
16b3309…
|
drh
|
562 |
/* |
|
16b3309…
|
drh
|
563 |
** WEBPAGE: test-robotck |
|
16b3309…
|
drh
|
564 |
** |
|
16b3309…
|
drh
|
565 |
** Run the robot_restrict() function using the value of the "name=" |
|
16b3309…
|
drh
|
566 |
** query parameter as an argument. Used for testing the robot_restrict() |
|
16b3309…
|
drh
|
567 |
** logic. |
|
16b3309…
|
drh
|
568 |
** |
|
16b3309…
|
drh
|
569 |
** Whenever this page is successfully rendered (when it doesn't go to |
|
16b3309…
|
drh
|
570 |
** the captcha) it deletes the proof-of-work cookie. So reloading the |
|
16b3309…
|
drh
|
571 |
** page will reset the cookie and restart the verification. |
|
4d198d0…
|
drh
|
572 |
** |
|
4d198d0…
|
drh
|
573 |
** If the zip=CHECKIN query parameter is provided, then also invoke |
|
4d198d0…
|
drh
|
574 |
** robot_restrict_archive() on the RID of CHECKIN. |
|
16b3309…
|
drh
|
575 |
*/ |
|
16b3309…
|
drh
|
576 |
void robot_restrict_test_page(void){ |
|
16b3309…
|
drh
|
577 |
const char *zName = P("name"); |
|
4d198d0…
|
drh
|
578 |
const char *zZip = P("zip"); |
|
16b3309…
|
drh
|
579 |
const char *zP1 = P("proof"); |
|
dc2232c…
|
drh
|
580 |
const char *zP2 = P(ROBOT_COOKIE); |
|
16b3309…
|
drh
|
581 |
const char *z; |
|
4d198d0…
|
drh
|
582 |
int rid = 0; |
|
16b3309…
|
drh
|
583 |
if( zName==0 || zName[0]==0 ) zName = g.zPath; |
|
16b3309…
|
drh
|
584 |
login_check_credentials(); |
|
508d3cd…
|
drh
|
585 |
if( g.zLogin==0 ){ login_needed(1); return; } |
|
16b3309…
|
drh
|
586 |
g.zLogin = 0; |
|
16b3309…
|
drh
|
587 |
if( robot_restrict(zName) ) return; |
|
4d198d0…
|
drh
|
588 |
if( zZip && zZip[0] ){ |
|
4d198d0…
|
drh
|
589 |
rid = symbolic_name_to_rid(zZip, "ci"); |
|
4d198d0…
|
drh
|
590 |
if( rid && robot_restrict_zip(rid) ) return; |
|
4d198d0…
|
drh
|
591 |
} |
|
16b3309…
|
drh
|
592 |
style_set_current_feature("test"); |
|
16b3309…
|
drh
|
593 |
style_header("robot_restrict() test"); |
|
16b3309…
|
drh
|
594 |
@ <h1>Captcha passed</h1> |
|
16b3309…
|
drh
|
595 |
@ |
|
16b3309…
|
drh
|
596 |
@ <p> |
|
16b3309…
|
drh
|
597 |
if( zP1 && zP1[0] ){ |
|
16b3309…
|
drh
|
598 |
@ proof=%h(zP1)<br> |
|
16b3309…
|
drh
|
599 |
} |
|
16b3309…
|
drh
|
600 |
if( zP2 && zP2[0] ){ |
|
dc2232c…
|
drh
|
601 |
@ %h(ROBOT_COOKIE)=%h(zP2)<br> |
|
dc2232c…
|
drh
|
602 |
cgi_set_cookie(ROBOT_COOKIE,"",0,-1); |
|
4d198d0…
|
drh
|
603 |
} |
|
4d198d0…
|
drh
|
604 |
if( zZip && zZip[0] ){ |
|
4d198d0…
|
drh
|
605 |
@ zip=%h(zZip)<br> |
|
4d198d0…
|
drh
|
606 |
@ rid=%d(rid)<br> |
|
4fa618f…
|
drh
|
607 |
} |
|
4fa618f…
|
drh
|
608 |
if( g.perm.Admin ){ |
|
4fa618f…
|
drh
|
609 |
z = db_get("robot-restrict",robot_restrict_default()); |
|
4fa618f…
|
drh
|
610 |
if( z && z[0] ){ |
|
4fa618f…
|
drh
|
611 |
@ robot-restrict=%h(z)</br> |
|
4fa618f…
|
drh
|
612 |
} |
|
4fa618f…
|
drh
|
613 |
@ robot.h1=%u(robot.h1)<br> |
|
4fa618f…
|
drh
|
614 |
@ robot.h2=%u(robot.h2)<br> |
|
4fa618f…
|
drh
|
615 |
switch( robot.resultCache ){ |
|
4fa618f…
|
drh
|
616 |
case MIGHT_BE_ROBOT: { |
|
4fa618f…
|
drh
|
617 |
@ robot.resultCache=MIGHT_BE_ROBOT<br> |
|
4fa618f…
|
drh
|
618 |
break; |
|
4fa618f…
|
drh
|
619 |
} |
|
4fa618f…
|
drh
|
620 |
case KNOWN_NOT_ROBOT: { |
|
4fa618f…
|
drh
|
621 |
@ robot.resultCache=KNOWN_NOT_ROBOT<br> |
|
4fa618f…
|
drh
|
622 |
break; |
|
4fa618f…
|
drh
|
623 |
} |
|
4fa618f…
|
drh
|
624 |
default: { |
|
4fa618f…
|
drh
|
625 |
@ robot.resultCache=OTHER (%d(robot.resultCache))<br> |
|
4fa618f…
|
drh
|
626 |
break; |
|
4fa618f…
|
drh
|
627 |
} |
|
4fa618f…
|
drh
|
628 |
} |
|
16b3309…
|
drh
|
629 |
} |
|
16b3309…
|
drh
|
630 |
@ </p> |
|
16b3309…
|
drh
|
631 |
@ <p><a href="%R/test-robotck/%h(zName)">Retry</a> |
|
2a3d303…
|
drh
|
632 |
style_finish_page(); |
|
2a3d303…
|
drh
|
633 |
} |
|
2a3d303…
|
drh
|
634 |
|
|
2a3d303…
|
drh
|
635 |
/* |
|
2a3d303…
|
drh
|
636 |
** WEBPAGE: tokens |
|
2a3d303…
|
drh
|
637 |
** |
|
2a3d303…
|
drh
|
638 |
** Allow users to create, delete, and view their access token. |
|
2a3d303…
|
drh
|
639 |
** |
|
2a3d303…
|
drh
|
640 |
** The access token is a string TOKEN which if included in a query |
|
2a3d303…
|
drh
|
641 |
** parameter like "token=TOKEN" authenticates a request as coming |
|
2a3d303…
|
drh
|
642 |
** from an authorized agent. This can be used, for example, by |
|
2a3d303…
|
drh
|
643 |
** script to access content without running into problems with |
|
2a3d303…
|
drh
|
644 |
** robot defenses. |
|
2a3d303…
|
drh
|
645 |
*/ |
|
2a3d303…
|
drh
|
646 |
void tokens_page(void){ |
|
2a3d303…
|
drh
|
647 |
char *zMyToken; |
|
2a3d303…
|
drh
|
648 |
|
|
2a3d303…
|
drh
|
649 |
login_check_credentials(); |
|
2a3d303…
|
drh
|
650 |
style_set_current_feature("tokens"); |
|
2a3d303…
|
drh
|
651 |
style_header("Access Tokens"); |
|
2a3d303…
|
drh
|
652 |
if( g.zLogin==0 || fossil_strcmp(g.zLogin,"anonymous")==0 ){ |
|
2a3d303…
|
drh
|
653 |
@ User "%h(g.zLogin?g.zLogin:"anonymous")" is not allowed to |
|
2a3d303…
|
drh
|
654 |
@ own or use access tokens. |
|
2a3d303…
|
drh
|
655 |
style_finish_page(); |
|
2a3d303…
|
drh
|
656 |
return; |
|
2a3d303…
|
drh
|
657 |
} |
|
2a3d303…
|
drh
|
658 |
if( g.perm.Admin && P("del")!=0 ){ |
|
2a3d303…
|
drh
|
659 |
const char *zDel = P("del"); |
|
2a3d303…
|
drh
|
660 |
db_unprotect(PROTECT_CONFIG); |
|
2a3d303…
|
drh
|
661 |
db_multi_exec( |
|
2a3d303…
|
drh
|
662 |
"DELETE FROM config WHERE name='token-%q'", |
|
2a3d303…
|
drh
|
663 |
zDel); |
|
2a3d303…
|
drh
|
664 |
db_protect_pop(); |
|
2a3d303…
|
drh
|
665 |
} |
|
2a3d303…
|
drh
|
666 |
zMyToken = db_text(0, |
|
2a3d303…
|
drh
|
667 |
"SELECT substr(name,7) FROM config" |
|
2a3d303…
|
drh
|
668 |
" WHERE name GLOB 'token-*'" |
|
2a3d303…
|
drh
|
669 |
" AND json_valid(value,6)" |
|
2a3d303…
|
drh
|
670 |
" AND value->>'user' = %Q", |
|
2a3d303…
|
drh
|
671 |
g.zLogin |
|
2a3d303…
|
drh
|
672 |
); |
|
2a3d303…
|
drh
|
673 |
if( zMyToken==0 && P("new") ){ |
|
2a3d303…
|
drh
|
674 |
sqlite3_uint64 r; |
|
2a3d303…
|
drh
|
675 |
sqlite3_randomness(sizeof(r),&r); |
|
2a3d303…
|
drh
|
676 |
zMyToken = mprintf("%016llx", r); |
|
2a3d303…
|
drh
|
677 |
db_unprotect(PROTECT_CONFIG); |
|
2a3d303…
|
drh
|
678 |
db_multi_exec( |
|
2a3d303…
|
drh
|
679 |
"INSERT INTO config(name,value,mtime)" |
|
2a3d303…
|
drh
|
680 |
"VALUES('token-%q','{user:%!j}',now())", |
|
2a3d303…
|
drh
|
681 |
zMyToken, g.zLogin |
|
2a3d303…
|
drh
|
682 |
); |
|
2a3d303…
|
drh
|
683 |
db_protect_pop(); |
|
2a3d303…
|
drh
|
684 |
}else if( zMyToken!=0 && P("selfdel") |
|
2a3d303…
|
drh
|
685 |
&& fossil_strcmp(zMyToken,P("selfdel"))==0 ){ |
|
2a3d303…
|
drh
|
686 |
db_unprotect(PROTECT_CONFIG); |
|
2a3d303…
|
drh
|
687 |
db_multi_exec( |
|
2a3d303…
|
drh
|
688 |
"DELETE FROM config WHERE name='token-%q'", |
|
2a3d303…
|
drh
|
689 |
zMyToken); |
|
2a3d303…
|
drh
|
690 |
db_protect_pop(); |
|
2a3d303…
|
drh
|
691 |
zMyToken = 0; |
|
2a3d303…
|
drh
|
692 |
} |
|
2a3d303…
|
drh
|
693 |
if( zMyToken==0 ){ |
|
2a3d303…
|
drh
|
694 |
@ <p>You do not currently have an access token. |
|
2a3d303…
|
drh
|
695 |
@ <a href="%R/tokens?new=true">Create one</a> |
|
2a3d303…
|
drh
|
696 |
}else{ |
|
2a3d303…
|
drh
|
697 |
@ <p>Your access token is "%h(zMyToken)". |
|
2a3d303…
|
drh
|
698 |
@ <p>Use this token as the value of the token= query parameter |
|
2a3d303…
|
drh
|
699 |
@ to bypass robot defenses on unauthenticated queries to this |
|
2a3d303…
|
drh
|
700 |
@ server (%R). Do not misuse your token. Keep it confidential. |
|
2a3d303…
|
drh
|
701 |
@ If you misuse your token, or if somebody else steals your token |
|
2a3d303…
|
drh
|
702 |
@ and misuses, that can result in loss of access privileges to this |
|
2a3d303…
|
drh
|
703 |
@ server. |
|
2a3d303…
|
drh
|
704 |
@ <p><a href="%R/tokens?selfdel=%h(zMyToken)">Delete my token</a> |
|
2a3d303…
|
drh
|
705 |
} |
|
2a3d303…
|
drh
|
706 |
if( g.perm.Admin ){ |
|
2a3d303…
|
drh
|
707 |
int nTok = 0; |
|
2a3d303…
|
drh
|
708 |
Stmt s; |
|
2a3d303…
|
drh
|
709 |
db_prepare(&s, |
|
2a3d303…
|
drh
|
710 |
"SELECT substr(name,7), value->>'user', datetime(mtime,'unixepoch')" |
|
2a3d303…
|
drh
|
711 |
" FROM config" |
|
2a3d303…
|
drh
|
712 |
" WHERE name GLOB 'token-*'" |
|
2a3d303…
|
drh
|
713 |
" AND json_valid(value,6)" |
|
2a3d303…
|
drh
|
714 |
); |
|
2a3d303…
|
drh
|
715 |
while( db_step(&s)==SQLITE_ROW ){ |
|
2a3d303…
|
drh
|
716 |
if( nTok==0 ){ |
|
2a3d303…
|
drh
|
717 |
@ <hr> |
|
2a3d303…
|
drh
|
718 |
@ <p>All tokens</p> |
|
2a3d303…
|
drh
|
719 |
@ <table border="1" cellpadding="5" cellspacing="0"> |
|
2a3d303…
|
drh
|
720 |
@ <tr><th>User <th>Token <th>Date <th> </tr> |
|
2a3d303…
|
drh
|
721 |
} |
|
2a3d303…
|
drh
|
722 |
nTok++; |
|
2a3d303…
|
drh
|
723 |
@ <tr><td>%h(db_column_text(&s,1)) |
|
2a3d303…
|
drh
|
724 |
@ <td>%h(db_column_text(&s,0)) |
|
2a3d303…
|
drh
|
725 |
@ <td>%h(db_column_text(&s,2)) |
|
2a3d303…
|
drh
|
726 |
@ <td><a href="%R/tokens?del=%h(db_column_text(&s,0))">delete</a> |
|
2a3d303…
|
drh
|
727 |
@ </tr> |
|
2a3d303…
|
drh
|
728 |
} |
|
2a3d303…
|
drh
|
729 |
db_finalize(&s); |
|
2a3d303…
|
drh
|
730 |
if( nTok==0 ){ |
|
2a3d303…
|
drh
|
731 |
@ <hr> |
|
2a3d303…
|
drh
|
732 |
@ <p>There are access tokens defined for this repository. |
|
2a3d303…
|
drh
|
733 |
}else{ |
|
2a3d303…
|
drh
|
734 |
@ </table> |
|
2a3d303…
|
drh
|
735 |
} |
|
2a3d303…
|
drh
|
736 |
} |
|
16b3309…
|
drh
|
737 |
style_finish_page(); |
|
de66eea…
|
drh
|
738 |
} |