James T
James T

Reputation: 1156

a user to ubuntu server from PHP

Im currently trying to make it so a user can be added from a PHP interface on an Ubuntu web server of mine. I have this file on the server

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);



if(!empty($_GET) && !empty($_GET['user']) && !empty($_GET['mem']) && !empty($_GET['id'])) {
    $user = $_GET['user'];
    $mem = $_GET['mem'];
    $id = $_GET['id'];

    $user =  escapeshellarg($user.$id);

    echo $user; 
    echo $mem;
    echo $id; 

    $output = shell_exec('sudo useradd $user');
    echo "<pre>$output</pre>";
}


?>

The echos and error reporting are just for debugging.
in my /etc/sudoers file i have this line

www-data ALL=(root) NOPASSWD: /usr/sbin/useradd

and ttl is not required. Does anybody know why this doesnt work?

Upvotes: 0

Views: 234

Answers (2)

Nanne
Nanne

Reputation: 64399

This

$output = shell_exec('sudo useradd $user');

will NOT insert $user in that string but will literally execute that string. use

$output = shell_exec("sudo useradd $user");

Upvotes: 1

Theox
Theox

Reputation: 1363

First, I agree with Jamie Taylor : executing shell commands as root seems like a bad idea.

Second, when you use the useradd command, you need to manually input a password. This is done on purpose on Ubuntu.

You can bypass this using the --geckos option.

Here is the command :

adduser --disabled-password --gecos "" username

(Credits to https://askubuntu.com/questions/94060/run-adduser-non-interactively for the gecos part.)

Upvotes: 1

Related Questions