#!/usr/bin/php
<?php
(file_exists("/usr/share/mcms/user_management_functions.php")) or die("Unable to open /usr/share/mcms/user_management_functions.php");
(include("/usr/share/mcms/user_management_functions.php")) or die("Unable to open /usr/share/mcms/user_management_functions.php");
global $passchange;

function parseargs($argc, $argv){
  global $gecos, $uid, $gid, $groups, $homedir, $shell, $password, $forcepwchange, $quiet, $comment, $login, $randpass, $verbose, $passchange;
  if($argc<2 || in_array($argv[1],array('--help','-h','-help','-?'))) {
    echo "Usage: $argv[0] [options] LOGIN\n";
    echo "  -g, --gid GID               group ID of the account\n";
    echo "  -G, --groups GROUPS         comma separated list of additional group names\n";
    echo "  -d, --dir DIR               home directory\n";
    echo "  -s, --shell SHELL           users shell\n";
    echo "  -c, --comment COMMENT       GECOS field of the account, typically full name\n";
    echo "  -p, --password <PASSWORD>   change the password. If the password is not\n";
    echo "                              provided, it will be prompted for\n";
    echo "  -P, --randpass              generate random password, displayed in summary\n";
    echo "  -C, --forcechange           require the user to change the pass 1st login\n";
    echo "  -q, --quiet                 do not print summary of user addition\n";
    echo "  -v, --verbose               print additional details in summary\n";
    echo "  -h, --help                  print this help message\n";
    exit(254);
  }
  $shortopts="g:G:d:s:c:p::PCqv";
  $longopts=array("gid:", "groups:", "dir:", "shell:", "comment:", "password::", "randpass", "forcechange", "quiet", "verbose");
  $options=getopt($shortopts, $longopts);
  $login=$argv[$argc-1];

  if (isset($options['g']) || isset($options['gid'])) {
    if (isset($options['g'])) {
      $gid=(int)$options['g'];
    } else {
      $gid=(int)$options['gid'];
    }
    $gidtest=popen("getent group $gid | awk 'BEGIN{FS=\":\"}{print $3}'","r");
    $gidresult=(int)trim(fgets($gidtest));
    if($gid!=$gidresult) {
      echo "Specified gid $gid does not exist\n";
      exit(3);
    }
  } else {
    $gid="";
  }

  if (isset($options['G']) || isset($options['groups'])) {
    if (isset($options['G'])) {
      $groups=$options['G'];
    } else {
      $groups=$options['groups'];
    }
  } else {
    $groups="";
  }

  if (isset($options['c']) || isset($options['comment']) ) {
    if (isset($options['c'])) {
      $comment=$options['c'];
    } else {
      $comment=$options['comment'];
    }
  } else {
    $comment="";
  }

  if (isset($options['d']) || isset($options['dir']) ) {
    if (isset($options['d'])) {
      $homedir=$options['d'];
    } else {
      $homedir=$options['dir'];
    }
  } else {
    $homedir="";
  }

  if (isset($options['s']) || isset($options['shell']) ) {
    if (isset($options['s'])) {
      $shell=$options['s'];
    } else {
      $shell=$options['shell'];
    }
  } else {
    $shell="";
  }

  if (isset($options['p']) || isset($options['password']) ) {
    if (isset($options['p'])) {
      $password=$options['p'];
    } else {
      $password=$options['password'];
    }
    $passchange=true;
  } else {
    $passchange=false;
    $password="";
  }

  if (isset($options['P']) || isset($options['randpass']) ) {
    if($password!=null) {
      echo "Cannot use the randpass option when also specifying a password\n";
      exit(4);
    }
    $passchange=true;
    $randpass=true;
  } else {
    $randpass=false;
  }

  if (isset($options['C']) || isset($options['forcechange'])) {
    $forcepwchange=true;
  } else {
    $forcepwchange=false;
  }

  if (isset($options['q']) || isset($options['quiet']) ) {
    $quiet=true;
  } else {
    $quiet=false;
  }

  if (isset($options['v']) || isset($options['verbose'])) {
    $quiet=false;
    $verbose=true;
  } else {
    $verbose=false;
  }
}  //end parseargs

//execution starts here
init();
parseargs($argc,$argv);

$logincheck=popen("getent passwd $login","r");
$loginfound=trim(fgets($logincheck,4096));
if($loginfound=="") {
  echo "User $login does not exist\n";
  exit(5);
}

if ($randpass) {
  $password=randomPassword();
  $passmsg="randomly generated password $password";
}

if ($passchange && $password==null) {
  $pass1=getPass("Please enter the password for $login: ");
  $pass2=getPass("Verify: ");
  while ($pass1!=$pass2) {
    echo "Passwords do not match.  Try again.\n";
    $pass1=getPass("Please enter the password for $login: ");
    $pass2=getPass("Verify: ");
  }
  $passmsg="provided password";
  $password=$pass1;
}

ldap_update_user($login,$gid,$password,$homedir,$comment,$shell,$forcepwchange);
if ($groups!="") ldap_user_set_groups($login, $groups);

if(!$quiet) {
  echo "Modified user $login\n";
  if($password!="" && $randpass) echo "password: $passmsg\n";
  if($verbose) {
    if($password!="" && !$randpass) echo "password: $passmsg\n";
    if($gid!="") echo "gid: $gid\n";
    if($homedir!="") echo "homedir: $homedir\n";
    if($comment!="") echo "comment: $comment\n";
    if($shell!="") echo "shell: $shell\n";
  }
}
?>
