home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume3 / xsaver / part02 / password.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-10  |  4.6 KB  |  180 lines

  1. /*
  2.  * $Source: /mit/sipbsrc/uus/src/xscreensaver/RCS/password.c,v $
  3.  * $Author: jik $
  4.  *
  5.  * This file is part of xscreensaver.  It contains the code for
  6.  * password manipulation.
  7.  *
  8.  * Author: Jonathan Kamens, MIT Project Athena and
  9.  *                          MIT Student Information Processing Board
  10.  *
  11.  * Copyright (c) 1989 by Jonathan Kamens.  This code may be
  12.  * distributed freely as long as this notice is kept intact in its
  13.  * entirety and every effort is made to send all corrections and
  14.  * improvements to the code back to the author.  Also, don't try to
  15.  * make any money off of it or pretend that you wrote it.
  16.  */
  17.  
  18. #ifndef lint
  19.      static char rcsid_password_c[] = "$Header: password.c,v 1.6 89/02/28 06:55:12 jik Exp $";
  20. #endif
  21.  
  22. #include <X11/Intrinsic.h>
  23. #include <X11/StringDefs.h>
  24. #include <X11/Form.h>
  25. #include <X11/Label.h>
  26. #include <X11/Shell.h>
  27. #include <stdio.h>
  28. #include <pwd.h>
  29. #include "xsaver.h"
  30. #include "globals.h"
  31.  
  32.  
  33. char *get_passwd();
  34. Widget PasswordWindow();
  35.  
  36. extern char *time_string(), *timeleft_string(), *timeout_string(),
  37.      *user_string(), *get_user(), *malloc(), *crypt();
  38. extern Widget PromptBox();
  39. extern long random();
  40.  
  41.  
  42.  
  43.  
  44. Widget PasswordWindow(which)
  45. int which;
  46. {
  47.      PromptLine prompt_lines[MAXPROMPT];
  48.      int i = 0;
  49.      Widget prompt;
  50.      
  51.      prompt_lines[i] = default_line;
  52.      prompt_lines[i].str = user_string(USER_FORMAT); i++;
  53.      prompt_lines[i] = default_line;
  54.      prompt_lines[i].name = "time";
  55.      prompt_lines[i].str = time_string(TIME_FORMAT, Force); i++;
  56.      if (defs.timeout && lock_flag) {
  57.       prompt_lines[i] = default_line;
  58.       if (which < 3) {
  59.            prompt_lines[i].str = timeout_string(TIMEOUT_FORMAT, Force);
  60.            i++;
  61.       }
  62.       else {
  63.            prompt_lines[i].str = timeleft_string(TIMELEFT_FORMAT, Force);
  64.            i++;
  65.       }
  66.      }
  67.      prompt_lines[i] = default_line;
  68.      prompt_lines[i].str = "";
  69.      prompt_lines[i].center = False; i++;
  70.      prompt_lines[i] = default_line;
  71.      if (which != 2)
  72.       prompt_lines[i].str = PASS_PROMPT1;
  73.      else
  74.       prompt_lines[i].str = PASS_PROMPT2;
  75.      prompt_lines[i].center = False; i++;
  76.      prompt_lines[i].spread = 10;
  77.  
  78.      prompt = PromptBox(root_widget, "prompt", prompt_lines, i,
  79.             (Widget *) NULL);
  80.  
  81.      XtMapWidget(prompt);
  82.  
  83.      return(prompt);
  84. }
  85.  
  86.  
  87.  
  88.  
  89.      
  90.      
  91. char *get_passwd()
  92. {
  93.      struct passwd *pwent;
  94.      char *user;
  95.      char *passwd;
  96.      
  97.      user = get_user();
  98.  
  99.      if (! *user)
  100.       return((char *) NULL);
  101.      
  102.      pwent = getpwnam(user);
  103.  
  104.      if (pwent) if (strlen(pwent->pw_passwd) == PASSWDLENGTH)
  105.       return(pwent->pw_passwd);
  106.      
  107.      passwd = malloc(1);
  108.      *passwd = '\0';
  109.      return(passwd);
  110. }
  111.  
  112.  
  113. Boolean correct_password(passwd)
  114. char *passwd;
  115. {
  116.      char seed[3];
  117.  
  118.      if (*defs.ekey) {
  119.       strncpy(seed, defs.ekey, 2);
  120.       return(! strcmp(crypt(passwd, seed), defs.ekey));
  121.      }
  122.      else if (*defs.key) {
  123.       if (! strcmp(passwd, defs.key))
  124.            return(True);
  125.       else
  126.            return(False);
  127.      }
  128.      else
  129.       return(False);
  130. }
  131.  
  132.  
  133. /*
  134.  * Takes the password from defs.key, encrypts it, places its value
  135.  * into defs.ekey, and then zeros out defs.key.  This is basically for
  136.  * security reasons -- if someone gcore's the file or manages to read
  137.  * memory and the key is stored in plaintext, they can read it and do
  138.  * nasty things.
  139.  *
  140.  * There's no sanity checking here -- if there's nothing in defs.key,
  141.  * things will lose.
  142.  * 
  143.  * This is relatively useless when the unencrypted key is given on the
  144.  * command line or put in the user's resources, since the toolkit
  145.  * keeps several copies of the key floating around.  Even if we bzero
  146.  * the copy we have, there are still several others in memory.  It
  147.  * becomes useful when the key is typed in the first time the screen
  148.  * is locked, and since we're assuming that that's what will be
  149.  * happening most of the time, this is a useful procedure.
  150.  */
  151. install_password()
  152. {
  153.      char seed[3];
  154.      char error_buf[BUFSIZ];
  155.      
  156.      /* Calculate the seed for the password */
  157.      *seed = *(seed + 1) = *(seed + 2) = 0;
  158.      do {
  159.       *seed = (char) (random() % 0172);
  160.      } while (! (((*seed >= 056) && (*seed <= 071)) || /* /, ., 0-9 */
  161.          ((*seed >= 0101) && (*seed <= 0132)) || /* A-Z */
  162.          ((*seed >= 0141) && (*seed <= 0172)))); /* a-z */
  163.      do {
  164.       seed[1] = (char) (random() % 0172);
  165.      } while (! (((seed[1] >= 056) && (seed[1] <= 071)) || /* /, ., 0-9 */
  166.          ((seed[1] >= 0101) && (seed[1] <= 0132)) || /* A-Z */
  167.          ((seed[1] >= 0141) && (seed[1] <= 0172)))); /* a-z */
  168.  
  169.      defs.ekey = malloc(PASSWDLENGTH + 1);
  170.      if (! defs.ekey) {
  171.       perror(sprintf(error_buf, "%s: install_password", whoami));
  172.       exit(1);
  173.      }
  174.      strcpy(defs.ekey, crypt(defs.key, seed));
  175.  
  176.      bzero(defs.key, strlen(defs.key));
  177. }
  178.  
  179.       
  180.