1/* 2 +----------------------------------------------------------------------+ 3 | Copyright (c) The PHP Group | 4 +----------------------------------------------------------------------+ 5 | This source file is subject to version 3.01 of the PHP license, | 6 | that is bundled with this package in the file LICENSE, and is | 7 | available through the world-wide-web at the following url: | 8 | https://www.php.net/license/3_01.txt | 9 | If you did not receive a copy of the PHP license and are unable to | 10 | obtain it through the world-wide-web, please send a note to | 11 | license@php.net so we can mail you a copy immediately. | 12 +----------------------------------------------------------------------+ 13 | Author: Sascha Schumann <sascha@schumann.cx> | 14 | Yasuo Ohgaki <yohgaki@ohgaki.net> | 15 +----------------------------------------------------------------------+ 16*/ 17 18#include "php.h" 19 20#ifdef HAVE_UNISTD_H 21#include <unistd.h> 22#endif 23 24#include <limits.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28 29#include "SAPI.h" 30#include "php_ini.h" 31#include "php_globals.h" 32#include "php_string.h" 33#define STATE_TAG SOME_OTHER_STATE_TAG 34#include "basic_functions.h" 35#include "url.h" 36#include "html.h" 37#undef STATE_TAG 38 39#define url_scanner url_scanner_ex 40 41#include "zend_smart_str.h" 42 43static void tag_dtor(zval *zv) 44{ 45 free(Z_PTR_P(zv)); 46} 47 48static int php_ini_on_update_tags(zend_ini_entry *entry, zend_string *new_value, void *mh_arg1, void *mh_arg2, void *mh_arg3, int stage, int type) 49{ 50 url_adapt_state_ex_t *ctx; 51 char *key; 52 char *tmp; 53 char *lasts = NULL; 54 55 if (type) { 56 ctx = &BG(url_adapt_session_ex); 57 } else { 58 ctx = &BG(url_adapt_output_ex); 59 } 60 61 tmp = estrndup(ZSTR_VAL(new_value), ZSTR_LEN(new_value)); 62 63 if (ctx->tags) 64 zend_hash_destroy(ctx->tags); 65 else { 66 ctx->tags = malloc(sizeof(HashTable)); 67 if (!ctx->tags) { 68 efree(tmp); 69 return FAILURE; 70 } 71 } 72 73 zend_hash_init(ctx->tags, 0, NULL, tag_dtor, 1); 74 75 for (key = php_strtok_r(tmp, ",", &lasts); 76 key; 77 key = php_strtok_r(NULL, ",", &lasts)) { 78 char *val; 79 80 val = strchr(key, '='); 81 if (val) { 82 char *q; 83 size_t keylen; 84 zend_string *str; 85 86 *val++ = '\0'; 87 for (q = key; *q; q++) { 88 *q = tolower(*q); 89 } 90 keylen = q - key; 91 str = zend_string_init(key, keylen, 1); 92 GC_MAKE_PERSISTENT_LOCAL(str); 93 zend_hash_add_mem(ctx->tags, str, val, strlen(val)+1); 94 zend_string_release_ex(str, 1); 95 } 96 } 97 98 efree(tmp); 99 100 return SUCCESS; 101} 102 103static PHP_INI_MH(OnUpdateSessionTags) 104{ 105 return php_ini_on_update_tags(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage, 1); 106} 107 108static PHP_INI_MH(OnUpdateOutputTags) 109{ 110 return php_ini_on_update_tags(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage, 0); 111} 112 113static int php_ini_on_update_hosts(zend_ini_entry *entry, zend_string *new_value, void *mh_arg1, void *mh_arg2, void *mh_arg3, int stage, int type) 114{ 115 HashTable *hosts; 116 char *key; 117 char *tmp; 118 char *lasts = NULL; 119 120 if (type) { 121 hosts = &BG(url_adapt_session_hosts_ht); 122 } else { 123 hosts = &BG(url_adapt_output_hosts_ht); 124 } 125 zend_hash_clean(hosts); 126 127 /* Use user supplied host whitelist */ 128 tmp = estrndup(ZSTR_VAL(new_value), ZSTR_LEN(new_value)); 129 for (key = php_strtok_r(tmp, ",", &lasts); 130 key; 131 key = php_strtok_r(NULL, ",", &lasts)) { 132 size_t keylen; 133 zend_string *tmp_key; 134 char *q; 135 136 for (q = key; *q; q++) { 137 *q = tolower(*q); 138 } 139 keylen = q - key; 140 if (keylen > 0) { 141 /* Note: the hash table is persistently allocated, so the strings must be too! */ 142 tmp_key = zend_string_init(key, keylen, true); 143 GC_MAKE_PERSISTENT_LOCAL(tmp_key); 144 zend_hash_add_empty_element(hosts, tmp_key); 145 zend_string_release_ex(tmp_key, true); 146 } 147 } 148 efree(tmp); 149 150 return SUCCESS; 151} 152 153static PHP_INI_MH(OnUpdateSessionHosts) 154{ 155 return php_ini_on_update_hosts(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage, 1); 156} 157 158static PHP_INI_MH(OnUpdateOutputHosts) 159{ 160 return php_ini_on_update_hosts(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage, 0); 161} 162 163/* FIXME: OnUpdate*Hosts cannot set default to $_SERVER['HTTP_HOST'] at startup */ 164PHP_INI_BEGIN() 165 STD_PHP_INI_ENTRY("session.trans_sid_tags", "a=href,area=href,frame=src,form=", PHP_INI_ALL, OnUpdateSessionTags, url_adapt_session_ex, php_basic_globals, basic_globals) 166 STD_PHP_INI_ENTRY("session.trans_sid_hosts", "", PHP_INI_ALL, OnUpdateSessionHosts, url_adapt_session_hosts_ht, php_basic_globals, basic_globals) 167 STD_PHP_INI_ENTRY("url_rewriter.tags", "form=", PHP_INI_ALL, OnUpdateOutputTags, url_adapt_session_ex, php_basic_globals, basic_globals) 168 STD_PHP_INI_ENTRY("url_rewriter.hosts", "", PHP_INI_ALL, OnUpdateOutputHosts, url_adapt_session_hosts_ht, php_basic_globals, basic_globals) 169PHP_INI_END() 170 171/*!re2c 172any = [\000-\377]; 173N = (any\[<]); 174alpha = [a-zA-Z]; 175alphanamespace = [a-zA-Z:]; 176alphadash = ([a-zA-Z] | "-"); 177*/ 178 179#define YYFILL(n) goto done 180#define YYCTYPE unsigned char 181#define YYCURSOR p 182#define YYLIMIT q 183#define YYMARKER r 184 185static inline void append_modified_url(smart_str *url, smart_str *dest, smart_str *url_app, const char *separator, int type) 186{ 187 php_url *url_parts; 188 189 smart_str_0(url); /* FIXME: Bug #70480 php_url_parse_ex() crashes by processing chars exceed len */ 190 url_parts = php_url_parse_ex(ZSTR_VAL(url->s), ZSTR_LEN(url->s)); 191 192 /* Ignore malformed URLs */ 193 if (!url_parts) { 194 smart_str_append_smart_str(dest, url); 195 return; 196 } 197 198 /* Don't modify URLs of the format "#mark" */ 199 if (url_parts->fragment && '#' == ZSTR_VAL(url->s)[0]) { 200 smart_str_append_smart_str(dest, url); 201 php_url_free(url_parts); 202 return; 203 } 204 205 /* Check protocol. Only http/https is allowed. */ 206 if (url_parts->scheme 207 && !zend_string_equals_literal_ci(url_parts->scheme, "http") 208 && !zend_string_equals_literal_ci(url_parts->scheme, "https")) { 209 smart_str_append_smart_str(dest, url); 210 php_url_free(url_parts); 211 return; 212 } 213 214 /* Check host whitelist. If it's not listed, do nothing. */ 215 if (url_parts->host) { 216 zend_string *tmp = zend_string_tolower(url_parts->host); 217 HashTable *allowed_hosts = type ? &BG(url_adapt_session_hosts_ht) : &BG(url_adapt_output_hosts_ht); 218 if (!zend_hash_exists(allowed_hosts, tmp)) { 219 zend_string_release_ex(tmp, 0); 220 smart_str_append_smart_str(dest, url); 221 php_url_free(url_parts); 222 return; 223 } 224 zend_string_release_ex(tmp, 0); 225 } 226 227 /* 228 * When URL does not have path and query string add "/?". 229 * i.e. If URL is only "?foo=bar", should not add "/?". 230 */ 231 if (!url_parts->path && !url_parts->query && !url_parts->fragment) { 232 /* URL is http://php.net or like */ 233 smart_str_append_smart_str(dest, url); 234 smart_str_appendc(dest, '/'); 235 smart_str_appendc(dest, '?'); 236 smart_str_append_smart_str(dest, url_app); 237 php_url_free(url_parts); 238 return; 239 } 240 241 if (url_parts->scheme) { 242 smart_str_appends(dest, ZSTR_VAL(url_parts->scheme)); 243 smart_str_appends(dest, "://"); 244 } else if (*(ZSTR_VAL(url->s)) == '/' && *(ZSTR_VAL(url->s)+1) == '/') { 245 smart_str_appends(dest, "//"); 246 } 247 if (url_parts->user) { 248 smart_str_appends(dest, ZSTR_VAL(url_parts->user)); 249 if (url_parts->pass) { 250 smart_str_appends(dest, ZSTR_VAL(url_parts->pass)); 251 smart_str_appendc(dest, ':'); 252 } 253 smart_str_appendc(dest, '@'); 254 } 255 if (url_parts->host) { 256 smart_str_appends(dest, ZSTR_VAL(url_parts->host)); 257 } 258 if (url_parts->port) { 259 smart_str_appendc(dest, ':'); 260 smart_str_append_unsigned(dest, (long)url_parts->port); 261 } 262 if (url_parts->path) { 263 smart_str_appends(dest, ZSTR_VAL(url_parts->path)); 264 } 265 smart_str_appendc(dest, '?'); 266 if (url_parts->query) { 267 smart_str_appends(dest, ZSTR_VAL(url_parts->query)); 268 smart_str_appends(dest, separator); 269 smart_str_append_smart_str(dest, url_app); 270 } else { 271 smart_str_append_smart_str(dest, url_app); 272 } 273 if (url_parts->fragment) { 274 smart_str_appendc(dest, '#'); 275 smart_str_appends(dest, ZSTR_VAL(url_parts->fragment)); 276 } 277 php_url_free(url_parts); 278} 279 280enum { 281 TAG_NORMAL = 0, 282 TAG_FORM 283}; 284 285enum { 286 ATTR_NORMAL = 0, 287 ATTR_ACTION 288}; 289 290#undef YYFILL 291#undef YYCTYPE 292#undef YYCURSOR 293#undef YYLIMIT 294#undef YYMARKER 295 296static inline void tag_arg(url_adapt_state_ex_t *ctx, char quotes, char type) 297{ 298 char f = 0; 299 300 /* arg.s is string WITHOUT NUL. 301 To avoid partial match, NUL is added here */ 302 ZSTR_VAL(ctx->arg.s)[ZSTR_LEN(ctx->arg.s)] = '\0'; 303 if (!strcasecmp(ZSTR_VAL(ctx->arg.s), ctx->lookup_data)) { 304 f = 1; 305 } 306 307 if (quotes) { 308 smart_str_appendc(&ctx->result, type); 309 } 310 if (f) { 311 append_modified_url(&ctx->val, &ctx->result, &ctx->url_app, PG(arg_separator).output, ctx->type); 312 } else { 313 smart_str_append_smart_str(&ctx->result, &ctx->val); 314 } 315 if (quotes) { 316 smart_str_appendc(&ctx->result, type); 317 } 318} 319 320enum { 321 STATE_PLAIN = 0, 322 STATE_TAG, 323 STATE_NEXT_ARG, 324 STATE_ARG, 325 STATE_BEFORE_VAL, 326 STATE_VAL 327}; 328 329#define YYFILL(n) goto stop 330#define YYCTYPE unsigned char 331#define YYCURSOR xp 332#define YYLIMIT end 333#define YYMARKER q 334#define STATE ctx->state 335 336#define STD_PARA url_adapt_state_ex_t *ctx, char *start, char *YYCURSOR 337#define STD_ARGS ctx, start, xp 338 339#ifdef SCANNER_DEBUG 340#define scdebug(x) printf x 341#else 342#define scdebug(x) 343#endif 344 345static inline void passthru(STD_PARA) 346{ 347 scdebug(("appending %d chars, starting with %c\n", YYCURSOR-start, *start)); 348 smart_str_appendl(&ctx->result, start, YYCURSOR - start); 349} 350 351 352static int check_http_host(char *target) 353{ 354 zval *host, *tmp; 355 zend_string *host_tmp; 356 char *colon; 357 358 if ((tmp = zend_hash_find(&EG(symbol_table), ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_SERVER))) && 359 Z_TYPE_P(tmp) == IS_ARRAY && 360 (host = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("HTTP_HOST"))) && 361 Z_TYPE_P(host) == IS_STRING) { 362 host_tmp = zend_string_init(Z_STRVAL_P(host), Z_STRLEN_P(host), 0); 363 /* HTTP_HOST could be 'localhost:8888' etc. */ 364 colon = strchr(ZSTR_VAL(host_tmp), ':'); 365 if (colon) { 366 ZSTR_LEN(host_tmp) = colon - ZSTR_VAL(host_tmp); 367 ZSTR_VAL(host_tmp)[ZSTR_LEN(host_tmp)] = '\0'; 368 } 369 if (!strcasecmp(ZSTR_VAL(host_tmp), target)) { 370 zend_string_release_ex(host_tmp, 0); 371 return SUCCESS; 372 } 373 zend_string_release_ex(host_tmp, 0); 374 } 375 return FAILURE; 376} 377 378static int check_host_whitelist(url_adapt_state_ex_t *ctx) 379{ 380 php_url *url_parts = NULL; 381 HashTable *allowed_hosts = ctx->type ? &BG(url_adapt_session_hosts_ht) : &BG(url_adapt_output_hosts_ht); 382 383 ZEND_ASSERT(ctx->tag_type == TAG_FORM); 384 385 if (ctx->attr_val.s && ZSTR_LEN(ctx->attr_val.s)) { 386 url_parts = php_url_parse_ex(ZSTR_VAL(ctx->attr_val.s), ZSTR_LEN(ctx->attr_val.s)); 387 } else { 388 return SUCCESS; /* empty URL is valid */ 389 } 390 391 if (!url_parts) { 392 return FAILURE; 393 } 394 if (url_parts->scheme) { 395 /* Only http/https should be handled. 396 A bit hacky check this here, but saves a URL parse. */ 397 if (!zend_string_equals_literal_ci(url_parts->scheme, "http") && 398 !zend_string_equals_literal_ci(url_parts->scheme, "https")) { 399 php_url_free(url_parts); 400 return FAILURE; 401 } 402 } 403 if (!url_parts->host) { 404 php_url_free(url_parts); 405 return SUCCESS; 406 } 407 if (!zend_hash_num_elements(allowed_hosts) && 408 check_http_host(ZSTR_VAL(url_parts->host)) == SUCCESS) { 409 php_url_free(url_parts); 410 return SUCCESS; 411 } 412 if (!zend_hash_find(allowed_hosts, url_parts->host)) { 413 php_url_free(url_parts); 414 return FAILURE; 415 } 416 php_url_free(url_parts); 417 return SUCCESS; 418} 419 420/* 421 * This function appends a hidden input field after a <form>. 422 */ 423static void handle_form(STD_PARA) 424{ 425 int doit = 0; 426 427 if (ZSTR_LEN(ctx->form_app.s) > 0) { 428 switch (ZSTR_LEN(ctx->tag.s)) { 429 case sizeof("form") - 1: 430 if (!strncasecmp(ZSTR_VAL(ctx->tag.s), "form", ZSTR_LEN(ctx->tag.s)) 431 && check_host_whitelist(ctx) == SUCCESS) { 432 doit = 1; 433 } 434 break; 435 } 436 } 437 438 if (doit) { 439 smart_str_append_smart_str(&ctx->result, &ctx->form_app); 440 } 441} 442 443/* 444 * HANDLE_TAG copies the HTML Tag and checks whether we 445 * have that tag in our table. If we might modify it, 446 * we continue to scan the tag, otherwise we simply copy the complete 447 * HTML stuff to the result buffer. 448 */ 449 450static inline void handle_tag(STD_PARA) 451{ 452 int ok = 0; 453 unsigned int i; 454 455 if (ctx->tag.s) { 456 ZSTR_LEN(ctx->tag.s) = 0; 457 } 458 smart_str_appendl(&ctx->tag, start, YYCURSOR - start); 459 for (i = 0; i < ZSTR_LEN(ctx->tag.s); i++) 460 ZSTR_VAL(ctx->tag.s)[i] = tolower((int)(unsigned char)ZSTR_VAL(ctx->tag.s)[i]); 461 /* intentionally using str_find here, in case the hash value is set, but the string val is changed later */ 462 if ((ctx->lookup_data = zend_hash_str_find_ptr(ctx->tags, ZSTR_VAL(ctx->tag.s), ZSTR_LEN(ctx->tag.s))) != NULL) { 463 ok = 1; 464 if (ZSTR_LEN(ctx->tag.s) == sizeof("form")-1 465 && !strncasecmp(ZSTR_VAL(ctx->tag.s), "form", ZSTR_LEN(ctx->tag.s))) { 466 ctx->tag_type = TAG_FORM; 467 } else { 468 ctx->tag_type = TAG_NORMAL; 469 } 470 } 471 STATE = ok ? STATE_NEXT_ARG : STATE_PLAIN; 472} 473 474static inline void handle_arg(STD_PARA) 475{ 476 if (ctx->arg.s) { 477 ZSTR_LEN(ctx->arg.s) = 0; 478 } 479 smart_str_appendl(&ctx->arg, start, YYCURSOR - start); 480 if (ctx->tag_type == TAG_FORM && 481 strncasecmp(ZSTR_VAL(ctx->arg.s), "action", ZSTR_LEN(ctx->arg.s)) == 0) { 482 ctx->attr_type = ATTR_ACTION; 483 } else { 484 ctx->attr_type = ATTR_NORMAL; 485 } 486} 487 488static inline void handle_val(STD_PARA, char quotes, char type) 489{ 490 smart_str_setl(&ctx->val, start + quotes, YYCURSOR - start - quotes * 2); 491 if (ctx->tag_type == TAG_FORM && ctx->attr_type == ATTR_ACTION) { 492 smart_str_setl(&ctx->attr_val, start + quotes, YYCURSOR - start - quotes * 2); 493 } 494 tag_arg(ctx, quotes, type); 495} 496 497static inline void xx_mainloop(url_adapt_state_ex_t *ctx, const char *newdata, size_t newlen) 498{ 499 char *end, *q; 500 char *xp; 501 char *start; 502 size_t rest; 503 504 smart_str_appendl(&ctx->buf, newdata, newlen); 505 506 YYCURSOR = ZSTR_VAL(ctx->buf.s); 507 YYLIMIT = ZSTR_VAL(ctx->buf.s) + ZSTR_LEN(ctx->buf.s); 508 509 switch (STATE) { 510 case STATE_PLAIN: goto state_plain; 511 case STATE_TAG: goto state_tag; 512 case STATE_NEXT_ARG: goto state_next_arg; 513 case STATE_ARG: goto state_arg; 514 case STATE_BEFORE_VAL: goto state_before_val; 515 case STATE_VAL: goto state_val; 516 } 517 518 519state_plain_begin: 520 STATE = STATE_PLAIN; 521 522state_plain: 523 start = YYCURSOR; 524/*!re2c 525 "<" { passthru(STD_ARGS); STATE = STATE_TAG; goto state_tag; } 526 N+ { passthru(STD_ARGS); goto state_plain; } 527*/ 528 529state_tag: 530 start = YYCURSOR; 531/*!re2c 532 alphanamespace+ { handle_tag(STD_ARGS); /* Sets STATE */; passthru(STD_ARGS); if (STATE == STATE_PLAIN) goto state_plain; else goto state_next_arg; } 533 any { passthru(STD_ARGS); goto state_plain_begin; } 534*/ 535 536state_next_arg_begin: 537 STATE = STATE_NEXT_ARG; 538 539state_next_arg: 540 start = YYCURSOR; 541/*!re2c 542 [/]? [>] { passthru(STD_ARGS); handle_form(STD_ARGS); goto state_plain_begin; } 543 [ \v\r\t\n]+ { passthru(STD_ARGS); goto state_next_arg; } 544 alpha { --YYCURSOR; STATE = STATE_ARG; goto state_arg; } 545 any { passthru(STD_ARGS); goto state_plain_begin; } 546*/ 547 548state_arg: 549 start = YYCURSOR; 550/*!re2c 551 alpha alphadash* { passthru(STD_ARGS); handle_arg(STD_ARGS); STATE = STATE_BEFORE_VAL; goto state_before_val; } 552 any { passthru(STD_ARGS); STATE = STATE_NEXT_ARG; goto state_next_arg; } 553*/ 554 555state_before_val: 556 start = YYCURSOR; 557/*!re2c 558 [ ]* "=" [ ]* { passthru(STD_ARGS); STATE = STATE_VAL; goto state_val; } 559 any { --YYCURSOR; goto state_next_arg_begin; } 560*/ 561 562 563state_val: 564 start = YYCURSOR; 565/*!re2c 566 ["] (any\[">])* ["] { handle_val(STD_ARGS, 1, '"'); goto state_next_arg_begin; } 567 ['] (any\['>])* ['] { handle_val(STD_ARGS, 1, '\''); goto state_next_arg_begin; } 568 (any\[ \r\t\n>'"])+ { handle_val(STD_ARGS, 0, ' '); goto state_next_arg_begin; } 569 any { passthru(STD_ARGS); goto state_next_arg_begin; } 570*/ 571 572stop: 573 if (YYLIMIT < start) { 574 /* XXX: Crash avoidance. Need to work with reporter to figure out what goes wrong */ 575 rest = 0; 576 } else { 577 rest = YYLIMIT - start; 578 scdebug(("stopped in state %d at pos %d (%d:%c) %d\n", STATE, YYCURSOR - ctx->buf.c, *YYCURSOR, *YYCURSOR, rest)); 579 } 580 581 if (rest) memmove(ZSTR_VAL(ctx->buf.s), start, rest); 582 ZSTR_LEN(ctx->buf.s) = rest; 583} 584 585 586PHPAPI char *php_url_scanner_adapt_single_url(const char *url, size_t urllen, const char *name, const char *value, size_t *newlen, int encode) 587{ 588 char *result; 589 smart_str surl = {0}; 590 smart_str buf = {0}; 591 smart_str url_app = {0}; 592 zend_string *encoded; 593 594 smart_str_appendl(&surl, url, urllen); 595 596 if (encode) { 597 encoded = php_raw_url_encode(name, strlen(name)); 598 smart_str_appendl(&url_app, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); 599 zend_string_free(encoded); 600 } else { 601 smart_str_appends(&url_app, name); 602 } 603 smart_str_appendc(&url_app, '='); 604 if (encode) { 605 encoded = php_raw_url_encode(value, strlen(value)); 606 smart_str_appendl(&url_app, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); 607 zend_string_free(encoded); 608 } else { 609 smart_str_appends(&url_app, value); 610 } 611 612 append_modified_url(&surl, &buf, &url_app, PG(arg_separator).output, 1); 613 614 smart_str_0(&buf); 615 if (newlen) *newlen = ZSTR_LEN(buf.s); 616 result = estrndup(ZSTR_VAL(buf.s), ZSTR_LEN(buf.s)); 617 618 smart_str_free(&url_app); 619 smart_str_free(&buf); 620 621 return result; 622} 623 624 625static char *url_adapt_ext(const char *src, size_t srclen, size_t *newlen, bool do_flush, url_adapt_state_ex_t *ctx) 626{ 627 char *retval; 628 629 xx_mainloop(ctx, src, srclen); 630 631 if (!ctx->result.s) { 632 smart_str_appendl(&ctx->result, "", 0); 633 *newlen = 0; 634 } else { 635 *newlen = ZSTR_LEN(ctx->result.s); 636 } 637 smart_str_0(&ctx->result); 638 if (do_flush) { 639 smart_str_append(&ctx->result, ctx->buf.s); 640 *newlen += ZSTR_LEN(ctx->buf.s); 641 smart_str_free(&ctx->buf); 642 smart_str_free(&ctx->val); 643 smart_str_free(&ctx->attr_val); 644 } 645 retval = estrndup(ZSTR_VAL(ctx->result.s), ZSTR_LEN(ctx->result.s)); 646 smart_str_free(&ctx->result); 647 return retval; 648} 649 650static int php_url_scanner_ex_activate(int type) 651{ 652 url_adapt_state_ex_t *ctx; 653 654 if (type) { 655 ctx = &BG(url_adapt_session_ex); 656 } else { 657 ctx = &BG(url_adapt_output_ex); 658 } 659 660 memset(ctx, 0, XtOffsetOf(url_adapt_state_ex_t, tags)); 661 662 return SUCCESS; 663} 664 665static int php_url_scanner_ex_deactivate(int type) 666{ 667 url_adapt_state_ex_t *ctx; 668 669 if (type) { 670 ctx = &BG(url_adapt_session_ex); 671 } else { 672 ctx = &BG(url_adapt_output_ex); 673 } 674 675 smart_str_free(&ctx->result); 676 smart_str_free(&ctx->buf); 677 smart_str_free(&ctx->tag); 678 smart_str_free(&ctx->arg); 679 smart_str_free(&ctx->attr_val); 680 681 return SUCCESS; 682} 683 684static inline void php_url_scanner_session_handler_impl(char *output, size_t output_len, char **handled_output, size_t *handled_output_len, int mode, int type) 685{ 686 size_t len; 687 url_adapt_state_ex_t *url_state; 688 689 if (type) { 690 url_state = &BG(url_adapt_session_ex); 691 } else { 692 url_state = &BG(url_adapt_output_ex); 693 } 694 695 if (ZSTR_LEN(url_state->url_app.s) != 0) { 696 *handled_output = url_adapt_ext(output, output_len, &len, (bool) (mode & (PHP_OUTPUT_HANDLER_END | PHP_OUTPUT_HANDLER_CONT | PHP_OUTPUT_HANDLER_FLUSH | PHP_OUTPUT_HANDLER_FINAL) ? 1 : 0), url_state); 697 if (sizeof(unsigned int) < sizeof(size_t)) { 698 if (len > UINT_MAX) 699 len = UINT_MAX; 700 } 701 *handled_output_len = len; 702 } else if (ZSTR_LEN(url_state->url_app.s) == 0) { 703 url_adapt_state_ex_t *ctx = url_state; 704 if (ctx->buf.s && ZSTR_LEN(ctx->buf.s)) { 705 smart_str_append(&ctx->result, ctx->buf.s); 706 smart_str_appendl(&ctx->result, output, output_len); 707 708 *handled_output = estrndup(ZSTR_VAL(ctx->result.s), ZSTR_LEN(ctx->result.s)); 709 *handled_output_len = ZSTR_LEN(ctx->buf.s) + output_len; 710 711 smart_str_free(&ctx->buf); 712 smart_str_free(&ctx->result); 713 } else { 714 *handled_output = estrndup(output, *handled_output_len = output_len); 715 } 716 } else { 717 *handled_output = NULL; 718 } 719} 720 721static void php_url_scanner_session_handler(char *output, size_t output_len, char **handled_output, size_t *handled_output_len, int mode) 722{ 723 php_url_scanner_session_handler_impl(output, output_len, handled_output, handled_output_len, mode, 1); 724} 725 726static void php_url_scanner_output_handler(char *output, size_t output_len, char **handled_output, size_t *handled_output_len, int mode) 727{ 728 php_url_scanner_session_handler_impl(output, output_len, handled_output, handled_output_len, mode, 0); 729} 730 731static inline int php_url_scanner_add_var_impl(const char *name, size_t name_len, const char *value, size_t value_len, int encode, int type) 732{ 733 smart_str sname = {0}; 734 smart_str svalue = {0}; 735 smart_str hname = {0}; 736 smart_str hvalue = {0}; 737 zend_string *encoded; 738 url_adapt_state_ex_t *url_state; 739 php_output_handler_func_t handler; 740 741 if (type) { 742 url_state = &BG(url_adapt_session_ex); 743 handler = php_url_scanner_session_handler; 744 } else { 745 url_state = &BG(url_adapt_output_ex); 746 handler = php_url_scanner_output_handler; 747 } 748 749 if (!url_state->active) { 750 php_url_scanner_ex_activate(type); 751 php_output_start_internal(ZEND_STRL("URL-Rewriter"), handler, 0, PHP_OUTPUT_HANDLER_STDFLAGS); 752 url_state->active = 1; 753 url_state->type = type; 754 } 755 756 if (url_state->url_app.s && ZSTR_LEN(url_state->url_app.s) != 0) { 757 smart_str_appends(&url_state->url_app, PG(arg_separator).output); 758 } 759 760 if (encode) { 761 encoded = php_raw_url_encode(name, name_len); 762 smart_str_appendl(&sname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); 763 encoded = php_raw_url_encode(value, value_len); 764 smart_str_appendl(&svalue, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); 765 encoded = php_escape_html_entities_ex((const unsigned char *) name, name_len, 0, ENT_QUOTES|ENT_SUBSTITUTE, NULL, /* double_encode */ 0, /* quiet */ 1); 766 smart_str_appendl(&hname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); 767 encoded = php_escape_html_entities_ex((const unsigned char *) value, value_len, 0, ENT_QUOTES|ENT_SUBSTITUTE, NULL, /* double_encode */ 0, /* quiet */ 1); 768 smart_str_appendl(&hvalue, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); zend_string_free(encoded); 769 } else { 770 smart_str_appendl(&sname, name, name_len); 771 smart_str_appendl(&svalue, value, value_len); 772 smart_str_appendl(&hname, name, name_len); 773 smart_str_appendl(&hvalue, value, value_len); 774 } 775 776 smart_str_append_smart_str(&url_state->url_app, &sname); 777 smart_str_appendc(&url_state->url_app, '='); 778 smart_str_append_smart_str(&url_state->url_app, &svalue); 779 780 smart_str_appends(&url_state->form_app, "<input type=\"hidden\" name=\""); 781 smart_str_append_smart_str(&url_state->form_app, &hname); 782 smart_str_appends(&url_state->form_app, "\" value=\""); 783 smart_str_append_smart_str(&url_state->form_app, &hvalue); 784 smart_str_appends(&url_state->form_app, "\" />"); 785 786 smart_str_free(&sname); 787 smart_str_free(&svalue); 788 smart_str_free(&hname); 789 smart_str_free(&hvalue); 790 791 return SUCCESS; 792} 793 794 795PHPAPI int php_url_scanner_add_session_var(const char *name, size_t name_len, const char *value, size_t value_len, int encode) 796{ 797 return php_url_scanner_add_var_impl(name, name_len, value, value_len, encode, 1); 798} 799 800 801PHPAPI int php_url_scanner_add_var(const char *name, size_t name_len, const char *value, size_t value_len, int encode) 802{ 803 return php_url_scanner_add_var_impl(name, name_len, value, value_len, encode, 0); 804} 805 806 807static inline void php_url_scanner_reset_vars_impl(int type) { 808 url_adapt_state_ex_t *url_state; 809 810 if (type) { 811 url_state = &BG(url_adapt_session_ex); 812 } else { 813 url_state = &BG(url_adapt_output_ex); 814 } 815 816 if (url_state->form_app.s) { 817 ZSTR_LEN(url_state->form_app.s) = 0; 818 } 819 if (url_state->url_app.s) { 820 ZSTR_LEN(url_state->url_app.s) = 0; 821 } 822} 823 824 825PHPAPI int php_url_scanner_reset_session_vars(void) 826{ 827 php_url_scanner_reset_vars_impl(1); 828 return SUCCESS; 829} 830 831 832PHPAPI int php_url_scanner_reset_vars(void) 833{ 834 php_url_scanner_reset_vars_impl(0); 835 return SUCCESS; 836} 837 838 839static inline int php_url_scanner_reset_var_impl(zend_string *name, int encode, int type) 840{ 841 char *start, *end, *limit; 842 size_t separator_len; 843 smart_str sname = {0}; 844 smart_str hname = {0}; 845 smart_str url_app = {0}; 846 smart_str form_app = {0}; 847 zend_string *encoded; 848 int ret = SUCCESS; 849 bool sep_removed = 0; 850 url_adapt_state_ex_t *url_state; 851 852 if (type) { 853 url_state = &BG(url_adapt_session_ex); 854 } else { 855 url_state = &BG(url_adapt_output_ex); 856 } 857 858 /* Short circuit check. Only check url_app. */ 859 if (!url_state->url_app.s || !ZSTR_LEN(url_state->url_app.s)) { 860 return SUCCESS; 861 } 862 863 if (encode) { 864 encoded = php_raw_url_encode(ZSTR_VAL(name), ZSTR_LEN(name)); 865 smart_str_appendl(&sname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); 866 zend_string_free(encoded); 867 encoded = php_escape_html_entities_ex((const unsigned char *) ZSTR_VAL(name), ZSTR_LEN(name), 0, ENT_QUOTES|ENT_SUBSTITUTE, SG(default_charset), /* double_encode */ 0, /* quiet */ 1); 868 smart_str_appendl(&hname, ZSTR_VAL(encoded), ZSTR_LEN(encoded)); 869 zend_string_free(encoded); 870 } else { 871 smart_str_appendl(&sname, ZSTR_VAL(name), ZSTR_LEN(name)); 872 smart_str_appendl(&hname, ZSTR_VAL(name), ZSTR_LEN(name)); 873 } 874 smart_str_0(&sname); 875 smart_str_0(&hname); 876 877 smart_str_append_smart_str(&url_app, &sname); 878 smart_str_appendc(&url_app, '='); 879 smart_str_0(&url_app); 880 881 smart_str_appends(&form_app, "<input type=\"hidden\" name=\""); 882 smart_str_append_smart_str(&form_app, &hname); 883 smart_str_appends(&form_app, "\" value=\""); 884 smart_str_0(&form_app); 885 886 /* Short circuit check. Only check url_app. */ 887 start = (char *) php_memnstr(ZSTR_VAL(url_state->url_app.s), 888 ZSTR_VAL(url_app.s), ZSTR_LEN(url_app.s), 889 ZSTR_VAL(url_state->url_app.s) + ZSTR_LEN(url_state->url_app.s)); 890 if (!start) { 891 ret = FAILURE; 892 goto finish; 893 } 894 895 /* Get end of url var */ 896 limit = ZSTR_VAL(url_state->url_app.s) + ZSTR_LEN(url_state->url_app.s); 897 end = start + ZSTR_LEN(url_app.s); 898 separator_len = strlen(PG(arg_separator).output); 899 while (end < limit) { 900 if (!memcmp(end, PG(arg_separator).output, separator_len)) { 901 end += separator_len; 902 sep_removed = 1; 903 break; 904 } 905 end++; 906 } 907 /* Remove all when this is the only rewrite var */ 908 if (ZSTR_LEN(url_state->url_app.s) == end - start) { 909 php_url_scanner_reset_vars_impl(type); 910 goto finish; 911 } 912 /* Check preceding separator */ 913 if (!sep_removed 914 && (size_t)(start - PG(arg_separator).output) >= separator_len 915 && !memcmp(start - separator_len, PG(arg_separator).output, separator_len)) { 916 start -= separator_len; 917 } 918 /* Remove partially */ 919 memmove(start, end, 920 ZSTR_LEN(url_state->url_app.s) - (end - ZSTR_VAL(url_state->url_app.s))); 921 ZSTR_LEN(url_state->url_app.s) -= end - start; 922 ZSTR_VAL(url_state->url_app.s)[ZSTR_LEN(url_state->url_app.s)] = '\0'; 923 924 /* Remove form var */ 925 start = (char *) php_memnstr(ZSTR_VAL(url_state->form_app.s), 926 ZSTR_VAL(form_app.s), ZSTR_LEN(form_app.s), 927 ZSTR_VAL(url_state->form_app.s) + ZSTR_LEN(url_state->form_app.s)); 928 if (!start) { 929 /* Should not happen */ 930 ret = FAILURE; 931 php_url_scanner_reset_vars_impl(type); 932 goto finish; 933 } 934 /* Get end of form var */ 935 limit = ZSTR_VAL(url_state->form_app.s) + ZSTR_LEN(url_state->form_app.s); 936 end = start + ZSTR_LEN(form_app.s); 937 while (end < limit) { 938 if (*end == '>') { 939 end += 1; 940 break; 941 } 942 end++; 943 } 944 /* Remove partially */ 945 memmove(start, end, 946 ZSTR_LEN(url_state->form_app.s) - (end - ZSTR_VAL(url_state->form_app.s))); 947 ZSTR_LEN(url_state->form_app.s) -= end - start; 948 ZSTR_VAL(url_state->form_app.s)[ZSTR_LEN(url_state->form_app.s)] = '\0'; 949 950finish: 951 smart_str_free(&url_app); 952 smart_str_free(&form_app); 953 smart_str_free(&sname); 954 smart_str_free(&hname); 955 return ret; 956} 957 958 959PHPAPI int php_url_scanner_reset_session_var(zend_string *name, int encode) 960{ 961 return php_url_scanner_reset_var_impl(name, encode, 1); 962} 963 964 965PHPAPI int php_url_scanner_reset_var(zend_string *name, int encode) 966{ 967 return php_url_scanner_reset_var_impl(name, encode, 0); 968} 969 970 971PHP_MINIT_FUNCTION(url_scanner) 972{ 973 REGISTER_INI_ENTRIES(); 974 return SUCCESS; 975} 976 977PHP_MSHUTDOWN_FUNCTION(url_scanner) 978{ 979 UNREGISTER_INI_ENTRIES(); 980 981 return SUCCESS; 982} 983 984PHP_RINIT_FUNCTION(url_scanner) 985{ 986 BG(url_adapt_session_ex).active = 0; 987 BG(url_adapt_session_ex).tag_type = 0; 988 BG(url_adapt_session_ex).attr_type = 0; 989 BG(url_adapt_output_ex).active = 0; 990 BG(url_adapt_output_ex).tag_type = 0; 991 BG(url_adapt_output_ex).attr_type = 0; 992 return SUCCESS; 993} 994 995PHP_RSHUTDOWN_FUNCTION(url_scanner) 996{ 997 if (BG(url_adapt_session_ex).active) { 998 php_url_scanner_ex_deactivate(1); 999 BG(url_adapt_session_ex).active = 0; 1000 BG(url_adapt_session_ex).tag_type = 0; 1001 BG(url_adapt_session_ex).attr_type = 0; 1002 } 1003 smart_str_free(&BG(url_adapt_session_ex).form_app); 1004 smart_str_free(&BG(url_adapt_session_ex).url_app); 1005 1006 if (BG(url_adapt_output_ex).active) { 1007 php_url_scanner_ex_deactivate(0); 1008 BG(url_adapt_output_ex).active = 0; 1009 BG(url_adapt_output_ex).tag_type = 0; 1010 BG(url_adapt_output_ex).attr_type = 0; 1011 } 1012 smart_str_free(&BG(url_adapt_output_ex).form_app); 1013 smart_str_free(&BG(url_adapt_output_ex).url_app); 1014 1015 return SUCCESS; 1016} 1017