1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "vars.h"
6 
7 struct section **redir;
8 char **allow_urls;
9 int allow_urls_count;
10 
11 /***************************************
12  ********** Start MAIN function ********/
main(int argc,char * argv[])13  int main(int argc, char* argv[])
14 {
15 
16   int z;
17   char etmp[ML_ETMP],str[ML_URL];
18   int flag;
19 
20     //Init some vars
21     init_vars();
22 
23     // Load config file
24     if (argc!=2) {get_opt(DEFAULT_CONFIG);}
25     else {
26           if (!strcmp(argv[1],"-V"))
27            {
28             printf("Redirector %s\n",VERSION);
29             exit(0);
30            }
31            get_opt(argv[1]);
32          }
33 
34     // Run make-cahe
35     if (argc!=2) {run_make_cache(DEFAULT_CONFIG);}
36     else {run_make_cache(argv[1]);}
37 
38     // Load allow urls cache
39     sprintf(etmp,"%s/allow_urls.cache",redir[0]->ban_dir);
40     load_cache(&allow_urls,&allow_urls_count,etmp);
41 
42     // Prepare section data
43     for (z=0;z<sections_count; z++)
44 	{
45          // Prepare new_url
46 	 prepare_new_url(z);
47 	 // Load urls cache
48 	 sprintf(etmp,"%s/urls.cache",redir[z]->ban_dir);
49          load_cache(&redir[z]->ban_urls,&redir[z]->ban_urls_count,etmp);
50 	 // Load pcre lists
51 	 load_pcre_file(z);
52 	}
53 
54 
55     //Open log file
56     if (chg_log)
57     for (z=0;z<sections_count; z++)
58      {
59        if (!redir[z]->log_flag)
60         {
61 	    if ((change_log=fopen(chg_log,"a"))==NULL)
62     	    {
63              sprintf(etmp,"ERROR: Can't make chg_log file %s: %s\n",chg_log,strerror(errno));
64              err_mes(etmp);
65 	     exit(-1);
66     	    }
67 
68 	#ifdef DEBUG_CYCLE
69 	 fprintf (stderr,"\nOpen log file: %s\n",chg_log);
70 	#endif
71 	break;
72 	}
73      }
74 
75       sprintf(etmp,"Redirector start and working (%s)",VERSION);
76       err_mes(etmp);
77 
78 /***********************
79  ****** Work cycle *****/
80     // Get input string
81     while (fgets(str,ML_URL,stdin)!=NULL)
82     {
83         // convert input string to input structure->(url,who,ident,method)
84         if(parse_input(str))
85 	   {
86 
87 	    // If error in convert
88 	    printf ("%s",str);
89 	    fflush(stdout);
90 	    sprintf(etmp,"Error convert input string to input structure str=%s",str);
91 	    err_mes(etmp);
92 	    continue;
93 	   };
94 
95 	  // Check to work_id
96 	  if (work_id_count && !check_id(work_id,work_id_count))
97 	   {
98 	    #ifdef DEBUG_CYCLE
99 	    fprintf (stderr," -->Skiping by not hit in work_id in global section\n");
100 	    #endif
101 	    pass();
102 	    continue;
103 	   }
104 
105 	  // Check to allow_id
106 	  if (allow_id_count && check_id(allow_id,allow_id_count))
107 	   {
108 	    #ifdef DEBUG_CYCLE
109 	    fprintf (stderr," -->Skiping by allow_id in global section\n");
110 	    #endif
111 	    pass();
112 	    continue;
113 	   }
114 
115 	  // Check ip to "work_ip" option
116 	  if (work_ip_count && !check_ip(work_ip,work_ip_count))
117 	   {
118 	    #ifdef DEBUG_CYCLE
119 	    fprintf (stderr," --> Skiping by not wokr_ip in global section\n");
120 	    #endif
121 	    pass();
122 	    continue;
123 	   }
124 
125 	  // Check ip to "allow_ip" option
126 	  if (allow_ip_count && check_ip(allow_ip,allow_ip_count))
127 	   {
128 	    #ifdef DEBUG_CYCLE
129 	    fprintf (stderr," --> Skiping by allow_ip in global section\n");
130 	    #endif
131 	    pass();
132 	    continue;
133 	   }
134 	   // Check for allow_urls
135 	   #ifdef CASE_INDEPENDENT
136 	   z=parse_urls(i_url,input_url_uc);
137            if (z<0)
138            {
139             sprintf(etmp, "REDIRECTOR: WARNING: Can't parse url: %s. Pass.",input_url_uc);
140             err_mes(etmp);
141 	    pass();
142             continue;
143            }
144            #else
145 	   z=parse_urls(i_url,input_url_un);
146            if (z<0)
147            {
148             sprintf(etmp, "REDIRECTOR: WARNING: Can't parse url: %s. Pass.",input_url_un);
149             err_mes(etmp);
150 	    pass();
151             continue;
152            }
153            #endif
154 
155 
156 
157 
158 	  if (check_url(allow_urls,allow_urls_count))
159 	   {
160 	    #ifdef DEBUG_CYCLE
161 	    fprintf (stderr," --> Skiping by allow_urls in global section\n");
162 	    #endif
163 	    pass();
164 	    continue;
165 	   }
166 
167     flag=0;
168 
169   /***** Check sections rules *****/
170     for (z=0;z<sections_count; z++)
171     {
172 
173 	  // Check for work_id
174 	  if ((!redir[z]->work_id_count && redir[z]->work_id_flag) || (redir[z]->work_id_count && !check_id(redir[z]->work_id,redir[z]->work_id_count)))
175 	   {
176 	    #ifdef DEBUG_CYCLE
177 	    fprintf (stderr," -->Skiping by not hit in work_id in \"%s\" section\n",redir[z]->name);
178 	    #endif
179 	    continue;
180 	   }
181 
182           // Check for allow_id
183 	  if (redir[z]->allow_id_count && check_id(redir[z]->allow_id,redir[z]->allow_id_count))
184 	   {
185 	    #ifdef DEBUG_CYCLE
186 	    fprintf (stderr," -->Skiping by allow_id in \"%s\" section\n",redir[z]->name);
187 	    #endif
188 	    continue;
189 	   }
190 
191 	  // Check for work_ip
192 	  if ((!redir[z]->work_ip_count && redir[z]->work_ip_flag) || (redir[z]->work_ip_count && !check_ip(redir[z]->work_ip,redir[z]->work_ip_count)))
193 	   {
194 	    #ifdef DEBUG_CYCLE
195 	    fprintf (stderr," --> Skiping by not work_ip in \"%s\" section\n",redir[z]->name);
196 	    #endif
197 	    continue;
198 	   }
199 
200 	  // Check for allow_ip
201 	  if (redir[z]->allow_ip_count && check_ip(redir[z]->allow_ip,redir[z]->allow_ip_count))
202 	   {
203 	    #ifdef DEBUG_CYCLE
204 	    fprintf (stderr," --> Skiping by allow_ip in \"%s\" section\n",redir[z]->name);
205 	    #endif
206 	    continue;
207 	   }
208 
209   //Check for BAN URLS
210   if (redir[z]->ban_urls_count)
211     {
212        flag=check_url(redir[z]->ban_urls,redir[z]->ban_urls_count);
213        if ((flag && !redir[z]->revers) || (!flag && redir[z]->revers))
214         {
215 	 flag=1;
216 	 redirect(z);
217 	 break;
218         }
219     }
220   else flag=0;
221 
222   //Check for BAN PCRE
223   if (redir[z]->ban_pcre_count)
224     {
225        flag=check_pcre(z);
226        if ((flag && !redir[z]->revers) || (!flag && redir[z]->revers))
227         {
228 	 flag=1;
229 	 redirect(z);
230 	 break;
231         }
232     }
233   else flag=0;
234 
235   }// sections rules
236       if (!flag)
237       {
238 	#ifdef DEBUG_CYCLE
239 
240 	 fprintf(stderr,"\n=== URL NOT FOUND IN BLACK LIST ========\n");
241 	 fprintf(stderr,"str\t:%s",str);
242 	 fprintf(stderr,"url\t:%s:\n",input_url);
243 	 fprintf(stderr,"url_un\t:%s:\n",input_url_un);
244 	 fprintf(stderr,"ip\t:%d.%d.%d.%d:\n",(unsigned char)input_ip[0],(unsigned char)input_ip[1],(unsigned char)input_ip[2],(unsigned char)input_ip[3]);
245 	 fprintf(stderr,"host\t:%s:\n",input_host);
246 	 fprintf(stderr,"ident\t:%s:\n",input_ident);
247 	 fprintf(stderr,"ident_un:%s:\n",input_ident_un);
248 	 fprintf(stderr,"method\t:%s:\n\n",input_method);
249 	#endif
250 
251 	pass();
252       }
253     }//work cycle
254 
255  exit(0);
256 }
257