b07839f329b1ed56bf9f685af1b4ac3fda8bab95
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

1) <?php
2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
5) Written 2008-2012 by schokokeks.org Hosting, namely
6)   Bernd Wurst <bernd@schokokeks.org>
7)   Hanno Böck <hanno@schokokeks.org>
8) 
9) 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.
10) 
11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see 
12) http://creativecommons.org/publicdomain/zero/1.0/
13) 
14) 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.
15) */
16) 
17) function account_has_googleauth($username)
18) {
19)   $username = mysql_real_escape_string($username);
20)   $result = db_query("SELECT id FROM mail.webmail_googleauth WHERE email='{$username}'");
21)   if (mysql_num_rows($result) > 0) {
22)     $tmp = mysql_fetch_assoc($result);
23)     $id = $tmp['id'];
24)     return $id;
25)   } else {
26)     return false;
27)   }
28) }
29) 
30) 
31) 
32) function validate_password($username, $password) 
33) {
34)   $username = mysql_real_escape_string($username);
35)   $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}'");
36)   if (mysql_num_rows($result) != 1) {
37)     // Kein Account mit dem Namen oder Name nicht eindeutig
38)     return false;
39)   }
40)   $account = mysql_fetch_assoc($result);
41)   return (crypt($password, $account['cryptpass']) == $account['cryptpass']);
42) }
43) 
44) 
45) function store_webmail_password($username, $oldpw, $newpw)
46) {
47)   $secret = $newpw;
48)   if (strlen($oldpw) > strlen($newpw)) {
49)     $secret = str_pad($newpw, strlen($oldpw), $newpw);
50)   }
51)   if (strlen($oldpw) < strlen($newpw)) {
52)     $newpw = substr($newpw, 0, strlen($oldpw));
53)   }
54)   if (strlen($oldpw) != strlen($secret)) {
55)     system_failure('Interner Fehler: Passwörter sind nicht gleich lang');
56)   }
57)   $code = '';
58)   for ($i = 0 ; $i != strlen($oldpw) ; $i++) {
59)     $code .= chr( ord($oldpw[$i]) ^ ord($secret[$i]) );
60)   }
61)   $code = base64_encode($code);
62)   DEBUG(array($oldpw, $newpw, $code));
63) 
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

64)   $uid = (int) $_SESSION['userinfo']['uid'];
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

65) 
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

66)   db_query("REPLACE INTO mail.webmail_googleauth (useraccount, email, webmailpass) VALUES ({$uid}, '{$username}', '{$code}')");
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

67) }
68) 
69) 
70) function decode_webmail_password($crypted, $webmailpw)   
71) {
72)   $crypted = base64_decode($crypted);
73)   $secret = $webmailpw;
74)   if (strlen($crypted) > strlen($webmailpw)) {
75)     $secret = str_pad($webmailpw, strlen($crypted), $webmailpw);
76)   }
77)   if (strlen($crypted) < strlen($webmailpw)) {
78)     $webmailpw = substr($webmailpw, 0, strlen($crypted));
79)   }
80)   $clear = '';
81)   for ($i = 0 ; $i != strlen($crypted) ; $i++) {
82)     $clear .= chr( ord($crypted[$i]) ^ ord($secret[$i]) );
83)   }
84)   DEBUG('decrypted: '.$clear);
85)   return $clear;
86) }
87) 
88) 
89) function check_webmail_password($username, $webmailpass)
90) {
91)   $username = mysql_real_escape_string($username);
92)   $result = db_query("SELECT webmailpass FROM mail.webmail_googleauth WHERE email='{$username}'");
93)   $tmp = mysql_fetch_assoc($result);
94)   
95)   $crypted = $tmp['webmailpass'];
96)     
97)   $clear = decode_webmail_password($crypted, $webmailpass);
98) 
99)   return validate_password($username, $clear);
100)   
101) }
102) 
103) 
104) function generate_secret($username)
105) {
106)   $username = mysql_real_escape_string($username);
107)   require_once('external/googleauthenticator/GoogleAuthenticator.php');
108)   $ga = new PHPGangsta_GoogleAuthenticator();
109)   
110)   $secret = $ga->createSecret();
111)   DEBUG('GA-Secret: '.$secret);
112)   DEBUG('QrCode: '.$ga->getQRCodeGoogleUrl('Blog', $secret));
113)   db_query("UPDATE mail.webmail_googleauth SET ga_secret='{$secret}' WHERE email='{$username}'");
114)   return $secret;
115) }
116) 
Bernd Wurst blacklist für eingegebene C...

