I am writing a perl program for user administration, and I keep getting this error: useradd: Permission denied. useradd: cannot lock /etc/passwd; try again later. I run the program using su -c command, the code below is the relevant snippet - full code at https://github.com/Kajkacz/ASU_Indexing/blob/master/Users.pl .
sub createUserPage{
$Mw->destroy;
$Mw = MainWindow->new;
$Mw->geometry("500x300");
$Mw->title("Create User");
my $uid = &getFreeUID;
my $uspasswd = &randomizePassword;
my $usname = 'UsernameHere';
my $username = $Mw->Entry(-textvariable=> \$usname )->grid($Mw->Label(-text => 'Username'));
my $UID = $Mw->Entry(-textvariable=> \$uid )->grid($Mw->Label(-text => 'UID'));
my $password = $Mw ->Entry(-textvariable=> \$uspasswd)->grid($Mw->Label(-text => 'Password'));
$Mw->Button(-text=>"Create User", -command =>sub{&createUser($uid,$usname,$uspasswd)},-width => $buttonWidth)->grid();
$Mw->Button(-text=>"Check If UID is free", -command =>sub{&checkUID($uid)},-width => $buttonWidth)->grid();
$Mw->Button(-text=>"Get Random Password", -command =>sub{$uspasswd = &randomizePassword},-width => $buttonWidth)->grid();
$Mw->Button(-text=>"Back to Main Menu", -command =>sub{&getStartWindow},-width => $buttonWidth)->grid();}
sub createUser{
my $UID = shift;
my $username = shift;
my $password = shift;
my $adduser = '/usr/sbin/useradd';
my $cmd = qq($adduser \"$username\");
# my $cmd = qq($adduser \"$username\" -p \"$password\" -u $UID);
print "$cmd \n";
system $cmd; }
I'd welcome any insight possible.Also, my first ever Perl program, so feel free to correct my code