Only unlink the pidfile if it's actually used.
[privoxy.git] / jcc.c
diff --git a/jcc.c b/jcc.c
index a977c23..911c505 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.96 2006/08/15 20:12:36 david__schmidt Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.107 2006/11/13 19:05:51 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -33,6 +33,74 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.96 2006/08/15 20:12:36 david__schmidt Exp
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    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.
+ *
+ *    Revision 1.103  2006/09/21 12:54:43  fabiankeil
+ *    Fix +redirect{}. Didn't work with -fast-redirects.
+ *
+ *    Revision 1.102  2006/09/06 13:03:04  fabiankeil
+ *    Respond with 400 and a short text message
+ *    if the client tries to use Privoxy as FTP proxy.
+ *
+ *    Revision 1.101  2006/09/06 09:23:37  fabiankeil
+ *    Make number of retries in case of forwarded-connect problems
+ *    a config file option (forwarded-connect-retries) and use 0 as
+ *    default.
+ *
+ *    Revision 1.100  2006/09/03 19:42:59  fabiankeil
+ *    Set random(3) seed.
+ *
+ *    Revision 1.99  2006/09/02 15:36:42  fabiankeil
+ *    Follow the OpenBSD port's lead and protect the resolve
+ *    functions on OpenBSD as well.
+ *
+ *    Revision 1.98  2006/08/24 11:01:34  fabiankeil
+ *    --user fix. Only use the user as group if no group is specified.
+ *    Solves BR 1492612. Thanks to Spinor S. and David Laight.
+ *
+ *    Revision 1.97  2006/08/18 15:23:17  david__schmidt
+ *    Windows service (re-)integration
+ *
+ *    The new args are:
+ *
+ *    --install[:service_name]
+ *    --uninstall[:service_name]
+ *    --service
+ *
+ *    They work as follows:
+ *    --install will create a service for you and then terminate.
+ *    By default the service name will be "privoxy" (without the quotes).
+ *    However you can run multiple services if you wish, just by adding
+ *    a colon and then a name (no spaces).
+ *
+ *    --uninstall follows the exact same rules a --install.
+ *
+ *    --service is used when the program is executed by the service
+ *    control manager, and in normal circumstances would never be
+ *    used as a command line argument.
+ *
  *    Revision 1.96  2006/08/15 20:12:36  david__schmidt
  *    Windows service integration
  *
@@ -628,10 +696,6 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.96 2006/08/15 20:12:36 david__schmidt Exp
 #include <fcntl.h>
 #include <errno.h>
 
-#ifdef FEATURE_PTHREAD
-#include <pthread.h>
-#endif /* def FEATURE_PTHREAD */
-
 #ifdef _WIN32
 # ifndef FEATURE_PTHREAD
 #  ifndef STRICT
@@ -740,19 +804,25 @@ static int32 server_thread(void *data);
 #define sleep(N)  DosSleep(((N) * 100))
 #endif
 
-#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 /* ndef HAVE_GMTIME_R */
+
+#ifndef HAVE_GETHOSTBYADDR_R
 pthread_mutex_t gethostbyaddr_mutex;
-pthread_mutex_t gethostbyname_mutex;
-#endif /* def OSX_DARWIN */
+#endif /* ndef HAVE_GETHOSTBYADDR_R */
 
-#ifdef FEATURE_PTHREAD
-pthread_mutex_t log_mutex;
-pthread_mutex_t log_init_mutex;
+#ifndef HAVE_GETHOSTBYNAME_R
+pthread_mutex_t gethostbyname_mutex;
+#endif /* ndef HAVE_GETHOSTBYNAME_R */
 #endif /* FEATURE_PTHREAD */
 
 #if defined(unix) || defined(__EMX__)
@@ -797,7 +867,10 @@ static void sig_handler(int the_signal)
       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;
@@ -871,7 +944,8 @@ static void chat(struct client_state *csp)
    int server_body;
    int ms_iis5_hack = 0;
    int byte_count = 0;
-   unsigned int socks_retries = 0;
+   unsigned int forwarded_connect_retries = 0;
+   unsigned int max_forwarded_connect_retries = csp->config->forwarded_connect_retries;
    const struct forward_spec * fwd;
    struct http_request *http;
    int len; /* for buffer sizes */
