*** empty log message ***
authorroot <root>
Sun, 22 Jan 2006 04:01:52 +0000 (04:01 +0000)
committerroot <root>
Sun, 22 Jan 2006 04:01:52 +0000 (04:01 +0000)
src/logging.C
src/main.C
src/ptytty.C
src/ptytty.h
src/ptytty_conf.h
src/rxvtc.C
src/rxvtd.C

index 4f1dbf6c56bb5f302552026b00ba73d5cc6892c9..a31d46c2155d76151a4293c738d2bc714ee3a1b5 100644 (file)
 
 #include "../config.h"
 
+#include <cstdio>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/fcntl.h>
+#include <unistd.h>
+#include <time.h>
+
 #include "ptytty.h"
 
 #if UTMP_SUPPORT
@@ -89,7 +97,7 @@ ptytty_unix::login (int cmd_pid, bool login_shell, const char *hostname)
     sprintf (ut_id, "vt%02x", (i & 0xff));     /* sysv naming */
   else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3))
     {
-      warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
+      ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty);
       return;
     }
 #endif
@@ -419,7 +427,7 @@ update_lastlog (const char *fname, const char *pty, const char *host)
   pwent = getpwuid (getuid ());
   if (!pwent)
     {
-      warn ("no entry in password file, not updating lastlog.\n");
+      ptytty_warn ("no entry in password file, not updating lastlog.\n");
       return;
     }
 
index 56c72d27fd624dc9935d5f6fd7451d2707e7b91d..c5f258348980b3e436c1511a13c754059d7b11fd 100644 (file)
@@ -476,7 +476,7 @@ rxvt_term::init (int argc, const char *const *argv, stringvec *envv)
     scrollBar.setIdle ();    /* set existence for size calculations */
 #endif
 
-  pty = ptytty_new ();
+  pty = ptytty::create ();
 
   create_windows (argc, argv);
 
@@ -552,36 +552,7 @@ char **rxvt_environ; // startup environment
 void
 rxvt_init ()
 {
-  uid_t uid = getuid ();
-  gid_t gid = getgid ();
-      
-  // before doing anything else, check for setuid/setgid operation,
-  // start the helper process and drop privileges
-  if (uid != geteuid ()
-      || gid != getegid ())
-    {
-#if PTYTTY_HELPER
-      ptytty_server ();
-#else
-      rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
-#endif
-
-      // drop privileges
-#if HAVE_SETRESUID
-      setresgid (gid, gid, gid);
-      setresuid (uid, uid, uid);
-#elif HAVE_SETREUID
-      setregid (gid, gid);
-      setreuid (uid, uid);
-#elif HAVE_SETUID
-      setgid (gid);
-      setuid (uid);
-#endif
-
-      if (uid != geteuid ()
-          || gid != getegid ())
-        rxvt_fatal ("unable to drop privileges, aborting.\n");
-    }
+  ptytty::init ();
 
   rxvt_environ = environ;
 
index 1c2cb7e579b1f080683785c0637f7727169d02e2..c4580f312170f52c4d72bd2e8153d2e6242f4e01 100644 (file)
@@ -26,7 +26,6 @@
 
 #include "../config.h"
 
-#include "fdpass.h"
 #include "ptytty.h"
 
 #include <cstdlib>
@@ -237,7 +236,7 @@ control_tty (int fd_tty)
 
   int fd = open ("/dev/tty", O_WRONLY);
   if (fd < 0)
-    return -1;         /* fatal */
+    return -1; /* fatal */
 
   close (fd);
 
@@ -375,8 +374,8 @@ ptytty_unix::get ()
 
 #if PTYTTY_HELPER
 
-static int sock_fd;
-static int pid;
+static int sock_fd = -1;
+static int helper_pid, owner_pid;
 
 struct command
 {
@@ -414,14 +413,14 @@ ptytty_proxy::get ()
   write (sock_fd, &cmd, sizeof (cmd));
 
   if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
-    fatal ("protocol error while creating pty using helper process, aborting.\n");
+    ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
 
   if (!id)
     return false;
 
-  if ((pty = ptytty_recv_fd (sock_fd)) < 0
-      || (tty = ptytty_recv_fd (sock_fd)) < 0)
-    fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
+  if ((pty = recv_fd (sock_fd)) < 0
+      || (tty = recv_fd (sock_fd)) < 0)
+    ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
 
   return true;
 }
@@ -471,8 +470,8 @@ void serve ()
               write (sock_fd, &cmd.id, sizeof (cmd.id));
               ptys.push_back (cmd.id);
 
-              ptytty_send_fd (sock_fd, cmd.id->pty);
-              ptytty_send_fd (sock_fd, cmd.id->tty);
+              ptytty::send_fd (sock_fd, cmd.id->pty);
+              ptytty::send_fd (sock_fd, cmd.id->tty);
             }
           else
             {
@@ -493,9 +492,9 @@ void serve ()
         }
       else if (cmd.type == command::destroy)
         {
-          ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id);
+          vector<ptytty *>::iterator pty = find (ptys.begin (), ptys.end (), cmd.id);
 
-          if (pty)
+          if (pty != ptys.end ())
             {
               delete *pty;
               ptys.erase (pty);
@@ -506,23 +505,31 @@ void serve ()
     }
 
   // destroy all ptys
-  for (ptytty **i = ptys.end (); i-- > ptys.begin (); )
+  for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
     delete *i;
 }
 
