#!/usr/bin/perl -T ########################################################################### ## Created by Pararox ## Rolled on 3/15/03 ## Perl 5.8.0, Slackware Linux ## ## downUnder.pl ## VERSION 1.03.00 ########################################################################### $ENV{PATH}=''; use strict; use Email::Valid; use Term::ReadKey; use Mail::Send; # use constant LOG_FILE => "/home/newuser/downUnder.log"; my ($i, # general iterative value $valid, # basic true/false for input validity checking $make_account, # same as comply really, will merge $comply, # hold answer to yes/no questions $username, # user request for username $full_name, # user's full name $first_name, # user's first name $email, # user's email address $uid, # user's uid (next in '100' group on this system) $max, # for use finding max currently held UID $passwd, # user's request for a password $passwd2, # for password consistency checking $date); # precise time at which user added my $gid=100; # change to suit your system's needs my $shell='/bin/bash'; # user's shell, this is all I have on my box system ("/usr/bin/clear"); print "\nWelcome to the official server of the IIT Linux User's Group\n\n"; ########################################################################### ## This could probably be removed. It's just making sure the user ## is ready to proceede with account creation. ########################################################################### $i=0; do { print "Not an option!\n" if $i>0; print "Are you going to make a shell account now? (y/n): "; chomp($make_account = ); ++$i; die "\nCiao!\n" if ($make_account eq 'n'); } while ($make_account !~ /^\s*y/i); # while input isn't 'Y|y' system ("/usr/bin/clear"); print <0; print "\nI fully understand/comply and respect the system (y/n): "; chomp($comply=); ++$i; die "\nCiao!\n" if (($comply eq 'n') || ($comply eq 'N')); } while ($comply !~ /\s*y/i); # while input isn't 'Y|y' ########################################################################### ## Start of the *main* do loop; this loop goes for quite a while ## and only drops into the final stages of the program once the user ## has reviewed inputs and agreed to creating the account with the ## given information. ########################################################################### do { system ("/usr/bin/clear"); print "\nOf the following, NOTHING will be public but your username\n\n"; ################################################################### ## Getting user's fullname, for system records ################################################################### $i=0; do { print "\nYour full name is required!\n" if $i>0; print "(*) Your actual full name: "; chomp($full_name = ); ++$i; } while ($full_name !~ /^\s*(\D+)\s+\D+\b/); # while not 2 distinct # non-space boundaries $first_name = $1; # for personalized output :) ################################################################### ## Getting user's current email address for system records only. ## Using the Email::Valid mod to make sure the format is valid and ## also that the domain name is valid (yes it runs a DNS query) ################################################################### $i=0; $valid=1; # setting to *invalid* status to begin with do { print "\nYour *valid* email address is required!\n" if $i>0; print "(*) Your email address: "; chomp($email = ); ++$i; # -address (checks for validity), -mxcheck (DNS check) Email::Valid->address( -address=>$email, -mxcheck=>1) ? ($valid=0) : ($valid=1); } while ($valid); # while Email::Valid comes up as invalid ################################################################## ## Asking for and getting user's requested username for the ## system. Opening the passwd file in order to ensure user ## name requested isn't already taken. ################################################################## $i=0; print "\nThe following items are CASE SENSITIVE!\n"; do { if ($i>0) {print "\nYour username is required!\n";} elsif ($valid==1) {print "\nThat username is taken!\n";} print "(*) Your desired username: "; chomp($username = ); ++$i; $valid=0; open PASSWD, ") { /#*(\S+):x/; # getting usernames from file if ($username eq $1) {$valid=1; $i=0}; #invalid if match } close PASSWD; } while (($username !~ /^\s*\S+\s*$/) || ($valid==1)); #while blank ##################################################################### ## Getting the user's desired password, which thru a regex must ## be an alphanumeric between 6 and 100 characters in length. ## Using Term::ReadKey to switch stty -echo (which doesn't seem ## to work itself (stty that is), maybe because of do-while? ##################################################################### $i=0; do { print "\n\nCommon! A *real* password, now (6 or more alphanumerics)!\n" if ($i>0); print "\n\nPasswords don't match!\n" if ($valid != 0); ++$i; $valid=0; ReadMode 2; # Term:ReadKey (cooked mode,echo off) print "(*) Your account password (won't echo): "; chomp($passwd = ); print "\n(!) Retype that password to make certain: "; chomp($passwd2 = ); ReadMode 0; # Term:ReadKey (restore original settings) if ($passwd ne $passwd2) { $valid=1; $i=0; } } while ( ($passwd !~ /^\s*\S{6,100}\s*$/) || $valid); $passwd = crypt($passwd, time()); # encrypting for shadow, time() seeded ########################################################################## ## Outputting what the user has given me, making sure all is well. ## If user doesn't like, start from beginning (I'd like to implement ## function calls for all of these input sections so that user ## can specify one particular area that needs to be amended, but for ## now it'll suffice ########################################################################## print "\n\n----------------------------------------------------------\n" . "Ok, $first_name, here is how you'll be entered into the system:\n" . "Name: $full_name\n" . "Email: $email\n" . "Username: $username\n" . "---------------------------------------------------------\n\n"; $i=0; do { print "\nChoose a valid option to continue!\n" if ($i>0); print "Is this correct? Now is your last chance to bail! (y/n): "; chomp($comply=); ++$i; } while ($comply !~ /^\s*(y|n)\s*$/i); } while ($comply !~ /^\s*y\s*$/i); ############################################################################ ## Now we are checking for the next available UID on the system; now ## on this system UID's start at 100 for users, but this number may ## not be very portable across platforms, should put a DEF at beginning ############################################################################ $max=100; open PASSWD, "/etc/passwd"; while () { chomp; if (/^#*\S+:x:(1\d\d):/) { $max=$1 if ($max<$1); } } close PASSWD; $uid=($max+1); ######################################################################### ## Actually making the system call to useradd, this is it baby! ######################################################################### system( '/usr/bin/sudo', '/usr/sbin/useradd', '-u'=> $uid, '-s'=> $shell, '-p'=> $passwd, '-g'=> $gid, '-m'=> $username ); chomp($date=localtime); # setting for log output ############################################################################ ## Sending logged information to system administrator and putting info ## into a *hardcoded* log file within the 'newuser' ~ ############################################################################ my $msg = new Mail::Send; $msg = new Mail::Send ( Subject =>'New User Added!', To =>'root'); my $fh = $msg->open; print $fh "Attention Administrator!\n\n" . "A new user has recently been added. Information follows:\n\n" . "Date: " . $date . "\nUsername: " . $username . " (UID: " . $uid . ")" . "\nUser: " . $full_name . "\nEmail: " . $email; $fh->close; open Log, ">>/home/newuser/downUnder.log" or warn "Problem writing to log file ($!)"; print Log "$username :: $full_name :: $email - $date\n"; close Log; ############################################################################ ## Terminating message ############################################################################ system("/usr/bin/clear"); print <