@@ -954,6 +1028,18 @@ static void chat(struct client_state *csp)
       return;
    }
 
+   if (!strncmpic(http->cmd, "GET ftp://", 10))
+   {
+      strcpy(buf, FTP_RESPONSE);
+      write_socket(csp->cfd, buf, strlen(buf));
+
+      log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as FTP proxy: %s",
+         csp->ip_addr_str, http->cmd);
+
+      free_http_request(http);
+      return;
+   }
+
    /* decide how to route the HTTP request */
 
    if ((fwd = forward_url(http, csp)) == NULL)
@@ -1164,9 +1250,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 (
 
@@ -1178,13 +1261,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
-          || (((csp->action->flags & ACTION_FAST_REDIRECTS) != 0) &&
-                (NULL != (rsp = redirect_url(csp))))
-#endif /* def FEATURE_FAST_REDIRECTS */
+          /* ..or a redirect kicked in */
+          || ( NULL != (rsp = redirect_url(csp)))
           ))
-      )
+
+       /*
+        * .. 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)
@@ -1231,10 +1320,10 @@ static void chat(struct client_state *csp)
    /* here we connect to the server, gateway, or the forwarder */
 
    while ( (csp->sfd = forwarded_connect(fwd, http, csp))
-         && (errno == EINVAL) && (socks_retries++ < 3))
+         && (errno == EINVAL) && (forwarded_connect_retries++ < max_forwarded_connect_retries))
    {
-               log_error(LOG_LEVEL_ERROR, "failed request #%u to connect to %s. Trying again.",
-                socks_retries, http->hostport);
+      log_error(LOG_LEVEL_ERROR, "failed request #%u to connect to %s. Trying again.",
+                forwarded_connect_retries, http->hostport);
    }
 
    if (csp->sfd == JB_INVALID_SOCKET)
@@ -1857,6 +1946,9 @@ 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;
@@ -2001,21 +2093,35 @@ int main(int argc, const char *argv[])
    InitWin32();
 #endif
 
-#ifdef OSX_DARWIN
+#ifdef FEATURE_PTHREAD
    /*
     * Prepare global mutex semaphores
     */
+   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 /* ndef HAVE_GMTIME_R */
+
+#ifndef HAVE_GETHOSTBYADDR_R
    pthread_mutex_init(&gethostbyaddr_mutex,0);
-   pthread_mutex_init(&gethostbyname_mutex,0);
-#endif /* def OSX_DARWIN */
+#endif /* ndef HAVE_GETHOSTBYADDR_R */
 
-#ifdef FEATURE_PTHREAD
-   pthread_mutex_init(&log_mutex,0);
-   pthread_mutex_init(&log_init_mutex,0);
+#ifndef HAVE_GETHOSTBYNAME_R
+   pthread_mutex_init(&gethostbyname_mutex,0);
+#endif /* ndef HAVE_GETHOSTBYNAME_R */
 #endif /* FEATURE_PTHREAD */
 
+#ifdef HAVE_RANDOM
+   random_seed = (unsigned int)time(NULL);
+   srandom(random_seed);
+#endif /* ifdef HAVE_RANDOM */
+
    /*
     * Unix signal handling
     *
@@ -2031,7 +2137,11 @@ int main(int argc, const char *argv[])
 
    for (idx = 0; catched_signals[idx] != 0; idx++)
    {
+#ifdef sun /* FIXME: Is it safe to check for HAVE_SIGSET instead? */ 
+      if (sigset(catched_signals[idx], sig_handler) == SIG_ERR)
+#else
       if (signal(catched_signals[idx], sig_handler) == SIG_ERR)
+#endif /* ifdef sun */
       {
          log_error(LOG_LEVEL_FATAL, "Can't set signal-handler for signal %d: %E", catched_signals[idx]);
       }
@@ -2135,7 +2245,7 @@ int main(int argc, const char *argv[])
    
    if (NULL != pw)
    {
-      if (((NULL != grp) && setgid(grp->gr_gid)) || (setgid(pw->pw_gid)))
+      if (setgid((NULL != grp) ? grp->gr_gid : pw->pw_gid))
       {
          log_error(LOG_LEVEL_FATAL, "Cannot setgid(): Insufficient permissions.");
       }