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.

298 lines
9.9 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. * Puts the given XPM error code to stderr
  57. *
  58. */
  59. void print_xpm_error(int err)
  60. {
  61. switch (err) {
  62. case XpmColorError:
  63. fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
  64. break;
  65. case XpmOpenFailed:
  66. fprintf(stderr, "XPM: Cannot open file\n");
  67. break;
  68. case XpmFileInvalid:
  69. fprintf(stderr, "XPM: invalid XPM file\n");
  70. break;
  71. case XpmNoMemory:
  72. fprintf(stderr, "XPM: Not enough memory\n");
  73. break;
  74. case XpmColorFailed:
  75. fprintf(stderr, "XPM: Color not found\n");
  76. break;
  77. }
  78. }
  79. /*
  80. * Callback function for PAM. We only react on password request callbacks.
  81. *
  82. */
  83. static int conv_callback(int num_msg, const struct pam_message **msg,
  84. struct pam_response **resp, void *appdata_ptr) {
  85. if (num_msg == 0)
  86. return 1;
  87. /* PAM expects an arry of responses, one for each message */
  88. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  89. perror("calloc");
  90. return 1;
  91. }
  92. for (int c = 0; c < num_msg; c++) {
  93. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  94. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  95. continue;
  96. /* return code is currently not used but should be set to zero */
  97. resp[c]->resp_retcode = 0;
  98. if ((resp[c]->resp = strdup(passwd)) == NULL) {
  99. perror("strdup");
  100. return 1;
  101. }
  102. }
  103. return 0;
  104. }
  105. int main(int argc, char *argv[]) {
  106. char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
  107. char buf[32];
  108. char *username;
  109. int num, screen;
  110. unsigned int len;
  111. bool running = true;
  112. /* By default, fork, don’t beep and don’t turn off monitor */
  113. bool dont_fork = false;
  114. bool beep = false;
  115. bool dpms = false;
  116. bool xpm_image = false;
  117. char xpm_image_path[256];
  118. Cursor invisible;
  119. Display *dpy;
  120. KeySym ksym;
  121. Pixmap pmap;
  122. Window root, w;
  123. XColor black, dummy;
  124. XEvent ev;
  125. XSetWindowAttributes wa;
  126. pam_handle_t *handle;
  127. struct pam_conv conv = {conv_callback, NULL};
  128. char opt;
  129. int optind = 0;
  130. static struct option long_options[] = {
  131. {"version", no_argument, NULL, 'v'},
  132. {"nofork", no_argument, NULL, 'n'},
  133. {"beep", no_argument, NULL, 'b'},
  134. {"dpms", no_argument, NULL, 'd'},
  135. {"image", required_argument, NULL, 'i'},
  136. {NULL, no_argument, NULL, 0}
  137. };
  138. while ((opt = getopt_long(argc, argv, "vnbdi:", long_options, &optind)) != -1) {
  139. switch (opt) {
  140. case 'v':
  141. die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
  142. "based on slock, which is © 2006-2008 Anselm R Garbe\n");
  143. case 'n':
  144. dont_fork = true;
  145. break;
  146. case 'b':
  147. beep = true;
  148. break;
  149. case 'd':
  150. dpms = true;
  151. break;
  152. case 'i':
  153. strncpy(xpm_image_path, optarg, 255);
  154. xpm_image = true;
  155. break;
  156. default:
  157. die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm]\n");
  158. }
  159. }
  160. if ((username = getenv("USER")) == NULL)
  161. die("USER environment variable not set, please set it.\n");
  162. int ret = pam_start("i3lock", username, &conv, &handle);
  163. if (ret != PAM_SUCCESS)
  164. die("PAM: %s\n", pam_strerror(handle, ret));
  165. if(!(dpy = XOpenDisplay(0)))
  166. die("i3lock: cannot open display\n");
  167. screen = DefaultScreen(dpy);
  168. root = RootWindow(dpy, screen);
  169. if (!dont_fork) {
  170. if (fork() != 0)
  171. return 0;
  172. }
  173. /* init */
  174. wa.override_redirect = 1;
  175. wa.background_pixel = WhitePixel(dpy, screen);
  176. w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
  177. 0, DefaultDepth(dpy, screen), CopyFromParent,
  178. DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
  179. XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
  180. pmap = XCreateBitmapFromData(dpy, w, curs, 8, 8);
  181. invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
  182. XDefineCursor(dpy, w, invisible);
  183. XMapRaised(dpy, w);
  184. if(xpm_image && file_exists(xpm_image_path))
  185. {
  186. GC gc = XDefaultGC(dpy, 0);
  187. int depth = DefaultDepth(dpy, screen);
  188. int disp_width = DisplayWidth(dpy, screen);
  189. int disp_height = DisplayHeight(dpy, screen);
  190. Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
  191. int err = XpmReadFileToPixmap(dpy, w, xpm_image_path, &pix, 0, 0);
  192. if (err != 0) {
  193. print_xpm_error(err);
  194. return 1;
  195. }
  196. XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
  197. }
  198. for(len = 1000; len; len--) {
  199. if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
  200. GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
  201. break;
  202. usleep(1000);
  203. }
  204. if((running = running && (len > 0))) {
  205. for(len = 1000; len; len--) {
  206. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  207. == GrabSuccess)
  208. break;
  209. usleep(1000);
  210. }
  211. running = (len > 0);
  212. }
  213. len = 0;
  214. XSync(dpy, False);
  215. /* main event loop */
  216. while(running && !XNextEvent(dpy, &ev)) {
  217. if (len == 0 && dpms && DPMSCapable(dpy)) {
  218. DPMSEnable(dpy);
  219. DPMSForceLevel(dpy, DPMSModeOff);
  220. }
  221. if(ev.type != KeyPress)
  222. continue;
  223. buf[0] = 0;
  224. num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
  225. if(IsKeypadKey(ksym)) {
  226. if(ksym == XK_KP_Enter)
  227. ksym = XK_Return;
  228. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  229. ksym = (ksym - XK_KP_0) + XK_0;
  230. }
  231. if(IsFunctionKey(ksym) ||
  232. IsKeypadKey(ksym) ||
  233. IsMiscFunctionKey(ksym) ||
  234. IsPFKey(ksym) ||
  235. IsPrivateKeypadKey(ksym))
  236. continue;
  237. switch(ksym) {
  238. case XK_Return:
  239. passwd[len] = 0;
  240. if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
  241. running = false;
  242. else {
  243. fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
  244. if (beep)
  245. XBell(dpy, 100);
  246. }
  247. len = 0;
  248. break;
  249. case XK_Escape:
  250. len = 0;
  251. break;
  252. case XK_BackSpace:
  253. if (len > 0)
  254. len--;
  255. break;
  256. default:
  257. if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
  258. memcpy(passwd + len, buf, num);
  259. len += num;
  260. }
  261. break;
  262. }
  263. }
  264. XUngrabPointer(dpy, CurrentTime);
  265. XFreePixmap(dpy, pmap);
  266. XDestroyWindow(dpy, w);
  267. XCloseDisplay(dpy);
  268. return 0;
  269. }