5e7e9f9a38210b434206ccf786d23f3de89cee29
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) 
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) */
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 Beachte Groß- und Kleinshre...

Bernd Wurst authored 11 years ago

46)     if ($entry->username != $login) {
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 Auch mailaccounts können si...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

84)   }
85)   $result = db_query("SELECT cryptpass FROM mail.courier_mailaccounts WHERE account='{$account}' LIMIT 1;");
86)   if (@mysql_num_rows($result) > 0)
87)   {
88)     $entry = mysql_fetch_object($result);
89)     $db_password = $entry->cryptpass;
90)     $hash = crypt($password, $db_password);
91)     if ($hash == $db_password || $i_am_admin)
92)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

94)       return ROLE_MAILACCOUNT;
95)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

97)   }
98)   
bernd VMail-accounts können sich...

bernd authored 16 years ago

99)   // virtueller Mail-Account
100)   $account = $login;
101)   $result = db_query("SELECT cryptpass FROM mail.courier_virtual_accounts WHERE account='{$account}' LIMIT 1;");
102)   if (@mysql_num_rows($result) > 0)
103)   {
104)     $entry = mysql_fetch_object($result);
105)     $db_password = $entry->cryptpass;
106)     $hash = crypt($password, $db_password);
107)     if ($hash == $db_password || $i_am_admin)
108)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

110)       return ROLE_VMAIL_ACCOUNT;
111)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

113)   }
114)   
bernd Auch mailaccounts können si...

bernd authored 16 years ago

115) 
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

116)   // Sub-User
117) 
bernd Cert-Login geht jetztauch m...

bernd authored 13 years ago

118)   $result = db_query("SELECT password FROM system.subusers WHERE username='{$login}'");
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

119)   if (@mysql_num_rows($result) > 0)
120)   {
bernd Cert-Login geht jetztauch m...

bernd authored 13 years ago

121)     $entry = mysql_fetch_object($result);
122)     $db_password = $entry->password;
Bernd Wurst Subusers-Modul in einer ers...

Bernd Wurst authored 12 years ago

123)     // SHA1 für alte Subuser (kaylee), SHA256 für neue Subuser
124)     if (hash("sha1", $password) == $db_password || hash("sha256", $password) == $db_password || $i_am_admin)
bernd Cert-Login geht jetztauch m...

bernd authored 13 years ago

125)     {
126)       logger(LOG_INFO, "session/checkuser", "login", "logged in virtual subuser »{$login}«.");
127)       return ROLE_SUBUSER;
128)     }
129)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing subuser »{$login}«.");
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

130)   }
131) 
bernd Auch mailaccounts können si...

bernd authored 16 years ago

132) 
bernd webinterface => /webinterface

bernd authored 17 years ago

133)   // Nothing?
134)   return NULL;
135) }
136) 
137) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

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

bernd authored 17 years ago

139) {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

143)   $customerno = (int) $customer;
144)   if ($customerno != 0)
145)   {
146)     DEBUG('Looking up customerinfo for customer no. '.$customerno);
bernd Warnung gefixed

bernd authored 14 years ago

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

148)   }
149)   else
150)   {
151)     $username = mysql_real_escape_string($customer);
152)     DEBUG('looking up customer info for username '.$username);
bernd Warnung gefixed

bernd authored 14 years ago

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

154)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 14 years ago

157)   $data = mysql_fetch_assoc($result);
158)   DEBUG($data);
159)   $ret['customerno'] = $data['id'];
160)   $ret['title'] = $data['anrede'];
161)   $ret['company'] = $data['firma'];
162)   $ret['name'] = $data['name'];
163)   $ret['email'] = $data['email'];
bernd webinterface => /webinterface

bernd authored 17 years ago

164)   
165)   return $ret;
166) }
167) 
168) 
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

169) function get_subuser_info($username)
170) {
171)   $result = db_query("SELECT uid, modules FROM system.subusers WHERE username='{$username}'");
172)   if (mysql_num_rows($result) < 1)
173)   {
174)     logger(LOG_ERR, "session/checkuser", "login", "error reading subuser's data: »{$username}«");
175)     system_failure('Das Auslesen Ihrer Benutzerdaten ist fehlgeschlagen. Bitte melden Sie dies einem Administrator');
176)   }
177)   $data = mysql_fetch_assoc($result);
178)   $userinfo = get_user_info($data['uid']);
179)   $userinfo['modules'] = $data['modules'];
180)   return $userinfo;
181) }
182) 
183) 
bernd webinterface => /webinterface

