Merge commit 'origin/platal-0.10.0'
[platal.git] / classes / xorgsession.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
c0799142 22class XorgSession extends PlSession
0337d704 23{
c0799142
FB
24 public function __construct()
25 {
26 parent::__construct();
abde67b1
FB
27 }
28
c0799142 29 public function startAvailableAuth()
5480a216 30 {
c0799142
FB
31 if (!S::logged()) {
32 $cookie = $this->tryCookie();
33 if ($cookie == 0) {
34 return $this->start(AUTH_COOKIE);
998c070f 35 } else if ($cookie == 1 || $cookie == -2) {
c0799142
FB
36 return false;
37 }
5480a216 38 }
0be07aa6 39 if ((check_ip('dangerous') && S::has('uid')) || check_account()) {
54e73532 40 S::logger()->log("view_page", $_SERVER['REQUEST_URI']);
0337d704 41 }
c0799142 42 return true;
0337d704 43 }
44
c0799142
FB
45 /** Check the cookie and set the associated user_id in the auth_by_cookie session variable.
46 */
47 private function tryCookie()
48 {
49 S::kill('auth_by_cookie');
50 if (Cookie::v('ORGaccess') == '' || !Cookie::has('ORGuid')) {
51 return -1;
52 }
cab08090 53
c0799142
FB
54 $res = XDB::query('SELECT user_id, password
55 FROM auth_user_md5
56 WHERE user_id = {?} AND perms IN(\'admin\', \'user\')',
57 Cookie::i('ORGuid'));
58 if ($res->numRows() != 0) {
59 list($uid, $password) = $res->fetchOneRow();
60 require_once 'secure_hash.inc.php';
61 $expected_value = hash_encrypt($password);
62 if ($expected_value == Cookie::v('ORGaccess')) {
63 S::set('auth_by_cookie', $uid);
64 return 0;
65 } else {
66 return 1;
67 }
68 }
69 return -2;
70 }
71
72 private function checkPassword($uname, $login, $response, $login_type)
5480a216 73 {
c0799142
FB
74 $res = XDB::query('SELECT u.user_id, u.password
75 FROM auth_user_md5 AS u
76 INNER JOIN aliases AS a ON (a.id = u.user_id AND type != \'homonyme\')
77 WHERE a.' . $login_type . ' = {?} AND u.perms IN(\'admin\', \'user\')',
78 $login);
79 if (list($uid, $password) = $res->fetchOneRow()) {
80 require_once 'secure_hash.inc.php';
81 $expected_response = hash_encrypt("$uname:$password:" . S::v('challenge'));
79afc233
FB
82 if ($response != $expected_response && Env::has('xorpass')
83 && !preg_match('/^0*$/', Env::v('xorpass'))) {
c0799142
FB
84 $new_password = hash_xor(Env::v('xorpass'), $password);
85 $expected_response = hash_encrypt("$uname:$new_password:" . S::v('challenge'));
86 if ($response == $expected_response) {
e0e27d63
VZ
87 XDB::execute('UPDATE auth_user_md5
88 SET password = {?}
89 WHERE user_id = {?}',
90 $new_password, $uid);
91
92 // Update the GoogleApps password as well, if required.
93 global $globals;
94 if ($globals->mailstorage->googleapps_domain) {
95 require_once 'googleapps.inc.php';
0aa026b3
VZ
96 $user = User::getSilent($uid);
97 $account = new GoogleAppsAccount($user);
e0e27d63
VZ
98 if ($account->active() && $account->sync_password) {
99 $account->set_password($new_password);
100 }
101 }
c0799142
FB
102 }
103 }
104 if ($response != $expected_response) {
732e5855 105 S::logger($uid)->log('auth_fail', 'bad password');
c0799142
FB
106 return null;
107 }
108 return $uid;
109 }
110 return null;
0337d704 111 }
cab08090 112
0337d704 113
c0799142
FB
114 /** Check auth.
115 */
116 protected function doAuth($level)
0337d704 117 {
e74411f7 118 global $globals;
c0799142
FB
119
120 /* Cookie authentication
121 */
122 if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) {
123 $this->tryCookie();
124 }
125 if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) {
126 if (!S::logged()) {
127 S::set('auth', AUTH_COOKIE);
128 }
129 return S::i('auth_by_cookie');
e74411f7 130 }
0337d704 131
c0799142
FB
132
133 /* We want to do auth... we must have infos from a form.
134 */
e64c8b61 135 if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) {
c0799142 136 return null;
63528107 137 }
138
c0799142
FB
139 /** We come from an authentication form.
140 */
e74411f7 141 if (S::has('suid')) {
c0799142 142 $suid = S::v('suid');
888465dd 143 $login = $uname = $suid['uid'];
e74411f7 144 $redirect = false;
145 } else {
146 $uname = Env::v('username');
e74411f7 147 if (Env::v('domain') == "alias") {
c0799142
FB
148 $res = XDB::query('SELECT redirect
149 FROM virtual
150 INNER JOIN virtual_redirect USING(vid)
151 WHERE alias LIKE {?}',
152 $uname . '@' . $globals->mail->alias_dom);
e74411f7 153 $redirect = $res->fetchOneCell();
154 if ($redirect) {
155 $login = substr($redirect, 0, strpos($redirect, '@'));
156 } else {
c0799142 157 $login = '';
e74411f7 158 }
0337d704 159 } else {
e74411f7 160 $login = $uname;
161 $redirect = false;
0337d704 162 }
63528107 163 }
cab08090 164
888465dd
FB
165 $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias');
166 if (!is_null($uid) && S::has('suid')) {
167 $suid = S::v('suid');
168 if ($suid['uid'] == $uid) {
169 $uid = S::i('uid');
170 } else {
171 $uid = null;
172 }
173 }
c0799142
FB
174 if (!is_null($uid)) {
175 S::set('auth', AUTH_MDP);
888465dd
FB
176 if (!S::has('suid')) {
177 if (Post::has('domain')) {
178 if (($domain = Post::v('domain', 'login')) == 'alias') {
179 setcookie('ORGdomain', "alias", (time() + 25920000), '/', '', 0);
180 } else {
181 setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
182 }
183 // pour que la modification soit effective dans le reste de la page
184 $_COOKIE['ORGdomain'] = $domain;
e74411f7 185 }
186 }
c0799142 187 S::kill('challenge');
732e5855 188 S::logger($uid)->log('auth_ok');
c0799142
FB
189 }
190 return $uid;
191 }
cab08090 192
c0799142
FB
193 protected function startSessionAs($uid, $level)
194 {
195 if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) {
196 return false;
197 } else if (S::has('uid')) {
198 return true;
199 }
888465dd
FB
200 if ($level == AUTH_SUID) {
201 S::set('auth', AUTH_MDP);
54e73532 202 unset($_SESSION['log']);
c0799142 203 }
70232020 204
6672b29b 205 // Retrieves main user properties.
70232020
VZ
206 $res = XDB::query("SELECT u.user_id AS uid, u.hruid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie,
207 matricule, password, FIND_IN_SET('femme', u.flags) AS femme,
c0799142 208 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
6a61c46d
FB
209 FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version, g.g_account_name IS NOT NULL AS googleapps,
210 UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
c0799142
FB
211 FROM auth_user_md5 AS u
212 INNER JOIN auth_user_quick AS q USING(user_id)
70232020 213 LEFT JOIN gapps_accounts AS g ON (u.user_id = g.l_userid AND g.g_status = 'active')
6a61c46d
FB
214 LEFT JOIN logger.last_sessions AS ls ON (ls.uid = u.user_id)
215 LEFT JOIN logger.sessions AS s ON(s.id = ls.id)
70232020 216 WHERE u.user_id = {?} AND u.perms IN('admin', 'user')", $uid);
c0799142
FB
217 $sess = $res->fetchOneAssoc();
218 $perms = $sess['perms'];
219 unset($sess['perms']);
6672b29b 220
6672b29b
VZ
221 // Loads the data into the real session.
222 $_SESSION = array_merge($_SESSION, $sess);
223
224 // Starts the session's logger, and sets up the permanent cookie.
225 if (S::has('suid')) {
226 $suid = S::v('suid');
e4501b51 227 $logger = S::logger($uid);
d0b64586 228 $logger->log("suid_start", S::v('hruid') . " by " . $suid['hruid']);
c0799142 229 } else {
e4501b51 230 $logger = S::logger($uid);
4ea44525 231 $logger->saveLastSession();
c0799142 232 setcookie('ORGuid', $uid, (time() + 25920000), '/', '', 0);
a90cde48 233
d0621f88 234 if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') {
c0799142
FB
235 $cookie = hash_encrypt($sess['password']);
236 setcookie('ORGaccess', $cookie, (time() + 25920000), '/', '', 0);
54e73532 237 if (S::i('auth_by_cookie') != $uid) {
c0799142 238 $logger->log("cookie_on");
63528107 239 }
c0799142
FB
240 } else {
241 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
54e73532 242 $logger->log("cookie_off");
0337d704 243 }
63528107 244 }
b0b937fd 245
6672b29b 246 // Finalizes the session setup.
50d5ec0b 247 S::set('perms', User::makePerms($perms));
c0799142
FB
248 $this->securityChecks();
249 $this->setSkin();
ebfdf077 250 $this->updateNbNotifs();
c0799142 251 check_redirect();
d0621f88
FB
252
253 // We should not have to use this private data anymore
254 S::kill('auth_by_cookie');
c0799142 255 return true;
0337d704 256 }
257
c0799142 258 private function securityChecks()
0337d704 259 {
c0799142
FB
260 $mail_subject = array();
261 if (check_account()) {
262 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
0337d704 263 }
c0799142
FB
264 if (check_ip('unsafe')) {
265 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
266 if (check_ip('ban')) {
267 send_warning_mail(implode(' - ', $mail_subject));
268 $this->destroy();
269 Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
270 . 'Merci de contacter au plus vite '
271 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
272 return false;
273 }
0337d704 274 }
c0799142
FB
275 if (count($mail_subject)) {
276 send_warning_mail(implode(' - ', $mail_subject));
0337d704 277 }
278 }
279
0d44ce42
FB
280 public function tokenAuth($login, $token)
281 {
50d5ec0b 282 $res = XDB::query('SELECT u.hruid
0d44ce42
FB
283 FROM aliases AS a
284 INNER JOIN auth_user_md5 AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user"))
285 INNER JOIN auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?})
286 WHERE a.alias = {?} AND a.type != "homonyme"', $token, $login);
287 if ($res->numRows() == 1) {
50d5ec0b
FB
288 $data = $res->fetchOneAssoc();
289 return new User($data['hruid'], $data);
0d44ce42
FB
290 }
291 return null;
292 }
293
732e5855 294 public function makePerms($perm)
bf517daf 295 {
113f6de8 296 $flags = new PlFlagSet();
bf517daf 297 if ($perm == 'disabled' || $perm == 'ext') {
ab694eb5 298 S::set('perms', $flags);
ab694eb5 299 return;
bf517daf 300 }
301 $flags->addFlag(PERMS_USER);
302 if ($perm == 'admin') {
303 $flags->addFlag(PERMS_ADMIN);
304 }
c0799142 305 S::set('perms', $flags);
bf517daf 306 }
307
c0799142
FB
308 public function setSkin()
309 {
c0799142
FB
310 if (S::logged() && (!S::has('skin') || S::has('suid'))) {
311 $uid = S::v('uid');
312 $res = XDB::query("SELECT skin_tpl
313 FROM auth_user_quick AS a
314 INNER JOIN skins AS s ON a.skin = s.id
315 WHERE user_id = {?} AND skin_tpl != ''", $uid);
316 S::set('skin', $res->fetchOneCell());
5480a216 317 }
318 }
0337d704 319
faa06583
FB
320 public function loggedLevel()
321 {
322 return AUTH_COOKIE;
323 }
324
c0799142
FB
325 public function sureLevel()
326 {
327 return AUTH_MDP;
0337d704 328 }
ebfdf077
FB
329
330
331 public function updateNbNotifs()
332 {
333 require_once 'notifs.inc.php';
334 $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false);
335 S::set('notifs', $n->numRows());
336 }
0337d704 337}
338
a7de4ef7 339// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 340?>