Hello, list,

It seems xc/programs/luit/sys.c in XFree86 4.3.0 contains a off-by-one
bug.  A patch (sys.c.patch) to fix this is attached.

Besides, I have some more modifications to luit which is useful on
FreeBSD:
- Make luit use openpty to search an unused pty.  Without this patch,
  luit aborts after opening ten or so xterms.
- Allow one to setuid luit.
The patch to modify these is attached (luit-freebsd.patch).  To try
it, apply this patch instead of sys.c.patch.

Best regards,
Tsuyoshi

---   ITO Tsuyoshi  <[EMAIL PROTECTED]>   ---
--- Dept. of Computer Science, University of Tokyo. ---
--- xc/programs/luit/sys.c.orig Tue Jan  8 05:38:30 2002
+++ xc/programs/luit/sys.c      Fri Mar 14 13:17:15 2003
@@ -373,7 +373,7 @@
     goto bail;
 
   found:
-    line = malloc(strlen(name));
+    line = malloc(strlen(name) + 1);
     strcpy(line, name);
     line[5] = 't';
     fix_pty_perms(line);
--- xc/programs/luit/Imakefile.orig     Thu Oct 17 10:06:09 2002
+++ xc/programs/luit/Imakefile  Tue Mar 11 03:19:58 2003
@@ -8,7 +8,7 @@
 LOCAL_LIBRARIES = $(XFONTENCLIB)
 DEPLIBS = $(DEPXFONTENCLIB)
 
-SYS_LIBRARIES = MathLibrary GzipLibrary
+SYS_LIBRARIES = MathLibrary GzipLibrary -lutil
 
 SRCS = luit.c iso2022.c charset.c parser.c sys.c other.c
 
