c208bd906b3991555db11b9229846c4601ca408c
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 Sperre Login für gesperrte...

bernd authored 12 years ago

42)   $result = db_query("SELECT 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);
46)     $db_password = $entry->password;
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 12 years ago

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

bernd authored 16 years ago

49)     {
50)       $role = ROLE_SYSTEMUSER;
51)       if ($entry->primary)
52)         $role = $role | ROLE_CUSTOMER;
53)       if ($entry->admin)
54)         $role = $role | ROLE_SYSADMIN;
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

56)       return $role;
57)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 15 years ago

59)   } else {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

61)   }
62) 
63)   // Customer?
64)   $customerno = (int) $login;
65)   $pass = sha1($password);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

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

bernd authored 17 years ago

69)   if (@mysql_num_rows($result) > 0)
70)   {
71)     return ROLE_CUSTOMER;
72)   }
73) 
bernd Auch mailaccounts können si...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

78)   }
79)   $result = db_query("SELECT cryptpass FROM mail.courier_mailaccounts WHERE account='{$account}' LIMIT 1;");
80)   if (@mysql_num_rows($result) > 0)
81)   {
82)     $entry = mysql_fetch_object($result);
83)     $db_password = $entry->cryptpass;
84)     $hash = crypt($password, $db_password);
85)     if ($hash == $db_password || $i_am_admin)
86)     {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

88)       return ROLE_MAILACCOUNT;
89)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

91)   }
92)   
bernd VMail-accounts können sich...

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 16 years ago

104)       return ROLE_VMAIL_ACCOUNT;
105)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

107)   }
108)   
bernd Auch mailaccounts können si...

bernd authored 16 years ago

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

bernd authored 13 years ago

110)   // Sub-User
111) 
bernd Cert-Login geht jetztauch m...

bernd authored 13 years ago

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

bernd authored 13 years ago

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

bernd authored 13 years ago

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

Bernd Wurst authored 12 years ago

117)     // SHA1 für alte Subuser (kaylee), SHA256 für neue Subuser
118)     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

119)     {
120)       logger(LOG_INFO, "session/checkuser", "login", "logged in virtual subuser »{$login}«.");
121)       return ROLE_SUBUSER;
122)     }
123)     logger(LOG_WARNING, "session/checkuser", "login", "wrong password for existing subuser »{$login}«.");
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

124)   }
125) 
bernd Auch mailaccounts können si...

bernd authored 16 years ago

126) 
bernd webinterface => /webinterface

bernd authored 17 years ago

127)   // Nothing?
128)   return NULL;
129) }
130) 
131) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

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

bernd authored 17 years ago

133) {
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

137)   $customerno = (int) $customer;
138)   if ($customerno != 0)
139)   {
140)     DEBUG('Looking up customerinfo for customer no. '.$customerno);
bernd Warnung gefixed

bernd authored 14 years ago

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

142)   }
143)   else
144)   {
145)     $username = mysql_real_escape_string($customer);
146)     DEBUG('looking up customer info for username '.$username);
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 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

148)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

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

bernd authored 14 years ago

151)   $data = mysql_fetch_assoc($result);
152)   DEBUG($data);
153)   $ret['customerno'] = $data['id'];
154)   $ret['title'] = $data['anrede'];
155)   $ret['company'] = $data['firma'];
156)   $ret['name'] = $data['name'];
157)   $ret['email'] = $data['email'];
bernd webinterface => /webinterface

bernd authored 17 years ago

158)   
159)   return $ret;
160) }
161) 
162) 
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

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

bernd authored 17 years ago

178) function get_user_info($username)
179) {
180)   $username = mysql_real_escape_string($username);
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

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

bernd authored 16 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

184)   {
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 17 years ago

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

bernd authored 16 years ago

187)   }
bernd webinterface => /webinterface

bernd authored 17 years ago

188)   $val = @mysql_fetch_object($result);
189)   return array(
190)           'username'      => $val->username,
191)           'customerno'    => $val->customerno,
192)           'uid'           => $val->uid,
193)           'homedir'       => $val->homedir,
bernd IPv6-Option nur anzeigen we...

bernd authored 13 years ago

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

bernd authored 17 years ago

195)           'name'          => $val->name,
196)           );
197) }
198) 
bernd Kunden-Status wird benutzt...

