9086c9ad77db90633e0629e9e86a88366265ded0
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 Copyright year update

Bernd Wurst authored 6 years ago

5) Written 2008-2018 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) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

11) You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
Bernd Wurst Added license tags for CC0,...

Bernd Wurst authored 12 years ago

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 17 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) 
22) define('ROLE_ANONYMOUS', 0);
bernd Auch mailaccounts können si...

bernd authored 17 years ago

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

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 17 years ago

29) 
30) 
31) // Gibt die Rolle aus, wenn das Passwort stimmt
32) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

33) function find_role($login, $password, $i_am_admin = false)
bernd webinterface => /webinterface

bernd authored 17 years ago

34) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

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

Bernd Wurst authored 12 years ago

40)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

41)     $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;", array(":uid" => $uid, ":login" => $login));
42)     if (@$result->rowCount() > 0) {
43)         $entry = $result->fetch(PDO::FETCH_OBJ);
44)         if (strcasecmp($entry->username, $login) == 0 && $entry->username != $login) {
45)             // MySQL matched (warum auch immer) ohne Beachtung der Schreibweise. Wir wollen aber case-sensitive sein.
46)             logger(LOG_WARNING, "session/checkuser", "login", "denying login to wrong cased username »{$login}«.");
47)             warning('Beachten Sie bei der Eingabe Ihrer Zugangsdaten bitte die Groß- und Kleinschreibung.');
48)             return null;
49)         }
50)         $db_password = $entry->password;
51)         $hash = crypt($password, $db_password);
52)         if (($entry->status == 0 && $hash == $db_password) || $i_am_admin) {
53)             $role = ROLE_SYSTEMUSER;
54)             if ($entry->primary) {
55)                 $role = $role | ROLE_CUSTOMER;
56)             }
57)             if ($entry->admin) {
58)                 $role = $role | ROLE_SYSADMIN;
59)             }
60)             logger(LOG_INFO, "session/checkuser", "login", "logged in systemuser »{$login}«.");
61)             return $role;
62)         }
63)         logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing useraccount »{$login}«.");
64)     } else {
65)         logger(LOG_WARNING, "session/checkuser", "login", "did not find useraccount »{$login}«. trying other roles...");
bernd * Initialisierung der Sessi...

bernd authored 17 years ago

66)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

67) 
68)     // Customer?
69)     $customerno = (int) $login;
70)     $pass = sha1($password);
71)     $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id=:customerno AND passwort=:pass", array(":customerno" => $customerno, ":pass" => $pass));
72)     if ($i_am_admin) {
73)         $result = db_query("SELECT passwort AS password FROM kundendaten.kunden WHERE status=0 AND id=?", array($customerno));
74)     }
75)     if (@$result->rowCount() > 0) {
76)         return ROLE_CUSTOMER;
77)     }
78) 
79)     // Sub-User
80) 
81)     $result = db_query("SELECT password FROM system.subusers WHERE username=?", array($login));
82)     if (@$result->rowCount() > 0) {
83)         $entry = $result->fetch(PDO::FETCH_OBJ);
84)         $db_password = $entry->password;
85)         // SHA1 für alte Subuser (kaylee), SHA256 für neue Subuser
86)         if (hash("sha1", $password) == $db_password || hash("sha256", $password) == $db_password || $i_am_admin) {
87)             logger(LOG_INFO, "session/checkuser", "login", "logged in virtual subuser »{$login}«.");
88)             return ROLE_SUBUSER;
89)         }
90)         logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing subuser »{$login}«.");
91)     }
92) 
93) 
94)     // Mail-Account
95)     $account = $login;
96)     if (! strstr($account, '@')) {
97)         $account .= '@'.config('masterdomain');
Bernd Wurst Login am Webiterface mit Go...

Bernd Wurst authored 12 years ago

98)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

