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
  
     | 
    
      Author: Alexander Zangerl <az@debian.org>
Subject: add support for squid 3
--- a/rewrite.c
+++ b/rewrite.c
@@ -25,7 +25,8 @@
  *
  */
 
-#include<stdio.h>
+#include <stdio.h>
+#include <stdlib.h>
 #ifdef LINUX
 #include<string.h>
 #else
@@ -61,89 +62,85 @@ static int match_accel(char *, char *, i
 
 int
 parse_buff(char *buff, char **url, char **src_addr, char **ident,
-	     char **method, char **urlgroup, ip_acl *ip, pattern_item *p)
-	     //char **method, ip_acl *ip, pattern_item *p)
+	   char **method, int *chanid, ip_acl *ip)
 {
-    int c, i;
-    struct in_addr address;
-    char *token, *new_token;
-    char *end[5];
-    //char **urlgroup;
-    
-    c = 0;
-    *urlgroup = '\0';
-
-    token = strchr(buff,' ');
-    if ( token ) {       /* URL */
-	c++;
-	*token = '\0';
-	end[0] = token;
-	*url = buff;
-
-	new_token = strchr(++token,' ');
-	if (new_token) {     /* Address */
-	    c++;
-	    *new_token = '\0';
-	    end[1] = new_token;
-	    *src_addr = token;
-
-	    token = strchr(++new_token,' ');
-	    if (token) {      /* Ident */
-		c++;
-		*token = '\0';
-		end[2] = token;
-		*ident = new_token;
-
-		/* this might be the last token, check for a space
-		 or a newline */
-		if (!( new_token = strchr(++token,' ')))
-			new_token = strchr(token,'\n');
-		if (new_token) { /* Method */
-		    c++;
-		    *new_token = '\0';
-		    end[3] = new_token;
-		    *method = token;
-
-		    /* this will be the last token, stop at a space or newline
-                       to avoid spaces in urlgroup.  maybe be too rare to 
-                       waste a test on */
-		    if (!( token = strchr(++new_token,' ')))
-			token = strchr(new_token,'\n');
-		    if (token) {      /* urlgroup */
-			c++;
-			*token = '\0';
-			end[4] = token;
-			/* squid sends "-" as indicator for no urlgroup  */
-			if (strcmp(new_token,"-"))
-			   *urlgroup = new_token;
-
-		}
-	    }
-
-	    }
-	}
-    }
-
-    /* 4 pre 2.6 or no urlgroup
-       5 post 2.6 with a urlgroup */
-    if(( c != 5) && ( c != 4)) {
-	for(i = 0; i < c; i++) {
-	    if ( end[i] )
-		*end[i] = ' ';
-	}
-	mylog(ERROR, "incorrect input (%d): %s", c, buff);
-	return 1;
-    }
-
-    
+   int j;
+   struct in_addr address;
+   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: %s %s %s %s\n", *url, *src_addr, *ident, *method);
+   mylog(DEBG, "Request: %d %s %s %s %s\n", *chanid, *url, *src_addr, *ident, *method);
 #endif    
     
-    /* forward all methods */
-    /* removed restriction to GET or ICP_QUERY */
-	    
     /* URL with less than 7 char is invalid */
     if(strlen(*url) <= 7) {
 	mylog(ERROR, "strlen url to short (%d)\n", strlen(*url));
@@ -159,7 +156,7 @@ parse_buff(char *buff, char **url, char
        it is already loaded, when squid runs - so not much waste of
        memory ;-) */
     if ( (address.s_addr = inet_addr(*src_addr)) == -1 ) {
-	mylog(ERROR, "client IP address not valid %s\n",
+	mylog(ERROR, "client IP address (%s) not valid\n",
 	    *src_addr ? *src_addr : "");
 	if ( token )
 	    *token = '/';
@@ -171,7 +168,7 @@ parse_buff(char *buff, char **url, char
     /* make sure the IP source address matches that of the ones in our list */
     if( ip_access_check(address, ip) == IP_DENY ) {
 #ifdef DEBUG
-	mylog(DEBG, "client IP address %s not matched\n", *src_addr);
+	mylog(DEBG, "client IP address (%s) not matched\n", *src_addr);
 #endif  
 	return 1;
     }
--- a/main.c
+++ b/main.c
@@ -23,6 +23,7 @@
 
 #include<stdio.h>
 #include<stdlib.h>
+#include <unistd.h>
 #include<string.h>
 #include<sys/signal.h>
 #include<sys/types.h>
@@ -75,7 +76,7 @@ int main(int argc, char **argv)
 /*    int first_run = 1; */
     char buff[BUFSIZE];
     char redirect_url[BUFSIZE];
-    char *url, *src_addr, *ident, *method, *urlgroup;
+    char *url, *src_addr, *ident, *method, *urlgroup = "";
     int finished = 0;
     int buff_status = 0;
     ip_acl *ip_list = NULL;
@@ -93,7 +94,7 @@ int main(int argc, char **argv)
     /* main program loop, executed  forever more unless terminated
        by a kill signal or EOF on stdin */
     while(! finished) {
-	int val;
+       int val, chanid;
 	
 	sig_hup = 0;
 	mylog(INFO, "Freeing up old linked lists\n");
@@ -111,49 +112,86 @@ int main(int argc, char **argv)
 	mylog(INFO, "%s (PID %d) started\n", APPNAME, (int)getpid());
 	
 	while((!sig_hup) && (fgets(buff, BUFSIZE, stdin) != NULL)){
-	    if(echo_mode) {
-		puts("");
-		fflush(stdout);
-		continue;
+	    if(echo_mode) {	/* this doesn't work if a channel-id is present. */
+	       printf("ERR\n");
+	       fflush(stdout);
+	       continue;
 	    }
-	    /* separate the four fields from the single input line of stdin */
+	    /* separate the fields from the single input line of stdin */
 	    buff_status = parse_buff(buff, &url, &src_addr, &ident, &method,
-				     &urlgroup, ip_list, pattern_list);
+				     &chanid, ip_list);
 	    /* error during parsing the passed line from squid - no rewrite */
 	    if(buff_status) {
-		puts("");
-		fflush(stdout);
-		continue;
+	       if (chanid != -1)
+		  printf("%d ",chanid);
+	       printf("ERR\n");
+	       fflush(stdout);
+	       continue;
 	    }
 	    /* check echo_mode again (sighup)*/
 	    if(echo_mode) {
-		puts("");
-		fflush(stdout);
-		mylog(ERROR, "Invalid condition - continuing in ECHO mode\n");
-		continue;
+	       if (chanid != -1)
+		  printf("%d ",chanid);
+	       printf("ERR\n");
+	       fflush(stdout);
+	       mylog(ERROR, "Invalid condition - continuing in ECHO mode\n");
+	       continue;
 	    }
 	    /* find a rule for rewriting the URL */
 	    val = pattern_compare(url, urlgroup, redirect_url, pattern_list);
-	    if( val < 1 ) {
-		/* no rule found = 0, or ABORT rule -N */
-		puts("");
-		fflush(stdout);
-		continue;
+	    if( val < 1 )
+	    {
+	       /* no rule found = 0, or ABORT rule -N */
+	       if (chanid != -1)
+		  printf("%d ", chanid);
+	       printf("OK\n");
+	       fflush(stdout);
+	       continue;
 	    }
-	    else {
-		/* redirect_url contains the replacement URL */
-		if ( redirect_url[0] == '\0' ) {
-		    /* regex[i] abort, i.e. empty replacement URL */
-		    puts("");
-		    fflush(stdout);
-		}
-		else {
-		    printf("%s %s %s %s\n",
-			   redirect_url, src_addr, ident, method);
-		    fflush(stdout);
-		    mylog(MATCH, "%s %s %s %d\n", src_addr, url, redirect_url,
+	    else
+	    {
+	       /* redirect_url contains the replacement URL */
+	       if ( redirect_url[0] == '\0' ) {
+		  /* regex[i] abort, i.e. empty replacement URL */
+		  if (chanid != -1)
+		     printf("%d ", chanid);
+		  printf("OK\n");
+		  fflush(stdout);
+	       }
+	       else
+	       {
+		  int code;
+		  char *just_url;
+
+		  if (chanid != -1)
+		     printf("%d ", chanid);
+
+		  /* az [2015-10-23 Fri 23:19]
+		     squid3.4 and 3.5 no longer support the 30X:url format,
+		     at least according to SOME of the very inconsistent docs.
+
+		     the protocol helper descripton page
+		     http://wiki.squid-cache.org/Features/AddonHelpers
+		     makes little sense; the protocol it describes is not at
+		     all what the url_rewrite_program describes.
+		     so i'll take a guess :-(
+
+		     the tool should provide a very different response
+		     for these...the sscanf isn't very efficient, but
+		     i can't be bothered rewriting the rules parser
+		     to support a separate code field...
+		  */
+		  if (2 == sscanf(redirect_url, "%d:%ms", &code, &just_url))
+		  {
+		     printf("OK code=%d url=%s\n", code, just_url);
+		     free(just_url);
+		  }
+		  else
+		     printf("OK rewrite-url=%s\n", redirect_url);
+		  fflush(stdout);
+		  mylog(MATCH, "%s %s %s %d\n", src_addr, url, redirect_url,
 			val);
-		}
+	       }
 	    }
 	}
 	if(! sig_hup)
--- a/rewrite.h
+++ b/rewrite.h
@@ -28,7 +28,7 @@
 #ifndef REWRITE_H
 #define REWRITE_H
 
-extern int parse_buff(char *, char **, char **, char **, char **, char **,
-		      ip_acl *, pattern_item *);
+extern int parse_buff(char *buff, char **url, char **src_addr, char **ident, char **method,
+		      int *chanid, ip_acl *ip);
 
 #endif
 
     |