c4acdd91b85bc9f88bc98833456e2f7128737b80
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) 
Bernd Wurst Attribution für die externe...

Bernd Wurst authored 11 years ago

89) function get_imap_password($username, $webmailpass) {
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

90)   $username = mysql_real_escape_string($username);
91)   $result = db_query("SELECT webmailpass FROM mail.webmail_googleauth WHERE email='{$username}'");
92)   $tmp = mysql_fetch_assoc($result);
93)   
94)   $crypted = $tmp['webmailpass'];
95)     
96)   $clear = decode_webmail_password($crypted, $webmailpass);
Bernd Wurst Attribution für die externe...

Bernd Wurst authored 11 years ago

97)   return $clear;
98) }
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

99) 
Bernd Wurst Attribution für die externe...

Bernd Wurst authored 11 years ago

100) 
101) function check_webmail_password($username, $webmailpass)
102) {
103)   $clear = get_imap_password($username, $webmailpass);
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

104)   return validate_password($username, $clear);
105) }
106) 
107) 
108) function generate_secret($username)
109) {
110)   $username = mysql_real_escape_string($username);
111)   require_once('external/googleauthenticator/GoogleAuthenticator.php');
112)   $ga = new PHPGangsta_GoogleAuthenticator();
113)   
114)   $secret = $ga->createSecret();
115)   DEBUG('GA-Secret: '.$secret);
116)   DEBUG('QrCode: '.$ga->getQRCodeGoogleUrl('Blog', $secret));
117)   db_query("UPDATE mail.webmail_googleauth SET ga_secret='{$secret}' WHERE email='{$username}'");
118)   return $secret;
119) }
120) 
Bernd Wurst blacklist für eingegebene C...

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

129)   if (check_blacklist($username, $code)) {
130)     DEBUG('Replay-Attack');
131)     return false;
132)   }
133) 
Bernd Wurst Google-Auth-Token setzen un...

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

136)   $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

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

Bernd Wurst authored 11 years ago

145)     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

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

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

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

Bernd Wurst authored 11 years ago

188)   } else {
189)     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

190)   }
191)   
192)   
193) }
194) 
Bernd Wurst löschen der OTP-Authentifiz...

Bernd Wurst authored 11 years ago

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