X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=jcc.c;h=4cbf24baefd1e6ff75f71310f706c18f62486656;hp=adaaa9d9533a8c5a68e917eca137e52bc50f208f;hb=88d71df2d51ef289c934de95813bf229229870fd;hpb=791dc4ba995831c0d1bb2032e1f50e8f4537f186 diff --git a/jcc.c b/jcc.c index adaaa9d9..4cbf24ba 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.104 2006/09/23 13:26:38 roro Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.117 2006/12/31 17:56:37 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,75 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.104 2006/09/23 13:26:38 roro Exp $"; * * Revisions : * $Log: jcc.c,v $ + * Revision 1.117 2006/12/31 17:56:37 fabiankeil + * Added config option accept-intercepted-requests + * and disabled it by default. + * + * Revision 1.116 2006/12/29 19:08:22 fabiankeil + * Reverted parts of my last commit + * to keep error handling working. + * + * Revision 1.115 2006/12/29 17:38:57 fabiankeil + * Fixed gcc43 conversion warnings. + * + * Revision 1.114 2006/12/27 18:52:02 fabiankeil + * Fix -pedantic ISO C warning about converting + * from function pointer to object pointer. + * + * Revision 1.113 2006/12/26 17:38:50 fabiankeil + * Silence compiler warning I introduced with my last commit. + * + * Revision 1.112 2006/12/26 17:31:41 fabiankeil + * Mutex protect rand() if POSIX threading + * is used, warn the user if that's not possible + * and stop using it on _WIN32 where it could + * cause crashes. + * + * Revision 1.111 2006/12/23 16:15:06 fabiankeil + * Don't prevent core dumps by catching SIGABRT. + * It's rude and makes debugging unreasonable painful. + * + * Revision 1.110 2006/12/13 14:52:53 etresoft + * Fix build failure on MacOS X. Global symbols can be either static or extern, but not both. + * + * Revision 1.109 2006/12/06 19:41:40 fabiankeil + * Privoxy is now able to run as intercepting + * proxy in combination with any packet filter + * that does the port redirection. The destination + * is extracted from the "Host:" header which + * should be available for nearly all requests. + * + * Moved HTTP snipplets into jcc.c. + * Added error message for gopher proxy requests. + * + * Revision 1.108 2006/11/28 15:38:51 fabiankeil + * Only unlink the pidfile if it's actually used. + * + * Change order of interception checks to make + * it possible to block or redirect requests for + * the cgi pages. + * + * Revision 1.107 2006/11/13 19:05:51 fabiankeil + * Make pthread mutex locking more generic. Instead of + * checking for OSX and OpenBSD, check for FEATURE_PTHREAD + * and use mutex locking unless there is an _r function + * available. Better safe than sorry. + * + * Fixes "./configure --disable-pthread" and should result + * in less threading-related problems on pthread-using platforms, + * but it still doesn't fix BR#1122404. + * + * Revision 1.106 2006/11/06 19:58:23 fabiankeil + * Move pthread.h inclusion from jcc.c to jcc.h. + * Fixes build on x86-freebsd1 (FreeBSD 5.4-RELEASE). + * + * Revision 1.105 2006/11/06 14:26:02 fabiankeil + * Don't exit after receiving the second SIGHUP on Solaris. + * + * Fixes BR 1052235, but the same problem may exist on other + * systems. Once 3.0.6 is out we should use sigset() + * where available and see if it breaks anything. + * * Revision 1.104 2006/09/23 13:26:38 roro * Replace TABs by spaces in source code. * @@ -84,7 +153,8 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.104 2006/09/23 13:26:38 roro Exp $"; * Windows service integration * * Revision 1.95 2006/08/03 02:46:41 david__schmidt - * Incorporate Fabian Keil's patch work: http://www.fabiankeil.de/sourcecode/privoxy/ + * Incorporate Fabian Keil's patch work: +http://www.fabiankeil.de/sourcecode/privoxy/ * * Revision 1.94 2006/07/18 14:48:46 david__schmidt * Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch) @@ -675,10 +745,6 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.104 2006/09/23 13:26:38 roro Exp $"; #include #include -#ifdef FEATURE_PTHREAD -#include -#endif /* def FEATURE_PTHREAD */ - #ifdef _WIN32 # ifndef FEATURE_PTHREAD # ifndef STRICT @@ -787,24 +853,30 @@ static int32 server_thread(void *data); #define sleep(N) DosSleep(((N) * 100)) #endif -#if defined(OSX_DARWIN) || defined(__OpenBSD__) -#ifdef OSX_DARWIN -/* - * Hit OSX over the head with a hammer. Protect all *_r functions. - */ +#ifdef FEATURE_PTHREAD +pthread_mutex_t log_mutex; +pthread_mutex_t log_init_mutex; + +#ifndef HAVE_GMTIME_R pthread_mutex_t gmtime_mutex; +#endif /* ndef HAVE_GMTIME_R */ + +#ifndef HAVE_LOCALTIME_R pthread_mutex_t localtime_mutex; -#endif /* def OSX_DARWIN */ -/* - * Protect only the resolve functions for OpenBSD. - */ +#endif /* ndef HAVE_GMTIME_R */ + +#ifndef HAVE_GETHOSTBYADDR_R pthread_mutex_t gethostbyaddr_mutex; +#endif /* ndef HAVE_GETHOSTBYADDR_R */ + +#ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_t gethostbyname_mutex; -#endif /* defined(OSX_DARWIN) || defined(__OpenBSD__) */ +#endif /* ndef HAVE_GETHOSTBYNAME_R */ + +#ifndef HAVE_RANDOM +pthread_mutex_t rand_mutex; +#endif /* ndef HAVE_RANDOM */ -#ifdef FEATURE_PTHREAD -pthread_mutex_t log_mutex; -pthread_mutex_t log_init_mutex; #endif /* FEATURE_PTHREAD */ #if defined(unix) || defined(__EMX__) @@ -823,6 +895,36 @@ static const char VANILLA_WAFER[] = "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_" "(copyright_or_otherwise)_applying_to_any_cookie._"; +/* HTTP snipplets. */ +const char CSUCCEED[] = + "HTTP/1.0 200 Connection established\n" + "Proxy-Agent: Privoxy/" VERSION "\r\n\r\n"; + +const char CHEADER[] = + "HTTP/1.0 400 Invalid header received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid header received from browser."; + +const char CFORBIDDEN[] = + "HTTP/1.0 403 Connection not allowable\r\n" + "X-Hint: If you read this message interactively, then you know why this happens ,-)\r\n" + "Connection: close\r\n\r\n"; + +const char FTP_RESPONSE[] = + "HTTP/1.0 400 Invalid request received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid request. Privoxy doesn't support FTP.\r\n"; + +const char GOPHER_RESPONSE[] = + "HTTP/1.0 400 Invalid request received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid request. Privoxy doesn't support gopher.\r\n"; + +const char MISSING_DESTINATION_RESPONSE[] = + "HTTP/1.0 400 Bad request received from browser\r\n" + "Connection: close\r\n\r\n" + "Bad request. Privoxy was unable to extract the destination.\r\n"; + #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA) /********************************************************************* @@ -830,7 +932,7 @@ static const char VANILLA_WAFER[] = * Function : sig_handler * * Description : Signal handler for different signals. - * Exit gracefully on ABRT, TERM and INT + * Exit gracefully on TERM and INT * or set a flag that will cause the errlog * to be reopened by the main thread on HUP. * @@ -844,12 +946,14 @@ static void sig_handler(int the_signal) { switch(the_signal) { - case SIGABRT: case SIGTERM: case SIGINT: log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal); #if defined(unix) - unlink(pidfile); + if(pidfile) + { + unlink(pidfile); + } #endif /* unix */ exit(the_signal); break; @@ -922,12 +1026,12 @@ static void chat(struct client_state *csp) jb_socket maxfd; int server_body; int ms_iis5_hack = 0; - int byte_count = 0; - unsigned int forwarded_connect_retries = 0; - unsigned int max_forwarded_connect_retries = csp->config->forwarded_connect_retries; + size_t byte_count = 0; + int forwarded_connect_retries = 0; + int max_forwarded_connect_retries = csp->config->forwarded_connect_retries; const struct forward_spec * fwd; struct http_request *http; - int len; /* for buffer sizes */ + int len; /* for buffer sizes (and negative error codes) */ #ifdef FEATURE_KILL_POPUPS int block_popups; /* bool, 1==will block popups */ int block_popups_now = 0; /* bool, 1==currently blocking popups */ @@ -943,6 +1047,10 @@ static void chat(struct client_state *csp) /* Skeleton for HTTP response, if we should intercept the request */ struct http_response *rsp; + /* Temporary copy of the client's headers before they get enlisted in csp->headers */ + struct list header_list; + struct list *headers = &header_list; + http = csp->http; /* @@ -977,6 +1085,38 @@ static void chat(struct client_state *csp) continue; /* more to come! */ } + /* + * If it's a FTP or gopher request, we don't support it. + * + * These checks are better than nothing, but they might + * not work in all configurations and some clients might + * have problems digesting the answer. + * + * They should, however, never cause more problems than + * Privoxy's old behaviour (returning the misleading HTML error message: + * "Could not resolve http://(ftp|gopher)://example.org"). + */ + if (!strncmpic(req, "GET ftp://", 10) || !strncmpic(req, "GET gopher://", 13)) + { + if (!strncmpic(req, "GET ftp://", 10)) + { + strcpy(buf, FTP_RESPONSE); + log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as FTP proxy: %s", + csp->ip_addr_str, req); + } + else + { + strcpy(buf, GOPHER_RESPONSE); + log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as gopher proxy: %s", + csp->ip_addr_str, req); + } + log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, req); + freez(req); + write_socket(csp->cfd, buf, strlen(buf)); + free_http_request(http); + return; + } + #ifdef FEATURE_FORCE_LOAD /* If this request contains the FORCE_PREFIX, * better get rid of it now and set the force flag --oes @@ -991,7 +1131,16 @@ static void chat(struct client_state *csp) #endif /* def FEATURE_FORCE_LOAD */ - parse_http_request(req, http, csp); + switch( parse_http_request(req, http, csp) ) + { + case JB_ERR_MEMORY: + log_error(LOG_LEVEL_ERROR, "Out of memory while parsing request."); + break; + case JB_ERR_PARSE: + log_error(LOG_LEVEL_ERROR, "Couldn't parse request: %s.", req); + break; + } + freez(req); break; } @@ -1007,16 +1156,85 @@ static void chat(struct client_state *csp) return; } - if (!strncmpic(http->cmd, "GET ftp://", 10)) + /* grab the rest of the client's headers */ + init_list(headers); + for (;;) { - strcpy(buf, FTP_RESPONSE); - write_socket(csp->cfd, buf, strlen(buf)); + if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) ) + { + len = read_socket(csp->cfd, buf, sizeof(buf)); + if (len <= 0) + { + log_error(LOG_LEVEL_ERROR, "read from client failed: %E"); + return; + } + + /* + * If there is no memory left for buffering the + * request, there is nothing we can do but hang up + */ + if (add_to_iob(csp, buf, len)) + { + return; + } + continue; + } - log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as FTP proxy: %s", - csp->ip_addr_str, http->cmd); + if (p == NULL) break; + + enlist(headers, p); + freez(p); - free_http_request(http); - return; + } + + if (http->host == NULL) + { + /* + * Intercepted or invalid request without domain + * inside the request line. Try to get it another way, + * unless accept-intercepted-requests is disabled. + */ + if (!(csp->config->feature_flags & RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS)) + { + log_error(LOG_LEVEL_ERROR, "%s's request: \'%s\' is invalid." + " Privoxy isn't configured to accept intercepted requests.", + csp->ip_addr_str, http->cmd); + log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, http->cmd); + + strcpy(buf, CHEADER); + write_socket(csp->cfd, buf, strlen(buf)); + free_http_request(http); + destroy_list(headers); + return; + } + else if (JB_ERR_OK == get_destination_from_headers(headers, http)) + { + /* Split the domain we just got for pattern matching */ + init_domain_components(http); + } + else + { + /* We can't work without destination. Go spread the news.*/ + + req = list_to_text(headers); + chomp(req); + log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, http->cmd); + log_error(LOG_LEVEL_ERROR, + "Privoxy was unable to get the destination for %s's request:\n%s\n%s", + csp->ip_addr_str, http->cmd, req); + freez(req); + + strcpy(buf, MISSING_DESTINATION_RESPONSE); + write_socket(csp->cfd, buf, strlen(buf)); + free_http_request(http); + destroy_list(headers); + return; + } + /* + * TODO: If available, use PF's ioctl DIOCNATLOOK as last resort + * to get the destination IP address, use it as host directly + * or do a reverse DNS lookup first. + */ } /* decide how to route the HTTP request */ @@ -1167,6 +1385,9 @@ static void chat(struct client_state *csp) } enlist(csp->headers, http->cmd); + /* Append the previously read headers */ + list_append_list_unique(csp->headers, headers); + destroy_list(headers); /* * If the user has not supplied any wafers, and the user has not @@ -1190,36 +1411,6 @@ static void chat(struct client_state *csp) jpeg_inspect = ((csp->action->flags & ACTION_JPEG_INSPECT) != 0); - /* grab the rest of the client's headers */ - - for (;;) - { - if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) ) - { - len = read_socket(csp->cfd, buf, sizeof(buf)); - if (len <= 0) - { - log_error(LOG_LEVEL_ERROR, "read from client failed: %E"); - return; - } - - /* - * If there is no memory left for buffering the - * request, there is nothing we can do but hang up - */ - if (add_to_iob(csp, buf, len)) - { - return; - } - continue; - } - - if (p == NULL) break; - - enlist(csp->headers, p); - freez(p); - } - /* * We have a request. Now, check to see if we need to * intercept it, i.e. If .. @@ -1229,9 +1420,6 @@ static void chat(struct client_state *csp) /* We may not forward the request by rfc2616 sect 14.31 */ (NULL != (rsp = direct_response(csp))) - /* or a CGI call was detected and answered */ - || (NULL != (rsp = dispatch_cgi(csp))) - /* or we are enabled and... */ || (IS_ENABLED_AND ( @@ -1243,12 +1431,19 @@ static void chat(struct client_state *csp) || ( NULL != (rsp = trust_url(csp))) #endif /* def FEATURE_TRUST */ - /* ..or a fast redirect kicked in */ -#ifdef FEATURE_FAST_REDIRECTS + /* ..or a redirect kicked in */ || ( NULL != (rsp = redirect_url(csp))) -#endif /* def FEATURE_FAST_REDIRECTS */ )) - ) + + /* + * .. or a CGI call was detected and answered. + * + * This check comes last to give the user the power + * to deny acces to some (or all) of the cgi pages. + */ + || (NULL != (rsp = dispatch_cgi(csp))) + + ) { /* Write the answer to the client */ if (write_socket(csp->cfd, rsp->head, rsp->head_length) @@ -1526,7 +1721,7 @@ static void chat(struct client_state *csp) */ if (NULL == (p = (*content_filter)(csp))) { - csp->content_length = csp->iob->eod - csp->iob->cur; + csp->content_length = (size_t)(csp->iob->eod - csp->iob->cur); } hdr = sed(server_patterns_light, NULL, csp); @@ -1557,7 +1752,8 @@ static void chat(struct client_state *csp) * This is NOT the body, so * Let's pretend the server just sent us a blank line. */ - len = sprintf(buf, "\r\n"); + snprintf(buf, sizeof(buf), "\r\n"); + len = (int)strlen(buf); /* * Now, let the normal header parsing algorithm below do its @@ -1611,7 +1807,7 @@ static void chat(struct client_state *csp) if (write_socket(csp->cfd, hdr, hdrlen) || ((flushed = flush_socket(csp->cfd, csp)) < 0) - || (write_socket(csp->cfd, buf, (size_t) len))) + || (write_socket(csp->cfd, buf, (size_t)len))) { log_error(LOG_LEVEL_CONNECT, "Flush header and buffers to client failed: %E"); @@ -1619,7 +1815,7 @@ static void chat(struct client_state *csp) return; } - byte_count += hdrlen + flushed + len; + byte_count += hdrlen + (size_t)(flushed + len); freez(hdr); content_filter = NULL; server_body = 1; @@ -1634,7 +1830,7 @@ static void chat(struct client_state *csp) return; } } - byte_count += len; + byte_count += (size_t)len; continue; } else @@ -1782,7 +1978,7 @@ static void chat(struct client_state *csp) return; } - byte_count += len; + byte_count += (size_t)len; } /* we're finished with the server's header */ @@ -1921,9 +2117,7 @@ int main(int argc, const char *argv[]) #endif { int argc_pos = 0; -#ifdef HAVE_RANDOM unsigned int random_seed; -#endif /* ifdef HAVE_RANDOM */ #ifdef unix struct passwd *pw = NULL; struct group *grp = NULL; @@ -1988,7 +2182,7 @@ int main(int argc, const char *argv[]) #if defined(unix) - else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0) + else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0) { no_daemon = 1; } @@ -2068,26 +2262,40 @@ int main(int argc, const char *argv[]) InitWin32(); #endif -#if defined(OSX_DARWIN) || defined(__OpenBSD__) +#ifdef FEATURE_PTHREAD /* * Prepare global mutex semaphores */ -#ifdef OSX_DARWIN + pthread_mutex_init(&log_mutex,0); + pthread_mutex_init(&log_init_mutex,0); + +#ifndef HAVE_GMTIME_R pthread_mutex_init(&gmtime_mutex,0); +#endif /* ndef HAVE_GMTIME_R */ + +#ifndef HAVE_LOCALTIME_R pthread_mutex_init(&localtime_mutex,0); -#endif /* def OSX_DARWIN */ +#endif /* ndef HAVE_GMTIME_R */ + +#ifndef HAVE_GETHOSTBYADDR_R pthread_mutex_init(&gethostbyaddr_mutex,0); +#endif /* ndef HAVE_GETHOSTBYADDR_R */ + +#ifndef HAVE_GETHOSTBYNAME_R pthread_mutex_init(&gethostbyname_mutex,0); -#endif /* defined(OSX_DARWIN) || defined(__OpenBSD__) */ +#endif /* ndef HAVE_GETHOSTBYNAME_R */ + +#ifndef HAVE_RANDOM + pthread_mutex_init(&rand_mutex,0); +#endif /* ndef HAVE_RANDOM */ -#ifdef FEATURE_PTHREAD - pthread_mutex_init(&log_mutex,0); - pthread_mutex_init(&log_init_mutex,0); #endif /* FEATURE_PTHREAD */ -#ifdef HAVE_RANDOM random_seed = (unsigned int)time(NULL); +#ifdef HAVE_RANDOM srandom(random_seed); +#else + srand(random_seed); #endif /* ifdef HAVE_RANDOM */ /* @@ -2100,7 +2308,7 @@ int main(int argc, const char *argv[]) #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA) { int idx; - const int catched_signals[] = { SIGABRT, SIGTERM, SIGINT, SIGHUP, 0 }; + const int catched_signals[] = { SIGTERM, SIGINT, SIGHUP, 0 }; const int ignored_signals[] = { SIGPIPE, 0 }; for (idx = 0; catched_signals[idx] != 0; idx++) @@ -2130,7 +2338,7 @@ int main(int argc, const char *argv[]) * We *are* in a windows console app. * Print a verbose messages about FAQ's and such */ - printf(win32_blurb); + printf("%s", win32_blurb); # endif /* def _WIN_CONSOLE */ #endif /* def _WIN32 */ @@ -2210,7 +2418,7 @@ int main(int argc, const char *argv[]) * to the user and group ID indicated by the --user option */ write_pid_file(); - + if (NULL != pw) { if (setgid((NULL != grp) ? grp->gr_gid : pw->pw_gid)) @@ -2544,7 +2752,7 @@ static void listen_loop(void) pthread_attr_init(&attrs); pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); errno = pthread_create(&the_thread, &attrs, - (void*)serve, csp); + (void * (*)(void *))serve, csp); child_id = errno ? -1 : 0; pthread_attr_destroy(&attrs); } @@ -2588,13 +2796,22 @@ static void listen_loop(void) #define SELECTED_ONE_OPTION csp->cfd = ReleaseSocket(csp->cfd, -1); - if((child_id = (int)CreateNewProcTags( - NP_Entry, (ULONG)server_thread, - NP_Output, Output(), - NP_CloseOutput, FALSE, - NP_Name, (ULONG)"privoxy child", - NP_StackSize, 200*1024, - TAG_DONE))) +#ifdef __amigaos4__ + child_id = (int)CreateNewProcTags(NP_Entry, (ULONG)server_thread, + NP_Output, Output(), + NP_CloseOutput, FALSE, + NP_Name, (ULONG)"privoxy child", + NP_Child, TRUE, + TAG_DONE); +#else + child_id = (int)CreateNewProcTags(NP_Entry, (ULONG)server_thread, + NP_Output, Output(), + NP_CloseOutput, FALSE, + NP_Name, (ULONG)"privoxy child", + NP_StackSize, 200*1024, + TAG_DONE); +#endif + if(0 != child_id) { childs++; ((struct Task *)child_id)->tc_UserData = csp; @@ -2686,7 +2903,7 @@ static void listen_loop(void) log_error(LOG_LEVEL_ERROR, "can't fork: %E"); - sprintf(buf , "Privoxy: can't fork: errno = %d", errno); + snprintf(buf , sizeof(buf), "Privoxy: can't fork: errno = %d", errno); write_socket(csp->cfd, buf, strlen(buf)); close_socket(csp->cfd);