Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 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 | { |
45dd32db FB |
24 | const INVALID_USER = -2; |
25 | const NO_COOKIE = -1; | |
26 | const COOKIE_SUCCESS = 0; | |
27 | const INVALID_COOKIE = 1; | |
28 | ||
c0799142 FB |
29 | public function __construct() |
30 | { | |
31 | parent::__construct(); | |
abde67b1 FB |
32 | } |
33 | ||
c0799142 | 34 | public function startAvailableAuth() |
5480a216 | 35 | { |
c0799142 | 36 | if (!S::logged()) { |
45dd32db FB |
37 | switch ($this->tryCookie()) { |
38 | case self::COOKIE_SUCCESS: | |
39 | if (!$this->start(AUTH_COOKIE)) { | |
40 | return false; | |
41 | } | |
42 | break; | |
43 | ||
44 | case self::INVALID_USER: | |
45 | case self::INVALID_COOKIE: | |
c0799142 FB |
46 | return false; |
47 | } | |
5480a216 | 48 | } |
0be07aa6 | 49 | if ((check_ip('dangerous') && S::has('uid')) || check_account()) { |
9d214e8e | 50 | S::logger()->log("view_page", $_SERVER['REQUEST_URI']); |
0337d704 | 51 | } |
c0799142 | 52 | return true; |
0337d704 | 53 | } |
54 | ||
1bf36cd1 | 55 | /** Check the cookie and set the associated uid in the auth_by_cookie session variable. |
c0799142 FB |
56 | */ |
57 | private function tryCookie() | |
58 | { | |
59 | S::kill('auth_by_cookie'); | |
40565fa2 | 60 | if (Cookie::v('access') == '' || !Cookie::has('uid')) { |
45dd32db | 61 | return self::NO_COOKIE; |
c0799142 | 62 | } |
cab08090 | 63 | |
c67ba12a FB |
64 | $res = XDB::query('SELECT uid, password |
65 | FROM accounts | |
66 | WHERE uid = {?} AND state = \'active\'', | |
8cd3dccc | 67 | Cookie::i('uid')); |
c0799142 FB |
68 | if ($res->numRows() != 0) { |
69 | list($uid, $password) = $res->fetchOneRow(); | |
45dd32db | 70 | if (sha1($password) == Cookie::v('access')) { |
c0799142 | 71 | S::set('auth_by_cookie', $uid); |
45dd32db | 72 | return self::COOKIE_SUCCESS; |
c0799142 | 73 | } else { |
45dd32db | 74 | return self::INVALID_COOKIE; |
c0799142 FB |
75 | } |
76 | } | |
45dd32db | 77 | return self::INVALID_USER; |
c0799142 FB |
78 | } |
79 | ||
6cce7840 RB |
80 | const TEXT_INVALID_LOGIN = "Mot de passe ou nom d'utilisateur invalide"; |
81 | const TEXT_INVALID_PASS = "Mot de passe invalide"; | |
82 | ||
83 | private function checkPassword($login, User $user, $response) | |
5480a216 | 84 | { |
6cce7840 RB |
85 | if ($user === null) { |
86 | Platal::page()->trigError(self::TEXT_INVALID_LOGIN); | |
87 | return false; | |
675f3174 | 88 | } else { |
6cce7840 RB |
89 | $password = $user->password(); |
90 | $expected_response = sha1("$login:$password:" . S::v('challenge')); | |
7a12b2ca | 91 | /* Deprecates len(password) > 10 conversion. */ |
c0799142 | 92 | if ($response != $expected_response) { |
86777849 | 93 | if (!S::logged()) { |
6cce7840 | 94 | Platal::page()->trigError(self::TEXT_INVALID_LOGIN); |
86777849 | 95 | } else { |
6cce7840 | 96 | Platal::page()->trigError(self::TEXT_INVALID_PASS); |
86777849 | 97 | } |
732e5855 | 98 | S::logger($uid)->log('auth_fail', 'bad password'); |
6cce7840 | 99 | return false; |
c0799142 | 100 | } |
6cce7840 | 101 | return true; |
c0799142 | 102 | } |
0337d704 | 103 | } |
cab08090 | 104 | |
0337d704 | 105 | |
c0799142 FB |
106 | /** Check auth. |
107 | */ | |
108 | protected function doAuth($level) | |
0337d704 | 109 | { |
e74411f7 | 110 | global $globals; |
c0799142 FB |
111 | |
112 | /* Cookie authentication | |
113 | */ | |
114 | if ($level == AUTH_COOKIE && !S::has('auth_by_cookie')) { | |
115 | $this->tryCookie(); | |
116 | } | |
117 | if ($level == AUTH_COOKIE && S::has('auth_by_cookie')) { | |
118 | if (!S::logged()) { | |
119 | S::set('auth', AUTH_COOKIE); | |
120 | } | |
19fdac5d | 121 | return User::getSilentWithUID(S::i('auth_by_cookie')); |
e74411f7 | 122 | } |
0337d704 | 123 | |
c0799142 FB |
124 | |
125 | /* We want to do auth... we must have infos from a form. | |
126 | */ | |
e64c8b61 | 127 | if (!Post::has('username') || !Post::has('response') || !S::has('challenge')) { |
c0799142 | 128 | return null; |
63528107 | 129 | } |
130 | ||
c0799142 FB |
131 | /** We come from an authentication form. |
132 | */ | |
0c02607e | 133 | if (S::suid()) { |
6cce7840 | 134 | $login = S::suid('uid'); |
e74411f7 | 135 | } else { |
6cce7840 | 136 | $login = Post::v('username'); |
63528107 | 137 | } |
cab08090 | 138 | |
6cce7840 RB |
139 | $user = User::getSilent($login); |
140 | ||
6cce7840 RB |
141 | if (!is_null($user) && S::suid()) { |
142 | $success = (S::suid('uid') == $user->id()); | |
143 | } else { | |
144 | $success = $this->checkPassword($login, $user, Post::v('response')); | |
888465dd | 145 | } |
6cce7840 RB |
146 | |
147 | if ($success) { | |
c0799142 | 148 | S::set('auth', AUTH_MDP); |
c0799142 | 149 | S::kill('challenge'); |
6cce7840 | 150 | S::logger($user->id())->log('auth_ok'); |
c0799142 | 151 | } |
6cce7840 | 152 | return $user; |
c0799142 | 153 | } |
cab08090 | 154 | |
e2cb093d | 155 | protected function startSessionAs($user, $level) |
c0799142 | 156 | { |
b281eb7b | 157 | if ((!is_null(S::user()) && S::user()->id() != $user->id()) |
e2cb093d | 158 | || (S::has('uid') && S::i('uid') != $user->id())) { |
c0799142 FB |
159 | return false; |
160 | } else if (S::has('uid')) { | |
161 | return true; | |
162 | } | |
888465dd FB |
163 | if ($level == AUTH_SUID) { |
164 | S::set('auth', AUTH_MDP); | |
c0799142 | 165 | } |
70232020 | 166 | |
2ab3486b | 167 | // Loads uid and hruid into the session for developement conveniance. |
81b0f4ed | 168 | $_SESSION = array_merge($_SESSION, array('uid' => $user->id(), 'hruid' => $user->hruid, 'token' => $user->token, 'user' => $user)); |
6672b29b VZ |
169 | |
170 | // Starts the session's logger, and sets up the permanent cookie. | |
0c02607e FB |
171 | if (S::suid()) { |
172 | S::logger()->log("suid_start", S::v('hruid') . ' by ' . S::suid('hruid')); | |
c0799142 | 173 | } else { |
e2cb093d FB |
174 | S::logger()->saveLastSession(); |
175 | Cookie::set('uid', $user->id(), 300); | |
a90cde48 | 176 | |
e2cb093d FB |
177 | if (S::i('auth_by_cookie') == $user->id() || Post::v('remember', 'false') == 'true') { |
178 | $this->setAccessCookie(false, S::i('auth_by_cookie') != $user->id()); | |
c0799142 | 179 | } else { |
604dfd58 | 180 | $this->killAccessCookie(); |
0337d704 | 181 | } |
63528107 | 182 | } |
b0b937fd | 183 | |
6672b29b | 184 | // Finalizes the session setup. |
2ab3486b | 185 | $this->makePerms($user->perms, $user->is_admin); |
c0799142 FB |
186 | $this->securityChecks(); |
187 | $this->setSkin(); | |
ebfdf077 | 188 | $this->updateNbNotifs(); |
1d82f2d0 RB |
189 | // Only check email redirection for 'internal' users. |
190 | if ($user->checkPerms(PERMS_USER)) { | |
191 | check_redirect(); | |
192 | } | |
d0621f88 FB |
193 | |
194 | // We should not have to use this private data anymore | |
195 | S::kill('auth_by_cookie'); | |
c0799142 | 196 | return true; |
0337d704 | 197 | } |
198 | ||
c0799142 | 199 | private function securityChecks() |
0337d704 | 200 | { |
c0799142 FB |
201 | $mail_subject = array(); |
202 | if (check_account()) { | |
203 | $mail_subject[] = 'Connexion d\'un utilisateur surveillé'; | |
0337d704 | 204 | } |
c0799142 FB |
205 | if (check_ip('unsafe')) { |
206 | $mail_subject[] = 'Une IP surveillee a tente de se connecter'; | |
207 | if (check_ip('ban')) { | |
208 | send_warning_mail(implode(' - ', $mail_subject)); | |
209 | $this->destroy(); | |
210 | Platal::page()->kill('Une erreur est survenue lors de la procédure d\'authentification. ' | |
211 | . 'Merci de contacter au plus vite ' | |
212 | . '<a href="mailto:support@polytechnique.org">support@polytechnique.org</a>'); | |
213 | return false; | |
214 | } | |
0337d704 | 215 | } |
c0799142 FB |
216 | if (count($mail_subject)) { |
217 | send_warning_mail(implode(' - ', $mail_subject)); | |
0337d704 | 218 | } |
219 | } | |
220 | ||
8ebd6f86 VZ |
221 | /** |
222 | * The authentication schema is based on three query parameters: | |
223 | * ?user=<hruid>×tamp=<timestamp>&sig=<sig> | |
224 | * where: | |
225 | * - hruid is the hruid of the querying user | |
226 | * - timestamp is the current UNIX timestamp, which has to be within a | |
227 | * given distance of the server-side UNIX timestamp | |
228 | * - sig is the HMAC of "<method>#<resource>#<payload>#<timestamp>" using | |
229 | * a known secret of the user as the key. | |
230 | * | |
231 | * At the moment, the shared secret of the user is the sha1 hash of its | |
232 | * password. This is temporary, though, until better support for tokens is | |
233 | * implemented in plat/al. | |
234 | * TODO(vzanotti): Switch to dedicated secrets for authentication. | |
235 | */ | |
236 | public function apiAuth($method, $resource, $payload) | |
237 | { | |
238 | // Verify that the timestamp is within acceptable bounds. | |
239 | $timestamp = Env::i('timestamp', 0); | |
240 | if (abs($timestamp - time()) > Platal::globals()->api->timestamp_tolerance) { | |
241 | return null; | |
242 | } | |
243 | ||
244 | // Retrieve the user corresponding to the forlife. Note that at the | |
245 | // moment, other aliases are also accepted. | |
246 | $user = User::getSilent(Env::s('user', '')); | |
247 | if (is_null($user) || !$user->isActive()) { | |
248 | return null; | |
249 | } | |
250 | ||
251 | // Determine the list of tokens associated with the user. At the moment, | |
252 | // this is just the sha1 of the password. | |
253 | $tokens = array($user->password()); | |
254 | ||
255 | // For each token, try to validate the signature. | |
256 | $message = implode('#', array($method, $resource, $payload, $timestamp)); | |
257 | $signature = Env::s('sig'); | |
258 | foreach ($tokens as $token) { | |
259 | $expected_signature = hash_hmac( | |
260 | Platal::globals()->api->hmac_algo, $message, $token); | |
261 | if ($signature == $expected_signature) { | |
262 | return $user; | |
263 | } | |
264 | } | |
265 | ||
266 | return null; | |
267 | } | |
268 | ||
0d44ce42 FB |
269 | public function tokenAuth($login, $token) |
270 | { | |
1bf36cd1 | 271 | $res = XDB::query('SELECT a.uid, a.hruid |
c3e97f0c FB |
272 | FROM accounts AS a |
273 | WHERE a.token = {?} AND a.hruid = {?} AND a.state = \'active\'', | |
274 | $token, $login); | |
0d44ce42 | 275 | if ($res->numRows() == 1) { |
8c12f931 | 276 | return new User(null, $res->fetchOneAssoc()); |
0d44ce42 FB |
277 | } |
278 | return null; | |
279 | } | |
280 | ||
365ba8c3 | 281 | protected function makePerms($perm, $is_admin) |
bf517daf | 282 | { |
365ba8c3 | 283 | S::set('perms', User::makePerms($perm, $is_admin)); |
bf517daf | 284 | } |
285 | ||
c0799142 FB |
286 | public function setSkin() |
287 | { | |
0c02607e | 288 | if (S::logged() && (!S::has('skin') || S::suid())) { |
c67ba12a FB |
289 | $res = XDB::query('SELECT skin_tpl |
290 | FROM accounts AS a | |
291 | INNER JOIN skins AS s on (a.skin = s.id) | |
292 | WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid')); | |
c0799142 | 293 | S::set('skin', $res->fetchOneCell()); |
5480a216 | 294 | } |
295 | } | |
0337d704 | 296 | |
faa06583 FB |
297 | public function loggedLevel() |
298 | { | |
299 | return AUTH_COOKIE; | |
300 | } | |
301 | ||
c0799142 FB |
302 | public function sureLevel() |
303 | { | |
304 | return AUTH_MDP; | |
0337d704 | 305 | } |
ebfdf077 FB |
306 | |
307 | ||
308 | public function updateNbNotifs() | |
309 | { | |
009b8ab7 FB |
310 | require_once 'notifs.inc.php'; |
311 | $user = S::user(); | |
069ddda8 | 312 | $n = Watch::getCount($user); |
009b8ab7 | 313 | S::set('notifs', $n); |
ebfdf077 | 314 | } |
604dfd58 FB |
315 | |
316 | public function setAccessCookie($replace = false, $log = true) { | |
0c02607e | 317 | if (S::suid() || ($replace && !Cookie::blank('access'))) { |
604dfd58 FB |
318 | return; |
319 | } | |
45dd32db | 320 | Cookie::set('access', sha1(S::user()->password()), 300, true); |
604dfd58 FB |
321 | if ($log) { |
322 | S::logger()->log('cookie_on'); | |
323 | } | |
324 | } | |
325 | ||
326 | public function killAccessCookie($log = true) { | |
327 | Cookie::kill('access'); | |
328 | if ($log) { | |
329 | S::logger()->log('cookie_off'); | |
330 | } | |
331 | } | |
332 | ||
333 | public function killLoginFormCookies() { | |
334 | Cookie::kill('uid'); | |
335 | Cookie::kill('domain'); | |
336 | } | |
0337d704 | 337 | } |
338 | ||
a7de4ef7 | 339 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 340 | ?> |