--- xc/programs/luit/luit.c.orig        Mon Feb 24 10:10:25 2003
+++ xc/programs/luit/luit.c     Tue Mar 11 04:04:53 2003
@@ -439,8 +439,8 @@
 condom(int argc, char **argv)
 {
     int pty;
+    int tty;
     int pid;
-    char *line;
     char *path;
     char **child_argv;
     int rc;
@@ -450,7 +450,7 @@
     if(rc < 0)
         FatalError("Couldn't parse arguments\n");
 
-    rc = allocatePty(&pty, &line);
+    rc = allocatePty(&pty, &tty);
     if(rc < 0) {
         perror("Couldn't allocate pty");
         exit(1);
@@ -470,11 +470,11 @@
 
     if(pid == 0) {
         close(pty);
-        child(line, path, child_argv);
+        child(tty, path, child_argv);
     } else {
+        close(tty);
         free(child_argv);
         free(path);
-        free(line);
         parent(pid, pty);
     }
 
@@ -482,9 +482,8 @@
 }
 
 void
-child(char *line, char *path, char **argv)
+child(int tty, char *path, char **argv)
 {
-    int tty;
     int pgrp;
 
     close(0);
@@ -496,8 +495,7 @@
         exit(1);
     }
 
-    tty = openTty(line);
-    if(tty < 0) {
+    if(openTty(tty) < 0) {
         kill(getppid(), SIGABRT);
         exit(1);
     }
diff -ru old/xc/programs/luit/luit.h new/xc/programs/luit/luit.h
--- xc/programs/luit/luit.h.orig        Fri Nov  2 12:06:43 2001
+++ xc/programs/luit/luit.h     Tue Mar 11 04:04:03 2003
@@ -26,6 +26,6 @@
 extern int ilog;
 extern int olog;
 
-void child(char*, char*, char**);
+void child(int, char*, char**);
 void parent(int, int);
 
--- xc/programs/luit/sys.c.orig Tue Jan  8 05:38:30 2002
+++ xc/programs/luit/sys.c      Tue Mar 11 06:54:05 2003
@@ -33,6 +33,7 @@
 #include <termios.h>
 #include <signal.h>
 #include <errno.h>
+#include <libutil.h>
 
 #ifdef SVR4
 #define HAVE_POLL
@@ -276,17 +277,17 @@
 }
 
 static int
-fix_pty_perms(char *line)
+fix_pty_perms(int tty)
 {
     int rc;
     struct stat s;
     int uid = getuid(), gid = getgid();
 
-    rc = stat(line, &s);
+    rc = fstat(tty, &s);
     if(rc < 0)
         return -1;
     if(s.st_uid != uid || s.st_gid != gid) {
-        rc = chown(line, getuid(), getgid());
+        rc = fchown(tty, getuid(), getgid());
         if(rc < 0) {
             fprintf(stderr, 
                     "Warning: could not change ownership of tty -- "
@@ -295,7 +296,7 @@
         }
     }
     if((s.st_mode & 0777) != (S_IRUSR | S_IWUSR | S_IWGRP)) {
-        rc = chmod(line, S_IRUSR | S_IWUSR | S_IWGRP);
+        rc = fchmod(tty, S_IRUSR | S_IWUSR | S_IWGRP);
         if (rc < 0) {
             fprintf(stderr,
                     "Warning: could not change permissions of tty -- "
@@ -307,98 +308,23 @@
 }
 
 int
-allocatePty(int *pty_return, char **line_return)
+allocatePty(int *pty_return, int *tty_return)
 {
-    char name[12], *line = NULL;
-    int pty = -1;
-    char *name1 = "pqrstuvwxyzPQRST", *name2 = "0123456789abcdef";
-    char *p1, *p2;
+    int pty;
+    int tty;
 
-#ifdef HAVE_GRANTPT
-    char *temp_line;
-    int rc;
-
-    pty = open("/dev/ptmx", O_RDWR);
-    if(pty < 0)
-        goto bsd;
-
-    rc = grantpt(pty);
-    if(rc < 0) {
-        close(pty);
-        goto bsd;
-    }
-
-    rc = unlockpt(pty);
-    if(rc < 0) {
-        close(pty);
-        goto bsd;
-    }
-
-    temp_line = ptsname(pty);
-    if(!temp_line) {
-        close(pty);
-        goto bsd;
-    }
-    line = malloc(strlen(temp_line) + 1);
-    if(!line) {
-        close(pty);
+    if (openpty(&pty, &tty, NULL, NULL, NULL) == -1)
         return -1;
-    }
-    strcpy(line, temp_line);
-
-    fix_pty_perms(line);
-
-    *pty_return = pty;
-    *line_return = line;
-    return 0;
-
-  bsd:
-#endif /* HAVE_GRANTPT */
-
-    strcpy(name, "/dev/pty??");
-    for(p1 = name1; *p1; p1++) {
-        name[8] = *p1;
-        for(p2 = name2; *p2; p2++) {
-            name[9] = *p2;
-            pty = open(name, O_RDWR);
-            if(pty >= 0)
-                goto found;
-            if(errno == ENOENT)
-                goto bail;
-            else
-                continue;
-        }
-    }
-
-    goto bail;
-
-  found:
-    line = malloc(strlen(name));
-    strcpy(line, name);
-    line[5] = 't';
-    fix_pty_perms(line);
+    fix_pty_perms(tty);
     *pty_return = pty;
-    *line_return = line;
+    *tty_return = tty;
     return 0;
-
-  bail:
-    if(pty >= 0)
-        close(pty);
-    if(line)
-        free(line);
-    return -1;
 }
 
 int
-openTty(char *line)
+openTty(int tty)
 {
     int rc;
-    int tty = -1;
-
-    tty = open(line, O_RDWR | O_NOCTTY);
- 
-    if(tty < 0)
-        goto bail;
 
 #ifdef TIOCSCTTY
     rc = ioctl(tty, TIOCSCTTY, (char *)0);
@@ -429,7 +355,6 @@
     return -1;
 }
 
-#ifdef _POSIX_SAVED_IDS
 int
 droppriv()
 {
@@ -439,19 +364,3 @@
         return rc;
     return setgid(getgid());
 }
-#else
-int
-droppriv()
-{
-    int uid = getuid();
-    int euid = geteuid();
-    int gid = getgid();
-    int egid = getegid();
-
-    if(uid != euid || gid != egid) {
-        errno = ENOSYS;
-        return -1;
-    }
-    return 0;
-}
-#endif    
--- xc/programs/luit/sys.h.orig Fri Nov  2 12:06:43 2001
+++ xc/programs/luit/sys.h      Tue Mar 11 04:02:28 2003
@@ -28,6 +28,6 @@
 int restoreTermios(void);
 int setRawTermios(void);
 char *my_basename(char *path);
-int allocatePty(int *pty_return, char **line_return);
-int openTty(char *line);
+int allocatePty(int *pty_return, int *tty_return);
+int openTty(int tty);
 int droppriv(void);

Reply via email to