99)     if (!$i_am_admin && have_module('webmailtotp')) {
100)         require_once('modules/webmailtotp/include/totp.php');
101)         if (account_has_totp($account)) {
102)             if (check_webmail_password($account, $password)) {
103)                 $_SESSION['totp_username'] = $account;
104)                 $_SESSION['totp'] = true;
105)                 show_page('webmailtotp-login');
106)                 die();
107)             } else {
108)                 return null;
109)             }
110)         }
Bernd Wurst Login am Webiterface mit Go...

Bernd Wurst authored 12 years ago

111)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

112)     $result = db_query("SELECT cryptpass FROM mail.courier_mailaccounts WHERE account=?", array($account));
113)     if (@$result->rowCount() > 0) {
114)         $entry = $result->fetch(PDO::FETCH_OBJ);
115)         $db_password = $entry->cryptpass;
116)         $hash = crypt($password, $db_password);
117)         if ($hash == $db_password || $i_am_admin) {
118)             logger(LOG_INFO, "session/checkuser", "login", "logged in e-mail-account »{$account}«.");
119)             return ROLE_MAILACCOUNT;
120)         }
121)         logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing e-mail-account »{$account}«.");
bernd Auch mailaccounts können si...

bernd authored 17 years ago

122)     }
Hanno remove whitespace in empty...

Hanno authored 6 years ago

123) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

124)     // virtueller Mail-Account
125)     $account = $login;
126)     $result = db_query("SELECT cryptpass FROM mail.courier_virtual_accounts WHERE account=?", array($account));
127)     if (@$result->rowCount() > 0) {
128)         $entry = $result->fetch(PDO::FETCH_OBJ);
129)         $db_password = $entry->cryptpass;
130)         $hash = crypt($password, $db_password);
131)         if ($hash == $db_password || $i_am_admin) {
132)             logger(LOG_INFO, "session/checkuser", "login", "logged in virtual e-mail-account »{$account}«.");
133)             return ROLE_VMAIL_ACCOUNT;
134)         }
135)         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

136)     }
Hanno remove whitespace in empty...

Hanno authored 6 years ago

137) 
bernd Auch mailaccounts können si...

bernd authored 17 years ago

138) 
139) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

140)     // Nothing?
141)     return null;
bernd webinterface => /webinterface

bernd authored 17 years ago

142) }
143) 
144) 
bernd * Initialisierung der Sessi...

bernd authored 17 years ago

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

bernd authored 17 years ago

146) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

147)     if (! $_SESSION['role'] & ROLE_CUSTOMER) {
148)         return array();
149)     }
150)     $ret = array();
151)     $customerno = (int) $customer;
152)     if ($customerno != 0) {
153)         DEBUG('Looking up customerinfo for customer no. '.$customerno);
154)         $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=?", array($customerno));
155)     } else {
156)         $username = $customer;
157)         DEBUG('looking up customer info for username '.$username);
158)         $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=?", array($username));
159)     }
160)     if (@$result->rowCount() == 0) {
161)         system_failure("Konnte Kundendaten nicht auslesen!");
162)     }
163)     $data = $result->fetch();
164)     DEBUG($data);
165)     $ret['customerno'] = $data['id'];
166)     $ret['title'] = $data['anrede'];
167)     $ret['company'] = $data['firma'];
168)     $ret['name'] = $data['name'];
169)     $ret['email'] = $data['email'];
Hanno remove whitespace in empty...

Hanno authored 6 years ago

170) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

171)     return $ret;
bernd webinterface => /webinterface

bernd authored 17 years ago

172) }
173) 
174) 
bernd Erlaube subusers, die nur Z...

bernd authored 14 years ago

175) function get_subuser_info($username)
176) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

177)     $result = db_query("SELECT uid, modules FROM system.subusers WHERE username=?", array($username));
178)     if ($result->rowCount() < 1) {
179)         logger(LOG_ERR, "session/checkuser", "login", "error reading subuser's data: »{$username}«");
180)         system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
181)     }
182)     $data = $result->fetch();
183)     $userinfo = get_user_info($data['uid']);
184)     $userinfo['modules'] = $data['modules'];
185)     return $userinfo;
bernd Erlaube subusers, die nur Z...

bernd authored 14 years ago

186) }
187) 
188) 
bernd webinterface => /webinterface

bernd authored 17 years ago

189) function get_user_info($username)
190) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

191)     $result = db_query("SELECT kunde AS customerno, username, uid, homedir, name, server
Bernd Wurst * Weitere Module auf prepar...

Bernd Wurst authored 10 years ago

192)                       FROM system.v_useraccounts WHERE username=:username OR uid=:username", array(":username" => $username));
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

193)     if ($result->rowCount() < 1) {
194)         logger(LOG_ERR, "session/checkuser", "login", "error reading user's data: »{$username}«");
195)         system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
196)     }
197)     $val = @$result->fetch(PDO::FETCH_OBJ);
198)     return array(
bernd webinterface => /webinterface

bernd authored 17 years ago

199)           'username'      => $val->username,
200)           'customerno'    => $val->customerno,
201)           'uid'           => $val->uid,
202)           'homedir'       => $val->homedir,
bernd IPv6-Option nur anzeigen we...

bernd authored 14 years ago

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

bernd authored 17 years ago

204)           'name'          => $val->name,
205)           );
206) }
207) 
bernd Kunden-Status wird benutzt...

bernd authored 17 years ago

208) function set_customer_verified($customerno)
209) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

210)     $customerno = (int) $customerno;
211)     db_query("UPDATE kundendaten.kunden SET status=0 WHERE id=?", array($customerno));
212)     logger(LOG_INFO, "session/checkuser", "register", "set customer's status to 0.");
bernd Kunden-Status wird benutzt...

bernd authored 17 years ago

213) }
214) 
215) function set_customer_lastlogin($customerno)
216) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

217)     $customerno = (int) $customerno;
218)     db_query("UPDATE kundendaten.kunden SET lastlogin=NOW() WHERE id=?", array($customerno));
bernd Kunden-Status wird benutzt...

bernd authored 17 years ago

219) }
220) 
bernd webinterface => /webinterface

bernd authored 17 years ago

221) function set_customer_password($customerno, $newpass)
222) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

223)     $customerno = (int) $customerno;
224)     $newpass = sha1($newpass);
225)     db_query("UPDATE kundendaten.kunden SET passwort=:newpass WHERE id=:customerno", array(":newpass" => $newpass, ":customerno" => $customerno));
226)     logger(LOG_INFO, "session/checkuser", "pwchange", "changed customer's password.");
bernd Passwort-Ändern geht jetzt...

bernd authored 14 years ago

227) }
228) 
229) function set_subuser_password($subuser, $newpass)
230) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

231)     $args = array(":subuser" => $subuser,
Bernd Wurst * Weitere Module auf prepar...

Bernd Wurst authored 10 years ago

232)                 ":uid" => (int) $_SESSION['userinfo']['uid'],
233)                 ":newpass" => sha1($newpass));
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

234)     db_query("UPDATE system.subusers SET password=:newpass WHERE username=:subuser AND uid=:uid", $args);
235)     logger(LOG_INFO, "session/checkuser", "pwchange", "changed subuser's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

236) }
237) 
238) function set_systemuser_password($uid, $newpass)
239) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

240)     $uid = (int) $uid;
241)     require_once('inc/base.php');
242)     if (defined("CRYPT_SHA512") && CRYPT_SHA512 == 1) {
243)         $rounds = rand(1000, 5000);
244)         $salt = "rounds=".$rounds."$".random_string(8);
245)         $newpass = crypt($newpass, "\$6\${$salt}\$");
246)     } else {
247)         $salt = random_string(8);
248)         $newpass = crypt($newpass, "\$1\${$salt}\$");
249)     }
250)     db_query("UPDATE system.passwoerter SET passwort=:newpass WHERE uid=:uid", array(":newpass" => $newpass, ":uid" => $uid));
251)     logger(LOG_INFO, "session/checkuser", "pwchange", "changed user's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

252) }
253) 
bernd * Initialisierung der Sessi...

bernd authored 17 years ago

254) 
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

255) function user_for_mailaccount($account)
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

256) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

