PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/opcache - zend_accelerator_blacklist.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 12 168 7.1 %
Date: 2014-10-22 Functions: 2 9 22.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    |          Stanislav Malyshev <stas@zend.com>                          |
      18             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      19             :    +----------------------------------------------------------------------+
      20             : */
      21             : 
      22             : #include "main/php.h"
      23             : #include "main/fopen_wrappers.h"
      24             : #include "ZendAccelerator.h"
      25             : #include "zend_accelerator_blacklist.h"
      26             : #include "ext/ereg/php_regex.h"
      27             : 
      28             : #ifdef ZEND_WIN32
      29             : # define REGEX_MODE (REG_EXTENDED|REG_NOSUB|REG_ICASE)
      30             : #else
      31             : # define REGEX_MODE (REG_EXTENDED|REG_NOSUB)
      32             : #endif
      33             : 
      34             : #ifdef HAVE_GLOB
      35             : #ifdef PHP_WIN32
      36             : #include "win32/glob.h"
      37             : #else
      38             : #include <glob.h>
      39             : #endif
      40             : #endif
      41             : 
      42             : #define ZEND_BLACKLIST_BLOCK_SIZE       32
      43             : 
      44             : struct _zend_regexp_list {
      45             :         regex_t           comp_regex;
      46             :         zend_regexp_list *next;
      47             : };
      48             : 
      49             : zend_blacklist accel_blacklist;
      50             : 
      51           0 : void zend_accel_blacklist_init(zend_blacklist *blacklist)
      52             : {
      53           0 :         blacklist->pos = 0;
      54           0 :         blacklist->size = ZEND_BLACKLIST_BLOCK_SIZE;
      55             : 
      56           0 :         if (blacklist->entries != NULL) {
      57           0 :                 zend_accel_blacklist_shutdown(blacklist);
      58             :         }
      59             : 
      60           0 :         blacklist->entries = (zend_blacklist_entry *) calloc(sizeof(zend_blacklist_entry), blacklist->size);
      61           0 :         if (!blacklist->entries) {
      62           0 :                 zend_accel_error(ACCEL_LOG_FATAL, "Blacklist initialization: no memory\n");
      63           0 :                 return;
      64             :         }
      65           0 :         blacklist->regexp_list = NULL;
      66             : }
      67             : 
      68           0 : static void blacklist_report_regexp_error(regex_t *comp_regex, int reg_err)
      69             : {
      70             :         char *errbuf;
      71           0 :         int errsize = regerror(reg_err, comp_regex, NULL, 0);
      72           0 :         errbuf = malloc(errsize);
      73           0 :         if (!errbuf) {
      74           0 :                 zend_accel_error(ACCEL_LOG_ERROR, "Blacklist compilation: no memory\n");
      75           0 :                 return;
      76             :         }
      77           0 :         regerror(reg_err, comp_regex, errbuf, errsize);
      78           0 :         zend_accel_error(ACCEL_LOG_ERROR, "Blacklist compilation: %s\n", errbuf);
      79           0 :         free(errbuf);
      80             : }
      81             : 
      82           0 : static void zend_accel_blacklist_update_regexp(zend_blacklist *blacklist)
      83             : {
      84             :         int i, reg_err;
      85             :         zend_regexp_list **regexp_list_it, *it;
      86           0 :         char regexp[12*1024], *p, *end, *c, *backtrack = NULL;
      87             : 
      88           0 :         if (blacklist->pos == 0) {
      89             :                 /* we have no blacklist to talk about */
      90           0 :                 return;
      91             :         }
      92             : 
      93           0 :         regexp_list_it = &(blacklist->regexp_list);
      94             : 
      95           0 :         regexp[0] = '^';
      96           0 :         regexp[1] = '(';
      97           0 :         p = regexp + 2;
      98           0 :         end = regexp + sizeof(regexp) - sizeof("[^\\\\]*)\0");
      99             : 
     100           0 :         for (i = 0; i < blacklist->pos; ) {
     101           0 :                 c = blacklist->entries[i].path;
     102           0 :                 if (p + blacklist->entries[i].path_length < end) {
     103           0 :                         while (*c && p < end) {
     104           0 :                                 switch (*c) {
     105             :                                         case '?':
     106           0 :                                                 c++;
     107             : #ifdef ZEND_WIN32
     108             :                                                 p[0] = '[';                     /* * => [^\\] on Win32 */
     109             :                                                 p[1] = '^';
     110             :                                                 p[2] = '\\';
     111             :                                                 p[3] = '\\';
     112             :                                                 p[4] = ']';
     113             :                                                 p += 5;
     114             : #else
     115           0 :                                                 p[0] = '[';                     /* * => [^/] on *nix */
     116           0 :                                                 p[1] = '^';
     117           0 :                                                 p[2] = '/';
     118           0 :                                                 p[3] = ']';
     119           0 :                                                 p += 4;
     120             : #endif
     121           0 :                                                 break;
     122             :                                         case '*':
     123           0 :                                                 c++;
     124           0 :                                                 if (*c == '*') {
     125           0 :                                                         c++;
     126           0 :                                                         p[0] = '.';                     /* ** => .* */
     127           0 :                                                         p[1] = '*';
     128           0 :                                                         p += 2;
     129             :                                                 } else {
     130             : #ifdef ZEND_WIN32
     131             :                                                         p[0] = '[';                     /* * => [^\\]* on Win32 */
     132             :                                                         p[1] = '^';
     133             :                                                         p[2] = '\\';
     134             :                                                         p[3] = '\\';
     135             :                                                         p[4] = ']';
     136             :                                                         p[5] = '*';
     137             :                                                         p += 6;
     138             : #else
     139           0 :                                                         p[0] = '[';                     /* * => [^/]* on *nix */
     140           0 :                                                         p[1] = '^';
     141           0 :                                                         p[2] = '/';
     142           0 :                                                         p[3] = ']';
     143           0 :                                                         p[4] = '*';
     144           0 :                                                         p += 5;
     145             : #endif
     146             :                                                 }
     147           0 :                                                 break;
     148             :                                         case '^':
     149             :                                         case '.':
     150             :                                         case '[':
     151             :                                         case ']':
     152             :                                         case '$':
     153             :                                         case '(':
     154             :                                         case ')':
     155             :                                         case '|':
     156             :                                         case '+':
     157             :                                         case '{':
     158             :                                         case '}':
     159             :                                         case '\\':
     160           0 :                                                 *p++ = '\\';
     161             :                                                 /* break missing intentionally */
     162             :                                         default:
     163           0 :                                                 *p++ = *c++;
     164             :                                 }
     165             :                         }
     166             :                 }
     167             : 
     168           0 :                 if (*c || i == blacklist->pos - 1) {
     169           0 :                         if (*c) {
     170           0 :                                 if (!backtrack) {
     171           0 :                                         zend_accel_error(ACCEL_LOG_ERROR, "Too long blacklist entry\n");
     172             :                                 }
     173           0 :                                 p = backtrack;
     174             :                         } else {
     175           0 :                                 i++;
     176             :                         }
     177           0 :                         *p++ = ')';
     178           0 :                         *p++ = '\0';
     179             : 
     180           0 :                         it = (zend_regexp_list*)malloc(sizeof(zend_regexp_list));
     181           0 :                         if (!it) {
     182           0 :                                 zend_accel_error(ACCEL_LOG_ERROR, "malloc() failed\n");
     183           0 :                                 return;
     184             :                         }
     185           0 :                         it->next = NULL;
     186             : 
     187           0 :                         if ((reg_err = regcomp(&it->comp_regex, regexp, REGEX_MODE)) != 0) {
     188           0 :                                 blacklist_report_regexp_error(&it->comp_regex, reg_err);
     189             :                         }
     190             :                         /* prepare for the next iteration */
     191           0 :                         p = regexp + 2;
     192           0 :                         *regexp_list_it = it;
     193           0 :                         regexp_list_it = &it->next;
     194             :                 } else {
     195           0 :                         backtrack = p;
     196           0 :                         *p++ = '|';
     197           0 :                         i++;
     198             :                 }
     199             :         }
     200             : }
     201             : 
     202       20301 : void zend_accel_blacklist_shutdown(zend_blacklist *blacklist)
     203             : {
     204       20301 :         zend_blacklist_entry *p = blacklist->entries, *end = blacklist->entries + blacklist->pos;
     205             : 
     206       40602 :         while (p<end) {
     207           0 :                 free(p->path);
     208           0 :                 p++;
     209             :         }
     210       20301 :         free(blacklist->entries);
     211       20301 :         blacklist->entries = NULL;
     212       20301 :         if (blacklist->regexp_list) {
     213           0 :                 zend_regexp_list *temp, *it = blacklist->regexp_list;
     214           0 :                 while (it) {
     215           0 :                         regfree(&it->comp_regex);
     216           0 :                         temp = it;
     217           0 :                         it = it->next;
     218           0 :                         free(temp);
     219             :                 }
     220             :         }
     221       20301 : }
     222             : 
     223           0 : static inline void zend_accel_blacklist_allocate(zend_blacklist *blacklist)
     224             : {
     225           0 :         if (blacklist->pos == blacklist->size) {
     226           0 :                 blacklist->size += ZEND_BLACKLIST_BLOCK_SIZE;
     227           0 :                 blacklist->entries = (zend_blacklist_entry *) realloc(blacklist->entries, sizeof(zend_blacklist_entry)*blacklist->size);
     228             :         }
     229           0 : }
     230             : 
     231             : #ifdef HAVE_GLOB
     232           0 : static void zend_accel_blacklist_loadone(zend_blacklist *blacklist, char *filename)
     233             : #else
     234             : void zend_accel_blacklist_load(zend_blacklist *blacklist, char *filename)
     235             : #endif
     236             : {
     237           0 :         char buf[MAXPATHLEN + 1], real_path[MAXPATHLEN + 1], *blacklist_path = NULL;
     238             :         FILE *fp;
     239             :         int path_length, blacklist_path_length;
     240             :         TSRMLS_FETCH();
     241             : 
     242           0 :         if ((fp = fopen(filename, "r")) == NULL) {
     243           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Cannot load blacklist file: %s\n", filename);
     244           0 :                 return;
     245             :         }
     246             : 
     247           0 :         zend_accel_error(ACCEL_LOG_DEBUG,"Loading blacklist file:  '%s'", filename);
     248             : 
     249           0 :         if (VCWD_REALPATH(filename, buf)) {
     250           0 :                 blacklist_path_length = zend_dirname(buf, strlen(buf));
     251           0 :                 blacklist_path = zend_strndup(buf, blacklist_path_length);
     252             :         }
     253             : 
     254           0 :         memset(buf, 0, sizeof(buf));
     255           0 :         memset(real_path, 0, sizeof(real_path));
     256             : 
     257           0 :         while (fgets(buf, MAXPATHLEN, fp) != NULL) {
     258             :                 char *path_dup, *pbuf;
     259           0 :                 path_length = strlen(buf);
     260           0 :                 if (path_length > 0 && buf[path_length - 1] == '\n') {
     261           0 :                         buf[--path_length] = 0;
     262           0 :                         if (path_length > 0 && buf[path_length - 1] == '\r') {
     263           0 :                                 buf[--path_length] = 0;
     264             :                         }
     265             :                 }
     266             : 
     267             :                 /* Strip ctrl-m prefix */
     268           0 :                 pbuf = &buf[0];
     269           0 :                 while (*pbuf == '\r') {
     270           0 :                         *pbuf++ = 0;
     271           0 :                         path_length--;
     272             :                 }
     273             : 
     274             :                 /* strip \" */
     275           0 :                 if (pbuf[0] == '\"' && pbuf[path_length - 1]== '\"') {
     276           0 :                         *pbuf++ = 0;
     277           0 :                         path_length -= 2;
     278             :                 }
     279             : 
     280           0 :                 if (path_length == 0) {
     281           0 :                         continue;
     282             :                 }
     283             : 
     284             :                 /* skip comments */
     285           0 :                 if (pbuf[0]==';') {
     286           0 :                         continue;
     287             :                 }
     288             : 
     289           0 :                 path_dup = zend_strndup(pbuf, path_length);
     290           0 :                 if (blacklist_path) {
     291           0 :                         expand_filepath_ex(path_dup, real_path, blacklist_path, blacklist_path_length TSRMLS_CC);
     292             :                 } else {
     293           0 :                         expand_filepath(path_dup, real_path TSRMLS_CC);
     294             :                 }
     295           0 :                 path_length = strlen(real_path);
     296             : 
     297           0 :                 free(path_dup);
     298             : 
     299           0 :                 zend_accel_blacklist_allocate(blacklist);
     300           0 :                 blacklist->entries[blacklist->pos].path_length = path_length;
     301           0 :                 blacklist->entries[blacklist->pos].path = (char *)malloc(path_length + 1);
     302           0 :                 if (!blacklist->entries[blacklist->pos].path) {
     303           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "malloc() failed\n");
     304           0 :                         fclose(fp);
     305           0 :                         return;
     306             :                 }
     307           0 :                 blacklist->entries[blacklist->pos].id = blacklist->pos;
     308           0 :                 memcpy(blacklist->entries[blacklist->pos].path, real_path, path_length + 1);
     309           0 :                 blacklist->pos++;
     310             :         }
     311           0 :         fclose(fp);
     312           0 :         if (blacklist_path) {
     313           0 :                 free(blacklist_path);
     314             :         }
     315           0 :         zend_accel_blacklist_update_regexp(blacklist);
     316             : }
     317             : 
     318             : #ifdef HAVE_GLOB
     319           0 : void zend_accel_blacklist_load(zend_blacklist *blacklist, char *filename)
     320             : {
     321             :         glob_t globbuf;
     322             :         int    ret;
     323             :         unsigned int i;
     324             : 
     325           0 :         memset(&globbuf, 0, sizeof(glob_t));
     326             : 
     327           0 :         ret = glob(filename, 0, NULL, &globbuf);
     328             : #ifdef GLOB_NOMATCH
     329           0 :         if (ret == GLOB_NOMATCH || !globbuf.gl_pathc) {
     330             : #else
     331             :         if (!globbuf.gl_pathc) {
     332             : #endif
     333           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "No blacklist file found matching: %s\n", filename);
     334             :         } else {
     335           0 :                 for(i=0 ; i<globbuf.gl_pathc; i++) {
     336           0 :                         zend_accel_blacklist_loadone(blacklist, globbuf.gl_pathv[i]);
     337             :                 }
     338           0 :                 globfree(&globbuf);
     339             :         }
     340           0 : }
     341             : #endif
     342             : 
     343         423 : zend_bool zend_accel_blacklist_is_blacklisted(zend_blacklist *blacklist, char *verify_path)
     344             : {
     345         423 :         int ret = 0;
     346         423 :         zend_regexp_list *regexp_list_it = blacklist->regexp_list;
     347             : 
     348         423 :         if (regexp_list_it == NULL) {
     349         423 :                 return 0;
     350             :         }
     351           0 :         while (regexp_list_it != NULL) {
     352           0 :                 if (regexec(&(regexp_list_it->comp_regex), verify_path, 0, NULL, 0) == 0) {
     353           0 :                         ret = 1;
     354           0 :                         break;
     355             :                 }
     356           0 :                 regexp_list_it = regexp_list_it->next;
     357             :         }
     358           0 :         return ret;
     359             : }
     360             : 
     361           0 : void zend_accel_blacklist_apply(zend_blacklist *blacklist, blacklist_apply_func_arg_t func, void *argument TSRMLS_DC)
     362             : {
     363             :         int i;
     364             : 
     365           0 :         for (i = 0; i < blacklist->pos; i++) {
     366           0 :                 func(&blacklist->entries[i], argument TSRMLS_CC);
     367             :         }
     368           0 : }

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:53 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.