#!/usr/bin/perl
# Main path to users account
$home_directory = "PATH"; #/home/username
# Mailbox path
$mail_path = $home_directory . "/mail";
# This directory is where the passwd and shadow files are stored
$p_directory = $home_directory . "/etc";
$html_content_header = "Content-type: text/html\n\n";
$newuser = "USERNAME";
$usrPassword = "PASSWORD";
print $html_content_header;
&CreateNewUser;
# Function to create a new user
sub CreateNewUser {
mkdir ("$mail_path/$newuser",0770) or die "Could not create directory $mail_path/$newuser. $!";
mkdir ("$mail_path/$newuser/uploads",0770) or die "Could not create directory $mail_path/$newuser/uploads. $!";
mkdir ("$mail_path/$newuser/addressbook",0770) or die "Could not create directory $mail_path/$newuser/addressbook. $!";
# one handle to read file data
open(MAILFILE, "$p_directory/passwd") or die "Cant access file";
open(SHADOWFILE, "$p_directory/shadow") or die "Cant access file";
#another handle to write data
open(MAILFILE2, ">>$p_directory/passwd") or die "Cant access file";
open(SHADOWFILE2, ">>$p_directory/shadow") or die "Cant access file";
# get the users and store in array in reverse order
@lines = reverse <MAILFILE>;
foreach $line (@lines) {
if ($line =~ /$newuser/) {
print "User already exists";
exit;
}
}
#get this line from your passwd file just duplicate it as passwords are stored in the shadow file
print MAILFILE2 "$newuser:x:xxxxx:xxx::$home_directory:/bin/bash\n";
#this is the important file
print SHADOWFILE2 "$newuser:" . crypt($usrPassword, $usrPassword) . ":::::::\n";
close(MAILFILE);
close(SHADOWFILE);
close(SHADOWFILE2);
close(MAILFILE2);
}