Bernd Wurst authored 11 years ago

117) function check_locked($username) 
118) {
119)   $username = mysql_real_escape_string($username);
120)   $result = db_query("SELECT 1 FROM mail.webmail_googleauth WHERE unlock_timestamp IS NOT NULL and unlock_timestamp > NOW() AND email='{$username}'");
121)   return (mysql_num_rows($result) > 0);
122) }
123) 
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

124) function check_googleauth($username, $code) {
Bernd Wurst blacklist für eingegebene C...

Bernd Wurst authored 11 years ago

125)   if (check_blacklist($username, $code)) {
126)     DEBUG('Replay-Attack');
127)     return false;
128)   }
129) 
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

130)   $username = mysql_real_escape_string($username);
131) 
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

132)   $result = db_query("SELECT ga_secret, failures FROM mail.webmail_googleauth WHERE email='{$username}' AND (unlock_timestamp IS NULL OR unlock_timestamp <= NOW())");
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

133)   $tmp = mysql_fetch_assoc($result);
134)   $secret = $tmp['ga_secret'];
135) 
136)   require_once('external/googleauthenticator/GoogleAuthenticator.php');
137)   $ga = new PHPGangsta_GoogleAuthenticator();
138)   
139)   $checkResult = $ga->verifyCode($secret, $code, 2);    // 2 = 2*30sec clock tolerance
140)   if ($checkResult) {
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

141)     db_query("UPDATE mail.webmail_googleauth SET failures = 0, unlock_timestamp=NULL WHERE email='{$username}'");
Bernd Wurst blacklist für eingegebene C...

Bernd Wurst authored 11 years ago

142)     blacklist_token($username, $code);
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

143)     DEBUG('OK');
144)   } else {
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

145)     if ($tmp['failures'] > 0 && $tmp['failures'] % 5 == 0) {
146)       db_query("UPDATE mail.webmail_googleauth SET failures = failures+1, unlock_timestamp = NOW() + INTERVAL 5 MINUTE WHERE email='{$username}'");
147)     } else {
148)       db_query("UPDATE mail.webmail_googleauth SET failures = failures+1 WHERE email='{$username}'");
149)     }
150)     
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

151)     DEBUG('FAILED');
152)   }
153)   return $checkResult;
154) 
155) }
156) 
157) function generate_qrcode_image($secret) {
158)   $url = 'otpauth://totp/Webmail?secret='.$secret;
159)   
160)   $descriptorspec = array(
161)     0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
162)     1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
163)     2 => array("pipe", "w") 
164)   );
165) 
166)   $process = proc_open('qrencode -t PNG -s 5 -o -', $descriptorspec, $pipes);
167) 
168)   if (is_resource($process)) {
169)     // $pipes sieht nun so aus:
170)     // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
171)     // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
172) 
173)     fwrite($pipes[0], $url);
174)     fclose($pipes[0]);
175) 
176)     $pngdata = stream_get_contents($pipes[1]);
177)     fclose($pipes[1]);
178) 
179)     // Es ist wichtig, dass Sie alle Pipes schließen bevor Sie
180)     // proc_close aufrufen, um Deadlocks zu vermeiden
181)     $return_value = proc_close($process);
182)   
183)     return $pngdata;
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

184)   } else {
185)     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.');
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

186)   }
187)   
188)   
189) }
190) 
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

191) function accountname($id) 
192) {
193)   $id = (int) $id;
194)   $uid = (int) $_SESSION['userinfo']['uid'];
195)   $result = db_query("SELECT email FROM mail.webmail_googleauth WHERE id={$id} AND useraccount={$uid}");
196)   if ($tmp = mysql_fetch_assoc($result)) {
197)     return $tmp['email'];
198)   }
199) }
200) 
201) 
202) function delete_googleauth($id) 
203) {
204)   $id = (int) $id;
205)   $uid = (int) $_SESSION['userinfo']['uid'];
206)   
207)   db_query("DELETE FROM mail.webmail_googleauth WHERE id={$id} AND useraccount={$uid}");
208) }
209)