stderr = $stderr; $this->infoXml = new DomDocument("1.0","UTF-8"); $this->infoXml->preserveWhiteSpace = false; $this->infoXml->formatOutput = true; } function GetInfoXmlFilePath() { return getenv("DDINFO")."/".getenv("AMDA_USERS_INFO"); } function GetGroupsXmlFilePath() { return getenv("DDINFO")."/".getenv("AMDA_GROUPS_INFO"); } function LoadInfoXmlFile() { if (file_exists($this->GetInfoXmlFilePath())) { $this->infoXml->load($this->GetInfoXmlFilePath()); $this->rootElement = $this->infoXml->documentElement; } else { $this->rootElement = $this->infoXml->createElement("users"); $this->infoXml->appendChild($this->rootElement); } return 1; } function AddUser($login, $pwd_hash, $first_name, $last_name, $email, $news, $groups) { if (!$this->LoadInfoXmlFile()) return 0; $users = $this->infoXml->getElementsByTagName("user"); //test if login is already used for ($i = 0; $i < $users->length; $i++) { $crtLogin = $users->item($i)->getAttribute("login"); if ($crtLogin == $login) { fprintf($this->stderr,"Login already exist on users info file\n"); return 0; } } date_default_timezone_set('UTC'); $user = $this->infoXml->createElement("user"); $user->setAttribute("login",$login); $user->setAttribute("name",$last_name); $user->setAttribute("first_name",$first_name); $user->setAttribute("group",$groups); $user->setAttribute("email",$email); $user->setAttribute("date",date('j/m/y')); $user->setAttribute("news",$news); $this->rootElement->appendChild($user); exec('DDadmin -a '.$login.' '.$pwd_hash,$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output[0]."\n"); return 0; } else $this->infoXml->save($this->GetInfoXmlFilePath()); return 1; } function ModifyUserPwd($login, $pwd_hash, $pwd_hash_new) { exec('DDadmin -m '.$login.' '.$pwd_hash.' '.$pwd_hash_new,$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output[0]."\n"); return 0; } return 1; } function ModifyUserGroup($login,$groups) { if (!$this->LoadInfoXmlFile()) return 0; $users = $this->infoXml->getElementsByTagName("user"); for ($i = 0; $i < $users->length; $i++) { $crtLogin = $users->item($i)->getAttribute("login"); if ($crtLogin == $login) { $users->item($i)->setAttribute("group",$groups); $this->infoXml->save($this->GetInfoXmlFilePath()); return 1; } } fprintf($this->stderr,"Cannot found user\n"); return 0; } function ResetUserPwd($login) { exec('DDadmin -r '.$login,$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output."\n"); return 0; } else fprintf($this->stderr,"New password is : ".$output[0]."\n"); return 1; } function DeleteUser($login) { if (!$this->LoadInfoXmlFile()) return 0; $users = $this->infoXml->getElementsByTagName("user"); for ($i = 0; $i < $users->length; $i++) { $crtLogin = $users->item($i)->getAttribute("login"); if ($crtLogin == $login) { $this->rootElement->removeChild($users->item($i)); break; } } exec('DDadmin -d '.$login,$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output[0]."\n"); return 0; } $this->infoXml->save($this->GetInfoXmlFilePath()); return 1; } //This function will be deprecated for AMDA-NG function GenerateGroupsXmlFile() { if (!$this->LoadInfoXmlFile()) return 0; $users = $this->infoXml->getElementsByTagName("user"); $groupsXml = new DomDocument("1.0","UTF-8"); $groupsXml->preserveWhiteSpace = false; $groupsXml->formatOutput = true; $rootGroupsElement = $groupsXml->createElement("AMDA_USERS"); $groupsXml->appendChild($rootGroupsElement); for ($i = 0; $i < $users->length; $i++) { if (strcmp($users->item($i)->getAttribute("group"),"") == 0) continue; $u = $groupsXml->createElement("user",$users->item($i)->getAttribute("login")); $u->setAttribute("group",$users->item($i)->getAttribute("group")); $rootGroupsElement->appendChild($u); } $groupsXml->save($this->GetGroupsXmlFilePath()); return 1; } function SendRegistrationMail($login,$pwd,$first_name, $last_name, $email) { $subject = 'AMDA registration'; $msg = "Dear $first_name $last_name, \r\n\r\n"; $msg .= "Thanks for your interest in AMDA\r\n\r\n"; $msg .= "Your login: $login \r\n"; $msg .= " password: $pwd \r\n\r\n"; $msg .= "at http://cdpp3.irap.omp.eu \r\n\r\n"; $msg .= "Please contact us in case of any problems or questions.\r\n\r\n"; $msg .= "Best regards,\r\n\r\n"; $msg .= "CDPP-AMDA Team"; $headers = "From: amda@irap.omp.eu " . "\r\n". "Reply-To: amda@irap.omp.eu " . "\r\n". "Cc: amda@irap.omp.eu" . "\r\n". "Content-type: text/plain; charset=utf-8\r\n"; mail($email, $subject, $msg, $headers); } function Check() { if (!$this->LoadInfoXmlFile()) return 0; $users = $this->infoXml->getElementsByTagName("user"); exec('DDadmin -l',$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output[0]."\n"); return 0; } $ddAdminUsers = explode(',',$output[0]); for ($i = 0; $i < $users->length; $i++) { $found = false; for ($j = 0; $j < count($ddAdminUsers); $j++) if (strcmp($users->item($i)->getAttribute("login"),$ddAdminUsers[$j]) == 0) { $found = true; break; } if (!$found) fprintf($this->stderr,"User ".$users->item($i)->getAttribute("login")." not found on nc file\n"); } for ($i = 0; $i < count($ddAdminUsers); $i++) { $found = false; for ($j = 0; $j < $users->length; $j++) if (strcmp($users->item($j)->getAttribute("login"),$ddAdminUsers[$i]) == 0) { $found = true; break; } if (!$found) fprintf($this->stderr,"User ".$ddAdminUsers[$i]." not found on info file\n"); } return 1; } } //----------------------------------------------------------------------------------------------------- //crypt the password with the salt corresponding to the login function cryptPwd($login,$pwd) { exec('DDadmin -s '.$login,$output,$return); if ($return != 0) { fprintf($this->stderr,"DDadmin error : ".$output."\n"); return ''; } $salt = $output[0]; return crypt($pwd,$salt); } //---------------------------------------------------------------------------------------------------- putenv("LD_LIBRARY_PATH=".getenv("LD_LIBRARY_PATH")); putenv("PATH=./:".getenv("DDBASEBIN").":/bin:/usr/bin"); $stderr = fopen("php://stderr","w"); $shortopts = "j:"; $longopts = array("json:"); $options = getopt($shortopts, $longopts); $json_file = ""; if (array_key_exists("json",$options)) { $json_file = $options["json"]; } else if (array_key_exists("j",$options)) { $json_file = $options["j"]; } if (!empty($json_file)) { if (!file_exists($json_file)) { echo "[ERROR] Cannot find json file: ".$json_file.PHP_EOL; exit(1); } $json_string = file_get_contents($json_file); $args = json_decode($json_string); if (empty($args)) { echo "[ERROR] Cannot decode json data from: ".$json_file.PHP_EOL; exit(1); } } else { $args = new stdClass(); $args->action = $argv[1]; switch ($args->action) { case 'delete' : $args->login = $argv[2]; break; case 'groups' : $args->login = $argv[2]; $args->groups = $argv[3]; break; case 'add' : $args->login = $argv[6]; $args->pwd = $argv[7]; $args->first_name= $argv[8]; $args->last_name= $argv[9]; $args->email= $argv[10]; $args->pwd_hashed = $argv[2]; $args->news= $argv[3]; $args->groups= $argv[4]; $args->sendEmail= $argv[5]; break; default : fprintf($stderr,"Not implemented action ".$args->action."\n"); exit; } } if (!isset($args->action)) { fprintf($stderr,"Missing action argument\n"); exit; } $userManager = new UserManagerClass($stderr); switch ($args->action) { case 'add' : if (!isset($args->login) || !isset($args->pwd) || !isset($args->first_name) || !isset($args->last_name) || !isset($args->email)) { fprintf($stderr,"Missing argument(s) to add a user\n"); exit; } if (isset($args->pwd_hashed) && !($args->pwd_hashed)) $pwd_hash = crypt($args->pwd,chr(rand(97,122)).chr(rand(97,122))); else $pwd_hash = $args->pwd; if ($userManager->AddUser($args->login,$pwd_hash,$args->first_name,$args->last_name,$args->email, isset($args->news) ? $args->news : 1, isset($args->groups) ? $args->groups : '')) { $userManager->GenerateGroupsXmlFile(); if (isset($args->sendEmail) && $args->sendEmail) $userManager->SendRegistrationMail($args->login,$args->pwd,$args->first_name,$args->last_name,$args->email); fprintf($stderr,"User ".$args->login." added\n"); } break; case 'modify' : if (!isset($args->login) || !isset($args->pwd) || !isset($args->pwd_new)) { fprintf($stderr,"Missing argument(s) to modify the user password\n"); exit; } if (isset($args->pwd_hashed) && !($args->pwd_hashed)) $pwd_hash = cryptPwd($args->login,$args->pwd); else $pwd_hash = $args->pwd; if (isset($args->pwd_new_hashed) && !($args->pwd_new_hashed)) $pwd_new_hash = crypt($args->pwd_new,chr(rand(97,122)).chr(rand(97,122))); else $pwd_new_hash = $args->pwd_new; if ($userManager->ModifyUserPwd($args->login,$pwd_hash,$pwd_new_hash)) fprintf($stderr,"Password of".$args->login." modified\n"); break; case 'groups' : if (!isset($args->login) || !isset($args->groups)) { fprintf($stderr,"Missing argument(s) to modify the user groups\n"); exit; } if ($userManager->ModifyUserGroup($args->login,$args->groups)) { $userManager->GenerateGroupsXmlFile(); fprintf($stderr,"Groups of ".$args->login." modified\n"); } break; case 'reset' : if (!isset($args->login)) { fprintf($stderr,"Missing argument(s) to reset the user password\n"); exit; } $userManager->ResetUserPwd($args->login); break; case 'delete' : if (!isset($args->login)) { fprintf($stderr,"Missing argument(s) to delete user\n"); exit; } if ($userManager->DeleteUser($args->login)) { $userManager->GenerateGroupsXmlFile(); fprintf($stderr,"User ".$args->login." deleted\n"); } break; case 'check' : $userManager->Check(); break; } ?>