+ memset(rhttp, '\0', sizeof(*rhttp));
+
+ /*
+ * Do we trust the request URL itself?
+ */
+ for (b = b->next; b ; b = b->next)
+ {
+ if (url_match(b->url, csp->http))
+ {
+ return b->reject;
+ }
+ }
+
+ if (NULL == (referer = get_header_value(csp->headers, "Referer:")))
+ {
+ /* no referrer was supplied */
+ return 1;
+ }
+
+
+ /*
+ * If not, do we maybe trust its referrer?
+ */
+ err = parse_http_url(referer, rhttp, REQUIRE_PROTOCOL);
+ if (err)
+ {
+ return 1;
+ }
+
+ for (trusted_url = csp->config->trust_list; *trusted_url != NULL; trusted_url++)
+ {
+ if (url_match(*trusted_url, rhttp))
+ {
+ /* if the URL's referrer is from a trusted referrer, then
+ * add the target spec to the trustfile as an unblocked
+ * domain and return 0 (which means it's OK).
+ */
+
+ FILE *fp;
+
+ if (NULL != (fp = fopen(csp->config->trustfile, "a")))
+ {
+ char * path;
+ char * path_end;
+ char * new_entry = strdup("~");
+
+ string_append(&new_entry, csp->http->hostport);
+
+ path = csp->http->path;
+ if ( (path[0] == '/')
+ && (path[1] == '~')
+ && ((path_end = strchr(path + 2, '/')) != NULL))
+ {
+ /* since this path points into a user's home space
+ * be sure to include this spec in the trustfile.
+ */
+ long path_len = path_end - path; /* save offset */
+ path = strdup(path); /* Copy string */
+ if (path != NULL)
+ {
+ path_end = path + path_len; /* regenerate ptr to new buffer */
+ *(path_end + 1) = '\0'; /* Truncate path after '/' */
+ }
+ string_join(&new_entry, path);
+ }
+
+ /*
+ * Give a reason for generating this entry.
+ */
+ string_append(&new_entry, " # Trusted referrer was: ");
+ string_append(&new_entry, referer);
+
+ if (new_entry != NULL)
+ {
+ if (-1 == fprintf(fp, "%s\n", new_entry))
+ {
+ log_error(LOG_LEVEL_ERROR, "Failed to append \'%s\' to trustfile \'%s\': %E",
+ new_entry, csp->config->trustfile);
+ }
+ freez(new_entry);
+ }
+ else
+ {
+ /* FIXME: No way to handle out-of memory, so mostly ignoring it */
+ log_error(LOG_LEVEL_ERROR, "Out of memory adding pattern to trust file");
+ }
+
+ fclose(fp);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_ERROR, "Failed to append new entry for \'%s\' to trustfile \'%s\': %E",
+ csp->http->hostport, csp->config->trustfile);
+ }
+ return 0;
+ }
+ }
+
+ return 1;
+}
+#endif /* def FEATURE_TRUST */
+
+
+/*********************************************************************
+ *
+ * Function : pcrs_filter_response
+ *
+ * Description : Execute all text substitutions from all applying
+ * +filter actions on the text buffer that's been
+ * accumulated in csp->iob->buf.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL if there were no hits or something went wrong
+ *
+ *********************************************************************/
+static char *pcrs_filter_response(struct client_state *csp)
+{
+ int hits = 0;
+ int i;
+ size_t size, prev_size;
+
+ char *old = NULL;
+ char *new = NULL;
+ pcrs_job *job;
+
+ struct file_list *fl;
+ struct re_filterfile_spec *b;
+ struct list_entry *filtername;
+
+ /*
+ * Sanity first
+ */
+ if (csp->iob->cur >= csp->iob->eod)
+ {
+ return(NULL);
+ }
+
+ if (filters_available(csp) == FALSE)
+ {
+ log_error(LOG_LEVEL_ERROR, "Inconsistent configuration: "
+ "content filtering enabled, but no content filters available.");
+ return(NULL);
+ }
+
+ size = (size_t)(csp->iob->eod - csp->iob->cur);
+ old = csp->iob->cur;
+
+ for (i = 0; i < MAX_AF_FILES; i++)
+ {
+ fl = csp->rlist[i];
+ if ((NULL == fl) || (NULL == fl->f))
+ {
+ /*
+ * Either there are no filter files
+ * left, or this filter file just
+ * contains no valid filters.
+ *
+ * Continue to be sure we don't miss
+ * valid filter files that are chained
+ * after empty or invalid ones.
+ */
+ continue;
+ }
+ /*
+ * For all applying +filter actions, look if a filter by that
+ * name exists and if yes, execute it's pcrs_joblist on the
+ * buffer.
+ */
+ for (b = fl->f; b; b = b->next)
+ {
+ if (b->type != FT_CONTENT_FILTER)
+ {
+ /* Skip header filters */
+ continue;
+ }
+
+ for (filtername = csp->action->multi[ACTION_MULTI_FILTER]->first;
+ filtername ; filtername = filtername->next)
+ {
+ if (strcmp(b->name, filtername->str) == 0)
+ {
+ int current_hits = 0; /* Number of hits caused by this filter */
+ int job_number = 0; /* Which job we're currently executing */
+ int job_hits = 0; /* How many hits the current job caused */
+ pcrs_job *joblist = b->joblist;
+
+ if (b->dynamic) joblist = compile_dynamic_pcrs_job_list(csp, b);
+
+ if (NULL == joblist)
+ {
+ log_error(LOG_LEVEL_RE_FILTER, "Filter %s has empty joblist. Nothing to do.", b->name);
+ continue;
+ }
+
+ prev_size = size;
+ /* Apply all jobs from the joblist */
+ for (job = joblist; NULL != job; job = job->next)
+ {
+ job_number++;
+ job_hits = pcrs_execute(job, old, size, &new, &size);
+
+ if (job_hits >= 0)
+ {
+ /*
+ * That went well. Continue filtering
+ * and use the result of this job as
+ * input for the next one.
+ */
+ current_hits += job_hits;
+ if (old != csp->iob->cur)
+ {
+ freez(old);
+ }
+ old = new;
+ }
+ else
+ {
+ /*
+ * This job caused an unexpected error. Inform the user
+ * and skip the rest of the jobs in this filter. We could
+ * continue with the next job, but usually the jobs
+ * depend on each other or are similar enough to
+ * fail for the same reason.
+ *
+ * At the moment our pcrs expects the error codes of pcre 3.4,
+ * but newer pcre versions can return additional error codes.
+ * As a result pcrs_strerror()'s error message might be
+ * "Unknown error ...", therefore we print the numerical value
+ * as well.
+ *
+ * XXX: Is this important enough for LOG_LEVEL_ERROR or
+ * should we use LOG_LEVEL_RE_FILTER instead?
+ */
+ log_error(LOG_LEVEL_ERROR, "Skipped filter \'%s\' after job number %u: %s (%d)",
+ b->name, job_number, pcrs_strerror(job_hits), job_hits);
+ break;
+ }
+ }
+
+ if (b->dynamic) pcrs_free_joblist(joblist);
+
+ log_error(LOG_LEVEL_RE_FILTER,
+ "filtering %s%s (size %d) with \'%s\' produced %d hits (new size %d).",
+ csp->http->hostport, csp->http->path, prev_size, b->name, current_hits, size);
+
+ hits += current_hits;
+ }
+ }
+ }
+ }
+
+ /*
+ * If there were no hits, destroy our copy and let
+ * chat() use the original in csp->iob
+ */
+ if (!hits)
+ {
+ freez(new);
+ return(NULL);
+ }
+
+ csp->flags |= CSP_FLAG_MODIFIED;
+ csp->content_length = size;
+ IOB_RESET(csp);
+
+ return(new);
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : gif_deanimate_response
+ *
+ * Description : Deanimate the GIF image that has been accumulated in
+ * csp->iob->buf, set csp->content_length to the modified
+ * size and raise the CSP_FLAG_MODIFIED flag.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : a pointer to the (newly allocated) modified buffer.
+ * or NULL in case something went wrong.
+ *
+ *********************************************************************/
+static char *gif_deanimate_response(struct client_state *csp)
+{
+ struct binbuffer *in, *out;
+ char *p;
+ size_t size;
+
+ size = (size_t)(csp->iob->eod - csp->iob->cur);
+
+ if ( (NULL == (in = (struct binbuffer *)zalloc(sizeof *in )))
+ || (NULL == (out = (struct binbuffer *)zalloc(sizeof *out))) )
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "failed! (no mem)");
+ return NULL;
+ }
+
+ in->buffer = csp->iob->cur;
+ in->size = size;
+
+ if (gif_deanimate(in, out, strncmp("last", csp->action->string[ACTION_STRING_DEANIMATE], 4)))
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "failed! (gif parsing)");
+ freez(in);
+ buf_free(out);
+ return(NULL);
+ }
+ else
+ {
+ if ((int)size == out->offset)
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "GIF not changed.");
+ }
+ else
+ {
+ log_error(LOG_LEVEL_DEANIMATE, "Success! GIF shrunk from %d bytes to %d.", size, out->offset);
+ }
+ csp->content_length = out->offset;
+ csp->flags |= CSP_FLAG_MODIFIED;
+ p = out->buffer;
+ freez(in);
+ freez(out);
+ return(p);
+ }
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : get_filter_function
+ *
+ * Description : Decides which content filter function has
+ * to be applied (if any).
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : The content filter function to run, or
+ * NULL if no content filter is active
+ *
+ *********************************************************************/
+static filter_function_ptr get_filter_function(const struct client_state *csp)
+{
+ filter_function_ptr filter_function = NULL;
+
+ /*
+ * Choose the applying filter function based on
+ * the content type and action settings.
+ */
+ if ((csp->content_type & CT_TEXT) &&
+ (csp->rlist != NULL) &&
+ (!list_is_empty(csp->action->multi[ACTION_MULTI_FILTER])))
+ {
+ filter_function = pcrs_filter_response;
+ }
+ else if ((csp->content_type & CT_GIF) &&
+ (csp->action->flags & ACTION_DEANIMATE))
+ {
+ filter_function = gif_deanimate_response;
+ }
+
+ return filter_function;
+}
+
+
+/*********************************************************************
+ *
+ * Function : remove_chunked_transfer_coding
+ *
+ * Description : In-situ remove the "chunked" transfer coding as defined
+ * in rfc2616 from a buffer.
+ *
+ * Parameters :
+ * 1 : buffer = Pointer to the text buffer
+ * 2 : size = In: Number of bytes to be processed,
+ * Out: Number of bytes after de-chunking.
+ * (undefined in case of errors)
+ *
+ * Returns : JB_ERR_OK for success,
+ * JB_ERR_PARSE otherwise
+ *
+ *********************************************************************/
+static jb_err remove_chunked_transfer_coding(char *buffer, size_t *size)
+{
+ size_t newsize = 0;
+ unsigned int chunksize = 0;
+ char *from_p, *to_p;
+
+ assert(buffer);
+ from_p = to_p = buffer;
+
+ if (sscanf(buffer, "%x", &chunksize) != 1)
+ {
+ log_error(LOG_LEVEL_ERROR, "Invalid first chunksize while stripping \"chunked\" transfer coding");
+ return JB_ERR_PARSE;
+ }
+
+ while (chunksize > 0U)
+ {
+ if (NULL == (from_p = strstr(from_p, "\r\n")))
+ {
+ log_error(LOG_LEVEL_ERROR, "Parse error while stripping \"chunked\" transfer coding");
+ return JB_ERR_PARSE;
+ }
+
+ if (chunksize >= *size - newsize)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Chunk size %u exceeds buffered data left. "
+ "Already digested %u of %u buffered bytes.",
+ chunksize, (unsigned int)newsize, (unsigned int)*size);
+ return JB_ERR_PARSE;
+ }
+ newsize += chunksize;
+ from_p += 2;
+
+ memmove(to_p, from_p, (size_t) chunksize);
+ to_p = buffer + newsize;
+ from_p += chunksize + 2;
+
+ if (sscanf(from_p, "%x", &chunksize) != 1)
+ {
+ log_error(LOG_LEVEL_INFO, "Invalid \"chunked\" transfer encoding detected and ignored.");
+ break;
+ }
+ }
+
+ /* XXX: Should get its own loglevel. */
+ log_error(LOG_LEVEL_RE_FILTER, "De-chunking successful. Shrunk from %d to %d", *size, newsize);
+
+ *size = newsize;
+
+ return JB_ERR_OK;