+ csp->headers->first->str[30] = '\0';
+ }
+ log_error(LOG_LEVEL_ERROR,
+ "Invalid server or forwarder response. Starts with: %s",
+ csp->headers->first->str);
+ log_error(LOG_LEVEL_CLF,
+ "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Sending data with standard or secured connection (HTTP/HTTPS)
+ */
+ if (client_use_ssl(csp))
+ {
+ ssl_send_data(&(csp->mbedtls_client_attr.ssl),
+ (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE));
+ }
+ else
+#endif /* def FEATURE_HTTPS_FILTERING */
+ {
+ write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
+ }
+ free_http_request(http);
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+
+ /*
+ * We have now received the entire server header,
+ * filter it and send the result to the client
+ */
+ if (JB_ERR_OK != sed(csp, FILTER_SERVER_HEADERS))
+ {
+ log_error(LOG_LEVEL_CLF,
+ "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Sending data with standard or secured connection (HTTP/HTTPS)
+ */
+ if (client_use_ssl(csp))
+ {
+ ssl_send_data(&(csp->mbedtls_client_attr.ssl),
+ (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE));
+ }
+ else
+#endif
+ {
+ write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
+ }
+ free_http_request(http);
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+ hdr = list_to_text(csp->headers);
+ if (hdr == NULL)
+ {
+ /* FIXME Should handle error properly */
+ log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
+ }
+
+ if ((csp->flags & CSP_FLAG_CHUNKED)
+ && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
+ && ((csp->iob->eod - csp->iob->cur) >= 5)
+ && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Looks like we got the last chunk together with "
+ "the server headers. We better stop reading.");
+ byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
+ csp->expected_content_length = byte_count;
+ csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
+ }
+
+ csp->server_connection.response_received = time(NULL);
+
+ if (crunch_response_triggered(csp, crunchers_light))
+ {
+ /*
+ * One of the tags created by a server-header
+ * tagger triggered a crunch. We already
+ * delivered the crunch response to the client
+ * and are done here after cleaning up.
+ */
+ freez(hdr);
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+
+ /* Buffer and pcrs filter this if appropriate. */
+ buffer_and_filter_content = content_requires_filtering(csp);
+
+ if (!buffer_and_filter_content)
+ {
+ /*
+ * Write the server's (modified) header to
+ * the client (along with anything else that
+ * may be in the buffer). Use standard or secured
+ * connection.
+ */
+#ifdef FEATURE_HTTPS_FILTERING
+ if (client_use_ssl(csp))
+ {
+ if ((ssl_send_data(&(csp->mbedtls_client_attr.ssl),
+ (const unsigned char *)hdr, strlen(hdr)) < 0)
+ || (len = ssl_flush_socket(&(csp->mbedtls_client_attr.ssl),
+ csp->iob) < 0))
+ {
+ log_error(LOG_LEVEL_CONNECT, "Write header to client failed");
+
+ /*
+ * The write failed, so don't bother mentioning it
+ * to the client... it probably can't hear us anyway.
+ */
+ freez(hdr);
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+ }
+ else
+#endif /* def FEATURE_HTTPS_FILTERING */
+ {
+ if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
+ || ((len = flush_iob(csp->cfd, csp->iob, write_delay)) < 0))
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "write header to client failed");
+ /*
+ * The write failed, so don't bother mentioning it
+ * to the client... it probably can't hear us anyway.
+ */
+ freez(hdr);
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+ }
+ }
+
+ /* we're finished with the server's header */
+
+ freez(hdr);
+ server_body = 1;
+
+ /*
+ * If this was a MS IIS/5 hack then it means the server
+ * has already closed the connection. Nothing more to read.
+ * Time to bail.
+ */
+ if (ms_iis5_hack)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Closed server connection detected. "
+ "Applying the MS IIS5 hack didn't help.");
+ log_error(LOG_LEVEL_CLF,
+ "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Sending data with standard or secured connection (HTTP/HTTPS)
+ */
+ if (client_use_ssl(csp))
+ {
+ ssl_send_data(&(csp->mbedtls_client_attr.ssl),
+ (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE));
+ }
+ else
+#endif /* def FEATURE_HTTPS_FILTERING */
+ {
+ write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
+ strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
+ }
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return;
+ }
+ }
+ continue;
+ }
+ mark_server_socket_tainted(csp);
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ return; /* huh? we should never get here */
+ }
+#ifdef FEATURE_HTTPS_FILTERING
+ close_client_and_server_ssl_connections(csp);
+#endif
+ if (csp->content_length == 0)
+ {
+ /*
+ * If Privoxy didn't recalculate the Content-Length,
+ * byte_count is still correct.
+ */
+ csp->content_length = byte_count;
+ }
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+ if ((csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
+ && (csp->expected_content_length != byte_count))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Received %llu bytes while expecting %llu.",
+ byte_count, csp->expected_content_length);
+ mark_server_socket_tainted(csp);
+ }
+#endif
+
+ log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %llu",
+ csp->ip_addr_str, http->ocmd, csp->content_length);
+
+ csp->server_connection.timestamp = time(NULL);
+}
+
+
+/*********************************************************************
+ *
+ * Function : chat
+ *
+ * Description : Once a connection from the client has been accepted,
+ * this function is called (via serve()) to handle the
+ * main business of the communication. This function
+ * returns after dealing with a single request. It can
+ * be called multiple times with the same client socket
+ * if the client is keeping the connection alive.
+ *
+ * The decision whether or not a client connection will
+ * be kept alive is up to the caller which also must
+ * close the client socket when done.
+ *
+ * FIXME: chat is nearly thousand lines long.
+ * Ridiculous.
+ *
+ * Parameters :
+ * 1 : csp = Current client state (buffers, headers, etc...)
+ *
+ * Returns : Nothing.
+ *
+ *********************************************************************/
+static void chat(struct client_state *csp)
+{
+ const struct forward_spec *fwd;
+ struct http_request *http;
+ /* Skeleton for HTTP response, if we should intercept the request */
+ struct http_response *rsp;
+#ifdef FEATURE_HTTPS_FILTERING
+ int use_ssl_tunnel = 0;
+#endif
+
+ http = csp->http;
+
+ if (receive_client_request(csp) != JB_ERR_OK)
+ {
+ return;
+ }
+ if (parse_client_request(csp) != JB_ERR_OK)
+ {
+ return;
+ }
+
+ /* decide how to route the HTTP request */
+ fwd = forward_url(csp, http);
+ if (NULL == fwd)
+ {
+ log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
+ /* Never get here - LOG_LEVEL_FATAL causes program exit */
+ return;
+ }
+
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Setting flags to use old solution with SSL tunnel and to disable
+ * certificates verification.
+ */
+ if (csp->http->ssl && !(csp->action->flags & ACTION_ENABLE_HTTPS_FILTER))
+ {
+ use_ssl_tunnel = 1;
+ }
+
+ if (http->ssl && csp->action->flags & ACTION_IGNORE_CERTIFICATE_ERRORS)
+ {
+ csp->dont_verify_certificate = 1;
+ }
+#endif
+
+ /*
+ * build the http request to send to the server
+ * we have to do one of the following:
+ *
+ * create = use the original HTTP request to create a new
+ * HTTP request that has either the path component
+ * without the http://domainspec (w/path) or the
+ * full orininal URL (w/url)
+ * Note that the path and/or the HTTP version may
+ * have been altered by now.
+ *
+ * SSL proxy = Open a socket to the host:port of the server
+ * and create TLS/SSL connection with server and
+ * with client. Then behave like mediator between
+ * client and server over TLS/SSL.
+ *
+ * SSL proxy = Pass the request unchanged if forwarding a CONNECT
+ * with request to a parent proxy. Note that we'll be sending
+ * forwarding the CFAIL message ourselves if connecting to the parent
+ * fails, but we won't send a CSUCCEED message if it works,
+ * since that would result in a double message (ours and the
+ * parent's). After sending the request to the parent, we
+ * must parse answer and send it to client. If connection
+ * with server is established, we do TLS/SSL proxy. Otherwise
+ * we send parent response to client and close connections.
+ *
+ * here's the matrix:
+ * SSL
+ * 0 1
+ * +--------+--------+
+ * | | |
+ * 0 | create | SSL |
+ * | w/path | proxy |
+ * Forwarding +--------+--------+
+ * | | SSL |
+ * 1 | create | proxy |
+ * | w/url |+forward|
+ * +--------+--------+
+ *
+ */
+
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Presetting SSL client and server flags
+ */
+ if (http->ssl && !use_ssl_tunnel)
+ {
+ http->client_ssl = 1;
+ http->server_ssl = 1;
+ }
+ else
+ {
+ http->client_ssl = 0;
+ http->server_ssl = 0;
+ }
+#endif
+
+ if (http->ssl && connect_port_is_forbidden(csp))
+ {
+ const char *acceptable_connect_ports =
+ csp->action->string[ACTION_STRING_LIMIT_CONNECT];
+ assert(NULL != acceptable_connect_ports);
+ log_error(LOG_LEVEL_INFO, "Request from %s marked for blocking. "
+ "limit-connect{%s} doesn't allow CONNECT requests to %s",
+ csp->ip_addr_str, acceptable_connect_ports, csp->http->hostport);
+ csp->action->flags |= ACTION_BLOCK;
+ http->ssl = 0;
+#ifdef FEATURE_HTTPS_FILTERING
+ http->client_ssl = 0;
+ http->server_ssl = 0;
+#endif
+ }
+
+
+ freez(csp->headers->first->str);
+ build_request_line(csp, fwd, &csp->headers->first->str);
+
+ /*
+ * We have a request. Check if one of the crunchers wants it
+ * unless the client wants to use TLS/SSL in which case we
+ * haven't setup the TLS context yet and will send the crunch
+ * response later.
+ */
+ if (
+#ifdef FEATURE_HTTPS_FILTERING
+ !client_use_ssl(csp) &&
+#endif
+ crunch_response_triggered(csp, crunchers_all))
+ {
+ /*
+ * Yes. The client got the crunch response and we're done here.
+ */
+ return;
+ }
+
+ log_applied_actions(csp->action);
+ log_error(LOG_LEVEL_GPC, "%s%s", http->hostport, http->path);
+
+ if (fwd->forward_host)
+ {
+ log_error(LOG_LEVEL_CONNECT, "via [%s]:%d to: %s",
+ fwd->forward_host, fwd->forward_port, http->hostport);
+ }
+ else
+ {
+ log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
+ }
+
+ /* here we connect to the server, gateway, or the forwarder */
+
+#ifdef FEATURE_CONNECTION_KEEP_ALIVE
+ if ((csp->server_connection.sfd != JB_INVALID_SOCKET)
+ && socket_is_still_alive(csp->server_connection.sfd)
+ && connection_destination_matches(&csp->server_connection, http, fwd))
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Reusing server socket %d connected to %s. Total requests: %u.",
+ csp->server_connection.sfd, csp->server_connection.host,
+ csp->server_connection.requests_sent_total);
+ }
+ else
+ {
+ if (csp->server_connection.sfd != JB_INVALID_SOCKET)
+ {
+#ifdef FEATURE_CONNECTION_SHARING
+ if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
+ {
+ remember_connection(&csp->server_connection);
+ }
+ else
+#endif /* def FEATURE_CONNECTION_SHARING */
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Closing server socket %d connected to %s. Total requests: %u.",
+ csp->server_connection.sfd, csp->server_connection.host,
+ csp->server_connection.requests_sent_total);
+ close_socket(csp->server_connection.sfd);
+ }
+ mark_connection_closed(&csp->server_connection);
+ }
+#endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
+#ifdef FEATURE_HTTPS_FILTERING
+ if (http->ssl && !use_ssl_tunnel)
+ {
+ int ret;
+ /*
+ * Creating an SSL proxy. If forwarding is disabled, we must send
+ * CSUCCEED mesage to client. Then TLS/SSL connection with client
+ * is created.
+ */
+
+ if (fwd->forward_host == NULL)
+ {
+ /*
+ * We're lying to the client as the connection hasn't actually
+ * been established yet. We don't establish the connection until
+ * we have seen and parsed the encrypted client headers.
+ */
+ if (write_socket_delayed(csp->cfd, CSUCCEED,
+ strlen(CSUCCEED), get_write_delay(csp)) != 0)
+ {
+ log_error(LOG_LEVEL_ERROR, "Sending SUCCEED to client failed");
+ return;
+ }
+ }
+
+ ret = create_client_ssl_connection(csp);
+ if (ret != 0)
+ {
+ log_error(LOG_LEVEL_ERROR,
+ "Can't open secure connection with client");
+ close_client_ssl_connection(csp); /* XXX: Is this needed? */
+ return;
+ }
+ if (JB_ERR_OK != process_encrypted_request(csp))
+ {
+ log_error(LOG_LEVEL_ERROR, "Failed to parse encrypted request.");
+ close_client_ssl_connection(csp);
+ return;
+ }
+ /*
+ * We have an encrypted request. Check if one of the crunchers now
+ * wants it (for example because the previously invisible path was
+ * required to match).
+ */
+ if (crunch_response_triggered(csp, crunchers_all))
+ {
+ /*
+ * Yes. The client got the crunch response and we're done here.
+ */
+ close_client_ssl_connection(csp);
+ return;
+ }
+ }
+#endif
+ /*
+ * Connecting to destination server
+ */
+ csp->server_connection.sfd = forwarded_connect(fwd, http, csp);
+
+ if (csp->server_connection.sfd == JB_INVALID_SOCKET)
+ {
+ if (fwd->type != SOCKS_NONE)
+ {
+ /* Socks error. */
+ rsp = error_response(csp, "forwarding-failed");
+ }
+ else if (errno == EINVAL)
+ {
+ rsp = error_response(csp, "no-such-domain");
+ }
+ else
+ {
+ rsp = error_response(csp, "connect-failed");
+ }
+
+ /* Write the answer to the client */
+ if (rsp != NULL)
+ {
+ send_crunch_response(csp, rsp);
+ }
+
+ /*
+ * Temporary workaround to prevent already-read client
+ * bodies from being parsed as new requests. For now we
+ * err on the safe side and throw all the following
+ * requests under the bus, even if no client body has been
+ * buffered. A compliant client will repeat the dropped
+ * requests on an untainted connection.
+ *
+ * The proper fix is to discard the no longer needed
+ * client body in the buffer (if there is one) and to
+ * continue parsing the bytes that follow.
+ */
+ drain_and_close_socket(csp->cfd);
+ csp->cfd = JB_INVALID_SOCKET;
+
+ return;
+ }
+
+#ifdef FEATURE_HTTPS_FILTERING
+ /*
+ * Creating TLS/SSL connections with destination server or parent
+ * proxy. If forwarding is enabled, we must send client request to
+ * parent proxy and receive, parse and resend parent proxy answer.
+ */
+ if (http->ssl && !use_ssl_tunnel)
+ {
+ if (fwd->forward_host != NULL)
+ {
+ char server_response[BUFFER_SIZE];
+ int ret = 0;
+ int len = 0;
+ char *hdr = list_to_text(csp->headers);
+ memset(server_response, 0, sizeof(server_response));
+
+ if (hdr == NULL)
+ {
+ log_error(LOG_LEVEL_FATAL,
+ "Out of memory parsing client header");
+ }
+ list_remove_all(csp->headers);
+
+ /*
+ * Sending client's CONNECT request to the parent proxy
+ */
+ ret = write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
+
+ freez(hdr);
+
+ if (ret != 0)
+ {
+ log_error(LOG_LEVEL_CONNECT,
+ "Sending request headers to: %s failed", http->hostport);
+ mark_server_socket_tainted(csp);
+ close_client_ssl_connection(csp);
+ return;
+ }
+
+ /* Waiting for parent proxy server response */
+ len = read_socket(csp->server_connection.sfd, server_response,
+ sizeof(server_response)-1);
+
+ if (len <= 0)
+ {
+ log_error(LOG_LEVEL_ERROR, "No response from parent proxy "
+ "server on socket %d.", csp->server_connection.sfd);
+
+ rsp = error_response(csp, "no-server-data");
+ if (rsp)
+ {
+ send_crunch_response(csp, rsp);