Catching up: chroot
[privoxy.git] / jcc.c
diff --git a/jcc.c b/jcc.c
index 797405f..49bb5e8 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.92.2.4 2003/03/07 03:41:04 david__schmidt Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.92.2.6 2003/03/11 11:55:00 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/Attic/jcc.c,v $
@@ -33,6 +33,18 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.92.2.4 2003/03/07 03:41:04 david__schmidt
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    Revision 1.92.2.6  2003/03/11 11:55:00  oes
+ *    Clean-up and extension of improvements for forked mode:
+ *     - Child's return code now consists of flags RC_FLAG_*
+ *     - Reporting toggle to parent now properly #ifdef'ed
+ *     - Children now report blocking to parent. This enables
+ *       statistics in forked mode
+ *
+ *    Revision 1.92.2.5  2003/03/10 23:45:32  oes
+ *    Fixed bug #700381: Non-Threaded version now capable of being toggled.
+ *    Children now report having been toggled through _exit(17), parents
+ *    watch for that code and toggle themselves if found.
+ *
  *    Revision 1.92.2.4  2003/03/07 03:41:04  david__schmidt
  *    Wrapping all *_r functions (the non-_r versions of them) with mutex semaphores for OSX.  Hopefully this will take care of all of those pesky crash reports.
  *
@@ -1757,6 +1769,7 @@ int main(int argc, const char *argv[])
    struct passwd *pw = NULL;
    struct group *grp = NULL;
    char *p;
+   int do_chroot = 0;
 #endif
 
    Argc = argc;
@@ -1819,6 +1832,11 @@ int main(int argc, const char *argv[])
 
          if (p != NULL) *--p = '\0';
       }
+
+      else if (strcmp(argv[argc_pos], "--chroot" ) == 0)
+      {
+         do_chroot = 1;
+      }
 #endif /* defined(unix) */
       else
 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
@@ -1996,10 +2014,40 @@ int main(int argc, const char *argv[])
       {
          log_error(LOG_LEVEL_FATAL, "Cannot setgid(): Insufficient permissions.");
       }
+      if (do_chroot)
+      {
+         if (!pw->pw_dir)
+         {
+            log_error(LOG_LEVEL_FATAL, "Home directory for %s undefined", pw->pw_name);
+         }
+         if (chroot(pw->pw_dir) < 0)
+         {
+            log_error(LOG_LEVEL_FATAL, "Cannot chroot to %s", pw->pw_dir);
+         }
+         if (chdir ("/"))
+         {
+            log_error(LOG_LEVEL_FATAL, "Cannot chdir /");
+         }
+      }
       if (setuid(pw->pw_uid))
       {
          log_error(LOG_LEVEL_FATAL, "Cannot setuid(): Insufficient permissions.");
       }
+      if (do_chroot)
+      {
+         if (setenv ("HOME", "/", 1) < 0)
+         {
+            log_error(LOG_LEVEL_FATAL, "Cannot setenv(): HOME");
+         }
+         if (setenv ("USER", pw->pw_name, 1) < 0)
+         {
+            log_error(LOG_LEVEL_FATAL, "Cannot setenv(): USER");
+         }
+      }
+   }
+   else if (do_chroot)
+   {
+      log_error(LOG_LEVEL_FATAL, "Cannot chroot without --user argument.");
    }
 }
 #endif /* defined unix */
@@ -2203,7 +2251,7 @@ static void listen_loop(void)
       }
 
 #ifdef FEATURE_TOGGLE
-      if (g_bToggleIJB)
+      if (global_toggle_state)
       {
          csp->flags |= CSP_FLAG_TOGGLED_ON;
       }
@@ -2288,6 +2336,7 @@ static void listen_loop(void)
 #if defined(AMIGA) && !defined(SELECTED_ONE_OPTION)
 #define SELECTED_ONE_OPTION
          csp->cfd = ReleaseSocket(csp->cfd, -1);
+         
          if((child_id = (int)CreateNewProcTags(
             NP_Entry, (ULONG)server_thread,
             NP_Output, Output(),
@@ -2312,20 +2361,32 @@ static void listen_loop(void)
           */
          if (child_id == 0)   /* child */
          {
-            int inherited_toggle_state = g_bToggleIJB;
+            int rc = 0;
+#ifdef FEATURE_TOGGLE
+            int inherited_toggle_state = global_toggle_state;
+#endif /* def FEATURE_TOGGLE */
 
             serve(csp);
+
             /* 
-             * If we've been toggled, tell Mom
+             * If we've been toggled or we'be blocked the request, tell Mom
              */
-            if (inherited_toggle_state != g_bToggleIJB)
+
+#ifdef FEATURE_TOGGLE
+            if (inherited_toggle_state != global_toggle_state)
             {
-               _exit(17);
+               rc |= RC_FLAG_TOGGLED;
             }
-            else
+#endif /* def FEATURE_TOGGLE */
+
+#ifdef FEATURE_STATISTICS  
+            if (csp->flags & CSP_FLAG_REJECTED)
             {
-               _exit(0);
+               rc |= RC_FLAG_BLOCKED;
             }
+#endif /* ndef FEATURE_STATISTICS */
+
+            _exit(rc);
          }
          else if (child_id > 0) /* parent */
          {
@@ -2335,14 +2396,30 @@ static void listen_loop(void)
              */
             int child_status;
 #if !defined(_WIN32) && !defined(__CYGWIN__)
+
             wait( &child_status );
+
             /* 
-             * If the child has been toggled (return code 17), toggle ourselves
+             * Evaluate child's return code: If the child has
+             *  - been toggled, toggle ourselves
+             *  - blocked its request, bump up the stats counter
              */
-            if (WIFEXITED(child_status) && (WEXITSTATUS(child_status) == 17))
+
+#ifdef FEATURE_TOGGLE
+            if (WIFEXITED(child_status) && (WEXITSTATUS(child_status) & RC_FLAG_TOGGLED))
+            {
+               global_toggle_state = !global_toggle_state;
+            }
+#endif /* def FEATURE_TOGGLE */
+
+#ifdef FEATURE_STATISTICS
+            urls_read++;
+            if (WIFEXITED(child_status) && (WEXITSTATUS(child_status) & RC_FLAG_BLOCKED))
             {
-               g_bToggleIJB = !g_bToggleIJB;
+               urls_rejected++;
             }
+#endif /* def FEATURE_STATISTICS */ 
+
 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
             close_socket(csp->cfd);
             csp->flags &= ~CSP_FLAG_ACTIVE;