+/*********************************************************************
+ *
+ * Function : create_forged_referrer
+ *
+ * Description : Helper for client_referrer to forge a referer as
+ * 'http://[hostname:port/' to fool stupid
+ * checks for in-site links
+ *
+ * Parameters :
+ * 1 : header = Pointer to header pointer
+ * 2 : hostport = Host and optionally port as string
+ *
+ * Returns : JB_ERR_OK in case of success, or
+ * JB_ERR_MEMORY in case of memory problems.
+ *
+ *********************************************************************/
+static jb_err create_forged_referrer(char **header, const char *hostport)
+{
+ assert(NULL == *header);
+
+ *header = strdup("Referer: http://");
+ string_append(header, hostport);
+ string_append(header, "/");
+
+ if (NULL == *header)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ log_error(LOG_LEVEL_HEADER, "Referer forged to: %s", *header);
+
+ return JB_ERR_OK;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : create_fake_referrer
+ *
+ * Description : Helper for client_referrer to create a fake referrer
+ * based on a string supplied by the user.
+ *
+ * Parameters :
+ * 1 : header = Pointer to header pointer
+ * 2 : hosthost = Referrer to fake
+ *
+ * Returns : JB_ERR_OK in case of success, or
+ * JB_ERR_MEMORY in case of memory problems.
+ *
+ *********************************************************************/
+static jb_err create_fake_referrer(char **header, const char *fake_referrer)
+{
+ assert(NULL == *header);
+
+ if ((0 != strncmpic(fake_referrer, "http://", 7)) && (0 != strncmpic(fake_referrer, "https://", 8)))
+ {
+ log_error(LOG_LEVEL_HEADER,
+ "Parameter: +hide-referrer{%s} is a bad idea, but I don't care.", fake_referrer);
+ }
+ *header = strdup("Referer: ");
+ string_append(header, fake_referrer);
+
+ if (NULL == *header)
+ {
+ return JB_ERR_MEMORY;
+ }
+
+ log_error(LOG_LEVEL_HEADER, "Referer replaced with: %s", *header);
+
+ return JB_ERR_OK;
+
+}
+
+
+/*********************************************************************
+ *
+ * Function : handle_conditional_hide_referrer_parameter
+ *
+ * Description : Helper for client_referrer to crunch or forge
+ * the referrer header if the host has changed.
+ *
+ * Parameters :
+ * 1 : header = Pointer to header pointer
+ * 2 : host = The target host (may include the port)
+ * 3 : parameter_conditional_block = Boolean to signal
+ * if we're in conditional-block mode. If not set,
+ * we're in conditional-forge mode.
+ *
+ * Returns : JB_ERR_OK in case of success, or
+ * JB_ERR_MEMORY in case of memory problems.
+ *
+ *********************************************************************/
+static jb_err handle_conditional_hide_referrer_parameter(char **header,
+ const char *host, const int parameter_conditional_block)
+{
+ char *referer = strdup(*header);
+ const size_t hostlenght = strlen(host);
+
+ if (NULL == referer)
+ {
+ freez(*header);
+ return JB_ERR_MEMORY;
+ }
+
+ /* referer begins with 'Referer: http[s]://' */
+ if (hostlenght < (strlen(referer)-17))
+ {
+ /*
+ * Shorten referer to make sure the referer is blocked
+ * if www.example.org/www.example.com-shall-see-the-referer/
+ * links to www.example.com/
+ */
+ referer[hostlenght+17] = '\0';
+ }
+ if (NULL == strstr(referer, host))
+ {
+ /* Host has changed */
+ if (parameter_conditional_block)
+ {
+ log_error(LOG_LEVEL_HEADER, "New host is: %s. Crunching %s!", host, *header);
+ freez(*header);
+ }
+ else
+ {
+ freez(*header);
+ freez(referer);
+ return create_forged_referrer(header, host);
+ }
+ }
+ freez(referer);
+
+ return JB_ERR_OK;
+
+}
+