1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*
* DEBUG: section 66 HTTP Header Tools
* AUTHOR: Alex Rousskov
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from
* the Internet community; see the CONTRIBUTORS file for full
* details. Many organizations have provided support for Squid's
* development; see the SPONSORS file for full details. Squid is
* Copyrighted (C) 2001 by the Regents of the University of
* California; see the COPYRIGHT file for full details. Squid
* incorporates software developed and/or copyrighted by other
* sources; see the CREDITS file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "squid.h"
#include "globals.h"
#include "acl/FilledChecklist.h"
#include "acl/Gadgets.h"
#include "client_side_request.h"
#include "client_side.h"
#include "comm/Connection.h"
#include "compat/strtoll.h"
#include "ConfigParser.h"
#include "fde.h"
#include "HttpHdrContRange.h"
#include "HttpHeader.h"
#include "HttpHeaderFieldInfo.h"
#include "HttpHeaderTools.h"
#include "HttpRequest.h"
#include "MemBuf.h"
#include "SquidConfig.h"
#include "Store.h"
#include "StrList.h"
#if USE_SSL
#include "ssl/support.h"
#endif
#include <algorithm>
#include <string>
#if HAVE_ERRNO_H
#include <errno.h>
#endif
static void httpHeaderPutStrvf(HttpHeader * hdr, http_hdr_type id, const char *fmt, va_list vargs);
HttpHeaderFieldInfo *
httpHeaderBuildFieldsInfo(const HttpHeaderFieldAttrs * attrs, int count)
{
int i;
HttpHeaderFieldInfo *table = NULL;
assert(attrs && count);
/* allocate space */
table = new HttpHeaderFieldInfo[count];
for (i = 0; i < count; ++i) {
const http_hdr_type id = attrs[i].id;
HttpHeaderFieldInfo *info = table + id;
/* sanity checks */
assert(id >= 0 && id < count);
assert(attrs[i].name);
assert(info->id == HDR_ACCEPT && info->type == ftInvalid); /* was not set before */
/* copy and init fields */
info->id = id;
info->type = attrs[i].type;
info->name = attrs[i].name;
assert(info->name.size());
}
return table;
}
void
httpHeaderDestroyFieldsInfo(HttpHeaderFieldInfo * table, int count)
{
int i;
for (i = 0; i < count; ++i)
table[i].name.clean();
delete [] table;
}
void
httpHeaderMaskInit(HttpHeaderMask * mask, int value)
{
memset(mask, value, sizeof(*mask));
}
/** calculates a bit mask of a given array; does not reset mask! */
void
httpHeaderCalcMask(HttpHeaderMask * mask, http_hdr_type http_hdr_type_enums[], size_t count)
{
size_t i;
const int * enums = (const int *) http_hdr_type_enums;
assert(mask && enums);
assert(count < sizeof(*mask) * 8); /* check for overflow */
for (i = 0; i < count; ++i) {
assert(!CBIT_TEST(*mask, enums[i])); /* check for duplicates */
CBIT_SET(*mask, enums[i]);
}
}
/* same as httpHeaderPutStr, but formats the string using snprintf first */
void
httpHeaderPutStrf(HttpHeader * hdr, http_hdr_type id, const char *fmt,...)
{
va_list args;
va_start(args, fmt);
httpHeaderPutStrvf(hdr, id, fmt, args);
va_end(args);
}
/* used by httpHeaderPutStrf */
static void
httpHeaderPutStrvf(HttpHeader * hdr, http_hdr_type id, const char *fmt, va_list vargs)
{
MemBuf mb;
mb.init();
mb.vPrintf(fmt, vargs);
hdr->putStr(id, mb.buf);
mb.clean();
}
/** wrapper arrounf PutContRange */
void
httpHeaderAddContRange(HttpHeader * hdr, HttpHdrRangeSpec spec, int64_t ent_len)
{
HttpHdrContRange *cr = httpHdrContRangeCreate();
assert(hdr && ent_len >= 0);
httpHdrContRangeSet(cr, spec, ent_len);
hdr->putContRange(cr);
httpHdrContRangeDestroy(cr);
}
/**
* return true if a given directive is found in at least one of
* the "connection" header-fields note: if HDR_PROXY_CONNECTION is
* present we ignore HDR_CONNECTION.
*/
int
httpHeaderHasConnDir(const HttpHeader * hdr, const char *directive)
{
String list;
int res;
/* what type of header do we have? */
#if USE_HTTP_VIOLATIONS
if (hdr->has(HDR_PROXY_CONNECTION))
list = hdr->getList(HDR_PROXY_CONNECTION);
else
#endif
if (hdr->has(HDR_CONNECTION))
list = hdr->getList(HDR_CONNECTION);
else
return 0;
res = strListIsMember(&list, directive, ',');
list.clean();
return res;
}
/** handy to printf prefixes of potentially very long buffers */
const char *
getStringPrefix(const char *str, const char *end)
{
#define SHORT_PREFIX_SIZE 512
LOCAL_ARRAY(char, buf, SHORT_PREFIX_SIZE);
const int sz = 1 + (end ? end - str : strlen(str));
xstrncpy(buf, str, (sz > SHORT_PREFIX_SIZE) ? SHORT_PREFIX_SIZE : sz);
return buf;
}
/**
* parses an int field, complains if soemthing went wrong, returns true on
* success
*/
int
httpHeaderParseInt(const char *start, int *value)
{
assert(value);
*value = atoi(start);
if (!*value && !xisdigit(*start)) {
debugs(66, 2, "failed to parse an int header field near '" << start << "'");
return 0;
}
return 1;
}
int
httpHeaderParseOffset(const char *start, int64_t * value)
{
errno = 0;
int64_t res = strtoll(start, NULL, 10);
if (!res && EINVAL == errno) /* maybe not portable? */
return 0;
*value = res;
return 1;
}
/**
* Parses a quoted-string field (RFC 2616 section 2.2), complains if
* something went wrong, returns non-zero on success.
* Un-escapes quoted-pair characters found within the string.
* start should point at the first double-quote.
*/
int
httpHeaderParseQuotedString(const char *start, const int len, String *val)
{
const char *end, *pos;
val->clean();
if (*start != '"') {
debugs(66, 2, HERE << "failed to parse a quoted-string header field near '" << start << "'");
return 0;
}
pos = start + 1;
while (*pos != '"' && len > (pos-start)) {
if (*pos =='\r') {
++pos;
if ((pos-start) > len || *pos != '\n') {
debugs(66, 2, HERE << "failed to parse a quoted-string header field with '\\r' octet " << (start-pos)
<< " bytes into '" << start << "'");
val->clean();
return 0;
}
}
if (*pos == '\n') {
++pos;
if ( (pos-start) > len || (*pos != ' ' && *pos != '\t')) {
debugs(66, 2, HERE << "failed to parse multiline quoted-string header field '" << start << "'");
val->clean();
return 0;
}
// TODO: replace the entire LWS with a space
val->append(" ");
++pos;
debugs(66, 2, HERE << "len < pos-start => " << len << " < " << (pos-start));
continue;
}
bool quoted = (*pos == '\\');
if (quoted) {
++pos;
if (!*pos || (pos-start) > len) {
debugs(66, 2, HERE << "failed to parse a quoted-string header field near '" << start << "'");
val->clean();
return 0;
}
}
end = pos;
while (end < (start+len) && *end != '\\' && *end != '\"' && (unsigned char)*end > 0x1F && *end != 0x7F)
++end;
if (((unsigned char)*end <= 0x1F && *end != '\r' && *end != '\n') || *end == 0x7F) {
debugs(66, 2, HERE << "failed to parse a quoted-string header field with CTL octet " << (start-pos)
<< " bytes into '" << start << "'");
val->clean();
return 0;
}
val->append(pos, end-pos);
pos = end;
}
if (*pos != '\"') {
debugs(66, 2, HERE << "failed to parse a quoted-string header field which did not end with \" ");
val->clean();
return 0;
}
/* Make sure it's defined even if empty "" */
if (!val->defined())
val->limitInit("", 0);
return 1;
}
/**
* Checks the anonymizer (header_access) configuration.
*
* \retval 0 Header is explicitly blocked for removal
* \retval 1 Header is explicitly allowed
* \retval 1 Header has been replaced, the current version can be used.
* \retval 1 Header has no access controls to test
*/
static int
httpHdrMangle(HttpHeaderEntry * e, HttpRequest * request, int req_or_rep)
{
int retval;
/* check with anonymizer tables */
HeaderManglers *hms = NULL;
assert(e);
if (ROR_REQUEST == req_or_rep) {
hms = Config.request_header_access;
} else if (ROR_REPLY == req_or_rep) {
hms = Config.reply_header_access;
} else {
/* error. But let's call it "request". */
hms = Config.request_header_access;
}
/* manglers are not configured for this message kind */
if (!hms)
return 1;
const headerMangler *hm = hms->find(*e);
/* mangler or checklist went away. default allow */
if (!hm || !hm->access_list) {
return 1;
}
ACLFilledChecklist checklist(hm->access_list, request, NULL);
if (checklist.fastCheck() == ACCESS_ALLOWED) {
/* aclCheckFast returns true for allow. */
retval = 1;
} else if (NULL == hm->replacement) {
/* It was denied, and we don't have any replacement */
retval = 0;
} else {
/* It was denied, but we have a replacement. Replace the
* header on the fly, and return that the new header
* is allowed.
*/
e->value = hm->replacement;
retval = 1;
}
return retval;
}
/** Mangles headers for a list of headers. */
void
httpHdrMangleList(HttpHeader * l, HttpRequest * request, int req_or_rep)
{
HttpHeaderEntry *e;
HttpHeaderPos p = HttpHeaderInitPos;
int headers_deleted = 0;
while ((e = l->getEntry(&p)))
if (0 == httpHdrMangle(e, request, req_or_rep))
l->delAt(p, headers_deleted);
if (headers_deleted)
l->refreshMask();
}
static
void header_mangler_clean(headerMangler &m)
{
aclDestroyAccessList(&m.access_list);
safe_free(m.replacement);
}
static
void header_mangler_dump_access(StoreEntry * entry, const char *option,
const headerMangler &m, const char *name)
{
if (m.access_list != NULL) {
storeAppendPrintf(entry, "%s ", option);
dump_acl_access(entry, name, m.access_list);
}
}
static
void header_mangler_dump_replacement(StoreEntry * entry, const char *option,
const headerMangler &m, const char *name)
{
if (m.replacement)
storeAppendPrintf(entry, "%s %s %s\n", option, name, m.replacement);
}
HeaderManglers::HeaderManglers()
{
memset(known, 0, sizeof(known));
memset(&all, 0, sizeof(all));
}
HeaderManglers::~HeaderManglers()
{
for (int i = 0; i < HDR_ENUM_END; ++i)
header_mangler_clean(known[i]);
typedef ManglersByName::iterator MBNI;
for (MBNI i = custom.begin(); i != custom.end(); ++i)
header_mangler_clean(i->second);
header_mangler_clean(all);
}
void
HeaderManglers::dumpAccess(StoreEntry * entry, const char *name) const
{
for (int i = 0; i < HDR_ENUM_END; ++i) {
header_mangler_dump_access(entry, name, known[i],
httpHeaderNameById(i));
}
typedef ManglersByName::const_iterator MBNCI;
for (MBNCI i = custom.begin(); i != custom.end(); ++i)
header_mangler_dump_access(entry, name, i->second, i->first.c_str());
header_mangler_dump_access(entry, name, all, "All");
}
void
HeaderManglers::dumpReplacement(StoreEntry * entry, const char *name) const
{
for (int i = 0; i < HDR_ENUM_END; ++i) {
header_mangler_dump_replacement(entry, name, known[i],
httpHeaderNameById(i));
}
typedef ManglersByName::const_iterator MBNCI;
for (MBNCI i = custom.begin(); i != custom.end(); ++i) {
header_mangler_dump_replacement(entry, name, i->second,
i->first.c_str());
}
header_mangler_dump_replacement(entry, name, all, "All");
}
headerMangler *
HeaderManglers::track(const char *name)
{
int id = httpHeaderIdByNameDef(name, strlen(name));
if (id == HDR_BAD_HDR) { // special keyword or a custom header
if (strcmp(name, "All") == 0)
id = HDR_ENUM_END;
else if (strcmp(name, "Other") == 0)
id = HDR_OTHER;
}
headerMangler *m = NULL;
if (id == HDR_ENUM_END) {
m = &all;
} else if (id == HDR_BAD_HDR) {
m = &custom[name];
} else {
m = &known[id]; // including HDR_OTHER
}
assert(m);
return m;
}
void
HeaderManglers::setReplacement(const char *name, const char *value)
{
// for backword compatibility, we allow replacements to be configured
// for headers w/o access rules, but such replacements are ignored
headerMangler *m = track(name);
safe_free(m->replacement); // overwrite old value if any
m->replacement = xstrdup(value);
}
const headerMangler *
HeaderManglers::find(const HttpHeaderEntry &e) const
{
// a known header with a configured ACL list
if (e.id != HDR_OTHER && 0 <= e.id && e.id < HDR_ENUM_END &&
known[e.id].access_list)
return &known[e.id];
// a custom header
if (e.id == HDR_OTHER) {
// does it have an ACL list configured?
// Optimize: use a name type that we do not need to convert to here
const ManglersByName::const_iterator i = custom.find(e.name.termedBuf());
if (i != custom.end())
return &i->second;
}
// Next-to-last resort: "Other" rules match any custom header
if (e.id == HDR_OTHER && known[HDR_OTHER].access_list)
return &known[HDR_OTHER];
// Last resort: "All" rules match any header
if (all.access_list)
return &all;
return NULL;
}
void
httpHdrAdd(HttpHeader *heads, HttpRequest *request, const AccessLogEntryPointer &al, HeaderWithAclList &headersAdd)
{
ACLFilledChecklist checklist(NULL, request, NULL);
for (HeaderWithAclList::const_iterator hwa = headersAdd.begin(); hwa != headersAdd.end(); ++hwa) {
if (!hwa->aclList || checklist.fastCheck(hwa->aclList) == ACCESS_ALLOWED) {
const char *fieldValue = NULL;
MemBuf mb;
if (hwa->quoted) {
if (al != NULL) {
mb.init();
hwa->valueFormat->assemble(mb, al, 0);
fieldValue = mb.content();
}
} else {
fieldValue = hwa->fieldValue.c_str();
}
if (!fieldValue || fieldValue[0] == '\0')
fieldValue = "-";
HttpHeaderEntry *e = new HttpHeaderEntry(hwa->fieldId, hwa->fieldName.c_str(),
fieldValue);
heads->addEntry(e);
}
}
}
|