1 const char filters_rcs[] = "$Id: filters.c,v 1.40 2001/10/26 17:37:55 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/filters.c,v $
6 * Purpose : Declares functions to parse/crunch headers and pages.
7 * Functions declared include:
8 * `acl_addr', `add_stats', `block_acl', `block_imageurl',
9 * `block_url', `url_actions', `domaincmp', `dsplit',
10 * `filter_popups', `forward_url', 'redirect_url',
11 * `ij_untrusted_url', `intercept_url', `pcrs_filter_respose',
12 * `show_proxy_args', 'ijb_send_banner', and `trust_url'
14 * Copyright : Written by and Copyright (C) 2001 the SourceForge
15 * IJBSWA team. http://ijbswa.sourceforge.net
17 * Based on the Internet Junkbuster originally written
18 * by and Copyright (C) 1997 Anonymous Coders and
19 * Junkbusters Corporation. http://www.junkbusters.com
21 * This program is free software; you can redistribute it
22 * and/or modify it under the terms of the GNU General
23 * Public License as published by the Free Software
24 * Foundation; either version 2 of the License, or (at
25 * your option) any later version.
27 * This program is distributed in the hope that it will
28 * be useful, but WITHOUT ANY WARRANTY; without even the
29 * implied warranty of MERCHANTABILITY or FITNESS FOR A
30 * PARTICULAR PURPOSE. See the GNU General Public
31 * License for more details.
33 * The GNU General Public License should be included with
34 * this file. If not, you can view it at
35 * http://www.gnu.org/copyleft/gpl.html
36 * or write to the Free Software Foundation, Inc., 59
37 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 * Revision 1.40 2001/10/26 17:37:55 oes
42 * - Re-enabled Netscape 200/404 bug workaround in block_url():
43 * - Removed OS/2 special case
44 * - Made block_url() independant from sed() having been run
45 * - Made trust_url independant from sed() having been run
46 * - Made is_imageurl independant from sed() having been run.
47 * It now checks User-Agent: and Accept: by itself.
50 * Revision 1.39 2001/10/25 03:40:48 david__schmidt
51 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
52 * threads to call select() simultaneously. So, it's time to do a real, live,
53 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
54 * (native). Both versions will work, but using __OS2__ offers multi-threading.
56 * Revision 1.38 2001/10/23 21:32:33 jongfoster
57 * Adding error-checking to selected functions
59 * Revision 1.37 2001/10/22 15:33:56 david__schmidt
60 * Special-cased OS/2 out of the Netscape-abort-on-404-in-js problem in
61 * filters.c. Added a FIXME in front of the offending code. I'll gladly
62 * put in a better/more robust fix for all parties if one is presented...
63 * It seems that just returning 200 instead of 404 would pretty much fix
64 * it for everyone, but I don't know all the history of the problem.
66 * Revision 1.36 2001/10/10 16:44:16 oes
67 * Added match_portlist function
69 * Revision 1.35 2001/10/07 15:41:23 oes
70 * Replaced 6 boolean members of csp with one bitmap (csp->flags)
72 * New function remove_chunked_transfer_coding that strips chunked
73 * transfer coding to plain and is called by pcrs_filter_response
74 * and gif_deanimate_response if neccessary
76 * Improved handling of zero-change re_filter runs
78 * pcrs_filter_response and gif_deanimate_response now remove
79 * chunked transfer codeing before processing the body.
81 * Revision 1.34 2001/09/20 15:49:36 steudten
83 * Fix BUG: Change int size to size_t size in pcrs_filter_response().
84 * See cgi.c fill_template().
86 * Revision 1.33 2001/09/16 17:05:14 jongfoster
87 * Removing unused #include showarg.h
89 * Revision 1.32 2001/09/16 13:21:27 jongfoster
90 * Changes to use new list functions.
92 * Revision 1.31 2001/09/16 11:38:02 jongfoster
93 * Splitting fill_template() into 2 functions:
94 * template_load() loads the file
95 * template_fill() performs the PCRS regexps.
96 * This is because the CGI edit interface has a "table row"
97 * template which is used many times in the page - this
98 * change means it's only loaded from disk once.
100 * Revision 1.30 2001/09/16 11:00:10 jongfoster
101 * New function alloc_http_response, for symmetry with free_http_response
103 * Revision 1.29 2001/09/13 23:32:40 jongfoster
104 * Moving image data to cgi.c rather than cgi.h
105 * Fixing a GPF under Win32 (and any other OS that protects global
106 * constants from being written to).
108 * Revision 1.28 2001/09/10 10:18:51 oes
109 * Silenced compiler warnings
111 * Revision 1.27 2001/08/05 16:06:20 jongfoster
112 * Modifiying "struct map" so that there are now separate header and
113 * "map_entry" structures. This means that functions which modify a
114 * map no longer need to return a pointer to the modified map.
115 * Also, it no longer reverses the order of the entries (which may be
116 * important with some advanced template substitutions).
118 * Revision 1.26 2001/07/30 22:08:36 jongfoster
119 * Tidying up #defines:
120 * - All feature #defines are now of the form FEATURE_xxx
121 * - Permanently turned off WIN_GUI_EDIT
122 * - Permanently turned on WEBDAV and SPLIT_PROXY_ARGS
124 * Revision 1.25 2001/07/26 10:09:46 oes
125 * Made browser detection a little less naive
127 * Revision 1.24 2001/07/25 17:22:51 oes
128 * Added workaround for Netscape bug that prevents display of page when loading a component fails.
130 * Revision 1.23 2001/07/23 13:40:12 oes
131 * Fixed bug that caused document body to be dropped when pcrs joblist was empty.
133 * Revision 1.22 2001/07/18 12:29:34 oes
134 * - Made gif_deanimate_response respect
135 * csp->action->string[ACTION_STRING_DEANIMATE]
136 * - Logging cosmetics
138 * Revision 1.21 2001/07/13 13:59:53 oes
139 * - Introduced gif_deanimate_response which shares the
140 * generic content modification interface of pcrs_filter_response
141 * and acts as a wrapper to deanimate.c:gif_deanimate()
142 * - Renamed re_process_buffer to pcrs_filter_response
143 * - pcrs_filter_response now returns NULL on failiure
144 * - Removed all #ifdef PCRS
146 * Revision 1.20 2001/07/01 17:01:04 oes
147 * Added comments and missing return statement in is_untrusted_url()
149 * Revision 1.19 2001/06/29 21:45:41 oes
150 * Indentation, CRLF->LF, Tab-> Space
152 * Revision 1.18 2001/06/29 13:27:38 oes
153 * - Cleaned up, renamed and reorderd functions
154 * and improved comments
157 * - Ported to CGI platform. Now delivers
158 * http_response or NULL
159 * - Unified HTML and GIF generation (moved image detection
160 * and GIF generation here from jcc.c:chat())
161 * - Fixed HTTP status to:
162 * - 403 (Forbidden) for the "blocked" HTML message
163 * - 200 (OK) for GIF answers
164 * - 302 (Redirect) for redirect to GIF
167 * - Ported to CGI platform. Now delivers
168 * http_response or NULL
169 * - Separated detection of untrusted URL into
170 * (bool)is_untrusted_url
171 * - Added enforcement of untrusted requests
173 * - Moved redirect_url() from cgi.c to here
174 * and ported it to the CGI platform
176 * - Removed logentry from cancelled commit
178 * Revision 1.17 2001/06/09 10:55:28 jongfoster
179 * Changing BUFSIZ ==> BUFFER_SIZE
181 * Revision 1.16 2001/06/07 23:10:26 jongfoster
182 * Allowing unanchored domain patterns to back off and retry
183 * if they partially match. Optimized right-anchored patterns.
184 * Moving ACL and forward files into config file.
185 * Replacing struct gateway with struct forward_spec
187 * Revision 1.15 2001/06/03 19:12:00 oes
188 * extracted-CGI relevant stuff
190 * Revision 1.14 2001/06/01 10:30:55 oes
191 * Added optional left-anchoring to domaincmp
193 * Revision 1.13 2001/05/31 21:21:30 jongfoster
194 * Permissionsfile / actions file changes:
195 * - Changed "permission" to "action" throughout
196 * - changes to file format to allow string parameters
197 * - Moved helper functions to actions.c
199 * Revision 1.12 2001/05/31 17:35:20 oes
201 * - Enhanced domain part globbing with infix and prefix asterisk
202 * matching and optional unanchored operation
204 * Revision 1.11 2001/05/29 11:53:23 oes
205 * "See why" link added to "blocked" page
207 * Revision 1.10 2001/05/29 09:50:24 jongfoster
208 * Unified blocklist/imagelist/permissionslist.
209 * File format is still under discussion, but the internal changes
212 * Also modified interceptor behaviour:
213 * - We now intercept all URLs beginning with one of the following
214 * prefixes (and *only* these prefixes):
216 * * http://ijbswa.sf.net/config/
217 * * http://ijbswa.sourceforge.net/config/
218 * - New interceptors "home page" - go to http://i.j.b/ to see it.
219 * - Internal changes so that intercepted and fast redirect pages
220 * are not replaced with an image.
221 * - Interceptors now have the option to send a binary page direct
222 * to the client. (i.e. ijb-send-banner uses this)
223 * - Implemented show-url-info interceptor. (Which is why I needed
224 * the above interceptors changes - a typical URL is
225 * "http://i.j.b/show-url-info?url=www.somesite.com/banner.gif".
226 * The previous mechanism would not have intercepted that, and
227 * if it had been intercepted then it then it would have replaced
230 * Revision 1.9 2001/05/27 22:17:04 oes
232 * - re_process_buffer no longer writes the modified buffer
233 * to the client, which was very ugly. It now returns the
234 * buffer, which it is then written by chat.
236 * - content_length now adjusts the Content-Length: header
237 * for modified documents rather than crunch()ing it.
238 * (Length info in csp->content_length, which is 0 for
239 * unmodified documents)
241 * - For this to work, sed() is called twice when filtering.
243 * Revision 1.8 2001/05/26 17:13:28 jongfoster
244 * Filled in a function comment.
246 * Revision 1.7 2001/05/26 15:26:15 jongfoster
247 * ACL feature now provides more security by immediately dropping
248 * connections from untrusted hosts.
250 * Revision 1.6 2001/05/26 00:28:36 jongfoster
251 * Automatic reloading of config file.
252 * Removed obsolete SIGHUP support (Unix) and Reload menu option (Win32).
253 * Most of the global variables have been moved to a new
254 * struct configuration_spec, accessed through csp->config->globalname
255 * Most of the globals remaining are used by the Win32 GUI.
257 * Revision 1.5 2001/05/25 22:34:30 jongfoster
260 * Revision 1.4 2001/05/22 18:46:04 oes
262 * - Enabled filtering banners by size rather than URL
263 * by adding patterns that replace all standard banner
264 * sizes with the "Junkbuster" gif to the re_filterfile
266 * - Enabled filtering WebBugs by providing a pattern
267 * which kills all 1x1 images
269 * - Added support for PCRE_UNGREEDY behaviour to pcrs,
270 * which is selected by the (nonstandard and therefore
271 * capital) letter 'U' in the option string.
272 * It causes the quantifiers to be ungreedy by default.
273 * Appending a ? turns back to greedy (!).
275 * - Added a new interceptor ijb-send-banner, which
276 * sends back the "Junkbuster" gif. Without imagelist or
277 * MSIE detection support, or if tinygif = 1, or the
278 * URL isn't recognized as an imageurl, a lame HTML
279 * explanation is sent instead.
281 * - Added new feature, which permits blocking remote
282 * script redirects and firing back a local redirect
284 * The feature is conditionally compiled, i.e. it
285 * can be disabled with --disable-fast-redirects,
286 * plus it must be activated by a "fast-redirects"
287 * line in the config file, has its own log level
288 * and of course wants to be displayed by show-proxy-args
289 * Note: Boy, all the #ifdefs in 1001 locations and
290 * all the fumbling with configure.in and acconfig.h
291 * were *way* more work than the feature itself :-(
293 * - Because a generic redirect template was needed for
294 * this, tinygif = 3 now uses the same.
296 * - Moved GIFs, and other static HTTP response templates
301 * - Removed some >400 CRs again (Jon, you really worked
304 * Revision 1.3 2001/05/20 16:44:47 jongfoster
305 * Removing last hardcoded JunkBusters.com URLs.
307 * Revision 1.2 2001/05/20 01:21:20 jongfoster
308 * Version 2.9.4 checkin.
309 * - Merged popupfile and cookiefile, and added control over PCRS
310 * filtering, in new "permissionsfile".
311 * - Implemented LOG_LEVEL_FATAL, so that if there is a configuration
312 * file error you now get a message box (in the Win32 GUI) rather
313 * than the program exiting with no explanation.
314 * - Made killpopup use the PCRS MIME-type checking and HTTP-header
316 * - Removed tabs from "config"
317 * - Moved duplicated url parsing code in "loaders.c" to a new funcition.
318 * - Bumped up version number.
320 * Revision 1.1.1.1 2001/05/15 13:58:52 oes
321 * Initial import of version 2.9.3 source tree
324 *********************************************************************/
330 #include <sys/types.h>
339 #endif /* ndef __OS2__ */
340 #include <netinet/in.h>
342 #include <winsock2.h>
343 #endif /* ndef _WIN32 */
347 #endif /* def __OS2__ */
355 #include "jbsockets.h"
356 #include "miscutil.h"
360 #include "deanimate.h"
366 const char filters_h_rcs[] = FILTERS_H_VERSION;
368 /* Fix a problem with Solaris. There should be no effect on other
370 * Solaris's isspace() is a macro which uses it's argument directly
371 * as an array index. Therefore we need to make sure that high-bit
372 * characters generate +ve values, and ideally we also want to make
373 * the argument match the declared parameter type of "int".
375 #define ijb_isdigit(__X) isdigit((int)(unsigned char)(__X))
379 /*********************************************************************
381 * Function : block_acl
383 * Description : Block this request?
384 * Decide yes or no based on ACL file.
387 * 1 : dst = The proxy or gateway address this is going to.
388 * Or NULL to check all possible targets.
389 * 2 : csp = Current client state (buffers, headers, etc...)
390 * Also includes the client IP address.
392 * Returns : 0 = FALSE (don't block) and 1 = TRUE (do block)
394 *********************************************************************/
395 int block_acl(struct access_control_addr *dst, struct client_state *csp)
397 struct access_control_list *acl = csp->config->acl;
399 /* if not using an access control list, then permit the connection */
405 /* search the list */
408 if ((csp->ip_addr_long & acl->src->mask) == acl->src->addr)
412 /* Just want to check if they have any access */
413 if (acl->action == ACL_PERMIT)
418 else if ( ((dst->addr & acl->dst->mask) == acl->dst->addr)
419 && ((dst->port == acl->dst->port) || (acl->dst->port == 0)))
421 if (acl->action == ACL_PERMIT)
439 /*********************************************************************
441 * Function : acl_addr
443 * Description : Called from `load_aclfile' to parse an ACL address.
446 * 1 : aspec = String specifying ACL address.
447 * 2 : aca = struct access_control_addr to fill in.
449 * Returns : 0 => Ok, everything else is an error.
451 *********************************************************************/
452 int acl_addr(char *aspec, struct access_control_addr *aca)
454 int i, masklength, port;
460 if ((p = strchr(aspec, '/')))
464 if (ijb_isdigit(*p) == 0)
468 masklength = atoi(p);
471 if ((masklength < 0) || (masklength > 32))
476 if ((p = strchr(aspec, ':')))
480 if (ijb_isdigit(*p) == 0)
489 aca->addr = ntohl(resolve_hostname_to_ip(aspec));
493 log_error(LOG_LEVEL_ERROR, "can't resolve address for %s", aspec);
497 /* build the netmask */
499 for (i=1; i <= masklength ; i++)
501 aca->mask |= (1 << (32 - i));
504 /* now mask off the host portion of the ip address
505 * (i.e. save on the network portion of the address).
507 aca->addr = aca->addr & aca->mask;
512 #endif /* def FEATURE_ACL */
515 /*********************************************************************
517 * Function : match_portlist
519 * Description : Check if a given number is covered by a comma
520 * separated list of numbers and ranges (a,b-c,d,..)
523 * 1 : portlist = String with list
524 * 2 : port = port to check
526 * Returns : 0 => no match
529 *********************************************************************/
530 int match_portlist(const char *portlist, int port)
532 char *min, *max, *next, *portlist_copy;
534 min = next = portlist_copy = strdup(portlist);
537 * Zero-terminate first item and remember offset for next
539 if (NULL != (next = strchr(portlist_copy, (int) ',')))
545 * Loop through all items, checking for match
549 if (NULL == (max = strchr(min, (int) '-')))
552 * No dash, check for equality
554 if (port == atoi(min))
563 * This is a range, so check if between min and max,
564 * or, if max was omitted, between min and 65K
567 if(port >= atoi(min) && port <= (atoi(max) ? atoi(max) : 65535))
581 * Zero-terminate next item and remember offset for n+1
583 if ((NULL != next) && (NULL != (next = strchr(next, (int) ','))))
595 /*********************************************************************
597 * Function : block_url
599 * Description : Called from `chat'. Check to see if we need to block this.
602 * 1 : csp = Current client state (buffers, headers, etc...)
604 * Returns : NULL => unblocked, else HTTP block response
606 *********************************************************************/
607 struct http_response *block_url(struct client_state *csp)
609 #ifdef FEATURE_IMAGE_BLOCKING
611 #endif /* def FEATURE_IMAGE_BLOCKING */
612 struct http_response *rsp;
615 * If it's not blocked, don't block it ;-)
617 if ((csp->action->flags & ACTION_BLOCK) == 0)
623 * Else, prepare a response
625 if (NULL == (rsp = alloc_http_response()))
627 return cgi_error_memory();
631 * If it's an image-url, send back an image or redirect
632 * as specified by the relevant +image action
634 #ifdef FEATURE_IMAGE_BLOCKING
635 if (((csp->action->flags & ACTION_IMAGE_BLOCKER) != 0)
638 /* determine HOW images should be blocked */
639 p = csp->action->string[ACTION_STRING_IMAGE_BLOCKER];
641 /* and handle accordingly: */
642 if ((p == NULL) || (0 == strcmpic(p, "logo")))
644 rsp->body = bindup(image_junkbuster_gif_data, image_junkbuster_gif_length);
645 if (rsp->body == NULL)
647 free_http_response(rsp);
648 return cgi_error_memory();
650 rsp->content_length = image_junkbuster_gif_length;
652 if (enlist_unique_header(rsp->headers, "Content-Type", "image/gif"))
654 free_http_response(rsp);
655 return cgi_error_memory();
659 else if (0 == strcmpic(p, "blank"))
661 rsp->body = bindup(image_blank_gif_data, image_blank_gif_length);
662 if (rsp->body == NULL)
664 free_http_response(rsp);
665 return cgi_error_memory();
667 rsp->content_length = image_blank_gif_length;
669 if (enlist_unique_header(rsp->headers, "Content-Type", "image/gif"))
671 free_http_response(rsp);
672 return cgi_error_memory();
678 rsp->status = strdup("302 Local Redirect from Junkbuster");
679 if (rsp->status == NULL)
681 free_http_response(rsp);
682 return cgi_error_memory();
685 if (enlist_unique_header(rsp->headers, "Location", p))
687 free_http_response(rsp);
688 return cgi_error_memory();
693 #endif /* def FEATURE_IMAGE_BLOCKING */
696 * Else, generate an HTML "blocked" message:
700 struct map * exports;
703 * Workaround for stupid Netscape bug which prevents
704 * pages from being displayed if loading a referenced
705 * JavaScript or style sheet fails. So make it appear
706 * as if it succeeded.
708 if ( NULL != (p = get_header_value(csp->headers, "User-Agent:"))
709 && !strncmpic(p, "mozilla", 7) /* Catch Netscape but */
710 && !strstr(p, "Gecko") /* save Mozilla, */
711 && !strstr(p, "compatible") /* MSIE */
712 && !strstr(p, "Opera")) /* and Opera. */
714 rsp->status = strdup("200 Request for blocked URL");
718 rsp->status = strdup("404 Request for blocked URL");
721 if (rsp->status == NULL)
723 free_http_response(rsp);
724 return cgi_error_memory();
727 exports = default_exports(csp, NULL);
730 free_http_response(rsp);
731 return cgi_error_memory();
734 #ifdef FEATURE_FORCE_LOAD
735 err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
736 #else /* ifndef FEATURE_FORCE_LOAD */
737 err = map_block_killer(exports, "force-support");
738 #endif /* ndef FEATURE_FORCE_LOAD */
740 if (!err) err = map(exports, "hostport", 1, csp->http->hostport, 1);
741 if (!err) err = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
742 if (!err) err = map(exports, "path", 1, csp->http->path, 1);
743 if (!err) err = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
748 free_http_response(rsp);
749 return cgi_error_memory();
752 err = template_fill_for_cgi(csp, "blocked", exports, rsp);
755 free_http_response(rsp);
756 return cgi_error_memory();
760 return finish_http_response(rsp);
766 /*********************************************************************
768 * Function : trust_url FIXME: I should be called distrust_url
770 * Description : Calls is_untrusted_url to determine if the URL is trusted
771 * and if not, returns a HTTP 304 response with a reject message.
774 * 1 : csp = Current client state (buffers, headers, etc...)
776 * Returns : NULL => trusted, else http_response.
778 *********************************************************************/
779 struct http_response *trust_url(struct client_state *csp)
781 struct http_response *rsp;
782 struct map * exports;
783 char buf[BUFFER_SIZE];
785 struct url_spec **tl;
790 * Don't bother to work on trusted URLs
792 if (!is_untrusted_url(csp))
798 * Else, prepare a response:
800 if (NULL == (rsp = alloc_http_response()))
802 return cgi_error_memory();
805 exports = default_exports(csp, NULL);
808 free_http_response(rsp);
809 return cgi_error_memory();
813 * Export the host, port, and referrer information
815 err = map(exports, "hostport", 1, csp->http->hostport, 1);
816 if (!err) err = map(exports, "path", 1, csp->http->path, 1);
817 if (!err) err = map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0);
818 if (!err) err = map(exports, "path-html", 1, html_encode(csp->http->path), 0);
820 if (NULL != (p = get_header_value(csp->headers, "Referer:")))
822 if (!err) err = map(exports, "referrer", 1, p, 1);
823 if (!err) err = map(exports, "referrer-html", 1, html_encode(p), 0);
827 if (!err) err = map(exports, "referrer", 1, "unknown", 1);
828 if (!err) err = map(exports, "referrer-html", 1, "unknown", 1);
834 free_http_response(rsp);
835 return cgi_error_memory();
839 * Export the trust list
842 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
844 sprintf(buf, "<li>%s</li>\n", t->spec);
845 string_append(&p, buf);
847 err = map(exports, "trusted-referrers", 1, p, 0);
852 free_http_response(rsp);
853 return cgi_error_memory();
857 * Export the trust info, if available
859 if (csp->config->trust_info->first)
861 struct list_entry *l;
864 for (l = csp->config->trust_info->first; l ; l = l->next)
866 sprintf(buf, "<li> <a href=%s>%s</a><br>\n",l->str, l->str);
867 string_append(&p, buf);
869 err = map(exports, "trust-info", 1, p, 0);
873 err = map_block_killer(exports, "have-trust-info");
879 free_http_response(rsp);
880 return cgi_error_memory();
884 * Export the force prefix or the force conditional block killer
886 #ifdef FEATURE_FORCE_LOAD
887 err = map(exports, "force-prefix", 1, FORCE_PREFIX, 1);
888 #else /* ifndef FEATURE_FORCE_LOAD */
889 err = map_block_killer(exports, "force-support");
890 #endif /* ndef FEATURE_FORCE_LOAD */
895 free_http_response(rsp);
896 return cgi_error_memory();
902 err = template_fill_for_cgi(csp, "untrusted", exports, rsp);
905 free_http_response(rsp);
906 return cgi_error_memory();
909 return finish_http_response(rsp);
911 #endif /* def FEATURE_TRUST */
914 #ifdef FEATURE_FAST_REDIRECTS
915 /*********************************************************************
917 * Function : redirect_url
919 * Description : Checks for redirection URLs and returns a HTTP redirect
920 * to the destination URL, if necessary
923 * 1 : csp = Current client state (buffers, headers, etc...)
925 * Returns : NULL if URL was clean, HTTP redirect otherwise.
927 *********************************************************************/
928 struct http_response *redirect_url(struct client_state *csp)
931 struct http_response *rsp;
933 p = q = csp->http->path;
934 log_error(LOG_LEVEL_REDIRECTS, "checking path for redirects: %s", p);
937 * find the last URL encoded in the request
939 while ((p = strstr(p, "http://")))
945 * if there was any, generate and return a HTTP redirect
947 if (q != csp->http->path)
949 log_error(LOG_LEVEL_REDIRECTS, "redirecting to: %s", q);
951 if (NULL == (rsp = alloc_http_response()))
953 return cgi_error_memory();
956 if ( enlist_unique_header(rsp->headers, "Location", q)
957 || (NULL == (rsp->status = strdup("302 Local Redirect from Junkbuster"))) )
959 free_http_response(rsp);
960 return cgi_error_memory();
963 return finish_http_response(rsp);
971 #endif /* def FEATURE_FAST_REDIRECTS */
974 #ifdef FEATURE_IMAGE_BLOCKING
975 /*********************************************************************
977 * Function : is_imageurl
979 * Description : Given a URL, decide whether it is an image or not,
980 * using either the info from a previous +image action
981 * or, #ifdef FEATURE_IMAGE_DETECT_MSIE, the info from
982 * the browser's accept header.
985 * 1 : csp = Current client state (buffers, headers, etc...)
987 * Returns : True (nonzero) if URL is an image, false (0)
990 *********************************************************************/
991 int is_imageurl(struct client_state *csp)
993 #ifdef FEATURE_IMAGE_DETECT_MSIE
996 tmp = get_header_value(csp->headers, "User-Agent:");
997 if (tmp && strstr(tmp, "MSIE"))
999 tmp = get_header_value(csp->headers, "Accept:");
1000 if (tmp && strstr(tmp, "image/gif"))
1002 /* Client will accept HTML. If this seems counterintuitive,
1012 #endif /* def FEATURE_IMAGE_DETECT_MSIE */
1014 return ((csp->action->flags & ACTION_IMAGE) != 0);
1017 #endif /* def FEATURE_IMAGE_BLOCKING */
1020 #ifdef FEATURE_COOKIE_JAR
1021 /*********************************************************************
1023 * Function : is_untrusted_url
1025 * Description : Should we "distrust" this URL (and block it)?
1027 * Yes if it matches a line in the trustfile, or if the
1028 * referrer matches a line starting with "+" in the
1033 * 1 : csp = Current client state (buffers, headers, etc...)
1035 * Returns : 0 => trusted, 1 => untrusted
1037 *********************************************************************/
1038 int is_untrusted_url(struct client_state *csp)
1040 struct file_list *fl;
1041 struct block_spec *b;
1042 struct url_spec url[1], **tl, *t;
1043 struct http_request rhttp[1];
1047 * If we don't have a trustlist, we trust everybody
1049 if (((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL))
1056 * Do we trust the request URL itself?
1058 *url = dsplit(csp->http->host);
1060 /* if splitting the domain fails, punt */
1061 if (url->dbuf == NULL) return(0);
1063 memset(rhttp, '\0', sizeof(*rhttp));
1065 for (b = b->next; b ; b = b->next)
1067 if ((b->url->port == 0) || (b->url->port == csp->http->port))
1069 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1071 if ((b->url->path == NULL) ||
1073 (regexec(b->url->preg, csp->http->path, 0, NULL, 0) == 0)
1075 (strncmp(b->url->path, csp->http->path, b->url->pathlen) == 0)
1082 if (b->reject == 0) return(0);
1093 if (NULL == (h = get_header_value(csp->headers, "Referer:")))
1095 /* no referrer was supplied */
1099 /* forge a URL from the referrer so we can use
1100 * convert_url() to parse it into its components.
1104 p = strsav(p, "GET ");
1106 p = strsav(p, " HTTP/1.0");
1108 parse_http_request(p, rhttp, csp);
1111 if (rhttp->cmd == NULL)
1118 * If not, do we maybe trust its referrer?
1120 *url = dsplit(rhttp->host);
1122 /* if splitting the domain fails, punt */
1123 if (url->dbuf == NULL) return(1);
1125 for (tl = csp->config->trust_list; (t = *tl) ; tl++)
1127 if ((t->port == 0) || (t->port == rhttp->port))
1129 if ((t->domain[0] == '\0') || domaincmp(t, url) == 0)
1131 if ((t->path == NULL) ||
1133 (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0)
1135 (strncmp(t->path, rhttp->path, t->pathlen) == 0)
1139 /* if the URL's referrer is from a trusted referrer, then
1140 * add the target spec to the trustfile as an unblocked
1141 * domain and return NULL (which means it's OK).
1149 if ((fp = fopen(csp->config->trustfile, "a")))
1154 h = strsav(h, csp->http->hostport);
1156 p = csp->http->path;
1160 /* since this path points into a user's home space
1161 * be sure to include this spec in the trustfile.
1163 if ((p = strchr(p, '/')))
1166 h = strsav(h, csp->http->path); /* FIXME: p?! */
1171 fprintf(fp, "%s\n", h);
1182 #endif /* def FEATURE_COOKIE_JAR */
1185 /*********************************************************************
1187 * Function : pcrs_filter_response
1189 * Description : Apply all the pcrs jobs from the joblist (re_filterfile)
1190 * to the text buffer that's been accumulated in
1191 * csp->iob->buf and set csp->content_length to the modified
1192 * size and raise the CSP_FLAG_MODIFIED flag if appropriate.
1195 * 1 : csp = Current client state (buffers, headers, etc...)
1197 * Returns : a pointer to the (newly allocated) modified buffer.
1198 * or NULL in case something went wrong
1200 *********************************************************************/
1201 char *pcrs_filter_response(struct client_state *csp)
1206 char *old = csp->iob->cur, *new = NULL;
1209 struct file_list *fl;
1210 struct re_filterfile_spec *b;
1213 if (csp->iob->cur >= csp->iob->eod)
1217 size = csp->iob->eod - csp->iob->cur;
1220 * If the body has a "chunked" transfer-encoding,
1221 * get rid of it first, adjusting size and iob->eod
1223 if (csp->flags & CSP_FLAG_CHUNKED)
1225 log_error(LOG_LEVEL_RE_FILTER, "Need to de-chunk first");
1226 if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1230 csp->iob->eod = csp->iob->cur + size;
1231 csp->flags |= CSP_FLAG_MODIFIED;
1234 if ( ( NULL == (fl = csp->rlist) ) || ( NULL == (b = fl->f) ) )
1236 log_error(LOG_LEVEL_ERROR, "Unable to get current state of regexp filtering.");
1240 if ( NULL == b->joblist )
1242 log_error(LOG_LEVEL_RE_FILTER, "Empty joblist. Nothing to do.");
1246 log_error(LOG_LEVEL_RE_FILTER, "re_filtering %s%s (size %d) ...",
1247 csp->http->hostport, csp->http->path, size);
1249 /* Apply all jobs from the joblist */
1250 for (job = b->joblist; NULL != job; job = job->next)
1252 hits += pcrs_execute(job, old, size, &new, &size);
1253 if (old != csp->iob->cur) free(old);
1257 log_error(LOG_LEVEL_RE_FILTER, " produced %d hits (new size %d).", hits, size);
1260 * If there were no hits, destroy our copy and let
1261 * chat() use the original in csp->iob
1269 csp->flags |= CSP_FLAG_MODIFIED;
1270 csp->content_length = size;
1278 /*********************************************************************
1280 * Function : gif_deanimate_response
1282 * Description : Deanimate the GIF image that has been accumulated in
1283 * csp->iob->buf, set csp->content_length to the modified
1284 * size and raise the CSP_FLAG_MODIFIED flag.
1287 * 1 : csp = Current client state (buffers, headers, etc...)
1289 * Returns : a pointer to the (newly allocated) modified buffer.
1290 * or NULL in case something went wrong.
1292 *********************************************************************/
1293 char *gif_deanimate_response(struct client_state *csp)
1295 struct binbuffer *in, *out;
1297 int size = csp->iob->eod - csp->iob->cur;
1300 * If the body has a "chunked" transfer-encoding,
1301 * get rid of it first, adjusting size and iob->eod
1303 if (csp->flags & CSP_FLAG_CHUNKED)
1305 log_error(LOG_LEVEL_DEANIMATE, "Need to de-chunk first");
1306 if (0 == (size = remove_chunked_transfer_coding(csp->iob->cur, size)))
1310 csp->iob->eod = csp->iob->cur + size;
1311 csp->flags |= CSP_FLAG_MODIFIED;
1314 if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
1315 || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
1317 log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
1321 in->buffer = csp->iob->cur;
1324 if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
1326 log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
1333 log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
1334 csp->content_length = out->offset;
1335 csp->flags |= CSP_FLAG_MODIFIED;
1345 /*********************************************************************
1347 * Function : remove_chunked_transfer_coding
1349 * Description : In-situ remove the "chunked" transfer coding as defined
1350 * in rfc2616 from a buffer.
1353 * 1 : buffer = Pointer to the text buffer
1354 * 2 : size = Number of bytes to be processed
1356 * Returns : The new size, i.e. the number of bytes from buffer which
1357 * are occupied by the stripped body, or 0 in case something
1360 *********************************************************************/
1361 int remove_chunked_transfer_coding(char *buffer, const size_t size)
1364 unsigned int chunksize = 0;
1365 char *from_p, *to_p;
1368 from_p = to_p = buffer;
1370 if (sscanf(buffer, "%x", &chunksize) != 1)
1372 log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
1376 while (chunksize > 0)
1378 if (NULL == (from_p = strstr(from_p, "\r\n")))
1380 log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1383 newsize += chunksize;
1386 memmove(to_p, from_p, (size_t) chunksize);
1387 to_p = buffer + newsize;
1388 from_p += chunksize + 2;
1390 if (sscanf(from_p, "%x", &chunksize) != 1)
1392 log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
1397 /* FIXME: Should this get its own loglevel? */
1398 log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d\n", size, newsize);
1404 /*********************************************************************
1406 * Function : url_actions
1408 * Description : Gets the actions for this URL.
1411 * 1 : http = http_request request for blocked URLs
1412 * 2 : csp = Current client state (buffers, headers, etc...)
1416 *********************************************************************/
1417 void url_actions(struct http_request *http,
1418 struct client_state *csp)
1420 struct file_list *fl;
1421 struct url_actions *b;
1423 init_current_action(csp->action);
1425 if (((fl = csp->actions_list) == NULL) || ((b = fl->f) == NULL))
1430 apply_url_actions(csp->action, http, b);
1435 /*********************************************************************
1437 * Function : apply_url_actions
1439 * Description : Applies a list of URL actions.
1442 * 1 : action = Destination.
1443 * 2 : http = Current URL
1444 * 3 : b = list of URL actions to apply
1448 *********************************************************************/
1449 void apply_url_actions(struct current_action_spec *action,
1450 struct http_request *http,
1451 struct url_actions *b)
1453 struct url_spec url[1];
1457 /* Should never happen */
1461 *url = dsplit(http->host);
1463 /* if splitting the domain fails, punt */
1464 if (url->dbuf == NULL)
1469 for (b = b->next; NULL != b; b = b->next)
1471 if ((b->url->port == 0) || (b->url->port == http->port))
1473 if ((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0))
1475 if ((b->url->path == NULL) ||
1477 (regexec(b->url->preg, http->path, 0, NULL, 0) == 0)
1479 (strncmp(b->url->path, http->path, b->url->pathlen) == 0)
1483 merge_current_action(action, b->action);
1494 /*********************************************************************
1496 * Function : forward_url
1498 * Description : Should we forward this to another proxy?
1501 * 1 : http = http_request request for current URL
1502 * 2 : csp = Current client state (buffers, headers, etc...)
1504 * Returns : Pointer to forwarding information.
1506 *********************************************************************/
1507 const struct forward_spec * forward_url(struct http_request *http,
1508 struct client_state *csp)
1510 static const struct forward_spec fwd_default[1] = { FORWARD_SPEC_INITIALIZER };
1511 struct forward_spec *fwd = csp->config->forward;
1512 struct url_spec url[1];
1516 return(fwd_default);
1519 *url = dsplit(http->host);
1521 /* if splitting the domain fails, punt */
1522 if (url->dbuf == NULL)
1524 return(fwd_default);
1529 if ((fwd->url->port == 0) || (fwd->url->port == http->port))
1531 if ((fwd->url->domain[0] == '\0') || (domaincmp(fwd->url, url) == 0))
1533 if ((fwd->url->path == NULL) ||
1535 (regexec(fwd->url->preg, http->path, 0, NULL, 0) == 0)
1537 (strncmp(fwd->url->path, http->path, fwd->url->pathlen) == 0)
1552 return(fwd_default);
1557 /*********************************************************************
1561 * Description : Takes a domain and returns a pointer to a url_spec
1562 * structure populated with dbuf, dcnt and dvec. The
1563 * other fields in the structure that is returned are zero.
1566 * 1 : domain = a URL address
1568 * Returns : url_spec structure populated with dbuf, dcnt and dvec.
1569 * On error, the dbuf field will be set to NULL. (As
1570 * will all the others, but you don't need to check
1573 * FIXME: Returning a structure is horribly inefficient, please can
1574 * this structure take a (struct url_spec * dest)
1577 *********************************************************************/
1578 struct url_spec dsplit(char *domain)
1580 struct url_spec ret[1];
1581 char *v[BUFFER_SIZE];
1585 memset(ret, '\0', sizeof(*ret));
1587 if (domain[strlen(domain) - 1] == '.')
1589 ret->unanchored |= ANCHOR_RIGHT;
1592 if (domain[0] == '.')
1594 ret->unanchored |= ANCHOR_LEFT;
1597 ret->dbuf = strdup(domain);
1598 if (NULL == ret->dbuf)
1603 /* map to lower case */
1604 for (p = ret->dbuf; *p ; p++)
1606 *p = tolower((int)(unsigned char)*p);
1609 /* split the domain name into components */
1610 ret->dcnt = ssplit(ret->dbuf, ".", v, SZ(v), 1, 1);
1615 memset(ret, '\0', sizeof(ret));
1618 else if (ret->dcnt == 0)
1623 /* save a copy of the pointers in dvec */
1624 size = ret->dcnt * sizeof(*ret->dvec);
1626 ret->dvec = (char **)malloc(size);
1627 if (NULL == ret->dvec)
1630 memset(ret, '\0', sizeof(ret));
1634 memcpy(ret->dvec, v, size);
1641 /*********************************************************************
1643 * Function : simple_domaincmp
1645 * Description : Domain-wise Compare fqdn's. The comparison is
1646 * both left- and right-anchored. The individual
1647 * domain names are compared with simplematch().
1648 * This is only used by domaincmp.
1651 * 1 : pv = array of patterns to compare
1652 * 2 : fv = array of domain components to compare
1653 * 3 : len = length of the arrays (both arrays are the
1654 * same length - if they weren't, it couldn't
1655 * possibly be a match).
1657 * Returns : 0 => domains are equivalent, else no match.
1659 *********************************************************************/
1660 static int simple_domaincmp(char **pv, char **fv, int len)
1664 for (n = 0; n < len; n++)
1666 if (simplematch(pv[n], fv[n]))
1677 /*********************************************************************
1679 * Function : domaincmp
1681 * Description : Domain-wise Compare fqdn's. Governed by the bimap in
1682 * pattern->unachored, the comparison is un-, left-,
1683 * right-anchored, or both.
1684 * The individual domain names are compared with
1688 * 1 : pattern = a domain that may contain a '*' as a wildcard.
1689 * 2 : fqdn = domain name against which the patterns are compared.
1691 * Returns : 0 => domains are equivalent, else no match.
1693 *********************************************************************/
1694 int domaincmp(struct url_spec *pattern, struct url_spec *fqdn)
1696 char **pv, **fv; /* vectors */
1698 int unanchored = pattern->unanchored & (ANCHOR_RIGHT | ANCHOR_LEFT);
1700 plen = pattern->dcnt;
1705 /* fqdn is too short to match this pattern */
1712 if (unanchored == ANCHOR_LEFT)
1717 * Convert this into a fully anchored pattern with
1718 * the fqdn and pattern the same length
1720 fv += (flen - plen); /* flen - plen >= 0 due to check above */
1721 return simple_domaincmp(pv, fv, plen);
1723 else if (unanchored == 0)
1725 /* Fully anchored, check length */
1730 return simple_domaincmp(pv, fv, plen);
1732 else if (unanchored == ANCHOR_RIGHT)
1734 /* Left anchored, ignore all extra in fqdn */
1735 return simple_domaincmp(pv, fv, plen);
1741 int maxn = flen - plen;
1742 for (n = 0; n <= maxn; n++)
1744 if (!simple_domaincmp(pv, fv, plen))
1749 * Doesn't match from start of fqdn
1750 * Try skipping first part of fqdn