#!/usr/bin/perl
# Added by Debian package rules script
push @INC, ("/usr/lib/perl5/misterhouse","/usr/share/perl5/misterhouse");
# -*- Perl -*-

#---------------------------------------------------------------------------
#  File:
#      set_password
#  Description:
#      A perl script that creates and checks for a valid password
#  Author:
#      Bruce Winter    bruce@misterhouse.net
#  Latest version:
#      http://misterhouse.net/mh/bin/set_password
#  Change log:
#    - 01/20/99  Created.
#    - The rest of the change log is at the bottom of this file.
#
#  This free software is licensed under the terms of the GNU public license. 
#  Copyright 1998 Bruce Winter
#
#---------------------------------------------------------------------------

use strict;
my($Pgm_Path, $Pgm_Name, $Version);
BEGIN {
    ($Pgm_Path, $Pgm_Name) = $0 =~ /(.*)[\\\/](.+)\.?/;
    ($Pgm_Name) = $0 =~ /([^.]+)/, $Pgm_Path = '.' unless $Pgm_Name;
}

($Version) = q$Revision: 442 $ =~ /: (\S+)/; # Note: revision number is auto-updated by cvs

#print "Command: $Pgm_Name @ARGV\n";
#print "Version: $Version\n";

use Getopt::Long;
my %parms;
if (!&GetOptions(\%parms, 'h', 'help', 'pw_file=s', 'password=s', 'user=s', 'check') or @ARGV or 
    ($parms{h} or $parms{help})) {
    print<<eof;

$Pgm_Name creates and/or querys a password file, using the crypt function
If -user is not specified, it defaults to family.

Usage:

  $Pgm_Name [options] 

    -h         => This help text
    -help      => This help text

    -check     => Will check, not set, the password.

    -password xyz => xyz is the password to check.  If not used, a TK popup window will prompt for it.

    -user xyz     => xyz is the user to set the password for.  

    -pw_file xyz  => xyz is the file that the crypted password is read/written to.
                     Default is mh.ini data_dir/.password

Examples:
    $Pgm_Name 
    $Pgm_Name -user admin
    $Pgm_Name -user family -password xyz
    $Pgm_Name -check 
    $Pgm_Name -check -user admin 

eof
    exit;
  }

				# Declare global vars
my ($use_Tk, $top, $loop, $f1, $f2, $f3);

my $return_flag = (caller) ? 1 : 0;

&setup;

if ($use_Tk) {
    (-e $parms{pw_file} and $parms{check}) ? &pw_check : &pw_create;
    &MainLoop if $loop;
}
else {
    (-e $parms{pw_file} and $parms{check}) ? &pw_check2($parms{password}) : &pw_create2($parms{password},$parms{password});
}

my (%config_parms, %passwords);
sub setup {
    eval "use lib '$Pgm_Path/../lib', '$Pgm_Path/../lib/site'"; # So perl2exe works
    require 'misterhouse/handy_utilities.pl';       # For read_mh_opts funcion
    &main::read_mh_opts(\%config_parms, $Pgm_Path);

    $parms{pw_file} = $config_parms{pw_file}                 unless $parms{pw_file};
    $parms{pw_file} = $config_parms{password_file}           unless $parms{pw_file};
    $parms{pw_file} = $config_parms{data_dir} . "/.password" unless $parms{pw_file};

    $use_Tk++ unless $parms{password};

    if ($use_Tk) {
        eval "use Tk";
        if ($@) {
            &pw_exit("\nYou either need the perl Tk package installed, or you need to use\n" .
                     "the -password option.  For more help, type $Pgm_Name -h\n\n");
        }
        else {

            if ($main::MW) { 
                $top = $main::MW->Toplevel; 
                $loop = 0;
            } 
            else { 
                $top = MainWindow->new;  
                $loop = 1;
            }

            $f1 = $top->Frame->pack;
            $f2 = $top->Frame->pack;
            $f3 = $top->Frame->pack;
        }
    }

    if (-e $parms{pw_file}){                     # Check to see if
        open (IN, $parms{pw_file});
        while (<IN>) {
            chomp;
            if (/User: (\S+) (.+)/) {
                $passwords{$1} = $2;
            }
            else {
                $passwords{family} = $_;       # Grandfather old
            }
        }
    }

}

sub pw_exit {
  my ($results) = @_;
  if ($return_flag) {
    print "Returning to caller: $results\n" if $results;
    $top->destroy if $use_Tk;
    return $results;
  }
  else {
    print $results, "\n";
    exit;
  }
}

