#!/usr/bin/perl -w

# creates list of passwords and times and stores them in each user's own file
#
# this means that they can have their own file that they can print out
# without knowing other people's passwords.

$pth = "/etc/httpd/daccess/";


# open dgroup to get a list of the users
open GRP, "<".$pth."dgroup";
  @group = <GRP>;
close GRP;

# last line is just a carriage return so ignore that
foreach $x (0..$#group) {
  my $a = $group[$x];
  @u = split /\s+/,$a;
  #first segment of the line is the group name so discard that
  foreach $y (1..$#u) {
    my $a = $u[$y];
    $notfound = 1;
    foreach $z (@users) {
      if ($z eq $a) {
        $notfound = 0;
        last;
      };
    };
    if ($notfound == 1) {
      push (@users, $a);
    };
  };
};
# We now have a list in @users of all of the users that we want.

# for each user, generate a list of passwords (a-z,0-9) that are 8 chars long
# and save them as a file.

# This could be for any period, defined in any way (day of week,
# hour of day, hour of week and so on).

# Hour of day = 1
# Hour of week or whatever you like = 2
$mode = 1;

# full uc/lc alphabet + numbers
# suitable for a list on a machine such that the pwd is cut and pasted.
# @chars = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
#             a b c d e f g i j k l m n o p q r s t u v w x y z
#             0 1 2 3 4 5 6 7 8 9 );

# full lc alphabet + numbers
# use as above
# @chars = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9 );

# reduced lc alphabet/numbers so that ambiguities such as Il1 and gq are eliminated.
# suitable for a printed out list where ambiguities are a pain in the you-know-where.
@chars = qw(a b c d e f h j k m n p r s t u v w x y z 2 3 4 5 6 7 8 9 );

# hexadecimal
# smaller character set and easier to use (and break)
# @chars = qw(0 1 2 3 4 5 6 7 8 9 a b c d e f);

# numerical
# even smaller character set and very easy to use (and break)
# @chars = qw(0 1 2 3 4 5 6 7 8 9);

if ($mode == 1) {
  #Hour of day
  foreach $x (@users) {
    open(UDP, ">".$pth."dp$x");
      foreach $c (0..23) {
        # build random string
        $pw = "";
        foreach (1..8) {
          $pw .= $chars [rand @chars];
        };
        print UDP "$c $pw\n";
      };
    close(UDP);
  };
} else {
  # $mode == 2 goes here. Make up whatever you like
};
