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.

269 lines
9.0 KiB

  1. /*
  2. * vim:ts=8:expandtab
  3. *
  4. * i3lock - an improved version of slock
  5. *
  6. * i3lock © 2009 Michael Stapelberg and contributors
  7. * slock © 2006-2008 Anselm R Garbe
  8. *
  9. * See file LICENSE for license information.
  10. *
  11. * Note that on any error (calloc is out of memory for example)
  12. * we do not do anything so that the user can fix the error by
  13. * himself (kill X to get more free memory or stop some other
  14. * program using SSH/console).
  15. *
  16. */
  17. #define _XOPEN_SOURCE 500
  18. #include <ctype.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <X11/keysym.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/xpm.h>
  29. #include <X11/extensions/dpms.h>
  30. #include <stdbool.h>
  31. #include <getopt.h>
  32. #include <security/pam_appl.h>
  33. static char passwd[256];
  34. static void die(const char *errstr, ...) {
  35. va_list ap;
  36. va_start(ap, errstr);
  37. vfprintf(stderr, errstr, ap);
  38. va_end(ap);
  39. exit(EXIT_FAILURE);
  40. }
  41. /*
  42. * Check if given file can be opened => exists
  43. *
  44. */
  45. bool file_exists(const char * filename)
  46. {
  47. FILE * file = fopen(filename, "r");
  48. if(file)
  49. {
  50. fclose(file);
  51. return true;
  52. }
  53. return false;
  54. }
  55. /*
  56. * Callback function for PAM. We only react on password request callbacks.
  57. *
  58. */
  59. static int conv_callback(int num_msg, const struct pam_message **msg,
  60. struct pam_response **resp, void *appdata_ptr) {
  61. if (num_msg == 0)
  62. return 1;
  63. /* PAM expects an arry of responses, one for each message */
  64. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  65. perror("calloc");
  66. return 1;
  67. }
  68. for (int c = 0; c < num_msg; c++) {
  69. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  70. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  71. continue;
  72. /* return code is currently not used but should be set to zero */
  73. resp[c]->resp_retcode = 0;
  74. if ((resp[c]->resp = strdup(passwd)) == NULL) {
  75. perror("strdup");
  76. return 1;
  77. }
  78. }
  79. return 0;
  80. }
  81. int main(int argc, char *argv[]) {
  82. char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
  83. char buf[32];
  84. char *username;
  85. int num, screen;
  86. unsigned int len;
  87. bool running = true;
  88. /* By default, fork, don’t beep and don’t turn off monitor */
  89. bool dont_fork = false;
  90. bool beep = false;
  91. bool dpms = false;
  92. bool xpm_image = false;
  93. char xpm_image_path[256];
  94. Cursor invisible;
  95. Display *dpy;
  96. KeySym ksym;
  97. Pixmap pmap;
  98. Window root, w;
  99. XColor black, dummy;
  100. XEvent ev;
  101. XSetWindowAttributes wa;
  102. pam_handle_t *handle;
  103. struct pam_conv conv = {conv_callback, NULL};
  104. char opt;
  105. int optind = 0;
  106. static struct option long_options[] = {
  107. {"version", no_argument, NULL, 'v'},
  108. {"nofork", no_argument, NULL, 'n'},
  109. {"beep", no_argument, NULL, 'b'},
  110. {"dpms", no_argument, NULL, 'd'},
  111. {"image", required_argument, NULL, 'i'},
  112. {NULL, no_argument, NULL, 0}
  113. };
  114. while ((opt = getopt_long(argc, argv, "vnbdi:", long_options, &optind)) != -1) {
  115. switch (opt) {
  116. case 'v':
  117. die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
  118. "based on slock, which is © 2006-2008 Anselm R Garbe\n");
  119. case 'n':
  120. dont_fork = true;
  121. break;
  122. case 'b':
  123. beep = true;
  124. break;
  125. case 'd':
  126. dpms = true;
  127. break;
  128. case 'i':
  129. strncpy(xpm_image_path, optarg, 255);
  130. xpm_image = true;
  131. break;
  132. default:
  133. die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm]\n");
  134. }
  135. }
  136. if ((username = getenv("USER")) == NULL)
  137. die("USER environment variable not set, please set it.\n");
  138. int ret = pam_start("i3lock", username, &conv, &handle);
  139. if (ret != PAM_SUCCESS)
  140. die("PAM: %s\n", pam_strerror(handle, ret));
  141. if(!(dpy = XOpenDisplay(0)))
  142. die("i3lock: cannot open display\n");
  143. screen = DefaultScreen(dpy);
  144. root = RootWindow(dpy, screen);
  145. if (!dont_fork) {
  146. if (fork() != 0)
  147. return 0;
  148. }
  149. /* init */
  150. wa.override_redirect = 1;
  151. wa.background_pixel = WhitePixel(dpy, screen);
  152. w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
  153. 0, DefaultDepth(dpy, screen), CopyFromParent,
  154. DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
  155. XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
  156. pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
  157. invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
  158. XDefineCursor(dpy, w, invisible);
  159. XMapRaised(dpy, w);
  160. if(xpm_image && file_exists(xpm_image_path))
  161. {
  162. GC gc = XDefaultGC(dpy, 0);
  163. int depth = DefaultDepth(dpy, screen);
  164. int disp_width = DisplayWidth(dpy, screen);
  165. int disp_height = DisplayHeight(dpy, screen);
  166. Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
  167. XpmReadFileToPixmap(dpy, w, xpm_image_path, &pix, 0, 0);
  168. XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
  169. }
  170. for(len = 1000; len; len--) {
  171. if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
  172. GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
  173. break;
  174. usleep(1000);
  175. }
  176. if((running = running && (len > 0))) {
  177. for(len = 1000; len; len--) {
  178. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  179. == GrabSuccess)
  180. break;
  181. usleep(1000);
  182. }
  183. running = (len > 0);
  184. }
  185. len = 0;
  186. XSync(dpy, False);
  187. /* main event loop */
  188. while(running && !XNextEvent(dpy, &ev)) {
  189. if (len == 0 && dpms && DPMSCapable(dpy)) {
  190. DPMSEnable(dpy);
  191. DPMSForceLevel(dpy, DPMSModeOff);
  192. }
  193. if(ev.type != KeyPress)
  194. continue;
  195. buf[0] = 0;
  196. num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
  197. if(IsKeypadKey(ksym)) {
  198. if(ksym == XK_KP_Enter)
  199. ksym = XK_Return;
  200. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  201. ksym = (ksym - XK_KP_0) + XK_0;
  202. }
  203. if(IsFunctionKey(ksym) ||
  204. IsKeypadKey(ksym) ||
  205. IsMiscFunctionKey(ksym) ||
  206. IsPFKey(ksym) ||
  207. IsPrivateKeypadKey(ksym))
  208. continue;
  209. switch(ksym) {
  210. case XK_Return:
  211. passwd[len] = 0;
  212. if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
  213. running = false;
  214. else {
  215. fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
  216. if (beep)
  217. XBell(dpy, 100);
  218. }
  219. len = 0;
  220. break;
  221. case XK_Escape:
  222. len = 0;
  223. break;
  224. case XK_BackSpace:
  225. if (len > 0)
  226. len--;
  227. break;
  228. default:
  229. if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
  230. memcpy(passwd + len, buf, num);
  231. len += num;
  232. }
  233. break;
  234. }
  235. }
  236. XUngrabPointer(dpy, CurrentTime);
  237. XFreePixmap(dpy, pmap);
  238. XDestroyWindow(dpy, w);
  239. XCloseDisplay(dpy);
  240. return 0;
  241. }