Command line script to create user passwd - php

I'm very new to Linux and PHP so forgive me if this is a dumb question.
I need to create a PHP command line script that will add a user to the system, to be used by the ftp server. I have it set up at this point that it creates the account just fine. The problem I'm facing is that even though I pass through the password when I create the account ie. -p $password, I still have to run the "passwd" command on the account and set the password manually before proftpd will let that account log in.
For the life of me I can't figure out how to run the passwd command from within the php. Obviously I can run it, but I have no idea how to programatically enter the password when prompted.
Just for more info, this is the command I run when creating the account. $username and $ password are passed into the function.
shell_exec("useradd ".$username." -g ftpusers -m -p ".$password." -d
/home/ftp/".$username."/ -s /bin/false);
now im looking to add something along the lines of:
shell_exec("passwd ".$username);
echo $password;
echo $password;
Just for the record, this last example is just for illustration.
Any help appreciated.

You need to use the hashed password using useradd, e.g.
$password = escapeshellarg( crypt('password') );
shell_exec("useradd username -p $password");
-p, --password PASSWORD
The encrypted password, as returned by crypt(3). The default is
to disable the account.
(man useradd)

Can't guarantee it'll work, but on first thoughts, my solution would be:-
<?php
$fp = popen("/usr/bin/passwd " . $user, "w");
fwrite($fp, $password . "\n");
fwrite($fp, $password . "\n");
fclose($fp);

$connection = ssh2_connect('localhost', 22);
if (ssh2_auth_password($connection, 'root', '')) {
$username = '';
$password = '';
ssh2_exec($connection, "useradd $username -m -p $password -d /home/$username/ -s /bin/false");
} else {
die('Authentication Failed...');
}

Related

mysqldump php command easyphp

