Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
9f5bd98e | 3 | * Copyright (C) 2003-2010 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 | ||
c0799142 FB |
55 | /** Check the cookie and set the associated user_id in the auth_by_cookie session variable. |
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 | ||
80 | private function checkPassword($uname, $login, $response, $login_type) | |
5480a216 | 81 | { |
c67ba12a FB |
82 | $res = XDB::query('SELECT a.uid, a.password |
83 | FROM accounts AS a | |
84 | INNER JOIN aliases AS l ON (l.id = a.uid AND l.type != \'homonyme\') | |
85 | WHERE l.' . $login_type . ' = {?} AND a.state = \'active\'', | |
c0799142 FB |
86 | $login); |
87 | if (list($uid, $password) = $res->fetchOneRow()) { | |
c67ba12a FB |
88 | $expected_response = sha1("$uname:$password:" . S::v('challenge')); |
89 | /* XXX: Deprecates len(password) > 10 conversion */ | |
c0799142 | 90 | if ($response != $expected_response) { |
86777849 FB |
91 | if (!S::logged()) { |
92 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); | |
93 | } else { | |
94 | Platal::page()->trigError('Mot de passe invalide'); | |
95 | } | |
732e5855 | 96 | S::logger($uid)->log('auth_fail', 'bad password'); |
c0799142 FB |
97 | return null; |
98 | } | |
99 | return $uid; | |
100 | } | |
c6bad0e7 | 101 | Platal::page()->trigError('Mot de passe ou nom d\'utilisateur invalide'); |
c0799142 | 102 | return null; |
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 FB |
133 | if (S::suid()) { |
134 | $login = $uname = S::suid('uid'); | |
e74411f7 | 135 | $redirect = false; |
136 | } else { | |
137 | $uname = Env::v('username'); | |
e74411f7 | 138 | if (Env::v('domain') == "alias") { |
c0799142 FB |
139 | $res = XDB::query('SELECT redirect |
140 | FROM virtual | |
141 | INNER JOIN virtual_redirect USING(vid) | |
142 | WHERE alias LIKE {?}', | |
143 | $uname . '@' . $globals->mail->alias_dom); | |
e74411f7 | 144 | $redirect = $res->fetchOneCell(); |
145 | if ($redirect) { | |
146 | $login = substr($redirect, 0, strpos($redirect, '@')); | |
147 | } else { | |
c0799142 | 148 | $login = ''; |
e74411f7 | 149 | } |
0337d704 | 150 | } else { |
e74411f7 | 151 | $login = $uname; |
152 | $redirect = false; | |
0337d704 | 153 | } |
63528107 | 154 | } |
cab08090 | 155 | |
888465dd | 156 | $uid = $this->checkPassword($uname, $login, Post::v('response'), (!$redirect && is_numeric($uname)) ? 'id' : 'alias'); |
0c02607e FB |
157 | if (!is_null($uid) && S::suid()) { |
158 | if (S::suid('uid') == $uid) { | |
888465dd FB |
159 | $uid = S::i('uid'); |
160 | } else { | |
161 | $uid = null; | |
162 | } | |
163 | } | |
c0799142 FB |
164 | if (!is_null($uid)) { |
165 | S::set('auth', AUTH_MDP); | |
0c02607e | 166 | if (!S::suid()) { |
888465dd FB |
167 | if (Post::has('domain')) { |
168 | if (($domain = Post::v('domain', 'login')) == 'alias') { | |
40565fa2 | 169 | Cookie::set('domain', 'alias', 300); |
888465dd | 170 | } else { |
40565fa2 | 171 | Cookie::kill('domain'); |
888465dd | 172 | } |
e74411f7 | 173 | } |
174 | } | |
c0799142 | 175 | S::kill('challenge'); |
732e5855 | 176 | S::logger($uid)->log('auth_ok'); |
c0799142 | 177 | } |
19fdac5d | 178 | return User::getSilentWithUID($uid); |
c0799142 | 179 | } |
cab08090 | 180 | |
e2cb093d | 181 | protected function startSessionAs($user, $level) |
c0799142 | 182 | { |
b281eb7b | 183 | if ((!is_null(S::user()) && S::user()->id() != $user->id()) |
e2cb093d | 184 | || (S::has('uid') && S::i('uid') != $user->id())) { |
c0799142 FB |
185 | return false; |
186 | } else if (S::has('uid')) { | |
187 | return true; | |
188 | } | |
888465dd FB |
189 | if ($level == AUTH_SUID) { |
190 | S::set('auth', AUTH_MDP); | |
c0799142 | 191 | } |
70232020 | 192 | |
6672b29b | 193 | // Retrieves main user properties. |
4182c6b5 FB |
194 | /** TODO: Move needed informations to account tables */ |
195 | /** TODO: Currently suppressed data are matricule, promo */ | |
e2cb093d | 196 | /** TODO: Use the User object to fetch all this */ |
bfc1eaeb | 197 | $res = XDB::query("SELECT a.uid, a.hruid, a.display_name, a.full_name, |
867b04ef | 198 | a.sex = 'female' AS femme, a.email_format, |
4182c6b5 | 199 | a.token, FIND_IN_SET('watch', a.flags) AS watch_account, |
7e735012 | 200 | UNIX_TIMESTAMP(fp.last_seen) AS banana_last, UNIX_TIMESTAMP(w.last) AS watch_last, |
0511895d | 201 | a.last_version, g.g_account_name IS NOT NULL AS googleapps, |
4182c6b5 | 202 | UNIX_TIMESTAMP(s.start) AS lastlogin, s.host, |
365ba8c3 | 203 | a.is_admin, at.perms |
4182c6b5 | 204 | FROM accounts AS a |
365ba8c3 | 205 | INNER JOIN account_types AS at ON(a.type = at.type) |
0511895d | 206 | INNER JOIN watch AS w ON(w.uid = a.uid) |
7d15f750 | 207 | LEFT JOIN forum_profiles AS fp ON(fp.uid = a.uid) |
4182c6b5 | 208 | LEFT JOIN gapps_accounts AS g ON(a.uid = g.l_userid AND g.g_status = 'active') |
6586a74f FB |
209 | LEFT JOIN log_last_sessions AS ls ON (ls.uid = a.uid) |
210 | LEFT JOIN log_sessions AS s ON(s.id = ls.id) | |
83ff5281 | 211 | WHERE a.uid = {?} AND a.state = 'active'", $user->id()); |
fee8bd20 VZ |
212 | if ($res->numRows() != 1) { |
213 | return false; | |
214 | } | |
215 | ||
c0799142 FB |
216 | $sess = $res->fetchOneAssoc(); |
217 | $perms = $sess['perms']; | |
218 | unset($sess['perms']); | |
6672b29b | 219 | |
6672b29b VZ |
220 | // Loads the data into the real session. |
221 | $_SESSION = array_merge($_SESSION, $sess); | |
222 | ||
223 | // Starts the session's logger, and sets up the permanent cookie. | |
0c02607e FB |
224 | if (S::suid()) { |
225 | S::logger()->log("suid_start", S::v('hruid') . ' by ' . S::suid('hruid')); | |
c0799142 | 226 | } else { |
e2cb093d FB |
227 | S::logger()->saveLastSession(); |
228 | Cookie::set('uid', $user->id(), 300); | |
a90cde48 | 229 | |
e2cb093d FB |
230 | if (S::i('auth_by_cookie') == $user->id() || Post::v('remember', 'false') == 'true') { |
231 | $this->setAccessCookie(false, S::i('auth_by_cookie') != $user->id()); | |
c0799142 | 232 | } else { |
604dfd58 | 233 | $this->killAccessCookie(); |
0337d704 | 234 | } |
63528107 | 235 | } |
b0b937fd | 236 | |
6672b29b | 237 | // Finalizes the session setup. |
365ba8c3 | 238 | $this->makePerms($perms, S::b('is_admin')); |
c0799142 FB |
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 | { | |
8c12f931 | 273 | $res = XDB::query('SELECT a.uid AS user_id, a.hruid |
c67ba12a FB |
274 | FROM aliases AS l |
275 | INNER JOIN accounts AS a ON (l.id = a.uid AND a.state = \'active\') | |
276 | WHERE a.token = {?} AND l.alias = {?} AND l.type != \'homonyme\'', | |
277 | $token, $login); | |
0d44ce42 | 278 | if ($res->numRows() == 1) { |
8c12f931 | 279 | return new User(null, $res->fetchOneAssoc()); |
0d44ce42 FB |
280 | } |
281 | return null; | |
282 | } | |
283 | ||
365ba8c3 | 284 | protected function makePerms($perm, $is_admin) |
bf517daf | 285 | { |
365ba8c3 | 286 | S::set('perms', User::makePerms($perm, $is_admin)); |
bf517daf | 287 | } |
288 | ||
c0799142 FB |
289 | public function setSkin() |
290 | { | |
0c02607e | 291 | if (S::logged() && (!S::has('skin') || S::suid())) { |
c67ba12a FB |
292 | $res = XDB::query('SELECT skin_tpl |
293 | FROM accounts AS a | |
294 | INNER JOIN skins AS s on (a.skin = s.id) | |
295 | WHERE a.uid = {?} AND skin_tpl != \'\'', S::i('uid')); | |
c0799142 | 296 | S::set('skin', $res->fetchOneCell()); |
5480a216 | 297 | } |
298 | } | |
0337d704 | 299 | |
faa06583 FB |
300 | public function loggedLevel() |
301 | { | |
302 | return AUTH_COOKIE; | |
303 | } | |
304 | ||
c0799142 FB |
305 | public function sureLevel() |
306 | { | |
307 | return AUTH_MDP; | |
0337d704 | 308 | } |
ebfdf077 FB |
309 | |
310 | ||
311 | public function updateNbNotifs() | |
312 | { | |
009b8ab7 FB |
313 | require_once 'notifs.inc.php'; |
314 | $user = S::user(); | |
315 | $n = Watch::getCount($user); | |
316 | S::set('notifs', $n); | |
ebfdf077 | 317 | } |
604dfd58 FB |
318 | |
319 | public function setAccessCookie($replace = false, $log = true) { | |
0c02607e | 320 | if (S::suid() || ($replace && !Cookie::blank('access'))) { |
604dfd58 FB |
321 | return; |
322 | } | |
45dd32db | 323 | Cookie::set('access', sha1(S::user()->password()), 300, true); |
604dfd58 FB |
324 | if ($log) { |
325 | S::logger()->log('cookie_on'); | |
326 | } | |
327 | } | |
328 | ||
329 | public function killAccessCookie($log = true) { | |
330 | Cookie::kill('access'); | |
331 | if ($log) { | |
332 | S::logger()->log('cookie_off'); | |
333 | } | |
334 | } | |
335 | ||
336 | public function killLoginFormCookies() { | |
337 | Cookie::kill('uid'); | |
338 | Cookie::kill('domain'); | |
339 | } | |
0337d704 | 340 | } |
341 | ||
a7de4ef7 | 342 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 343 | ?> |