You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
3.9 KiB

  1. /* See LICENSE file for license details. */
  2. #define _XOPEN_SOURCE 500
  3. #if HAVE_SHADOW_H
  4. #include <shadow.h>
  5. #endif
  6. #include <ctype.h>
  7. #include <pwd.h>
  8. #include <stdarg.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <X11/keysym.h>
  15. #include <X11/Xlib.h>
  16. #include <X11/Xutil.h>
  17. #if HAVE_BSD_AUTH
  18. #include <login_cap.h>
  19. #include <bsd_auth.h>
  20. #endif
  21. static void
  22. die(const char *errstr, ...) {
  23. va_list ap;
  24. va_start(ap, errstr);
  25. vfprintf(stderr, errstr, ap);
  26. va_end(ap);
  27. exit(EXIT_FAILURE);
  28. }
  29. #ifndef HAVE_BSD_AUTH
  30. static const char *
  31. get_password() { /* only run as root */
  32. const char *rval;
  33. struct passwd *pw;
  34. if(geteuid() != 0)
  35. die("slock: cannot retrieve password entry (make sure to suid slock)\n");
  36. pw = getpwuid(getuid());
  37. endpwent();
  38. rval = pw->pw_passwd;
  39. #if HAVE_SHADOW_H
  40. {
  41. struct spwd *sp;
  42. sp = getspnam(getenv("USER"));
  43. endspent();
  44. rval = sp->sp_pwdp;
  45. }
  46. #endif
  47. /* drop privileges */
  48. if(setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0)
  49. die("slock: cannot drop privileges\n");
  50. return rval;
  51. }
  52. #endif
  53. int
  54. main(int argc, char **argv) {
  55. char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
  56. char buf[32], passwd[256];
  57. int num, screen;
  58. #ifndef HAVE_BSD_AUTH
  59. const char *pws;
  60. #endif
  61. unsigned int len;
  62. Bool running = True;
  63. Cursor invisible;
  64. Display *dpy;
  65. KeySym ksym;
  66. Pixmap pmap;
  67. Window root, w;
  68. XColor black, dummy;
  69. XEvent ev;
  70. XSetWindowAttributes wa;
  71. if((argc == 2) && !strcmp("-v", argv[1]))
  72. die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
  73. "based on slock, which is © 2006-2008 Anselm R Garbe\n");
  74. else if(argc != 1)
  75. die("usage: slock [-v]\n");
  76. #ifndef HAVE_BSD_AUTH
  77. pws = get_password();
  78. #endif
  79. if(!(dpy = XOpenDisplay(0)))
  80. die("slock: cannot open display\n");
  81. screen = DefaultScreen(dpy);
  82. root = RootWindow(dpy, screen);
  83. if (fork() != 0)
  84. return 0;
  85. /* init */
  86. wa.override_redirect = 1;
  87. wa.background_pixel = WhitePixel(dpy, screen);
  88. w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
  89. 0, DefaultDepth(dpy, screen), CopyFromParent,
  90. DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
  91. XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
  92. pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
  93. invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
  94. XDefineCursor(dpy, w, invisible);
  95. XMapRaised(dpy, w);
  96. for(len = 1000; len; len--) {
  97. if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  98. GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
  99. break;
  100. usleep(1000);
  101. }
  102. if((running = running && (len > 0))) {
  103. for(len = 1000; len; len--) {
  104. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  105. == GrabSuccess)
  106. break;
  107. usleep(1000);
  108. }
  109. running = (len > 0);
  110. }
  111. len = 0;
  112. XSync(dpy, False);
  113. /* main event loop */
  114. while(running && !XNextEvent(dpy, &ev)) {
  115. if(ev.type == KeyPress) {
  116. buf[0] = 0;
  117. num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
  118. if(IsKeypadKey(ksym)) {
  119. if(ksym == XK_KP_Enter)
  120. ksym = XK_Return;
  121. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  122. ksym = (ksym - XK_KP_0) + XK_0;
  123. }
  124. if(IsFunctionKey(ksym) || IsKeypadKey(ksym)
  125. || IsMiscFunctionKey(ksym) || IsPFKey(ksym)
  126. || IsPrivateKeypadKey(ksym))
  127. continue;
  128. switch(ksym) {
  129. case XK_Return:
  130. passwd[len] = 0;
  131. #ifdef HAVE_BSD_AUTH
  132. running = !auth_userokay(getlogin(), NULL, "auth-xlock", passwd);
  133. #else
  134. running = strcmp(crypt(passwd, pws), pws);
  135. #endif
  136. len = 0;
  137. break;
  138. case XK_Escape:
  139. len = 0;
  140. break;
  141. case XK_BackSpace:
  142. if(len)
  143. --len;
  144. break;
  145. default:
  146. if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
  147. memcpy(passwd + len, buf, num);
  148. len += num;
  149. }
  150. break;
  151. }
  152. }
  153. }
  154. XUngrabPointer(dpy, CurrentTime);
  155. XFreePixmap(dpy, pmap);
  156. XDestroyWindow(dpy, w);
  157. XCloseDisplay(dpy);
  158. return 0;
  159. }