[comp.windows.x] xlock patches

naughton@sun.soe.clarkson.edu (Patrick Naughton) (04/12/88)

  Due to suggestions from several comp.windows.x readers, I have made
several more modifications to xlock.  The context diffs are below.

  Philip Karlton of DECWRL suggested that I do an XGrabServer() while
the display was locked and an XUngrabServer() when done.  This turned
out to be not-so-great as Phil and others (including the Xlib docs)
later pointed out.  This locks out all clients from server access,
including ones that are currently running.  After leaving the screen
locked for a day it is kind of fun to watch the clock catch up on its
redisplays, but that's about the only advantage...  8^).  So, what I do
know is save all of the hosts from the access list, remove them, and
disable access control from any of the clients.  When the user unlocks
the screen, the hosts are restored.  They go back in reverse order but
that shouldn't bother anything.  (I believe the access control list is
an unordered list.)

   I also turn off the screen saver while xlock is running and restore
it when it is done, since xlock functions quite nicely as a screen saver
itself. 

   In addition to the user's password, now the root password can unlock
the display.

   Thanks to all those who have made additions or comments about this
program.  It won't be long now before all of our favorite X10 programs
will be up and running under X11. 

-Patrick	 ___________________________________________
		|                                           |
		|  Internet: naughton@sun.soe.clarkson.edu  |
		|  BITNET:   naughton@CLUTX.BITNET          |
		|  uucp:     {rpics, gould}!clutx!naughton  |
		|___________________________________________|


--------------------------------cut here-------------------------------
*** xlock.c.orig	Tue Apr 12 01:20:30 1988
--- xlock.c	Tue Apr 12 02:24:30 1988
***************
*** 1,5 ****
  /*
!  *                             XLOCK V1.3
   *
   *                      A Terminal Locker for X11
   *
--- 1,5 ----
  /*
!  *                             XLOCK V1.4
   *
   *                      A Terminal Locker for X11
   *
***************
*** 31,36 ****
--- 31,40 ----
   *
   *
   * Revision History:
+  * 12-Apr-88: Added root password override.
+  *            Added screen saver override.
+  *            Removed XGrabServer/XUngrabServer (Bad idea Phil...)
+  *            Added access control handling instead.
   * 01-Apr-88: Added XGrabServer/XUngrabServer for more security.
   * 30-Mar-88: Removed startup password requirement (why did I add that?)
   *            Removed cursor to avoid phosphor burn.
***************
*** 120,129 ****
--- 124,136 ----
  char *argv[];
  {
      char buf[10];
+     char *pass;
+     char *rootpass[10];
      XSetWindowAttributes attrs;
      XGCValues xgcv;
      struct passwd *pw;
      Pixmap lockc, lockm;   
+     int timeout, interval, blanking, exposures; /* screen saver parameters */
  
      color = 0;
      if (argc > 1) {
***************
*** 134,139 ****
--- 141,149 ----
  	}
      }
  
+     pw = getpwuid(0);
+     strcpy(rootpass, pw->pw_passwd);
+ 
      if (!(dsp = XOpenDisplay("unix:0"))) {
  	fprintf(stderr, "%s:  unable to open display.\n", argv[0]);
  	exit(1);
***************
*** 176,186 ****
      xgcv.background = black_pixel;
      gc = (GContext) XCreateGC(dsp, w, GCForeground | GCBackground, &xgcv);
  
      XGrabKeyboard(dsp, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
      XGrabPointer(dsp, w, False, -1, GrabModeAsync, GrabModeAsync, None,
  		 mycursor, CurrentTime);
-     XGrabServer(dsp);
  
      pw = getpwuid(getuid());
  
      srandom(time(NULL));
--- 186,200 ----
      xgcv.background = black_pixel;
      gc = (GContext) XCreateGC(dsp, w, GCForeground | GCBackground, &xgcv);
  
+     XGetScreenSaver(dsp, &timeout, &interval, &blanking, &exposures);
+     XSetScreenSaver(dsp, 0, 0, 0, 0); /* disable screen saver */
+ 
      XGrabKeyboard(dsp, w, True, GrabModeAsync, GrabModeAsync, CurrentTime);
      XGrabPointer(dsp, w, False, -1, GrabModeAsync, GrabModeAsync, None,
  		 mycursor, CurrentTime);
  
+     GrabHosts();
+ 
      pw = getpwuid(getuid());
  
      srandom(time(NULL));
***************
*** 187,199 ****
      do {
  	inithop();
  	ReadXString(buf, 10);
!     } while (strcmp(crypt(buf, pw->pw_passwd), pw->pw_passwd));
  
!     XUngrabServer(dsp);
      XUngrabPointer(dsp, CurrentTime);
      XUngrabKeyboard(dsp, CurrentTime);
      XDestroyWindow(dsp, w);
      XFlush(dsp);
  }
  
  
--- 201,236 ----
      do {
  	inithop();
  	ReadXString(buf, 10);
!     } while ((strcmp(crypt(buf, pw->pw_passwd), pw->pw_passwd))
! 	  && (strcmp(crypt(buf, rootpass), rootpass)));
  
!     UngrabHosts();
! 
      XUngrabPointer(dsp, CurrentTime);
      XUngrabKeyboard(dsp, CurrentTime);
+ 
+     XSetScreenSaver(dsp, timeout, interval, blanking, exposures);
+ 
      XDestroyWindow(dsp, w);
      XFlush(dsp);
+ }
+ 
+ XHostAddress *hosts;
+ int num_hosts, state;
+ 
+ GrabHosts()
+ {
+     XDisableAccessControl(dsp);
+     hosts = XListHosts(dsp, &num_hosts, &state);
+     XRemoveHosts(dsp, hosts, num_hosts);
+ }
+ 
+ 
+ UngrabHosts()
+ {
+     XEnableAccessControl(dsp);
+     XAddHosts(dsp, hosts, num_hosts);
+     XFree(hosts);
  }