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: 2015-03-01 Functions: 2 9 22.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend OPcache                                                         |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 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       20749 : void zend_accel_blacklist_shutdown(zend_blacklist *blacklist)
     203             : {
     204       20749 :         zend_blacklist_entry *p = blacklist->entries, *end = blacklist->entries + blacklist->pos;
     205             : 
     206       41498 :         while (p<end) {
     207           0 :                 free(p->path);
     208           0 :                 p++;
     209             :         }
     210       20749 :         free(blacklist->entries);
     211       20749 :         blacklist->entries = NULL;
     212       20749 :         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       20749 : }
     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             : 
     241           0 :         if ((fp = fopen(filename, "r")) == NULL) {
     242           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "Cannot load blacklist file: %s\n", filename);
     243           0 :                 return;
     244             :         }
     245             : 
     246           0 :         zend_accel_error(ACCEL_LOG_DEBUG,"Loading blacklist file:  '%s'", filename);
     247             : 
     248           0 :         if (VCWD_REALPATH(filename, buf)) {
     249           0 :                 blacklist_path_length = zend_dirname(buf, strlen(buf));
     250           0 :                 blacklist_path = zend_strndup(buf, blacklist_path_length);
     251             :         }
     252             : 
     253           0 :         memset(buf, 0, sizeof(buf));
     254           0 :         memset(real_path, 0, sizeof(real_path));
     255             : 
     256           0 :         while (fgets(buf, MAXPATHLEN, fp) != NULL) {
     257             :                 char *path_dup, *pbuf;
     258           0 :                 path_length = strlen(buf);
     259           0 :                 if (path_length > 0 && buf[path_length - 1] == '\n') {
     260           0 :                         buf[--path_length] = 0;
     261           0 :                         if (path_length > 0 && buf[path_length - 1] == '\r') {
     262           0 :                                 buf[--path_length] = 0;
     263             :                         }
     264             :                 }
     265             : 
     266             :                 /* Strip ctrl-m prefix */
     267           0 :                 pbuf = &buf[0];
     268           0 :                 while (*pbuf == '\r') {
     269           0 :                         *pbuf++ = 0;
     270           0 :                         path_length--;
     271             :                 }
     272             : 
     273             :                 /* strip \" */
     274           0 :                 if (pbuf[0] == '\"' && pbuf[path_length - 1]== '\"') {
     275           0 :                         *pbuf++ = 0;
     276           0 :                         path_length -= 2;
     277             :                 }
     278             : 
     279           0 :                 if (path_length == 0) {
     280           0 :                         continue;
     281             :                 }
     282             : 
     283             :                 /* skip comments */
     284           0 :                 if (pbuf[0]==';') {
     285           0 :                         continue;
     286             :                 }
     287             : 
     288           0 :                 path_dup = zend_strndup(pbuf, path_length);
     289           0 :                 if (blacklist_path) {
     290           0 :                         expand_filepath_ex(path_dup, real_path, blacklist_path, blacklist_path_length);
     291             :                 } else {
     292           0 :                         expand_filepath(path_dup, real_path);
     293             :                 }
     294           0 :                 path_length = strlen(real_path);
     295             : 
     296           0 :                 free(path_dup);
     297             : 
     298           0 :                 zend_accel_blacklist_allocate(blacklist);
     299           0 :                 blacklist->entries[blacklist->pos].path_length = path_length;
     300           0 :                 blacklist->entries[blacklist->pos].path = (char *)malloc(path_length + 1);
     301           0 :                 if (!blacklist->entries[blacklist->pos].path) {
     302           0 :                         zend_accel_error(ACCEL_LOG_ERROR, "malloc() failed\n");
     303           0 :                         fclose(fp);
     304           0 :                         return;
     305             :                 }
     306           0 :                 blacklist->entries[blacklist->pos].id = blacklist->pos;
     307           0 :                 memcpy(blacklist->entries[blacklist->pos].path, real_path, path_length + 1);
     308           0 :                 blacklist->pos++;
     309             :         }
     310           0 :         fclose(fp);
     311           0 :         if (blacklist_path) {
     312           0 :                 free(blacklist_path);
     313             :         }
     314           0 :         zend_accel_blacklist_update_regexp(blacklist);
     315             : }
     316             : 
     317             : #ifdef HAVE_GLOB
     318           0 : void zend_accel_blacklist_load(zend_blacklist *blacklist, char *filename)
     319             : {
     320             :         glob_t globbuf;
     321             :         int    ret;
     322             :         unsigned int i;
     323             : 
     324           0 :         memset(&globbuf, 0, sizeof(glob_t));
     325             : 
     326           0 :         ret = glob(filename, 0, NULL, &globbuf);
     327             : #ifdef GLOB_NOMATCH
     328           0 :         if (ret == GLOB_NOMATCH || !globbuf.gl_pathc) {
     329             : #else
     330             :         if (!globbuf.gl_pathc) {
     331             : #endif
     332           0 :                 zend_accel_error(ACCEL_LOG_WARNING, "No blacklist file found matching: %s\n", filename);
     333             :         } else {
     334           0 :                 for(i=0 ; i<globbuf.gl_pathc; i++) {
     335           0 :                         zend_accel_blacklist_loadone(blacklist, globbuf.gl_pathv[i]);
     336             :                 }
     337           0 :                 globfree(&globbuf);
     338             :         }
     339           0 : }
     340             : #endif
     341             : 
     342         428 : zend_bool zend_accel_blacklist_is_blacklisted(zend_blacklist *blacklist, char *verify_path)
     343             : {
     344         428 :         int ret = 0;
     345         428 :         zend_regexp_list *regexp_list_it = blacklist->regexp_list;
     346             : 
     347         428 :         if (regexp_list_it == NULL) {
     348         428 :                 return 0;
     349             :         }
     350           0 :         while (regexp_list_it != NULL) {
     351           0 :                 if (regexec(&(regexp_list_it->comp_regex), verify_path, 0, NULL, 0) == 0) {
     352           0 :                         ret = 1;
     353           0 :                         break;
     354             :                 }
     355           0 :                 regexp_list_it = regexp_list_it->next;
     356             :         }
     357           0 :         return ret;
     358             : }
     359             : 
     360           0 : void zend_accel_blacklist_apply(zend_blacklist *blacklist, blacklist_apply_func_arg_t func, void *argument)
     361             : {
     362             :         int i;
     363             : 
     364           0 :         for (i = 0; i < blacklist->pos; i++) {
     365           0 :                 func(&blacklist->entries[i], argument);
     366             :         }
     367           0 : }

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:29 +0000 (2 days ago)

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