]>
Commit | Line | Data |
---|---|---|
41462d93 | 1 | /* |
41462d93 | 2 | * DEBUG: section 17 Request Forwarding |
3 | * AUTHOR: Duane Wessels | |
4 | * | |
2b6662ba | 5 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
e25c139f | 6 | * ---------------------------------------------------------- |
41462d93 | 7 | * |
2b6662ba | 8 | * Squid is the result of efforts by numerous individuals from |
9 | * the Internet community; see the CONTRIBUTORS file for full | |
10 | * details. Many organizations have provided support for Squid's | |
11 | * development; see the SPONSORS file for full details. Squid is | |
12 | * Copyrighted (C) 2001 by the Regents of the University of | |
13 | * California; see the COPYRIGHT file for full details. Squid | |
14 | * incorporates software developed and/or copyrighted by other | |
15 | * sources; see the CREDITS file for full details. | |
41462d93 | 16 | * |
17 | * This program is free software; you can redistribute it and/or modify | |
18 | * it under the terms of the GNU General Public License as published by | |
19 | * the Free Software Foundation; either version 2 of the License, or | |
20 | * (at your option) any later version. | |
26ac0430 | 21 | * |
41462d93 | 22 | * This program is distributed in the hope that it will be useful, |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
25 | * GNU General Public License for more details. | |
26ac0430 | 26 | * |
41462d93 | 27 | * You should have received a copy of the GNU General Public License |
28 | * along with this program; if not, write to the Free Software | |
cbdec147 | 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
e25c139f | 30 | * |
41462d93 | 31 | */ |
32 | ||
582c2af2 | 33 | #include "squid.h" |
4bf68cfa | 34 | #include "AccessLogEntry.h" |
0dc8ffa5 | 35 | #include "acl/AclAddress.h" |
c0941a6a AR |
36 | #include "acl/FilledChecklist.h" |
37 | #include "acl/Gadgets.h" | |
5ae65581 | 38 | #include "anyp/PortCfg.h" |
a011edee | 39 | #include "CachePeer.h" |
25b481e6 | 40 | #include "CacheManager.h" |
582c2af2 | 41 | #include "client_side.h" |
f9b72e0c | 42 | #include "comm/Connection.h" |
aed188fd | 43 | #include "comm/ConnOpener.h" |
d841c88d | 44 | #include "comm/Loops.h" |
582c2af2 | 45 | #include "CommCalls.h" |
aa839030 | 46 | #include "errorpage.h" |
582c2af2 | 47 | #include "event.h" |
fc54b8d2 | 48 | #include "fd.h" |
aa839030 | 49 | #include "fde.h" |
fc54b8d2 | 50 | #include "ftp.h" |
eb13c21e | 51 | #include "FwdState.h" |
582c2af2 | 52 | #include "globals.h" |
fc54b8d2 | 53 | #include "gopher.h" |
bbaf2685 | 54 | #include "hier_code.h" |
fc54b8d2 | 55 | #include "http.h" |
924f73bc | 56 | #include "HttpReply.h" |
aa839030 | 57 | #include "HttpRequest.h" |
582c2af2 | 58 | #include "icmp/net_db.h" |
fc54b8d2 | 59 | #include "internal.h" |
582c2af2 | 60 | #include "ip/Intercept.h" |
425de4c8 | 61 | #include "ip/QosConfig.h" |
582c2af2 | 62 | #include "ip/tools.h" |
aa839030 | 63 | #include "MemObject.h" |
582c2af2 | 64 | #include "mgr/Registration.h" |
fc54b8d2 | 65 | #include "neighbors.h" |
781ce8ff | 66 | #include "pconn.h" |
cfd66529 | 67 | #include "PeerSelectState.h" |
4d5904f7 | 68 | #include "SquidConfig.h" |
aa839030 | 69 | #include "SquidTime.h" |
70 | #include "Store.h" | |
e87137f1 | 71 | #include "StoreClient.h" |
5eb529cb | 72 | #include "urn.h" |
fc54b8d2 | 73 | #include "whois.h" |
4db984be | 74 | #if USE_SSL |
2cef0ca6 AR |
75 | #include "ssl/cert_validate_message.h" |
76 | #include "ssl/Config.h" | |
77 | #include "ssl/helper.h" | |
4db984be | 78 | #include "ssl/support.h" |
4d16918e | 79 | #include "ssl/ErrorDetail.h" |
fd4624d7 | 80 | #include "ssl/ServerBump.h" |
4db984be | 81 | #endif |
21d845b1 FC |
82 | #if HAVE_ERRNO_H |
83 | #include <errno.h> | |
84 | #endif | |
cfd66529 | 85 | |
6b679a01 | 86 | static PSC fwdPeerSelectionCompleteWrapper; |
575d05c4 | 87 | static CLCB fwdServerClosedWrapper; |
b6b6f466 | 88 | #if USE_SSL |
89 | static PF fwdNegotiateSSLWrapper; | |
90 | #endif | |
b6b6f466 | 91 | static CNCB fwdConnectDoneWrapper; |
92 | ||
8ddcc35d | 93 | static OBJH fwdStats; |
94 | ||
95 | #define MAX_FWD_STATS_IDX 9 | |
955394ce | 96 | static int FwdReplyCodes[MAX_FWD_STATS_IDX + 1][Http::scInvalidHeader + 1]; |
db1cd23c | 97 | |
781ce8ff | 98 | static PconnPool *fwdPconnPool = new PconnPool("server-side"); |
b6b6f466 | 99 | CBDATA_CLASS_INIT(FwdState); |
781ce8ff | 100 | |
429871db | 101 | void |
102 | FwdState::abort(void* d) | |
103 | { | |
104 | FwdState* fwd = (FwdState*)d; | |
6ecaf21a | 105 | Pointer tmp = fwd; // Grab a temporary pointer to keep the object alive during our scope. |
429871db | 106 | |
6b679a01 | 107 | if (Comm::IsConnOpen(fwd->serverConnection())) { |
5229395c | 108 | comm_remove_close_handler(fwd->serverConnection()->fd, fwdServerClosedWrapper, fwd); |
6dd9a2e4 AJ |
109 | debugs(17, 3, HERE << "store entry aborted; closing " << |
110 | fwd->serverConnection()); | |
111 | fwd->serverConnection()->close(); | |
112 | } else { | |
113 | debugs(17, 7, HERE << "store entry aborted; no connection to close"); | |
cff02fa6 | 114 | } |
00ae51e4 | 115 | fwd->serverDestinations.clean(); |
429871db | 116 | fwd->self = NULL; |
117 | } | |
118 | ||
b6b6f466 | 119 | /**** PUBLIC INTERFACE ********************************************************/ |
c7f9eb6d | 120 | |
4bf68cfa | 121 | FwdState::FwdState(const Comm::ConnectionPointer &client, StoreEntry * e, HttpRequest * r, const AccessLogEntryPointer &alp): |
c901ed8c | 122 | al(alp) |
db1cd23c | 123 | { |
3157a816 | 124 | debugs(17, 2, HERE << "Forwarding client request " << client << ", url=" << e->url() ); |
b6b6f466 | 125 | entry = e; |
5c336a3b | 126 | clientConn = client; |
b248c2a3 AJ |
127 | request = r; |
128 | HTTPMSGLOCK(request); | |
bc81ee68 | 129 | pconnRace = raceImpossible; |
b6b6f466 | 130 | start_t = squid_curtime; |
00ae51e4 | 131 | serverDestinations.reserve(Config.forward_max_tries); |
3d0ac046 | 132 | e->lock(); |
b6b6f466 | 133 | EBIT_SET(e->flags, ENTRY_FWD_HDR_WAIT); |
7a0fb323 | 134 | } |
135 | ||
136 | // Called once, right after object creation, when it is safe to set self | |
fc68f6b1 | 137 | void FwdState::start(Pointer aSelf) |
138 | { | |
7a0fb323 | 139 | // Protect ourselves from being destroyed when the only Server pointing |
140 | // to us is gone (while we expect to talk to more Servers later). | |
141 | // Once we set self, we are responsible for clearing it when we do not | |
142 | // expect to talk to any servers. | |
143 | self = aSelf; // refcounted | |
144 | ||
145 | // We hope that either the store entry aborts or peer is selected. | |
146 | // Otherwise we are going to leak our object. | |
34266cde | 147 | |
3900307b | 148 | entry->registerAbort(FwdState::abort, this); |
bfe4e2fe | 149 | |
32c32865 | 150 | #if STRICT_ORIGINAL_DST |
bfe4e2fe AJ |
151 | // Bug 3243: CVE 2009-0801 |
152 | // Bypass of browser same-origin access control in intercepted communication | |
153 | // To resolve this we must force DIRECT and only to the original client destination. | |
0d901ef4 | 154 | const bool isIntercepted = request && !request->flags.redirected && (request->flags.intercepted || request->flags.interceptTproxy); |
2962f8b8 AJ |
155 | const bool useOriginalDst = Config.onoff.client_dst_passthru || (request && !request->flags.hostVerified); |
156 | if (isIntercepted && useOriginalDst) { | |
d7ce0bcd | 157 | selectPeerForIntercepted(); |
7177edfb AJ |
158 | // 3.2 does not suppro re-wrapping inside CONNECT. |
159 | // our only alternative is to fake destination "found" and continue with the forwarding. | |
bfe4e2fe | 160 | startConnectionOrFail(); |
7177edfb | 161 | return; |
bfe4e2fe | 162 | } |
32c32865 AJ |
163 | #endif |
164 | ||
7177edfb AJ |
165 | // do full route options selection |
166 | peerSelect(&serverDestinations, request, entry, fwdPeerSelectionCompleteWrapper, this); | |
db1cd23c | 167 | } |
168 | ||
32c32865 | 169 | #if STRICT_ORIGINAL_DST |
d7ce0bcd AR |
170 | /// bypasses peerSelect() when dealing with intercepted requests |
171 | void | |
172 | FwdState::selectPeerForIntercepted() | |
173 | { | |
174 | // use pinned connection if available | |
175 | Comm::ConnectionPointer p; | |
693cb033 | 176 | if (ConnStateData *client = request->pinnedConnection()) { |
d7ce0bcd | 177 | p = client->validatePinnedConnection(request, NULL); |
693cb033 CT |
178 | if (Comm::IsConnOpen(p)) { |
179 | /* duplicate peerSelectPinned() effects */ | |
180 | p->peerType = PINNED; | |
181 | entry->ping_status = PING_DONE; /* Skip ICP */ | |
d7ce0bcd | 182 | |
693cb033 CT |
183 | debugs(17, 3, "reusing a pinned conn: " << *p); |
184 | serverDestinations.push_back(p); | |
185 | } else { | |
186 | debugs(17,2, "Pinned connection is not valid: " << p); | |
955394ce | 187 | ErrorState *anErr = new ErrorState(ERR_ZERO_SIZE_OBJECT, Http::scServiceUnavailable, request); |
693cb033 CT |
188 | fail(anErr); |
189 | } | |
190 | // Either use the valid pinned connection or fail if it is invalid. | |
191 | return; | |
d7ce0bcd AR |
192 | } |
193 | ||
7177edfb AJ |
194 | // use client original destination as second preferred choice |
195 | p = new Comm::Connection(); | |
196 | p->peerType = ORIGINAL_DST; | |
197 | p->remote = clientConn->local; | |
198 | getOutgoingAddress(request, p); | |
199 | ||
200 | debugs(17, 3, HERE << "using client original destination: " << *p); | |
d7ce0bcd AR |
201 | serverDestinations.push_back(p); |
202 | } | |
32c32865 | 203 | #endif |
d7ce0bcd | 204 | |
802a8c1d | 205 | void |
206 | FwdState::completed() | |
41462d93 | 207 | { |
e857372a | 208 | if (flags.forward_completed) { |
e0236918 | 209 | debugs(17, DBG_IMPORTANT, HERE << "FwdState::completed called on a completed request! Bad!"); |
fc68f6b1 | 210 | return; |
211 | } | |
212 | ||
e857372a | 213 | flags.forward_completed = true; |
802a8c1d | 214 | |
c4a88a3e CT |
215 | if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) { |
216 | debugs(17, 3, HERE << "entry aborted"); | |
217 | return ; | |
218 | } | |
219 | ||
bc87dc25 | 220 | #if URL_CHECKSUM_DEBUG |
62e76326 | 221 | |
b6b6f466 | 222 | entry->mem_obj->checkUrlChecksum(); |
225644d7 | 223 | #endif |
62e76326 | 224 | |
b6b6f466 | 225 | if (entry->store_status == STORE_PENDING) { |
226 | if (entry->isEmpty()) { | |
a04b7e6e | 227 | if (!err) // we quit (e.g., fd closed) before an error or content |
955394ce | 228 | fail(new ErrorState(ERR_READ_ERROR, Http::scBadGateway, request)); |
b6b6f466 | 229 | assert(err); |
230 | errorAppendEntry(entry, err); | |
231 | err = NULL; | |
2cef0ca6 AR |
232 | #if USE_SSL |
233 | if (request->flags.sslPeek && request->clientConnectionManager.valid()) { | |
234 | CallJobHere1(17, 4, request->clientConnectionManager, ConnStateData, | |
235 | ConnStateData::httpsPeeked, Comm::ConnectionPointer(NULL)); | |
236 | } | |
237 | #endif | |
62e76326 | 238 | } else { |
b6b6f466 | 239 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); |
240 | entry->complete(); | |
d88e3c49 | 241 | entry->releaseRequest(); |
62e76326 | 242 | } |
f563eea9 | 243 | } |
62e76326 | 244 | |
b6b6f466 | 245 | if (storePendingNClients(entry) > 0) |
246 | assert(!EBIT_TEST(entry->flags, ENTRY_FWD_HDR_WAIT)); | |
62e76326 | 247 | |
802a8c1d | 248 | } |
249 | ||
250 | FwdState::~FwdState() | |
251 | { | |
252 | debugs(17, 3, HERE << "FwdState destructor starting"); | |
fc68f6b1 | 253 | |
802a8c1d | 254 | if (! flags.forward_completed) |
fc68f6b1 | 255 | completed(); |
802a8c1d | 256 | |
9d2760b6 AR |
257 | doneWithRetries(); |
258 | ||
6dd9f4bd | 259 | HTTPMSGUNLOCK(request); |
62e76326 | 260 | |
913524f0 | 261 | delete err; |
62e76326 | 262 | |
3900307b | 263 | entry->unregisterAbort(); |
429871db | 264 | |
97b5e68f | 265 | entry->unlock(); |
62e76326 | 266 | |
b6b6f466 | 267 | entry = NULL; |
62e76326 | 268 | |
e3a4aecc AJ |
269 | if (calls.connector != NULL) { |
270 | calls.connector->cancel("FwdState destructed"); | |
271 | calls.connector = NULL; | |
272 | } | |
273 | ||
6b679a01 | 274 | if (Comm::IsConnOpen(serverConn)) { |
5229395c AJ |
275 | comm_remove_close_handler(serverConnection()->fd, fwdServerClosedWrapper, this); |
276 | debugs(17, 3, HERE << "closing FD " << serverConnection()->fd); | |
00ae51e4 | 277 | serverConn->close(); |
b6b6f466 | 278 | } |
fc68f6b1 | 279 | |
00ae51e4 | 280 | serverDestinations.clean(); |
cfd66529 | 281 | |
b6b6f466 | 282 | debugs(17, 3, HERE << "FwdState destructor done"); |
283 | } | |
62e76326 | 284 | |
38413773 | 285 | /** |
b6b6f466 | 286 | * This is the entry point for client-side to start forwarding |
287 | * a transaction. It is a static method that may or may not | |
288 | * allocate a FwdState. | |
289 | */ | |
be0c6690 | 290 | void |
4bf68cfa | 291 | FwdState::Start(const Comm::ConnectionPointer &clientConn, StoreEntry *entry, HttpRequest *request, const AccessLogEntryPointer &al) |
b6b6f466 | 292 | { |
b50e327b | 293 | /** \note |
b6b6f466 | 294 | * client_addr == no_addr indicates this is an "internal" request |
295 | * from peer_digest.c, asn.c, netdb.c, etc and should always | |
296 | * be allowed. yuck, I know. | |
297 | */ | |
62e76326 | 298 | |
4dd643d5 | 299 | if ( Config.accessList.miss && !request->client_addr.isNoAddr() && |
39a19cb7 | 300 | request->protocol != AnyP::PROTO_INTERNAL && request->protocol != AnyP::PROTO_CACHE_OBJECT) { |
b50e327b | 301 | /** |
fbd3636b NR |
302 | * Check if this host is allowed to fetch MISSES from us (miss_access). |
303 | * Intentionally replace the src_addr automatically selected by the checklist code | |
304 | * we do NOT want the indirect client address to be tested here. | |
b6b6f466 | 305 | */ |
c0941a6a | 306 | ACLFilledChecklist ch(Config.accessList.miss, request, NULL); |
b6b6f466 | 307 | ch.src_addr = request->client_addr; |
2efeb0b7 | 308 | if (ch.fastCheck() == ACCESS_DENIED) { |
b6b6f466 | 309 | err_type page_id; |
9ce7856a | 310 | page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, 1); |
b6b6f466 | 311 | |
312 | if (page_id == ERR_NONE) | |
313 | page_id = ERR_FORWARDING_DENIED; | |
314 | ||
955394ce | 315 | ErrorState *anErr = new ErrorState(page_id, Http::scForbidden, request); |
b6b6f466 | 316 | errorAppendEntry(entry, anErr); // frees anErr |
be0c6690 | 317 | return; |
b6b6f466 | 318 | } |
319 | } | |
320 | ||
cfd66529 | 321 | debugs(17, 3, HERE << "'" << entry->url() << "'"); |
f4ef658f | 322 | /* |
323 | * This seems like an odd place to bind mem_obj and request. | |
324 | * Might want to assert that request is NULL at this point | |
325 | */ | |
b248c2a3 AJ |
326 | entry->mem_obj->request = request; |
327 | HTTPMSGLOCK(entry->mem_obj->request); | |
b6b6f466 | 328 | #if URL_CHECKSUM_DEBUG |
329 | ||
330 | entry->mem_obj->checkUrlChecksum(); | |
331 | #endif | |
332 | ||
333 | if (shutting_down) { | |
334 | /* more yuck */ | |
955394ce | 335 | ErrorState *anErr = new ErrorState(ERR_SHUTTING_DOWN, Http::scServiceUnavailable, request); |
b6b6f466 | 336 | errorAppendEntry(entry, anErr); // frees anErr |
be0c6690 | 337 | return; |
6801f8a8 | 338 | } |
62e76326 | 339 | |
b6b6f466 | 340 | switch (request->protocol) { |
341 | ||
0c3d3f65 | 342 | case AnyP::PROTO_INTERNAL: |
e37bd29b | 343 | internalStart(clientConn, request, entry); |
be0c6690 | 344 | return; |
b6b6f466 | 345 | |
39a19cb7 | 346 | case AnyP::PROTO_CACHE_OBJECT: |
5c336a3b | 347 | CacheManager::GetInstance()->Start(clientConn, request, entry); |
be0c6690 | 348 | return; |
b6b6f466 | 349 | |
0c3d3f65 | 350 | case AnyP::PROTO_URN: |
b6b6f466 | 351 | urnStart(request, entry); |
be0c6690 | 352 | return; |
b6b6f466 | 353 | |
354 | default: | |
4bf68cfa | 355 | FwdState::Pointer fwd = new FwdState(clientConn, entry, request, al); |
7a0fb323 | 356 | fwd->start(fwd); |
be0c6690 | 357 | return; |
b6b6f466 | 358 | } |
359 | ||
360 | /* NOTREACHED */ | |
41462d93 | 361 | } |
362 | ||
4bf68cfa AR |
363 | void |
364 | FwdState::fwdStart(const Comm::ConnectionPointer &clientConn, StoreEntry *entry, HttpRequest *request) | |
365 | { | |
366 | // Hides AccessLogEntry.h from code that does not supply ALE anyway. | |
367 | Start(clientConn, entry, request, NULL); | |
368 | } | |
369 | ||
cfd66529 | 370 | void |
6b679a01 | 371 | FwdState::startConnectionOrFail() |
cfd66529 | 372 | { |
8652f8e7 | 373 | debugs(17, 3, HERE << entry->url()); |
cfd66529 | 374 | |
00ae51e4 | 375 | if (serverDestinations.size() > 0) { |
8652f8e7 AJ |
376 | // Ditch error page if it was created before. |
377 | // A new one will be created if there's another problem | |
913524f0 AJ |
378 | delete err; |
379 | err = NULL; | |
8652f8e7 AJ |
380 | |
381 | // Update the logging information about this new server connection. | |
382 | // Done here before anything else so the errors get logged for | |
383 | // this server link regardless of what happens when connecting to it. | |
384 | // IF sucessfuly connected this top destination will become the serverConnection(). | |
385 | request->hier.note(serverDestinations[0], request->GetHost()); | |
129fe2a1 | 386 | request->clearError(); |
8652f8e7 | 387 | |
cfd66529 AJ |
388 | connectStart(); |
389 | } else { | |
95b83010 CT |
390 | debugs(17, 3, HERE << "Connection failed: " << entry->url()); |
391 | if (!err) { | |
955394ce | 392 | ErrorState *anErr = new ErrorState(ERR_CANNOT_FORWARD, Http::scInternalServerError, request); |
95b83010 | 393 | fail(anErr); |
8652f8e7 | 394 | } // else use actual error from last connection attempt |
cfd66529 AJ |
395 | self = NULL; // refcounted |
396 | } | |
397 | } | |
398 | ||
b6b6f466 | 399 | void |
400 | FwdState::fail(ErrorState * errorState) | |
401 | { | |
9b769c67 | 402 | debugs(17, 3, err_type_str[errorState->type] << " \"" << Http::StatusCodeString(errorState->httpStatus) << "\"\n\t" << entry->url()); |
b6b6f466 | 403 | |
913524f0 | 404 | delete err; |
b6b6f466 | 405 | err = errorState; |
406 | ||
b248c2a3 AJ |
407 | if (!errorState->request) { |
408 | errorState->request = request; | |
409 | HTTPMSGLOCK(errorState->request); | |
410 | } | |
64b66b76 | 411 | |
693cb033 CT |
412 | if (err->type != ERR_ZERO_SIZE_OBJECT) |
413 | return; | |
414 | ||
415 | if (pconnRace == racePossible) { | |
bc81ee68 AR |
416 | debugs(17, 5, HERE << "pconn race happened"); |
417 | pconnRace = raceHappened; | |
418 | } | |
693cb033 CT |
419 | |
420 | if (ConnStateData *pinned_connection = request->pinnedConnection()) { | |
421 | pinned_connection->pinning.zeroReply = true; | |
422 | flags.dont_retry = true; // we want to propagate failure to the client | |
423 | debugs(17, 4, "zero reply on pinned connection"); | |
424 | } | |
b6b6f466 | 425 | } |
426 | ||
38413773 | 427 | /** |
b6b6f466 | 428 | * Frees fwdState without closing FD or generating an abort |
429 | */ | |
430 | void | |
00ae51e4 | 431 | FwdState::unregister(Comm::ConnectionPointer &conn) |
5229395c AJ |
432 | { |
433 | debugs(17, 3, HERE << entry->url() ); | |
434 | assert(serverConnection() == conn); | |
6b679a01 | 435 | assert(Comm::IsConnOpen(conn)); |
5229395c | 436 | comm_remove_close_handler(conn->fd, fwdServerClosedWrapper, this); |
00ae51e4 | 437 | serverConn = NULL; |
5229395c AJ |
438 | } |
439 | ||
440 | // Legacy method to be removed in favor of the above as soon as possible | |
441 | void | |
b6b6f466 | 442 | FwdState::unregister(int fd) |
443 | { | |
5229395c AJ |
444 | debugs(17, 3, HERE << entry->url() ); |
445 | assert(fd == serverConnection()->fd); | |
00ae51e4 | 446 | unregister(serverConn); |
b6b6f466 | 447 | } |
448 | ||
38413773 | 449 | /** |
b6b6f466 | 450 | * server-side modules call fwdComplete() when they are done |
451 | * downloading an object. Then, we either 1) re-forward the | |
452 | * request somewhere else if needed, or 2) call storeComplete() | |
453 | * to finish it off | |
454 | */ | |
455 | void | |
456 | FwdState::complete() | |
457 | { | |
9b769c67 | 458 | debugs(17, 3, HERE << entry->url() << "\n\tstatus " << entry->getReply()->sline.status()); |
b6b6f466 | 459 | #if URL_CHECKSUM_DEBUG |
460 | ||
461 | entry->mem_obj->checkUrlChecksum(); | |
462 | #endif | |
463 | ||
9b769c67 | 464 | logReplyStatus(n_tries, entry->getReply()->sline.status()); |
b6b6f466 | 465 | |
466 | if (reforward()) { | |
9b769c67 | 467 | debugs(17, 3, HERE << "re-forwarding " << entry->getReply()->sline.status() << " " << entry->url()); |
b6b6f466 | 468 | |
6b679a01 | 469 | if (Comm::IsConnOpen(serverConn)) |
00ae51e4 | 470 | unregister(serverConn); |
b6b6f466 | 471 | |
cfd66529 AJ |
472 | entry->reset(); |
473 | ||
8652f8e7 AJ |
474 | // drop the last path off the selection list. try the next one. |
475 | serverDestinations.shift(); | |
476 | startConnectionOrFail(); | |
477 | ||
b6b6f466 | 478 | } else { |
6b679a01 | 479 | if (Comm::IsConnOpen(serverConn)) |
9b769c67 | 480 | debugs(17, 3, HERE << "server FD " << serverConnection()->fd << " not re-forwarding status " << entry->getReply()->sline.status()); |
6b679a01 | 481 | else |
9b769c67 | 482 | debugs(17, 3, HERE << "server (FD closed) not re-forwarding status " << entry->getReply()->sline.status()); |
9e5c22cf AJ |
483 | EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT); |
484 | entry->complete(); | |
fc68f6b1 | 485 | |
6b679a01 | 486 | if (!Comm::IsConnOpen(serverConn)) |
fc68f6b1 | 487 | completed(); |
488 | ||
7a0fb323 | 489 | self = NULL; // refcounted |
b6b6f466 | 490 | } |
491 | } | |
492 | ||
b6b6f466 | 493 | /**** CALLBACK WRAPPERS ************************************************************/ |
494 | ||
495 | static void | |
a37fdd8a | 496 | fwdPeerSelectionCompleteWrapper(Comm::ConnectionList * unused, ErrorState *err, void *data) |
b6b6f466 | 497 | { |
498 | FwdState *fwd = (FwdState *) data; | |
a37fdd8a AJ |
499 | if (err) |
500 | fwd->fail(err); | |
6b679a01 | 501 | fwd->startConnectionOrFail(); |
b6b6f466 | 502 | } |
503 | ||
504 | static void | |
575d05c4 | 505 | fwdServerClosedWrapper(const CommCloseCbParams ¶ms) |
b6b6f466 | 506 | { |
575d05c4 AJ |
507 | FwdState *fwd = (FwdState *)params.data; |
508 | fwd->serverClosed(params.fd); | |
b6b6f466 | 509 | } |
510 | ||
b6b6f466 | 511 | #if USE_SSL |
512 | static void | |
513 | fwdNegotiateSSLWrapper(int fd, void *data) | |
514 | { | |
515 | FwdState *fwd = (FwdState *) data; | |
516 | fwd->negotiateSSL(fd); | |
517 | } | |
518 | ||
519 | #endif | |
520 | ||
cfd66529 | 521 | void |
f01d4b80 | 522 | fwdConnectDoneWrapper(const Comm::ConnectionPointer &conn, comm_err_t status, int xerrno, void *data) |
b6b6f466 | 523 | { |
524 | FwdState *fwd = (FwdState *) data; | |
aed188fd | 525 | fwd->connectDone(conn, status, xerrno); |
b6b6f466 | 526 | } |
527 | ||
528 | /**** PRIVATE *****************************************************************/ | |
529 | ||
545782b8 | 530 | /* |
531 | * FwdState::checkRetry | |
26ac0430 | 532 | * |
545782b8 | 533 | * Return TRUE if the request SHOULD be retried. This method is |
534 | * called when the HTTP connection fails, or when the connection | |
535 | * is closed before server-side read the end of HTTP headers. | |
536 | */ | |
b6b6f466 | 537 | bool |
538 | FwdState::checkRetry() | |
68bd6892 | 539 | { |
d8fd0f18 | 540 | if (shutting_down) |
b6b6f466 | 541 | return false; |
62e76326 | 542 | |
9d2760b6 AR |
543 | if (!self) { // we have aborted before the server called us back |
544 | debugs(17, 5, HERE << "not retrying because of earlier abort"); | |
545 | // we will be destroyed when the server clears its Pointer to us | |
546 | return false; | |
547 | } | |
548 | ||
b6b6f466 | 549 | if (entry->store_status != STORE_PENDING) |
550 | return false; | |
62e76326 | 551 | |
b6b6f466 | 552 | if (!entry->isEmpty()) |
553 | return false; | |
62e76326 | 554 | |
23a1e39c | 555 | if (n_tries > Config.forward_max_tries) |
b6b6f466 | 556 | return false; |
62e76326 | 557 | |
b6b6f466 | 558 | if (squid_curtime - start_t > Config.Timeout.forward) |
559 | return false; | |
62e76326 | 560 | |
b6b6f466 | 561 | if (flags.dont_retry) |
562 | return false; | |
62e76326 | 563 | |
3b9899f7 AR |
564 | if (request->bodyNibbled()) |
565 | return false; | |
566 | ||
0bbd5532 AJ |
567 | // NP: not yet actually connected anywhere. retry is safe. |
568 | if (!flags.connected_okay) | |
569 | return true; | |
570 | ||
5d4989a8 | 571 | if (!checkRetriable()) |
572 | return false; | |
573 | ||
b6b6f466 | 574 | return true; |
68bd6892 | 575 | } |
576 | ||
545782b8 | 577 | /* |
578 | * FwdState::checkRetriable | |
579 | * | |
580 | * Return TRUE if this is the kind of request that can be retried | |
581 | * after a failure. If the request is not retriable then we don't | |
582 | * want to risk sending it on a persistent connection. Instead we'll | |
583 | * force it to go on a new HTTP connection. | |
584 | */ | |
b6b6f466 | 585 | bool |
586 | FwdState::checkRetriable() | |
cb928909 | 587 | { |
ccbcff0e AR |
588 | // Optimize: A compliant proxy may retry PUTs, but Squid lacks the [rather |
589 | // complicated] code required to protect the PUT request body from being | |
590 | // nibbled during the first try. Thus, Squid cannot retry some PUTs today. | |
591 | if (request->body_pipe != NULL) | |
592 | return false; | |
593 | ||
c2a7cefd AJ |
594 | // RFC2616 9.1 Safe and Idempotent Methods |
595 | return (request->method.isHttpSafe() || request->method.isIdempotent()); | |
cb928909 | 596 | } |
597 | ||
b6b6f466 | 598 | void |
599 | FwdState::serverClosed(int fd) | |
910169e5 | 600 | { |
cfd66529 | 601 | debugs(17, 2, HERE << "FD " << fd << " " << entry->url()); |
3e8c047e | 602 | retryOrBail(); |
603 | } | |
604 | ||
605 | void | |
26ac0430 AJ |
606 | FwdState::retryOrBail() |
607 | { | |
b6b6f466 | 608 | if (checkRetry()) { |
cfd66529 | 609 | debugs(17, 3, HERE << "re-forwarding (" << n_tries << " tries, " << (squid_curtime - start_t) << " secs)"); |
bc81ee68 AR |
610 | // we should retry the same destination if it failed due to pconn race |
611 | if (pconnRace == raceHappened) | |
612 | debugs(17, 4, HERE << "retrying the same destination"); | |
613 | else | |
614 | serverDestinations.shift(); // last one failed. try another. | |
8652f8e7 AJ |
615 | startConnectionOrFail(); |
616 | return; | |
d8fd0f18 | 617 | } |
62e76326 | 618 | |
9d2760b6 AR |
619 | // TODO: should we call completed() here and move doneWithRetries there? |
620 | doneWithRetries(); | |
621 | ||
622 | if (self != NULL && !err && shutting_down) { | |
955394ce | 623 | ErrorState *anErr = new ErrorState(ERR_SHUTTING_DOWN, Http::scServiceUnavailable, request); |
f01d4b80 | 624 | errorAppendEntry(entry, anErr); |
f563eea9 | 625 | } |
62e76326 | 626 | |
b6b6f466 | 627 | self = NULL; // refcounted |
910169e5 | 628 | } |
629 | ||
9d2760b6 AR |
630 | // If the Server quits before nibbling at the request body, the body sender |
631 | // will not know (so that we can retry). Call this if we will not retry. We | |
632 | // will notify the sender so that it does not get stuck waiting for space. | |
633 | void | |
634 | FwdState::doneWithRetries() | |
635 | { | |
636 | if (request && request->body_pipe != NULL) | |
637 | request->body_pipe->expectNoConsumption(); | |
638 | } | |
639 | ||
3e8c047e | 640 | // called by the server that failed after calling unregister() |
641 | void | |
642 | FwdState::handleUnregisteredServerEnd() | |
643 | { | |
cfd66529 | 644 | debugs(17, 2, HERE << "self=" << self << " err=" << err << ' ' << entry->url()); |
6b679a01 | 645 | assert(!Comm::IsConnOpen(serverConn)); |
3e8c047e | 646 | retryOrBail(); |
647 | } | |
648 | ||
a7ad6e4e | 649 | #if USE_SSL |
b6b6f466 | 650 | void |
651 | FwdState::negotiateSSL(int fd) | |
a7ad6e4e | 652 | { |
8e9bae99 | 653 | unsigned long ssl_lib_error = SSL_ERROR_NONE; |
a7ad6e4e | 654 | SSL *ssl = fd_table[fd].ssl; |
655 | int ret; | |
62e76326 | 656 | |
a7ad6e4e | 657 | if ((ret = SSL_connect(ssl)) <= 0) { |
62e76326 | 658 | int ssl_error = SSL_get_error(ssl, ret); |
8e9bae99 CT |
659 | #ifdef EPROTO |
660 | int sysErrNo = EPROTO; | |
661 | #else | |
662 | int sysErrNo = EACCES; | |
663 | #endif | |
62e76326 | 664 | |
665 | switch (ssl_error) { | |
666 | ||
667 | case SSL_ERROR_WANT_READ: | |
d841c88d | 668 | Comm::SetSelect(fd, COMM_SELECT_READ, fwdNegotiateSSLWrapper, this, 0); |
62e76326 | 669 | return; |
670 | ||
671 | case SSL_ERROR_WANT_WRITE: | |
d841c88d | 672 | Comm::SetSelect(fd, COMM_SELECT_WRITE, fwdNegotiateSSLWrapper, this, 0); |
62e76326 | 673 | return; |
674 | ||
8e9bae99 CT |
675 | case SSL_ERROR_SSL: |
676 | case SSL_ERROR_SYSCALL: | |
677 | ssl_lib_error = ERR_get_error(); | |
e0236918 | 678 | debugs(81, DBG_IMPORTANT, "fwdNegotiateSSL: Error negotiating SSL connection on FD " << fd << |
8e9bae99 | 679 | ": " << ERR_error_string(ssl_lib_error, NULL) << " (" << ssl_error << |
26ac0430 | 680 | "/" << ret << "/" << errno << ")"); |
62e76326 | 681 | |
8e9bae99 CT |
682 | // store/report errno when ssl_error is SSL_ERROR_SYSCALL, ssl_lib_error is 0, and ret is -1 |
683 | if (ssl_error == SSL_ERROR_SYSCALL && ret == -1 && ssl_lib_error == 0) | |
684 | sysErrNo = errno; | |
62e76326 | 685 | |
8e9bae99 CT |
686 | // falling through to complete error handling |
687 | ||
688 | default: | |
e7a7152c CT |
689 | // TODO: move into a method before merge |
690 | Ssl::ErrorDetail *errDetails; | |
4d16918e | 691 | Ssl::ErrorDetail *errFromFailure = (Ssl::ErrorDetail *)SSL_get_ex_data(ssl, ssl_ex_index_ssl_error_detail); |
e34763f4 | 692 | if (errFromFailure != NULL) { |
4d16918e CT |
693 | // The errFromFailure is attached to the ssl object |
694 | // and will be released when ssl object destroyed. | |
7a957a93 | 695 | // Copy errFromFailure to a new Ssl::ErrorDetail object. |
e7a7152c | 696 | errDetails = new Ssl::ErrorDetail(*errFromFailure); |
ef5fbcff | 697 | } else { |
7a957a93 | 698 | // server_cert can be NULL here |
061bbdec | 699 | X509 *server_cert = SSL_get_peer_certificate(ssl); |
de878a55 | 700 | errDetails = new Ssl::ErrorDetail(SQUID_ERR_SSL_HANDSHAKE, server_cert, NULL); |
061bbdec | 701 | X509_free(server_cert); |
8e9bae99 CT |
702 | } |
703 | ||
704 | if (ssl_lib_error != SSL_ERROR_NONE) | |
e7a7152c | 705 | errDetails->setLibError(ssl_lib_error); |
4d16918e | 706 | |
061bbdec | 707 | if (request->clientConnectionManager.valid()) { |
7a957a93 | 708 | // remember the server certificate from the ErrorDetail object |
fd4624d7 CT |
709 | if (Ssl::ServerBump *serverBump = request->clientConnectionManager->serverBump()) { |
710 | serverBump->serverCert.resetAndLock(errDetails->peerCert()); | |
fb2178bb | 711 | |
7a957a93 | 712 | // remember validation errors, if any |
62a7607e | 713 | if (Ssl::CertErrors *errs = static_cast<Ssl::CertErrors*>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors))) |
7a957a93 | 714 | serverBump->sslErrors = cbdataReference(errs); |
87f237a9 | 715 | } |
061bbdec CT |
716 | } |
717 | ||
7a957a93 AR |
718 | // For intercepted connections, set the host name to the server |
719 | // certificate CN. Otherwise, we just hope that CONNECT is using | |
720 | // a user-entered address (a host name or a user-entered IP). | |
c3d24490 | 721 | const bool isConnectRequest = !request->clientConnectionManager->port->flags.isIntercepted(); |
35cd4281 | 722 | if (request->flags.sslPeek && !isConnectRequest) { |
002db1fa | 723 | if (X509 *srvX509 = errDetails->peerCert()) { |
2c065fc8 AR |
724 | if (const char *name = Ssl::CommonHostName(srvX509)) { |
725 | request->SetHost(name); | |
726 | debugs(83, 3, HERE << "reset request host: " << name); | |
727 | } | |
728 | } | |
e7a7152c | 729 | } |
4d16918e | 730 | |
2c065fc8 | 731 | ErrorState *const anErr = makeConnectingError(ERR_SECURE_CONNECT_FAIL); |
e7a7152c CT |
732 | anErr->xerrno = sysErrNo; |
733 | anErr->detail = errDetails; | |
b6b6f466 | 734 | fail(anErr); |
62e76326 | 735 | |
5229395c AJ |
736 | if (serverConnection()->getPeer()) { |
737 | peerConnectFailed(serverConnection()->getPeer()); | |
62e76326 | 738 | } |
739 | ||
00ae51e4 | 740 | serverConn->close(); |
62e76326 | 741 | return; |
742 | } | |
a7ad6e4e | 743 | } |
62e76326 | 744 | |
fb2178bb | 745 | if (request->clientConnectionManager.valid()) { |
7a957a93 | 746 | // remember the server certificate from the ErrorDetail object |
fd4624d7 CT |
747 | if (Ssl::ServerBump *serverBump = request->clientConnectionManager->serverBump()) { |
748 | serverBump->serverCert.reset(SSL_get_peer_certificate(ssl)); | |
749 | ||
7a957a93 | 750 | // remember validation errors, if any |
62a7607e | 751 | if (Ssl::CertErrors *errs = static_cast<Ssl::CertErrors *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors))) |
7a957a93 | 752 | serverBump->sslErrors = cbdataReference(errs); |
fd4624d7 | 753 | } |
fb2178bb | 754 | } |
62e76326 | 755 | |
5229395c AJ |
756 | if (serverConnection()->getPeer() && !SSL_session_reused(ssl)) { |
757 | if (serverConnection()->getPeer()->sslSession) | |
758 | SSL_SESSION_free(serverConnection()->getPeer()->sslSession); | |
f38c5e43 | 759 | |
5229395c | 760 | serverConnection()->getPeer()->sslSession = SSL_get1_session(ssl); |
f38c5e43 | 761 | } |
762 | ||
2cef0ca6 | 763 | if (Ssl::TheConfig.ssl_crt_validator) { |
b56756cb | 764 | Ssl::CertValidationRequest validationRequest; |
14798e73 CT |
765 | // WARNING: Currently we do not use any locking for any of the |
766 | // members of the Ssl::CertValidationRequest class. In this code the | |
b56756cb | 767 | // Ssl::CertValidationRequest object used only to pass data to |
2cef0ca6 | 768 | // Ssl::CertValidationHelper::submit method. |
14798e73 | 769 | validationRequest.ssl = ssl; |
b56756cb | 770 | validationRequest.domainName = request->GetHost(); |
62a7607e | 771 | if (Ssl::CertErrors *errs = static_cast<Ssl::CertErrors *>(SSL_get_ex_data(ssl, ssl_ex_index_ssl_errors))) |
b56756cb CT |
772 | // validationRequest disappears on return so no need to cbdataReference |
773 | validationRequest.errors = errs; | |
2cef0ca6 | 774 | else |
b56756cb | 775 | validationRequest.errors = NULL; |
2cef0ca6 | 776 | try { |
4a77bb4e | 777 | debugs(83, 5, "Sending SSL certificate for validation to ssl_crtvd."); |
14798e73 | 778 | Ssl::CertValidationHelper::GetInstance()->sslSubmit(validationRequest, sslCrtvdHandleReplyWrapper, this); |
2cef0ca6 AR |
779 | return; |
780 | } catch (const std::exception &e) { | |
4a77bb4e | 781 | debugs(33, DBG_IMPORTANT, "ERROR: Failed to compose ssl_crtvd " << |
b56756cb | 782 | "request for " << validationRequest.domainName << |
2cef0ca6 AR |
783 | " certificate: " << e.what() << "; will now block to " << |
784 | "validate that certificate."); | |
785 | // fall through to do blocking in-process generation. | |
955394ce | 786 | ErrorState *anErr = new ErrorState(ERR_GATEWAY_FAILURE, Http::scInternalServerError, request); |
77dce8a5 CT |
787 | fail(anErr); |
788 | if (serverConnection()->getPeer()) { | |
789 | peerConnectFailed(serverConnection()->getPeer()); | |
790 | } | |
791 | serverConn->close(); | |
792 | self = NULL; | |
793 | return; | |
2cef0ca6 AR |
794 | } |
795 | } | |
2cef0ca6 | 796 | |
b6b6f466 | 797 | dispatch(); |
a7ad6e4e | 798 | } |
799 | ||
2cef0ca6 | 800 | void |
14798e73 | 801 | FwdState::sslCrtvdHandleReplyWrapper(void *data, Ssl::CertValidationResponse const &validationResponse) |
2cef0ca6 AR |
802 | { |
803 | FwdState * fwd = (FwdState *)(data); | |
14798e73 | 804 | fwd->sslCrtvdHandleReply(validationResponse); |
2cef0ca6 AR |
805 | } |
806 | ||
807 | void | |
14798e73 | 808 | FwdState::sslCrtvdHandleReply(Ssl::CertValidationResponse const &validationResponse) |
2cef0ca6 | 809 | { |
62a7607e | 810 | Ssl::CertErrors *errs = NULL; |
2cef0ca6 | 811 | Ssl::ErrorDetail *errDetails = NULL; |
77dce8a5 | 812 | bool validatorFailed = false; |
2cef0ca6 AR |
813 | if (!Comm::IsConnOpen(serverConnection())) { |
814 | return; | |
815 | } | |
2cef0ca6 | 816 | |
14798e73 CT |
817 | debugs(83,5, request->GetHost() << " cert validation result: " << validationResponse.resultCode); |
818 | ||
819 | if (validationResponse.resultCode == HelperReply::Error) | |
820 | errs = sslCrtvdCheckForErrors(validationResponse, errDetails); | |
821 | else if (validationResponse.resultCode != HelperReply::Okay) | |
4a77bb4e | 822 | validatorFailed = true; |
dd7167ea | 823 | |
14798e73 CT |
824 | if (!errDetails && !validatorFailed) { |
825 | dispatch(); | |
826 | return; | |
2cef0ca6 | 827 | } |
77dce8a5 CT |
828 | |
829 | ErrorState *anErr = NULL; | |
830 | if (validatorFailed) { | |
955394ce | 831 | anErr = new ErrorState(ERR_GATEWAY_FAILURE, Http::scInternalServerError, request); |
77dce8a5 CT |
832 | } else { |
833 | ||
834 | // Check the list error with | |
835 | if (errDetails && request->clientConnectionManager.valid()) { | |
836 | // remember the server certificate from the ErrorDetail object | |
837 | if (Ssl::ServerBump *serverBump = request->clientConnectionManager->serverBump()) { | |
838 | // remember validation errors, if any | |
839 | if (errs) { | |
840 | if (serverBump->sslErrors) | |
268a2e8f | 841 | cbdataReferenceDone(serverBump->sslErrors); |
77dce8a5 CT |
842 | serverBump->sslErrors = cbdataReference(errs); |
843 | } | |
2cef0ca6 AR |
844 | } |
845 | } | |
77dce8a5 CT |
846 | |
847 | anErr = makeConnectingError(ERR_SECURE_CONNECT_FAIL); | |
848 | anErr->detail = errDetails; | |
849 | /*anErr->xerrno= Should preserved*/ | |
2cef0ca6 AR |
850 | } |
851 | ||
2cef0ca6 AR |
852 | fail(anErr); |
853 | if (serverConnection()->getPeer()) { | |
854 | peerConnectFailed(serverConnection()->getPeer()); | |
855 | } | |
856 | serverConn->close(); | |
857 | self = NULL; | |
858 | return; | |
859 | } | |
268a2e8f | 860 | |
97538c72 AR |
861 | /// Checks errors in the cert. validator response against sslproxy_cert_error. |
862 | /// The first honored error, if any, is returned via errDetails parameter. | |
62a7607e CT |
863 | /// The method returns all seen errors except SSL_ERROR_NONE as Ssl::CertErrors. |
864 | Ssl::CertErrors * | |
14798e73 | 865 | FwdState::sslCrtvdCheckForErrors(Ssl::CertValidationResponse const &resp, Ssl::ErrorDetail *& errDetails) |
268a2e8f | 866 | { |
62a7607e | 867 | Ssl::CertErrors *errs = NULL; |
268a2e8f | 868 | |
97538c72 | 869 | ACLFilledChecklist *check = NULL; |
268a2e8f CT |
870 | if (acl_access *acl = Config.ssl_client.cert_error) |
871 | check = new ACLFilledChecklist(acl, request, dash_str); | |
872 | ||
97538c72 | 873 | SSL *ssl = fd_table[serverConnection()->fd].ssl; |
b56756cb | 874 | typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI; |
97538c72 | 875 | for (SVCRECI i = resp.errors.begin(); i != resp.errors.end(); ++i) { |
268a2e8f CT |
876 | debugs(83, 7, "Error item: " << i->error_no << " " << i->error_reason); |
877 | ||
4a77bb4e | 878 | assert(i->error_no != SSL_ERROR_NONE); |
268a2e8f | 879 | |
97538c72 | 880 | if (!errDetails) { |
268a2e8f CT |
881 | bool allowed = false; |
882 | if (check) { | |
62a7607e | 883 | check->sslErrors = new Ssl::CertErrors(Ssl::CertError(i->error_no, i->cert.get())); |
268a2e8f CT |
884 | if (check->fastCheck() == ACCESS_ALLOWED) |
885 | allowed = true; | |
886 | } | |
887 | // else the Config.ssl_client.cert_error access list is not defined | |
888 | // and the first error will cause the error page | |
889 | ||
890 | if (allowed) { | |
891 | debugs(83, 3, "bypassing SSL error " << i->error_no << " in " << "buffer"); | |
892 | } else { | |
893 | debugs(83, 5, "confirming SSL error " << i->error_no); | |
4a77bb4e CT |
894 | X509 *brokenCert = i->cert.get(); |
895 | Ssl::X509_Pointer peerCert(SSL_get_peer_certificate(ssl)); | |
268a2e8f | 896 | const char *aReason = i->error_reason.empty() ? NULL : i->error_reason.c_str(); |
4a77bb4e | 897 | errDetails = new Ssl::ErrorDetail(i->error_no, peerCert.get(), brokenCert, aReason); |
268a2e8f | 898 | } |
7830d88a FC |
899 | if (check) { |
900 | delete check->sslErrors; | |
901 | check->sslErrors = NULL; | |
902 | } | |
268a2e8f CT |
903 | } |
904 | ||
97538c72 | 905 | if (!errs) |
62a7607e | 906 | errs = new Ssl::CertErrors(Ssl::CertError(i->error_no, i->cert.get())); |
22636a68 | 907 | else |
62a7607e | 908 | errs->push_back_unique(Ssl::CertError(i->error_no, i->cert.get())); |
268a2e8f | 909 | } |
c732c598 CT |
910 | if (check) |
911 | delete check; | |
268a2e8f CT |
912 | |
913 | return errs; | |
914 | } | |
915 | ||
b6b6f466 | 916 | void |
917 | FwdState::initiateSSL() | |
a7ad6e4e | 918 | { |
a7ad6e4e | 919 | SSL *ssl; |
920 | SSL_CTX *sslContext = NULL; | |
a3c6762c | 921 | const CachePeer *peer = serverConnection()->getPeer(); |
5229395c | 922 | int fd = serverConnection()->fd; |
62e76326 | 923 | |
a7ad6e4e | 924 | if (peer) { |
62e76326 | 925 | assert(peer->use_ssl); |
926 | sslContext = peer->sslContext; | |
a7ad6e4e | 927 | } else { |
62e76326 | 928 | sslContext = Config.ssl_client.sslContext; |
a7ad6e4e | 929 | } |
62e76326 | 930 | |
a7ad6e4e | 931 | assert(sslContext); |
62e76326 | 932 | |
a7ad6e4e | 933 | if ((ssl = SSL_new(sslContext)) == NULL) { |
e0236918 | 934 | debugs(83, DBG_IMPORTANT, "fwdInitiateSSL: Error allocating handle: " << ERR_error_string(ERR_get_error(), NULL) ); |
955394ce | 935 | ErrorState *anErr = new ErrorState(ERR_SOCKET_FAILURE, Http::scInternalServerError, request); |
129fe2a1 | 936 | // TODO: create Ssl::ErrorDetail with OpenSSL-supplied error code |
b6b6f466 | 937 | fail(anErr); |
938 | self = NULL; // refcounted | |
62e76326 | 939 | return; |
a7ad6e4e | 940 | } |
62e76326 | 941 | |
a7ad6e4e | 942 | SSL_set_fd(ssl, fd); |
62e76326 | 943 | |
a7ad6e4e | 944 | if (peer) { |
62e76326 | 945 | if (peer->ssldomain) |
946 | SSL_set_ex_data(ssl, ssl_ex_index_server, peer->ssldomain); | |
947 | ||
a7ad6e4e | 948 | #if NOT_YET |
62e76326 | 949 | |
950 | else if (peer->name) | |
951 | SSL_set_ex_data(ssl, ssl_ex_index_server, peer->name); | |
952 | ||
a7ad6e4e | 953 | #endif |
62e76326 | 954 | |
955 | else | |
956 | SSL_set_ex_data(ssl, ssl_ex_index_server, peer->host); | |
f38c5e43 | 957 | |
958 | if (peer->sslSession) | |
959 | SSL_set_session(ssl, peer->sslSession); | |
960 | ||
a7ad6e4e | 961 | } else { |
53650ffa CT |
962 | // While we are peeking at the certificate, we may not know the server |
963 | // name that the client will request (after interception or CONNECT) | |
35cd4281 | 964 | // unless it was the CONNECT request with a user-typed address. |
53650ffa CT |
965 | const char *hostname = request->GetHost(); |
966 | const bool hostnameIsIp = request->GetHostIsNumeric(); | |
c3d24490 | 967 | const bool isConnectRequest = !request->clientConnectionManager->port->flags.isIntercepted(); |
35cd4281 | 968 | if (!request->flags.sslPeek || isConnectRequest) |
2c065fc8 AR |
969 | SSL_set_ex_data(ssl, ssl_ex_index_server, (void*)hostname); |
970 | ||
53650ffa | 971 | // Use SNI TLS extension only when we connect directly |
7a957a93 | 972 | // to the origin server and we know the server host name. |
53650ffa CT |
973 | if (!hostnameIsIp) |
974 | Ssl::setClientSNI(ssl, hostname); | |
a7ad6e4e | 975 | } |
62e76326 | 976 | |
2cef0ca6 AR |
977 | // If CertValidation Helper used do not lookup checklist for errors, |
978 | // but keep a list of errors to send it to CertValidator | |
979 | if (!Ssl::TheConfig.ssl_crt_validator) { | |
2cef0ca6 AR |
980 | // Create the ACL check list now, while we have access to more info. |
981 | // The list is used in ssl_verify_cb() and is freed in ssl_free(). | |
982 | if (acl_access *acl = Config.ssl_client.cert_error) { | |
983 | ACLFilledChecklist *check = new ACLFilledChecklist(acl, request, dash_str); | |
2cef0ca6 AR |
984 | SSL_set_ex_data(ssl, ssl_ex_index_cert_error_check, check); |
985 | } | |
4d5a6db8 | 986 | } |
987 | ||
e7bcc25f CT |
988 | // store peeked cert to check SQUID_X509_V_ERR_CERT_CHANGE |
989 | X509 *peeked_cert; | |
fd4624d7 | 990 | if (request->clientConnectionManager.valid() && |
87f237a9 A |
991 | request->clientConnectionManager->serverBump() && |
992 | (peeked_cert = request->clientConnectionManager->serverBump()->serverCert.get())) { | |
e7bcc25f CT |
993 | CRYPTO_add(&(peeked_cert->references),1,CRYPTO_LOCK_X509); |
994 | SSL_set_ex_data(ssl, ssl_ex_index_ssl_peeked_cert, peeked_cert); | |
995 | } | |
996 | ||
a7ad6e4e | 997 | fd_table[fd].ssl = ssl; |
998 | fd_table[fd].read_method = &ssl_read_method; | |
999 | fd_table[fd].write_method = &ssl_write_method; | |
b6b6f466 | 1000 | negotiateSSL(fd); |
a7ad6e4e | 1001 | } |
62e76326 | 1002 | |
a7ad6e4e | 1003 | #endif |
1004 | ||
b6b6f466 | 1005 | void |
f01d4b80 | 1006 | FwdState::connectDone(const Comm::ConnectionPointer &conn, comm_err_t status, int xerrno) |
41462d93 | 1007 | { |
2f538b78 | 1008 | if (status != COMM_OK) { |
9c8a6c3b | 1009 | ErrorState *const anErr = makeConnectingError(ERR_CONNECT_FAIL); |
b6b6f466 | 1010 | anErr->xerrno = xerrno; |
b6b6f466 | 1011 | fail(anErr); |
62e76326 | 1012 | |
2f538b78 | 1013 | /* it might have been a timeout with a partially open link */ |
00ae51e4 AJ |
1014 | if (conn != NULL) { |
1015 | if (conn->getPeer()) | |
1016 | peerConnectFailed(conn->getPeer()); | |
62e76326 | 1017 | |
80463bb4 | 1018 | conn->close(); |
2f538b78 | 1019 | } |
aed188fd | 1020 | retryOrBail(); |
2f538b78 AJ |
1021 | return; |
1022 | } | |
62e76326 | 1023 | |
00ae51e4 | 1024 | serverConn = conn; |
3b9899f7 | 1025 | flags.connected_okay = true; |
00ae51e4 | 1026 | |
f01d4b80 | 1027 | debugs(17, 3, HERE << serverConnection() << ": '" << entry->url() << "'" ); |
62e76326 | 1028 | |
5229395c | 1029 | comm_add_close_handler(serverConnection()->fd, fwdServerClosedWrapper, this); |
2f538b78 | 1030 | |
5229395c AJ |
1031 | if (serverConnection()->getPeer()) |
1032 | peerConnectSucceded(serverConnection()->getPeer()); | |
2f538b78 AJ |
1033 | |
1034 | #if USE_SSL | |
693cb033 | 1035 | if (!request->flags.pinned) { |
39322eba | 1036 | if ((serverConnection()->getPeer() && serverConnection()->getPeer()->use_ssl) || |
37276dbb AR |
1037 | (!serverConnection()->getPeer() && request->protocol == AnyP::PROTO_HTTPS) || |
1038 | request->flags.sslPeek) { | |
39322eba J |
1039 | initiateSSL(); |
1040 | return; | |
1041 | } | |
41462d93 | 1042 | } |
2f538b78 AJ |
1043 | #endif |
1044 | ||
1045 | dispatch(); | |
41462d93 | 1046 | } |
1047 | ||
b6b6f466 | 1048 | void |
1049 | FwdState::connectTimeout(int fd) | |
41462d93 | 1050 | { |
bf8fe701 | 1051 | debugs(17, 2, "fwdConnectTimeout: FD " << fd << ": '" << entry->url() << "'" ); |
6b679a01 AJ |
1052 | assert(serverDestinations[0] != NULL); |
1053 | assert(fd == serverDestinations[0]->fd); | |
62e76326 | 1054 | |
528b2c61 | 1055 | if (entry->isEmpty()) { |
955394ce | 1056 | ErrorState *anErr = new ErrorState(ERR_CONNECT_FAIL, Http::scGateway_Timeout, request); |
b6b6f466 | 1057 | anErr->xerrno = ETIMEDOUT; |
1058 | fail(anErr); | |
62e76326 | 1059 | |
cfd66529 | 1060 | /* This marks the peer DOWN ... */ |
6b679a01 AJ |
1061 | if (serverDestinations[0]->getPeer()) |
1062 | peerConnectFailed(serverDestinations[0]->getPeer()); | |
41462d93 | 1063 | } |
62e76326 | 1064 | |
6b679a01 AJ |
1065 | if (Comm::IsConnOpen(serverDestinations[0])) { |
1066 | serverDestinations[0]->close(); | |
746beefe | 1067 | } |
41462d93 | 1068 | } |
1069 | ||
cfd66529 | 1070 | /** |
ae18571d AJ |
1071 | * Called after Forwarding path selection (via peer select) has taken place. |
1072 | * And whenever forwarding needs to attempt a new connection (routing failover) | |
1073 | * We have a vector of possible localIP->remoteIP paths now ready to start being connected. | |
cfd66529 | 1074 | */ |
b6b6f466 | 1075 | void |
1076 | FwdState::connectStart() | |
41462d93 | 1077 | { |
6b679a01 AJ |
1078 | assert(serverDestinations.size() > 0); |
1079 | ||
cfd66529 | 1080 | debugs(17, 3, "fwdConnectStart: " << entry->url()); |
62e76326 | 1081 | |
a122fdeb | 1082 | if (!request->hier.first_conn_start.tv_sec) // first attempt |
3ff65596 AR |
1083 | request->hier.first_conn_start = current_time; |
1084 | ||
cfd66529 AJ |
1085 | /* connection timeout */ |
1086 | int ctimeout; | |
6b679a01 AJ |
1087 | if (serverDestinations[0]->getPeer()) { |
1088 | ctimeout = serverDestinations[0]->getPeer()->connect_timeout > 0 ? | |
1089 | serverDestinations[0]->getPeer()->connect_timeout : Config.Timeout.peer_connect; | |
db1cd23c | 1090 | } else { |
62e76326 | 1091 | ctimeout = Config.Timeout.connect; |
db1cd23c | 1092 | } |
62e76326 | 1093 | |
cfd66529 AJ |
1094 | /* calculate total forwarding timeout ??? */ |
1095 | int ftimeout = Config.Timeout.forward - (squid_curtime - start_t); | |
777831e0 | 1096 | if (ftimeout < 0) |
1097 | ftimeout = 5; | |
1098 | ||
1099 | if (ftimeout < ctimeout) | |
1100 | ctimeout = ftimeout; | |
1101 | ||
450fe1cb | 1102 | if (serverDestinations[0]->getPeer() && request->flags.sslBumped) { |
b68415a1 | 1103 | debugs(50, 4, "fwdConnectStart: Ssl bumped connections through parrent proxy are not allowed"); |
955394ce | 1104 | ErrorState *anErr = new ErrorState(ERR_CANNOT_FORWARD, Http::scServiceUnavailable, request); |
b68415a1 CT |
1105 | fail(anErr); |
1106 | self = NULL; // refcounted | |
1107 | return; | |
985bfe6b | 1108 | } |
d67acb4e | 1109 | |
e857372a | 1110 | request->flags.pinned = false; // XXX: what if the ConnStateData set this to flag existing credentials? |
2efeb0b7 AJ |
1111 | // XXX: answer: the peer selection *should* catch it and give us only the pinned peer. so we reverse the =0 step below. |
1112 | // XXX: also, logs will now lie if pinning is broken and leads to an error message. | |
6b679a01 | 1113 | if (serverDestinations[0]->peerType == PINNED) { |
26ac0430 | 1114 | ConnStateData *pinned_connection = request->pinnedConnection(); |
693cb033 | 1115 | debugs(17,7, "pinned peer connection: " << pinned_connection); |
bc81ee68 AR |
1116 | // pinned_connection may become nil after a pconn race |
1117 | if (pinned_connection) | |
1118 | serverConn = pinned_connection->validatePinnedConnection(request, serverDestinations[0]->getPeer()); | |
1119 | else | |
1120 | serverConn = NULL; | |
e3a4aecc | 1121 | if (Comm::IsConnOpen(serverConn)) { |
3b9899f7 | 1122 | flags.connected_okay = true; |
95dc7ff4 | 1123 | ++n_tries; |
e857372a | 1124 | request->flags.pinned = true; |
d67acb4e | 1125 | if (pinned_connection->pinnedAuth()) |
e857372a | 1126 | request->flags.auth = true; |
23211a9f | 1127 | comm_add_close_handler(serverConn->fd, fwdServerClosedWrapper, this); |
bc81ee68 AR |
1128 | // the server may close the pinned connection before this request |
1129 | pconnRace = racePossible; | |
6b679a01 | 1130 | dispatch(); |
d67acb4e AJ |
1131 | return; |
1132 | } | |
693cb033 | 1133 | // Pinned connection failure. |
85563fd9 | 1134 | debugs(17,2,HERE << "Pinned connection failed: " << pinned_connection); |
955394ce | 1135 | ErrorState *anErr = new ErrorState(ERR_ZERO_SIZE_OBJECT, Http::scServiceUnavailable, request); |
693cb033 CT |
1136 | fail(anErr); |
1137 | self = NULL; // refcounted | |
1138 | return; | |
26ac0430 | 1139 | } |
d67acb4e | 1140 | |
642a305c | 1141 | // Use pconn to avoid opening a new connection. |
7a150c71 AJ |
1142 | const char *host = NULL; |
1143 | if (!serverDestinations[0]->getPeer()) | |
06093389 | 1144 | host = request->GetHost(); |
bc81ee68 AR |
1145 | |
1146 | Comm::ConnectionPointer temp; | |
1147 | // Avoid pconns after races so that the same client does not suffer twice. | |
1148 | // This does not increase the total number of connections because we just | |
1149 | // closed the connection that failed the race. And re-pinning assumes this. | |
1150 | if (pconnRace != raceHappened) | |
1151 | temp = fwdPconnPool->pop(serverDestinations[0], host, checkRetriable()); | |
bbf706ff | 1152 | |
bc81ee68 AR |
1153 | const bool openedPconn = Comm::IsConnOpen(temp); |
1154 | pconnRace = openedPconn ? racePossible : raceImpossible; | |
cfd66529 | 1155 | |
6b679a01 | 1156 | // if we found an open persistent connection to use. use it. |
bc81ee68 | 1157 | if (openedPconn) { |
80463bb4 | 1158 | serverConn = temp; |
3b9899f7 | 1159 | flags.connected_okay = true; |
642a305c | 1160 | debugs(17, 3, HERE << "reusing pconn " << serverConnection()); |
95dc7ff4 | 1161 | ++n_tries; |
4ed0e075 | 1162 | |
5229395c | 1163 | comm_add_close_handler(serverConnection()->fd, fwdServerClosedWrapper, this); |
b5523edc AJ |
1164 | |
1165 | /* Update server side TOS and Netfilter mark on the connection. */ | |
1166 | if (Ip::Qos::TheConfig.isAclTosActive()) { | |
1167 | temp->tos = GetTosToServer(request); | |
1168 | Ip::Qos::setSockTos(temp, temp->tos); | |
1169 | } | |
1170 | #if SO_MARK | |
1171 | if (Ip::Qos::TheConfig.isAclNfmarkActive()) { | |
1172 | temp->nfmark = GetNfmarkToServer(request); | |
1173 | Ip::Qos::setSockNfmark(temp, temp->nfmark); | |
1174 | } | |
1175 | #endif | |
1176 | ||
c8ceec27 | 1177 | dispatch(); |
c8ceec27 | 1178 | return; |
41462d93 | 1179 | } |
62e76326 | 1180 | |
bc81ee68 AR |
1181 | // We will try to open a new connection, possibly to the same destination. |
1182 | // We reset serverDestinations[0] in case we are using it again because | |
1183 | // ConnOpener modifies its destination argument. | |
4dd643d5 | 1184 | serverDestinations[0]->local.port(0); |
bc81ee68 AR |
1185 | serverConn = NULL; |
1186 | ||
bc87dc25 | 1187 | #if URL_CHECKSUM_DEBUG |
b6b6f466 | 1188 | entry->mem_obj->checkUrlChecksum(); |
bc87dc25 | 1189 | #endif |
62e76326 | 1190 | |
b5523edc AJ |
1191 | /* Get the server side TOS and Netfilter mark to be set on the connection. */ |
1192 | if (Ip::Qos::TheConfig.isAclTosActive()) { | |
1193 | serverDestinations[0]->tos = GetTosToServer(request); | |
1194 | } | |
11e8cfe3 | 1195 | #if SO_MARK && USE_LIBCAP |
a750e510 | 1196 | serverDestinations[0]->nfmark = GetNfmarkToServer(request); |
25b481e6 | 1197 | debugs(17, 3, "fwdConnectStart: got outgoing addr " << serverDestinations[0]->local << ", tos " << int(serverDestinations[0]->tos) |
a750e510 | 1198 | << ", netfilter mark " << serverDestinations[0]->nfmark); |
425de4c8 | 1199 | #else |
a750e510 | 1200 | serverDestinations[0]->nfmark = 0; |
25b481e6 | 1201 | debugs(17, 3, "fwdConnectStart: got outgoing addr " << serverDestinations[0]->local << ", tos " << int(serverDestinations[0]->tos)); |
b5523edc AJ |
1202 | #endif |
1203 | ||
e3a4aecc AJ |
1204 | calls.connector = commCbCall(17,3, "fwdConnectDoneWrapper", CommConnectCbPtrFun(fwdConnectDoneWrapper, this)); |
1205 | Comm::ConnOpener *cs = new Comm::ConnOpener(serverDestinations[0], calls.connector, ctimeout); | |
7a150c71 AJ |
1206 | if (host) |
1207 | cs->setHost(host); | |
855150a4 | 1208 | AsyncJob::Start(cs); |
41462d93 | 1209 | } |
1210 | ||
b6b6f466 | 1211 | void |
1212 | FwdState::dispatch() | |
41462d93 | 1213 | { |
5c336a3b | 1214 | debugs(17, 3, HERE << clientConn << ": Fetching '" << RequestMethodStr(request->method) << " " << entry->url() << "'"); |
e0ebe27c | 1215 | /* |
1216 | * Assert that server_fd is set. This is to guarantee that fwdState | |
1217 | * is attached to something and will be deallocated when server_fd | |
1218 | * is closed. | |
1219 | */ | |
6b679a01 | 1220 | assert(Comm::IsConnOpen(serverConn)); |
62e76326 | 1221 | |
5229395c | 1222 | fd_note(serverConnection()->fd, entry->url()); |
62e76326 | 1223 | |
5229395c | 1224 | fd_table[serverConnection()->fd].noteUse(fwdPconnPool); |
62e76326 | 1225 | |
a7ad6e4e | 1226 | /*assert(!EBIT_TEST(entry->flags, ENTRY_DISPATCHED)); */ |
1227 | assert(entry->ping_status != PING_WAITING); | |
62e76326 | 1228 | |
a7ad6e4e | 1229 | assert(entry->lock_count); |
62e76326 | 1230 | |
a7ad6e4e | 1231 | EBIT_SET(entry->flags, ENTRY_DISPATCHED); |
62e76326 | 1232 | |
cc192b50 | 1233 | netdbPingSite(request->GetHost()); |
62e76326 | 1234 | |
425de4c8 | 1235 | /* Retrieves remote server TOS or MARK value, and stores it as part of the |
7172612f | 1236 | * original client request FD object. It is later used to forward |
425de4c8 | 1237 | * remote server's TOS/MARK in the response to the client in case of a MISS. |
7172612f | 1238 | */ |
425de4c8 | 1239 | if (Ip::Qos::TheConfig.isHitNfmarkActive()) { |
b5523edc AJ |
1240 | if (Comm::IsConnOpen(clientConn) && Comm::IsConnOpen(serverConnection())) { |
1241 | fde * clientFde = &fd_table[clientConn->fd]; // XXX: move the fd_table access into Ip::Qos | |
425de4c8 | 1242 | /* Get the netfilter mark for the connection */ |
b5523edc | 1243 | Ip::Qos::getNfmarkFromServer(serverConnection(), clientFde); |
425de4c8 AJ |
1244 | } |
1245 | } | |
1246 | ||
1247 | #if _SQUID_LINUX_ | |
1248 | /* Bug 2537: The TOS forward part of QOS only applies to patched Linux kernels. */ | |
1249 | if (Ip::Qos::TheConfig.isHitTosActive()) { | |
b5523edc AJ |
1250 | if (Comm::IsConnOpen(clientConn)) { |
1251 | fde * clientFde = &fd_table[clientConn->fd]; // XXX: move the fd_table access into Ip::Qos | |
425de4c8 | 1252 | /* Get the TOS value for the packet */ |
b5523edc | 1253 | Ip::Qos::getTosFromServer(serverConnection(), clientFde); |
7172612f | 1254 | } |
26ac0430 | 1255 | } |
7172612f AJ |
1256 | #endif |
1257 | ||
d7ce0bcd | 1258 | #if USE_SSL |
2c065fc8 | 1259 | if (request->flags.sslPeek) { |
d7ce0bcd | 1260 | CallJobHere1(17, 4, request->clientConnectionManager, ConnStateData, |
061bbdec | 1261 | ConnStateData::httpsPeeked, serverConnection()); |
d7ce0bcd AR |
1262 | unregister(serverConn); // async call owns it now |
1263 | complete(); // destroys us | |
1264 | return; | |
061bbdec | 1265 | } |
d7ce0bcd AR |
1266 | #endif |
1267 | ||
5229395c | 1268 | if (serverConnection()->getPeer() != NULL) { |
95dc7ff4 | 1269 | ++ serverConnection()->getPeer()->stats.fetches; |
5229395c AJ |
1270 | request->peer_login = serverConnection()->getPeer()->login; |
1271 | request->peer_domain = serverConnection()->getPeer()->domain; | |
b6b6f466 | 1272 | httpStart(this); |
41462d93 | 1273 | } else { |
2c065fc8 | 1274 | assert(!request->flags.sslPeek); |
b6b6f466 | 1275 | request->peer_login = NULL; |
1276 | request->peer_domain = NULL; | |
62e76326 | 1277 | |
1278 | switch (request->protocol) { | |
a7ad6e4e | 1279 | #if USE_SSL |
62e76326 | 1280 | |
0c3d3f65 | 1281 | case AnyP::PROTO_HTTPS: |
b6b6f466 | 1282 | httpStart(this); |
62e76326 | 1283 | break; |
a7ad6e4e | 1284 | #endif |
62e76326 | 1285 | |
0c3d3f65 | 1286 | case AnyP::PROTO_HTTP: |
b6b6f466 | 1287 | httpStart(this); |
62e76326 | 1288 | break; |
1289 | ||
0c3d3f65 | 1290 | case AnyP::PROTO_GOPHER: |
b6b6f466 | 1291 | gopherStart(this); |
62e76326 | 1292 | break; |
1293 | ||
0c3d3f65 | 1294 | case AnyP::PROTO_FTP: |
b6b6f466 | 1295 | ftpStart(this); |
62e76326 | 1296 | break; |
1297 | ||
39a19cb7 | 1298 | case AnyP::PROTO_CACHE_OBJECT: |
62e76326 | 1299 | |
0c3d3f65 | 1300 | case AnyP::PROTO_INTERNAL: |
62e76326 | 1301 | |
0c3d3f65 | 1302 | case AnyP::PROTO_URN: |
62e76326 | 1303 | fatal_dump("Should never get here"); |
1304 | break; | |
1305 | ||
0c3d3f65 | 1306 | case AnyP::PROTO_WHOIS: |
b6b6f466 | 1307 | whoisStart(this); |
62e76326 | 1308 | break; |
1309 | ||
0c3d3f65 | 1310 | case AnyP::PROTO_WAIS: /* Not implemented */ |
fc68f6b1 | 1311 | |
62e76326 | 1312 | default: |
7be06178 | 1313 | debugs(17, DBG_IMPORTANT, "WARNING: Cannot retrieve '" << entry->url() << "'."); |
955394ce | 1314 | ErrorState *anErr = new ErrorState(ERR_UNSUP_REQ, Http::scBadRequest, request); |
b6b6f466 | 1315 | fail(anErr); |
7be06178 | 1316 | // Set the dont_retry flag because this is not a transient (network) error. |
e857372a | 1317 | flags.dont_retry = true; |
6b679a01 | 1318 | if (Comm::IsConnOpen(serverConn)) { |
00ae51e4 | 1319 | serverConn->close(); |
746beefe | 1320 | } |
62e76326 | 1321 | break; |
1322 | } | |
41462d93 | 1323 | } |
1324 | } | |
1325 | ||
545782b8 | 1326 | /* |
1327 | * FwdState::reforward | |
1328 | * | |
1329 | * returns TRUE if the transaction SHOULD be re-forwarded to the | |
8bbb16e3 | 1330 | * next choice in the serverDestinations list. This method is called when |
545782b8 | 1331 | * server-side communication completes normally, or experiences |
1332 | * some error after receiving the end of HTTP headers. | |
1333 | */ | |
b6b6f466 | 1334 | int |
1335 | FwdState::reforward() | |
db1cd23c | 1336 | { |
b6b6f466 | 1337 | StoreEntry *e = entry; |
c4a88a3e CT |
1338 | |
1339 | if (EBIT_TEST(e->flags, ENTRY_ABORTED)) { | |
1340 | debugs(17, 3, HERE << "entry aborted"); | |
1341 | return 0; | |
1342 | } | |
1343 | ||
db1cd23c | 1344 | assert(e->store_status == STORE_PENDING); |
1345 | assert(e->mem_obj); | |
bc87dc25 | 1346 | #if URL_CHECKSUM_DEBUG |
62e76326 | 1347 | |
528b2c61 | 1348 | e->mem_obj->checkUrlChecksum(); |
bc87dc25 | 1349 | #endif |
62e76326 | 1350 | |
cfd66529 | 1351 | debugs(17, 3, HERE << e->url() << "?" ); |
62e76326 | 1352 | |
d6eb18d6 | 1353 | if (!EBIT_TEST(e->flags, ENTRY_FWD_HDR_WAIT)) { |
cfd66529 | 1354 | debugs(17, 3, HERE << "No, ENTRY_FWD_HDR_WAIT isn't set"); |
62e76326 | 1355 | return 0; |
d6eb18d6 | 1356 | } |
62e76326 | 1357 | |
437823b4 | 1358 | if (n_tries > Config.forward_max_tries) |
62e76326 | 1359 | return 0; |
1360 | ||
58217e94 | 1361 | if (request->bodyNibbled()) |
62e76326 | 1362 | return 0; |
1363 | ||
8652f8e7 AJ |
1364 | if (serverDestinations.size() <= 1) { |
1365 | // NP: <= 1 since total count includes the recently failed one. | |
cfd66529 | 1366 | debugs(17, 3, HERE << "No alternative forwarding paths left"); |
62e76326 | 1367 | return 0; |
db1cd23c | 1368 | } |
62e76326 | 1369 | |
9b769c67 | 1370 | const Http::StatusCode s = e->getReply()->sline.status(); |
cfd66529 | 1371 | debugs(17, 3, HERE << "status " << s); |
b6b6f466 | 1372 | return reforwardableStatus(s); |
db1cd23c | 1373 | } |
1374 | ||
2ac4f6b5 AR |
1375 | /** |
1376 | * Create "503 Service Unavailable" or "504 Gateway Timeout" error depending | |
1377 | * on whether this is a validation request. RFC 2616 says that we MUST reply | |
1378 | * with "504 Gateway Timeout" if validation fails and cached reply has | |
1379 | * proxy-revalidate, must-revalidate or s-maxage Cache-Control directive. | |
1380 | */ | |
1381 | ErrorState * | |
1382 | FwdState::makeConnectingError(const err_type type) const | |
1383 | { | |
450fe1cb | 1384 | return new ErrorState(type, request->flags.needValidation ? |
955394ce | 1385 | Http::scGateway_Timeout : Http::scServiceUnavailable, request); |
2ac4f6b5 AR |
1386 | } |
1387 | ||
b6b6f466 | 1388 | static void |
1389 | fwdStats(StoreEntry * s) | |
64d8034e | 1390 | { |
b6b6f466 | 1391 | int i; |
1392 | int j; | |
1393 | storeAppendPrintf(s, "Status"); | |
62e76326 | 1394 | |
95dc7ff4 FC |
1395 | for (j = 1; j < MAX_FWD_STATS_IDX; ++j) { |
1396 | storeAppendPrintf(s, "\ttry#%d", j); | |
64d8034e | 1397 | } |
64d8034e | 1398 | |
b6b6f466 | 1399 | storeAppendPrintf(s, "\n"); |
0185bd6f | 1400 | |
955394ce | 1401 | for (i = 0; i <= (int) Http::scInvalidHeader; ++i) { |
b6b6f466 | 1402 | if (FwdReplyCodes[0][i] == 0) |
1403 | continue; | |
0185bd6f | 1404 | |
b6b6f466 | 1405 | storeAppendPrintf(s, "%3d", i); |
0185bd6f | 1406 | |
95dc7ff4 | 1407 | for (j = 0; j <= MAX_FWD_STATS_IDX; ++j) { |
b6b6f466 | 1408 | storeAppendPrintf(s, "\t%d", FwdReplyCodes[j][i]); |
62e76326 | 1409 | } |
62e76326 | 1410 | |
b6b6f466 | 1411 | storeAppendPrintf(s, "\n"); |
e0ebe27c | 1412 | } |
7197b20d | 1413 | } |
1414 | ||
b6b6f466 | 1415 | /**** STATIC MEMBER FUNCTIONS *************************************************/ |
db1cd23c | 1416 | |
b6b6f466 | 1417 | bool |
955394ce | 1418 | FwdState::reforwardableStatus(const Http::StatusCode s) const |
db1cd23c | 1419 | { |
b6b6f466 | 1420 | switch (s) { |
62e76326 | 1421 | |
955394ce | 1422 | case Http::scBadGateway: |
62e76326 | 1423 | |
955394ce | 1424 | case Http::scGateway_Timeout: |
b6b6f466 | 1425 | return true; |
62e76326 | 1426 | |
955394ce | 1427 | case Http::scForbidden: |
62e76326 | 1428 | |
955394ce | 1429 | case Http::scInternalServerError: |
62e76326 | 1430 | |
955394ce | 1431 | case Http::scNotImplemented: |
62e76326 | 1432 | |
955394ce | 1433 | case Http::scServiceUnavailable: |
b6b6f466 | 1434 | return Config.retry.onerror; |
62e76326 | 1435 | |
b6b6f466 | 1436 | default: |
1437 | return false; | |
db1cd23c | 1438 | } |
b6b6f466 | 1439 | |
1440 | /* NOTREACHED */ | |
db1cd23c | 1441 | } |
8ddcc35d | 1442 | |
06093389 AJ |
1443 | /** |
1444 | * Decide where details need to be gathered to correctly describe a persistent connection. | |
1445 | * What is needed: | |
642a305c AJ |
1446 | * - the address/port details about this link |
1447 | * - domain name of server at other end of this link (either peer or requested host) | |
06093389 | 1448 | */ |
781ce8ff | 1449 | void |
642a305c | 1450 | FwdState::pconnPush(Comm::ConnectionPointer &conn, const char *domain) |
781ce8ff | 1451 | { |
642a305c | 1452 | if (conn->getPeer()) { |
7a150c71 | 1453 | fwdPconnPool->push(conn, NULL); |
06093389 | 1454 | } else { |
642a305c | 1455 | fwdPconnPool->push(conn, domain); |
06093389 | 1456 | } |
781ce8ff | 1457 | } |
1458 | ||
8ddcc35d | 1459 | void |
b6b6f466 | 1460 | FwdState::initModule() |
8ddcc35d | 1461 | { |
6852be71 | 1462 | RegisterWithCacheManager(); |
8ddcc35d | 1463 | } |
1464 | ||
62ee09ca | 1465 | void |
84f50787 | 1466 | FwdState::RegisterWithCacheManager(void) |
62ee09ca | 1467 | { |
8822ebee | 1468 | Mgr::RegisterAction("forward", "Request Forwarding Statistics", fwdStats, 0, 1); |
62ee09ca | 1469 | } |
1470 | ||
b6b6f466 | 1471 | void |
955394ce | 1472 | FwdState::logReplyStatus(int tries, const Http::StatusCode status) |
8ddcc35d | 1473 | { |
955394ce | 1474 | if (status > Http::scInvalidHeader) |
62e76326 | 1475 | return; |
1476 | ||
75eb730e | 1477 | assert(tries >= 0); |
62e76326 | 1478 | |
8ddcc35d | 1479 | if (tries > MAX_FWD_STATS_IDX) |
62e76326 | 1480 | tries = MAX_FWD_STATS_IDX; |
1481 | ||
95dc7ff4 | 1482 | ++ FwdReplyCodes[tries][status]; |
8ddcc35d | 1483 | } |
1484 | ||
b6b6f466 | 1485 | /**** PRIVATE NON-MEMBER FUNCTIONS ********************************************/ |
62e76326 | 1486 | |
057f5854 | 1487 | /* |
1488 | * DPW 2007-05-19 | |
1489 | * Formerly static, but now used by client_side_request.cc | |
1490 | */ | |
425de4c8 AJ |
1491 | /// Checks for a TOS value to apply depending on the ACL |
1492 | tos_t | |
b6b6f466 | 1493 | aclMapTOS(acl_tos * head, ACLChecklist * ch) |
b6a2f15e | 1494 | { |
b6b6f466 | 1495 | acl_tos *l; |
62e76326 | 1496 | |
b6b6f466 | 1497 | for (l = head; l; l = l->next) { |
2efeb0b7 | 1498 | if (!l->aclList || ch->fastCheck(l->aclList) == ACCESS_ALLOWED) |
b6b6f466 | 1499 | return l->tos; |
1500 | } | |
5894ad28 | 1501 | |
b6b6f466 | 1502 | return 0; |
1503 | } | |
5894ad28 | 1504 | |
425de4c8 AJ |
1505 | /// Checks for a netfilter mark value to apply depending on the ACL |
1506 | nfmark_t | |
1507 | aclMapNfmark(acl_nfmark * head, ACLChecklist * ch) | |
1508 | { | |
1509 | acl_nfmark *l; | |
1510 | ||
1511 | for (l = head; l; l = l->next) { | |
2efeb0b7 | 1512 | if (!l->aclList || ch->fastCheck(l->aclList) == ACCESS_ALLOWED) |
425de4c8 AJ |
1513 | return l->nfmark; |
1514 | } | |
1515 | ||
1516 | return 0; | |
1517 | } | |
1518 | ||
cfd66529 | 1519 | void |
f9b72e0c | 1520 | getOutgoingAddress(HttpRequest * request, Comm::ConnectionPointer conn) |
b6b6f466 | 1521 | { |
07f889c1 | 1522 | // skip if an outgoing address is already set. |
4dd643d5 | 1523 | if (!conn->local.isAnyAddr()) return; |
cfd66529 | 1524 | |
07f889c1 | 1525 | // ensure that at minimum the wildcard local matches remote protocol |
4dd643d5 AJ |
1526 | if (conn->remote.isIPv4()) |
1527 | conn->local.setIPv4(); | |
07f889c1 | 1528 | |
cfd66529 | 1529 | // maybe use TPROXY client address |
450fe1cb | 1530 | if (request && request->flags.spoofClientIp) { |
739b352a | 1531 | if (!conn->getPeer() || !conn->getPeer()->options.no_tproxy) { |
96d64448 AJ |
1532 | #if FOLLOW_X_FORWARDED_FOR && LINUX_NETFILTER |
1533 | if (Config.onoff.tproxy_uses_indirect_client) | |
45906573 | 1534 | conn->local = request->indirect_client_addr; |
96d64448 AJ |
1535 | else |
1536 | #endif | |
45906573 | 1537 | conn->local = request->client_addr; |
cfd66529 AJ |
1538 | // some flags need setting on the socket to use this address |
1539 | conn->flags |= COMM_DOBIND; | |
1540 | conn->flags |= COMM_TRANSPARENT; | |
1541 | return; | |
1542 | } | |
b0758e04 AJ |
1543 | // else no tproxy today ... |
1544 | } | |
c303f6e3 | 1545 | |
b50e327b | 1546 | if (!Config.accessList.outgoing_address) { |
cfd66529 | 1547 | return; // anything will do. |
b50e327b AJ |
1548 | } |
1549 | ||
c0941a6a | 1550 | ACLFilledChecklist ch(NULL, request, NULL); |
739b352a | 1551 | ch.dst_peer = conn->getPeer(); |
cfd66529 AJ |
1552 | ch.dst_addr = conn->remote; |
1553 | ||
1554 | // TODO use the connection details in ACL. | |
1555 | // needs a bit of rework in ACLFilledChecklist to use Comm::Connection instead of ConnStateData | |
6db78a1a | 1556 | |
0dc8ffa5 | 1557 | AclAddress *l; |
cfd66529 AJ |
1558 | for (l = Config.accessList.outgoing_address; l; l = l->next) { |
1559 | ||
1560 | /* check if the outgoing address is usable to the destination */ | |
4dd643d5 | 1561 | if (conn->remote.isIPv4() != l->addr.isIPv4()) continue; |
cfd66529 AJ |
1562 | |
1563 | /* check ACLs for this outgoing address */ | |
2efeb0b7 | 1564 | if (!l->aclList || ch.fastCheck(l->aclList) == ACCESS_ALLOWED) { |
cfd66529 AJ |
1565 | conn->local = l->addr; |
1566 | return; | |
1567 | } | |
1568 | } | |
b6b6f466 | 1569 | } |
62e76326 | 1570 | |
425de4c8 AJ |
1571 | tos_t |
1572 | GetTosToServer(HttpRequest * request) | |
1573 | { | |
1574 | ACLFilledChecklist ch(NULL, request, NULL); | |
425de4c8 AJ |
1575 | return aclMapTOS(Ip::Qos::TheConfig.tosToServer, &ch); |
1576 | } | |
1577 | ||
1578 | nfmark_t | |
1579 | GetNfmarkToServer(HttpRequest * request) | |
b6b6f466 | 1580 | { |
c0941a6a | 1581 | ACLFilledChecklist ch(NULL, request, NULL); |
425de4c8 | 1582 | return aclMapNfmark(Ip::Qos::TheConfig.nfmarkToServer, &ch); |
b6a2f15e | 1583 | } |