257)     $result = db_query("SELECT uid FROM mail.courier_mailaccounts WHERE account=?", array($account));
258)     if ($result->rowCount() != 1) {
259)         system_failure('Diese Adresse ist herrenlos?!');
260)     }
261)     $tmp = $result->fetch();
262)     return $tmp['uid'];
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

263) }
264) 
265) function user_for_vmail_account($account)
266) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

267)     $result = db_query("SELECT useraccount FROM mail.v_vmail_accounts WHERE CONCAT_WS('@', local, domainname)=?", array($account));
268)     if ($result->rowCount() != 1) {
269)         system_failure('Diese Adresse ist herrenlos?!');
270)     }
271)     $tmp = $result->fetch();
272)     return $tmp['useraccount'];
Bernd Wurst Zeige User-Infos beim Login...

Bernd Wurst authored 11 years ago

273) }
274) 
275) 
bernd * Initialisierung der Sessi...

bernd authored 17 years ago

276) function setup_session($role, $useridentity)
277) {
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

278)     session_regenerate_id();
279)     $_SESSION['role'] = $role;
280)     if ($role & ROLE_SUBUSER) {
281)         DEBUG("We are a sub-user");
282)         $info = get_subuser_info($useridentity);
283)         $_SESSION['userinfo'] = $info;
284)         $_SESSION['restrict_modules'] = explode(',', $info['modules']);
285)         $_SESSION['role'] = ROLE_SYSTEMUSER | ROLE_SUBUSER;
286)         $_SESSION['subuser'] = $useridentity;
287)         $data = db_query("SELECT kundenaccount FROM system.useraccounts WHERE username=?", array($info['username']));
288)         if ($entry = $data->fetch()) {
289)             if ($entry['kundenaccount'] == 1) {
290)                 $customer = get_customer_info($_SESSION['userinfo']['username']);
291)                 $_SESSION['customerinfo'] = $customer;
292)                 $_SESSION['role'] = ROLE_SYSTEMUSER | ROLE_CUSTOMER | ROLE_SUBUSER;
293)             }
294)         }
295)         logger(LOG_INFO, "session/start", "login", "logged in user »{$info['username']}«");
Bernd Wurst Subuser sollen nicht automa...

Bernd Wurst authored 11 years ago

296)     }
Hanno Fix coding style with php-c...

Hanno authored 6 years ago

297)     if ($role & ROLE_SYSTEMUSER) {
298)         DEBUG("We are system user");
299)         $info = get_user_info($useridentity);
300)         $_SESSION['userinfo'] = $info;
301)         logger(LOG_INFO, "session/start", "login", "logged in user »{$info['username']}«");
302)         $useridentity = $info['customerno'];
303)     }
304)     if ($role & ROLE_CUSTOMER) {
305)         $info = get_customer_info($useridentity);
306)         $_SESSION['customerinfo'] = $info;
307)         if (!isset($_SESSION['admin_user'])) {
308)             set_customer_lastlogin($info['customerno']);
309)         }
310)         logger(LOG_INFO, "session/start", "login", "logged in customer no »{$info['customerno']}«");
311)     }
312)     if ($role & ROLE_MAILACCOUNT) {
313)         $id = $useridentity;
314)         if (! strstr($id, '@')) {
315)             $id .= '@'.config('masterdomain');
316)         }
317)         $uid = user_for_mailaccount($id);
318)         $_SESSION['mailaccount'] = $id;
319)         $_SESSION['userinfo'] = get_user_info($uid);
320)         DEBUG("We are mailaccount: {$_SESSION['mailaccount']}");
321)     }
322)     if ($role & ROLE_VMAIL_ACCOUNT) {
323)         $id = $useridentity;
324)         $uid = user_for_vmail_account($id);
325)         $_SESSION['mailaccount'] = $id;
326)         $_SESSION['userinfo'] = get_user_info($uid);
327)         DEBUG("We are virtual mailaccount: {$_SESSION['mailaccount']}");
bernd Setze lastlogin nur bei Nic...

bernd authored 13 years ago

328)     }