Reputation: 100
I've searched for some hint about this one but I'm clueless
I'm trying to add a user to a linux machine with perl code :
my $md5password = `openssl passwd -1 $password1`;
system("useradd -s /bin/bash -d /home/$username -p $md5password");
but this is resulted in :
sh: 2: -p: not found
when I'm executing this commands via terminal there's no problem... arrr! anyone?
update:
10x to @BraveNewCurrency advice it work's :)
code:
if(($password1 eq $password2) && length($password1) > 0)
{
$valid = 1;
my $md5password = `openssl passwd -1 $password1`;
chomp($md5password);
system("useradd -s /bin/bash -d /home/$username -p \'$md5password\' $username");
I will look more into sanitizing $username
later on.
I cant upvote @BraveNewCurrency cause my rep is under 15 ...I will be glad if some of you will upvote him.
Upvotes: 2
Views: 5368
Reputation: 16786
As far as I know, the system
command uses a list of arguments, not a string.
you should also probably be explicit regarding the path to the command you are trying to execute.
Have you tried:
@args = (
'/usr/sbin/useradd',
'-s', '/bin/bash',
'-d', "/home/$username",
'-p', $md5password
);
system(@args) == 0
or die "Failed adding user: @args failed: $?"
As others have said, you should make sure you sanitize the $username
before you create the user or you could get in big trouble.
Upvotes: 0
Reputation: 13065
You should be checking your username/password for shell meta characters, and escaping them. (Otherwise it's a security problem waiting to happen.)
You should also look into Chef/Puppet/Salt/Ansible/.. or the like, which allow you to automate building servers at a much higher level. They are all declarative (and idempotent) so don't worry about icky details.)
Upvotes: 1