Remove MD5 -> SHA1 password transition and replace it by (10 characters -> 256 charac...
[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()) {
5480a216 40 $_SESSION['log']->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'));
82 if ($response != $expected_response) {
83 $new_password = hash_xor(Env::v('xorpass'), $password);
84 $expected_response = hash_encrypt("$uname:$new_password:" . S::v('challenge'));
85 if ($response == $expected_response) {
86 XDB::execute('UPDATE auth_user_md5
87 SET password = {?}
88 WHERE user_id = {?}',
89 $new_password, $uid);
2b18a8ff 90 /* TODO: update GApps password here!!! */
c0799142
FB
91 }
92 }
93 if ($response != $expected_response) {
732e5855 94 S::logger($uid)->log('auth_fail', 'bad password');
c0799142
FB
95 return null;
96 }
97 return $uid;
98 }
99 return null;
0337d704 100 }
cab08090 101
0337d704 102
c0799142
FB
103 /** Check auth.
104 */
105 protected function doAuth($level)
0337d704 106 {
e74411f7 107 global $globals;
c0799142
FB
108
109 /* Cookie authentication
110 */
111 if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) {
112 $this->tryCookie();
113 }
114 if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) {
115 if (!S::logged()) {
116 S::set('auth', AUTH_COOKIE);
117 }
118 return S::i('auth_by_cookie');
e74411f7 119 }
0337d704 120
c0799142
FB
121
122 /* We want to do auth... we must have infos from a form.
123 */
e64c8b61 124 if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) {
c0799142 125 return null;
63528107 126 }
127
c0799142
FB
128 /** We come from an authentication form.
129 */
e74411f7 130 if (S::has('suid')) {
c0799142 131 $suid = S::v('suid');
e74411f7 132 $login = $uname = $suid['forlife'];
133 $redirect = false;
134 } else {
135 $uname = Env::v('username');
136
137 if (Env::v('domain') == "alias") {
c0799142
FB
138 $res = XDB::query('SELECT redirect
139 FROM virtual
140 INNER JOIN virtual_redirect USING(vid)
141 WHERE alias LIKE {?}',
142 $uname . '@' . $globals->mail->alias_dom);
e74411f7 143 $redirect = $res->fetchOneCell();
144 if ($redirect) {
145 $login = substr($redirect, 0, strpos($redirect, '@'));
146 } else {
c0799142 147 $login = '';
e74411f7 148 }
0337d704 149 } else {
e74411f7 150 $login = $uname;
151 $redirect = false;
0337d704 152 }
63528107 153 }
cab08090 154
c0799142
FB
155 $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && preg_match('/^\d*$/', $uname)) ? 'id' : 'alias');
156 if (!is_null($uid)) {
157 S::set('auth', AUTH_MDP);
158 if (Post::has('domain')) {
159 if (($domain = Post::v('domain', 'login')) == 'alias') {
160 setcookie('ORGdomain', "alias", (time() + 25920000), '/', '', 0);
161 } else {
162 setcookie('ORGdomain', '', (time() - 3600), '/', '', 0);
e74411f7 163 }
c0799142
FB
164 // pour que la modification soit effective dans le reste de la page
165 $_COOKIE['ORGdomain'] = $domain;
e74411f7 166 }
c0799142 167 S::kill('challenge');
732e5855 168 S::logger($uid)->log('auth_ok');
c0799142
FB
169 }
170 return $uid;
171 }
cab08090 172
c0799142
FB
173 protected function startSessionAs($uid, $level)
174 {
175 if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) {
176 return false;
177 } else if (S::has('uid')) {
178 return true;
179 }
180 if ($level == -1) {
181 S::set('auth', AUTH_COOKIE);
182 }
183 unset($_SESSION['log']);
6672b29b
VZ
184
185 // Retrieves main user properties.
c0799142
FB
186 $res = XDB::query('SELECT u.user_id AS uid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie,
187 matricule, password, FIND_IN_SET(\'femme\', u.flags) AS femme,
188 a.alias AS forlife, a2.alias AS bestalias,
189 q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash,
1610f13a 190 FIND_IN_SET(\'watch\', u.flags) AS watch_account, q.last_version, g.g_account_name IS NOT NULL AS googleapps
c0799142
FB
191 FROM auth_user_md5 AS u
192 INNER JOIN auth_user_quick AS q USING(user_id)
193 INNER JOIN aliases AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
194 INNER JOIN aliases AS a2 ON (u.user_id = a2.id AND FIND_IN_SET(\'bestalias\', a2.flags))
1610f13a 195 LEFT JOIN gapps_accounts AS g ON (u.user_id = g.l_userid AND g.g_status = \'active\')
c0799142
FB
196 WHERE u.user_id = {?} AND u.perms IN(\'admin\', \'user\')', $uid);
197 $sess = $res->fetchOneAssoc();
198 $perms = $sess['perms'];
199 unset($sess['perms']);
6672b29b
VZ
200
201 // Retrieves account usage information (last login, last host).
c0799142
FB
202 $res = XDB::query('SELECT UNIX_TIMESTAMP(s.start) AS lastlogin, s.host
203 FROM logger.sessions AS s
204 WHERE s.uid = {?} AND s.suid = 0
205 ORDER BY s.start DESC
206 LIMIT 1', $uid);
207 if ($res->numRows()) {
208 $sess = array_merge($sess, $res->fetchOneAssoc());
209 }
c0799142 210
6672b29b
VZ
211 // Loads the data into the real session.
212 $_SESSION = array_merge($_SESSION, $sess);
213
214 // Starts the session's logger, and sets up the permanent cookie.
215 if (S::has('suid')) {
216 $suid = S::v('suid');
e4501b51 217 $logger = S::logger($uid);
6672b29b 218 $logger->log("suid_start", S::v('forlife') . " by " . $suid['uid']);
c0799142 219 } else {
e4501b51 220 $logger = S::logger($uid);
c0799142 221 setcookie('ORGuid', $uid, (time() + 25920000), '/', '', 0);
a90cde48 222
d0621f88 223 if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') {
c0799142
FB
224 $cookie = hash_encrypt($sess['password']);
225 setcookie('ORGaccess', $cookie, (time() + 25920000), '/', '', 0);
d0621f88 226 if ($logger && S::i('auth_by_cookie') != $uid) {
c0799142 227 $logger->log("cookie_on");
63528107 228 }
c0799142
FB
229 } else {
230 setcookie('ORGaccess', '', time() - 3600, '/', '', 0);
231 if ($logger) {
232 $logger->log("cookie_off");
0337d704 233 }
0337d704 234 }
63528107 235 }
b0b937fd 236
6672b29b 237 // Finalizes the session setup.
c0799142
FB
238 $this->makePerms($perms);
239 $this->securityChecks();
240 $this->setSkin();
ebfdf077 241 $this->updateNbNotifs();
c0799142 242 check_redirect();
d0621f88
FB
243
244 // We should not have to use this private data anymore
245 S::kill('auth_by_cookie');
c0799142 246 return true;
0337d704 247 }
248
c0799142 249 private function securityChecks()
0337d704 250 {
c0799142
FB
251 $mail_subject = array();
252 if (check_account()) {
253 $mail_subject[] = 'Connexion d\'un utilisateur surveillé';
0337d704 254 }
c0799142
FB
255 if (check_ip('unsafe')) {
256 $mail_subject[] = 'Une IP surveillee a tente de se connecter';
257 if (check_ip('ban')) {
258 send_warning_mail(implode(' - ', $mail_subject));
259 $this->destroy();
260 Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. '
261 . 'Merci de contacter au plus vite '
262 . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>');
263 return false;
264 }
0337d704 265 }
c0799142
FB
266 if (count($mail_subject)) {
267 send_warning_mail(implode(' - ', $mail_subject));
0337d704 268 }
269 }
270
0d44ce42
FB
271 public function tokenAuth($login, $token)
272 {
273 // FIXME: we broke the session here because some RSS feeds (mainly wiki feeds) require
274 // a valid nome and checks the permissions. When the PlUser object will be ready, we'll
275 // be able to return a simple 'PlUser' object here without trying to alterate the
276 // session.
277 $res = XDB::query('SELECT u.user_id AS uid, u.perms, u.nom, u.nom_usage, u.prenom, u.promo, FIND_IN_SET(\'femme\', u.flags) AS sexe
278 FROM aliases AS a
279 INNER JOIN auth_user_md5 AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user"))
280 INNER JOIN auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?})
281 WHERE a.alias = {?} AND a.type != "homonyme"', $token, $login);
282 if ($res->numRows() == 1) {
283 $sess = $res->fetchOneAssoc();
284 if (!S::has('uid')) {
285 $_SESSION = $sess;
286 $this->makePerms($sess['perms']);
287 return S::i('uid');
288 } else if (S::i('uid') == $sess['uid']) {
289 return S::i('uid');
290 } else {
291 Platal::page()->kill('Invalid state. To be fixed when hruid is ready');
292 }
293 }
294 return null;
295 }
296
732e5855 297 public function makePerms($perm)
bf517daf 298 {
113f6de8 299 $flags = new PlFlagSet();
bf517daf 300 if ($perm == 'disabled' || $perm == 'ext') {
ab694eb5 301 S::set('perms', $flags);
ab694eb5 302 return;
bf517daf 303 }
304 $flags->addFlag(PERMS_USER);
305 if ($perm == 'admin') {
306 $flags->addFlag(PERMS_ADMIN);
307 }
c0799142 308 S::set('perms', $flags);
bf517daf 309 }
310
c0799142
FB
311 public function setSkin()
312 {
313 global $globals;
314 if (S::logged() && (!S::has('skin') || S::has('suid'))) {
315 $uid = S::v('uid');
316 $res = XDB::query("SELECT skin_tpl
317 FROM auth_user_quick AS a
318 INNER JOIN skins AS s ON a.skin = s.id
319 WHERE user_id = {?} AND skin_tpl != ''", $uid);
320 S::set('skin', $res->fetchOneCell());
5480a216 321 }
322 }
0337d704 323
faa06583
FB
324 public function loggedLevel()
325 {
326 return AUTH_COOKIE;
327 }
328
c0799142
FB
329 public function sureLevel()
330 {
331 return AUTH_MDP;
0337d704 332 }
ebfdf077
FB
333
334
335 public function updateNbNotifs()
336 {
337 require_once 'notifs.inc.php';
338 $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false);
339 S::set('notifs', $n->numRows());
340 }
0337d704 341}
342
a7de4ef7 343// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 344?>