git.schokokeks.org
Repositories
Help
Report an Issue
webinterface.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
6108de7
Branches
Tags
master
ticket
webinterface.git
modules
webmailtotp
include
totp.php
Umstellung auf mysqli
Bernd Wurst
commited
6108de7
at 2013-09-19 06:31:27
totp.php
Blame
History
Raw
<?php /* This file belongs to the Webinterface of schokokeks.org Hosting Written 2008-2013 by schokokeks.org Hosting, namely Bernd Wurst <bernd@schokokeks.org> Hanno Böck <hanno@schokokeks.org> To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see http://creativecommons.org/publicdomain/zero/1.0/ Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code. */ function account_has_totp($username) { $username = DB::escape($username); $result = DB::query("SELECT id FROM mail.webmail_totp WHERE email='{$username}'"); if ($result->num_rows > 0) { $tmp = $result->fetch_assoc(); $id = $tmp['id']; return $id; } else { return false; } } function validate_password($username, $password) { $username = DB::escape($username); $result = DB::query("SELECT account, cryptpass FROM mail.courier_mailaccounts WHERE account='{$username}' UNION SELECT account, cryptpass FROM mail.courier_virtual_accounts WHERE account='{$username}'"); if ($result->num_rows != 1) { // Kein Account mit dem Namen oder Name nicht eindeutig return false; } $account = $result->fetch_assoc(); return (crypt($password, $account['cryptpass']) == $account['cryptpass']); } function store_webmail_password($username, $oldpw, $newpw) { $secret = $newpw; if (strlen($oldpw) > strlen($newpw)) { $secret = str_pad($newpw, strlen($oldpw), $newpw); } if (strlen($oldpw) < strlen($newpw)) { $newpw = substr($newpw, 0, strlen($oldpw)); } if (strlen($oldpw) != strlen($secret)) { system_failure('Interner Fehler: Passwörter sind nicht gleich lang'); } $code = ''; for ($i = 0 ; $i != strlen($oldpw) ; $i++) { $code .= chr( ord($oldpw[$i]) ^ ord($secret[$i]) ); } $code = base64_encode($code); DEBUG(array($oldpw, $newpw, $code)); $uid = (int) $_SESSION['userinfo']['uid']; DB::query("REPLACE INTO mail.webmail_totp (useraccount, email, webmailpass) VALUES ({$uid}, '{$username}', '{$code}')"); } function decode_webmail_password($crypted, $webmailpw) { $crypted = base64_decode($crypted); $secret = $webmailpw; if (strlen($crypted) > strlen($webmailpw)) { $secret = str_pad($webmailpw, strlen($crypted), $webmailpw); } if (strlen($crypted) < strlen($webmailpw)) { $webmailpw = substr($webmailpw, 0, strlen($crypted)); } $clear = ''; for ($i = 0 ; $i != strlen($crypted) ; $i++) { $clear .= chr( ord($crypted[$i]) ^ ord($secret[$i]) ); } DEBUG('decrypted: '.$clear); return $clear; } function get_imap_password($username, $webmailpass) { $username = DB::escape($username); $result = DB::query("SELECT webmailpass FROM mail.webmail_totp WHERE email='{$username}'"); $tmp = $result->fetch_assoc(); $crypted = $tmp['webmailpass']; $clear = decode_webmail_password($crypted, $webmailpass); return $clear; } function check_webmail_password($username, $webmailpass) { $clear = get_imap_password($username, $webmailpass); return validate_password($username, $clear); } function generate_secret($username) { $username = DB::escape($username); require_once('external/googleauthenticator/GoogleAuthenticator.php'); $ga = new PHPGangsta_GoogleAuthenticator(); $secret = $ga->createSecret(); DEBUG('GA-Secret: '.$secret); DEBUG('QrCode: '.$ga->getQRCodeGoogleUrl('Blog', $secret)); DB::query("UPDATE mail.webmail_totp SET totp_secret='{$secret}' WHERE email='{$username}'"); return $secret; } function check_locked($username) { $username = DB::escape($username); $result = DB::query("SELECT 1 FROM mail.webmail_totp WHERE unlock_timestamp IS NOT NULL and unlock_timestamp > NOW() AND email='{$username}'"); return ($result->num_rows > 0); } function check_totp($username, $code) { if (check_blacklist($username, $code)) { DEBUG('Replay-Attack'); return false; } $username = DB::escape($username); $result = DB::query("SELECT totp_secret, failures FROM mail.webmail_totp WHERE email='{$username}' AND (unlock_timestamp IS NULL OR unlock_timestamp <= NOW())"); $tmp = $result->fetch_assoc(); $secret = $tmp['totp_secret']; require_once('external/googleauthenticator/GoogleAuthenticator.php'); $ga = new PHPGangsta_GoogleAuthenticator(); $checkResult = $ga->verifyCode($secret, $code, 2); // 2 = 2*30sec clock tolerance if ($checkResult) { DB::query("UPDATE mail.webmail_totp SET failures = 0, unlock_timestamp=NULL WHERE email='{$username}'"); blacklist_token($username, $code); DEBUG('OK'); } else { if ($tmp['failures'] > 0 && $tmp['failures'] % 5 == 0) { DB::query("UPDATE mail.webmail_totp SET failures = failures+1, unlock_timestamp = NOW() + INTERVAL 5 MINUTE WHERE email='{$username}'"); } else { DB::query("UPDATE mail.webmail_totp SET failures = failures+1 WHERE email='{$username}'"); } DEBUG('FAILED'); } return $checkResult; } function generate_qrcode_image($secret) { $url = 'otpauth://totp/Webmail?secret='.$secret; $descriptorspec = array( 0 => array("pipe", "r"), // STDIN ist eine Pipe, von der das Child liest 1 => array("pipe", "w"), // STDOUT ist eine Pipe, in die das Child schreibt 2 => array("pipe", "w") ); $process = proc_open('qrencode -t PNG -s 5 -o -', $descriptorspec, $pipes); if (is_resource($process)) { // $pipes sieht nun so aus: // 0 => Schreibhandle, das auf das Child STDIN verbunden ist // 1 => Lesehandle, das auf das Child STDOUT verbunden ist fwrite($pipes[0], $url); fclose($pipes[0]); $pngdata = stream_get_contents($pipes[1]); fclose($pipes[1]); // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie // proc_close aufrufen, um Deadlocks zu vermeiden $return_value = proc_close($process); return $pngdata; } else { warning('Es ist ein interner Fehler im Webinterface aufgetreten, aufgrund dessen kein QR-Code erstellt werden kann. Sollte dieser Fehler mehrfach auftreten, kontaktieren Sie bitte die Administratoren.'); } } function accountname($id) { $id = (int) $id; $uid = (int) $_SESSION['userinfo']['uid']; $result = DB::query("SELECT email FROM mail.webmail_totp WHERE id={$id} AND useraccount={$uid}"); if ($tmp = $result->fetch_assoc()) { return $tmp['email']; } } function delete_totp($id) { $id = (int) $id; $uid = (int) $_SESSION['userinfo']['uid']; DB::query("DELETE FROM mail.webmail_totp WHERE id={$id} AND useraccount={$uid}"); } function blacklist_token($email, $token) { $email = DB::escape($email); $token = DB::escape($token); DB::query("INSERT INTO mail.webmail_totp_blacklist (timestamp, email, token) VALUES (NOW(), '{$email}', '{$token}')"); } function check_blacklist($email, $token) { $email = DB::escape($email); $token = DB::escape($token); DB::query("DELETE FROM mail.webmail_totp_blacklist WHERE timestamp < NOW() - INTERVAL 10 MINUTE"); $result = DB::query("SELECT id FROM mail.webmail_totp_blacklist WHERE email='{$email}' AND token='{$token}'"); return ($result->num_rows > 0); }