Linux用户创建+PHP和电子邮件


Linux User Creation + PHP and Email

我正在托管一个带有web登录界面的电子邮件服务器,但我以前没有有效的方法在服务器上注册用户以创建要登录的用户。正是因为这个原因,我创建了一个PHP系统来创建用户并设置密码,但创建了上述系统,我知道有一个主要的安全问题,因为服务器将执行创建用户的脚本,然后更改所述用户的密码。因此,人们可以更改不同用户的密码。我现在创建了一个根安全防护,但我需要一个永久性的解决方案来保护我的用户电子邮件。这是我用来创建和设置用户密码的php脚本和shell脚本。

index.php

<?php 
$user=$_POST['user'];
$pass=$_POST['pass'];
$check=$_POST['check'];
if(isset($_POST['submit'])) {
  if($user!="root"){
  if($pass == $check) {
   echo exec ("sudo /root/bin/newuser.sh ".$user." ".$pass."");
   echo ("<html> <title>User Created!</title> <p><h1>Congratulation on the successful creation of your user! </h1></p><p><h1>Your login credentials are as follows:</h1></p><p><h1>Username: ".$user."</h1></p><p><h1>Password: " .$pass."</h1></p></html>");
  } else {
  echo "<html><title>Error creating account!</title><p><h1>Passwords did not match! Please <a href='index.php' reload/> this page!</h1></p></html>";
}
} else {
    echo "Stahp plox!";
  }
}
?>
<html>
  <p align="center">Magnum Dongs Email Registration</p>
  <p align="center">Username will be created as Username@magnumdongs.com, there is no need to add the @magnumdongs.com to the enter of the username you create.</p>
  <p align="center">Upon creation, you will be able to access your email through our Magnum Dongs Email Web Interface, however you will be unable to recieve email via Outlook or an open source alternative at this time.</p>
  <form method="POST" action="index.php" align="center">
   <p>Username :
     <input name="user" type="text"/>
   </p> 
   <p>Password :
     <input name="pass" type="password"/>
   </p>
   <p>Verify &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:
      <input name="check" type="password"/>
   </p>
    <input type="submit" name="submit" value="Create User" />
  </form>
</html>

newuser.sh

 #!/usr/bin/env bash                                                                                                                                                                                                                       
    #echo "Enter username..."                                                                                                                                                                                                                 
    #read user                                                                                                                                                                                                                                
    #echo -e "Username :: $user 'n Is this correct? [y,n]"                                                                                                                                                                                    
    #read y                                                                                                                                                                                                                                   
    #if [[ $y == "y" ]]; then                                                                                                                                                                                                                 
    #       useradd -m $user                                                                                                                                                                                                                  
    #else                                                                                                                                                                                                                                     
    #       echo "Wrong username"                                                                                                                                                                                                             
    #       exit                                                                                                                                                                                                                              
    #fi                                                                                                                                                                                                                                       
    usr=$1                                                                                                                                                                                                                                    
    pass=$2                                                                                                                                                                                                                                   
    useradd -m -s /usr/bin/nologin $usr                                                                                                                                                                                                       
    echo "$usr:$pass" | chpasswd

提前感谢的帮助

还请修改您的代码以防止命令注入

echo-exec("sudo/root/bin/newuser.sh".$user.".$pass.");

如果任何用户提交这样的POST请求:$_POST['user'] = '; rm -rf /;',则服务器的很大一部分将被删除。使用escapeshellarg php函数来防止shell注入攻击。

考虑更改您的邮件系统以使用虚拟帐户,并赋予web应用程序用户添加和修改用户的权限,这将比在具有sudo权限的用户的权限下运行web应用程序更安全。