1

I'm creating a system that allow me to create users automatically after an inscription. In this way, i've made a Perl script that generate a random password, crypt it with "mkpasswd" and then create an user with "useradd", after that user-infos are sended to a database.

I've search documentation, and i've try to use the php exec function to call the Perl program.

$command = "/server/backoffice/scripts/usercreation.pl $username";
exec($command, $result);
print_r($result);

The usercreation.pl script work fine in a terminal, but for a unknown reason, it just not work from php, nothing happen, i know that the problem come from the useradd syntax or if not it come from the useradd command. The error value returned is 255 <=> 0xff <=> -1 .

I think the problem come from the --password option of useradd, in fact, this option use mkpasswd and maybe it could disturb the shell or something else like that :p

The complete useradd command used by the Perl program:

system("/usr/sbin/useradd $username -m -d $home --expiredate $expire --shell $shell --gid $gid --password $c_password");

EDIT: The usercreation.pl file on pastebin (has been truncated to the part we are investigate): http://pastebin.com/DgFdndXf

The php one : http://pastebin.com/pQhADNzg

The web server used is nginx/0.7.65.

The version of php used is PHP 5.3.3-1.

uname -a

Linux openco 2.6.35-32-generic #67-Ubuntu SMP Mon Mar 5 19:35:26 UTC 2012 i686 GNU/Linux

Thanks for all!

4
  • Does the PHP run from a web server? Commented Jun 2, 2012 at 14:45
  • 1
    See this question for a way to debug your perl script execution from within PHP. Commented Jun 2, 2012 at 14:55
  • The error does not come from Perl, but from the PHP interpretation of ... Commented Jun 3, 2012 at 14:36
  • @torr Do you run the perl script in the shell with the same user as runs the PHP script? Commented Jun 3, 2012 at 18:17

1 Answer 1

2

The useradd must be run as root, so you will need sudo to specify that nginx can run your script as root.

Be extremely careful with this stuff; make sure you sanitize user names and escape your command properly as well using escapeshellarg().

Sign up to request clarification or add additional context in comments.

10 Comments

So, it would work if i add www-data ALL= NOPASSWD: /usr/sbin/useradd in /etc/sudoers and if i change $command to "sudo ".$command ?
Now, the error returned is 127 <=> 0x7f, i've also changed the $command in the Perl program to sudo useradd [...]
@DaveNull Actually ... the sudoers file should give permission to run the Perl script as root, not useradd itself.
I've made the changes, exec("sudo ".$command, $result), useradd is called wthout sudo. www-data is no allowed to execute the Perl script as root. But, it do not change the error returned.
Well, then I'm out of ideas I'm afraid :-/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.