File: rewrite.c

package info (click to toggle)
jesred 1.2pl1-22
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 580 kB
  • ctags: 286
  • sloc: ansic: 1,359; makefile: 76
file content (460 lines) | stat: -rw-r--r-- 11,762 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * $Id: rewrite.c,v 1.3 1998/08/15 00:01:14 elkner Exp $
 *
 * Author:  Squirm derived      http://www.senet.com.au/squirm/
 * Project: Jesred       http://ivs.cs.uni-magdeburg.de/~elkner/webtools/jesred/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * http://www.gnu.org/copyleft/gpl.html or ./gpl.html
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Thanks to Chris Foote, chris@senet.com.au - except parse_buff
 * not much to change here (i.e. don't like to go deeper into the pattern stuff)
 * ;-)
 *
 */

#include <stdio.h>
#include <stdlib.h>
#ifdef LINUX
#include<string.h>
#else
#include<strings.h>
#endif
#include<ctype.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

#ifdef LOCAL_REGEX
#include "regex.h"
#else
#include<regex.h>
#endif

#include "log.h"
#include "ip_list.h"
#include "pattern_list.h"
#include "rewrite.h"
#include "main.h"

extern int count_parenthesis();		/* from pattern_list.c */

/* load the stdin for the redirector into an IN_BUFF structure 
   Sets in_buff.url to "" if the fields can't be converted */

int replace_string(pattern_item *, char *, char *);
#ifdef USE_ACCEL
static int match_accel(char *, char *, int, int);
#endif

int
parse_buff(char *buff, char **url, char **src_addr, char **ident,
	   char **method, int *chanid, ip_acl *ip)
{
   int j;
   struct in_addr address;
   struct in6_addr address6;
   int family=AF_INET;
   char *token;
   char *next_token = buff;
   char *errorptr;

   /* az [2015-10-23 Fri 21:20]
      goodbye squid2..3.3, hello squid3.5

      no more url groups; a numeric channel id, a url, space
      and extra stuff or a newline.
      apparently extras was configurable with url_rewrite_extras,
      but that has been removed in one of the newest squid
      versions (the docs re this are pretty damn confused...)

      [channel-ID <SP>] URL [<SP> extras]<NL>
      and extras are supposed to be (adjustable in 3.5,
      adjustability removed(??) in 4)
      ip/fqdn username method myip=<ip> myport=<port>
   */

   /* start of url or start of extras? */
   if (!(token = strsep(&next_token, " ")))
   {
      mylog(ERROR, "invalid input, no extras in (%s)", buff);
      return 1;
   }

   /* channel-id? must be numeric */
   j = (int)strtol(buff, &errorptr, 10);
   if (!*errorptr)	/* conversion successful */
   {
      *chanid = j;
      *url = next_token;

      /* now find end of url/start of ip/fqdn */
      if (!(token = strsep(&next_token, " ")))
      {
	 mylog(ERROR, "invalid input, no ip/fqdn in (%s)", buff);
	 return 1;
      }
   }
   else
   {
      *url = buff;
      *chanid = -1;	/* no channel id present; it is non-negative when used */
   }

   /* find end of ip/fqdn */
   if (!(token = strsep(&next_token, " ")))
   {
      mylog(ERROR, "invalid input, no username in (%s)", buff);
      return 1;
   }
   *src_addr = token;

   /* find end of username */
   if (!(token = strsep(&next_token, " ")))
   {
      mylog(ERROR, "invalid input, no method in (%s)", buff);
      return 1;
   }
   *ident = token;

   /* find end of method */
   if (!(token = strsep(&next_token, " ")))
   {
      mylog(ERROR, "invalid input, no kv pairs in (%s)", buff);
      return 1;
   }
   *method = token;
   /* and the rest we don't care about... */

#ifdef DEBUG
   mylog(DEBG, "Request: %d %s %s %s %s\n", *chanid, *url, *src_addr, *ident, *method);
#endif    
    
    /* URL with less than 7 char is invalid */
    if(strlen(*url) <= 7) {
	mylog(ERROR, "strlen url to short (%d)\n", strlen(*url));
	return 1;
    }

    /* check that the IP source address supplied is valid */
    token = strchr(*src_addr,'/');
    if ( token )
	*token = '\0';
    /* for inet_addr we have to link with libnsl on Solaris:
       i.e. on 2.6  448K r-x + 40K rwx, but since it is a shared lib,
       it is already loaded, when squid runs - so not much waste of
       memory ;-) */
     if ( (address.s_addr = inet_addr(*src_addr)) == -1 )
     {
        /* not ipv6, test ipv6 now */
        if (!inet_pton(AF_INET6,*src_addr,&address6.s6_addr))
        {
 	  mylog(ERROR, "client IP address (%s) not valid\n",
 		*src_addr ? *src_addr : "");
 	  if ( token )
 	     *token = '/';
 	  return 1;
        }
        else
 	  family=AF_INET6;
    }
    if ( token )
	*token = '/';
    
    /* make sure the IP source address matches that of the ones in our list */
    if( ip_access_check(family,(family==AF_INET?(void*)&address:
				(void*)&address6),ip) == IP_DENY ) {
#ifdef DEBUG
	mylog(DEBG, "client IP address (%s) not matched\n", *src_addr);
#endif  
	return 1;
    }
    return 0;
}

