#!/usr/bin/perl -w #Kyle Willett #ITMO 517 Perl HW 3 add user to system #check if user is root, if not exit $isRoot =`whoami`; chomp $isRoot; if ($isRoot ne "root") { print "Program requires root privileges, please run as root.\n"; print "Exiting\n"; exit 1; } #User name checks-------------------------------------------------------------------------------------------- print "Please enter a username for the new user: "; $newUserName = ; chomp $newUserName; print "Checking if user name already exists: \n"; `cat /etc/passwd | grep $newUserName > /dev/null`; if ( $? == 0 ) { print "Username exists, impossible to add duplicate \n"; print "Exiting \n"; exit 1; } print "User name is unique \n"; print "Checking if username meets minimum requirments: \n"; $userNameLength = length($newUserName); $minNameSize = 3; #print "$userNameLength \n"; #print "$minNameSize \n"; if ( $userNameLength < $minNameSize ) { print "Username must be at least 3 characters long \n"; print "Exiting \n"; exit 1; } print "Username meets minimum requirements \n"; print "Enter the full name for the user, it can not be blank."; $fullName = ; chomp $fullName; if ( $fullName eq '' ) { print "Full name can not be an empty string \n"; print "Exiting \n"; exit 1; } #End username checks------------------------------------------------------------------------------------------- #Set Shell for user print "Enter a default shell for the new user choices are csh, ksh, sh, or bash, press enter for default: \n"; $userShell = ; chomp $userShell; if ( $userShell eq "csh" ) { $shellToSet="/bin/csh"; } elsif ( $userShell eq "ksh" ) { $shellToSet="/bin/ksh"; } elsif ( $userShell eq "sh" ) { $shellToSet="/bin/sh"; } elsif ( $userShell eq "bash" ) { $shellToSet="/bin/bash"; } elsif ($userShell eq '') { $shellToSet = "/bin/bash"; } else { print "Invalid shell selected.\n"; print "Exiting\n"; exit 1; } print "Default shell will be set to: $shellToSet \n"; #End Shell check----------------------------------------------------------------------------------------------- #Set UID for user print "Enter UID for new user (greater than 1000) enter for default: \n"; $UIDtoSet = ; chomp $UIDtoSet; if ( $UIDtoSet ne '') #case user did select UID, check to see if valid if so set it { #Check to see if UID is a number or not. if ( $UIDtoSet =~ /^[0-9]+$/ ) { ;#Couldn't find way to negate the regular expression so this if statment is just blank. } else { print "UID must be a number\n" ; exit 1; } #Check to see if UID is in use already `getent passwd $UIDtoSet > /dev/null`; if ( $? == 0 ) { print "UID exists, impossible to add duplicate \n"; print "Exiting \n"; exit 1; } #Check to see if UID is in the reserved range. if ( $UIDtoSet < 1000 ) { print "UID too low, must be over 1000 \n"; print "Exiting \n"; exit 1; } #Check to see if UID is out of range. if ( $UIDtoSet > 4294967295 ) { print "UID too high, must be under 4,294,967,296 \n"; print "Exiting \n"; exit 1; } print "$UIDtoSet is a valid UID \n"; } else #case user didn't select UID { print "Checking highest UID in system and setting UID to first unused ID \n"; $UIDcurrent = 1000; #Check to see highest userid in system while ( 1 ) { `getent passwd $UIDcurrent > /dev/null`; #print "Value of ? is: $? \n"; #print "Value of UIDcurrent is: $UIDcurrent \n \n"; if ( $? == 512 ) #The value to check for from getent is 512 here and not 2 like in bash version. { print "$UIDcurrent is valid ID to use \n"; $UIDtoSet = $UIDcurrent; last; #Apparently you use last to break out of while loop with if statement in perl. } $UIDcurrent= $UIDcurrent + 1; #If UID 1000 is taken go to 1001, 1002, ... } print "$UIDtoSet is a valid UID and will be used \n"; } #End UID setup------------------------------------------------------------------------------------------------- #GID setup----------------------------------------------------------------------------------------------------- #We don't need quite as many checks as UID because GID can be duplicate print "Enter GID for new user (greater than 1000) enter for default: \n"; $GIDtoSet = ; chomp $GIDtoSet; if ($GIDtoSet ne '' ) #case user did select GID, check to see if valid if so set it { #Check to see if GID is a number or not. if ( $GIDtoSet =~ /^[0-9]+$/ ) { ; } else { print "GID must be a number\n" ; exit 1; } #Check to see if GID is in the reserved range. if ( $GIDtoSet < 1000 ) { print "GID too low, must be over 1000 \n"; print "Exiting \n"; exit 1; } #Check to see if GID is out of range. if ( $GIDtoSet > 4294967295 ) { print "GID too high, must be under 4,294,967,296 \n"; print "Exiting \n"; exit 1; } print "$GIDtoSet is a valid GID \n"; } else #Case user didn't select a GID, much simpler than UID case { $GIDtoSet = $UIDtoSet; #Just make new GID equal to UID. print "$GIDtoSet will be used for GID \n"; } #We need to real quick determine if the group already exists or not and add if it does not exist. `getent group $GIDtoSet > /dev/null`; if ( $? != 0 ) { `groupadd $GIDtoSet`; } #End GID setup------------------------------------------------------------------------------------------------- #Create User stage--------------------------------------------------------------------------------------------- print "\nAbout to create new user account with following settings: \n"; print "User name is: $newUserName \n"; print "Full user name is: $fullName\n"; print "UID: $UIDtoSet\n"; print "GID: $GIDtoSet\n"; print "Default login shell: $shellToSet\n"; print "Does this look correct?\n"; print "Enter y to confirm\n"; $confirmUserAdd = ; chomp $confirmUserAdd; if ( uc($confirmUserAdd) eq "Y" ) { `useradd $newUserName -m -c "$fullName" -u $UIDtoSet -g $GIDtoSet -s $shellToSet`; } else { print "User add aborted by user\n"; print "Exiting\n"; exit 1; } #End create user stage----------------------------------------------------------------------------------------- `echo "$newUserName:password123" | chpasswd`; print "User account $newUserName has been created with password password123 successfully!\n"; print "Exiting \n"; exit 0;