$OpenBSD: patch-konsole_konsole_konsole_grantpty_c,v 1.3 2003/01/27 19:22:50 espie Exp $
--- konsole/konsole/konsole_grantpty.c.orig	Sun Jan 12 15:24:11 2003
+++ konsole/konsole/konsole_grantpty.c	Mon Jan 13 10:02:31 2003
@@ -39,10 +39,64 @@
 #  include <paths.h>
 #  include <dirent.h>
 #endif
+#ifdef HAVE_LIBUTIL_H
+        #include <libutil.h>
+#elif defined(HAVE_UTIL_H)
+        #include <util.h>
+#endif
+#include <utmp.h>
+
 
 #define PTY_FILENO 3    /* keep in sync with grantpty */
 #define TTY_GROUP "tty"
 
+int perform_login()
+{
+  char *str_ptr;
+  struct utmp l_struct;
+  memset(&l_struct, 0, sizeof(struct utmp));
+
+  if (dup2(PTY_FILENO, 0) == -1)
+  	return 1;
+  if (! (str_ptr=getlogin()) ) {
+    if ( ! (str_ptr=getenv("LOGNAME"))) {
+      return 1;
+    }
+  }
+  strncpy(l_struct.ut_name, str_ptr, UT_NAMESIZE);
+
+  if (gethostname(l_struct.ut_host, UT_HOSTSIZE) == -1) {
+     if (errno != ENOMEM)
+        return 1;
+     l_struct.ut_host[UT_HOSTSIZE]=0;
+  }
+
+  if (! (str_ptr=ttyname(PTY_FILENO)))
+    return 1;
+  if (strncmp(str_ptr, "/dev/", 5) == 0)
+       str_ptr += 5;
+  strncpy(l_struct.ut_line, str_ptr, UT_LINESIZE);
+  time(&l_struct.ut_time);
+
+  login(&l_struct);
+  return 0;
+}
+
+int perform_logout()
+{
+  char *tty_name=ttyname(PTY_FILENO);
+  if (tty_name)
+  {
+  	if (strncmp(tty_name, "/dev/", 5) == 0)
+	    tty_name += 5;
+	if (*tty_name == 'p')
+		*tty_name = 't';
+        logout(tty_name);
+	return 0;
+  }
+  return 1;
+}
+
 int main (int argc, char *argv[])
 {
   char*         pty;
@@ -54,7 +108,7 @@ int main (int argc, char *argv[])
   char*         tty;
 
   /* check preconditions **************************************************/
-  if (argc != 2 || (strcmp(argv[1],"--grant") && strcmp(argv[1],"--revoke")))
+  if (argc != 2 || (strcmp(argv[1],"--grant") && strcmp(argv[1],"--revoke") && strcmp(argv[1], "--login") && strcmp(argv[1], "--logout")))
   {
     printf("usage: %s (--grant|--revoke)\n",argv[0]);
     printf("%s is a helper for\n",argv[0]);
@@ -70,6 +124,11 @@ int main (int argc, char *argv[])
     fprintf(stderr,"%s not installed root-suid\n",argv[0]);
     return 1; /* FAIL */
   }
+
+  if (!strcmp(argv[1], "--login"))
+  	return perform_login();
+  if (!strcmp(argv[1], "--logout"))
+  	return perform_logout();
 
   /* setup parameters for the operation ***********************************/
 