bernd authored 16 years ago

199) function set_customer_verified($customerno)
200) {
201)   $customerno = (int) $customerno;
202)   db_query("UPDATE kundendaten.kunden SET status=0 WHERE id={$customerno};");
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

204) }
205) 
206) function set_customer_lastlogin($customerno)
207) {
208)   $customerno = (int) $customerno;
209)   db_query("UPDATE kundendaten.kunden SET lastlogin=NOW() WHERE id={$customerno};");
210) }
211) 
bernd webinterface => /webinterface

bernd authored 17 years ago

212) function set_customer_password($customerno, $newpass)
213) {
214)   $customerno = (int) $customerno;
215)   $newpass = sha1($newpass);
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 13 years ago

218) }
219) 
220) function set_subuser_password($subuser, $newpass)
221) {
222)   $subuser = mysql_real_escape_string($subuser);
223)   $uid = (int) $_SESSION['userinfo']['uid'];
224)   $newpass = sha1($newpass);
225)   db_query("UPDATE system.subusers SET password='$newpass' WHERE username='{$subuser}' AND uid={$uid}");
226)   logger(LOG_INFO, "session/checkuser", "pwchange", "changed subuser's password.");
bernd webinterface => /webinterface

bernd authored 17 years ago

227) }
228) 
229) function set_systemuser_password($uid, $newpass)
230) {
231)   $uid = (int) $uid;
232)   require_once('inc/base.php');
bernd Benutzer SHA512 wenn möglich

bernd authored 14 years ago

233)   if (defined("CRYPT_SHA512") && CRYPT_SHA512 == 1)
234)   {
235)     $rounds = rand(1000, 5000);
236)     $salt = "rounds=".$rounds."$".random_string(8);
237)     $newpass = crypt($newpass, "\$6\${$salt}\$");
238)   }
239)   else
240)   {
241)     $salt = random_string(8);
242)     $newpass = crypt($newpass, "\$1\${$salt}\$");
243)   }
bernd sql-abfragen abstrahiert

bernd authored 16 years ago

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

bernd authored 14 years ago

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

bernd authored 17 years ago

246) }
247) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

248) 
249) function setup_session($role, $useridentity)
250) {
251)   session_regenerate_id();
252)   $_SESSION['role'] = $role;
bernd Erlaube subusers, die nur Z...

bernd authored 13 years ago

253)   if ($role & ROLE_SUBUSER)
254)   {
255)     DEBUG("We are a sub-user");
256)     $info = get_subuser_info($useridentity);
257)     $_SESSION['userinfo'] = $info;
258)     $_SESSION['subuser'] = $useridentity;
bernd Berechtigungen für Subuser...

bernd authored 12 years ago

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

bernd authored 13 years ago

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

bernd authored 16 years ago

265)   if ($role & ROLE_SYSTEMUSER)
266)   {
267)     DEBUG("We are system user");
268)     $info = get_user_info($useridentity);
269)     $_SESSION['userinfo'] = $info;
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

271)     $useridentity = $info['customerno'];
272)   }
273)   if ($role & ROLE_CUSTOMER)
274)   {
275)     $info = get_customer_info($useridentity);
276)     $_SESSION['customerinfo'] = $info;
bernd Setze lastlogin nur bei Nic...

bernd authored 13 years ago

277)     if (!isset($_SESSION['admin_user'])) {
278)       set_customer_lastlogin($info['customerno']);
279)     }
bernd Logger mit Logleveln

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

282)   if ($role & ROLE_MAILACCOUNT)
283)   {
284)     $id = $useridentity;
285)     if (! strstr($id, '@'))
bernd Mehr config-optionen und co...

bernd authored 14 years ago

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

bernd authored 16 years ago

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

bernd authored 16 years ago

290)   if ($role & ROLE_VMAIL_ACCOUNT)
291)   {
292)     $id = $useridentity;
293)     $_SESSION['mailaccount'] = $id;
294)     DEBUG("We are virtual mailaccount: {$_SESSION['mailaccount']}");
295)   }
bernd Auch mailaccounts können si...

bernd authored 16 years ago

296) 
bernd * Initialisierung der Sessi...

bernd authored 16 years ago

297) }
298)