Advertisement
Guest User

xtrlock custom password [xtrlock.c src]

a guest
Nov 27th, 2015
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.44 KB | None | 0 0
  1. /*
  2.  * xtrlock.c
  3.  *
  4.  * X Transparent Lock
  5.  *
  6.  * Copyright (C)1993,1994 Ian Jackson
  7.  *
  8.  * This is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2, or (at your option)
  11.  * any later version.
  12.  *
  13.  * This is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */
  18.  
  19. #include <X11/X.h>
  20. #include <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/keysym.h>
  23. #include <X11/Xos.h>
  24.  
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <errno.h>
  30. #include <pwd.h>
  31. #include <grp.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. #include <crypt.h>
  35. #include <unistd.h>
  36. #include <math.h>
  37. #include <ctype.h>
  38. #include <values.h>
  39.  
  40. /* required for custom passwd prompt --tetris11 */
  41. #include <unistd.h>  
  42.  
  43. #ifdef SHADOW_PWD
  44. #include <shadow.h>
  45. #endif
  46.  
  47. #include "lock.bitmap"
  48. #include "mask.bitmap"
  49. #include "patchlevel.h"
  50.  
  51. Display *display;
  52. Window window, root;
  53.  
  54. #define TIMEOUTPERATTEMPT 30000
  55. #define MAXGOODWILL  (TIMEOUTPERATTEMPT*5)
  56. #define INITIALGOODWILL MAXGOODWILL
  57. #define GOODWILLPORTION 0.3
  58.  
  59. struct passwd *pw;
  60.  
  61. int c_pass = 0; /* custom_password flag */
  62.  
  63. int passwordok(const char *s) {
  64.  
  65.   if ( c_pass == 1 ) {
  66.       return strcmp(s+1, pw->pw_passwd); /* skip first character to work --tetris11 */
  67.   }
  68.  
  69. #if 0
  70.   char key[3];
  71.   char *encr;
  72.  
  73.   key[0] = *(pw->pw_passwd);
  74.   key[1] =  (pw->pw_passwd)[1];
  75.   key[2] =  0;
  76.   encr = crypt(s, key);
  77.   return !strcmp(encr, pw->pw_passwd);
  78. #else
  79.   /* simpler, and should work with crypt() algorithms using longer
  80.      salt strings (like the md5-based one on freebsd).  --marekm */
  81.   return !strcmp(crypt(s, pw->pw_passwd), pw->pw_passwd);
  82. #endif
  83. }
  84.  
  85. int main(int argc, char **argv){
  86.   XEvent ev;
  87.   KeySym ks;
  88.   char cbuf[10], rbuf[128]; /* shadow appears to suggest 127 a good value here */
  89.   int clen, rlen=0;
  90.   long goodwill= INITIALGOODWILL, timeout= 0;
  91.   XSetWindowAttributes attrib;
  92.   Cursor cursor;
  93.   Pixmap csr_source,csr_mask;
  94.   XColor csr_fg, csr_bg, dummy, black;
  95.   int ret, screen, blank = 0;
  96.   char *custom_passwd = 0;
  97.  
  98. #define USAGE_EXIT \
  99.  fprintf(stderr,"xtrlock (version %s); usage: xtrlock [-b] [-p]\n", program_version); \
  100.  exit(1);
  101.  
  102. #define CUSTOM_PROMPT "custom passphrase: "
  103.  
  104. #ifdef SHADOW_PWD
  105.   struct spwd *sp;
  106. #endif
  107.   struct timeval tv;
  108.   int tvt, gs;
  109.  
  110.   if (argc == 2){
  111.     if (strcmp(argv[1], "-b") == 0 ) {
  112.       blank = 1;
  113.     }
  114.     else if (strcmp(argv[1],"-p") == 0 ) {
  115.       c_pass = 1;
  116.       custom_passwd = getpass(CUSTOM_PROMPT);
  117.     }
  118.     else {USAGE_EXIT}
  119.   }
  120.   else if (argc == 3) {
  121.     if (\
  122.      (strcmp(argv[1], "-b") == 0 ) && (strcmp(argv[2], "-p") == 0) || \
  123.      (strcmp(argv[1], "-p") == 0 ) && (strcmp(argv[2], "-b") == 0)\
  124.     ) {
  125.       blank = c_pass = 1;
  126.       custom_passwd = getpass(CUSTOM_PROMPT);
  127.     }
  128.     else {USAGE_EXIT}
  129.   }
  130.   else if (argc > 1) {USAGE_EXIT}
  131.  
  132.  
  133.  
  134.   errno=0;  pw= getpwuid(getuid());
  135.  
  136.   if (c_pass == 1) {
  137.   /* custom password bypasses gid uid checks, but still requires
  138.      a valid pw struct --tetris11 */
  139.     pw->pw_passwd = custom_passwd;
  140.   }
  141.   else {
  142.     if (!pw) { perror("password entry for uid not found"); exit(1); }
  143. #ifdef SHADOW_PWD
  144.       sp = getspnam(pw->pw_name);
  145.       if (sp)
  146.         pw->pw_passwd = sp->sp_pwdp;
  147.       endspent();
  148. #endif
  149.  
  150.       /* logically, if we need to do the following then the same
  151.          applies to being installed setgid shadow.  
  152.          we do this first, because of a bug in linux. --jdamery */
  153.       if (setgid(getgid())) { perror("setgid"); exit(1); }
  154.       /* we can be installed setuid root to support shadow passwords,
  155.          and we don't need root privileges any longer.  --marekm */
  156.       if (setuid(getuid())) { perror("setuid"); exit(1); }
  157.  
  158.       if (strlen(pw->pw_passwd) < 13) {
  159.         fputs("password entry has no pwd\n",stderr); exit(1);
  160.       }
  161.   }
  162.  
  163.   display= XOpenDisplay(0);
  164.  
  165.   if (display==NULL) {
  166.     fprintf(stderr,"xtrlock (version %s): cannot open display\n",
  167.         program_version);
  168.     exit(1);
  169.   }
  170.  
  171.   attrib.override_redirect= True;
  172.  
  173.   if (blank) {
  174.     screen = DefaultScreen(display);
  175.     attrib.background_pixel = BlackPixel(display, screen);
  176.     window= XCreateWindow(display,DefaultRootWindow(display),
  177.                           0,0,DisplayWidth(display, screen),DisplayHeight(display, screen),
  178.                           0,DefaultDepth(display, screen), CopyFromParent, DefaultVisual(display, screen),
  179.                           CWOverrideRedirect|CWBackPixel,&attrib);
  180.     XAllocNamedColor(display, DefaultColormap(display, screen), "black", &black, &dummy);
  181.   } else {
  182.     window= XCreateWindow(display,DefaultRootWindow(display),
  183.                           0,0,1,1,0,CopyFromParent,InputOnly,CopyFromParent,
  184.                           CWOverrideRedirect,&attrib);
  185.   }
  186.                        
  187.   XSelectInput(display,window,KeyPressMask|KeyReleaseMask);
  188.  
  189.   csr_source= XCreateBitmapFromData(display,window,lock_bits,lock_width,lock_height);
  190.   csr_mask= XCreateBitmapFromData(display,window,mask_bits,mask_width,mask_height);
  191.  
  192.   ret = XAllocNamedColor(display,
  193.                         DefaultColormap(display, DefaultScreen(display)),
  194.                         "steelblue3",
  195.                         &dummy, &csr_bg);
  196.   if (ret==0)
  197.     XAllocNamedColor(display,
  198.                     DefaultColormap(display, DefaultScreen(display)),
  199.                     "black",
  200.                     &dummy, &csr_bg);
  201.  
  202.   ret = XAllocNamedColor(display,
  203.                         DefaultColormap(display,DefaultScreen(display)),
  204.                         "grey25",
  205.                         &dummy, &csr_fg);
  206.   if (ret==0)
  207.     XAllocNamedColor(display,
  208.                     DefaultColormap(display, DefaultScreen(display)),
  209.                     "white",
  210.                     &dummy, &csr_bg);
  211.  
  212.  
  213.  
  214.   cursor= XCreatePixmapCursor(display,csr_source,csr_mask,&csr_fg,&csr_bg,
  215.                               lock_x_hot,lock_y_hot);
  216.  
  217.   XMapWindow(display,window);
  218.  
  219.   /*Sometimes the WM doesn't ungrab the keyboard quickly enough if
  220.    *launching xtrlock from a keystroke shortcut, meaning xtrlock fails
  221.    *to start We deal with this by waiting (up to 100 times) for 10,000
  222.    *microsecs and trying to grab each time. If we still fail
  223.    *(i.e. after 1s in total), then give up, and emit an error
  224.    */
  225.  
  226.   gs=0; /*gs==grab successful*/
  227.   for (tvt=0 ; tvt<100; tvt++) {
  228.     ret = XGrabKeyboard(display,window,False,GrabModeAsync,GrabModeAsync,
  229.             CurrentTime);
  230.     if (ret == GrabSuccess) {
  231.       gs=1;
  232.       break;
  233.     }
  234.     /*grab failed; wait .01s*/
  235.     tv.tv_sec=0;
  236.     tv.tv_usec=10000;
  237.     select(1,NULL,NULL,NULL,&tv);
  238.   }
  239.   if (gs==0){
  240.     fprintf(stderr,"xtrlock (version %s): cannot grab keyboard\n",
  241.         program_version);
  242.     exit(1);
  243.   }
  244.  
  245.   if (XGrabPointer(display,window,False,(KeyPressMask|KeyReleaseMask)&0,
  246.                GrabModeAsync,GrabModeAsync,None,
  247.                cursor,CurrentTime)!=GrabSuccess) {
  248.     XUngrabKeyboard(display,CurrentTime);
  249.     fprintf(stderr,"xtrlock (version %s): cannot grab pointer\n",
  250.         program_version);
  251.     exit(1);
  252.   }
  253.  
  254.   for (;;) {
  255.     XNextEvent(display,&ev);
  256.     switch (ev.type) {
  257.     case KeyPress:
  258.       if (ev.xkey.time < timeout) { XBell(display,0); break; }
  259.       clen= XLookupString(&ev.xkey,cbuf,9,&ks,0);
  260.       switch (ks) {
  261.       case XK_Escape: case XK_Clear:
  262.         rlen=0; break;
  263.       case XK_Delete: case XK_BackSpace:
  264.         if (rlen>0) rlen--;
  265.         break;
  266.       case XK_Linefeed: case XK_Return:
  267.         if (rlen==0) break;
  268.         rbuf[rlen]=0;
  269.         if (passwordok(rbuf)) goto loop_x;
  270.         XBell(display,0);
  271.         rlen= 0;
  272.         if (timeout) {
  273.           goodwill+= ev.xkey.time - timeout;
  274.           if (goodwill > MAXGOODWILL) {
  275.             goodwill= MAXGOODWILL;
  276.           }
  277.         }
  278.         timeout= -goodwill*GOODWILLPORTION;
  279.         goodwill+= timeout;
  280.         timeout+= ev.xkey.time + TIMEOUTPERATTEMPT;
  281.         break;
  282.       default:
  283.         if (clen != 1) break;
  284.         /* allow space for the trailing \0 */
  285.     if (rlen < (sizeof(rbuf) - 1)){
  286.       rbuf[rlen]=cbuf[0];
  287.       rlen++;
  288.     }
  289.         break;
  290.       }
  291.       break;
  292.     default:
  293.       break;
  294.     }
  295.   }
  296.  loop_x:
  297.   exit(0);
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement