f588b9c24fe58c388a2bda3e9c4b46e3404d8d39
bernd webinterface => /webinterface

bernd authored 17 years ago

1) <?php
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

2) /*
3) This file belongs to the Webinterface of schokokeks.org Hosting
4) 
Bernd Wurst Updated copyright notice (2...

Bernd Wurst authored 11 years ago

5) Written 2008-2013 by schokokeks.org Hosting, namely
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

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) */
bernd webinterface => /webinterface

bernd authored 17 years ago

16) 
bernd Logging aktiviert

bernd authored 16 years ago

17) require_once('inc/base.php');
bernd webinterface => /webinterface

bernd authored 17 years ago

18) require_once('inc/debug.php');
19) require_once('inc/error.php');
20) 
21) require_once('inc/db_connect.php');
22) 
23) define('ROLE_ANONYMOUS', 0);
bernd Auch mailaccounts können si...

bernd authored 16 years ago

24) define('ROLE_MAILACCOUNT', 1);
bernd VMail-accounts können sich...

bernd authored 16 years ago

25) define('ROLE_VMAIL_ACCOUNT', 2);
26) define('ROLE_SYSTEMUSER', 4);
27) define('ROLE_CUSTOMER', 8);
28) define('ROLE_SYSADMIN', 16);
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

29) define('ROLE_SUBUSER', 32);
bernd webinterface => /webinterface

bernd authored 17 years ago

30) 
31) 
32) // Gibt die Rolle aus, wenn das Passwort stimmt
33) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

34) function find_role($login, $password, $i_am_admin = False)
bernd webinterface => /webinterface

bernd authored 17 years ago

35) {
36)   $login = mysql_real_escape_string($login);
37)   // Domain-Admin?  <not implemented>
38)   // System-User?
39)   $uid = (int) $login;
40)   if ($uid == 0)
41)     $uid = 'NULL';
Bernd Wurst Beachte Groß- und Kleinshre...

Bernd Wurst authored 11 years ago

42)   $result = db_query("SELECT username, passwort AS password, kundenaccount AS `primary`, status, ((SELECT acc.uid FROM system.v_useraccounts AS acc LEFT JOIN system.gruppenzugehoerigkeit USING (uid) LEFT JOIN system.gruppen AS g ON (g.gid=gruppenzugehoerigkeit.gid) WHERE g.name='admin' AND acc.uid=u.uid) IS NOT NULL) AS admin FROM system.v_useraccounts AS u LEFT JOIN system.passwoerter USING(uid) WHERE u.uid={$uid} OR username='{$login}' LIMIT 1;");
bernd webinterface => /webinterface

bernd authored 17 years ago

43)   if (@mysql_num_rows($result) > 0)
44)   {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

45)     $entry = mysql_fetch_object($result);
Bernd Wurst Fixed: Login via UID nicht...

Bernd Wurst authored 11 years ago

46)     if (strcasecmp($entry->username, $login) == 0 && $entry->username != $login) {
Bernd Wurst Beachte Groß- und Kleinshre...

Bernd Wurst authored 11 years ago

47)       // MySQL matched (warum auch immer) ohne Beachtung der Schreibweise. Wir wollen aber case-sensitive sein.
48)       logger(LOG_WARNING, "session/checkuser", "login", "denying login to wrong cased username »{$login}«.");
49)       warning('Beachten Sie bei der Eingabe Ihrer Zugangsdaten bitte die Groß- und Kleinschreibung.');
50)       return NULL;  
51)     }
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

52)     $db_password = $entry->password;
bernd webinterface => /webinterface

bernd authored 17 years ago

53)     $hash = crypt($password, $db_password);
bernd Sperre Login für gesperrte...

bernd authored 12 years ago

54)     if (($entry->status == 0 && $hash == $db_password) || $i_am_admin)
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

55)     {
56)       $role = ROLE_SYSTEMUSER;
57)       if ($entry->primary)
58)         $role = $role | ROLE_CUSTOMER;
59)       if ($entry->admin)
60)         $role = $role | ROLE_SYSADMIN;
bernd Logger mit Logleveln

bernd authored 14 years ago

61)       logger(LOG_INFO, "session/checkuser", "login", "logged in systemuser »{$login}«.");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

62)       return $role;
63)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

64)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing useraccount »{$login}«.");
bernd Mehr logging

bernd authored 15 years ago

65)   } else {
bernd Logger mit Logleveln

bernd authored 14 years ago

66)     logger(LOG_WARNING, "session/checkuser", "login", "did not find useraccount »{$login}«. trying other roles...");
bernd webinterface => /webinterface

bernd authored 17 years ago

67)   }
68) 
69)   // Customer?
70)   $customerno = (int) $login;
71)   $pass = sha1($password);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

72)   $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id={$customerno} AND passwort='{$pass}';");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

73)   if ($i_am_admin)
74)     $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id={$customerno}");
bernd webinterface => /webinterface

bernd authored 17 years ago

75)   if (@mysql_num_rows($result) > 0)
76)   {
77)     return ROLE_CUSTOMER;
78)   }
79) 
Bernd Wurst Login am Webiterface mit Go...

Bernd Wurst authored 11 years ago

80)   // Sub-User
81) 
82)   $result = db_query("SELECT password FROM system.subusers WHERE username='{$login}'");
83)   if (@mysql_num_rows($result) > 0)
84)   {
85)     $entry = mysql_fetch_object($result);
86)     $db_password = $entry->password;
87)     // SHA1 für alte Subuser (kaylee), SHA256 für neue Subuser
88)     if (hash("sha1", $password) == $db_password || hash("sha256", $password) == $db_password || $i_am_admin)
89)     {
90)       logger(LOG_INFO, "session/checkuser", "login", "logged in virtual subuser »{$login}«.");
91)       return ROLE_SUBUSER;
92)     }
93)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing subuser »{$login}«.");
94)   }
95) 
96) 
bernd Auch mailaccounts können si...

bernd authored 16 years ago

97)   // Mail-Account
98)   $account = $login;
99)   if (! strstr($account, '@')) {
bernd Mehr config-optionen und co...

bernd authored 14 years ago

100)     $account .= '@'.config('masterdomain');
bernd Auch mailaccounts können si...

bernd authored 16 years ago

101)   }
Bernd Wurst Umbenennung Google-Auth nac...

Bernd Wurst authored 11 years ago

102)   if (!$i_am_admin && have_module('webmailtotp')) {
103)     require_once('modules/webmailtotp/include/totp.php');
104)     if (account_has_totp($account)) {
Bernd Wurst Login am Webiterface mit Go...

Bernd Wurst authored 11 years ago

105)       if (check_webmail_password($account, $password)) {
Bernd Wurst Umbenennung Google-Auth nac...

Bernd Wurst authored 11 years ago

106)         $_SESSION['totp_username'] = $account;
107)         $_SESSION['totp'] = True;
108)         show_page('webmailtotp-login');
Bernd Wurst Login am Webiterface mit Go...

Bernd Wurst authored 11 years ago

109)         die();
110)       } else {
111)         return NULL;
112)       }
113)     }
114)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

115)   $result = db_query("SELECT cryptpass FROM mail.courier_mailaccounts WHERE account='{$account}' LIMIT 1;");
116)   if (@mysql_num_rows($result) > 0)
117)   {
118)     $entry = mysql_fetch_object($result);
119)     $db_password = $entry->cryptpass;
120)     $hash = crypt($password, $db_password);
121)     if ($hash == $db_password || $i_am_admin)
122)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

123)       logger(LOG_INFO, "session/checkuser", "login", "logged in e-mail-account »{$account}«.");
bernd Auch mailaccounts können si...

bernd authored 16 years ago

124)       return ROLE_MAILACCOUNT;
125)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

126)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing e-mail-account »{$account}«.");
bernd Auch mailaccounts können si...

bernd authored 16 years ago

127)   }
128)   
bernd VMail-accounts können sich...

bernd authored 16 years ago

129)   // virtueller Mail-Account
130)   $account = $login;
131)   $result = db_query("SELECT cryptpass FROM mail.courier_virtual_accounts WHERE account='{$account}' LIMIT 1;");
132)   if (@mysql_num_rows($result) > 0)
133)   {
134)     $entry = mysql_fetch_object($result);
135)     $db_password = $entry->cryptpass;
136)     $hash = crypt($password, $db_password);
137)     if ($hash == $db_password || $i_am_admin)
138)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

139)       logger(LOG_INFO, "session/checkuser", "login", "logged in virtual e-mail-account »{$account}«.");
bernd VMail-accounts können sich...

bernd authored 16 years ago

140)       return ROLE_VMAIL_ACCOUNT;
141)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

142)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing virtual e-mail-account »{$account}«.");
bernd VMail-accounts können sich...

bernd authored 16 years ago

143)   }
144)   
bernd Auch mailaccounts können si...

bernd authored 16 years ago

145) 
146) 
bernd webinterface => /webinterface

bernd authored 17 years ago

147)   // Nothing?
148)   return NULL;
149) }
150) 
151) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

152) function get_customer_info($customer)
bernd webinterface => /webinterface

bernd authored 17 years ago

153) {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

154)   if (! $_SESSION['role'] & ROLE_CUSTOMER)
155)     return array();
bernd webinterface => /webinterface

bernd authored 17 years ago

156)   $ret = array();
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

157)   $customerno = (int) $customer;
158)   if ($customerno != 0)
159)   {
160)     DEBUG('Looking up customerinfo for customer no. '.$customerno);
bernd Warnung gefixed

bernd authored 13 years ago

161)     $result = db_query("SELECT id, anrede, firma, CONCAT_WS(' ', vorname, nachname) AS name, COALESCE(email,email_rechnung,email_extern) AS email FROM kundendaten.kunden WHERE id={$customerno} LIMIT 1;");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

162)   }
163)   else
164)   {
165)     $username = mysql_real_escape_string($customer);
166)     DEBUG('looking up customer info for username '.$username);
bernd Warnung gefixed

bernd authored 13 years ago

167)     $result = db_query("SELECT id, anrede, firma, CONCAT_WS(' ', vorname, nachname) AS name, COALESCE(email,email_rechnung,email_extern) AS email FROM kundendaten.kunden AS k JOIN system.v_useraccounts AS u ON (u.kunde=k.id) WHERE u.username='{$username}'");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

168)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

169)   if (@mysql_num_rows($result) == 0)
170)     system_failure("Konnte Kundendaten nicht auslesen!");
bernd Warnung gefixed

bernd authored 13 years ago

171)   $data = mysql_fetch_assoc($result);
172)   DEBUG($data);
173)   $ret['customerno'] = $data['id'];
174)   $ret['title'] = $data['anrede'];
175)   $ret['company'] = $data['firma'];
176)   $ret['name'] = $data['name'];
177)   $ret['email'] = $data['email'];
bernd webinterface => /webinterface

bernd authored 17 years ago

178)   
179)   return $ret;
180) }
181) 
182) 
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

183) function get_subuser_info($username)
184) {
185)   $result = db_query("SELECT uid, modules FROM system.subusers WHERE username='{$username}'");
186)   if (mysql_num_rows($result) < 1)
187)   {
188)     logger(LOG_ERR, "session/checkuser", "login", "error reading subuser's data: »{$username}«");
189)     system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
190)   }
191)   $data = mysql_fetch_assoc($result);
192)   $userinfo = get_user_info($data['uid']);
193)   $userinfo['modules'] = $data['modules'];
194)   return $userinfo;
195) }
196) 
197) 
bernd webinterface => /webinterface

bernd authored 17 years ago

198) function get_user_info($username)
199) {
200)   $username = mysql_real_escape_string($username);
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

201)   $result = db_query("SELECT kunde AS customerno, username, uid, homedir, name, server
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

202)                       FROM system.v_useraccounts WHERE username='{$username}' OR uid='{$username}' LIMIT 1");
bernd webinterface => /webinterface

bernd authored 17 years ago

203)   if (mysql_num_rows($result) < 1)
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

204)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

205)     logger(LOG_ERR, "session/checkuser", "login", "error reading user's data: »{$username}«");
bernd webinterface => /webinterface

bernd authored 17 years ago

206)     system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

207)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

208)   $val = @mysql_fetch_object($result);
209)   return array(
210)           'username'      => $val->username,
211)           'customerno'    => $val->customerno,
212)           'uid'           => $val->uid,
213)           'homedir'       => $val->homedir,
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

214)           'server'        => $val->server,
bernd webinterface => /webinterface

bernd authored 17 years ago

215)           'name'          => $val->name,
216)           );
217) }
218) 
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

219) function set_customer_verified($customerno)
220) {
221)   $customerno = (int) $customerno;
222)   db_query("UPDATE kundendaten.kunden SET status=0 WHERE id={$customerno};");
bernd Logger mit Logleveln

bernd authored 14 years ago

223)   logger(LOG_INFO, "session/checkuser", "register", "set customer's status to 0.");
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

224) }
225) 
226) function set_customer_lastlogin($customerno)
227) {
228)   $customerno = (int) $customerno;
229)   db_query("UPDATE kundendaten.kunden SET lastlogin=NOW() WHERE id={$customerno};");
230) }
231) 
bernd webinterface => /webinterface

bernd authored 17 years ago

232) function set_customer_password($customerno, $newpass)
233) {
234)   $customerno = (int) $customerno;
235)   $newpass = sha1($newpass);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

236)   db_query("UPDATE kundendaten.kunden SET passwort='$newpass' WHERE id='".$customerno."' LIMIT 1");
bernd Logger mit Logleveln

bernd authored 14 years ago

237)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed customer's password.");
bernd Passwort-Ändern geht jetzt...

bernd authored 13 years ago

238) }
239) 
240) function set_subuser_password($subuser, $newpass)
241) {
242)   $subuser = mysql_real_escape_string($subuser);
243)   $uid = (int) $_SESSION['userinfo']['uid'];
244)   $newpass = sha1($newpass);
245)   db_query("UPDATE system.subusers SET password='$newpass' WHERE username='{$subuser}' AND uid={$uid}");
246)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed subuser's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

247) }
248) 
249) function set_systemuser_password($uid, $newpass)
250) {
251)   $uid = (int) $uid;
252)   require_once('inc/base.php');
bernd Benutzer SHA512 wenn möglich

bernd authored 13 years ago

253)   if (defined("CRYPT_SHA512") && CRYPT_SHA512 == 1)
254)   {
255)     $rounds = rand(1000, 5000);
256)     $salt = "rounds=".$rounds."$".random_string(8);
257)     $newpass = crypt($newpass, "\$6\${$salt}\$");
258)   }
259)   else
260)   {
261)     $salt = random_string(8);
262)     $newpass = crypt($newpass, "\$1\${$salt}\$");
263)   }
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

264)   db_query("UPDATE system.passwoerter SET passwort='$newpass' WHERE uid='".$uid."' LIMIT 1");
bernd Logger mit Logleveln

bernd authored 14 years ago

265)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed user's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

266) }
267) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

268) 
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

269) function user_for_mailaccount($account) 
270) {
271)   $result = db_query("SELECT uid FROM mail.courier_mailaccounts WHERE account='{$account}' LIMIT 1;");
272)   if (mysql_num_rows($result) != 1) {
273)     system_failure('Diese Adresse ist herrenlos?!');
274)   }
275)   $tmp = mysql_fetch_assoc($result);
276)   return $tmp['uid'];
277) }
278) 
279) function user_for_vmail_account($account)
280) {
281)   $result = db_query("SELECT useraccount FROM mail.v_vmail_accounts WHERE CONCAT_WS('@', local, domainname)='{$account}' LIMIT 1;");
282)   if (mysql_num_rows($result) != 1) {
283)     system_failure('Diese Adresse ist herrenlos?!');
284)   }
285)   $tmp = mysql_fetch_assoc($result);
286)   return $tmp['useraccount'];
287) }
288) 
289) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

290) function setup_session($role, $useridentity)
291) {
292)   session_regenerate_id();
293)   $_SESSION['role'] = $role;
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

294)   if ($role & ROLE_SUBUSER)
295)   {
296)     DEBUG("We are a sub-user");
297)     $info = get_subuser_info($useridentity);
298)     $_SESSION['userinfo'] = $info;
299)     $_SESSION['subuser'] = $useridentity;
bernd Berechtigungen für Subuser...

bernd authored 12 years ago

300)     $customer = get_customer_info($_SESSION['userinfo']['username']);
301)     $_SESSION['customerinfo'] = $customer;
302)     $_SESSION['role'] = ROLE_SYSTEMUSER | ROLE_CUSTOMER | ROLE_SUBUSER;
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

303)     $_SESSION['restrict_modules'] = explode(',', $info['modules']);
304)     logger(LOG_INFO, "session/start", "login", "logged in user »{$info['username']}«");
305)   }
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

306)   if ($role & ROLE_SYSTEMUSER)
307)   {
308)     DEBUG("We are system user");
309)     $info = get_user_info($useridentity);
310)     $_SESSION['userinfo'] = $info;
bernd Logger mit Logleveln

bernd authored 14 years ago

311)     logger(LOG_INFO, "session/start", "login", "logged in user »{$info['username']}«");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

312)     $useridentity = $info['customerno'];
313)   }
314)   if ($role & ROLE_CUSTOMER)
315)   {
316)     $info = get_customer_info($useridentity);
317)     $_SESSION['customerinfo'] = $info;
bernd Setze lastlogin nur bei Nic...

bernd authored 13 years ago

318)     if (!isset($_SESSION['admin_user'])) {
319)       set_customer_lastlogin($info['customerno']);
320)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

321)     logger(LOG_INFO, "session/start", "login", "logged in customer no »{$info['customerno']}«");
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

322)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

323)   if ($role & ROLE_MAILACCOUNT)
324)   {
325)     $id = $useridentity;
326)     if (! strstr($id, '@'))
bernd Mehr config-optionen und co...

bernd authored 14 years ago

327)       $id .= '@'.config('masterdomain');
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

328)     $uid = user_for_mailaccount($id);
bernd Auch mailaccounts können si...

bernd authored 16 years ago

329)     $_SESSION['mailaccount'] = $id;
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

330)     $_SESSION['userinfo'] = get_user_info($uid);
bernd Auch mailaccounts können si...

bernd authored 16 years ago

331)     DEBUG("We are mailaccount: {$_SESSION['mailaccount']}");
332)   }
bernd VMail-accounts können sich...

bernd authored 16 years ago

333)   if ($role & ROLE_VMAIL_ACCOUNT)
334)   {
335)     $id = $useridentity;
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

336)     $uid = user_for_vmail_account($id);
bernd VMail-accounts können sich...

bernd authored 16 years ago

337)     $_SESSION['mailaccount'] = $id;
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

338)     $_SESSION['userinfo'] = get_user_info($uid);
bernd VMail-accounts können sich...

bernd authored 16 years ago

339)     DEBUG("We are virtual mailaccount: {$_SESSION['mailaccount']}");
340)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

341) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

342) }
343)