1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <pcre.h>
6 #include "vars.h"
7 
8 /************************************
9  ********* Load PCRE file ***********/
10 
load_pcre_file(int section)11 int load_pcre_file(int section)
12 {
13     FILE *fp;
14     char tmp[ML_URL],etmp[ML_ETMP];
15     const char *errstr;
16     int errchar;
17 
18 
19     //Set pointer
20     redir[section]->ban_pcre=NULL;
21     redir[section]->ban_pcre_s=NULL;
22     redir[section]->ban_pcre_count=0;
23 
24     // Load pcre file
25     sprintf(tmp,"%s/pcre",redir[section]->ban_dir);
26 
27     #ifdef DEBUG_PCRE
28     fprintf(stderr,"=== %s ===\n",tmp);
29     #endif
30 
31     // File exist?
32     if (access(tmp,F_OK)) return(1);
33 
34     // Open file
35     if ((fp=fopen(tmp,"r"))==NULL)
36 	{ // Open error
37           sprintf(etmp,"ERROR: Can't open file %s/pcre: %s, exit",redir[section]->ban_dir,strerror(errno));
38           err_mes(etmp);
39 
40 	  #ifdef DEBUG_PCRE
41           fprintf(stderr,"%s\n",tmp);
42 	  #endif
43 
44 	  exit(-1);
45         }
46     // Read file
47      while(!feof(fp))
48       {
49         //Get
50         if (fgets(tmp,ML_URL,fp)==NULL) continue;
51 
52 
53     	//Chomp
54 	if (tmp[strlen(tmp)-1]=='\n' || tmp[strlen(tmp)-1]=='\r') tmp[strlen(tmp)-1]=0;
55 	if (tmp[strlen(tmp)-1]=='\n' || tmp[strlen(tmp)-1]=='\r') tmp[strlen(tmp)-1]=0;
56 	if (strlen(tmp)<=0) continue;
57 
58     	#ifdef DEBUG_PCRE
59     	fprintf(stderr,"%s\n",tmp);
60     	#endif
61 
62         //Add memory line
63         redir[section]->ban_pcre=(pcre **)realloc(redir[section]->ban_pcre,(1+redir[section]->ban_pcre_count)*sizeof(pcre *));
64         redir[section]->ban_pcre_s=(pcre_extra **)realloc(redir[section]->ban_pcre_s,(1+redir[section]->ban_pcre_count)*sizeof(pcre_extra *));
65         if (redir[section]->ban_pcre==NULL || redir[section]->ban_pcre_s==NULL)
66             {
67              err_mes("Error: Can't allocate memory for pcre\n");
68              exit(-1);
69             }
70 
71        // Compile pattern
72        if((redir[section]->ban_pcre[redir[section]->ban_pcre_count]=pcre_compile(tmp,PCRE_CASELESS,&errstr,&errchar,NULL))==NULL)
73        {
74         sprintf(etmp,"ERROR: Can't compile pattern in %s/pcre line:%i %s",redir[section]->ban_dir,redir[section]->ban_pcre_count+1,errstr);
75         err_mes(etmp);
76 	exit(-1);
77        }
78 
79        // Optimize pattern ***************/
80        redir[section]->ban_pcre_s[redir[section]->ban_pcre_count]=pcre_study(redir[section]->ban_pcre[redir[section]->ban_pcre_count],0,&errstr);
81        redir[section]->ban_pcre_count++;
82 
83      }//while
84 
85     fclose(fp);
86 
87     sprintf(etmp,"Load %i pattern from %s pcre",redir[section]->ban_pcre_count,redir[section]->name);
88     err_mes(etmp);
89     return (0);
90 }
91 
92 
93 /******************************************
94  ******** Check url by pcre rules *********/
95 
check_pcre(int section)96  int check_pcre(int section)
97  {
98    int i;
99    int vector[ML_PCRE_VARS];
100    int vecsize=ML_PCRE_VARS;
101 
102     for (i=0;i<redir[section]->ban_pcre_count;i++)
103 
104     #ifdef CASE_INDEPENDENT
105     if(pcre_exec(redir[section]->ban_pcre[i],redir[section]->ban_pcre_s[i],input_url_uc,strlen(input_url_uc),0,PCRE_NOTEMPTY,vector,vecsize)>=0)
106     #else
107     if(pcre_exec(redir[section]->ban_pcre[i],redir[section]->ban_pcre_s[i],input_url_un,strlen(input_url_un),0,PCRE_NOTEMPTY,vector,vecsize)>=0)
108     #endif
109     {
110      sprintf(change_reason,"(pcre rule#: %d)",(unsigned char)(i+1));
111      return(1);
112     }
113     return(0);
114  }
115