/* returns replacement URL for a match in newurl
   < 0 if abort pattern match, 0 if no match found, > 1 pattern match
   if match, the number of the matching rule will be returned */
int
pattern_compare(char *url, char *urlgroup, char *newurl, pattern_item *phead)
{
    pattern_item *curr;
    int pos;
    int len;
    int i;
    int matched;
    int pattern_no = 0;
    curr = NULL;
  
    for(curr = phead; curr != NULL; curr = curr->next) {
	pattern_no++;
	matched = 1;
	/* assume a match until a character isn't the same */

	/* urlgroup is checked ONLY IF the rule has one */
        if (curr->urlgroup) {
            if (!urlgroup || strcmp(curr->urlgroup, urlgroup) != 0) 
	    {
               matched = 0;
               continue;
            } 
	}

	if(curr->type == ABORT) {
	    len = strlen(curr->pattern);
	    pos = strlen(url) - len; /* this is dangerous */
	    for(i = 0; i <= len; i++) {
		if (url[pos] != curr->pattern[i]) {
		    matched = 0;
		    break;
		}
		pos++;
	    }
	    if(matched) {
#ifdef DEBUG
		mylog(DEBG, "abort pattern matched: %s (rule %d)\n",
		    url, pattern_no);
#endif		
		return (0 - pattern_no); /* URL matches abort file extension */
	    }
	}
	else { 
	    /* check for accelerator string */
#ifdef USE_ACCEL
	    if(curr->has_accel) {
		/* check to see if the accelerator string matches, then bother
		   doing a regexec() on it */
		if(match_accel(url, curr->accel,
			       curr->accel_type, 
			       curr->case_sensitive)) {
#ifdef DEBUG
		    mylog(DEBG, "URL %s matches accelerator %s (rule %d)\n",
			url, curr->accel, pattern_no);
#endif		    
		    /* Now we must test for normal or extended */
		    if (curr->type == EXTENDED) {
			if ( replace_string(curr, url, newurl) == 1 )
			    return pattern_no;
		    }
		    else /* Type == NORMAL */ {
			if(regexec(&curr->cpattern, url, 0, 0, 0) == 0){
			    strcpy(newurl,curr->replacement);
			    return pattern_no;
			}
		    }
		} /* end match_accel loop */
	    }
	    else {
		/* we haven't got an accelerator string, so we use regex
		   instead */
		/* Now we must test for normal or extended */
#endif
		if (curr->type == EXTENDED) {
		    if ( replace_string(curr, url, newurl) == 1)
			return pattern_no;
		}
		else /* Type == NORMAL */ {
		    if(regexec(&curr->cpattern, url, 0, 0, 0) == 0) {
			strcpy(newurl,curr->replacement);
			return pattern_no;
		    }
		}
#ifdef USE_ACCEL
	    }
#endif
	}
    }
    return 0;
}
    
