Fixing me... OS/2 and Mac OSX
[privoxy.git] / jcc.c
diff --git a/jcc.c b/jcc.c
index a884031..e64a979 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.69 2002/03/04 23:50:00 jongfoster Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.71 2002/03/05 18:13:56 oes Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -33,6 +33,12 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.69 2002/03/04 23:50:00 jongfoster Exp $";
  *
  * Revisions   :
  *    $Log: jcc.c,v $
+ *    Revision 1.71  2002/03/05 18:13:56  oes
+ *    Added --user option
+ *
+ *    Revision 1.70  2002/03/05 04:52:42  oes
+ *    Deleted non-errlog debugging code
+ *
  *    Revision 1.69  2002/03/04 23:50:00  jongfoster
  *    Splitting off bind_port() call into bind_port_helper(), with
  *    improved logging.
@@ -463,6 +469,10 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.69 2002/03/04 23:50:00 jongfoster Exp $";
 #include <sys/termios.h>
 #endif /* sun */
 
+#ifdef unix
+#include <pwd.h>
+#endif
+
 # include <signal.h>
 
 # ifdef __BEOS__
@@ -1515,7 +1525,7 @@ static int32 server_thread(void *data)
 void usage(const char *myname)
 {
    printf("JunkBuster proxy version " VERSION " (" HOME_PAGE_URL ")\n"
-           "Usage: %s [--help] [--version] [--no-daemon] [--pidfile pidfile] [configfile]\n"
+           "Usage: %s [--help] [--version] [--no-daemon] [--pidfile pidfile] [--user user] [configfile]\n"
            "Aborting.\n", myname);
  
    exit(2);
@@ -1553,6 +1563,7 @@ int main(int argc, const char *argv[])
 #endif
 {
    int argc_pos = 0;
+   struct passwd *pw;
 
    Argc = argc;
    Argv = argv;
@@ -1587,12 +1598,24 @@ int main(int argc, const char *argv[])
       {
          no_daemon = 1;
       }
-
+#if !defined(__OS2__)
       else if (strcmp(argv[argc_pos], "--pidfile" ) == 0)
       {
          if (++argc_pos == argc) usage(argv[0]);
          pidfile = strdup(argv[argc_pos]);
       }
+
+      else if (strcmp(argv[argc_pos], "--user" ) == 0)
+      {
+         if (++argc_pos == argc) usage(argv[0]);
+         pw = getpwnam(argv[argc_pos]);
+                   s
+         if (pw == NULL)
+         {
+            log_error(LOG_LEVEL_FATAL, "User %s not found.", argv[argc_pos]);
+         }
+      }
+#endif /* !defined(__OS2__) */
       else
 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
       {
@@ -1744,9 +1767,19 @@ int main(int argc, const char *argv[])
       close( 1 );
       chdir("/");
 
-      write_pid_file();
-
    } /* -END- if (!no_daemon) */
+
+   /*
+    * As soon as we have written the PID file, we can switch
+    * to the user ID indicated by the --user option
+    */
+   write_pid_file();
+   
+   if (setuid(pw->pw_uid))
+   {
+      log_error(LOG_LEVEL_FATAL, "Cannot setuid(): Insufficient permissions.");
+   }
+
 }
 #endif /* defined unix */