sub pw_check {
    my $pw;
    $f1->Label(-text => "  Enter your password:", -font => 'fixed', -takefocus => 1) -> pack(-side => 'left');
    my $entry = $f1->Entry(-textvariable => \$pw, -width => 10, -show => '*') -> pack(-side => 'left');
    $entry->bind('<Return>', sub {&pw_check2($pw)}); 
#   $f2->Button(-text => "Continue", -command => sub{&pw_check2($pw)}) -> pack(-side => 'left'); 
    $f3->Label(-text => "To change your password, delete this file: $parms{pw_file}") -> pack(-side => 'left');
}

sub pw_create {
    my ($pw1, $pw2);
    $f1->Label(-text => "  Enter your password:", -font => 'fixed') -> pack(-side => 'left');
    my $entry1 = $f1->Entry(-textvariable => \$pw1, -width => 10, -show => '*') -> pack(-side => 'left');
    
    $f2->Label(-text => "ReEnter your password:", -font => 'fixed') -> pack(-side => 'left');
    my $entry2 = $f2->Entry(-textvariable => \$pw2, -width => 10, -show => '*') -> pack(-side => 'left');

    $entry1->bind('<Return>', sub {&pw_create2($pw1, $pw2)}); 
    $entry2->bind('<Return>', sub {&pw_create2($pw1, $pw2)}); 
  
#   $f3->Button(-text => "Continue", -command => sub{&pw_create2($pw1, $pw2)}) -> pack(-side => 'left'); 

}

sub pw_check2 {
    my ($pw) = @_;

#   my $pop1 = MainWindow->new()

    my $user_pw;
    for my $user (sort keys %passwords) {
        my $pwc = $passwords{$user};
        if (crypt($pw, $pwc) eq $pwc) {
            $user_pw = $user if !$parms{user} or $parms{user} eq $user;
        }
    }
    pw_exit(($user_pw) ? "Password match $user_pw" : "Password is not valid");
}

sub pw_create2 {
    my ($pw1, $pw2) = @_;
    if ($pw1 eq $pw2) {
        my $user = $parms{user};
        $user = 'family' unless $user;
        my $pwc = crypt($pw1, 'mh');
        $passwords{$user} = $pwc;
        open (OUT, ">$parms{pw_file}") or pw_exit "Error, could not open password file $parms{pw_file}: $!";
        for $user (sort keys %passwords) {
            print OUT "User: $user $passwords{$user}\n";
        }
        close OUT;
        #pw_exit 'Password has been saved';
                                 # Enable this as windows tk works!
        if ($use_Tk) {
            my $pop1 = MainWindow->new();
            my $label = $pop1->Label(-text => "Password has been saved") -> pack;
            my $button = $pop1->Button(-text => "OK", -command => sub{$pop1->destroy;pw_exit}) -> pack;
        }
    }
    else {
        if ($use_Tk) {
            my $pop1 = MainWindow->new();
            my $label = $pop1->Label(-text => "Password entries do not match. Please try again.") -> pack;
            $pop1->Button(-text => "OK", -command => sub{$pop1->destroy}) -> pack;
        }
    }
}

# $Log: set_password,v $
# Revision 1.14  2003/07/06 17:55:10  winter
#  - 2.82 release
#
# Revision 1.13  2003/04/20 21:43:59  winter
#  - 2.80 release
#
# Revision 1.12  2003/02/08 05:29:08  winter
#  - 2.78 release
#
# Revision 1.11  2000/02/15 13:16:28  winter
# - fix the 'global symbol' errors when run without mh
#
# Revision 1.10  2000/01/27 13:31:30  winter
# - update version number
#
# Revision 1.9  1999/10/02 22:40:05  winter
# - fix use lib eval
#
# Revision 1.8  1999/09/27 03:15:56  winter
# - read mh.ini parms for default pw_file location
#
# Revision 1.7  1999/09/12 16:56:02  winter
# *** empty log message ***
#
# Revision 1.6  1999/03/21 17:39:19  winter
# - email change
#
# Revision 1.5  1999/02/08 00:35:56  winter
# - use pw_exit, not exit, on 'no tk' messages
#
# Revision 1.4  1999/02/04 14:36:13  winter
# - use fewer popups
#
# Revision 1.3  1999/02/01 00:09:52  winter
# - take out non-eval use tk
#
# Revision 1.2  1999/01/30 20:02:12  winter
# - add close IN
#
# Revision 1.1  1999/01/24 20:16:21  winter
# - created.
#