when i run the following command from the cmd box, it works fine; creates the file with my database data inside it:
"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql
but then when i run this one from a php script, it makes the file, but its empty:
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql');
(note: i have no password setup)
what am i doing wrong?
please help
thanks
If you don't have any password on your user account, you should try without the -p argument.
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root databasename > dumpedfile.sql');
Try something like this:
<?php
$User = "root";
$Password = "mypass";
$DatabaseName = "bcms6"
$File = "mydump.sql";
shell_exec("fullpath\mysqldump --allow-keywords --opt -u $User -p $Password $DatabaseName > $File");
//Or this way
shell_exec('"C:\Program Files (x86)\EasyPHP-5.3.9\mysql\bin\mysqldump.exe" -u root -p database > database_dump.sql'); //second way with full path.
?>
You can try this way too:
$command = 'mysqldump -u root -ppassword database > /path/database.sql';
system($command, $output);
if($output != 0) {
echo 'Error during backup';
else {
echo 'Database dumped';
}
EDITED..
The password has to be hardcoded.

Trouble with mysqldump when backing up db as xml

I'm trying to backup one of my databases with mysqldump and I'm trying to export it to an xml file which already exists. Here is what I have:
<?php
$dbUser = 'user'; // db User
$dbPass = 'pass'; // db User Password
$dbName = 'db'; // db name
$dest = $_SERVER['DOCUMENT_ROOT'] . 'backup'; // Path to directory
class MySQLDump {
private $cmd;
function MySQLDump($dbUser,$dbPass,$dbName,$dest) {
$fname = $dbName.'.xml';
$this->cmd='mysqldump -X -u'.$dbUser.' -p'.$dbPass.' '.$dbName.
' >'.$dest.'/'.$fname;
}
public function backup() {
system($this->cmd, $error);
if($error) {
trigger_error ('Backup failed: ' . $error . '<br />Attempted: ' . $this->cmd);
}
}
} // end class def
$mysqlDump = new MySQLDump($dbUser, $dbPass, $dbName, $dest);
$mysqlDump->backup();
?>
This always generates the error thrown in the backup function. Here is a sample of $cmd's output:
mysqldump -X -udan -pdanPass danDB >/var/www/prod/dan/web/backup/danDB.xml
Any idea what's going wrong? I've never really used mysqldump so I'm not sure if I'm approaching this correctly. Any help is greatly appreciated.
try adding a space between -u and your $dbUser.
According to Does mysqldump return a status? something is going wrong, yes. We can not say if a warning or an error because you didn't share the return code.
Apart from that you can easily learn what exactly goes wrong by looking into STDERR output returned from the mysqldump command. E.g. you can pipe it into database.err:
mysqldump -X -udan -pdanPass danDB >/path/to/backup/danDB.xml 2>database.err
^^^^^^^^^^^^^^
This one work for me
mysqldump --user=xxx --password=xxx databasename --xml > file.xml

sudo with libssh2

<?php
$ssh = ssh2_connect('domain.tld');
ssh2_auth_password($ssh, 'username', 'password');
$shell = ssh2_shell($ssh);
echo fread($shell, 1024*1024);
fwrite($shell, "sudo ls -la\n");
$output = fread($shell, 1024*1024);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
fwrite($shell, "password\n");
echo fread($shell, 1024*1024);
}
All that does is display the banner and prompt. It doesn't actually give me the output of the ls -la command. On phpseclib it works just fine.
Any ideas?
I wrote an SSH wrapper (https://github.com/bubba-h57/PHP-SSH2) to facilitate a lot of my own PHP scripting that needs to SSH into remote servers, and run into this issue quite often.
I solved it with this incantation:
echo <password> | sudo -S <command>
Which for you might look like:
fwrite($shell, "echo $mySudoPassword | sudo -S ls -la\n");
I just pipe the password everytime I use sudo, whether I think I need it or not.
Using my own wrapper, it looks like:
require_once 'SSH2.php';
// Test Unix
$username = 'someuser';
$password = 'somepwd';
$host = 'somenixhost.com';
$port = 22;
$ssh2 = new My_SSH2($host, $port);
$ssh2->authPassword( $username, $password);
$ssh2->setPrompt(':~#'); // Set initial expected prompt
$ssh2->openShell();
$ssh2->setPrompt("MYCUSTOMSSHPROMPT> "); // Create a unique, easily found prompt
$ssh2->exec("PS1='MYCUSTOMSSHPROMPT> '"); // Execute the command.
echo $ssh2->exec('cd /var/www') . "\n"; // Change directories.
echo $ssh2->exec("echo $password | sudo -S ls -la\n") . "\n"; // Print LS
echo "\n===================Begin History=============\n";
echo $ssh2->getHistory();
$ssh2->disconnect();
echo "\n===================end=============\n";
exit;

Issues binding to Active Directory using PHP on Apache

I have a PHP script on an apache server, and every time I try to run it, it tells me:
unable to bind to the AD
It connects fine though. I took it off the apache server, and ran it locally from my machine, and it was able to bind just fine. I'm assuming that there is something wrong with my configuration of apache.
I'm using the adLDAP API, and this is the following script I'm trying to run. It's basically a test to see whether I'm able to bind successfully or not and check to see if the credentials entered are in the Active Directory.
$username = $_POST["username"];
$password = $_POST["password"];
$formage = $_POST["formage"];
if ($_POST["oldform"]) { //prevent null bind
if ($username != NULL && $password != NULL){
//include the class and create a connection
include (dirname(__FILE__) . "/../src/adLDAP.php");
try {
$adldap = new adLDAP();
}
catch (adLDAPException $e) {
echo $e;
exit();
}
//authenticate the user
if ($adldap->authenticate($username, $password)){
//establish your session and redirect
session_start();
$_SESSION["username"] = $username;
$_SESSION["userinfo"] = $adldap->user()->info($username);
$redir = "Location: https://" . $_SERVER['HTTP_HOST'] .
dirname($_SERVER['PHP_SELF']) . "/menu.php";
header($redir);
exit;
}
}
$failed = 1;
}
Why am I getting this error: unable to bind to the AD?
I figured out the issue.
Apparently SELinux was blocking the port I needed to connect to, so I just essentially told SELinux that it's ok to connect to that port. Now everything works like a charm. Here is how I allowed Apache access to the port:
grep httpd /var/log/audit/audit.log | audit2allow -M mypol
semodule -i mypol.pp

Making a database backup

I created two simple PHP scripts to back up a MySQL database, but these are not working as expected. I use my program on a Mac using MAMP and using AMPPS on Windows 7.
Here the two scripts I use to backup the database:
[Message edited]
The First and only code
<?php
$host="localhost";
$user="root";
$password="root";
$db="trasporti";
$dbcnx_backup=#mysql_connect("$host", "$user", "$password");
mysql_select_db("$db");
$backupFile = '../../t6/backup/' . $db . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h $host -u $user -p $password $db > $backupFile";
system($command, $retval);
echo $command;
var_dump($retval);
if ($retval==0)
{
echo "BackUP Riuscito!";
}
else
{
echo "BackUP Fallito!";
}
mysql_close($dbcnx_backup);
?>
Can someone explain what I am doing wrong here?
Remove the space after -p.
You can remove all the calls to the mysql_ functions in your code.
Is safe_mode enabled in your PHP configuration? Use phpinfo() to tell, if yes, you might need to configure PHP properly or turn it off.

Categories