From 7acc132f302b7619de972b08bd7c4fb6a098750e Mon Sep 17 00:00:00 2001 From: Pinga <121483313+getpinga@users.noreply.github.com> Date: Thu, 3 Aug 2023 15:49:50 +0300 Subject: [PATCH] Added rudimentary EPP server --- epp/epp.php | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 epp/epp.php diff --git a/epp/epp.php b/epp/epp.php new file mode 100644 index 0000000..e63081d --- /dev/null +++ b/epp/epp.php @@ -0,0 +1,103 @@ +column('logged_in', Table::TYPE_INT, 1); +$table->create(); + +$db = new PDO('mysql:host=localhost;dbname=epp', 'username', 'password'); + +$server = new Server('0.0.0.0', 700); + +$server->handle(function (Connection $conn) use ($table, $db) { + $data = $conn->recv(); + $xml = simplexml_load_string($data); + + if ($xml === false) { + sendEppError($conn, 2001, 'Invalid XML'); + return; + } + + $clID = (string) $xml->command->clTRID; + $isLoggedIn = $table->get($clID, 'logged_in'); + + // Parsing a login command + if ($xml->getName() == 'epp' && isset($xml->command->login)) { + $clID = (string) $xml->command->login->clID; + $pw = (string) $xml->command->login->pw; + + if (checkLogin($db, $clID, $pw)) { + $table->set($clID, ['logged_in' => 1]); + $conn->send('Login success!'); + } else { + sendEppError($conn, 2200, 'Authentication error'); + } + return; + } + + // Parsing a logout command + if ($xml->getName() == 'epp' && isset($xml->command->logout)) { + $table->del($clID); + $conn->send('Logout success!'); + return; + } + + if (!$isLoggedIn) { + sendEppError($conn, 2202, 'Authorization error'); + return; + } + + // Parsing a domain:check command + if ($xml->getName() == 'epp' && isset($xml->command->{'check'}->{'domain:check'})) { + processDomainCheck($conn, $db, $xml); + return; + } + + sendEppError($conn, 2100, 'Unknown command'); +}); + +$server->start(); + +function processDomainCheck($conn, $db, $xml) { + $domains = $xml->command->{'check'}->{'domain:check'}->children('domain', true); + $response = 'Command completed successfully'; + + foreach ($domains as $domain) { + $domainName = (string) $domain; + $availability = $db->query("SELECT availability FROM domains WHERE domain_name = '$domainName'")->fetchColumn(); + $availString = $availability ? 'available' : 'unavailable'; + $response .= "$domainName"; + } + + $response .= ''; + $conn->send($response); +} + +function checkLogin($db, $clID, $pw) { + $stmt = $db->prepare("SELECT password FROM users WHERE username = :username"); + $stmt->execute(['username' => $clID]); + $hashedPassword = $stmt->fetchColumn(); + + return password_verify($pw, $hashedPassword); +} + +function sendEppError($conn, $code, $msg) { + $errorResponse = << + + + + $msg + + + +XML; + + $conn->send($errorResponse); +} \ No newline at end of file