-void ptytty_server ()
+void
+ptytty::use_helper ()
 {
+  int pid = getpid ();
+
+  if (sock_fd >= 0 && pid == owner_pid)
+    return;
+
+  owner_pid = pid;
+
   int sv[2];
 
   if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
-    fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
+    ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
 
-  pid = fork ();
+  helper_pid = fork ();
 
-  if (pid < 0)
-    fatal ("could not create pty/sessiondb helper process, aborting.\n");
+  if (helper_pid < 0)
+    ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
 
-  if (pid)
+  if (helper_pid)
     {
       // client, process
       sock_fd = sv[0];
@@ -534,6 +541,8 @@ void ptytty_server ()
       // server, pty-helper
       sock_fd = sv[1];
 
+      chdir ("/");
+
       for (int fd = 0; fd < 1023; fd++)
         if (fd != sock_fd)
           close (fd);
@@ -545,12 +554,11 @@ void ptytty_server ()
 
 #endif
 
-// a "factory" *g*
 ptytty *
-new_ptytty ()
+ptytty::create ()
 {
 #if PTYTTY_HELPER
-  if (pid > 0)
+  if (helper_pid && getpid () == owner_pid)
     // use helper process
     return new ptytty_proxy;
   else
@@ -558,5 +566,47 @@ new_ptytty ()
     return new ptytty_unix;
 }
 
-/*----------------------- end-of-file (C source) -----------------------*/
+void
+ptytty::init ()
+{
+  uid_t uid = getuid ();
+  gid_t gid = getgid ();
+      
+  // before doing anything else, check for setuid/setgid operation,
+  // start the helper process and drop privileges
+  if (uid != geteuid ()
+      || gid != getegid ())
+    {
+#if PTYTTY_HELPER
+      use_helper ();
+#else
+      ptytty_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
+#endif
+
+      drop_privileges ();
+    }
+}
+
+void
+ptytty::drop_privileges ()
+{
+  uid_t uid = getuid ();
+  gid_t gid = getgid ();
+
+  // drop privileges
+#if HAVE_SETRESUID
+  setresgid (gid, gid, gid);
+  setresuid (uid, uid, uid);
+#elif HAVE_SETREUID
+  setregid (gid, gid);
+  setreuid (uid, uid);
+#elif HAVE_SETUID
+  setgid (gid);
+  setuid (uid);
+#endif
+
+  if (uid != geteuid ()
+      || gid != getegid ())
+    ptytty_fatal ("unable to drop privileges, aborting.\n");
+}
 
index ff58306f5e93f48436774f1d91f4762737e2314d..651e32220270cb22eea473aacc81ce727f21ef4a 100644 (file)
@@ -49,6 +49,8 @@
 
 #endif
 
+#define fatal(msg) do { write (2, msg, sizeof (msg) - 1); _exit (255); } while (0)
+
 struct ptytty_unix : ptytty
 {
   char *name;
index 05b4ba19dcd46627c513c206ecf864b8a622d38a..e74c9f97d60718640d287ade8a011f1321c90219 100644 (file)
@@ -1,3 +1,5 @@
 
-#include "rxvtutil.h"
-#include "feature.h"
+#include "rxvt.h"
+
+#define ptytty_fatal rxvt_fatal
+#define ptytty_warn  rxvt_warn
index 31a7ff4c0a1b6ba9c8d15ad8f759f33739557871..417a855185fc4bc794ee0c3985179cc73a88c9c6 100644 (file)
  *----------------------------------------------------------------------*/
 
 #include "../config.h"
-#include "rxvtdaemon.h"
-#include "fdpass.h"
-
-#include "rxvt.h"
-
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
 #include <sys/socket.h>
 #include <sys/un.h>
 
+#include "rxvtdaemon.h"
+#include "libptytty.h"
+
+#include "rxvt.h"
+
 struct client : rxvt_connection {
   client ();
 };
@@ -112,7 +112,7 @@ main (int argc, const char *const *argv)
       fprintf (stderr, "%s", (const char *)tok);
     else if (!strcmp (tok, "GETFD") && c.recv (cint))
       {
-        if (rxvt_send_fd (c.fd, cint) < 0)
+        if (ptytty::send_fd (c.fd, cint) < 0)
           {
             fprintf (stderr, "unable to send fd %d: ", cint); perror (0);
             exit (EXIT_FAILURE);
index 4758132d5c91f72cb9a7a865cf106cb0e200ee61..cdb6bd73bb7bef7aa72a6c4dc3b80566b945babc 100644 (file)
  *----------------------------------------------------------------------*/
 
 #include "../config.h"
-#include "rxvt.h"
-#include "rxvtdaemon.h"
-#include "fdpass.h"
-#include "iom.h"
-
 #include <cstdio>
 #include <cstdlib>
 #include <cstdarg>
 
 #include <cerrno>
 
-extern char **environ;
+#include "rxvt.h"
+#include "rxvtdaemon.h"
+#include "libptytty.h"
+#include "iom.h"
 
 struct server : rxvt_connection {
   log_callback log_cb;
@@ -122,7 +120,7 @@ int server::getfd (int remote_fd)
 {
   send ("GETFD");
   send (remote_fd);
-  return rxvt_recv_fd (fd);
+  return ptytty::recv_fd (fd);
 }
 
 void server::log_msg (const char *msg)