int
replace_string (pattern_item *curr, char *url, char *buffer)
{
/*    char *replacement_string = NULL; */
    regmatch_t match_data[10];
    int parenthesis;
    char *in_ptr;
    char *out_ptr;
    int replay_num;
    int count;
   
    /* Perform the regex call */
    if (regexec (&curr->cpattern, url, 10, &match_data[0], 0) != 0)
	return 0;
  
    /* Ok, setup the traversal pointers */
    in_ptr = curr->replacement;
    out_ptr = buffer;
  
    /* Count the number of replays in the pattern */
    parenthesis = count_parenthesis (curr->pattern);
    if (parenthesis < 0) {
	/* Invalid return value - don't log because we already have done it */
	return 0;
    }
  
    /* Traverse the url string now */
    while (*in_ptr != '\0') {
	if (isdigit (*in_ptr)) {
	    /* We have a number, how many chars are there before us? */
	    switch (in_ptr - curr->replacement) {
		case 0:
		    /* This is the first char
		       Since there is no backslash before hand, this is not
		       a pattern match, so loop around */
		    {
			*out_ptr = *in_ptr;
			out_ptr++;
			in_ptr++;
			continue;
		    }
		case 1:
		    /* Only one char back to check, so see if it's a backslash */
		    if (*(in_ptr - 1) != '\\') {
			*out_ptr = *in_ptr;
			out_ptr++;
			in_ptr++;
			continue;
		    }
		    break;
		default:
		    /* Two or more chars back to check, so see if the previous is
		       a backslash, and also the one before. Two backslashes mean
		       that we should not replace anything! */
		    if ( (*(in_ptr - 1) != '\\') ||
			 ((*(in_ptr - 1) == '\\') && (*(in_ptr - 2) == '\\')) ) {
			*out_ptr = *in_ptr;
			out_ptr++;
			in_ptr++;
			continue;
		    }
	    }
	    
	    /* Ok, if we reach this point, then we have found something to
	       replace. It also means that the last time we went through here,
	       we copied in a backslash char, so we should backtrack one on
	       the output string before continuing */
	    out_ptr--;
	    
	    /* We need to convert the current in_ptr into a number for array
	       lookups */
	    replay_num = (*in_ptr) - '0';
	    
	    /* Now copy in the chars from the replay string */
	    for (count = match_data[replay_num].rm_so; 
		 count < match_data[replay_num].rm_eo; count++) {
		/* Copy in the chars */
		*out_ptr = url[count];
		out_ptr++;
	    }
      
	    /* Increment the in pointer */
	    in_ptr++;
	} else {
	    *out_ptr = *in_ptr;
	    out_ptr++;
	    in_ptr++;
	}
	
	/* Increment the in pointer and loop around */
	/* in_ptr++; */
    }
  
    /* Terminate the string */
    *out_ptr = '\0';
    
    /* return to the caller (buffer contains the new url) */
    return 1;
}

#ifdef USE_ACCEL
static int
match_accel(char *url, char *accel, int accel_type, int case_sensitive)
{
    /* return 1 if url contains accel */
    int i, offset;
    static char l_accel[BUFSIZE];
    int accel_len;
    int url_len;
  
    if(accel_type == ACCEL_NORMAL) {
	if(case_sensitive) {
	    if(strstr(url, accel))
		return 1;
	    else
		return 0;
	}
	else {
	    /* convert to lower case */
	    for(i = 0; url[i] != '\0'; i++)
		l_accel[i] = tolower(url[i]);
	    l_accel[i] = '\0';   
	    if(strstr(l_accel, accel))
		return 1;
	    else
		return 0;
	}
    }
    if(accel_type == ACCEL_START) {
	accel_len = strlen(accel);
	url_len = strlen(url);
	if(url_len < accel_len)
	    return 0;
	if(case_sensitive) {
	    for(i = 0; i < accel_len; i++) {
		if(accel[i] != url[i])
		    return 0;
	    }
	}
	else {
	    for(i = 0; i < accel_len; i++) {
		if(accel[i] != tolower(url[i]))
		    return 0;
	    }
	}
	return 1;
    }
    if(accel_type == ACCEL_END) {
	accel_len = strlen(accel);
	url_len = strlen(url);
	offset = url_len - accel_len;
	if(offset < 0)
	    return 0;
	if(case_sensitive) {
	    for(i = 0; i < accel_len; i++) {
		if(accel[i] != url[i+offset])
		    return 0;
	    }
	}
	else {
	    for(i = 0; i < accel_len; i++) {
		if(accel[i] != tolower(url[i+offset]))
		    return 0;
	    }
	}
	return 1;
    }
  
    /* we shouldn't reach this section! */
    return 0;
}
#endif