Path: utzoo!mnetor!uunet!husc6!bbn!heron.bbn.com!milliken From: milliken@heron.bbn.com (Walter Milliken) Newsgroups: comp.windows.x Subject: Re: How to lock a sun under X Message-ID: <22533@bbn.COM> Date: 23 Mar 88 18:18:27 GMT References: <3757@mtgzz.UUCP> Sender: news@bbn.COM Reply-To: milliken@bbn.com (Walter Milliken) Organization: BBN Advanced Computers, Inc., Cambridge, MA Lines: 206 Keywords: Security Privacy Proprietary X_Window_System Summary: X11 locking program In article <3757@mtgzz.UUCP> avr@mtgzz.UUCP (XMRP50000[jcm]-a.v.reed) writes: >Our company policy on protecting proprietary information requires that all >workstations and terminals be locked when left unattended, however briefly. >Under suntools, this is readily accomplished with the "Lock Screen" selection in >the top-level menu, which calls /usr/bin/lockscreen. Since there is as yet no >comparable program under the X Window System, the easiest way to be able to lock >the screen under X is to run X 11 (made with -DSUN_WINDOWS) over suntools. I posted such a program for X10 to the xpert list a while back. Here's the X11 version (apologies for posting source here, but it's relatively short). The program locks the workstation and also performs as a screensaver. To unlock the terminal, type your password, followed by a return. No line editing is provided, but you can abort password entry by typing return and starting over. For the incredibly paranoid, you can supply an optional key string on the command line, which will become the unlocking key instead of your password. --------- cut here ------- /* program to secure SUN console under X11 */ #include #include #include #include #define FALSE 0 #define TRUE 1 char * crypt(); #define lckcurs_width 32 #define lckcurs_height 32 #define lckcurs_x_hot 16 #define lckcurs_y_hot 20 static char lckcurs_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0x3e, 0x7c, 0x00, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0xc0, 0x01, 0x80, 0x03, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0xe0, 0x07, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00}; #define lckmask_width 32 #define lckmask_height 32 #define lckmask_x_hot 16 #define lckmask_y_hot 20 static char lckmask_bits[] = { 0x00, 0xe0, 0x07, 0x00, 0x00, 0xf8, 0x1f, 0x00, 0x00, 0xfc, 0x3f, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0xff, 0xff, 0x00, 0x80, 0x3f, 0xfc, 0x01, 0x80, 0x0f, 0xf0, 0x01, 0xc0, 0x07, 0xe0, 0x03, 0xc0, 0x07, 0xe0, 0x03, 0xc0, 0x07, 0xe0, 0x03, 0xe0, 0x03, 0xc0, 0x07, 0xe0, 0x03, 0xc0, 0x07, 0xe0, 0x03, 0xc0, 0x07, 0xf0, 0x03, 0xc0, 0x0f, 0xf0, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf8, 0xff, 0xff, 0x1f, 0xf0, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00}; #endif struct passwd * pw; Display * dsp; /* current display */ Window w; /* window used to cover screen */ int width; /* width of screen */ int height; /* height of screen */ Cursor mycursor; /* "lock" cursor */ Window rootw; /* root window */ int scrn; /* current screen */ Colormap cmap; /* colormap of current screen */ unsigned long black_pixel; /* pixel value for black */ unsigned long white_pixel; /* pixel value for white */ XColor black_color; /* color value for black */ XColor white_color; /* color value for white */ ReadXString(s, slen) char * s; int slen; { int bp; char c; int n; XEvent evt; XKeyEvent * kpevt = (XKeyEvent *) &evt; char keystr[20]; bp = 0; c = 'x'; while (c != '\r') { if (XPending(dsp) == 0) { XWarpPointer(dsp, None, w, 0, 0, 0, 0, (random() % (width-64))+32, (random() % (height-64))+32); sleep(2); } else { XNextEvent(dsp, &evt); if (evt.type == KeyPress) { n = XLookupString(kpevt, keystr, 20, NULL, NULL); if (n>0) { c = keystr[0]; if (c == '\r') s[bp] = '\0'; else s[bp] = c; if (bp < slen-1) bp++; } } else ; /* do nothing */ } } } main(argc, argv) int argc; char * argv[]; { char geometry[40]; char buf[200]; char * pass; char cryptpass[20]; char * s; int n; int i; int done; Pixmap lckcurs; /* temp pixmaps for cursor creation */ Pixmap lckmask; XSetWindowAttributes attrs; /* used to set background, kill redirects */ pw = getpwuid(getuid()); if (argc == 1) pass = pw->pw_passwd; else if (argc == 2) { pass = crypt(argv[1], pw->pw_passwd); strcpy(cryptpass, pass); pass = cryptpass; } else { fprintf(stderr, "usage: %s []\n", argv[0]); exit(1); } dsp = XOpenDisplay(NULL); rootw = DefaultRootWindow(dsp); scrn = DefaultScreen(dsp); width = DisplayWidth(dsp, scrn); height = DisplayHeight(dsp, scrn); cmap = DefaultColormap(dsp, scrn); black_pixel = BlackPixel(dsp, scrn); black_color.pixel = black_pixel; XQueryColor(dsp, cmap, &black_color); white_pixel = WhitePixel(dsp, scrn); white_color.pixel = white_pixel; XQueryColor(dsp, cmap, &white_color); attrs.background_pixel = black_pixel; attrs.override_redirect = True; w = XCreateWindow(dsp, rootw, 0, 0, width, height, 0, 0, InputOutput, CopyFromParent, CWOverrideRedirect|CWBackPixel, &attrs); lckcurs = XCreateBitmapFromData(dsp, w, lckcurs_bits, lckcurs_width, lckcurs_height); lckmask = XCreateBitmapFromData(dsp, w, lckmask_bits, lckmask_width, lckmask_height); mycursor = XCreatePixmapCursor(dsp, lckcurs, lckmask, &black_color, &white_color, lckcurs_x_hot, lckcurs_y_hot); XFreePixmap(dsp, lckcurs); XFreePixmap(dsp, lckmask); XSelectInput(dsp, w, KeyPressMask); XMapWindow(dsp, w); XGrabKeyboard(dsp, w, True, GrabModeAsync, GrabModeAsync, CurrentTime); XGrabPointer(dsp, w, False, -1, GrabModeAsync, GrabModeAsync, None, mycursor, CurrentTime); do { ReadXString(buf, 200); s = crypt(buf, pw->pw_passwd); } while (! (strcmp(s, pass) == 0)); XUngrabPointer(dsp, CurrentTime); XUngrabKeyboard(dsp, CurrentTime); XDestroyWindow(dsp, w); XFlush(dsp); }