#!/usr/bin/perl
#
#      Author       : Gerald (Jerry) Carter
#      E-mail       : jerry@eng.auburn.edu
#      Filename     : nt2passwd
#      Date Created : August 13, 1998
#      Last Update  :
#
#      Usage : 
#
#      Simple perl script to accept the input from the 
#      'net user /domain > users.txt' on an NT domain member
#      and place the users in an /etc/passwd formatted file
#      in the current directory
#
#      The program will prompt for a starting uid and a gid.
#      It will not allow starting at uid 0 nor will it allow 
#      assigning gid 0 to generated entries.
#
#      The generated file may be catenated to /etc/passwd at
#      your descretion.  I make no claims about the script.
#      **Use it at your own risk**  No warrenty expressed or 
#      implied.  
#
#########################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#########################################################################

$no_input = 1;
while ( 1 ) {
   $currentarg = shift;
   last if (!defined($currentarg));

   if (defined($currentarg)) {
      open ( USER_LIST, "< $currentarg") || die $!;
      $infile = INPUT;
      $no_input = 0;
      $currentarg = shift;
      if (defined($currentarg)) {
         &print_usage();
         exit -1;
      }
   }
   
}

if ( $no_input ) {
   &print_usage();
   exit -1;
}

# get the starting uid
print "Enter the uid to start with : ";
$start_uid = <STDIN>;
$start_uid = int ( $start_uid );

if ( $start_uid eq 0 ) {
   printf STDERR "Cannot start with uid 0!\nProgram exiting...\n";
   exit (-1);
}

$current_uid = $start_uid;

print "Enter the gid to use : ";
$gid = <STDIN>;
$gid = int ( $gid );
if ( $gid eq 0 ) {
   printf STDERR "Cannot use gid 0 as the group ID!\n";
   printf STDERR "Program exiting...\n";
   exit (-1);
}

print "\nDo you want to create a home directory for the users? (y/n) ";
$make_home = <STDIN>;
$make_home = substr ($make_home, 0, 1);
$make_home =~ s/./\l$&/g;
if ( "$make_home" eq "y" ) {
   print "Please enter the base directory for the users home : ";
   $homebase = <STDIN>;
   chop ( $homebase );
   print "Do you want me to make the home directories for you? (y/n) ";
   $create = <STDIN>;
   $create = substr ($create, 0, 1);
}

$comment = 'NT Dummy account';
$shell = '/bin/False';

# open the output file
open ( PASSWD, "> passwd.new" ) || die $!;
open ( MAPFILE, "> username.map" ) || die $!;

# loop through the input file 
while ( $string = <USER_LIST> ) {

   chop ( $string );

   # weed out the command output and keep the list of users
   $string = &checkInput ( $string );

   # break up the input
   if ( "$string" ne "" ) {
      $tmp_list = "";
      while ( "$string" ne "" ) {
         $user1 = substr ($string, 0, 25);
         $user1 =~ s/\s+$//;
         $string = substr ($string, 25, length($string)-25);
         $tmp_list .= $user1 . ":";
      }
      @users = split (/:/, $tmp_list);

      foreach $user ( @users ) {
         ($name) = getpwuid ( $current_uid );
         while ( "$name" ne "" ) {
            $current_uid++;
            ($name) = getpwuid ( $current_uid );
         }
         $user =~ s/./\l$&/g;
         if ( length($user) > 8 ) {
            $got_user = 0;
            $new_user = "";
            while ( $got_user == 0 ) {
               print "Please enter a username for [$user] of 8 characters of less: ";
               $new_user = <STDIN>;
               chop ($new_user);
               ($new_username) = getpwnam ($new_user);
               if ("$new_username" ne "") {
                  print "[$new_username] exists.  Do you want to enter another name? (y/n) ";
                  $username_result = <STDIN>;
                  $username_result = substr ($username_result, 0, 1);
                  $username_result =~ s/./\l$&/g;
                  if ( "$username_result" eq "n" ) {
                     $got_user = 1;
                  }
               }
               else {
                  $got_user = 1;
               }
            }
            if ( ($got_user == 1) && ("$new_user" ne "") ) {
               printf MAPFILE "$new_user=$user\n";
               $user = $new_user;                    
            }
         }
         if ( (length($user) <= 8) && (length($user) != 0) ) {
            ( $username ) = getpwnam ( $user );
            if ( "$username" eq "" ) {
               if ( "$make_home" eq "y" ) {
                  $homedir = $homebase . '/' . $user;
                  if ( "$create" eq "y" ) {
                      mkdir($homedir, 0755);
                      if ( $! ) {
                         printf STDERR "Error creating $homedir!\n";
                      }
                      chown ( $current_uid, $gid, $homedir );
                  }
               }
               else {
                  $homedir = '/dev/null';
               }
               printf PASSWD "$user:*:$current_uid:$gid:$comment:$homedir:$shell\n";
            }
            $current_uid++;
         }
      }
   }
}

printf STDERR "The new accounts have been created in passswd.new.  They\n";
printf STDERR "have not been automatically added to /etc/passwd\n\n";
printf STDERR "To create the accounts, you must execute\n\n";
printf STDERR "    cat passwd.new >> /etc/passwd\n\n";

# close the files 
close ( USER_LIST );
close ( PASSWD );
close ( MAPFILE );

# successful completion
exit (0);
#################################################################

sub checkInput {
   local ( $input ) = @_;

   if ( $input =~ '\\\\' ) {
      $input = '';
   }
   elsif ( $input =~ "command completed" ) {
      $input = '';
   }
   elsif ( $input =~ '---------' ) {
      $input = '';
   }
   elsif ( $input =~ 'The request will be processed at a domain controller' ) {
      $input = '';
   }


   $input;
}

#####################################################################
sub print_usage {

   printf STDERR "Usage: nt2passwd \"inputfile\"\n";
   printf STDERR "   inputfile - output from net user /domain\n";

}

