Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 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 | 22 | class 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'); | |
40565fa2 | 50 | if (Cookie::v('access') == '' || !Cookie::has('uid')) { |
c0799142 FB |
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\')', | |
40565fa2 | 57 | Cookie::i('uid')); |
c0799142 FB |
58 | if ($res->numRows() != 0) { |
59 | list($uid, $password) = $res->fetchOneRow(); | |
60 | require_once 'secure_hash.inc.php'; | |
61 | $expected_value = hash_encrypt($password); | |
40565fa2 | 62 | if ($expected_value == Cookie::v('access')) { |
c0799142 FB |
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) { | |
86777849 FB |
105 | if (!S::logged()) { |
106 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); | |
107 | } else { | |
108 | Platal::page()->trigError('Mot de passe invalide'); | |
109 | } | |
732e5855 | 110 | S::logger($uid)->log('auth_fail', 'bad password'); |
c0799142 FB |
111 | return null; |
112 | } | |
113 | return $uid; | |
114 | } | |
c6bad0e7 | 115 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); |
c0799142 | 116 | return null; |
0337d704 | 117 | } |
cab08090 | 118 | |
0337d704 | 119 | |
c0799142 FB |
120 | /** Check auth. |
121 | */ | |
122 | protected function doAuth($level) | |
0337d704 | 123 | { |
e74411f7 | 124 | global $globals; |
c0799142 FB |
125 | |
126 | /* Cookie authentication | |
127 | */ | |
128 | if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) { | |
129 | $this->tryCookie(); | |
130 | } | |
131 | if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) { | |
132 | if (!S::logged()) { | |
133 | S::set('auth', AUTH_COOKIE); | |
134 | } | |
135 | return S::i('auth_by_cookie'); | |
e74411f7 | 136 | } |
0337d704 | 137 | |
c0799142 FB |
138 | |
139 | /* We want to do auth... we must have infos from a form. | |
140 | */ | |
e64c8b61 | 141 | if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) { |
c0799142 | 142 | return null; |
63528107 | 143 | } |
144 | ||
c0799142 FB |
145 | /** We come from an authentication form. |
146 | */ | |
e74411f7 | 147 | if (S::has('suid')) { |
c0799142 | 148 | $suid = S::v('suid'); |
888465dd | 149 | $login = $uname = $suid['uid']; |
e74411f7 | 150 | $redirect = false; |
151 | } else { | |
152 | $uname = Env::v('username'); | |
e74411f7 | 153 | if (Env::v('domain') == "alias") { |
c0799142 FB |
154 | $res = XDB::query('SELECT redirect |
155 | FROM virtual | |
156 | INNER JOIN virtual_redirect USING(vid) | |
157 | WHERE alias LIKE {?}', | |
158 | $uname . '@' . $globals->mail->alias_dom); | |
e74411f7 | 159 | $redirect = $res->fetchOneCell(); |
160 | if ($redirect) { | |
161 | $login = substr($redirect, 0, strpos($redirect, '@')); | |
162 | } else { | |
c0799142 | 163 | $login = ''; |
e74411f7 | 164 | } |
0337d704 | 165 | } else { |
e74411f7 | 166 | $login = $uname; |
167 | $redirect = false; | |
0337d704 | 168 | } |
63528107 | 169 | } |
cab08090 | 170 | |
888465dd FB |
171 | $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); |
172 | if (!is_null($uid) && S::has('suid')) { | |
173 | $suid = S::v('suid'); | |
174 | if ($suid['uid'] == $uid) { | |
175 | $uid = S::i('uid'); | |
176 | } else { | |
177 | $uid = null; | |
178 | } | |
179 | } | |
c0799142 FB |
180 | if (!is_null($uid)) { |
181 | S::set('auth', AUTH_MDP); | |
888465dd FB |
182 | if (!S::has('suid')) { |
183 | if (Post::has('domain')) { | |
184 | if (($domain = Post::v('domain', 'login')) == 'alias') { | |
40565fa2 | 185 | Cookie::set('domain', 'alias', 300); |
888465dd | 186 | } else { |
40565fa2 | 187 | Cookie::kill('domain'); |
888465dd | 188 | } |
e74411f7 | 189 | } |
190 | } | |
c0799142 | 191 | S::kill('challenge'); |
732e5855 | 192 | S::logger($uid)->log('auth_ok'); |
c0799142 FB |
193 | } |
194 | return $uid; | |
195 | } | |
cab08090 | 196 | |
c0799142 FB |
197 | protected function startSessionAs($uid, $level) |
198 | { | |
199 | if ((!is_null(S::v('user')) && S::i('user') != $uid) || (S::has('uid') && S::i('uid') != $uid)) { | |
200 | return false; | |
201 | } else if (S::has('uid')) { | |
202 | return true; | |
203 | } | |
888465dd FB |
204 | if ($level == AUTH_SUID) { |
205 | S::set('auth', AUTH_MDP); | |
c0799142 | 206 | } |
70232020 | 207 | |
6672b29b | 208 | // Retrieves main user properties. |
70232020 VZ |
209 | $res = XDB::query("SELECT u.user_id AS uid, u.hruid, prenom, prenom_ini, nom, nom_ini, nom_usage, perms, promo, promo_sortie, |
210 | matricule, password, FIND_IN_SET('femme', u.flags) AS femme, | |
c0799142 | 211 | q.core_mail_fmt AS mail_fmt, UNIX_TIMESTAMP(q.banana_last) AS banana_last, q.watch_last, q.core_rss_hash, |
6a61c46d FB |
212 | FIND_IN_SET('watch', u.flags) AS watch_account, q.last_version, g.g_account_name IS NOT NULL AS googleapps, |
213 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host | |
c0799142 FB |
214 | FROM auth_user_md5 AS u |
215 | INNER JOIN auth_user_quick AS q USING(user_id) | |
70232020 | 216 | LEFT JOIN gapps_accounts AS g ON (u.user_id = g.l_userid AND g.g_status = 'active') |
6a61c46d FB |
217 | LEFT JOIN logger.last_sessions AS ls ON (ls.uid = u.user_id) |
218 | LEFT JOIN logger.sessions AS s ON(s.id = ls.id) | |
70232020 | 219 | WHERE u.user_id = {?} AND u.perms IN('admin', 'user')", $uid); |
c0799142 FB |
220 | $sess = $res->fetchOneAssoc(); |
221 | $perms = $sess['perms']; | |
222 | unset($sess['perms']); | |
6672b29b | 223 | |
6672b29b VZ |
224 | // Loads the data into the real session. |
225 | $_SESSION = array_merge($_SESSION, $sess); | |
226 | ||
227 | // Starts the session's logger, and sets up the permanent cookie. | |
228 | if (S::has('suid')) { | |
229 | $suid = S::v('suid'); | |
e4501b51 | 230 | $logger = S::logger($uid); |
d0b64586 | 231 | $logger->log("suid_start", S::v('hruid') . " by " . $suid['hruid']); |
c0799142 | 232 | } else { |
e4501b51 | 233 | $logger = S::logger($uid); |
4ea44525 | 234 | $logger->saveLastSession(); |
40565fa2 | 235 | Cookie::set('uid', $uid, 300); |
a90cde48 | 236 | |
d0621f88 | 237 | if (S::i('auth_by_cookie') == $uid || Post::v('remember', 'false') == 'true') { |
604dfd58 | 238 | $this->setAccessCookie(false, S::i('auth_by_cookie') != $uid); |
c0799142 | 239 | } else { |
604dfd58 | 240 | $this->killAccessCookie(); |
0337d704 | 241 | } |
63528107 | 242 | } |
b0b937fd | 243 | |
6672b29b | 244 | // Finalizes the session setup. |
50d5ec0b | 245 | S::set('perms', User::makePerms($perms)); |
c0799142 FB |
246 | $this->securityChecks(); |
247 | $this->setSkin(); | |
ebfdf077 | 248 | $this->updateNbNotifs(); |
c0799142 | 249 | check_redirect(); |
d0621f88 FB |
250 | |
251 | // We should not have to use this private data anymore | |
252 | S::kill('auth_by_cookie'); | |
c0799142 | 253 | return true; |
0337d704 | 254 | } |
255 | ||
edc55095 FB |
256 | /** Start a session without authentication data for the given user. |
257 | * This is used to identify the user after his registration, to be | |
258 | * removed after rewriting registration procedure. | |
259 | * XXX: Temporary | |
260 | */ | |
261 | public function startWeakSession($user) | |
262 | { | |
263 | if (!$this->startSessionAs($user, AUTH_MDP)) { | |
264 | $this->destroy(); | |
265 | return false; | |
266 | } | |
267 | S::set('auth', AUTH_MDP); | |
268 | return true; | |
269 | } | |
270 | ||
c0799142 | 271 | private function securityChecks() |
0337d704 | 272 | { |
c0799142 FB |
273 | $mail_subject = array(); |
274 | if (check_account()) { | |
275 | $mail_subject[] = 'Connexion d\'un utilisateur surveillé'; | |
0337d704 | 276 | } |
c0799142 FB |
277 | if (check_ip('unsafe')) { |
278 | $mail_subject[] = 'Une IP surveillee a tente de se connecter'; | |
279 | if (check_ip('ban')) { | |
280 | send_warning_mail(implode(' - ', $mail_subject)); | |
281 | $this->destroy(); | |
282 | Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. ' | |
283 | . 'Merci de contacter au plus vite ' | |
284 | . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>'); | |
285 | return false; | |
286 | } | |
0337d704 | 287 | } |
c0799142 FB |
288 | if (count($mail_subject)) { |
289 | send_warning_mail(implode(' - ', $mail_subject)); | |
0337d704 | 290 | } |
291 | } | |
292 | ||
0d44ce42 FB |
293 | public function tokenAuth($login, $token) |
294 | { | |
50d5ec0b | 295 | $res = XDB::query('SELECT u.hruid |
0d44ce42 FB |
296 | FROM aliases AS a |
297 | INNER JOIN auth_user_md5 AS u ON (a.id = u.user_id AND u.perms IN ("admin", "user")) | |
298 | INNER JOIN auth_user_quick AS q ON (a.id = q.user_id AND q.core_rss_hash = {?}) | |
299 | WHERE a.alias = {?} AND a.type != "homonyme"', $token, $login); | |
300 | if ($res->numRows() == 1) { | |
50d5ec0b FB |
301 | $data = $res->fetchOneAssoc(); |
302 | return new User($data['hruid'], $data); | |
0d44ce42 FB |
303 | } |
304 | return null; | |
305 | } | |
306 | ||
1fec3da6 | 307 | protected function makePerms($perm, $is_admin) |
bf517daf | 308 | { |
113f6de8 | 309 | $flags = new PlFlagSet(); |
bf517daf | 310 | if ($perm == 'disabled' || $perm == 'ext') { |
ab694eb5 | 311 | S::set('perms', $flags); |
ab694eb5 | 312 | return; |
bf517daf | 313 | } |
314 | $flags->addFlag(PERMS_USER); | |
315 | if ($perm == 'admin') { | |
316 | $flags->addFlag(PERMS_ADMIN); | |
317 | } | |
c0799142 | 318 | S::set('perms', $flags); |
bf517daf | 319 | } |
320 | ||
c0799142 FB |
321 | public function setSkin() |
322 | { | |
c0799142 FB |
323 | if (S::logged() && (!S::has('skin') || S::has('suid'))) { |
324 | $uid = S::v('uid'); | |
325 | $res = XDB::query("SELECT skin_tpl | |
326 | FROM auth_user_quick AS a | |
327 | INNER JOIN skins AS s ON a.skin = s.id | |
328 | WHERE user_id = {?} AND skin_tpl != ''", $uid); | |
329 | S::set('skin', $res->fetchOneCell()); | |
5480a216 | 330 | } |
331 | } | |
0337d704 | 332 | |
faa06583 FB |
333 | public function loggedLevel() |
334 | { | |
335 | return AUTH_COOKIE; | |
336 | } | |
337 | ||
c0799142 FB |
338 | public function sureLevel() |
339 | { | |
340 | return AUTH_MDP; | |
0337d704 | 341 | } |
ebfdf077 FB |
342 | |
343 | ||
344 | public function updateNbNotifs() | |
345 | { | |
346 | require_once 'notifs.inc.php'; | |
347 | $n = select_notifs(false, S::i('uid'), S::v('watch_last'), false); | |
348 | S::set('notifs', $n->numRows()); | |
349 | } | |
604dfd58 FB |
350 | |
351 | public function setAccessCookie($replace = false, $log = true) { | |
352 | if (S::has('suid') || ($replace && !Cookie::blank('access'))) { | |
353 | return; | |
354 | } | |
355 | require_once('secure_hash.inc.php'); | |
356 | Cookie::set('access', hash_encrypt(S::v('password')), 300, true); | |
357 | if ($log) { | |
358 | S::logger()->log('cookie_on'); | |
359 | } | |
360 | } | |
361 | ||
362 | public function killAccessCookie($log = true) { | |
363 | Cookie::kill('access'); | |
364 | if ($log) { | |
365 | S::logger()->log('cookie_off'); | |
366 | } | |
367 | } | |
368 | ||
369 | public function killLoginFormCookies() { | |
370 | Cookie::kill('uid'); | |
371 | Cookie::kill('domain'); | |
372 | } | |
0337d704 | 373 | } |
374 | ||
a7de4ef7 | 375 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 376 | ?> |