bernd authored 17 years ago

184) function get_user_info($username)
185) {
186)   $username = mysql_real_escape_string($username);
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

190)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

193)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

194)   $val = @mysql_fetch_object($result);
195)   return array(
196)           'username'      => $val->username,
197)           'customerno'    => $val->customerno,
198)           'uid'           => $val->uid,
199)           'homedir'       => $val->homedir,
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

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

bernd authored 17 years ago

201)           'name'          => $val->name,
202)           );
203) }
204) 
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

205) function set_customer_verified($customerno)
206) {
207)   $customerno = (int) $customerno;
208)   db_query("UPDATE kundendaten.kunden SET status=0 WHERE id={$customerno};");
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

210) }
211) 
212) function set_customer_lastlogin($customerno)
213) {
214)   $customerno = (int) $customerno;
215)   db_query("UPDATE kundendaten.kunden SET lastlogin=NOW() WHERE id={$customerno};");
216) }
217) 
bernd webinterface => /webinterface

bernd authored 17 years ago

218) function set_customer_password($customerno, $newpass)
219) {
220)   $customerno = (int) $customerno;
221)   $newpass = sha1($newpass);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 13 years ago

224) }
225) 
226) function set_subuser_password($subuser, $newpass)
227) {
228)   $subuser = mysql_real_escape_string($subuser);
229)   $uid = (int) $_SESSION['userinfo']['uid'];
230)   $newpass = sha1($newpass);
231)   db_query("UPDATE system.subusers SET password='$newpass' WHERE username='{$subuser}' AND uid={$uid}");
232)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed subuser's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

233) }
234) 
235) function set_systemuser_password($uid, $newpass)
236) {
237)   $uid = (int) $uid;
238)   require_once('inc/base.php');
bernd Benutzer SHA512 wenn möglich

bernd authored 14 years ago

239)   if (defined("CRYPT_SHA512") && CRYPT_SHA512 == 1)
240)   {
241)     $rounds = rand(1000, 5000);
242)     $salt = "rounds=".$rounds."$".random_string(8);
243)     $newpass = crypt($newpass, "\$6\${$salt}\$");
244)   }
245)   else
246)   {
247)     $salt = random_string(8);
248)     $newpass = crypt($newpass, "\$1\${$salt}\$");
249)   }
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

bernd authored 14 years ago

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 16 years ago

254) 
255) function setup_session($role, $useridentity)
256) {
257)   session_regenerate_id();
258)   $_SESSION['role'] = $role;
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

259)   if ($role & ROLE_SUBUSER)
260)   {
261)     DEBUG("We are a sub-user");
262)     $info = get_subuser_info($useridentity);
263)     $_SESSION['userinfo'] = $info;
264)     $_SESSION['subuser'] = $useridentity;
bernd Berechtigungen für Subuser...

bernd authored 12 years ago

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

bernd authored 13 years ago

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

bernd authored 16 years ago

271)   if ($role & ROLE_SYSTEMUSER)
272)   {
273)     DEBUG("We are system user");
274)     $info = get_user_info($useridentity);
275)     $_SESSION['userinfo'] = $info;
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

277)     $useridentity = $info['customerno'];
278)   }
279)   if ($role & ROLE_CUSTOMER)
280)   {
281)     $info = get_customer_info($useridentity);
282)     $_SESSION['customerinfo'] = $info;
bernd Setze lastlogin nur bei Nic...

bernd authored 13 years ago

283)     if (!isset($_SESSION['admin_user'])) {
284)       set_customer_lastlogin($info['customerno']);
285)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

288)   if ($role & ROLE_MAILACCOUNT)
289)   {
290)     $id = $useridentity;
291)     if (! strstr($id, '@'))
bernd Mehr config-optionen und co...

bernd authored 14 years ago

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

bernd authored 16 years ago

293)     $_SESSION['mailaccount'] = $id;
294)     DEBUG("We are mailaccount: {$_SESSION['mailaccount']}");
295)   }
bernd VMail-accounts können sich...

bernd authored 16 years ago

296)   if ($role & ROLE_VMAIL_ACCOUNT)
297)   {
298)     $id = $useridentity;
299)     $_SESSION['mailaccount'] = $id;
300)     DEBUG("We are virtual mailaccount: {$_SESSION['mailaccount']}");
301)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

302) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

303) }
304)