0

Can anyone help me on this simple thing? I have a php registration form that communicates directly with MySQL database and adds users when someone fulfils the form. The problem starts when I want to also create a home directory for each user (with a bash script) that makes this registration.

What I've done: I created a bash script

#!/bin/bash

sudo useradd -m /home/$1

Whatever parameter I made on this simple script until now didn't work in order to create the user's home directory.

4
  • what kind of 'users' are they who get a home directory on the server? Commented Dec 16, 2012 at 9:40
  • Is apache (or www-data) a sudoer? Commented Dec 16, 2012 at 9:46
  • I hope your users can't input names like user -g0 or user -Gsudo or you'll have surprises! Anyway, my message is: Use More Quotes! Commented Dec 16, 2012 at 12:58
  • to accomplish that, you'll probably also need something like suexec on your apache server Commented Dec 16, 2012 at 13:06

1 Answer 1

1

Problems:

  1. Explicit home directory: Your command line syntax is wrong. Try the following instead:

    sudo useradd -m username

    or

    sudo useradd -d /home/username -m username

  2. Is PHP a sudoer? Only certain accounts are able to execute sudo. Check that the account PHP runs under is listed in the sudoers file. You can usually easily the file with

    visudo

  3. Is PHP chroot-ed? If so, you may not be able to create new linux user accounts from scripts with any reasonable code.

A working test

The following worked for me under Debian (without sudo).

newuser.sh

#!/bin/bash
useradd -d /home/$1 -m $1

newuser.php

<?php
  exec('/bin/bash /var/www-scripts/newuser.sh joe');
?>

test invocation: php newuser.php

If you can perform the same tests (but add sudo as needed), then the only reason it would not work when invoked by your web server is a security problem. That could either be a chroot environment, or lacking sudoer privilege.

Additional resources

You can see how other people have solved this problem by reviewing the following:

  1. http://www.weberdev.com/get_example.php3?ExampleID=3766
  2. http://www.linuxforums.org/forum/programming-scripting/176031-solved-crafting-php-script-create-unix-user.html
Sign up to request clarification or add additional context in comments.

6 Comments

no nothing again :( I also used the sudo visudo command www-data ALL=(ALL) NOPASSWD: /var/www/fredehosting/adduser.sh
@misthe Have you confirmed that PHP runs under www-data? Sometimes PHP is configured to run under a different account for security reasons -- particularly if serving scripts from multiple users.
well after looking it more carefully i think that the request might not works correct from my php. form, that's why the bash script not executed !!!
@misthe Ok, it sounds like the problem is solved. Hopefully I was at least helpful with the command line stuff. :-)
to be honest No i type the command from my PhP like this and can create the User. I also try it the shell_exec the same problem .. exec(" /bin/bash /var/www/fredhosting/adduser.sh $username");
|

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.